Statistics
| Branch: | Revision:

ffmpeg / libavformat / http.c @ 682d49f4

History | View | Annotate | Download (9.44 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/base64.h"
23
#include "libavutil/avstring.h"
24
#include "avformat.h"
25
#include <unistd.h>
26
#include "network.h"
27
#include "os_support.h"
28

    
29
/* XXX: POST protocol is not completely implemented because ffmpeg uses
30
   only a subset of it. */
31

    
32
//#define DEBUG
33

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

    
39
typedef struct {
40
    URLContext *hd;
41
    unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
42
    int line_count;
43
    int http_code;
44
    int64_t off, filesize;
45
    char location[URL_SIZE];
46
} HTTPContext;
47

    
48
static int http_connect(URLContext *h, const char *path, const char *hoststr,
49
                        const char *auth, int *new_location);
50
static int http_write(URLContext *h, uint8_t *buf, int size);
51

    
52

    
53
/* return non zero if error */
54
static int http_open_cnx(URLContext *h)
55
{
56
    const char *path, *proxy_path;
57
    char hostname[1024], hoststr[1024];
58
    char auth[1024];
59
    char path1[1024];
60
    char buf[1024];
61
    int port, use_proxy, err, location_changed = 0, redirects = 0;
62
    HTTPContext *s = h->priv_data;
63
    URLContext *hd = NULL;
64

    
65
    proxy_path = getenv("http_proxy");
66
    use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
67
        av_strstart(proxy_path, "http://", NULL);
68

    
69
    /* fill the dest addr */
70
 redo:
71
    /* needed in any case to build the host string */
72
    url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
73
              path1, sizeof(path1), s->location);
74
    if (port > 0) {
75
        snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
76
    } else {
77
        av_strlcpy(hoststr, hostname, sizeof(hoststr));
78
    }
79

    
80
    if (use_proxy) {
81
        url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
82
                  NULL, 0, proxy_path);
83
        path = s->location;
84
    } else {
85
        if (path1[0] == '\0')
86
            path = "/";
87
        else
88
            path = path1;
89
    }
90
    if (port < 0)
91
        port = 80;
92

    
93
    snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
94
    err = url_open(&hd, buf, URL_RDWR);
95
    if (err < 0)
96
        goto fail;
97

    
98
    s->hd = hd;
99
    if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
100
        goto fail;
101
    if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
102
        /* url moved, get next */
103
        url_close(hd);
104
        if (redirects++ >= MAX_REDIRECTS)
105
            return AVERROR(EIO);
106
        location_changed = 0;
107
        goto redo;
108
    }
109
    return 0;
110
 fail:
111
    if (hd)
112
        url_close(hd);
113
    return AVERROR(EIO);
114
}
115

    
116
static int http_open(URLContext *h, const char *uri, int flags)
117
{
118
    HTTPContext *s;
119
    int ret;
120

    
121
    h->is_streamed = 1;
122

    
123
    s = av_malloc(sizeof(HTTPContext));
124
    if (!s) {
125
        return AVERROR(ENOMEM);
126
    }
127
    h->priv_data = s;
128
    s->filesize = -1;
129
    s->off = 0;
130
    av_strlcpy(s->location, uri, URL_SIZE);
131

    
132
    ret = http_open_cnx(h);
133
    if (ret != 0)
134
        av_free (s);
135
    return ret;
136
}
137
static int http_getc(HTTPContext *s)
138
{
139
    int len;
140
    if (s->buf_ptr >= s->buf_end) {
141
        len = url_read(s->hd, s->buffer, BUFFER_SIZE);
142
        if (len < 0) {
143
            return AVERROR(EIO);
144
        } else if (len == 0) {
145
            return -1;
146
        } else {
147
            s->buf_ptr = s->buffer;
148
            s->buf_end = s->buffer + len;
149
        }
150
    }
151
    return *s->buf_ptr++;
152
}
153

    
154
static int http_get_line(HTTPContext *s, char *line, int line_size)
155
{
156
    int ch;
157
    char *q;
158

    
159
    q = line;
160
    for(;;) {
161
        ch = http_getc(s);
162
        if (ch < 0)
163
            return AVERROR(EIO);
164
        if (ch == '\n') {
165
            /* process line */
166
            if (q > line && q[-1] == '\r')
167
                q--;
168
            *q = '\0';
169

    
170
            return 0;
171
        } else {
172
            if ((q - line) < line_size - 1)
173
                *q++ = ch;
174
        }
175
    }
176
}
177

    
178
static int process_line(URLContext *h, char *line, int line_count,
179
                        int *new_location)
180
{
181
    HTTPContext *s = h->priv_data;
182
    char *tag, *p;
183

    
184
    /* end of header */
185
    if (line[0] == '\0')
186
        return 0;
187

    
188
    p = line;
189
    if (line_count == 0) {
190
        while (!isspace(*p) && *p != '\0')
191
            p++;
192
        while (isspace(*p))
193
            p++;
194
        s->http_code = strtol(p, NULL, 10);
195
#ifdef DEBUG
196
        printf("http_code=%d\n", s->http_code);
197
#endif
198
        /* error codes are 4xx and 5xx */
199
        if (s->http_code >= 400 && s->http_code < 600)
200
            return -1;
201
    } else {
202
        while (*p != '\0' && *p != ':')
203
            p++;
204
        if (*p != ':')
205
            return 1;
206

    
207
        *p = '\0';
208
        tag = line;
209
        p++;
210
        while (isspace(*p))
211
            p++;
212
        if (!strcmp(tag, "Location")) {
213
            strcpy(s->location, p);
214
            *new_location = 1;
215
        } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
216
            s->filesize = atoll(p);
217
        } else if (!strcmp (tag, "Content-Range")) {
218
            /* "bytes $from-$to/$document_size" */
219
            const char *slash;
220
            if (!strncmp (p, "bytes ", 6)) {
221
                p += 6;
222
                s->off = atoll(p);
223
                if ((slash = strchr(p, '/')) && strlen(slash) > 0)
224
                    s->filesize = atoll(slash+1);
225
            }
226
            h->is_streamed = 0; /* we _can_ in fact seek */
227
        }
228
    }
229
    return 1;
230
}
231

    
232
static int http_connect(URLContext *h, const char *path, const char *hoststr,
233
                        const char *auth, int *new_location)
234
{
235
    HTTPContext *s = h->priv_data;
236
    int post, err;
237
    char line[1024];
238
    char *auth_b64;
239
    int auth_b64_len = (strlen(auth) + 2) / 3 * 4 + 1;
240
    int64_t off = s->off;
241

    
242

    
243
    /* send http header */
244
    post = h->flags & URL_WRONLY;
245
    auth_b64 = av_malloc(auth_b64_len);
246
    av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth));
247
    snprintf(s->buffer, sizeof(s->buffer),
248
             "%s %s HTTP/1.1\r\n"
249
             "User-Agent: %s\r\n"
250
             "Accept: */*\r\n"
251
             "Range: bytes=%"PRId64"-\r\n"
252
             "Host: %s\r\n"
253
             "Authorization: Basic %s\r\n"
254
             "Connection: close\r\n"
255
             "\r\n",
256
             post ? "POST" : "GET",
257
             path,
258
             LIBAVFORMAT_IDENT,
259
             s->off,
260
             hoststr,
261
             auth_b64);
262

    
263
    av_freep(&auth_b64);
264
    if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
265
        return AVERROR(EIO);
266

    
267
    /* init input buffer */
268
    s->buf_ptr = s->buffer;
269
    s->buf_end = s->buffer;
270
    s->line_count = 0;
271
    s->off = 0;
272
    s->filesize = -1;
273
    if (post) {
274
        return 0;
275
    }
276

    
277
    /* wait for header */
278
    for(;;) {
279
        if (http_get_line(s, line, sizeof(line)) < 0)
280
            return AVERROR(EIO);
281
#ifdef DEBUG
282
            printf("header='%s'\n", line);
283
#endif
284
            err = process_line(h, line, s->line_count, new_location);
285
            if (err < 0)
286
                return err;
287
            if (err == 0)
288
                break;
289
            s->line_count++;
290
    }
291

    
292
    return (off == s->off) ? 0 : -1;
293
}
294

    
295

    
296
static int http_read(URLContext *h, uint8_t *buf, int size)
297
{
298
    HTTPContext *s = h->priv_data;
299
    int len;
300

    
301
    /* read bytes from input buffer first */
302
    len = s->buf_end - s->buf_ptr;
303
    if (len > 0) {
304
        if (len > size)
305
            len = size;
306
        memcpy(buf, s->buf_ptr, len);
307
        s->buf_ptr += len;
308
    } else {
309
        len = url_read(s->hd, buf, size);
310
    }
311
    if (len > 0)
312
        s->off += len;
313
    return len;
314
}
315

    
316
/* used only when posting data */
317
static int http_write(URLContext *h, uint8_t *buf, int size)
318
{
319
    HTTPContext *s = h->priv_data;
320
    return url_write(s->hd, buf, size);
321
}
322

    
323
static int http_close(URLContext *h)
324
{
325
    HTTPContext *s = h->priv_data;
326
    url_close(s->hd);
327
    av_free(s);
328
    return 0;
329
}
330

    
331
static int64_t http_seek(URLContext *h, int64_t off, int whence)
332
{
333
    HTTPContext *s = h->priv_data;
334
    URLContext *old_hd = s->hd;
335
    int64_t old_off = s->off;
336

    
337
    if (whence == AVSEEK_SIZE)
338
        return s->filesize;
339
    else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
340
        return -1;
341

    
342
    /* we save the old context in case the seek fails */
343
    s->hd = NULL;
344
    if (whence == SEEK_CUR)
345
        off += s->off;
346
    else if (whence == SEEK_END)
347
        off += s->filesize;
348
    s->off = off;
349

    
350
    /* if it fails, continue on old connection */
351
    if (http_open_cnx(h) < 0) {
352
        s->hd = old_hd;
353
        s->off = old_off;
354
        return -1;
355
    }
356
    url_close(old_hd);
357
    return off;
358
}
359

    
360
static int
361
http_get_file_handle(URLContext *h)
362
{
363
    HTTPContext *s = h->priv_data;
364
    return url_get_file_handle(s->hd);
365
}
366

    
367
URLProtocol http_protocol = {
368
    "http",
369
    http_open,
370
    http_read,
371
    http_write,
372
    http_seek,
373
    http_close,
374
    .url_get_file_handle = http_get_file_handle,
375
};