📄 defaultservlet.java
字号:
pathInfo=(String)request.getAttribute(Dispatcher.__INCLUDE_PATH_INFO); if (servletPath==null) { servletPath=request.getServletPath(); pathInfo=request.getPathInfo(); } } else { included=Boolean.FALSE; servletPath=request.getServletPath(); pathInfo=request.getPathInfo(); // Is this a range request? reqRanges = request.getHeaders(HttpHeaders.RANGE); if (reqRanges!=null && !reqRanges.hasMoreElements()) reqRanges=null; } String pathInContext=URIUtil.addPaths(servletPath,pathInfo); boolean endsWithSlash=pathInContext.endsWith(URIUtil.SLASH); // Can we gzip this request? String pathInContextGz=null; boolean gzip=false; if (!included.booleanValue() && _gzip && reqRanges==null && !endsWithSlash ) { String accept=request.getHeader(HttpHeaders.ACCEPT_ENCODING); if (accept!=null && accept.indexOf("gzip")>=0) gzip=true; } // Find the resource and content Resource resource=null; HttpContent content=null; Connector connector = HttpConnection.getCurrentConnection().getConnector(); ResourceCache cache=(connector instanceof NIOConnector) ?_nioCache:_bioCache; try { // Try gzipped content first if (gzip) { pathInContextGz=pathInContext+".gz"; resource=getResource(pathInContextGz); if (resource==null || !resource.exists()|| resource.isDirectory()) { gzip=false; pathInContextGz=null; } else if (cache!=null) { content=cache.lookup(pathInContextGz,resource); if (content!=null) resource=content.getResource(); } if (resource==null || !resource.exists()|| resource.isDirectory()) { gzip=false; pathInContextGz=null; } } // find resource if (!gzip) { if (cache==null) resource=getResource(pathInContext); else { content=cache.lookup(pathInContext,this); if (content!=null) resource=content.getResource(); else resource=getResource(pathInContext); } } if (Log.isDebugEnabled()) Log.debug("resource="+resource+(content!=null?" content":"")); // Handle resource if (resource==null || !resource.exists()) response.sendError(HttpServletResponse.SC_NOT_FOUND); else if (!resource.isDirectory()) { // ensure we have content if (content==null) content=new UnCachedContent(resource); if (included.booleanValue() || passConditionalHeaders(request,response, resource,content)) { if (gzip) { response.setHeader(HttpHeaders.CONTENT_ENCODING,"gzip"); String mt=_context.getMimeType(pathInContext); if (mt!=null) response.setContentType(mt); } sendData(request,response,included.booleanValue(),resource,content,reqRanges); } } else { String welcome=null; if (!endsWithSlash || (pathInContext.length()==1 && request.getAttribute("org.mortbay.jetty.nullPathInfo")!=null)) { StringBuffer buf=request.getRequestURL(); int param=buf.lastIndexOf(";"); if (param<0) buf.append('/'); else buf.insert(param,'/'); String q=request.getQueryString(); if (q!=null&&q.length()!=0) { buf.append('?'); buf.append(q); } response.setContentLength(0); response.sendRedirect(response.encodeRedirectURL(buf.toString())); } // else look for a welcome file else if (null!=(welcome=getWelcomeFile(resource))) { String ipath=URIUtil.addPaths(pathInContext,welcome); if (_redirectWelcome) { // Redirect to the index response.setContentLength(0); String q=request.getQueryString(); if (q!=null&&q.length()!=0) response.sendRedirect(URIUtil.addPaths( _context.getContextPath(),ipath)+"?"+q); else response.sendRedirect(URIUtil.addPaths( _context.getContextPath(),ipath)); } else { // Forward to the index RequestDispatcher dispatcher=request.getRequestDispatcher(ipath); if (dispatcher!=null) { if (included.booleanValue()) dispatcher.include(request,response); else { request.setAttribute("org.mortbay.jetty.welcome",ipath); dispatcher.forward(request,response); } } } } else { content=new UnCachedContent(resource); if (included.booleanValue() || passConditionalHeaders(request,response, resource,content)) sendDirectory(request,response,resource,pathInContext.length()>1); } } } catch(IllegalArgumentException e) { Log.warn(Log.EXCEPTION,e); if(!response.isCommitted()) response.sendError(500, e.getMessage()); } finally { if (content!=null) content.release(); else if (resource!=null) resource.release(); } } /* ------------------------------------------------------------ */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } /* ------------------------------------------------------------ */ /* (non-Javadoc) * @see javax.servlet.http.HttpServlet#doTrace(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } /* ------------------------------------------------------------ */ /** * Finds a matching welcome file for the supplied {@link Resource}. This will be the first entry in the list of * configured {@link #_welcomes welcome files} that existing within the directory referenced by the <code>Resource</code>. * If the resource is not a directory, or no matching file is found, then it may look for a valid servlet mapping. * If there is none, then <code>null</code> is returned. * The list of welcome files is read from the {@link ContextHandler} for this servlet, or * <code>"index.jsp" , "index.html"</code> if that is <code>null</code>. * @param resource * @return The name of the matching welcome file. * @throws IOException * @throws MalformedURLException */ private String getWelcomeFile(Resource resource) throws MalformedURLException, IOException { if (!resource.isDirectory() || _welcomes==null) return null; for (int i=0;i<_welcomes.length;i++) { Resource welcome=resource.addPath(_welcomes[i]); if (welcome.exists()) return _welcomes[i]; } if (_welcomeServlets) { ServletHandler servletHandler = (ServletHandler)_context.getContextHandler().getChildHandlerByClass(ServletHandler.class); for (int i=0;i<_welcomes.length;i++) { if (servletHandler.matchesPath(_welcomes[i])) return _welcomes[i]; } } return null; } /* ------------------------------------------------------------ */ /* Check modification date headers. */ protected boolean passConditionalHeaders(HttpServletRequest request,HttpServletResponse response, Resource resource, HttpContent content) throws IOException { try { if (!request.getMethod().equals(HttpMethods.HEAD) ) { String ifms=request.getHeader(HttpHeaders.IF_MODIFIED_SINCE); if (ifms!=null) { if (content!=null) { Buffer mdlm=content.getLastModified(); if (mdlm!=null) { if (ifms.equals(mdlm.toString())) { response.reset(); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); response.flushBuffer(); return false; } } } long ifmsl=request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); if (ifmsl!=-1) { if (resource.lastModified()/1000 <= ifmsl/1000) { response.reset(); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); response.flushBuffer(); return false; } } } // Parse the if[un]modified dates and compare to resource long date=request.getDateHeader(HttpHeaders.IF_UNMODIFIED_SINCE); if (date!=-1) { if (resource.lastModified()/1000 > date/1000) { response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); return false; } } } } catch(IllegalArgumentException iae) { if(!response.isCommitted()) response.sendError(400, iae.getMessage()); throw iae; } return true; } /* ------------------------------------------------------------------- */ protected void sendDirectory(HttpServletRequest request, HttpServletResponse response, Resource resource, boolean parent) throws IOException { if (!_dirAllowed) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } byte[] data=null; String base = URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH); String dir = resource.getListHTML(base,parent); if (dir==null) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "No directory"); return; } data=dir.getBytes("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setContentLength(data.length); response.getOutputStream().write(data); } /* ------------------------------------------------------------ */ protected void sendData(HttpServletRequest request,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -