KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jhttptunnel > Page


1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /*
3 Copyright (c) 2004 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

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 JavaDoc src, Object JavaDoc dst){
40     synchronized(map){
41       map.put(src, dst);
42     }
43   }
44
45   static Object JavaDoc map(String JavaDoc 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 JavaDoc decode(String JavaDoc arg){
55
56     byte[] foo=arg.getBytes();
57     StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
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 JavaDoc location) throws IOException{
81     mysocket.println("HTTP/1.0 302 Found");
82     //mysocket.println("Location: "+HttpServer.myURL+location);
83
mysocket.println("Location: "+location);
84     mysocket.println("");
85     mysocket.flush();
86     mysocket.close();
87   }
88
89   static void unknown(MySocket mysocket, String JavaDoc 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 JavaDoc 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("Content-Type: text/html; charset=iso-8859-1");
110
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 JavaDoc 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 JavaDoc 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 JavaDoc sb=new StringBuffer JavaDoc();
161     String JavaDoc 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