File: file.c

package info (click to toggle)
vlc 3.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208,020 kB
  • sloc: ansic: 443,448; cpp: 111,223; objc: 36,399; sh: 6,737; makefile: 6,627; javascript: 4,902; xml: 1,611; asm: 1,355; yacc: 644; python: 321; lex: 88; perl: 77; sed: 16
file content (255 lines) | stat: -rw-r--r-- 7,764 bytes parent folder | download | duplicates (4)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
128
129
130
131
132
133
134
135
136
137
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*****************************************************************************
 * file.c: HTTP read-only file
 *****************************************************************************
 * Copyright (C) 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>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <vlc_common.h>
#include <vlc_block.h>
#include <vlc_strings.h>
#include "message.h"
#include "resource.h"
#include "file.h"

#pragma GCC visibility push(default)

struct vlc_http_file
{
    struct vlc_http_resource resource;
    uintmax_t offset;
};

static int vlc_http_file_req(const struct vlc_http_resource *res,
                             struct vlc_http_msg *req, void *opaque)
{
    struct vlc_http_file *file = (struct vlc_http_file *)res;
    const uintmax_t *offset = opaque;

    if (file->resource.response != NULL)
    {
        const char *str = vlc_http_msg_get_header(file->resource.response,
                                                  "ETag");
        if (str != NULL)
        {
            if (!memcmp(str, "W/", 2))
                str += 2; /* skip weak mark */
            vlc_http_msg_add_header(req, "If-Match", "%s", str);
        }
        else
        {
            time_t mtime = vlc_http_msg_get_mtime(file->resource.response);
            if (mtime != -1)
                vlc_http_msg_add_time(req, "If-Unmodified-Since", &mtime);
        }
    }

    if (vlc_http_msg_add_header(req, "Range", "bytes=%" PRIuMAX "-", *offset)
     && *offset != 0)
        return -1;
    return 0;
}

static int vlc_http_file_resp(const struct vlc_http_resource *res,
                              const struct vlc_http_msg *resp, void *opaque)
{
    const uintmax_t *offset = opaque;

    if (vlc_http_msg_get_status(resp) == 206)
    {
        const char *str = vlc_http_msg_get_header(resp, "Content-Range");
        if (str == NULL)
            /* A multipart/byteranges response. This is not what we asked for
             * and we do not support it. */
            goto fail;

        uintmax_t start, end;
        if (sscanf(str, "bytes %" SCNuMAX "-%" SCNuMAX, &start, &end) != 2
         || start != *offset || start > end)
            /* A single range response is what we asked for, but not at that
             * start offset. */
            goto fail;
    }

    (void) res;
    return 0;

fail:
    errno = EIO;
    return -1;
}

static const struct vlc_http_resource_cbs vlc_http_file_callbacks =
{
    vlc_http_file_req,
    vlc_http_file_resp,
};

struct vlc_http_resource *vlc_http_file_create(struct vlc_http_mgr *mgr,
                                               const char *uri, const char *ua,
                                               const char *ref)
{
    struct vlc_http_file *file = malloc(sizeof (*file));
    if (unlikely(file == NULL))
        return NULL;

    if (vlc_http_res_init(&file->resource, &vlc_http_file_callbacks, mgr,
                          uri, ua, ref))
    {
        free(file);
        return NULL;
    }

    file->offset = 0;
    return &file->resource;
}

static uintmax_t vlc_http_msg_get_file_size(const struct vlc_http_msg *resp)
{
    int status = vlc_http_msg_get_status(resp);
    const char *range = vlc_http_msg_get_header(resp, "Content-Range");

    if (status == 206 /* Partial Content */)
    {   /* IETF RFC7233 §4.1 */
        assert(range != NULL); /* checked by vlc_http_file_resp() */

        uintmax_t end, total;

        switch (sscanf(range, "bytes %*u-%" SCNuMAX "/%" SCNuMAX, &end, &total))
        {
            case 1:
                if (unlikely(end == UINTMAX_MAX))
                    return -1; /* avoid wrapping to zero */
                return end + 1;
            case 2:
                return total;
        }
        vlc_assert_unreachable(); /* checked by vlc_http_file_resp() */
    }

    if (status == 416 /* Range Not Satisfiable */)
    {   /* IETF RFC7233 §4.4 */
        uintmax_t total;

        if (range == NULL)
            return -1; /* valid but helpless response */

        if (sscanf(range, "bytes */%" SCNuMAX, &total) == 1)
            return total; /* this occurs when seeking beyond EOF */
    }

    return -1;
}

static bool vlc_http_msg_can_seek(const struct vlc_http_msg *resp)
{
    int status = vlc_http_msg_get_status(resp);
    if (status == 206 || status == 416)
        return true; /* Partial Content */

    return vlc_http_msg_get_token(resp, "Accept-Ranges", "bytes") != NULL;
}

uintmax_t vlc_http_file_get_size(struct vlc_http_resource *res)
{
    int status = vlc_http_res_get_status(res);
    if (status < 0)
        return -1;

    uintmax_t ret = vlc_http_msg_get_file_size(res->response);
    if (ret != (uintmax_t)-1)
        return ret;

    if (status >= 300 || status == 201)
        return -1; /* Error or redirection, size is unknown/irrelevant. */

    /* Content-Range is meaningless here (see RFC7233 B), so check if the size
     * of the response entity body is known. */
    return vlc_http_msg_get_size(res->response);
}

bool vlc_http_file_can_seek(struct vlc_http_resource *res)
{   /* See IETF RFC7233 */
    int status = vlc_http_res_get_status(res);
    if (status < 0)
        return false;
    return vlc_http_msg_can_seek(res->response);
}

int vlc_http_file_seek(struct vlc_http_resource *res, uintmax_t offset)
{
    struct vlc_http_msg *resp = vlc_http_res_open(res, &offset);
    if (resp == NULL)
        return -1;

    struct vlc_http_file *file = (struct vlc_http_file *)res;

    int status = vlc_http_msg_get_status(resp);
    if (res->response != NULL)
    {   /* Accept the new and ditch the old one if:
         * - requested succeeded and range was accepted (206),
         * - requested failed due to out-of-range (416),
         * - request succeeded and seek offset is zero (2xx).
         */
        if (status != 206 && status != 416 && (offset != 0 || status >= 300))
        {
            vlc_http_msg_destroy(resp);
            return -1;
        }
        vlc_http_msg_destroy(res->response);
    }

    res->response = resp;
    file->offset = offset;
    return 0;
}

block_t *vlc_http_file_read(struct vlc_http_resource *res)
{
    struct vlc_http_file *file = (struct vlc_http_file *)res;
    block_t *block = vlc_http_res_read(res);

    if (block == vlc_http_error)
        block = NULL;

    /* Automatically resume on short response or error if possible */
    if (block == NULL && res->response != NULL
     && vlc_http_msg_can_seek(res->response)
     && file->offset < vlc_http_msg_get_file_size(res->response)
     && vlc_http_file_seek(res, file->offset) == 0)
    {
        block = vlc_http_res_read(res);

        if (block == vlc_http_error)
            block = NULL; /* Non-recovered error */
    }

    if (block != NULL)
        file->offset += block->i_buffer;
    return block;
}