chunked.c 4.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*****************************************************************************
 * chunked.c: HTTP 1.1 chunked encoding
 *****************************************************************************
 * Copyright © 2015 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <assert.h>
26
#include <errno.h>
27 28 29 30 31 32 33
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vlc_common.h>
#include <vlc_block.h>
34
#include <vlc_tls.h> /* TODO: remove this */
35 36

#include "message.h"
37
#include "conn.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51

struct vlc_chunked_stream
{
    struct vlc_http_stream stream;
    struct vlc_http_stream *parent;
    struct vlc_tls *tls;
    uintmax_t chunk_length;
    bool eof;
    bool error;
};

static void *vlc_chunked_fatal(struct vlc_chunked_stream *s)
{
    s->error = true;
52
    return vlc_http_error;
53 54 55 56 57 58 59 60 61 62 63 64
}

static struct vlc_http_msg *vlc_chunked_wait(struct vlc_http_stream *stream)
{
    /* Request trailers are not supported so far.
     * There cannot be headers during chunked encoding. */
    (void) stream;
    return NULL;
}

static block_t *vlc_chunked_read(struct vlc_http_stream *stream)
{
65 66
    struct vlc_chunked_stream *s =
        container_of(stream, struct vlc_chunked_stream, stream);
67 68
    block_t *block = NULL;

69
    if (s->eof)
70
        return NULL;
71 72
    if (s->error)
        return vlc_http_error;
73 74 75 76 77 78

    /* Read chunk size (hexadecimal length) */
    if (s->chunk_length == 0)
    {   /* NOTE: This accepts LF in addition to CRLF. No big deal. */
        char *line = vlc_tls_GetLine(s->tls);
        if (line == NULL)
79 80
        {
            errno = EPROTO;
81
            return vlc_chunked_fatal(s);
82
        }
83 84 85 86 87 88 89 90 91 92

        int end;

        if (sscanf(line, "%jx%n", &s->chunk_length, &end) < 1
         || (line[end] != '\0' && line[end] != ';' /* ignore extension(s) */))
            s->chunk_length = UINTMAX_MAX;

        free(line);

        if (s->chunk_length == UINTMAX_MAX)
93 94
        {
            errno = EPROTO;
95
            return vlc_chunked_fatal(s);
96
        }
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    }

    /* Read chunk data */
    if (s->chunk_length > 0)
    {
        size_t size = 1536; /* arbitrary */
        if (size > s->chunk_length)
            size = s->chunk_length;

        block = block_Alloc(size);
        if (unlikely(block == NULL))
            return NULL;

        ssize_t val = vlc_tls_Read(s->tls, block->p_buffer, size, false);
        if (val <= 0)
        {   /* Connection error (-) or unexpected end of connection (0) */
            block_Release(block);
            return vlc_chunked_fatal(s);
        }

        block->i_buffer = val;
        s->chunk_length -= val;
    }
    else
        s->eof = true;

    /* Read chunk end (CRLF) */
    if (s->chunk_length == 0)
    {
        char crlf[2];

128
        if (vlc_tls_Read(s->tls, crlf, 2, true) < 2 || memcmp(crlf, "\r\n", 2))
129 130 131 132 133 134 135
            vlc_chunked_fatal(s);
    }
    return block;
}

static void vlc_chunked_close(struct vlc_http_stream *stream, bool abort)
{
136 137
    struct vlc_chunked_stream *s =
        container_of(stream, struct vlc_chunked_stream, stream);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    if (!s->eof) /* Abort connection if stream is closed before end */
        vlc_chunked_fatal(s);

    vlc_http_stream_close(s->parent, abort || s->error);
    free(s);
}

static struct vlc_http_stream_cbs vlc_chunked_callbacks =
{
    vlc_chunked_wait,
    vlc_chunked_read,
    vlc_chunked_close,
};

struct vlc_http_stream *vlc_chunked_open(struct vlc_http_stream *parent,
                                         struct vlc_tls *tls)
{
    struct vlc_chunked_stream *s = malloc(sizeof (*s));
    if (unlikely(s == NULL))
        return NULL;

    s->stream.cbs = &vlc_chunked_callbacks;
    s->parent = parent;
    s->tls = tls;
    s->chunk_length = 0;
    s->eof = false;
    s->error = false;
    return &s->stream;
}