[Libwebsockets] fail uploading file

Andy Green andy at warmcat.com
Sun Jun 27 11:09:21 CEST 2021



On 6/27/21 9:15 AM, Avigail Wilk wrote:
> Hi,
> 
> My code is failing to upload files via http post request. I was able to 
> reproduce it with lws-minimal-http-server-form-post-file.
> 
> I changed lws-minimal-http-server-form-post-file as follows: (in order 
> to make it as similar to my project)

I appreciate you made a reproducer.

> I added ssl and made the post url ("/form1") be served manually with 
> LWSMPRO_CALLBACK.
> 
> These changes made no problems.
> 
> But when I added In LWS_CALLBACK_HTTP_WRITEABLE a write to the socket 
> with lws_write the post request (the upload) on files larger than 4080 
> bytes fails.
> 
> Is there an issue calling lws_write before finishing a post request when 
> getting a callback LWS_CALLBACK_HTTP_WRITEABLE ?
> 
> I'm attaching the code with the changes of 
> lws-minimal-http-server-form-post-file example.

It's better to send a diff or patch, it's easier to see what the changes 
are in a compact way then.

@@ -172,6 +198,21 @@

  		break;

+	case LWS_CALLBACK_HTTP_WRITEABLE:
+	{
+        lwsl_user("in writable callback\n");
+        char tmp[1024];
+        strcpy(tmp , "message");
+
+        int res = lws_write(wsi, (unsigned char *)tmp ,
+                1024, LWS_WRITE_HTTP);
+
+        if (res < 0)
+        {
+            return 1;
+        }
+	}
+	    break;

With lws, getting the WRITEABLE callback is just like being told that 
you "could write".  Sometimes, you will get the callback that you "could 
write" when you did not ask for it.  So you have to figure out in your 
callback code if you "should write", if not, just return 0 / break out.

Otherwise you may be getting this callback, and writing "message" when 
the remote server thinks the next thing it should hear from you is the 
next transaction, and hang up on you.

You can add some logging there and see if that is what is happening.

  	case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
  		/* called when our wsi user_space is going to be destroyed */
  		if (pss->spa) {
@@ -184,7 +225,10 @@
  		break;
  	}

-	return lws_callback_http_dummy(wsi, reason, user, in, len);
+	int tmp = lws_callback_http_dummy(wsi, reason, user, in, len);
+	if(0!=tmp)
+	    lwsl_user("returning from callback with %d\n", tmp);
+	return tmp;
  }

It's not a good idea to use the same symbol name in different scopes 
with different types, eventually it will catch you out with a bug you 
just cannot see by eye.  Some compilers will warn on same symbol in one 
function at different scopes by default because it is very easy to go wrong.

-Andy


More information about the Libwebsockets mailing list