1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-30 00:00:16 +01:00

http: spa: fetch any params

Added the ability to get additional form parameters with unknown names in the form parameters parser lws_spa. The example of using the form parameters parser has been updated. Fixed bug of double freeing memory in the example.
This commit is contained in:
Orefkov Aleksander 2021-10-14 11:06:38 +03:00 committed by Andy Green
parent 43c4b79960
commit abef9ba252
3 changed files with 28 additions and 8 deletions

View file

@ -102,7 +102,8 @@ lws_spa_create(struct lws *wsi, const char * const *param_names,
void *opt_data); void *opt_data);
typedef struct lws_spa_create_info { typedef struct lws_spa_create_info {
const char * const *param_names; /* array of form parameter names, like "username" */ const char * const *param_names; /**< array of form parameter names, like "username"
These may be NULL, to make space for arbitrary POST items */
int count_params; /* count of param_names */ int count_params; /* count of param_names */
int max_storage; /* total amount of form parameter values we can store */ int max_storage; /* total amount of form parameter values we can store */
lws_spa_fileupload_cb opt_cb; /* NULL, or callback to receive file upload data. */ lws_spa_fileupload_cb opt_cb; /* NULL, or callback to receive file upload data. */

View file

@ -1,7 +1,7 @@
/* /*
* libwebsockets - small server side websockets and web server implementation * libwebsockets - small server side websockets and web server implementation
* *
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to * of this software and associated documentation files (the "Software"), to
@ -482,7 +482,23 @@ lws_urldecode_spa_lookup(struct lws_spa *spa, const char *name)
int n; int n;
for (n = 0; n < spa->i.count_params; n++) { for (n = 0; n < spa->i.count_params; n++) {
if (!strcmp(*pp, name)) if (!*pp && !spa->i.param_names_stride && spa->i.ac) {
unsigned int len = (unsigned int)strlen(name);
char **ptr = (char**)spa->i.param_names;
/* Use NULLs at end of list to dynamically create
* unknown entries */
ptr[n] = lwsac_use(spa->i.ac, len + 1, spa->i.ac_chunk_size);
if (!ptr[n])
return -1;
memcpy(ptr[n], name, len);
ptr[n][len] = '\0';
return n;
}
if (*pp && !strcmp(*pp, name))
return n; return n;
if (spa->i.param_names_stride) if (spa->i.param_names_stride)

View file

@ -27,9 +27,13 @@ struct pss {
static int interrupted; static int interrupted;
static const char * const param_names[] = { /* get 2 parameters at predefined pos and up to 3 unknown parameters */
static const char * param_names[] = {
"text1", "text1",
"send", "send",
NULL,
NULL,
NULL
}; };
enum enum_param_names { enum enum_param_names {
@ -98,6 +102,8 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
/* we just dump the decoded things to the log */ /* we just dump the decoded things to the log */
for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) { for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) {
if (!param_names[n])
break;
if (!lws_spa_get_string(pss->spa, n)) if (!lws_spa_get_string(pss->spa, n))
lwsl_user("%s: undefined\n", param_names[n]); lwsl_user("%s: undefined\n", param_names[n]);
else else
@ -107,8 +113,6 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
lws_spa_get_string(pss->spa, n)); lws_spa_get_string(pss->spa, n));
} }
lwsac_free(&pss->ac);
/* /*
* Our response is to redirect to a static page. We could * Our response is to redirect to a static page. We could
* have generated a dynamic html page here instead. * have generated a dynamic html page here instead.
@ -123,10 +127,9 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
case LWS_CALLBACK_HTTP_DROP_PROTOCOL: case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
/* called when our wsi user_space is going to be destroyed */ /* called when our wsi user_space is going to be destroyed */
if (pss->spa) { if (pss->spa) {
lws_spa_destroy(pss->spa); lws_spa_destroy(pss->spa); /* lwsac_free called this */
pss->spa = NULL; pss->spa = NULL;
} }
lwsac_free(&pss->ac);
break; break;
default: default: