Dear Martin, The first call to the "test_send_response_helper" is made *before* the upload data is available (after we just got the headers) so that you can choose not to send 100 CONTINUE for http/1.1. You need to change your function to return an 'int' and return 'MHD_YES' to get the next call which will contain upload data. Furthermore, 'upload_data' is NOT a 0-terminated string so you *must* limit your processing manually to the first '*upload_data_size' bytes in that buffer. Finally, you then need to reduce '*upload_data_size' (i.e. to zero) to indicate how many bytes of 'upload_data' you did process (so you don't get them again). Finally, if you are eventually called a *second* time with '0 == *upload_data_size', you can be sure that the upload is complete.
If you respect these rules (which, btw, the PostProcessor examples also
do), you can totally manually process a POST request in any way you see fit.
Happy hacking!
Christian
On 04/24/2012 10:42 PM, Martin Dluhos wrote:
> Hi,
>
> I am trying to send a post request to MHD_Daemon and then access the
> post data from the request in MHD_AccessHandlerCallback function which
> MHD_Daemon calls when it receives the request. For some reason,
> however, I am unable to access the post data from the callback
> function. Do I always need to use MHD_PostProcessor to process the
> post data or can I do without it? Here is my code:
>
> void
> test_send_response_helper (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 **con_cls)
> {
> int ret_val;
>
> // print url and upload_data
> printf ("URL: %s\nDATA: %s\n", url, upload_data);
> }
>
> void
> test_send_response ()
> {
> long int ssl_port = 443;
> struct MHD_Daemon *ssl_daemon;
>
> ssl_daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION,
> ssl_port,
> NULL,
> NULL,
> &test_send_response_helper,
> NULL,
> MHD_OPTION_NOTIFY_COMPLETED,
> request_completed,
> NULL,
> MHD_OPTION_END
> );
>
> CURL *curl;
> CURLcode res;
>
> /* Initialize curl. */
> curl_global_init(CURL_GLOBAL_ALL);
> curl = curl_easy_init();
>
> /* Set curl options. */
> if (curl)
> {
> char* data_to_send = "field_name=value";
>
> /* We want to send a test request to the daemon. */
> curl_easy_setopt(curl, CURLOPT_URL,
> "http://localhost/target/www.wikipedia.org+443");
> curl_easy_setopt(curl, CURLOPT_PORT, ssl_port);
> curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_to_send);
> curl_easy_setopt(curl, CURLOPT_POST, 1L);
>
> /* Make the curl request. */
> res = curl_easy_perform(curl);
> }
> ...
> }
>
> When I call test_send_response(), I get the following output from the
> printf statement:
>
> $ ./test
> URL: /target/www.wikipedia.org 443
> DATA: (null)
>
> I would like to retrieve whatever is passed as data_to_send with curl,
> but instead I am getting (null). How can I access the data? Am I wrong
> to expect the post data curl sends to be stored in the upload_data
> variable in test_send_response_helper? What am I doing wrong?
>
> Thanks,
> Martin
>
signature.asc
Description: OpenPGP digital signature
