Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP fragmentation #3540

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 31 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,6 +137,26 @@ int httpd_get_val(void *e_data, void *data, void *r_data)
return 0;
}

int httpd_merge_data(void *e_data, void *data, void *r_data)
{
str_str_t *kv = (str_str_t*)e_data;
if (kv==NULL) {
LM_ERR("null data\n");
} else {
LM_DBG("data=[%p] [%p][%p] [%.*s]->[%.*s]\n",
kv, kv->key.s, kv->val.s,
kv->key.len, kv->key.s,
kv->val.len, kv->val.s);
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;
}

/**
* Function to print data stored in slinkedl_list list elemnts.
* For debugging purposes only.
Expand Down Expand Up @@ -498,9 +523,12 @@ 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);
kv = slinkedl_peek(pr->p_list);
if (kv)
saved_body = ((str_str_t *)kv)->val;
httpd_receive_buff_pos=0;
slinkedl_traverse(pr->p_list,&httpd_merge_data, NULL, NULL);
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,
method, version,
Expand Down Expand Up @@ -529,11 +557,6 @@ MHD_RET answer_to_connection (void *cls, struct MHD_Connection *connection,
err log printed in getConnectionHeader() */
return MHD_NO;
}
if (*upload_data_size != pr->content_len) {
/* For now, we don't support large POST with truncated data */
LM_ERR("got a truncated POST request\n");
return MHD_NO;
}
LM_DBG("got ContentType [%d] with len [%d]: %.*s\n",
pr->content_type, pr->content_len,
(int)*upload_data_size, upload_data);
Expand Down
Loading