亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php下HTTP Response中的Chunked編碼實(shí)現(xiàn)方法

 更新時(shí)間:2008年11月19日 17:43:48   作者:  
有時(shí)候,Web服務(wù)器生成HTTP Response是無(wú)法在Header就確定消息大小的,這時(shí)一般來(lái)說(shuō)服務(wù)器將不會(huì)提供Content-Length的頭信息,而采用Chunked編碼動(dòng)態(tài)的提供body內(nèi)容的長(zhǎng)度。
進(jìn)行Chunked編碼傳輸?shù)腍TTP Response會(huì)在消息頭部設(shè)置:
Transfer-Encoding: chunked
表示Content Body將用Chunked編碼傳輸內(nèi)容。
Chunked編碼使用若干個(gè)Chunk串連而成,由一個(gè)標(biāo)明長(zhǎng)度為0的chunk標(biāo)示結(jié)束。每個(gè)Chunk分為頭部和正文兩部分,頭部?jī)?nèi)容指定下一段正文的字符總數(shù)(十六進(jìn)制的數(shù)字)和數(shù)量單位(一般不寫),正文部分就是指定長(zhǎng)度的實(shí)際內(nèi)容,兩部分之間用回車換行(CRLF)隔開(kāi)。在最后一個(gè)長(zhǎng)度為0的Chunk中的內(nèi)容是稱為footer的內(nèi)容,是一些附加的Header信息(通常可以直接忽略)。具體的Chunk編碼格式如下:
復(fù)制代碼 代碼如下:

  Chunked-Body = *chunk
         "0" CRLF
         footer
         CRLF
  chunk = chunk-size [ chunk-ext ] CRLF
       chunk-data CRLF
  hex-no-zero = <HEX excluding "0">
  chunk-size = hex-no-zero *HEX
  chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-value ] )
  chunk-ext-name = token
  chunk-ext-val = token | quoted-string
  chunk-data = chunk-size(OCTET)
  footer = *entity-header

RFC文檔中的Chunked解碼過(guò)程如下:
復(fù)制代碼 代碼如下:

  length := 0
  read chunk-size, chunk-ext (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

最后提供一段PHP版本的chunked解碼代碼:
復(fù)制代碼 代碼如下:

$chunk_size = (integer)hexdec(fgets( $socket_fd, 4096 ) );
while(!feof($socket_fd) && $chunk_size > 0) {
$bodyContent .= fread( $socket_fd, $chunk_size );
fread( $socket_fd, 2 ); // skip \r\n
$chunk_size = (integer)hexdec(fgets( $socket_fd, 4096 ) );
}

相關(guān)文章

最新評(píng)論