summaryrefslogtreecommitdiff
path: root/echo.yaml
blob: 7f511ebadc751f0d97254aac412c735fdd1e9f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
apiVersion: v1
kind: Service
metadata:
  name: echo
spec:
  selector:
    app: echo
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: http
---
apiVersion: v1
kind: Pod
metadata:
  name: echo
  labels:
    app: echo
spec:
  containers:
    - name: echo-container
      image: python:3.9
      ports:
      - name: http
        containerPort: 8000
      env:
      - name: PYTHONUNBUFFERED
        value: "1"
      command: ["python", "-c"]
      args:
      - |
          import http.server
          import json

          class Handler(http.server.SimpleHTTPRequestHandler):
              def _headers_dict(self):
                  # Convert headers (email.message.Message) to a plain dict
                  # If a header appears multiple times, join values with ", "
                  out = {}
                  for k, v in self.headers.items():
                      if k in out:
                          out[k] = f"{out[k]}, {v}"
                      else:
                          out[k] = v
                  return out

              def _send_json(self, obj, status=http.server.HTTPStatus.OK):
                  payload = json.dumps(obj).encode("utf-8")
                  self.send_response(status)
                  self.send_header("Content-Type", "application/json; charset=utf-8")
                  self.send_header("Content-Length", str(len(payload)))
                  self.end_headers()
                  self.wfile.write(payload)

              def do_GET(self):
                  self._send_json({
                      "method": self.command,
                      "path": self.path,
                      "request_version": self.request_version,
                      "client": {"ip": self.client_address[0], "port": self.client_address[1]},
                      "headers": self._headers_dict(),
                  })

              def do_POST(self):
                  length = int(self.headers.get("Content-Length", "0"))
                  body_bytes = self.rfile.read(length)

                  # Try to decode as UTF-8 for convenience; fall back to base64-ish representation
                  try:
                      body_text = body_bytes.decode("utf-8")
                      body = {"raw": body_text}
                  except UnicodeDecodeError:
                      body = {"raw_bytes": list(body_bytes)}  # simple/portable, not efficient

                  self._send_json({
                      "method": self.command,
                      "path": self.path,
                      "request_version": self.request_version,
                      "client": {"ip": self.client_address[0], "port": self.client_address[1]},
                      "headers": self._headers_dict(),
                      "body": body,
                  })


          def run(
              server_class=http.server.HTTPServer,
              handler_class=http.server.BaseHTTPRequestHandler,
          ):
              server_address = ("", 8000)
              httpd = server_class(server_address, handler_class)
              httpd.serve_forever()


          if __name__ == "__main__":
              print("start")
              run(handler_class=Handler)