|
@@ -1,5 +1,6 @@
|
|
/*
|
|
/*
|
|
* Copyright (c) 2018 the CivetWeb developers
|
|
* Copyright (c) 2018 the CivetWeb developers
|
|
|
|
+ * Revisited version: Copyright (c) 2022 the CivetWeb developers
|
|
* MIT License
|
|
* 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
|
|
static int
|
|
mg_vsplit(const char *url, const char *pattern, va_list va)
|
|
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);
|
|
va_end(va);
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
|
|
static int
|
|
static int
|
|
@@ -193,15 +196,37 @@ ExampleHandler(struct mg_connection *conn, void *cbdata)
|
|
char path1[1024], path2[1024];
|
|
char path1[1024], path2[1024];
|
|
const struct mg_request_info *ri = mg_get_request_info(conn);
|
|
const struct mg_request_info *ri = mg_get_request_info(conn);
|
|
const char *url = ri->local_uri;
|
|
const char *url = ri->local_uri;
|
|
- (void)cbdata; /* currently unused */
|
|
|
|
|
|
+ size_t url_len = strlen(url);
|
|
|
|
|
|
/* Pattern matching */
|
|
/* Pattern matching */
|
|
|
|
+#if 0 /* Old version: User code had to split the url. */
|
|
if (2
|
|
if (2
|
|
!= mg_split(
|
|
!= mg_split(
|
|
url, EXAMPLE_URI, path1, sizeof(path1), path2, sizeof(path2))) {
|
|
url, EXAMPLE_URI, path1, sizeof(path1), path2, sizeof(path2))) {
|
|
mg_send_http_error(conn, 404, "Invalid path: %s\n", url);
|
|
mg_send_http_error(conn, 404, "Invalid path: %s\n", url);
|
|
return 404;
|
|
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 */
|
|
/* According to method */
|
|
if (0 == strcmp(ri->request_method, "GET")) {
|
|
if (0 == strcmp(ri->request_method, "GET")) {
|