20 * or visit www.oracle.com if you need additional information or have any 21 * 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); 273 for (int i=0; i<body.length; i++) { 274 String chunklen = Integer.toHexString (body[i].length()); 275 len += body[i].length(); 276 buf.append (chunklen).append ("\r\n"); 277 buf.append (body[i]).append ("\r\n"); 278 } 279 buf.append ("0\r\n"); 280 rspbody = new String (buf).getBytes(); 281 rspbodylen = rspbody.length; 282 rspchunked = true; 283 addResponseHeader ("Transfer-encoding", "chunked"); 284 } 285 286 /** 287 * Send the response with the current set of response parameters 288 * but using the response code and string tag line as specified 289 * @param rCode the response code to send 290 * @param rTag the response string to send with the response code 291 */ 292 public void sendResponse (int rCode, String rTag) throws IOException { 293 OutputStream os = new HttpServer.NioOutputStream(channel()); 294 PrintStream ps = new PrintStream (os); 295 ps.print ("HTTP/1.1 " + rCode + " " + rTag + "\r\n"); 296 if (rspheaders != null) { 297 rspheaders.print (ps); 298 } else { 299 ps.print ("\r\n"); 300 } 301 ps.flush (); 302 if (rspbody != null) { 303 os.write (rspbody, 0, rspbodylen); 304 os.flush(); 305 } 306 if (rsptrailers != null) { 307 rsptrailers.print (ps); 308 } else if (rspchunked) { 309 ps.print ("\r\n"); 310 } 311 ps.flush(); 312 } 313 314 /* sends one byte less than intended */ 315 316 public void sendPartialResponse (int rCode, String rTag)throws IOException { 317 OutputStream os = new HttpServer.NioOutputStream(channel()); 318 PrintStream ps = new PrintStream (os); 319 ps.print ("HTTP/1.1 " + rCode + " " + rTag + "\r\n"); 320 ps.flush(); 321 if (rspbody != null) { 322 os.write (rspbody, 0, rspbodylen-1); 323 os.flush(); 324 } 325 if (rsptrailers != null) { 326 rsptrailers.print (ps); 327 } 328 ps.flush(); 329 } 330 } | 20 * or visit www.oracle.com if you need additional information or have any 21 * 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 TestHttpServer.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 (TestHttpServer.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); 273 for (int i=0; i<body.length; i++) { 274 String chunklen = Integer.toHexString (body[i].length()); 275 len += body[i].length(); 276 buf.append (chunklen).append ("\r\n"); 277 buf.append (body[i]).append ("\r\n"); 278 } 279 buf.append ("0\r\n"); 280 rspbody = new String (buf).getBytes(); 281 rspbodylen = rspbody.length; 282 rspchunked = true; 283 addResponseHeader ("Transfer-encoding", "chunked"); 284 } 285 286 /** 287 * Send the response with the current set of response parameters 288 * but using the response code and string tag line as specified 289 * @param rCode the response code to send 290 * @param rTag the response string to send with the response code 291 */ 292 public void sendResponse (int rCode, String rTag) throws IOException { 293 OutputStream os = new TestHttpServer.NioOutputStream(channel()); 294 PrintStream ps = new PrintStream (os); 295 ps.print ("HTTP/1.1 " + rCode + " " + rTag + "\r\n"); 296 if (rspheaders != null) { 297 rspheaders.print (ps); 298 } else { 299 ps.print ("\r\n"); 300 } 301 ps.flush (); 302 if (rspbody != null) { 303 os.write (rspbody, 0, rspbodylen); 304 os.flush(); 305 } 306 if (rsptrailers != null) { 307 rsptrailers.print (ps); 308 } else if (rspchunked) { 309 ps.print ("\r\n"); 310 } 311 ps.flush(); 312 } 313 314 /* sends one byte less than intended */ 315 316 public void sendPartialResponse (int rCode, String rTag)throws IOException { 317 OutputStream os = new TestHttpServer.NioOutputStream(channel()); 318 PrintStream ps = new PrintStream (os); 319 ps.print ("HTTP/1.1 " + rCode + " " + rTag + "\r\n"); 320 ps.flush(); 321 if (rspbody != null) { 322 os.write (rspbody, 0, rspbodylen-1); 323 os.flush(); 324 } 325 if (rsptrailers != null) { 326 rsptrailers.print (ps); 327 } 328 ps.flush(); 329 } 330 } |