post_server.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. """POST echo endpoint for the compatibility ladder's 036a page.
  3. Run this on the host, then start QEMU with a user-mode NIC:
  4. python3 testdata/browser/compat/post_server.py
  5. qemu-system-i386 ... -netdev user,id=n0 -device ne2k_pci,netdev=n0
  6. 036a posts `alpha=two&beta=on` to http://10.0.2.2:8765/echo and expects the
  7. response page to say PASS. The guest uses QEMU's slirp host address, so the
  8. same script works for `make run-net` (use the host's LAN address instead).
  9. """
  10. from http.server import BaseHTTPRequestHandler, HTTPServer
  11. PORT = 8765
  12. class Handler(BaseHTTPRequestHandler):
  13. def do_POST(self):
  14. length = int(self.headers.get('Content-Length', '0') or 0)
  15. data = self.rfile.read(length).decode('latin-1')
  16. ok = (self.path == '/echo' and data == 'alpha=two&beta=on')
  17. verdict = 'PASS: server received POST' if ok else 'FAIL: body=' + data
  18. body = (
  19. '<!doctype html><html><head><meta charset="utf-8">'
  20. '<title>post result</title>'
  21. '<style>body{font-family:sans-serif;padding:10px}'
  22. 'pre{background:#eeeeee;padding:6px}</style></head><body>'
  23. '<h1>POST result</h1><p id="verdict">' + verdict + '</p>'
  24. '<pre>' + data.replace('&', '&amp;').replace('<', '&lt;') +
  25. '</pre></body></html>'
  26. ).encode('latin-1')
  27. self.send_response(200)
  28. self.send_header('Content-Type', 'text/html')
  29. self.send_header('Content-Length', str(len(body)))
  30. self.end_headers()
  31. self.wfile.write(body)
  32. def do_GET(self):
  33. body = b'POST test endpoint'
  34. self.send_response(200)
  35. self.send_header('Content-Type', 'text/plain')
  36. self.send_header('Content-Length', str(len(body)))
  37. self.end_headers()
  38. self.wfile.write(body)
  39. def log_message(self, fmt, *args):
  40. pass
  41. if __name__ == '__main__':
  42. HTTPServer(('0.0.0.0', PORT), Handler).serve_forever()