66import ujson as json
77from response import Response
88from request import Request
9+ import gc
10+ import os
911
1012
1113class GurgleAppsWebserver :
@@ -25,7 +27,6 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
2527 self .wlan .active (True )
2628 # connect to the access point with the ssid and password
2729 self .wlan .connect (self .wifi_ssid , self .wifi_password )
28-
2930 self .html = """<!DOCTYPE html>
3031 <html>
3132 <head> <title>GurgleApps.com Webserver</title> </head>
@@ -78,6 +79,7 @@ def add_function_route(self, route, function):
7879 self .function_routes .append ({"route" : route , "function" : function })
7980
8081 async def serve_request (self , reader , writer ):
82+ gc .collect ()
8183 try :
8284 url = ""
8385 method = ""
@@ -87,18 +89,27 @@ async def serve_request(self, reader, writer):
8789 post_data = None
8890 while True :
8991 line = await reader .readline ()
92+ #print("line: "+str(line))
9093 line = line .decode ('utf-8' ).strip ()
9194 if line == "" :
9295 break
9396 headers .append (line )
94- request_raw = str ("\r " .join (headers ))
97+ request_raw = str ("\r \n " .join (headers ))
9598 print (request_raw )
9699 request_pattern = re .compile (r"(GET|POST)\s+([^\s]+)\s+HTTP" )
97100 match = request_pattern .search (request_raw )
98101 if match :
99102 method = match .group (1 )
100103 url = match .group (2 )
101104 print (method , url )
105+ else : # regex didn't match, try splitting the request line
106+ request_parts = request_raw .split (" " )
107+ if len (request_parts ) > 1 :
108+ method = request_parts [0 ]
109+ url = request_parts [1 ]
110+ print (method , url )
111+ else :
112+ print ("no match" )
102113 # extract content length for POST requests
103114 if method == "POST" :
104115 content_length_pattern = re .compile (r"Content-Length:\s+(\d+)" )
@@ -123,15 +134,14 @@ async def serve_request(self, reader, writer):
123134 await route_function (request , response , * params )
124135 return
125136 # perhaps it is a file
126- file = self .get_file (self .doc_root + url )
127- if self .log_level > 2 :
128- print ("file: " + str (file ))
129- if file :
130- print ("file found so serving it" )
137+ file_path = self .doc_root + url
138+ if self .log_level > 0 :
139+ print ("file_path: " + str (file_path ))
140+ if uos .stat (file_path )[6 ] > 0 :
131141 content_type = self .get_content_type (url )
132142 if self .log_level > 1 :
133143 print ("content_type: " + str (content_type ))
134- await response .send ( file , 200 , content_type )
144+ await response .send_file ( file_path , content_type = content_type )
135145 return
136146 print ("file not found" )
137147 await response .send (self .html % "page not found " + url , status_code = 404 )
@@ -158,17 +168,21 @@ def get_file(self, filename):
158168 return False
159169
160170 def get_path_components (self , path ):
171+ print ("get_path_components: " + path )
161172 return tuple (filter (None , path .split ('/' )))
162173
163174 def match_route (self , path_components ):
164175 for route in self .function_routes :
165176 route_pattern = list (filter (None , route ["route" ].split ("/" )))
166- # print("route_pattern: "+str(route_pattern))
177+ if self .log_level > 1 :
178+ print ("route_pattern: " + str (route_pattern ))
167179 if len (route_pattern ) != len (path_components ):
168180 continue
169181 match = True
170182 params = []
171183 for idx , pattern_component in enumerate (route_pattern ):
184+ if self .log_level > 2 :
185+ print ("pattern_component: " + str (pattern_component ))
172186 if pattern_component .startswith ('<' ) and pattern_component .endswith ('>' ):
173187 param_value = path_components [idx ]
174188 params .append (param_value )
0 commit comments