-
Notifications
You must be signed in to change notification settings - Fork 143
/
CoyoteAdapter.java
834 lines (713 loc) · 31.7 KB
/
CoyoteAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
* Copyright (c) 2022, 2022 Contributors to the Eclipse Foundation.
* Copyright (c) 1997-2018 Oracle and/or its affiliates. All rights reserved.
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.connector;
import static jakarta.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;
import static org.apache.catalina.Globals.SESSION_PARAMETER_NAME;
import static org.apache.catalina.LogFacade.FAILED_TO_INITIALIZE_THE_INTERCEPTOR;
import static org.apache.catalina.LogFacade.HTTP_LISTENER_DISABLED;
import static org.apache.catalina.LogFacade.INTERNAL_ERROR;
import static org.apache.catalina.LogFacade.NO_HOST_MATCHES_SERVER_NAME_INFO;
import static org.apache.catalina.LogFacade.PARSING_CLIENT_CERT_EXCEPTION;
import static org.apache.catalina.LogFacade.REQUEST_PROCESSING_EXCEPTION;
import static org.apache.catalina.connector.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER;
import static org.glassfish.internal.api.Globals.getDefaultHabitat;
import java.io.CharConversionException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
import org.apache.catalina.Host;
import org.apache.catalina.LogFacade;
import org.apache.catalina.Wrapper;
import org.apache.catalina.core.ContainerBase;
import org.apache.catalina.util.ServerInfo;
import org.glassfish.common.util.InputValidationUtil;
import org.glassfish.grizzly.http.Method;
import org.glassfish.grizzly.http.Note;
import org.glassfish.grizzly.http.server.AfterServiceListener;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.util.MappingData;
import org.glassfish.grizzly.http.util.ByteChunk;
import org.glassfish.grizzly.http.util.CharChunk;
import org.glassfish.grizzly.http.util.DataChunk;
import org.glassfish.web.valve.GlassFishValve;
import org.glassfish.web.valve.ServletContainerInterceptor;
import com.sun.appserv.ProxyHandler;
/**
* Implementation of a request processor which delegates the processing to a Coyote processor.
*
* @author Craig R. McClanahan
* @author Remy Maucherat
* @version $Revision: 1.34 $ $Date: 2007/08/24 18:38:28 $
*/
public class CoyoteAdapter extends HttpHandler {
private static final Logger log = LogFacade.getLogger();
private static final ResourceBundle rb = log.getResourceBundle();
// -------------------------------------------------------------- Constants
static final String JVM_ROUTE = System.getProperty("jvmRoute");
protected static final boolean ALLOW_BACKSLASH =
Boolean.valueOf(System.getProperty("org.glassfish.grizzly.tcp.tomcat5.CoyoteAdapter.ALLOW_BACKSLASH", "false"));
private static final boolean COLLAPSE_ADJACENT_SLASHES =
Boolean.valueOf(System.getProperty("com.sun.enterprise.web.collapseAdjacentSlashes", "true"));
// Make sure this value is always aligned with {@link ContainerMapper}
// (@see com.sun.enterprise.v3.service.impl.ContainerMapper)
protected static final Note<MappingData> MAPPING_DATA =
org.glassfish.grizzly.http.server.Request.<MappingData>createNote("MappingData");
static final Note<Request> CATALINA_REQUEST_NOTE =
org.glassfish.grizzly.http.server.Request.createNote(Request.class.getName());
static final Note<Response> CATALINA_RESPONSE_NOTE =
org.glassfish.grizzly.http.server.Request.createNote(Response.class.getName());
static final CatalinaAfterServiceListener catalinaAfterServiceListener = new CatalinaAfterServiceListener();
// Make sure this value is always aligned with {@link ContainerMapper}
// (@see com.sun.enterprise.v3.service.impl.ContainerMapper)
private final static Note<DataChunk> DATA_CHUNK =
org.glassfish.grizzly.http.server.Request.<DataChunk>createNote("DataChunk");
// ----------------------------------------------------- Instance Variables
private Collection<ServletContainerInterceptor> interceptors;
/**
* When mod_jk is used, the adapter must be invoked the same way Tomcat does by invoking service(...) and the
* afterService(...). This is a hack to make it compatible with Tomcat 5|6.
*/
private boolean compatWithTomcat;
private String serverName = ServerInfo.getPublicServerInfo();
/**
* The CoyoteConnector with which this processor is associated.
*/
private Connector connector;
// ----------------------------------------------------------- Constructors
/**
* Construct a new CoyoteProcessor associated with the specified connector.
*
* @param connector CoyoteConnector that owns this processor
*/
public CoyoteAdapter(Connector connector) {
super();
this.connector = connector;
initServletInterceptors();
}
// -------------------------------------------------------- Adapter Methods
/**
* Service method.
*/
@Override
public void service(org.glassfish.grizzly.http.server.Request grizzlyRequest, org.glassfish.grizzly.http.server.Response grizzlyResponse) throws Exception {
grizzlyResponse.getResponse().setAllowCustomReasonPhrase(USE_CUSTOM_STATUS_MSG_IN_HEADER);
Request catalinaRequest = grizzlyRequest.getNote(CATALINA_REQUEST_NOTE);
Response catalinaResponse = grizzlyRequest.getNote(CATALINA_RESPONSE_NOTE);
// Grizzly already parsed, decoded, and mapped the request.
// Let's re-use this info here, before firing the
// requestStartEvent probe, so that the mapping data will be
// available to any probe event listener via standard
// ServletRequest APIs (such as getContextPath())
MappingData mappingData = grizzlyRequest.getNote(MAPPING_DATA);
final boolean v3Enabled = mappingData != null;
if (catalinaRequest == null) {
// Create objects
catalinaRequest = (Request) connector.createRequest();
catalinaResponse = (Response) connector.createResponse();
// Link objects
catalinaRequest.setResponse(catalinaResponse);
catalinaResponse.setRequest(catalinaRequest);
// Set as notes
grizzlyRequest.setNote(CATALINA_REQUEST_NOTE, catalinaRequest);
grizzlyRequest.setNote(CATALINA_RESPONSE_NOTE, catalinaResponse);
// Set query string encoding
grizzlyRequest.getRequest().getRequestURIRef().setDefaultURIEncoding(Charset.forName(connector.getURIEncoding()));
}
catalinaRequest.setGrizzlyRequest(grizzlyRequest);
catalinaResponse.setCoyoteResponse(grizzlyResponse);
if (v3Enabled && !compatWithTomcat) {
catalinaRequest.setMappingData(mappingData);
catalinaRequest.updatePaths(mappingData);
}
grizzlyRequest.addAfterServiceListener(catalinaAfterServiceListener);
try {
doService(grizzlyRequest, catalinaRequest, grizzlyResponse, catalinaResponse, v3Enabled);
// Request may want to initialize async processing
catalinaRequest.onExitService();
} catch (Throwable t) {
log.log(SEVERE, REQUEST_PROCESSING_EXCEPTION, t);
}
}
private void enteringServletContainer(Request req, Response res) {
if (interceptors == null) {
return;
}
for (ServletContainerInterceptor interceptor : interceptors) {
try {
interceptor.preInvoke(req, res);
} catch (Throwable th) {
log.log(SEVERE, INTERNAL_ERROR, th);
}
}
}
private void leavingServletContainer(Request req, Response res) {
if (interceptors == null) {
return;
}
for (ServletContainerInterceptor interceptor : interceptors) {
try {
interceptor.postInvoke(req, res);
} catch (Throwable th) {
log.log(SEVERE, INTERNAL_ERROR, th);
}
}
}
private void initServletInterceptors() {
try {
interceptors = getDefaultHabitat().getAllServices(ServletContainerInterceptor.class);
} catch (Throwable th) {
log.log(SEVERE, FAILED_TO_INITIALIZE_THE_INTERCEPTOR, th);
}
}
private void doService(
final org.glassfish.grizzly.http.server.Request grizzlyRequest, final Request catalinaRequest,
final org.glassfish.grizzly.http.server.Response grizzlyResponse, final Response catalinaResponse, final boolean v3Enabled) throws Exception {
// Check connector for disabled state
if (!connector.isEnabled()) {
String msg = MessageFormat.format(rb.getString(HTTP_LISTENER_DISABLED), String.valueOf(connector.getPort()));
if (log.isLoggable(FINE)) {
log.log(FINE, msg);
}
catalinaResponse.sendError(SC_NOT_FOUND, msg);
return;
}
// Parse and set Catalina and configuration specific request parameters
if (postParseRequest(grizzlyRequest, catalinaRequest, grizzlyResponse, catalinaResponse, v3Enabled)) {
boolean authPassthroughEnabled = connector.getAuthPassthroughEnabled();
ProxyHandler proxyHandler = connector.getProxyHandler();
if (authPassthroughEnabled && proxyHandler != null) {
// Otherwise Servlet request.isSecure() value is not propagated when authPassthroughEnabled is set to true
if (proxyHandler.getSSLKeysize(catalinaRequest.getRequest()) > 0) {
catalinaRequest.setSecure(true);
}
X509Certificate[] certs = null;
try {
certs = proxyHandler.getSSLClientCertificateChain(catalinaRequest.getRequest());
} catch (CertificateException ce) {
log.log(SEVERE, PARSING_CLIENT_CERT_EXCEPTION, ce);
}
if (certs != null) {
catalinaRequest.setAttribute(Globals.CERTIFICATES_ATTR, certs);
}
}
// Invoke the web container
connector.requestStartEvent(catalinaRequest.getRequest(), catalinaRequest.getHost(), catalinaRequest.getContext());
Container container = connector.getContainer();
enteringServletContainer(catalinaRequest, catalinaResponse);
try {
catalinaRequest.lockSession();
if (container.getPipeline().hasNonBasicValves() || container.hasCustomPipeline()) {
container.getPipeline().invoke(catalinaRequest, catalinaResponse);
} else {
// Invoke host directly
Host host = catalinaRequest.getHost();
if (host == null) {
catalinaResponse.sendError(SC_BAD_REQUEST);
catalinaResponse.setDetailMessage(
MessageFormat.format(
rb.getString(NO_HOST_MATCHES_SERVER_NAME_INFO),
catalinaRequest.getRequest().getServerName()));
return;
}
if (host.getPipeline().hasNonBasicValves() || host.hasCustomPipeline()) {
host.getPipeline().invoke(catalinaRequest, catalinaResponse);
} else {
GlassFishValve hostValve = host.getPipeline().getBasic();
hostValve.invoke(catalinaRequest, catalinaResponse);
// Error handling
hostValve.postInvoke(catalinaRequest, catalinaResponse);
}
}
} finally {
try {
connector.requestEndEvent(catalinaRequest.getRequest(), catalinaRequest.getHost(), catalinaRequest.getContext(), catalinaResponse.getStatus());
} finally {
leavingServletContainer(catalinaRequest, catalinaResponse);
}
}
}
}
// ------------------------------------------------------ Protected Methods
/**
* Parse additional request parameters.
*/
protected boolean postParseRequest(
final org.glassfish.grizzly.http.server.Request grizzlyRequest, final Request catalinaRequest,
final org.glassfish.grizzly.http.server.Response grizzlyResponse, final Response catalinaResponse, final boolean v3Enabled) throws Exception {
// XXX the processor may have set a correct scheme and port prior to this point,
// in ajp13 protocols dont make sense to get the port from the connector...
// otherwise, use connector configuration
catalinaRequest.setSecure(grizzlyRequest.isSecure());
// URI decoding
DataChunk decodedURI;
try {
decodedURI = grizzlyRequest.getRequest().getRequestURIRef().getDecodedRequestURIBC();
} catch (CharConversionException cce) {
catalinaResponse.sendError(SC_BAD_REQUEST, "Invalid URI");
return false;
}
// Normalize Decoded URI
try {
normalize(decodedURI);
} catch (IOException ioException) {
catalinaResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid URI");
catalinaResponse.setDetailMessage(ioException.getMessage());
return false;
}
if (compatWithTomcat || !v3Enabled) {
// Set the remote principal
String principal = grizzlyRequest.getRemoteUser();
if (principal != null) {
catalinaRequest.setUserPrincipal(new CoyotePrincipal(principal));
}
// Set the authorization type
String authtype = grizzlyRequest.getAuthType();
if (authtype != null) {
catalinaRequest.setAuthType(authtype);
}
}
/*
* Remove any parameters from the URI, so they won't be considered by the mapping algorithm, and save them in a
* temporary CharChunk, so that any session id param may be parsed once the target context, which may use a custom
* session parameter name, has been identified
*/
final CharChunk uriParamsCC = catalinaRequest.getURIParams();
final CharChunk uriCC = decodedURI.getCharChunk();
final int semicolon = uriCC.indexOf(';');
if (semicolon > 0) {
final int absSemicolon = uriCC.getStart() + semicolon;
uriParamsCC.setChars(uriCC.getBuffer(), absSemicolon, uriCC.getEnd() - absSemicolon);
decodedURI.setChars(uriCC.getBuffer(), uriCC.getStart(), absSemicolon - uriCC.getStart());
}
if (compatWithTomcat || !v3Enabled) {
/* mod_jk */
DataChunk localDecodedURI = decodedURI;
if (semicolon > 0) {
localDecodedURI = grizzlyRequest.getNote(DATA_CHUNK);
if (localDecodedURI == null) {
localDecodedURI = DataChunk.newInstance();
grizzlyRequest.setNote(DATA_CHUNK, localDecodedURI);
}
localDecodedURI.duplicate(decodedURI);
}
connector.getMapper()
.map(
grizzlyRequest.getRequest().serverName(),
localDecodedURI,
catalinaRequest.getMappingData());
MappingData md = catalinaRequest.getMappingData();
grizzlyRequest.setNote(MAPPING_DATA, md);
catalinaRequest.updatePaths(md);
}
// FIXME: the code below doesn't belongs to here, this is only have sense in Http11, not in ajp13..
// At this point the Host header has been processed. Override if the proxyPort/proxyHost are set
String proxyName = connector.getProxyName();
int proxyPort = connector.getProxyPort();
if (proxyPort != 0) {
grizzlyRequest.setServerPort(proxyPort);
}
if (proxyName != null) {
grizzlyRequest.setServerName(proxyName);
}
Context catalinaContext = (Context) catalinaRequest.getMappingData().context;
// Parse session id
if (catalinaContext != null) {
if (grizzlyRequest.isRequestedSessionIdFromURL() && SESSION_PARAMETER_NAME.equals(catalinaContext.getSessionParameterName())) {
catalinaRequest.obtainSessionId();
} else if (!uriParamsCC.isNull()) {
catalinaRequest.parseSessionId(catalinaContext.getSessionParameterName(), uriParamsCC);
}
}
catalinaRequest.setDefaultContext(catalinaRequest.getMappingData().isDefaultContext);
catalinaRequest.setContext(catalinaContext);
if (catalinaContext != null && !uriParamsCC.isNull()) {
catalinaRequest.parseSessionVersion(uriParamsCC);
}
if (!uriParamsCC.isNull()) {
catalinaRequest.parseJReplica(uriParamsCC);
}
catalinaRequest.setWrapper((Wrapper) catalinaRequest.getMappingData().wrapper);
// Filter trace method
if (!connector.getAllowTrace() && Method.TRACE.equals(grizzlyRequest.getMethod())) {
Wrapper wrapper = catalinaRequest.getWrapper();
String header = null;
if (wrapper != null) {
String[] methods = wrapper.getServletMethods();
if (methods != null) {
for (String method : methods) {
// Exclude TRACE from methods returned in Allow header
if ("TRACE".equals(method)) {
continue;
}
if (header == null) {
header = method;
} else {
header += ", " + method;
}
}
}
}
grizzlyResponse.setStatus(405, "TRACE method is not allowed");
grizzlyResponse.addHeader("Allow", header);
return false;
}
// Possible redirect
DataChunk redirectPathMB = catalinaRequest.getMappingData().redirectPath;
if (!redirectPathMB.isNull() && (!catalinaContext.hasAdHocPaths() || (catalinaContext.getAdHocServletName(catalinaRequest.getRequest().getServletPath()) == null))) {
String redirectPath = redirectPathMB.toString();
String query = catalinaRequest.getQueryString();
if (catalinaRequest.isRequestedSessionIdFromURL()) {
// This is not optimal, but as this is not very common, it shouldn't matter
redirectPath = redirectPath + ";" + catalinaContext.getSessionParameterName() + "=" + catalinaRequest.getRequestedSessionId();
}
redirectPath = catalinaResponse.encode(redirectPath);
if (query != null) {
// This is not optimal, but as this is not very common, it shouldn't matter
redirectPath = redirectPath + "?" + query;
}
boolean authPassthroughEnabled = connector.getAuthPassthroughEnabled();
ProxyHandler proxyHandler = connector.getProxyHandler();
if (authPassthroughEnabled && proxyHandler != null) {
if (proxyHandler.getSSLKeysize(catalinaRequest.getRequest()) > 0) {
catalinaRequest.setSecure(true);
}
}
// Issue a permanent redirect
// Validating the redirectPath for header injection
if (InputValidationUtil.validateStringforCRLF(redirectPath)) {
catalinaResponse.sendError(403, "Forbidden");
} else {
catalinaResponse.sendRedirect(InputValidationUtil.removeLinearWhiteSpaces(redirectPath), false);
}
return false;
}
// Parse session Id
catalinaRequest.parseSessionCookiesId();
catalinaRequest.parseJrouteCookie();
return true;
}
/**
* Normalize URI.
* <p>
* This method normalizes "\", "//", "/./" and "/../". This method will throw an error when trying to go above the root,
* or if the URI contains a null byte.
*
* @param uriDataChunk URI DataChunk to be normalized
*/
public static void normalize(DataChunk uriDataChunk) throws IOException {
DataChunk.Type type = uriDataChunk.getType();
if (type == DataChunk.Type.Chars) {
normalizeChars(uriDataChunk);
return;
}
normalizeBytes(uriDataChunk);
}
private static void normalizeBytes(DataChunk uriDataChunk) throws IOException {
ByteChunk uriBC = uriDataChunk.getByteChunk();
byte[] b = uriBC.getBytes();
int start = uriBC.getStart();
int end = uriBC.getEnd();
// An empty URL is not acceptable
if (start == end) {
throw new IOException("Empty URL");
}
// URL * is acceptable
if ((end - start == 1) && b[start] == (byte) '*') {
return;
}
int pos = 0;
int index = 0;
// Replace '\' with '/'
// Check for null byte
for (pos = start; pos < end; pos++) {
if (b[pos] == (byte) '\\') {
if (ALLOW_BACKSLASH) {
b[pos] = (byte) '/';
} else {
throw new IOException("Backslashes not allowed");
}
}
if (b[pos] == (byte) 0) {
throw new IOException("Null byte found during request normalization");
}
}
// The URL must start with '/'
if (b[start] != (byte) '/') {
throw new IOException("Request must start with /");
}
// Replace "//" with "/"
if (COLLAPSE_ADJACENT_SLASHES) {
for (pos = start; pos < (end - 1); pos++) {
if (b[pos] == (byte) '/') {
while ((pos + 1 < end) && (b[pos + 1] == (byte) '/')) {
copyBytes(b, pos, pos + 1, end - pos - 1);
end--;
}
}
}
}
// If the URI ends with "/." or "/..", then we append an extra "/"
// Note: It is possible to extend the URI by 1 without any side effect
// as the next character is a non-significant WS.
if (((end - start) > 2) && (b[end - 1] == (byte) '.')) {
if ((b[end - 2] == (byte) '/') || ((b[end - 2] == (byte) '.') && (b[end - 3] == (byte) '/'))) {
b[end] = (byte) '/';
end++;
}
}
uriBC.setEnd(end);
index = 0;
// Resolve occurrences of "/./" in the normalized path
while (true) {
index = uriBC.indexOf("/./", 0, 3, index);
if (index < 0) {
break;
}
copyBytes(b, start + index, start + index + 2, end - start - index - 2);
end = end - 2;
uriBC.setEnd(end);
}
index = 0;
// Resolve occurrences of "/../" in the normalized path
while (true) {
index = uriBC.indexOf("/../", 0, 4, index);
if (index < 0) {
break;
}
// Prevent from going outside our context
if (index == 0) {
throw new IOException("Request traversed outside of allowed context");
}
int index2 = -1;
for (pos = start + index - 1; (pos >= 0) && (index2 < 0); pos--) {
if (b[pos] == (byte) '/') {
index2 = pos;
}
}
copyBytes(b, start + index2, start + index + 3, end - start - index - 3);
end = end + index2 - index - 3;
uriBC.setEnd(end);
index = index2;
}
uriBC.setBytes(b, start, end);
}
private static void normalizeChars(DataChunk uriDataChunk) throws IOException {
CharChunk uriCharChunk = uriDataChunk.getCharChunk();
char[] c = uriCharChunk.getChars();
int start = uriCharChunk.getStart();
int end = uriCharChunk.getEnd();
// URL * is acceptable
if ((end - start == 1) && c[start] == '*') {
return;
}
int pos = 0;
int index = 0;
// Replace '\' with '/'
// Check for null char
for (pos = start; pos < end; pos++) {
if (c[pos] == '\\') {
if (ALLOW_BACKSLASH) {
c[pos] = '/';
} else {
throw new IOException("Backslashes not allowed");
}
}
if (c[pos] == (char) 0) {
throw new IOException("Null byte found during request normalization");
}
}
// The URL must start with '/'
if (c[start] != '/') {
throw new IOException("Request must start with /");
}
// Replace "//" with "/"
if (COLLAPSE_ADJACENT_SLASHES) {
for (pos = start; pos < (end - 1); pos++) {
if (c[pos] == '/') {
while ((pos + 1 < end) && (c[pos + 1] == '/')) {
copyChars(c, pos, pos + 1, end - pos - 1);
end--;
}
}
}
}
// If the URI ends with "/." or "/..", then we append an extra "/"
// Note: It is possible to extend the URI by 1 without any side effect
// as the next character is a non-significant WS.
if (((end - start) > 2) && (c[end - 1] == '.')) {
if ((c[end - 2] == '/') || ((c[end - 2] == '.') && (c[end - 3] == '/'))) {
c[end] = '/';
end++;
}
}
uriCharChunk.setEnd(end);
index = 0;
// Resolve occurrences of "/./" in the normalized path
while (true) {
index = uriCharChunk.indexOf("/./", 0, 3, index);
if (index < 0) {
break;
}
copyChars(c, start + index, start + index + 2, end - start - index - 2);
end = end - 2;
uriCharChunk.setEnd(end);
}
index = 0;
// Resolve occurrences of "/../" in the normalized path
while (true) {
index = uriCharChunk.indexOf("/../", 0, 4, index);
if (index < 0) {
break;
}
// Prevent from going outside our context
if (index == 0) {
throw new IOException("Request traversed outside of allowed context");
}
int index2 = -1;
for (pos = start + index - 1; (pos >= 0) && (index2 < 0); pos --) {
if (c[pos] == '/') {
index2 = pos;
}
}
copyChars(c, start + index2, start + index + 3, end - start - index - 3);
end = end + index2 - index - 3;
uriCharChunk.setEnd(end);
index = index2;
}
uriCharChunk.setChars(c, start, end);
}
// ------------------------------------------------------ Protected Methods
/**
* Copy an array of bytes to a different position. Used during normalization.
*/
protected static void copyBytes(byte[] b, int dest, int src, int len) {
for (int pos = 0; pos < len; pos++) {
b[pos + dest] = b[pos + src];
}
}
/**
* Copy an array of chars to a different position. Used during normalization.
*/
private static void copyChars(char[] c, int dest, int src, int len) {
for (int pos = 0; pos < len; pos++) {
c[pos + dest] = c[pos + src];
}
}
/**
* Log a message on the Logger associated with our Container (if any)
*
* @param message Message to be logged
*/
protected void log(String message) {
log.log(INFO, message);
}
/**
* Log a message on the Logger associated with our Container (if any)
*
* @param message Message to be logged
* @param throwable Associated exception
*/
protected void log(String message, Throwable throwable) {
log.log(SEVERE, message, throwable);
}
/**
* Notify all container event listeners that a particular event has occurred for this Adapter. The default
* implementation performs this notification synchronously using the calling thread.
*
* @param type Event type
* @param data Event data
*/
public void fireAdapterEvent(String type, Object data) {
if (connector != null && connector.getContainer() != null) {
try {
((ContainerBase) connector.getContainer()).fireContainerEvent(type, data);
} catch (Throwable t) {
log.log(SEVERE, REQUEST_PROCESSING_EXCEPTION, t);
}
}
}
/**
* Return true when an instance is executed the same way it does in Tomcat.
*/
public boolean isCompatWithTomcat() {
return compatWithTomcat;
}
/**
* <tt>true</tt> if this class needs to be compatible with Tomcat Adapter class. Since Tomcat Adapter implementation
* doesn't support the afterService method, the afterService method must be invoked inside the service method.
*/
public void setCompatWithTomcat(boolean compatWithTomcat) {
this.compatWithTomcat = compatWithTomcat;
// Add server header
if (compatWithTomcat) {
serverName = "Apache/" + serverName;
} else {
// Recalculate.
serverName = ServerInfo.getPublicServerInfo();
}
}
/**
* Gets the port of this CoyoteAdapter.
*
* @return the port of this CoyoteAdapter
*/
public int getPort() {
return connector.getPort();
}
/**
* AfterServiceListener, which is responsible for recycle catalina request and response objects.
*/
static final class CatalinaAfterServiceListener implements AfterServiceListener {
@Override
public void onAfterService(final org.glassfish.grizzly.http.server.Request grizzlyRequest) {
final Request catalinaRequest = grizzlyRequest.getNote(CATALINA_REQUEST_NOTE);
final Response catalinaResponse = grizzlyRequest.getNote(CATALINA_RESPONSE_NOTE);
if (catalinaRequest != null) {
try {
if (!catalinaRequest.isUpgrade()) {
catalinaResponse.finishResponse();
} else {
catalinaResponse.setUpgrade(catalinaRequest.isUpgrade());
}
} catch (Exception e) {
log.log(SEVERE, REQUEST_PROCESSING_EXCEPTION, e);
} finally {
try {
catalinaRequest.unlockSession();
} finally {
catalinaRequest.recycle();
catalinaResponse.recycle();
}
}
}
}
}
}