[WININET_WINETEST] Sync with Wine Staging 3.9. CORE-14656
[reactos.git] / modules / rostests / winetests / wininet / http.c
1 /*
2 * Wininet - HTTP tests
3 *
4 * Copyright 2002 Aric Stewart
5 * Copyright 2004 Mike McCormack
6 * Copyright 2005 Hans Leidekker
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30 #include "winineti.h"
31 #include "winsock2.h"
32
33 #include "wine/test.h"
34
35 /* Undocumented security flags */
36 #define _SECURITY_FLAG_CERT_REV_FAILED 0x00800000
37 #define _SECURITY_FLAG_CERT_INVALID_CA 0x01000000
38 #define _SECURITY_FLAG_CERT_INVALID_CN 0x02000000
39 #define _SECURITY_FLAG_CERT_INVALID_DATE 0x04000000
40
41 #define TEST_URL "http://test.winehq.org/tests/hello.html"
42
43 static BOOL first_connection_to_test_url = TRUE;
44
45 /* Adapted from dlls/urlmon/tests/protocol.c */
46
47 #define SET_EXPECT2(status, num) \
48 expect[status] = num
49
50 #define SET_EXPECT(status) \
51 SET_EXPECT2(status, 1)
52
53 #define SET_OPTIONAL2(status, num) \
54 optional[status] = num
55
56 #define SET_OPTIONAL(status) \
57 SET_OPTIONAL2(status, 1)
58
59 /* SET_WINE_ALLOW's should be used with an appropriate
60 * todo_wine CHECK_NOTIFIED at a later point in the code */
61 #define SET_WINE_ALLOW2(status, num) \
62 wine_allow[status] = num
63
64 #define SET_WINE_ALLOW(status) \
65 SET_WINE_ALLOW2(status, 1)
66
67 #define CHECK_EXPECT(status) \
68 do { \
69 if (!expect[status] && !optional[status] && wine_allow[status]) \
70 { \
71 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
72 status < MAX_INTERNET_STATUS && status_string[status] ? \
73 status_string[status] : "unknown"); \
74 wine_allow[status]--; \
75 } \
76 else \
77 { \
78 ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
79 status < MAX_INTERNET_STATUS && status_string[status] ? \
80 status_string[status] : "unknown"); \
81 if (expect[status]) expect[status]--; \
82 else if(optional[status]) optional[status]--; \
83 } \
84 notified[status]++; \
85 }while(0)
86
87 /* CLEAR_NOTIFIED used in cases when notification behavior
88 * differs between Windows versions */
89 #define CLEAR_NOTIFIED(status) \
90 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
91
92 #define CHECK_NOTIFIED2(status, num) \
93 do { \
94 ok(notified[status] + optional[status] == (num), \
95 "expected status %d (%s) %d times, received %d times\n", \
96 status, status < MAX_INTERNET_STATUS && status_string[status] ? \
97 status_string[status] : "unknown", (num), notified[status]); \
98 CLEAR_NOTIFIED(status); \
99 }while(0)
100
101 #define CHECK_NOTIFIED(status) \
102 CHECK_NOTIFIED2(status, 1)
103
104 #define CHECK_NOT_NOTIFIED(status) \
105 CHECK_NOTIFIED2(status, 0)
106
107 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
108 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
109 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
110 static const char *status_string[MAX_INTERNET_STATUS];
111
112 static HANDLE complete_event, conn_close_event, conn_wait_event, server_req_rec_event, request_sent_event;
113 static DWORD req_error;
114 static BOOL is_ie7plus = TRUE;
115
116 #define TESTF_REDIRECT 0x01
117 #define TESTF_COMPRESSED 0x02
118 #define TESTF_CHUNKED 0x04
119
120 typedef struct {
121 const char *url;
122 const char *redirected_url;
123 const char *host;
124 const char *path;
125 const char *headers;
126 DWORD flags;
127 const char *post_data;
128 const char *content;
129 } test_data_t;
130
131 static const test_data_t test_data[] = {
132 {
133 "http://test.winehq.org/tests/data.php",
134 "http://test.winehq.org/tests/data.php",
135 "test.winehq.org",
136 "/tests/data.php",
137 "",
138 TESTF_CHUNKED
139 },
140 {
141 "http://test.winehq.org/tests/redirect",
142 "http://test.winehq.org/tests/hello.html",
143 "test.winehq.org",
144 "/tests/redirect",
145 "",
146 TESTF_REDIRECT
147 },
148 {
149 "http://test.winehq.org/tests/gzip.php",
150 "http://test.winehq.org/tests/gzip.php",
151 "test.winehq.org",
152 "/tests/gzip.php",
153 "Accept-Encoding: gzip, deflate",
154 TESTF_COMPRESSED
155 },
156 {
157 "http://test.winehq.org/tests/post.php",
158 "http://test.winehq.org/tests/post.php",
159 "test.winehq.org",
160 "/tests/post.php",
161 "Content-Type: application/x-www-form-urlencoded",
162 0,
163 "mode=Test",
164 "mode => Test\n"
165 }
166 };
167
168 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
169 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackW)(HINTERNET ,INTERNET_STATUS_CALLBACK);
170 static BOOL (WINAPI *pInternetGetSecurityInfoByURLA)(LPSTR,PCCERT_CHAIN_CONTEXT*,DWORD*);
171
172 static int strcmp_wa(LPCWSTR strw, const char *stra)
173 {
174 WCHAR buf[512];
175 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
176 return lstrcmpW(strw, buf);
177 }
178
179 static BOOL proxy_active(void)
180 {
181 HKEY internet_settings;
182 DWORD proxy_enable;
183 DWORD size;
184
185 if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
186 0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
187 return FALSE;
188
189 size = sizeof(DWORD);
190 if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
191 proxy_enable = 0;
192
193 RegCloseKey(internet_settings);
194
195 return proxy_enable != 0;
196 }
197
198 static void init_events(void)
199 {
200 complete_event = CreateEventW(NULL, FALSE, FALSE, NULL);
201 conn_close_event = CreateEventW(NULL, FALSE, FALSE, NULL);
202 conn_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
203 server_req_rec_event = CreateEventW(NULL, FALSE, FALSE, NULL);
204 request_sent_event = CreateEventW(NULL, FALSE, FALSE, NULL);
205 }
206
207 static void free_events(void)
208 {
209 CloseHandle(complete_event);
210 CloseHandle(conn_close_event);
211 CloseHandle(conn_wait_event);
212 CloseHandle(server_req_rec_event);
213 CloseHandle(request_sent_event);
214 }
215
216 static void reset_events(void)
217 {
218 ResetEvent(complete_event);
219 ResetEvent(conn_close_event);
220 ResetEvent(conn_wait_event);
221 ResetEvent(server_req_rec_event);
222 ResetEvent(request_sent_event);
223 }
224
225 #define test_status_code(a,b) _test_status_code(__LINE__,a,b, FALSE)
226 #define test_status_code_todo(a,b) _test_status_code(__LINE__,a,b, TRUE)
227 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
228 {
229 DWORD code, size, index;
230 char exbuf[12], bufa[10];
231 WCHAR bufw[10];
232 BOOL res;
233
234 code = 0xdeadbeef;
235 size = sizeof(code);
236 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, NULL);
237 ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %u\n", GetLastError());
238 todo_wine_if (is_todo)
239 ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
240 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
241
242 code = 0xdeadbeef;
243 index = 0;
244 size = sizeof(code);
245 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
246 ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %u\n", GetLastError());
247 ok_(__FILE__,line)(!index, "index = %d, expected 0\n", index);
248 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
249
250 sprintf(exbuf, "%u", excode);
251
252 size = sizeof(bufa);
253 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, bufa, &size, NULL);
254 ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
255 todo_wine_if (is_todo)
256 ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
257 ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %d for \"%s\"\n", size, exbuf);
258
259 size = 0;
260 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
261 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
262 "[4] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
263 ok_(__FILE__,line)(size == strlen(exbuf)+1, "unexpected size %d for \"%s\"\n", size, exbuf);
264
265 size = sizeof(bufw);
266 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
267 ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
268 todo_wine_if (is_todo)
269 ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
270 ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
271
272 size = 0;
273 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
274 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
275 "[6] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
276 ok_(__FILE__,line)(size == (strlen(exbuf)+1)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
277
278 if(0) {
279 size = sizeof(bufw);
280 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
281 ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "HttpQueryInfo(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
282 ok(size == sizeof(bufw), "unexpected size %d\n", size);
283 }
284
285 code = 0xdeadbeef;
286 index = 1;
287 size = sizeof(code);
288 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
289 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
290 "[7] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
291
292 code = 0xdeadbeef;
293 size = sizeof(code);
294 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_REQUEST_HEADERS, &code, &size, NULL);
295 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_INVALID_QUERY_REQUEST,
296 "[8] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
297 }
298
299 #define test_request_flags(a,b) _test_request_flags(__LINE__,a,b,FALSE)
300 #define test_request_flags_todo(a,b) _test_request_flags(__LINE__,a,b,TRUE)
301 static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOOL is_todo)
302 {
303 DWORD flags, size;
304 BOOL res;
305
306 flags = 0xdeadbeef;
307 size = sizeof(flags);
308 res = InternetQueryOptionW(req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &size);
309 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_REQUEST_FLAGS) failed: %u\n", GetLastError());
310
311 /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
312 flags &= ~INTERNET_REQFLAG_CACHE_WRITE_DISABLED;
313 todo_wine_if (is_todo)
314 ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
315 }
316
317 #define test_request_url(a,b) _test_request_url(__LINE__,a,b)
318 static void _test_request_url(unsigned line, HINTERNET req, const char *expected_url)
319 {
320 char buf[INTERNET_MAX_URL_LENGTH];
321 DWORD size = sizeof(buf);
322 BOOL res;
323
324 res = InternetQueryOptionA(req, INTERNET_OPTION_URL, buf, &size);
325 ok_(__FILE__,line)(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
326 ok_(__FILE__,line)(size == strlen(expected_url), "size = %u\n", size);
327 ok_(__FILE__,line)(!strcmp(buf, expected_url), "unexpected URL %s, expected %s\n", buf, expected_url);
328 }
329
330 #define test_http_version(a) _test_http_version(__LINE__,a)
331 static void _test_http_version(unsigned line, HINTERNET req)
332 {
333 HTTP_VERSION_INFO v = {0xdeadbeef, 0xdeadbeef};
334 DWORD size;
335 BOOL res;
336
337 size = sizeof(v);
338 res = InternetQueryOptionW(req, INTERNET_OPTION_HTTP_VERSION, &v, &size);
339 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_HTTP_VERSION) failed: %u\n", GetLastError());
340 ok_(__FILE__,line)(v.dwMajorVersion == 1, "dwMajorVersion = %d\n", v.dwMajorVersion);
341 ok_(__FILE__,line)(v.dwMinorVersion == 1, "dwMinorVersion = %d\n", v.dwMinorVersion);
342 }
343
344 static int close_handle_cnt;
345
346 static VOID WINAPI callback(
347 HINTERNET hInternet,
348 DWORD_PTR dwContext,
349 DWORD dwInternetStatus,
350 LPVOID lpvStatusInformation,
351 DWORD dwStatusInformationLength
352 )
353 {
354 CHECK_EXPECT(dwInternetStatus);
355 switch (dwInternetStatus)
356 {
357 case INTERNET_STATUS_RESOLVING_NAME:
358 if(winetest_debug > 1)
359 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
360 GetCurrentThreadId(), hInternet, dwContext,
361 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
362 *(LPSTR)lpvStatusInformation = '\0';
363 break;
364 case INTERNET_STATUS_NAME_RESOLVED:
365 if(winetest_debug > 1)
366 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
367 GetCurrentThreadId(), hInternet, dwContext,
368 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
369 *(LPSTR)lpvStatusInformation = '\0';
370 break;
371 case INTERNET_STATUS_CONNECTING_TO_SERVER:
372 if(winetest_debug > 1)
373 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
374 GetCurrentThreadId(), hInternet, dwContext,
375 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
376 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
377 dwStatusInformationLength);
378 *(LPSTR)lpvStatusInformation = '\0';
379 break;
380 case INTERNET_STATUS_CONNECTED_TO_SERVER:
381 if(winetest_debug > 1)
382 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
383 GetCurrentThreadId(), hInternet, dwContext,
384 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
385 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
386 dwStatusInformationLength);
387 *(LPSTR)lpvStatusInformation = '\0';
388 break;
389 case INTERNET_STATUS_SENDING_REQUEST:
390 if(winetest_debug > 1)
391 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
392 GetCurrentThreadId(), hInternet, dwContext,
393 lpvStatusInformation,dwStatusInformationLength);
394 break;
395 case INTERNET_STATUS_REQUEST_SENT:
396 ok(dwStatusInformationLength == sizeof(DWORD),
397 "info length should be sizeof(DWORD) instead of %d\n",
398 dwStatusInformationLength);
399 if(winetest_debug > 1)
400 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
401 GetCurrentThreadId(), hInternet, dwContext,
402 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
403 break;
404 case INTERNET_STATUS_RECEIVING_RESPONSE:
405 if(winetest_debug > 1)
406 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
407 GetCurrentThreadId(), hInternet, dwContext,
408 lpvStatusInformation,dwStatusInformationLength);
409 break;
410 case INTERNET_STATUS_RESPONSE_RECEIVED:
411 ok(dwStatusInformationLength == sizeof(DWORD),
412 "info length should be sizeof(DWORD) instead of %d\n",
413 dwStatusInformationLength);
414 if(winetest_debug > 1)
415 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
416 GetCurrentThreadId(), hInternet, dwContext,
417 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
418 break;
419 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
420 if(winetest_debug > 1)
421 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
422 GetCurrentThreadId(), hInternet,dwContext,
423 lpvStatusInformation,dwStatusInformationLength);
424 break;
425 case INTERNET_STATUS_PREFETCH:
426 if(winetest_debug > 1)
427 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
428 GetCurrentThreadId(), hInternet, dwContext,
429 lpvStatusInformation,dwStatusInformationLength);
430 break;
431 case INTERNET_STATUS_CLOSING_CONNECTION:
432 if(winetest_debug > 1)
433 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
434 GetCurrentThreadId(), hInternet, dwContext,
435 lpvStatusInformation,dwStatusInformationLength);
436 break;
437 case INTERNET_STATUS_CONNECTION_CLOSED:
438 if(winetest_debug > 1)
439 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
440 GetCurrentThreadId(), hInternet, dwContext,
441 lpvStatusInformation,dwStatusInformationLength);
442 break;
443 case INTERNET_STATUS_HANDLE_CREATED:
444 ok(dwStatusInformationLength == sizeof(HINTERNET),
445 "info length should be sizeof(HINTERNET) instead of %d\n",
446 dwStatusInformationLength);
447 if(winetest_debug > 1)
448 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
449 GetCurrentThreadId(), hInternet, dwContext,
450 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
451 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
452 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
453 break;
454 case INTERNET_STATUS_HANDLE_CLOSING:
455 ok(dwStatusInformationLength == sizeof(HINTERNET),
456 "info length should be sizeof(HINTERNET) instead of %d\n",
457 dwStatusInformationLength);
458 if(winetest_debug > 1)
459 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
460 GetCurrentThreadId(), hInternet, dwContext,
461 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
462 if(!InterlockedDecrement(&close_handle_cnt))
463 SetEvent(complete_event);
464 break;
465 case INTERNET_STATUS_REQUEST_COMPLETE:
466 {
467 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
468 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
469 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
470 dwStatusInformationLength);
471 ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %ld\n", iar->dwResult);
472 if(winetest_debug > 1)
473 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
474 GetCurrentThreadId(), hInternet, dwContext,
475 iar->dwResult,iar->dwError,dwStatusInformationLength);
476 req_error = iar->dwError;
477 if(!close_handle_cnt)
478 SetEvent(complete_event);
479 break;
480 }
481 case INTERNET_STATUS_REDIRECT:
482 if(winetest_debug > 1)
483 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
484 GetCurrentThreadId(), hInternet, dwContext,
485 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
486 *(LPSTR)lpvStatusInformation = '\0';
487 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
488 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
489 break;
490 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
491 if(winetest_debug > 1)
492 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
493 GetCurrentThreadId(), hInternet, dwContext,
494 lpvStatusInformation, dwStatusInformationLength);
495 break;
496 default:
497 if(winetest_debug > 1)
498 trace("%04x:Callback %p 0x%lx %d %p %d\n",
499 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
500 lpvStatusInformation, dwStatusInformationLength);
501 }
502 }
503
504 typedef struct {
505 HINTERNET session;
506 HINTERNET connection;
507 HINTERNET request;
508 } test_request_t;
509
510 #define open_simple_request(a,b,c,d,e) _open_simple_request(__LINE__,a,b,c,d,e)
511 static void _open_simple_request(unsigned line, test_request_t *req, const char *host,
512 int port, const char *verb, const char *url)
513 {
514 req->session = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
515 ok_(__FILE__,line)(req->session != NULL, "InternetOpenA failed: %u\n", GetLastError());
516
517 req->connection = InternetConnectA(req->session, host, port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
518 ok_(__FILE__,line)(req->connection != NULL, "InternetConnectA failed: %u\n", GetLastError());
519
520 req->request = HttpOpenRequestA(req->connection, verb, url, NULL, NULL, NULL, 0, 0);
521 ok_(__FILE__,line)(req->request != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
522 }
523
524 #define close_request(a) _close_request(__LINE__,a)
525 static void _close_request(unsigned line, test_request_t *req)
526 {
527 BOOL ret;
528
529 ret = InternetCloseHandle(req->request);
530 ok_(__FILE__,line)(ret, "InternetCloseHandle(request) failed: %u\n", GetLastError());
531 ret = InternetCloseHandle(req->connection);
532 ok_(__FILE__,line)(ret, "InternetCloseHandle(connection) failed: %u\n", GetLastError());
533 ret = InternetCloseHandle(req->session);
534 ok_(__FILE__,line)(ret, "InternetCloseHandle(session) failed: %u\n", GetLastError());
535 }
536
537 #define receive_simple_request(a,b,c) _receive_simple_request(__LINE__,a,b,c)
538 static DWORD _receive_simple_request(unsigned line, HINTERNET req, char *buf, size_t buf_size)
539 {
540 DWORD read = 0;
541 BOOL ret;
542
543 ret = InternetReadFile(req, buf, buf_size, &read);
544 ok_(__FILE__,line)(ret, "InternetReadFile failed: %u\n", GetLastError());
545
546 return read;
547 }
548
549 static void close_async_handle(HINTERNET handle, int handle_cnt)
550 {
551 BOOL res;
552
553 close_handle_cnt = handle_cnt;
554
555 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
556 res = InternetCloseHandle(handle);
557 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
558 WaitForSingleObject(complete_event, INFINITE);
559 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
560 }
561
562 static void InternetReadFile_test(int flags, const test_data_t *test)
563 {
564 char *post_data = NULL;
565 BOOL res, on_async = TRUE;
566 CHAR buffer[4000];
567 WCHAR wbuffer[4000];
568 DWORD length, length2, index, exlen = 0, post_len = 0;
569 const char *types[2] = { "*", NULL };
570 HINTERNET hi, hic = 0, hor = 0;
571
572 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
573 reset_events();
574
575 trace("InternetOpenA <--\n");
576 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
577 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
578 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
579 trace("InternetOpenA -->\n");
580
581 if (hi == 0x0) goto abort;
582
583 pInternetSetStatusCallbackA(hi,&callback);
584
585 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
586
587 trace("InternetConnectA <--\n");
588 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
589 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
590 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
591 trace("InternetConnectA -->\n");
592
593 if (hic == 0x0) goto abort;
594
595 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
596 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
597
598 trace("HttpOpenRequestA <--\n");
599 hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
600 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
601 0xdeadbead);
602 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
603 /*
604 * If the internet name can't be resolved we are probably behind
605 * a firewall or in some other way not directly connected to the
606 * Internet. Not enough reason to fail the test. Just ignore and
607 * abort.
608 */
609 } else {
610 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
611 }
612 trace("HttpOpenRequestA -->\n");
613
614 if (hor == 0x0) goto abort;
615
616 test_request_flags(hor, INTERNET_REQFLAG_NO_HEADERS);
617 test_request_url(hor, test->url);
618
619 length = sizeof(buffer);
620 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
621 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
622 ok(length == 0 || (length == 1 && !*buffer) /* win10 */, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
623 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
624
625 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
626 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
627 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
628 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT,2);
629 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_RECEIVED,2);
630 if (first_connection_to_test_url)
631 {
632 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
633 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
634 }
635 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
636 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
637 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
638 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
639 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
640 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
641 if(test->flags & TESTF_REDIRECT) {
642 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
643 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
644 }
645 SET_EXPECT(INTERNET_STATUS_REDIRECT);
646 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
647 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
648 if (flags & INTERNET_FLAG_ASYNC)
649 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
650
651 if(test->flags & TESTF_COMPRESSED) {
652 BOOL b = TRUE;
653
654 res = InternetSetOptionA(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
655 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
656 "InternetSetOption failed: %u\n", GetLastError());
657 if(!res)
658 goto abort;
659 }
660
661 test_status_code(hor, 0);
662
663 trace("HttpSendRequestA -->\n");
664 if(test->post_data) {
665 post_len = strlen(test->post_data);
666 post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
667 memcpy(post_data, test->post_data, post_len);
668 }
669 SetLastError(0xdeadbeef);
670 res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
671 if (flags & INTERNET_FLAG_ASYNC)
672 ok(!res && (GetLastError() == ERROR_IO_PENDING),
673 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
674 else
675 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
676 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
677 trace("HttpSendRequestA <--\n");
678
679 if (flags & INTERNET_FLAG_ASYNC) {
680 WaitForSingleObject(complete_event, INFINITE);
681 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
682 }
683 HeapFree(GetProcessHeap(), 0, post_data);
684
685 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
686 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
687 if (first_connection_to_test_url)
688 {
689 if (! proxy_active())
690 {
691 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
692 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
693 }
694 else
695 {
696 CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
697 CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
698 }
699 }
700 else
701 {
702 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
703 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
704 }
705 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
706 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
707 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
708 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
709 if(test->flags & TESTF_REDIRECT)
710 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
711 if (flags & INTERNET_FLAG_ASYNC)
712 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
713 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
714 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
715 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
716
717 test_request_flags(hor, 0);
718
719 length = 100;
720 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
721 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
722
723 length = sizeof(buffer)-2;
724 memset(buffer, 0x77, sizeof(buffer));
725 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
726 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
727 /* show that the function writes data past the length returned */
728 ok(buffer[length-2], "Expected any header character, got 0x00\n");
729 ok(!buffer[length-1], "Expected 0x00, got %02X\n", buffer[length-1]);
730 ok(!buffer[length], "Expected 0x00, got %02X\n", buffer[length]);
731 ok(buffer[length+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length+1]);
732
733 length2 = length;
734 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
735 ok(!res, "Expected 0x00, got %d\n", res);
736 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
737 ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
738 /* the in length of the buffer must be +1 but the length returned does not count this */
739 length2 = length+1;
740 memset(buffer, 0x77, sizeof(buffer));
741 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
742 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
743 ok(buffer[length2] == 0x00, "Expected 0x00, got %02X\n", buffer[length2]);
744 ok(buffer[length2+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length2+1]);
745 ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
746
747 length = sizeof(wbuffer)-2*sizeof(WCHAR);
748 memset(wbuffer, 0x77, sizeof(wbuffer));
749 res = HttpQueryInfoW(hor, HTTP_QUERY_RAW_HEADERS, wbuffer, &length, 0x0);
750 ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
751 ok(length % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length);
752 length /= sizeof(WCHAR);
753 /* show that the function writes data past the length returned */
754 ok(wbuffer[length-2], "Expected any header character, got 0x0000\n");
755 ok(!wbuffer[length-1], "Expected 0x0000, got %04X\n", wbuffer[length-1]);
756 ok(!wbuffer[length], "Expected 0x0000, got %04X\n", wbuffer[length]);
757 ok(wbuffer[length+1] == 0x7777 || broken(wbuffer[length+1] != 0x7777),
758 "Expected 0x7777, got %04X\n", wbuffer[length+1]);
759
760 length2 = length*sizeof(WCHAR);
761 res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
762 ok(!res, "Expected 0x00, got %d\n", res);
763 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
764 ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
765 length2 /= sizeof(WCHAR);
766 ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
767 /* the in length of the buffer must be +1 but the length returned does not count this */
768 length2 = (length+1)*sizeof(WCHAR);
769 memset(wbuffer, 0x77, sizeof(wbuffer));
770 res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
771 ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
772 ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
773 length2 /= sizeof(WCHAR);
774 ok(!wbuffer[length2], "Expected 0x0000, got %04X\n", wbuffer[length2]);
775 ok(wbuffer[length2+1] == 0x7777, "Expected 0x7777, got %04X\n", wbuffer[length2+1]);
776 ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
777
778 test_request_url(hor, test->redirected_url);
779
780 index = 0;
781 length = 0;
782 SetLastError(0xdeadbeef);
783 ok(HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,NULL,&length,&index) == FALSE,"Query worked\n");
784 if(test->flags & TESTF_COMPRESSED)
785 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
786 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
787 ok(index == 0, "Index was incremented\n");
788
789 index = 0;
790 length = 16;
791 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,&index);
792 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
793 if(test->flags & TESTF_COMPRESSED)
794 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
795 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
796 ok(!res || index == 1, "Index was not incremented although result is %x (index = %u)\n", res, index);
797
798 length = 100;
799 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
800 buffer[length]=0;
801 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
802
803 length = 100;
804 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
805 buffer[length]=0;
806 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
807
808 SetLastError(0xdeadbeef);
809 res = InternetReadFile(NULL, buffer, 100, &length);
810 ok(!res, "InternetReadFile should have failed\n");
811 ok(GetLastError() == ERROR_INVALID_HANDLE,
812 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
813 GetLastError());
814
815 length = 100;
816 if(winetest_debug > 1)
817 trace("Entering Query loop\n");
818
819 while (TRUE)
820 {
821 if (flags & INTERNET_FLAG_ASYNC)
822 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
823
824 /* IE11 calls those in InternetQueryDataAvailable call. */
825 SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
826 SET_OPTIONAL(INTERNET_STATUS_RESPONSE_RECEIVED);
827
828 length = 0;
829 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
830
831 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
832
833 if (flags & INTERNET_FLAG_ASYNC)
834 {
835 if (res)
836 {
837 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
838 if(exlen) {
839 ok(length >= exlen, "length %u < exlen %u\n", length, exlen);
840 exlen = 0;
841 }
842 }
843 else if (GetLastError() == ERROR_IO_PENDING)
844 {
845 if(winetest_debug > 1)
846 trace("pending\n");
847 /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
848 if(!(test->flags & TESTF_CHUNKED))
849 ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
850 WaitForSingleObject(complete_event, INFINITE);
851 exlen = length;
852 ok(exlen, "length = 0\n");
853 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
854 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
855 ok(req_error, "req_error = 0\n");
856 continue;
857 }else {
858 ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
859 }
860 }else {
861 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
862 }
863 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
864
865 if(winetest_debug > 1)
866 trace("length %u\n", length);
867 if(test->flags & TESTF_CHUNKED)
868 ok(length <= 8192, "length = %d, expected <= 8192\n", length);
869 if (length)
870 {
871 char *buffer;
872 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
873
874 res = InternetReadFile(hor,buffer,length,&length);
875
876 buffer[length]=0;
877
878 if(winetest_debug > 1)
879 trace("ReadFile -> %s %i\n", res ? "TRUE" : "FALSE", length);
880
881 if(test->content)
882 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
883 HeapFree(GetProcessHeap(),0,buffer);
884 }else {
885 ok(!on_async, "Returned zero size in response to request complete\n");
886 break;
887 }
888 on_async = FALSE;
889 }
890 if(test->flags & TESTF_REDIRECT) {
891 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
892 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
893 }
894 abort:
895 if(winetest_debug > 1)
896 trace("aborting\n");
897 close_async_handle(hi, 2);
898 first_connection_to_test_url = FALSE;
899 }
900
901 static void InternetReadFile_chunked_test(void)
902 {
903 BOOL res;
904 CHAR buffer[4000];
905 DWORD length, got;
906 const char *types[2] = { "*", NULL };
907 HINTERNET hi, hic = 0, hor = 0;
908
909 trace("Starting InternetReadFile chunked test\n");
910
911 trace("InternetOpenA <--\n");
912 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
913 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
914 trace("InternetOpenA -->\n");
915
916 if (hi == 0x0) goto abort;
917
918 trace("InternetConnectA <--\n");
919 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
920 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
921 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
922 trace("InternetConnectA -->\n");
923
924 if (hic == 0x0) goto abort;
925
926 trace("HttpOpenRequestA <--\n");
927 hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
928 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
929 0xdeadbead);
930 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
931 /*
932 * If the internet name can't be resolved we are probably behind
933 * a firewall or in some other way not directly connected to the
934 * Internet. Not enough reason to fail the test. Just ignore and
935 * abort.
936 */
937 } else {
938 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
939 }
940 trace("HttpOpenRequestA -->\n");
941
942 if (hor == 0x0) goto abort;
943
944 trace("HttpSendRequestA -->\n");
945 SetLastError(0xdeadbeef);
946 res = HttpSendRequestA(hor, "", -1, NULL, 0);
947 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
948 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
949 trace("HttpSendRequestA <--\n");
950
951 test_request_flags(hor, 0);
952
953 length = 100;
954 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
955 buffer[length]=0;
956 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
957
958 SetLastError( 0xdeadbeef );
959 length = 100;
960 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
961 buffer[length]=0;
962 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
963 ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
964 "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
965 ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
966 "Wrong transfer encoding '%s'\n", buffer );
967
968 SetLastError( 0xdeadbeef );
969 length = 16;
970 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
971 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
972 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
973
974 length = 100;
975 trace("Entering Query loop\n");
976
977 while (TRUE)
978 {
979 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
980 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
981 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
982 trace("got %u available\n",length);
983 if (length)
984 {
985 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
986
987 res = InternetReadFile(hor,buffer,length,&got);
988
989 buffer[got]=0;
990 trace("ReadFile -> %i %i\n",res,got);
991 ok( length == got, "only got %u of %u available\n", got, length );
992 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
993
994 HeapFree(GetProcessHeap(),0,buffer);
995 if (!got) break;
996 }
997 if (length == 0)
998 {
999 got = 0xdeadbeef;
1000 res = InternetReadFile( hor, buffer, 1, &got );
1001 ok( res, "InternetReadFile failed: %u\n", GetLastError() );
1002 ok( !got, "got %u\n", got );
1003 break;
1004 }
1005 }
1006 abort:
1007 trace("aborting\n");
1008 if (hor != 0x0) {
1009 res = InternetCloseHandle(hor);
1010 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
1011 }
1012 if (hi != 0x0) {
1013 res = InternetCloseHandle(hi);
1014 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
1015 }
1016 }
1017
1018 static void InternetReadFileExA_test(int flags)
1019 {
1020 DWORD rc;
1021 DWORD length;
1022 const char *types[2] = { "*", NULL };
1023 HINTERNET hi, hic = 0, hor = 0;
1024 INTERNET_BUFFERSA inetbuffers;
1025
1026 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
1027 reset_events();
1028
1029 trace("InternetOpenA <--\n");
1030 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
1031 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
1032 trace("InternetOpenA -->\n");
1033
1034 if (hi == 0x0) goto abort;
1035
1036 pInternetSetStatusCallbackA(hi,&callback);
1037
1038 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1039
1040 trace("InternetConnectA <--\n");
1041 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
1042 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1043 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
1044 trace("InternetConnectA -->\n");
1045
1046 if (hic == 0x0) goto abort;
1047
1048 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1049 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1050
1051 trace("HttpOpenRequestA <--\n");
1052 hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
1053 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
1054 0xdeadbead);
1055 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
1056 /*
1057 * If the internet name can't be resolved we are probably behind
1058 * a firewall or in some other way not directly connected to the
1059 * Internet. Not enough reason to fail the test. Just ignore and
1060 * abort.
1061 */
1062 } else {
1063 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
1064 }
1065 trace("HttpOpenRequestA -->\n");
1066
1067 if (hor == 0x0) goto abort;
1068
1069 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1070 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1071 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1072 if (first_connection_to_test_url)
1073 {
1074 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
1075 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
1076 }
1077 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT, 2);
1078 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
1079 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
1080 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
1081 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
1082 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
1083 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
1084 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
1085 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
1086 SET_EXPECT(INTERNET_STATUS_REDIRECT);
1087 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
1088 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
1089 if (flags & INTERNET_FLAG_ASYNC)
1090 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1091 else
1092 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1093
1094 trace("HttpSendRequestA -->\n");
1095 SetLastError(0xdeadbeef);
1096 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
1097 if (flags & INTERNET_FLAG_ASYNC)
1098 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
1099 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
1100 else
1101 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
1102 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
1103 trace("HttpSendRequestA <--\n");
1104
1105 if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
1106 WaitForSingleObject(complete_event, INFINITE);
1107 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1108 }
1109
1110 if (first_connection_to_test_url)
1111 {
1112 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1113 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1114 }
1115 else
1116 {
1117 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1118 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1119 }
1120 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
1121 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
1122 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
1123 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
1124 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
1125 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
1126 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
1127 if (flags & INTERNET_FLAG_ASYNC)
1128 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1129 else
1130 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1131 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
1132 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
1133 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1134 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1135
1136 if(is_ie7plus) {
1137 rc = InternetReadFileExW(hor, NULL, 0, 0xdeadcafe);
1138 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1139 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1140 rc ? "TRUE" : "FALSE", GetLastError());
1141 }
1142
1143 /* tests invalid dwStructSize */
1144 inetbuffers.dwStructSize = sizeof(inetbuffers)+1;
1145 inetbuffers.lpcszHeader = NULL;
1146 inetbuffers.dwHeadersLength = 0;
1147 inetbuffers.dwBufferLength = 10;
1148 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
1149 inetbuffers.dwOffsetHigh = 1234;
1150 inetbuffers.dwOffsetLow = 5678;
1151 rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1152 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1153 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1154 rc ? "TRUE" : "FALSE", GetLastError());
1155 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1156
1157 test_request_flags(hor, 0);
1158
1159 /* tests to see whether lpcszHeader is used - it isn't */
1160 inetbuffers.dwStructSize = sizeof(inetbuffers);
1161 inetbuffers.lpcszHeader = (LPCSTR)0xdeadbeef;
1162 inetbuffers.dwHeadersLength = 255;
1163 inetbuffers.dwBufferLength = 0;
1164 inetbuffers.lpvBuffer = NULL;
1165 inetbuffers.dwOffsetHigh = 1234;
1166 inetbuffers.dwOffsetLow = 5678;
1167 rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1168 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
1169 trace("read %i bytes\n", inetbuffers.dwBufferLength);
1170
1171 rc = InternetReadFileExA(NULL, &inetbuffers, 0, 0xdeadcafe);
1172 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
1173 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
1174 rc ? "TRUE" : "FALSE", GetLastError());
1175
1176 length = 0;
1177 trace("Entering Query loop\n");
1178
1179 while (TRUE)
1180 {
1181 inetbuffers.dwStructSize = sizeof(inetbuffers);
1182 inetbuffers.dwBufferLength = 1024;
1183 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
1184 inetbuffers.dwOffsetHigh = 1234;
1185 inetbuffers.dwOffsetLow = 5678;
1186
1187 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1188 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1189 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1190 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
1191 if (!rc)
1192 {
1193 if (GetLastError() == ERROR_IO_PENDING)
1194 {
1195 trace("InternetReadFileEx -> PENDING\n");
1196 ok(flags & INTERNET_FLAG_ASYNC,
1197 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
1198 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1199 WaitForSingleObject(complete_event, INFINITE);
1200 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1201 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1202 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1203 }
1204 else
1205 {
1206 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
1207 break;
1208 }
1209 }
1210 else
1211 {
1212 trace("InternetReadFileEx -> SUCCEEDED\n");
1213 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1214 if (inetbuffers.dwBufferLength)
1215 {
1216 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1217 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1218 }
1219 else
1220 {
1221 /* Win98 still sends these when 0 bytes are read, WinXP does not */
1222 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1223 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1224 }
1225 }
1226
1227 trace("read %i bytes\n", inetbuffers.dwBufferLength);
1228 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
1229
1230 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
1231 "InternetReadFileEx sets offsets to 0x%x%08x\n",
1232 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
1233
1234 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1235
1236 if (!inetbuffers.dwBufferLength)
1237 break;
1238
1239 length += inetbuffers.dwBufferLength;
1240 }
1241 ok(length > 0, "failed to read any of the document\n");
1242 trace("Finished. Read %d bytes\n", length);
1243
1244 abort:
1245 close_async_handle(hi, 2);
1246 first_connection_to_test_url = FALSE;
1247 }
1248
1249 static void InternetOpenUrlA_test(void)
1250 {
1251 HINTERNET myhinternet, myhttp;
1252 char buffer[0x400];
1253 DWORD size, readbytes, totalbytes=0;
1254 BOOL ret;
1255
1256 ret = DeleteUrlCacheEntryA(TEST_URL);
1257 ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND,
1258 "DeleteUrlCacheEntry returned %x, GetLastError() = %d\n", ret, GetLastError());
1259
1260 myhinternet = InternetOpenA("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1261 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
1262 size = 0x400;
1263 ret = InternetCanonicalizeUrlA(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
1264 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1265
1266 SetLastError(0);
1267 myhttp = InternetOpenUrlA(myhinternet, TEST_URL, 0, 0,
1268 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1269 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1270 return; /* WinXP returns this when not connected to the net */
1271 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1272 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1273 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1274 totalbytes += readbytes;
1275 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1276 totalbytes += readbytes;
1277 trace("read 0x%08x bytes\n",totalbytes);
1278
1279 InternetCloseHandle(myhttp);
1280 InternetCloseHandle(myhinternet);
1281
1282 ret = DeleteUrlCacheEntryA(TEST_URL);
1283 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "INTERNET_FLAG_NO_CACHE_WRITE flag doesn't work\n");
1284 }
1285
1286 static void HttpSendRequestEx_test(void)
1287 {
1288 HINTERNET hSession;
1289 HINTERNET hConnect;
1290 HINTERNET hRequest;
1291
1292 INTERNET_BUFFERSA BufferIn;
1293 DWORD dwBytesWritten, dwBytesRead, error;
1294 CHAR szBuffer[256];
1295 int i;
1296 BOOL ret;
1297
1298 static char szPostData[] = "mode=Test";
1299 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1300
1301 hSession = InternetOpenA("Wine Regression Test",
1302 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1303 ok( hSession != NULL ,"Unable to open Internet session\n");
1304 hConnect = InternetConnectA(hSession, "test.winehq.org",
1305 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1306 0);
1307 ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1308 hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1309 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1310 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1311 {
1312 skip( "Network unreachable, skipping test\n" );
1313 goto done;
1314 }
1315 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1316
1317 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1318
1319 BufferIn.dwStructSize = sizeof(BufferIn);
1320 BufferIn.Next = (INTERNET_BUFFERSA*)0xdeadcab;
1321 BufferIn.lpcszHeader = szContentType;
1322 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1323 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1324 BufferIn.lpvBuffer = szPostData;
1325 BufferIn.dwBufferLength = 3;
1326 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1327 BufferIn.dwOffsetLow = 0;
1328 BufferIn.dwOffsetHigh = 0;
1329
1330 SetLastError(0xdeadbeef);
1331 ret = HttpSendRequestExA(hRequest, &BufferIn, NULL, 0 ,0);
1332 error = GetLastError();
1333 ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1334 ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1335
1336 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1337
1338 for (i = 3; szPostData[i]; i++)
1339 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1340 "InternetWriteFile failed\n");
1341
1342 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1343
1344 ok(HttpEndRequestA(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1345
1346 test_request_flags(hRequest, 0);
1347
1348 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1349 "Unable to read response\n");
1350 szBuffer[dwBytesRead] = 0;
1351
1352 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1353 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1354
1355 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1356 done:
1357 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1358 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1359 }
1360
1361 static void InternetOpenRequest_test(void)
1362 {
1363 HINTERNET session, connect, request;
1364 static const char *types[] = { "*", "", NULL };
1365 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1366 static const WCHAR *typesW[] = { any, empty, NULL };
1367 BOOL ret;
1368
1369 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1370 ok(session != NULL ,"Unable to open Internet session\n");
1371
1372 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1373 INTERNET_SERVICE_HTTP, 0, 0);
1374 ok(connect == NULL, "InternetConnectA should have failed\n");
1375 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1376
1377 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1378 INTERNET_SERVICE_HTTP, 0, 0);
1379 ok(connect == NULL, "InternetConnectA should have failed\n");
1380 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1381
1382 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1383 INTERNET_SERVICE_HTTP, 0, 0);
1384 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1385
1386 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1387 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1388 {
1389 skip( "Network unreachable, skipping test\n" );
1390 goto done;
1391 }
1392 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1393
1394 ret = HttpSendRequestW(request, NULL, 0, NULL, 0);
1395 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1396 ok(InternetCloseHandle(request), "Close request handle failed\n");
1397
1398 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1399 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1400
1401 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1402 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1403 ok(InternetCloseHandle(request), "Close request handle failed\n");
1404
1405 done:
1406 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1407 ok(InternetCloseHandle(session), "Close session handle failed\n");
1408 }
1409
1410 static void test_cache_read(void)
1411 {
1412 HINTERNET session, connection, req;
1413 FILETIME now, tomorrow, yesterday;
1414 BYTE content[1000], buf[2000];
1415 char file_path[MAX_PATH];
1416 ULARGE_INTEGER li;
1417 HANDLE file;
1418 DWORD size;
1419 unsigned i;
1420 BOOL res;
1421
1422 static const char cache_only_url[] = "http://test.winehq.org/tests/cache-only";
1423 BYTE cache_headers[] = "HTTP/1.1 200 OK\r\n\r\n";
1424
1425 trace("Testing cache read...\n");
1426 reset_events();
1427
1428 for(i = 0; i < sizeof(content); i++)
1429 content[i] = '0' + (i%10);
1430
1431 GetSystemTimeAsFileTime(&now);
1432 li.u.HighPart = now.dwHighDateTime;
1433 li.u.LowPart = now.dwLowDateTime;
1434 li.QuadPart += (LONGLONG)10000000 * 3600 * 24;
1435 tomorrow.dwHighDateTime = li.u.HighPart;
1436 tomorrow.dwLowDateTime = li.u.LowPart;
1437 li.QuadPart -= (LONGLONG)10000000 * 3600 * 24 * 2;
1438 yesterday.dwHighDateTime = li.u.HighPart;
1439 yesterday.dwLowDateTime = li.u.LowPart;
1440
1441 res = CreateUrlCacheEntryA(cache_only_url, sizeof(content), "", file_path, 0);
1442 ok(res, "CreateUrlCacheEntryA failed: %u\n", GetLastError());
1443
1444 file = CreateFileA(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1445 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1446
1447 WriteFile(file, content, sizeof(content), &size, NULL);
1448 CloseHandle(file);
1449
1450 res = CommitUrlCacheEntryA(cache_only_url, file_path, tomorrow, yesterday, NORMAL_CACHE_ENTRY,
1451 cache_headers, sizeof(cache_headers)-1, "", 0);
1452 ok(res, "CommitUrlCacheEntryA failed: %u\n", GetLastError());
1453
1454 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
1455 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
1456
1457 pInternetSetStatusCallbackA(session, callback);
1458
1459 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1460 connection = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT,
1461 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1462 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
1463 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1464
1465 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1466 req = HttpOpenRequestA(connection, "GET", "/tests/cache-only", NULL, NULL, NULL, 0, 0xdeadbead);
1467 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1468 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1469
1470 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
1471 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
1472 SET_WINE_ALLOW(INTERNET_STATUS_SENDING_REQUEST);
1473 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_SENT);
1474 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1475 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1476 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1477
1478 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
1479 todo_wine
1480 ok(res, "HttpSendRequest failed: %u\n", GetLastError());
1481
1482 if(res) {
1483 size = 0;
1484 res = InternetQueryDataAvailable(req, &size, 0, 0);
1485 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
1486 ok(size == sizeof(content), "size = %u\n", size);
1487
1488 size = sizeof(buf);
1489 res = InternetReadFile(req, buf, sizeof(buf), &size);
1490 ok(res, "InternetReadFile failed: %u\n", GetLastError());
1491 ok(size == sizeof(content), "size = %u\n", size);
1492 ok(!memcmp(content, buf, sizeof(content)), "unexpected content\n");
1493 }
1494
1495 close_async_handle(session, 2);
1496
1497 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1498 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1499 CLEAR_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
1500 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
1501 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1502 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1503 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1504
1505 res = DeleteUrlCacheEntryA(cache_only_url);
1506 ok(res, "DeleteUrlCacheEntryA failed: %u\n", GetLastError());
1507 }
1508
1509 static void test_http_cache(void)
1510 {
1511 HINTERNET session, connect, request;
1512 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1513 DWORD size, file_size;
1514 BYTE buf[100];
1515 HANDLE file;
1516 BOOL ret;
1517 FILETIME filetime_zero = {0};
1518
1519 static const char cached_content[] = "data read from cache";
1520 static const char *types[] = { "*", "", NULL };
1521
1522 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1523 ok(session != NULL ,"Unable to open Internet session\n");
1524
1525 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1526 INTERNET_SERVICE_HTTP, 0, 0);
1527 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1528
1529 request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1530 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1531 {
1532 skip( "Network unreachable, skipping test\n" );
1533
1534 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1535 ok(InternetCloseHandle(session), "Close session handle failed\n");
1536
1537 return;
1538 }
1539 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1540
1541 size = sizeof(url);
1542 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1543 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1544 ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1545
1546 size = sizeof(file_name);
1547 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1548 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1549 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1550 ok(!size, "size = %d\n", size);
1551
1552 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1553 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1554
1555 size = sizeof(file_name);
1556 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1557 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1558
1559 file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1560 FILE_ATTRIBUTE_NORMAL, NULL);
1561 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1562 file_size = GetFileSize(file, NULL);
1563 ok(file_size == 106, "file size = %u\n", file_size);
1564
1565 size = sizeof(buf);
1566 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1567 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1568 ok(size == 100, "size = %u\n", size);
1569
1570 file_size = GetFileSize(file, NULL);
1571 ok(file_size == 106, "file size = %u\n", file_size);
1572 CloseHandle(file);
1573
1574 ret = DeleteFileA(file_name);
1575 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1576
1577 ok(InternetCloseHandle(request), "Close request handle failed\n");
1578
1579 file = CreateFileA(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1580 FILE_ATTRIBUTE_NORMAL, NULL);
1581 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1582 ret = WriteFile(file, cached_content, sizeof(cached_content), &size, NULL);
1583 ok(ret && size, "WriteFile failed: %d, %d\n", ret, size);
1584 ret = CommitUrlCacheEntryA(url, file_name, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, NULL, 0);
1585 ok(ret, "CommitUrlCacheEntry failed: %d\n", GetLastError());
1586 CloseHandle(file);
1587
1588 /* Send the same request, requiring it to be retrieved from the cache */
1589 request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1590
1591 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1592 ok(ret, "HttpSendRequest failed\n");
1593
1594 size = sizeof(buf);
1595 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1596 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1597 ok(size == 100, "size = %u\n", size);
1598 buf[99] = 0;
1599 todo_wine ok(!strcmp((char*)buf, cached_content), "incorrect page data: %s\n", (char*)buf);
1600
1601 ok(InternetCloseHandle(request), "Close request handle failed\n");
1602
1603 DeleteUrlCacheEntryA(url);
1604 request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1605 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1606 todo_wine ok(!ret, "HttpSendRequest succeeded\n");
1607 if(!ret)
1608 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() = %d\n", GetLastError());
1609 ok(InternetCloseHandle(request), "Close request handle failed\n");
1610
1611 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1612 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1613
1614 size = sizeof(file_name);
1615 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1616 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1617 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1618 ok(!size, "size = %d\n", size);
1619
1620 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1621 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1622
1623 size = sizeof(file_name);
1624 file_name[0] = 0;
1625 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1626 if (ret)
1627 {
1628 file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1629 FILE_ATTRIBUTE_NORMAL, NULL);
1630 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1631 CloseHandle(file);
1632 }
1633 else
1634 {
1635 /* < IE8 */
1636 ok(file_name[0] == 0, "Didn't expect a file name\n");
1637 }
1638
1639 ok(InternetCloseHandle(request), "Close request handle failed\n");
1640 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1641 ok(InternetCloseHandle(session), "Close session handle failed\n");
1642
1643 test_cache_read();
1644 }
1645
1646 static void InternetLockRequestFile_test(void)
1647 {
1648 char file_name[MAX_PATH];
1649 test_request_t req;
1650 HANDLE lock, lock2;
1651 DWORD size;
1652 BOOL ret;
1653
1654 open_simple_request(&req, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, "/tests/hello.html");
1655
1656 size = sizeof(file_name);
1657 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1658 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1659 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1660 ok(!size, "size = %d\n", size);
1661
1662 lock = NULL;
1663 ret = InternetLockRequestFile(req.request, &lock);
1664 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1665
1666 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
1667 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1668
1669 size = sizeof(file_name);
1670 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1671 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1672
1673 ret = InternetLockRequestFile(req.request, &lock);
1674 ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1675 ok(lock != NULL, "lock == NULL\n");
1676
1677 ret = InternetLockRequestFile(req.request, &lock2);
1678 ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1679 ok(lock == lock2, "lock != lock2\n");
1680
1681 ret = InternetUnlockRequestFile(lock2);
1682 ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1683
1684 ret = DeleteFileA(file_name);
1685 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1686
1687 ok(InternetCloseHandle(req.request), "Close request handle failed\n");
1688
1689 ret = DeleteFileA(file_name);
1690 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1691
1692 ret = InternetUnlockRequestFile(lock);
1693 ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1694
1695 ret = DeleteFileA(file_name);
1696 ok(ret, "Deleting file returned %x(%u)\n", ret, GetLastError());
1697 }
1698
1699 static void HttpHeaders_test(void)
1700 {
1701 HINTERNET hSession;
1702 HINTERNET hConnect;
1703 HINTERNET hRequest;
1704 CHAR buffer[256];
1705 WCHAR wbuffer[256];
1706 DWORD len = 256;
1707 DWORD oldlen;
1708 DWORD index = 0;
1709 BOOL ret;
1710
1711 hSession = InternetOpenA("Wine Regression Test",
1712 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1713 ok( hSession != NULL ,"Unable to open Internet session\n");
1714 hConnect = InternetConnectA(hSession, "test.winehq.org",
1715 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1716 0);
1717 ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1718 hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1719 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1720 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1721 {
1722 skip( "Network unreachable, skipping test\n" );
1723 goto done;
1724 }
1725 ok( hRequest != NULL, "Failed to open request handle\n");
1726
1727 index = 0;
1728 len = sizeof(buffer);
1729 strcpy(buffer,"Warning");
1730 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1731 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1732
1733 ok(HttpAddRequestHeadersA(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1734 "Failed to add new header\n");
1735
1736 index = 0;
1737 len = sizeof(buffer);
1738 strcpy(buffer,"Warning");
1739 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1740 buffer,&len,&index),"Unable to query header\n");
1741 ok(index == 1, "Index was not incremented\n");
1742 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1743 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1744 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1745 len = sizeof(buffer);
1746 strcpy(buffer,"Warning");
1747 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1748 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1749
1750 index = 0;
1751 len = 5; /* could store the string but not the NULL terminator */
1752 strcpy(buffer,"Warning");
1753 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1754 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1755 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1756 ok(index == 0, "Index was incremented\n");
1757 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1758 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1759
1760 /* a call with NULL will fail but will return the length */
1761 index = 0;
1762 len = sizeof(buffer);
1763 SetLastError(0xdeadbeef);
1764 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1765 NULL,&len,&index) == FALSE,"Query worked\n");
1766 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1767 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1768 ok(index == 0, "Index was incremented\n");
1769
1770 /* even for a len that is too small */
1771 index = 0;
1772 len = 15;
1773 SetLastError(0xdeadbeef);
1774 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1775 NULL,&len,&index) == FALSE,"Query worked\n");
1776 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1777 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1778 ok(index == 0, "Index was incremented\n");
1779
1780 index = 0;
1781 len = 0;
1782 SetLastError(0xdeadbeef);
1783 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1784 NULL,&len,&index) == FALSE,"Query worked\n");
1785 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1786 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1787 ok(index == 0, "Index was incremented\n");
1788 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1789
1790
1791 /* a working query */
1792 index = 0;
1793 len = sizeof(buffer);
1794 memset(buffer, 'x', sizeof(buffer));
1795 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1796 buffer,&len,&index),"Unable to query header\n");
1797 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1798 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1799 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1800 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1801 ok(strncmp(buffer, "POST /tests/post.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1802 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1803 ok(index == 0, "Index was incremented\n");
1804
1805 /* Like above two tests, but for W version */
1806
1807 index = 0;
1808 len = 0;
1809 SetLastError(0xdeadbeef);
1810 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1811 NULL,&len,&index) == FALSE,"Query worked\n");
1812 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1813 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1814 ok(index == 0, "Index was incremented\n");
1815 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1816
1817 /* a working query */
1818 index = 0;
1819 len = sizeof(wbuffer);
1820 memset(wbuffer, 'x', sizeof(wbuffer));
1821 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1822 wbuffer,&len,&index),"Unable to query header\n");
1823 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1824 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1825 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1826 ok(index == 0, "Index was incremented\n");
1827
1828 /* end of W version tests */
1829
1830 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1831 index = 0;
1832 len = sizeof(buffer);
1833 memset(buffer, 'x', sizeof(buffer));
1834 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1835 buffer,&len,&index) == TRUE,"Query failed\n");
1836 ok(len == 2 || len == 4 /* win10 */, "Expected 2 or 4, got %d\n", len);
1837 ok(memcmp(buffer, "\r\n\r\n", len) == 0, "Expected CRLF, got '%s'\n", buffer);
1838 ok(index == 0, "Index was incremented\n");
1839
1840 ok(HttpAddRequestHeadersA(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1841 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1842
1843 index = 0;
1844 len = sizeof(buffer);
1845 strcpy(buffer,"Warning");
1846 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1847 buffer,&len,&index),"Unable to query header\n");
1848 ok(index == 1, "Index was not incremented\n");
1849 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1850 len = sizeof(buffer);
1851 strcpy(buffer,"Warning");
1852 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1853 buffer,&len,&index),"Failed to get second header\n");
1854 ok(index == 2, "Index was not incremented\n");
1855 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1856 len = sizeof(buffer);
1857 strcpy(buffer,"Warning");
1858 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1859 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1860
1861 ok(HttpAddRequestHeadersA(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1862
1863 index = 0;
1864 len = sizeof(buffer);
1865 strcpy(buffer,"Warning");
1866 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1867 buffer,&len,&index),"Unable to query header\n");
1868 ok(index == 1, "Index was not incremented\n");
1869 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1870 len = sizeof(buffer);
1871 strcpy(buffer,"Warning");
1872 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1873 buffer,&len,&index),"Failed to get second header\n");
1874 ok(index == 2, "Index was not incremented\n");
1875 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1876 len = sizeof(buffer);
1877 strcpy(buffer,"Warning");
1878 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1879 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1880
1881 ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1882
1883 index = 0;
1884 len = sizeof(buffer);
1885 strcpy(buffer,"Warning");
1886 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1887 buffer,&len,&index),"Unable to query header\n");
1888 ok(index == 1, "Index was not incremented\n");
1889 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1890 len = sizeof(buffer);
1891 strcpy(buffer,"Warning");
1892 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1893 buffer,&len,&index),"Failed to get second header\n");
1894 ok(index == 2, "Index was not incremented\n");
1895 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1896 len = sizeof(buffer);
1897 strcpy(buffer,"Warning");
1898 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1899 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1900
1901 ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1902
1903 index = 0;
1904 len = sizeof(buffer);
1905 strcpy(buffer,"Warning");
1906 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1907 buffer,&len,&index),"Unable to query header\n");
1908 ok(index == 1, "Index was not incremented\n");
1909 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1910 len = sizeof(buffer);
1911 strcpy(buffer,"Warning");
1912 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1913 ok(index == 2, "Index was not incremented\n");
1914 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1915 len = sizeof(buffer);
1916 strcpy(buffer,"Warning");
1917 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1918
1919 ok(HttpAddRequestHeadersA(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1920
1921 index = 0;
1922 len = sizeof(buffer);
1923 strcpy(buffer,"Warning");
1924 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1925 ok(index == 1, "Index was not incremented\n");
1926 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1927 len = sizeof(buffer);
1928 strcpy(buffer,"Warning");
1929 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1930 ok(index == 2, "Index was not incremented\n");
1931 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1932 len = sizeof(buffer);
1933 strcpy(buffer,"Warning");
1934 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1935
1936 ok(HttpAddRequestHeadersA(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1937
1938 index = 0;
1939 len = sizeof(buffer);
1940 strcpy(buffer,"Warning");
1941 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1942 ok(index == 1, "Index was not incremented\n");
1943 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1944 len = sizeof(buffer);
1945 strcpy(buffer,"Warning");
1946 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1947 ok(index == 2, "Index was not incremented\n");
1948 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1949 len = sizeof(buffer);
1950 strcpy(buffer,"Warning");
1951 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1952
1953 ok(HttpAddRequestHeadersA(hRequest,"Warning:test7",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE), "HTTP_ADDREQ_FLAG_ADD with HTTP_ADDREQ_FLAG_REPALCE Did not work\n");
1954
1955 index = 0;
1956 len = sizeof(buffer);
1957 strcpy(buffer,"Warning");
1958 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1959 ok(index == 1, "Index was not incremented\n");
1960 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1961 len = sizeof(buffer);
1962 strcpy(buffer,"Warning");
1963 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1964 ok(index == 2, "Index was not incremented\n");
1965 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1966 len = sizeof(buffer);
1967 strcpy(buffer,"Warning");
1968 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1969
1970 /* Ensure that blank headers are ignored and don't cause a failure */
1971 ok(HttpAddRequestHeadersA(hRequest,"\r\nBlankTest:value\r\n\r\n",-1, HTTP_ADDREQ_FLAG_ADD_IF_NEW), "Failed to add header with blank entries in list\n");
1972
1973 index = 0;
1974 len = sizeof(buffer);
1975 strcpy(buffer,"BlankTest");
1976 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1977 ok(index == 1, "Index was not incremented\n");
1978 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1979
1980 /* Ensure that malformed header separators are ignored and don't cause a failure */
1981 ok(HttpAddRequestHeadersA(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1982 "Failed to add header with malformed entries in list\n");
1983
1984 index = 0;
1985 len = sizeof(buffer);
1986 strcpy(buffer,"MalformedTest");
1987 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1988 ok(index == 1, "Index was not incremented\n");
1989 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1990 index = 0;
1991 len = sizeof(buffer);
1992 strcpy(buffer,"MalformedTestTwo");
1993 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1994 ok(index == 1, "Index was not incremented\n");
1995 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1996 index = 0;
1997 len = sizeof(buffer);
1998 strcpy(buffer,"MalformedTestThree");
1999 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2000 ok(index == 1, "Index was not incremented\n");
2001 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
2002
2003 ret = HttpAddRequestHeadersA(hRequest, "Authorization: Basic\r\n", -1, HTTP_ADDREQ_FLAG_ADD);
2004 ok(ret, "unable to add header %u\n", GetLastError());
2005
2006 index = 0;
2007 buffer[0] = 0;
2008 len = sizeof(buffer);
2009 ret = HttpQueryInfoA(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index);
2010 ok(ret, "unable to query header %u\n", GetLastError());
2011 ok(index == 1, "index was not incremented\n");
2012 ok(!strcmp(buffer, "Basic"), "incorrect string was returned (%s)\n", buffer);
2013
2014 ret = HttpAddRequestHeadersA(hRequest, "Authorization:\r\n", -1, HTTP_ADDREQ_FLAG_REPLACE);
2015 ok(ret, "unable to remove header %u\n", GetLastError());
2016
2017 index = 0;
2018 len = sizeof(buffer);
2019 SetLastError(0xdeadbeef);
2020 ok(!HttpQueryInfoA(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index),
2021 "header still present\n");
2022 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "got %u\n", GetLastError());
2023
2024 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
2025 done:
2026 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
2027 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
2028 }
2029
2030 static const char garbagemsg[] =
2031 "Garbage: Header\r\n";
2032
2033 static const char contmsg[] =
2034 "HTTP/1.1 100 Continue\r\n";
2035
2036 static const char expandcontmsg[] =
2037 "HTTP/1.1 100 Continue\r\n"
2038 "Server: winecontinue\r\n"
2039 "Tag: something witty\r\n";
2040
2041 static const char okmsg[] =
2042 "HTTP/1.1 200 OK\r\n"
2043 "Server: winetest\r\n"
2044 "\r\n";
2045
2046 static const char okmsg2[] =
2047 "HTTP/1.1 200 OK\r\n"
2048 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2049 "Server: winetest\r\n"
2050 "Content-Length: 0\r\n"
2051 "Set-Cookie: one\r\n"
2052 "Set-Cookie: two\r\n"
2053 "\r\n";
2054
2055 static const char okmsg_cookie_path[] =
2056 "HTTP/1.1 200 OK\r\n"
2057 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2058 "Server: winetest\r\n"
2059 "Content-Length: 0\r\n"
2060 "Set-Cookie: subcookie2=data; path=/test_cookie_set_path\r\n"
2061 "\r\n";
2062
2063 static const char okmsg_cookie[] =
2064 "HTTP/1.1 200 OK\r\n"
2065 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2066 "Server: winetest\r\n"
2067 "Content-Length: 0\r\n"
2068 "Set-Cookie: testcookie=testvalue\r\n"
2069 "\r\n";
2070
2071 static const char notokmsg[] =
2072 "HTTP/1.1 400 Bad Request\r\n"
2073 "Server: winetest\r\n"
2074 "\r\n";
2075
2076 static const char noauthmsg[] =
2077 "HTTP/1.1 401 Unauthorized\r\n"
2078 "Server: winetest\r\n"
2079 "Connection: close\r\n"
2080 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
2081 "\r\n";
2082
2083 static const char noauthmsg2[] =
2084 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
2085 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
2086 "\0d`0|6\n"
2087 "Server: winetest\r\n";
2088
2089 static const char proxymsg[] =
2090 "HTTP/1.1 407 Proxy Authentication Required\r\n"
2091 "Server: winetest\r\n"
2092 "Proxy-Connection: close\r\n"
2093 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
2094 "\r\n";
2095
2096 static const char page1[] =
2097 "<HTML>\r\n"
2098 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
2099 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
2100 "</HTML>\r\n\r\n";
2101
2102 static const char ok_with_length[] =
2103 "HTTP/1.1 200 OK\r\n"
2104 "Connection: Keep-Alive\r\n"
2105 "Content-Length: 18\r\n\r\n"
2106 "HTTP/1.1 211 OK\r\n\r\n";
2107
2108 static const char ok_with_length2[] =
2109 "HTTP/1.1 210 OK\r\n"
2110 "Connection: Keep-Alive\r\n"
2111 "Content-Length: 19\r\n\r\n"
2112 "HTTP/1.1 211 OK\r\n\r\n";
2113
2114 struct server_info {
2115 HANDLE hEvent;
2116 int port;
2117 };
2118
2119 static int test_cache_gzip;
2120 static const char *send_buffer;
2121 static int server_socket;
2122
2123 static DWORD CALLBACK server_thread(LPVOID param)
2124 {
2125 struct server_info *si = param;
2126 int r, c = -1, i, on, count = 0;
2127 SOCKET s;
2128 struct sockaddr_in sa;
2129 char *buffer;
2130 size_t buffer_size;
2131 WSADATA wsaData;
2132 int last_request = 0;
2133 char host_header[22];
2134 char host_header_override[30];
2135 static int test_no_cache = 0;
2136
2137 WSAStartup(MAKEWORD(1,1), &wsaData);
2138
2139 s = socket(AF_INET, SOCK_STREAM, 0);
2140 if (s == INVALID_SOCKET)
2141 return 1;
2142
2143 on = 1;
2144 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
2145
2146 memset(&sa, 0, sizeof sa);
2147 sa.sin_family = AF_INET;
2148 sa.sin_port = htons(si->port);
2149 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
2150
2151 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
2152 if (r<0)
2153 return 1;
2154
2155 listen(s, 0);
2156
2157 SetEvent(si->hEvent);
2158
2159 sprintf(host_header, "Host: localhost:%d", si->port);
2160 sprintf(host_header_override, "Host: test.local:%d\r\n", si->port);
2161 buffer = HeapAlloc(GetProcessHeap(), 0, buffer_size = 1000);
2162
2163 do
2164 {
2165 if(c == -1)
2166 c = accept(s, NULL, NULL);
2167
2168 memset(buffer, 0, buffer_size);
2169 for(i=0;; i++)
2170 {
2171 if(i == buffer_size)
2172 buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, buffer_size *= 2);
2173
2174 r = recv(c, buffer+i, 1, 0);
2175 if (r != 1)
2176 break;
2177 if (i<4) continue;
2178 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
2179 buffer[i-3] == '\r' && buffer[i-1] == '\r')
2180 break;
2181 }
2182 if (strstr(buffer, "GET /test1"))
2183 {
2184 if (!strstr(buffer, "Content-Length: 0"))
2185 {
2186 send(c, okmsg, sizeof okmsg-1, 0);
2187 send(c, page1, sizeof page1-1, 0);
2188 }
2189 else
2190 send(c, notokmsg, sizeof notokmsg-1, 0);
2191 }
2192 if (strstr(buffer, "CONNECT "))
2193 {
2194 if (!strstr(buffer, "Content-Length: 0"))
2195 send(c, notokmsg, sizeof notokmsg-1, 0);
2196 else
2197 send(c, proxymsg, sizeof proxymsg-1, 0);
2198 }
2199 if (strstr(buffer, "/test2"))
2200 {
2201 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
2202 {
2203 send(c, okmsg, sizeof okmsg-1, 0);
2204 send(c, page1, sizeof page1-1, 0);
2205 }
2206 else
2207 send(c, proxymsg, sizeof proxymsg-1, 0);
2208 }
2209 if (strstr(buffer, "/test3"))
2210 {
2211 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2212 send(c, okmsg, sizeof okmsg-1, 0);
2213 else
2214 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2215 }
2216 if (strstr(buffer, "/test4"))
2217 {
2218 if (strstr(buffer, "Connection: Close"))
2219 send(c, okmsg, sizeof okmsg-1, 0);
2220 else
2221 send(c, notokmsg, sizeof notokmsg-1, 0);
2222 }
2223 if (strstr(buffer, "POST /test5") ||
2224 strstr(buffer, "RPC_IN_DATA /test5") ||
2225 strstr(buffer, "RPC_OUT_DATA /test5"))
2226 {
2227 if (strstr(buffer, "Content-Length: 0"))
2228 {
2229 send(c, okmsg, sizeof okmsg-1, 0);
2230 send(c, page1, sizeof page1-1, 0);
2231 }
2232 else
2233 send(c, notokmsg, sizeof notokmsg-1, 0);
2234 }
2235 if (strstr(buffer, "GET /test6"))
2236 {
2237 send(c, contmsg, sizeof contmsg-1, 0);
2238 send(c, contmsg, sizeof contmsg-1, 0);
2239 send(c, okmsg, sizeof okmsg-1, 0);
2240 send(c, page1, sizeof page1-1, 0);
2241 }
2242 if (strstr(buffer, "POST /test7"))
2243 {
2244 if (strstr(buffer, "Content-Length: 100"))
2245 {
2246 if (strstr(buffer, "POST /test7b"))
2247 recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
2248 send(c, okmsg, sizeof okmsg-1, 0);
2249 send(c, page1, sizeof page1-1, 0);
2250 }
2251 else
2252 send(c, notokmsg, sizeof notokmsg-1, 0);
2253 }
2254 if (strstr(buffer, "/test8"))
2255 {
2256 if (!strstr(buffer, "Connection: Close") &&
2257 strstr(buffer, "Connection: Keep-Alive") &&
2258 !strstr(buffer, "Cache-Control: no-cache") &&
2259 !strstr(buffer, "Pragma: no-cache") &&
2260 strstr(buffer, host_header))
2261 send(c, okmsg, sizeof okmsg-1, 0);
2262 else
2263 send(c, notokmsg, sizeof notokmsg-1, 0);
2264 }
2265 if (strstr(buffer, "/test9"))
2266 {
2267 if (!strstr(buffer, "Connection: Close") &&
2268 !strstr(buffer, "Connection: Keep-Alive") &&
2269 !strstr(buffer, "Cache-Control: no-cache") &&
2270 !strstr(buffer, "Pragma: no-cache") &&
2271 strstr(buffer, host_header))
2272 send(c, okmsg, sizeof okmsg-1, 0);
2273 else
2274 send(c, notokmsg, sizeof notokmsg-1, 0);
2275 }
2276 if (strstr(buffer, "/testA"))
2277 {
2278 if (!strstr(buffer, "Connection: Close") &&
2279 !strstr(buffer, "Connection: Keep-Alive") &&
2280 (strstr(buffer, "Cache-Control: no-cache") ||
2281 strstr(buffer, "Pragma: no-cache")) &&
2282 strstr(buffer, host_header))
2283 send(c, okmsg, sizeof okmsg-1, 0);
2284 else
2285 send(c, notokmsg, sizeof notokmsg-1, 0);
2286 }
2287 if (strstr(buffer, "/testC"))
2288 {
2289 if (strstr(buffer, "cookie=biscuit"))
2290 send(c, okmsg, sizeof okmsg-1, 0);
2291 else
2292 send(c, notokmsg, sizeof notokmsg-1, 0);
2293 }
2294 if (strstr(buffer, "/testD"))
2295 {
2296 send(c, okmsg2, sizeof okmsg2-1, 0);
2297 }
2298 if (strstr(buffer, "/testE"))
2299 {
2300 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
2301 }
2302 if (strstr(buffer, "GET /quit"))
2303 {
2304 send(c, okmsg, sizeof okmsg-1, 0);
2305 send(c, page1, sizeof page1-1, 0);
2306 last_request = 1;
2307 }
2308 if (strstr(buffer, "GET /testF"))
2309 {
2310 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
2311 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2312 send(c, contmsg, sizeof contmsg-1, 0);
2313 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2314 send(c, okmsg, sizeof okmsg-1, 0);
2315 send(c, page1, sizeof page1-1, 0);
2316 }
2317 if (strstr(buffer, "GET /testG"))
2318 {
2319 send(c, page1, sizeof page1-1, 0);
2320 }
2321
2322 if (strstr(buffer, "GET /testJ"))
2323 {
2324 if (count == 0)
2325 {
2326 count++;
2327 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2328 }
2329 else
2330 {
2331 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2332 count = 0;
2333 }
2334 }
2335 if (strstr(buffer, "GET /testH"))
2336 {
2337 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2338 recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
2339 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2340 }
2341
2342 if (strstr(buffer, "GET /test_no_content"))
2343 {
2344 static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n";
2345 send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2346 }
2347 if (strstr(buffer, "GET /test_conn_close"))
2348 {
2349 static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
2350 send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
2351 WaitForSingleObject(conn_close_event, INFINITE);
2352 trace("closing connection\n");
2353 }
2354 if (strstr(buffer, "GET /test_cache_control_no_cache"))
2355 {
2356 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\n\r\nsome content";
2357 if(!test_no_cache++)
2358 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2359 else
2360 send(c, okmsg, sizeof(okmsg)-1, 0);
2361 }
2362 if (strstr(buffer, "GET /test_cache_control_no_store"))
2363 {
2364 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: junk, \t No-StOrE\r\n\r\nsome content";
2365 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2366 }
2367 if (strstr(buffer, "GET /test_cache_gzip"))
2368 {
2369 static const char gzip_response[] = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\n\r\n"
2370 "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xaf\xca\x2c\x50\x28"
2371 "\x49\x2d\x2e\xe1\x02\x00\x62\x92\xc7\x6c\x0a\x00\x00\x00";
2372 if(!test_cache_gzip++)
2373 send(c, gzip_response, sizeof(gzip_response), 0);
2374 else
2375 send(c, notokmsg, sizeof(notokmsg)-1, 0);
2376 }
2377 if (strstr(buffer, "HEAD /test_head")) {
2378 static const char head_response[] =
2379 "HTTP/1.1 200 OK\r\n"
2380 "Connection: Keep-Alive\r\n"
2381 "Content-Length: 100\r\n"
2382 "\r\n";
2383
2384 send(c, head_response, sizeof(head_response), 0);
2385 continue;
2386 }
2387 if (strstr(buffer, "GET /send_from_buffer"))
2388 send(c, send_buffer, strlen(send_buffer), 0);
2389 if (strstr(buffer, "/test_cache_control_verb"))
2390 {
2391 if (!memcmp(buffer, "GET ", sizeof("GET ")-1) &&
2392 !strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2393 else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2394 else send(c, notokmsg, sizeof(notokmsg)-1, 0);
2395 }
2396 if (strstr(buffer, "/test_request_content_length"))
2397 {
2398 static char msg[] = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\n\r\n";
2399 static int seen_content_length;
2400
2401 if (!seen_content_length)
2402 {
2403 if (strstr(buffer, "Content-Length: 0"))
2404 {
2405 seen_content_length = 1;
2406 send(c, msg, sizeof msg-1, 0);
2407 }
2408 else send(c, notokmsg, sizeof notokmsg-1, 0);
2409 WaitForSingleObject(complete_event, 5000);
2410 }
2411 else
2412 {
2413 if (strstr(buffer, "Content-Length: 0")) send(c, msg, sizeof msg-1, 0);
2414 else send(c, notokmsg, sizeof notokmsg-1, 0);
2415 WaitForSingleObject(complete_event, 5000);
2416 }
2417 }
2418 if (strstr(buffer, "GET /test_premature_disconnect"))
2419 trace("closing connection\n");
2420 if (strstr(buffer, "HEAD /upload.txt"))
2421 {
2422 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2423 send(c, okmsg, sizeof okmsg-1, 0);
2424 else
2425 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2426 }
2427 if (strstr(buffer, "PUT /upload2.txt"))
2428 {
2429 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2430 send(c, okmsg, sizeof okmsg-1, 0);
2431 else
2432 send(c, notokmsg, sizeof notokmsg-1, 0);
2433 }
2434 if (strstr(buffer, "/test_cookie_path1"))
2435 {
2436 if (strstr(buffer, "subcookie=data"))
2437 send(c, okmsg, sizeof okmsg-1, 0);
2438 else
2439 send(c, notokmsg, sizeof notokmsg-1, 0);
2440 }
2441 if (strstr(buffer, "/test_cookie_path2"))
2442 {
2443 if (strstr(buffer, "subcookie2=data"))
2444 send(c, okmsg, sizeof okmsg-1, 0);
2445 else
2446 send(c, notokmsg, sizeof notokmsg-1, 0);
2447 }
2448 if (strstr(buffer, "/test_cookie_set_path"))
2449 {
2450 send(c, okmsg_cookie_path, sizeof okmsg_cookie_path-1, 0);
2451 }
2452 if (strstr(buffer, "/test_cookie_merge"))
2453 {
2454 if (strstr(buffer, "subcookie=data") &&
2455 !strstr(buffer, "manual_cookie=test"))
2456 send(c, okmsg, sizeof okmsg-1, 0);
2457 else
2458 send(c, notokmsg, sizeof notokmsg-1, 0);
2459 }
2460 if (strstr(buffer, "/test_cookie_set_host_override"))
2461 {
2462 send(c, okmsg_cookie, sizeof okmsg_cookie-1, 0);
2463 }
2464 if (strstr(buffer, "/test_cookie_check_host_override"))
2465 {
2466 if (strstr(buffer, "Cookie:") && strstr(buffer, "testcookie=testvalue"))
2467 send(c, okmsg, sizeof okmsg-1, 0);
2468 else
2469 send(c, notokmsg, sizeof notokmsg-1, 0);
2470 }
2471 if (strstr(buffer, "/test_cookie_check_different_host"))
2472 {
2473 if (!strstr(buffer, "foo") &&
2474 strstr(buffer, "cookie=biscuit"))
2475 send(c, okmsg, sizeof okmsg-1, 0);
2476 else
2477 send(c, notokmsg, sizeof notokmsg-1, 0);
2478 }
2479 if (strstr(buffer, "/test_host_override"))
2480 {
2481 if (strstr(buffer, host_header_override))
2482 send(c, okmsg, sizeof okmsg-1, 0);
2483 else
2484 send(c, notokmsg, sizeof notokmsg-1, 0);
2485 }
2486 if (strstr(buffer, "/async_read"))
2487 {
2488 const char *page1_mid = page1 + (sizeof page1 - 1)/2;
2489 const char *page1_end = page1 + sizeof page1 - 1;
2490 send(c, okmsg, sizeof okmsg-1, 0);
2491 send(c, page1, page1_mid - page1, 0);
2492 WaitForSingleObject(conn_wait_event, INFINITE);
2493 send(c, page1_mid, page1_end - page1_mid, 0);
2494 }
2495 if (strstr(buffer, "/socket"))
2496 {
2497 server_socket = c;
2498 SetEvent(server_req_rec_event);
2499 WaitForSingleObject(conn_wait_event, INFINITE);
2500 }
2501 if (strstr(buffer, "/echo_request"))
2502 {
2503 send(c, okmsg, sizeof(okmsg)-1, 0);
2504 send(c, buffer, strlen(buffer), 0);
2505 }
2506 if (strstr(buffer, "GET /test_remove_dot_segments"))
2507 {
2508 send(c, okmsg, sizeof(okmsg)-1, 0);
2509 }
2510 if (strstr(buffer, "HEAD /test_auth_host1"))
2511 {
2512 if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzcw=="))
2513 send(c, okmsg, sizeof okmsg-1, 0);
2514 else
2515 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2516 }
2517 if (strstr(buffer, "HEAD /test_auth_host2"))
2518 {
2519 if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzczI="))
2520 send(c, okmsg, sizeof okmsg-1, 0);
2521 else
2522 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2523 }
2524 shutdown(c, 2);
2525 closesocket(c);
2526 c = -1;
2527 } while (!last_request);
2528
2529 closesocket(s);
2530 HeapFree(GetProcessHeap(), 0, buffer);
2531
2532 return 0;
2533 }
2534
2535 static void test_basic_request(int port, const char *verb, const char *url)
2536 {
2537 test_request_t req;
2538 DWORD r, count, error;
2539 char buffer[0x100];
2540
2541 trace("basic request %s %s\n", verb, url);
2542
2543 open_simple_request(&req, "localhost", port, verb, url);
2544
2545 SetLastError(0xdeadbeef);
2546 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2547 error = GetLastError();
2548 ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
2549 ok(r, "HttpSendRequest failed: %u\n", GetLastError());
2550
2551 count = 0;
2552 memset(buffer, 0, sizeof buffer);
2553 SetLastError(0xdeadbeef);
2554 r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
2555 ok(r, "InternetReadFile failed %u\n", GetLastError());
2556 ok(count == sizeof page1 - 1, "count was wrong\n");
2557 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
2558
2559 close_request(&req);
2560 }
2561
2562 static void test_proxy_indirect(int port)
2563 {
2564 test_request_t req;
2565 DWORD r, sz;
2566 char buffer[0x40];
2567
2568 open_simple_request(&req, "localhost", port, NULL, "/test2");
2569
2570 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2571 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2572
2573 sz = sizeof buffer;
2574 r = HttpQueryInfoA(req.request, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
2575 ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
2576 if (!r)
2577 {
2578 skip("missing proxy header, not testing remaining proxy headers\n");
2579 goto out;
2580 }
2581 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
2582
2583 test_status_code(req.request, 407);
2584 test_request_flags(req.request, 0);
2585
2586 sz = sizeof buffer;
2587 r = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
2588 ok(r, "HttpQueryInfo failed\n");
2589 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
2590
2591 sz = sizeof buffer;
2592 r = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &sz, NULL);
2593 ok(r, "HttpQueryInfo failed\n");
2594 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
2595
2596 sz = sizeof buffer;
2597 r = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &sz, NULL);
2598 ok(r, "HttpQueryInfo failed\n");
2599 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
2600
2601 sz = sizeof buffer;
2602 r = HttpQueryInfoA(req.request, HTTP_QUERY_CONTENT_ENCODING,