/* 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" + "
" + path + "
Filename | Size | Last Modified |
../ | ||
" + file.getName() + "/ | ||
" + file.getName() + " | " + file.length() + " | " + new Date(file.lastModified()).toString() + " |
" + path + "
" + path + "
" + t.toString() + "