4545import io .undertow .servlet .handlers .ServletRequestContext ;
4646import io .undertow .servlet .handlers .ServletChain ;
4747import io .undertow .servlet .handlers .ServletPathMatch ;
48- import io .undertow .util .QueryParameterUtils ;
48+ import io .undertow .servlet .util .DispatchUtils ;
49+ import io .undertow .util .ParameterLimitException ;
4950
5051/**
5152 * @author Stuart Douglas
@@ -55,32 +56,20 @@ public class RequestDispatcherImpl implements RequestDispatcher {
5556 private final String path ;
5657 private final ServletContextImpl servletContext ;
5758 private final ServletChain chain ;
58- private final ServletPathMatch pathMatch ;
5959 private final boolean named ;
6060
6161 public RequestDispatcherImpl (final String path , final ServletContextImpl servletContext ) {
6262 this .path = path ;
6363 this .servletContext = servletContext ;
64- String basePath = path ;
65- int qPos = basePath .indexOf ("?" );
66- if (qPos != -1 ) {
67- basePath = basePath .substring (0 , qPos );
68- }
69- int mPos = basePath .indexOf (";" );
70- if (mPos != -1 ) {
71- basePath = basePath .substring (0 , mPos );
72- }
73- this .pathMatch = servletContext .getDeployment ().getServletPaths ().getServletHandlerByPath (basePath );
74- this .chain = pathMatch .getServletChain ();
7564 this .named = false ;
65+ this .chain = null ;
7666 }
7767
7868 public RequestDispatcherImpl (final ServletChain chain , final ServletContextImpl servletContext ) {
7969 this .chain = chain ;
8070 this .named = true ;
8171 this .servletContext = servletContext ;
8272 this .path = null ;
83- this .pathMatch = null ;
8473 }
8574
8675
@@ -169,8 +158,6 @@ private void forwardImpl(ServletRequest request, ServletResponse response, Servl
169158 final ServletRequest oldRequest = servletRequestContext .getServletRequest ();
170159 final ServletResponse oldResponse = servletRequestContext .getServletResponse ();
171160
172- Map <String , Deque <String >> queryParameters = requestImpl .getQueryParameters ();
173-
174161 request .removeAttribute (INCLUDE_REQUEST_URI );
175162 request .removeAttribute (INCLUDE_CONTEXT_PATH );
176163 request .removeAttribute (INCLUDE_SERVLET_PATH );
@@ -181,38 +168,14 @@ private void forwardImpl(ServletRequest request, ServletResponse response, Servl
181168 final String oldRequestPath = requestImpl .getExchange ().getRequestPath ();
182169 final String oldPath = requestImpl .getExchange ().getRelativePath ();
183170 final ServletPathMatch oldServletPathMatch = requestImpl .getExchange ().getAttachment (ServletRequestContext .ATTACHMENT_KEY ).getServletPathMatch ();
184- if (!named ) {
185-
186- //only update if this is the first forward
187- if (request .getAttribute (FORWARD_REQUEST_URI ) == null ) {
188- requestImpl .setAttribute (FORWARD_REQUEST_URI , requestImpl .getRequestURI ());
189- requestImpl .setAttribute (FORWARD_CONTEXT_PATH , requestImpl .getContextPath ());
190- requestImpl .setAttribute (FORWARD_SERVLET_PATH , requestImpl .getServletPath ());
191- requestImpl .setAttribute (FORWARD_PATH_INFO , requestImpl .getPathInfo ());
192- requestImpl .setAttribute (FORWARD_QUERY_STRING , requestImpl .getQueryString ());
193- }
194171
195- int qsPos = path .indexOf ("?" );
196- String newServletPath = path ;
197- if (qsPos != -1 ) {
198- String newQueryString = newServletPath .substring (qsPos + 1 );
199- newServletPath = newServletPath .substring (0 , qsPos );
200-
201- String encoding = QueryParameterUtils .getQueryParamEncoding (servletRequestContext .getExchange ());
202- Map <String , Deque <String >> newQueryParameters = QueryParameterUtils .mergeQueryParametersWithNewQueryString (queryParameters , newQueryString , encoding );
203- requestImpl .getExchange ().setQueryString (newQueryString );
204- requestImpl .setQueryParameters (newQueryParameters );
172+ ServletPathMatch pathMatch = null ;
173+ if (!named ) {
174+ try {
175+ pathMatch = DispatchUtils .dispatchForward (path , requestImpl , responseImpl , servletContext );
176+ } catch (ParameterLimitException e ) {
177+ throw new ServletException (e );
205178 }
206- String newRequestUri = servletContext .getContextPath () + newServletPath ;
207-
208-
209-
210- requestImpl .getExchange ().setRelativePath (newServletPath );
211- requestImpl .getExchange ().setRequestPath (newRequestUri );
212- requestImpl .getExchange ().setRequestURI (newRequestUri );
213- requestImpl .getExchange ().getAttachment (ServletRequestContext .ATTACHMENT_KEY ).setServletPathMatch (pathMatch );
214- requestImpl .setServletContext (servletContext );
215- responseImpl .setServletContext (servletContext );
216179 }
217180
218181 try {
@@ -241,9 +204,7 @@ private void forwardImpl(ServletRequest request, ServletResponse response, Servl
241204 }
242205 }
243206 }
244- } catch (ServletException e ) {
245- throw e ;
246- } catch (IOException e ) {
207+ } catch (ServletException | IOException e ) {
247208 throw e ;
248209 } catch (Exception e ) {
249210 throw new RuntimeException (e );
@@ -348,32 +309,19 @@ private void includeImpl(ServletRequest request, ServletResponse response, Servl
348309 Object queryString = null ;
349310 Map <String , Deque <String >> queryParameters = requestImpl .getQueryParameters ();
350311
312+ ServletPathMatch pathMatch = null ;
351313 if (!named ) {
352314 requestUri = request .getAttribute (INCLUDE_REQUEST_URI );
353315 contextPath = request .getAttribute (INCLUDE_CONTEXT_PATH );
354316 servletPath = request .getAttribute (INCLUDE_SERVLET_PATH );
355317 pathInfo = request .getAttribute (INCLUDE_PATH_INFO );
356318 queryString = request .getAttribute (INCLUDE_QUERY_STRING );
357319
358- int qsPos = path .indexOf ("?" );
359- String newServletPath = path ;
360- if (qsPos != -1 ) {
361- String newQueryString = newServletPath .substring (qsPos + 1 );
362- newServletPath = newServletPath .substring (0 , qsPos );
363-
364- String encoding = QueryParameterUtils .getQueryParamEncoding (servletRequestContext .getExchange ());
365- Map <String , Deque <String >> newQueryParameters = QueryParameterUtils .mergeQueryParametersWithNewQueryString (queryParameters , newQueryString , encoding );
366- requestImpl .setQueryParameters (newQueryParameters );
367- requestImpl .setAttribute (INCLUDE_QUERY_STRING , newQueryString );
368- } else {
369- requestImpl .setAttribute (INCLUDE_QUERY_STRING , "" );
320+ try {
321+ pathMatch = DispatchUtils .dispatchInclude (path , requestImpl , responseImpl , servletContext );
322+ } catch (ParameterLimitException e ) {
323+ throw new ServletException (e );
370324 }
371- String newRequestUri = servletContext .getContextPath () + newServletPath ;
372-
373- requestImpl .setAttribute (INCLUDE_REQUEST_URI , newRequestUri );
374- requestImpl .setAttribute (INCLUDE_CONTEXT_PATH , servletContext .getContextPath ());
375- requestImpl .setAttribute (INCLUDE_SERVLET_PATH , pathMatch .getMatched ());
376- requestImpl .setAttribute (INCLUDE_PATH_INFO , pathMatch .getRemaining ());
377325 }
378326 boolean inInclude = responseImpl .isInsideInclude ();
379327 responseImpl .setInsideInclude (true );
@@ -386,10 +334,9 @@ private void includeImpl(ServletRequest request, ServletResponse response, Servl
386334 try {
387335 servletRequestContext .setServletRequest (request );
388336 servletRequestContext .setServletResponse (response );
389- servletContext .getDeployment ().getServletDispatcher ().dispatchToServlet (requestImpl .getExchange (), chain , DispatcherType .INCLUDE );
390- } catch (ServletException e ) {
391- throw e ;
392- } catch (IOException e ) {
337+ servletContext .getDeployment ().getServletDispatcher ().dispatchToServlet (requestImpl .getExchange (),
338+ named ? chain : pathMatch .getServletChain (), DispatcherType .INCLUDE );
339+ } catch (ServletException |IOException e ) {
393340 throw e ;
394341 } catch (Exception e ) {
395342 throw new RuntimeException (e );
@@ -452,59 +399,20 @@ private void error(ServletRequestContext servletRequestContext, final ServletReq
452399
453400 final ServletRequest oldRequest = servletRequestContext .getServletRequest ();
454401 final ServletResponse oldResponse = servletRequestContext .getServletResponse ();
455- servletRequestContext .setDispatcherType (DispatcherType .ERROR );
456-
457- //only update if this is the first forward, add forward attrs too
458- if (request .getAttribute (FORWARD_REQUEST_URI ) == null ) {
459- requestImpl .setAttribute (FORWARD_REQUEST_URI , requestImpl .getRequestURI ());
460- requestImpl .setAttribute (FORWARD_CONTEXT_PATH , requestImpl .getContextPath ());
461- requestImpl .setAttribute (FORWARD_SERVLET_PATH , requestImpl .getServletPath ());
462- requestImpl .setAttribute (FORWARD_PATH_INFO , requestImpl .getPathInfo ());
463- requestImpl .setAttribute (FORWARD_QUERY_STRING , requestImpl .getQueryString ());
464- }
465- requestImpl .setAttribute (ERROR_REQUEST_URI , requestImpl .getRequestURI ());
466- requestImpl .setAttribute (ERROR_SERVLET_NAME , servletName );
467- if (exception != null ) {
468- if (exception instanceof ServletException && ((ServletException )exception ).getRootCause () != null ) {
469- requestImpl .setAttribute (ERROR_EXCEPTION , ((ServletException ) exception ).getRootCause ());
470- requestImpl .setAttribute (ERROR_EXCEPTION_TYPE , ((ServletException ) exception ).getRootCause ().getClass ());
471- } else {
472- requestImpl .setAttribute (ERROR_EXCEPTION , exception );
473- requestImpl .setAttribute (ERROR_EXCEPTION_TYPE , exception .getClass ());
474- }
475- }
476- requestImpl .setAttribute (ERROR_MESSAGE , message );
477- requestImpl .setAttribute (ERROR_STATUS_CODE , responseImpl .getStatus ());
478-
479- int qsPos = path .indexOf ("?" );
480- String newServletPath = path ;
481- if (qsPos != -1 ) {
482- Map <String , Deque <String >> queryParameters = requestImpl .getQueryParameters ();
483- String newQueryString = newServletPath .substring (qsPos + 1 );
484- newServletPath = newServletPath .substring (0 , qsPos );
485-
486- String encoding = QueryParameterUtils .getQueryParamEncoding (servletRequestContext .getExchange ());
487- Map <String , Deque <String >> newQueryParameters = QueryParameterUtils .mergeQueryParametersWithNewQueryString (queryParameters , newQueryString , encoding );
488- requestImpl .getExchange ().setQueryString (newQueryString );
489- requestImpl .setQueryParameters (newQueryParameters );
490- }
491- String newRequestUri = servletContext .getContextPath () + newServletPath ;
492402
493- requestImpl . getExchange (). setRelativePath ( newServletPath ) ;
494- requestImpl . getExchange (). setRequestPath ( newRequestUri );
495- requestImpl . getExchange (). setRequestURI ( newRequestUri );
496- requestImpl . getExchange (). getAttachment ( ServletRequestContext . ATTACHMENT_KEY ). setServletPathMatch ( pathMatch );
497- requestImpl . setServletContext ( servletContext );
498- responseImpl . setServletContext ( servletContext );
403+ ServletPathMatch pathMatch ;
404+ try {
405+ pathMatch = DispatchUtils . dispatchError ( path , servletName , exception , message , requestImpl , responseImpl , servletContext );
406+ } catch ( ParameterLimitException e ) {
407+ throw new ServletException ( e );
408+ }
499409
500410 try {
501411 try {
502412 servletRequestContext .setServletRequest (request );
503413 servletRequestContext .setServletResponse (response );
504414 servletContext .getDeployment ().getServletDispatcher ().dispatchToPath (requestImpl .getExchange (), pathMatch , DispatcherType .ERROR );
505- } catch (ServletException e ) {
506- throw e ;
507- } catch (IOException e ) {
415+ } catch (ServletException | IOException e ) {
508416 throw e ;
509417 } catch (Exception e ) {
510418 throw new RuntimeException (e );
0 commit comments