2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
27 # include <arpa/inet.h>
29 # include "barpainet.h"
35 /* XXX: POST protocol is not completly implemented because ffmpeg use
36 only a subset of it */
40 /* used for protocol handling */
41 #define BUFFER_SIZE 1024
43 #define MAX_REDIRECTS 8
47 unsigned char buffer
[BUFFER_SIZE
], *buf_ptr
, *buf_end
;
50 offset_t off
, filesize
;
51 char location
[URL_SIZE
];
54 static int http_connect(URLContext
*h
, const char *path
, const char *hoststr
,
55 const char *auth
, int *new_location
);
56 static int http_write(URLContext
*h
, uint8_t *buf
, int size
);
59 /* return non zero if error */
60 static int http_open_cnx(URLContext
*h
)
62 const char *path
, *proxy_path
;
63 char hostname
[1024], hoststr
[1024];
67 int port
, use_proxy
, err
, location_changed
= 0, redirects
= 0;
68 HTTPContext
*s
= h
->priv_data
;
69 URLContext
*hd
= NULL
;
71 proxy_path
= getenv("http_proxy");
72 use_proxy
= (proxy_path
!= NULL
) && !getenv("no_proxy") &&
73 strstart(proxy_path
, "http://", NULL
);
75 /* fill the dest addr */
77 /* needed in any case to build the host string */
78 url_split(NULL
, 0, auth
, sizeof(auth
), hostname
, sizeof(hostname
), &port
,
79 path1
, sizeof(path1
), s
->location
);
81 snprintf(hoststr
, sizeof(hoststr
), "%s:%d", hostname
, port
);
83 pstrcpy(hoststr
, sizeof(hoststr
), hostname
);
87 url_split(NULL
, 0, auth
, sizeof(auth
), hostname
, sizeof(hostname
), &port
,
99 snprintf(buf
, sizeof(buf
), "tcp://%s:%d", hostname
, port
);
100 err
= url_open(&hd
, buf
, URL_RDWR
);
105 if (http_connect(h
, path
, hoststr
, auth
, &location_changed
) < 0)
107 if (s
->http_code
== 303 && location_changed
== 1) {
108 /* url moved, get next */
110 if (redirects
++ >= MAX_REDIRECTS
)
112 location_changed
= 0;
122 static int http_open(URLContext
*h
, const char *uri
, int flags
)
129 s
= av_malloc(sizeof(HTTPContext
));
136 pstrcpy (s
->location
, URL_SIZE
, uri
);
138 ret
= http_open_cnx(h
);
143 static int http_getc(HTTPContext
*s
)
146 if (s
->buf_ptr
>= s
->buf_end
) {
147 len
= url_read(s
->hd
, s
->buffer
, BUFFER_SIZE
);
150 } else if (len
== 0) {
153 s
->buf_ptr
= s
->buffer
;
154 s
->buf_end
= s
->buffer
+ len
;
157 return *s
->buf_ptr
++;
160 static int process_line(URLContext
*h
, char *line
, int line_count
,
163 HTTPContext
*s
= h
->priv_data
;
171 if (line_count
== 0) {
172 while (!isspace(*p
) && *p
!= '\0')
176 s
->http_code
= strtol(p
, NULL
, 10);
178 printf("http_code=%d\n", s
->http_code
);
181 while (*p
!= '\0' && *p
!= ':')
191 if (!strcmp(tag
, "Location")) {
192 strcpy(s
->location
, p
);
194 } else if (!strcmp (tag
, "Content-Length") && s
->filesize
== -1) {
195 s
->filesize
= atoll(p
);
196 } else if (!strcmp (tag
, "Content-Range")) {
197 /* "bytes $from-$to/$document_size" */
199 if (!strncmp (p
, "bytes ", 6)) {
202 if ((slash
= strchr(p
, '/')) && strlen(slash
) > 0)
203 s
->filesize
= atoll(slash
+1);
205 h
->is_streamed
= 0; /* we _can_ in fact seek */
211 static int http_connect(URLContext
*h
, const char *path
, const char *hoststr
,
212 const char *auth
, int *new_location
)
214 HTTPContext
*s
= h
->priv_data
;
218 offset_t off
= s
->off
;
221 /* send http header */
222 post
= h
->flags
& URL_WRONLY
;
224 auth_b64
= av_base64_encode((uint8_t *)auth
, strlen(auth
));
225 snprintf(s
->buffer
, sizeof(s
->buffer
),
229 "Range: bytes=%llu-\r\n"
231 "Authorization: Basic %s\r\n"
233 post ?
"POST" : "GET",
241 if (http_write(h
, s
->buffer
, strlen(s
->buffer
)) < 0)
244 /* init input buffer */
245 s
->buf_ptr
= s
->buffer
;
246 s
->buf_end
= s
->buffer
;
254 /* wait for header */
262 if (q
> line
&& q
[-1] == '\r')
266 printf("header='%s'\n", line
);
268 err
= process_line(h
, line
, s
->line_count
, new_location
);
276 if ((q
- line
) < sizeof(line
) - 1)
281 return (off
== s
->off
) ?
0 : -1;
285 static int http_read(URLContext
*h
, uint8_t *buf
, int size
)
287 HTTPContext
*s
= h
->priv_data
;
290 /* read bytes from input buffer first */
291 len
= s
->buf_end
- s
->buf_ptr
;
295 memcpy(buf
, s
->buf_ptr
, len
);
298 len
= url_read(s
->hd
, buf
, size
);
305 /* used only when posting data */
306 static int http_write(URLContext
*h
, uint8_t *buf
, int size
)
308 HTTPContext
*s
= h
->priv_data
;
309 return url_write(s
->hd
, buf
, size
);
312 static int http_close(URLContext
*h
)
314 HTTPContext
*s
= h
->priv_data
;
320 static offset_t
http_seek(URLContext
*h
, offset_t off
, int whence
)
322 HTTPContext
*s
= h
->priv_data
;
323 URLContext
*old_hd
= s
->hd
;
324 offset_t old_off
= s
->off
;
326 if (whence
== AVSEEK_SIZE
)
328 else if ((s
->filesize
== -1 && whence
== SEEK_END
) || h
->is_streamed
)
331 /* we save the old context in case the seek fails */
333 if (whence
== SEEK_CUR
)
335 else if (whence
== SEEK_END
)
339 /* if it fails, continue on old connection */
340 if (http_open_cnx(h
) < 0) {
349 URLProtocol http_protocol
= {