Quellcode durchsuchen

Changing API: callback doesnt get mg_request_info pointer anymore, but it is possible to get it using mg_get_request_info()

Sergey Lyubka vor 13 Jahren
Ursprung
Commit
0e0091e1c0
6 geänderte Dateien mit 24 neuen und 17 gelöschten Zeilen
  1. 2 1
      bindings/python/example.py
  2. 5 6
      bindings/python/mongoose.py
  3. 6 3
      mongoose.c
  4. 7 3
      mongoose.h
  5. 2 2
      test/embed.c
  6. 2 2
      test/unit_test.c

+ 2 - 1
bindings/python/example.py

@@ -8,7 +8,8 @@ import mongoose
 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')

+ 5 - 6
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 @@ class Mongoose(object):
     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)

+ 6 - 3
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 &&

+ 7 - 3
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);
 

+ 2 - 2
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++) {

+ 2 - 2
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"