-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp.py
236 lines (199 loc) · 8 KB
/
http.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""Integration of Dispatch functions with http."""
import logging
import os
from datetime import timedelta
from http.server import BaseHTTPRequestHandler
from typing import Mapping, Optional, Union
from http_message_signatures import InvalidSignature
from dispatch.asyncio import Runner
from dispatch.function import Registry
from dispatch.proto import Input
from dispatch.sdk.v1 import function_pb2 as function_pb
from dispatch.signature import (
CaseInsensitiveDict,
Ed25519PublicKey,
Request,
parse_verification_key,
verify_request,
)
from dispatch.status import Status
logger = logging.getLogger(__name__)
class Dispatch:
"""A Dispatch instance to be serviced by a http server. The Dispatch class
acts as a factory for DispatchHandler objects, by capturing the variables
that would be shared between all DispatchHandler instances it created."""
def __init__(
self,
registry: Registry,
verification_key: Optional[Union[Ed25519PublicKey, str, bytes]] = None,
):
"""Initialize a Dispatch http handler.
Args:
registry: The registry of functions to be serviced.
"""
self.registry = registry
self.verification_key = parse_verification_key(verification_key)
def __call__(self, request, client_address, server):
return FunctionService(
request,
client_address,
server,
registry=self.registry,
verification_key=self.verification_key,
)
class FunctionServiceError(Exception):
__slots__ = ("status", "code", "message")
def __init__(self, status, code, message):
self.status = status
self.code = code
self.message = message
class FunctionService(BaseHTTPRequestHandler):
def __init__(
self,
request,
client_address,
server,
registry: Registry,
verification_key: Optional[Ed25519PublicKey] = None,
):
self.registry = registry
self.verification_key = verification_key
self.error_content_type = "application/json"
super().__init__(request, client_address, server)
def send_error_response_invalid_argument(self, message: str):
self.send_error_response(400, "invalid_argument", message)
def send_error_response_not_found(self, message: str):
self.send_error_response(404, "not_found", message)
def send_error_response_unauthenticated(self, message: str):
self.send_error_response(401, "unauthenticated", message)
def send_error_response_permission_denied(self, message: str):
self.send_error_response(403, "permission_denied", message)
def send_error_response_internal(self, message: str):
self.send_error_response(500, "internal", message)
def send_error_response(self, status: int, code: str, message: str):
body = f'{{"code":"{code}","message":"{message}"}}'.encode()
self.send_response(status)
self.send_header("Content-Type", self.error_content_type)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_POST(self):
if self.path != "/dispatch.sdk.v1.FunctionService/Run":
self.send_error_response_not_found("path not found")
return
content_length = int(self.headers.get("Content-Length", 0))
if content_length == 0:
self.send_error_response_invalid_argument("content length is required")
return
if content_length < 0:
self.send_error_response_invalid_argument("content length is negative")
return
if content_length > 16_000_000:
self.send_error_response_invalid_argument("content length is too large")
return
data: bytes = self.rfile.read(content_length)
method = "POST"
url = self.requestline # TODO: need full URL
try:
with Runner() as runner:
content = runner.run(
function_service_run(
url,
method,
dict(self.headers),
data,
self.registry,
self.verification_key,
)
)
except FunctionServiceError as e:
return self.send_error_response(e.status, e.code, e.message)
self.send_response(200)
self.send_header("Content-Type", "application/proto")
self.end_headers()
self.wfile.write(content)
async def function_service_run(
url: str,
method: str,
headers: Mapping[str, str],
data: bytes,
function_registry: Registry,
verification_key: Optional[Ed25519PublicKey],
) -> bytes:
logger.debug("handling run request with %d byte body", len(data))
if verification_key is None:
logger.debug("skipping request signature verification")
else:
signed_request = Request(
method=method,
url=url,
headers=CaseInsensitiveDict(headers),
body=data,
)
max_age = timedelta(minutes=5)
try:
verify_request(signed_request, verification_key, max_age)
except ValueError as e:
raise FunctionServiceError(401, "unauthenticated", str(e))
except InvalidSignature as e:
# The http_message_signatures package sometimes wraps does not
# attach a message to the exception, so we set a default to
# have some context about the reason for the error.
message = str(e) or "invalid signature"
raise FunctionServiceError(403, "permission_denied", message)
req = function_pb.RunRequest.FromString(data)
if not req.function:
raise FunctionServiceError(400, "invalid_argument", "function is required")
try:
func = function_registry.functions[req.function]
except KeyError:
logger.debug("function '%s' not found", req.function)
raise FunctionServiceError(
404, "not_found", f"function '{req.function}' does not exist"
)
input = Input(req)
logger.info("running function '%s'", req.function)
try:
output = await func._primitive_call(input)
except Exception:
# This indicates that an exception was raised in a primitive
# function. Primitive functions must catch exceptions, categorize
# them in order to derive a Status, and then return a RunResponse
# that carries the Status and the error details. A failure to do
# so indicates a problem, and we return a 500 rather than attempt
# to catch and categorize the error here.
logger.error("function '%s' fatal error", req.function, exc_info=True)
raise FunctionServiceError(
500, "internal", f"function '{req.function}' fatal error"
)
response = output._message
status = Status(response.status)
if response.HasField("poll"):
logger.debug(
"function '%s' polling with %d call(s)",
req.function,
len(response.poll.calls),
)
elif response.HasField("exit"):
exit = response.exit
if not exit.HasField("result"):
logger.debug("function '%s' exiting with no result", req.function)
else:
result = exit.result
if result.HasField("output"):
logger.debug("function '%s' exiting with output value", req.function)
elif result.HasField("error"):
err = result.error
logger.debug(
"function '%s' exiting with error: %s (%s)",
req.function,
err.message,
err.type,
)
if exit.HasField("tail_call"):
logger.debug(
"function '%s' tail calling function '%s'",
exit.tail_call.function,
)
logger.debug("finished handling run request with status %s", status.name)
return response.SerializeToString()