@@ -70,14 +70,38 @@ def add_function_route(self, route, function):
7070 async def serve_request (self , reader , writer ):
7171 try :
7272 url = ""
73- request = await reader .read (1024 )
73+ method = ""
74+ content_length = 0
75+ # Read the request line by line because we want the post data potentially
76+ headers = []
77+ while True :
78+ line = await reader .readline ()
79+ line = line .decode ('utf-8' ).strip ()
80+ if line == "" :
81+ break
82+ headers .append (line )
83+
84+ request = "\r " .join (headers )
7485 print (request )
7586 request = str (request )
76- url_pattern = re .compile (r"GET\s+([^\s]+)\s+HTTP" )
77- match = url_pattern .search (request )
87+ request_pattern = re .compile (r"( GET|POST) \s+([^\s]+)\s+HTTP" )
88+ match = request_pattern .search (request )
7889 if match :
79- url = match .group (1 )
80- print (url )
90+ method = match .group (1 )
91+ url = match .group (2 )
92+ print (method , url )
93+ # extract content length for POST requests
94+ if method == "POST" :
95+ content_length_pattern = re .compile (r"Content-Length:\s+(\d+)" )
96+ match = content_length_pattern .search (request )
97+ if match :
98+ content_length = int (match .group (1 ))
99+ print ("content_length: " + str (content_length ))
100+ # Read the POST data if there's any
101+ post_data = None
102+ if content_length > 0 :
103+ post_data = await reader .readexactly (content_length )
104+ print ("POST data:" , post_data )
81105 # check if the url is a function route and if so run the function
82106 path_components = self .get_path_components (url )
83107 print ("path_components: " + str (path_components ))
0 commit comments