From 0e0091e1c0f66cce1ee050a43159b5f5abb1504b Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 3 Aug 2012 11:15:38 +0100 Subject: [PATCH] Changing API: callback doesnt get mg_request_info pointer anymore, but it is possible to get it using mg_get_request_info() --- bindings/python/example.py | 3 ++- bindings/python/mongoose.py | 11 +++++------ mongoose.c | 9 ++++++--- mongoose.h | 10 +++++++--- test/embed.c | 4 ++-- test/unit_test.c | 4 ++-- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/bindings/python/example.py b/bindings/python/example.py index 9c3f83ef28..1a82dc0d96 100644 --- a/bindings/python/example.py +++ b/bindings/python/example.py @@ -8,7 +8,8 @@ import sys # Handle /show and /form URIs. -def EventHandler(event, conn, info): +def EventHandler(event, conn): + info = conn.info if event == mongoose.HTTP_ERROR: conn.printf('%s', 'HTTP/1.0 200 OK\r\n') conn.printf('%s', 'Content-Type: text/plain\r\n\r\n') diff --git a/bindings/python/mongoose.py b/bindings/python/mongoose.py index d673836415..67a54d2d9f 100644 --- a/bindings/python/mongoose.py +++ b/bindings/python/mongoose.py @@ -73,10 +73,7 @@ class mg_request_info(ctypes.Structure): ] -mg_callback_t = ctypes.CFUNCTYPE(ctypes.c_void_p, - ctypes.c_int, - ctypes.c_void_p, - ctypes.POINTER(mg_request_info)) +mg_callback_t = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p) class Connection(object): @@ -86,6 +83,7 @@ class Connection(object): def __init__(self, mongoose, connection): self.m = mongoose self.conn = ctypes.c_void_p(connection) + self.info = self.m.dll.mg_get_request_info(self.conn).contents def get_header(self, name): val = self.m.dll.mg_get_header(self.conn, name) @@ -132,14 +130,15 @@ def __init__(self, callback, **kwargs): self.dll.mg_get_var.restype = ctypes.c_int self.dll.mg_get_cookie.restype = ctypes.c_int self.dll.mg_get_option.restype = ctypes.c_char_p + self.dll.mg_get_request_info.restype = ctypes.POINTER(mg_request_info) if callback: # Create a closure that will be called by the shared library. - def func(event, connection, request_info): + def func(event, connection): # Wrap connection pointer into the connection # object and call Python callback conn = Connection(self, connection) - return callback(event, conn, request_info.contents) and 1 or 0 + return callback(event, conn) and 1 or 0 # Convert the closure into C callable object self.callback = mg_callback_t(func) diff --git a/mongoose.c b/mongoose.c index 8f1119223b..521cb0fdb7 100644 --- a/mongoose.c +++ b/mongoose.c @@ -492,7 +492,7 @@ const char **mg_get_valid_option_names(void) { static void *call_user(struct mg_connection *conn, enum mg_event event) { conn->request_info.user_data = conn->ctx->user_data; return conn->ctx->user_callback == NULL ? NULL : - conn->ctx->user_callback(event, conn, &conn->request_info); + conn->ctx->user_callback(event, conn); } static int get_option_index(const char *name) { @@ -588,6 +588,10 @@ const char *mg_version(void) { return MONGOOSE_VERSION; } +const struct mg_request_info *mg_get_request_info(struct mg_connection *conn) { + return &conn->request_info; +} + static void mg_strlcpy(register char *dst, register const char *src, size_t n) { for (; *src != '\0' && n > 1; n--) { *dst++ = *src++; @@ -3758,8 +3762,7 @@ static int set_ssl_option(struct mg_context *ctx) { } else if (ctx->user_callback != NULL) { memset(&request_info, 0, sizeof(request_info)); request_info.user_data = ctx->user_data; - ctx->user_callback(MG_INIT_SSL, (struct mg_connection *) ctx->ssl_ctx, - &request_info); + ctx->user_callback(MG_INIT_SSL, (struct mg_connection *) ctx->ssl_ctx); } if (ctx->ssl_ctx != NULL && pem != NULL && diff --git a/mongoose.h b/mongoose.h index 33c2e49530..deff09d400 100644 --- a/mongoose.h +++ b/mongoose.h @@ -68,7 +68,6 @@ enum mg_event { // 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 have "mg_connection *" arg. -// request_info: Information about HTTP request. // // Return: // If handler returns non-NULL, that means that handler has processed the @@ -78,8 +77,7 @@ enum mg_event { // 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); + struct mg_connection *conn); // Start web server. @@ -151,6 +149,12 @@ int mg_modify_passwords_file(const char *passwords_file_name, const char *user, const char *password); + +// Return mg_request_info structure associated with the request. +// Always succeeds. +const struct mg_request_info *mg_get_request_info(struct mg_connection *); + + // Send data to the client. int mg_write(struct mg_connection *, const void *buf, size_t len); diff --git a/test/embed.c b/test/embed.c index b410901fee..0d4e602dd3 100644 --- a/test/embed.c +++ b/test/embed.c @@ -155,8 +155,8 @@ static const struct test_config { }; static void *callback(enum mg_event event, - struct mg_connection *conn, - const struct mg_request_info *request_info) { + struct mg_connection *conn) { + const struct mg_request_info *request_info = mg_get_request_info(conn); int i; for (i = 0; test_config[i].uri != NULL; i++) { diff --git a/test/unit_test.c b/test/unit_test.c index 04f6b86cb8..3564649972 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -129,8 +129,8 @@ static void test_remove_double_dots() { static const char *fetch_data = "hello world!\n"; static void *event_handler(enum mg_event event, - struct mg_connection *conn, - const struct mg_request_info *request_info) { + struct mg_connection *conn) { + const struct mg_request_info *request_info = mg_get_request_info(conn); if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) { mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Length: %d\r\n"