/**
    Very simple batch response parser. Only processes the first operation's response.

    Expects well formed batch responses like:
        --batchresponse_08764fd4-3946-4cd6-97cf-1ec7d9740551
        Content-Type: application/http
        Content-Transfer-Encoding: binary

        HTTP/1.1 200 OK
        Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
        OData-Version: 4.0

        {"@odata.context":"https://app.me.tfsallin.net:43526/_odata/$metadata#Projects","value":[{"ProjectSK":"ed2c5089-ffba-4ffc-a6cb-3fcdc0cef924","ProjectId":"ed2c5089-ffba-4ffc-a6cb-3fcdc0cef924","ProjectName":"ODataBatchTest"}]}
        --batchresponse_08764fd4-3946-4cd6-97cf-1ec7d9740551
*/ export class BatchResponseParser { private static ResponseStatusRegex : RegExp = /^HTTP\/1\.\d (\d{3}) (.*)/i ; private static ResponseHeaderRegex : RegExp = /^([^()<>@,;:\\"\/[\]?={} \t]+)\s?:\s?(.*)/ ; private position : number = 0 ; private content : string ; private batchSeparator : string ; private responseLine : any ; private responseHeaders : any ; private responseContent : string ; constructor ( content , batchSeparator ) { this . content = content ; this . batchSeparator = batchSeparator ; this . parseRawResponse ( ) ; } private readTo ( until ) { var start = this . position ; var end = this . content . indexOf ( until , this . position ) ; if ( end === - 1 ) { return null ; } this . position = end + until . length ; return this . content . substring ( start , end ) ; } private readLine ( ) { return this . readTo ( "\r\n" ) ; } /**
     * Parses the raw response content and sets the responseLine, responseHeader, and responseContent values
     */ private parseRawResponse ( ) { var responseSeparator = `--${ this . batchSeparator }` ; this . readTo ( responseSeparator ) ; this . readTo ( "\r\n\r\n" ) ; this . responseLine = this . readResponseLine ( ) ; this . responseHeaders = this . readResponseHeaders ( ) ; this . readLine ( ) ; this . responseContent = this . readLine ( ) ; } private readResponseLine ( ) { var start = this . position ; var resLine = this . readLine ( ) ; var match = BatchResponseParser . ResponseStatusRegex . exec ( resLine ) ; if ( match ) { return { StatusCode : match [ 1 ] , StatusText : match [ 2 ] } ; } else { this . position = start ; } return null ; } private readResponseHeaders ( ) { var headers = { } ; var start = this . position ; var line ; var match ; do { start = this . position ; line = this . readLine ( ) ; match = BatchResponseParser . ResponseHeaderRegex . exec ( line ) ; if ( match !== null ) { headers [ match [ 1 ] ] = match [ 2 ] ; } else { this . position = start ; } } while ( line && match ) ; return headers ; } public getResponseLine ( ) { return this . responseLine ; } public getResponseHeaders ( ) { return this . responseHeaders ; } public getResponseContent ( ) { return this . responseContent ; } }