protected void parse ( ) { // have we already parsed the input? if ( _parsed ) return ; _parsed = true ; try { // initialize _parts = new MultiMap <> ( ) ; // if its not a multipart request, don't parse it if ( _contentType == null || ! _contentType . startsWith ( "multipart/form-data" ) ) return ; // sort out the location to which to write the files if ( _config . getLocation ( ) == null ) _tmpDir = _contextTmpDir ; else if ( "" . equals ( _config . getLocation ( ) ) ) _tmpDir = _contextTmpDir ; else { File f = new File ( _config . getLocation ( ) ) ; if ( f . isAbsolute ( ) ) _tmpDir = f ; else _tmpDir = new File ( _contextTmpDir , _config . getLocation ( ) ) ; } if ( ! _tmpDir . exists ( ) ) _tmpDir . mkdirs ( ) ; String contentTypeBoundary = "" ; int bstart = _contentType . indexOf ( "boundary=" ) ; if ( bstart >= 0 ) { int bend = _contentType . indexOf ( ";" , bstart ) ; bend = ( bend < 0 ? _contentType . length ( ) : bend ) ; contentTypeBoundary = QuotedStringTokenizer . unquote ( value ( _contentType . substring ( bstart , bend ) ) . trim ( ) ) ; } Handler handler = new Handler ( ) ; MultiPartParser parser = new MultiPartParser ( handler , contentTypeBoundary ) ; byte [ ] data = new byte [ _bufferSize ] ; int len ; long total = 0 ; while ( true ) { len = _in . read ( data ) ; if ( len > 0 ) { // keep running total of size of bytes read from input and throw an exception if exceeds MultipartConfigElement._maxRequestSize total += len ; if ( _config . getMaxRequestSize ( ) > 0 && total > _config . getMaxRequestSize ( ) ) { _err = new IllegalStateException ( "Request exceeds maxRequestSize (" + _config . getMaxRequestSize ( ) + ")" ) ; return ; } ByteBuffer buffer = BufferUtils . toBuffer ( data ) ; buffer . limit ( len ) ; if ( parser . parse ( buffer , false ) ) break ; if ( buffer . hasRemaining ( ) ) throw new IllegalStateException ( "Buffer did not fully consume" ) ; } else if ( len == - 1 ) { parser . parse ( BufferUtils . EMPTY_BUFFER , true ) ; break ; } } // check for exceptions if ( _err != null ) { return ; } // check we read to the end of the message if ( parser . getState ( ) != MultiPartParser . State . END ) { if ( parser . getState ( ) == MultiPartParser . State . PREAMBLE ) _err = new IOException ( "Missing initial multi part boundary" ) ; else _err = new IOException ( "Incomplete Multipart" ) ; } if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( "Parsing Complete {} err={}" , parser , _err ) ; } } catch ( Throwable e ) { _err = e ; } }
Parse if necessary the multipart stream .