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
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
15 import java.util.Vector;
\r
16 import java.util.Date;
\r
18 public class Jhttpp2Server extends Thread
\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
26 private final String DATA_FILE;
\r
27 private final String SERVER_PROPERTIES_FILE;
\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
37 private boolean enable_cookies_by_default;
\r
38 private WildcardDictionary dic;
\r
39 private Vector urlactions;
\r
41 public final int DEFAULT_SERVER_PORT;
\r
42 public final String WEB_CONFIG_FILE;
\r
45 public InetAddress proxy;
\r
46 public int proxy_port;
\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
53 public static boolean error;
\r
54 public static String error_msg;
\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
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
83 config_session_id = 0;
\r
84 config_user = "root";
\r
85 config_password = "geheim";
\r
91 log_access_filename="paccess.log";
\r
99 access_logfile=new BufferedWriter(new FileWriter(log_access_filename,true));
\r
102 logfile=new BufferedWriter(new FileWriter(MAIN_LOGFILE,true));
\r
103 writeLog("server startup...");
\r
105 listen = new ServerSocket(port);
\r
108 writeLog(error_msg);
\r
112 public Jhttpp2Server() {
\r
116 public Jhttpp2Server(boolean b)
\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
125 /** calls init(), sets up the serverport and starts for each connection
\r
126 * new Jhttpp2Connection
\r
130 writeLog("Server running.");
\r
133 Socket client = listen.accept();
\r
134 new Jhttpp2HTTPSession(this,client);
\r
141 public void setErrorMsg(String a)
\r
147 * Tests what method is used with the reqest
\r
148 * @return -1 if the server doesn't support the method
\r
150 public int getHttpMethod(String d)
\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
157 return -1;/* No match...
\r
159 Following methods are not implemented:
\r
160 || startsWith(d,"TRACE") */
\r
162 public boolean startsWith(String a,String what)
\r
164 int l=what.length();
\r
167 return a.substring(0,l).equals(what);
\r
172 *@return the Server response-header field
\r
174 public String getServerIdentification()
\r
176 return "jHTTPp2/" + getServerVersion();
\r
178 public String getServerVersion()
\r
180 return VERSION + V_SPECIAL;
\r
183 * saves all settings with a ObjectOutputStream into a file
\r
186 /** restores all Jhttpp2 options from "settings.dat"
\r
190 * @return the HTTP version used by jHTTPp2
\r
192 public String getHttpVersion()
\r
194 return HTTP_VERSION;
\r
196 /** the User-Agent header field
\r
198 * @return User-Agent String
\r
200 public String getUserAgent()
\r
202 return http_useragent;
\r
204 public void setUserAgent(String ua)
\r
209 * writes into the server log file and adds a new line
\r
212 public void writeLog(String s)
\r
216 /** writes to the server log file
\r
219 public void writeLog(String s,boolean b)
\r
221 s=new Date().toString() + " " + s;
\r
222 logfile.write(s,0,s.length());
\r
223 if (b) logfile.newLine();
\r
225 if (debug)System.printString(s);
\r
228 public void closeLog()
\r
230 writeLog("Server shutdown.");
\r
233 access_logfile.close();
\r
236 public void addBytesRead(long read)
\r
241 * Functions for the jHTTPp2 statistics:
\r
242 * How many connections
\r
243 * Bytes read/written
\r
246 public void addBytesWritten(int written)
\r
248 byteswritten+=written;
\r
250 public int getServerConnections()
\r
252 return numconnections;
\r
254 public long getBytesRead()
\r
258 public long getBytesWritten()
\r
260 return byteswritten;
\r
262 public void increaseNumConnections()
\r
266 public void decreaseNumConnections()
\r
270 public void AuthenticateUser(String u,String p) {
\r
271 if (config_user.equals(u) && config_password.equals(p)) {
\r
273 } else config_auth = 0;
\r
275 public String getGMTString()
\r
277 return new Date().toString();
\r
279 public Jhttpp2URLMatch findMatch(String url)
\r
281 return (Jhttpp2URLMatch)dic.get(url);
\r
283 public WildcardDictionary getWildcardDictionary()
\r
287 public Vector getURLActions()
\r
291 public boolean enableCookiesByDefault()
\r
293 return this.enable_cookies_by_default;
\r
295 public void enableCookiesByDefault(boolean a)
\r
297 enable_cookies_by_default=a;
\r
299 public void resetStat()
\r
313 public void logAccess(String s)
\r
315 access_logfile.write("[" + new Date().toString() + "] " + s + "\r\n");
\r
316 access_logfile.flush();
\r
318 public void shutdownServer() {
\r