2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
29 /* XXX: POST protocol is not completly implemented because ffmpeg use
30 only a subset of it */
34 /* used for protocol handling */
35 #define BUFFER_SIZE 1024
40 unsigned char buffer
[BUFFER_SIZE
], *buf_ptr
, *buf_end
;
43 char location
[URL_SIZE
];
46 static int http_connect(URLContext
*h
, const char *path
, const char *hoststr
);
47 static int http_write(URLContext
*h
, UINT8
*buf
, int size
);
50 /* return non zero if error */
51 static int http_open(URLContext
*h
, const char *uri
, int flags
)
53 const char *path
, *proxy_path
;
54 char hostname
[1024], hoststr
[1024];
57 int port
, use_proxy
, err
;
59 URLContext
*hd
= NULL
;
63 s
= av_malloc(sizeof(HTTPContext
));
69 proxy_path
= getenv("http_proxy");
70 use_proxy
= (proxy_path
!= NULL
) && !getenv("no_proxy") &&
71 strstart(proxy_path
, "http://", NULL
);
73 /* fill the dest addr */
75 /* needed in any case to build the host string */
76 url_split(NULL
, 0, hostname
, sizeof(hostname
), &port
,
77 path1
, sizeof(path1
), uri
);
79 snprintf(hoststr
, sizeof(hoststr
), "%s:%d", hostname
, port
);
81 pstrcpy(hoststr
, sizeof(hoststr
), hostname
);
85 url_split(NULL
, 0, hostname
, sizeof(hostname
), &port
,
97 snprintf(buf
, sizeof(buf
), "tcp://%s:%d", hostname
, port
);
98 err
= url_open(&hd
, buf
, URL_RDWR
);
103 if (http_connect(h
, path
, hoststr
) < 0)
105 if (s
->http_code
== 303 && s
->location
[0] != '\0') {
106 /* url moved, get next */
119 static int http_getc(HTTPContext
*s
)
122 if (s
->buf_ptr
>= s
->buf_end
) {
123 len
= url_read(s
->hd
, s
->buffer
, BUFFER_SIZE
);
126 } else if (len
== 0) {
129 s
->buf_ptr
= s
->buffer
;
130 s
->buf_end
= s
->buffer
+ len
;
133 return *s
->buf_ptr
++;
136 static int process_line(HTTPContext
*s
, char *line
, int line_count
)
145 if (line_count
== 0) {
146 while (!isspace(*p
) && *p
!= '\0')
150 s
->http_code
= strtol(p
, NULL
, 10);
152 printf("http_code=%d\n", s
->http_code
);
155 while (*p
!= '\0' && *p
!= ':')
165 if (!strcmp(tag
, "Location")) {
166 strcpy(s
->location
, p
);
172 static int http_connect(URLContext
*h
, const char *path
, const char *hoststr
)
174 HTTPContext
*s
= h
->priv_data
;
179 /* send http header */
180 post
= h
->flags
& URL_WRONLY
;
182 snprintf(s
->buffer
, sizeof(s
->buffer
),
184 "User-Agent: FFmpeg %s\n"
188 post ?
"POST" : "GET",
193 if (http_write(h
, s
->buffer
, strlen(s
->buffer
)) < 0)
196 /* init input buffer */
197 s
->buf_ptr
= s
->buffer
;
198 s
->buf_end
= s
->buffer
;
200 s
->location
[0] = '\0';
206 /* wait for header */
214 if (q
> line
&& q
[-1] == '\r')
218 printf("header='%s'\n", line
);
220 err
= process_line(s
, line
, s
->line_count
);
228 if ((q
- line
) < sizeof(line
) - 1)
235 static int http_read(URLContext
*h
, UINT8
*buf
, int size
)
237 HTTPContext
*s
= h
->priv_data
;
242 /* read bytes from input buffer first */
243 len
= s
->buf_end
- s
->buf_ptr
;
247 memcpy(buf
, s
->buf_ptr
, len
);
250 len
= url_read (s
->hd
, buf
, size
);
253 } else if (len
== 0) {
263 /* used only when posting data */
264 static int http_write(URLContext
*h
, UINT8
*buf
, int size
)
266 HTTPContext
*s
= h
->priv_data
;
267 return url_write(s
->hd
, buf
, size
);
270 static int http_close(URLContext
*h
)
272 HTTPContext
*s
= h
->priv_data
;
278 URLProtocol http_protocol
= {