Source code: raining/protocol/http/HttpRequest.java
1 /*
2 * $Author: rahul_kumar $
3 * $Id: HttpRequest.java,v 1.2 2003/10/18 11:25:51 rahul_kumar Exp rahul $
4 */
5
6 package raining.protocol.http;
7 import raining.core.*;
8 import raining.util.*;
9 import java.nio.*;
10 import java.nio.channels.*;
11 import sun.net.www.*;
12 import java.io.*;
13
14 // TODO parse the string It should come as a char array since it cant be
15 // unicode
16 /**
17 * Class for representing HttpRequests.
18 * The server uses this to retrieve operations,
19 * and urls/paths from a request.
20 * RK added on 20031206 00:06:31 : added parsing of request using
21 * MessageHeader class. The URL etc should also be done from here.
22 */
23
24 public class HttpRequest implements Request {
25
26 /** constructor taking a SocketChannel and the incoming request.
27 * the request would be like "GET / HTTP/1.1"
28 * or "GET /index.html HTTP/1.1".
29 * THe split assumes there are no extra spaces, for that i would
30 * have to use a slower re, or modify my split to gobble consecutive
31 * delimiters.
32 */
33 public HttpRequest (SocketChannel sc, String mdata) {
34 this.sc = sc;
35 this.data = mdata;
36 a = NioUtil.split(mdata, ' ');
37 }
38 /** returns operation : e.g.GET or POST.
39 */
40 public String getOperation(){
41 return a[0];
42 }
43 /** returns the url such as / or index.html.
44 */
45 public String getURL(){
46 return a[1];
47 }
48 public SocketChannel getChannel(){
49 return this.sc;
50 }
51 public void setChannel(SocketChannel sc){
52 this.sc = sc;
53 }
54 public void setURL(String url){
55 this.data = url;
56 a = NioUtil.split(url, ' ');
57 }
58 public String toString (){
59 StringBuffer sb = new StringBuffer(128);
60 sb.append("HttpRequest:: data:[").append(data).append("]\n");
61 sb.append(" :: oper:[").append(getOperation()).append("]\n");
62 sb.append(" :: URL :[").append(getURL()).append("]\n");
63 sb.append(" :: sc :[").append(getChannel()).append("]\n");
64 return sb.toString();
65
66 }
67 public String findValue (String key){
68 if (properties == null){
69 try {
70 properties = new MessageHeader(new ByteArrayInputStream(data.getBytes()));
71 return properties.findValue(key);
72 } catch (Exception exc) { System.err.println( P+" L 68 EXC:"+ exc.toString()); }
73 }
74 return null;
75 }
76
77 /** incoming request string, as-is.
78 */
79 protected String data;
80 protected SocketChannel sc;
81 /** split version of incoming string */
82 protected String a[];
83 protected MessageHeader properties = null;
84 private static final String P = "HttpRequest";
85
86
87 public static void main (String args[]){
88
89 String CRLF = "\r\n";
90 String CRLF2 = "\r\n\r\n";
91
92 String r = "GET / HTTP/1.0\r\nHost: localhost"+CRLF2;
93 String r1 = "GET /index.html HTTP/1.0\r\nHost: localhost"+CRLF+
94 "If-Modified-Since: Tue, 15 Nov 1994 12:45:26 GMT"+CRLF2;
95 HttpRequest h = new HttpRequest(null, r);
96 HttpRequest h1 = new HttpRequest(null, r1);
97 try {
98 MessageHeader properties = new MessageHeader(new ByteArrayInputStream(r.getBytes()));
99 System.out.println( "props:"+ properties.toString());
100 properties = new MessageHeader(new ByteArrayInputStream(r1.getBytes()));
101 System.out.println( "props:"+ properties.toString());
102 System.out.println( properties.findValue("If-Modified-Since"));
103 System.out.println( h1.findValue("If-Modified-Since"));
104 } catch (Exception exc) { System.err.println( " L EXC:"+ exc.toString()); exc.printStackTrace(); }
105
106
107
108 }
109 } // end of class