Hessian Bug修复

一、背景说明

近日开发同学说Php调用Java一个接口报错,表现是如果参数比较大,如56K就报错,其它情况下不报错。让其提供相应参数,发现一个参数的长度是81360,对应十六进制是13DD0,通过抓包发现,实际上传到Java这里长度只有3DD0

在这里插入图片描述

即上图中第7行,这是什么情况呢,还是从Hessian协议说起,Hessian中字符长度只能是2字节,即单包最多只能传65535字节,如果长度超过65535,则需要封多次包发送,官方说明如下:

string ::= x52 b1 b0 string
::= S b1 b0
::= [x00-x1f]
::= [x30-x33] b0

A 16-bit unicode character string encoded in UTF-8. Strings are encoded in chunks. x53 (‘S’) represents the final chunk and x52 (‘R’) represents any non-final chunk. Each chunk has a 16-bit unsigned integer length value.

The length is the number of 16-bit characters, which may be different than the number of bytes.

String chunks may not split surrogate pairs.

分几种情况:

如果长度在0到1f(31)之间,直接附上字符串;

如果32-1023之间又是一种编码,具体编码方式后面会上代码;

1024-65535之间又是另一种编码;

如果长度大于65535,则前面的包都是R开头的包,最后一个包才是S包,表示结束。

举个例子,如果字符串长度为81360,则应该是这样封包:

R,FFFF,<前66535个字符>

S,3DD1,<最末15825个字符>

其中,是不存在的,只是方便阅读作为间隔符。

具体细节可以看官方文档:

http://hessian.caucho.com/doc/hessian-serialization.html#anchor32

二、代码分析&解决

我们看Php代码实现:

function writeString($value){
l e n = H e s s i a n U t i l s : : s t r i n g L e n g t h ( len = HessianUtils::stringLength( len=HessianUtils::stringLength(value);
if($len < 32){
return pack(‘C’, $len)
. t h i s − > w r i t e S t r i n g D a t a ( this->writeStringData( this>writeStringData(value);
} else
if($len < 1024){
b 0 = 0 x 30 + ( b0 = 0x30 + ( b0=0x30+(len >> 8);
$stream = pack(‘C’, $b0);
$stream .= pack(‘C’, $len);
return $stream . t h i s − > w r i t e S t r i n g D a t a ( this->writeStringData( this>writeStringData(value);
} else {
$total = $len;
$stream = ‘’;
$tag = ‘S’;
$stream .= $tag . pack(‘n’, $len);
$stream .= t h i s − > w r i t e S t r i n g D a t a ( this->writeStringData( this>writeStringData(value);
return $stream;
}
}

可以看到最后一个else判断里,并没有判断剩余长度是否大于65535,所以导致上面的问题,修改后的代码如下:

function writeString($value)
{
l e n = H e s s i a n U t i l s : : s t r i n g L e n g t h ( len = HessianUtils::stringLength( len=HessianUtils::stringLength(value);
if ($len < 32) {
return pack(‘C’, $len)
. t h i s − > w r i t e S t r i n g D a t a ( this->writeStringData( this>writeStringData(value);
} else if ($len < 1024) {
b 0 = 0 x 30 + ( b0 = 0x30 + ( b0=0x30+(len >> 8);
$stream = pack(‘C’, $b0);
$stream .= pack(‘C’, $len);
return $stream . t h i s − > w r i t e S t r i n g D a t a ( this->writeStringData( this>writeStringData(value);
} else if ($len < 65536) {
$total = $len;
$stream = ‘’;
$tag = ‘S’;
$stream .= $tag . pack(‘n’, $len);
$stream .= t h i s − > w r i t e S t r i n g D a t a ( this->writeStringData( this>writeStringData(value);
return $stream;
} else {
$left = $len;
$offset = 0;
//数据包分R包和S包
s t r e a m = ′ ′ ; w h i l e ( stream = ''; while ( stream=;while(left > 0) {
if ($left > 65535) {
$tag = ‘R’;
$stream .= $tag . pack(‘n’, 65535);
$stream .= t h i s − > w r i t e S t r i n g D a t a ( s u b s t r ( this->writeStringData(substr( this>writeStringData(substr(value, $offset, 65535));
$offset += 65535;
$left -= 65535;
} else {
$tag = ‘S’;
$stream .= $tag . pack(‘n’, $left);
$stream .= t h i s − > w r i t e S t r i n g D a t a ( s u b s t r ( this->writeStringData(substr( this>writeStringData(substr(value, $offset, $left));
$left = 0;
}
}

        return $stream;
    }
}

其实也可以参考Java代码的实现,

com.caucho.hessian.io.Hessian2Output用于Hessian封包,我们

看它的字符串封装:

while (length > 0x8000) {
int sublen = 0x8000;

    offset = _offset;

    if (SIZE <= offset + 16) {
      flushBuffer();
      offset = _offset;
    }

    // chunk can't end in high surrogate
    char tail = value.charAt(strOffset + sublen - 1);

    if (0xd800 <= tail && tail <= 0xdbff)
      sublen--;

    buffer[offset + 0] = (byte) BC_STRING_CHUNK;
    buffer[offset + 1] = (byte) (sublen >> 8);
    buffer[offset + 2] = (byte) (sublen);

    _offset = offset + 3;

    printString(value, strOffset, sublen);

    length -= sublen;
    strOffset += sublen;
  }

  offset = _offset;

  if (SIZE <= offset + 16) {
    flushBuffer();
    offset = _offset;
  }

  if (length <= STRING_DIRECT_MAX) {
    buffer[offset++] = (byte) (BC_STRING_DIRECT + length);
  }
  else if (length <= STRING_SHORT_MAX) {
    buffer[offset++] = (byte) (BC_STRING_SHORT + (length >> 8));
    buffer[offset++] = (byte) (length);
  }
  else {
    buffer[offset++] = (byte) ('S');
    buffer[offset++] = (byte) (length >> 8);
    buffer[offset++] = (byte) (length);
  }

  _offset = offset;

  printString(value, strOffset, length);

可以看到这里是以0x8000即32768作为单次包的最大字节数,如果大于32768就不断地封S包,剩下的才根据协议处理,当然其实现复杂些,引入了缓冲区,这里就不细讨论了。

360 Atlas使用

Dubbo2.7试用

FastDFS不同步怎么破

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值