1 package org.feistymeow.networking;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
23 static private Log logger = LogFactory.getLog(
BasicWebServer.class);
26 private boolean leaving =
false;
28 private ServerSocket realSocket;
38 if (realSocket !=
null) {
41 }
catch (IOException e) {
44 if (socketThread !=
null) {
51 private Thread thread;
52 private ServerSocket serverSocket;
56 serverSocket = socket;
57 thread =
new Thread(
this);
66 logger.debug(
"about to accept on server socket.");
67 Socket s = serverSocket.accept();
69 logger.debug(
"accepted client, spawning handler.");
72 }
catch (Throwable cause) {
73 logger.error(
"exception raised while handling accepted socket", cause);
82 if (socketThread !=
null)
85 realSocket =
new ServerSocket(port);
86 }
catch (Throwable cause) {
87 logger.error(
"failure to start server on port " + port, cause);
99 return "text/plain;charset=utf-8";
111 class ClientHandler
extends Thread
113 private Socket socket;
116 public ClientHandler(Socket s)
126 logger.debug(
"into client run(): listening for gets.");
129 BufferedReader in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
130 PrintStream out =
new PrintStream(
new BufferedOutputStream(socket.getOutputStream()));
134 String s = in.readLine();
135 logger.debug(
"request is: " + s);
140 String filename =
"";
141 StringTokenizer st =
new StringTokenizer(s);
144 boolean transferFile =
true;
146 String command = st.nextToken();
148 if (st.hasMoreElements() && command.equalsIgnoreCase(
"GET") && st.hasMoreElements()) {
149 filename = st.nextToken();
150 }
else if (st.hasMoreElements() && command.equalsIgnoreCase(
"HEAD") && st.hasMoreElements()) {
151 filename = st.nextToken();
152 transferFile =
false;
155 logger.error(
"going to blow file not found exception now.");
156 throw new FileNotFoundException();
158 logger.info(
"filename to handle is now: " + filename);
162 if (filename.endsWith(
"/"))
163 logger.error(
"unhandled attempt to get item ending in slash");
172 filename = filename.replace(
'/', File.separator.charAt(0));
174 logger.info(
"asking for rns path of " + filename);
178 if (filename.indexOf(
"..") >= 0 || filename.indexOf(
':') >= 0 || filename.indexOf(
'|') >= 0)
179 throw new FileNotFoundException();
181 logger.info(
"got past filename checks for: " + filename);
197 File source =
new File(filename);
198 if (!source.exists()) {
199 logger.error(
"source does not exist for serving: " + filename);
203 out.println(
"HTTP/1.1 404 Not Found\r\n" +
"Content-type: text/html\r\n\r\n"
204 +
"<html><head></head><body>" + filename +
" not found</body></html>\n");
208 out.print(
"HTTP/1.1 200 OK\r\n" +
"Content-type: " + mimeType +
"\r\n" +
"Connection: close" +
"\r\n"
213 logger.debug(
"closing stream for finished HEAD request.");
217 logger.debug(
"moving to handle GET request.");
218 FileInputStream
f =
new FileInputStream(filename);
219 logger.debug(
"opened stream on source");
221 byte[] a =
new byte[4096];
223 while ((n =
f.read(a)) > 0)
225 logger.debug(
"wrote file back for request, closing stream.");
228 }
catch (FileNotFoundException x) {
229 logger.error(
"failed to find requested file: " + filename);
230 out.println(
"HTTP/1.1 404 Not Found\r\n" +
"Content-type: text/html\r\n\r\n" +
"<html><head></head><body>"
231 + filename +
" not found</body></html>\n");
234 }
catch (IOException x) {
235 logger.error(
"exception blew out in outer area of web server", x);
String predictMimeType(String filename)