001: package com.meterware.pseudoserver;
002:
003: /********************************************************************************************************************
004: * $Id: HttpRequest.java,v 1.9 2006/03/24 20:19:23 russgold Exp $
005: *
006: * Copyright (c) 2001-2004, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import com.meterware.httpunit.HttpUnitUtils;
024:
025: import java.io.InputStream;
026: import java.io.IOException;
027:
028: import java.util.StringTokenizer;
029: import java.util.Hashtable;
030:
031: /**
032: * Represents a single HTTP request, extracted from the input stream.
033: *
034: * @since 1.6
035: */
036: public class HttpRequest extends ReceivedHttpMessage {
037:
038: private String _protocol;
039: private String _command;
040: private String _uri;
041: private Hashtable _parameters;
042:
043: HttpRequest(InputStream inputStream) throws IOException {
044: super (inputStream);
045: }
046:
047: void interpretMessageHeader(String messageHeader) {
048: StringTokenizer st = new StringTokenizer(messageHeader);
049: _command = st.nextToken();
050: _uri = st.nextToken();
051: _protocol = st.nextToken();
052: }
053:
054: void appendMessageHeader(StringBuffer sb) {
055: sb.append(_command).append(' ').append(_uri).append(' ')
056: .append(_protocol);
057: }
058:
059: /**
060: * Returns the command associated with this request.
061: */
062: public String getCommand() {
063: return _command;
064: }
065:
066: /**
067: * Returns the URI specified in the message header for this request.
068: */
069: public String getURI() {
070: return _uri;
071: }
072:
073: /**
074: * Returns the protocol string specified in the message header for this request.
075: */
076: public String getProtocol() {
077: return _protocol;
078: }
079:
080: /**
081: * Returns the parameter with the specified name. If no such parameter exists, will
082: * return null.
083: **/
084: public String[] getParameter(String name) {
085: if (_parameters == null) {
086: if (_command.equalsIgnoreCase("GET")
087: || _command.equalsIgnoreCase("HEAD")) {
088: _parameters = readParameters(getParameterString(_uri));
089: } else {
090: _parameters = readParameters(new String(getBody()));
091: }
092: }
093: return (String[]) _parameters.get(name);
094: }
095:
096: private String getParameterString(String uri) {
097: return uri.indexOf('?') < 0 ? "" : uri.substring(uri
098: .indexOf('?') + 1);
099: }
100:
101: boolean wantsKeepAlive() {
102: if ("Keep-alive".equalsIgnoreCase(getConnectionHeader())) {
103: return true;
104: } else if (_protocol.equals("HTTP/1.1")) {
105: return !"Close".equalsIgnoreCase(getConnectionHeader());
106: } else {
107: return false;
108: }
109: }
110:
111: private Hashtable readParameters(String content) {
112: Hashtable parameters = new Hashtable();
113: if (content == null || content.trim().length() == 0)
114: return parameters;
115:
116: StringTokenizer st = new StringTokenizer(content, "&=");
117: while (st.hasMoreTokens()) {
118: String name = st.nextToken();
119: if (st.hasMoreTokens()) {
120: addParameter(parameters, HttpUnitUtils.decode(name),
121: HttpUnitUtils.decode(st.nextToken()));
122: }
123: }
124: return parameters;
125: }
126:
127: private void addParameter(Hashtable parameters, String name,
128: String value) {
129: String[] oldValues = (String[]) parameters.get(name);
130: if (oldValues == null) {
131: parameters.put(name, new String[] { value });
132: } else {
133: String[] values = new String[oldValues.length + 1];
134: System.arraycopy(oldValues, 0, values, 0, oldValues.length);
135: values[oldValues.length] = value;
136: parameters.put(name, values);
137: }
138: }
139:
140: private String getConnectionHeader() {
141: return getHeader("Connection");
142: }
143:
144: }
|