3

I'm developing a simple moongose based web server to send a file passed as argument over HTTP whatever the request is, but on each request I'm getting a stack overflow error.

Here is my code:

#include <stdio.h>
#include <string.h>
#include "mongoose.h"

// file path
char *path;

static void *callback(enum mg_event event, struct mg_connection *conn) 
{
    const struct mg_request_info *request_info = mg_get_request_info(conn);
    mg_send_file(conn, path);

    return "";
}

int main(int argc,char *argv[]) 
{    
    struct mg_context *ctx;
    const char *options[] = {"listening_ports", "8081", NULL};

    // registers file
    path = argv[1];
    ctx = mg_start(&callback, NULL, options);
    printf("%s", path);
    getchar();  // Wait until user hits "enter"
    mg_stop(ctx);

    return 0;
}

I'm using visual studio 2010 to build the project

Does anyone have any idea on what may cause this error?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
Ben
  • 966
  • 2
  • 10
  • 24
  • 1
    By the way, are you compiling this program as a C or C++ program? Please keep one of the language tags and remove the other. – Robᵩ Jan 07 '13 at 21:29
  • @Rob sorry, that's just a simple c program compiled under visual studio 2010, i deleted the wrong tag – Ben Jan 08 '13 at 09:53

2 Answers2

1

You're assigning no return value for your callback function, which by definition is undefined behavior. check the proper return result requirements of your callback, because void * is not synonymous with void. I was pretty sure the return value is callback-event dependent, but don't quote me on that. (dammit... too late).

Taken from the mongoose headers (at least the version I have access to) describing the purpose and responsibilities of the callback function supplied to mg_start():

// Prototype for the user-defined function. Mongoose calls this function
// on every event mentioned above.
//
// Parameters:
//   event: which event has been triggered.
//   conn: opaque connection handler. Could be used to read, write data to the
//         client, etc. See functions below that accept "mg_connection *".
//   request_info: Information about HTTP request.
//
// Return:
//   If handler returns non-NULL, that means that handler has processed the
//   request by sending appropriate HTTP reply to the client. Mongoose treats
//   the request as served.
//   If callback returns NULL, that means that callback has not processed
//   the request. Handler must not send any data to the client in this case.
//   Mongoose proceeds with request handling as if nothing happened.

typedef void * (*mg_callback_t)(enum mg_event event,
                   struct mg_connection *conn,
                   const struct mg_request_info *request_info);
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • i tried returning "" or even NULL but the error remains. i based my code on this simle example: https://github.com/valenok/mongoose/blob/master/examples/hello.c and i can't see much difference – Ben Jan 07 '13 at 23:39
  • @BenoitHnte You may want to update your question the source that reflects your changes if that is the case, as it is bound to stir additional similar responses. Also, provide OS and compiler toolchain info as well. One way or another by spec in the header, that function *must* provide a valid return value. I'll keep digging and see if there is something I'm missing. – WhozCraig Jan 07 '13 at 23:51
0

Make sure "path" is not NULL. Assign a default value.

valenok
  • 827
  • 7
  • 9