문제

HTTP/HTTPS 요청에 대한 원시 TCP 클라이언트를 작성했지만 청크 인코딩 응답에 문제가 있습니다. HTTP/1.1은 요구 사항이므로 지원해야합니다.

Raw TCP는 내가 유지 해야하는 비즈니스 요구 사항이므로 .NET httpwebrequest/httpwebresponse로 전환 할 수 없습니다. 그러나 원시 HTTP 요청/응답을 HTTPWEBREQUEST/HTTPWEBRESPONSE로 변환하는 방법이있는 경우.

도움이 되었습니까?

해결책

시작하기 가장 좋은 곳은입니다 HTTP 1.1 사양, 청킹이 어떻게 작동하는지 설명합니다. 구체적으로 섹션 3.6.1.

3.6.1 청크 전송 코딩

청크 인코딩은 메시지 본체를 수정합니다.
자체 크기 표시기가있는 일련의 청크로 전송하십시오.
엔터티 헤더 필드가 포함 된 옵션 트레일러가 이어집니다. 이를 통해 동적으로 생성 된 컨텐츠를
수신자가
전체 메시지를 받았습니다.

   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 진수 숫자 문자열입니다.
청크. 청크 인코딩은 크기가있는 모든 청크에 의해 끝납니다.
0, 트레일러가 이어지고 빈 줄로 종료됩니다.

트레일러를 통해 발신자는 추가 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top