[Libwebsockets] [EXTERNAL] Re: fail uploading file

Avigail Wilk awilk at kramerav.com
Tue Jun 29 17:16:26 CEST 2021


Hi,

I added handling to the WRITEABLE callback, in order to avoid lws_write to be called at the wrong time and it solved the problem.

Thanks so much for your help!!!

-----Original Message-----
From: Andy Green <andy at warmcat.com> 
Sent: Sunday, June 27, 2021 12:09 PM
To: Avigail Wilk <awilk at kramerav.com>; libwebsockets at ml.libwebsockets.org
Subject: [EXTERNAL] Re: fail uploading file



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
CAUTION:This email originated from outside of the organization.
Do not click links or open attachments unless you recognize the sender and know the content is safe.



This message has been scanned for malware by Websense. www.websense.com


More information about the Libwebsockets mailing list