哪位大神能给在workerman中解析http response返回数据的 协议解析区分header和body的思路啊

bigman

主要想实现异步http client的 get post

2717 2 0
2个回答

抽不完的寂寞

head和body之间有四个字符 \r\n\r\n
 

  • bigman 2019-07-08

    我指的是完整的 resoponse 数据 解析怎么区分接受完一个完整的包

cxiaobo520

你可以看workman/Protocols/Http/Response.php该文件
定义了header变量:

    /**
     * Header data.
     *
     * @var array
     */
    protected $_header = null;

定义了body变量:

    /**
     * Http body.
     *
     * @var string
     */
    protected $_body = null;

然后通过header()和withBody()对数据进行赋值,在通过一下方法进行拼接:

   /**
     * __toString.
     *
     * @return string
     */
    public function __toString()
    {
        if (isset($this->file)) {
            return $this->createHeadForFile($this->file);
        }

        $reason = $this->_reason ? $this->_reason : static::$_phrases[$this->_status];
        $body_len = \strlen($this->_body);
        if (empty($this->_header)) {
            return "HTTP/{$this->_version} {$this->_status} $reason\r\nServer: workerman\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length: $body_len\r\nConnection: keep-alive\r\n\r\n{$this->_body}";
        }

        $head = "HTTP/{$this->_version} {$this->_status} $reason\r\n";
        $headers = $this->_header;
        if (!isset($headers['Server'])) {
            $head .= "Server: workerman\r\n";
        }
        foreach ($headers as $name => $value) {
            if (\is_array($value)) {
                foreach ($value as $item) {
                    $head .= "$name: $item\r\n";
                }
                continue;
            }
            $head .= "$name: $value\r\n";
        }
....

这样就区分header和body啦

  • 暂无评论
年代过于久远,无法发表回答
×
🔝