Browse Source

Merge pull request #48 from bel2125/master

mg_set_request_handler() mod to use pattern (Patch from Toni Wilk)
sunsetbrew 11 years ago
parent
commit
6fae12c37d
2 changed files with 17 additions and 1 deletions
  1. 10 0
      examples/embedded_c/embedded_c.c
  2. 7 1
      src/civetweb.c

+ 10 - 0
examples/embedded_c/embedded_c.c

@@ -58,6 +58,15 @@ int ABHandler(struct mg_connection *conn, void *cbdata)
     return 1;
 }
 
+int FooHandler(struct mg_connection *conn, void *cbdata)
+{
+    mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
+    mg_printf(conn, "<html><body>");
+    mg_printf(conn, "<h2>This is the Foo handler!!!</h2>");
+    mg_printf(conn, "</body></html>\n");
+    return 1;
+}
+
 
 int main(int argc, char *argv[])
 {
@@ -75,6 +84,7 @@ int main(int argc, char *argv[])
     mg_set_request_handler(ctx,EXIT_URI, ExitHandler,0);
     mg_set_request_handler(ctx,"/a", AHandler,0);
     mg_set_request_handler(ctx,"/a/b", ABHandler,0);
+    mg_set_request_handler( ctx, "**.foo$", FooHandler,0);
 
     printf("Browse files at http://localhost:%s/\n", PORT);
     printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);

+ 7 - 1
src/civetweb.c

@@ -4802,7 +4802,7 @@ static void handle_websocket_request(struct mg_connection *conn, const char *pat
 {
     const char *version = mg_get_header(conn, "Sec-WebSocket-Version");
 #ifdef USE_LUA
-    int lua_websock, shared_lua_websock = 0; 
+    int lua_websock, shared_lua_websock = 0;
     /* TODO: A websocket script may be shared between several clients, allowing them to communicate
              directly instead of writing to a data base and polling the data base. */
 #endif
@@ -5164,6 +5164,11 @@ static int use_request_handler(struct mg_connection *conn)
 
             return tmp_rh->handler(conn, tmp_rh->cbdata);
         }
+
+        /* try for pattern match */
+        if (match_prefix(tmp_rh->uri, tmp_rh->uri_len, uri) > 0) {
+           return tmp_rh->handler(conn, tmp_rh->cbdata);
+        }
 
     }
 
@@ -6366,6 +6371,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
     if (sTlsInit==0) {
         if (0 != pthread_key_create(&sTlsKey, NULL)) {
             mg_cry(fc(ctx), "Cannot initialize thread local storage");
+            free(ctx);
             return NULL;
         }
         sTlsInit++;