jboutcher    2002/12/01 19:00:26

  Modified:    src/protocol/http/org/apache/jmeter/protocol/http/sampler
                        HTTPSampler.java
  Log:
  fixes for bugs 14852 and ability to perform several redirects (up to 10)
  
  Revision  Changes    Path
  1.15      +56 -53    
jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
  
  Index: HTTPSampler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- HTTPSampler.java  21 Oct 2002 17:02:29 -0000      1.14
  +++ HTTPSampler.java  2 Dec 2002 03:00:26 -0000       1.15
  @@ -115,6 +115,7 @@
        public final static String CONTENT_TYPE = "HTTPSampler.CONTENT_TYPE";
        public final static String NORMAL_FORM = "normal_form";
        public final static String MULTIPART_FORM = "multipart_form";
  +     private static final int MAX_REDIRECTS=10;
        protected static String encoding = "iso-8859-1";
        private static final PostWriter postWriter = new PostWriter();
        transient protected HttpURLConnection conn;
  @@ -376,15 +377,15 @@
         *@param e  <code>Entry</code> to be sampled
         *@return   results of the sampling
         *@see      
org.apache.jmeter.protocol.http.sampler.HTTPSampler.sample(org.apache.jmeter.samplers.Entry,
  -      *      boolean)
  +      *      int)
         ***************************************/
        public SampleResult sample(Entry e)
        {
  -             return sample(false);
  +             return sample(0);
        }
        public SampleResult sample()
        {
  -             return sample(false);
  +             return sample(0);
        }
        /**
         *  !ToDoo (Method description)
  @@ -462,6 +463,18 @@
                }
                return buf.toString();
        }
  +
  +     /****************************************
  +      * Set request headers in preparation to opening a connection
  +      *
  +      *@param connection       <code>URLConnection</code> to set headers on
  +      *@exception IOException  if an I/O exception occurs
  +      ***************************************/
  +     public void setPostHeaders(URLConnection conn) throws IOException
  +     {
  +             postWriter.setHeaders(conn, this);
  +     }
  +
        /****************************************
         * Send POST data from <code>Entry</code> to the open connection.
         *
  @@ -474,6 +487,7 @@
        {
                postWriter.sendPostData(connection, this);
        }
  +
        /****************************************
         * Returns a <code>HttpURLConnection</code> with request method(GET or POST),
         * headers, cookies, authorization properly set for the URL request
  @@ -487,6 +501,16 @@
                throws IOException
        {
                HttpURLConnection conn;
  +             // [Jordi <[EMAIL PROTECTED]>]
  +             // I've not been able to find out why we're not using this
  +             // feature of HttpURLConnections and we're doing redirection
  +             // by hand instead. Everything would be so much simpler...
  +             // [/Jordi]
  +             // Mike: answer - it didn't work.  Maybe in JDK1.4 it works, but 
honestly,
  +             // it doesn't seem like they're working on this.
  +             // My longer term plan is to use Apache's home grown HTTP Client, or
  +             // maybe even HTTPUnit's classes.  I'm sure both would be better than 
Sun's
  +             HttpURLConnection.setFollowRedirects(false);
                conn = (HttpURLConnection) u.openConnection();
                // delegate SSL specific stuff to SSLManager so that compilation still 
works otherwise.
                if ("https".equals(u.getProtocol()))
  @@ -502,16 +526,6 @@
                                        e);
                        }
                }
  -             // [Jordi <[EMAIL PROTECTED]>]
  -             // I've not been able to find out why we're not using this
  -             // feature of HttpURLConnections and we're doing redirection
  -             // by hand instead. Everything would be so much simpler...
  -             // [/Jordi]
  -             // Mike: answer - it didn't work.  Maybe in JDK1.4 it works, but 
honestly,
  -             // it doesn't seem like they're working on this.
  -             // My longer term plan is to use Apache's home grown HTTP Client, or
  -             // maybe even HTTPUnit's classes.  I'm sure both would be better than 
Sun's
  -             conn.setFollowRedirects(false);
                // a well-bahaved browser is supposed to send 'Connection: close'
                // with the last request to an HTTP server. Instead, most browsers
                // leave it to the server to close the connection after their
  @@ -543,9 +557,9 @@
                {
                        // need to handle four cases:   string contains name=value
                        //                              string contains name=
  -                     //                              string contains name 
  +                     //                              string contains name
                        //                              empty string
  -                     // find end of parameter name 
  +                     // find end of parameter name
                        int endOfNameIndex = 0;
                        String metaData = ""; // records the existance of an equal sign
                        if (args[i].indexOf("=") != -1)
  @@ -802,17 +816,19 @@
                }
                URL newUrl = new URL(loc);
                setMethod(GET);
  +             setProtocol(newUrl.getProtocol());
                setDomain(newUrl.getHost());
  +             setPort(newUrl.getPort());
                setPath(newUrl.getFile());
                removeArguments();
                parseArguments(newUrl.getQuery());
        }
  -     
  +
        protected long connect() throws IOException
        {
                long time = System.currentTimeMillis();
                try
  -             {                               
  +             {
                        conn.connect();
                }
                catch(BindException e)
  @@ -831,7 +847,7 @@
                        conn = setupConnection(getUrl(),getMethod());
                        if(getMethod().equals(HTTPSampler.POST))
                        {
  -                             postWriter.setHeaders(conn,this);
  +                             setPostHeaders(conn);
                        }
                        time = connect();
                }
  @@ -846,29 +862,21 @@
                }
                return time;
        }
  -     
  +
        /****************************************
         * Samples <code>Entry</code> passed in and stores the result in 
<code>SampleResult</code>
         *
         *@param e           <code>Entry</code> to be sampled
  -      *@param redirected  whether we're processing a redirect
  +      *@param redirects   the level of redirection we're processing (0 means
  +      *                      original request) -- just used to prevent
  +      *                      an infinite loop.
         *@return            results of the sampling
         ***************************************/
  -     private SampleResult sample(boolean redirected)
  +     private SampleResult sample(int redirects)
        {
                log.debug("Start : sample2");
                long time = System.currentTimeMillis();
                SampleResult res = new SampleResult();
  -             if (redirected)
  -             {
  -                     //url.removeArguments();
  -                     // [Jordi <[EMAIL PROTECTED]>
  -                     // TO-DO: I need to investigate why this is necessary.
  -                     // ...although it won't do any harm...
  -                     // [/Jordi]
  -                     // Mike: arguments will be sent otherwise, which is not the 
way a browser
  -                     // behaves.  That's not to say it's perfect as is...
  -             }
                URL u = null;
                try
                {
  @@ -884,23 +892,13 @@
                                log.debug("sample2 : sampling url - " + u);
                        }
                        conn = setupConnection(u, getMethod());
  -                     // [Jordi <[EMAIL PROTECTED]>]
  -                     // There's some illegality here... see my comment in 
sendPostData.
  -                     // Also, we don't seem to be including the time needed to send 
the POST
  -                     // data in the count... should we? - mike: good point, I 
changed it
  -                     // TO-DO: Is there something I'm missing here?
  -                     // [/Jordi]                     
  -                     if (!redirected
  -                             && 
getProperty(HTTPSampler.METHOD).equals(HTTPSampler.POST))
  -                     {
  -                             postWriter.setHeaders(conn,this);
  -                     }
  -                     time = connect();
  -                     if (!redirected
  -                             && 
getProperty(HTTPSampler.METHOD).equals(HTTPSampler.POST))
  +                     if (getProperty(HTTPSampler.METHOD).equals(HTTPSampler.POST))
                        {
  +                             setPostHeaders(conn);
  +                             time = connect();
                                sendPostData(conn);
                        }
  +                     else time = connect();
                        saveConnectionCookies(conn, u, getCookieManager());
                        int errorLevel = 0;
                        try
  @@ -920,7 +918,12 @@
                        }
                        else if (errorLevel / 100 == 3)
                        {
  -                             if (redirected || !getFollowRedirects())
  +                             if (redirects >= MAX_REDIRECTS)
  +                             {
  +                                     throw new IOException("Maximum number of 
redirects exceeded");
  +                             }
  +
  +                             if (!getFollowRedirects())
                                {
                                        time = bundleResponseInResult(time, res, conn);
                                }
  @@ -928,13 +931,13 @@
                                {
                                        redirectUrl(conn, u);
                                        time = System.currentTimeMillis() - time;
  -                                     res = sample(true);
  +                                     res = sample(redirects+1);
                                        time += res.getTime();
                                }
                        }
                        else
                        {
  -                             // Could not sample the URL                            
                                 
  +                             // Could not sample the URL
                                time = bundleResponseInResult(time, res, conn);
                                res.setSuccessful(false);
                        }
  @@ -966,7 +969,7 @@
                        catch (Exception e)
                        {
                        }
  -                     
  +
                }
                log.debug("End : sample2");
                return res;
  @@ -984,8 +987,8 @@
                byte[] complete = new byte[ret.length + head.length];
                System.arraycopy(head, 0, complete, 0, head.length);
                System.arraycopy(ret, 0, complete, head.length, ret.length);
  -             res.setResponseData(complete);  
  -             res.setSuccessful(true);        
  +             res.setResponseData(complete);
  +             res.setSuccessful(true);
                return time;
        }
        /****************************************
  
  
  

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

Reply via email to