annotate http.c @ 774:820863425158 libavformat

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