View | Details | Raw Unified | Return to bug 55175
Collapse All | Expand All

(-)src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java (-58 / +88 lines)
Lines 224-235 Link Here
224
    protected HTTPSampleResult sample(URL url, String method,
224
    protected HTTPSampleResult sample(URL url, String method,
225
            boolean areFollowingRedirect, int frameDepth) {
225
            boolean areFollowingRedirect, int frameDepth) {
226
226
227
        HTTPSampleResult res = new HTTPSampleResult();
227
        HTTPSampleResult res = createSampleResult(url, method);
228
        res.setMonitor(isMonitor());
229
230
        res.setSampleLabel(url.toString()); // May be replaced later
231
        res.setHTTPMethod(method);
232
        res.setURL(url);
233
228
234
        HttpClient httpClient = setupClient(url);
229
        HttpClient httpClient = setupClient(url);
235
        
230
        
Lines 279-292 Link Here
279
274
280
        try {
275
        try {
281
            currentRequest = httpRequest;
276
            currentRequest = httpRequest;
282
            // Handle the various methods
277
            handleMethod(method, res, httpRequest, localContext);
283
            if (method.equals(HTTPConstants.POST)) {
284
                String postBody = sendPostData((HttpPost)httpRequest);
285
                res.setQueryString(postBody);
286
            } else if (method.equals(HTTPConstants.PUT) || method.equals(HTTPConstants.PATCH)) {
287
                String entityBody = sendEntityData(( HttpEntityEnclosingRequestBase)httpRequest);
288
                res.setQueryString(entityBody);
289
            }
290
            HttpResponse httpResponse = httpClient.execute(httpRequest, localContext); // perform the sample
278
            HttpResponse httpResponse = httpClient.execute(httpRequest, localContext); // perform the sample
291
279
292
            // Needs to be done after execute to pick up all the headers
280
            // Needs to be done after execute to pick up all the headers
Lines 378-383 Link Here
378
    }
366
    }
379
367
380
    /**
368
    /**
369
     * @param method String HTTP method
370
     * @param result {@link HTTPSampleResult}
371
     * @param httpRequest {@link HttpRequestBase}
372
     * @param localContext {@link HttpContext}
373
     * @throws IOException
374
     */
375
    protected void handleMethod(String method, HTTPSampleResult result,
376
            HttpRequestBase httpRequest, HttpContext localContext) throws IOException {
377
        // Handle the various methods
378
        if (method.equals(HTTPConstants.POST)) {
379
            String postBody = sendPostData((HttpPost)httpRequest);
380
            result.setQueryString(postBody);
381
        } else if (method.equals(HTTPConstants.PUT) || method.equals(HTTPConstants.PATCH)) {
382
            String entityBody = sendEntityData(( HttpEntityEnclosingRequestBase)httpRequest);
383
            result.setQueryString(entityBody);
384
        }
385
    }
386
387
    /**
388
     * Create HTTPSampleResult
389
     * @param url URL
390
     * @param method HTTP Method
391
     * @return {@link HTTPSampleResult}
392
     */
393
    protected HTTPSampleResult createSampleResult(URL url, String method) {
394
        HTTPSampleResult res = new HTTPSampleResult();
395
        res.setMonitor(isMonitor());
396
397
        res.setSampleLabel(url.toString()); // May be replaced later
398
        res.setHTTPMethod(method);
399
        res.setURL(url);
400
        
401
        return res;
402
    }
403
404
    /**
381
     * Holder class for all fields that define an HttpClient instance;
405
     * Holder class for all fields that define an HttpClient instance;
382
     * used as the key to the ThreadLocal map of HttpClient instances.
406
     * used as the key to the ThreadLocal map of HttpClient instances.
383
     */
407
     */
Lines 558-611 Link Here
558
        return httpClient;
582
        return httpClient;
559
    }
583
    }
560
584
561
    private void setupRequest(URL url, HttpRequestBase httpRequest, HTTPSampleResult res)
585
    protected void setupRequest(URL url, HttpRequestBase httpRequest, HTTPSampleResult res)
562
        throws IOException {
586
        throws IOException {
563
587
564
    HttpParams requestParams = httpRequest.getParams();
588
        HttpParams requestParams = httpRequest.getParams();
589
        
590
        // Set up the local address if one exists
591
        final String ipSource = getIpSource();
592
        if (ipSource.length() > 0) {// Use special field ip source address (for pseudo 'ip spoofing')
593
            InetAddress inetAddr = InetAddress.getByName(ipSource);
594
            requestParams.setParameter(ConnRoutePNames.LOCAL_ADDRESS, inetAddr);
595
        } else if (localAddress != null){
596
            requestParams.setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
597
        } else { // reset in case was set previously
598
            requestParams.removeParameter(ConnRoutePNames.LOCAL_ADDRESS);
599
        }
565
    
600
    
566
    // Set up the local address if one exists
601
        int rto = getResponseTimeout();
567
    final String ipSource = getIpSource();
602
        if (rto > 0){
568
    if (ipSource.length() > 0) {// Use special field ip source address (for pseudo 'ip spoofing')
603
            requestParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, rto);
569
        InetAddress inetAddr = InetAddress.getByName(ipSource);
604
        }
570
        requestParams.setParameter(ConnRoutePNames.LOCAL_ADDRESS, inetAddr);
571
    } else if (localAddress != null){
572
        requestParams.setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
573
    } else { // reset in case was set previously
574
        requestParams.removeParameter(ConnRoutePNames.LOCAL_ADDRESS);
575
    }
576
577
    int rto = getResponseTimeout();
578
    if (rto > 0){
579
        requestParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, rto);
580
    }
581
582
    int cto = getConnectTimeout();
583
    if (cto > 0){
584
        requestParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, cto);
585
    }
586
587
    requestParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, getAutoRedirects());
588
    
605
    
589
    // a well-behaved browser is supposed to send 'Connection: close'
606
        int cto = getConnectTimeout();
590
    // with the last request to an HTTP server. Instead, most browsers
607
        if (cto > 0){
591
    // leave it to the server to close the connection after their
608
            requestParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, cto);
592
    // timeout period. Leave it to the JMeter user to decide.
609
        }
593
    if (getUseKeepAlive()) {
610
    
594
        httpRequest.setHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.KEEP_ALIVE);
611
        requestParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, getAutoRedirects());
595
    } else {
612
        
596
        httpRequest.setHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.CONNECTION_CLOSE);
613
        // a well-behaved browser is supposed to send 'Connection: close'
597
    }
614
        // with the last request to an HTTP server. Instead, most browsers
598
615
        // leave it to the server to close the connection after their
599
    setConnectionHeaders(httpRequest, url, getHeaderManager(), getCacheManager());
616
        // timeout period. Leave it to the JMeter user to decide.
600
617
        if (getUseKeepAlive()) {
601
    String cookies = setConnectionCookie(httpRequest, url, getCookieManager());
618
            httpRequest.setHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.KEEP_ALIVE);
602
619
        } else {
603
    if (res != null) {
620
            httpRequest.setHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.CONNECTION_CLOSE);
604
        res.setCookies(cookies);
621
        }
622
    
623
        setConnectionHeaders(httpRequest, url, getHeaderManager(), getCacheManager());
624
    
625
        String cookies = setConnectionCookie(httpRequest, url, getCookieManager());
626
    
627
        if (res != null) {
628
            res.setCookies(cookies);
629
        }
630
    
605
    }
631
    }
606
632
607
}
608
609
    
633
    
610
    /**
634
    /**
611
     * Set any default request headers to include
635
     * Set any default request headers to include
Lines 790-796 Link Here
790
    }
814
    }
791
815
792
    // TODO needs cleaning up
816
    // TODO needs cleaning up
793
    private String sendPostData(HttpPost post)  throws IOException {
817
    /**
818
     * 
819
     * @param post {@link HttpPost}
820
     * @return String posted body if computable
821
     * @throws IOException
822
     */
823
    protected String sendPostData(HttpPost post)  throws IOException {
794
        // Buffer to hold the post body, except file content
824
        // Buffer to hold the post body, except file content
795
        StringBuilder postedBody = new StringBuilder(1000);
825
        StringBuilder postedBody = new StringBuilder(1000);
796
        HTTPFileArg files[] = getHTTPFiles();
826
        HTTPFileArg files[] = getHTTPFiles();
Lines 1008-1014 Link Here
1008
     * @throws  UnsupportedEncodingException for invalid charset name
1038
     * @throws  UnsupportedEncodingException for invalid charset name
1009
     * @throws IOException cannot really occur for ByteArrayOutputStream methods
1039
     * @throws IOException cannot really occur for ByteArrayOutputStream methods
1010
     */
1040
     */
1011
    private String sendEntityData( HttpEntityEnclosingRequestBase entity) throws IOException {
1041
    protected String sendEntityData( HttpEntityEnclosingRequestBase entity) throws IOException {
1012
        // Buffer to hold the entity body
1042
        // Buffer to hold the entity body
1013
        StringBuilder entityBody = new StringBuilder(1000);
1043
        StringBuilder entityBody = new StringBuilder(1000);
1014
        boolean hasEntityBody = false;
1044
        boolean hasEntityBody = false;

Return to bug 55175