当前位置: 移动技术网 > IT编程>开发语言>Java > http协议进阶之Transfer-Encoding和HttpCore实现详解

http协议进阶之Transfer-Encoding和HttpCore实现详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

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-extension和trailer的东西,因为这并不重要)。

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”头。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网