[Libwebsockets] How to get query parameters using the GET method?

Andy Green andy at warmcat.com
Wed Jun 8 11:42:14 CEST 2016



On 06/08/2016 04:10 AM, Andy Green wrote:
>
>
> On June 8, 2016 3:40:17 AM GMT+08:00, Thomas Spitz
> <thomas.spitz at hestia-france.com> wrote:
>> Hello everyone,
>>
>> It might not make sense as POST request are possibly much larger,
>> but is there also an official way to retrieve the query parameters,
>> which were appended in the URI using POST?
>>
>> I have seen that the parameters are in *in egg:
>>
>>> <form action="formtest" method="post"> Some text:<br> <input
>>> type="text" name="Text" value="Give me some text"><br> <input
>>> type="text" name="Text2" value="secondValue"><br> <input
>>> type="submit" value="Send the form"> </form>
>>
>> gives
>>
>>> Form results: 'Text=Give+me+some+text&Text2=secondValue'
>>
>> But then we need to parse it manually
>
> Until recently it has been out of scope for lws.  It handles the GET
> form parameter style with apis as you copied below.
>
> However with the generic-sessions plugin, lws itself does now have
> something nontrivial that parses POST form data.  Right now it's part
> of generic-sessions
>
> https://github.com/warmcat/libwebsockets/blob/master/plugins/protocol_generic_sessions.c#L1459
>
>  but now that works I am moving stuff out of it back into the main
> library (eg the smtp state machine support yesterday).
>
> That code assumes all the form data came in one POST_BODY packet,
> which for his small amount of form data is okay, but generally, POST
> can be used to upload arbitrary files.  So I'll take a closer look at
> that tomorrow.

I added support for that on master.

You need to give -DLWS_WITH_STATEFUL_URLDECODE=1 at cmake to enable it.

It should be clear enough how to use it from the protocol_post_demo.c 
changes to make it use it.

https://github.com/warmcat/libwebsockets/blob/master/plugins/protocol_post_demo.c

Basically there are two layers of it, the lower-level one splits the 
data into x=y units and calls a user-provided callback when it fills an 
output buffer with urldecoded data.  So you can use that to save 
uploaded files to disk with your own callback.

struct lws_urldecode_stateful;

typedef int (*lws_urldecode_stateful_cb)(void *data,
		const char *name, char **buf, int len, int final);

LWS_VISIBLE LWS_EXTERN struct lws_urldecode_stateful *
lws_urldecode_s_create(char *out, int out_len, void *data,
		     lws_urldecode_stateful_cb output);

LWS_VISIBLE LWS_EXTERN int
lws_urldecode_s_process(struct lws_urldecode_stateful *s,
			const char *in, int len);

LWS_VISIBLE LWS_EXTERN int
lws_urldecode_s_destroy(struct lws_urldecode_stateful *s);

There's a second layer which implements a generic callback in lws that 
captures the posted urldecoded data matching an array of name strings, 
and lets you query them by using the parameter name ordinal.  This makes 
it easy to handle normal form entities.

struct lws_urldecode_stateful_param_array;

LWS_VISIBLE LWS_EXTERN struct lws_urldecode_stateful_param_array *
lws_urldecode_spa_create(const char * const *param_names, int count_params,
			 int max_storage);

LWS_VISIBLE LWS_EXTERN int
lws_urldecode_spa_process(struct lws_urldecode_stateful_param_array *ludspa,
			  const char *in, int len);

LWS_VISIBLE LWS_EXTERN int
lws_urldecode_spa_finalize(struct lws_urldecode_stateful_param_array 
*ludspa);

LWS_VISIBLE LWS_EXTERN int
lws_urldecode_spa_get_length(struct lws_urldecode_stateful_param_array 
*ludspa,
			     int n);

LWS_VISIBLE LWS_EXTERN const char *
lws_urldecode_spa_get_string(struct lws_urldecode_stateful_param_array 
*ludspa,
			     int n);

LWS_VISIBLE LWS_EXTERN int
lws_urldecode_spa_destroy(struct lws_urldecode_stateful_param_array 
*ludspa);

-Andy


> -Andy
>
>> Best regards, Thomas
>>
>>
>> 2016-05-20 15:13 GMT+02:00 Roger Schreiter
>> <roger at planinternet.de>:
>>
>>> Am 20.05.2016 15:09, schrieb Andy Green:
>>>> ... /* dump the individual URI Arg parameters */ n = 0; while
>>>> (lws_hdr_copy_fragment(wsi, buf,
>> sizeof(buf),
>>>>
>>> WSI_TOKEN_HTTP_URI_ARGS, n) > 0) {
>>>> lwsl_notice("URI Arg %d: %s\n", ++n,
>> buf);
>>>
>>>
>>> Great!
>>>
>>> Regards, Roger. _______________________________________________
>>> Libwebsockets mailing list Libwebsockets at ml.libwebsockets.org
>>> http://libwebsockets.org/mailman/listinfo/libwebsockets
>>>
>
> _______________________________________________ Libwebsockets mailing
> list Libwebsockets at ml.libwebsockets.org
> http://libwebsockets.org/mailman/listinfo/libwebsockets
>



More information about the Libwebsockets mailing list