1 package com.quadcap.http.server22; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.BufferedOutputStream ; 43 import java.io.ByteArrayOutputStream ; 44 import java.io.FileOutputStream ; 45 import java.io.InputStream ; 46 import java.io.IOException ; 47 import java.io.OutputStream ; 48 import java.io.OutputStreamWriter ; 49 import java.io.PrintStream ; 50 import java.io.PrintWriter ; 51 52 import java.net.InetAddress ; 53 import java.net.Socket ; 54 import java.net.SocketException ; 55 56 import java.util.Enumeration ; 57 import java.util.Hashtable ; 58 import java.util.Vector ; 59 60 import javax.servlet.RequestDispatcher ; 61 import javax.servlet.Servlet ; 62 import javax.servlet.ServletException ; 63 import javax.servlet.SingleThreadModel ; 64 65 import javax.servlet.http.HttpServlet ; 66 import javax.servlet.http.HttpServletResponse ; 67 68 import com.quadcap.io.LogInputStream; 69 import com.quadcap.io.LogOutputStream; 70 import com.quadcap.io.IO; 71 72 import com.quadcap.util.Config; 73 import com.quadcap.util.Debug; 74 75 import com.quadcap.net.server.Server; 76 import com.quadcap.net.server.Worker; 77 78 84 public class WebWorker extends Worker { 85 WebServer s; 86 87 HttpInputStream is = new HttpInputStream(); 88 HttpOutputStream os = new HttpOutputStream(); 89 HttpRequest req = new HttpRequest(this); 90 HttpResponse res = new HttpResponse(this); 91 92 93 public void init(Server server, Object context) { 94 super.init(server, context); 95 this.s = (WebServer)context; 96 res.setFixedHeader("Server", s.getServerInfo()); 97 req.setResponse(res); 98 } 99 100 103 public void doSession() { 104 checkPermission(); 105 boolean close = true; 106 int cnt = 0; 108 do { 109 String uri = ""; 110 String proto = HttpRequest.proto_10; 111 try { 112 cnt++; 115 is.reset(getInputStream()); 116 os.reset(getOutputStream()); 118 try { 120 req.reset(is); 121 } catch (SocketException e) { 122 try { 123 os.reallyClose(); 124 } catch (Throwable t) {} 125 return; 126 } 127 if (req.badRequest()) { 129 try { 130 os.reallyClose(); 131 } catch (Throwable t) {} 132 return; 133 } 134 res.reset(os); 135 req.setResponse(res); 137 139 uri = req.getRequestURI(); 140 WebApplication app = s.getContext(uri); 142 proto = req.getProtocol(); 145 if (proto == HttpRequest.proto_11) { 147 String conn = req.getHeader("Connection"); 148 close = false; 149 if (conn != null) { 150 close = conn.equalsIgnoreCase("Close"); 152 } 153 } 154 156 res.setProtocol(proto); 158 res.setKeepAlive(!close); 159 if (app != null) { 161 String subPath = uri.substring(app.getContextPath().length()); 162 HttpDispatcher rd = (HttpDispatcher)app.getRequestDispatcher(subPath); 163 if (rd != null) { 164 try { 165 rd.service(req, res); 166 } catch (Throwable th) { 167 Debug.print(th); 168 showException(th, req, res); 169 } finally { 170 if (res.getContentLength() < 0) close = true; 171 if (Trace.level() > 0) { 172 Debug.println("[" + req.getMethod() + " " + 173 req.getRequestURI() + "] " + 174 res.sc + " " + res.sm); 175 } 176 res.flush(); 178 } 180 } else { 181 res.sendError(HttpServletResponse.SC_NOT_FOUND, 182 "Not Found (request dispatcher)"); 183 } 184 } else { 185 res.sendError(HttpServletResponse.SC_NOT_FOUND, 186 "Not Found (webapp)"); 187 } 188 } catch (SocketException e) { 189 close = true; 190 } catch (Throwable e) { 191 Debug.println(1, e.toString()); 192 e.printStackTrace(Debug.debugStream); 193 close = true; 194 } finally { 195 String qs = req.getQueryString(); 196 if (qs == null) { 197 qs = ""; 198 } else { 199 qs = "?" + qs; 200 } 201 s.requestLog(getRemoteHost() + " " + getRemoteAddr() + " " + 202 req.getMethod() + " " + uri + qs + " " + res.sc); 203 } 204 205 } while (!close); 206 207 if (os != null) { 209 try { 210 os.close(); 211 } catch (Exception e1) {} 212 } 213 } 215 216 final void showException(Throwable t, HttpRequest req, HttpResponse res) { 217 try { 218 res.reset(); 219 res.setContentType("text/html"); 220 PrintWriter pw = null; 221 if (res.getOutputStreamCalled) { 222 OutputStream os1 = res.getOutputStream(); 223 OutputStreamWriter w = new OutputStreamWriter (os1); 224 pw = new PrintWriter (w); 225 } else { 226 pw = res.getWriter(); 227 } 228 pw.println("<h3>Servlet Exception Report</h3>"); 229 pw.println("<pre>"); 230 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 231 PrintStream p = new PrintStream (bos); 232 t.printStackTrace(p); 233 if (t instanceof ServletException ) { 234 Throwable t1 = ((ServletException )t).getRootCause(); 235 if (t1 != null) { 236 p.println(""); 237 p.println("<b>Root Cause</b>:"); 238 t1.printStackTrace(p); 239 } 240 } 241 242 p.flush(); 243 pw.println(bos.toString()); 244 bos = null; 245 p = null; 246 pw.println("contextPath = " + req.getContextPath()); 247 pw.println("servletPath = " + req.getServletPath()); 248 pw.println("pathInfo = " + req.getPathInfo()); 249 pw.println("Headers:"); 250 Enumeration e1 = req.getHeaderNames(); 251 while (e1.hasMoreElements()) { 252 String name = e1.nextElement().toString(); 253 Enumeration e2 = req.getHeaders(name); 254 while (e2.hasMoreElements()) { 255 String val = e2.nextElement().toString(); 256 pw.println(name + ": " + val); 257 } 258 } 259 pw.println("</pre>"); 260 } catch (Throwable th) { 261 Debug.print(th); 262 } 263 } 264 265 268 public HttpOutputStream getHttpOutputStream() { return os; } 269 270 273 public HttpInputStream getHttpInputStream() { return is; } 274 275 278 public WebServer getHttpServer() { return s; } 279 280 void checkPermission() { 281 if (String.valueOf(System.getProperty("public")).equals("true")) { 282 return; 283 } 284 if (String.valueOf(Config.getProperty("public")).equals("true")) { 285 return; 286 } 287 InetAddress ia = socket.getInetAddress(); 288 if (ia.toString().equals("/127.0.0.1")) return; 289 try { 290 if (ia.equals(InetAddress.getLocalHost())) { 291 return; 292 } 293 } catch (Throwable t) {} 294 throw new RuntimeException ("No permission: " + ia); 295 } 296 } 297 | Popular Tags |