server.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. import os, re, hmac
  2. from wsgiref.simple_server import make_server
  3. KEY_AUTHORIZATION = {"uri": "/not_set", "data": ""}
  4. TRAVIS_SESSION = os.getenv("TRAVIS_SESSION", "not_yet_set")
  5. def app(req, resp):
  6. if req['REQUEST_METHOD'] == "POST":
  7. if hmac.compare_digest(req['QUERY_STRING'], TRAVIS_SESSION):
  8. body_len = min(int(req['CONTENT_LENGTH']), 90)
  9. body = req['wsgi.input'].read(body_len).decode("utf8")
  10. body = re.sub(r"[^A-Za-z0-9_\-\.]", "_", body)
  11. KEY_AUTHORIZATION['uri'] = "/{0}".format(body.split(".", 1)[0])
  12. KEY_AUTHORIZATION['data'] = body
  13. resp('201 Created', [])
  14. return ["".encode("utf8")]
  15. else:
  16. resp("403 Forbidden", [])
  17. return ["403 Forbidden".encode("utf8")]
  18. else:
  19. if hmac.compare_digest(req['PATH_INFO'], KEY_AUTHORIZATION['uri']):
  20. resp('200 OK', [])
  21. return [KEY_AUTHORIZATION['data'].encode("utf8")]
  22. else:
  23. resp("404 Not Found", [])
  24. return ["404 Not Found".encode("utf8")]
  25. make_server("localhost", 8888, app).serve_forever()