annotate http.c @ 1668:b609473bf4bb libavformat

simplify
author michael
date Wed, 17 Jan 2007 12:06:31 +0000
parents 0a990429e524
children 92afee454599
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * HTTP protocol for ffmpeg client
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1181
diff changeset
5 * This file is part of FFmpeg.
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1181
diff changeset
6 *
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1181
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1181
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1181
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1181
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
896
edbe5c3717f9 Update licensing information: The FSF changed postal address.
diego
parents: 885
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
20 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
21 #include "avformat.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22 #include <unistd.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
23 #include <sys/types.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24 #include <sys/socket.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25 #include <netinet/in.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
26 #ifndef __BEOS__
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
27 # include <arpa/inet.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
28 #else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29 # include "barpainet.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
30 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 #include <netdb.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32
1432
4f4bebc36aad switch to common base64 routines in lavf
gpoirier
parents: 1358
diff changeset
33 #include "base64.h"
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 /* XXX: POST protocol is not completly implemented because ffmpeg use
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36 only a subset of it */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 //#define DEBUG
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 /* used for protocol handling */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 #define BUFFER_SIZE 1024
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 #define URL_SIZE 4096
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
43 #define MAX_REDIRECTS 8
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 typedef struct {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46 URLContext *hd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 int line_count;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49 int http_code;
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
50 offset_t off, filesize;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
51 char location[URL_SIZE];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52 } HTTPContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
54 static int http_connect(URLContext *h, const char *path, const char *hoststr,
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
55 const char *auth, int *new_location);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 0
diff changeset
56 static int http_write(URLContext *h, uint8_t *buf, int size);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
58
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
59 /* return non zero if error */
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
60 static int http_open_cnx(URLContext *h)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62 const char *path, *proxy_path;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63 char hostname[1024], hoststr[1024];
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
64 char auth[1024];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65 char path1[1024];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 char buf[1024];
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
67 int port, use_proxy, err, location_changed = 0, redirects = 0;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
68 HTTPContext *s = h->priv_data;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
69 URLContext *hd = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
70
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
71 proxy_path = getenv("http_proxy");
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
72 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 strstart(proxy_path, "http://", NULL);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 /* fill the dest addr */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 redo:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 /* needed in any case to build the host string */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
78 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
79 path1, sizeof(path1), s->location);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80 if (port > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 pstrcpy(hoststr, sizeof(hoststr), hostname);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
86 if (use_proxy) {
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
87 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88 NULL, 0, proxy_path);
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
89 path = s->location;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 if (path1[0] == '\0')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
92 path = "/";
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
94 path = path1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96 if (port < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 port = 80;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 err = url_open(&hd, buf, URL_RDWR);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 if (err < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 s->hd = hd;
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
105 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 goto fail;
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
107 if (s->http_code == 303 && location_changed == 1) {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 /* url moved, get next */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 url_close(hd);
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
110 if (redirects++ >= MAX_REDIRECTS)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
111 return AVERROR_IO;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
112 location_changed = 0;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 if (hd)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 url_close(hd);
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 385
diff changeset
119 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
120 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
121
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
122 static int http_open(URLContext *h, const char *uri, int flags)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
123 {
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
124 HTTPContext *s;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
125 int ret;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
126
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
127 h->is_streamed = 1;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
128
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
129 s = av_malloc(sizeof(HTTPContext));
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
130 if (!s) {
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
131 return -ENOMEM;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
132 }
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
133 h->priv_data = s;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
134 s->filesize = -1;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
135 s->off = 0;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
136 pstrcpy (s->location, URL_SIZE, uri);
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
137
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
138 ret = http_open_cnx(h);
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
139 if (ret != 0)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
140 av_free (s);
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
141 return ret;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
142 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 static int http_getc(HTTPContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145 int len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146 if (s->buf_ptr >= s->buf_end) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 if (len < 0) {
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 385
diff changeset
149 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150 } else if (len == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153 s->buf_ptr = s->buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 s->buf_end = s->buffer + len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 return *s->buf_ptr++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
160 static int process_line(URLContext *h, char *line, int line_count,
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
161 int *new_location)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162 {
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
163 HTTPContext *s = h->priv_data;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 char *tag, *p;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
165
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
166 /* end of header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
167 if (line[0] == '\0')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
168 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 p = line;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 if (line_count == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 while (!isspace(*p) && *p != '\0')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 while (isspace(*p))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176 s->http_code = strtol(p, NULL, 10);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
177 #ifdef DEBUG
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 printf("http_code=%d\n", s->http_code);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181 while (*p != '\0' && *p != ':')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 p++;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
183 if (*p != ':')
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 return 1;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
185
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 *p = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 tag = line;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 while (isspace(*p))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 if (!strcmp(tag, "Location")) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 strcpy(s->location, p);
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
193 *new_location = 1;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
194 } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
195 s->filesize = atoll(p);
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
196 } else if (!strcmp (tag, "Content-Range")) {
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
197 /* "bytes $from-$to/$document_size" */
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
198 const char *slash;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
199 if (!strncmp (p, "bytes ", 6)) {
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
200 p += 6;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
201 s->off = atoll(p);
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
202 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
203 s->filesize = atoll(slash+1);
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
204 }
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
205 h->is_streamed = 0; /* we _can_ in fact seek */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 return 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
211 static int http_connect(URLContext *h, const char *path, const char *hoststr,
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
212 const char *auth, int *new_location)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 HTTPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 int post, err, ch;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 char line[1024], *q;
1181
c2f51d81c72e Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents: 1180
diff changeset
217 char *auth_b64;
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
218 offset_t off = s->off;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221 /* send http header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 post = h->flags & URL_WRONLY;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223
1432
4f4bebc36aad switch to common base64 routines in lavf
gpoirier
parents: 1358
diff changeset
224 auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 snprintf(s->buffer, sizeof(s->buffer),
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
226 "%s %s HTTP/1.1\r\n"
385
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
227 "User-Agent: %s\r\n"
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
228 "Accept: */*\r\n"
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
229 "Range: bytes=%llu-\r\n"
385
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
230 "Host: %s\r\n"
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
231 "Authorization: Basic %s\r\n"
385
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
232 "\r\n",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 post ? "POST" : "GET",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 path,
327
741c56c608bb using LIBAVFORMAT_IDENT
alex
parents: 229
diff changeset
235 LIBAVFORMAT_IDENT,
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
236 s->off,
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
237 hoststr,
1181
c2f51d81c72e Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents: 1180
diff changeset
238 auth_b64);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
239
1181
c2f51d81c72e Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents: 1180
diff changeset
240 av_freep(&auth_b64);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 385
diff changeset
242 return AVERROR_IO;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
243
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 /* init input buffer */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 s->buf_ptr = s->buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 s->buf_end = s->buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247 s->line_count = 0;
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
248 s->off = 0;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 if (post) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250 sleep(1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
253
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
254 /* wait for header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 q = line;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
257 ch = http_getc(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 if (ch < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 385
diff changeset
259 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260 if (ch == '\n') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 /* process line */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 if (q > line && q[-1] == '\r')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 q--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265 #ifdef DEBUG
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266 printf("header='%s'\n", line);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 #endif
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
268 err = process_line(h, line, s->line_count, new_location);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269 if (err < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270 return err;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 if (err == 0)
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
272 break;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 s->line_count++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 q = line;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 if ((q - line) < sizeof(line) - 1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 *q++ = ch;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 }
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
280
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
281 return (off == s->off) ? 0 : -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 0
diff changeset
285 static int http_read(URLContext *h, uint8_t *buf, int size)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 HTTPContext *s = h->priv_data;
385
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
288 int len;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289
385
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
290 /* read bytes from input buffer first */
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
291 len = s->buf_end - s->buf_ptr;
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
292 if (len > 0) {
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
293 if (len > size)
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
294 len = size;
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
295 memcpy(buf, s->buf_ptr, len);
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
296 s->buf_ptr += len;
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
297 } else {
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
298 len = url_read(s->hd, buf, size);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299 }
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
300 if (len > 0)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
301 s->off += len;
385
2f56d366a787 no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents: 327
diff changeset
302 return len;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 /* used only when posting data */
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 0
diff changeset
306 static int http_write(URLContext *h, uint8_t *buf, int size)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
308 HTTPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309 return url_write(s->hd, buf, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
310 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312 static int http_close(URLContext *h)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 HTTPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315 url_close(s->hd);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316 av_free(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
317 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
318 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
319
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
320 static offset_t http_seek(URLContext *h, offset_t off, int whence)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
321 {
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
322 HTTPContext *s = h->priv_data;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
323 URLContext *old_hd = s->hd;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
324 offset_t old_off = s->off;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
325
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
326 if (whence == AVSEEK_SIZE)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
327 return s->filesize;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
328 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
329 return -1;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
330
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
331 /* we save the old context in case the seek fails */
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
332 s->hd = NULL;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
333 if (whence == SEEK_CUR)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
334 off += s->off;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
335 else if (whence == SEEK_END)
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
336 off += s->filesize;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
337 s->off = off;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
338
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
339 /* if it fails, continue on old connection */
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
340 if (http_open_cnx(h) < 0) {
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
341 s->hd = old_hd;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
342 s->off = old_off;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
343 return -1;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
344 }
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
345 url_close(old_hd);
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
346 return off;
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
347 }
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
348
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
349 URLProtocol http_protocol = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
350 "http",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
351 http_open,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
352 http_read,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
353 http_write,
1647
0a990429e524 add support for HTTP seeking
gpoirier
parents: 1432
diff changeset
354 http_seek,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
355 http_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
356 };