Statistics
| Branch: | Revision:

ffmpeg / libavformat / http.c @ 1d7d9935

History | View | Annotate | Download (8.72 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * HTTP protocol for ffmpeg client
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 8be1c656 Fabrice Bellard
#include "avformat.h"
22 de6d9b64 Fabrice Bellard
#include <unistd.h>
23 42572ef5 Ramiro Polla
#include "network.h"
24 de6d9b64 Fabrice Bellard
25 a7c6aa73 Ryan Martell
#include "base64.h"
26 de6d9b64 Fabrice Bellard
27
/* XXX: POST protocol is not completly implemented because ffmpeg use
28
   only a subset of it */
29
30
//#define DEBUG
31
32
/* used for protocol handling */
33
#define BUFFER_SIZE 1024
34
#define URL_SIZE    4096
35 fb2b4fc5 Ronald S. Bultje
#define MAX_REDIRECTS 8
36 de6d9b64 Fabrice Bellard
37
typedef struct {
38 0e2a2197 Fabrice Bellard
    URLContext *hd;
39 de6d9b64 Fabrice Bellard
    unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
40
    int line_count;
41
    int http_code;
42 fb2b4fc5 Ronald S. Bultje
    offset_t off, filesize;
43 de6d9b64 Fabrice Bellard
    char location[URL_SIZE];
44
} HTTPContext;
45
46 6ba5cbc6 Petr Doubek
static int http_connect(URLContext *h, const char *path, const char *hoststr,
47 fb2b4fc5 Ronald S. Bultje
                        const char *auth, int *new_location);
48 0c1a9eda Zdenek Kabelac
static int http_write(URLContext *h, uint8_t *buf, int size);
49 de6d9b64 Fabrice Bellard
50 0e2a2197 Fabrice Bellard
51 de6d9b64 Fabrice Bellard
/* return non zero if error */
52 fb2b4fc5 Ronald S. Bultje
static int http_open_cnx(URLContext *h)
53 de6d9b64 Fabrice Bellard
{
54 0e2a2197 Fabrice Bellard
    const char *path, *proxy_path;
55
    char hostname[1024], hoststr[1024];
56 6ba5cbc6 Petr Doubek
    char auth[1024];
57 0e2a2197 Fabrice Bellard
    char path1[1024];
58
    char buf[1024];
59 fb2b4fc5 Ronald S. Bultje
    int port, use_proxy, err, location_changed = 0, redirects = 0;
60
    HTTPContext *s = h->priv_data;
61 0e2a2197 Fabrice Bellard
    URLContext *hd = NULL;
62 de6d9b64 Fabrice Bellard
63
    proxy_path = getenv("http_proxy");
64 115329f1 Diego Biurrun
    use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
65 0e2a2197 Fabrice Bellard
        strstart(proxy_path, "http://", NULL);
66 de6d9b64 Fabrice Bellard
67
    /* fill the dest addr */
68
 redo:
69 0e2a2197 Fabrice Bellard
    /* needed in any case to build the host string */
70 115329f1 Diego Biurrun
    url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
71 fb2b4fc5 Ronald S. Bultje
              path1, sizeof(path1), s->location);
72 0e2a2197 Fabrice Bellard
    if (port > 0) {
73
        snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
74 de6d9b64 Fabrice Bellard
    } else {
75 0e2a2197 Fabrice Bellard
        pstrcpy(hoststr, sizeof(hoststr), hostname);
76 de6d9b64 Fabrice Bellard
    }
77 0e2a2197 Fabrice Bellard
78 de6d9b64 Fabrice Bellard
    if (use_proxy) {
79 115329f1 Diego Biurrun
        url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
80 0e2a2197 Fabrice Bellard
                  NULL, 0, proxy_path);
81 fb2b4fc5 Ronald S. Bultje
        path = s->location;
82 de6d9b64 Fabrice Bellard
    } else {
83 0e2a2197 Fabrice Bellard
        if (path1[0] == '\0')
84 de6d9b64 Fabrice Bellard
            path = "/";
85
        else
86 0e2a2197 Fabrice Bellard
            path = path1;
87 de6d9b64 Fabrice Bellard
    }
88 0e2a2197 Fabrice Bellard
    if (port < 0)
89
        port = 80;
90 de6d9b64 Fabrice Bellard
91 0e2a2197 Fabrice Bellard
    snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
92
    err = url_open(&hd, buf, URL_RDWR);
93
    if (err < 0)
94 de6d9b64 Fabrice Bellard
        goto fail;
95
96 0e2a2197 Fabrice Bellard
    s->hd = hd;
97 fb2b4fc5 Ronald S. Bultje
    if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
98 de6d9b64 Fabrice Bellard
        goto fail;
99 fb2b4fc5 Ronald S. Bultje
    if (s->http_code == 303 && location_changed == 1) {
100 de6d9b64 Fabrice Bellard
        /* url moved, get next */
101 0e2a2197 Fabrice Bellard
        url_close(hd);
102 fb2b4fc5 Ronald S. Bultje
        if (redirects++ >= MAX_REDIRECTS)
103
            return AVERROR_IO;
104
        location_changed = 0;
105 de6d9b64 Fabrice Bellard
        goto redo;
106
    }
107
    return 0;
108
 fail:
109 0e2a2197 Fabrice Bellard
    if (hd)
110
        url_close(hd);
111 0bd586c5 Mike Melanson
    return AVERROR_IO;
112 de6d9b64 Fabrice Bellard
}
113
114 fb2b4fc5 Ronald S. Bultje
static int http_open(URLContext *h, const char *uri, int flags)
115
{
116
    HTTPContext *s;
117
    int ret;
118
119
    h->is_streamed = 1;
120
121
    s = av_malloc(sizeof(HTTPContext));
122
    if (!s) {
123
        return -ENOMEM;
124
    }
125
    h->priv_data = s;
126
    s->filesize = -1;
127
    s->off = 0;
128
    pstrcpy (s->location, URL_SIZE, uri);
129
130
    ret = http_open_cnx(h);
131
    if (ret != 0)
132
        av_free (s);
133
    return ret;
134
}
135 de6d9b64 Fabrice Bellard
static int http_getc(HTTPContext *s)
136
{
137
    int len;
138
    if (s->buf_ptr >= s->buf_end) {
139 0e2a2197 Fabrice Bellard
        len = url_read(s->hd, s->buffer, BUFFER_SIZE);
140 de6d9b64 Fabrice Bellard
        if (len < 0) {
141 0bd586c5 Mike Melanson
            return AVERROR_IO;
142 de6d9b64 Fabrice Bellard
        } else if (len == 0) {
143
            return -1;
144
        } else {
145
            s->buf_ptr = s->buffer;
146
            s->buf_end = s->buffer + len;
147
        }
148
    }
149
    return *s->buf_ptr++;
150
}
151
152 fb2b4fc5 Ronald S. Bultje
static int process_line(URLContext *h, char *line, int line_count,
153
                        int *new_location)
154 de6d9b64 Fabrice Bellard
{
155 fb2b4fc5 Ronald S. Bultje
    HTTPContext *s = h->priv_data;
156 de6d9b64 Fabrice Bellard
    char *tag, *p;
157 115329f1 Diego Biurrun
158 de6d9b64 Fabrice Bellard
    /* end of header */
159
    if (line[0] == '\0')
160
        return 0;
161
162
    p = line;
163
    if (line_count == 0) {
164
        while (!isspace(*p) && *p != '\0')
165
            p++;
166
        while (isspace(*p))
167
            p++;
168
        s->http_code = strtol(p, NULL, 10);
169
#ifdef DEBUG
170
        printf("http_code=%d\n", s->http_code);
171
#endif
172
    } else {
173
        while (*p != '\0' && *p != ':')
174
            p++;
175 115329f1 Diego Biurrun
        if (*p != ':')
176 de6d9b64 Fabrice Bellard
            return 1;
177 115329f1 Diego Biurrun
178 de6d9b64 Fabrice Bellard
        *p = '\0';
179
        tag = line;
180
        p++;
181
        while (isspace(*p))
182
            p++;
183
        if (!strcmp(tag, "Location")) {
184
            strcpy(s->location, p);
185 fb2b4fc5 Ronald S. Bultje
            *new_location = 1;
186
        } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
187
            s->filesize = atoll(p);
188
        } else if (!strcmp (tag, "Content-Range")) {
189
            /* "bytes $from-$to/$document_size" */
190
            const char *slash;
191
            if (!strncmp (p, "bytes ", 6)) {
192
                p += 6;
193
                s->off = atoll(p);
194
                if ((slash = strchr(p, '/')) && strlen(slash) > 0)
195
                    s->filesize = atoll(slash+1);
196
            }
197
            h->is_streamed = 0; /* we _can_ in fact seek */
198 de6d9b64 Fabrice Bellard
        }
199
    }
200
    return 1;
201
}
202
203 6ba5cbc6 Petr Doubek
static int http_connect(URLContext *h, const char *path, const char *hoststr,
204 fb2b4fc5 Ronald S. Bultje
                        const char *auth, int *new_location)
205 de6d9b64 Fabrice Bellard
{
206
    HTTPContext *s = h->priv_data;
207
    int post, err, ch;
208
    char line[1024], *q;
209 cfc4bd46 I. Po
    char *auth_b64;
210 fb2b4fc5 Ronald S. Bultje
    offset_t off = s->off;
211 de6d9b64 Fabrice Bellard
212
213
    /* send http header */
214
    post = h->flags & URL_WRONLY;
215
216 a7c6aa73 Ryan Martell
    auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
217 de6d9b64 Fabrice Bellard
    snprintf(s->buffer, sizeof(s->buffer),
218 fb2b4fc5 Ronald S. Bultje
             "%s %s HTTP/1.1\r\n"
219 9eef2b77 Michael Niedermayer
             "User-Agent: %s\r\n"
220
             "Accept: */*\r\n"
221 706c0d65 Guillaume Poirier
             "Range: bytes=%"PRId64"-\r\n"
222 9eef2b77 Michael Niedermayer
             "Host: %s\r\n"
223 6ba5cbc6 Petr Doubek
             "Authorization: Basic %s\r\n"
224 9eef2b77 Michael Niedermayer
             "\r\n",
225 de6d9b64 Fabrice Bellard
             post ? "POST" : "GET",
226
             path,
227 4250dafc Alex Beregszaszi
             LIBAVFORMAT_IDENT,
228 fb2b4fc5 Ronald S. Bultje
             s->off,
229 6ba5cbc6 Petr Doubek
             hoststr,
230 cfc4bd46 I. Po
             auth_b64);
231 115329f1 Diego Biurrun
232 cfc4bd46 I. Po
    av_freep(&auth_b64);
233 de6d9b64 Fabrice Bellard
    if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
234 0bd586c5 Mike Melanson
        return AVERROR_IO;
235 115329f1 Diego Biurrun
236 de6d9b64 Fabrice Bellard
    /* init input buffer */
237
    s->buf_ptr = s->buffer;
238
    s->buf_end = s->buffer;
239
    s->line_count = 0;
240 fb2b4fc5 Ronald S. Bultje
    s->off = 0;
241 ddceb31d Philip Gladstone
    if (post) {
242
        sleep(1);
243 de6d9b64 Fabrice Bellard
        return 0;
244 ddceb31d Philip Gladstone
    }
245 115329f1 Diego Biurrun
246 de6d9b64 Fabrice Bellard
    /* wait for header */
247
    q = line;
248
    for(;;) {
249
        ch = http_getc(s);
250
        if (ch < 0)
251 0bd586c5 Mike Melanson
            return AVERROR_IO;
252 de6d9b64 Fabrice Bellard
        if (ch == '\n') {
253
            /* process line */
254
            if (q > line && q[-1] == '\r')
255
                q--;
256
            *q = '\0';
257
#ifdef DEBUG
258
            printf("header='%s'\n", line);
259
#endif
260 fb2b4fc5 Ronald S. Bultje
            err = process_line(h, line, s->line_count, new_location);
261 de6d9b64 Fabrice Bellard
            if (err < 0)
262
                return err;
263
            if (err == 0)
264 fb2b4fc5 Ronald S. Bultje
                break;
265 de6d9b64 Fabrice Bellard
            s->line_count++;
266
            q = line;
267
        } else {
268
            if ((q - line) < sizeof(line) - 1)
269
                *q++ = ch;
270
        }
271
    }
272 fb2b4fc5 Ronald S. Bultje
273
    return (off == s->off) ? 0 : -1;
274 de6d9b64 Fabrice Bellard
}
275
276
277 0c1a9eda Zdenek Kabelac
static int http_read(URLContext *h, uint8_t *buf, int size)
278 de6d9b64 Fabrice Bellard
{
279
    HTTPContext *s = h->priv_data;
280 9eef2b77 Michael Niedermayer
    int len;
281 de6d9b64 Fabrice Bellard
282 9eef2b77 Michael Niedermayer
    /* read bytes from input buffer first */
283
    len = s->buf_end - s->buf_ptr;
284
    if (len > 0) {
285
        if (len > size)
286
            len = size;
287
        memcpy(buf, s->buf_ptr, len);
288
        s->buf_ptr += len;
289
    } else {
290
        len = url_read(s->hd, buf, size);
291 de6d9b64 Fabrice Bellard
    }
292 fb2b4fc5 Ronald S. Bultje
    if (len > 0)
293
        s->off += len;
294 9eef2b77 Michael Niedermayer
    return len;
295 de6d9b64 Fabrice Bellard
}
296
297
/* used only when posting data */
298 0c1a9eda Zdenek Kabelac
static int http_write(URLContext *h, uint8_t *buf, int size)
299 de6d9b64 Fabrice Bellard
{
300
    HTTPContext *s = h->priv_data;
301 0e2a2197 Fabrice Bellard
    return url_write(s->hd, buf, size);
302 de6d9b64 Fabrice Bellard
}
303
304
static int http_close(URLContext *h)
305
{
306
    HTTPContext *s = h->priv_data;
307 0e2a2197 Fabrice Bellard
    url_close(s->hd);
308
    av_free(s);
309 de6d9b64 Fabrice Bellard
    return 0;
310
}
311
312 fb2b4fc5 Ronald S. Bultje
static offset_t http_seek(URLContext *h, offset_t off, int whence)
313
{
314
    HTTPContext *s = h->priv_data;
315
    URLContext *old_hd = s->hd;
316
    offset_t old_off = s->off;
317
318
    if (whence == AVSEEK_SIZE)
319
        return s->filesize;
320
    else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
321
        return -1;
322
323
    /* we save the old context in case the seek fails */
324
    s->hd = NULL;
325
    if (whence == SEEK_CUR)
326
        off += s->off;
327
    else if (whence == SEEK_END)
328
        off += s->filesize;
329
    s->off = off;
330
331
    /* if it fails, continue on old connection */
332
    if (http_open_cnx(h) < 0) {
333
        s->hd = old_hd;
334
        s->off = old_off;
335
        return -1;
336
    }
337
    url_close(old_hd);
338
    return off;
339
}
340
341 de6d9b64 Fabrice Bellard
URLProtocol http_protocol = {
342
    "http",
343
    http_open,
344
    http_read,
345
    http_write,
346 fb2b4fc5 Ronald S. Bultje
    http_seek,
347 de6d9b64 Fabrice Bellard
    http_close,
348
};