Author: rjung
Date: Mon Mar  7 15:17:11 2011
New Revision: 1078805

URL: http://svn.apache.org/viewvc?rev=1078805&view=rev
Log:
Support Servlet API getRemotePort().

Works for Tomcat 5.5.28, 6.0.20 and 7.0.0 in
combination with Apache and ISAPI plugins.

BZ 41263

Modified:
    tomcat/jk/trunk/native/apache-1.3/mod_jk.c
    tomcat/jk/trunk/native/apache-2.0/mod_jk.c
    tomcat/jk/trunk/native/common/jk_ajp_common.c
    tomcat/jk/trunk/native/common/jk_ajp_common.h
    tomcat/jk/trunk/native/common/jk_service.h
    tomcat/jk/trunk/native/common/jk_util.c
    tomcat/jk/trunk/native/iis/jk_isapi_plugin.c
    tomcat/jk/trunk/native/netscape/jk_nsapi_plugin.c
    tomcat/jk/trunk/xdocs/generic_howto/proxy.xml
    tomcat/jk/trunk/xdocs/miscellaneous/changelog.xml
    tomcat/jk/trunk/xdocs/reference/apache.xml

Modified: tomcat/jk/trunk/native/apache-1.3/mod_jk.c
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/native/apache-1.3/mod_jk.c?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/native/apache-1.3/mod_jk.c (original)
+++ tomcat/jk/trunk/native/apache-1.3/mod_jk.c Mon Mar  7 15:17:11 2011
@@ -65,6 +65,7 @@
 #define JK_LOG_DEF_FILE             ("logs/mod_jk.log")
 #define JK_SHM_DEF_FILE             ("logs/jk-runtime-status")
 #define JK_ENV_REMOTE_ADDR          ("JK_REMOTE_ADDR")
+#define JK_ENV_REMOTE_PORT          ("JK_REMOTE_PORT")
 #define JK_ENV_REMOTE_HOST          ("JK_REMOTE_HOST")
 #define JK_ENV_REMOTE_USER          ("JK_REMOTE_USER")
 #define JK_ENV_AUTH_TYPE            ("JK_AUTH_TYPE")
@@ -171,6 +172,7 @@ typedef struct
      * proxy in front of us.
      */
     char *remote_addr_indicator;
+    char *remote_port_indicator;
     char *remote_host_indicator;
     char *remote_user_indicator;
     char *auth_type_indicator;
@@ -673,12 +675,20 @@ static int init_ws_service(apache_privat
     s->remote_host = get_env_string(r, s->remote_host,
                                     conf->remote_host_indicator, 1);
 
-    if (conf->options & JK_OPT_FWDLOCAL)
+    if (conf->options & JK_OPT_FWDLOCAL) {
         s->remote_addr = r->connection->local_ip;
-    else
+        /* We don't know the client port of the backend connection. */
+        s->remote_port = "0";
+    }
+    else {
         s->remote_addr = r->connection->remote_ip;
+        s->remote_port = ap_psprintf(r->pool, "%d",
+                                     
ntohs(r->connection->remote_addr.sin_port));
+    }
     s->remote_addr = get_env_string(r, s->remote_addr,
                                     conf->remote_addr_indicator, 1);
+    s->remote_port = get_env_string(r, s->remote_port,
+                                    conf->remote_port_indicator, 1);
 
     if (conf->options & JK_OPT_FLUSHPACKETS)
         s->flush_packets = 1;
@@ -1701,6 +1711,7 @@ static const char *jk_set_worker_indicat
  * Directives Handling for setting various environment names
  * used to overwrite the following request information:
  * - remote_addr
+ * - remote_port
  * - remote_host
  * - remote_user
  * - auth_type
@@ -1717,6 +1728,16 @@ static const char *jk_set_remote_addr_in
     return NULL;
 }
 
+static const char *jk_set_remote_port_indicator(cmd_parms * cmd,
+                                                void *dummy, char *indicator)
+{
+    server_rec *s = cmd->server;
+    jk_server_conf_t *conf =
+        (jk_server_conf_t *) ap_get_module_config(s->module_config, 
&jk_module);
+    conf->remote_port_indicator = ap_pstrdup(cmd->pool, indicator);
+    return NULL;
+}
+
 static const char *jk_set_remote_host_indicator(cmd_parms * cmd,
                                                 void *dummy, char *indicator)
 {
@@ -2147,6 +2168,7 @@ static const command_rec jk_cmds[] = {
      * Environment variables used to overwrite the following
      * request information which gets forwarded:
      * - remote_addr
+     * - remote_port
      * - remote_host
      * - remote_user
      * - auth_type
@@ -2155,6 +2177,8 @@ static const command_rec jk_cmds[] = {
      */
     {"JkRemoteAddrIndicator", jk_set_remote_addr_indicator, NULL, RSRC_CONF, 
TAKE1,
      "Name of the Apache environment that contains the remote address"},
+    {"JkRemotePortIndicator", jk_set_remote_port_indicator, NULL, RSRC_CONF, 
TAKE1,
+     "Name of the Apache environment that contains the remote port"},
     {"JkRemoteHostIndicator", jk_set_remote_host_indicator, NULL, RSRC_CONF, 
TAKE1,
      "Name of the Apache environment that contains the remote host name"},
     {"JkRemoteUserIndicator", jk_set_remote_user_indicator, NULL, RSRC_CONF, 
TAKE1,
@@ -2477,6 +2501,7 @@ static void *create_jk_config(ap_pool * 
          * proxy in front of us.
          */
         c->remote_addr_indicator = JK_ENV_REMOTE_ADDR;
+        c->remote_port_indicator = JK_ENV_REMOTE_PORT;
         c->remote_host_indicator = JK_ENV_REMOTE_HOST;
         c->remote_user_indicator = JK_ENV_REMOTE_USER;
         c->auth_type_indicator = JK_ENV_AUTH_TYPE;
@@ -2548,6 +2573,8 @@ static void *merge_jk_config(ap_pool * p
 
     if (!overrides->remote_addr_indicator)
         overrides->remote_addr_indicator = base->remote_addr_indicator;
+    if (!overrides->remote_port_indicator)
+        overrides->remote_port_indicator = base->remote_port_indicator;
     if (!overrides->remote_host_indicator)
         overrides->remote_host_indicator = base->remote_host_indicator;
     if (!overrides->remote_user_indicator)

Modified: tomcat/jk/trunk/native/apache-2.0/mod_jk.c
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/native/apache-2.0/mod_jk.c?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/native/apache-2.0/mod_jk.c (original)
+++ tomcat/jk/trunk/native/apache-2.0/mod_jk.c Mon Mar  7 15:17:11 2011
@@ -101,6 +101,7 @@
 #define JK_LOG_DEF_FILE             ("logs/mod_jk.log")
 #define JK_SHM_DEF_FILE             ("logs/jk-runtime-status")
 #define JK_ENV_REMOTE_ADDR          ("JK_REMOTE_ADDR")
+#define JK_ENV_REMOTE_PORT          ("JK_REMOTE_PORT")
 #define JK_ENV_REMOTE_HOST          ("JK_REMOTE_HOST")
 #define JK_ENV_REMOTE_USER          ("JK_REMOTE_USER")
 #define JK_ENV_AUTH_TYPE            ("JK_AUTH_TYPE")
@@ -202,6 +203,7 @@ typedef struct
      * proxy in front of us.
      */
     char *remote_addr_indicator;
+    char *remote_port_indicator;
     char *remote_host_indicator;
     char *remote_user_indicator;
     char *auth_type_indicator;
@@ -729,12 +731,20 @@ static int init_ws_service(apache_privat
                                                 REMOTE_HOST, NULL);
     s->remote_host = get_env_string(r, s->remote_host,
                                     conf->remote_host_indicator, 1);
-    if (conf->options & JK_OPT_FWDLOCAL)
+    if (conf->options & JK_OPT_FWDLOCAL) {
         s->remote_addr = r->connection->local_ip;
-    else
+        /* We don't know the client port of the backend connection. */
+        s->remote_port = "0";
+    }
+    else {
         s->remote_addr = r->connection->remote_ip;
+        s->remote_port = apr_itoa(r->pool, r->connection->remote_addr->port);
+    }
     s->remote_addr = get_env_string(r, s->remote_addr,
                                     conf->remote_addr_indicator, 1);
+    s->remote_port = get_env_string(r, s->remote_port,
+                                    conf->remote_port_indicator, 1);
+
     if (conf->options & JK_OPT_FLUSHPACKETS)
         s->flush_packets = 1;
     if (conf->options & JK_OPT_FLUSHEADER)
@@ -1799,6 +1809,7 @@ static const char *jk_set_worker_indicat
  * Directives Handling for setting various environment names
  * used to overwrite the following request information:
  * - remote_addr
+ * - remote_port
  * - remote_host
  * - remote_user
  * - auth_type
@@ -1815,6 +1826,16 @@ static const char *jk_set_remote_addr_in
     return NULL;
 }
 
+static const char *jk_set_remote_port_indicator(cmd_parms * cmd,
+                                                void *dummy, const char 
*indicator)
+{
+    server_rec *s = cmd->server;
+    jk_server_conf_t *conf =
+        (jk_server_conf_t *) ap_get_module_config(s->module_config, 
&jk_module);
+    conf->remote_port_indicator = apr_pstrdup(cmd->pool, indicator);
+    return NULL;
+}
+
 static const char *jk_set_remote_host_indicator(cmd_parms * cmd,
                                                 void *dummy, const char 
*indicator)
 {
@@ -2274,6 +2295,7 @@ static const command_rec jk_cmds[] = {
      * Environment variables used to overwrite the following
      * request information which gets forwarded:
      * - remote_addr
+     * - remote_port
      * - remote_host
      * - remote_user
      * - auth_type
@@ -2282,6 +2304,8 @@ static const command_rec jk_cmds[] = {
      */
     AP_INIT_TAKE1("JkRemoteAddrIndicator", jk_set_remote_addr_indicator, NULL, 
RSRC_CONF,
                   "Name of the Apache environment that contains the remote 
address"),
+    AP_INIT_TAKE1("JkRemotePortIndicator", jk_set_remote_port_indicator, NULL, 
RSRC_CONF,
+                  "Name of the Apache environment that contains the remote 
port"),
     AP_INIT_TAKE1("JkRemoteHostIndicator", jk_set_remote_host_indicator, NULL, 
RSRC_CONF,
                   "Name of the Apache environment that contains the remote 
host name"),
     AP_INIT_TAKE1("JkRemoteUserIndicator", jk_set_remote_user_indicator, NULL, 
RSRC_CONF,
@@ -2715,6 +2739,7 @@ static void *create_jk_config(apr_pool_t
          * proxy in front of us.
          */
         c->remote_addr_indicator = JK_ENV_REMOTE_ADDR;
+        c->remote_port_indicator = JK_ENV_REMOTE_PORT;
         c->remote_host_indicator = JK_ENV_REMOTE_HOST;
         c->remote_user_indicator = JK_ENV_REMOTE_USER;
         c->auth_type_indicator = JK_ENV_AUTH_TYPE;
@@ -2789,6 +2814,8 @@ static void *merge_jk_config(apr_pool_t 
 
     if (!overrides->remote_addr_indicator)
         overrides->remote_addr_indicator = base->remote_addr_indicator;
+    if (!overrides->remote_port_indicator)
+        overrides->remote_port_indicator = base->remote_port_indicator;
     if (!overrides->remote_host_indicator)
         overrides->remote_host_indicator = base->remote_host_indicator;
     if (!overrides->remote_user_indicator)

Modified: tomcat/jk/trunk/native/common/jk_ajp_common.c
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/native/common/jk_ajp_common.c?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/native/common/jk_ajp_common.c (original)
+++ tomcat/jk/trunk/native/common/jk_ajp_common.c Mon Mar  7 15:17:11 2011
@@ -580,6 +580,25 @@ static int ajp_marshal_into_msgb(jk_msg_
         }
     }
 
+    /* Forward the remote port information, which was forgotten
+     * from the builtin data of the AJP 13 protocol.
+     * Since the servlet spec allows to retrieve it via getRemotePort(),
+     * we provide the port to the Tomcat connector as a request
+     * attribute. Modern Tomcat versions know how to retrieve
+     * the remote port from this attribute.
+     */
+    {
+        if (jk_b_append_byte(msg, SC_A_REQ_ATTRIBUTE) ||
+            jk_b_append_string(msg, SC_A_REQ_REMOTE_PORT)   ||
+            jk_b_append_string(msg, s->remote_port)) {
+            jk_log(l, JK_LOG_ERROR,
+                   "failed appending the remote port %s",
+                   s->remote_port);
+            JK_TRACE_EXIT(l);
+            return JK_FALSE;
+        }
+    }
+
     if (s->num_attributes > 0) {
         for (i = 0; i < s->num_attributes; i++) {
             if (jk_b_append_byte(msg, SC_A_REQ_ATTRIBUTE) ||

Modified: tomcat/jk/trunk/native/common/jk_ajp_common.h
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/native/common/jk_ajp_common.h?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/native/common/jk_ajp_common.h (original)
+++ tomcat/jk/trunk/native/common/jk_ajp_common.h Mon Mar  7 15:17:11 2011
@@ -71,6 +71,14 @@ extern "C"
 #define SC_A_ARE_DONE           (unsigned char)0xFF
 
 /*
+ * AJP private request attributes
+ *
+ * The following request attribute is recognized by Tomcat
+ * to contain the forwarded remote port.
+ */
+#define SC_A_REQ_REMOTE_PORT    ("AJP_REMOTE_PORT")
+
+/*
  * Request methods, coded as numbers instead of strings.
  * The list of methods was taken from Section 5.1.1 of RFC 2616,
  * RFC 2518, the ACL IETF draft, and the DeltaV IESG Proposed Standard.

Modified: tomcat/jk/trunk/native/common/jk_service.h
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/native/common/jk_service.h?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/native/common/jk_service.h (original)
+++ tomcat/jk/trunk/native/common/jk_service.h Mon Mar  7 15:17:11 2011
@@ -149,6 +149,7 @@ struct jk_ws_service
     const char *protocol;
     char *req_uri;
     const char *remote_addr;
+    const char *remote_port;
     const char *remote_host;
     const char *remote_user;
     const char *auth_type;

Modified: tomcat/jk/trunk/native/common/jk_util.c
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/native/common/jk_util.c?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/native/common/jk_util.c (original)
+++ tomcat/jk/trunk/native/common/jk_util.c Mon Mar  7 15:17:11 2011
@@ -2043,6 +2043,7 @@ void jk_init_ws_service(jk_ws_service_t 
     s->protocol = NULL;
     s->req_uri = NULL;
     s->remote_addr = NULL;
+    s->remote_port = NULL;
     s->remote_host = NULL;
     s->remote_user = NULL;
     s->auth_type = NULL;

Modified: tomcat/jk/trunk/native/iis/jk_isapi_plugin.c
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/native/iis/jk_isapi_plugin.c?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/native/iis/jk_isapi_plugin.c (original)
+++ tomcat/jk/trunk/native/iis/jk_isapi_plugin.c Mon Mar  7 15:17:11 2011
@@ -3011,7 +3011,7 @@ static int init_ws_service(isapi_private
     GET_SERVER_VARIABLE_VALUE("REMOTE_USER", s->remote_user);
     GET_SERVER_VARIABLE_VALUE("SERVER_PROTOCOL", s->protocol);
     GET_SERVER_VARIABLE_VALUE("REMOTE_HOST", s->remote_host);
-    GET_SERVER_VARIABLE_VALUE("REMOTE_ADDR", s->remote_addr);
+    GET_SERVER_VARIABLE_VALUE("REMOTE_PORT", s->remote_port);
     GET_SERVER_VARIABLE_VALUE(SERVER_NAME, s->server_name);
     GET_SERVER_VARIABLE_VALUE_INT("SERVER_PORT", s->server_port, 80);
     GET_SERVER_VARIABLE_VALUE(SERVER_SOFTWARE, s->server_software);

Modified: tomcat/jk/trunk/native/netscape/jk_nsapi_plugin.c
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/native/netscape/jk_nsapi_plugin.c?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/native/netscape/jk_nsapi_plugin.c (original)
+++ tomcat/jk/trunk/native/netscape/jk_nsapi_plugin.c Mon Mar  7 15:17:11 2011
@@ -482,6 +482,8 @@ static int init_ws_service(nsapi_private
 
     s->remote_host = session_dns(private_data->sn);
     s->remote_addr = pblock_findval("ip", private_data->sn->client);
+    /* Remote port is not available from NSAPI. */
+    s->remote_port = "0";
 
     tmp = pblock_findval("uri", private_data->rq->reqpb);
     size = 3 * strlen(tmp) + 1;

Modified: tomcat/jk/trunk/xdocs/generic_howto/proxy.xml
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/xdocs/generic_howto/proxy.xml?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/xdocs/generic_howto/proxy.xml (original)
+++ tomcat/jk/trunk/xdocs/generic_howto/proxy.xml Mon Mar  7 15:17:11 2011
@@ -73,6 +73,16 @@ if it contains an explicit port, or is e
 </li>
 <li>client address: <code>getRemoteAddr()</code>
 </li>
+<li>client port: <code>getRemotePort()</code>
+The remote port was initially not supported. It is available when using mod_jk 
1.2.32
+with Apache or IIS (not for the NSAPI plugin) together with Tomcat version at 
least
+5.5.28, 6.0.20 or 7.0.0. For older versions, <code>getRemotePort()</code>
+will incorrectly return 0 or -1. As a workaround you can forward the remote 
port by setting
+<code>JkEnvVar REMOTE_PORT</code> and then either using
+<code>request.getAttribute("REMOTE_PORT")</code> instead of 
<code>getRemotePort()</code>
+or wrapping the request using a filter and overriding 
<code>getRemotePort()</code> with
+<code>request.getAttribute("REMOTE_PORT")</code>.
+</li>
 <li>client host: <code>getRemoteHost()</code>
 </li>
 <li>authentication type: <code>getAuthType()</code>
@@ -110,18 +120,6 @@ This is for Tomcat, it has not yet been 
 </li>
 </ul>
 </p>
-<warn>
-The remote port number has been forgotten in the AJP13 protocol.
-</warn>
-<p>
-So <code>getRemotePort()</code>
-will incorrectly return 0 or -1. As a workaround you can forward the remote 
port by setting
-<code>JkEnvVar REMOTE_PORT</code> and then either using
-<code>request.getAttribute("REMOTE_PORT")</code> instead of 
<code>getRemotePort()</code>
-or wrapping the request in a filter and overriding 
<code>getRemotePort()</code> with
-<code>request.getAttribute("REMOTE_PORT")</code>. Recent versions of Tomcat 
might
-automatically respect the forwarded attribute <code>REMOTE_PORT</code>.
-</p>
 </section>
 <section name="Fine Tuning">
 <br/>

Modified: tomcat/jk/trunk/xdocs/miscellaneous/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/xdocs/miscellaneous/changelog.xml?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/xdocs/miscellaneous/changelog.xml (original)
+++ tomcat/jk/trunk/xdocs/miscellaneous/changelog.xml Mon Mar  7 15:17:11 2011
@@ -50,6 +50,11 @@
         the session id. (rjung)
       </add>
       <fix>
+        <bug>41263</bug>: Support Servlet API getRemotePort().
+        Works for Tomcat 5.5.28, 6.0.20 and 7.0.0 and Apache and ISAPI
+        plugins.
+      </fix>
+      <fix>
         <bug>50363</bug>: IIS: Prevent chunk encoding of empty message
         bodies for 204, 205 and 304 responses. (timw)
       </fix>

Modified: tomcat/jk/trunk/xdocs/reference/apache.xml
URL: 
http://svn.apache.org/viewvc/tomcat/jk/trunk/xdocs/reference/apache.xml?rev=1078805&r1=1078804&r2=1078805&view=diff
==============================================================================
--- tomcat/jk/trunk/xdocs/reference/apache.xml (original)
+++ tomcat/jk/trunk/xdocs/reference/apache.xml Mon Mar  7 15:17:11 2011
@@ -342,6 +342,16 @@ The default value is "JK_REMOTE_ADDR".
 <br/>
 This directive has been added in version 1.2.28 of mod_jk.
 </p></attribute>
+<attribute name="JkRemotePortIndicator" required="false"><p>
+Name of the Apache environment variable which can be used to overwrite
+the forwarded remote (client) IP address.
+Use this only if you need to adjust the data (see the
+<a href="../generic_howto/proxy.html">proxy</a> documentation).
+<br/>
+The default value is "JK_REMOTE_PORT".
+<br/>
+This directive has been added in version 1.2.32 of mod_jk.
+</p></attribute>
 <attribute name="JkRemoteUserIndicator" required="false"><p>
 Name of the Apache environment variable which can be used to overwrite
 the forwarded user name.



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to