ssize_t
ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
{
    u_char        *prev;
    ssize_t        n, size;
    ngx_err_t      err;
    ngx_array_t    vec;
    ngx_event_t   *rev;
    struct iovec  *iov, iovs[NGX_IOVS];
    prev = NULL;
    iov = NULL;
    size = 0;
    vec.elts = iovs;
    vec.nelts = 0;
    vec.size = sizeof(struct iovec);
    vec.nalloc = NGX_IOVS;
    vec.pool = c->pool;
    while (chain) {
        if (prev == chain->buf->last) {
            iov->iov_len += chain->buf->end - chain->buf->last;
        } else {
            iov = ngx_array_push(&vec);
            if (iov == NULL) {
                return NGX_ERROR;
            }
            iov->iov_base = (void *) chain->buf->last;
            iov->iov_len = chain->buf->end - chain->buf->last;
        }
        size += chain->buf->end - chain->buf->last;
        prev = chain->buf->end;
        chain = chain->next;
    }
    ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
                   "readv: %d:%d", vec.nelts, iov->iov_len);
    rev = c->read;
    do {
        n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts);
        if (n == 0) {
            rev->ready = 0;
            rev->eof = 1;
            return n;
        } else if (n > 0) {
            if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) {
                rev->ready = 0;
            }
            return n;
        }
        err = ngx_socket_errno;
        if (err == NGX_EAGAIN || err == NGX_EINTR) {
            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
                           "readv() not ready");
            n = NGX_AGAIN;
        } else {
            n = ngx_connection_error(c, err, "readv() failed");
            break;
        }
    } while (err == NGX_EINTR);
    rev->ready = 0;
    if (n == NGX_ERROR){
        c->read->error = 1;
    }
    return n;
}
['ssize_t\nngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)\n{\n    u_char        *prev;\n    ssize_t        n, size;\n    ngx_err_t      err;\n    ngx_array_t    vec;\n    ngx_event_t   *rev;\n    struct iovec  *iov, iovs[NGX_IOVS];\n    prev = NULL;\n    iov = NULL;\n    size = 0;\n    vec.elts = iovs;\n    vec.nelts = 0;\n    vec.size = sizeof(struct iovec);\n    vec.nalloc = NGX_IOVS;\n    vec.pool = c->pool;\n    while (chain) {\n        if (prev == chain->buf->last) {\n            iov->iov_len += chain->buf->end - chain->buf->last;\n        } else {\n            iov = ngx_array_push(&vec);\n            if (iov == NULL) {\n                return NGX_ERROR;\n            }\n            iov->iov_base = (void *) chain->buf->last;\n            iov->iov_len = chain->buf->end - chain->buf->last;\n        }\n        size += chain->buf->end - chain->buf->last;\n        prev = chain->buf->end;\n        chain = chain->next;\n    }\n    ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,\n                   "readv: %d:%d", vec.nelts, iov->iov_len);\n    rev = c->read;\n    do {\n        n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts);\n        if (n == 0) {\n            rev->ready = 0;\n            rev->eof = 1;\n            return n;\n        } else if (n > 0) {\n            if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) {\n                rev->ready = 0;\n            }\n            return n;\n        }\n        err = ngx_socket_errno;\n        if (err == NGX_EAGAIN || err == NGX_EINTR) {\n            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,\n                           "readv() not ready");\n            n = NGX_AGAIN;\n        } else {\n            n = ngx_connection_error(c, err, "readv() failed");\n            break;\n        }\n    } while (err == NGX_EINTR);\n    rev->ready = 0;\n    if (n == NGX_ERROR){\n        c->read->error = 1;\n    }\n    return n;\n}']