001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.config;
020:
021: import java.io.Serializable;
022:
023: import org.apache.jmeter.config.Arguments;
024: import org.apache.jmeter.protocol.http.util.HTTPArgument;
025: import org.apache.jmeter.util.JMeterUtils;
026: import org.apache.jorphan.util.JOrphanUtils;
027: import org.apache.oro.text.regex.Pattern;
028: import org.apache.oro.text.regex.Perl5Compiler;
029: import org.apache.oro.text.regex.Perl5Matcher;
030:
031: /**
032: * @author Michael Stover
033: */
034: public class MultipartUrlConfig implements Serializable {
035:
036: private String boundary, filename, fileField, mimetype;
037:
038: private Arguments args;
039:
040: public MultipartUrlConfig() {
041: args = new Arguments();
042: }
043:
044: public MultipartUrlConfig(String boundary) {
045: this ();
046: this .boundary = boundary;
047: }
048:
049: public void setBoundary(String boundary) {
050: this .boundary = boundary;
051: }
052:
053: public String getBoundary() {
054: return boundary;
055: }
056:
057: public void setFilename(String filename) {
058: this .filename = filename;
059: }
060:
061: public String getFilename() {
062: return filename;
063: }
064:
065: public Arguments getArguments() {
066: return args;
067: }
068:
069: public void setFileFieldName(String name) {
070: this .fileField = name;
071: }
072:
073: public String getFileFieldName() {
074: return fileField;
075: }
076:
077: public void setMimeType(String type) {
078: mimetype = type;
079: }
080:
081: public String getMimeType() {
082: return mimetype;
083: }
084:
085: public void addArgument(String name, String value) {
086: Arguments myArgs = this .getArguments();
087: myArgs.addArgument(new HTTPArgument(name, value));
088: }
089:
090: public void addArgument(String name, String value, String metadata) {
091: Arguments myArgs = this .getArguments();
092: myArgs.addArgument(new HTTPArgument(name, value, metadata));
093: }
094:
095: // NOT USED
096: // /**
097: // * @deprecated values in a multipart/form-data are not urlencoded,
098: // * so it does not make sense to add a value as a encoded value
099: // */
100: // public void addEncodedArgument(String name, String value) {
101: // Arguments myArgs = getArguments();
102: // HTTPArgument arg = new HTTPArgument(name, value, true);
103: // if (arg.getName().equals(arg.getEncodedName()) && arg.getValue().equals(arg.getEncodedValue())) {
104: // arg.setAlwaysEncoded(false);
105: // }
106: // myArgs.addArgument(arg);
107: // }
108:
109: /**
110: * Add a value that is not URL encoded, and make sure it
111: * appears in the GUI that it will not be encoded when
112: * the request is sent.
113: *
114: * @param name
115: * @param value
116: */
117: private void addNonEncodedArgument(String name, String value) {
118: Arguments myArgs = getArguments();
119: // The value is not encoded
120: HTTPArgument arg = new HTTPArgument(name, value, false);
121: // Let the GUI show that it will not be encoded
122: arg.setAlwaysEncoded(false);
123: myArgs.addArgument(arg);
124: }
125:
126: /**
127: * This method allows a proxy server to send over the raw text from a
128: * browser's output stream to be parsed and stored correctly into the
129: * UrlConfig object.
130: */
131: public void parseArguments(String queryString) {
132: String[] parts = JOrphanUtils.split(queryString, "--"
133: + getBoundary());
134: for (int i = 0; i < parts.length; i++) {
135: String contentDisposition = getHeaderValue(
136: "Content-disposition", parts[i]);
137: String contentType = getHeaderValue("Content-type",
138: parts[i]);
139: // Check if it is form data
140: if (contentDisposition != null
141: && contentDisposition.indexOf("form-data") > -1) {
142: // Get the form field name
143: int index = contentDisposition.indexOf("name=\"") + 6;
144: String name = contentDisposition.substring(index,
145: contentDisposition.indexOf("\"", index));
146:
147: // Check if it is a file being uploaded
148: if (contentDisposition.indexOf("filename=") > -1) {
149: // Get the filename
150: index = contentDisposition.indexOf("filename=\"") + 10;
151: String fn = contentDisposition.substring(index,
152: contentDisposition.indexOf("\"", index));
153: if (fn != null && fn.length() > 0) {
154: // Set the values retrieves for the file upload
155: this .setFileFieldName(name);
156: this .setFilename(fn);
157: this .setMimeType(contentType);
158: }
159: } else {
160: // Find the first empty line of the multipart, it signals end of headers for multipart
161: int indexEmptyLfCrLfLinePos = parts[i]
162: .indexOf("\n\r\n");
163: int indexEmptyLfLfLinePos = parts[i]
164: .indexOf("\n\n");
165: String value = null;
166: if (indexEmptyLfCrLfLinePos > -1) {
167: value = parts[i].substring(
168: indexEmptyLfCrLfLinePos).trim();
169: } else if (indexEmptyLfLfLinePos > -1) {
170: value = parts[i].substring(
171: indexEmptyLfLfLinePos).trim();
172: }
173: this .addNonEncodedArgument(name, value);
174: }
175: }
176: }
177: }
178:
179: private String getHeaderValue(String headerName, String multiPart) {
180: String regularExpression = headerName + "\\s*:\\s*(.*)$";
181: Perl5Matcher localMatcher = JMeterUtils.getMatcher();
182: Pattern pattern = JMeterUtils.getPattern(regularExpression,
183: Perl5Compiler.READ_ONLY_MASK
184: | Perl5Compiler.CASE_INSENSITIVE_MASK
185: | Perl5Compiler.MULTILINE_MASK);
186: if (localMatcher.contains(multiPart, pattern)) {
187: return localMatcher.getMatch().group(1).trim();
188: } else {
189: return null;
190: }
191: }
192: }
|