[Libwebsockets] On MASTER: file upload is working but only with -DCMAKE_BUILD_TYPE=DEBUG ??

Shmuel Weiss sweiss at kramerel.com
Sat Jun 25 22:56:38 CEST 2016


When I am compiling the lib with -DCMAKE_BUILD_TYPE=DEBUG the file is uploaded properly and everything works fine.
When I omit this flag this is no more working.

An idea why ?

Thanks.

-----Original Message-----
From: Shmuel Weiss 
Sent: Friday, June 24, 2016 18:16
To: 'Andy Green'
Subject: RE: [Libwebsockets] How to handle GET and POST request in the same callback

Hi Andy,

One more trouble now, The whole file sounds to arrive correctly, I am seeing these outputs:
LWS_CALLBACK_HTTP_BODY: len 4096
LWS_CALLBACK_HTTP_BODY: len 4096
LWS_CALLBACK_HTTP_BODY: len 4096
LWS_CALLBACK_HTTP_BODY: len 469
LWS_CALLBACK_HTTP_BODY_COMPLETION
LWS_CALLBACK_HTTP_WRITEABLE: sending 389

But my "file_upload_cb" seems not to be called, and the file is not created.
What I am missing in the concept? I didn’t use PLUGINS, I have just reused your code from the Plugin and incorporated it into my previous code.
I have also sniffed the "testserver" demo in order to see the requests sent, I don’t see something different in my case. 
I have also looked at the server.c in order to understand what the spa_create does, I see parsing of "& =", what this means ? what is the intent there ?


MY upload.html is a s following:

<!DOCTYPE html>
<html>
<body>

<form action="formtest" method="post" enctype="multipart/form-data">
    Select image to Upgrade:
    <input type="text" name="text" value="Give me some text">
    <input type="file" name="file" id="file">
    <input type="submit" value="Install firmware" id="upload" name="upload"> </form>

</body>
</html>


Thanks.
-----Original Message-----
From: Andy Green [mailto:andy at warmcat.com]
Sent: Friday, June 24, 2016 00:32
To: Shmuel Weiss; libwebsockets at ml.libwebsockets.org
Subject: RE: [Libwebsockets] How to handle GET and POST request in the same callback



On June 24, 2016 5:14:50 AM GMT+08:00, Shmuel Weiss <sweiss at kramerel.com> wrote:
>Means I have to use one PSS structure for all my scenarios handled in 
>this protocol ?

Yeah but it's a struct.  So you can put other structs / unions as members if you wanted.

This is a C question not an lws question.

>If I want to cast the PSS ptr depending of the command received, where 
>I can handle the Type?
>How can I merge this  in the same PSS?
>
>struct per_session_data__post_demo {
>	struct lws_spa *spa;
>	char result[LWS_PRE + 500];
>	int result_len;
>
>	char filename[256];
>	long file_length;
>	lws_filefd_type fd;
>};

From your description, you don't need that.  You just need

   unsigned int post_flag:1;

and set it if you're doing the POST.

If you want to nicely do something elaborate as you suggest, you'd use per-case structs and compose them into a union member in the pss, along with an enum saying which union member was active.  So

enum case {
   CASE1,
   CASE2
};

struct mypss_case1 {
...
};

struct mypss_case2 {
...
};

struct per_session_data__post_demo {
...
    enum case case;
    union u {
        struct mypss_case1 c1;
        struct mypss_case2 c2;
    };
...
};

When you face "case 1", set pss->case = CASE1 and initialize pss->u.c1

>And
>
>//this is a structure of per socket, user data.
>struct per_session_data__wsproxy {
>    int socket;                         //holds the udp socket id.
>unsigned int time_prev_keep_alive;    //holds time in seconds (time is
>counted since system boot), at which last keep alive made.
>    lws_filefd_type fd;
>};
>
>I need something like
>
>struct per_session_data_MYSTRUCT {
>	MyTypes type;
>	void *my_pss;
>}
>
>And cast it depending of the type?

No casting of pss (that'd be crap because other members in the pss want to be available the same no matter what case).

Depending on whether it implements case 1 stuff or case2 stuff, your code directly uses pss->u.c1.xxxx or pss->u.c2.yyyy.

-Andy

>case FILE_UPLOAD:
>	struct per_session_data__post_demo *pss =
>			(struct per_session_data__post_demo *)data;
>	blabla....
>	break;
>
>case FILE_DOWNLOAD:
>	struct per_session_data__post_demo *pss =
>			(struct per_session_data__post_demo *)user;
>	blabla....
>	break;
>
>
>thanks
>-----Original Message-----
>From: Andy Green [mailto:andy at warmcat.com]
>Sent: Friday, June 24, 2016 00:04
>To: Shmuel Weiss; libwebsockets at ml.libwebsockets.org
>Subject: Re: [Libwebsockets] How to handle GET and POST request in the 
>same callback
>
>
>
>On June 24, 2016 4:38:28 AM GMT+08:00, Shmuel Weiss 
><sweiss at kramerel.com> wrote:
>>Hi All,
>>
>>I looked at the newest example about the POST DEMO uploading a file
>via
>>HTTP.
>>I have already an HTTP callback which has an implementation for HTTP 
>>GET download files.
>>
>>I would like to understand How I can merge the different behavior 
>>inside the same case:
>>case LWS_CALLBACK_HTTP_WRITEABLE:
>>
>>Sometimes I have to send response for the GET request and sometimes
>for
>>the POST.
>>
>>What is the right approach to handle these scenarios ?
>
>Basically the pss struct is there to hold state that's interesting for 
>that particular wsi.
>
>https://github.com/warmcat/libwebsockets/blob/master/plugins/protocol_p
>ost_demo.c#L34
>
>So generally, if you have to defer doing something until another event, 
>you would have a member in the pss struct that you set, then later you 
>can check what it had been set to, and do the right thing.
>
>That might be a state enum, or just a bit in the pss depending on how 
>many possibilities you need to cover.
>
>The pss is very convenient for this since
>
> - it's typed
>
> - it's something private to your protocol you can change how you like
>
> - it's auto-allocated / freed for each wsi
>
>- the allocation adapts to changes to the struct size without you 
>having to do anything.
>
>-Andy
>
>>thanks
>>
>>Shmulito
>>
>>
>>
>>----------------------------------------------------------------------
>>-
>>-
>>
>>_______________________________________________
>>Libwebsockets mailing list
>>Libwebsockets at ml.libwebsockets.org
>>http://libwebsockets.org/mailman/listinfo/libwebsockets

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20160625/74bc52d9/attachment.html>


More information about the Libwebsockets mailing list