libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] How to process a post request without MHD_post_proce


From: Martin Dluhos
Subject: Re: [libmicrohttpd] How to process a post request without MHD_post_processor?
Date: Wed, 25 Apr 2012 22:50:30 -0500
User-agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120329 Thunderbird/11.0.1

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 04/25/2012 09:22 AM, Christian Grothoff wrote:
> 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.

Thank you for your help, Christian. I forgot that the function behaves
in this way.

I actually have a follow up question. Inside test_send_response_helper
I make a call to a function called send_response, which uses
microhttpd library functions to create and send an MHD_Response. Here
is the code:

int
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;

  /* The first time through only headers are processed. Only the second
   * time this function is called can we access upload_data.
   */
  if (*con_cls == NULL)
  {
    struct connection_info_struct *con_info;
    con_info = malloc (sizeof (struct connection_info_struct));

    /* Process POST and GET request separately. Signal an error
     * on any other method.
     */
    if  (strcmp (method, "POST") == 0)
      con_info->connection_type = POST;
    else if (strcmp (method, "GET") == 0)
      con_info->connection_type = GET;

    *con_cls = (void *) con_info;
    return MHD_YES;
  } // if

  /* This gets executed the second time the function is called. Only
   * now can we access upload_data.
   */
  printf ("SECOND: URL: %s DATA: %s\n", url, upload_data);

  /* Response strings for the server to return. */
  const char *busy_page =
    "The server is too busy to handle the verification request.";

  /* The server is too busy to respond. */
  ret_val = send_response(connection, busy_page,
MHD_HTTP_SERVICE_UNAVAILABLE);

  return MHD_YES;
}

int
send_response (struct MHD_Connection *connection, const char
*response_data, int status_code)
{
  int return_value;
  struct MHD_Response *response;

  response = MHD_create_response_from_data (strlen(response_data),
(void*) response_data, MHD_NO, MHD_NO);

  if (response == NULL)
  {
    return MHD_NO;
  }

  return_value = MHD_queue_response (connection, status_code, response);
  MHD_destroy_response (response);

  return return_value;
}

For some reason, MHD_queue_response returns MHD_NO indicating an error
instead of MHD_YES. Is there a way for me to find out what kind of
error I am getting here? Can you see what I am doing wrong here?

Thanks in advance for your help,
Martin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPmMYGAAoJEOzT5+ruTKvqE6gH/RBDcf0LRh7oyEZV37eXYZN9
AD5aB5zp3eIepIDfgUwSvSJyMrYfCFC3Rb1M7Z+TVF4T+m7X7gIb0lvymYTUHDgb
m/PbAWMYTOb4HUUzYu96VBwn3OBmhM/mhllfDOWdA2+qJxXUsA5bUvlIro9mAKXr
LUDaPJ8zIcfnE+gtE/E1foWnOrscragEj1Bww5hytm7SqcErU+3UIp0bTcr5BpEp
A0B7RSwB3/Z7lD0ojexp5LSkD2E1UXEnUZmFqK6t53QvQ6pw3s1Ax13UjCIqx8qx
Phn2tlP1ym9fkcLE34WZ9bEToAS98ghQi7RTZslWAmncDMIp7tEMubH4U/QAfZ0=
=JeRe
-----END PGP SIGNATURE-----



reply via email to

[Prev in Thread] Current Thread [Next in Thread]