From 3ffdf5459ac28a5fb31461e614a3c069934c35cb Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Thu, 27 Sep 2012 22:20:02 +0100 Subject: [PATCH] API change: folded user_data into request_info, and introduced event-specific ev_data --- mongoose.c | 29 ++++++++--------------------- test/embed.c | 6 +++--- test/unit_test.c | 4 ++-- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/mongoose.c b/mongoose.c index 22ea85b2..ee8352c6 100644 --- a/mongoose.c +++ b/mongoose.c @@ -500,7 +500,6 @@ struct mg_connection { int64_t consumed_content; // How many bytes of content have been read char *buf; // Buffer for received data char *path_info; // PATH_INFO part of the URL - char *log_message; // Placeholder for the mongoose error log message int must_close; // 1 if connection must be closed int buf_size; // Buffer size int request_len; // Size of the request + headers in a buffer @@ -520,22 +519,6 @@ static void *call_user(struct mg_connection *conn, enum mg_event event) { NULL : conn->ctx->user_callback(event, conn); } -void *mg_get_user_data(struct mg_connection *conn) { - return conn != NULL && conn->ctx != NULL ? conn->ctx->user_data : NULL; -} - -const char *mg_get_log_message(const struct mg_connection *conn) { - return conn == NULL ? NULL : conn->log_message; -} - -int mg_get_reply_status_code(const struct mg_connection *conn) { - return conn == NULL ? -1 : conn->status_code; -} - -void *mg_get_ssl_context(const struct mg_connection *conn) { - return conn == NULL || conn->ctx == NULL ? NULL : conn->ctx->ssl_ctx; -} - static int get_option_index(const char *name) { int i; @@ -591,7 +574,7 @@ static void cry(struct mg_connection *conn, const char *fmt, ...) { // Do not lock when getting the callback value, here and below. // I suppose this is fine, since function cannot disappear in the // same way string option can. - conn->log_message = buf; + conn->request_info.ev_data = buf; if (call_user(conn, MG_EVENT_LOG) == NULL) { fp = conn->ctx == NULL || conn->ctx->config[ERROR_LOG_FILE] == NULL ? NULL : mg_fopen(conn->ctx->config[ERROR_LOG_FILE], "a+"); @@ -617,7 +600,7 @@ static void cry(struct mg_connection *conn, const char *fmt, ...) { } } } - conn->log_message = NULL; + conn->request_info.ev_data = NULL; } // Return fake connection structure. Used for logging, if connection @@ -903,6 +886,7 @@ static void send_http_error(struct mg_connection *conn, int status, int len; conn->status_code = status; + conn->request_info.ev_data = (void *) status; if (call_user(conn, MG_HTTP_ERROR) == NULL) { buf[0] = '\0'; len = 0; @@ -4342,6 +4326,7 @@ static int load_dll(struct mg_context *ctx, const char *dll_name, // Dynamically load SSL library. Set up ctx->ssl_ctx pointer. static int set_ssl_option(struct mg_context *ctx) { + struct mg_connection *conn; int i, size; const char *pem; @@ -4372,7 +4357,9 @@ static int set_ssl_option(struct mg_context *ctx) { // If user callback returned non-NULL, that means that user callback has // set up certificate itself. In this case, skip sertificate setting. - if (call_user(fc(ctx), MG_INIT_SSL) == NULL && + conn = fc(ctx); + conn->request_info.ev_data = ctx->ssl_ctx; + if (call_user(conn, MG_INIT_SSL) == NULL && (SSL_CTX_use_certificate_file(ctx->ssl_ctx, pem, SSL_FILETYPE_PEM) == 0 || SSL_CTX_use_PrivateKey_file(ctx->ssl_ctx, pem, SSL_FILETYPE_PEM) == 0)) { cry(fc(ctx), "%s: cannot open %s: %s", __func__, pem, ssl_error()); @@ -4425,7 +4412,7 @@ static int set_acl_option(struct mg_context *ctx) { } static void reset_per_request_attributes(struct mg_connection *conn) { - conn->path_info = conn->log_message = NULL; + conn->path_info = conn->request_info.ev_data = NULL; conn->num_bytes_sent = conn->consumed_content = 0; conn->status_code = -1; conn->must_close = conn->request_len = conn->throttle = 0; diff --git a/test/embed.c b/test/embed.c index 4033507f..ff4cf49d 100644 --- a/test/embed.c +++ b/test/embed.c @@ -119,10 +119,10 @@ static void test_get_request_info(struct mg_connection *conn, static void test_error(struct mg_connection *conn, const struct mg_request_info *ri) { - (void) ri; + int status = (int) ri->ev_data; mg_printf(conn, "HTTP/1.1 %d XX\r\n" - "Conntection: close\r\n\r\n", mg_get_reply_status_code(conn)); - mg_printf(conn, "Error: [%d]", mg_get_reply_status_code(conn)); + "Conntection: close\r\n\r\n", status); + mg_printf(conn, "Error: [%d]", status); } static void test_post(struct mg_connection *conn, diff --git a/test/unit_test.c b/test/unit_test.c index 47dc0de6..95cfaf0c 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -136,9 +136,9 @@ static void *event_handler(enum mg_event event, "%s", (int) strlen(fetch_data), fetch_data); return ""; } else if (event == MG_EVENT_LOG) { - printf("%s\n", mg_get_log_message(conn)); + printf("%s\n", (const char *) mg_get_request_info(conn)->ev_data); } - + return NULL; }