001 package org.shiftone.jrat.http;
002
003 import org.shiftone.jrat.util.log.Logger;
004
005 import java.io.BufferedReader;
006 import java.io.InputStream;
007 import java.io.InputStreamReader;
008 import java.util.HashMap;
009 import java.util.Map;
010
011 /**
012 * This object us mutable.
013 * I want to be able to modify a request and then redispatch it. I could have done that by
014 * wrapping the request in another implementation of a request, but that's a bit too complex for
015 * such a simple http server. The handler owns the request. If it wants to change the data and
016 * redispatch it, then it can, without any need to wrap it.
017 *
018 * @author jeff@shiftone.org (Jeff Drost)
019 */
020 public class Request {
021
022 private static final Logger LOG = Logger.getLogger(Request.class);
023
024 private String method;
025 private String requestUri;
026 private String httpVersion;
027 private String queryString;
028
029 private Map headers = new HashMap();
030
031 public Request(InputStream inputStream) throws Exception {
032
033 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
034 String line = reader.readLine();
035
036 parseRequestLine(line);
037
038 // this code is not expecially robust
039 while (((line = reader.readLine()) != null) && (line.length() > 0)) {
040 parseHeaderField(line);
041 }
042
043 }
044
045
046 private void parseRequestLine(String line) {
047
048 LOG.info(line);
049 int a = line.indexOf(' ');
050 int b = line.lastIndexOf(' ');
051
052 method = line.substring(0, a);
053 httpVersion = line.substring(b + 1);
054 requestUri = line.substring(a + 1, b);
055
056 // query string
057 int q = requestUri.indexOf('?');
058 if (q != -1) {
059 queryString = requestUri.substring(q + 1); // get just the query string
060 requestUri = requestUri.substring(0, q); // remove the query string and ?
061 }
062
063 }
064
065 private void parseHeaderField(String line) {
066 int a = line.indexOf(':');
067 String key = line.substring(0, a);
068 String value = line.substring(a + 2); // remove ": "
069 headers.put(key, value);
070 }
071
072
073 public String getMethod() {
074 return method;
075 }
076
077
078 public void setRequestUri(String requestUri) {
079 this.requestUri = requestUri;
080 }
081
082 public String getRequestUri() {
083 return requestUri;
084 }
085
086 public String getHttpVersion() {
087 return httpVersion;
088 }
089
090 public String getQueryString() {
091 return queryString;
092 }
093
094 public Map getHeaders() {
095 return headers;
096 }
097 }