You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jakarta.apache.org by sebb <se...@gmail.com> on 2011/09/14 21:01:12 UTC

Re: svn commit: r1170701 - in /jakarta/jmeter/trunk: src/protocol/http/org/apache/jmeter/protocol/http/sampler/ xdocs/

On 14 September 2011 17:10,  <mi...@apache.org> wrote:
> Author: milamber
> Date: Wed Sep 14 16:10:06 2011
> New Revision: 1170701
>
> URL: http://svn.apache.org/viewvc?rev=1170701&view=rev
> Log:
> Bug 51775 - Port number duplicates in Host header when capturing by HttpClient (3.1 and 4.x)
>
> Modified:
>    jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java
>    jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
>    jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHCAbstractImpl.java
>    jakarta/jmeter/trunk/xdocs/changes.xml
>
> Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java
> URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java?rev=1170701&r1=1170700&r2=1170701&view=diff
> ==============================================================================
> --- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java (original)
> +++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java Wed Sep 14 16:10:06 2011
> @@ -592,6 +592,7 @@ public class HTTPHC3Impl extends HTTPHCA
>                     if (! HEADER_CONTENT_LENGTH.equalsIgnoreCase(n)){
>                         String v = header.getValue();
>                         if (HEADER_HOST.equalsIgnoreCase(n)) {
> +                            v = removePortInHostHeader(v, u.getPort());
>                             method.getParams().setVirtualHost(v);
>                         } else {
>                             method.addRequestHeader(n, v);
>
> Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
> URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java?rev=1170701&r1=1170700&r2=1170701&view=diff
> ==============================================================================
> --- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java (original)
> +++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java Wed Sep 14 16:10:06 2011
> @@ -638,8 +638,8 @@ public class HTTPHC4Impl extends HTTPHCA
>                     if (! HEADER_CONTENT_LENGTH.equalsIgnoreCase(n)){
>                         String v = header.getValue();
>                         if (HEADER_HOST.equalsIgnoreCase(n)) {
> -                            // TODO is it a bug that HC 4.x does not add the correct port to the generated Host header?
>                             int port = url.getPort();
> +                            v = removePortInHostHeader(v, port);
>                             if (port != -1) {
>                                 if (port == url.getDefaultPort()) {
>                                     port = -1; // no need to specify the port if it is the default
>
> Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHCAbstractImpl.java
> URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHCAbstractImpl.java?rev=1170701&r1=1170700&r2=1170701&view=diff
> ==============================================================================
> --- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHCAbstractImpl.java (original)
> +++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHCAbstractImpl.java Wed Sep 14 16:10:06 2011
> @@ -152,4 +152,19 @@ public abstract class HTTPHCAbstractImpl
>     protected static boolean isStaticProxy(String host){
>         return PROXY_DEFINED && !isNonProxy(host);
>     }
> +
> +    /**
> +     * In proxy record mode, remove port to host header value
> +     *
> +     * @param hostHeader
> +     * @param port to remove
> +     * @return host value without ":port"
> +     */
> +    protected static String removePortInHostHeader(String hostHeader, int port) {
> +        String rePort = ":" + String.valueOf(port); // $NON-NLS-1$
> +        if (hostHeader.trim().endsWith(rePort)) {
> +            return hostHeader.replaceFirst(rePort + "$", ""); // $NON-NLS-1$ $NON-NLS-2$
> +        }
> +        return hostHeader;
> +    }
>  }
>


I'm not sure the changes above will always work.
Users are allowed to specify the Host in a Header Manager, in which
case the port might not equal the URL port.

The HTTP header is allowed to specify a port (indeed is required if
not the default), but the virtual host must not include the port.
So maybe it would just be simpler to strip colon followed by any digit
string from the end of the Host value.

i.e. the call to removePortInHostHeader would become:

v = v.hostHeader.replaceFirst(":\d+$",""); // remove any port specification

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: dev-help@jakarta.apache.org