2008-11-25 5 views
3

HTTP/HTTPS 요청에 원시 TCP 클라이언트를 작성했지만 청크 인코딩 응답에 문제가 있습니다. HTTP/1.1은 요구 사항이므로 지원해야합니다..NET에서 청크 인코딩 구현 (또는 적어도 의사 코드)

원시 TCP 그러므로 내가 그러나 .NET의 HttpWebRequest/HttpWebResponse 클래스로 전환 할 수 없습니다, 내가 계속해야 비즈니스 요구 사항입니다 HttpWebRequest를/HttpWebResponse 클래스로 RAW HTTP 요청/응답을 변환 할 수있는 방법이 있다면 거라고 작업.

답변

5

가장 좋은 곳은 http 1.1 specification으로 청킹이 어떻게 작동하는지 보여줍니다. 3.6.1 절.

는 는

는 3.6 청크 전송은

청크 인코딩 자체 크기 표시기 청크 일련
전송하여, 각각하기 위해 메시지의 체를 수정,
따랐다 코딩 엔티티 헤더 필드가 포함 된 OPTIONAL 예고편 이 동적 수신자가
전체 메시지를 수신 한 것을 확인하기위한 콘텐츠가 필요한
정보와 함께 전송 될 수있게 제조.

Chunked-Body = *chunk 
        last-chunk 
        trailer 
        CRLF 

    chunk   = chunk-size [ chunk-extension ] CRLF 
        chunk-data CRLF 
    chunk-size  = 1*HEX 
    last-chunk  = 1*("0") [ chunk-extension ] CRLF 

    chunk-extension= *(";" chunk-ext-name [ "=" chunk-ext-val ]) 
    chunk-ext-name = token 
    chunk-ext-val = token | quoted-string 
    chunk-data  = chunk-size(OCTET) 
    trailer  = *(entity-header CRLF) 

청크 크기 필드
청크의 크기를 나타내는 16 진수의 스트링이다. 청크 인코딩은
인 청크로 끝나는 이며 트레일러가 이어지고 은 빈 줄로 끝납니다.

예고편을 사용하면 보낸 사람이 에 추가 HTTP 헤더
필드를 메시지 끝에 포함 할 수 있습니다. 트레일러 헤더 필드는 에 사용되어 트레일러에 포함 된 인 헤더 필드를 나타낼 수 있습니다 ( 14.40 참조).

이미 응답에서 헤더를 읽은 것으로 가정하고 의사 코드는 다음과 같이 보일 것이다 스트림의 다음의 바이트를 가리키는 :

done = false; 
uint8 bytes[]; 
while (!done) 
{ 
    chunksizeString = readuntilCRLF(); // read in the chunksize as a string 
    chunksizeString.strip(); // strip off the CRLF 
    chunksize = chunksizeString.convertHexString2Int(); // convert the hex string to an integer. 
    bytes.append(readXBytes(chunksize)); // read in the x bytes and append them to your buffer. 
    readCRLF(); // read the trailing CRLF and throw it away. 
    if (chunksize == 0) 
    done = true; // 

} 
// now read the trailer if any 
// trailer is optional, so it may be just the empty string 
trailer = readuntilCRLF() 
trailer = trailer.strip() 
if (trailer != "") 
    readCRLF(); // read out the last CRLF and we are done. 

이것은 청크-확장을 무시를 부분이 있지만 ";" 그것을 분리하는 것이 쉬워야합니다. 이것은 당신을 시작하게하는데 충분할 것입니다. 청크 문자열 에 "0x"가 붙지 않습니다. 다시, 지금은 잘 작동하고

length := 0 
    read chunk-size, chunk-extension (if any) and CRLF 
    while (chunk-size > 0) { 
     read chunk-data and CRLF 
     append chunk-data to entity-body 
     length := length + chunk-size 
     read chunk-size and CRLF 
    } 
    read entity-header 
    while (entity-header not empty) { 
     append entity-header to existing header fields 
     read entity-header 
    } 
    Content-Length := length 
    Remove "chunked" from Transfer-Encoding 
+0

감사 : 미래 참고로 –

1

또한 내가이 발견했다.