static apr_status_t wait_for_chunk()

in buckets/dechunk_buckets.c [68:159]


static apr_status_t wait_for_chunk(serf_bucket_t *bucket)
{
    dechunk_context_t *ctx = bucket->data;
    apr_status_t status;

    while (1) {
        switch (ctx->state) {
        case STATE_SIZE:

            /* fetch a line terminated by CRLF */
            status = serf_linebuf_fetch(&ctx->linebuf, ctx->stream,
                                        SERF_NEWLINE_CRLF);
            if (SERF_BUCKET_READ_ERROR(status))
                return status;

            /* if a line was read, then parse it. */
            if (ctx->linebuf.state == SERF_LINEBUF_READY) {
                char *end;

                /* Convert from HEX digits. The linebuffer ensures a '\0' */
                ctx->body_left = apr_strtoi64(ctx->linebuf.line, &end, 16);
                if (errno == ERANGE) {
                    return APR_FROM_OS_ERROR(ERANGE);
                }
                else if (ctx->linebuf.line == end) {
                    /* Invalid chunk length, bail out. */
                    return SERF_ERROR_BAD_HTTP_RESPONSE;
                }

                if (ctx->body_left == 0) {
                    /* Just read the last-chunk marker. We're DONE. */
                    ctx->state = STATE_DONE;
                    status = APR_EOF;
                }
                else {
                    /* Got a size, so we'll start reading the chunk now. */
                    ctx->state = STATE_CHUNK;
                }

                /* If we can read more, then go do so. */
                if (!status)
                    continue;
            }
            /* assert: status != 0 */

            return status;

        case STATE_CHUNK:
            return APR_SUCCESS;

        case STATE_TERM:
          {
            /* Delegate to the stream bucket to do the read. */
            const char *data;
            apr_size_t len;

            status = serf_bucket_read(ctx->stream,
                                      (apr_size_t)ctx->body_left /* 2 or 1 */,
                                      &data, &len);
            if (SERF_BUCKET_READ_ERROR(status))
                return status;

            /* Some data was read, so decrement the amount left and see
             * if we're done reading the chunk terminator.
             */
            ctx->body_left -= len;

            /* We need more data but there is no more available. */
            if (ctx->body_left && APR_STATUS_IS_EOF(status))
                return SERF_ERROR_TRUNCATED_HTTP_RESPONSE;

            if (!ctx->body_left) {
                ctx->state = STATE_SIZE;
            }

            /* Don't return the CR of CRLF to the caller! */
            if (status)
                return status;

            break;
          }
        case STATE_DONE:
            /* Just keep returning EOF */
            return APR_EOF;

        default:
            /* Not reachable */
            return APR_EGENERAL;
        }
    }
    /* NOTREACHED */
}