2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 /* XXX: POST protocol is not completly implemented because ffmpeg use
28 only a subset of it */
32 /* used for protocol handling */
33 #define BUFFER_SIZE 1024
35 #define MAX_REDIRECTS 8
39 unsigned char buffer
[BUFFER_SIZE
], *buf_ptr
, *buf_end
;
42 offset_t off
, filesize
;
43 char location
[URL_SIZE
];
46 static int http_connect(URLContext
*h
, const char *path
, const char *hoststr
,
47 const char *auth
, int *new_location
);
48 static int http_write(URLContext
*h
, uint8_t *buf
, int size
);
51 /* return non zero if error */
52 static int http_open_cnx(URLContext
*h
)
54 const char *path
, *proxy_path
;
55 char hostname
[1024], hoststr
[1024];
59 int port
, use_proxy
, err
, location_changed
= 0, redirects
= 0;
60 HTTPContext
*s
= h
->priv_data
;
61 URLContext
*hd
= NULL
;
63 proxy_path
= getenv("http_proxy");
64 use_proxy
= (proxy_path
!= NULL
) && !getenv("no_proxy") &&
65 strstart(proxy_path
, "http://", NULL
);
67 /* fill the dest addr */
69 /* needed in any case to build the host string */
70 url_split(NULL
, 0, auth
, sizeof(auth
), hostname
, sizeof(hostname
), &port
,
71 path1
, sizeof(path1
), s
->location
);
73 snprintf(hoststr
, sizeof(hoststr
), "%s:%d", hostname
, port
);
75 pstrcpy(hoststr
, sizeof(hoststr
), hostname
);
79 url_split(NULL
, 0, auth
, sizeof(auth
), hostname
, sizeof(hostname
), &port
,
91 snprintf(buf
, sizeof(buf
), "tcp://%s:%d", hostname
, port
);
92 err
= url_open(&hd
, buf
, URL_RDWR
);
97 if (http_connect(h
, path
, hoststr
, auth
, &location_changed
) < 0)
99 if ((s
->http_code
== 302 || s
->http_code
== 303) && location_changed
== 1) {
100 /* url moved, get next */
102 if (redirects
++ >= MAX_REDIRECTS
)
104 location_changed
= 0;
114 static int http_open(URLContext
*h
, const char *uri
, int flags
)
121 s
= av_malloc(sizeof(HTTPContext
));
123 return AVERROR(ENOMEM
);
128 pstrcpy (s
->location
, URL_SIZE
, uri
);
130 ret
= http_open_cnx(h
);
135 static int http_getc(HTTPContext
*s
)
138 if (s
->buf_ptr
>= s
->buf_end
) {
139 len
= url_read(s
->hd
, s
->buffer
, BUFFER_SIZE
);
142 } else if (len
== 0) {
145 s
->buf_ptr
= s
->buffer
;
146 s
->buf_end
= s
->buffer
+ len
;
149 return *s
->buf_ptr
++;
152 static int process_line(URLContext
*h
, char *line
, int line_count
,
155 HTTPContext
*s
= h
->priv_data
;
163 if (line_count
== 0) {
164 while (!isspace(*p
) && *p
!= '\0')
168 s
->http_code
= strtol(p
, NULL
, 10);
170 printf("http_code=%d\n", s
->http_code
);
172 /* error codes are 4xx and 5xx */
173 if (s
->http_code
>= 400 && s
->http_code
< 600)
176 while (*p
!= '\0' && *p
!= ':')
186 if (!strcmp(tag
, "Location")) {
187 strcpy(s
->location
, p
);
189 } else if (!strcmp (tag
, "Content-Length") && s
->filesize
== -1) {
190 s
->filesize
= atoll(p
);
191 } else if (!strcmp (tag
, "Content-Range")) {
192 /* "bytes $from-$to/$document_size" */
194 if (!strncmp (p
, "bytes ", 6)) {
197 if ((slash
= strchr(p
, '/')) && strlen(slash
) > 0)
198 s
->filesize
= atoll(slash
+1);
200 h
->is_streamed
= 0; /* we _can_ in fact seek */
206 static int http_connect(URLContext
*h
, const char *path
, const char *hoststr
,
207 const char *auth
, int *new_location
)
209 HTTPContext
*s
= h
->priv_data
;
213 offset_t off
= s
->off
;
216 /* send http header */
217 post
= h
->flags
& URL_WRONLY
;
219 auth_b64
= av_base64_encode((uint8_t *)auth
, strlen(auth
));
220 snprintf(s
->buffer
, sizeof(s
->buffer
),
224 "Range: bytes=%"PRId64
"-\r\n"
226 "Authorization: Basic %s\r\n"
227 "Connection: close\r\n"
229 post ?
"POST" : "GET",
237 if (http_write(h
, s
->buffer
, strlen(s
->buffer
)) < 0)
240 /* init input buffer */
241 s
->buf_ptr
= s
->buffer
;
242 s
->buf_end
= s
->buffer
;
251 /* wait for header */
259 if (q
> line
&& q
[-1] == '\r')
263 printf("header='%s'\n", line
);
265 err
= process_line(h
, line
, s
->line_count
, new_location
);
273 if ((q
- line
) < sizeof(line
) - 1)
278 return (off
== s
->off
) ?
0 : -1;
282 static int http_read(URLContext
*h
, uint8_t *buf
, int size
)
284 HTTPContext
*s
= h
->priv_data
;
287 /* read bytes from input buffer first */
288 len
= s
->buf_end
- s
->buf_ptr
;
292 memcpy(buf
, s
->buf_ptr
, len
);
295 len
= url_read(s
->hd
, buf
, size
);
302 /* used only when posting data */
303 static int http_write(URLContext
*h
, uint8_t *buf
, int size
)
305 HTTPContext
*s
= h
->priv_data
;
306 return url_write(s
->hd
, buf
, size
);
309 static int http_close(URLContext
*h
)
311 HTTPContext
*s
= h
->priv_data
;
317 static offset_t
http_seek(URLContext
*h
, offset_t off
, int whence
)
319 HTTPContext
*s
= h
->priv_data
;
320 URLContext
*old_hd
= s
->hd
;
321 offset_t old_off
= s
->off
;
323 if (whence
== AVSEEK_SIZE
)
325 else if ((s
->filesize
== -1 && whence
== SEEK_END
) || h
->is_streamed
)
328 /* we save the old context in case the seek fails */
330 if (whence
== SEEK_CUR
)
332 else if (whence
== SEEK_END
)
336 /* if it fails, continue on old connection */
337 if (http_open_cnx(h
) < 0) {
346 URLProtocol http_protocol
= {