Skip to content

Commit

Permalink
Add receive_buf_size param
Browse files Browse the repository at this point in the history
  • Loading branch information
vladpaiu committed Dec 12, 2024
1 parent 8db36e1 commit 15d433d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
19 changes: 19 additions & 0 deletions modules/httpd/doc/httpd_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,25 @@ modparam("httpd", "buf_size", 524288)
...
modparam("httpd", "post_buf_size", 4096)
...
</programlisting>
</example>
</section>

<section id="param_receive_buf_size" xreflabel="receive_buf_size">
<title><varname>receive_buf_size</varname> (integer)</title>
<para>
It specifies the maximum length (in bytes) of the received HTTP requests.
For receiving large POST request, the default value might require to be increased.
</para>
<para>
<emphasis> The default value is 1024.</emphasis>
</para>
<example>
<title>Set <varname>receive_buf_size</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("httpd", "receive_buf_size", 4096)
...
</programlisting>
</example>
</section>
Expand Down
10 changes: 10 additions & 0 deletions modules/httpd/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ str tls_cert_file = {NULL, 0};
str tls_key_file = {NULL, 0};
str tls_ciphers = {"SECURE256:+SECURE192:-VERS-ALL:+VERS-TLS1.2", 45};
int post_buf_size = DEFAULT_POST_BUF_SIZE;
int receive_buf_size = DEFAULT_POST_BUF_SIZE;
struct httpd_cb *httpd_cb_list = NULL;

char *httpd_receive_buff = NULL;
int httpd_receive_buff_pos=0;

static const proc_export_t mi_procs[] = {
{"HTTPD", 0, 0, httpd_proc, 1,
Expand All @@ -81,6 +84,7 @@ static const param_export_t params[] = {
{"buf_size", INT_PARAM, &buffer.len},
{"conn_timeout", INT_PARAM, &hd_conn_timeout_s},
{"post_buf_size", INT_PARAM, &post_buf_size},
{"receive_buf_size", INT_PARAM, &receive_buf_size},
{"tls_cert_file", STR_PARAM, &tls_cert_file.s},
{"tls_key_file", STR_PARAM, &tls_key_file.s},
{"tls_ciphers", STR_PARAM, &tls_ciphers.s},
Expand Down Expand Up @@ -185,6 +189,12 @@ static int mod_init(void)
buffer.len = (pkg_mem_size/4);
LM_DBG("buf_size=[%d]\n", buffer.len);

httpd_receive_buff = pkg_malloc(receive_buf_size);
if (httpd_receive_buff == NULL) {
LM_ERR("No more pkg\n");
return -1;
}

return 0;
}

Expand Down
23 changes: 15 additions & 8 deletions modules/httpd/httpd_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ extern str tls_ciphers;
extern struct httpd_cb *httpd_cb_list;
static union sockaddr_union httpd_server_info;


extern int receive_buf_size;
extern char *httpd_receive_buff;
extern int httpd_receive_buff_pos;

static const str MI_HTTP_U_URL = str_init("<html><body>"
"Unable to parse URL!</body></html>");
static const str MI_HTTP_U_METHOD = str_init("<html><body>"
Expand Down Expand Up @@ -132,8 +137,6 @@ int httpd_get_val(void *e_data, void *data, void *r_data)
return 0;
}

static char httpd_full_buff[16384];
static int httpd_full_buff_pos=0;
int httpd_merge_data(void *e_data, void *data, void *r_data)
{
str_str_t *kv = (str_str_t*)e_data;
Expand All @@ -144,8 +147,12 @@ int httpd_merge_data(void *e_data, void *data, void *r_data)
kv, kv->key.s, kv->val.s,
kv->key.len, kv->key.s,
kv->val.len, kv->val.s);
memcpy(httpd_full_buff+httpd_full_buff_pos,kv->val.s,kv->val.len);
httpd_full_buff_pos+=kv->val.len;
if (httpd_receive_buff_pos + kv->val.len > receive_buf_size) {
LM_ERR("Received too big HTTP request ( %d bytes ), increase receive_buf_size param value\n", httpd_receive_buff_pos + kv->val.len);
} else {
memcpy(httpd_receive_buff+httpd_receive_buff_pos,kv->val.s,kv->val.len);
httpd_receive_buff_pos+=kv->val.len;
}
}
return 0;
}
Expand Down Expand Up @@ -516,11 +523,11 @@ MHD_RET answer_to_connection (void *cls, struct MHD_Connection *connection,
if (cb) {
normalised_url = &url[cb->http_root->len+1];
LM_DBG("normalised_url=[%s]\n", normalised_url);
httpd_full_buff_pos=0;
httpd_receive_buff_pos=0;
slinkedl_traverse(pr->p_list,&httpd_merge_data, NULL, NULL);
if (httpd_full_buff_pos>0) {
saved_body.len = httpd_full_buff_pos;
saved_body.s = httpd_full_buff;
if (httpd_receive_buff_pos>0) {
saved_body.len = httpd_receive_buff_pos;
saved_body.s = httpd_receive_buff;
}
ret_code = cb->callback(cls, (void*)connection,
normalised_url,
Expand Down

0 comments on commit 15d433d

Please sign in to comment.