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

(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/proxy/HttpRequestHdr.java (-24 / +47 lines)
Lines 233-249 Link Here
233
		return ""; // $NON-NLS-1$
233
		return ""; // $NON-NLS-1$
234
	}
234
	}
235
235
236
	private MultipartUrlConfig isMultipart(String contentType) {
236
    private boolean isMultipart(String contentType) {
237
		if (contentType != null && contentType.startsWith(MultipartUrlConfig.MULTIPART_FORM)) {
237
        if (contentType != null && contentType.startsWith(HTTPSamplerBase.MULTIPART_FORM_DATA)) {
238
			return new MultipartUrlConfig(contentType.substring(contentType.indexOf("oundary=") + 8));
238
            return true;
239
		} else {
239
        } else {
240
			return null;
240
            return false;
241
		}
241
        }
242
	}
242
    }
243
243
244
    private MultipartUrlConfig getMultipartConfig(String contentType) {
245
        if(isMultipart(contentType)) {
246
            // Get the boundary string for the multiparts from the content type
247
            String boundaryString = contentType.substring(contentType.toLowerCase().indexOf("boundary=") + "boundary=".length());
248
            return new MultipartUrlConfig(boundaryString);
249
        }
250
        else {
251
            return null;
252
        }
253
    }
254
244
	private void populateSampler() {
255
	private void populateSampler() {
245
		MultipartUrlConfig urlConfig = null;
246
		
247
		sampler.setDomain(serverName());
256
		sampler.setDomain(serverName());
248
        if (log.isDebugEnabled())
257
        if (log.isDebugEnabled())
249
    		log.debug("Proxy: setting server: " + sampler.getDomain());
258
    		log.debug("Proxy: setting server: " + sampler.getDomain());
Lines 275-301 Link Here
275
    			log.debug("Proxy setting default protocol to: http");
284
    			log.debug("Proxy setting default protocol to: http");
276
			sampler.setProtocol(HTTP);
285
			sampler.setProtocol(HTTP);
277
		}
286
		}
278
		if ((urlConfig = isMultipart(getContentType())) != null) {
287
279
			urlConfig.parseArguments(postData);
288
        // Check if it was a multipart http post request
280
			// If no file is uploaded, then it was really a multipart/form-data
289
        String contentType = getContentType();
281
			// post request. But currently, that is not supported, so we must
290
        MultipartUrlConfig urlConfig = getMultipartConfig(contentType);
282
			// change the "Content-Type" header from multipart/form-data to
291
        if (urlConfig != null) {
283
			// application/x-www-form-urlencoded, which is the one the HTTP Request
292
            urlConfig.parseArguments(postData);
284
			// sampler will send
293
            // Tell the sampler to do a multipart post
285
			if(urlConfig.getFilename() == null) {
294
            sampler.setDoMultipartPost(true);
286
				System.out.println("jada");
295
            // Remove the header for content-type and content-length, since
287
				getHeaderManager().removeHeaderNamed("Content-Type");
296
            // those values will most likely be incorrect when the sampler
288
				getHeaderManager().add(new Header("Content-Type", "application/x-www-form-urlencoded"));
297
            // performs the multipart request, because the boundary string
289
			}
298
            // will change
290
			sampler.setArguments(urlConfig.getArguments());
299
            getHeaderManager().removeHeaderNamed(CONTENT_TYPE);
300
            getHeaderManager().removeHeaderNamed(CONTENT_LENGTH);
301
            
302
            // Set the form data
303
            sampler.setArguments(urlConfig.getArguments());
304
            // Set the file uploads
291
			sampler.setFileField(urlConfig.getFileFieldName());
305
			sampler.setFileField(urlConfig.getFileFieldName());
292
			sampler.setFilename(urlConfig.getFilename());
306
			sampler.setFilename(urlConfig.getFilename());
293
			sampler.setMimetype(urlConfig.getMimeType());
307
			sampler.setMimetype(urlConfig.getMimeType());
294
        } else if (postData != null && postData.trim().startsWith("<?")) {
308
        } else if (postData != null && postData.trim().startsWith("<?")) {
309
            // Not sure if this is needed anymore. I assume these requests
310
            // do not have HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED as content type,
311
            // and they would therefore be catched by the last else if of these if else if tests
295
            sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
312
            sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
296
		} else {
313
		} else if (contentType == null || contentType.equals(HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED) ){
314
            // It is the most common post request, with parameter name and values
315
            // We also assume this if no content type is present, to be most backwards compatible,
316
            // but maybe we should only parse arguments if the content type is as expected            
297
			sampler.parseArguments(postData); //standard name=value postData
317
			sampler.parseArguments(postData); //standard name=value postData
298
		}
318
		} else if (postData != null) {
319
            // Just put the whole postbody as the value of a parameter
320
            sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
321
        }
299
        if (log.isDebugEnabled())
322
        if (log.isDebugEnabled())
300
    		log.debug("sampler path = " + sampler.getPath());
323
    		log.debug("sampler path = " + sampler.getPath());
301
	}
324
	}
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java (-3 / +38 lines)
Lines 29-34 Link Here
29
import java.net.URLConnection;
29
import java.net.URLConnection;
30
import java.net.URLEncoder;
30
import java.net.URLEncoder;
31
31
32
import org.apache.commons.httpclient.Header;
32
import org.apache.jmeter.protocol.http.util.HTTPArgument;
33
import org.apache.jmeter.protocol.http.util.HTTPArgument;
33
import org.apache.jmeter.testelement.property.PropertyIterator;
34
import org.apache.jmeter.testelement.property.PropertyIterator;
34
35
Lines 220-239 Link Here
220
            connection.setDoInput(true);
221
            connection.setDoInput(true);
221
        }
222
        }
222
        else {
223
        else {
223
            // Set the content type
224
            // Check if the header manager had a content type header
224
            connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED);
225
            // This allows the user to specify his own content-type for a POST request
226
            String contentTypeHeader = connection.getRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE);
227
            boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.length() > 0; 
225
            
228
            
226
            // If there are no arguments, we can send a file as the body of the request
229
            // If there are no arguments, we can send a file as the body of the request
227
            if(sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) {
230
            if(sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) {
231
                if(!hasContentTypeHeader) {
232
                    // Allow the mimetype of the file to control the content type
233
                    if(sampler.getMimetype() != null && sampler.getMimetype().length() > 0) {
234
                        connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, sampler.getMimetype());
235
                    }
236
                    else {
237
                        connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED);
238
                    }
239
                }
240
                
228
                // Create the content length we are going to write
241
                // Create the content length we are going to write
229
                File inputFile = new File(sampler.getFilename());
242
                File inputFile = new File(sampler.getFilename());
230
                contentLength = inputFile.length();
243
                contentLength = inputFile.length();
231
            }
244
            }
232
            else {
245
            else {
246
                // Set the content type
247
                if(!hasContentTypeHeader) {
248
                    connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED);
249
                }
250
                
233
                // We create the post body content now, so we know the size
251
                // We create the post body content now, so we know the size
234
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
252
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
235
                
253
                
236
                String postBody = getQueryStringForPostBody(sampler, contentEncoding);
254
                // If none of the arguments have a name specified, we
255
                // just send all the values as the post body
256
                String postBody = null;
257
                if(!sampler.getSendParameterValuesAsPostBody()) {
258
                    // It is a normal post request, with parameter names and values
259
                    postBody = getQueryStringForPostBody(sampler, contentEncoding);
260
                }
261
                else {
262
                    // Just append all the parameter values, and use that as the post body
263
                    StringBuffer postBodyBuffer = new StringBuffer();
264
                    PropertyIterator args = sampler.getArguments().iterator();
265
                    while (args.hasNext()) {
266
                        HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
267
                        postBodyBuffer.append(arg.getValue());
268
                    }
269
                    postBody = postBodyBuffer.toString();
270
                }
271
237
                // Query string should be encoded in UTF-8
272
                // Query string should be encoded in UTF-8
238
                bos.write(postBody.getBytes("UTF-8")); // $NON-NLS-1$
273
                bos.write(postBody.getBytes("UTF-8")); // $NON-NLS-1$
239
                bos.flush();
274
                bos.flush();
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java (-9 / +42 lines)
Lines 54-59 Link Here
54
import org.apache.commons.httpclient.methods.PostMethod;
54
import org.apache.commons.httpclient.methods.PostMethod;
55
import org.apache.commons.httpclient.methods.PutMethod;
55
import org.apache.commons.httpclient.methods.PutMethod;
56
import org.apache.commons.httpclient.methods.RequestEntity;
56
import org.apache.commons.httpclient.methods.RequestEntity;
57
import org.apache.commons.httpclient.methods.StringRequestEntity;
57
import org.apache.commons.httpclient.methods.TraceMethod;
58
import org.apache.commons.httpclient.methods.TraceMethod;
58
import org.apache.commons.httpclient.methods.multipart.FilePart;
59
import org.apache.commons.httpclient.methods.multipart.FilePart;
59
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
60
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
Lines 313-323 Link Here
313
            }
314
            }
314
        }
315
        }
315
        else {
316
        else {
316
            // Set the content type
317
            // Check if the header manager had a content type header
317
            post.setRequestHeader(HEADER_CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
318
            // This allows the user to specify his own content-type for a POST request
319
            Header contentTypeHeader = post.getRequestHeader(HEADER_CONTENT_TYPE);
320
            boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.getValue() != null && contentTypeHeader.getValue().length() > 0; 
318
321
319
            // If there are no arguments, we can send a file as the body of the request
322
            // If there are no arguments, we can send a file as the body of the request
320
            if(getArguments().getArgumentCount() == 0 && getSendFileAsPostBody()) {
323
            if(getArguments().getArgumentCount() == 0 && getSendFileAsPostBody()) {
324
                if(!hasContentTypeHeader) {
325
                    // Allow the mimetype of the file to control the content type
326
                    if(getMimetype() != null && getMimetype().length() > 0) {
327
                        post.setRequestHeader(HEADER_CONTENT_TYPE, getMimetype());
328
                    }
329
                    else {
330
                        post.setRequestHeader(HEADER_CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
331
                    }
332
                }
333
                
321
                FileRequestEntity fileRequestEntity = new FileRequestEntity(new File(getFilename()),null); 
334
                FileRequestEntity fileRequestEntity = new FileRequestEntity(new File(getFilename()),null); 
322
                post.setRequestEntity(fileRequestEntity);
335
                post.setRequestEntity(fileRequestEntity);
323
                
336
                
Lines 327-347 Link Here
327
            else {            
340
            else {            
328
                // In an application/x-www-form-urlencoded request, we only support
341
                // In an application/x-www-form-urlencoded request, we only support
329
                // parameters, no file upload is allowed
342
                // parameters, no file upload is allowed
330
            
343
                if(!hasContentTypeHeader) {
344
                    // Set the content type
345
                    post.setRequestHeader(HEADER_CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
346
                }
347
331
                // If a content encoding is specified, we set it as http parameter, so that
348
                // If a content encoding is specified, we set it as http parameter, so that
332
                // the post body will be encoded in the specified content encoding
349
                // the post body will be encoded in the specified content encoding
333
                final String contentEncoding = getContentEncoding();
350
                final String contentEncoding = getContentEncoding();
334
                if(contentEncoding != null && contentEncoding.trim().length() > 0) {
351
                if(contentEncoding != null && contentEncoding.trim().length() > 0) {
335
                    post.getParams().setContentCharset(contentEncoding);
352
                    post.getParams().setContentCharset(contentEncoding);
336
                }
353
                }
337
            
354
                
338
                PropertyIterator args = getArguments().iterator();
355
                // If none of the arguments have a name specified, we
339
                while (args.hasNext()) {
356
                // just send all the values as the post body
340
                    HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
357
                if(!getSendParameterValuesAsPostBody()) {
341
                    post.addParameter(arg.getName(), arg.getValue());
358
                    // It is a normal post request, with parameter names and values
359
                    PropertyIterator args = getArguments().iterator();
360
                    while (args.hasNext()) {
361
                        HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
362
                        post.addParameter(arg.getName(), arg.getValue());
363
                    }
342
                }
364
                }
365
                else {
366
                    // Just append all the parameter values, and use that as the post body
367
                    StringBuffer postBody = new StringBuffer();
368
                    PropertyIterator args = getArguments().iterator();
369
                    while (args.hasNext()) {
370
                        HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
371
                        postBody.append(arg.getValue());
372
                    }
373
                    StringRequestEntity requestEntity = new StringRequestEntity(postBody.toString(), post.getRequestHeader(HEADER_CONTENT_TYPE).getValue(), post.getRequestCharSet());
374
                    post.setRequestEntity(requestEntity);
375
                }
343
                
376
                
344
                // If the Multipart is repeatable, we can send it first to
377
                // If the request entity is repeatable, we can send it first to
345
                // our own stream, so we can return it
378
                // our own stream, so we can return it
346
                if(post.getRequestEntity().isRepeatable()) {
379
                if(post.getRequestEntity().isRepeatable()) {
347
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
380
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java (-5 / +26 lines)
Lines 207-215 Link Here
207
209
208
    protected static final String HEADER_LOCATION = "Location"; // $NON-NLS-1$
210
    protected static final String HEADER_LOCATION = "Location"; // $NON-NLS-1$
209
211
210
	protected static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; // $NON-NLS-1$
212
	public static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; // $NON-NLS-1$
211
    
213
    
212
    protected static final String MULTIPART_FORM_DATA = "multipart/form-data"; // $NON-NLS-1$
214
    public static final String MULTIPART_FORM_DATA = "multipart/form-data"; // $NON-NLS-1$
213
    
215
    
214
    // Derive the mapping of content types to parsers
216
    // Derive the mapping of content types to parsers
215
    private static Map parsersForType = new HashMap();
217
    private static Map parsersForType = new HashMap();
Lines 288-298 Link Here
288
	 * i.e. without any additional wrapping
290
	 * i.e. without any additional wrapping
289
	 * 
291
	 * 
290
	 * @return true if specified file is to be sent as the body,
292
	 * @return true if specified file is to be sent as the body,
291
	 * i.e. both FileField and MimeType are blank
293
	 * i.e. both FileField is blank
292
	 */
294
	 */
293
	public boolean getSendFileAsPostBody(){
295
	public boolean getSendFileAsPostBody() {
294
		return getFileField().length()== 0 && getMimetype().length() == 0;
296
        // If no file field is specified, the file is sent as post body
297
		return getFileField().length()== 0 && getFilename().length() > 0;
295
	}
298
	}
299
    
300
    /**
301
     * Determine if none of the parameters have a name, and if that
302
     * is the case, it means that the parameter values should be sent
303
     * as the post body
304
     * 
305
     * @return true if none of the parameters have a name specified
306
     */
307
    public boolean getSendParameterValuesAsPostBody() {
308
        boolean noArgumentsHasName = true;
309
        PropertyIterator args = getArguments().iterator();
310
        while (args.hasNext()) {
311
            HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
312
            if(arg.getName() != null && arg.getName().length() > 0) {
313
                noArgumentsHasName = false;
314
                break;
315
            }
316
        }
317
        return noArgumentsHasName;
318
    }
296
319
297
    /**
320
    /**
298
     * Determine if we should use multipart/form-data or 
321
     * Determine if we should use multipart/form-data or 
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/config/MultipartUrlConfig.java (-1 / +1 lines)
Lines 29-35 Link Here
29
 * @version $Revision$
29
 * @version $Revision$
30
 */
30
 */
31
public class MultipartUrlConfig implements Serializable {
31
public class MultipartUrlConfig implements Serializable {
32
32
    /** @deprecated use HTTPSamplerBase.MULTIPART_FORM_DATA instead */
33
	public static final String MULTIPART_FORM = "multipart/form-data";
33
	public static final String MULTIPART_FORM = "multipart/form-data";
34
34
35
	private String boundary, filename, fileField, mimetype;
35
	private String boundary, filename, fileField, mimetype;

Return to bug 41518