annotate http.c @ 6163:2e0ee73855cd libavformat

Add an AVClass to the HTTPContext
author mstorsjo
date Tue, 22 Jun 2010 14:13:55 +0000
parents 4c0017a87855
children 72ea866c62fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
1 /*
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
2 * HTTP protocol for ffmpeg client
4251
77e0c7511d41 cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 3973
diff changeset
3 * Copyright (c) 2000, 2001 Fabrice Bellard
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
4 *
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
5 * This file is part of FFmpeg.
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
6 *
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
11 *
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
15 * Lesser General Public License for more details.
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
16 *
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
20 */
3286
6f61c3b36632 Use full path for #includes from another directory.
diego
parents: 3001
diff changeset
21
6f61c3b36632 Use full path for #includes from another directory.
diego
parents: 3001
diff changeset
22 #include "libavutil/avstring.h"
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
23 #include "avformat.h"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
24 #include <unistd.h>
5059
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
25 #include <strings.h>
5837
d605f589f0be move ff_url_split() and ff_url_join() declarations to internal.h
aurel
parents: 5776
diff changeset
26 #include "internal.h"
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
27 #include "network.h"
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
28 #include "http.h"
2774
477419a721a3 os_support.h is also needed for usleep and lseek on MinGW.
ramiro
parents: 2274
diff changeset
29 #include "os_support.h"
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
30 #include "httpauth.h"
6163
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
31 #include "libavcodec/opt.h"
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
32
1986
cf1622476eb7 typos/grammar
diego
parents: 1934
diff changeset
33 /* XXX: POST protocol is not completely implemented because ffmpeg uses
cf1622476eb7 typos/grammar
diego
parents: 1934
diff changeset
34 only a subset of it. */
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
35
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
36 /* used for protocol handling */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
37 #define BUFFER_SIZE 1024
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
38 #define URL_SIZE 4096
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
39 #define MAX_REDIRECTS 8
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
40
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
41 typedef struct {
6163
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
42 const AVClass *class;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
43 URLContext *hd;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
44 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
45 int line_count;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
46 int http_code;
5059
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
47 int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
3973
549a09cf23fe Remove offset_t typedef and use int64_t directly instead.
diego
parents: 3286
diff changeset
48 int64_t off, filesize;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
49 char location[URL_SIZE];
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
50 HTTPAuthState auth_state;
6103
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
51 int init;
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
52 unsigned char headers[BUFFER_SIZE];
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
53 } HTTPContext;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
54
6163
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
55 #define OFFSET(x) offsetof(HTTPContext, x)
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
56 static const AVOption options[] = {
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
57 {"chunksize", "use chunked transfer-encoding for posts, -1 disables it, 0 enables it", OFFSET(chunksize), FF_OPT_TYPE_INT64, 0, -1, 0 }, /* Default to 0, for chunked POSTs */
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
58 {NULL}
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
59 };
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
60 static const AVClass httpcontext_class = {
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
61 "HTTP", av_default_item_name, options, LIBAVUTIL_VERSION_INT
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
62 };
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
63
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
64 static int http_connect(URLContext *h, const char *path, const char *hoststr,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
65 const char *auth, int *new_location);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
66
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
67 void ff_http_set_headers(URLContext *h, const char *headers)
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
68 {
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
69 HTTPContext *s = h->priv_data;
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
70 int len = strlen(headers);
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
71
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
72 if (len && strcmp("\r\n", headers + len - 2))
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
73 av_log(NULL, AV_LOG_ERROR, "No trailing CRLF found in HTTP header.\n");
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
74
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
75 av_strlcpy(s->headers, headers, sizeof(s->headers));
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
76 }
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
77
6108
117b1826ce8e Add support for disabling chunked transfer encoding for the http protocol
mstorsjo
parents: 6107
diff changeset
78 void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked)
117b1826ce8e Add support for disabling chunked transfer encoding for the http protocol
mstorsjo
parents: 6107
diff changeset
79 {
6153
2e07dcbab954 HTTP: Get rid of the is_chunked variable, use the chunksize variable instead
mstorsjo
parents: 6152
diff changeset
80 ((HTTPContext*)h->priv_data)->chunksize = is_chunked ? 0 : -1;
6108
117b1826ce8e Add support for disabling chunked transfer encoding for the http protocol
mstorsjo
parents: 6107
diff changeset
81 }
117b1826ce8e Add support for disabling chunked transfer encoding for the http protocol
mstorsjo
parents: 6107
diff changeset
82
6156
85710c0ef275 HTTP: Add a method for initializing the authentication state from another connection
mstorsjo
parents: 6155
diff changeset
83 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
85710c0ef275 HTTP: Add a method for initializing the authentication state from another connection
mstorsjo
parents: 6155
diff changeset
84 {
85710c0ef275 HTTP: Add a method for initializing the authentication state from another connection
mstorsjo
parents: 6155
diff changeset
85 memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
85710c0ef275 HTTP: Add a method for initializing the authentication state from another connection
mstorsjo
parents: 6155
diff changeset
86 &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
85710c0ef275 HTTP: Add a method for initializing the authentication state from another connection
mstorsjo
parents: 6155
diff changeset
87 }
85710c0ef275 HTTP: Add a method for initializing the authentication state from another connection
mstorsjo
parents: 6155
diff changeset
88
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
89 /* return non zero if error */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
90 static int http_open_cnx(URLContext *h)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
91 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
92 const char *path, *proxy_path;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
93 char hostname[1024], hoststr[1024];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
94 char auth[1024];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
95 char path1[1024];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
96 char buf[1024];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
97 int port, use_proxy, err, location_changed = 0, redirects = 0;
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
98 HTTPAuthType cur_auth_type;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
99 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
100 URLContext *hd = NULL;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
101
6103
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
102 s->init = 1;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
103 proxy_path = getenv("http_proxy");
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
104 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
2193
5ce5fad0dfac replace the uses of old string functions that Reimar missed
mru
parents: 2189
diff changeset
105 av_strstart(proxy_path, "http://", NULL);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
106
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
107 /* fill the dest addr */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
108 redo:
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
109 /* needed in any case to build the host string */
5775
7a123cc24a81 Rename url_split to ff_url_split
mstorsjo
parents: 5756
diff changeset
110 ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
5776
d34f985d6e8f Reindent
mstorsjo
parents: 5775
diff changeset
111 path1, sizeof(path1), s->location);
5756
7c7fe75728dd Use ff_url_join for assembling URLs, instead of snprintf
mstorsjo
parents: 5544
diff changeset
112 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
113
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
114 if (use_proxy) {
5775
7a123cc24a81 Rename url_split to ff_url_split
mstorsjo
parents: 5756
diff changeset
115 ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
5776
d34f985d6e8f Reindent
mstorsjo
parents: 5775
diff changeset
116 NULL, 0, proxy_path);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
117 path = s->location;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
118 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
119 if (path1[0] == '\0')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
120 path = "/";
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
121 else
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
122 path = path1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
123 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
124 if (port < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
125 port = 80;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
126
5756
7c7fe75728dd Use ff_url_join for assembling URLs, instead of snprintf
mstorsjo
parents: 5544
diff changeset
127 ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
128 err = url_open(&hd, buf, URL_RDWR);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
129 if (err < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
130 goto fail;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
131
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
132 s->hd = hd;
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
133 cur_auth_type = s->auth_state.auth_type;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
134 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
135 goto fail;
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
136 if (s->http_code == 401) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
137 if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
138 url_close(hd);
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
139 goto redo;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
140 } else
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
141 goto fail;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
142 }
1852
467a411b5d26 make HTTP MOVED messages also work in ffmpeg (302 is essentially the same as 303).
gpoirier
parents: 1822
diff changeset
143 if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
144 /* url moved, get next */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
145 url_close(hd);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
146 if (redirects++ >= MAX_REDIRECTS)
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2193
diff changeset
147 return AVERROR(EIO);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
148 location_changed = 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
149 goto redo;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
150 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
151 return 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
152 fail:
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
153 if (hd)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
154 url_close(hd);
6107
5ad38b8596b2 Fix handling of errors in the http protocol
mstorsjo
parents: 6106
diff changeset
155 s->hd = NULL;
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2193
diff changeset
156 return AVERROR(EIO);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
157 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
158
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
159 static int http_open(URLContext *h, const char *uri, int flags)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
160 {
6162
4c0017a87855 Allocate the HTTPContext through URLProtocol.priv_data_size
mstorsjo
parents: 6156
diff changeset
161 HTTPContext *s = h->priv_data;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
162
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
163 h->is_streamed = 1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
164
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
165 s->filesize = -1;
2193
5ce5fad0dfac replace the uses of old string functions that Reimar missed
mru
parents: 2189
diff changeset
166 av_strlcpy(s->location, uri, URL_SIZE);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
167
6103
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
168 return 0;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
169 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
170 static int http_getc(HTTPContext *s)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
171 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
172 int len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
173 if (s->buf_ptr >= s->buf_end) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
174 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
175 if (len < 0) {
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2193
diff changeset
176 return AVERROR(EIO);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
177 } else if (len == 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
178 return -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
179 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
180 s->buf_ptr = s->buffer;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
181 s->buf_end = s->buffer + len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
182 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
183 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
184 return *s->buf_ptr++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
185 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
186
5007
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
187 static int http_get_line(HTTPContext *s, char *line, int line_size)
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
188 {
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
189 int ch;
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
190 char *q;
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
191
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
192 q = line;
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
193 for(;;) {
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
194 ch = http_getc(s);
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
195 if (ch < 0)
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
196 return AVERROR(EIO);
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
197 if (ch == '\n') {
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
198 /* process line */
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
199 if (q > line && q[-1] == '\r')
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
200 q--;
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
201 *q = '\0';
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
202
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
203 return 0;
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
204 } else {
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
205 if ((q - line) < line_size - 1)
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
206 *q++ = ch;
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
207 }
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
208 }
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
209 }
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
210
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
211 static int process_line(URLContext *h, char *line, int line_count,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
212 int *new_location)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
213 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
214 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
215 char *tag, *p;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
216
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
217 /* end of header */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
218 if (line[0] == '\0')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
219 return 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
220
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
221 p = line;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
222 if (line_count == 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
223 while (!isspace(*p) && *p != '\0')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
224 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
225 while (isspace(*p))
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
226 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
227 s->http_code = strtol(p, NULL, 10);
5009
822a36d7ac0a Cosmetics : Use dprintf instead of printf.
jai_menon
parents: 5008
diff changeset
228
822a36d7ac0a Cosmetics : Use dprintf instead of printf.
jai_menon
parents: 5008
diff changeset
229 dprintf(NULL, "http_code=%d\n", s->http_code);
822a36d7ac0a Cosmetics : Use dprintf instead of printf.
jai_menon
parents: 5008
diff changeset
230
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
231 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
232 * don't abort until all headers have been parsed. */
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
233 if (s->http_code >= 400 && s->http_code < 600 && s->http_code != 401)
1881
a7abdfdb5ed7 handle http error codes
alex
parents: 1853
diff changeset
234 return -1;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
235 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
236 while (*p != '\0' && *p != ':')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
237 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
238 if (*p != ':')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
239 return 1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
240
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
241 *p = '\0';
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
242 tag = line;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
243 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
244 while (isspace(*p))
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
245 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
246 if (!strcmp(tag, "Location")) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
247 strcpy(s->location, p);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
248 *new_location = 1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
249 } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
250 s->filesize = atoll(p);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
251 } else if (!strcmp (tag, "Content-Range")) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
252 /* "bytes $from-$to/$document_size" */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
253 const char *slash;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
254 if (!strncmp (p, "bytes ", 6)) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
255 p += 6;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
256 s->off = atoll(p);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
257 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
258 s->filesize = atoll(slash+1);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
259 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
260 h->is_streamed = 0; /* we _can_ in fact seek */
5059
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
261 } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
262 s->filesize = -1;
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
263 s->chunksize = 0;
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
264 } else if (!strcmp (tag, "WWW-Authenticate")) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
265 ff_http_auth_handle_header(&s->auth_state, tag, p);
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
266 } else if (!strcmp (tag, "Authentication-Info")) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
267 ff_http_auth_handle_header(&s->auth_state, tag, p);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
268 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
269 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
270 return 1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
271 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
272
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
273 static inline int has_header(const char *str, const char *header)
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
274 {
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
275 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
276 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
277 }
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
278
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
279 static int http_connect(URLContext *h, const char *path, const char *hoststr,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
280 const char *auth, int *new_location)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
281 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
282 HTTPContext *s = h->priv_data;
5007
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
283 int post, err;
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
284 char line[1024];
6106
44ee60d0c688 Fix bugs in the http custom header handling
mstorsjo
parents: 6104
diff changeset
285 char headers[1024] = "";
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
286 char *authstr = NULL;
3973
549a09cf23fe Remove offset_t typedef and use int64_t directly instead.
diego
parents: 3286
diff changeset
287 int64_t off = s->off;
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
288 int len = 0;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
289
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
290
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
291 /* send http header */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
292 post = h->flags & URL_WRONLY;
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
293 authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
294 post ? "POST" : "GET");
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
295
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
296 /* set default headers if needed */
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
297 if (!has_header(s->headers, "\r\nUser-Agent: "))
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
298 len += av_strlcatf(headers + len, sizeof(headers) - len,
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
299 "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
300 if (!has_header(s->headers, "\r\nAccept: "))
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
301 len += av_strlcpy(headers + len, "Accept: */*\r\n",
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
302 sizeof(headers) - len);
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
303 if (!has_header(s->headers, "\r\nRange: "))
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
304 len += av_strlcatf(headers + len, sizeof(headers) - len,
6106
44ee60d0c688 Fix bugs in the http custom header handling
mstorsjo
parents: 6104
diff changeset
305 "Range: bytes=%"PRId64"-\r\n", s->off);
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
306 if (!has_header(s->headers, "\r\nConnection: "))
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
307 len += av_strlcpy(headers + len, "Connection: close\r\n",
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
308 sizeof(headers)-len);
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
309 if (!has_header(s->headers, "\r\nHost: "))
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
310 len += av_strlcatf(headers + len, sizeof(headers) - len,
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
311 "Host: %s\r\n", hoststr);
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
312
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
313 /* now add in custom headers */
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
314 av_strlcpy(headers+len, s->headers, sizeof(headers)-len);
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
315
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
316 snprintf(s->buffer, sizeof(s->buffer),
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
317 "%s %s HTTP/1.1\r\n"
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
318 "%s"
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
319 "%s"
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
320 "%s"
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
321 "\r\n",
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
322 post ? "POST" : "GET",
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
323 path,
6153
2e07dcbab954 HTTP: Get rid of the is_chunked variable, use the chunksize variable instead
mstorsjo
parents: 6152
diff changeset
324 post && s->chunksize >= 0 ? "Transfer-Encoding: chunked\r\n" : "",
6104
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
325 headers,
a5eaf91edd4a Add the capability to write custom HTTP headers
mstorsjo
parents: 6103
diff changeset
326 authstr ? authstr : "");
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
327
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents: 5837
diff changeset
328 av_freep(&authstr);
6150
6b770ef6cbea Use url_write(), not http_write(), for sending the HTTP headers. This prevents
rbultje
parents: 6117
diff changeset
329 if (url_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2193
diff changeset
330 return AVERROR(EIO);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
331
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
332 /* init input buffer */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
333 s->buf_ptr = s->buffer;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
334 s->buf_end = s->buffer;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
335 s->line_count = 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
336 s->off = 0;
1909
663139d56669 Properly initialize filesize during input buffer initialization.
diego
parents: 1881
diff changeset
337 s->filesize = -1;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
338 if (post) {
6080
c7ddd67739a6 http: Set http_code to 200 when doing a POST
mstorsjo
parents: 6068
diff changeset
339 /* Pretend that it did work. We didn't read any header yet, since
c7ddd67739a6 http: Set http_code to 200 when doing a POST
mstorsjo
parents: 6068
diff changeset
340 * we've still to send the POST data, but the code calling this
c7ddd67739a6 http: Set http_code to 200 when doing a POST
mstorsjo
parents: 6068
diff changeset
341 * function will check http_code after we return. */
c7ddd67739a6 http: Set http_code to 200 when doing a POST
mstorsjo
parents: 6068
diff changeset
342 s->http_code = 200;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
343 return 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
344 }
6153
2e07dcbab954 HTTP: Get rid of the is_chunked variable, use the chunksize variable instead
mstorsjo
parents: 6152
diff changeset
345 s->chunksize = -1;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
346
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
347 /* wait for header */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
348 for(;;) {
5007
1d5f8836c8a8 Introduce http_get_line and modify http_connect to use http_get_line.
jai_menon
parents: 4640
diff changeset
349 if (http_get_line(s, line, sizeof(line)) < 0)
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2193
diff changeset
350 return AVERROR(EIO);
5009
822a36d7ac0a Cosmetics : Use dprintf instead of printf.
jai_menon
parents: 5008
diff changeset
351
822a36d7ac0a Cosmetics : Use dprintf instead of printf.
jai_menon
parents: 5008
diff changeset
352 dprintf(NULL, "header='%s'\n", line);
822a36d7ac0a Cosmetics : Use dprintf instead of printf.
jai_menon
parents: 5008
diff changeset
353
5008
370160e040d9 Cosmetics : Fix indentation after last commit.
jai_menon
parents: 5007
diff changeset
354 err = process_line(h, line, s->line_count, new_location);
370160e040d9 Cosmetics : Fix indentation after last commit.
jai_menon
parents: 5007
diff changeset
355 if (err < 0)
370160e040d9 Cosmetics : Fix indentation after last commit.
jai_menon
parents: 5007
diff changeset
356 return err;
370160e040d9 Cosmetics : Fix indentation after last commit.
jai_menon
parents: 5007
diff changeset
357 if (err == 0)
370160e040d9 Cosmetics : Fix indentation after last commit.
jai_menon
parents: 5007
diff changeset
358 break;
370160e040d9 Cosmetics : Fix indentation after last commit.
jai_menon
parents: 5007
diff changeset
359 s->line_count++;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
360 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
361
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
362 return (off == s->off) ? 0 : -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
363 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
364
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
365
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
366 static int http_read(URLContext *h, uint8_t *buf, int size)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
367 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
368 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
369 int len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
370
6103
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
371 if (!s->init) {
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
372 int ret = http_open_cnx(h);
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
373 if (ret != 0)
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
374 return ret;
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
375 }
6117
f221641c262a Make sure the http protocol handler returns errors if a delayed open had failed
mstorsjo
parents: 6115
diff changeset
376 if (!s->hd)
f221641c262a Make sure the http protocol handler returns errors if a delayed open had failed
mstorsjo
parents: 6115
diff changeset
377 return AVERROR(EIO);
6103
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
378
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
379 /* A size of zero can be used to force
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
380 * initializaton of the connection. */
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
381 if (!size)
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
382 return 0;
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
383
5059
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
384 if (s->chunksize >= 0) {
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
385 if (!s->chunksize) {
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
386 char line[32];
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
387
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
388 for(;;) {
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
389 do {
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
390 if (http_get_line(s, line, sizeof(line)) < 0)
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
391 return AVERROR(EIO);
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
392 } while (!*line); /* skip CR LF from last chunk */
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
393
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
394 s->chunksize = strtoll(line, NULL, 16);
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
395
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
396 dprintf(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
397
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
398 if (!s->chunksize)
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
399 return 0;
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
400 break;
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
401 }
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
402 }
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
403 size = FFMIN(size, s->chunksize);
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
404 }
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
405 /* read bytes from input buffer first */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
406 len = s->buf_end - s->buf_ptr;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
407 if (len > 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
408 if (len > size)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
409 len = size;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
410 memcpy(buf, s->buf_ptr, len);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
411 s->buf_ptr += len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
412 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
413 len = url_read(s->hd, buf, size);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
414 }
5059
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
415 if (len > 0) {
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
416 s->off += len;
5059
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
417 if (s->chunksize > 0)
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
418 s->chunksize -= len;
a5b8b8cce2dd Add support for "chunked" data blocks. Patch by Peter Holik (peter holik at).
rbultje
parents: 5009
diff changeset
419 }
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
420 return len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
421 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
422
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
423 /* used only when posting data */
6068
7fdda2416684 Declare the url_write buffer parameter as const
mstorsjo
parents: 5879
diff changeset
424 static int http_write(URLContext *h, const uint8_t *buf, int size)
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
425 {
6108
117b1826ce8e Add support for disabling chunked transfer encoding for the http protocol
mstorsjo
parents: 6107
diff changeset
426 char temp[11] = ""; /* 32-bit hex + CRLF + nul */
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
427 int ret;
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
428 char crlf[] = "\r\n";
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
429 HTTPContext *s = h->priv_data;
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
430
6103
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
431 if (!s->init) {
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
432 int ret = http_open_cnx(h);
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
433 if (ret != 0)
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
434 return ret;
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
435 }
6117
f221641c262a Make sure the http protocol handler returns errors if a delayed open had failed
mstorsjo
parents: 6115
diff changeset
436 if (!s->hd)
f221641c262a Make sure the http protocol handler returns errors if a delayed open had failed
mstorsjo
parents: 6115
diff changeset
437 return AVERROR(EIO);
6103
447fe3440991 Modify the behaviour of http_open to implicitly delay connection establishment
mstorsjo
parents: 6080
diff changeset
438
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
439 if (s->chunksize == -1) {
6152
b9dee5077174 HTTP: Clarify a comment
mstorsjo
parents: 6151
diff changeset
440 /* non-chunked data is sent without any special encoding */
5527
2d0a0d3e5df1 Reindent after r21166.
rbultje
parents: 5526
diff changeset
441 return url_write(s->hd, buf, size);
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
442 }
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
443
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
444 /* silently ignore zero-size data since chunk encoding that would
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
445 * signal EOF */
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
446 if (size > 0) {
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
447 /* upload data using chunked encoding */
6155
a7094b61b3fb Reindent
mstorsjo
parents: 6154
diff changeset
448 snprintf(temp, sizeof(temp), "%x\r\n", size);
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
449
6154
78f96a0ef08c HTTP: Compact the code for writing chunked post data
mstorsjo
parents: 6153
diff changeset
450 if ((ret = url_write(s->hd, temp, strlen(temp))) < 0 ||
78f96a0ef08c HTTP: Compact the code for writing chunked post data
mstorsjo
parents: 6153
diff changeset
451 (ret = url_write(s->hd, buf, size)) < 0 ||
78f96a0ef08c HTTP: Compact the code for writing chunked post data
mstorsjo
parents: 6153
diff changeset
452 (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
453 return ret;
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
454 }
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
455 return size;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
456 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
457
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
458 static int http_close(URLContext *h)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
459 {
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
460 int ret = 0;
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
461 char footer[] = "0\r\n\r\n";
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
462 HTTPContext *s = h->priv_data;
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
463
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
464 /* signal end of chunked encoding if used */
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
465 if ((h->flags & URL_WRONLY) && s->chunksize != -1) {
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
466 ret = url_write(s->hd, footer, sizeof(footer) - 1);
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
467 ret = ret > 0 ? 0 : ret;
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
468 }
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
469
6107
5ad38b8596b2 Fix handling of errors in the http protocol
mstorsjo
parents: 6106
diff changeset
470 if (s->hd)
5ad38b8596b2 Fix handling of errors in the http protocol
mstorsjo
parents: 6106
diff changeset
471 url_close(s->hd);
5526
f09594ca5f77 Use chunked encoding for HTTP uploads. Patch by Tomas H¸«£rdin
rbultje
parents: 5059
diff changeset
472 return ret;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
473 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
474
3973
549a09cf23fe Remove offset_t typedef and use int64_t directly instead.
diego
parents: 3286
diff changeset
475 static int64_t http_seek(URLContext *h, int64_t off, int whence)
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
476 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
477 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
478 URLContext *old_hd = s->hd;
3973
549a09cf23fe Remove offset_t typedef and use int64_t directly instead.
diego
parents: 3286
diff changeset
479 int64_t old_off = s->off;
5544
fc48f8bfbf2e restore old buffer content when seek failed in http protocol, fix issue #1631
bcoudurier
parents: 5527
diff changeset
480 uint8_t old_buf[BUFFER_SIZE];
fc48f8bfbf2e restore old buffer content when seek failed in http protocol, fix issue #1631
bcoudurier
parents: 5527
diff changeset
481 int old_buf_size;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
482
6115
4c91cdcb8a52 Initialize the http connection in http_seek, too
mstorsjo
parents: 6110
diff changeset
483 if (!s->init) {
4c91cdcb8a52 Initialize the http connection in http_seek, too
mstorsjo
parents: 6110
diff changeset
484 int ret = http_open_cnx(h);
4c91cdcb8a52 Initialize the http connection in http_seek, too
mstorsjo
parents: 6110
diff changeset
485 if (ret != 0)
4c91cdcb8a52 Initialize the http connection in http_seek, too
mstorsjo
parents: 6110
diff changeset
486 return ret;
4c91cdcb8a52 Initialize the http connection in http_seek, too
mstorsjo
parents: 6110
diff changeset
487 }
6117
f221641c262a Make sure the http protocol handler returns errors if a delayed open had failed
mstorsjo
parents: 6115
diff changeset
488 if (!s->hd)
f221641c262a Make sure the http protocol handler returns errors if a delayed open had failed
mstorsjo
parents: 6115
diff changeset
489 return AVERROR(EIO);
6115
4c91cdcb8a52 Initialize the http connection in http_seek, too
mstorsjo
parents: 6110
diff changeset
490
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
491 if (whence == AVSEEK_SIZE)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
492 return s->filesize;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
493 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
494 return -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
495
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
496 /* we save the old context in case the seek fails */
5544
fc48f8bfbf2e restore old buffer content when seek failed in http protocol, fix issue #1631
bcoudurier
parents: 5527
diff changeset
497 old_buf_size = s->buf_end - s->buf_ptr;
fc48f8bfbf2e restore old buffer content when seek failed in http protocol, fix issue #1631
bcoudurier
parents: 5527
diff changeset
498 memcpy(old_buf, s->buf_ptr, old_buf_size);
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
499 s->hd = NULL;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
500 if (whence == SEEK_CUR)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
501 off += s->off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
502 else if (whence == SEEK_END)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
503 off += s->filesize;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
504 s->off = off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
505
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
506 /* if it fails, continue on old connection */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
507 if (http_open_cnx(h) < 0) {
5544
fc48f8bfbf2e restore old buffer content when seek failed in http protocol, fix issue #1631
bcoudurier
parents: 5527
diff changeset
508 memcpy(s->buffer, old_buf, old_buf_size);
fc48f8bfbf2e restore old buffer content when seek failed in http protocol, fix issue #1631
bcoudurier
parents: 5527
diff changeset
509 s->buf_ptr = s->buffer;
fc48f8bfbf2e restore old buffer content when seek failed in http protocol, fix issue #1631
bcoudurier
parents: 5527
diff changeset
510 s->buf_end = s->buffer + old_buf_size;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
511 s->hd = old_hd;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
512 s->off = old_off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
513 return -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
514 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
515 url_close(old_hd);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
516 return off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
517 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
518
4640
b34d9614b887 Add url_get_file_handle(), which is used to get the file descriptor
rbultje
parents: 4630
diff changeset
519 static int
b34d9614b887 Add url_get_file_handle(), which is used to get the file descriptor
rbultje
parents: 4630
diff changeset
520 http_get_file_handle(URLContext *h)
b34d9614b887 Add url_get_file_handle(), which is used to get the file descriptor
rbultje
parents: 4630
diff changeset
521 {
b34d9614b887 Add url_get_file_handle(), which is used to get the file descriptor
rbultje
parents: 4630
diff changeset
522 HTTPContext *s = h->priv_data;
b34d9614b887 Add url_get_file_handle(), which is used to get the file descriptor
rbultje
parents: 4630
diff changeset
523 return url_get_file_handle(s->hd);
b34d9614b887 Add url_get_file_handle(), which is used to get the file descriptor
rbultje
parents: 4630
diff changeset
524 }
b34d9614b887 Add url_get_file_handle(), which is used to get the file descriptor
rbultje
parents: 4630
diff changeset
525
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
526 URLProtocol http_protocol = {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
527 "http",
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
528 http_open,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
529 http_read,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
530 http_write,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
531 http_seek,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
532 http_close,
4640
b34d9614b887 Add url_get_file_handle(), which is used to get the file descriptor
rbultje
parents: 4630
diff changeset
533 .url_get_file_handle = http_get_file_handle,
6162
4c0017a87855 Allocate the HTTPContext through URLProtocol.priv_data_size
mstorsjo
parents: 6156
diff changeset
534 .priv_data_size = sizeof(HTTPContext),
6163
2e0ee73855cd Add an AVClass to the HTTPContext
mstorsjo
parents: 6162
diff changeset
535 .priv_data_class = &httpcontext_class,
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
536 };