@Override public void handleRequest(HttpServerExchange exchange) throws Exception { String old = exchange.getAttachment(OLD_RELATIVE_PATH); if(!old.equals(exchange.getRelativePath())) { ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); ServletPathMatch info = src.getDeployment().getServletPaths().getServletHandlerByPath(exchange.getRelativePath()); src.setCurrentServlet(info.getServletChain()); src.setServletPathMatch(info); } handler.handleRequest(exchange); } };
if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) { exchange.getResponseHeaders().put(Headers.LOCATION, RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/", true)); return; } else if (info.getType() == ServletPathMatch.Type.REWRITE) { exchange.setRelativePath(exchange.getRelativePath() + info.getRewriteLocation()); exchange.setRequestURI(exchange.getRequestURI() + info.getRewriteLocation()); exchange.setRequestPath(exchange.getRequestPath() + info.getRewriteLocation()); final ServletRequestContext servletRequestContext = new ConvergedServletRequestContext(convergedServletContext.getDeployment(), convergedRequest, convergedResponse, info); if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) { exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize()); servletRequestContext.setServletPathMatch(info); Executor executor = info.getServletChain().getExecutor(); if (executor == null) { executor = convergedServletContext.getDeployment().getExecutor(); dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
private ServletPathMatch findWelcomeServlet(final String path, boolean requiresRedirect) { StringBuilder sb = new StringBuilder(); for (String i : welcomePages) { sb.append(path); sb.append(i); final String mergedPath = sb.toString(); sb.setLength(0); final ServletPathMatch handler = data.getServletHandlerByPath(mergedPath); if (handler != null && !handler.isRequiredWelcomeFileMatch()) { return new ServletPathMatch(handler.getServletChain(), handler.getMatched(), handler.getRemaining(), requiresRedirect ? REDIRECT : REWRITE, mergedPath); } } return null; }
switch (match.getMappingMatch()) { case EXACT: matchValue = match.getMatched(); if(matchValue.startsWith("/")) { matchValue = matchValue.substring(1); break; case PATH: matchValue = match.getRemaining(); if(matchValue.startsWith("/")) { matchValue = matchValue.substring(1); matchValue = match.getMatched().substring(0, match.getMatched().length() - match.getMatchString().length() + 1); if(matchValue.startsWith("/")) { matchValue = matchValue.substring(1); matchValue = match.getRemaining(); return new MappingImpl(matchValue, match.getMatchString(), match.getMappingMatch(), match.getServletChain().getManagedServlet().getServletInfo().getName());
private ServletPathMatch findWelcomeFile(final String path, boolean requiresRedirect) { if(File.separatorChar != '/' && path.contains(File.separator)) { return null; } StringBuilder sb = new StringBuilder(); for (String i : welcomePages) { try { sb.append(path); sb.append(i); final String mergedPath = sb.toString(); sb.setLength(0); Resource resource = resourceManager.getResource(mergedPath); if (resource != null) { final ServletPathMatch handler = data.getServletHandlerByPath(mergedPath); return new ServletPathMatch(handler.getServletChain(), mergedPath, null, requiresRedirect ? REDIRECT : REWRITE, mergedPath); } } catch (IOException e) { } } return null; }
ServletPathMatchesData(final Map<String, ServletChain> exactPathMatches, final SubstringMap<PathMatch> prefixMatches, final Map<String, ServletChain> nameMatches) { this.prefixMatches = prefixMatches; this.nameMatches = nameMatches; Map<String, ServletPathMatch> newExactPathMatches = new HashMap<>(); for (Map.Entry<String, ServletChain> entry : exactPathMatches.entrySet()) { newExactPathMatches.put(entry.getKey(), new ServletPathMatch(entry.getValue(), entry.getKey(), entry.getValue().isDefaultServletMapping())); } this.exactPathMatches = newExactPathMatches; }
if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) { exchange.getResponseHeaders().put(Headers.LOCATION, RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/", true)); return; } else if (info.getType() == ServletPathMatch.Type.REWRITE) { exchange.setRelativePath(exchange.getRelativePath() + info.getRewriteLocation()); exchange.setRequestURI(exchange.getRequestURI() + info.getRewriteLocation()); exchange.setRequestPath(exchange.getRequestPath() + info.getRewriteLocation()); final ServletRequestContext servletRequestContext = new ConvergedServletRequestContext(convergedServletContext.getDeployment(), convergedRequest, convergedResponse, info); if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) { exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize()); servletRequestContext.setServletPathMatch(info); Executor executor = info.getServletChain().getExecutor(); if (executor == null) { executor = convergedServletContext.getDeployment().getExecutor(); dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
if (!match.isRequiredWelcomeFileMatch()) { pathMatchCache.add(path, match); return match; String remaining = match.getRemaining() == null ? match.getMatched() : match.getRemaining(); Resource resource = resourceManager.getResource(remaining); if (resource == null || !resource.isDirectory()) { return match; } else { ServletPathMatch redirect = new ServletPathMatch(match.getServletChain(), match.getMatched(), match.getRemaining(), REDIRECT, "/"); pathMatchCache.add(path, redirect); return redirect;
private ServletPathMatch handleMatch(final String path, final PathMatch match, final int extensionPos) { if (match.extensionMatches.isEmpty()) { return new ServletPathMatch(match.defaultHandler, path, match.requireWelcomeFileMatch); } if (extensionPos == -1) { return new ServletPathMatch(match.defaultHandler, path, match.requireWelcomeFileMatch); } final String ext; ext = path.substring(extensionPos + 1, path.length()); ServletChain handler = match.extensionMatches.get(ext); if (handler != null) { return new ServletPathMatch(handler, path, handler.getManagedServlet().getServletInfo().isRequireWelcomeFileMapping()); } return new ServletPathMatch(match.defaultHandler, path, match.requireWelcomeFileMatch); }
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { if(System.getSecurityManager() == null) { dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST); } else { //sometimes thread pools inherit some random AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception{ dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST); return null; } }); } } });
if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) { } else if (info.getType() == ServletPathMatch.Type.REWRITE) { exchange.setRelativePath(info.getRewriteLocation()); exchange.setRequestPath(exchange.getResolvedPath() + info.getRewriteLocation()); final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), request, response, info); if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) { exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize()); servletRequestContext.setServletPathMatch(info); Executor executor = info.getServletChain().getExecutor(); if (executor == null) { executor = servletContext.getDeployment().getExecutor(); dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { if(System.getSecurityManager() == null) { dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST); } else { //sometimes thread pools inherit some random AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception{ dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST); return null; } }); } } });
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); if (System.getSecurityManager() == null) { dispatchRequest(exchange, servletRequestContext, servletRequestContext.getOriginalServletPathMatch().getServletChain(), DispatcherType.REQUEST); } else { //sometimes thread pools inherit some random AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { dispatchRequest(exchange, servletRequestContext, servletRequestContext.getOriginalServletPathMatch().getServletChain(), DispatcherType.REQUEST); return null; } }); } } };
public RequestDispatcherImpl(final String path, final ServletContextImpl servletContext) { this.path = path; this.servletContext = servletContext; String basePath = path; int qPos = basePath.indexOf("?"); if (qPos != -1) { basePath = basePath.substring(0, qPos); } int mPos = basePath.indexOf(";"); if(mPos != -1) { basePath = basePath.substring(0, mPos); } this.pathMatch = servletContext.getDeployment().getServletPaths().getServletHandlerByPath(basePath); this.chain = pathMatch.getServletChain(); this.named = false; }
public void dispatchToPath(final HttpServerExchange exchange, final ServletPathMatch pathInfo, final DispatcherType dispatcherType) throws Exception { final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); servletRequestContext.setServletPathMatch(pathInfo); dispatchRequest(exchange, servletRequestContext, pathInfo.getServletChain(), dispatcherType); }
@Override public void setCharacterEncoding(final String env) throws UnsupportedEncodingException { if (readStarted) { return; } try { characterEncoding = Charset.forName(env); final ManagedServlet originalServlet = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getOriginalServletPathMatch().getServletChain().getManagedServlet(); final FormDataParser parser = originalServlet.getFormParserFactory().createParser(exchange); if (parser != null) { parser.setCharacterEncoding(env); } } catch (UnsupportedCharsetException e) { throw new UnsupportedEncodingException(); } }
private void verifyMultipartServlet() { ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); MultipartConfigElement multipart = src.getServletPathMatch().getServletChain().getManagedServlet().getMultipartConfig(); if(multipart == null) { throw UndertowServletMessages.MESSAGES.multipartConfigNotPresent(); } }
servletRequestContext.setServletResponse(response); if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) { exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize()); dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST); } catch (Exception e) { if (e instanceof RuntimeException) {
servletRequestContext.setServletResponse(response); if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) { exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize()); dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST); } catch (Exception e) { if (e instanceof RuntimeException) {
private void loadParts() throws IOException, ServletException { final ServletRequestContext requestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); if (parts == null) { final List<Part> parts = new ArrayList<>(); String mimeType = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE); if (mimeType != null && mimeType.startsWith(MultiPartParserDefinition.MULTIPART_FORM_DATA)) { FormData formData = parseFormData(); if(formData != null) { for (final String namedPart : formData) { for (FormData.FormValue part : formData.get(namedPart)) { parts.add(new PartImpl(namedPart, part, requestContext.getOriginalServletPathMatch().getServletChain().getManagedServlet().getMultipartConfig(), servletContext, this)); } } } } else { throw UndertowServletMessages.MESSAGES.notAMultiPartRequest(); } this.parts = parts; } }