Statistics
| Branch: | Revision:

ffmpeg / libavformat / http.c @ 701ea516

History | View | Annotate | Download (14.3 KB)

1
/*
2
 * HTTP protocol for ffmpeg client
3
 * Copyright (c) 2000, 2001 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "libavutil/avstring.h"
23
#include "avformat.h"
24
#include <unistd.h>
25
#include <strings.h>
26
#include "internal.h"
27
#include "network.h"
28
#include "http.h"
29
#include "os_support.h"
30
#include "httpauth.h"
31

    
32
/* XXX: POST protocol is not completely implemented because ffmpeg uses
33
   only a subset of it. */
34

    
35
/* used for protocol handling */
36
#define BUFFER_SIZE 1024
37
#define URL_SIZE    4096
38
#define MAX_REDIRECTS 8
39

    
40
typedef struct {
41
    URLContext *hd;
42
    unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
43
    int line_count;
44
    int http_code;
45
    int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
46
    int64_t off, filesize;
47
    char location[URL_SIZE];
48
    HTTPAuthState auth_state;
49
    int init;
50
    unsigned char headers[BUFFER_SIZE];
51
} HTTPContext;
52

    
53
static int http_connect(URLContext *h, const char *path, const char *hoststr,
54
                        const char *auth, int *new_location);
55
static int http_write(URLContext *h, const uint8_t *buf, int size);
56

    
57
void ff_http_set_headers(URLContext *h, const char *headers)
58
{
59
    HTTPContext *s = h->priv_data;
60
    int len = strlen(headers);
61

    
62
    if (len && strcmp("\r\n", headers + len - 2))
63
        av_log(NULL, AV_LOG_ERROR, "No trailing CRLF found in HTTP header.\n");
64

    
65
    av_strlcpy(s->headers, headers, sizeof(s->headers));
66
}
67

    
68
/* return non zero if error */
69
static int http_open_cnx(URLContext *h)
70
{
71
    const char *path, *proxy_path;
72
    char hostname[1024], hoststr[1024];
73
    char auth[1024];
74
    char path1[1024];
75
    char buf[1024];
76
    int port, use_proxy, err, location_changed = 0, redirects = 0;
77
    HTTPAuthType cur_auth_type;
78
    HTTPContext *s = h->priv_data;
79
    URLContext *hd = NULL;
80

    
81
    s->init = 1;
82
    proxy_path = getenv("http_proxy");
83
    use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
84
        av_strstart(proxy_path, "http://", NULL);
85

    
86
    /* fill the dest addr */
87
 redo:
88
    /* needed in any case to build the host string */
89
    ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
90
                 path1, sizeof(path1), s->location);
91
    ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
92

    
93
    if (use_proxy) {
94
        ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
95
                     NULL, 0, proxy_path);
96
        path = s->location;
97
    } else {
98
        if (path1[0] == '\0')
99
            path = "/";
100
        else
101
            path = path1;
102
    }
103
    if (port < 0)
104
        port = 80;
105

    
106
    ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
107
    err = url_open(&hd, buf, URL_RDWR);
108
    if (err < 0)
109
        goto fail;
110

    
111
    s->hd = hd;
112
    cur_auth_type = s->auth_state.auth_type;
113
    if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
114
        goto fail;
115
    if (s->http_code == 401) {
116
        if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
117
            url_close(hd);
118
            goto redo;
119
        } else
120
            goto fail;
121
    }
122
    if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
123
        /* url moved, get next */
124
        url_close(hd);
125
        if (redirects++ >= MAX_REDIRECTS)
126
            return AVERROR(EIO);
127
        location_changed = 0;
128
        goto redo;
129
    }
130
    return 0;
131
 fail:
132
    if (hd)
133
        url_close(hd);
134
    s->hd = NULL;
135
    return AVERROR(EIO);
136
}
137

    
138
static int http_open(URLContext *h, const char *uri, int flags)
139
{
140
    HTTPContext *s;
141

    
142
    h->is_streamed = 1;
143

    
144
    s = av_malloc(sizeof(HTTPContext));
145
    if (!s) {
146
        return AVERROR(ENOMEM);
147
    }
148
    h->priv_data = s;
149
    s->filesize = -1;
150
    s->chunksize = -1;
151
    s->off = 0;
152
    s->init = 0;
153
    s->hd = NULL;
154
    *s->headers = '\0';
155
    memset(&s->auth_state, 0, sizeof(s->auth_state));
156
    av_strlcpy(s->location, uri, URL_SIZE);
157

    
158
    return 0;
159
}
160
static int http_getc(HTTPContext *s)
161
{
162
    int len;
163
    if (s->buf_ptr >= s->buf_end) {
164
        len = url_read(s->hd, s->buffer, BUFFER_SIZE);
165
        if (len < 0) {
166
            return AVERROR(EIO);
167
        } else if (len == 0) {
168
            return -1;
169
        } else {
170
            s->buf_ptr = s->buffer;
171
            s->buf_end = s->buffer + len;
172
        }
173
    }
174
    return *s->buf_ptr++;
175
}
176

    
177
static int http_get_line(HTTPContext *s, char *line, int line_size)
178
{
179
    int ch;
180
    char *q;
181

    
182
    q = line;
183
    for(;;) {
184
        ch = http_getc(s);
185
        if (ch < 0)
186
            return AVERROR(EIO);
187
        if (ch == '\n') {
188
            /* process line */
189
            if (q > line && q[-1] == '\r')
190
                q--;
191
            *q = '\0';
192

    
193
            return 0;
194
        } else {
195
            if ((q - line) < line_size - 1)
196
                *q++ = ch;
197
        }
198
    }
199
}
200

    
201
static int process_line(URLContext *h, char *line, int line_count,
202
                        int *new_location)
203
{
204
    HTTPContext *s = h->priv_data;
205
    char *tag, *p;
206

    
207
    /* end of header */
208
    if (line[0] == '\0')
209
        return 0;
210

    
211
    p = line;
212
    if (line_count == 0) {
213
        while (!isspace(*p) && *p != '\0')
214
            p++;
215
        while (isspace(*p))
216
            p++;
217
        s->http_code = strtol(p, NULL, 10);
218

    
219
        dprintf(NULL, "http_code=%d\n", s->http_code);
220

    
221
        /* error codes are 4xx and 5xx, but regard 401 as a success, so we
222
         * don't abort until all headers have been parsed. */
223
        if (s->http_code >= 400 && s->http_code < 600 && s->http_code != 401)
224
            return -1;
225
    } else {
226
        while (*p != '\0' && *p != ':')
227
            p++;
228
        if (*p != ':')
229
            return 1;
230

    
231
        *p = '\0';
232
        tag = line;
233
        p++;
234
        while (isspace(*p))
235
            p++;
236
        if (!strcmp(tag, "Location")) {
237
            strcpy(s->location, p);
238
            *new_location = 1;
239
        } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
240
            s->filesize = atoll(p);
241
        } else if (!strcmp (tag, "Content-Range")) {
242
            /* "bytes $from-$to/$document_size" */
243
            const char *slash;
244
            if (!strncmp (p, "bytes ", 6)) {
245
                p += 6;
246
                s->off = atoll(p);
247
                if ((slash = strchr(p, '/')) && strlen(slash) > 0)
248
                    s->filesize = atoll(slash+1);
249
            }
250
            h->is_streamed = 0; /* we _can_ in fact seek */
251
        } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
252
            s->filesize = -1;
253
            s->chunksize = 0;
254
        } else if (!strcmp (tag, "WWW-Authenticate")) {
255
            ff_http_auth_handle_header(&s->auth_state, tag, p);
256
        } else if (!strcmp (tag, "Authentication-Info")) {
257
            ff_http_auth_handle_header(&s->auth_state, tag, p);
258
        }
259
    }
260
    return 1;
261
}
262

    
263
static inline int has_header(const char *str, const char *header)
264
{
265
    /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
266
    return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
267
}
268

    
269
static int http_connect(URLContext *h, const char *path, const char *hoststr,
270
                        const char *auth, int *new_location)
271
{
272
    HTTPContext *s = h->priv_data;
273
    int post, err;
274
    char line[1024];
275
    char headers[1024] = "";
276
    char *authstr = NULL;
277
    int64_t off = s->off;
278
    int len = 0;
279

    
280

    
281
    /* send http header */
282
    post = h->flags & URL_WRONLY;
283
    authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
284
                                        post ? "POST" : "GET");
285

    
286
    /* set default headers if needed */
287
    if (!has_header(s->headers, "\r\nUser-Agent: "))
288
       len += av_strlcatf(headers + len, sizeof(headers) - len,
289
                          "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
290
    if (!has_header(s->headers, "\r\nAccept: "))
291
        len += av_strlcpy(headers + len, "Accept: */*\r\n",
292
                          sizeof(headers) - len);
293
    if (!has_header(s->headers, "\r\nRange: "))
294
        len += av_strlcatf(headers + len, sizeof(headers) - len,
295
                           "Range: bytes=%"PRId64"-\r\n", s->off);
296
    if (!has_header(s->headers, "\r\nConnection: "))
297
        len += av_strlcpy(headers + len, "Connection: close\r\n",
298
                          sizeof(headers)-len);
299
    if (!has_header(s->headers, "\r\nHost: "))
300
        len += av_strlcatf(headers + len, sizeof(headers) - len,
301
                           "Host: %s\r\n", hoststr);
302

    
303
    /* now add in custom headers */
304
    av_strlcpy(headers+len, s->headers, sizeof(headers)-len);
305

    
306
    snprintf(s->buffer, sizeof(s->buffer),
307
             "%s %s HTTP/1.1\r\n"
308
             "%s"
309
             "%s"
310
             "%s"
311
             "\r\n",
312
             post ? "POST" : "GET",
313
             path,
314
             post ? "Transfer-Encoding: chunked\r\n" : "",
315
             headers,
316
             authstr ? authstr : "");
317

    
318
    av_freep(&authstr);
319
    if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
320
        return AVERROR(EIO);
321

    
322
    /* init input buffer */
323
    s->buf_ptr = s->buffer;
324
    s->buf_end = s->buffer;
325
    s->line_count = 0;
326
    s->off = 0;
327
    s->filesize = -1;
328
    if (post) {
329
        /* always use chunked encoding for upload data */
330
        s->chunksize = 0;
331
        /* Pretend that it did work. We didn't read any header yet, since
332
         * we've still to send the POST data, but the code calling this
333
         * function will check http_code after we return. */
334
        s->http_code = 200;
335
        return 0;
336
    }
337

    
338
    /* wait for header */
339
    for(;;) {
340
        if (http_get_line(s, line, sizeof(line)) < 0)
341
            return AVERROR(EIO);
342

    
343
        dprintf(NULL, "header='%s'\n", line);
344

    
345
        err = process_line(h, line, s->line_count, new_location);
346
        if (err < 0)
347
            return err;
348
        if (err == 0)
349
            break;
350
        s->line_count++;
351
    }
352

    
353
    return (off == s->off) ? 0 : -1;
354
}
355

    
356

    
357
static int http_read(URLContext *h, uint8_t *buf, int size)
358
{
359
    HTTPContext *s = h->priv_data;
360
    int len;
361

    
362
    if (!s->init) {
363
        int ret = http_open_cnx(h);
364
        if (ret != 0)
365
            return ret;
366
    }
367

    
368
    /* A size of zero can be used to force
369
     * initializaton of the connection. */
370
    if (!size)
371
        return 0;
372

    
373
    if (s->chunksize >= 0) {
374
        if (!s->chunksize) {
375
            char line[32];
376

    
377
            for(;;) {
378
                do {
379
                    if (http_get_line(s, line, sizeof(line)) < 0)
380
                        return AVERROR(EIO);
381
                } while (!*line);    /* skip CR LF from last chunk */
382

    
383
                s->chunksize = strtoll(line, NULL, 16);
384

    
385
                dprintf(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
386

    
387
                if (!s->chunksize)
388
                    return 0;
389
                break;
390
            }
391
        }
392
        size = FFMIN(size, s->chunksize);
393
    }
394
    /* read bytes from input buffer first */
395
    len = s->buf_end - s->buf_ptr;
396
    if (len > 0) {
397
        if (len > size)
398
            len = size;
399
        memcpy(buf, s->buf_ptr, len);
400
        s->buf_ptr += len;
401
    } else {
402
        len = url_read(s->hd, buf, size);
403
    }
404
    if (len > 0) {
405
        s->off += len;
406
        if (s->chunksize > 0)
407
            s->chunksize -= len;
408
    }
409
    return len;
410
}
411

    
412
/* used only when posting data */
413
static int http_write(URLContext *h, const uint8_t *buf, int size)
414
{
415
    char temp[11];  /* 32-bit hex + CRLF + nul */
416
    int ret;
417
    char crlf[] = "\r\n";
418
    HTTPContext *s = h->priv_data;
419

    
420
    if (!s->init) {
421
        int ret = http_open_cnx(h);
422
        if (ret != 0)
423
            return ret;
424
    }
425

    
426
    if (s->chunksize == -1) {
427
        /* headers are sent without any special encoding */
428
        return url_write(s->hd, buf, size);
429
    }
430

    
431
    /* silently ignore zero-size data since chunk encoding that would
432
     * signal EOF */
433
    if (size > 0) {
434
        /* upload data using chunked encoding */
435
        snprintf(temp, sizeof(temp), "%x\r\n", size);
436

    
437
        if ((ret = url_write(s->hd, temp, strlen(temp))) < 0 ||
438
            (ret = url_write(s->hd, buf, size)) < 0 ||
439
            (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
440
            return ret;
441
    }
442
    return size;
443
}
444

    
445
static int http_close(URLContext *h)
446
{
447
    int ret = 0;
448
    char footer[] = "0\r\n\r\n";
449
    HTTPContext *s = h->priv_data;
450

    
451
    /* signal end of chunked encoding if used */
452
    if ((h->flags & URL_WRONLY) && s->chunksize != -1) {
453
        ret = url_write(s->hd, footer, sizeof(footer) - 1);
454
        ret = ret > 0 ? 0 : ret;
455
    }
456

    
457
    if (s->hd)
458
        url_close(s->hd);
459
    av_free(s);
460
    return ret;
461
}
462

    
463
static int64_t http_seek(URLContext *h, int64_t off, int whence)
464
{
465
    HTTPContext *s = h->priv_data;
466
    URLContext *old_hd = s->hd;
467
    int64_t old_off = s->off;
468
    uint8_t old_buf[BUFFER_SIZE];
469
    int old_buf_size;
470

    
471
    if (whence == AVSEEK_SIZE)
472
        return s->filesize;
473
    else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
474
        return -1;
475

    
476
    /* we save the old context in case the seek fails */
477
    old_buf_size = s->buf_end - s->buf_ptr;
478
    memcpy(old_buf, s->buf_ptr, old_buf_size);
479
    s->hd = NULL;
480
    if (whence == SEEK_CUR)
481
        off += s->off;
482
    else if (whence == SEEK_END)
483
        off += s->filesize;
484
    s->off = off;
485

    
486
    /* if it fails, continue on old connection */
487
    if (http_open_cnx(h) < 0) {
488
        memcpy(s->buffer, old_buf, old_buf_size);
489
        s->buf_ptr = s->buffer;
490
        s->buf_end = s->buffer + old_buf_size;
491
        s->hd = old_hd;
492
        s->off = old_off;
493
        return -1;
494
    }
495
    url_close(old_hd);
496
    return off;
497
}
498

    
499
static int
500
http_get_file_handle(URLContext *h)
501
{
502
    HTTPContext *s = h->priv_data;
503
    return url_get_file_handle(s->hd);
504
}
505

    
506
URLProtocol http_protocol = {
507
    "http",
508
    http_open,
509
    http_read,
510
    http_write,
511
    http_seek,
512
    http_close,
513
    .url_get_file_handle = http_get_file_handle,
514
};