2
|
1 |
/*
|
|
2 |
* Copyright 2002-2004 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
import java.io.*;
|
|
25 |
import java.nio.*;
|
|
26 |
import java.nio.channels.*;
|
|
27 |
import java.net.*;
|
|
28 |
import sun.net.www.MessageHeader;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* This class encapsulates a HTTP request received and a response to be
|
|
32 |
* generated in one transaction. It provides methods for examaining the
|
|
33 |
* request from the client, and for building and sending a reply.
|
|
34 |
*/
|
|
35 |
|
|
36 |
public class HttpTransaction {
|
|
37 |
|
|
38 |
String command;
|
|
39 |
URI requesturi;
|
|
40 |
HttpServer.Server server;
|
|
41 |
MessageHeader reqheaders, reqtrailers;
|
|
42 |
String reqbody;
|
|
43 |
byte[] rspbody;
|
|
44 |
MessageHeader rspheaders, rsptrailers;
|
|
45 |
SelectionKey key;
|
|
46 |
int rspbodylen;
|
|
47 |
boolean rspchunked;
|
|
48 |
|
|
49 |
HttpTransaction (HttpServer.Server server, String command,
|
|
50 |
URI requesturi, MessageHeader headers,
|
|
51 |
String body, MessageHeader trailers, SelectionKey key) {
|
|
52 |
this.command = command;
|
|
53 |
this.requesturi = requesturi;
|
|
54 |
this.reqheaders = headers;
|
|
55 |
this.reqbody = body;
|
|
56 |
this.reqtrailers = trailers;
|
|
57 |
this.key = key;
|
|
58 |
this.server = server;
|
|
59 |
}
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Get the value of a request header whose name is specified by the
|
|
63 |
* String argument.
|
|
64 |
*
|
|
65 |
* @param key the name of the request header
|
|
66 |
* @return the value of the header or null if it does not exist
|
|
67 |
*/
|
|
68 |
public String getRequestHeader (String key) {
|
|
69 |
return reqheaders.findValue (key);
|
|
70 |
}
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Get the value of a response header whose name is specified by the
|
|
74 |
* String argument.
|
|
75 |
*
|
|
76 |
* @param key the name of the response header
|
|
77 |
* @return the value of the header or null if it does not exist
|
|
78 |
*/
|
|
79 |
public String getResponseHeader (String key) {
|
|
80 |
return rspheaders.findValue (key);
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Get the request URI
|
|
85 |
*
|
|
86 |
* @return the request URI
|
|
87 |
*/
|
|
88 |
public URI getRequestURI () {
|
|
89 |
return requesturi;
|
|
90 |
}
|
|
91 |
|
|
92 |
public String toString () {
|
|
93 |
StringBuffer buf = new StringBuffer();
|
|
94 |
buf.append ("Request from: ").append (key.channel().toString()).append("\r\n");
|
|
95 |
buf.append ("Command: ").append (command).append("\r\n");
|
|
96 |
buf.append ("Request URI: ").append (requesturi).append("\r\n");
|
|
97 |
buf.append ("Headers: ").append("\r\n");
|
|
98 |
buf.append (reqheaders.toString()).append("\r\n");
|
|
99 |
buf.append ("Body: ").append (reqbody).append("\r\n");
|
|
100 |
buf.append ("---------Response-------\r\n");
|
|
101 |
buf.append ("Headers: ").append("\r\n");
|
|
102 |
if (rspheaders != null) {
|
|
103 |
buf.append (rspheaders.toString()).append("\r\n");
|
|
104 |
}
|
|
105 |
buf.append ("Body: ").append (new String(rspbody)).append("\r\n");
|
|
106 |
return new String (buf);
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Get the value of a request trailer whose name is specified by
|
|
111 |
* the String argument.
|
|
112 |
*
|
|
113 |
* @param key the name of the request trailer
|
|
114 |
* @return the value of the trailer or null if it does not exist
|
|
115 |
*/
|
|
116 |
public String getRequestTrailer (String key) {
|
|
117 |
return reqtrailers.findValue (key);
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Add a response header to the response. Multiple calls with the same
|
|
122 |
* key value result in multiple header lines with the same key identifier
|
|
123 |
* @param key the name of the request header to add
|
|
124 |
* @param val the value of the header
|
|
125 |
*/
|
|
126 |
public void addResponseHeader (String key, String val) {
|
|
127 |
if (rspheaders == null)
|
|
128 |
rspheaders = new MessageHeader ();
|
|
129 |
rspheaders.add (key, val);
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Set a response header. Searches for first header with named key
|
|
134 |
* and replaces its value with val
|
|
135 |
* @param key the name of the request header to add
|
|
136 |
* @param val the value of the header
|
|
137 |
*/
|
|
138 |
public void setResponseHeader (String key, String val) {
|
|
139 |
if (rspheaders == null)
|
|
140 |
rspheaders = new MessageHeader ();
|
|
141 |
rspheaders.set (key, val);
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Add a response trailer to the response. Multiple calls with the same
|
|
146 |
* key value result in multiple trailer lines with the same key identifier
|
|
147 |
* @param key the name of the request trailer to add
|
|
148 |
* @param val the value of the trailer
|
|
149 |
*/
|
|
150 |
public void addResponseTrailer (String key, String val) {
|
|
151 |
if (rsptrailers == null)
|
|
152 |
rsptrailers = new MessageHeader ();
|
|
153 |
rsptrailers.add (key, val);
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
|
157 |
* Get the request method
|
|
158 |
*
|
|
159 |
* @return the request method
|
|
160 |
*/
|
|
161 |
public String getRequestMethod (){
|
|
162 |
return command;
|
|
163 |
}
|
|
164 |
|
|
165 |
/**
|
|
166 |
* Perform an orderly close of the TCP connection associated with this
|
|
167 |
* request. This method guarantees that any response already sent will
|
|
168 |
* not be reset (by this end). The implementation does a shutdownOutput()
|
|
169 |
* of the TCP connection and for a period of time consumes and discards
|
|
170 |
* data received on the reading side of the connection. This happens
|
|
171 |
* in the background. After the period has expired the
|
|
172 |
* connection is completely closed.
|
|
173 |
*/
|
|
174 |
|
|
175 |
public void orderlyClose () {
|
|
176 |
try {
|
|
177 |
server.orderlyCloseChannel (key);
|
|
178 |
} catch (IOException e) {
|
|
179 |
System.out.println (e);
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
/**
|
|
184 |
* Do an immediate abortive close of the TCP connection associated
|
|
185 |
* with this request.
|
|
186 |
*/
|
|
187 |
public void abortiveClose () {
|
|
188 |
try {
|
|
189 |
server.abortiveCloseChannel(key);
|
|
190 |
} catch (IOException e) {
|
|
191 |
System.out.println (e);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* Get the SocketChannel associated with this request
|
|
197 |
*
|
|
198 |
* @return the socket channel
|
|
199 |
*/
|
|
200 |
public SocketChannel channel() {
|
|
201 |
return (SocketChannel) key.channel();
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Get the request entity body associated with this request
|
|
206 |
* as a single String.
|
|
207 |
*
|
|
208 |
* @return the entity body in one String
|
|
209 |
*/
|
|
210 |
public String getRequestEntityBody (){
|
|
211 |
return reqbody;
|
|
212 |
}
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Set the entity response body with the given string
|
|
216 |
* The content length is set to the length of the string
|
|
217 |
* @param body the string to send in the response
|
|
218 |
*/
|
|
219 |
public void setResponseEntityBody (String body){
|
|
220 |
rspbody = body.getBytes();
|
|
221 |
rspbodylen = body.length();
|
|
222 |
rspchunked = false;
|
|
223 |
addResponseHeader ("Content-length", Integer.toString (rspbodylen));
|
|
224 |
}
|
|
225 |
/**
|
|
226 |
* Set the entity response body with the given byte[]
|
|
227 |
* The content length is set to the gven length
|
|
228 |
* @param body the string to send in the response
|
|
229 |
*/
|
|
230 |
public void setResponseEntityBody (byte[] body, int len){
|
|
231 |
rspbody = body;
|
|
232 |
rspbodylen = len;
|
|
233 |
rspchunked = false;
|
|
234 |
addResponseHeader ("Content-length", Integer.toString (rspbodylen));
|
|
235 |
}
|
|
236 |
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Set the entity response body by reading the given inputstream
|
|
240 |
*
|
|
241 |
* @param is the inputstream from which to read the body
|
|
242 |
*/
|
|
243 |
public void setResponseEntityBody (InputStream is) throws IOException {
|
|
244 |
byte[] buf = new byte [2048];
|
|
245 |
byte[] total = new byte [2048];
|
|
246 |
int total_len = 2048;
|
|
247 |
int c, len=0;
|
|
248 |
while ((c=is.read (buf)) != -1) {
|
|
249 |
if (len+c > total_len) {
|
|
250 |
byte[] total1 = new byte [total_len * 2];
|
|
251 |
System.arraycopy (total, 0, total1, 0, len);
|
|
252 |
total = total1;
|
|
253 |
total_len = total_len * 2;
|
|
254 |
}
|
|
255 |
System.arraycopy (buf, 0, total, len, c);
|
|
256 |
len += c;
|
|
257 |
}
|
|
258 |
setResponseEntityBody (total, len);
|
|
259 |
}
|
|
260 |
|
|
261 |
/* chunked */
|
|
262 |
|
|
263 |
/**
|
|
264 |
* Set the entity response body with the given array of strings
|
|
265 |
* The content encoding is set to "chunked" and each array element
|
|
266 |
* is sent as one chunk.
|
|
267 |
* @param body the array of string chunks to send in the response
|
|
268 |
*/
|
|
269 |
public void setResponseEntityBody (String[] body) {
|
|
270 |
StringBuffer buf = new StringBuffer ();
|
|
271 |
int len = 0;
|
|
272 |
for (int i=0; i<body.length; i++) {
|
|
273 |
String chunklen = Integer.toHexString (body[i].length());
|
|
274 |
len += body[i].length();
|
|
275 |
buf.append (chunklen).append ("\r\n");
|
|
276 |
buf.append (body[i]).append ("\r\n");
|
|
277 |
}
|
|
278 |
buf.append ("0\r\n");
|
|
279 |
rspbody = new String (buf).getBytes();
|
|
280 |
rspbodylen = rspbody.length;
|
|
281 |
rspchunked = true;
|
|
282 |
addResponseHeader ("Transfer-encoding", "chunked");
|
|
283 |
}
|
|
284 |
|
|
285 |
/**
|
|
286 |
* Send the response with the current set of response parameters
|
|
287 |
* but using the response code and string tag line as specified
|
|
288 |
* @param rCode the response code to send
|
|
289 |
* @param rTag the response string to send with the response code
|
|
290 |
*/
|
|
291 |
public void sendResponse (int rCode, String rTag) throws IOException {
|
|
292 |
OutputStream os = new HttpServer.NioOutputStream(channel());
|
|
293 |
PrintStream ps = new PrintStream (os);
|
|
294 |
ps.print ("HTTP/1.1 " + rCode + " " + rTag + "\r\n");
|
|
295 |
if (rspheaders != null) {
|
|
296 |
rspheaders.print (ps);
|
|
297 |
} else {
|
|
298 |
ps.print ("\r\n");
|
|
299 |
}
|
|
300 |
ps.flush ();
|
|
301 |
if (rspbody != null) {
|
|
302 |
os.write (rspbody, 0, rspbodylen);
|
|
303 |
os.flush();
|
|
304 |
}
|
|
305 |
if (rsptrailers != null) {
|
|
306 |
rsptrailers.print (ps);
|
|
307 |
} else if (rspchunked) {
|
|
308 |
ps.print ("\r\n");
|
|
309 |
}
|
|
310 |
ps.flush();
|
|
311 |
}
|
|
312 |
|
|
313 |
/* sends one byte less than intended */
|
|
314 |
|
|
315 |
public void sendPartialResponse (int rCode, String rTag)throws IOException {
|
|
316 |
OutputStream os = new HttpServer.NioOutputStream(channel());
|
|
317 |
PrintStream ps = new PrintStream (os);
|
|
318 |
ps.print ("HTTP/1.1 " + rCode + " " + rTag + "\r\n");
|
|
319 |
ps.flush();
|
|
320 |
if (rspbody != null) {
|
|
321 |
os.write (rspbody, 0, rspbodylen-1);
|
|
322 |
os.flush();
|
|
323 |
}
|
|
324 |
if (rsptrailers != null) {
|
|
325 |
rsptrailers.print (ps);
|
|
326 |
}
|
|
327 |
ps.flush();
|
|
328 |
}
|
|
329 |
}
|