Dear developers of microhttpd,

trying to port an application using microhttpd to macOS, I hit a strange
behaviour. Could you help me understanding whether it is my fault or a
hint of a bug?

Consider the attached file micro.c, which is the example on [1] with a
few modifications in the parameters of MHD_start_daemon. If I compile it
on Linux (Debian unstable with distributed microhttpd), it works fine.

 [1] https://www.gnu.org/software/libmicrohttpd/

If I compile it on macOS Sierra (with microhttpd 0.9.59 from brew), the
compilation succeeds, but when I execute:

$ ./micro 1234
Failed to create listen thread: Resource temporarily unavailable

However, if I switch MHD_OPTION_NOTIFY_COMPLETED and
MHD_OPTION_THREAD_STACK_SIZE (together with their arguments), or if I
remove either of the two, then it compiles and runs correctly. Is this
the intended behaviour?

(to my actual case it matters very few, since switching back the two
options is perfectly fine; however, this seems funny, and I thought that
you might want to check it thoroughly, if this is not meant to happen)

Please Cc me when replying, I am not subscribed to the list.

Thanks for your work on microhttpd!

Giovanni.
-- 
Giovanni Mascellani <[email protected]>
Postdoc researcher - Université Libre de Bruxelles
#include <microhttpd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define PAGE "<html><head><title>libmicrohttpd demo</title>"\
             "</head><body>libmicrohttpd demo</body></html>"

static int ahc_echo(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 dummy;
  const char * page = (const char*) cls;
  struct MHD_Response * response;
  int ret;

  if (0 != strcmp(method, "GET"))
    return MHD_NO; /* unexpected method */
  if (&dummy != *ptr)
    {
      /* The first time only the headers are valid,
         do not respond in the first round... */
      *ptr = &dummy;
      return MHD_YES;
    }
  if (0 != *upload_data_size)
    return MHD_NO; /* upload data in a GET!? */
  *ptr = NULL; /* clear context pointer */
  response = MHD_create_response_from_buffer (strlen(page),
                                              (void*) page,
					      MHD_RESPMEM_PERSISTENT);
  ret = MHD_queue_response(connection,
			   MHD_HTTP_OK,
			   response);
  MHD_destroy_response(response);
  return ret;
}

void *completed_cb(void *cls, struct MHD_Connection *conn, void **con_cls, enum MHD_RequestTerminationCode code) {
  return NULL;
}

int main(int argc,
	 char ** argv) {
  struct MHD_Daemon * d;
  if (argc != 2) {
    printf("%s PORT\n",
	   argv[0]);
    return 1;
  }
  d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_POLL | MHD_USE_DEBUG,
		       atoi(argv[1]),
		       NULL,
		       NULL,
		       &ahc_echo,
		       (void*) PAGE,
		       MHD_OPTION_NOTIFY_COMPLETED, completed_cb, NULL,
		       MHD_OPTION_THREAD_STACK_SIZE, 8*1024*1024,
		       MHD_OPTION_END);
  if (d == NULL)
    return 1;
  (void) getc (stdin);
  MHD_stop_daemon(d);
  return 0;
}

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to