~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to extern/ffmpeg/libavformat/http.c

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "avformat.h"
 
22
#include <unistd.h>
 
23
#include "network.h"
 
24
 
 
25
#include "base64.h"
 
26
#include "avstring.h"
 
27
 
 
28
/* XXX: POST protocol is not completely implemented because ffmpeg uses
 
29
   only a subset of it. */
 
30
 
 
31
//#define DEBUG
 
32
 
 
33
/* used for protocol handling */
 
34
#define BUFFER_SIZE 1024
 
35
#define URL_SIZE    4096
 
36
#define MAX_REDIRECTS 8
 
37
 
 
38
typedef struct {
 
39
    URLContext *hd;
 
40
    unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
 
41
    int line_count;
 
42
    int http_code;
 
43
    offset_t off, filesize;
 
44
    char location[URL_SIZE];
 
45
} HTTPContext;
 
46
 
 
47
static int http_connect(URLContext *h, const char *path, const char *hoststr,
 
48
                        const char *auth, int *new_location);
 
49
static int http_write(URLContext *h, uint8_t *buf, int size);
 
50
 
 
51
 
 
52
/* return non zero if error */
 
53
static int http_open_cnx(URLContext *h)
 
54
{
 
55
    const char *path, *proxy_path;
 
56
    char hostname[1024], hoststr[1024];
 
57
    char auth[1024];
 
58
    char path1[1024];
 
59
    char buf[1024];
 
60
    int port, use_proxy, err, location_changed = 0, redirects = 0;
 
61
    HTTPContext *s = h->priv_data;
 
62
    URLContext *hd = NULL;
 
63
 
 
64
    proxy_path = getenv("http_proxy");
 
65
    use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
 
66
        av_strstart(proxy_path, "http://", NULL);
 
67
 
 
68
    /* fill the dest addr */
 
69
 redo:
 
70
    /* needed in any case to build the host string */
 
71
    url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
 
72
              path1, sizeof(path1), s->location);
 
73
    if (port > 0) {
 
74
        snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
 
75
    } else {
 
76
        av_strlcpy(hoststr, hostname, sizeof(hoststr));
 
77
    }
 
78
 
 
79
    if (use_proxy) {
 
80
        url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
 
81
                  NULL, 0, proxy_path);
 
82
        path = s->location;
 
83
    } else {
 
84
        if (path1[0] == '\0')
 
85
            path = "/";
 
86
        else
 
87
            path = path1;
 
88
    }
 
89
    if (port < 0)
 
90
        port = 80;
 
91
 
 
92
    snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
 
93
    err = url_open(&hd, buf, URL_RDWR);
 
94
    if (err < 0)
 
95
        goto fail;
 
96
 
 
97
    s->hd = hd;
 
98
    if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
 
99
        goto fail;
 
100
    if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
 
101
        /* url moved, get next */
 
102
        url_close(hd);
 
103
        if (redirects++ >= MAX_REDIRECTS)
 
104
            return AVERROR(EIO);
 
105
        location_changed = 0;
 
106
        goto redo;
 
107
    }
 
108
    return 0;
 
109
 fail:
 
110
    if (hd)
 
111
        url_close(hd);
 
112
    return AVERROR(EIO);
 
113
}
 
114
 
 
115
static int http_open(URLContext *h, const char *uri, int flags)
 
116
{
 
117
    HTTPContext *s;
 
118
    int ret;
 
119
 
 
120
    h->is_streamed = 1;
 
121
 
 
122
    s = av_malloc(sizeof(HTTPContext));
 
123
    if (!s) {
 
124
        return AVERROR(ENOMEM);
 
125
    }
 
126
    h->priv_data = s;
 
127
    s->filesize = -1;
 
128
    s->off = 0;
 
129
    av_strlcpy(s->location, uri, URL_SIZE);
 
130
 
 
131
    ret = http_open_cnx(h);
 
132
    if (ret != 0)
 
133
        av_free (s);
 
134
    return ret;
 
135
}
 
136
static int http_getc(HTTPContext *s)
 
137
{
 
138
    int len;
 
139
    if (s->buf_ptr >= s->buf_end) {
 
140
        len = url_read(s->hd, s->buffer, BUFFER_SIZE);
 
141
        if (len < 0) {
 
142
            return AVERROR(EIO);
 
143
        } else if (len == 0) {
 
144
            return -1;
 
145
        } else {
 
146
            s->buf_ptr = s->buffer;
 
147
            s->buf_end = s->buffer + len;
 
148
        }
 
149
    }
 
150
    return *s->buf_ptr++;
 
151
}
 
152
 
 
153
static int process_line(URLContext *h, char *line, int line_count,
 
154
                        int *new_location)
 
155
{
 
156
    HTTPContext *s = h->priv_data;
 
157
    char *tag, *p;
 
158
 
 
159
    /* end of header */
 
160
    if (line[0] == '\0')
 
161
        return 0;
 
162
 
 
163
    p = line;
 
164
    if (line_count == 0) {
 
165
        while (!isspace(*p) && *p != '\0')
 
166
            p++;
 
167
        while (isspace(*p))
 
168
            p++;
 
169
        s->http_code = strtol(p, NULL, 10);
 
170
#ifdef DEBUG
 
171
        printf("http_code=%d\n", s->http_code);
 
172
#endif
 
173
        /* error codes are 4xx and 5xx */
 
174
        if (s->http_code >= 400 && s->http_code < 600)
 
175
            return -1;
 
176
    } else {
 
177
        while (*p != '\0' && *p != ':')
 
178
            p++;
 
179
        if (*p != ':')
 
180
            return 1;
 
181
 
 
182
        *p = '\0';
 
183
        tag = line;
 
184
        p++;
 
185
        while (isspace(*p))
 
186
            p++;
 
187
        if (!strcmp(tag, "Location")) {
 
188
            strcpy(s->location, p);
 
189
            *new_location = 1;
 
190
        } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
 
191
            s->filesize = atoll(p);
 
192
        } else if (!strcmp (tag, "Content-Range")) {
 
193
            /* "bytes $from-$to/$document_size" */
 
194
            const char *slash;
 
195
            if (!strncmp (p, "bytes ", 6)) {
 
196
                p += 6;
 
197
                s->off = atoll(p);
 
198
                if ((slash = strchr(p, '/')) && strlen(slash) > 0)
 
199
                    s->filesize = atoll(slash+1);
 
200
            }
 
201
            h->is_streamed = 0; /* we _can_ in fact seek */
 
202
        }
 
203
    }
 
204
    return 1;
 
205
}
 
206
 
 
207
static int http_connect(URLContext *h, const char *path, const char *hoststr,
 
208
                        const char *auth, int *new_location)
 
209
{
 
210
    HTTPContext *s = h->priv_data;
 
211
    int post, err, ch;
 
212
    char line[1024], *q;
 
213
    char *auth_b64;
 
214
    int auth_b64_len = strlen(auth)* 4 / 3 + 12;
 
215
    offset_t off = s->off;
 
216
 
 
217
 
 
218
    /* send http header */
 
219
    post = h->flags & URL_WRONLY;
 
220
    auth_b64 = av_malloc(auth_b64_len);
 
221
    av_base64_encode(auth_b64, auth_b64_len, (uint8_t *)auth, strlen(auth));
 
222
    snprintf(s->buffer, sizeof(s->buffer),
 
223
             "%s %s HTTP/1.1\r\n"
 
224
             "User-Agent: %s\r\n"
 
225
             "Accept: */*\r\n"
 
226
             "Range: bytes=%"PRId64"-\r\n"
 
227
             "Host: %s\r\n"
 
228
             "Authorization: Basic %s\r\n"
 
229
             "Connection: close\r\n"
 
230
             "\r\n",
 
231
             post ? "POST" : "GET",
 
232
             path,
 
233
             LIBAVFORMAT_IDENT,
 
234
             s->off,
 
235
             hoststr,
 
236
             auth_b64);
 
237
 
 
238
    av_freep(&auth_b64);
 
239
    if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
 
240
        return AVERROR(EIO);
 
241
 
 
242
    /* init input buffer */
 
243
    s->buf_ptr = s->buffer;
 
244
    s->buf_end = s->buffer;
 
245
    s->line_count = 0;
 
246
    s->off = 0;
 
247
    s->filesize = -1;
 
248
    if (post) {
 
249
        usleep(1000*1000);
 
250
        return 0;
 
251
    }
 
252
 
 
253
    /* wait for header */
 
254
    q = line;
 
255
    for(;;) {
 
256
        ch = http_getc(s);
 
257
        if (ch < 0)
 
258
            return AVERROR(EIO);
 
259
        if (ch == '\n') {
 
260
            /* process line */
 
261
            if (q > line && q[-1] == '\r')
 
262
                q--;
 
263
            *q = '\0';
 
264
#ifdef DEBUG
 
265
            printf("header='%s'\n", line);
 
266
#endif
 
267
            err = process_line(h, line, s->line_count, new_location);
 
268
            if (err < 0)
 
269
                return err;
 
270
            if (err == 0)
 
271
                break;
 
272
            s->line_count++;
 
273
            q = line;
 
274
        } else {
 
275
            if ((q - line) < sizeof(line) - 1)
 
276
                *q++ = ch;
 
277
        }
 
278
    }
 
279
 
 
280
    return (off == s->off) ? 0 : -1;
 
281
}
 
282
 
 
283
 
 
284
static int http_read(URLContext *h, uint8_t *buf, int size)
 
285
{
 
286
    HTTPContext *s = h->priv_data;
 
287
    int len;
 
288
 
 
289
    /* read bytes from input buffer first */
 
290
    len = s->buf_end - s->buf_ptr;
 
291
    if (len > 0) {
 
292
        if (len > size)
 
293
            len = size;
 
294
        memcpy(buf, s->buf_ptr, len);
 
295
        s->buf_ptr += len;
 
296
    } else {
 
297
        len = url_read(s->hd, buf, size);
 
298
    }
 
299
    if (len > 0)
 
300
        s->off += len;
 
301
    return len;
 
302
}
 
303
 
 
304
/* used only when posting data */
 
305
static int http_write(URLContext *h, uint8_t *buf, int size)
 
306
{
 
307
    HTTPContext *s = h->priv_data;
 
308
    return url_write(s->hd, buf, size);
 
309
}
 
310
 
 
311
static int http_close(URLContext *h)
 
312
{
 
313
    HTTPContext *s = h->priv_data;
 
314
    url_close(s->hd);
 
315
    av_free(s);
 
316
    return 0;
 
317
}
 
318
 
 
319
static offset_t http_seek(URLContext *h, offset_t off, int whence)
 
320
{
 
321
    HTTPContext *s = h->priv_data;
 
322
    URLContext *old_hd = s->hd;
 
323
    offset_t old_off = s->off;
 
324
 
 
325
    if (whence == AVSEEK_SIZE)
 
326
        return s->filesize;
 
327
    else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
 
328
        return -1;
 
329
 
 
330
    /* we save the old context in case the seek fails */
 
331
    s->hd = NULL;
 
332
    if (whence == SEEK_CUR)
 
333
        off += s->off;
 
334
    else if (whence == SEEK_END)
 
335
        off += s->filesize;
 
336
    s->off = off;
 
337
 
 
338
    /* if it fails, continue on old connection */
 
339
    if (http_open_cnx(h) < 0) {
 
340
        s->hd = old_hd;
 
341
        s->off = old_off;
 
342
        return -1;
 
343
    }
 
344
    url_close(old_hd);
 
345
    return off;
 
346
}
 
347
 
 
348
URLProtocol http_protocol = {
 
349
    "http",
 
350
    http_open,
 
351
    http_read,
 
352
    http_write,
 
353
    http_seek,
 
354
    http_close,
 
355
};