patx/mrhttp-asgi

Fixes seg fault during uploading for issue 26

Commit 9097ecb · Mark · 2024-07-18T09:10:42-07:00

Changeset
9097ecbe075fd36dad432f9bbd7790196ce02deb
Parents
fb0870254016dae2d224915f634448c395914e0d

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/examples/14_upload.py b/examples/14_upload.py
index c707f19..567d113 100644
--- a/examples/14_upload.py
+++ b/examples/14_upload.py
@@ -3,16 +3,17 @@ from mrhttp import app
 
 @app.route('/')
 def hello(r):
+  print(r.headers)
   if r.file == None:
     return "No file uploaded"
-  #for f in r.files:
-    #print(f)
+  for f in r.files:
+    print(f['name'])
   name = r.file['name']
   typ  = r.file['type']
   body = r.file['body']
   return name
 
-app.run(cores=4)
+app.run(cores=1)
 
 # curl -i -X POST -F "data=@14_upload.py" http://localhost:8080/
 
diff --git a/readme b/readme
index 085e726..4ad7e37 100644
--- a/readme
+++ b/readme
@@ -1,4 +1,19 @@
 
+
+Fix up the static cache
+
+-app.config["memcache"] = [ ("127.0.0.1", 11211) ]
+-#app.static_cached("www","/path/to/www")
+-
+-#@app.on('at_start')
+-#async def setup():
+-  #app.c = asyncmrq.Client()
+-  #await app.c.connect(servers=[("127.0.0.1",7100)])
+-
[email protected]('/',_type='text',options=['cache'])
+
+
+
 Updating version:
   setup.py
   src/mrhttp/__init__.py
@@ -36,7 +51,7 @@ attach to child process
 
 Memory issues:  TODO AddressSanitizer false positives on the ranges* see if I can ignore
 CFLAGS='-Wall -O0 -g -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address' python setup.py install --force
-ASAN_OPTIONS=halt_on_error=0 LD_PRELOAD=/usr/lib/gcc/x86_64-linux-gnu/7/libasan.so python tst.py
+ASAN_OPTIONS=halt_on_error=0 LD_PRELOAD=/usr/lib/gcc/x86_64-linux-gnu/13/libasan.so python tst.py
 
 
 Telnet and send partial / bad requests
diff --git a/src/mrhttp/internals/request.c b/src/mrhttp/internals/request.c
index 7fb69d3..2a05100 100644
--- a/src/mrhttp/internals/request.c
+++ b/src/mrhttp/internals/request.c
@@ -671,7 +671,7 @@ PyObject* Request_parse_mp_form(Request* self) {
   static char colon[] = "::" ";;" "==" "\015\015";
   static char semi[] = ";;" "=="; 
     
-  //printf("ct >%.*s<\n", ctlen, ct);
+  DBG printf("ct >%.*s<\n", ctlen, ct);
 
 /*
   char *p = ct;
@@ -679,7 +679,6 @@ PyObject* Request_parse_mp_form(Request* self) {
   char *bnd = NULL;
   int bndlen;
   p = findchar(p, pend, semi, sizeof(semi) - 1, &found);
-  //printf("find >%.*s<\n", 5, p);
   if ( p[2] == 'b' ) {
     bnd = p + 11;
     bndlen = pend-bnd;
@@ -701,6 +700,7 @@ PyObject* Request_parse_mp_form(Request* self) {
     PyTuple_SetItem(ret, 1, Py_None);
     return ret;
   }
+  DBG printf("bnd >%.*s<\n", bndlen, bnd);
 
   int i = 0;
   p = self->body;
@@ -718,6 +718,7 @@ PyObject* Request_parse_mp_form(Request* self) {
 //Content-Disposition: form-data; name="fieldName"
 //Content-Disposition: form-data; name="fieldName"; filename="filename.jpg"
 
+
   // We're looping line by line until end of body
   // TODO add a findBoundary function and just search on '-' instead of line by line
   //      then add a get headers for parsing the lines after the boundary
@@ -786,7 +787,6 @@ PyObject* Request_parse_mp_form(Request* self) {
     //Content-Type: text/plain; charset=utf-8
     else if ( state == 1 ) {
       if ( p[0] == '\r' ) {
-        //printf("end of state 1\n");
         state = 0;
         body = p+2;
       }
@@ -837,10 +837,7 @@ PyObject* Request_parse_mp_form(Request* self) {
       }    
     }
     
-    //if ( state == 2 ) {
-    //}
-    //p = findchar(p, pend, crlf, sizeof(crlf) - 1, &found);
-    p = my_get_eol(p, pend);
+    p = findchar(p, pend, crlf, sizeof(crlf) - 1, &found);
     p += 2;
   }
 
diff --git a/tests/up.py b/tests/up.py
new file mode 100644
index 0000000..c8e00be
--- /dev/null
+++ b/tests/up.py
@@ -0,0 +1,5 @@
+import requests
+
+with open('delme', 'r') as f:
+    files = {"data": f}
+    requests.post(f'http://localhost:8080/', files=files)
diff --git a/tst.py b/tst.py
index 5cb5746..316ec00 100755
--- a/tst.py
+++ b/tst.py
@@ -3,17 +3,7 @@ import traceback, mrjson
 from mrhttp import app
 import mrpacker
 
-
-app.config["memcache"] = [ ("127.0.0.1", 11211) ]
-#app.static_cached("www","/path/to/www")
-
-#@app.on('at_start')
-#async def setup():
-  #app.c = asyncmrq.Client()
-  #await app.c.connect(servers=[("127.0.0.1",7100)])
-
[email protected]('/',_type='text',options=['cache'])
-#@app.route('/',_type='text')
[email protected]('/',_type='text')
 def index(r):
   return "hello world"  
 
@@ -21,11 +11,19 @@ def index(r):
 def json(r):
   return mrjson.dumps({'message': 'Hello, world!'})
 
[email protected]('/upload')
+def upload(r):
+  try:
+    print(r.headers)
+    print(r.file)
+  except:
+    print("ERROR")
+  return "Success"
 
 
 try:
   app.run(cores=1)
 except Exception as e:
-  print("YAY",e)
+  print("Done",e)