1 2 29 30 package com.jcraft.jhttptunnel; 31 import java.util.*; 32 import java.io.*; 33 34 public abstract class Page{ 35 static Hashtable map=new Hashtable(); 36 37 static void register(){} 38 39 static void register(String src, Object dst){ 40 synchronized(map){ 41 map.put(src, dst); 42 } 43 } 44 45 static Object map(String foo){ 46 if(foo!=null && foo.startsWith("///")){ 47 foo=foo.substring(2); 48 } 49 synchronized(map){ 50 return map.get(foo); 51 } 52 } 53 54 String decode(String arg){ 55 56 byte[] foo=arg.getBytes(); 57 StringBuffer sb=new StringBuffer (); 58 for(int i=0; i<foo.length; i++){ 59 if(foo[i]=='+'){ sb.append((char)' '); continue; } 60 if(foo[i]=='%' && i+2<foo.length){ 61 int bar=foo[i+1]; 62 bar=('0'<=bar && bar<='9')? bar-'0' : 63 ('a'<=bar && bar<='z')? bar-'a'+10 : 64 ('A'<=bar && bar<='Z')? bar-'A'+10 : bar; 65 bar*=16; 66 int goo=foo[i+2]; 67 goo=('0'<=goo && goo<='9')? goo-'0' : 68 ('a'<=goo && goo<='f')? goo-'a'+10 : 69 ('A'<=goo && goo<='F')? goo-'A'+10 : goo; 70 bar+=goo; bar&=0xff; 71 sb.append((char)bar); 72 i+=2; 73 continue; 74 } 75 sb.append((char)foo[i]); 76 } 77 return sb.toString(); 78 } 79 80 static void forward(MySocket mysocket, String location) throws IOException{ 81 mysocket.println("HTTP/1.0 302 Found"); 82 mysocket.println("Location: "+location); 84 mysocket.println(""); 85 mysocket.flush(); 86 mysocket.close(); 87 } 88 89 static void unknown(MySocket mysocket, String location) throws IOException{ 90 mysocket.println("HTTP/1.0 404 Not Found"); 91 mysocket.println("Connection: close"); 92 mysocket.println("Content-Type: text/html; charset=iso-8859-1"); 93 mysocket.println(""); 94 mysocket.println("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"); 95 mysocket.println("<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY>"); 96 mysocket.println("<H1>Not Found</H1>"); 97 mysocket.println("The requested URL "+location+" was not found on this server.<P>"); 98 mysocket.println("<HR>"); 99 mysocket.println("<ADDRESS>JRoar at "+HttpServer.myURL+"/</ADDRESS>"); 100 mysocket.println("</BODY></HTML>"); 101 mysocket.flush(); 102 mysocket.close(); 103 } 104 105 static void ok(MySocket mysocket, String location) throws IOException{ 106 mysocket.println("HTTP/1.0 200 OK"); 107 mysocket.println("Last-Modified: Thu, 04 Oct 2001 14:09:23 GMT"); 108 mysocket.println("Connection: close"); 109 mysocket.println(""); 111 mysocket.flush(); 112 mysocket.close(); 113 } 114 115 abstract void kick(MySocket mysocket, Hashtable ht, Vector v) throws IOException; 116 117 Hashtable getVars(String arg){ 118 119 Hashtable vars=new Hashtable(); 120 vars.put("jroar-method", "GET"); 121 if(arg==null) return vars; 122 123 arg=decode(arg); 124 125 int foo=0; 126 int i=0; 127 int c=0; 128 129 String key, value; 130 131 while(true){ 132 key=value=null; 133 134 foo=arg.indexOf('='); 135 if(foo==-1)break; 136 key=arg.substring(0, foo); 137 arg=arg.substring(foo+1); 138 139 foo=arg.indexOf('&'); 140 if(foo!=-1){ 141 value=arg.substring(0, foo); 142 arg=arg.substring(foo+1); 143 } 144 else value=arg; 145 146 vars.put(key, value); 147 148 if(foo==-1)break; 149 } 150 return vars; 151 } 152 153 Hashtable getVars(MySocket mysocket, int len){ 154 Hashtable vars=new Hashtable(); 155 vars.put("jroar-method", "POST"); 156 if(len==0) return vars; 157 158 int i=0; 159 int c=0; 160 StringBuffer sb=new StringBuffer (); 161 String key, value; 162 163 while(i<len){ 164 key=value=null; 165 sb.setLength(0); 166 while(i<len){ 167 c=mysocket.readByte( ); i++; 168 if(c=='='){ 169 key=sb.toString(); 170 break; 171 } 172 sb.append((char)c); 173 } 174 sb.setLength(0); 175 while(i<len){ 176 c=mysocket.readByte( ); i++; 177 if(c=='&'){ 178 value=sb.toString(); 179 break; 180 } 181 sb.append((char)c); 182 } 183 if(key!=null && value!=null){ 184 key=decode(key); value=decode(value); 185 vars.put(key, value); 186 } 187 } 188 return vars; 189 } 190 191 static void notFound(MySocket ms) throws IOException{ 192 ms.println("HTTP/1.0 404 Not Found") ; 193 ms.println("Content-Type: text/html") ; 194 ms.println("") ; 195 ms.println("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"); 196 ms.println("<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY>"); 197 ms.println("<H1>Not Found</H1>The requested URL was not found on this server.<HR>"); 198 ms.println("</BODY></HTML>"); 199 ms.flush(); 200 ms.close(); 201 } 202 } 203 | Popular Tags |