forked from jenkinsci/stapler
/
Stapler.java
1213 lines (1091 loc) · 49.1 KB
/
Stapler.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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2004-2010, Kohsuke Kawaguchi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.kohsuke.stapler;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.bind.BoundObjectTable;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static javax.servlet.http.HttpServletResponse.*;
import static org.kohsuke.stapler.Dispatcher.*;
/**
* Maps an HTTP request to a method call / JSP invocation against a model object
* by evaluating the request URL in a EL-ish way.
*
* <p>
* This servlet should be used as the default servlet.
*
* @author Kohsuke Kawaguchi
*/
public class Stapler extends HttpServlet {
private /*final*/ ServletContext context;
@SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "The Stapler class is not expected to be serialized.")
private /*final*/ WebApp webApp;
/**
* All the resources that exist in {@link ServletContext#getResource(String)},
* as a cache.
*
* If this field is null, no cache.
*/
private volatile Map<String,URL> resourcePaths;
/**
* Temporarily updates the thread name to reflect the request being processed.
* On by default for convenience, but for webapps that use filters, use
* {@link DiagnosticThreadNameFilter} as the first filter and switch this off.
*/
private boolean diagnosticThreadName = true;
public @Override void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
this.context = servletConfig.getServletContext();
this.webApp = WebApp.get(context);
String defaultEncodings = servletConfig.getInitParameter("default-encodings");
if(defaultEncodings!=null) {
for(String t : defaultEncodings.split(";")) {
t=t.trim();
int idx=t.indexOf('=');
if(idx<0) throw new ServletException("Invalid format: "+t);
webApp.defaultEncodingForStaticResources.put(t.substring(0,idx),t.substring(idx+1));
}
}
buildResourcePaths();
webApp.addStaplerServlet(servletConfig.getServletName(),this);
String v = servletConfig.getInitParameter("diagnosticThreadName");
if (v!=null)
diagnosticThreadName = Boolean.parseBoolean(v);
}
/**
* Rebuild the internal cache for static resources.
*/
public void buildResourcePaths() {
try {
if (Boolean.getBoolean(Stapler.class.getName()+".noResourcePathCache")) {
resourcePaths = null;
return;
}
Map<String,URL> paths = new HashMap<>();
Stack<String> q = new Stack<>();
q.push("/");
while (!q.isEmpty()) {
String dir = q.pop();
Set<String> children = context.getResourcePaths(dir);
if (children!=null) {
for (String child : children) {
if (child.endsWith("/"))
q.push(child);
else {
URL v = context.getResource(child);
if (v==null) {
resourcePaths = null;
return; // this can't happen. abort with no cache
}
paths.put(child, v);
}
}
}
}
resourcePaths = Collections.unmodifiableMap(paths);
} catch (MalformedURLException e) {
resourcePaths = null; // abort
}
}
public WebApp getWebApp() {
return webApp;
}
/*package*/ void setWebApp(WebApp webApp) {
this.webApp = webApp;
}
protected @Override void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
Thread t = Thread.currentThread();
final String oldName = t.getName();
try {
if (diagnosticThreadName)
t.setName("Handling "+req.getMethod()+' '+req.getRequestURI()+" : "+oldName);
String servletPath = getServletPath(req);
if(LOGGER.isLoggable(Level.FINE))
LOGGER.fine("Processing request for "+servletPath);
if (servletPath.startsWith(BoundObjectTable.PREFIX)) {
// serving exported objects
invoke( req, rsp, webApp.boundObjectTable, servletPath.substring(BoundObjectTable.PREFIX.length()));
return;
}
boolean staticLink = false;
if(servletPath.startsWith("/static/")) {
// skip "/static/..../ portion
int idx = servletPath.indexOf('/',8);
if (idx != -1) {
servletPath=servletPath.substring(idx);
staticLink = true;
}
}
String lowerPath = servletPath.toLowerCase(Locale.ENGLISH);
if(servletPath.length()!=0 && !lowerPath.startsWith("/web-inf") && !lowerPath.startsWith("/meta-inf")) {
// getResource requires '/' prefix (and resin insists on that, too) but servletPath can be empty string (JENKINS-879)
// so make sure servletPath is at least length 1 before calling getResource()
// WEB-INF and META-INF are by convention hidden and not supposed to be rendered to clients (JENKINS-7457/JENKINS-11538)
// also note that Windows allows "/WEB-INF./" to refer to refer to this directory.
// here we also reject that (by rejecting /WEB-INF*)
OpenConnection con = openResourcePathByLocale(req,servletPath);
if(con!=null) {
long expires = MetaClass.NO_CACHE ? 0 : 24L * 60 * 60 * 1000; /*1 day*/
if(staticLink)
expires*=365; // static resources are unique, so we can set a long expiration date
if(serveStaticResource(req, new ResponseImpl(this, rsp), con, expires))
return; // done
}
}
Object root = webApp.getApp();
if(root==null)
throw new ServletException("there's no \"app\" attribute in the application context.");
// consider reusing this ArrayList.
invoke( req, rsp, root, servletPath);
} finally {
t.setName(oldName);
}
}
/**
* Tomcat and GlassFish returns a fresh {@link InputStream} every time
* {@link URLConnection#getInputStream()} is invoked in their {@code org.apache.naming.resources.DirContextURLConnection}.
*
* <p>
* All the other {@link URLConnection}s in JDK don't do this --- they return the same {@link InputStream},
* even the one for the file:// URLs.
*
* <p>
* In Tomcat (and most likely in GlassFish, although this is not verified), resource look up on
* {@link ServletContext#getResource(String)} successfully returns non-existent URL, and
* the failure can be only detected by {@link IOException} from {@link URLConnection#getInputStream()}.
*
* <p>
* Therefore, for the whole thing to work without resource leak, once we open {@link InputStream}
* for the sake of really making sure that the resource exists, we need to hang on to that stream.
*
* <p>
* Hence the need for this tuple.
*/
private static final class OpenConnection {
final URLConnection connection;
final InputStream stream;
private OpenConnection(URLConnection connection, InputStream stream) {
this.connection = connection;
this.stream = stream;
}
private OpenConnection(URLConnection connection) throws IOException {
this(connection,connection.getInputStream());
}
private void close() throws IOException {
stream.close();
}
/**
* Can't just use {@code connection.getLastModified()} because
* of a file descriptor leak in {@code JarURLConnection}.
* See http://sourceforge.net/p/freemarker/bugs/189/
*/
public long getLastModified() {
if (connection instanceof JarURLConnection) {
// There is a bug in sun's jar url connection that causes file handle leaks when calling getLastModified()
// Since the time stamps of jar file contents can't vary independent from the jar file timestamp, just use
// the jar file timestamp
URL jarURL = ((JarURLConnection) connection).getJarFileURL();
if (jarURL.getProtocol().equals("file")) {
// Return the last modified time of the underlying file - saves some opening and closing
return determineLastModified(jarURL);
} else {
// Use the URL mechanism
URLConnection jarConn = null;
try {
jarConn = openConnection(jarURL);
return jarConn.getLastModified();
} catch (IOException e) {
return -1;
} finally {
if (jarConn != null) {
try {
IOUtils.closeQuietly(jarConn.getInputStream());
} catch (IOException e) {
// ignore this error
}
}
}
}
} else {
return connection.getLastModified();
}
}
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "Protected by checks at other layers.")
private long determineLastModified(URL jarURL) {
// TODO Fix this so that it works correctly. Needs to handle metacharacters
// Windows UNC, etc. Should use URL.toURI, Paths.get(URI), and Files.getLastModifiedTime.
return new File(jarURL.getFile()).lastModified();
}
}
/**
* Logic for performing locale driven resource selection.
*
* Different subtypes provide different meanings for the 'path' parameter.
*/
private abstract class LocaleDrivenResourceSelector {
/**
* The 'path' is divided into the base part and the extension, and the locale-specific
* suffix is inserted to the base portion. {@link #map(String)} is used to convert
* the combined path into {@link URL}, until we find one that works.
*
* <p>
* The syntax of the locale specific resource is the same as property file localization.
* So Japanese resource for {@code foo.html} would be named {@code foo_ja.html}.
*
* @param path
* path/URL-like string that represents the path of the base resource,
* say "foo/bar/index.html" or "file:///a/b/c/d/efg.png"
* @param locale
* The preferred locale
* @param fallback
* The {@link URL} representation of the {@code path} parameter
* Used as a fallback.
*/
OpenConnection open(String path, Locale locale, URL fallback) throws IOException {
String s = path;
int idx = s.lastIndexOf('.');
if(idx<0) // no file extension, so no locale switch available
return openURL(fallback);
String base = s.substring(0,idx);
String ext = s.substring(idx);
if(ext.indexOf('/')>=0) // the '.' we found was not an extension separator
return openURL(fallback);
// RegExps found in Locale JavaDoc
String language = locale.getLanguage();
boolean languageOk = language.matches("^[a-zA-Z]{2,8}$");
String country = locale.getCountry();
boolean countryOk = country.matches("^[a-zA-Z]{2}|[0-9]{3}$");
String variant = locale.getVariant();
String SUBTAG = "(?:[0-9][0-9a-zA-Z]{3}|[0-9a-zA-Z]{5,8})";
boolean variantOk = variant.matches("^" + SUBTAG + "(?:[_\\-]" + SUBTAG + ")*$");
OpenConnection con;
// try locale specific resources first.
if(languageOk && countryOk && variantOk){
con = openURL(map(base + '_' + language + '_' + country + '_' + variant + ext));
if(con!=null)
return con;
}
if(languageOk && countryOk){
con = openURL(map(base + '_'+ language + '_' + country + ext));
if(con!=null)
return con;
}
if(languageOk){
con = openURL(map(base + '_' + language + ext));
if(con!=null)
return con;
}
// default
return openURL(fallback);
}
/**
* Maps the 'path' into {@link URL}.
*/
abstract URL map(String path) throws IOException;
}
@SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "The Stapler class is not expected to be serialized.")
private final LocaleDrivenResourceSelector resourcePathLocaleSelector = new LocaleDrivenResourceSelector() {
@Override
URL map(String path) throws IOException {
URL override = LocaleDrivenResourceProvider.lookupResource(path);
if (override != null) {
return override;
}
return getResource(path);
}
};
private OpenConnection openResourcePathByLocale(HttpServletRequest req,String resourcePath) throws IOException {
URL url = getResource(resourcePath);
if(url==null) return null;
// hopefully HotSpot would be able to inline all the virtual calls in here
return resourcePathLocaleSelector.open(resourcePath,req.getLocale(),url);
}
/**
* {@link LocaleDrivenResourceSelector} that uses a complete URL as 'path'
*/
@SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "The Stapler class is not expected to be serialized.")
private final LocaleDrivenResourceSelector urlLocaleSelector = new LocaleDrivenResourceSelector() {
@Override
URL map(String url) throws IOException {
URL override = LocaleDrivenResourceProvider.lookupResource(url);
if (override != null) {
return override;
}
return new URL(url);
}
};
OpenConnection selectResourceByLocale(URL url, Locale locale) throws IOException {
// hopefully HotSpot would be able to inline all the virtual calls in here
return urlLocaleSelector.open(url.toString(),locale,url);
}
/**
* Serves the specified {@link URLConnection} as a static resource.
*/
boolean serveStaticResource(HttpServletRequest req, StaplerResponse rsp, OpenConnection con, long expiration) throws IOException {
if (con == null) return false;
try {
return serveStaticResource(req, rsp, con.stream,
con.getLastModified(),
expiration,
con.connection.getContentLength(),
con.connection.getURL().toString());
} finally {
con.close();
}
}
/**
* Serves the specified {@link URL} as a static resource.
*/
boolean serveStaticResource(HttpServletRequest req, StaplerResponse rsp, URL url, long expiration) throws IOException {
return serveStaticResource(req,rsp,openURL(url),expiration);
}
/**
* Opens URL, with error handling to absorb container differences.
* <p>
* This method returns null if the resource pointed by URL doesn't exist. The initial attempt was to
* distinguish "resource exists but failed to load" vs "resource doesn't exist", but as more reports
* from the field come in, we discovered that it's impossible to make such a distinction and work with
* many environments both at the same time.
*/
private OpenConnection openURL(URL url) {
if(url==null) return null;
// jetty reports directories as URLs, which isn't what this is intended for,
// so check and reject.
File f = toFile(url);
if(f!=null && f.isDirectory())
return null;
try {
// in normal protocol handlers like http/file, openConnection doesn't actually open a connection
// (that's deferred until URLConnection.connect()), so this method doesn't result in an error,
// even if URL points to something that doesn't exist.
//
// but we've heard a report from http://github.com/adreghiciu that some URLS backed by custom
// protocol handlers can throw an exception as early as here. So treat this IOException
// as "the resource pointed by URL is missing".
URLConnection con = openConnection(url);
OpenConnection c = new OpenConnection(con);
// Some URLs backed by custom broken protocol handler can return null from getInputStream(),
// so let's be defensive here. An example of that is an OSGi container --- unfortunately
// we don't have more details than that.
if(c.stream==null)
return null;
return c;
} catch (IOException e) {
// Tomcat only reports a missing resource error here, from URLConnection.getInputStream()
return null;
}
}
@SuppressFBWarnings(value = "URLCONNECTION_SSRF_FD", justification = "Not relevant in this situation.")
private static URLConnection openConnection(URL url) throws IOException {
return url.openConnection();
}
/**
* Serves the specified {@link InputStream} as a static resource.
*
* @param contentLength
* if the length of the input stream is known in advance, specify that value
* so that HTTP keep-alive works. Otherwise specify -1 to indicate that the length is unknown.
* @param expiration
* The number of milliseconds until the resource will "expire".
* Until it expires the browser will be allowed to cache it
* and serve it without checking back with the server.
* After it expires, the client will send conditional GET to
* check if the resource is actually modified or not.
* If 0, it will immediately expire.
* @param fileName
* file name of this resource. Used to determine the MIME type.
* Since the only important portion is the file extension, this could be just a file name,
* or a full path name, or even a pseudo file name that doesn't actually exist.
* It supports both '/' and '\\' as the path separator.
* @return false
* if the resource doesn't exist.
*/
boolean serveStaticResource(HttpServletRequest req, StaplerResponse rsp, InputStream in, long lastModified, long expiration, long contentLength, String fileName) throws IOException {
try {
{// send out Last-Modified, or check If-Modified-Since
if(lastModified!=0) {
String since = req.getHeader("If-Modified-Since");
SimpleDateFormat format = HTTP_DATE_FORMAT.get();
if(since!=null) {
try {
long ims = format.parse(since).getTime();
if(lastModified<ims+1000) {
// +1000 because date header is second-precision and Java has milli-second precision
rsp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return true;
}
} catch (ParseException e) {
// just ignore and serve the content
} catch (NumberFormatException e) {
// trying to locate a bug with Jetty
getServletContext().log("Error parsing ["+since+"]",e);
throw e;
}
}
String lastModifiedStr = format.format(new Date(lastModified));
rsp.setHeader("Last-Modified", lastModifiedStr);
if(expiration<=0)
rsp.setHeader("Expires",lastModifiedStr);
else
rsp.setHeader("Expires",format.format(new Date(new Date().getTime()+expiration)));
}
}
rsp.setHeader("Accept-Ranges","bytes"); // advertize that we support the range header
String mimeType = getMimeType(fileName);
rsp.setContentType(mimeType);
// use nosniff to enforce the content type we are setting above, instead of letting browser
// guess it on its own. I found http://security.stackexchange.com/questions/12896/
// a comprehensive discussion on this topic
rsp.setHeader("X-Content-Type-Options","nosniff");
int idx = fileName.lastIndexOf('.');
String ext = fileName.substring(idx+1);
OutputStream out = null;
if(mimeType.startsWith("text/") || TEXT_FILES.contains(ext)) {
// Need to duplicate this logic from ResponseImpl.getCompressedOutputStream,
// since we want to set content length if we are not using encoding.
String acceptEncoding = req.getHeader("Accept-Encoding");
if (acceptEncoding != null && acceptEncoding.contains("gzip")) {
// with gzip compression, Content-Length header needs to indicate the # of bytes after compression,
// so we can't compute it upfront.
out = rsp.getCompressedOutputStream(req);
}
}
// somewhat limited implementation of the partial GET
String range = req.getHeader("Range");
if(range!=null && contentLength!=-1) {// I'm lazy and only implementing this for known content length case
if(range.startsWith("bytes=")) {
range = range.substring(6);
Matcher m = RANGE_SPEC.matcher(range);
if(m.matches()) {
long s = Long.parseLong(m.group(1));
long e = m.group(2).length()>0
? Long.parseLong(m.group(2))+1 //range set is inclusive
: contentLength; // unspecified value means "all the way to the end"
e = Math.min(e,contentLength);
// ritual for responding to a partial GET
rsp.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
rsp.setHeader("Content-Range","bytes "+s+"-"+(e-1)+'/'+contentLength); // end is inclusive.
// prepare to send the partial content
DataInputStream dis = new DataInputStream(in);
long toSkip = s, thisSkip;
while (toSkip > 0 && (thisSkip = dis.skipBytes((int)Math.min(toSkip, Integer.MAX_VALUE))) > 0)
toSkip -= thisSkip;
if (toSkip > 0)
throw new IOException(
"skipBytes failure (" + toSkip + " of " + s + " bytes unskipped)");
in = new TruncatedInputStream(in,e-s);
contentLength = Math.min(e-s,contentLength);
}
// if the Range header doesn't look like what we can handle,
// pretend as if we didn't understand it, instead of doing a proper error reporting
}
}
if (out == null) {
if(contentLength!=-1)
rsp.setHeader("Content-Length", Long.toString(contentLength));
out = rsp.getOutputStream();
}
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0)
out.write(buf,0,len);
out.close();
return true;
} finally {
in.close();
}
}
/**
* Strings like "5-300", "0-900", or "100-"
*/
private static final Pattern RANGE_SPEC = Pattern.compile("([\\d]+)-([\\d]*)");
private String getMimeType(String fileName) {
if(fileName.startsWith("mime-type:"))
return fileName.substring("mime-type:".length());
int idx = fileName.lastIndexOf('/');
fileName = fileName.substring(idx+1);
idx = fileName.lastIndexOf('\\');
fileName = fileName.substring(idx+1);
String extension = fileName.substring(fileName.lastIndexOf('.')+1);
String mimeType = webApp.mimeTypes.get(extension);
if(mimeType==null) mimeType = getServletContext().getMimeType(fileName);
if(mimeType==null) mimeType="application/octet-stream";
if(webApp.defaultEncodingForStaticResources.containsKey(mimeType))
mimeType += ";charset="+webApp.defaultEncodingForStaticResources.get(mimeType);
return mimeType;
}
/**
* If the URL is "file://", return its file representation.
*
* See http://weblogs.java.net/blog/kohsuke/archive/2007/04/how_to_convert.html
*/
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "Protected by checks at other layers.")
/*package for test*/ File toFile(URL url) {
// TODO Fix this so that it works correctly. Should use URI and Path.
String urlstr = url.toExternalForm();
if(!urlstr.startsWith("file:"))
return null;
// File(String) does fs.normalize, which is really forgiving in fixing up
// malformed stuff. I couldn't make the other URL.toURI() or File(URI) work
// in all the cases that we test
return new File(URLDecoder.decode(urlstr.substring(5), StandardCharsets.UTF_8));
}
/**
* Performs stapler processing on the given root object and request URL.
*/
public void invoke(HttpServletRequest req, HttpServletResponse rsp, Object root, String url) throws IOException, ServletException {
RequestImpl sreq = new RequestImpl(this, req, new ArrayList<>(), new TokenList(url));
RequestImpl oreq = CURRENT_REQUEST.get();
CURRENT_REQUEST.set(sreq);
ResponseImpl srsp = new ResponseImpl(this, rsp);
ResponseImpl orsp = CURRENT_RESPONSE.get();
CURRENT_RESPONSE.set(srsp);
try {
invoke(sreq,srsp,root);
} finally {
CURRENT_REQUEST.set(oreq);
CURRENT_RESPONSE.set(orsp);
}
}
/**
* Try to dispatch the request against the given node, and if there's no route to deliver a request, return false.
*
* <p>
* If a route to deliver a request is found but it failed to handle the request, the request is considered
* handled and the method returns true.
*
* @see #invoke(RequestImpl, ResponseImpl, Object)
*/
boolean tryInvoke(RequestImpl req, ResponseImpl rsp, Object node ) throws IOException, ServletException {
Dispatcher.anonymizedTraceEval(req, rsp, node, "%s: Dispatch");
if(traceable())
traceEval(req,rsp,node);
if(node instanceof StaplerProxy) {
Dispatcher.anonymizedTraceEval(req,rsp,node,"%s: StaplerProxy.getTarget()");
if(traceable())
traceEval(req,rsp,node,"((StaplerProxy)",").getTarget()");
Object n = null;
try {
n = ((StaplerProxy)node).getTarget();
} catch (RuntimeException e) {
if (Function.renderResponse(req,rsp,node,e))
return true; // let the exception serve the request and we are done
else
throw e; // unprocessed exception
}
if (n == node) {
// if the proxy returns itself, assume that it doesn't want to proxy.
} else if (n == null) {
// if null, no one will handle the request
return false;
} else {
// recursion helps debugging by leaving the trace in the stack.
invoke(req,rsp,n);
return true;
}
}
// adds this node to ancestor list
AncestorImpl ancestor = new AncestorImpl(req, node);
ancestor.addToOwner();
// try overrides
if (node instanceof StaplerOverridable) {
StaplerOverridable o = (StaplerOverridable) node;
Collection<?> list = o.getOverrides();
if (list!=null) {
int count = 0;
for (Object subject : list) {
if (subject==null) continue;
Dispatcher.anonymizedTraceEval(req, rsp, node, "%s: StaplerOverridable.getOverrides()[i]");
if(traceable())
traceEval(req,rsp,node,"((StaplerOverridable)",").getOverrides()["+(count++)+']');
if (tryInvoke(req,rsp,subject))
return true;
}
}
}
MetaClass metaClass = webApp.getMetaClass(node);
try {
for( Dispatcher d : metaClass.dispatchers ) {
if(d.dispatch(req,rsp,node)) {
if(LOGGER.isLoggable(Level.FINER))
LOGGER.finer("Handled by "+d);
return true;
}
}
} catch (IllegalAccessException e) {
// this should never really happen
if (!isSocketException(e)) {
getServletContext().log("Error while serving "+req.getRequestURL(),e);
}
throw new ServletException(e);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause == null) { // ???
getServletContext().log("Error while serving " + req.getRequestURL(), e);
throw new ServletException();
}
// allow the exception from the dispatch to be handled. This is handy to throw HttpResponse as an exception
// from the getXyz method.
for (HttpResponseRenderer r : webApp.getResponseRenderers())
if (r.generateResponse(req,rsp,node,cause))
return true;
StringBuffer url = req.getRequestURL();
if (cause instanceof IOException) {
if (!isSocketException(e)) {
getServletContext().log("Error while serving " + url, e);
}
throw (IOException) cause;
}
if (cause instanceof ServletException) {
if (!isSocketException(e)) {
getServletContext().log("Error while serving " + url, e);
}
throw (ServletException) cause;
}
for (Class<?> c = cause.getClass(); c != null; c = c.getSuperclass()) {
if (c == Object.class) {
if (!isSocketException(e)) {
getServletContext().log("Error while serving " + url, e);
}
} else if (c.getName().equals("org.acegisecurity.AccessDeniedException")) {
// [HUDSON-4834] A stack trace is too noisy for this; could just need to log in.
// (Could consider doing this for all AcegiSecurityException's.)
getServletContext().log("While serving " + url + ": " + cause);
break;
}
}
throw new ServletException(cause);
}
if(node instanceof StaplerFallback) {
Dispatcher.anonymizedTraceEval(req, rsp, node, "%s: StaplerFallback.getStaplerFallback()");
if(traceable())
traceEval(req,rsp,node,"((StaplerFallback)",").getStaplerFallback()");
Object n;
try {
n = ((StaplerFallback)node).getStaplerFallback();
} catch (RuntimeException e) {
if (Function.renderResponse(req,rsp,node,e))
return true; // let the exception serve the request and we are done
else
throw e; // unprocessed exception
}
if(n!=node && n!=null) {
// delegate to the fallback object
invoke(req,rsp,n);
return true;
}
}
return false;
}
/**
* Used to detect exceptions thrown when writing content that seem to be due merely to a closed socket.
* @param x an exception that got caught
* @return true if this looks like a closed stream, false for some real problem
* @since 1.222
*/
public static boolean isSocketException(Throwable x) { // JENKINS-10524
if (x == null) {
return false;
}
if (x instanceof SocketException) {
return true;
}
if (String.valueOf(x.getMessage()).equals("Broken pipe")) { // TBD I18N
return true;
}
// JENKINS-20074: various things that Jetty and other components could throw
if (x instanceof EOFException) {
return true;
}
if (x instanceof IOException && "Closed".equals(x.getMessage())) { // org.eclipse.jetty.server.HttpOutput.print
return true;
}
if (x instanceof IOException && "finished".equals(x.getMessage())) { //com.jcraft.jzlib.DeflaterOutputStream.write
return true;
}
return isSocketException(x.getCause());
}
/**
* Try to dispatch the request against the given node, and if it fails, report an error to the client.
*/
@SuppressFBWarnings(value = "XSS_SERVLET", justification = "Handled by the escape() method or implementing code.")
void invoke(RequestImpl req, ResponseImpl rsp, Object node ) throws IOException, ServletException {
if(node==null) {
// node is null
if(!Dispatcher.isTraceEnabled(req)) {
rsp.sendError(SC_NOT_FOUND);
} else {
// show error page
rsp.setStatus(SC_NOT_FOUND);
rsp.setContentType("text/html;charset=UTF-8");
PrintWriter w = rsp.getWriter();
w.println("<html><body>");
w.println("<h1>404 Not Found</h1>");
w.println("<p>Stapler processed this HTTP request as follows, but couldn't find the resource to consume the request");
w.println("<pre>");
EvaluationTrace.get(req).printHtml(w);
w.println("<font color=red>-> unexpected null!</font>");
w.println("</pre>");
w.println("<p>If this 404 is unexpected, double check the last part of the trace to see if it should have evaluated to null.");
w.println("</body></html>");
}
return;
}
if (tryInvoke(req,rsp,node))
return; // done
// we really run out of options.
if(!Dispatcher.isTraceEnabled(req)) {
rsp.sendError(SC_NOT_FOUND);
} else {
// show error page
rsp.setStatus(SC_NOT_FOUND);
rsp.setContentType("text/html;charset=UTF-8");
PrintWriter w = rsp.getWriter();
w.println("<html><body>");
w.println("<h1>404 Not Found</h1>");
w.println("<p>Stapler processed this HTTP request as follows, but couldn't find the resource to consume the request");
w.println("<pre>");
EvaluationTrace.get(req).printHtml(w);
w.printf("<font color=red>-> No matching rule was found on <%s> for \"%s\"</font>%n", escape(node.toString()), req.tokens.assembleOriginalRestOfPath());
w.println("</pre>");
w.printf("<p><%s> has the following URL mappings, in the order of preference:", escape(node.toString()));
w.println("<ol>");
MetaClass metaClass = webApp.getMetaClass(node);
for (Dispatcher d : metaClass.dispatchers) {
w.println("<li>");
w.println(d.toString());
}
w.println("</ol>");
w.println("</body></html>");
}
}
@SuppressFBWarnings(value = "REQUESTDISPATCHER_FILE_DISCLOSURE", justification = "Forwarding the request to be handled correctly.")
public void forward(RequestDispatcher dispatcher, StaplerRequest req, HttpServletResponse rsp) throws ServletException, IOException {
dispatcher.forward(req,new ResponseImpl(this,rsp));
}
/**
* {@link ServletContext#getResource(String)} with caching.
*/
/*package*/ URL getResource(String name) throws MalformedURLException {
if (resourcePaths!=null)
return resourcePaths.get(name);
else
return context.getResource(name);
}
/**
* Gets the URL (e.g., "/WEB-INF/side-files/fully/qualified/class/name/jspName")
* from a class and the JSP name.
*/
public static String getViewURL(Class clazz,String jspName) {
return "/WEB-INF/side-files/"+clazz.getName().replace('.','/')+'/'+jspName;
}
/**
* Sets the specified object as the root of the web application.
*
* <p>
* This method should be invoked from your implementation of
* {@link ServletContextListener#contextInitialized(ServletContextEvent)}.
*
* <p>
* This is just a convenience method to invoke
* <code>servletContext.setAttribute("app",rootApp)</code>.
*
* <p>
* The root object is bound to the URL '/' and used to resolve
* all the requests to this web application.
*/
public static void setRoot( ServletContextEvent event, Object rootApp ) {
event.getServletContext().setAttribute("app",rootApp);
}
/**
* Sets the classloader used by {@link StaplerRequest#bindJSON(Class, JSONObject)} and its sibling methods.
*
* @deprecated
* Use {@link WebApp#setClassLoader(ClassLoader)}
*/
@Deprecated
public static void setClassLoader( ServletContext context, ClassLoader classLoader ) {
WebApp.get(context).setClassLoader(classLoader);
}
/**
* @deprecated
* Use {@link WebApp#getClassLoader()}
*/
@Deprecated
public static ClassLoader getClassLoader( ServletContext context ) {
return WebApp.get(context).getClassLoader();
}
/**
* @deprecated
* Use {@link WebApp#getClassLoader()}
*/
@Deprecated
public ClassLoader getClassLoader() {
return webApp.getClassLoader();
}
/**
* Gets the current {@link StaplerRequest} that the calling thread is associated with.
*/
public static StaplerRequest getCurrentRequest() {
return CURRENT_REQUEST.get();