瀏覽代碼

Update rest example to use new mg_match() function

See #499
Also fixes a missing ; in the header file.
bel2125 3 年之前
父節點
當前提交
afb14a5b3e
共有 4 個文件被更改,包括 41 次插入16 次删除
  1. 3 3
      examples/rest/Makefile
  2. 26 1
      examples/rest/rest.c
  3. 11 11
      include/civetweb.h
  4. 1 1
      src/main.c

+ 3 - 3
examples/rest/Makefile

@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2018 CivetWeb Developers
+# Copyright (c) 2018, 2022 CivetWeb Developers
 # Copyright (c) 2013 No Face Press, LLC
 # License http://opensource.org/licenses/mit-license.php MIT License
 #
@@ -13,7 +13,7 @@ SRC = rest.c cJSON/cJSON.c cJSON/cJSON_Utils.c
 TOP = ../..
 CIVETWEB_LIB = libcivetweb.a
 
-CFLAGS = -I$(TOP)/include -IcJSON $(COPT) -DNO_FILES
+CFLAGS = -I$(TOP)/include -IcJSON $(COPT) -DNO_FILES -DMG_EXPERIMENTAL_INTERFACES
 LIBS = -lpthread
 
 include $(TOP)/resources/Makefile.in-os
@@ -28,7 +28,7 @@ $(PROG): $(CIVETWEB_LIB) $(SRC)
 	$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS)
 
 $(CIVETWEB_LIB):
-	$(MAKE) -C $(TOP) WITH_IPV6=1 WITH_WEBSOCKET=1 COPT='-DNO_SSL' clean lib
+	$(MAKE) -C $(TOP) WITH_IPV6=1 WITH_WEBSOCKET=1 COPT='-DNO_SSL -DMG_EXPERIMENTAL_INTERFACES' clean lib
 	cp $(TOP)/$(CIVETWEB_LIB) .
 
 clean:

+ 26 - 1
examples/rest/rest.c

@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2018 the CivetWeb developers
+ * Revisited version: Copyright (c) 2022 the CivetWeb developers
  * MIT License
  */
 
@@ -141,6 +142,7 @@ ExamplePUT(struct mg_connection *conn, const char *p1, const char *p2)
 }
 
 
+#if 0 /* Old version: User code had to split the url. */
 static int
 mg_vsplit(const char *url, const char *pattern, va_list va)
 {
@@ -185,6 +187,7 @@ mg_split(const char *url, const char *pattern, ...)
 	va_end(va);
 	return ret;
 }
+#endif
 
 
 static int
@@ -193,15 +196,37 @@ ExampleHandler(struct mg_connection *conn, void *cbdata)
 	char path1[1024], path2[1024];
 	const struct mg_request_info *ri = mg_get_request_info(conn);
 	const char *url = ri->local_uri;
-	(void)cbdata; /* currently unused */
+	size_t url_len = strlen(url);
 
 	/* Pattern matching */
+#if 0 /* Old version: User code had to split the url. */
 	if (2
 	    != mg_split(
 	           url, EXAMPLE_URI, path1, sizeof(path1), path2, sizeof(path2))) {
 		mg_send_http_error(conn, 404, "Invalid path: %s\n", url);
 		return 404;
 	}
+#else /* New version: User mg_match. */
+	struct mg_match_context mcx;
+	mcx.case_sensitive = 0;
+	ptrdiff_t ret = mg_match(EXAMPLE_URI, url, &mcx);
+	if ((ret != url_len) || (mcx.num_matches != 2)) {
+		/* Note: Could have done this with a $ at the end of the match
+		 * pattern as well. Then we would have to check for a return value
+		 * of -1 only. Here we use this version as minumum modification
+		 * of the existing code. */
+		printf("Match ret: %i\n", (int)ret);
+		mg_send_http_error(conn, 404, "Invalid path: %s\n", url);
+		return 404;
+	}
+	memcpy(path1, mcx.match[0].str, mcx.match[0].len);
+	path1[mcx.match[0].len] = 0;
+	memcpy(path2, mcx.match[1].str, mcx.match[1].len);
+	path2[mcx.match[1].len] = 0;
+#endif
+
+
+	(void)cbdata; /* currently unused */
 
 	/* According to method */
 	if (0 == strcmp(ri->request_method, "GET")) {

+ 11 - 11
include/civetweb.h

@@ -1396,20 +1396,20 @@ struct mg_match_context {
 */
 CIVETWEB_API ptrdiff_t mg_match(const char *pat,
                                 const char *str,
-                                struct mg_match_context *mcx)
+                                struct mg_match_context *mcx);
 #endif
 
 
-    /* Print error message to the opened error log stream.
-       This utilizes the provided logging configuration.
-         conn: connection (not used for sending data, but to get perameters)
-         fmt: format string without the line return
-         ...: variable argument list
-       Example:
-         mg_cry(conn,"i like %s", "logging"); */
-    CIVETWEB_API void mg_cry(const struct mg_connection *conn,
-                             PRINTF_FORMAT_STRING(const char *fmt),
-                             ...) PRINTF_ARGS(2, 3);
+/* Print error message to the opened error log stream.
+   This utilizes the provided logging configuration.
+     conn: connection (not used for sending data, but to get perameters)
+     fmt: format string without the line return
+     ...: variable argument list
+   Example:
+     mg_cry(conn,"i like %s", "logging"); */
+CIVETWEB_API void mg_cry(const struct mg_connection *conn,
+                         PRINTF_FORMAT_STRING(const char *fmt),
+                         ...) PRINTF_ARGS(2, 3);
 
 
 /* utility methods to compare two buffers, case insensitive. */

+ 1 - 1
src/main.c

@@ -1398,7 +1398,7 @@ start_civetweb(int argc, char *argv[])
 		}
 
 		for (j = 0; options[j] != NULL; j++) {
-			free(options[j]);
+			free((void *)options[j]);
 		}
 	}
 #endif