start of new file
[IRC.git] / Robust / src / Benchmarks / Jhttpp2 / BR / 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\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   public void setErrorMsg(String a)\r
129   {\r
130     error=true;\r
131     error_msg=a;\r
132   }\r
133   /**\r
134    * Tests what method is used with the reqest\r
135    * @return -1 if the server doesn't support the method\r
136    */\r
137   public static int getHttpMethod(String d)\r
138   {\r
139     if (startsWith(d,"GET")  || startsWith(d,"HEAD")) return 0;\r
140     if (startsWith(d,"POST") || startsWith(d,"PUT")) return 1;\r
141     if (startsWith(d,"CONNECT")) return 2;\r
142     if (startsWith(d,"OPTIONS")) return 3;\r
143 \r
144     return -1;/* No match...\r
145 \r
146     Following methods are not implemented:\r
147     || startsWith(d,"TRACE") */\r
148   }\r
149   public static boolean startsWith(String a,String what)\r
150   {\r
151     int l=what.length();\r
152     int l2=a.length();\r
153     if (l2>l)\r
154         return a.substring(0,l).equals(what);\r
155     else\r
156         return false;\r
157   }\r
158   /**\r
159    *@return the Server response-header field\r
160    */\r
161   public String getServerIdentification()\r
162   {\r
163     return "jHTTPp2/" + getServerVersion();\r
164   }\r
165   public String getServerVersion()\r
166   {\r
167     return VERSION + V_SPECIAL;\r
168   }\r
169   /**\r
170    *  saves all settings with a ObjectOutputStream into a file\r
171    * @since 0.2.10\r
172    */\r
173   /** restores all Jhttpp2 options from "settings.dat"\r
174    * @since 0.2.10\r
175    */\r
176   /**\r
177    * @return the HTTP version used by jHTTPp2\r
178    */\r
179   public String getHttpVersion()\r
180   {\r
181     return HTTP_VERSION;\r
182   }\r
183   /** the User-Agent header field\r
184    * @since 0.2.17\r
185    * @return User-Agent String\r
186    */\r
187   public String getUserAgent()\r
188   {\r
189     return http_useragent;\r
190   }\r
191   public void setUserAgent(String ua)\r
192   {\r
193     http_useragent=ua;\r
194   }\r
195   /**\r
196    * writes into the server log file and adds a new line\r
197    * @since 0.2.21\r
198    */\r
199   public void writeLog(String s)\r
200   {\r
201     writeLog(s,true);\r
202   }\r
203   /** writes to the server log file\r
204    * @since 0.2.21\r
205    */\r
206   public void writeLog(String s,boolean b)\r
207   {\r
208       s=new Date().toString() + " " + s;\r
209       logfile.write(s,0,s.length());\r
210       if (b) logfile.newLine();\r
211       logfile.flush();\r
212       if (debug)System.printString(s);\r
213   }\r
214 \r
215   public void closeLog()\r
216   {\r
217       writeLog("Server shutdown.");\r
218       logfile.flush();\r
219       logfile.close();\r
220       access_logfile.close();\r
221   }\r
222 \r
223   public void addBytesRead(long read)\r
224   {\r
225     bytesread+=read;\r
226   }\r
227   /**\r
228    * Functions for the jHTTPp2 statistics:\r
229    * How many connections\r
230    * Bytes read/written\r
231    * @since 0.3.0\r
232    */\r
233   public void addBytesWritten(int written)\r
234   {\r
235     byteswritten+=written;\r
236   }\r
237   public int getServerConnections()\r
238   {\r
239     return numconnections;\r
240   }\r
241   public long getBytesRead()\r
242   {\r
243     return bytesread;\r
244   }\r
245   public long getBytesWritten()\r
246   {\r
247     return byteswritten;\r
248   }\r
249   public void increaseNumConnections()\r
250   {\r
251     numconnections++;\r
252   }\r
253   public void decreaseNumConnections()\r
254   {\r
255     numconnections--;\r
256   }\r
257   public void AuthenticateUser(String u,String p) {\r
258           if (config_user.equals(u) && config_password.equals(p)) {\r
259                 config_auth = 1;\r
260           } else config_auth = 0;\r
261         }\r
262   public String getGMTString()\r
263   {\r
264     return new Date().toString();\r
265   }\r
266   public Jhttpp2URLMatch findMatch(String url)\r
267   {\r
268     return (Jhttpp2URLMatch)dic.get(url);\r
269   }\r
270   public WildcardDictionary getWildcardDictionary()\r
271   {\r
272     return dic;\r
273   }\r
274   public Vector getURLActions()\r
275   {\r
276     return urlactions;\r
277   }\r
278   public boolean enableCookiesByDefault()\r
279   {\r
280     return this.enable_cookies_by_default;\r
281   }\r
282   public void enableCookiesByDefault(boolean a)\r
283   {\r
284     enable_cookies_by_default=a;\r
285   }\r
286   public void resetStat()\r
287   {\r
288     bytesread=0;\r
289     byteswritten=0;\r
290   }\r
291   /**\r
292    * @since 0.4.10a\r
293    */\r
294   /**\r
295    * @since 0.4.10a\r
296    */\r
297   /**\r
298    * @since 0.4.10a\r
299    */\r
300   public void logAccess(String s)\r
301   {\r
302       access_logfile.write("[" + new Date().toString() + "] " + s + "\r\n");\r
303       access_logfile.flush();\r
304   }\r
305   public void shutdownServer() {\r
306           closeLog();\r
307           System.exit(0);\r
308   }\r
309 \r
310 }\r