1
1
"""Integration of Dispatch functions with http."""
2
2
3
+ from datetime import datetime
4
+
3
5
import logging
4
6
import os
5
7
from datetime import timedelta
@@ -61,10 +63,12 @@ def __init__(
61
63
registry : Registry ,
62
64
verification_key : Optional [Ed25519PublicKey ] = None ,
63
65
):
64
- super ().__init__ (request , client_address , server )
65
66
self .registry = registry
66
67
self .verification_key = verification_key
67
68
self .error_content_type = "application/json"
69
+ print (datetime .now (), "INITIALIZING FUNCTION SERVICE" )
70
+ super ().__init__ (request , client_address , server )
71
+ print (datetime .now (), "DONE HANDLING REQUEST" )
68
72
69
73
def send_error_response_invalid_argument (self , message : str ):
70
74
self .send_error_response (400 , "invalid_argument" , message )
@@ -82,17 +86,33 @@ def send_error_response_internal(self, message: str):
82
86
self .send_error_response (500 , "internal" , message )
83
87
84
88
def send_error_response (self , status : int , code : str , message : str ):
89
+ body = f'{{"code":"{ code } ","message":"{ message } "}}' .encode ()
85
90
self .send_response (status )
86
91
self .send_header ("Content-Type" , self .error_content_type )
92
+ self .send_header ("Content-Length" , str (len (body )))
87
93
self .end_headers ()
88
- self .wfile .write (f'{{"code":"{ code } ","message":"{ message } "}}' .encode ())
94
+ print (datetime .now (), "SENDING ERROR RESPONSE" )
95
+ self .wfile .write (body )
96
+ print (datetime .now (), f"SERVER IS DONE { len (body )} " )
89
97
90
98
def do_POST (self ):
91
99
if self .path != "/dispatch.sdk.v1.FunctionService/Run" :
92
100
self .send_error_response_not_found ("path not found" )
93
101
return
94
102
95
- data : bytes = self .rfile .read ()
103
+ content_length = int (self .headers .get ("Content-Length" , 0 ))
104
+ if content_length == 0 :
105
+ self .send_error_response_invalid_argument ("content length is required" )
106
+ return
107
+ if content_length < 0 :
108
+ self .send_error_response_invalid_argument ("content length is negative" )
109
+ return
110
+ if content_length > 16_000_000 :
111
+ self .send_error_response_invalid_argument ("content length is too large" )
112
+ return
113
+
114
+ data : bytes = self .rfile .read (content_length )
115
+ print (datetime .now (), f"RECEIVED POST REQUEST: { self .path } { len (data )} { self .request_version } { self .headers } " )
96
116
logger .debug ("handling run request with %d byte body" , len (data ))
97
117
98
118
if self .verification_key is not None :
@@ -130,7 +150,7 @@ def do_POST(self):
130
150
)
131
151
return
132
152
133
- logger . info ( "running function '%s'" , req .function )
153
+ print ( datetime . now (), "running function '%s'" , req .function )
134
154
try :
135
155
output = func ._primitive_call (Input (req ))
136
156
except Exception :
0 commit comments