access.c 8.56 KB
Newer Older
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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
/*****************************************************************************
 * 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>
27 28
#include <string.h>
#include <stdlib.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
29 30 31

#include <vlc_common.h>
#include <vlc_access.h>
32
#include <vlc_keystore.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
33
#include <vlc_plugin.h>
34
#include <vlc_url.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
35 36

#include "connmgr.h"
37
#include "resource.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
38
#include "file.h"
39
#include "live.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
40

41
typedef struct
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
42 43
{
    struct vlc_http_mgr *manager;
44
    struct vlc_http_resource *resource;
45
} access_sys_t;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
46

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
47
static block_t *FileRead(stream_t *access, bool *restrict eof)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
48 49 50
{
    access_sys_t *sys = access->p_sys;

51
    block_t *b = vlc_http_file_read(sys->resource);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
52
    if (b == NULL)
53
        *eof = true;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
54 55 56
    return b;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
57
static int FileSeek(stream_t *access, uint64_t pos)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
58 59 60
{
    access_sys_t *sys = access->p_sys;

61
    if (vlc_http_file_seek(sys->resource, pos))
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62 63 64 65
        return VLC_EGENERIC;
    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
66
static int FileControl(stream_t *access, int query, va_list args)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
67 68 69 70 71
{
    access_sys_t *sys = access->p_sys;

    switch (query)
    {
72
        case STREAM_CAN_SEEK:
73
            *va_arg(args, bool *) = vlc_http_file_can_seek(sys->resource);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
74 75
            break;

76
        case STREAM_CAN_FASTSEEK:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
77 78 79
            *va_arg(args, bool *) = false;
            break;

80 81
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
82 83 84
            *va_arg(args, bool *) = true;
            break;

85
        case STREAM_GET_SIZE:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
86
        {
87
            uintmax_t val = vlc_http_file_get_size(sys->resource);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88 89 90 91 92 93 94
            if (val >= UINT64_MAX)
                return VLC_EGENERIC;

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

95
        case STREAM_GET_PTS_DELAY:
96 97
            *va_arg(args, int64_t *) = INT64_C(1000) *
                var_InheritInteger(access, "network-caching");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
98 99
            break;

100
        case STREAM_GET_CONTENT_TYPE:
101
            *va_arg(args, char **) = vlc_http_file_get_type(sys->resource);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102 103
            break;

104
        case STREAM_SET_PAUSE_STATE:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
105 106 107 108
            break;

        default:
            return VLC_EGENERIC;
109 110 111 112
    }
    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
113
static block_t *LiveRead(stream_t *access, bool *restrict eof)
114 115 116
{
    access_sys_t *sys = access->p_sys;

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
123
static int NoSeek(stream_t *access, uint64_t pos)
124 125 126 127 128
{
    (void) access;
    (void) pos;
    return VLC_EGENERIC;
}
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
129

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
130
static int LiveControl(stream_t *access, int query, va_list args)
131 132 133 134 135
{
    access_sys_t *sys = access->p_sys;

    switch (query)
    {
136 137 138 139
        case STREAM_CAN_SEEK:
        case STREAM_CAN_FASTSEEK:
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
140 141 142
            *va_arg(args, bool *) = false;
            break;

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

148
        case STREAM_GET_CONTENT_TYPE:
149
            *va_arg(args, char **) = vlc_http_live_get_type(sys->resource);
150 151 152 153
            break;

        default:
            return VLC_EGENERIC;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
154 155 156 157 158 159
    }
    return VLC_SUCCESS;
}

static int Open(vlc_object_t *obj)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
160
    stream_t *access = (stream_t *)obj;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
161 162 163 164 165 166 167
    access_sys_t *sys = malloc(sizeof (*sys));
    int ret = VLC_ENOMEM;

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

    sys->manager = NULL;
168
    sys->resource = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
169

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

174 175
    struct vlc_credential crd;
    struct vlc_url_t crd_url;
176
    char *psz_realm = NULL;
177 178 179 180

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

181
    sys->manager = vlc_http_mgr_create(obj, jar);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
182 183 184 185
    if (sys->manager == NULL)
        goto error;

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

189 190
    sys->resource = (live ? vlc_http_live_create : vlc_http_file_create)(
        sys->manager, access->psz_url, ua, referer);
191
    free(referer);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
192 193
    free(ua);

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

197 198 199
    if (vlc_credential_get(&crd, obj, NULL, NULL, NULL, NULL))
        vlc_http_res_set_login(sys->resource,
                               crd.psz_username, crd.psz_password);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
200 201 202

    ret = VLC_EGENERIC;

203
    int status = vlc_http_res_get_status(sys->resource);
204 205 206 207

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

211
        if (psz_realm == NULL)
212
            break;
213
        crd.psz_realm = psz_realm;
214 215 216 217 218 219 220 221 222 223
        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);
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
224 225 226 227 228
    if (status < 0)
    {
        msg_Err(access, "HTTP connection failure");
        goto error;
    }
229 230 231 232 233 234 235 236 237

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
238 239 240 241 242 243
    if (status >= 300)
    {
        msg_Err(access, "HTTP %d error", status);
        goto error;
    }

244
    vlc_credential_store(&crd, obj);
245
    free(psz_realm);
246 247 248
    vlc_credential_clean(&crd);
    vlc_UrlClean(&crd_url);

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
249
    access->pf_read = NULL;
250 251 252 253 254 255 256 257 258 259 260 261
    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;
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
262 263 264 265
    access->p_sys = sys;
    return VLC_SUCCESS;

error:
266 267
    if (sys->resource != NULL)
        vlc_http_res_destroy(sys->resource);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
268 269
    if (sys->manager != NULL)
        vlc_http_mgr_destroy(sys->manager);
270
    free(psz_realm);
271 272
    vlc_credential_clean(&crd);
    vlc_UrlClean(&crd_url);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
273 274 275 276 277 278
    free(sys);
    return ret;
}

static void Close(vlc_object_t *obj)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
279
    stream_t *access = (stream_t *)obj;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
280 281
    access_sys_t *sys = access->p_sys;

282
    vlc_http_res_destroy(sys->resource);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
283 284 285 286 287
    vlc_http_mgr_destroy(sys->manager);
    free(sys);
}

vlc_module_begin()
288
    set_description(N_("HTTPS input"))
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
289 290 291
    set_shortname(N_("HTTPS"))
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_ACCESS)
292
    set_capability("access", 2)
293
    add_shortcut("https", "http")
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
294
    set_callbacks(Open, Close)
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    add_bool("http-continuous", false, N_("Continuous stream"),
             N_("Keep reading a resource that keeps being updated."), true)
        change_safe()
        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()
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
314
vlc_module_end()