public static ByteRange parse ( String rangeHeader ) { if ( rangeHeader == null || rangeHeader . length ( ) < 7 ) { return null ; } if ( ! rangeHeader . startsWith ( "bytes=" ) ) { return null ; } List < Range > ranges = new ArrayList <> ( ) ; String [ ] parts = rangeHeader . substring ( 6 ) . split ( "," ) ; for ( String part : parts ) { try { int index = part . indexOf ( ' ' ) ; if ( index == 0 ) { //suffix range spec //represents the last N bytes //internally we represent this using a -1 as the start position long val = Long . parseLong ( part . substring ( 1 ) ) ; if ( val < 0 ) { UndertowLogger . REQUEST_LOGGER . debugf ( "Invalid range spec %s" , rangeHeader ) ; return null ; } ranges . add ( new Range ( - 1 , val ) ) ; } else { if ( index == - 1 ) { UndertowLogger . REQUEST_LOGGER . debugf ( "Invalid range spec %s" , rangeHeader ) ; return null ; } long start = Long . parseLong ( part . substring ( 0 , index ) ) ; if ( start < 0 ) { UndertowLogger . REQUEST_LOGGER . debugf ( "Invalid range spec %s" , rangeHeader ) ; return null ; } long end ; if ( index + 1 < part . length ( ) ) { end = Long . parseLong ( part . substring ( index + 1 ) ) ; } else { end = - 1 ; } ranges . add ( new Range ( start , end ) ) ; } } catch ( NumberFormatException e ) { UndertowLogger . REQUEST_LOGGER . debugf ( "Invalid range spec %s" , rangeHeader ) ; return null ; } } if ( ranges . isEmpty ( ) ) { return null ; } return new ByteRange ( ranges ) ; }
Attempts to parse a range request . If the range request is invalid it will just return null so that it may be ignored .