Przeglądaj źródła

Support absolute uri (#197) - (Step 6/?)

bel 9 lat temu
rodzic
commit
e29dbdc5a0
3 zmienionych plików z 24 dodań i 14 usunięć
  1. 2 1
      include/civetweb.h
  2. 10 6
      src/civetweb.c
  3. 12 7
      test/private.c

+ 2 - 1
include/civetweb.h

@@ -54,7 +54,8 @@ struct mg_connection; /* Handle for the individual connection */
 /* This structure contains information about the HTTP request. */
 struct mg_request_info {
 	const char *request_method; /* "GET", "POST", etc */
-	const char *uri;            /* URL-decoded URI */
+	const char *uri;            /* URL-decoded URI (full) */
+	const char *rel_uri;        /* URL-decoded URI (relative) */
 	const char *http_version;   /* E.g. "1.0", "1.1" */
 	const char *query_string;   /* URL part after '?', not including '?', or
 	                               NULL */

+ 10 - 6
src/civetweb.c

@@ -9850,8 +9850,13 @@ struct {
                                 {NULL, 0, 0}};
 
 
-/* return 0 for invalid uri, 1 for *, 2 for relative uri, 3 for absolute uri without port and 4 dor absolute uri with port */
-static int is_valid_uri(const char *uri)
+/* Check if the uri is valid.
+ * return 0 for invalid uri,
+ * return 1 for *,
+ * return 2 for relative uri,
+ * return 3 for absolute uri without port,
+ * return 4 for absolute uri with port */
+static int get_uri_type(const char *uri)
 {
 	int i;
 	char *hostend, *portbegin, *portend;
@@ -9870,11 +9875,10 @@ static int is_valid_uri(const char *uri)
 		return 2;
 	}
 
-	/* it could be an absolute uri */
-	/* is_valid_uri only checks if the uri is valid, not if it is
+	/* It could be an absolute uri: */
+	/* This function only checks if the uri is valid, not if it is
 	 * addressing the current server. So civetweb can also be used
 	 * as a proxy server. */
-
 	for (i = 0; abs_uri_protocols[i].proto != NULL; i++) {
 		if (mg_strncasecmp(uri,
 		                   abs_uri_protocols[i].proto,
@@ -10335,7 +10339,7 @@ static void process_new_connection(struct mg_connection *conn)
 					/*assert(ebuf[0] != '\0');*/
 					send_http_error(conn, reqerr, "%s", ebuf);
 				}
-			} else if ((uri_type = is_valid_uri(conn->request_info.uri)) == 0) {
+			} else if ((uri_type = get_uri_type(conn->request_info.uri)) == 0) {
 				mg_snprintf(conn,
 				            NULL, /* No truncation check for ebuf */
 				            ebuf,

+ 12 - 7
test/private.c

@@ -208,13 +208,18 @@ END_TEST
 
 START_TEST(test_is_valid_uri)
 {
-	ck_assert_int_eq(1, is_valid_uri("/api"));
-	ck_assert_int_eq(1, is_valid_uri("/api/"));
-	ck_assert_int_eq(1,
-	                 is_valid_uri("/some/long/path%20with%20space/file.xyz"));
-	ck_assert_int_eq(0, is_valid_uri("api"));
-	ck_assert_int_eq(1, is_valid_uri("*"));
-	ck_assert_int_eq(0, is_valid_uri("*xy"));
+	/* is_valid_uri is superseeded by get_uri_type */
+	ck_assert_int_eq(2, get_uri_type("/api"));
+	ck_assert_int_eq(2, get_uri_type("/api/"));
+	ck_assert_int_eq(2,
+	                 get_uri_type("/some/long/path%20with%20space/file.xyz"));
+	ck_assert_int_eq(0, get_uri_type("api"));
+	ck_assert_int_eq(1, get_uri_type("*"));
+	ck_assert_int_eq(0, get_uri_type("*xy"));
+	ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
+	ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
+	ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
+	ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
 }
 END_TEST