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