2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
31 /* XXX: POST protocol is not completly implemented because ffmpeg use
32 only a subset of it */
36 /* used for protocol handling */
37 #define BUFFER_SIZE 1024
39 #define MAX_REDIRECTS 8
43 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
46 offset_t off, filesize;
47 char location[URL_SIZE];
50 static int http_connect(URLContext *h, const char *path, const char *hoststr,
51 const char *auth, int *new_location);
52 static int http_write(URLContext *h, uint8_t *buf, int size);
55 /* return non zero if error */
56 static int http_open_cnx(URLContext *h)
58 const char *path, *proxy_path;
59 char hostname[1024], hoststr[1024];
63 int port, use_proxy, err, location_changed = 0, redirects = 0;
64 HTTPContext *s = h->priv_data;
65 URLContext *hd = NULL;
67 proxy_path = getenv("http_proxy");
68 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
69 strstart(proxy_path, "http://", NULL);
71 /* fill the dest addr */
73 /* needed in any case to build the host string */
74 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
75 path1, sizeof(path1), s->location);
77 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
79 pstrcpy(hoststr, sizeof(hoststr), hostname);
83 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
95 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
96 err = url_open(&hd, buf, URL_RDWR);
101 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
103 if (s->http_code == 303 && location_changed == 1) {
104 /* url moved, get next */
106 if (redirects++ >= MAX_REDIRECTS)
108 location_changed = 0;
118 static int http_open(URLContext *h, const char *uri, int flags)
125 s = av_malloc(sizeof(HTTPContext));
132 pstrcpy (s->location, URL_SIZE, uri);
134 ret = http_open_cnx(h);
139 static int http_getc(HTTPContext *s)
142 if (s->buf_ptr >= s->buf_end) {
143 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
146 } else if (len == 0) {
149 s->buf_ptr = s->buffer;
150 s->buf_end = s->buffer + len;
153 return *s->buf_ptr++;
156 static int process_line(URLContext *h, char *line, int line_count,
159 HTTPContext *s = h->priv_data;
167 if (line_count == 0) {
168 while (!isspace(*p) && *p != '\0')
172 s->http_code = strtol(p, NULL, 10);
174 printf("http_code=%d\n", s->http_code);
177 while (*p != '\0' && *p != ':')
187 if (!strcmp(tag, "Location")) {
188 strcpy(s->location, p);
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" */
195 if (!strncmp (p, "bytes ", 6)) {
198 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
199 s->filesize = atoll(slash+1);
201 h->is_streamed = 0; /* we _can_ in fact seek */
207 static int http_connect(URLContext *h, const char *path, const char *hoststr,
208 const char *auth, int *new_location)
210 HTTPContext *s = h->priv_data;
214 offset_t off = s->off;
217 /* send http header */
218 post = h->flags & URL_WRONLY;
220 auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
221 snprintf(s->buffer, sizeof(s->buffer),
225 "Range: bytes=%llu-\r\n"
227 "Authorization: Basic %s\r\n"
229 post ? "POST" : "GET",
237 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
240 /* init input buffer */
241 s->buf_ptr = s->buffer;
242 s->buf_end = s->buffer;
250 /* wait for header */
258 if (q > line && q[-1] == '\r')
262 printf("header='%s'\n", line);
264 err = process_line(h, line, s->line_count, new_location);
272 if ((q - line) < sizeof(line) - 1)
277 return (off == s->off) ? 0 : -1;
281 static int http_read(URLContext *h, uint8_t *buf, int size)
283 HTTPContext *s = h->priv_data;
286 /* read bytes from input buffer first */
287 len = s->buf_end - s->buf_ptr;
291 memcpy(buf, s->buf_ptr, len);
294 len = url_read(s->hd, buf, size);
301 /* used only when posting data */
302 static int http_write(URLContext *h, uint8_t *buf, int size)
304 HTTPContext *s = h->priv_data;
305 return url_write(s->hd, buf, size);
308 static int http_close(URLContext *h)
310 HTTPContext *s = h->priv_data;
316 static offset_t http_seek(URLContext *h, offset_t off, int whence)
318 HTTPContext *s = h->priv_data;
319 URLContext *old_hd = s->hd;
320 offset_t old_off = s->off;
322 if (whence == AVSEEK_SIZE)
324 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
327 /* we save the old context in case the seek fails */
329 if (whence == SEEK_CUR)
331 else if (whence == SEEK_END)
335 /* if it fails, continue on old connection */
336 if (http_open_cnx(h) < 0) {
345 URLProtocol http_protocol = {