1

I am trying to set a mongoose web server v3.3 with a self-signed SSL certificate. I know how to do it without SSL but I want to implement HTTPS.

I have implemented something like this:

void *event_handler(enum mg_event event,
struct mg_connection *conn) {

const struct mg_request_info *request_info = mg_get_request_info(conn);

static void* done = "done";

if (event == MG_NEW_REQUEST) {
    if (strcmp(request_info->uri, "/hello") == 0) {
        // handle c[renderer] request
        if(strcmp(request_info->request_method, "GET") != 0) {
            // send error (we only care about HTTP GET)
            mg_printf(conn, "HTTP/1.1 %d Error (%s)\r\n\r\n%s",
                500,
                "we only care about HTTP GET",
                "we only care about HTTP GET");
            // return not null means we handled the request
            return done;
        }

        // handle your GET request to /hello
        char* content = "Hello World!";
        char* mimeType = "text/plain";
        int contentLength = strlen(content);

        mg_printf(conn,
            "HTTP/1.1 200 OK\r\n"
            "Cache: no-cache\r\n"
            "Content-Type: %s\r\n"
            "Content-Length: %d\r\n"
            "\r\n",
            mimeType,
            contentLength);
        mg_write(conn, content, contentLength);
        return done;
        }
    }
    // in this example i only handle /hello
    mg_printf(conn, "HTTP/1.1 %d Error (%s)\r\n\r\n%s",
        500, /* This the error code you want to send back*/
        "Invalid Request.",
        "Invalid Request.");
    return done;
}

// No suitable handler found, mark as not processed. Mongoose will
// try to serve the request.
return NULL;
}

int main(int argc, char **argv) {

const char *options[] = {
    "ssl_certificate", "cert.pem",
    "listening_ports", "443s",
    "num_threads", "10",
    NULL
};

static struct mg_context *ctx;

ctx = mg_start(&event_handler, options);
if(ctx == NULL) {
    exit(EXIT_FAILURE);
}

puts("Server running, press enter to exit\n");
getchar();
mg_stop(ctx);

return EXIT_SUCCESS;
}

The problem is I am not able to access my server from the web browser. I think the problem is that the first event my callback receives is MG_INIT_SSL but I do not know how to handle or process it. Could anybody please help?

1 Answers1

0

Firstly, I believe you should not have to handle other events than MG_NEW_REQUEST in your event handler.

I would also debug using openssl:

openssl s_client -connect <hostname:port>

to see that the SSL connection gets set up properly.

In any case, Cesanta does provide a complete working example for you to use:

https://github.com/cesanta/mongoose/tree/master/examples/simplest_web_server_ssl

larslars
  • 168
  • 1
  • 14
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/24679387) – Mathieu Nov 25 '19 at 13:26
  • Thanks Mathieu, I see how my comment can be misunderstood. Tried to rectify now. – larslars Jan 06 '20 at 11:52
  • (..) and the link is dead – Vega4 Sep 24 '21 at 06:17