View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  import java.io.BufferedInputStream;
22  import java.io.BufferedOutputStream;
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.FileOutputStream;
28  import java.io.FilterInputStream;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.nio.charset.StandardCharsets;
33  import java.nio.file.Files;
34  import java.nio.file.Path;
35  import java.nio.file.StandardCopyOption;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.List;
40  import java.util.Locale;
41  
42  import javax.servlet.MultipartConfigElement;
43  import javax.servlet.http.Part;
44  
45  import org.eclipse.jetty.util.log.Log;
46  import org.eclipse.jetty.util.log.Logger;
47  
48  
49  
50  /**
51   * MultiPartInputStream
52   *
53   * Handle a MultiPart Mime input stream, breaking it up on the boundary into files and strings.
54   */
55  public class MultiPartInputStreamParser
56  {
57      private static final Logger LOG = Log.getLogger(MultiPartInputStreamParser.class);
58      public static final MultipartConfigElement  __DEFAULT_MULTIPART_CONFIG = new MultipartConfigElement(System.getProperty("java.io.tmpdir"));
59      protected InputStream _in;
60      protected MultipartConfigElement _config;
61      protected String _contentType;
62      protected MultiMap<Part> _parts;
63      protected File _tmpDir;
64      protected File _contextTmpDir;
65      protected boolean _deleteOnExit;
66  
67  
68  
69      public class MultiPart implements Part
70      {
71          protected String _name;
72          protected String _filename;
73          protected File _file;
74          protected OutputStream _out;
75          protected ByteArrayOutputStream2 _bout;
76          protected String _contentType;
77          protected MultiMap<String> _headers;
78          protected long _size = 0;
79          protected boolean _temporary = true;
80  
81          public MultiPart (String name, String filename)
82          throws IOException
83          {
84              _name = name;
85              _filename = filename;
86          }
87  
88          protected void setContentType (String contentType)
89          {
90              _contentType = contentType;
91          }
92  
93  
94          protected void open()
95          throws IOException
96          {
97              //Write to a buffer in memory until we discover we've exceed the
98              //MultipartConfig fileSizeThreshold
99              _out = _bout= new ByteArrayOutputStream2();
100         }
101 
102         protected void close()
103         throws IOException
104         {
105             _out.close();
106         }
107 
108 
109         protected void write (int b)
110         throws IOException
111         {
112             if (MultiPartInputStreamParser.this._config.getMaxFileSize() > 0 && _size + 1 > MultiPartInputStreamParser.this._config.getMaxFileSize())
113                 throw new IllegalStateException ("Multipart Mime part "+_name+" exceeds max filesize");
114 
115             if (MultiPartInputStreamParser.this._config.getFileSizeThreshold() > 0 && _size + 1 > MultiPartInputStreamParser.this._config.getFileSizeThreshold() && _file==null)
116                 createFile();
117 
118             _out.write(b);
119             _size ++;
120         }
121 
122         protected void write (byte[] bytes, int offset, int length)
123         throws IOException
124         {
125             if (MultiPartInputStreamParser.this._config.getMaxFileSize() > 0 && _size + length > MultiPartInputStreamParser.this._config.getMaxFileSize())
126                 throw new IllegalStateException ("Multipart Mime part "+_name+" exceeds max filesize");
127 
128             if (MultiPartInputStreamParser.this._config.getFileSizeThreshold() > 0 && _size + length > MultiPartInputStreamParser.this._config.getFileSizeThreshold() && _file==null)
129                 createFile();
130            
131             _out.write(bytes, offset, length);
132             _size += length;
133         }
134 
135         protected void createFile ()
136         throws IOException
137         {
138             _file = File.createTempFile("MultiPart", "", MultiPartInputStreamParser.this._tmpDir);
139             
140             if (_deleteOnExit)
141                 _file.deleteOnExit();
142             FileOutputStream fos = new FileOutputStream(_file);
143             BufferedOutputStream bos = new BufferedOutputStream(fos);
144 
145             if (_size > 0 && _out != null)
146             {
147                 //already written some bytes, so need to copy them into the file
148                 _out.flush();
149                 _bout.writeTo(bos);
150                 _out.close();
151                 _bout = null;
152             }
153             _out = bos;
154         }
155 
156 
157 
158         protected void setHeaders(MultiMap<String> headers)
159         {
160             _headers = headers;
161         }
162 
163         /**
164          * @see javax.servlet.http.Part#getContentType()
165          */
166         public String getContentType()
167         {
168             return _contentType;
169         }
170 
171         /**
172          * @see javax.servlet.http.Part#getHeader(java.lang.String)
173          */
174         public String getHeader(String name)
175         {
176             if (name == null)
177                 return null;
178             return (String)_headers.getValue(name.toLowerCase(Locale.ENGLISH), 0);
179         }
180 
181         /**
182          * @see javax.servlet.http.Part#getHeaderNames()
183          */
184         public Collection<String> getHeaderNames()
185         {
186             return _headers.keySet();
187         }
188 
189         /**
190          * @see javax.servlet.http.Part#getHeaders(java.lang.String)
191          */
192         public Collection<String> getHeaders(String name)
193         {
194            return _headers.getValues(name);
195         }
196 
197         /**
198          * @see javax.servlet.http.Part#getInputStream()
199          */
200         public InputStream getInputStream() throws IOException
201         {
202            if (_file != null)
203            {
204                //written to a file, whether temporary or not
205                return new BufferedInputStream (new FileInputStream(_file));
206            }
207            else
208            {
209                //part content is in memory
210                return new ByteArrayInputStream(_bout.getBuf(),0,_bout.size());
211            }
212         }
213 
214         
215         /** 
216          * @see javax.servlet.http.Part#getSubmittedFileName()
217          */
218         @Override
219         public String getSubmittedFileName()
220         {
221             return getContentDispositionFilename();
222         }
223 
224         public byte[] getBytes()
225         {
226             if (_bout!=null)
227                 return _bout.toByteArray();
228             return null;
229         }
230 
231         /**
232          * @see javax.servlet.http.Part#getName()
233          */
234         public String getName()
235         {
236            return _name;
237         }
238 
239         /**
240          * @see javax.servlet.http.Part#getSize()
241          */
242         public long getSize()
243         {
244             return _size;         
245         }
246 
247         /**
248          * @see javax.servlet.http.Part#write(java.lang.String)
249          */
250         public void write(String fileName) throws IOException
251         {
252             if (_file == null)
253             {
254                 _temporary = false;
255                 
256                 //part data is only in the ByteArrayOutputStream and never been written to disk
257                 _file = new File (_tmpDir, fileName);
258 
259                 BufferedOutputStream bos = null;
260                 try
261                 {
262                     bos = new BufferedOutputStream(new FileOutputStream(_file));
263                     _bout.writeTo(bos);
264                     bos.flush();
265                 }
266                 finally
267                 {
268                     if (bos != null)
269                         bos.close();
270                     _bout = null;
271                 }
272             }
273             else
274             {
275                 //the part data is already written to a temporary file, just rename it
276                 _temporary = false;
277 
278                 Path src = _file.toPath();
279                 Path target = src.resolveSibling(fileName);
280                 Files.move(src, target, StandardCopyOption.REPLACE_EXISTING);
281                 _file = target.toFile();
282             }
283         }
284 
285         /**
286          * Remove the file, whether or not Part.write() was called on it
287          * (ie no longer temporary)
288          * @see javax.servlet.http.Part#delete()
289          */
290         public void delete() throws IOException
291         {
292             if (_file != null && _file.exists())
293                 _file.delete();     
294         }
295         
296         /**
297          * Only remove tmp files.
298          * 
299          * @throws IOException if unable to delete the file
300          */
301         public void cleanUp() throws IOException
302         {
303             if (_temporary && _file != null && _file.exists())
304                 _file.delete();
305         }
306 
307 
308         /**
309          * Get the file
310          * @return the file, if any, the data has been written to.
311          */
312         public File getFile ()
313         {
314             return _file;
315         }
316 
317 
318         /**
319          * Get the filename from the content-disposition.
320          * @return null or the filename
321          */
322         public String getContentDispositionFilename ()
323         {
324             return _filename;
325         }
326     }
327 
328 
329 
330 
331     /**
332      * @param in Request input stream
333      * @param contentType Content-Type header
334      * @param config MultipartConfigElement
335      * @param contextTmpDir javax.servlet.context.tempdir
336      */
337     public MultiPartInputStreamParser (InputStream in, String contentType, MultipartConfigElement config, File contextTmpDir)
338     {
339         _in = new ReadLineInputStream(in);
340        _contentType = contentType;
341        _config = config;
342        _contextTmpDir = contextTmpDir;
343        if (_contextTmpDir == null)
344            _contextTmpDir = new File (System.getProperty("java.io.tmpdir"));
345        
346        if (_config == null)
347            _config = new MultipartConfigElement(_contextTmpDir.getAbsolutePath());
348     }
349 
350     /**
351      * Get the already parsed parts.
352      * @return the parts that were parsed
353      */
354     public Collection<Part> getParsedParts()
355     {
356         if (_parts == null)
357             return Collections.emptyList();
358 
359         Collection<List<Part>> values = _parts.values();
360         List<Part> parts = new ArrayList<Part>();
361         for (List<Part> o: values)
362         {
363             List<Part> asList = LazyList.getList(o, false);
364             parts.addAll(asList);
365         }
366         return parts;
367     }
368 
369     /**
370      * Delete any tmp storage for parts, and clear out the parts list.
371      * 
372      * @throws MultiException if unable to delete the parts
373      */
374     public void deleteParts ()
375     throws MultiException
376     {
377         Collection<Part> parts = getParsedParts();
378         MultiException err = new MultiException();
379         for (Part p:parts)
380         {
381             try
382             {
383                 ((MultiPartInputStreamParser.MultiPart)p).cleanUp();
384             } 
385             catch(Exception e)
386             {     
387                 err.add(e); 
388             }
389         }
390         _parts.clear();
391         
392         err.ifExceptionThrowMulti();
393     }
394 
395    
396     /**
397      * Parse, if necessary, the multipart data and return the list of Parts.
398      * 
399      * @return the parts 
400      * @throws IOException if unable to get the parts
401      */
402     public Collection<Part> getParts()
403     throws IOException
404     {
405         parse();
406         Collection<List<Part>> values = _parts.values();
407         List<Part> parts = new ArrayList<Part>();
408         for (List<Part> o: values)
409         {
410             List<Part> asList = LazyList.getList(o, false);
411             parts.addAll(asList);
412         }
413         return parts;
414     }
415 
416 
417     /**
418      * Get the named Part.
419      * 
420      * @param name the part name
421      * @return the parts
422      * @throws IOException if unable to get the part
423      */
424     public Part getPart(String name)
425     throws IOException
426     {
427         parse();
428         return (Part)_parts.getValue(name, 0);
429     }
430 
431 
432     /**
433      * Parse, if necessary, the multipart stream.
434      * 
435      * @throws IOException if unable to parse
436      */
437     protected void parse ()
438     throws IOException
439     {
440         //have we already parsed the input?
441         if (_parts != null)
442             return;
443 
444         //initialize
445         long total = 0; //keep running total of size of bytes read from input and throw an exception if exceeds MultipartConfigElement._maxRequestSize
446         _parts = new MultiMap<Part>();
447 
448         //if its not a multipart request, don't parse it
449         if (_contentType == null || !_contentType.startsWith("multipart/form-data"))
450             return;
451 
452         //sort out the location to which to write the files
453 
454         if (_config.getLocation() == null)
455             _tmpDir = _contextTmpDir;
456         else if ("".equals(_config.getLocation()))
457             _tmpDir = _contextTmpDir;
458         else
459         {
460             File f = new File (_config.getLocation());
461             if (f.isAbsolute())
462                 _tmpDir = f;
463             else
464                 _tmpDir = new File (_contextTmpDir, _config.getLocation());
465         }
466 
467         if (!_tmpDir.exists())
468             _tmpDir.mkdirs();
469 
470         String contentTypeBoundary = "";
471         int bstart = _contentType.indexOf("boundary=");
472         if (bstart >= 0)
473         {
474             int bend = _contentType.indexOf(";", bstart);
475             bend = (bend < 0? _contentType.length(): bend);
476             contentTypeBoundary = QuotedStringTokenizer.unquote(value(_contentType.substring(bstart,bend)).trim());
477         }
478         
479         String boundary="--"+contentTypeBoundary;
480         byte[] byteBoundary=(boundary+"--").getBytes(StandardCharsets.ISO_8859_1);
481 
482         // Get first boundary
483         String line = null;
484         try
485         {
486             line=((ReadLineInputStream)_in).readLine();  
487         }
488         catch (IOException e)
489         {
490             LOG.warn("Badly formatted multipart request");
491             throw e;
492         }
493         
494         if (line == null)
495             throw new IOException("Missing content for multipart request");
496         
497         boolean badFormatLogged = false;
498         line=line.trim();
499         while (line != null && !line.equals(boundary))
500         {
501             if (!badFormatLogged)
502             {
503                 LOG.warn("Badly formatted multipart request");
504                 badFormatLogged = true;
505             }
506             line=((ReadLineInputStream)_in).readLine();
507             line=(line==null?line:line.trim());
508         }
509 
510         if (line == null)
511             throw new IOException("Missing initial multi part boundary");
512 
513         // Read each part
514         boolean lastPart=false;
515 
516         outer:while(!lastPart)
517         {
518             String contentDisposition=null;
519             String contentType=null;
520             String contentTransferEncoding=null;
521             
522             MultiMap<String> headers = new MultiMap<String>();
523             while(true)
524             {
525                 line=((ReadLineInputStream)_in).readLine();
526                 
527                 //No more input
528                 if(line==null)
529                     break outer;
530                 
531                 //end of headers:
532                 if("".equals(line))
533                     break;
534            
535                 total += line.length();
536                 if (_config.getMaxRequestSize() > 0 && total > _config.getMaxRequestSize())
537                     throw new IllegalStateException ("Request exceeds maxRequestSize ("+_config.getMaxRequestSize()+")");
538 
539                 //get content-disposition and content-type
540                 int c=line.indexOf(':',0);
541                 if(c>0)
542                 {
543                     String key=line.substring(0,c).trim().toLowerCase(Locale.ENGLISH);
544                     String value=line.substring(c+1,line.length()).trim();
545                     headers.put(key, value);
546                     if (key.equalsIgnoreCase("content-disposition"))
547                         contentDisposition=value;
548                     if (key.equalsIgnoreCase("content-type"))
549                         contentType = value;
550                     if(key.equals("content-transfer-encoding"))
551                         contentTransferEncoding=value;
552                 }
553             }
554 
555             // Extract content-disposition
556             boolean form_data=false;
557             if(contentDisposition==null)
558             {
559                 throw new IOException("Missing content-disposition");
560             }
561 
562             QuotedStringTokenizer tok=new QuotedStringTokenizer(contentDisposition,";", false, true);
563             String name=null;
564             String filename=null;
565             while(tok.hasMoreTokens())
566             {
567                 String t=tok.nextToken().trim();
568                 String tl=t.toLowerCase(Locale.ENGLISH);
569                 if(t.startsWith("form-data"))
570                     form_data=true;
571                 else if(tl.startsWith("name="))
572                     name=value(t);
573                 else if(tl.startsWith("filename="))
574                     filename=filenameValue(t);
575             }
576 
577             // Check disposition
578             if(!form_data)
579             {
580                 continue;
581             }
582             //It is valid for reset and submit buttons to have an empty name.
583             //If no name is supplied, the browser skips sending the info for that field.
584             //However, if you supply the empty string as the name, the browser sends the
585             //field, with name as the empty string. So, only continue this loop if we
586             //have not yet seen a name field.
587             if(name==null)
588             {
589                 continue;
590             }
591 
592             //Have a new Part
593             MultiPart part = new MultiPart(name, filename);
594             part.setHeaders(headers);
595             part.setContentType(contentType);
596             _parts.add(name, part);
597             part.open();
598             
599             InputStream partInput = null;
600             if ("base64".equalsIgnoreCase(contentTransferEncoding))
601             {
602                 partInput = new Base64InputStream((ReadLineInputStream)_in);
603             }
604             else if ("quoted-printable".equalsIgnoreCase(contentTransferEncoding))
605             {
606                 partInput = new FilterInputStream(_in)
607                 {
608                     @Override
609                     public int read() throws IOException
610                     {
611                         int c = in.read();
612                         if (c >= 0 && c == '=')
613                         {
614                             int hi = in.read();
615                             int lo = in.read();
616                             if (hi < 0 || lo < 0)
617                             {
618                                 throw new IOException("Unexpected end to quoted-printable byte");
619                             }
620                             char[] chars = new char[] { (char)hi, (char)lo };
621                             c = Integer.parseInt(new String(chars),16);
622                         }
623                         return c;
624                     }
625                 };
626             }
627             else
628                 partInput = _in;
629 
630             
631             try
632             {
633                 int state=-2;
634                 int c;
635                 boolean cr=false;
636                 boolean lf=false;
637 
638                 // loop for all lines
639                 while(true)
640                 {
641                     int b=0;
642                     while((c=(state!=-2)?state:partInput.read())!=-1)
643                     {
644                         total ++;
645                         if (_config.getMaxRequestSize() > 0 && total > _config.getMaxRequestSize())
646                             throw new IllegalStateException("Request exceeds maxRequestSize ("+_config.getMaxRequestSize()+")");
647 
648                         state=-2;
649                         
650                         // look for CR and/or LF
651                         if(c==13||c==10)
652                         {
653                             if(c==13)
654                             {
655                                 partInput.mark(1);
656                                 int tmp=partInput.read();
657                                 if (tmp!=10)
658                                     partInput.reset();
659                                 else
660                                     state=tmp;
661                             }
662                             break;
663                         }
664                         
665                         // Look for boundary
666                         if(b>=0&&b<byteBoundary.length&&c==byteBoundary[b])
667                         {
668                             b++;
669                         }
670                         else
671                         {
672                             // Got a character not part of the boundary, so we don't have the boundary marker.
673                             // Write out as many chars as we matched, then the char we're looking at.
674                             if(cr)
675                                 part.write(13);
676 
677                             if(lf)
678                                 part.write(10);
679 
680                             cr=lf=false;
681                             if(b>0)
682                                 part.write(byteBoundary,0,b);
683 
684                             b=-1;
685                             part.write(c);
686                         }
687                     }
688                     
689                     // Check for incomplete boundary match, writing out the chars we matched along the way
690                     if((b>0&&b<byteBoundary.length-2)||(b==byteBoundary.length-1))
691                     {
692                         if(cr)
693                             part.write(13);
694 
695                         if(lf)
696                             part.write(10);
697 
698                         cr=lf=false;
699                         part.write(byteBoundary,0,b);
700                         b=-1;
701                     }
702                     
703                     // Boundary match. If we've run out of input or we matched the entire final boundary marker, then this is the last part.
704                     if(b>0||c==-1)
705                     {
706                        
707                         if(b==byteBoundary.length)
708                             lastPart=true;
709                         if(state==10)
710                             state=-2;
711                         break;
712                     }
713                     
714                     // handle CR LF
715                     if(cr)
716                         part.write(13);
717 
718                     if(lf)
719                         part.write(10);
720 
721                     cr=(c==13);
722                     lf=(c==10||state==10);
723                     if(state==10)
724                         state=-2;
725                 }
726             }
727             finally
728             {
729 
730                 part.close();
731             }
732         }
733         if (!lastPart)
734             throw new IOException("Incomplete parts");
735     }
736     
737     public void setDeleteOnExit(boolean deleteOnExit)
738     {
739         _deleteOnExit = deleteOnExit;
740     }
741 
742 
743     public boolean isDeleteOnExit()
744     {
745         return _deleteOnExit;
746     }
747 
748 
749     /* ------------------------------------------------------------ */
750     private String value(String nameEqualsValue)
751     {
752         int idx = nameEqualsValue.indexOf('=');
753         String value = nameEqualsValue.substring(idx+1).trim();
754         return QuotedStringTokenizer.unquoteOnly(value);
755     }
756     
757     
758     /* ------------------------------------------------------------ */
759     private String filenameValue(String nameEqualsValue)
760     {
761         int idx = nameEqualsValue.indexOf('=');
762         String value = nameEqualsValue.substring(idx+1).trim();
763 
764         if (value.matches(".??[a-z,A-Z]\\:\\\\[^\\\\].*"))
765         {
766             //incorrectly escaped IE filenames that have the whole path
767             //we just strip any leading & trailing quotes and leave it as is
768             char first=value.charAt(0);
769             if (first=='"' || first=='\'')
770                 value=value.substring(1);
771             char last=value.charAt(value.length()-1);
772             if (last=='"' || last=='\'')
773                 value = value.substring(0,value.length()-1);
774 
775             return value;
776         }
777         else
778             //unquote the string, but allow any backslashes that don't
779             //form a valid escape sequence to remain as many browsers
780             //even on *nix systems will not escape a filename containing
781             //backslashes
782             return QuotedStringTokenizer.unquoteOnly(value, true);
783     }
784 
785     
786 
787     private static class Base64InputStream extends InputStream
788     {
789         ReadLineInputStream _in;
790         String _line;
791         byte[] _buffer;
792         int _pos;
793 
794     
795         public Base64InputStream(ReadLineInputStream rlis)
796         {
797             _in = rlis;
798         }
799 
800         @Override
801         public int read() throws IOException
802         {
803             if (_buffer==null || _pos>= _buffer.length)
804             {
805                 //Any CR and LF will be consumed by the readLine() call.
806                 //We need to put them back into the bytes returned from this
807                 //method because the parsing of the multipart content uses them
808                 //as markers to determine when we've reached the end of a part.
809                 _line = _in.readLine(); 
810                 if (_line==null)
811                     return -1;  //nothing left
812                 if (_line.startsWith("--"))
813                     _buffer=(_line+"\r\n").getBytes(); //boundary marking end of part
814                 else if (_line.length()==0)
815                     _buffer="\r\n".getBytes(); //blank line
816                 else
817                 {
818                     ByteArrayOutputStream baos = new ByteArrayOutputStream((4*_line.length()/3)+2);
819                     B64Code.decode(_line, baos);
820                     baos.write(13);
821                     baos.write(10);
822                     _buffer = baos.toByteArray();
823                 }
824 
825                 _pos=0;
826             }
827             
828             return _buffer[_pos++];
829         }
830     }
831 }