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

Andy Green andy at warmcat.com
Fri May 20 15:09:11 CEST 2016

On May 20, 2016 8:54:47 PM GMT+08:00, Roger Schreiter <roger at planinternet.de> wrote:
>a client (web browser) is requesting a web page from a server.
>The server is running libwebsockets.
>The URI contains query parameters in the form:
>However using libwebsockets, I was unable to get the query parameters.
>I do get the scriptname "file" in the variable "in" when getting
>the callback with reason LWS_CALLBACK_HTTP.
>I assume, the query parameters are still at their place, but lws did
>replace the separating ? by a \0.

Sort of.  Actually lws does a lot of work sanitizing and urldecoding the params, and making them available logically separately.


/* 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);

So fragment 0 of WSI_TOKEN_HTTP_URI_ARGS will contain "param1=value1", fragment 1 "param2=value2", and fragment 2 would fail with -1 return.

%20 urlencoding decode is already done.  Illegal sequences like %JK are already rejected with a 4xx HTTP response.

There's also a lot of cleaning and protection done on the url path before you get it, eliminating ../ behind the start and other illegal forms.


>Is there a official way to retrieve the query parameters, which were
>appended in the URI (using GET)?
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org

More information about the Libwebsockets mailing list