40b3566fc1cb8a469763e56b66216ad936801ebe
[IRC.git] / Robust / src / Benchmarks / Jhttpp2 / Java / Jhttpp2Server.java
1 /* Written and copyright 2001-2003 Benjamin Kohl.\r
2  * Distributed under the GNU General Public License; see the README file.\r
3  * This code comes with NO WARRANTY.\r
4  * More Information and documentation: HTTP://jhttp2.sourceforge.net/\r
5  */\r
6 \r
7 import java.net.ServerSocket;\r
8 import java.net.Socket;\r
9 import java.net.UnknownHostException;\r
10 import java.net.InetAddress;\r
11 import java.net.BindException;\r
12 \r
13 import java.io.*;\r
14 \r
15 import java.util.Vector;\r
16 import java.util.Date;\r
17 \r
18 public class Jhttpp2Server extends Thread\r
19 {\r
20     private static final String CRLF;\r
21     private final String VERSION;\r
22     private final String V_SPECIAL;\r
23     private final String HTTP_VERSION;\r
24     private final String MAIN_LOGFILE;\r
25 \r
26     private final String DATA_FILE;\r
27     private final String SERVER_PROPERTIES_FILE;\r
28 \r
29     private String http_useragent;\r
30     private ServerSocket listen;\r
31     private BufferedWriter logfile;\r
32     private BufferedWriter access_logfile;\r
33     private long bytesread;\r
34     private long byteswritten;\r
35     private int numconnections;\r
36 \r
37     private boolean enable_cookies_by_default;\r
38     private WildcardDictionary dic;\r
39     private Vector urlactions;\r
40 \r
41     public final int DEFAULT_SERVER_PORT;\r
42     public final String WEB_CONFIG_FILE;\r
43 \r
44     public int port;\r
45     public InetAddress proxy;\r
46     public int proxy_port;\r
47 \r
48     public long config_auth;\r
49     public long config_session_id;\r
50     public String config_user;\r
51     public String config_password;\r
52 \r
53     public static boolean error;\r
54     public static String error_msg;\r
55 \r
56     public boolean use_proxy;\r
57     public boolean block_urls;\r
58     public boolean filter_http;\r
59     public boolean debug;\r
60     public boolean log_access;\r
61     public String log_access_filename;\r
62     public boolean webconfig;\r
63     public boolean www_server;\r
64     \r
65 \r
66     public initvars() {\r
67         CRLF="\r\n";\r
68         VERSION = "0.4.62";\r
69         V_SPECIAL = " 2003-05-20";\r
70         HTTP_VERSION = "HTTP/1.1";\r
71         MAIN_LOGFILE = "server.log";\r
72         DATA_FILE = "server.data";\r
73         SERVER_PROPERTIES_FILE = "server.properties";\r
74         http_useragent = "Mozilla/4.0 (compatible; MSIE 4.0; WindowsNT 5.0)";\r
75         enable_cookies_by_default=true;\r
76         dic = new WildcardDictionary();\r
77         urlactions = new Vector();\r
78         DEFAULT_SERVER_PORT = 8088;\r
79         WEB_CONFIG_FILE = "admin/jp2-config";\r
80         port = DEFAULT_SERVER_PORT;\r
81         proxy_port = 0;\r
82         config_auth = 0;\r
83         config_session_id = 0;\r
84         config_user = "root";\r
85         config_password = "geheim";\r
86         use_proxy=false;\r
87         block_urls=false;\r
88         filter_http=false;\r
89         debug=false;\r
90         log_access = true;\r
91         log_access_filename="paccess.log";\r
92         webconfig = true;\r
93         www_server = true;\r
94 }\r
95 \r
96   void init()\r
97   {\r
98       if(log_access) {\r
99           access_logfile=new BufferedWriter(new FileWriter(log_access_filename,true));      \r
100       }\r
101 \r
102       logfile=new BufferedWriter(new FileWriter(MAIN_LOGFILE,true));\r
103       writeLog("server startup...");\r
104 \r
105       listen = new ServerSocket(port);\r
106       \r
107       if (error) {\r
108           writeLog(error_msg);\r
109           return;\r
110       }\r
111   }\r
112     public Jhttpp2Server() {\r
113         initvars();\r
114         init();\r
115     }\r
116   public Jhttpp2Server(boolean b)\r
117   {\r
118     initvars();\r
119     System.printString("jHTTPp2 HTTP Proxy Server Release " + getServerVersion() + "\r\n"\r
120       +"Copyright (c) 2001-2003 Benjamin Kohl <bkohl@users.sourceforge.net>\r\n"\r
121       +"This software comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\r\n"\r
122       +"http://jhttp2.sourceforge.net/\n");\r
123     init();\r
124   }\r
125   /** calls init(), sets up the serverport and starts for each connection\r
126    * new Jhttpp2Connection\r
127    */\r
128   void serve()\r
129   {\r
130       writeLog("Server running.");\r
131       while(true)\r
132           {\r
133               Socket client = listen.accept();\r
134               new Jhttpp2HTTPSession(this,client);\r
135           }\r
136   }\r
137   public void run()\r
138   {\r
139     serve();\r
140   }\r
141   public void setErrorMsg(String a)\r
142   {\r
143     error=true;\r
144     error_msg=a;\r
145   }\r
146   /**\r
147    * Tests what method is used with the reqest\r
148    * @return -1 if the server doesn't support the method\r
149    */\r
150   public int getHttpMethod(String d)\r
151   {\r
152     if (startsWith(d,"GET")  || startsWith(d,"HEAD")) return 0;\r
153     if (startsWith(d,"POST") || startsWith(d,"PUT")) return 1;\r
154     if (startsWith(d,"CONNECT")) return 2;\r
155     if (startsWith(d,"OPTIONS")) return 3;\r
156 \r
157     return -1;/* No match...\r
158 \r
159     Following methods are not implemented:\r
160     || startsWith(d,"TRACE") */\r
161   }\r
162   public boolean startsWith(String a,String what)\r
163   {\r
164     int l=what.length();\r
165     int l2=a.length();\r
166     if (l2>l)\r
167         return a.substring(0,l).equals(what);\r
168     else\r
169         return false;\r
170   }\r
171   /**\r
172    *@return the Server response-header field\r
173    */\r
174   public String getServerIdentification()\r
175   {\r
176     return "jHTTPp2/" + getServerVersion();\r
177   }\r
178   public String getServerVersion()\r
179   {\r
180     return VERSION + V_SPECIAL;\r
181   }\r
182   /**\r
183    *  saves all settings with a ObjectOutputStream into a file\r
184    * @since 0.2.10\r
185    */\r
186   /** restores all Jhttpp2 options from "settings.dat"\r
187    * @since 0.2.10\r
188    */\r
189   /**\r
190    * @return the HTTP version used by jHTTPp2\r
191    */\r
192   public String getHttpVersion()\r
193   {\r
194     return HTTP_VERSION;\r
195   }\r
196   /** the User-Agent header field\r
197    * @since 0.2.17\r
198    * @return User-Agent String\r
199    */\r
200   public String getUserAgent()\r
201   {\r
202     return http_useragent;\r
203   }\r
204   public void setUserAgent(String ua)\r
205   {\r
206     http_useragent=ua;\r
207   }\r
208   /**\r
209    * writes into the server log file and adds a new line\r
210    * @since 0.2.21\r
211    */\r
212   public void writeLog(String s)\r
213   {\r
214     writeLog(s,true);\r
215   }\r
216   /** writes to the server log file\r
217    * @since 0.2.21\r
218    */\r
219   public void writeLog(String s,boolean b)\r
220   {\r
221       s=new Date().toString() + " " + s;\r
222       logfile.write(s,0,s.length());\r
223       if (b) logfile.newLine();\r
224       logfile.flush();\r
225       if (debug)System.printString(s);\r
226   }\r
227 \r
228   public void closeLog()\r
229   {\r
230       writeLog("Server shutdown.");\r
231       logfile.flush();\r
232       logfile.close();\r
233       access_logfile.close();\r
234   }\r
235 \r
236   public void addBytesRead(long read)\r
237   {\r
238     bytesread+=read;\r
239   }\r
240   /**\r
241    * Functions for the jHTTPp2 statistics:\r
242    * How many connections\r
243    * Bytes read/written\r
244    * @since 0.3.0\r
245    */\r
246   public void addBytesWritten(int written)\r
247   {\r
248     byteswritten+=written;\r
249   }\r
250   public int getServerConnections()\r
251   {\r
252     return numconnections;\r
253   }\r
254   public long getBytesRead()\r
255   {\r
256     return bytesread;\r
257   }\r
258   public long getBytesWritten()\r
259   {\r
260     return byteswritten;\r
261   }\r
262   public void increaseNumConnections()\r
263   {\r
264     numconnections++;\r
265   }\r
266   public void decreaseNumConnections()\r
267   {\r
268     numconnections--;\r
269   }\r
270   public void AuthenticateUser(String u,String p) {\r
271           if (config_user.equals(u) && config_password.equals(p)) {\r
272                 config_auth = 1;\r
273           } else config_auth = 0;\r
274         }\r
275   public String getGMTString()\r
276   {\r
277     return new Date().toString();\r
278   }\r
279   public Jhttpp2URLMatch findMatch(String url)\r
280   {\r
281     return (Jhttpp2URLMatch)dic.get(url);\r
282   }\r
283   public WildcardDictionary getWildcardDictionary()\r
284   {\r
285     return dic;\r
286   }\r
287   public Vector getURLActions()\r
288   {\r
289     return urlactions;\r
290   }\r
291   public boolean enableCookiesByDefault()\r
292   {\r
293     return this.enable_cookies_by_default;\r
294   }\r
295   public void enableCookiesByDefault(boolean a)\r
296   {\r
297     enable_cookies_by_default=a;\r
298   }\r
299   public void resetStat()\r
300   {\r
301     bytesread=0;\r
302     byteswritten=0;\r
303   }\r
304   /**\r
305    * @since 0.4.10a\r
306    */\r
307   /**\r
308    * @since 0.4.10a\r
309    */\r
310   /**\r
311    * @since 0.4.10a\r
312    */\r
313   public void logAccess(String s)\r
314   {\r
315       access_logfile.write("[" + new Date().toString() + "] " + s + "\r\n");\r
316       access_logfile.flush();\r
317   }\r
318   public void shutdownServer() {\r
319           closeLog();\r
320           System.exit(0);\r
321   }\r
322 \r
323 }\r