3

HTTP/HTTPS リクエスト用の未加工の TCP クライアントを作成しましたが、チャンク エンコーディング レスポンスに問題があります。HTTP/1.1 は要件であるため、サポートする必要があります。

生の TCP は維持する必要があるビジネス要件であるため、.NET HTTPWebRequest/HTTPWebResponse に切り替えることはできません

4

2 に答える 2

5

開始するのに最適な場所はhttp 1.1 仕様で、チャンクがどのように機能するかを示しています。特にセクション 3.6.1。

3.6.1 チャンク転送コーディング

チャンク エンコーディングは、メッセージの本文を変更し
て、一連のチャンクとして転送します。それぞれに独自のサイズ インジケーターがあり、
その後にエンティティ ヘッダー フィールドを含む 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 ヘッダー フィールドを含めることができます。Trailer ヘッダー フィールドは、どのヘッダー フィールドがトレーラーに含まれているかを示すために使用できます (セクション 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」がないことに注意してください。

于 2008-11-25T19:35:06.110 に答える
1

将来の参考のために、これも見つけました:

 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
于 2008-11-28T22:24:53.053 に答える