Statistics
| Branch: | Revision:

ffmpeg / libavformat / http.c @ 25f8db58

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