Statistics
| Branch: | Revision:

ffmpeg / libavformat / http.c @ 20cd83e2

History | View | Annotate | Download (6.89 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
#include <sys/types.h>
24 b8a78f41 Michael Niedermayer
#include <sys/socket.h>
25 de6d9b64 Fabrice Bellard
#include <netinet/in.h>
26 9ddd71fc Fran├žois Revol
#ifndef __BEOS__
27
# include <arpa/inet.h>
28
#else
29
# include "barpainet.h"
30
#endif
31 de6d9b64 Fabrice Bellard
#include <netdb.h>
32
33 a7c6aa73 Ryan Martell
#include "base64.h"
34 de6d9b64 Fabrice Bellard
35
/* XXX: POST protocol is not completly implemented because ffmpeg use
36
   only a subset of it */
37
38
//#define DEBUG
39
40
/* used for protocol handling */
41
#define BUFFER_SIZE 1024
42
#define URL_SIZE    4096
43
44
typedef struct {
45 0e2a2197 Fabrice Bellard
    URLContext *hd;
46 de6d9b64 Fabrice Bellard
    unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
47
    int line_count;
48
    int http_code;
49
    char location[URL_SIZE];
50
} HTTPContext;
51
52 6ba5cbc6 Petr Doubek
static int http_connect(URLContext *h, const char *path, const char *hoststr,
53
                        const char *auth);
54 0c1a9eda Zdenek Kabelac
static int http_write(URLContext *h, uint8_t *buf, int size);
55 de6d9b64 Fabrice Bellard
56 0e2a2197 Fabrice Bellard
57 de6d9b64 Fabrice Bellard
/* return non zero if error */
58
static int http_open(URLContext *h, const char *uri, int flags)
59
{
60 0e2a2197 Fabrice Bellard
    const char *path, *proxy_path;
61
    char hostname[1024], hoststr[1024];
62 6ba5cbc6 Petr Doubek
    char auth[1024];
63 0e2a2197 Fabrice Bellard
    char path1[1024];
64
    char buf[1024];
65
    int port, use_proxy, err;
66 de6d9b64 Fabrice Bellard
    HTTPContext *s;
67 0e2a2197 Fabrice Bellard
    URLContext *hd = NULL;
68 de6d9b64 Fabrice Bellard
69
    h->is_streamed = 1;
70
71 1ea4f593 Fabrice Bellard
    s = av_malloc(sizeof(HTTPContext));
72 de6d9b64 Fabrice Bellard
    if (!s) {
73
        return -ENOMEM;
74
    }
75
    h->priv_data = s;
76
77
    proxy_path = getenv("http_proxy");
78 115329f1 Diego Biurrun
    use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
79 0e2a2197 Fabrice Bellard
        strstart(proxy_path, "http://", NULL);
80 de6d9b64 Fabrice Bellard
81
    /* fill the dest addr */
82
 redo:
83 0e2a2197 Fabrice Bellard
    /* needed in any case to build the host string */
84 115329f1 Diego Biurrun
    url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
85 0e2a2197 Fabrice Bellard
              path1, sizeof(path1), uri);
86
    if (port > 0) {
87
        snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
88 de6d9b64 Fabrice Bellard
    } else {
89 0e2a2197 Fabrice Bellard
        pstrcpy(hoststr, sizeof(hoststr), hostname);
90 de6d9b64 Fabrice Bellard
    }
91 0e2a2197 Fabrice Bellard
92 de6d9b64 Fabrice Bellard
    if (use_proxy) {
93 115329f1 Diego Biurrun
        url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
94 0e2a2197 Fabrice Bellard
                  NULL, 0, proxy_path);
95 de6d9b64 Fabrice Bellard
        path = uri;
96
    } else {
97 0e2a2197 Fabrice Bellard
        if (path1[0] == '\0')
98 de6d9b64 Fabrice Bellard
            path = "/";
99
        else
100 0e2a2197 Fabrice Bellard
            path = path1;
101 de6d9b64 Fabrice Bellard
    }
102 0e2a2197 Fabrice Bellard
    if (port < 0)
103
        port = 80;
104 de6d9b64 Fabrice Bellard
105 0e2a2197 Fabrice Bellard
    snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
106
    err = url_open(&hd, buf, URL_RDWR);
107
    if (err < 0)
108 de6d9b64 Fabrice Bellard
        goto fail;
109
110 0e2a2197 Fabrice Bellard
    s->hd = hd;
111 6ba5cbc6 Petr Doubek
    if (http_connect(h, path, hoststr, auth) < 0)
112 de6d9b64 Fabrice Bellard
        goto fail;
113
    if (s->http_code == 303 && s->location[0] != '\0') {
114
        /* url moved, get next */
115
        uri = s->location;
116 0e2a2197 Fabrice Bellard
        url_close(hd);
117 de6d9b64 Fabrice Bellard
        goto redo;
118
    }
119
    return 0;
120
 fail:
121 0e2a2197 Fabrice Bellard
    if (hd)
122
        url_close(hd);
123 1ea4f593 Fabrice Bellard
    av_free(s);
124 0bd586c5 Mike Melanson
    return AVERROR_IO;
125 de6d9b64 Fabrice Bellard
}
126
127
static int http_getc(HTTPContext *s)
128
{
129
    int len;
130
    if (s->buf_ptr >= s->buf_end) {
131 0e2a2197 Fabrice Bellard
        len = url_read(s->hd, s->buffer, BUFFER_SIZE);
132 de6d9b64 Fabrice Bellard
        if (len < 0) {
133 0bd586c5 Mike Melanson
            return AVERROR_IO;
134 de6d9b64 Fabrice Bellard
        } else if (len == 0) {
135
            return -1;
136
        } else {
137
            s->buf_ptr = s->buffer;
138
            s->buf_end = s->buffer + len;
139
        }
140
    }
141
    return *s->buf_ptr++;
142
}
143
144
static int process_line(HTTPContext *s, char *line, int line_count)
145
{
146
    char *tag, *p;
147 115329f1 Diego Biurrun
148 de6d9b64 Fabrice Bellard
    /* end of header */
149
    if (line[0] == '\0')
150
        return 0;
151
152
    p = line;
153
    if (line_count == 0) {
154
        while (!isspace(*p) && *p != '\0')
155
            p++;
156
        while (isspace(*p))
157
            p++;
158
        s->http_code = strtol(p, NULL, 10);
159
#ifdef DEBUG
160
        printf("http_code=%d\n", s->http_code);
161
#endif
162
    } else {
163
        while (*p != '\0' && *p != ':')
164
            p++;
165 115329f1 Diego Biurrun
        if (*p != ':')
166 de6d9b64 Fabrice Bellard
            return 1;
167 115329f1 Diego Biurrun
168 de6d9b64 Fabrice Bellard
        *p = '\0';
169
        tag = line;
170
        p++;
171
        while (isspace(*p))
172
            p++;
173
        if (!strcmp(tag, "Location")) {
174
            strcpy(s->location, p);
175
        }
176
    }
177
    return 1;
178
}
179
180 6ba5cbc6 Petr Doubek
static int http_connect(URLContext *h, const char *path, const char *hoststr,
181
                        const char *auth)
182 de6d9b64 Fabrice Bellard
{
183
    HTTPContext *s = h->priv_data;
184
    int post, err, ch;
185
    char line[1024], *q;
186 cfc4bd46 I. Po
    char *auth_b64;
187 de6d9b64 Fabrice Bellard
188
189
    /* send http header */
190
    post = h->flags & URL_WRONLY;
191
192 a7c6aa73 Ryan Martell
    auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
193 de6d9b64 Fabrice Bellard
    snprintf(s->buffer, sizeof(s->buffer),
194 9eef2b77 Michael Niedermayer
             "%s %s HTTP/1.0\r\n"
195
             "User-Agent: %s\r\n"
196
             "Accept: */*\r\n"
197
             "Host: %s\r\n"
198 6ba5cbc6 Petr Doubek
             "Authorization: Basic %s\r\n"
199 9eef2b77 Michael Niedermayer
             "\r\n",
200 de6d9b64 Fabrice Bellard
             post ? "POST" : "GET",
201
             path,
202 4250dafc Alex Beregszaszi
             LIBAVFORMAT_IDENT,
203 6ba5cbc6 Petr Doubek
             hoststr,
204 cfc4bd46 I. Po
             auth_b64);
205 115329f1 Diego Biurrun
206 cfc4bd46 I. Po
    av_freep(&auth_b64);
207 de6d9b64 Fabrice Bellard
    if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
208 0bd586c5 Mike Melanson
        return AVERROR_IO;
209 115329f1 Diego Biurrun
210 de6d9b64 Fabrice Bellard
    /* init input buffer */
211
    s->buf_ptr = s->buffer;
212
    s->buf_end = s->buffer;
213
    s->line_count = 0;
214
    s->location[0] = '\0';
215 ddceb31d Philip Gladstone
    if (post) {
216
        sleep(1);
217 de6d9b64 Fabrice Bellard
        return 0;
218 ddceb31d Philip Gladstone
    }
219 115329f1 Diego Biurrun
220 de6d9b64 Fabrice Bellard
    /* wait for header */
221
    q = line;
222
    for(;;) {
223
        ch = http_getc(s);
224
        if (ch < 0)
225 0bd586c5 Mike Melanson
            return AVERROR_IO;
226 de6d9b64 Fabrice Bellard
        if (ch == '\n') {
227
            /* process line */
228
            if (q > line && q[-1] == '\r')
229
                q--;
230
            *q = '\0';
231
#ifdef DEBUG
232
            printf("header='%s'\n", line);
233
#endif
234
            err = process_line(s, line, s->line_count);
235
            if (err < 0)
236
                return err;
237
            if (err == 0)
238
                return 0;
239
            s->line_count++;
240
            q = line;
241
        } else {
242
            if ((q - line) < sizeof(line) - 1)
243
                *q++ = ch;
244
        }
245
    }
246
}
247
248
249 0c1a9eda Zdenek Kabelac
static int http_read(URLContext *h, uint8_t *buf, int size)
250 de6d9b64 Fabrice Bellard
{
251
    HTTPContext *s = h->priv_data;
252 9eef2b77 Michael Niedermayer
    int len;
253 de6d9b64 Fabrice Bellard
254 9eef2b77 Michael Niedermayer
    /* read bytes from input buffer first */
255
    len = s->buf_end - s->buf_ptr;
256
    if (len > 0) {
257
        if (len > size)
258
            len = size;
259
        memcpy(buf, s->buf_ptr, len);
260
        s->buf_ptr += len;
261
    } else {
262
        len = url_read(s->hd, buf, size);
263 de6d9b64 Fabrice Bellard
    }
264 9eef2b77 Michael Niedermayer
    return len;
265 de6d9b64 Fabrice Bellard
}
266
267
/* used only when posting data */
268 0c1a9eda Zdenek Kabelac
static int http_write(URLContext *h, uint8_t *buf, int size)
269 de6d9b64 Fabrice Bellard
{
270
    HTTPContext *s = h->priv_data;
271 0e2a2197 Fabrice Bellard
    return url_write(s->hd, buf, size);
272 de6d9b64 Fabrice Bellard
}
273
274
static int http_close(URLContext *h)
275
{
276
    HTTPContext *s = h->priv_data;
277 0e2a2197 Fabrice Bellard
    url_close(s->hd);
278
    av_free(s);
279 de6d9b64 Fabrice Bellard
    return 0;
280
}
281
282
URLProtocol http_protocol = {
283
    "http",
284
    http_open,
285
    http_read,
286
    http_write,
287
    NULL, /* seek */
288
    http_close,
289
};