[Libwebsockets] uri-args

Andy Green andy at warmcat.com
Fri Mar 31 00:46:01 CEST 2017



On 03/31/2017 06:41 AM, Joel Winarske wrote:
> Hi Andy,
>
> I'm calling the below in LWS_CALLBACK_HTTP, and am seeing the uri-args 
> value without any instances of "&".
>
> int len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_URI_ARGS);
>
> if (len) {
> vhd->uri_args = malloc(len + 1);
> vhd->uri_args[len] = '\0';
> lws_hdr_copy(wsi, uri_args, len + 1, WSI_TOKEN_HTTP_URI_ARGS);
> lwsl_info("[%s]", uri_args);
> }
>
>
> Is there a recommended way to parse uri-args?

Yes... it's like that from the whole-token view because they have 
already been parsed in-place.

You can use

/**
  * lws_hdr_fragment_length: report length of a single fragment of a header
  *        The returned length does not include the space for a
  *        terminating '\0'
  *
  * \param wsi: websocket connection
  * \param h: which header index we are interested in
  * \param frag_idx: which fragment of h we want to get the length of
  */
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int 
frag_idx);

and

/**
  * lws_hdr_copy_fragment() - copy a single fragment of the given header 
to a buffer
  *        The buffer length len must include space for an additional
  *        terminating '\0', or it will fail returning -1.
  *        If the requested fragment index is not present, it fails
  *        returning -1.
  *
  * \param wsi: websocket connection
  * \param dest: destination buffer
  * \param len: length of destination buffer
  * \param h: which header index we are interested in
  * \param frag_idx: which fragment of h we want to copy
  *
  * Normally this is only useful
  * to parse URI arguments like ?x=1&y=2, token index 
WSI_TOKEN_HTTP_URI_ARGS
  * fragment 0 will contain "x=1" and fragment 1 "y=2"
  */
LWS_VISIBLE LWS_EXTERN int
lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
               enum lws_token_indexes h, int frag_idx);

to get the parsed pieces, or more conveniently for most cases

/**
  * lws_get_urlarg_by_name() - return pointer to arg value if present
  * \param wsi: the connection to check
  * \param name: the arg name, like "token="
  * \param buf: the buffer to receive the urlarg (including the name= part)
  * \param len: the length of the buffer to receive the urlarg
  *
  *     Returns NULL if not found or a pointer inside buf to just after the
  *     name= part.
  */
LWS_VISIBLE LWS_EXTERN const char *
lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int 
len);

-Andy

>
>
> -Joel
>
>
>
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets




More information about the Libwebsockets mailing list