📄 jhttpserverthread.java
字号:
/*This file is part of JHttpServer.This package is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.JHttpServer is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with JHttpServer; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/// Title : HttpServerThread.java// Version : 0.92// Copyright : Copyright (c) 2001// Author : Florent CUETO (fcueto@wanadoo.fr)// Description : Http Server Thread (One instance per request)package jhttpserver;import javax.servlet.*;import javax.servlet.http.*;import java.net.*;import java.io.*;import java.util.*;public class JHttpServerThread extends Thread{ private JHttpServer server; private Socket socket; // Constructor public JHttpServerThread(JHttpServer server, Socket socket) { super(); this.server = server; this.socket = socket; } // Thread's run method public void run() { //System.out.println("Got connection from " + socket.getInetAddress().getHostAddress()); InputStream is; try { is = socket.getInputStream(); } catch (IOException e) { System.out.println("Error while initializing streams : " + e); closeSocket(); return; } boolean cont = true; int pos = 0; HttpServletRequestImpl request = new HttpServletRequestImpl(); request.setServerName(server.getServerName()); request.setServerPort(server.getServerPort()); request.setSocket(socket); while(cont) { // TO CHANGE (optimize) try { StringBuffer bLine = new StringBuffer(); boolean cont2 = true; boolean caution = false; while(cont2) { int c = is.read(); if (c == -1) cont2 = false; else { bLine.append((char)c); if (caution & (c == '\n')) cont2 = false; if (c == '\r') caution = true; else caution = false; } } String line = bLine.substring(0, bLine.length() - 2); if ((line != null) && (line.length() > 0)) { if (pos == 0) { // Command //System.out.println("COMMAND " + line); String[] tab = stringSplit(line, " ", false); request.setMethod(tab[0]); int qpos = tab[1].indexOf("?"); if (qpos >= 0) { request.setRequestURI(tab[1].substring(0, qpos)); // TO CHANGE request.setServletPath(tab[1].substring(0, qpos)); request.setQueryString(tab[1].substring(1 + qpos)); request.computeQueryParameters(); } else { request.setRequestURI(tab[1]); // TO CHANGE request.setServletPath(tab[1]); request.setQueryString(null); } request.setProtocol(tab[2]); } else { // Header //System.out.println("HEADER " + line); int tokenPos = line.indexOf(": "); String headerName = line.substring(0, tokenPos); String headerValue = line.substring(2 + tokenPos); request.setHeader(headerName, headerValue); } pos++; } else cont = false; } catch (IOException e) { //System.out.println("IOException in ThreadComm read : " + e); // Client closed the connection try { is.close(); } catch(IOException ioe){} closeSocket(); return; } } request.setInputStream(is); // HttpServletResponseImpl response = new HttpServletResponseImpl(); response.setServer(server); response.setSocket(socket); response.setProtocol(request.getProtocol()); try { response.setOutputStream(new BufferedOutputStream(socket.getOutputStream())); //response.setOutputStream(socket.getOutputStream()); } catch (IOException e) { System.out.println("Exception : " + e); } GenericServlet servlet = null; try { int spos = request.getRequestURI().indexOf("/servlet/"); String servletName = null; if (spos == -1) { if (server.getServerRoot() == null) throw new Exception("I'm not serving static documents"); servletName = "jhttpserver.ServletSendFile"; } else { servletName = request.getRequestURI().substring(9 + spos); } servlet = server.getLoadedServlet(servletName); if (servlet == null) { servlet = (HttpServlet)Class.forName(servletName).newInstance(); ServletConfigImpl servletConfigImpl = new ServletConfigImpl(); servletConfigImpl.setServletContext(server.getServletContext()); servletConfigImpl.setServletName(servletName); try { servlet.init(servletConfigImpl); } catch (ServletException se) { System.out.println("ServletException while init : " + se); } server.addLoadedServlet(servletName, servlet); } } catch (Exception e) { //System.out.println("Exception while creating servlet : " + e); try { response.sendError(HttpServletResponse.SC_NOT_FOUND); } catch(IOException ioe){} } if (servlet != null) { try { servlet.service(request, response); } catch (Exception e) { System.out.println("Exception while calling service : " + e); } } try { response.writeHead(); //response.flushBuffer(); response.close(); } catch (Exception e) { //System.out.println("Exception while closing response : " + e); } // Close input stream try { is.close(); } catch (IOException e) { System.out.println("Error while closing input stream : " + e); } // On ferme la socket closeSocket(); //System.out.println("Disconnected of " + socket.getInetAddress().getHostAddress()); } public void closeSocket() { try { socket.close(); } catch (IOException e) { System.out.println("Error while closing socket : " + e); } } // Split a string public static String[] stringSplit(String string, String tokens, boolean trimStrings) { if (string == null) return(null); if (string.length() == 0) return(new String[0]); Vector res = new Vector(); StringTokenizer stk = new StringTokenizer(string, tokens, false); while (stk.hasMoreTokens()) res.addElement(stk.nextToken()); String[] res2 = new String[res.size()]; for (int i = 0; i < res.size(); i++) { res2[i] = (String)res.elementAt(i); if (trimStrings) res2[i] = res2[i].trim(); } return(res2); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -