jsalvata    2004/01/22 17:27:09

  Modified:    src/protocol/http/org/apache/jmeter/protocol/http/control
                        AuthManager.java
  Log:
  Sometimes my brain just breaks...
  
  Revision  Changes    Path
  1.8       +49 -21    
jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/control/AuthManager.java
  
  Index: AuthManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/control/AuthManager.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AuthManager.java  23 Jan 2004 00:38:14 -0000      1.7
  +++ AuthManager.java  23 Jan 2004 01:27:09 -0000      1.8
  @@ -61,6 +61,7 @@
   import java.io.IOException;
   import java.io.PrintWriter;
   import java.io.Serializable;
  +import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.ArrayList;
   import java.util.Collection;
  @@ -188,35 +189,62 @@
   
       public String getAuthHeaderForURL(URL url)
       {
  -        if (isUnsupportedProtocol(url))
  +        if (! isSupportedProtocol(url))
           {
               return null;
           }
   
  -        StringBuffer header = new StringBuffer();
  -        for (PropertyIterator enum = getAuthObjects().iterator();
  -            enum.hasNext();
  -            )
  +             // TODO: replace all this url2 mess with a proper method 
"areEquivalent(url1, url2)" that
  +             // would also ignore case in protocol and host names, etc. -- use that 
method in the CookieManager too.
  +
  +             URL url2= null;
  +             
  +             try
           {
  -            Authorization auth = (Authorization) enum.next().getObjectValue();
  -            if (url.toString().startsWith(auth.getURL()))
  +            if (url.getPort() == -1)
               {
  -                header.append(
  -                    "Basic "
  -                        + Base64Encoder.encode(
  -                            auth.getUser() + ":" + auth.getPass()));
  -                break;
  +                // Obtain another URL with an explicit port:
  +                int port= url.getProtocol().equalsIgnoreCase("http") ? 80 : 443;
  +                // only http and https are supported
  +                url2=
  +                    new URL(
  +                        url.getProtocol(),
  +                        url.getHost(),
  +                        port,
  +                        url.getPath());
  +            }
  +            else if (
  +                (url.getPort() == 80
  +                    && url.getProtocol().equalsIgnoreCase("http"))
  +                    || (url.getPort() == 443
  +                        && url.getProtocol().equalsIgnoreCase("https")))
  +            {
  +                url2= new URL(url.getProtocol(), url.getHost(), url.getPath());
               }
           }
  -
  -        if (header.length() != 0)
  +        catch (MalformedURLException e)
           {
  -            return header.toString();
  +             log.error("Internal error!", e); // this should never happen
  +             // anyway, we'll continue with url2 set to null.
           }
  -        else
  +        
  +        String s1= url.toString();
  +        String s2= null;
  +        if (url2 != null) s2= url2.toString();
  +        
  +        for (PropertyIterator enum = getAuthObjects().iterator();
  +            enum.hasNext();
  +            )
           {
  -            return null;
  +            Authorization auth = (Authorization) enum.next().getObjectValue();
  +            
  +            if (s1.startsWith(auth.getURL()) || s2 != null && 
s2.startsWith(auth.getURL()))
  +            {
  +                return "Basic "
  +                     + Base64Encoder.encode(auth.getUser() + ":" + auth.getPass());
  +            }
           }
  +        return null;
       }
   
       public String getName()
  @@ -333,9 +361,9 @@
           return getAuthObjects().size();
       }
   
  -    private boolean isUnsupportedProtocol(URL url)
  +    private boolean isSupportedProtocol(URL url)
       {
  -        return !url.getProtocol().toUpperCase().equals("HTTP")
  -            && !url.getProtocol().toUpperCase().equals("HTTPS");
  +        return url.getProtocol().toUpperCase().equals("HTTP")
  +            || url.getProtocol().toUpperCase().equals("HTTPS");
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to