function _M.read_response()

in lib/resty/http.lua [678:777]


function _M.read_response(self, params)
    local sock = self.sock

    local status, version, reason, err

    
    
    if params.headers["Expect"] == "100-continue" then
        local _status, _version, _err = _handle_continue(sock, params.body)
        if not _status then
            return nil, _err
        elseif _status ~= 100 then
            status, version, err = _status, _version, _err 
        end
    end

    
    if not status then
        status, version, reason, err = _receive_status(sock)
        if not status then
            return nil, err
        end
    end


    local res_headers, err = _receive_headers(sock)
    if not res_headers then
        return nil, err
    end

    
    local ok, connection = pcall(str_lower, res_headers["Connection"])
    if ok then
        if (version == 1.1 and str_find(connection, "close", 1, true)) or
           (version == 1.0 and not str_find(connection, "keep-alive", 1, true)) then
            self.keepalive = false
        end
    else
        
        if version == 1.0 then
            self.keepalive = false
        end
    end

    local body_reader = _no_body_reader
    local trailer_reader, err
    local has_body = false

    
    if _should_receive_body(params.method, status) then
        has_body = true

        local te = res_headers["Transfer-Encoding"]

        
        
        if type(te) == "table" then
            te = tbl_concat(te, "")
        end

        local ok, encoding = pcall(str_lower, te)
        if not ok then
            encoding = ""
        end

        if version == 1.1 and str_find(encoding, "chunked", 1, true) ~= nil then
            body_reader, err = _chunked_body_reader(sock)

        else
            local ok, length = pcall(tonumber, res_headers["Content-Length"])
            if not ok then
                
                length = nil
            end

            body_reader, err = _body_reader(sock, length)

        end

    end

    if res_headers["Trailer"] then
        trailer_reader, err = _trailer_reader(sock)
    end

    if err then
        return nil, err
    else
        return {
            status = status,
            reason = reason,
            headers = res_headers,
            has_body = has_body,
            body_reader = body_reader,
            read_body = _read_body,
            trailer_reader = trailer_reader,
            read_trailers = _read_trailers,
        }
    end
end