Source code: raining/protocol/http/Http304Response.java
1 /*
2 * $Author: rahul_kumar $
3 * $Id: Http404Response.java,v 1.1 2003/10/20 18:09:56 rahul_kumar Exp rahul $
4 */
5
6 package raining.protocol.http;
7 import raining.core.*;
8 import raining.util.*;
9 import java.util.*;
10
11 /**
12 * Class representing a request (non GET) our server doesnt understand.
13 * @author rahul kumar
14 * @see Response
15 */
16
17 public class Http304Response implements Response {
18
19 ///// static variables ////////////////////////////////
20 //static byte[] header = null;
21 static byte[] content = null;
22 static String sheader = null;
23
24 public final static String HTTP_VERSION = "HTTP/1.1";
25 public final static String CRLF = "\r\n";
26 public final static String CRLF2 = "\r\n\r\n";
27 public final static String contentType = "text/html";
28 protected final static String scontent = "<html><head><title>304 Not Modified</title></head><body bgcolor=white><font face=\"helvetica\"><big><big><b>304 Not Modified </b></big></big><p></body></html>";
29
30 static {
31 sheader = HTTP_VERSION + " 304 NOT MODIFIED"+ CRLF +
32 "Content-Type: "+ contentType + CRLF +
33 "Content-Length: "+ NioUtil.int2byte(scontent.length())+ CRLF +
34 "Connection: close" + CRLF2 +scontent + CRLF2 ;
35 content = sheader.getBytes();
36 }
37 ///// instance variables ////////////////////////////////
38 //byte[] content = null;
39 /** ctor that takes a request object.
40 * Usually the URL would be used in the reply, and logged.
41 * Currently a static response is returned.
42 */
43 public Http304Response (Request request){
44
45 // currently i keep a hardcoded 304 message, so dont do
46 // anything. Later we would log this to some file.
47
48
49 }
50
51 /** Return as a stream of bytes.
52 * Preferred since we can use bytes
53 * throughout and avoid costly string to byte conversions.
54 */
55 public byte[] getBytes(){
56 return content;
57 }
58 /** return content as a String: to start with we use this, this does a conversion 3 times, so
59 * avoid. */
60 public String getString(){
61 return new String(content);
62 }
63 public static final String P="Http304Response";
64 /** Returns a static 503 string to be used, when a server is
65 * reaching capacity, and wants to reject connections with a reply.
66 */
67 public static byte[] getStaticContent(){
68 return content;
69 }
70
71
72 public static void main (String args[]){
73
74 System.out.println( "--string cons--------");
75 Http304Response ok = new Http304Response(null);
76 System.out.println( ok.getString());
77 String s1 = ok.getString();
78 System.out.println( "-As Bytes:");
79 System.out.println( ok.getBytes());
80 byte[] b1 = ok.getBytes();
81 System.out.println( "----------");
82 System.out.println( "-- byte cons--------");
83 ok = new Http304Response(null);
84 System.out.println( ok.getString());
85 String s2 = ok.getString();
86 System.out.println( "-As Bytes:");
87 System.out.println( ok.getBytes());
88 byte[] b2 = ok.getBytes();
89 System.out.println( "s2 eq s1:"+s2.equals(s1));
90 System.out.println( "Length:"+s1.length()+","+s2.length());
91 System.out.println( "BLength:"+b1.length+","+b2.length);
92 if (!s2.equals(s1)){
93 System.out.println( ">>>>:"+s2.compareTo(s1));
94 for( int i = 0; i < s1.length(); i++ ){
95 if (s1.charAt(i) != s2.charAt(i)){ System.out.print( i+" not equal!");
96 System.out.println( "["+s1.charAt(i) + "]:["+ s2.charAt(i)+ "]" );
97 }
98 }
99 System.out.println( "---");
100
101 }
102 for( int i = 0; i < s1.length(); i++ ){
103 if (b1[i] != b2[i]) System.out.println( i+" not equal!");
104 }
105 long start = System.currentTimeMillis();
106 for( int j = 0; j < 100000; j++ ){
107 ok = new Http304Response(null);
108 }
109 long finish = System.currentTimeMillis();
110 System.out.println( "time:"+ (finish-start));
111
112 start = System.currentTimeMillis();
113 byte b3[] = "Hello There Boys".getBytes();
114 for( int j = 0; j < 100000; j++ ){
115 ok = new Http304Response(null);
116 }
117 finish = System.currentTimeMillis();
118 System.out.println( "time:"+ (finish-start));
119
120 }
121
122 } // end of class
123
124 // String string = "<html><head><title>304 Bad Request</title></head><body bgcolor=white><font face=\"helvetica\"><big><big><b>304 Bad Request</b></big></big><p>The URL you requested:<p><blockquote><tt>"+request.getURL()+"</tt></blockquote><p>contained a bad request. The reason given by the server was:<p><blockquote><tt>"+reason+"</tt></blockquote></body></html>\n";
125
126
127