(event: dict[str, Any], context: Any)
| 1097 | port = server.server_address[1] # type: ignore[attr-defined] |
| 1098 | |
| 1099 | def vc_handler(event: dict[str, Any], context: Any) -> dict[str, Any]: |
| 1100 | payload = json.loads(event["body"]) |
| 1101 | path, _ = apply_service_route_prefix_to_target(payload["path"]) |
| 1102 | headers = normalize_event_headers(payload.get("headers", {})) |
| 1103 | method = payload["method"] |
| 1104 | encoding = payload.get("encoding") |
| 1105 | body = payload.get("body") |
| 1106 | |
| 1107 | sc_no_header_leak = bool(headers.get(SC_NO_HEADER_LEAK_HEADER)) |
| 1108 | set_runtime_cache_from_http_headers(headers) |
| 1109 | for sc_header in SC_HEADERS_ALWAYS_STRIP: |
| 1110 | headers.pop(sc_header, None) |
| 1111 | if sc_no_header_leak: |
| 1112 | for sc_header in SC_HEADERS_STRIP_ON_NO_LEAK: |
| 1113 | headers.pop(sc_header, None) |
| 1114 | |
| 1115 | # `_thread.start_new_thread` does not propagate contextvars |
| 1116 | captured_ctx = contextvars.copy_context() |
| 1117 | _thread.start_new_thread( |
| 1118 | captured_ctx.run, |
| 1119 | (server.handle_request,), # type: ignore[attr-defined] |
| 1120 | ) |
| 1121 | clear_runtime_cache_context() |
| 1122 | |
| 1123 | if (body is not None and len(body) > 0) and ( |
| 1124 | encoding is not None and encoding == "base64" |
| 1125 | ): |
| 1126 | body = base64.b64decode(body) |
| 1127 | |
| 1128 | request_body = body.encode("utf-8") if isinstance(body, str) else body |
| 1129 | conn = http.client.HTTPConnection("127.0.0.1", port) |
| 1130 | try: |
| 1131 | conn.request(method, path, headers=headers, body=request_body) |
| 1132 | except (OSError, http.client.HTTPException) as ex: |
| 1133 | _stderr(f"Request Error: {ex}") |
| 1134 | res = conn.getresponse() |
| 1135 | |
| 1136 | return_dict: dict[str, Any] = { |
| 1137 | "statusCode": res.status, |
| 1138 | "headers": format_headers(res.headers), |
| 1139 | } |
| 1140 | |
| 1141 | data = res.read() |
| 1142 | |
| 1143 | try: |
| 1144 | return_dict["body"] = data.decode("utf-8") |
| 1145 | except UnicodeDecodeError: |
| 1146 | return_dict["body"] = base64.b64encode(data).decode("utf-8") |
| 1147 | return_dict["encoding"] = "base64" |
| 1148 | |
| 1149 | return return_dict |
| 1150 | |
| 1151 | else: |
| 1152 | try: |
no test coverage detected