Module: kamailio Branch: 5.8 Commit: 89b1f907da5428de79670df28d0fd7135a8379ff URL: https://github.com/kamailio/kamailio/commit/89b1f907da5428de79670df28d0fd7135a8379ff
Author: Daniel-Constantin Mierla <mico...@gmail.com> Committer: Daniel-Constantin Mierla <mico...@gmail.com> Date: 2025-03-20T07:31:09+01:00 microhttpd: handle all incoming body data - GH #4176 (cherry picked from commit 96392eda8fac1cb075e7f407c0155085acc92c5f) (cherry picked from commit f24315c2bc4ec7b9507516a96603e91ae2ce0fd1) --- Modified: src/modules/microhttpd/microhttpd_mod.c --- Diff: https://github.com/kamailio/kamailio/commit/89b1f907da5428de79670df28d0fd7135a8379ff.diff Patch: https://github.com/kamailio/kamailio/commit/89b1f907da5428de79670df28d0fd7135a8379ff.patch --- diff --git a/src/modules/microhttpd/microhttpd_mod.c b/src/modules/microhttpd/microhttpd_mod.c index 1bf684d01a7..3bfef79b038 100644 --- a/src/modules/microhttpd/microhttpd_mod.c +++ b/src/modules/microhttpd/microhttpd_mod.c @@ -176,6 +176,13 @@ static void mod_destroy(void) { } +typedef struct ksr_mhd_cstream +{ + int ctype; + int rcvmode; + str data; +} ksr_mhd_cstream_t; + typedef struct ksr_mhttpd_ctx { struct MHD_Connection *connection; @@ -436,23 +443,68 @@ static int fixup_mhttpd_send_reply(void **param, int param_no) } + static enum MHD_Result ksr_microhttpd_request(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) { - static int _first_callback; sr_kemi_eng_t *keng = NULL; str evname = str_init("microhttpd:request"); sip_msg_t *fmsg = NULL; run_act_ctx_t ctx; int rtb; + ksr_mhd_cstream_t *cstream = NULL; + + cstream = (ksr_mhd_cstream_t *)*ptr; + if(cstream == NULL) { + cstream = (ksr_mhd_cstream_t *)malloc(sizeof(ksr_mhd_cstream_t)); + if(cstream == NULL) { + LM_ERR("no more system memroy\n"); + return MHD_NO; + } + memset(cstream, 0, sizeof(ksr_mhd_cstream_t)); + *ptr = cstream; + } - if(&_first_callback != *ptr) { + if(cstream->rcvmode == 0) { + cstream->rcvmode = 1; /* the first time only the headers are valid, - do not respond in the first round... */ - *ptr = &_first_callback; + * do not respond in the first round */ return MHD_YES; + } else { + if(*upload_data_size != 0) { + char *buf = NULL; + int bsize = 0; + if(cstream->data.s != NULL) { + bsize = *upload_data_size + cstream->data.len + 1; + } else { + bsize = *upload_data_size + 1; + } + buf = (char *)malloc(sizeof(char) * bsize); + if(buf == NULL) { + if(cstream->data.s != NULL) { + free(cstream->data.s); + free(cstream); + *ptr = NULL; + return MHD_NO; + } + } + if(cstream->data.s != NULL) { + snprintf(buf, bsize, "%s%s", cstream->data.s, upload_data); + free(cstream->data.s); + } else { + snprintf(buf, bsize, "%s", upload_data); + } + cstream->data.s = buf; + cstream->data.len = bsize - 1; + *upload_data_size = 0; + return MHD_YES; + } else { + LM_DBG("incoming data: [%.*s]\n", + cstream->data.len <= 64 ? cstream->data.len : 64, + cstream->data.s); + } } *ptr = NULL; /* clear context pointer */ @@ -463,13 +515,19 @@ static enum MHD_Result ksr_microhttpd_request(void *cls, _ksr_mhttpd_ctx.url.len = strlen(_ksr_mhttpd_ctx.url.s); _ksr_mhttpd_ctx.httpversion.s = (char *)version; _ksr_mhttpd_ctx.httpversion.len = strlen(_ksr_mhttpd_ctx.httpversion.s); - if(*upload_data_size > 0) { - _ksr_mhttpd_ctx.data.s = (char *)upload_data; - _ksr_mhttpd_ctx.data.len = (int)(*upload_data_size); + if(_ksr_mhttpd_ctx.data.s != NULL) { + free(_ksr_mhttpd_ctx.data.s); + } + if(cstream->data.len > 0) { + if(_ksr_mhttpd_ctx.data.s != NULL) { + free(_ksr_mhttpd_ctx.data.s); + } + _ksr_mhttpd_ctx.data = cstream->data; } else { _ksr_mhttpd_ctx.data.s = NULL; _ksr_mhttpd_ctx.data.len = 0; } + free(cstream); _ksr_mhttpd_ctx.cinfo = MHD_get_connection_info( connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS); _ksr_mhttpd_ctx.srcip.s = NULL; _______________________________________________ Kamailio - Development Mailing List -- sr-dev@lists.kamailio.org To unsubscribe send an email to sr-dev-le...@lists.kamailio.org Important: keep the mailing list in the recipients, do not reply only to the sender!