http协议之Transfer-Encoding及HttpCore实现

Transfer-Encoding简介

transfer-eccoding所描述的是消息请求(request)和响应(response)所附带的实体对象(entity)的传输形式,规范定义格式如下:

Transfer-Encoding = "Transfer-Encoding" ":" 1#transfer-coding 

  举个例子:Transfer-Encoding: chunked

transfer-encoding的可选值有:chunked,identity ;
transfer-encoding的可选值有:chunked,identity,从字面意义可以理解,前者指把要发送传输的数据切割成一系列的块数据传输,后者指传输时不做任何处理,自身的本质数据形式传输。举个例子,如果我们要传输一本“红楼梦”小说到服务器,chunked方式就会先把这本小说分成一章一章的,然后逐个章节上传,而identity方式则是从小说的第一个字按顺序传输到最后一个字结束。

相关的头定义

Content-Encoding : content-encoding和transfer-encoding所作用的对象不同,行为目标也不同,前者是对数据内容采用什么样的编码方式,后者是对数据传输采用什么样的编码。前者通常是对数据内容进行一些压缩编码操作,后者通常是对传传输采用分块策略之类的。

Content-length : content-length头的作用是指定待传输的内容的字节长度。比如上面举的例子中,我们要上传一本红楼梦小说,则可以指定其长度大小,如:content-length:731017。细心的读者可能会有疑惑,它和transfer-encoding又有什么关系呢?如果想知道它们的关系,只要反过来问下自己,为什么transfer-encoding会有identity和chunked两种,各在什么上下文情景中要用到。比如chunked方式,把数据分块传输在很多地方就非常有用,如服务端在处理一个复杂的问题时,其返回结果是阶段性的产出,不能一次性知道最终的返回的总长度(content-lenght值),所以这时候返回头中就不能有content-lenght头信息,有也要忽略处理。所以你可以这样理解,transfer-encoding在不能一次性确定消息实体(entity)内容时自定义一些传输协议,如果能确定的话,则可以在消息头中加入content-length头信息指示其长度,可以把transfer-encoding和content-length看成互斥性的两种头。

 transfer-encoding详解

chunked格式(rfc2616 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)

还是以上传“红楼梦”这本书举例:

 24E5是指第一个块数据长度为24E5(16进制格式字符串表示),CRLF为换行控制符。紧接着是第一个块数据内容,其长度就是上面定义的24E5,以CRLF标志结束。3485是指第二块数据长度为3485,CRLF结束,然后后面是第二块的数据内容......,以这样的格式直到所有的块数据结束。最后以“0”CRLF结束,表示数据传输完成(这里对比rfc规范内容,省略了chunk-extensiontrailer的东西,因为这并不重要)。

 

public class Main {

/** * @param args
*/ public static final int CR = 13; // <US-ASCII CR, carriage return (13)> public static final int LF = 10; // <US-ASCII LF, linefeed (10)> public static void main(String[] args) throws Exception{
Socket socket
= new Socket("localhost",8080);
OutputStream out
= socket.getOutputStream();
InputStream in
= socket.getInputStream();

//send requestline out.write("POST /web/Hello HTTP/1.1".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);

//send request header out.write("Host:localhost:8080".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);
out.write(
"Accept-Encoding:gzip,deflate".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);
out.write(
"Transfer-Encoding:chunked".getBytes());// 指定transfer-encodeing为chunked方式 out.write(CR & 0xFF);
out.write(LF
& 0xFF);
out.write(
"Content-Type:application/x-www-form-urlencoded;charset=utf-8".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);

// CRLF between headers and entity out.write(CR & 0xFF);
out.write(LF
& 0xFF);

/* * send chunked data */ //send the first chunked data:hello,world
//the first chunked data's size : 11 out.write("B".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);
//the first chunked data's content : hello,world out.write("hello,world".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);
//send the second chunked data:tony
//the first chunked data's size : 4 out.write("4".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);
//the first chunked data's content : hello,world out.write("tony".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);
//send the chunked data end flag out.write("0".getBytes());
out.write(CR
& 0xFF);
out.write(LF
& 0xFF);
//send CRLF out.write(CR & 0xFF);
out.write(LF
& 0xFF);

out.flush();

//
byte[] buffer = new byte[512];
ByteArrayOutputStream bufferStream
= new ByteArrayOutputStream();
int len = -1;
while((len = in.read(buffer)) != -1){
bufferStream.write(buffer,
0,len);
}

System.out.println(
new String(bufferStream.toByteArray()));

socket.close();

}

 上面这段代码发了两块数据,第一块是“hello,world”这11个字节长度的字符,第二块发送了“tony”四个字长的数据块。在服务端将收到“hello,worldtony”这个字符串.

HttpCore对transfer-encoding的实现

所以不管是对输入流(InputStream),还是输出流(OutputStream),httpcore都有三种实现:contentlength,identity,chunked。这是完全按照http规范实现的。这里再重复总结下这三种这间的关系。
当指定了"content-length"头信息时,说明已经确定消息体(entity)的长度大小,其值必需为非负整数。反之,如果有“transfer-encoding”头信息时,其值为“chunked”或者“identity”,说明不确定消息体的大小,这时应该不存在“content-length”头。

 

posted @ 2012-10-19 17:20  海鸟  阅读(10156)  评论(1编辑  收藏  举报