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
\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
128 public void setErrorMsg(String a)
\r
134 * Tests what method is used with the reqest
\r
135 * @return -1 if the server doesn't support the method
\r
137 public static int getHttpMethod(String d)
\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
144 return -1;/* No match...
\r
146 Following methods are not implemented:
\r
147 || startsWith(d,"TRACE") */
\r
149 public static boolean startsWith(String a,String what)
\r
151 int l=what.length();
\r
154 return a.substring(0,l).equals(what);
\r
159 *@return the Server response-header field
\r
161 public String getServerIdentification()
\r
163 return "jHTTPp2/" + getServerVersion();
\r
165 public String getServerVersion()
\r
167 return VERSION + V_SPECIAL;
\r
170 * saves all settings with a ObjectOutputStream into a file
\r
173 /** restores all Jhttpp2 options from "settings.dat"
\r
177 * @return the HTTP version used by jHTTPp2
\r
179 public String getHttpVersion()
\r
181 return HTTP_VERSION;
\r
183 /** the User-Agent header field
\r
185 * @return User-Agent String
\r
187 public String getUserAgent()
\r
189 return http_useragent;
\r
191 public void setUserAgent(String ua)
\r
196 * writes into the server log file and adds a new line
\r
199 public void writeLog(String s)
\r
203 /** writes to the server log file
\r
206 public void writeLog(String s,boolean b)
\r
208 s=new Date().toString() + " " + s;
\r
209 logfile.write(s,0,s.length());
\r
210 if (b) logfile.newLine();
\r
212 if (debug)System.printString(s);
\r
215 public void closeLog()
\r
217 writeLog("Server shutdown.");
\r
220 access_logfile.close();
\r
223 public void addBytesRead(long read)
\r
228 * Functions for the jHTTPp2 statistics:
\r
229 * How many connections
\r
230 * Bytes read/written
\r
233 public void addBytesWritten(int written)
\r
235 byteswritten+=written;
\r
237 public int getServerConnections()
\r
239 return numconnections;
\r
241 public long getBytesRead()
\r
245 public long getBytesWritten()
\r
247 return byteswritten;
\r
249 public void increaseNumConnections()
\r
253 public void decreaseNumConnections()
\r
257 public void AuthenticateUser(String u,String p) {
\r
258 if (config_user.equals(u) && config_password.equals(p)) {
\r
260 } else config_auth = 0;
\r
262 public String getGMTString()
\r
264 return new Date().toString();
\r
266 public Jhttpp2URLMatch findMatch(String url)
\r
268 return (Jhttpp2URLMatch)dic.get(url);
\r
270 public WildcardDictionary getWildcardDictionary()
\r
274 public Vector getURLActions()
\r
278 public boolean enableCookiesByDefault()
\r
280 return this.enable_cookies_by_default;
\r
282 public void enableCookiesByDefault(boolean a)
\r
284 enable_cookies_by_default=a;
\r
286 public void resetStat()
\r
300 public void logAccess(String s)
\r
302 access_logfile.write("[" + new Date().toString() + "] " + s + "\r\n");
\r
303 access_logfile.flush();
\r
305 public void shutdownServer() {
\r