File: access.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 (313 lines) | stat: -rw-r--r-- 8,738 bytes parent folder | download | duplicates (9)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*****************************************************************************
 * access.c: HTTP/TLS VLC access plug-in
 *****************************************************************************
 * 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>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#include <vlc_common.h>
#include <vlc_access.h>
#include <vlc_keystore.h>
#include <vlc_plugin.h>
#include <vlc_url.h>

#include "connmgr.h"
#include "resource.h"
#include "file.h"
#include "live.h"

struct access_sys_t
{
    struct vlc_http_mgr *manager;
    struct vlc_http_resource *resource;
};

static block_t *FileRead(stream_t *access, bool *restrict eof)
{
    access_sys_t *sys = access->p_sys;

    block_t *b = vlc_http_file_read(sys->resource);
    if (b == NULL)
        *eof = true;
    return b;
}

static int FileSeek(stream_t *access, uint64_t pos)
{
    access_sys_t *sys = access->p_sys;

    if (vlc_http_file_seek(sys->resource, pos))
        return VLC_EGENERIC;
    return VLC_SUCCESS;
}

static int FileControl(stream_t *access, int query, va_list args)
{
    access_sys_t *sys = access->p_sys;

    switch (query)
    {
        case STREAM_CAN_SEEK:
            *va_arg(args, bool *) = vlc_http_file_can_seek(sys->resource);
            break;

        case STREAM_CAN_FASTSEEK:
            *va_arg(args, bool *) = false;
            break;

        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
            *va_arg(args, bool *) = true;
            break;

        case STREAM_GET_SIZE:
        {
            uintmax_t val = vlc_http_file_get_size(sys->resource);
            if (val >= UINT64_MAX)
                return VLC_EGENERIC;

            *va_arg(args, uint64_t *) = val;
            break;
        }

        case STREAM_GET_PTS_DELAY:
            *va_arg(args, int64_t *) = INT64_C(1000) *
                var_InheritInteger(access, "network-caching");
            break;

        case STREAM_GET_CONTENT_TYPE:
            *va_arg(args, char **) = vlc_http_file_get_type(sys->resource);
            break;

        case STREAM_SET_PAUSE_STATE:
            break;

        default:
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

static block_t *LiveRead(stream_t *access, bool *restrict eof)
{
    access_sys_t *sys = access->p_sys;

    block_t *b = vlc_http_live_read(sys->resource);
    if (b == NULL) /* TODO: loop instead of EOF, see vlc_http_live_read() */
        *eof = true;
    return b;
}

static int NoSeek(stream_t *access, uint64_t pos)
{
    (void) access;
    (void) pos;
    return VLC_EGENERIC;
}

static int LiveControl(stream_t *access, int query, va_list args)
{
    access_sys_t *sys = access->p_sys;

    switch (query)
    {
        case STREAM_CAN_SEEK:
        case STREAM_CAN_FASTSEEK:
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
            *va_arg(args, bool *) = false;
            break;

        case STREAM_GET_PTS_DELAY:
            *va_arg(args, int64_t *) = INT64_C(1000) *
                var_InheritInteger(access, "network-caching");
            break;

        case STREAM_GET_CONTENT_TYPE:
            *va_arg(args, char **) = vlc_http_live_get_type(sys->resource);
            break;

        default:
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

static int Open(vlc_object_t *obj)
{
    stream_t *access = (stream_t *)obj;
    access_sys_t *sys = malloc(sizeof (*sys));
    int ret = VLC_ENOMEM;

    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

    sys->manager = NULL;
    sys->resource = NULL;

    void *jar = NULL;
    if (var_InheritBool(obj, "http-forward-cookies"))
        jar = var_InheritAddress(obj, "http-cookies");

    struct vlc_credential crd;
    struct vlc_url_t crd_url;
    char *psz_realm = NULL;

    vlc_UrlParse(&crd_url, access->psz_url);
    vlc_credential_init(&crd, &crd_url);

    sys->manager = vlc_http_mgr_create(obj, jar);
    if (sys->manager == NULL)
        goto error;

    char *ua = var_InheritString(obj, "http-user-agent");
    char *referer = var_InheritString(obj, "http-referrer");
    bool live = var_InheritBool(obj, "http-continuous");

    sys->resource = (live ? vlc_http_live_create : vlc_http_file_create)(
        sys->manager, access->psz_url, ua, referer);
    free(referer);
    free(ua);

    if (sys->resource == NULL)
        goto error;

    if (vlc_credential_get(&crd, obj, NULL, NULL, NULL, NULL))
        vlc_http_res_set_login(sys->resource,
                               crd.psz_username, crd.psz_password);

    ret = VLC_EGENERIC;

    int status = vlc_http_res_get_status(sys->resource);

    while (status == 401) /* authentication */
    {
        crd.psz_authtype = "Basic";
        free(psz_realm);
        psz_realm = vlc_http_res_get_basic_realm(sys->resource);

        if (psz_realm == NULL)
            break;
        crd.psz_realm = psz_realm;
        if (!vlc_credential_get(&crd, obj, NULL, NULL, _("HTTP authentication"),
                                _("Please enter a valid login name and "
                                  "a password for realm %s."), crd.psz_realm))
            break;

        vlc_http_res_set_login(sys->resource,
                               crd.psz_username, crd.psz_password);
        status = vlc_http_res_get_status(sys->resource);
    }

    if (status < 0)
    {
        msg_Err(access, "HTTP connection failure");
        goto error;
    }

    char *redir = vlc_http_res_get_redirect(sys->resource);
    if (redir != NULL)
    {
        access->psz_url = redir;
        ret = VLC_ACCESS_REDIRECT;
        goto error;
    }

    if (status >= 300)
    {
        msg_Err(access, "HTTP %d error", status);
        goto error;
    }

    vlc_credential_store(&crd, obj);
    free(psz_realm);
    vlc_credential_clean(&crd);
    vlc_UrlClean(&crd_url);

    access->pf_read = NULL;
    if (live)
    {
        access->pf_block = LiveRead;
        access->pf_seek = NoSeek;
        access->pf_control = LiveControl;
    }
    else
    {
        access->pf_block = FileRead;
        access->pf_seek = FileSeek;
        access->pf_control = FileControl;
    }
    access->p_sys = sys;
    return VLC_SUCCESS;

error:
    if (sys->resource != NULL)
        vlc_http_res_destroy(sys->resource);
    if (sys->manager != NULL)
        vlc_http_mgr_destroy(sys->manager);
    free(psz_realm);
    vlc_credential_clean(&crd);
    vlc_UrlClean(&crd_url);
    free(sys);
    return ret;
}

static void Close(vlc_object_t *obj)
{
    stream_t *access = (stream_t *)obj;
    access_sys_t *sys = access->p_sys;

    vlc_http_res_destroy(sys->resource);
    vlc_http_mgr_destroy(sys->manager);
    free(sys);
}

vlc_module_begin()
    set_description(N_("HTTPS input"))
    set_shortname(N_("HTTPS"))
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_ACCESS)
    set_capability("access", 2)
    add_shortcut("https", "http")
    set_callbacks(Open, Close)

    add_bool("http-continuous", false, N_("Continuous stream"),
             N_("Keep reading a resource that keeps being updated."), true)
        change_volatile()
    add_bool("http-forward-cookies", true, N_("Cookies forwarding"),
             N_("Forward cookies across HTTP redirections."), true)
    add_string("http-referrer", NULL, N_("Referrer"),
               N_("Provide the referral URL, i.e. HTTP \"Referer\" (sic)."),
               true)
        change_safe()
        change_volatile()
    add_string("http-user-agent", NULL, N_("User agent"),
               N_("Override the name and version of the application as "
                  "provided to the HTTP server, i.e. the HTTP \"User-Agent\". "
                  "Name and version must be separated by a forward slash, "
                  "e.g. \"FooBar/1.2.3\"."), true)
        change_safe()
        change_private()
vlc_module_end()