Преглед на файлове

Add server_port to mg_request_info

The server port is available for CGI, Lua and Duktape, but it was missing in
the C API. Add it to mg_request_info, and use this new data filed in CGI,
Lua and Duktape. Add documentation.
See also #936.
bel2125 преди 4 години
родител
ревизия
2fe6637d71
променени са 5 файла, в които са добавени 14 реда и са изтрити 9 реда
  1. 2 1
      docs/api/mg_request_info.md
  2. 5 2
      include/civetweb.h
  3. 5 4
      src/civetweb.c
  4. 1 1
      src/mod_duktape.inl
  5. 1 1
      src/mod_lua.inl

+ 2 - 1
docs/api/mg_request_info.md

@@ -16,7 +16,8 @@
 |**`remote addr`**|`char[48]`| The IP address of the remote client as a string. This can either represent an IPv4 or an IPv6 address.  Example: "127.0.0.1" |
 |~~`remote_ip`~~|`long`| *Deprecated. Use* `remote_addr` *instead* |
 |**`content_length`**|`long long`| The content length of the request body. This value can be -1 if no content length was provided. The request may still have body data, but the server cannot determine the length until all data has arrived (e.g. when the client closes the connection, or the final chunk of a chunked request has been received). |
-|**`remote_port`**|`int`| The port number at the client's side (an integer number between 1 and 65535). |
+|**`remote_port`**|`int`| The port number at the client side (an integer number between 1 and 65535). |
+|**`server_port`**|`int`| The port number at the server side (an integer number between 0 and 65535). |
 |**`is_ssl`**|`int`| 1 if the connection is over SSL (https), and 0 if it is a plain connection (http) |
 |**`user_data`**|`void *`| A pointer to the `user_data` information which was provided as a parameter to `mg_start()`. |
 |**`conn_data`**|`void *`| A pointer to connection specific user data |

+ 5 - 2
include/civetweb.h

@@ -166,8 +166,11 @@ struct mg_request_info {
 
 	long long content_length; /* Length (in bytes) of the request body,
 	                             can be -1 if no length was given. */
-	int remote_port;          /* Client's port */
-	int is_ssl;               /* 1 if SSL-ed, 0 if not */
+	int remote_port;          /* Port at client side */
+	int server_port;          /* Port at server side (one of the listening
+	                             ports) */
+	int is_ssl;               /* 1 if HTTPS or WS is used (SSL/TLS used),
+	                             0 if not */
 	void *user_data;          /* User data pointer passed to mg_start() */
 	void *conn_data;          /* Connection-specific user data */
 

+ 5 - 4
src/civetweb.c

@@ -4213,9 +4213,7 @@ mg_construct_local_link(const struct mg_connection *conn,
 		    (define_uri != NULL)
 		        ? define_uri
 		        : ((ri->request_uri != NULL) ? ri->request_uri : ri->local_uri);
-		int port = (define_port > 0)
-		               ? define_port
-		               : htons(USA_IN_PORT_UNSAFE(&conn->client.lsa));
+		int port = (define_port > 0) ? define_port : ri->server_port;
 		int default_port = 80;
 
 		if (uri == NULL) {
@@ -11490,7 +11488,7 @@ prepare_cgi_environment(struct mg_connection *conn,
 	addenv(env, "%s", "SERVER_PROTOCOL=HTTP/1.1");
 	addenv(env, "%s", "REDIRECT_STATUS=200"); /* For PHP */
 
-	addenv(env, "SERVER_PORT=%d", ntohs(USA_IN_PORT_UNSAFE(&conn->client.lsa)));
+	addenv(env, "SERVER_PORT=%d", conn->request_info.server_port);
 
 	sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
 	addenv(env, "REMOTE_ADDR=%s", src_addr);
@@ -19117,6 +19115,9 @@ worker_thread_run(struct mg_connection *conn)
 		conn->request_info.remote_port =
 		    ntohs(USA_IN_PORT_UNSAFE(&conn->client.rsa));
 
+		conn->request_info.server_port =
+		    ntohs(USA_IN_PORT_UNSAFE(&conn->client.lsa));
+
 		sockaddr_to_string(conn->request_info.remote_addr,
 		                   sizeof(conn->request_info.remote_addr),
 		                   &conn->client.rsa);

+ 1 - 1
src/mod_duktape.inl

@@ -262,7 +262,7 @@ mg_exec_duktape_script(struct mg_connection *conn, const char *script_name)
 	duk_push_int(duk_ctx, conn->request_info.remote_port);
 	duk_put_prop_string(duk_ctx, -2, "remote_port");
 
-	duk_push_int(duk_ctx, ntohs(conn->client.lsa.sin.sin_port));
+	duk_push_int(duk_ctx, conn->request_info.server_port);
 	duk_put_prop_string(duk_ctx, -2, "server_port");
 
 	duk_push_object(duk_ctx); /* subfolder "conn.http_headers" */

+ 1 - 1
src/mod_lua.inl

@@ -2437,8 +2437,8 @@ prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
 	reg_string(L, "remote_addr", conn->request_info.remote_addr);
 	/* TODO (high): ip version */
 	reg_int(L, "remote_port", conn->request_info.remote_port);
+	reg_int(L, "server_port", conn->request_info.server_port);
 	reg_int(L, "num_headers", conn->request_info.num_headers);
-	reg_int(L, "server_port", ntohs(conn->client.lsa.sin.sin_port));
 
 	if (conn->path_info != NULL) {
 		reg_string(L, "path_info", conn->path_info);