annotate http.c @ 1340:79f90a950fe1 libavformat

add string param to PRINT_KEY for debug
author bcoudurier
date Fri, 29 Sep 2006 11:25:40 +0000
parents c2f51d81c72e
children 0899bfe4105c
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
896
edbe5c3717f9 Update licensing information: The FSF changed postal address.
diego
parents: 885
diff changeset
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0
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");
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
76 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
0
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 */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
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) {
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
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;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
145
0
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++;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
163 if (*p != ':')
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 return 1;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
165
0
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;
1181
c2f51d81c72e Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents: 1180
diff changeset
184 char *auth_b64;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 /* send http header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 post = h->flags & URL_WRONLY;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189
1181
c2f51d81c72e Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents: 1180
diff changeset
190 auth_b64 = b64_encode(auth);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 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
192 "%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
193 "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
194 "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
195 "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
196 "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
197 "\r\n",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 post ? "POST" : "GET",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 path,
327
741c56c608bb using LIBAVFORMAT_IDENT
alex
parents: 229
diff changeset
200 LIBAVFORMAT_IDENT,
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
201 hoststr,
1181
c2f51d81c72e Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents: 1180
diff changeset
202 auth_b64);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
203
1181
c2f51d81c72e Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents: 1180
diff changeset
204 av_freep(&auth_b64);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 385
diff changeset
206 return AVERROR_IO;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
207
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 /* init input buffer */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 s->buf_ptr = s->buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210 s->buf_end = s->buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211 s->line_count = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 s->location[0] = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 if (post) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 sleep(1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
217
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 /* wait for header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 q = line;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221 ch = http_getc(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 if (ch < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 385
diff changeset
223 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224 if (ch == '\n') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 /* process line */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226 if (q > line && q[-1] == '\r')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
227 q--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 #ifdef DEBUG
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230 printf("header='%s'\n", line);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
232 err = process_line(s, line, s->line_count);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 if (err < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 return err;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
235 if (err == 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237 s->line_count++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
238 q = line;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
239 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240 if ((q - line) < sizeof(line) - 1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 *q++ = ch;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 0
diff changeset
247 static int http_read(URLContext *h, uint8_t *buf, int size)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 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
250 int len;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251
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
252 /* 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
253 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
254 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
255 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
256 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
257 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
258 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
259 } 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
260 len = url_read(s->hd, buf, size);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 }
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
262 return len;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265 /* used only when posting data */
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 0
diff changeset
266 static int http_write(URLContext *h, uint8_t *buf, int size)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 HTTPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269 return url_write(s->hd, buf, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 static int http_close(URLContext *h)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 HTTPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 url_close(s->hd);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 av_free(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280 URLProtocol http_protocol = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
281 "http",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 http_open,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283 http_read,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 http_write,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285 NULL, /* seek */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 http_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
289 /*****************************************************************************
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
290 * b64_encode: stolen from VLC's http.c
1180
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
291 * simplified by michael
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
292 *****************************************************************************/
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
293
683
095009fc2f35 kill warnings patch by (Mns Rullgrd <mru inprovide com>)
michael
parents: 639
diff changeset
294 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
295 {
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
296 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
639
0b52743104ac integer overflows, heap corruption
michael
parents: 511
diff changeset
297 unsigned int len= strlen(src);
0b52743104ac integer overflows, heap corruption
michael
parents: 511
diff changeset
298 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
299 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
300 unsigned i_shift = 0;
639
0b52743104ac integer overflows, heap corruption
michael
parents: 511
diff changeset
301
0b52743104ac integer overflows, heap corruption
michael
parents: 511
diff changeset
302 if(len < UINT_MAX/4){
0b52743104ac integer overflows, heap corruption
michael
parents: 511
diff changeset
303 ret=dst= av_malloc( len * 4 / 3 + 12 );
0b52743104ac integer overflows, heap corruption
michael
parents: 511
diff changeset
304 }else
0b52743104ac integer overflows, heap corruption
michael
parents: 511
diff changeset
305 return NULL;
0b52743104ac integer overflows, heap corruption
michael
parents: 511
diff changeset
306
1180
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
307 while(*src){
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
308 i_bits = (i_bits << 8) + *src++;
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
309 i_shift += 8;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
310
1180
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
311 do{
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
312 *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
511
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;
1180
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
314 }while( i_shift > 6 || (*src == 0 && i_shift>0));
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
315 }
1180
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
316 *dst++ = '=';
92fdb5e2a2d7 simplify b64_encode()
michael
parents: 896
diff changeset
317 *dst = '\0';
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 683
diff changeset
318
511
056991ab9f10 HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents: 482
diff changeset
319 return ret;
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