Source code: juju/reattore/protocol/http/impl/BaseHttpRequest.java
1 /* Reattore HTTP Server
2
3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 $Id: BaseHttpRequest.java,v 1.12 2003/02/22 04:29:52 michaelh Exp $
20 */
21
22 package juju.reattore.protocol.http.impl;
23
24 import java.util.*;
25
26 import juju.reattore.io.ByteSource;
27 import juju.reattore.protocol.http.*;
28
29 /** HTTP request implementation. Wraps a HTTP parser and provides the
30 request access methods. Also acts as a proxy, only resolving
31 certain fields as required.
32
33 @see HttpRequest
34 */
35 public class BaseHttpRequest
36 implements HttpRequest {
37
38 private Map headers = new HashMap();
39
40 private String method;
41 private String path;
42 private String query;
43 private String version;
44 private String originalPath;
45
46 private ByteSource body;
47
48 /** Receives a new header from the parser.
49
50 @param name Name of the header
51 @param val Value of
52 */
53 public void addHeader(String name, String val) {
54 headers.put(name.toLowerCase(), val);
55 }
56
57 /** Bulk populate this request with the given values.
58
59 @param method As named
60 @param path As named
61 @param query As named
62 @param version As named
63
64 */
65 public void setStartLine(String method, String path, String query, String version) {
66 this.method = method;
67 this.path = path;
68 this.originalPath = path;
69 this.query = query;
70 this.version = version;
71 }
72
73 /** @todo Not thread safe */
74 private static final IdentityHashMap TLC = new IdentityHashMap();
75
76 private String toLowerCase(String key) {
77 String ret;
78
79 if ((ret = (String)TLC.get(key)) != null) {
80 return ret;
81 }
82 else {
83 ret = key.toLowerCase();
84 TLC.put(key, ret);
85
86 return ret;
87 }
88 }
89
90 /** @see HttpMessage */
91 public String getHeader(String key) {
92 return (String)headers.get(toLowerCase(key));
93 }
94
95 /** Sets the body attached to this request.
96
97 @param body The new body.
98 */
99 public void setBody(ByteSource body) {
100 this.body = body;
101 }
102
103 /** @see HttpRequest */
104 public String getMethod() {
105 return method;
106 }
107
108 /** @see HttpRequest */
109 public String getPath() {
110 return path;
111 }
112
113 /** @see HttpRequest */
114 public String getQueryString() {
115 return query;
116 }
117
118 /** @see HttpRequest */
119 public String getVersion() {
120 return version;
121 }
122
123 /** @see HttpRequest */
124 public ByteSource getBody() {
125 return body;
126 }
127
128 /** @see HttpMessage */
129 public int getNumHeaders() {
130 return headers.size();
131 }
132
133 /** @see HttpRequest */
134 public String getOriginalPath() {
135 return originalPath;
136 }
137
138 /** @see HttpRequest */
139 public void changePath(String to) {
140 path = to;
141 }
142
143 /** @see HttpMessage */
144 public Set getHeaders() {
145 return headers.entrySet();
146 }
147 }
148