1 /*****************************************************************************
2 * file_test.c: HTTP file download test
3 *****************************************************************************
4 * Copyright (C) 2015 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
32 #include <vlc_common.h>
38 static const char url
[] = "https://www.example.com:8443/dir/file.ext?a=b";
39 static const char url_http
[] = "http://www.example.com:8443/dir/file.ext?a=b";
40 static const char url_mmsh
[] = "mmsh://www.example.com:8443/dir/file.ext?a=b";
41 static const char url_icyx
[] = "icyx://www.example.com:8443/dir/file.ext?a=b";
42 static const char ua
[] = PACKAGE_NAME
"/" PACKAGE_VERSION
" (test suite)";
44 static const char *replies
[2] = { NULL
, NULL
};
45 static uintmax_t offset
= 0;
46 static bool secure
= true;
47 static bool etags
= false;
50 static vlc_http_cookie_jar_t
*jar
;
54 struct vlc_http_resource
*f
;
57 jar
= vlc_http_cookies_new();
59 /* Request failure test */
60 f
= vlc_http_file_create(NULL
, url
, ua
, NULL
);
62 vlc_http_res_set_login(f
, NULL
, NULL
);
63 vlc_http_res_set_login(f
, "john", NULL
);
64 vlc_http_res_set_login(f
, NULL
, NULL
);
65 vlc_http_res_set_login(f
, "john", "secret");
66 vlc_http_res_set_login(f
, NULL
, NULL
);
67 vlc_http_file_seek(f
, 0);
68 assert(vlc_http_file_get_status(f
) < 0);
69 assert(vlc_http_file_get_redirect(f
) == NULL
);
70 assert(vlc_http_file_get_size(f
) == (uintmax_t)-1);
71 assert(!vlc_http_file_can_seek(f
));
72 assert(vlc_http_file_get_type(f
) == NULL
);
73 assert(vlc_http_file_read(f
) == NULL
);
74 vlc_http_res_destroy(f
);
76 /* Non-seekable stream test */
77 replies
[0] = "HTTP/1.1 200 OK\r\n"
78 "ETag: \"foobar42\"\r\n"
79 "Content-Type: video/mpeg\r\n"
84 f
= vlc_http_file_create(NULL
, url
, ua
, NULL
);
86 assert(vlc_http_file_get_status(f
) == 200);
87 assert(!vlc_http_file_can_seek(f
));
88 assert(vlc_http_file_get_size(f
) == (uintmax_t)-1);
89 str
= vlc_http_file_get_type(f
);
90 assert(str
!= NULL
&& !strcmp(str
, "video/mpeg"));
94 replies
[0] = "HTTP/1.1 200 OK\r\nETag: \"foobar42\"\r\n\r\n";
96 assert(vlc_http_file_seek(f
, offset
= 1234) < 0);
97 vlc_http_file_destroy(f
);
99 /* Seekable file test */
100 replies
[0] = "HTTP/1.1 206 Partial Content\r\n"
101 "Content-Range: bytes 0-2344/2345\r\n"
102 "ETag: W/\"foobar42\"\r\n"
103 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
107 f
= vlc_http_file_create(NULL
, url
, ua
, NULL
);
109 assert(vlc_http_file_can_seek(f
));
110 assert(vlc_http_file_get_size(f
) == 2345);
111 assert(vlc_http_file_read(f
) == NULL
);
114 replies
[0] = "HTTP/1.1 206 Partial Content\r\n"
115 "Content-Range: bytes 1234-3455/3456\r\n"
116 "ETag: W/\"foobar42\"\r\n"
117 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
119 assert(vlc_http_file_seek(f
, offset
= 1234) == 0);
120 assert(vlc_http_file_can_seek(f
));
121 assert(vlc_http_file_get_size(f
) == 3456);
122 assert(vlc_http_file_read(f
) == NULL
);
125 replies
[0] = "HTTP/1.1 416 Range Not Satisfiable\r\n"
126 "Content-Range: bytes */4567\r\n"
127 "ETag: W/\"foobar42\"\r\n"
128 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
130 vlc_http_file_seek(f
, offset
= 5678);
131 assert(vlc_http_file_can_seek(f
));
132 assert(vlc_http_file_get_size(f
) == 4567);
133 assert(vlc_http_file_read(f
) == NULL
);
134 vlc_http_file_destroy(f
);
137 replies
[0] = "HTTP/1.1 301 Permanent Redirect\r\n"
138 "Location: /somewhere/else/#here\r\n"
142 f
= vlc_http_file_create(NULL
, url
, ua
, NULL
);
144 assert(!vlc_http_file_can_seek(f
));
145 assert(vlc_http_file_get_size(f
) == (uintmax_t)-1);
146 str
= vlc_http_file_get_redirect(f
);
148 assert(!strcmp(str
, "https://www.example.com:8443/somewhere/else/"));
150 vlc_http_file_destroy(f
);
153 replies
[0] = "HTTP/1.1 100 Standby\r\n"
155 replies
[1] = "HTTP/1.1 200 OK\r\n"
156 "Content-Length: 9999\r\n"
159 f
= vlc_http_file_create(NULL
, url
, ua
, NULL
);
161 assert(vlc_http_file_get_size(f
) == 9999);
162 assert(vlc_http_file_get_redirect(f
) == NULL
);
163 vlc_http_file_destroy(f
);
166 replies
[0] = "HTTP/1.1 206 Partial Content\r\n"
167 "Content-Range: bytes 0-2344/2345\r\n"
168 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
173 f
= vlc_http_file_create(NULL
, url
, ua
, NULL
);
175 assert(vlc_http_file_can_seek(f
));
177 replies
[0] = "HTTP/1.1 206 Partial Content\r\n"
178 "Content-Range: bytes 1234-3455/3456\r\n"
179 "Last-Modified: Mon, 21 Oct 2013 20:13:22 GMT\r\n"
181 assert(vlc_http_file_seek(f
, offset
= 1234) == 0);
182 vlc_http_file_destroy(f
);
184 /* Invalid responses */
185 replies
[0] = "HTTP/1.1 206 Partial Content\r\n"
186 "Content-Type: multipart/byteranges\r\n"
190 f
= vlc_http_file_create(NULL
, url
, ua
, NULL
);
192 assert(vlc_http_file_get_size(f
) == (uintmax_t)-1);
194 replies
[0] = "HTTP/1.1 206 Partial Content\r\n"
195 "Content-Range: seconds 60-120/180\r\n"
197 assert(vlc_http_file_seek(f
, 0) == -1);
199 /* Incomplete range */
200 replies
[0] = "HTTP/1.1 206 Partial Content\r\n"
201 "Content-Range: bytes 0-1233/*\r\n"
203 assert(vlc_http_file_seek(f
, 0) == 0);
204 assert(vlc_http_file_get_size(f
) == 1234);
206 /* Extraneous range */
207 replies
[0] = "HTTP/1.1 200 OK\r\n"
208 "Content-Range: bytes 0-1233/1234\r\n"
210 assert(vlc_http_file_seek(f
, 0) == 0);
211 assert(vlc_http_file_get_size(f
) == (uintmax_t)-1);
213 /* Non-negotiable language */
214 replies
[0] = "HTTP/1.1 406 Not Acceptable\r\n"
216 replies
[1] = "HTTP/1.1 206 OK\r\n"
217 "Content-Range: bytes 0-1/2\r\n"
220 assert(vlc_http_file_seek(f
, 0) == 0);
221 assert(vlc_http_file_can_seek(f
));
222 assert(vlc_http_file_get_size(f
) == 2);
224 /* Protocol redirect hacks - not over TLS */
225 replies
[0] = "HTTP/1.1 200 OK\r\n"
226 "Pragma: features\r\n"
228 assert(vlc_http_file_seek(f
, 0) == 0);
229 assert(vlc_http_file_get_redirect(f
) == NULL
);
231 replies
[0] = "HTTP/1.1 200 OK\r\n"
232 "Icy-Name:CraptasticRadio\r\n"
234 assert(vlc_http_file_seek(f
, 0) == 0);
235 assert(vlc_http_file_get_redirect(f
) == NULL
);
237 vlc_http_file_destroy(f
);
241 f
= vlc_http_file_create(NULL
, url_http
, ua
, NULL
);
244 /* Protocol redirect hacks - over insecure HTTP */
245 replies
[0] = "HTTP/1.1 200 OK\r\n"
246 "Pragma: features\r\n"
248 str
= vlc_http_file_get_redirect(f
);
249 assert(str
!= NULL
&& strcmp(str
, url_mmsh
) == 0);
252 replies
[0] = "HTTP/1.1 200 OK\r\n"
253 "Icy-Name:CraptasticRadio\r\n"
255 vlc_http_file_seek(f
, 0);
256 str
= vlc_http_file_get_redirect(f
);
257 assert(str
!= NULL
&& strcmp(str
, url_icyx
) == 0);
260 vlc_http_file_destroy(f
);
262 /* Dummy API calls */
263 f
= vlc_http_file_create(NULL
, "ftp://localhost/foo", NULL
, NULL
);
265 f
= vlc_http_file_create(NULL
, "/foo", NULL
, NULL
);
267 f
= vlc_http_file_create(NULL
, "http://www.example.com", NULL
, NULL
);
269 vlc_http_file_destroy(f
);
271 vlc_http_cookies_destroy(jar
);
275 /* Callback for vlc_http_msg_h2_frame */
278 struct vlc_h2_frame
*
279 vlc_h2_frame_headers(uint_fast32_t id
, uint_fast32_t mtu
, bool eos
,
280 unsigned count
, const char *const tab
[][2])
282 (void) id
; (void) mtu
; (void) count
, (void) tab
;
287 /* Callback for the HTTP request */
290 static struct vlc_http_stream stream
;
292 static struct vlc_http_msg
*stream_read_headers(struct vlc_http_stream
*s
)
294 assert(s
== &stream
);
296 /* return next reply */
297 struct vlc_http_msg
*m
= NULL
;
298 const char *answer
= replies
[0];
302 m
= vlc_http_msg_headers(answer
);
304 vlc_http_msg_attach(m
, s
);
307 memmove(replies
, replies
+ 1, sizeof (replies
) - sizeof (replies
[0]));
308 replies
[(sizeof (replies
) / sizeof (replies
[0])) - 1] = NULL
;
313 static struct block_t
*stream_read(struct vlc_http_stream
*s
)
315 assert(s
== &stream
);
319 static void stream_close(struct vlc_http_stream
*s
, bool abort
)
321 assert(s
== &stream
);
325 static const struct vlc_http_stream_cbs stream_callbacks
=
332 static struct vlc_http_stream stream
= { &stream_callbacks
};
334 struct vlc_http_msg
*vlc_http_mgr_request(struct vlc_http_mgr
*mgr
, bool https
,
335 const char *host
, unsigned port
,
336 const struct vlc_http_msg
*req
)
341 assert(https
== secure
);
343 assert(!strcmp(host
, "www.example.com"));
344 assert(port
== 8443);
346 str
= vlc_http_msg_get_method(req
);
347 assert(!strcmp(str
, "GET"));
348 str
= vlc_http_msg_get_scheme(req
);
349 assert(!strcmp(str
, secure
? "https" : "http"));
350 str
= vlc_http_msg_get_authority(req
);
351 assert(!strcmp(str
, "www.example.com:8443"));
352 str
= vlc_http_msg_get_path(req
);
353 assert(!strcmp(str
, "/dir/file.ext?a=b"));
354 str
= vlc_http_msg_get_agent(req
);
355 assert(!strcmp(str
, ua
));
356 str
= vlc_http_msg_get_header(req
, "Referer");
358 str
= vlc_http_msg_get_header(req
, "Accept");
359 assert(str
== NULL
|| strstr(str
, "*/*") != NULL
);
361 str
= vlc_http_msg_get_header(req
, "Accept-Language");
362 /* This test case does not call setlocale(), so en_US can be assumed. */
365 assert(str
!= NULL
&& strncmp(str
, "en_US", 5) == 0);
372 str
= vlc_http_msg_get_header(req
, "Range");
373 assert(str
!= NULL
&& !strncmp(str
, "bytes=", 6)
374 && strtoul(str
+ 6, &end
, 10) == offset
&& *end
== '-');
376 time_t mtime
= vlc_http_msg_get_time(req
, "If-Unmodified-Since");
377 str
= vlc_http_msg_get_header(req
, "If-Match");
382 assert(str
!= NULL
&& !strcmp(str
, "\"foobar42\""));
385 assert(strcmp(str
, "*") || strcmp(str
, "\"foobar42\""));
390 assert(mtime
== 1382386402);
393 return vlc_http_msg_get_initial(&stream
);
396 struct vlc_http_cookie_jar_t
*vlc_http_mgr_get_jar(struct vlc_http_mgr
*mgr
)