/* Copyright Paul James Mutton, 2001-2004, http://www.jibble.org/ This file is part of Jibble Web Server / WebServerLite. This software is dual-licensed, allowing you to choose between the GNU General Public License (GPL) and the www.jibble.org Commercial License. Since the GPL may be too restrictive for use in a proprietary application, a commercial license is also provided. Full license information can be found at http://www.jibble.org/licenses/ $Author: pjm2 $ $Id: CartoonStripBot.java,v 1.4 2004/02/01 13:19:54 pjm2 Exp $ */ import java.io.*; import java.net.*; import java.util.*; /** * A thread which deals with an individual request to the web server. * This is passed a socket from the WebServer when a connection is * accepted. * * @author Copyright Paul Mutton, http://www.jibble.org/ */ public class RequestThread implements Runnable { public RequestThread(Socket socket, File rootDir) { _socket = socket; _rootDir = rootDir; } // handles a connction from a client. public void run() { String ip = "unknown"; String request = "unknown"; int bytesSent = 0; BufferedInputStream reader = null; //added by plee String method = "GET"; boolean debug = true; boolean getwithquery = false; String query=""; //moved up by plee HashMap headers = new HashMap(); //System.out.println("plee: entering requestthread"); try { ip = _socket.getInetAddress().getHostAddress(); BufferedReader in = new BufferedReader(new InputStreamReader(_socket.getInputStream())); BufferedOutputStream out = new BufferedOutputStream(_socket.getOutputStream()); String path = ""; // Read the first line from the client. request = in.readLine(); //modefied by plee Logger.log("plee request " , request, 1000); if (request.startsWith("POST")) { method = "POST"; } if ((request != null) && (request.endsWith(" HTTP/1.0") || request.endsWith("HTTP/1.1"))) { //path = request.substring(4, request.length() - 9); //modefied by plee if (request.startsWith("GET")) { int compget = 0; compget = request.indexOf('?'); if (compget > 0) { path=request.substring(4,compget); getwithquery = true; } else // just get path = request.substring(4, request.length() - 9); } else if (request.startsWith("POST")) { path = request.substring(5, request.length() - 9); } else { // Invalid request (no "GET" or "POST") Logger.log(ip, request, 405); _socket.close(); return; } Logger.log("plee path:" , path , 1000); } else { // other Invalid request type Logger.log(ip, request, 405); _socket.close(); return; } //Read in and store all the headers. if (getwithquery) { String n=""; String v=""; int i=0; int j=0; String subs=request.substring(request.indexOf('?')+1, request.length()-9); Logger.log("plee query:" , subs , 1000); query = subs; int last = subs.lastIndexOf('&'); int start=0; while (start < last) { i = 0; j = subs.indexOf('='); n = subs.substring(i,j); i = subs.indexOf('&'); v = subs.substring(j+1,i); headers.put(n, v.trim()); start = start+i+1; subs = subs.substring(i+1); } i = 0; j = subs.indexOf('='); n = subs.substring(i,j); v = subs.substring(j+1); headers.put(n, v.trim()); } else if (request.startsWith("POST")) { String line = null; while ((line = in.readLine()) != null) { line = line.trim(); query=query + line; if (line.equals("")) { //if (line.equals("\n")) { break; } int colonPos = line.indexOf(":"); if (colonPos > 0) { String key = line.substring(0, colonPos); String value = line.substring(colonPos + 1); headers.put(key, value.trim()); } } } File file=null; try { file = new File(_rootDir, URLDecoder.decode(path)); } catch (Exception e) {} file = file.getCanonicalFile(); if (!file.toString().startsWith(_rootDir.toString())) { // Uh-oh, it looks like some lamer is trying to take a peek // outside of our web root directory. Logger.log(ip, request, 404); out.write(("HTTP/1.0 403 Forbidden\r\n" + "Content-Type: text/html\r\n" + "Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" + "\r\n" + "

403 Forbidden

" + path + "


" + "" + WebServerConfig.VERSION + "").getBytes()); out.flush(); _socket.close(); return; } if (file.isDirectory()) { // Check to see if there are any index files in the directory. for (int i = 0; i < WebServerConfig.DEFAULT_FILES.length; i++) { File indexFile = new File(file, WebServerConfig.DEFAULT_FILES[i]); if (indexFile.exists() && !indexFile.isDirectory()) { file = indexFile; break; } } if (file.isDirectory()) { // print directory listing Logger.log(ip, request, 200); if (!path.endsWith("/")) { path = path + "/"; } File[] files = file.listFiles(); out.write(("HTTP/1.0 200 OK\r\n" + "Content-Type: text/html\r\n" + "Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" + "\r\n" + "

Directory Listing

" + "

" + path + "

" + "" + "" + "").getBytes()); for (int i = 0; i < files.length; i++) { file = files[i]; if (file.isDirectory()) { out.write(("").getBytes()); } else { out.write(("").getBytes()); } } out.write(("
Filename
SizeLast Modified
../
" + file.getName() + "/
" + file.getName() + "" + file.length() + "" + new Date(file.lastModified()).toString() + "

" + "" + WebServerConfig.VERSION + "").getBytes()); out.flush(); _socket.close(); return; } } if (file.exists()) { Logger.log("plee file:" , file.getName() , 1000); } if (!file.exists()) { // The file was not found. Logger.log(ip, request, 404); out.write(("HTTP/1.0 404 File Not Found\r\n" + "Content-Type: text/html\r\n" + "Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" + "\r\n" + "

404 File Not Found

" + path + "


" + "" + WebServerConfig.VERSION + "").getBytes()); out.flush(); _socket.close(); return; } String extension = WebServerConfig.getExtension(file); // Execute any files in any cgi-bin directories under the web root. if (file.getParent().indexOf("cgi-bin") >= 0) { try { out.write("HTTP/1.0 200 OK\r\n".getBytes()); ServerSideScriptEngine.execute(out, query, file, path); out.flush(); Logger.log(ip, path, 200); } catch (Throwable t) { // Internal server error! Logger.log(ip, request, 500); out.write(("Content-Type: text/html\r\n\r\n" + "

Internal Server Error

" + path + "
Your script produced the following error: -

" +
                               t.toString() + 
                               "

" + WebServerConfig.VERSION + "").getBytes()); out.flush(); _socket.close(); return; } out.flush(); _socket.close(); return; } reader = new BufferedInputStream(new FileInputStream(file)); Logger.log(ip, request, 200); String contentType = (String)WebServerConfig.MIME_TYPES.get(extension); if (contentType == null) { contentType = "application/octet-stream"; } out.write(("HTTP/1.0 200 OK\r\n" + "Date: " + new Date().toString() + "\r\n" + "Server: JibbleWebServer/1.0\r\n" + "Content-Type: " + contentType + "\r\n" + "Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" + "Content-Length: " + file.length() + "\r\n" + "Last-modified: " + new Date(file.lastModified()).toString() + "\r\n" + "\r\n").getBytes()); if (WebServerConfig.SSI_EXTENSIONS.contains(extension)) { reader.close(); ServerSideIncludeEngine.deliverDocument(out, file); _socket.close(); return; } String line=""; if ((line = in.readLine()) != null) { System.out.println("plee:another line: "); Logger.log("plee last line[ " , line, 1000); } byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = reader.read(buffer, 0, 4096)) != -1) { out.write(buffer, 0, bytesRead); bytesSent += bytesRead; } System.out.print("plee last in req "); for (int i=0;i<4095;i++) System.out.print((char) buffer[i]); System.out.println("after the above last line"); out.flush(); reader.close(); _socket.close(); } catch (IOException e) { Logger.log(ip, "ERROR " + e.toString() + " " + request, 0); if (reader != null) { try { reader.close(); } catch (Exception anye) { // Do nothing. } } } } private Socket _socket; private File _rootDir; }