wininet/tests: Collect persistent connection before running tests in test_async_HttpS...
[wine.git] / dlls / wininet / tests / http.c
blobaadedd27f244c3f21a3a9c95550446948691b0b6
1 /*
2 * Wininet - HTTP tests
4 * Copyright 2002 Aric Stewart
5 * Copyright 2004 Mike McCormack
6 * Copyright 2005 Hans Leidekker
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.
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.
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
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <limits.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wininet.h"
31 #include "winineti.h"
32 #include "winsock2.h"
34 #include "wine/test.h"
36 /* Undocumented security flags */
37 #define _SECURITY_FLAG_CERT_REV_FAILED 0x00800000
38 #define _SECURITY_FLAG_CERT_INVALID_CA 0x01000000
39 #define _SECURITY_FLAG_CERT_INVALID_CN 0x02000000
40 #define _SECURITY_FLAG_CERT_INVALID_DATE 0x04000000
42 #define TEST_URL "http://test.winehq.org/tests/hello.html"
44 static BOOL first_connection_to_test_url = TRUE;
45 static BOOL https_support = TRUE;
47 /* Adapted from dlls/urlmon/tests/protocol.c */
49 #define SET_EXPECT2(status, num) \
50 expect[status] = num
52 #define SET_EXPECT(status) \
53 SET_EXPECT2(status, 1)
55 #define SET_OPTIONAL2(status, num) \
56 optional[status] = num
58 #define SET_OPTIONAL(status) \
59 SET_OPTIONAL2(status, 1)
61 /* SET_WINE_ALLOW's should be used with an appropriate
62 * todo_wine CHECK_NOTIFIED at a later point in the code */
63 #define SET_WINE_ALLOW2(status, num) \
64 wine_allow[status] = num
66 #define SET_WINE_ALLOW(status) \
67 SET_WINE_ALLOW2(status, 1)
69 #define CHECK_EXPECT(status) \
70 do { \
71 if (!expect[status] && !optional[status] && wine_allow[status]) \
72 { \
73 todo_wine ok(expect[status], "unexpected status %ld (%s)\n", status, \
74 status < MAX_INTERNET_STATUS && status_string[status] ? \
75 status_string[status] : "unknown"); \
76 wine_allow[status]--; \
77 } \
78 else \
79 { \
80 ok(expect[status] || optional[status], "unexpected status %ld (%s)\n", status, \
81 status < MAX_INTERNET_STATUS && status_string[status] ? \
82 status_string[status] : "unknown"); \
83 if (expect[status]) expect[status]--; \
84 else if(optional[status]) optional[status]--; \
85 } \
86 notified[status]++; \
87 }while(0)
89 /* CLEAR_NOTIFIED used in cases when notification behavior
90 * differs between Windows versions */
91 #define CLEAR_NOTIFIED(status) \
92 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
94 #define CHECK_NOTIFIED2(status, num) \
95 do { \
96 ok(notified[status] + optional[status] == (num), \
97 "expected status %d (%s) %d times, received %d times\n", \
98 status, status < MAX_INTERNET_STATUS && status_string[status] ? \
99 status_string[status] : "unknown", (num), notified[status]); \
100 CLEAR_NOTIFIED(status); \
101 }while(0)
103 #define CHECK_NOTIFIED(status) \
104 CHECK_NOTIFIED2(status, 1)
106 #define CHECK_NOT_NOTIFIED(status) \
107 CHECK_NOTIFIED2(status, 0)
109 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
110 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
111 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
112 static const char *status_string[MAX_INTERNET_STATUS];
114 static HANDLE complete_event, conn_close_event, conn_wait_event, server_req_rec_event, request_sent_event;
115 static DWORD req_error;
116 static BOOL is_ie7plus = TRUE;
118 #define TESTF_REDIRECT 0x01
119 #define TESTF_COMPRESSED 0x02
120 #define TESTF_CHUNKED 0x04
122 typedef struct {
123 const char *url;
124 const char *redirected_url;
125 const char *host;
126 const char *path;
127 const char *headers;
128 DWORD flags;
129 const char *post_data;
130 const char *content;
131 } test_data_t;
133 static const test_data_t test_data[] = {
135 "http://test.winehq.org/tests/data.php",
136 "http://test.winehq.org/tests/data.php",
137 "test.winehq.org",
138 "/tests/data.php",
140 TESTF_CHUNKED
143 "http://test.winehq.org/tests/redirect",
144 "http://test.winehq.org/tests/hello.html",
145 "test.winehq.org",
146 "/tests/redirect",
148 TESTF_REDIRECT
151 "http://test.winehq.org/tests/gzip.php",
152 "http://test.winehq.org/tests/gzip.php",
153 "test.winehq.org",
154 "/tests/gzip.php",
155 "Accept-Encoding: gzip, deflate",
156 TESTF_COMPRESSED
159 "http://test.winehq.org/tests/post.php",
160 "http://test.winehq.org/tests/post.php",
161 "test.winehq.org",
162 "/tests/post.php",
163 "Content-Type: application/x-www-form-urlencoded",
165 "mode=Test",
166 "mode => Test\n"
170 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
171 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackW)(HINTERNET ,INTERNET_STATUS_CALLBACK);
172 static BOOL (WINAPI *pInternetGetSecurityInfoByURLA)(LPSTR,PCCERT_CHAIN_CONTEXT*,DWORD*);
174 static BOOL is_lang_english(void)
176 static HMODULE hkernel32 = NULL;
177 static LANGID (WINAPI *pGetThreadUILanguage)(void) = NULL;
178 static LANGID (WINAPI *pGetUserDefaultUILanguage)(void) = NULL;
180 if (!hkernel32)
182 hkernel32 = GetModuleHandleA("kernel32.dll");
183 pGetThreadUILanguage = (void*)GetProcAddress(hkernel32, "GetThreadUILanguage");
184 pGetUserDefaultUILanguage = (void*)GetProcAddress(hkernel32, "GetUserDefaultUILanguage");
186 if (pGetThreadUILanguage)
187 return PRIMARYLANGID(pGetThreadUILanguage()) == LANG_ENGLISH;
188 if (pGetUserDefaultUILanguage)
189 return PRIMARYLANGID(pGetUserDefaultUILanguage()) == LANG_ENGLISH;
191 return PRIMARYLANGID(GetUserDefaultLangID()) == LANG_ENGLISH;
194 static int strcmp_wa(LPCWSTR strw, const char *stra)
196 WCHAR buf[512];
197 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, ARRAY_SIZE(buf));
198 return lstrcmpW(strw, buf);
201 static BOOL proxy_active(void)
203 HKEY internet_settings;
204 DWORD proxy_enable;
205 DWORD size;
207 if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
208 0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
209 return FALSE;
211 size = sizeof(DWORD);
212 if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
213 proxy_enable = 0;
215 RegCloseKey(internet_settings);
217 return proxy_enable != 0;
220 static void init_events(void)
222 complete_event = CreateEventW(NULL, FALSE, FALSE, NULL);
223 conn_close_event = CreateEventW(NULL, FALSE, FALSE, NULL);
224 conn_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
225 server_req_rec_event = CreateEventW(NULL, FALSE, FALSE, NULL);
226 request_sent_event = CreateEventW(NULL, FALSE, FALSE, NULL);
229 static void free_events(void)
231 CloseHandle(complete_event);
232 CloseHandle(conn_close_event);
233 CloseHandle(conn_wait_event);
234 CloseHandle(server_req_rec_event);
235 CloseHandle(request_sent_event);
238 static void reset_events(void)
240 ResetEvent(complete_event);
241 ResetEvent(conn_close_event);
242 ResetEvent(conn_wait_event);
243 ResetEvent(server_req_rec_event);
244 ResetEvent(request_sent_event);
247 #define test_status_code(a,b) _test_status_code(__LINE__,a,b, FALSE)
248 #define test_status_code_todo(a,b) _test_status_code(__LINE__,a,b, TRUE)
249 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
251 DWORD code, size, index;
252 char exbuf[12], bufa[10];
253 WCHAR bufw[10];
254 BOOL res;
256 code = 0xdeadbeef;
257 size = sizeof(code);
258 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, NULL);
259 ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %lu\n", GetLastError());
260 todo_wine_if (is_todo)
261 ok_(__FILE__,line)(code == excode, "code = %ld, expected %ld\n", code, excode);
262 ok_(__FILE__,line)(size == sizeof(code), "size = %lu\n", size);
264 code = 0xdeadbeef;
265 index = 0;
266 size = sizeof(code);
267 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
268 ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %lu\n", GetLastError());
269 ok_(__FILE__,line)(!index, "index = %ld, expected 0\n", index);
270 ok_(__FILE__,line)(size == sizeof(code), "size = %lu\n", size);
272 sprintf(exbuf, "%lu", excode);
274 size = sizeof(bufa);
275 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, bufa, &size, NULL);
276 ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %lu\n", GetLastError());
277 todo_wine_if (is_todo)
278 ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
279 ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %ld for \"%s\"\n", size, exbuf);
281 size = 0;
282 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
283 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
284 "[4] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %lu\n", GetLastError());
285 ok_(__FILE__,line)(size == strlen(exbuf)+1, "unexpected size %ld for \"%s\"\n", size, exbuf);
287 size = sizeof(bufw);
288 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
289 ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %lu\n", GetLastError());
290 todo_wine_if (is_todo)
291 ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
292 ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %ld for \"%s\"\n", size, exbuf);
294 size = 0;
295 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
296 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
297 "[6] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %lu\n", GetLastError());
298 ok_(__FILE__,line)(size == (strlen(exbuf)+1)*sizeof(WCHAR), "unexpected size %ld for \"%s\"\n", size, exbuf);
300 if(0) {
301 size = sizeof(bufw);
302 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
303 ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "HttpQueryInfo(HTTP_QUERY_STATUS_CODE) failed: %lu\n", GetLastError());
304 ok(size == sizeof(bufw), "unexpected size %ld\n", size);
307 code = 0xdeadbeef;
308 index = 1;
309 size = sizeof(code);
310 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
311 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
312 "[7] HttpQueryInfoA failed: %x(%ld)\n", res, GetLastError());
314 code = 0xdeadbeef;
315 size = sizeof(code);
316 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_REQUEST_HEADERS, &code, &size, NULL);
317 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_INVALID_QUERY_REQUEST,
318 "[8] HttpQueryInfoA failed: %x(%ld)\n", res, GetLastError());
321 #define test_request_flags(a,b) _test_request_flags(__LINE__,a,b,FALSE)
322 #define test_request_flags_todo(a,b) _test_request_flags(__LINE__,a,b,TRUE)
323 static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOOL is_todo)
325 DWORD flags, size;
326 BOOL res;
328 flags = 0xdeadbeef;
329 size = sizeof(flags);
330 res = InternetQueryOptionW(req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &size);
331 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_REQUEST_FLAGS) failed: %lu\n", GetLastError());
333 /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
334 flags &= ~INTERNET_REQFLAG_CACHE_WRITE_DISABLED;
335 todo_wine_if (is_todo)
336 ok_(__FILE__,line)(flags == exflags, "flags = %lx, expected %lx\n", flags, exflags);
339 #define test_request_url(a,b) _test_request_url(__LINE__,a,b)
340 static void _test_request_url(unsigned line, HINTERNET req, const char *expected_url)
342 char buf[INTERNET_MAX_URL_LENGTH];
343 DWORD size = sizeof(buf);
344 BOOL res;
346 res = InternetQueryOptionA(req, INTERNET_OPTION_URL, buf, &size);
347 ok_(__FILE__,line)(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %lu\n", GetLastError());
348 ok_(__FILE__,line)(size == strlen(expected_url), "size = %lu\n", size);
349 ok_(__FILE__,line)(!strcmp(buf, expected_url), "unexpected URL %s, expected %s\n", buf, expected_url);
352 #define test_http_version(a) _test_http_version(__LINE__,a)
353 static void _test_http_version(unsigned line, HINTERNET req)
355 HTTP_VERSION_INFO v = {0xdeadbeef, 0xdeadbeef};
356 DWORD size;
357 BOOL res;
359 size = sizeof(v);
360 res = InternetQueryOptionW(req, INTERNET_OPTION_HTTP_VERSION, &v, &size);
361 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_HTTP_VERSION) failed: %lu\n", GetLastError());
362 ok_(__FILE__,line)(v.dwMajorVersion == 1, "dwMajorVersion = %ld\n", v.dwMajorVersion);
363 ok_(__FILE__,line)(v.dwMinorVersion == 1, "dwMinorVersion = %ld\n", v.dwMinorVersion);
366 static LONG close_handle_cnt;
368 static VOID WINAPI callback(
369 HINTERNET hInternet,
370 DWORD_PTR dwContext,
371 DWORD dwInternetStatus,
372 LPVOID lpvStatusInformation,
373 DWORD dwStatusInformationLength
376 CHECK_EXPECT(dwInternetStatus);
377 switch (dwInternetStatus)
379 case INTERNET_STATUS_RESOLVING_NAME:
380 if(winetest_debug > 1)
381 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_RESOLVING_NAME \"%s\" %ld\n",
382 GetCurrentThreadId(), hInternet, dwContext,
383 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
384 *(LPSTR)lpvStatusInformation = '\0';
385 break;
386 case INTERNET_STATUS_NAME_RESOLVED:
387 if(winetest_debug > 1)
388 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_NAME_RESOLVED \"%s\" %ld\n",
389 GetCurrentThreadId(), hInternet, dwContext,
390 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
391 *(LPSTR)lpvStatusInformation = '\0';
392 break;
393 case INTERNET_STATUS_CONNECTING_TO_SERVER:
394 if(winetest_debug > 1)
395 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %ld\n",
396 GetCurrentThreadId(), hInternet, dwContext,
397 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
398 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %lu\n",
399 dwStatusInformationLength);
400 *(LPSTR)lpvStatusInformation = '\0';
401 break;
402 case INTERNET_STATUS_CONNECTED_TO_SERVER:
403 if(winetest_debug > 1)
404 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %ld\n",
405 GetCurrentThreadId(), hInternet, dwContext,
406 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
407 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %lu\n",
408 dwStatusInformationLength);
409 *(LPSTR)lpvStatusInformation = '\0';
410 break;
411 case INTERNET_STATUS_SENDING_REQUEST:
412 if(winetest_debug > 1)
413 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_SENDING_REQUEST %p %ld\n",
414 GetCurrentThreadId(), hInternet, dwContext,
415 lpvStatusInformation,dwStatusInformationLength);
416 break;
417 case INTERNET_STATUS_REQUEST_SENT:
418 ok(dwStatusInformationLength == sizeof(DWORD),
419 "info length should be sizeof(DWORD) instead of %ld\n",
420 dwStatusInformationLength);
421 if(winetest_debug > 1)
422 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_REQUEST_SENT 0x%lx %ld\n",
423 GetCurrentThreadId(), hInternet, dwContext,
424 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
425 break;
426 case INTERNET_STATUS_RECEIVING_RESPONSE:
427 if(winetest_debug > 1)
428 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_RECEIVING_RESPONSE %p %ld\n",
429 GetCurrentThreadId(), hInternet, dwContext,
430 lpvStatusInformation,dwStatusInformationLength);
431 break;
432 case INTERNET_STATUS_RESPONSE_RECEIVED:
433 ok(dwStatusInformationLength == sizeof(DWORD),
434 "info length should be sizeof(DWORD) instead of %ld\n",
435 dwStatusInformationLength);
436 if(winetest_debug > 1)
437 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_RESPONSE_RECEIVED 0x%lx %ld\n",
438 GetCurrentThreadId(), hInternet, dwContext,
439 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
440 break;
441 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
442 if(winetest_debug > 1)
443 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %ld\n",
444 GetCurrentThreadId(), hInternet,dwContext,
445 lpvStatusInformation,dwStatusInformationLength);
446 break;
447 case INTERNET_STATUS_PREFETCH:
448 if(winetest_debug > 1)
449 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_PREFETCH %p %ld\n",
450 GetCurrentThreadId(), hInternet, dwContext,
451 lpvStatusInformation,dwStatusInformationLength);
452 break;
453 case INTERNET_STATUS_CLOSING_CONNECTION:
454 if(winetest_debug > 1)
455 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_CLOSING_CONNECTION %p %ld\n",
456 GetCurrentThreadId(), hInternet, dwContext,
457 lpvStatusInformation,dwStatusInformationLength);
458 break;
459 case INTERNET_STATUS_CONNECTION_CLOSED:
460 if(winetest_debug > 1)
461 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_CONNECTION_CLOSED %p %ld\n",
462 GetCurrentThreadId(), hInternet, dwContext,
463 lpvStatusInformation,dwStatusInformationLength);
464 break;
465 case INTERNET_STATUS_HANDLE_CREATED:
466 ok(dwStatusInformationLength == sizeof(HINTERNET),
467 "info length should be sizeof(HINTERNET) instead of %ld\n",
468 dwStatusInformationLength);
469 if(winetest_debug > 1)
470 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_HANDLE_CREATED %p %ld\n",
471 GetCurrentThreadId(), hInternet, dwContext,
472 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
473 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
474 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
475 break;
476 case INTERNET_STATUS_HANDLE_CLOSING:
477 ok(dwStatusInformationLength == sizeof(HINTERNET),
478 "info length should be sizeof(HINTERNET) instead of %ld\n",
479 dwStatusInformationLength);
480 if(winetest_debug > 1)
481 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_HANDLE_CLOSING %p %ld\n",
482 GetCurrentThreadId(), hInternet, dwContext,
483 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
484 if(!InterlockedDecrement(&close_handle_cnt))
485 SetEvent(complete_event);
486 break;
487 case INTERNET_STATUS_REQUEST_COMPLETE:
489 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
490 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
491 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %ld\n",
492 dwStatusInformationLength);
493 ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %Id\n", iar->dwResult);
494 if(winetest_debug > 1)
495 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_REQUEST_COMPLETE {%Id,%ld} %ld\n",
496 GetCurrentThreadId(), hInternet, dwContext,
497 iar->dwResult,iar->dwError,dwStatusInformationLength);
498 req_error = iar->dwError;
499 if(!close_handle_cnt)
500 SetEvent(complete_event);
501 break;
503 case INTERNET_STATUS_REDIRECT:
504 if(winetest_debug > 1)
505 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_REDIRECT \"%s\" %ld\n",
506 GetCurrentThreadId(), hInternet, dwContext,
507 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
508 *(LPSTR)lpvStatusInformation = '\0';
509 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
510 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
511 break;
512 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
513 if(winetest_debug > 1)
514 trace("%04lx:Callback %p 0x%Ix INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %ld\n",
515 GetCurrentThreadId(), hInternet, dwContext,
516 lpvStatusInformation, dwStatusInformationLength);
517 break;
518 default:
519 if(winetest_debug > 1)
520 trace("%04lx:Callback %p 0x%Ix %ld %p %ld\n",
521 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
522 lpvStatusInformation, dwStatusInformationLength);
526 typedef struct {
527 HINTERNET session;
528 HINTERNET connection;
529 HINTERNET request;
530 } test_request_t;
532 #define open_simple_request(a,b,c,d,e) _open_simple_request(__LINE__,a,b,c,d,e)
533 static void _open_simple_request(unsigned line, test_request_t *req, const char *host,
534 int port, const char *verb, const char *url)
536 req->session = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
537 ok_(__FILE__,line)(req->session != NULL, "InternetOpenA failed: %lu\n", GetLastError());
539 req->connection = InternetConnectA(req->session, host, port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
540 ok_(__FILE__,line)(req->connection != NULL, "InternetConnectA failed: %lu\n", GetLastError());
542 req->request = HttpOpenRequestA(req->connection, verb, url, NULL, NULL, NULL, 0, 0);
543 ok_(__FILE__,line)(req->request != NULL, "HttpOpenRequest failed: %lu\n", GetLastError());
546 #define close_request(a) _close_request(__LINE__,a)
547 static void _close_request(unsigned line, test_request_t *req)
549 BOOL ret;
551 ret = InternetCloseHandle(req->request);
552 ok_(__FILE__,line)(ret, "InternetCloseHandle(request) failed: %lu\n", GetLastError());
553 ret = InternetCloseHandle(req->connection);
554 ok_(__FILE__,line)(ret, "InternetCloseHandle(connection) failed: %lu\n", GetLastError());
555 ret = InternetCloseHandle(req->session);
556 ok_(__FILE__,line)(ret, "InternetCloseHandle(session) failed: %lu\n", GetLastError());
559 #define receive_simple_request(a,b,c) _receive_simple_request(__LINE__,a,b,c)
560 static DWORD _receive_simple_request(unsigned line, HINTERNET req, char *buf, size_t buf_size)
562 DWORD read = 0;
563 BOOL ret;
565 ret = InternetReadFile(req, buf, buf_size, &read);
566 ok_(__FILE__,line)(ret, "InternetReadFile failed: %lu\n", GetLastError());
568 return read;
571 static void close_async_handle(HINTERNET handle, int handle_cnt)
573 BOOL res;
575 close_handle_cnt = handle_cnt;
577 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
578 res = InternetCloseHandle(handle);
579 ok(res, "InternetCloseHandle failed: %lu\n", GetLastError());
580 WaitForSingleObject(complete_event, INFINITE);
581 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
584 static void InternetReadFile_test(int flags, const test_data_t *test)
586 char *post_data = NULL;
587 BOOL res, on_async = TRUE;
588 CHAR buffer[4000];
589 WCHAR wbuffer[4000];
590 DWORD length, length2, index, exlen = 0, post_len = 0;
591 const char *types[2] = { "*", NULL };
592 HINTERNET hi, hic = 0, hor = 0;
593 DWORD contents_length, accepts_ranges;
594 BOOL not_supported;
596 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
597 reset_events();
599 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
600 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
601 ok((hi != 0x0),"InternetOpen failed with error %lu\n", GetLastError());
603 if (hi == 0x0) goto abort;
605 pInternetSetStatusCallbackA(hi,&callback);
607 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
609 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
610 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
611 ok((hic != 0x0),"InternetConnect failed with error %lu\n", GetLastError());
613 if (hic == 0x0) goto abort;
615 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
616 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
618 hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
619 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
620 0xdeadbead);
621 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
623 * If the internet name can't be resolved we are probably behind
624 * a firewall or in some other way not directly connected to the
625 * Internet. Not enough reason to fail the test. Just ignore and
626 * abort.
628 } else {
629 ok((hor != 0x0),"HttpOpenRequest failed with error %lu\n", GetLastError());
632 if (hor == 0x0) goto abort;
634 test_request_flags(hor, INTERNET_REQFLAG_NO_HEADERS);
635 test_request_url(hor, test->url);
637 length = sizeof(buffer);
638 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
639 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %ld\n", GetLastError());
640 ok(length == 0 || (length == 1 && !*buffer) /* win10 */, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %ld\n", length);
641 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
643 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
644 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
645 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
646 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT,2);
647 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_RECEIVED,2);
648 if (first_connection_to_test_url)
650 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
651 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
653 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
654 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
655 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
656 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
657 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
658 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
659 if(test->flags & TESTF_REDIRECT) {
660 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
661 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
663 SET_EXPECT(INTERNET_STATUS_REDIRECT);
664 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
665 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
666 if (flags & INTERNET_FLAG_ASYNC)
667 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
669 if(test->flags & TESTF_COMPRESSED) {
670 BOOL b = TRUE;
672 res = InternetSetOptionA(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
673 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
674 "InternetSetOption failed: %lu\n", GetLastError());
675 if(!res)
676 goto abort;
679 test_status_code(hor, 0);
681 if(test->post_data) {
682 post_len = strlen(test->post_data);
683 post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
684 memcpy(post_data, test->post_data, post_len);
686 SetLastError(0xdeadbeef);
687 res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
688 if (flags & INTERNET_FLAG_ASYNC)
689 ok(!res && (GetLastError() == ERROR_IO_PENDING),
690 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
691 else
692 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
693 "Synchronous HttpSendRequest returning 0, error %lu\n", GetLastError());
695 if (flags & INTERNET_FLAG_ASYNC) {
696 WaitForSingleObject(complete_event, INFINITE);
697 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
699 HeapFree(GetProcessHeap(), 0, post_data);
701 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
702 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
703 if (first_connection_to_test_url)
705 if (! proxy_active())
707 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
708 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
710 else
712 CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
713 CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
716 else
718 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
719 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
721 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
722 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
723 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
724 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
725 if(test->flags & TESTF_REDIRECT)
726 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
727 if (flags & INTERNET_FLAG_ASYNC)
728 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
729 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
730 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
731 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
733 test_request_flags(hor, 0);
735 length = 100;
736 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
737 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %ld\n", GetLastError());
739 length = sizeof(buffer)-2;
740 memset(buffer, 0x77, sizeof(buffer));
741 SetLastError(0xdeadbeef);
742 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
743 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %ld\n", GetLastError());
744 ok(GetLastError() == 0 ||
745 broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %lu\n", GetLastError());
746 /* show that the function writes data past the length returned */
747 ok(buffer[length-2], "Expected any header character, got 0x00\n");
748 ok(!buffer[length-1], "Expected 0x00, got %02X\n", buffer[length-1]);
749 ok(!buffer[length], "Expected 0x00, got %02X\n", buffer[length]);
750 ok(buffer[length+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length+1]);
752 length2 = length;
753 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
754 ok(!res, "Expected 0x00, got %d\n", res);
755 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %ld\n", GetLastError());
756 ok(length2 == length+1, "Expected %ld, got %ld\n", length+1, length2);
757 /* the in length of the buffer must be +1 but the length returned does not count this */
758 length2 = length+1;
759 memset(buffer, 0x77, sizeof(buffer));
760 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
761 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %ld\n", GetLastError());
762 ok(buffer[length2] == 0x00, "Expected 0x00, got %02X\n", buffer[length2]);
763 ok(buffer[length2+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length2+1]);
764 ok(length2 == length, "Value should not have changed: %ld != %ld\n", length2, length);
766 length = sizeof(wbuffer)-2*sizeof(WCHAR);
767 memset(wbuffer, 0x77, sizeof(wbuffer));
768 res = HttpQueryInfoW(hor, HTTP_QUERY_RAW_HEADERS, wbuffer, &length, 0x0);
769 ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %ld\n", GetLastError());
770 ok(length % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %ld.\n", length);
771 length /= sizeof(WCHAR);
772 /* show that the function writes data past the length returned */
773 ok(wbuffer[length-2], "Expected any header character, got 0x0000\n");
774 ok(!wbuffer[length-1], "Expected 0x0000, got %04X\n", wbuffer[length-1]);
775 ok(!wbuffer[length], "Expected 0x0000, got %04X\n", wbuffer[length]);
776 ok(wbuffer[length+1] == 0x7777 || broken(wbuffer[length+1] != 0x7777),
777 "Expected 0x7777, got %04X\n", wbuffer[length+1]);
779 length2 = length*sizeof(WCHAR);
780 res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
781 ok(!res, "Expected 0x00, got %d\n", res);
782 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %ld\n", GetLastError());
783 ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %ld.\n", length2);
784 length2 /= sizeof(WCHAR);
785 ok(length2 == length+1, "Expected %ld, got %ld\n", length+1, length2);
786 /* the in length of the buffer must be +1 but the length returned does not count this */
787 length2 = (length+1)*sizeof(WCHAR);
788 memset(wbuffer, 0x77, sizeof(wbuffer));
789 res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
790 ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %ld\n", GetLastError());
791 ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %ld.\n", length2);
792 length2 /= sizeof(WCHAR);
793 ok(!wbuffer[length2], "Expected 0x0000, got %04X\n", wbuffer[length2]);
794 ok(wbuffer[length2+1] == 0x7777, "Expected 0x7777, got %04X\n", wbuffer[length2+1]);
795 ok(length2 == length, "Value should not have changed: %ld != %ld\n", length2, length);
797 test_request_url(hor, test->redirected_url);
799 index = 0;
800 length = 0;
801 SetLastError(0xdeadbeef);
802 ok(HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,NULL,&length,&index) == FALSE,"Query worked\n");
803 if(test->flags & TESTF_COMPRESSED)
804 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
805 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %lu\n", GetLastError());
806 ok(index == 0, "Index was incremented\n");
808 index = 0;
809 length = 16;
810 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,&index);
811 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%lu)\n",res,buffer,GetLastError());
812 if(test->flags & TESTF_COMPRESSED)
814 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
815 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%lu)\n", res, GetLastError());
816 contents_length = 0;
818 else
820 contents_length = atoi(buffer);
822 ok(!res || index == 1, "Index was not incremented although result is %x (index = %lu)\n", res, index);
824 length = 64;
825 *buffer = 0;
826 res = HttpQueryInfoA(hor,HTTP_QUERY_ACCEPT_RANGES,&buffer,&length,0x0);
827 trace("Option HTTP_QUERY_ACCEPT_RANGES -> %i %s (%lu)\n",res,buffer,GetLastError());
828 accepts_ranges = res && !strcmp(buffer, "bytes");
830 length = 100;
831 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
832 buffer[length]=0;
833 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
835 length = 100;
836 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
837 buffer[length]=0;
838 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
840 SetLastError(0xdeadbeef);
841 length = InternetSetFilePointer(hor, 0, NULL, FILE_END, 0);
842 not_supported = length == INVALID_SET_FILE_POINTER
843 && GetLastError() == ERROR_INTERNET_INVALID_OPERATION;
844 if (accepts_ranges)
845 todo_wine ok((length == contents_length && (GetLastError() == ERROR_SUCCESS
846 || broken(GetLastError() == 0xdeadbeef))) || broken(not_supported),
847 "Got unexpected length %#lx, GetLastError() %lu, contents_length %lu, accepts_ranges %#lx.\n",
848 length, GetLastError(), contents_length, accepts_ranges);
849 else
850 ok(not_supported, "Got unexpected length %#lx, GetLastError() %lu.\n", length, GetLastError());
852 if (length != INVALID_SET_FILE_POINTER)
854 SetLastError(0xdeadbeef);
855 length = InternetSetFilePointer(hor, 0, NULL, FILE_BEGIN, 0);
856 ok(!length && (GetLastError() == ERROR_SUCCESS || broken(GetLastError() == 0xdeadbeef)),
857 "Got unexpected length %#lx, GetLastError() %lu.\n", length, GetLastError());
860 SetLastError(0xdeadbeef);
861 res = InternetReadFile(NULL, buffer, 100, &length);
862 ok(!res, "InternetReadFile should have failed\n");
863 ok(GetLastError() == ERROR_INVALID_HANDLE,
864 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %lu\n",
865 GetLastError());
867 length = 100;
868 if(winetest_debug > 1)
869 trace("Entering Query loop\n");
871 while (TRUE)
873 if (flags & INTERNET_FLAG_ASYNC)
874 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
876 /* IE11 calls those in InternetQueryDataAvailable call. */
877 SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
878 SET_OPTIONAL(INTERNET_STATUS_RESPONSE_RECEIVED);
880 length = 0;
881 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
883 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
885 if (flags & INTERNET_FLAG_ASYNC)
887 if (res)
889 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
890 if(exlen) {
891 ok(length >= exlen, "length %lu < exlen %lu\n", length, exlen);
892 exlen = 0;
895 else if (GetLastError() == ERROR_IO_PENDING)
897 if(winetest_debug > 1)
898 trace("pending\n");
899 /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
900 if(!(test->flags & TESTF_CHUNKED))
901 ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %lu length\n", length);
902 WaitForSingleObject(complete_event, INFINITE);
903 exlen = length;
904 ok(exlen, "length = 0\n");
905 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
906 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
907 ok(req_error, "req_error = 0\n");
908 continue;
909 }else {
910 ok(0, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
912 }else {
913 ok(res, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
915 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
917 if(winetest_debug > 1)
918 trace("length %lu\n", length);
919 if(test->flags & TESTF_CHUNKED)
920 ok(length <= 8192, "length = %ld, expected <= 8192\n", length);
921 if (length)
923 char *buffer;
924 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
926 res = InternetReadFile(hor,buffer,length,&length);
928 buffer[length]=0;
930 if(winetest_debug > 1)
931 trace("ReadFile -> %s %li\n", res ? "TRUE" : "FALSE", length);
933 if(test->content)
934 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
935 HeapFree(GetProcessHeap(),0,buffer);
936 }else {
937 ok(!on_async, "Returned zero size in response to request complete\n");
938 break;
940 on_async = FALSE;
942 if(test->flags & TESTF_REDIRECT) {
943 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
944 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
946 abort:
947 if(winetest_debug > 1)
948 trace("aborting\n");
949 close_async_handle(hi, 2);
950 first_connection_to_test_url = FALSE;
953 static void InternetReadFile_chunked_test(void)
955 BOOL res;
956 CHAR buffer[4000];
957 DWORD length, got;
958 const char *types[2] = { "*", NULL };
959 HINTERNET hi, hic = 0, hor = 0;
961 trace("Starting InternetReadFile chunked test\n");
963 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
964 ok((hi != 0x0),"InternetOpen failed with error %lu\n", GetLastError());
966 if (hi == 0x0) goto abort;
968 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
969 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
970 ok((hic != 0x0),"InternetConnect failed with error %lu\n", GetLastError());
972 if (hic == 0x0) goto abort;
974 hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
975 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
976 0xdeadbead);
977 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
979 * If the internet name can't be resolved we are probably behind
980 * a firewall or in some other way not directly connected to the
981 * Internet. Not enough reason to fail the test. Just ignore and
982 * abort.
984 } else {
985 ok((hor != 0x0),"HttpOpenRequest failed with error %lu\n", GetLastError());
988 if (hor == 0x0) goto abort;
990 SetLastError(0xdeadbeef);
991 res = HttpSendRequestA(hor, "", -1, NULL, 0);
992 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
993 "Synchronous HttpSendRequest returning 0, error %lu\n", GetLastError());
995 test_request_flags(hor, 0);
997 length = 100;
998 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
999 buffer[length]=0;
1000 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
1002 SetLastError( 0xdeadbeef );
1003 length = 100;
1004 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
1005 buffer[length]=0;
1006 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
1007 ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
1008 "Failed to get TRANSFER_ENCODING option, error %lu\n", GetLastError() );
1009 ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
1010 "Wrong transfer encoding '%s'\n", buffer );
1012 SetLastError( 0xdeadbeef );
1013 length = 16;
1014 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
1015 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
1016 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %lu\n", GetLastError() );
1018 length = 100;
1019 trace("Entering Query loop\n");
1021 while (TRUE)
1023 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
1024 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
1025 ok(res, "InternetQueryDataAvailable failed, error %ld\n", GetLastError());
1026 trace("got %lu available\n",length);
1027 if (length)
1029 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
1031 SetLastError(0xdeadbeef);
1032 res = InternetReadFile(hor,buffer,length,&got);
1033 ok(GetLastError() == 0 ||
1034 broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %lu\n", GetLastError());
1036 buffer[got]=0;
1037 trace("ReadFile -> %i %li\n",res,got);
1038 ok( length == got, "only got %lu of %lu available\n", got, length );
1039 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
1041 HeapFree(GetProcessHeap(),0,buffer);
1042 if (!got) break;
1044 if (length == 0)
1046 got = 0xdeadbeef;
1047 SetLastError(0xdeadbeef);
1048 res = InternetReadFile( hor, buffer, 1, &got );
1049 ok( res, "InternetReadFile failed: %lu\n", GetLastError() );
1050 ok(GetLastError() == 0 ||
1051 broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %lu\n", GetLastError());
1052 ok( !got, "got %lu\n", got );
1053 break;
1056 abort:
1057 trace("aborting\n");
1058 if (hor != 0x0) {
1059 res = InternetCloseHandle(hor);
1060 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
1062 if (hi != 0x0) {
1063 res = InternetCloseHandle(hi);
1064 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
1068 static void InternetReadFileExA_test(int flags)
1070 DWORD rc;
1071 DWORD length;
1072 const char *types[2] = { "*", NULL };
1073 HINTERNET hi, hic = 0, hor = 0;
1074 INTERNET_BUFFERSA inetbuffers;
1076 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
1077 reset_events();
1079 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
1080 ok((hi != 0x0),"InternetOpen failed with error %lu\n", GetLastError());
1082 if (hi == 0x0) goto abort;
1084 pInternetSetStatusCallbackA(hi,&callback);
1086 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1088 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
1089 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1090 ok((hic != 0x0),"InternetConnect failed with error %lu\n", GetLastError());
1092 if (hic == 0x0) goto abort;
1094 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1095 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1097 hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
1098 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
1099 0xdeadbead);
1100 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
1102 * If the internet name can't be resolved we are probably behind
1103 * a firewall or in some other way not directly connected to the
1104 * Internet. Not enough reason to fail the test. Just ignore and
1105 * abort.
1107 } else {
1108 ok((hor != 0x0),"HttpOpenRequest failed with error %lu\n", GetLastError());
1111 if (hor == 0x0) goto abort;
1113 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1114 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1115 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1116 if (first_connection_to_test_url)
1118 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
1119 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
1121 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT, 2);
1122 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
1123 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
1124 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
1125 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
1126 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
1127 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
1128 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
1129 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
1130 SET_EXPECT(INTERNET_STATUS_REDIRECT);
1131 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
1132 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
1133 if (flags & INTERNET_FLAG_ASYNC)
1134 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1135 else
1136 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1138 SetLastError(0xdeadbeef);
1139 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
1140 if (flags & INTERNET_FLAG_ASYNC)
1141 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
1142 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
1143 else
1144 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
1145 "Synchronous HttpSendRequest returning 0, error %lu\n", GetLastError());
1147 if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
1148 WaitForSingleObject(complete_event, INFINITE);
1149 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
1152 if (first_connection_to_test_url)
1154 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1155 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1157 else
1159 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1160 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1162 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
1163 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
1164 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
1165 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
1166 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
1167 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
1168 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
1169 if (flags & INTERNET_FLAG_ASYNC)
1170 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1171 else
1172 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1173 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
1174 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
1175 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1176 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1178 if(is_ie7plus) {
1179 rc = InternetReadFileExW(hor, NULL, 0, 0xdeadcafe);
1180 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1181 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %lu\n",
1182 rc ? "TRUE" : "FALSE", GetLastError());
1185 /* tests invalid dwStructSize */
1186 inetbuffers.dwStructSize = sizeof(inetbuffers)+1;
1187 inetbuffers.lpcszHeader = NULL;
1188 inetbuffers.dwHeadersLength = 0;
1189 inetbuffers.dwBufferLength = 10;
1190 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
1191 inetbuffers.dwOffsetHigh = 1234;
1192 inetbuffers.dwOffsetLow = 5678;
1193 rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1194 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1195 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %lu\n",
1196 rc ? "TRUE" : "FALSE", GetLastError());
1197 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1199 test_request_flags(hor, 0);
1201 /* tests to see whether lpcszHeader is used - it isn't */
1202 inetbuffers.dwStructSize = sizeof(inetbuffers);
1203 inetbuffers.lpcszHeader = (LPCSTR)0xdeadbeef;
1204 inetbuffers.dwHeadersLength = 255;
1205 inetbuffers.dwBufferLength = 0;
1206 inetbuffers.lpvBuffer = NULL;
1207 inetbuffers.dwOffsetHigh = 1234;
1208 inetbuffers.dwOffsetLow = 5678;
1209 rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1210 ok(rc, "InternetReadFileEx failed with error %lu\n", GetLastError());
1211 trace("read %li bytes\n", inetbuffers.dwBufferLength);
1213 rc = InternetReadFileExA(NULL, &inetbuffers, 0, 0xdeadcafe);
1214 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
1215 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %lu\n",
1216 rc ? "TRUE" : "FALSE", GetLastError());
1218 length = 0;
1219 trace("Entering Query loop\n");
1221 while (TRUE)
1223 inetbuffers.dwStructSize = sizeof(inetbuffers);
1224 inetbuffers.dwBufferLength = 1024;
1225 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
1226 inetbuffers.dwOffsetHigh = 1234;
1227 inetbuffers.dwOffsetLow = 5678;
1229 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1230 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1231 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1232 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
1233 if (!rc)
1235 if (GetLastError() == ERROR_IO_PENDING)
1237 trace("InternetReadFileEx -> PENDING\n");
1238 ok(flags & INTERNET_FLAG_ASYNC,
1239 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
1240 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1241 WaitForSingleObject(complete_event, INFINITE);
1242 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1243 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1244 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
1246 else
1248 trace("InternetReadFileEx -> FAILED %lu\n", GetLastError());
1249 break;
1252 else
1254 trace("InternetReadFileEx -> SUCCEEDED\n");
1255 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1256 if (inetbuffers.dwBufferLength)
1258 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1259 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1261 else
1263 /* Win98 still sends these when 0 bytes are read, WinXP does not */
1264 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1265 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1269 trace("read %li bytes\n", inetbuffers.dwBufferLength);
1270 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
1272 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
1273 "InternetReadFileEx sets offsets to 0x%lx%08lx\n",
1274 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
1276 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1278 if (!inetbuffers.dwBufferLength)
1279 break;
1281 length += inetbuffers.dwBufferLength;
1283 ok(length > 0, "failed to read any of the document\n");
1284 trace("Finished. Read %ld bytes\n", length);
1286 abort:
1287 close_async_handle(hi, 2);
1288 first_connection_to_test_url = FALSE;
1291 static void InternetOpenUrlA_test(void)
1293 HINTERNET myhinternet, myhttp;
1294 char buffer[0x400];
1295 DWORD size, readbytes, totalbytes=0;
1296 BOOL ret;
1298 ret = DeleteUrlCacheEntryA(TEST_URL);
1299 ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND,
1300 "DeleteUrlCacheEntry returned %x, GetLastError() = %ld\n", ret, GetLastError());
1302 myhinternet = InternetOpenA("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1303 ok((myhinternet != 0), "InternetOpen failed, error %lu\n",GetLastError());
1304 size = 0x400;
1305 ret = InternetCanonicalizeUrlA(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
1306 ok( ret, "InternetCanonicalizeUrl failed, error %lu\n",GetLastError());
1308 SetLastError(0);
1309 myhttp = InternetOpenUrlA(myhinternet, TEST_URL, 0, 0,
1310 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1311 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1312 return; /* WinXP returns this when not connected to the net */
1313 ok((myhttp != 0),"InternetOpenUrl failed, error %lu\n",GetLastError());
1314 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1315 ok( ret, "InternetReadFile failed, error %lu\n",GetLastError());
1316 totalbytes += readbytes;
1317 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1318 totalbytes += readbytes;
1319 trace("read 0x%08lx bytes\n",totalbytes);
1321 InternetCloseHandle(myhttp);
1322 InternetCloseHandle(myhinternet);
1324 ret = DeleteUrlCacheEntryA(TEST_URL);
1325 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "INTERNET_FLAG_NO_CACHE_WRITE flag doesn't work\n");
1328 static void HttpSendRequestEx_test(void)
1330 HINTERNET hSession;
1331 HINTERNET hConnect;
1332 HINTERNET hRequest;
1334 INTERNET_BUFFERSA BufferIn;
1335 DWORD dwBytesWritten, dwBytesRead, error;
1336 CHAR szBuffer[256];
1337 int i;
1338 BOOL ret;
1340 static char szPostData[] = "mode=Test";
1341 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1343 hSession = InternetOpenA("Wine Regression Test",
1344 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1345 ok( hSession != NULL ,"Unable to open Internet session\n");
1346 hConnect = InternetConnectA(hSession, "test.winehq.org",
1347 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1349 ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1350 hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1351 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1352 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1354 skip( "Network unreachable, skipping test\n" );
1355 goto done;
1357 ok( hRequest != NULL, "Failed to open request handle err %lu\n", GetLastError());
1359 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1361 BufferIn.dwStructSize = sizeof(BufferIn);
1362 BufferIn.Next = (INTERNET_BUFFERSA*)0xdeadcab;
1363 BufferIn.lpcszHeader = szContentType;
1364 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1365 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1366 BufferIn.lpvBuffer = szPostData;
1367 BufferIn.dwBufferLength = 3;
1368 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1369 BufferIn.dwOffsetLow = 0;
1370 BufferIn.dwOffsetHigh = 0;
1372 SetLastError(0xdeadbeef);
1373 ret = HttpSendRequestExA(hRequest, &BufferIn, NULL, 0 ,0);
1374 error = GetLastError();
1375 ok(ret, "HttpSendRequestEx Failed with error %lu\n", error);
1376 ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", error);
1378 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1380 for (i = 3; szPostData[i]; i++)
1381 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1382 "InternetWriteFile failed\n");
1384 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1386 ok(HttpEndRequestA(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1388 test_request_flags(hRequest, 0);
1390 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1391 "Unable to read response\n");
1392 szBuffer[dwBytesRead] = 0;
1394 ok(dwBytesRead == 13,"Read %lu bytes instead of 13\n",dwBytesRead);
1395 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1397 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1398 done:
1399 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1400 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1403 static void InternetOpenRequest_test(void)
1405 HINTERNET session, connect, request;
1406 static const char *types[] = { "*", "", NULL };
1407 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1408 static const WCHAR *typesW[] = { any, empty, NULL };
1409 BOOL ret;
1411 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1412 ok(session != NULL ,"Unable to open Internet session\n");
1414 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1415 INTERNET_SERVICE_HTTP, 0, 0);
1416 ok(connect == NULL, "InternetConnectA should have failed\n");
1417 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %ld\n", GetLastError());
1419 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1420 INTERNET_SERVICE_HTTP, 0, 0);
1421 ok(connect == NULL, "InternetConnectA should have failed\n");
1422 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %ld\n", GetLastError());
1424 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1425 INTERNET_SERVICE_HTTP, 0, 0);
1426 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %ld\n", GetLastError());
1428 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1429 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1431 skip( "Network unreachable, skipping test\n" );
1432 goto done;
1434 ok(request != NULL, "Failed to open request handle err %lu\n", GetLastError());
1436 ret = HttpSendRequestW(request, NULL, 0, NULL, 0);
1437 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
1438 ok(InternetCloseHandle(request), "Close request handle failed\n");
1440 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1441 ok(request != NULL, "Failed to open request handle err %lu\n", GetLastError());
1443 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1444 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
1445 ok(InternetCloseHandle(request), "Close request handle failed\n");
1447 done:
1448 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1449 ok(InternetCloseHandle(session), "Close session handle failed\n");
1452 static void test_cache_read(void)
1454 HINTERNET session, connection, req;
1455 FILETIME now, tomorrow, yesterday;
1456 BYTE content[1000], buf[2000];
1457 char file_path[MAX_PATH];
1458 ULARGE_INTEGER li;
1459 HANDLE file;
1460 DWORD size;
1461 unsigned i;
1462 BOOL res;
1464 static const char cache_only_url[] = "http://test.winehq.org/tests/cache-only";
1465 BYTE cache_headers[] = "HTTP/1.1 200 OK\r\n\r\n";
1467 trace("Testing cache read...\n");
1468 reset_events();
1470 for(i = 0; i < sizeof(content); i++)
1471 content[i] = '0' + (i%10);
1473 GetSystemTimeAsFileTime(&now);
1474 li.u.HighPart = now.dwHighDateTime;
1475 li.u.LowPart = now.dwLowDateTime;
1476 li.QuadPart += (LONGLONG)10000000 * 3600 * 24;
1477 tomorrow.dwHighDateTime = li.u.HighPart;
1478 tomorrow.dwLowDateTime = li.u.LowPart;
1479 li.QuadPart -= (LONGLONG)10000000 * 3600 * 24 * 2;
1480 yesterday.dwHighDateTime = li.u.HighPart;
1481 yesterday.dwLowDateTime = li.u.LowPart;
1483 res = CreateUrlCacheEntryA(cache_only_url, sizeof(content), "", file_path, 0);
1484 ok(res, "CreateUrlCacheEntryA failed: %lu\n", GetLastError());
1486 file = CreateFileA(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1487 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1489 WriteFile(file, content, sizeof(content), &size, NULL);
1490 CloseHandle(file);
1492 res = CommitUrlCacheEntryA(cache_only_url, file_path, tomorrow, yesterday, NORMAL_CACHE_ENTRY,
1493 cache_headers, sizeof(cache_headers)-1, "", 0);
1494 ok(res, "CommitUrlCacheEntryA failed: %lu\n", GetLastError());
1496 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
1497 ok(session != NULL,"InternetOpen failed with error %lu\n", GetLastError());
1499 pInternetSetStatusCallbackA(session, callback);
1501 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1502 connection = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT,
1503 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1504 ok(connection != NULL,"InternetConnect failed with error %lu\n", GetLastError());
1505 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1507 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1508 req = HttpOpenRequestA(connection, "GET", "/tests/cache-only", NULL, NULL, NULL, 0, 0xdeadbead);
1509 ok(req != NULL, "HttpOpenRequest failed: %lu\n", GetLastError());
1510 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1512 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
1513 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
1514 SET_WINE_ALLOW(INTERNET_STATUS_SENDING_REQUEST);
1515 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_SENT);
1516 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1517 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1518 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1520 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
1521 todo_wine
1522 ok(res, "HttpSendRequest failed: %lu\n", GetLastError());
1524 if(res) {
1525 size = 0;
1526 res = InternetQueryDataAvailable(req, &size, 0, 0);
1527 ok(res, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
1528 ok(size == sizeof(content), "size = %lu\n", size);
1530 size = sizeof(buf);
1531 res = InternetReadFile(req, buf, sizeof(buf), &size);
1532 ok(res, "InternetReadFile failed: %lu\n", GetLastError());
1533 ok(size == sizeof(content), "size = %lu\n", size);
1534 ok(!memcmp(content, buf, sizeof(content)), "unexpected content\n");
1537 close_async_handle(session, 2);
1539 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1540 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1541 CLEAR_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
1542 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
1543 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1544 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1545 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1547 res = DeleteUrlCacheEntryA(cache_only_url);
1548 ok(res, "DeleteUrlCacheEntryA failed: %lu\n", GetLastError());
1551 static void test_http_cache(void)
1553 HINTERNET session, connect, request;
1554 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1555 DWORD size, file_size;
1556 BYTE buf[100];
1557 HANDLE file;
1558 BOOL ret;
1559 FILETIME filetime_zero = {0};
1561 static const char cached_content[] = "data read from cache";
1562 static const char *types[] = { "*", "", NULL };
1564 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1565 ok(session != NULL ,"Unable to open Internet session\n");
1567 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1568 INTERNET_SERVICE_HTTP, 0, 0);
1569 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %ld\n", GetLastError());
1571 request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1572 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1574 skip( "Network unreachable, skipping test\n" );
1576 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1577 ok(InternetCloseHandle(session), "Close session handle failed\n");
1579 return;
1581 ok(request != NULL, "Failed to open request handle err %lu\n", GetLastError());
1583 size = sizeof(url);
1584 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1585 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %lu\n", GetLastError());
1586 ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1588 size = sizeof(file_name);
1589 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1590 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1591 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%lu\n", GetLastError());
1592 ok(!size, "size = %ld\n", size);
1594 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1595 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
1597 size = sizeof(file_name);
1598 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1599 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %lu\n", GetLastError());
1601 file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1602 FILE_ATTRIBUTE_NORMAL, NULL);
1603 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %lu\n", GetLastError());
1604 file_size = GetFileSize(file, NULL);
1605 ok(file_size == 106, "file size = %lu\n", file_size);
1607 size = sizeof(buf);
1608 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1609 ok(ret, "InternetReadFile failed: %lu\n", GetLastError());
1610 ok(size == 100, "size = %lu\n", size);
1612 file_size = GetFileSize(file, NULL);
1613 ok(file_size == 106, "file size = %lu\n", file_size);
1614 CloseHandle(file);
1616 ret = DeleteFileA(file_name);
1617 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%lu)\n", ret, GetLastError());
1619 ok(InternetCloseHandle(request), "Close request handle failed\n");
1621 file = CreateFileA(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1622 FILE_ATTRIBUTE_NORMAL, NULL);
1623 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %lu\n", GetLastError());
1624 ret = WriteFile(file, cached_content, sizeof(cached_content), &size, NULL);
1625 ok(ret && size, "WriteFile failed: %d, %ld\n", ret, size);
1626 ret = CommitUrlCacheEntryA(url, file_name, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, NULL, 0);
1627 ok(ret, "CommitUrlCacheEntry failed: %ld\n", GetLastError());
1628 CloseHandle(file);
1630 /* Send the same request, requiring it to be retrieved from the cache */
1631 request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1633 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1634 ok(ret, "HttpSendRequest failed\n");
1636 size = sizeof(buf);
1637 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1638 ok(ret, "InternetReadFile failed: %lu\n", GetLastError());
1639 ok(size == 100, "size = %lu\n", size);
1640 buf[99] = 0;
1641 todo_wine ok(!strcmp((char*)buf, cached_content), "incorrect page data: %s\n", (char*)buf);
1643 ok(InternetCloseHandle(request), "Close request handle failed\n");
1645 DeleteUrlCacheEntryA(url);
1646 request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1647 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1648 todo_wine ok(!ret, "HttpSendRequest succeeded\n");
1649 if(!ret)
1650 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() = %ld\n", GetLastError());
1651 ok(InternetCloseHandle(request), "Close request handle failed\n");
1653 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1654 ok(request != NULL, "Failed to open request handle err %lu\n", GetLastError());
1656 size = sizeof(file_name);
1657 ret = InternetQueryOptionA(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()=%lu\n", GetLastError());
1660 ok(!size, "size = %ld\n", size);
1662 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1663 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
1665 size = sizeof(file_name);
1666 file_name[0] = 0;
1667 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1668 if (ret)
1670 file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1671 FILE_ATTRIBUTE_NORMAL, NULL);
1672 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %lu\n", GetLastError());
1673 CloseHandle(file);
1675 else
1677 /* < IE8 */
1678 ok(file_name[0] == 0, "Didn't expect a file name\n");
1681 ok(InternetCloseHandle(request), "Close request handle failed\n");
1682 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1683 ok(InternetCloseHandle(session), "Close session handle failed\n");
1685 test_cache_read();
1688 static void InternetLockRequestFile_test(void)
1690 char file_name[MAX_PATH];
1691 test_request_t req;
1692 HANDLE lock, lock2;
1693 DWORD size;
1694 BOOL ret;
1696 open_simple_request(&req, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, "/tests/hello.html");
1698 size = sizeof(file_name);
1699 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1700 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1701 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%lu\n", GetLastError());
1702 ok(!size, "size = %ld\n", size);
1704 lock = NULL;
1705 ret = InternetLockRequestFile(req.request, &lock);
1706 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "InternetLockRequestFile returned: %x(%lu)\n", ret, GetLastError());
1708 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
1709 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
1711 size = sizeof(file_name);
1712 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1713 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %lu\n", GetLastError());
1715 ret = InternetLockRequestFile(req.request, &lock);
1716 ok(ret, "InternetLockRequestFile returned: %x(%lu)\n", ret, GetLastError());
1717 ok(lock != NULL, "lock == NULL\n");
1719 ret = InternetLockRequestFile(req.request, &lock2);
1720 ok(ret, "InternetLockRequestFile returned: %x(%lu)\n", ret, GetLastError());
1721 ok(lock == lock2, "lock != lock2\n");
1723 ret = InternetUnlockRequestFile(lock2);
1724 ok(ret, "InternetUnlockRequestFile failed: %lu\n", GetLastError());
1726 ret = DeleteFileA(file_name);
1727 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%lu)\n", ret, GetLastError());
1729 ok(InternetCloseHandle(req.request), "Close request handle failed\n");
1731 ret = DeleteFileA(file_name);
1732 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%lu)\n", ret, GetLastError());
1734 ret = InternetUnlockRequestFile(lock);
1735 ok(ret, "InternetUnlockRequestFile failed: %lu\n", GetLastError());
1737 ret = DeleteFileA(file_name);
1738 ok(ret, "Deleting file returned %x(%lu)\n", ret, GetLastError());
1741 static void HttpHeaders_test(void)
1743 HINTERNET hSession;
1744 HINTERNET hConnect;
1745 HINTERNET hRequest;
1746 CHAR buffer[256];
1747 WCHAR wbuffer[256];
1748 DWORD len = 256;
1749 DWORD oldlen;
1750 DWORD index = 0;
1751 BOOL ret;
1753 hSession = InternetOpenA("Wine Regression Test",
1754 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1755 ok( hSession != NULL ,"Unable to open Internet session\n");
1756 hConnect = InternetConnectA(hSession, "test.winehq.org",
1757 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1759 ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1760 hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1761 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1762 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1764 skip( "Network unreachable, skipping test\n" );
1765 goto done;
1767 ok( hRequest != NULL, "Failed to open request handle\n");
1769 index = 0;
1770 len = sizeof(buffer);
1771 strcpy(buffer,"Warning");
1772 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1773 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1775 ok(HttpAddRequestHeadersA(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1776 "Failed to add new header\n");
1778 index = 0;
1779 len = sizeof(buffer);
1780 strcpy(buffer,"Warning");
1781 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1782 buffer,&len,&index),"Unable to query header\n");
1783 ok(index == 1, "Index was not incremented\n");
1784 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1785 ok(len == 5, "Invalid length (exp. 5, got %ld)\n", len);
1786 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1787 len = sizeof(buffer);
1788 strcpy(buffer,"Warning");
1789 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1790 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1792 index = 0;
1793 len = 5; /* could store the string but not the NULL terminator */
1794 strcpy(buffer,"Warning");
1795 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1796 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1797 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %ld\n", GetLastError());
1798 ok(index == 0, "Index was incremented\n");
1799 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1800 ok(len == 6, "Invalid length (exp. 6, got %ld)\n", len); /* unlike success, the length includes the NULL-terminator */
1802 /* a call with NULL will fail but will return the length */
1803 index = 0;
1804 len = sizeof(buffer);
1805 SetLastError(0xdeadbeef);
1806 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1807 NULL,&len,&index) == FALSE,"Query worked\n");
1808 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %ld\n", GetLastError());
1809 ok(len > 40, "Invalid length (exp. more than 40, got %ld)\n", len);
1810 ok(index == 0, "Index was incremented\n");
1812 /* even for a len that is too small */
1813 index = 0;
1814 len = 15;
1815 SetLastError(0xdeadbeef);
1816 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1817 NULL,&len,&index) == FALSE,"Query worked\n");
1818 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %ld\n", GetLastError());
1819 ok(len > 40, "Invalid length (exp. more than 40, got %ld)\n", len);
1820 ok(index == 0, "Index was incremented\n");
1822 index = 0;
1823 len = 0;
1824 SetLastError(0xdeadbeef);
1825 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1826 NULL,&len,&index) == FALSE,"Query worked\n");
1827 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %ld\n", GetLastError());
1828 ok(len > 40, "Invalid length (exp. more than 40, got %ld)\n", len);
1829 ok(index == 0, "Index was incremented\n");
1830 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1833 /* a working query */
1834 index = 0;
1835 len = sizeof(buffer);
1836 memset(buffer, 'x', sizeof(buffer));
1837 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1838 buffer,&len,&index),"Unable to query header\n");
1839 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1840 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1841 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1842 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1843 ok(strncmp(buffer, "POST /tests/post.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1844 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1845 ok(index == 0, "Index was incremented\n");
1847 /* Like above two tests, but for W version */
1849 index = 0;
1850 len = 0;
1851 SetLastError(0xdeadbeef);
1852 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1853 NULL,&len,&index) == FALSE,"Query worked\n");
1854 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %ld\n", GetLastError());
1855 ok(len > 80, "Invalid length (exp. more than 80, got %ld)\n", len);
1856 ok(index == 0, "Index was incremented\n");
1857 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1859 /* a working query */
1860 index = 0;
1861 len = sizeof(wbuffer);
1862 memset(wbuffer, 'x', sizeof(wbuffer));
1863 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1864 wbuffer,&len,&index),"Unable to query header\n");
1865 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1866 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1867 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1868 ok(index == 0, "Index was incremented\n");
1870 /* end of W version tests */
1872 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1873 index = 0;
1874 len = sizeof(buffer);
1875 memset(buffer, 'x', sizeof(buffer));
1876 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1877 buffer,&len,&index) == TRUE,"Query failed\n");
1878 ok(len == 2 || len == 4 /* win10 */, "Expected 2 or 4, got %ld\n", len);
1879 ok(memcmp(buffer, "\r\n\r\n", len) == 0, "Expected CRLF, got '%s'\n", buffer);
1880 ok(index == 0, "Index was incremented\n");
1882 ok(HttpAddRequestHeadersA(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1883 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1885 index = 0;
1886 len = sizeof(buffer);
1887 strcpy(buffer,"Warning");
1888 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1889 buffer,&len,&index),"Unable to query header\n");
1890 ok(index == 1, "Index was not incremented\n");
1891 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1892 len = sizeof(buffer);
1893 strcpy(buffer,"Warning");
1894 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1895 buffer,&len,&index),"Failed to get second header\n");
1896 ok(index == 2, "Index was not incremented\n");
1897 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1898 len = sizeof(buffer);
1899 strcpy(buffer,"Warning");
1900 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1901 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1903 ok(HttpAddRequestHeadersA(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1905 index = 0;
1906 len = sizeof(buffer);
1907 strcpy(buffer,"Warning");
1908 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1909 buffer,&len,&index),"Unable to query header\n");
1910 ok(index == 1, "Index was not incremented\n");
1911 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1912 len = sizeof(buffer);
1913 strcpy(buffer,"Warning");
1914 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1915 buffer,&len,&index),"Failed to get second header\n");
1916 ok(index == 2, "Index was not incremented\n");
1917 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1918 len = sizeof(buffer);
1919 strcpy(buffer,"Warning");
1920 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1921 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1923 ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1925 index = 0;
1926 len = sizeof(buffer);
1927 strcpy(buffer,"Warning");
1928 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1929 buffer,&len,&index),"Unable to query header\n");
1930 ok(index == 1, "Index was not incremented\n");
1931 ok(strcmp(buffer,"test2")==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,
1935 buffer,&len,&index),"Failed to get second header\n");
1936 ok(index == 2, "Index was not incremented\n");
1937 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1938 len = sizeof(buffer);
1939 strcpy(buffer,"Warning");
1940 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1941 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1943 ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1945 index = 0;
1946 len = sizeof(buffer);
1947 strcpy(buffer,"Warning");
1948 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1949 buffer,&len,&index),"Unable to query header\n");
1950 ok(index == 1, "Index was not incremented\n");
1951 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1952 len = sizeof(buffer);
1953 strcpy(buffer,"Warning");
1954 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1955 ok(index == 2, "Index was not incremented\n");
1956 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1957 len = sizeof(buffer);
1958 strcpy(buffer,"Warning");
1959 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1961 ok(HttpAddRequestHeadersA(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1963 index = 0;
1964 len = sizeof(buffer);
1965 strcpy(buffer,"Warning");
1966 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1967 ok(index == 1, "Index was not incremented\n");
1968 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1969 len = sizeof(buffer);
1970 strcpy(buffer,"Warning");
1971 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1972 ok(index == 2, "Index was not incremented\n");
1973 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1974 len = sizeof(buffer);
1975 strcpy(buffer,"Warning");
1976 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1978 ok(HttpAddRequestHeadersA(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1980 index = 0;
1981 len = sizeof(buffer);
1982 strcpy(buffer,"Warning");
1983 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1984 ok(index == 1, "Index was not incremented\n");
1985 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1986 len = sizeof(buffer);
1987 strcpy(buffer,"Warning");
1988 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1989 ok(index == 2, "Index was not incremented\n");
1990 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1991 len = sizeof(buffer);
1992 strcpy(buffer,"Warning");
1993 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1995 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");
1997 index = 0;
1998 len = sizeof(buffer);
1999 strcpy(buffer,"Warning");
2000 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2001 ok(index == 1, "Index was not incremented\n");
2002 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
2003 len = sizeof(buffer);
2004 strcpy(buffer,"Warning");
2005 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
2006 ok(index == 2, "Index was not incremented\n");
2007 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
2008 len = sizeof(buffer);
2009 strcpy(buffer,"Warning");
2010 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
2012 /* Ensure that blank headers are ignored and don't cause a failure */
2013 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");
2015 index = 0;
2016 len = sizeof(buffer);
2017 strcpy(buffer,"BlankTest");
2018 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2019 ok(index == 1, "Index was not incremented\n");
2020 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
2022 /* Ensure that malformed header separators are ignored and don't cause a failure */
2023 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),
2024 "Failed to add header with malformed entries in list\n");
2026 index = 0;
2027 len = sizeof(buffer);
2028 strcpy(buffer,"MalformedTest");
2029 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2030 ok(index == 1, "Index was not incremented\n");
2031 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
2032 index = 0;
2033 len = sizeof(buffer);
2034 strcpy(buffer,"MalformedTestTwo");
2035 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2036 ok(index == 1, "Index was not incremented\n");
2037 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
2038 index = 0;
2039 len = sizeof(buffer);
2040 strcpy(buffer,"MalformedTestThree");
2041 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2042 ok(index == 1, "Index was not incremented\n");
2043 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
2045 ret = HttpAddRequestHeadersA(hRequest, "Authorization: Basic\r\n", -1, HTTP_ADDREQ_FLAG_ADD);
2046 ok(ret, "unable to add header %lu\n", GetLastError());
2048 index = 0;
2049 buffer[0] = 0;
2050 len = sizeof(buffer);
2051 ret = HttpQueryInfoA(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index);
2052 ok(ret, "unable to query header %lu\n", GetLastError());
2053 ok(index == 1, "index was not incremented\n");
2054 ok(!strcmp(buffer, "Basic"), "incorrect string was returned (%s)\n", buffer);
2056 ret = HttpAddRequestHeadersA(hRequest, "Authorization:\r\n", -1, HTTP_ADDREQ_FLAG_REPLACE);
2057 ok(ret, "unable to remove header %lu\n", GetLastError());
2059 index = 0;
2060 len = sizeof(buffer);
2061 SetLastError(0xdeadbeef);
2062 ok(!HttpQueryInfoA(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index),
2063 "header still present\n");
2064 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "got %lu\n", GetLastError());
2066 /* Header with empty value should cause a failure */
2067 todo_wine
2069 SetLastError(0xdeadbeef);
2070 ok(!HttpAddRequestHeadersA(hRequest, "EmptyTest1:", -1, HTTP_ADDREQ_FLAG_ADD), "Empty header should not be added.\n");
2071 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Got unexpected error code %lu.\n", GetLastError());
2073 SetLastError(0xdeadbeef);
2074 ok(!HttpAddRequestHeadersA(hRequest, "EmptyTest2:\r\n", -1, HTTP_ADDREQ_FLAG_ADD), "Empty header should not be added.\n");
2075 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Got unexpected error code %lu.\n", GetLastError());
2077 len = sizeof(buffer);
2078 strcpy(buffer, "EmptyTest1");
2079 SetLastError(0xdeadbeef);
2080 ok(!HttpQueryInfoA(hRequest, HTTP_QUERY_CUSTOM | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, NULL),
2081 "Header with empty value is present.\n");
2082 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Got unexpected error code %lu.\n", GetLastError());
2084 len = sizeof(buffer);
2085 strcpy(buffer, "EmptyTest2");
2086 SetLastError(0xdeadbeef);
2087 ok(!HttpQueryInfoA(hRequest, HTTP_QUERY_CUSTOM | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, NULL),
2088 "Header with empty value is present.\n");
2089 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Got unexpected error code %lu.\n", GetLastError());
2092 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
2093 done:
2094 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
2095 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
2098 static const char garbagemsg[] =
2099 "Garbage: Header\r\n";
2101 static const char contmsg[] =
2102 "HTTP/1.1 100 Continue\r\n";
2104 static const char expandcontmsg[] =
2105 "HTTP/1.1 100 Continue\r\n"
2106 "Server: winecontinue\r\n"
2107 "Tag: something witty\r\n";
2109 static const char okmsg[] =
2110 "HTTP/1.1 200 OK\r\n"
2111 "Server: winetest\r\n"
2112 "\r\n";
2114 static const char okmsg201[] =
2115 "HTTP/1.1 201 OK\r\n"
2116 "Server: winetest\r\n"
2117 "\r\n";
2119 static const char okmsg2[] =
2120 "HTTP/1.1 200 OK\r\n"
2121 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2122 "Server: winetest\r\n"
2123 "Content-Length: 0\r\n"
2124 "Set-Cookie: one\r\n"
2125 "Set-Cookie: two\r\n"
2126 "\r\n";
2128 static DWORD64 content_length;
2129 static const char largemsg[] =
2130 "HTTP/1.1 200 OK\r\n"
2131 "Content-Length: %I64u\r\n"
2132 "\r\n";
2134 static const char notokmsg[] =
2135 "HTTP/1.1 400 Bad Request\r\n"
2136 "Server: winetest\r\n"
2137 "\r\n";
2139 static const char noauthmsg[] =
2140 "HTTP/1.1 401 Unauthorized\r\n"
2141 "Server: winetest\r\n"
2142 "Connection: close\r\n"
2143 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
2144 "\r\n";
2146 static const char noauthmsg2[] =
2147 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
2148 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
2149 "\0d`0|6\n"
2150 "Server: winetest\r\n";
2152 static const char proxymsg[] =
2153 "HTTP/1.1 407 Proxy Authentication Required\r\n"
2154 "Server: winetest\r\n"
2155 "Proxy-Connection: close\r\n"
2156 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
2157 "\r\n";
2159 static const char page1[] =
2160 "<HTML>\r\n"
2161 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
2162 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
2163 "</HTML>\r\n\r\n";
2165 static const char ok_with_length[] =
2166 "HTTP/1.1 200 OK\r\n"
2167 "Connection: Keep-Alive\r\n"
2168 "Content-Length: 18\r\n\r\n"
2169 "HTTP/1.1 211 OK\r\n\r\n";
2171 static const char ok_with_length2[] =
2172 "HTTP/1.1 210 OK\r\n"
2173 "Connection: Keep-Alive\r\n"
2174 "Content-Length: 19\r\n\r\n"
2175 "HTTP/1.1 211 OK\r\n\r\n";
2177 struct server_info {
2178 HANDLE hEvent;
2179 int port;
2182 static int test_cache_gzip;
2183 static const char *send_buffer;
2184 static int server_socket;
2186 static DWORD CALLBACK server_thread(LPVOID param)
2188 struct server_info *si = param;
2189 int r, c = -1, i, on, count = 0;
2190 SOCKET s;
2191 struct sockaddr_in sa;
2192 char *buffer;
2193 size_t buffer_size;
2194 WSADATA wsaData;
2195 int last_request = 0;
2196 char host_header[22];
2197 char host_header_override[30];
2198 static int test_no_cache = 0;
2200 WSAStartup(MAKEWORD(1,1), &wsaData);
2202 s = socket(AF_INET, SOCK_STREAM, 0);
2203 if (s == INVALID_SOCKET)
2204 return 1;
2206 on = 1;
2207 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
2209 memset(&sa, 0, sizeof sa);
2210 sa.sin_family = AF_INET;
2211 sa.sin_port = htons(si->port);
2212 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
2214 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
2215 if (r<0)
2216 return 1;
2218 listen(s, 0);
2220 SetEvent(si->hEvent);
2222 sprintf(host_header, "Host: localhost:%d", si->port);
2223 sprintf(host_header_override, "Host: test.local:%d\r\n", si->port);
2224 buffer = HeapAlloc(GetProcessHeap(), 0, buffer_size = 1000);
2228 if(c == -1)
2229 c = accept(s, NULL, NULL);
2231 memset(buffer, 0, buffer_size);
2232 for(i=0;; i++)
2234 if(i == buffer_size)
2235 buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, buffer_size *= 2);
2237 r = recv(c, buffer+i, 1, 0);
2238 if (r != 1)
2239 break;
2240 if (i<4) continue;
2241 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
2242 buffer[i-3] == '\r' && buffer[i-1] == '\r')
2243 break;
2245 if (strstr(buffer, "GET /test1"))
2247 if (!strstr(buffer, "Content-Length: 0"))
2249 send(c, okmsg, sizeof okmsg-1, 0);
2250 send(c, page1, sizeof page1-1, 0);
2252 else
2253 send(c, notokmsg, sizeof notokmsg-1, 0);
2255 if (strstr(buffer, "CONNECT "))
2257 if (!strstr(buffer, "Content-Length: 0"))
2258 send(c, notokmsg, sizeof notokmsg-1, 0);
2259 else
2260 send(c, proxymsg, sizeof proxymsg-1, 0);
2262 if (strstr(buffer, "/test2"))
2264 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
2266 send(c, okmsg, sizeof okmsg-1, 0);
2267 send(c, page1, sizeof page1-1, 0);
2269 else
2270 send(c, proxymsg, sizeof proxymsg-1, 0);
2272 if (strstr(buffer, "/test3"))
2274 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2275 send(c, okmsg, sizeof okmsg-1, 0);
2276 else
2277 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2279 if (strstr(buffer, "/test4"))
2281 if (strstr(buffer, "Connection: Close"))
2282 send(c, okmsg, sizeof okmsg-1, 0);
2283 else
2284 send(c, notokmsg, sizeof notokmsg-1, 0);
2286 if (strstr(buffer, "POST /test5") ||
2287 strstr(buffer, "RPC_IN_DATA /test5") ||
2288 strstr(buffer, "RPC_OUT_DATA /test5"))
2290 if (strstr(buffer, "Content-Length: 0"))
2292 send(c, okmsg, sizeof okmsg-1, 0);
2293 send(c, page1, sizeof page1-1, 0);
2295 else
2296 send(c, notokmsg, sizeof notokmsg-1, 0);
2298 if (strstr(buffer, "GET /test6"))
2300 send(c, contmsg, sizeof contmsg-1, 0);
2301 send(c, contmsg, sizeof contmsg-1, 0);
2302 send(c, okmsg, sizeof okmsg-1, 0);
2303 send(c, page1, sizeof page1-1, 0);
2305 if (strstr(buffer, "POST /test7"))
2307 if (strstr(buffer, "Content-Length: 100"))
2309 if (strstr(buffer, "POST /test7b"))
2310 recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
2311 send(c, okmsg, sizeof okmsg-1, 0);
2312 send(c, page1, sizeof page1-1, 0);
2314 else
2315 send(c, notokmsg, sizeof notokmsg-1, 0);
2317 if (strstr(buffer, "/test8"))
2319 if (!strstr(buffer, "Connection: Close") &&
2320 strstr(buffer, "Connection: Keep-Alive") &&
2321 !strstr(buffer, "Cache-Control: no-cache") &&
2322 !strstr(buffer, "Pragma: no-cache") &&
2323 strstr(buffer, host_header))
2324 send(c, okmsg, sizeof okmsg-1, 0);
2325 else
2326 send(c, notokmsg, sizeof notokmsg-1, 0);
2328 if (strstr(buffer, "/test9"))
2330 if (!strstr(buffer, "Connection: Close") &&
2331 !strstr(buffer, "Connection: Keep-Alive") &&
2332 !strstr(buffer, "Cache-Control: no-cache") &&
2333 !strstr(buffer, "Pragma: no-cache") &&
2334 strstr(buffer, host_header))
2335 send(c, okmsg, sizeof okmsg-1, 0);
2336 else
2337 send(c, notokmsg, sizeof notokmsg-1, 0);
2339 if (strstr(buffer, "/testA"))
2341 if (!strstr(buffer, "Connection: Close") &&
2342 !strstr(buffer, "Connection: Keep-Alive") &&
2343 (strstr(buffer, "Cache-Control: no-cache") ||
2344 strstr(buffer, "Pragma: no-cache")) &&
2345 strstr(buffer, host_header))
2346 send(c, okmsg, sizeof okmsg-1, 0);
2347 else
2348 send(c, notokmsg, sizeof notokmsg-1, 0);
2350 if (strstr(buffer, "/testC"))
2352 if (strstr(buffer, "Cookie: cookie=biscuit"))
2353 send(c, okmsg, sizeof okmsg-1, 0);
2354 else
2355 send(c, notokmsg, sizeof notokmsg-1, 0);
2357 if (strstr(buffer, "/testD"))
2359 send(c, okmsg2, sizeof okmsg2-1, 0);
2361 if (strstr(buffer, "/testE"))
2363 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
2365 if (strstr(buffer, "GET /quit"))
2367 send(c, okmsg, sizeof okmsg-1, 0);
2368 send(c, page1, sizeof page1-1, 0);
2369 last_request = 1;
2371 if (strstr(buffer, "GET /testF"))
2373 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
2374 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2375 send(c, contmsg, sizeof contmsg-1, 0);
2376 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2377 send(c, okmsg, sizeof okmsg-1, 0);
2378 send(c, page1, sizeof page1-1, 0);
2380 if (strstr(buffer, "GET /testG"))
2382 send(c, page1, sizeof page1-1, 0);
2385 if (strstr(buffer, "GET /testJ"))
2387 if (count == 0)
2389 count++;
2390 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2392 else
2394 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2395 count = 0;
2398 if (strstr(buffer, "GET /testH"))
2400 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2401 recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
2402 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2404 if (strstr(buffer, "GET /test_no_content_content_length"))
2406 static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n"
2407 "Content-Length: 10\r\n\r\n0123456789";
2408 send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2410 if (strstr(buffer, "GET /test_no_content"))
2412 static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n"
2413 "0123456789";
2414 send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2416 if (strstr(buffer, "GET /test_not_modified_content_length"))
2418 static const char notmodifiedmsg[] = "HTTP/1.1 304 Not Modified\r\nConnection: close\r\n"
2419 "Content-Length: 10\r\n\r\n0123456789";
2420 send(c, notmodifiedmsg, sizeof(notmodifiedmsg)-1, 0);
2422 else if (strstr(buffer, "GET /test_not_modified"))
2424 static const char notmodifiedmsg[] = "HTTP/1.1 304 Not Modified\r\nConnection: close\r\n"
2425 "\r\n0123456789";
2426 send(c, notmodifiedmsg, sizeof(notmodifiedmsg)-1, 0);
2428 if (strstr(buffer, "HEAD /head_content_length"))
2430 static const char headmsg[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n"
2431 "Content-Length: 10\r\n\r\n0123456789";
2432 send(c, headmsg, sizeof(headmsg)-1, 0);
2434 else if (strstr(buffer, "HEAD /head"))
2436 static const char headmsg[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n0123456789";
2437 send(c, headmsg, sizeof(headmsg)-1, 0);
2439 if (strstr(buffer, "GET /test_large_header"))
2441 static const char allokmsg[] = "HTTP/1.1 200 OK\r\nServer: winetest\r\n";
2442 char header[4000 + sizeof("wine-header: ") - 1];
2444 memset(header, 'A', sizeof(header));
2445 memcpy(header, "wine-header: ", sizeof("wine-header: ") - 1);
2446 send(c, allokmsg, sizeof(allokmsg) - 1, 0);
2447 send(c, header, sizeof(header), 0);
2448 send(c, "\r\n\r\n", 4, 0);
2450 if (strstr(buffer, "GET /test_conn_close"))
2452 static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
2453 send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
2454 WaitForSingleObject(conn_close_event, INFINITE);
2455 trace("closing connection\n");
2457 if (strstr(buffer, "GET /test_cache_control_no_cache"))
2459 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\n\r\nsome content";
2460 if(!test_no_cache++)
2461 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2462 else
2463 send(c, okmsg, sizeof(okmsg)-1, 0);
2465 if (strstr(buffer, "GET /test_cache_control_no_store"))
2467 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: junk, \t No-StOrE\r\n\r\nsome content";
2468 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2470 if (strstr(buffer, "GET /test_cache_gzip"))
2472 static const char gzip_response[] = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\n\r\n"
2473 "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xaf\xca\x2c\x50\x28"
2474 "\x49\x2d\x2e\xe1\x02\x00\x62\x92\xc7\x6c\x0a\x00\x00\x00";
2475 if(!test_cache_gzip++)
2476 send(c, gzip_response, sizeof(gzip_response), 0);
2477 else
2478 send(c, notokmsg, sizeof(notokmsg)-1, 0);
2480 if (strstr(buffer, "HEAD /test_head")) {
2481 static const char head_response[] =
2482 "HTTP/1.1 200 OK\r\n"
2483 "Connection: Keep-Alive\r\n"
2484 "Content-Length: 100\r\n"
2485 "\r\n";
2487 send(c, head_response, sizeof(head_response), 0);
2488 continue;
2490 if (strstr(buffer, "GET /send_from_buffer"))
2491 send(c, send_buffer, strlen(send_buffer), 0);
2492 if (strstr(buffer, "/test_cache_control_verb"))
2494 if (!memcmp(buffer, "GET ", sizeof("GET ")-1) &&
2495 !strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2496 else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2497 else send(c, notokmsg, sizeof(notokmsg)-1, 0);
2499 if (strstr(buffer, "/test_request_content_length"))
2501 static char msg[] = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\n\r\n";
2502 static int seen_content_length;
2504 if (!seen_content_length)
2506 if (strstr(buffer, "Content-Length: 0"))
2508 seen_content_length = 1;
2509 send(c, msg, sizeof msg-1, 0);
2511 else send(c, notokmsg, sizeof notokmsg-1, 0);
2512 WaitForSingleObject(complete_event, 5000);
2514 else
2516 if (strstr(buffer, "Content-Length: 0")) send(c, msg, sizeof msg-1, 0);
2517 else send(c, notokmsg, sizeof notokmsg-1, 0);
2518 WaitForSingleObject(complete_event, 5000);
2521 if (strstr(buffer, "GET /test_premature_disconnect"))
2522 trace("closing connection\n");
2523 if (strstr(buffer, "HEAD /upload.txt"))
2525 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2526 send(c, okmsg, sizeof okmsg-1, 0);
2527 else
2528 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2530 if (strstr(buffer, "PUT /upload2.txt"))
2532 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2533 send(c, okmsg, sizeof okmsg-1, 0);
2534 else
2535 send(c, notokmsg, sizeof notokmsg-1, 0);
2537 if (strstr(buffer, "HEAD /upload3.txt"))
2539 if (strstr(buffer, "Authorization: Basic dXNlcjE6cHdkMQ=="))
2540 send(c, okmsg, sizeof okmsg-1, 0);
2541 else
2542 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2544 if (strstr(buffer, "HEAD /upload4.txt"))
2546 if (strstr(buffer, "Authorization: Bearer dXNlcjE6cHdkMQ=="))
2547 send(c, okmsg, sizeof okmsg-1, 0);
2548 else if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2549 send(c, okmsg201, sizeof okmsg-1, 0);
2550 else
2551 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2553 if (strstr(buffer, "/test_host_override"))
2555 if (strstr(buffer, host_header_override))
2556 send(c, okmsg, sizeof okmsg-1, 0);
2557 else
2558 send(c, notokmsg, sizeof notokmsg-1, 0);
2560 if (strstr(buffer, "/async_read"))
2562 const char *page1_mid = page1 + (sizeof page1 - 1)/2;
2563 const char *page1_end = page1 + sizeof page1 - 1;
2564 send(c, okmsg, sizeof okmsg-1, 0);
2565 send(c, page1, page1_mid - page1, 0);
2566 WaitForSingleObject(conn_wait_event, INFINITE);
2567 send(c, page1_mid, page1_end - page1_mid, 0);
2569 if (strstr(buffer, "/socket"))
2571 server_socket = c;
2572 SetEvent(server_req_rec_event);
2573 WaitForSingleObject(conn_wait_event, INFINITE);
2575 if (strstr(buffer, "/echo_request"))
2577 send(c, okmsg, sizeof(okmsg)-1, 0);
2578 send(c, buffer, strlen(buffer), 0);
2580 if (strstr(buffer, "GET /test_remove_dot_segments"))
2582 send(c, okmsg, sizeof(okmsg)-1, 0);
2584 if (strstr(buffer, "HEAD /test_large_content"))
2586 char msg[sizeof(largemsg) + 16];
2587 sprintf(msg, largemsg, content_length);
2588 send(c, msg, strlen(msg), 0);
2590 shutdown(c, 2);
2591 closesocket(c);
2592 c = -1;
2593 } while (!last_request);
2595 closesocket(s);
2596 HeapFree(GetProcessHeap(), 0, buffer);
2598 return 0;
2601 static void test_basic_request(int port, const char *verb, const char *url)
2603 test_request_t req;
2604 DWORD r, count, error;
2605 char buffer[0x100];
2607 trace("basic request %s %s\n", verb, url);
2609 open_simple_request(&req, "localhost", port, verb, url);
2611 SetLastError(0xdeadbeef);
2612 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2613 error = GetLastError();
2614 ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %lu\n", error);
2615 ok(r, "HttpSendRequest failed: %lu\n", GetLastError());
2617 count = 0;
2618 memset(buffer, 0, sizeof buffer);
2619 SetLastError(0xdeadbeef);
2620 r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
2621 ok(r, "InternetReadFile failed %lu\n", GetLastError());
2622 ok(count == sizeof page1 - 1, "count was wrong\n");
2623 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
2625 close_request(&req);
2628 static void test_proxy_indirect(int port)
2630 test_request_t req;
2631 DWORD r, sz;
2632 char buffer[0x40];
2634 open_simple_request(&req, "localhost", port, NULL, "/test2");
2636 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2637 ok(r, "HttpSendRequest failed %lu\n", GetLastError());
2639 sz = sizeof buffer;
2640 r = HttpQueryInfoA(req.request, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
2641 ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %ld\n", GetLastError());
2642 if (!r)
2644 skip("missing proxy header, not testing remaining proxy headers\n");
2645 goto out;
2647 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
2649 test_status_code(req.request, 407);
2650 test_request_flags(req.request, 0);
2652 sz = sizeof buffer;
2653 r = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
2654 ok(r, "HttpQueryInfo failed\n");
2655 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
2657 sz = sizeof buffer;
2658 r = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &sz, NULL);
2659 ok(r, "HttpQueryInfo failed\n");
2660 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
2662 sz = sizeof buffer;
2663 r = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &sz, NULL);
2664 ok(r, "HttpQueryInfo failed\n");
2665 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
2667 sz = sizeof buffer;
2668 r = HttpQueryInfoA(req.request, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
2669 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
2670 ok(r == FALSE, "HttpQueryInfo failed\n");
2672 out:
2673 close_request(&req);
2676 static void test_proxy_direct(int port)
2678 HINTERNET hi, hc, hr;
2679 DWORD r, sz, error;
2680 char buffer[0x40], *url;
2681 WCHAR bufferW[0x40];
2682 static const char url_fmt[] = "http://test.winehq.org:%u/test2";
2683 static CHAR username[] = "mike",
2684 password[] = "1101",
2685 useragent[] = "winetest";
2686 static const WCHAR usernameW[] = {'m','i','k','e',0},
2687 passwordW[] = {'1','1','0','1',0},
2688 useragentW[] = {'w','i','n','e','t','e','s','t',0};
2690 /* specify proxy type without the proxy and bypass */
2691 SetLastError(0xdeadbeef);
2692 hi = InternetOpenW(NULL, INTERNET_OPEN_TYPE_PROXY, NULL, NULL, 0);
2693 error = GetLastError();
2694 ok(error == ERROR_INVALID_PARAMETER ||
2695 broken(error == ERROR_SUCCESS) /* WinXPProSP2 */, "got %lu\n", error);
2696 ok(hi == NULL || broken(!!hi) /* WinXPProSP2 */, "open should have failed\n");
2698 sprintf(buffer, "localhost:%d\n", port);
2699 hi = InternetOpenA(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
2700 ok(hi != NULL, "open failed\n");
2702 /* try connect without authorization */
2703 hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2704 ok(hc != NULL, "connect failed\n");
2706 hr = HttpOpenRequestA(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
2707 ok(hr != NULL, "HttpOpenRequest failed\n");
2709 sz = 0;
2710 SetLastError(0xdeadbeef);
2711 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, NULL, &sz);
2712 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2713 ok(!r, "unexpected success\n");
2714 ok(sz == 1, "got %lu\n", sz);
2716 sz = 0;
2717 SetLastError(0xdeadbeef);
2718 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, NULL, &sz);
2719 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2720 ok(!r, "unexpected success\n");
2721 ok(sz == 1, "got %lu\n", sz);
2723 sz = sizeof(buffer);
2724 SetLastError(0xdeadbeef);
2725 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2726 ok(r, "unexpected failure %lu\n", GetLastError());
2727 ok(!sz, "got %lu\n", sz);
2729 sz = sizeof(buffer);
2730 SetLastError(0xdeadbeef);
2731 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2732 ok(r, "unexpected failure %lu\n", GetLastError());
2733 ok(!sz, "got %lu\n", sz);
2735 sz = 0;
2736 SetLastError(0xdeadbeef);
2737 r = InternetQueryOptionA(hr, INTERNET_OPTION_PASSWORD, NULL, &sz);
2738 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2739 ok(!r, "unexpected success\n");
2740 ok(sz == 1, "got %lu\n", sz);
2742 sz = 0;
2743 SetLastError(0xdeadbeef);
2744 r = InternetQueryOptionA(hr, INTERNET_OPTION_USERNAME, NULL, &sz);
2745 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2746 ok(!r, "unexpected success\n");
2747 ok(sz == 1, "got %lu\n", sz);
2749 sz = sizeof(buffer);
2750 SetLastError(0xdeadbeef);
2751 r = InternetQueryOptionA(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2752 ok(r, "unexpected failure %lu\n", GetLastError());
2753 ok(!sz, "got %lu\n", sz);
2755 sz = sizeof(buffer);
2756 SetLastError(0xdeadbeef);
2757 r = InternetQueryOptionA(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2758 ok(r, "unexpected failure %lu\n", GetLastError());
2759 ok(!sz, "got %lu\n", sz);
2761 sz = 0;
2762 SetLastError(0xdeadbeef);
2763 r = InternetQueryOptionA(hr, INTERNET_OPTION_URL, NULL, &sz);
2764 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2765 ok(!r, "unexpected success\n");
2766 ok(sz == 34, "got %lu\n", sz);
2768 sz = sizeof(buffer);
2769 SetLastError(0xdeadbeef);
2770 r = InternetQueryOptionA(hr, INTERNET_OPTION_URL, buffer, &sz);
2771 ok(r, "unexpected failure %lu\n", GetLastError());
2772 ok(sz == 33, "got %lu\n", sz);
2774 r = HttpSendRequestW(hr, NULL, 0, NULL, 0);
2775 ok(r || broken(!r), "HttpSendRequest failed %lu\n", GetLastError());
2776 if (!r)
2778 win_skip("skipping proxy tests on broken wininet\n");
2779 goto done;
2782 test_status_code(hr, 407);
2784 /* set the user + password then try again */
2785 r = InternetSetOptionA(hi, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2786 ok(!r, "unexpected success\n");
2788 r = InternetSetOptionA(hc, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2789 ok(r, "failed to set user\n");
2791 r = InternetSetOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2792 ok(r, "failed to set user\n");
2794 buffer[0] = 0;
2795 sz = 3;
2796 SetLastError(0xdeadbeef);
2797 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2798 ok(!r, "unexpected failure %lu\n", GetLastError());
2799 ok(!buffer[0], "got %s\n", buffer);
2800 ok(sz == strlen(username) + 1, "got %lu\n", sz);
2802 buffer[0] = 0;
2803 sz = 0;
2804 SetLastError(0xdeadbeef);
2805 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2806 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2807 ok(!r, "unexpected success\n");
2808 ok(sz == strlen(username) + 1, "got %lu\n", sz);
2810 bufferW[0] = 0;
2811 sz = 0;
2812 SetLastError(0xdeadbeef);
2813 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_USERNAME, bufferW, &sz);
2814 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2815 ok(!r, "unexpected success\n");
2816 ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %lu\n", sz);
2818 buffer[0] = 0;
2819 sz = sizeof(buffer);
2820 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2821 ok(r, "failed to get username\n");
2822 ok(!strcmp(buffer, username), "got %s\n", buffer);
2823 ok(sz == strlen(username), "got %lu\n", sz);
2825 buffer[0] = 0;
2826 sz = sizeof(bufferW);
2827 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_USERNAME, bufferW, &sz);
2828 ok(r, "failed to get username\n");
2829 ok(!lstrcmpW(bufferW, usernameW), "wrong username\n");
2830 ok(sz == lstrlenW(usernameW), "got %lu\n", sz);
2832 r = InternetSetOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, username, 1);
2833 ok(r, "failed to set user\n");
2835 buffer[0] = 0;
2836 sz = sizeof(buffer);
2837 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2838 ok(r, "failed to get username\n");
2839 ok(!strcmp(buffer, username), "got %s\n", buffer);
2840 ok(sz == strlen(username), "got %lu\n", sz);
2842 r = InternetSetOptionA(hi, INTERNET_OPTION_USER_AGENT, useragent, 1);
2843 ok(r, "failed to set useragent\n");
2845 buffer[0] = 0;
2846 sz = 0;
2847 SetLastError(0xdeadbeef);
2848 r = InternetQueryOptionA(hi, INTERNET_OPTION_USER_AGENT, buffer, &sz);
2849 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2850 ok(!r, "unexpected success\n");
2851 ok(sz == strlen(useragent) + 1, "got %lu\n", sz);
2853 buffer[0] = 0;
2854 sz = sizeof(buffer);
2855 r = InternetQueryOptionA(hi, INTERNET_OPTION_USER_AGENT, buffer, &sz);
2856 ok(r, "failed to get user agent\n");
2857 ok(!strcmp(buffer, useragent), "got %s\n", buffer);
2858 ok(sz == strlen(useragent), "got %lu\n", sz);
2860 bufferW[0] = 0;
2861 sz = 0;
2862 SetLastError(0xdeadbeef);
2863 r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2864 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2865 ok(!r, "unexpected success\n");
2866 ok(sz == (lstrlenW(useragentW) + 1) * sizeof(WCHAR), "got %lu\n", sz);
2868 bufferW[0] = 0;
2869 sz = sizeof(bufferW);
2870 r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2871 ok(r, "failed to get user agent\n");
2872 ok(!lstrcmpW(bufferW, useragentW), "wrong user agent\n");
2873 ok(sz == lstrlenW(useragentW), "got %lu\n", sz);
2875 r = InternetSetOptionA(hr, INTERNET_OPTION_USERNAME, username, 1);
2876 ok(r, "failed to set user\n");
2878 buffer[0] = 0;
2879 sz = 0;
2880 SetLastError(0xdeadbeef);
2881 r = InternetQueryOptionA(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2882 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2883 ok(!r, "unexpected success\n");
2884 ok(sz == strlen(username) + 1, "got %lu\n", sz);
2886 buffer[0] = 0;
2887 sz = sizeof(buffer);
2888 r = InternetQueryOptionA(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2889 ok(r, "failed to get user\n");
2890 ok(!strcmp(buffer, username), "got %s\n", buffer);
2891 ok(sz == strlen(username), "got %lu\n", sz);
2893 bufferW[0] = 0;
2894 sz = 0;
2895 SetLastError(0xdeadbeef);
2896 r = InternetQueryOptionW(hr, INTERNET_OPTION_USERNAME, bufferW, &sz);
2897 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2898 ok(!r, "unexpected success\n");
2899 ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %lu\n", sz);
2901 bufferW[0] = 0;
2902 sz = sizeof(bufferW);
2903 r = InternetQueryOptionW(hr, INTERNET_OPTION_USERNAME, bufferW, &sz);
2904 ok(r, "failed to get user\n");
2905 ok(!lstrcmpW(bufferW, usernameW), "wrong user\n");
2906 ok(sz == lstrlenW(usernameW), "got %lu\n", sz);
2908 r = InternetSetOptionA(hr, INTERNET_OPTION_PASSWORD, password, 1);
2909 ok(r, "failed to set password\n");
2911 buffer[0] = 0;
2912 sz = 0;
2913 SetLastError(0xdeadbeef);
2914 r = InternetQueryOptionA(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2915 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2916 ok(!r, "unexpected success\n");
2917 ok(sz == strlen(password) + 1, "got %lu\n", sz);
2919 buffer[0] = 0;
2920 sz = sizeof(buffer);
2921 r = InternetQueryOptionA(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2922 ok(r, "failed to get password\n");
2923 ok(!strcmp(buffer, password), "got %s\n", buffer);
2924 ok(sz == strlen(password), "got %lu\n", sz);
2926 bufferW[0] = 0;
2927 sz = 0;
2928 SetLastError(0xdeadbeef);
2929 r = InternetQueryOptionW(hr, INTERNET_OPTION_PASSWORD, bufferW, &sz);
2930 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2931 ok(!r, "unexpected success\n");
2932 ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %lu\n", sz);
2934 bufferW[0] = 0;
2935 sz = sizeof(bufferW);
2936 r = InternetQueryOptionW(hr, INTERNET_OPTION_PASSWORD, bufferW, &sz);
2937 ok(r, "failed to get password\n");
2938 ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2939 ok(sz == lstrlenW(passwordW), "got %lu\n", sz);
2941 url = HeapAlloc(GetProcessHeap(), 0, strlen(url_fmt) + 11);
2942 sprintf(url, url_fmt, port);
2943 buffer[0] = 0;
2944 sz = 0;
2945 SetLastError(0xdeadbeef);
2946 r = InternetQueryOptionA(hr, INTERNET_OPTION_URL, buffer, &sz);
2947 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2948 ok(!r, "unexpected success\n");
2949 ok(sz == strlen(url) + 1, "got %lu\n", sz);
2951 buffer[0] = 0;
2952 sz = sizeof(buffer);
2953 r = InternetQueryOptionA(hr, INTERNET_OPTION_URL, buffer, &sz);
2954 ok(r, "failed to get url\n");
2955 ok(!strcmp(buffer, url), "got %s\n", buffer);
2956 ok(sz == strlen(url), "got %lu\n", sz);
2958 bufferW[0] = 0;
2959 sz = 0;
2960 SetLastError(0xdeadbeef);
2961 r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2962 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2963 ok(!r, "unexpected success\n");
2964 ok(sz == (strlen(url) + 1) * sizeof(WCHAR), "got %lu\n", sz);
2966 bufferW[0] = 0;
2967 sz = sizeof(bufferW);
2968 r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2969 ok(r, "failed to get url\n");
2970 ok(!strcmp_wa(bufferW, url), "wrong url\n");
2971 ok(sz == strlen(url), "got %lu\n", sz);
2972 HeapFree(GetProcessHeap(), 0, url);
2974 r = InternetSetOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
2975 ok(r, "failed to set password\n");
2977 buffer[0] = 0;
2978 sz = 0;
2979 SetLastError(0xdeadbeef);
2980 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2981 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2982 ok(!r, "unexpected success\n");
2983 ok(sz == strlen(password) + 1, "got %lu\n", sz);
2985 buffer[0] = 0;
2986 sz = sizeof(buffer);
2987 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2988 ok(r, "failed to get password\n");
2989 ok(!strcmp(buffer, password), "got %s\n", buffer);
2990 ok(sz == strlen(password), "got %lu\n", sz);
2992 bufferW[0] = 0;
2993 sz = 0;
2994 SetLastError(0xdeadbeef);
2995 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_PASSWORD, bufferW, &sz);
2996 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %lu\n", GetLastError());
2997 ok(!r, "unexpected success\n");
2998 ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %lu\n", sz);
3000 bufferW[0] = 0;
3001 sz = sizeof(bufferW);
3002 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_PASSWORD, bufferW, &sz);
3003 ok(r, "failed to get password\n");
3004 ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
3005 ok(sz == lstrlenW(passwordW), "got %lu\n", sz);
3007 r = HttpSendRequestW(hr, NULL, 0, NULL, 0);
3008 if (!r)
3010 win_skip("skipping proxy tests on broken wininet\n");
3011 goto done;
3013 ok(r, "HttpSendRequest failed %lu\n", GetLastError());
3014 sz = sizeof buffer;
3015 r = HttpQueryInfoA(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
3016 ok(r, "HttpQueryInfo failed\n");
3017 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
3019 InternetCloseHandle(hr);
3020 InternetCloseHandle(hc);
3021 InternetCloseHandle(hi);
3023 sprintf(buffer, "localhost:%d\n", port);
3024 hi = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
3025 ok(hi != NULL, "InternetOpen failed\n");
3027 hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3028 ok(hc != NULL, "InternetConnect failed\n");
3030 hr = HttpOpenRequestA(hc, "POST", "/test2", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
3031 ok(hr != NULL, "HttpOpenRequest failed\n");
3033 r = HttpSendRequestA(hr, NULL, 0, (char *)"data", sizeof("data"));
3034 ok(r, "HttpSendRequest failed %lu\n", GetLastError());
3036 test_status_code(hr, 407);
3038 done:
3039 InternetCloseHandle(hr);
3040 InternetCloseHandle(hc);
3041 InternetCloseHandle(hi);
3044 static void test_header_handling_order(int port)
3046 static const char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
3047 static const char connection[] = "Connection: Close";
3048 static const char *types[2] = { "*", NULL };
3049 char data[32];
3050 HINTERNET session, connect, request;
3051 DWORD size, status, data_len;
3052 BOOL ret;
3054 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3055 ok(session != NULL, "InternetOpen failed\n");
3057 connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3058 ok(connect != NULL, "InternetConnect failed\n");
3060 request = HttpOpenRequestA(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
3061 ok(request != NULL, "HttpOpenRequest failed\n");
3063 ret = HttpAddRequestHeadersA(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
3064 ok(ret, "HttpAddRequestHeaders failed\n");
3066 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
3067 ok(ret, "HttpSendRequest failed\n");
3069 test_status_code(request, 200);
3070 test_request_flags(request, 0);
3072 InternetCloseHandle(request);
3074 request = HttpOpenRequestA(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
3075 ok(request != NULL, "HttpOpenRequest failed\n");
3077 ret = HttpSendRequestA(request, connection, ~0u, NULL, 0);
3078 ok(ret, "HttpSendRequest failed\n");
3080 status = 0;
3081 size = sizeof(status);
3082 ret = HttpQueryInfoA( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
3083 ok(ret, "HttpQueryInfo failed\n");
3084 ok(status == 200 || status == 400 /* IE6 */, "got status %lu, expected 200 or 400\n", status);
3086 InternetCloseHandle(request);
3087 InternetCloseHandle(connect);
3089 connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3090 ok(connect != NULL, "InternetConnect failed\n");
3092 request = HttpOpenRequestA(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
3093 ok(request != NULL, "HttpOpenRequest failed\n");
3095 ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3096 ok(ret, "HttpAddRequestHeaders failed\n");
3098 ret = HttpSendRequestA(request, connection, ~0u, NULL, 0);
3099 ok(ret, "HttpSendRequest failed\n");
3101 status = 0;
3102 size = sizeof(status);
3103 ret = HttpQueryInfoA( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
3104 ok(ret, "HttpQueryInfo failed\n");
3105 ok(status == 200, "got status %lu, expected 200\n", status);
3107 InternetCloseHandle(request);
3108 InternetCloseHandle(connect);
3110 connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3111 ok(connect != NULL, "InternetConnect failed\n");
3113 request = HttpOpenRequestA(connect, "POST", "/test7b", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
3114 ok(request != NULL, "HttpOpenRequest failed\n");
3116 ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3117 ok(ret, "HttpAddRequestHeaders failed\n");
3119 data_len = sizeof(data);
3120 memset(data, 'a', sizeof(data));
3121 ret = HttpSendRequestA(request, NULL, 0, data, data_len);
3122 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
3124 status = 0;
3125 size = sizeof(status);
3126 ret = HttpQueryInfoA( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
3127 ok(ret, "HttpQueryInfo failed\n");
3128 ok(status == 200, "got status %lu, expected 200\n", status);
3130 InternetCloseHandle(request);
3131 InternetCloseHandle(connect);
3132 InternetCloseHandle(session);
3135 static void test_connection_header(int port)
3137 HINTERNET ses, con, req;
3138 BOOL ret;
3140 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3141 ok(ses != NULL, "InternetOpen failed\n");
3143 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3144 ok(con != NULL, "InternetConnect failed\n");
3146 req = HttpOpenRequestA(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3147 ok(req != NULL, "HttpOpenRequest failed\n");
3149 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3150 ok(ret, "HttpSendRequest failed\n");
3152 test_status_code(req, 200);
3154 InternetCloseHandle(req);
3156 req = HttpOpenRequestA(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
3157 ok(req != NULL, "HttpOpenRequest failed\n");
3159 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3160 ok(ret, "HttpSendRequest failed\n");
3162 test_status_code(req, 200);
3164 InternetCloseHandle(req);
3166 req = HttpOpenRequestA(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
3167 ok(req != NULL, "HttpOpenRequest failed\n");
3169 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3170 ok(ret, "HttpSendRequest failed\n");
3172 test_status_code(req, 200);
3174 InternetCloseHandle(req);
3176 req = HttpOpenRequestA(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
3177 ok(req != NULL, "HttpOpenRequest failed\n");
3179 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3180 ok(ret, "HttpSendRequest failed\n");
3182 test_status_code(req, 200);
3184 InternetCloseHandle(req);
3185 InternetCloseHandle(con);
3186 InternetCloseHandle(ses);
3189 static void test_header_override(int port)
3191 char buffer[128], host_header_override[30], full_url[128];
3192 HINTERNET ses, con, req;
3193 DWORD size, count, err;
3194 BOOL ret;
3196 sprintf(host_header_override, "Host: test.local:%d\r\n", port);
3197 sprintf(full_url, "http://localhost:%d/test_host_override", port);
3199 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3200 ok(ses != NULL, "InternetOpen failed\n");
3202 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3203 ok(con != NULL, "InternetConnect failed\n");
3205 req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3206 ok(req != NULL, "HttpOpenRequest failed\n");
3208 size = sizeof(buffer) - 1;
3209 count = 0;
3210 memset(buffer, 0, sizeof(buffer));
3211 ret = HttpQueryInfoA(req, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
3212 err = GetLastError();
3213 ok(!ret, "HttpQueryInfo succeeded\n");
3214 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %ld\n", err);
3216 test_request_url(req, full_url);
3218 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3219 ok(ret, "HttpAddRequestHeaders failed\n");
3221 size = sizeof(buffer) - 1;
3222 count = 0;
3223 memset(buffer, 0, sizeof(buffer));
3224 ret = HttpQueryInfoA(req, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
3225 ok(ret, "HttpQueryInfo failed\n");
3227 test_request_url(req, full_url);
3229 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3230 ok(ret, "HttpSendRequest failed\n");
3232 test_status_code(req, 200);
3234 InternetCloseHandle(req);
3235 req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3236 ok(req != NULL, "HttpOpenRequest failed\n");
3238 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3239 ok(ret, "HttpAddRequestHeaders failed\n");
3241 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3242 ok(ret, "HttpAddRequestHeaders failed\n");
3244 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3245 ok(ret, "HttpSendRequest failed\n");
3247 test_status_code(req, 400);
3249 InternetCloseHandle(req);
3250 req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3251 ok(req != NULL, "HttpOpenRequest failed\n");
3253 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3254 ok(ret, "HttpAddRequestHeaders failed\n");
3256 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3257 ok(ret, "HttpSendRequest failed\n");
3259 test_status_code(req, 200);
3261 InternetCloseHandle(req);
3262 req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3263 ok(req != NULL, "HttpOpenRequest failed\n");
3265 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_REPLACE);
3266 if(ret) { /* win10 returns success */
3267 trace("replacing host header is supported.\n");
3269 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3270 ok(ret, "HttpSendRequest failed\n");
3272 test_status_code(req, 200);
3273 }else {
3274 trace("replacing host header is not supported.\n");
3276 err = GetLastError();
3277 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %ld\n", err);
3279 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3280 ok(ret, "HttpSendRequest failed\n");
3282 test_status_code(req, 400);
3285 InternetCloseHandle(req);
3286 InternetCloseHandle(con);
3287 InternetCloseHandle(ses);
3290 static void test_connection_closing(int port)
3292 HINTERNET session, connection, req;
3293 DWORD res;
3295 reset_events();
3297 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3298 ok(session != NULL,"InternetOpen failed with error %lu\n", GetLastError());
3300 pInternetSetStatusCallbackA(session, callback);
3302 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3303 connection = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3304 ok(connection != NULL,"InternetConnect failed with error %lu\n", GetLastError());
3305 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3307 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3308 req = HttpOpenRequestA(connection, "GET", "/testJ", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0xdeadbeaf);
3309 ok(req != NULL, "HttpOpenRequest failed: %lu\n", GetLastError());
3310 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3312 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3313 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3314 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3315 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3316 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3317 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3318 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3319 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3320 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
3321 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
3322 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3324 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3325 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3326 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3327 WaitForSingleObject(complete_event, INFINITE);
3328 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
3330 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3331 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3332 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3333 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3334 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3335 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3336 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3337 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3338 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3339 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3340 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3342 test_status_code(req, 200);
3344 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3345 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3346 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3347 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3348 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3349 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3350 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3351 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3352 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3354 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3355 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3356 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3357 WaitForSingleObject(complete_event, INFINITE);
3358 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
3360 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3361 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3362 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3363 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3364 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3365 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3366 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3367 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3368 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3370 test_status_code(req, 210);
3372 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3373 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3374 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3375 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3376 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3377 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3378 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3379 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3380 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
3381 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
3382 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3384 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3385 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3386 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3387 WaitForSingleObject(complete_event, INFINITE);
3388 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
3390 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3391 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3392 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3393 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3394 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3395 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3396 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3397 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3398 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3399 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3400 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3402 test_status_code(req, 200);
3404 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
3405 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
3407 close_async_handle(session, 2);
3410 static void test_successive_HttpSendRequest(int port)
3412 HINTERNET session, connection, req;
3413 DWORD res;
3415 reset_events();
3417 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3418 ok(session != NULL,"InternetOpen failed with error %lu\n", GetLastError());
3420 pInternetSetStatusCallbackA(session, callback);
3422 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3423 connection = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3424 ok(connection != NULL,"InternetConnect failed with error %lu\n", GetLastError());
3425 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3427 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3428 req = HttpOpenRequestA(connection, "GET", "/testH", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0xdeadbeaf);
3429 ok(req != NULL, "HttpOpenRequest failed: %lu\n", GetLastError());
3430 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3432 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3433 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3434 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3435 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3436 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3437 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3438 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3439 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3440 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3442 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3443 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3444 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3445 WaitForSingleObject(complete_event, INFINITE);
3446 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
3448 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3449 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3450 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3451 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3452 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3453 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3454 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3455 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3456 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3458 test_status_code(req, 210);
3460 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3461 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3462 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3463 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3464 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3465 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3466 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
3467 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
3468 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3470 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3471 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3472 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3473 WaitForSingleObject(complete_event, INFINITE);
3474 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
3476 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3477 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3478 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3479 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3480 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3481 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3482 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3483 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3484 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3486 test_status_code(req, 200);
3488 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
3489 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
3491 close_async_handle(session, 2);
3494 static void test_no_content(int port)
3496 HINTERNET session, connection, req;
3497 DWORD res;
3499 trace("Testing 204 no content response...\n");
3501 reset_events();
3503 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3504 ok(session != NULL,"InternetOpen failed with error %lu\n", GetLastError());
3506 pInternetSetStatusCallbackA(session, callback);
3508 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3509 connection = InternetConnectA(session, "localhost", port,
3510 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3511 ok(connection != NULL,"InternetConnect failed with error %lu\n", GetLastError());
3512 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3514 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3515 req = HttpOpenRequestA(connection, "GET", "/test_no_content", NULL, NULL, NULL,
3516 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3517 ok(req != NULL, "HttpOpenRequest failed: %lu\n", GetLastError());
3518 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3520 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3521 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3522 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3523 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3524 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3525 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3526 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3527 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
3528 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
3529 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3531 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
3532 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3533 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3534 WaitForSingleObject(complete_event, INFINITE);
3535 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
3537 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3538 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3539 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3540 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3541 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3542 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3543 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3544 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3546 close_async_handle(session, 2);
3549 * The connection should be closed before closing handle. This is true for most
3550 * wininet versions (including Wine), but some old win2k versions fail to do that.
3552 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3553 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3556 static void test_not_modified(int port)
3558 DWORD avail;
3559 HINTERNET ses, con, req;
3560 BOOL ret;
3562 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3563 ok(ses != NULL, "InternetOpen failed\n");
3565 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3566 ok(con != NULL, "InternetConnect failed\n");
3568 req = HttpOpenRequestA(con, NULL, "/test_not_modified", NULL, NULL, NULL, 0, 0);
3569 ok(req != NULL, "HttpOpenRequest failed\n");
3571 SetLastError(0xdeadbeef);
3572 ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
3573 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
3574 test_status_code(req, 304);
3576 avail = 0xdeadbeef;
3577 ret = InternetQueryDataAvailable(req, &avail, 0, 0);
3578 ok(ret, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
3579 ok(!avail, "got %ld\n", avail);
3580 InternetCloseHandle(req);
3582 req = HttpOpenRequestA(con, NULL, "/test_not_modified_content_length", NULL, NULL, NULL, 0, 0);
3583 ok(req != NULL, "HttpOpenRequest failed\n");
3585 SetLastError(0xdeadbeef);
3586 ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
3587 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
3588 test_status_code(req, 304);
3590 avail = 0xdeadbeef;
3591 ret = InternetQueryDataAvailable(req, &avail, 0, 0);
3592 ok(ret, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
3593 ok(avail == 10, "got %ld\n", avail);
3594 InternetCloseHandle(req);
3596 req = HttpOpenRequestA(con, NULL, "/test_no_content", NULL, NULL, NULL, 0, 0);
3597 ok(req != NULL, "HttpOpenRequest failed\n");
3599 SetLastError(0xdeadbeef);
3600 ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
3601 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
3602 test_status_code(req, 204);
3604 avail = 0xdeadbeef;
3605 ret = InternetQueryDataAvailable(req, &avail, 0, 0);
3606 ok(ret, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
3607 ok(!avail, "got %ld\n", avail);
3608 InternetCloseHandle(req);
3610 req = HttpOpenRequestA(con, NULL, "/test_no_content_content_length", NULL, NULL, NULL, 0, 0);
3611 ok(req != NULL, "HttpOpenRequest failed\n");
3613 SetLastError(0xdeadbeef);
3614 ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
3615 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
3616 test_status_code(req, 204);
3618 avail = 0xdeadbeef;
3619 ret = InternetQueryDataAvailable(req, &avail, 0, 0);
3620 ok(ret, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
3621 ok(avail == 10, "got %ld\n", avail);
3622 InternetCloseHandle(req);
3624 req = HttpOpenRequestA(con, "HEAD", "/head", NULL, NULL, NULL, 0, 0);
3625 ok(req != NULL, "HttpOpenRequest failed\n");
3627 SetLastError(0xdeadbeef);
3628 ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
3629 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
3630 test_status_code(req, 200);
3632 avail = 0xdeadbeef;
3633 ret = InternetQueryDataAvailable(req, &avail, 0, 0);
3634 ok(ret, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
3635 ok(!avail, "got %ld\n", avail);
3636 InternetCloseHandle(req);
3638 req = HttpOpenRequestA(con, "HEAD", "/head_content_length", NULL, NULL, NULL, 0, 0);
3639 ok(req != NULL, "HttpOpenRequest failed\n");
3641 SetLastError(0xdeadbeef);
3642 ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
3643 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
3644 test_status_code(req, 200);
3646 avail = 0xdeadbeef;
3647 ret = InternetQueryDataAvailable(req, &avail, 0, 0);
3648 ok(ret, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
3649 ok(!avail, "got %ld\n", avail);
3651 InternetCloseHandle(req);
3652 InternetCloseHandle(con);
3653 InternetCloseHandle(ses);
3656 static void test_large_header(int port)
3658 HINTERNET ses, con, req;
3659 BOOL ret;
3660 DWORD size, index, error;
3661 char buffer[13];
3663 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3664 ok(ses != NULL, "InternetOpen failed\n");
3666 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3667 ok(con != NULL, "InternetConnect failed\n");
3669 req = HttpOpenRequestA(con, NULL, "/test_large_header", NULL, NULL, NULL, 0, 0);
3670 ok(req != NULL, "HttpOpenRequest failed\n");
3672 SetLastError(0xdeadbeef);
3673 ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
3674 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
3675 test_status_code(req, 200);
3677 index = 0;
3678 size = sizeof(buffer);
3679 strcpy(buffer, "wine-header");
3680 ret = HttpQueryInfoA(req, HTTP_QUERY_CUSTOM, buffer, &size, &index);
3681 error = GetLastError();
3682 ok(!ret, "HttpQueryInfoA succeeded\n");
3683 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %lu\n", error);
3684 ok(size == 4001, "got %lu\n", size);
3686 InternetCloseHandle(req);
3687 InternetCloseHandle(con);
3688 InternetCloseHandle(ses);
3691 static void test_conn_close(int port)
3693 HINTERNET session, connection, req;
3694 DWORD res, avail, size;
3695 BYTE buf[1024];
3697 trace("Testing connection close connection...\n");
3699 reset_events();
3701 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3702 ok(session != NULL,"InternetOpen failed with error %lu\n", GetLastError());
3704 pInternetSetStatusCallbackA(session, callback);
3706 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3707 connection = InternetConnectA(session, "localhost", port,
3708 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3709 ok(connection != NULL,"InternetConnect failed with error %lu\n", GetLastError());
3710 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3712 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3713 req = HttpOpenRequestA(connection, "GET", "/test_conn_close", NULL, NULL, NULL,
3714 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3715 ok(req != NULL, "HttpOpenRequest failed: %lu\n", GetLastError());
3716 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3718 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3719 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3720 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3721 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3722 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3723 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3724 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3725 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3727 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
3728 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3729 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3730 WaitForSingleObject(complete_event, INFINITE);
3731 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
3733 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3734 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3735 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3736 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3737 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3738 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3739 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3740 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3742 avail = 0;
3743 res = InternetQueryDataAvailable(req, &avail, 0, 0);
3744 ok(res, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
3745 ok(avail != 0, "avail = 0\n");
3747 size = 0;
3748 res = InternetReadFile(req, buf, avail, &size);
3749 ok(res, "InternetReadFile failed: %lu\n", GetLastError());
3751 /* IE11 calls those in InternetQueryDataAvailable call. */
3752 SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
3753 SET_OPTIONAL(INTERNET_STATUS_RESPONSE_RECEIVED);
3755 res = InternetQueryDataAvailable(req, &avail, 0, 0);
3756 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3757 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3758 ok(!avail, "avail = %lu, expected 0\n", avail);
3760 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3762 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
3763 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
3764 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3765 SetEvent(conn_close_event);
3766 WaitForSingleObject(complete_event, INFINITE);
3767 ok(req_error == ERROR_SUCCESS, "req_error = %lu\n", req_error);
3768 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3769 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3770 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3771 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3773 close_async_handle(session, 2);
3776 static void test_no_cache(int port)
3778 static const char cache_control_no_cache[] = "/test_cache_control_no_cache";
3779 static const char cache_control_no_store[] = "/test_cache_control_no_store";
3780 static const char cache_url_fmt[] = "http://localhost:%d%s";
3782 char cache_url[256], buf[256];
3783 HINTERNET ses, con, req;
3784 DWORD read, size;
3785 BOOL ret;
3787 trace("Testing no-cache header\n");
3789 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3790 ok(ses != NULL,"InternetOpen failed with error %lu\n", GetLastError());
3792 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3793 ok(con != NULL, "InternetConnect failed with error %lu\n", GetLastError());
3795 req = HttpOpenRequestA(con, NULL, cache_control_no_cache, NULL, NULL, NULL, 0, 0);
3796 ok(req != NULL, "HttpOpenRequest failed\n");
3798 sprintf(cache_url, cache_url_fmt, port, cache_control_no_cache);
3799 DeleteUrlCacheEntryA(cache_url);
3801 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3802 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
3803 size = 0;
3804 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3805 size += read;
3806 ok(size == 12, "read %ld bytes of data\n", size);
3807 InternetCloseHandle(req);
3809 req = HttpOpenRequestA(con, NULL, cache_control_no_cache, NULL, NULL, NULL, 0, 0);
3810 ok(req != NULL, "HttpOpenRequest failed\n");
3812 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3813 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
3814 size = 0;
3815 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3816 size += read;
3817 ok(size == 0, "read %ld bytes of data\n", size);
3818 InternetCloseHandle(req);
3819 DeleteUrlCacheEntryA(cache_url);
3821 req = HttpOpenRequestA(con, NULL, cache_control_no_store, NULL, NULL, NULL, 0, 0);
3822 ok(req != NULL, "HttpOpenRequest failed\n");
3824 sprintf(cache_url, cache_url_fmt, port, cache_control_no_store);
3825 DeleteUrlCacheEntryA(cache_url);
3827 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3828 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
3829 size = 0;
3830 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3831 size += read;
3832 ok(size == 12, "read %ld bytes of data\n", size);
3833 InternetCloseHandle(req);
3835 ret = DeleteUrlCacheEntryA(cache_url);
3836 ok(!ret && GetLastError()==ERROR_FILE_NOT_FOUND, "cache entry should not exist\n");
3838 InternetCloseHandle(con);
3839 InternetCloseHandle(ses);
3842 static void test_cache_read_gzipped(int port)
3844 static const char cache_url_fmt[] = "http://localhost:%d%s";
3845 static const char get_gzip[] = "/test_cache_gzip";
3846 static const char content[] = "gzip test\n";
3847 static const char text_html[] = "text/html";
3848 static const char raw_header[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
3850 HINTERNET ses, con, req;
3851 DWORD read, size;
3852 char cache_url[256], buf[256];
3853 BOOL ret;
3855 trace("Testing reading compressed content from cache\n");
3857 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3858 ok(ses != NULL,"InternetOpen failed with error %lu\n", GetLastError());
3860 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3861 ok(con != NULL, "InternetConnect failed with error %lu\n", GetLastError());
3863 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3864 ok(req != NULL, "HttpOpenRequest failed\n");
3866 ret = TRUE;
3867 ret = InternetSetOptionA(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3868 if(!ret && GetLastError()==ERROR_INTERNET_INVALID_OPTION) {
3869 win_skip("INTERNET_OPTION_HTTP_DECODING not supported\n");
3870 InternetCloseHandle(req);
3871 InternetCloseHandle(con);
3872 InternetCloseHandle(ses);
3873 return;
3875 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %ld\n", GetLastError());
3877 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3878 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
3879 size = 0;
3880 while(InternetReadFile(req, buf+size, sizeof(buf)-size, &read) && read)
3881 size += read;
3882 ok(size == 10, "read %ld bytes of data\n", size);
3883 buf[size] = 0;
3884 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3886 size = sizeof(buf)-1;
3887 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_TYPE, buf, &size, 0);
3888 ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %ld\n", GetLastError());
3889 buf[size] = 0;
3890 ok(!strncmp(text_html, buf, size), "buf = %s\n", buf);
3892 size = sizeof(buf)-1;
3893 ret = HttpQueryInfoA(req, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &size, 0);
3894 ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %ld\n", GetLastError());
3895 buf[size] = 0;
3896 ok(!strncmp(raw_header, buf, size), "buf = %s\n", buf);
3897 InternetCloseHandle(req);
3899 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
3900 ok(req != NULL, "HttpOpenRequest failed\n");
3902 ret = TRUE;
3903 ret = InternetSetOptionA(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3904 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %ld\n", GetLastError());
3906 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3907 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
3908 size = 0;
3909 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3910 size += read;
3911 todo_wine ok(size == 10, "read %ld bytes of data\n", size);
3912 buf[size] = 0;
3913 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3915 size = sizeof(buf);
3916 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
3917 ok(!ret && GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND,
3918 "HttpQueryInfo(HTTP_QUERY_CONTENT_ENCODING) returned %d, %ld\n",
3919 ret, GetLastError());
3921 size = sizeof(buf)-1;
3922 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_TYPE, buf, &size, 0);
3923 todo_wine ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %ld\n", GetLastError());
3924 buf[size] = 0;
3925 todo_wine ok(!strncmp(text_html, buf, size), "buf = %s\n", buf);
3926 InternetCloseHandle(req);
3928 /* Decompression doesn't work while reading from cache */
3929 test_cache_gzip = 0;
3930 sprintf(cache_url, cache_url_fmt, port, get_gzip);
3931 DeleteUrlCacheEntryA(cache_url);
3933 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3934 ok(req != NULL, "HttpOpenRequest failed\n");
3936 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3937 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
3938 size = 0;
3939 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3940 size += read;
3941 ok(size == 31, "read %ld bytes of data\n", size);
3942 InternetCloseHandle(req);
3944 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
3945 ok(req != NULL, "HttpOpenRequest failed\n");
3947 ret = TRUE;
3948 ret = InternetSetOptionA(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3949 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %ld\n", GetLastError());
3951 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3952 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
3953 size = 0;
3954 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3955 size += read;
3956 todo_wine ok(size == 31, "read %ld bytes of data\n", size);
3958 size = sizeof(buf);
3959 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
3960 todo_wine ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_ENCODING) failed: %ld\n", GetLastError());
3961 InternetCloseHandle(req);
3963 InternetCloseHandle(con);
3964 InternetCloseHandle(ses);
3966 /* Decompression doesn't work while reading from cache */
3967 test_cache_gzip = 0;
3968 sprintf(cache_url, cache_url_fmt, port, get_gzip);
3969 DeleteUrlCacheEntryA(cache_url);
3971 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3972 ok(ses != NULL,"InternetOpen failed with error %lu\n", GetLastError());
3974 ret = TRUE;
3975 ret = InternetSetOptionA(ses, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3976 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %ld\n", GetLastError());
3978 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3979 ok(con != NULL, "InternetConnect failed with error %lu\n", GetLastError());
3981 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3982 ok(req != NULL, "HttpOpenRequest failed\n");
3984 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3985 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
3986 size = 0;
3987 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3988 size += read;
3989 ok(size == 10, "read %ld bytes of data\n", size);
3990 buf[size] = 0;
3991 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3992 InternetCloseHandle(req);
3994 InternetCloseHandle(con);
3995 InternetCloseHandle(ses);
3997 /* Decompression doesn't work while reading from cache */
3998 test_cache_gzip = 0;
3999 sprintf(cache_url, cache_url_fmt, port, get_gzip);
4000 DeleteUrlCacheEntryA(cache_url);
4002 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4003 ok(ses != NULL,"InternetOpen failed with error %lu\n", GetLastError());
4005 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4006 ok(con != NULL, "InternetConnect failed with error %lu\n", GetLastError());
4008 ret = TRUE;
4009 ret = InternetSetOptionA(con, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
4010 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %ld\n", GetLastError());
4012 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
4013 ok(req != NULL, "HttpOpenRequest failed\n");
4015 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
4016 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
4017 size = 0;
4018 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
4019 size += read;
4020 ok(size == 10, "read %ld bytes of data\n", size);
4021 buf[size] = 0;
4022 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
4023 InternetCloseHandle(req);
4025 InternetCloseHandle(con);
4026 InternetCloseHandle(ses);
4028 DeleteUrlCacheEntryA(cache_url);
4031 static void test_HttpSendRequestW(int port)
4033 static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
4034 HINTERNET ses, con, req;
4035 DWORD error;
4036 BOOL ret;
4038 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
4039 ok(ses != NULL, "InternetOpen failed\n");
4041 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4042 ok(con != NULL, "InternetConnect failed\n");
4044 req = HttpOpenRequestA(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
4045 ok(req != NULL, "HttpOpenRequest failed\n");
4047 SetLastError(0xdeadbeef);
4048 ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
4049 error = GetLastError();
4050 ok(!ret, "HttpSendRequestW succeeded\n");
4051 ok(error == ERROR_IO_PENDING ||
4052 broken(error == ERROR_HTTP_HEADER_NOT_FOUND) || /* IE6 */
4053 broken(error == ERROR_INVALID_PARAMETER), /* IE5 */
4054 "got %lu expected ERROR_IO_PENDING\n", error);
4056 InternetCloseHandle(req);
4057 InternetCloseHandle(con);
4058 InternetCloseHandle(ses);
4061 static void test_cookie_header(int port)
4063 HINTERNET ses, con, req;
4064 DWORD size, error;
4065 BOOL ret;
4066 char buffer[64];
4068 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4069 ok(ses != NULL, "InternetOpen failed\n");
4071 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4072 ok(con != NULL, "InternetConnect failed\n");
4074 InternetSetCookieA("http://localhost", "cookie", "biscuit");
4076 req = HttpOpenRequestA(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
4077 ok(req != NULL, "HttpOpenRequest failed\n");
4079 buffer[0] = 0;
4080 size = sizeof(buffer);
4081 SetLastError(0xdeadbeef);
4082 ret = HttpQueryInfoA(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4083 error = GetLastError();
4084 ok(!ret, "HttpQueryInfo succeeded\n");
4085 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %lu expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
4087 ret = HttpAddRequestHeadersA(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
4088 ok(ret, "HttpAddRequestHeaders failed: %lu\n", GetLastError());
4090 buffer[0] = 0;
4091 size = sizeof(buffer);
4092 ret = HttpQueryInfoA(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4093 ok(ret, "HttpQueryInfo failed: %lu\n", GetLastError());
4094 ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
4096 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
4097 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
4099 test_status_code(req, 200);
4101 buffer[0] = 0;
4102 size = sizeof(buffer);
4103 ret = HttpQueryInfoA(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4104 ok(ret, "HttpQueryInfo failed: %lu\n", GetLastError());
4105 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
4107 InternetCloseHandle(req);
4108 InternetCloseHandle(con);
4109 InternetCloseHandle(ses);
4112 static void test_basic_authentication(int port)
4114 HINTERNET session, connect, request;
4115 BOOL ret;
4117 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4118 ok(session != NULL, "InternetOpen failed\n");
4120 connect = InternetConnectA(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
4121 ok(connect != NULL, "InternetConnect failed\n");
4123 request = HttpOpenRequestA(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
4124 ok(request != NULL, "HttpOpenRequest failed\n");
4126 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4127 ok(ret, "HttpSendRequest failed %lu\n", GetLastError());
4129 test_status_code(request, 200);
4130 test_request_flags(request, 0);
4132 InternetCloseHandle(request);
4133 InternetCloseHandle(connect);
4134 InternetCloseHandle(session);
4137 static void test_premature_disconnect(int port)
4139 test_request_t req;
4140 DWORD err;
4141 BOOL ret;
4143 open_simple_request(&req, "localhost", port, NULL, "/premature_disconnect");
4145 SetLastError(0xdeadbeef);
4146 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4147 err = GetLastError();
4148 todo_wine ok(!ret, "HttpSendRequest succeeded\n");
4149 todo_wine ok(err == ERROR_HTTP_INVALID_SERVER_RESPONSE, "got %lu\n", err);
4151 close_request(&req);
4154 static void test_invalid_response_headers(int port)
4156 test_request_t req;
4157 DWORD size;
4158 BOOL ret;
4159 char buffer[256];
4161 open_simple_request(&req, "localhost", port, NULL, "/testE");
4163 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4164 ok(ret, "HttpSendRequest failed %lu\n", GetLastError());
4166 test_status_code(req.request, 401);
4167 test_request_flags(req.request, 0);
4169 buffer[0] = 0;
4170 size = sizeof(buffer);
4171 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
4172 ok(ret, "HttpQueryInfo failed\n");
4173 ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
4174 "headers wrong \"%s\"\n", buffer);
4176 buffer[0] = 0;
4177 size = sizeof(buffer);
4178 ret = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &size, NULL);
4179 ok(ret, "HttpQueryInfo failed\n");
4180 ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
4182 close_request(&req);
4185 static void test_response_without_headers(int port)
4187 test_request_t req;
4188 DWORD r, count, size;
4189 char buffer[1024];
4191 open_simple_request(&req, "localhost", port, NULL, "/testG");
4193 test_request_flags(req.request, INTERNET_REQFLAG_NO_HEADERS);
4195 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4196 ok(r, "HttpSendRequest failed %lu\n", GetLastError());
4198 test_request_flags_todo(req.request, INTERNET_REQFLAG_NO_HEADERS);
4200 count = 0;
4201 memset(buffer, 0, sizeof buffer);
4202 r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
4203 ok(r, "InternetReadFile failed %lu\n", GetLastError());
4204 todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
4205 todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
4207 test_status_code(req.request, 200);
4208 test_request_flags_todo(req.request, INTERNET_REQFLAG_NO_HEADERS);
4210 buffer[0] = 0;
4211 size = sizeof(buffer);
4212 r = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
4213 ok(r, "HttpQueryInfo failed %lu\n", GetLastError());
4214 ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
4216 buffer[0] = 0;
4217 size = sizeof(buffer);
4218 r = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &size, NULL);
4219 ok(r, "HttpQueryInfo failed %lu\n", GetLastError());
4220 ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
4222 buffer[0] = 0;
4223 size = sizeof(buffer);
4224 r = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
4225 ok(r, "HttpQueryInfo failed %lu\n", GetLastError());
4226 ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
4228 close_request(&req);
4231 static void test_head_request(int port)
4233 DWORD len, content_length;
4234 test_request_t req;
4235 BYTE buf[100];
4236 BOOL ret;
4238 open_simple_request(&req, "localhost", port, "HEAD", "/test_head");
4240 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4241 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
4243 len = sizeof(content_length);
4244 content_length = -1;
4245 ret = HttpQueryInfoA(req.request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH, &content_length, &len, 0);
4246 ok(ret, "HttpQueryInfo failed: %lu\n", GetLastError());
4247 ok(len == sizeof(DWORD), "len = %lu\n", len);
4248 ok(content_length == 100, "content_length = %lu\n", content_length);
4250 len = -1;
4251 ret = InternetReadFile(req.request, buf, sizeof(buf), &len);
4252 ok(ret, "InternetReadFile failed: %lu\n", GetLastError());
4254 len = -1;
4255 ret = InternetReadFile(req.request, buf, sizeof(buf), &len);
4256 ok(ret, "InternetReadFile failed: %lu\n", GetLastError());
4258 close_request(&req);
4261 static void test_HttpQueryInfo(int port)
4263 test_request_t req;
4264 DWORD size, index, error;
4265 char buffer[1024];
4266 BOOL ret;
4268 open_simple_request(&req, "localhost", port, NULL, "/testD");
4270 size = sizeof(buffer);
4271 ret = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
4272 error = GetLastError();
4273 ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
4274 if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %lu expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
4276 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4277 ok(ret, "HttpSendRequest failed\n");
4279 index = 0;
4280 size = sizeof(buffer);
4281 ret = HttpQueryInfoA(req.request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
4282 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4283 ok(index == 1, "expected 1 got %lu\n", index);
4285 index = 0;
4286 size = sizeof(buffer);
4287 ret = HttpQueryInfoA(req.request, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
4288 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4289 ok(index == 1, "expected 1 got %lu\n", index);
4291 index = 0;
4292 size = sizeof(buffer);
4293 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
4294 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4295 ok(index == 0, "expected 0 got %lu\n", index);
4297 size = sizeof(buffer);
4298 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
4299 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4300 ok(index == 0, "expected 0 got %lu\n", index);
4302 index = 0xdeadbeef; /* invalid start index */
4303 size = sizeof(buffer);
4304 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
4305 todo_wine ok(!ret, "HttpQueryInfo should have failed\n");
4306 todo_wine ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
4307 "Expected ERROR_HTTP_HEADER_NOT_FOUND, got %lu\n", GetLastError());
4309 index = 0;
4310 size = sizeof(buffer);
4311 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
4312 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4313 ok(index == 0, "expected 0 got %lu\n", index);
4315 size = sizeof(buffer);
4316 ret = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
4317 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4318 ok(index == 0, "expected 0 got %lu\n", index);
4320 size = sizeof(buffer);
4321 ret = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &size, &index);
4322 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4323 ok(index == 0, "expected 0 got %lu\n", index);
4325 test_status_code(req.request, 200);
4327 index = 0xdeadbeef;
4328 size = sizeof(buffer);
4329 ret = HttpQueryInfoA(req.request, HTTP_QUERY_FORWARDED, buffer, &size, &index);
4330 ok(!ret, "HttpQueryInfo succeeded\n");
4331 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %lu\n", index);
4333 index = 0;
4334 size = sizeof(buffer);
4335 ret = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &size, &index);
4336 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4337 ok(index == 1, "expected 1 got %lu\n", index);
4339 index = 0;
4340 size = sizeof(buffer);
4341 strcpy(buffer, "Server");
4342 ret = HttpQueryInfoA(req.request, HTTP_QUERY_CUSTOM, buffer, &size, &index);
4343 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4344 ok(index == 1, "expected 1 got %lu\n", index);
4346 index = 0;
4347 size = sizeof(buffer);
4348 ret = HttpQueryInfoA(req.request, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
4349 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4350 ok(index == 1, "expected 1 got %lu\n", index);
4352 size = sizeof(buffer);
4353 ret = HttpQueryInfoA(req.request, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
4354 ok(ret, "HttpQueryInfo failed %lu\n", GetLastError());
4355 ok(index == 2, "expected 2 got %lu\n", index);
4357 close_request(&req);
4360 static void test_options(int port)
4362 INTERNET_DIAGNOSTIC_SOCKET_INFO idsi;
4363 HINTERNET ses, con, req;
4364 DWORD size, error;
4365 DWORD_PTR ctx;
4366 BOOL ret;
4368 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4369 ok(ses != NULL, "InternetOpen failed\n");
4371 SetLastError(0xdeadbeef);
4372 ret = InternetSetOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, 0);
4373 error = GetLastError();
4374 ok(!ret, "InternetSetOption succeeded\n");
4375 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
4377 SetLastError(0xdeadbeef);
4378 ret = InternetSetOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, sizeof(ctx));
4379 ok(!ret, "InternetSetOption succeeded\n");
4380 error = GetLastError();
4381 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
4383 SetLastError(0xdeadbeef);
4384 ret = InternetSetOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, 0);
4385 ok(!ret, "InternetSetOption succeeded\n");
4386 error = GetLastError();
4387 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
4389 ctx = 1;
4390 ret = InternetSetOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
4391 ok(ret, "InternetSetOption failed %lu\n", GetLastError());
4393 SetLastError(0xdeadbeef);
4394 ret = InternetQueryOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, NULL);
4395 error = GetLastError();
4396 ok(!ret, "InternetQueryOption succeeded\n");
4397 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
4399 SetLastError(0xdeadbeef);
4400 ret = InternetQueryOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, NULL);
4401 error = GetLastError();
4402 ok(!ret, "InternetQueryOption succeeded\n");
4403 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
4405 size = 0;
4406 SetLastError(0xdeadbeef);
4407 ret = InternetQueryOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, &size);
4408 error = GetLastError();
4409 ok(!ret, "InternetQueryOption succeeded\n");
4410 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %lu\n", error);
4412 size = sizeof(ctx);
4413 SetLastError(0xdeadbeef);
4414 ret = InternetQueryOptionA(NULL, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4415 error = GetLastError();
4416 ok(!ret, "InternetQueryOption succeeded\n");
4417 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %lu\n", error);
4419 ctx = 0xdeadbeef;
4420 size = sizeof(ctx);
4421 ret = InternetQueryOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4422 ok(ret, "InternetQueryOption failed %lu\n", GetLastError());
4423 ok(ctx == 1, "expected 1 got %Iu\n", ctx);
4425 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4426 ok(con != NULL, "InternetConnect failed\n");
4428 ctx = 0xdeadbeef;
4429 size = sizeof(ctx);
4430 ret = InternetQueryOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4431 ok(ret, "InternetQueryOption failed %lu\n", GetLastError());
4432 ok(ctx == 0, "expected 0 got %Iu\n", ctx);
4434 ctx = 2;
4435 ret = InternetSetOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
4436 ok(ret, "InternetSetOption failed %lu\n", GetLastError());
4438 ctx = 0xdeadbeef;
4439 size = sizeof(ctx);
4440 ret = InternetQueryOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4441 ok(ret, "InternetQueryOption failed %lu\n", GetLastError());
4442 ok(ctx == 2, "expected 2 got %Iu\n", ctx);
4444 req = HttpOpenRequestA(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
4445 ok(req != NULL, "HttpOpenRequest failed\n");
4447 ctx = 0xdeadbeef;
4448 size = sizeof(ctx);
4449 ret = InternetQueryOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4450 ok(ret, "InternetQueryOption failed %lu\n", GetLastError());
4451 ok(ctx == 0, "expected 0 got %Iu\n", ctx);
4453 ctx = 3;
4454 ret = InternetSetOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
4455 ok(ret, "InternetSetOption failed %lu\n", GetLastError());
4457 ctx = 0xdeadbeef;
4458 size = sizeof(ctx);
4459 ret = InternetQueryOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4460 ok(ret, "InternetQueryOption failed %lu\n", GetLastError());
4461 ok(ctx == 3, "expected 3 got %Iu\n", ctx);
4463 size = sizeof(idsi);
4464 ret = InternetQueryOptionA(req, INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, &idsi, &size);
4465 ok(ret, "InternetQueryOption failed %lu\n", GetLastError());
4467 size = 0;
4468 SetLastError(0xdeadbeef);
4469 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
4470 error = GetLastError();
4471 ok(!ret, "InternetQueryOption succeeded\n");
4472 ok(error == ERROR_INTERNET_INVALID_OPERATION, "expected ERROR_INTERNET_INVALID_OPERATION, got %lu\n", error);
4474 /* INTERNET_OPTION_PROXY */
4475 SetLastError(0xdeadbeef);
4476 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, NULL);
4477 error = GetLastError();
4478 ok(!ret, "InternetQueryOption succeeded\n");
4479 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
4481 SetLastError(0xdeadbeef);
4482 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, &ctx, NULL);
4483 error = GetLastError();
4484 ok(!ret, "InternetQueryOption succeeded\n");
4485 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
4487 size = 0;
4488 SetLastError(0xdeadbeef);
4489 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, &size);
4490 error = GetLastError();
4491 ok(!ret, "InternetQueryOption succeeded\n");
4492 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %lu\n", error);
4493 ok(size >= sizeof(INTERNET_PROXY_INFOA), "expected size to be greater or equal to the struct size\n");
4495 InternetCloseHandle(req);
4496 InternetCloseHandle(con);
4497 InternetCloseHandle(ses);
4500 typedef struct {
4501 const char *response_text;
4502 int status_code;
4503 const char *status_text;
4504 const char *raw_headers;
4505 } http_status_test_t;
4507 static const http_status_test_t http_status_tests[] = {
4509 "HTTP/1.1 200 OK\r\n"
4510 "Content-Length: 1\r\n"
4511 "\r\nx",
4512 200,
4513 "OK"
4516 "HTTP/1.1 404 Fail\r\n"
4517 "Content-Length: 1\r\n"
4518 "\r\nx",
4519 404,
4520 "Fail"
4523 "HTTP/1.1 200\r\n"
4524 "Content-Length: 1\r\n"
4525 "\r\nx",
4526 200,
4530 "HTTP/1.1 200 \r\n"
4531 "Content-Length: 1\r\n"
4532 "\r\nx",
4533 200,
4537 "HTTP/1.1 410 \r\n"
4538 "Content-Length: 1\r\n"
4539 "\r\nx",
4540 410,
4545 static void test_http_status(int port)
4547 test_request_t req;
4548 char buf[1000];
4549 DWORD i, size;
4550 BOOL res;
4552 for(i = 0; i < ARRAY_SIZE(http_status_tests); i++) {
4553 send_buffer = http_status_tests[i].response_text;
4555 open_simple_request(&req, "localhost", port, NULL, "/send_from_buffer");
4557 res = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4558 ok(res, "HttpSendRequest failed\n");
4560 test_status_code(req.request, http_status_tests[i].status_code);
4562 size = sizeof(buf);
4563 res = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buf, &size, NULL);
4564 ok(res, "HttpQueryInfo failed: %lu\n", GetLastError());
4565 ok(!strcmp(buf, http_status_tests[i].status_text), "[%lu] Unexpected status text \"%s\", expected \"%s\"\n",
4566 i, buf, http_status_tests[i].status_text);
4568 close_request(&req);
4572 static void test_cache_control_verb(int port)
4574 HINTERNET session, connect, request;
4575 BOOL ret;
4577 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4578 ok(session != NULL, "InternetOpen failed\n");
4580 connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4581 ok(connect != NULL, "InternetConnect failed\n");
4583 request = HttpOpenRequestA(connect, "RPC_OUT_DATA", "/test_cache_control_verb", NULL, NULL, NULL,
4584 INTERNET_FLAG_NO_CACHE_WRITE, 0);
4585 ok(request != NULL, "HttpOpenRequest failed\n");
4586 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4587 ok(ret, "HttpSendRequest failed %lu\n", GetLastError());
4588 test_status_code(request, 200);
4589 InternetCloseHandle(request);
4591 request = HttpOpenRequestA(connect, "POST", "/test_cache_control_verb", NULL, NULL, NULL,
4592 INTERNET_FLAG_NO_CACHE_WRITE, 0);
4593 ok(request != NULL, "HttpOpenRequest failed\n");
4594 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4595 ok(ret, "HttpSendRequest failed %lu\n", GetLastError());
4596 test_status_code(request, 200);
4597 InternetCloseHandle(request);
4599 request = HttpOpenRequestA(connect, "HEAD", "/test_cache_control_verb", NULL, NULL, NULL,
4600 INTERNET_FLAG_NO_CACHE_WRITE, 0);
4601 ok(request != NULL, "HttpOpenRequest failed\n");
4602 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4603 ok(ret, "HttpSendRequest failed %lu\n", GetLastError());
4604 test_status_code(request, 200);
4605 InternetCloseHandle(request);
4607 request = HttpOpenRequestA(connect, "GET", "/test_cache_control_verb", NULL, NULL, NULL,
4608 INTERNET_FLAG_NO_CACHE_WRITE, 0);
4609 ok(request != NULL, "HttpOpenRequest failed\n");
4610 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4611 ok(ret, "HttpSendRequest failed %lu\n", GetLastError());
4612 test_status_code(request, 200);
4613 InternetCloseHandle(request);
4615 InternetCloseHandle(connect);
4616 InternetCloseHandle(session);
4619 static void test_request_content_length(int port)
4621 char data[] = {'t','e','s','t'};
4622 test_request_t req;
4623 BOOL ret;
4625 reset_events();
4626 open_simple_request(&req, "localhost", port, "POST", "/test_request_content_length");
4628 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4629 ok(ret, "HttpSendRequest failed %lu\n", GetLastError());
4630 test_status_code(req.request, 200);
4632 SetEvent(complete_event);
4634 ret = HttpSendRequestA(req.request, NULL, 0, data, sizeof(data));
4635 ok(ret, "HttpSendRequest failed %lu\n", GetLastError());
4636 test_status_code(req.request, 200);
4638 SetEvent(complete_event);
4639 close_request(&req);
4642 static void test_accept_encoding(int port)
4644 HINTERNET ses, con, req;
4645 char buf[1000];
4646 BOOL ret;
4648 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4649 ok(ses != NULL, "InternetOpen failed\n");
4651 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4652 ok(con != NULL, "InternetConnect failed\n");
4654 req = HttpOpenRequestA(con, "GET", "/echo_request", "HTTP/1.0", NULL, NULL, 0, 0);
4655 ok(req != NULL, "HttpOpenRequest failed\n");
4657 ret = HttpAddRequestHeadersA(req, "Accept-Encoding: gzip\r\n", ~0u, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
4658 ok(ret, "HttpAddRequestHeaders failed\n");
4660 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
4661 ok(ret, "HttpSendRequestA failed\n");
4663 test_status_code(req, 200);
4664 receive_simple_request(req, buf, sizeof(buf));
4665 ok(strstr(buf, "Accept-Encoding: gzip") != NULL, "Accept-Encoding header not found in %s\n", buf);
4667 InternetCloseHandle(req);
4669 req = HttpOpenRequestA(con, "GET", "/echo_request", "HTTP/1.0", NULL, NULL, 0, 0);
4670 ok(req != NULL, "HttpOpenRequest failed\n");
4672 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", ~0u, NULL, 0);
4673 ok(ret, "HttpSendRequestA failed\n");
4675 test_status_code(req, 200);
4676 receive_simple_request(req, buf, sizeof(buf));
4677 ok(strstr(buf, "Accept-Encoding: gzip") != NULL, "Accept-Encoding header not found in %s\n", buf);
4679 InternetCloseHandle(req);
4680 InternetCloseHandle(con);
4681 InternetCloseHandle(ses);
4684 static void test_basic_auth_credentials_reuse(int port)
4686 HINTERNET ses, con, req;
4687 DWORD status, size;
4688 BOOL ret;
4689 char buffer[0x40];
4691 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4692 ok( ses != NULL, "InternetOpenA failed\n" );
4694 con = InternetConnectA( ses, "localhost", port, "user", "pwd",
4695 INTERNET_SERVICE_HTTP, 0, 0 );
4696 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
4698 req = HttpOpenRequestA( con, "HEAD", "/upload.txt", NULL, NULL, NULL, 0, 0 );
4699 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
4701 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4702 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
4704 size = sizeof(buffer);
4705 SetLastError(0xdeadbeef);
4706 ret = InternetQueryOptionA(req, INTERNET_OPTION_USERNAME, buffer, &size);
4707 ok(ret, "unexpected failure %lu\n", GetLastError());
4708 ok(!strcmp(buffer, "user"), "got %s\n", buffer);
4709 ok(size == 4, "got %lu\n", size);
4711 size = sizeof(buffer);
4712 SetLastError(0xdeadbeef);
4713 ret = InternetQueryOptionA(req, INTERNET_OPTION_PASSWORD, buffer, &size);
4714 ok(ret, "unexpected failure %lu\n", GetLastError());
4715 ok(!strcmp(buffer, "pwd"), "got %s\n", buffer);
4716 ok(size == 3, "got %lu\n", size);
4718 status = 0xdeadbeef;
4719 size = sizeof(status);
4720 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4721 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
4722 ok( status == 200, "got %lu\n", status );
4724 InternetCloseHandle( req );
4725 InternetCloseHandle( con );
4726 InternetCloseHandle( ses );
4728 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4729 ok( ses != NULL, "InternetOpenA failed\n" );
4731 con = InternetConnectA( ses, "localhost", port, NULL, NULL,
4732 INTERNET_SERVICE_HTTP, 0, 0 );
4733 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
4735 req = HttpOpenRequestA( con, "PUT", "/upload2.txt", NULL, NULL, NULL, 0, 0 );
4736 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
4738 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4739 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
4741 size = sizeof(buffer);
4742 SetLastError(0xdeadbeef);
4743 ret = InternetQueryOptionA(req, INTERNET_OPTION_USERNAME, buffer, &size);
4744 ok(ret, "unexpected failure %lu\n", GetLastError());
4745 ok(!strcmp(buffer, "user"), "got %s\n", buffer);
4746 ok(size == 4, "got %lu\n", size);
4748 size = sizeof(buffer);
4749 SetLastError(0xdeadbeef);
4750 ret = InternetQueryOptionA(req, INTERNET_OPTION_PASSWORD, buffer, &size);
4751 ok(ret, "unexpected failure %lu\n", GetLastError());
4752 ok(!strcmp(buffer, "pwd"), "got %s\n", buffer);
4753 ok(size == 3, "got %lu\n", size);
4755 status = 0xdeadbeef;
4756 size = sizeof(status);
4757 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4758 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
4759 ok( status == 200, "got %lu\n", status );
4761 InternetCloseHandle( req );
4762 InternetCloseHandle( con );
4763 InternetCloseHandle( ses );
4766 static void test_basic_auth_credentials_end_session(int port)
4768 HINTERNET ses, con, req;
4769 DWORD status, size;
4770 BOOL ret;
4771 char buffer[0x40];
4773 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4774 ok( ses != NULL, "InternetOpenA failed\n" );
4776 con = InternetConnectA( ses, "localhost", port, "user", "pwd",
4777 INTERNET_SERVICE_HTTP, 0, 0 );
4778 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
4780 req = HttpOpenRequestA( con, "HEAD", "/upload.txt", NULL, NULL, NULL, 0, 0 );
4781 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
4783 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4784 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
4786 size = sizeof(buffer);
4787 SetLastError(0xdeadbeef);
4788 ret = InternetQueryOptionA(req, INTERNET_OPTION_USERNAME, buffer, &size);
4789 ok(ret, "unexpected failure %lu\n", GetLastError());
4790 ok(!strcmp(buffer, "user"), "got %s\n", buffer);
4791 ok(size == 4, "got %lu\n", size);
4793 size = sizeof(buffer);
4794 SetLastError(0xdeadbeef);
4795 ret = InternetQueryOptionA(req, INTERNET_OPTION_PASSWORD, buffer, &size);
4796 ok(ret, "unexpected failure %lu\n", GetLastError());
4797 ok(!strcmp(buffer, "pwd"), "got %s\n", buffer);
4798 ok(size == 3, "got %lu\n", size);
4800 status = 0xdeadbeef;
4801 size = sizeof(status);
4802 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4803 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
4804 ok( status == HTTP_STATUS_OK, "got %lu\n", status );
4806 InternetCloseHandle( req );
4807 InternetCloseHandle( con );
4808 InternetCloseHandle( ses );
4810 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4811 ok( ses != NULL, "InternetOpenA failed\n" );
4813 /* Clear the cached credentials */
4814 ret = InternetSetOptionA(ses, INTERNET_OPTION_END_BROWSER_SESSION, NULL, 0);
4815 ok(ret, "unexpected failure %lu\n", GetLastError());
4817 con = InternetConnectA( ses, "localhost", port, NULL, NULL,
4818 INTERNET_SERVICE_HTTP, 0, 0 );
4819 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
4821 req = HttpOpenRequestA( con, "PUT", "/upload2.txt", NULL, NULL, NULL, 0, 0 );
4822 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
4824 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4825 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
4827 size = sizeof(buffer);
4828 SetLastError(0xdeadbeef);
4829 ret = InternetQueryOptionA(req, INTERNET_OPTION_USERNAME, buffer, &size);
4830 ok(ret, "unexpected failure %lu\n", GetLastError());
4831 ok(!strcmp(buffer, ""), "got %s\n", buffer);
4832 ok(size == 0, "got %lu\n", size);
4834 size = sizeof(buffer);
4835 SetLastError(0xdeadbeef);
4836 ret = InternetQueryOptionA(req, INTERNET_OPTION_PASSWORD, buffer, &size);
4837 ok(ret, "unexpected failure %lu\n", GetLastError());
4838 ok(!strcmp(buffer, ""), "got %s\n", buffer);
4839 ok(size == 0, "got %lu\n", size);
4841 status = 0xdeadbeef;
4842 size = sizeof(status);
4843 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4844 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
4845 ok( status == HTTP_STATUS_BAD_REQUEST, "got %lu\n", status );
4847 InternetCloseHandle( req );
4848 InternetCloseHandle( con );
4849 InternetCloseHandle( ses );
4852 static void test_basic_auth_credentials_different(int port)
4854 HINTERNET ses, con, req;
4855 DWORD status, size;
4856 BOOL ret;
4857 char buffer[0x40];
4859 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4860 ok( ses != NULL, "InternetOpenA failed\n" );
4862 con = InternetConnectA( ses, "localhost", port, "user", "pwd",
4863 INTERNET_SERVICE_HTTP, 0, 0 );
4864 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
4866 req = HttpOpenRequestA( con, "HEAD", "/upload.txt", NULL, NULL, NULL, 0, 0 );
4867 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
4869 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4870 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
4872 size = sizeof(buffer);
4873 SetLastError(0xdeadbeef);
4874 ret = InternetQueryOptionA(req, INTERNET_OPTION_USERNAME, buffer, &size);
4875 ok(ret, "unexpected failure %lu\n", GetLastError());
4876 ok(!strcmp(buffer, "user"), "got %s\n", buffer);
4877 ok(size == 4, "got %lu\n", size);
4879 size = sizeof(buffer);
4880 SetLastError(0xdeadbeef);
4881 ret = InternetQueryOptionA(req, INTERNET_OPTION_PASSWORD, buffer, &size);
4882 ok(ret, "unexpected failure %lu\n", GetLastError());
4883 ok(!strcmp(buffer, "pwd"), "got %s\n", buffer);
4884 ok(size == 3, "got %lu\n", size);
4886 status = 0xdeadbeef;
4887 size = sizeof(status);
4888 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4889 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
4890 ok( status == 200, "got %lu\n", status );
4892 InternetCloseHandle( req );
4893 InternetCloseHandle( con );
4894 InternetCloseHandle( ses );
4896 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4897 ok( ses != NULL, "InternetOpenA failed\n" );
4899 con = InternetConnectA( ses, "localhost", port, "user1", "pwd1",
4900 INTERNET_SERVICE_HTTP, 0, 0 );
4901 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
4903 req = HttpOpenRequestA( con, "HEAD", "/upload3.txt", NULL, NULL, NULL, 0, 0 );
4904 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
4906 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4907 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
4909 size = sizeof(buffer);
4910 SetLastError(0xdeadbeef);
4911 ret = InternetQueryOptionA(req, INTERNET_OPTION_USERNAME, buffer, &size);
4912 ok(ret, "unexpected failure %lu\n", GetLastError());
4913 ok(!strcmp(buffer, "user1"), "got %s\n", buffer);
4914 ok(size == 5, "got %lu\n", size);
4916 size = sizeof(buffer);
4917 SetLastError(0xdeadbeef);
4918 ret = InternetQueryOptionA(req, INTERNET_OPTION_PASSWORD, buffer, &size);
4919 ok(ret, "unexpected failure %lu\n", GetLastError());
4920 ok(!strcmp(buffer, "pwd1"), "got %s\n", buffer);
4921 ok(size == 4, "got %lu\n", size);
4923 status = 0xdeadbeef;
4924 size = sizeof(status);
4925 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4926 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
4927 ok( status == 200, "got %lu\n", status );
4929 InternetCloseHandle( req );
4930 InternetCloseHandle( con );
4931 InternetCloseHandle( ses );
4935 * Manually set the Authorization for both calls.
4937 static void test_basic_auth_credentials_manual(int port)
4939 HINTERNET ses, con, req;
4940 DWORD status, size;
4941 BOOL ret;
4943 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4944 ok( ses != NULL, "InternetOpenA failed\n" );
4946 /* Clear the cached credentials */
4947 ret = InternetSetOptionA(ses, INTERNET_OPTION_END_BROWSER_SESSION, NULL, 0);
4948 ok(ret, "unexpected failure %lu\n", GetLastError());
4950 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );
4951 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
4953 req = HttpOpenRequestA( con, "HEAD", "/upload.txt", NULL, NULL, NULL, 0, 0 );
4954 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
4956 /* Set Authorization Header */
4957 ret = HttpAddRequestHeadersA(req, "Authorization: Basic dXNlcjpwd2Q=\r\n", ~0u,
4958 HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
4959 ok(ret, "HttpAddRequestHeaders Failed\n");
4961 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4962 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
4964 status = 0xdeadbeef;
4965 size = sizeof(status);
4966 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4967 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
4968 ok( status == 200, "got %lu\n", status );
4970 InternetCloseHandle( req );
4971 InternetCloseHandle( con );
4972 InternetCloseHandle( ses );
4974 /* Show manual headers are cached. */
4975 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4976 ok( ses != NULL, "InternetOpenA failed\n" );
4978 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );
4979 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
4981 req = HttpOpenRequestA( con, "HEAD", "/upload.txt", NULL, NULL, NULL, 0, 0 );
4982 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
4984 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4985 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
4987 status = 0xdeadbeef;
4988 size = sizeof(status);
4989 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4990 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
4991 ok( status == 401, "got %lu\n", status );
4993 InternetCloseHandle( req );
4994 InternetCloseHandle( con );
4995 InternetCloseHandle( ses );
4997 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4998 ok( ses != NULL, "InternetOpenA failed\n" );
5000 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );
5001 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
5003 req = HttpOpenRequestA( con, "HEAD", "/upload4.txt", NULL, NULL, NULL, 0, 0 );
5004 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
5006 /* Set Authorization Header */
5007 ret = HttpAddRequestHeadersA(req, "Authorization: Bearer dXNlcjE6cHdkMQ==\r\n", ~0u,
5008 HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
5009 ok(ret, "HttpAddRequestHeaders Failed\n");
5011 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
5012 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
5014 status = 0xdeadbeef;
5015 size = sizeof(status);
5016 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
5017 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
5018 ok( status == 200, "got %lu\n", status );
5020 InternetCloseHandle( req );
5021 InternetCloseHandle( con );
5022 InternetCloseHandle( ses );
5026 * Manually set the Authorization for the bearer call, which shows the cached is used.
5028 static void test_basic_auth_credentials_cached_manual(int port)
5030 HINTERNET ses, con, req;
5031 DWORD status, size;
5032 BOOL ret;
5034 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
5035 ok( ses != NULL, "InternetOpenA failed\n" );
5037 /* Clear the cached credentials */
5038 ret = InternetSetOptionA(ses, INTERNET_OPTION_END_BROWSER_SESSION, NULL, 0);
5039 ok(ret, "unexpected failure %lu\n", GetLastError());
5041 con = InternetConnectA( ses, "localhost", port, "user", "pwd",
5042 INTERNET_SERVICE_HTTP, 0, 0 );
5043 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
5045 req = HttpOpenRequestA( con, "HEAD", "/upload.txt", NULL, NULL, NULL, 0, 0 );
5046 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
5048 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
5049 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
5051 status = 0xdeadbeef;
5052 size = sizeof(status);
5053 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
5054 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
5055 ok( status == 200, "got %lu\n", status );
5057 InternetCloseHandle( req );
5058 InternetCloseHandle( con );
5059 InternetCloseHandle( ses );
5061 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
5062 ok( ses != NULL, "InternetOpenA failed\n" );
5064 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );
5065 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
5067 req = HttpOpenRequestA( con, "HEAD", "/upload4.txt", NULL, NULL, NULL, 0, 0 );
5068 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
5070 /* Setting an Authorization Header doesn't override the cached one. */
5071 ret = HttpAddRequestHeadersA(req, "Authorization: Bearer dXNlcjE6cHdkMQ==\r\n", ~0u,
5072 HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
5073 ok(ret, "HttpAddRequestHeaders Failed\n");
5075 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
5076 ok( ret, "HttpSendRequestA failed %lu\n", GetLastError() );
5078 status = 0xdeadbeef;
5079 size = sizeof(status);
5080 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
5081 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
5082 ok( status == 201, "got %lu\n", status );
5084 InternetCloseHandle( req );
5085 InternetCloseHandle( con );
5086 InternetCloseHandle( ses );
5089 static void test_async_read(int port)
5091 HINTERNET ses, con, req;
5092 INTERNET_BUFFERSA ib;
5093 char buffer[0x100];
5094 DWORD pending_reads;
5095 DWORD res, count, bytes;
5096 BOOL ret;
5098 reset_events();
5100 /* test asynchronous InternetReadFileEx */
5101 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC );
5102 ok( ses != NULL, "InternetOpenA failed\n" );
5103 pInternetSetStatusCallbackA( ses, &callback );
5105 SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
5106 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef );
5107 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
5108 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
5110 SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
5111 req = HttpOpenRequestA( con, "GET", "/async_read", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef );
5112 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
5113 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
5115 SET_OPTIONAL( INTERNET_STATUS_COOKIE_SENT );
5116 SET_OPTIONAL( INTERNET_STATUS_DETECTING_PROXY );
5117 SET_EXPECT( INTERNET_STATUS_CONNECTING_TO_SERVER );
5118 SET_EXPECT( INTERNET_STATUS_CONNECTED_TO_SERVER );
5119 SET_EXPECT( INTERNET_STATUS_SENDING_REQUEST );
5120 SET_EXPECT( INTERNET_STATUS_REQUEST_SENT );
5121 SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
5122 SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
5123 SET_OPTIONAL( INTERNET_STATUS_CLOSING_CONNECTION );
5124 SET_OPTIONAL( INTERNET_STATUS_CONNECTION_CLOSED );
5125 SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
5127 SetLastError( 0xdeadbeef );
5128 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
5129 ok( !ret, "HttpSendRequestA unexpectedly succeeded\n" );
5130 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %lu\n", GetLastError() );
5131 WaitForSingleObject( complete_event, INFINITE );
5132 ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error );
5134 CLEAR_NOTIFIED( INTERNET_STATUS_COOKIE_SENT );
5135 CLEAR_NOTIFIED( INTERNET_STATUS_DETECTING_PROXY );
5136 CHECK_NOTIFIED( INTERNET_STATUS_CONNECTING_TO_SERVER );
5137 CHECK_NOTIFIED( INTERNET_STATUS_CONNECTED_TO_SERVER );
5138 CHECK_NOTIFIED( INTERNET_STATUS_SENDING_REQUEST );
5139 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_SENT );
5140 CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
5141 CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
5142 CLEAR_NOTIFIED( INTERNET_STATUS_CLOSING_CONNECTION );
5143 CLEAR_NOTIFIED( INTERNET_STATUS_CONNECTION_CLOSED );
5144 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
5146 pending_reads = 0;
5147 memset( &ib, 0, sizeof(ib) );
5148 memset( buffer, 0, sizeof(buffer) );
5149 ib.dwStructSize = sizeof(ib);
5150 for (count = 0; count < sizeof(buffer); count += ib.dwBufferLength)
5152 ib.lpvBuffer = buffer + count;
5153 ib.dwBufferLength = min(16, sizeof(buffer) - count);
5155 SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
5156 SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
5158 ret = InternetReadFileExA( req, &ib, 0, 0xdeadbeef );
5159 if (!count) /* the first part should arrive immediately */
5160 ok( ret, "InternetReadFileExA failed %lu\n", GetLastError() );
5161 if (!ret)
5163 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %lu\n", GetLastError() );
5164 CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
5165 SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
5166 if (!pending_reads++)
5168 res = WaitForSingleObject( complete_event, 0 );
5169 ok( res == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %lu\n", res );
5170 SetEvent( conn_wait_event );
5172 res = WaitForSingleObject( complete_event, INFINITE );
5173 ok( res == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %lu\n", res );
5174 ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error );
5175 todo_wine_if( pending_reads > 1 )
5176 ok( ib.dwBufferLength != 0, "expected ib.dwBufferLength != 0\n" );
5177 CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
5178 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
5181 CLEAR_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
5182 CLEAR_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
5183 if (!ib.dwBufferLength) break;
5186 ok( pending_reads == 1, "expected 1 pending read, got %lu\n", pending_reads );
5187 ok( !strcmp(buffer, page1), "unexpected buffer content\n" );
5188 close_async_handle( ses, 2 );
5189 ResetEvent( conn_wait_event );
5191 /* test asynchronous InternetReadFile */
5192 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC );
5193 ok( ses != NULL, "InternetOpenA failed\n" );
5194 pInternetSetStatusCallbackA( ses, &callback );
5196 SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
5197 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef );
5198 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
5199 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
5201 SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
5202 req = HttpOpenRequestA( con, "GET", "/async_read", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef );
5203 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
5204 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
5206 SET_OPTIONAL( INTERNET_STATUS_COOKIE_SENT );
5207 SET_OPTIONAL( INTERNET_STATUS_DETECTING_PROXY );
5208 SET_EXPECT( INTERNET_STATUS_CONNECTING_TO_SERVER );
5209 SET_EXPECT( INTERNET_STATUS_CONNECTED_TO_SERVER );
5210 SET_EXPECT( INTERNET_STATUS_SENDING_REQUEST );
5211 SET_EXPECT( INTERNET_STATUS_REQUEST_SENT );
5212 SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
5213 SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
5214 SET_OPTIONAL( INTERNET_STATUS_CLOSING_CONNECTION );
5215 SET_OPTIONAL( INTERNET_STATUS_CONNECTION_CLOSED );
5216 SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
5218 SetLastError( 0xdeadbeef );
5219 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
5220 ok( !ret, "HttpSendRequestA unexpectedly succeeded\n" );
5221 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %lu\n", GetLastError() );
5222 WaitForSingleObject( complete_event, INFINITE );
5223 ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error );
5225 CLEAR_NOTIFIED( INTERNET_STATUS_COOKIE_SENT );
5226 CLEAR_NOTIFIED( INTERNET_STATUS_DETECTING_PROXY );
5227 CHECK_NOTIFIED( INTERNET_STATUS_CONNECTING_TO_SERVER );
5228 CHECK_NOTIFIED( INTERNET_STATUS_CONNECTED_TO_SERVER );
5229 CHECK_NOTIFIED( INTERNET_STATUS_SENDING_REQUEST );
5230 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_SENT );
5231 CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
5232 CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
5233 CLEAR_NOTIFIED( INTERNET_STATUS_CLOSING_CONNECTION );
5234 CLEAR_NOTIFIED( INTERNET_STATUS_CONNECTION_CLOSED );
5235 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
5237 pending_reads = 0;
5238 memset( buffer, 0, sizeof(buffer) );
5239 for (count = 0; count < sizeof(buffer); count += bytes)
5241 SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
5242 SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
5244 bytes = 0xdeadbeef;
5245 ret = InternetReadFile( req, buffer + count, min(16, sizeof(buffer) - count), &bytes );
5246 if (!count) /* the first part should arrive immediately */
5247 ok( ret, "InternetReadFile failed %lu\n", GetLastError() );
5248 if (!ret)
5250 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %lu\n", GetLastError() );
5251 ok( bytes == 0, "expected 0, got %lu\n", bytes );
5252 CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
5253 SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
5254 if (!pending_reads++)
5256 res = WaitForSingleObject( complete_event, 0 );
5257 ok( res == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %lu\n", res );
5258 SetEvent( conn_wait_event );
5260 res = WaitForSingleObject( complete_event, INFINITE );
5261 ok( res == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %lu\n", res );
5262 ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error );
5263 todo_wine_if( pending_reads > 1 )
5264 ok( bytes != 0, "expected bytes != 0\n" );
5265 CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
5266 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
5269 CLEAR_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
5270 CLEAR_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
5271 if (!bytes) break;
5274 ok( pending_reads == 1, "expected 1 pending read, got %lu\n", pending_reads );
5275 ok( !strcmp(buffer, page1), "unexpected buffer content\n" );
5276 close_async_handle( ses, 2 );
5279 static void server_send_string(const char *msg)
5281 send(server_socket, msg, strlen(msg), 0);
5284 static size_t server_read_data(char *buf, size_t buf_size)
5286 return recv(server_socket, buf, buf_size, 0);
5289 #define server_read_request(a) _server_read_request(__LINE__,a)
5290 static void _server_read_request(unsigned line, const char *expected_request)
5292 char buf[4000], *p;
5293 size_t size;
5295 size = server_read_data(buf, sizeof(buf) - 1);
5296 buf[size] = 0;
5297 p = strstr(buf, "\r\n");
5298 if(p) *p = 0;
5299 ok_(__FILE__,line)(p && !strcmp(buf, expected_request), "unexpected request %s\n", buf);
5302 static BOOL skip_receive_notification_tests;
5303 static DWORD received_response_size;
5305 static void WINAPI readex_callback(HINTERNET handle, DWORD_PTR context, DWORD status, void *info, DWORD info_size)
5307 switch(status) {
5308 case INTERNET_STATUS_RECEIVING_RESPONSE:
5309 if(!skip_receive_notification_tests)
5310 callback(handle, context, status, info, info_size);
5311 break;
5312 case INTERNET_STATUS_RESPONSE_RECEIVED:
5313 if(!skip_receive_notification_tests)
5314 callback(handle, context, status, info, info_size);
5315 received_response_size = *(DWORD*)info;
5316 break;
5317 case INTERNET_STATUS_REQUEST_SENT:
5318 callback(handle, context, status, info, info_size);
5319 SetEvent(request_sent_event);
5320 break;
5321 default:
5322 callback(handle, context, status, info, info_size);
5326 static void send_socket_request(test_request_t *req, BOOL new_connection)
5328 BOOL ret;
5330 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5331 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5332 if(new_connection) {
5333 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5334 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5336 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
5337 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
5338 if(!skip_receive_notification_tests)
5339 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5341 SetLastError(0xdeadbeef);
5342 ret = HttpSendRequestA(req->request, NULL, 0, NULL, 0);
5343 ok(!ret, "HttpSendRequestA unexpectedly succeeded\n");
5344 ok(GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %lu\n", GetLastError());
5346 if(new_connection)
5347 WaitForSingleObject(server_req_rec_event, INFINITE);
5348 WaitForSingleObject(request_sent_event, INFINITE);
5350 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5351 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5352 if(new_connection) {
5353 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
5354 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
5356 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
5357 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
5360 static void open_socket_request(int port, test_request_t *req, const char *verb)
5362 /* We're connecting to new socket */
5363 if(!verb)
5364 reset_events();
5366 req->session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
5367 ok(req->session != NULL, "InternetOpenA failed\n");
5368 pInternetSetStatusCallbackA(req->session, readex_callback);
5370 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5371 req->connection = InternetConnectA(req->session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef);
5372 ok(req->connection != NULL, "InternetConnectA failed %lu\n", GetLastError());
5373 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED );
5375 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5376 req->request = HttpOpenRequestA(req->connection, "GET", verb ? verb : "/socket",
5377 NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef);
5378 ok(req->request != NULL, "HttpOpenRequestA failed %lu\n", GetLastError());
5379 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
5381 send_socket_request(req, !verb);
5384 static void open_read_test_request(int port, test_request_t *req, const char *response)
5386 if(!skip_receive_notification_tests)
5387 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5389 open_socket_request(port, req, NULL);
5391 if(!skip_receive_notification_tests) {
5392 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5393 received_response_size = 0xdeadbeef;
5395 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5397 server_send_string(response);
5398 WaitForSingleObject(complete_event, INFINITE);
5400 if(!skip_receive_notification_tests) {
5401 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5402 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5403 todo_wine
5404 ok(received_response_size == strlen(response), "received_response_size = %lu\n", received_response_size);
5406 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5407 ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error);
5410 #define readex_expect_sync_data_len(a,b,c,d,e,f,g) _readex_expect_sync_data_len(__LINE__,a,b,c,d,e,f,g)
5411 static void _readex_expect_sync_data_len(unsigned line, HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf,
5412 DWORD buf_size, const char *exdata, DWORD len, DWORD expect_receive)
5414 BOOL ret;
5416 if(!skip_receive_notification_tests && expect_receive) {
5417 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5418 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5419 received_response_size = 0xdeadbeef;
5422 memset(buf->lpvBuffer, 0xff, buf_size);
5423 buf->dwBufferLength = buf_size;
5424 ret = InternetReadFileExW(req, buf, flags, 0xdeadbeef);
5425 ok_(__FILE__,line)(ret, "InternetReadFileExW failed: %lu\n", GetLastError());
5426 ok_(__FILE__,line)(buf->dwBufferLength == len, "dwBufferLength = %lu, expected %lu\n", buf->dwBufferLength, len);
5427 if(len && exdata)
5428 ok_(__FILE__,line)(!memcmp(buf->lpvBuffer, exdata, len), "Unexpected data\n");
5430 if(!skip_receive_notification_tests && expect_receive) {
5431 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5432 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5433 ok_(__FILE__,line)(received_response_size == len, "received_response_size = %lu\n", received_response_size);
5437 #define readex_expect_sync_data(a,b,c,d,e,f) _readex_expect_sync_data(__LINE__,a,b,c,d,e,f)
5438 static void _readex_expect_sync_data(unsigned line, HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf,
5439 DWORD buf_size, const char *exdata, DWORD expect_receive)
5441 _readex_expect_sync_data_len(line, req, flags, buf, buf_size, exdata, strlen(exdata), expect_receive);
5444 #define read_expect_sync_data_len(a,b,c,d,e) _read_expect_sync_data_len(__LINE__,a,b,c,d,e)
5445 static void _read_expect_sync_data_len(unsigned line, HINTERNET req, void *buf, DWORD buf_size,
5446 const char *exdata, DWORD len)
5448 DWORD ret_size = 0xdeadbeef;
5449 BOOL ret;
5451 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5452 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5453 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5454 received_response_size = 0xdeadbeef;
5456 memset(buf, 0xff, buf_size);
5457 ret = InternetReadFile(req, buf, buf_size, &ret_size);
5458 ok_(__FILE__,line)(ret, "InternetReadFileExW failed: %lu\n", GetLastError());
5459 ok_(__FILE__,line)(ret_size == len, "dwBufferLength = %lu, expected %lu\n", ret_size, len);
5460 if(len && exdata)
5461 ok_(__FILE__,line)(!memcmp(buf, exdata, len), "Unexpected data\n");
5463 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5464 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5465 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5466 ok_(__FILE__,line)(received_response_size == len, "received_response_size = %lu\n", received_response_size);
5467 ok_(__FILE__,line)(!req_error, "req_error = %lu\n", req_error);
5470 #define read_expect_sync_data(a,b,c,d) _read_expect_sync_data(__LINE__,a,b,c,d)
5471 static void _read_expect_sync_data(unsigned line, HINTERNET req, void *buf,
5472 DWORD buf_size, const char *exdata)
5474 _read_expect_sync_data_len(line, req, buf, buf_size, exdata, strlen(exdata));
5477 static void close_connection(void)
5479 char c;
5480 SetEvent(conn_wait_event);
5481 recv(server_socket, &c, 1, 0);
5484 #define send_response_and_wait(a,b,c,d,e,f,g,h) _send_response_and_wait(__LINE__,a,b,c,d,e,f,g,h)
5485 static void _send_response_and_wait(unsigned line, const char *response, BOOL do_close_connection,
5486 void *buf, DWORD *ret_size, const char *exdata,
5487 DWORD expected_size, DWORD expected_req_error, DWORD expected_receive_size)
5489 if(!skip_receive_notification_tests)
5490 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5491 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5493 if(response)
5494 server_send_string(response);
5496 if(do_close_connection)
5497 close_connection();
5499 WaitForSingleObject(complete_event, INFINITE);
5501 if(!skip_receive_notification_tests)
5502 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5503 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5504 if(!skip_receive_notification_tests && expected_receive_size != -1)
5505 todo_wine_if(received_response_size != expected_receive_size) /* FIXME! remove when wine is fixed */
5506 ok_(__FILE__,line)(received_response_size == expected_receive_size,
5507 "received_response_size = %lu\n", received_response_size);
5508 ok_(__FILE__,line)(req_error == expected_req_error, "req_error = %lu, expected %lu\n", req_error, expected_req_error);
5510 /* If IRF_NO_WAIT is used, buffer is not changed. */
5511 ok_(__FILE__,line)(*ret_size == expected_size, "dwBufferLength = %lu\n", *ret_size);
5512 if(exdata)
5513 ok_(__FILE__,line)(!memcmp(buf, exdata, strlen(exdata)), "unexpected buffer data\n");
5514 else if(buf)
5515 ok_(__FILE__,line)(!*(DWORD*)buf, "buffer data changed\n");
5518 #define send_response_ex_and_wait(a,b,c,d,e,f) _send_response_ex_and_wait(__LINE__,a,b,c,d,e,f)
5519 static void _send_response_ex_and_wait(unsigned line, const char *response, BOOL close_connection,
5520 INTERNET_BUFFERSW *buf, const char *exdata, DWORD expected_req_error,
5521 DWORD expected_receive_size)
5523 _send_response_and_wait(line, response, close_connection, buf->lpvBuffer, &buf->dwBufferLength,
5524 exdata, exdata ? strlen(exdata) : buf->dwBufferLength, expected_req_error,
5525 expected_receive_size);
5528 static void send_response_len_and_wait(unsigned len, BOOL close_connection, INTERNET_BUFFERSW *buf)
5530 char *response;
5532 response = HeapAlloc(GetProcessHeap(), 0, len+1);
5533 memset(response, 'x', len);
5534 response[len] = 0;
5535 send_response_ex_and_wait(response, close_connection, buf, NULL, 0, -1);
5536 HeapFree(GetProcessHeap(), 0, response);
5539 #define readex_expect_async(a,b,c,d,e) _readex_expect_async(__LINE__,a,b,c,d,e)
5540 static void _readex_expect_async(unsigned line, HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf,
5541 DWORD buf_size, const char *exdata)
5543 unsigned len = 0;
5544 BOOL ret;
5546 if(!skip_receive_notification_tests)
5547 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5549 memset(buf->lpvBuffer, 0, max(buf_size, sizeof(DWORD)));
5550 buf->dwBufferLength = buf_size;
5551 ret = InternetReadFileExW(req, buf, flags, 0xdeadbeef);
5552 ok_(__FILE__,line)(!ret && GetLastError() == ERROR_IO_PENDING, "InternetReadFileExW returned %x (%lu)\n", ret, GetLastError());
5553 ok_(__FILE__,line)(buf->dwBufferLength == buf_size, "dwBufferLength = %lu, expected %lu\n", buf->dwBufferLength, buf_size);
5554 if(exdata) {
5555 len = strlen(exdata);
5556 ok_(__FILE__,line)(!memcmp(buf->lpvBuffer, exdata, len), "unexpected buffer data\n");
5557 }else {
5558 ok_(__FILE__,line)(!*(DWORD*)buf->lpvBuffer, "buffer data changed\n");
5561 if(!skip_receive_notification_tests)
5562 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5565 static void read_expect_async(HINTERNET req, void *buf, DWORD buf_size, DWORD *ret_size, const char *exdata)
5567 unsigned len = 0;
5568 const char *p;
5569 BOOL ret;
5571 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5573 *ret_size = 0xdeadbeef;
5574 memset(buf, 0, buf_size);
5575 ret = InternetReadFile(req, buf, buf_size, ret_size);
5576 ok(!ret && GetLastError() == ERROR_IO_PENDING, "InternetReadFileExW returned %x (%lu)\n", ret, GetLastError());
5577 ok(*ret_size == 0, "dwBufferLength = %lu\n", *ret_size);
5578 if(exdata) {
5579 len = strlen(exdata);
5580 ok(!memcmp(buf, exdata, len), "unexpected buffer data\n");
5582 for(p = (const char*)buf + len; p < (const char*)buf + buf_size; p++) {
5583 if(*p)
5584 ok(0, "buffer data changed\n");
5587 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5590 #define expect_data_available(a,b) _expect_data_available(__LINE__,a,b)
5591 static DWORD _expect_data_available(unsigned line, HINTERNET req, int exsize)
5593 DWORD size = 0;
5594 BOOL res;
5596 res = InternetQueryDataAvailable(req, &size, 0, 0);
5597 ok_(__FILE__,line)(res, "InternetQueryDataAvailable failed: %lu\n", GetLastError());
5598 if(exsize != -1)
5599 ok_(__FILE__,line)(size == exsize, "size = %lu, expected %u\n", size, exsize);
5601 return size;
5604 #define async_query_data_available(a,b) _async_query_data_available(__LINE__,a,b)
5605 static void _async_query_data_available(unsigned line, HINTERNET req, DWORD *size)
5607 BOOL res;
5609 if(!skip_receive_notification_tests)
5610 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5612 *size = 0xdeadbeef;
5613 res = InternetQueryDataAvailable(req, size, 0, 0);
5614 ok_(__FILE__,line)(!res && GetLastError() == ERROR_IO_PENDING,
5615 "InternetQueryDataAvailable returned: %x(%lu)\n", res, GetLastError());
5616 ok_(__FILE__,line)(!*size, "size = %lu\n", *size);
5618 if(!skip_receive_notification_tests)
5619 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5622 static void test_http_read(int port)
5624 INTERNET_BUFFERSW ib;
5625 test_request_t req;
5626 DWORD read_size;
5627 char buf[24000];
5628 DWORD avail, i;
5630 if(!is_ie7plus)
5631 return;
5633 memset(&ib, 0, sizeof(ib));
5634 ib.dwStructSize = sizeof(ib);
5635 ib.lpvBuffer = buf;
5637 trace("Testing InternetReadFileExW with IRF_ASYNC flag...\n");
5639 open_read_test_request(port, &req,
5640 "HTTP/1.1 200 OK\r\n"
5641 "Server: winetest\r\n"
5642 "\r\n"
5643 "xx");
5645 readex_expect_async(req.request, IRF_ASYNC, &ib, 4, "xx");
5647 send_response_ex_and_wait("yy1234567890", FALSE, &ib, "xxyy", 0, 2);
5648 readex_expect_sync_data(req.request, IRF_ASYNC, &ib, 4, "1234", 4);
5649 readex_expect_sync_data(req.request, IRF_ASYNC, &ib, 5, "56789", 5);
5651 readex_expect_async(req.request, IRF_ASYNC, &ib, sizeof(buf), "0");
5652 send_response_ex_and_wait("123", TRUE, &ib, "0123", 0, 4);
5654 close_async_handle(req.session, 2);
5656 trace("Testing InternetReadFileExW with no flags...\n");
5658 open_read_test_request(port, &req,
5659 "HTTP/1.1 200 OK\r\n"
5660 "Server: winetest\r\n"
5661 "\r\n"
5662 "xx");
5664 readex_expect_async(req.request, 0, &ib, 4, "xx");
5666 send_response_ex_and_wait("yy1234567890", FALSE, &ib, "xxyy", 0, 2);
5667 readex_expect_sync_data(req.request, 0, &ib, 4, "1234", 4);
5668 readex_expect_sync_data(req.request, 0, &ib, 5, "56789", 5);
5670 readex_expect_async(req.request, 0, &ib, sizeof(buf), "0");
5671 send_response_ex_and_wait("123", TRUE, &ib, "0123", 0, 4);
5673 close_async_handle(req.session, 2);
5675 trace("Testing InternetReadFile...\n");
5677 open_read_test_request(port, &req,
5678 "HTTP/1.1 200 OK\r\n"
5679 "Server: winetest\r\n"
5680 "\r\n"
5681 "xx");
5683 read_expect_async(req.request, buf, 4, &read_size, "xx");
5685 send_response_and_wait("yy1234567890", FALSE, buf, &read_size, "xxyy", 4, 0, 2);
5686 read_expect_sync_data(req.request, buf, 4, "1234");
5687 read_expect_sync_data(req.request, buf, 5, "56789");
5689 read_expect_async(req.request, buf, sizeof(buf), &read_size, "0");
5690 send_response_and_wait("123", TRUE, buf, &read_size, "0123", 4, 0, 4);
5692 close_async_handle(req.session, 2);
5694 trace("Testing InternetReadFileExW with IRF_NO_WAIT flag...\n");
5696 open_read_test_request(port, &req,
5697 "HTTP/1.1 200 OK\r\n"
5698 "Server: winetest\r\n"
5699 "\r\n"
5700 "xx");
5702 SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
5704 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "xx", 0);
5706 if(notified[INTERNET_STATUS_RECEIVING_RESPONSE]) {
5707 win_skip("Skipping receive notification tests on too old Windows.\n");
5708 skip_receive_notification_tests = TRUE;
5710 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5712 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5713 send_response_ex_and_wait("1234567890", FALSE, &ib, NULL, 0, 10);
5714 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, 5, "12345", 0);
5715 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "67890", 0);
5717 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5718 send_response_ex_and_wait("12345", TRUE, &ib, NULL, 0, 5);
5720 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "12345", 0);
5721 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "", TRUE);
5723 close_async_handle(req.session, 2);
5725 open_read_test_request(port, &req,
5726 "HTTP/1.1 200 OK\r\n"
5727 "Server: winetest\r\n"
5728 "Transfer-Encoding: chunked\r\n"
5729 "\r\n"
5730 "9\r\n123456789");
5731 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123456789", 0);
5732 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5734 send_response_ex_and_wait("\r\n1\r\na\r\n1\r\nb\r", FALSE, &ib, NULL, 0, 13);
5735 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab", 0);
5736 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5738 send_response_ex_and_wait("\n3\r\nab", FALSE, &ib, NULL, 0, 6);
5739 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab", 0);
5740 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5742 send_response_ex_and_wait("c", FALSE, &ib, NULL, 0, 1);
5743 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "c", 0);
5744 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5746 send_response_ex_and_wait("\r\n1\r\nx\r\n0\r\n\r\n", TRUE, &ib, NULL, 0, 13);
5747 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "x", 0);
5748 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "", 0);
5750 close_async_handle(req.session, 2);
5752 open_read_test_request(port, &req,
5753 "HTTP/1.1 200 OK\r\n"
5754 "Server: winetest\r\n"
5755 "Transfer-Encoding: chunked\r\n"
5756 "\r\n"
5757 "3\r\n123\r\n");
5758 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123", 0);
5759 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5761 send_response_ex_and_wait("0\r\n\r\n", TRUE, &ib, NULL, 0, 5);
5762 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "", 0);
5764 close_async_handle(req.session, 2);
5766 open_read_test_request(port, &req,
5767 "HTTP/1.1 200 OK\r\n"
5768 "Server: winetest\r\n"
5769 "Connection: close\r\n"
5770 "\r\n");
5771 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5772 send_response_ex_and_wait("123", TRUE, &ib, NULL, 0, 3);
5773 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123", 0);
5775 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
5776 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
5777 close_async_handle(req.session, 2);
5778 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5779 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5781 trace("Testing InternetQueryDataAvailable...\n");
5783 open_read_test_request(port, &req,
5784 "HTTP/1.1 200 OK\r\n"
5785 "Server: winetest\r\n"
5786 "\r\n"
5787 "123");
5788 expect_data_available(req.request, 3);
5789 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123", 0);
5790 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf), NULL);
5792 send_response_len_and_wait(20000, TRUE, &ib);
5793 avail = expect_data_available(req.request, -1);
5794 ok(avail <= 20000, "avail = %lu\n", avail);
5796 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
5797 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
5798 close_async_handle(req.session, 2);
5799 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5800 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5802 open_read_test_request(port, &req,
5803 "HTTP/1.1 200 OK\r\n"
5804 "Server: winetest\r\n"
5805 "Connection: close\r\n"
5806 "\r\n"
5807 "123");
5809 expect_data_available(req.request, 3);
5810 readex_expect_sync_data(req.request, 0, &ib, 3, "123", 0);
5812 async_query_data_available(req.request, &read_size);
5813 send_response_and_wait("1234567890", FALSE, NULL, &read_size, NULL, 10, 10, 10);
5815 readex_expect_sync_data(req.request, 0, &ib, 9, "123456789", 0);
5816 expect_data_available(req.request, 1);
5817 readex_expect_sync_data(req.request, 0, &ib, 1, "0", 0);
5819 async_query_data_available(req.request, &read_size);
5820 send_response_and_wait("1234567890", FALSE, NULL, &read_size, NULL, 10, 10, 10);
5821 expect_data_available(req.request, 10);
5822 for(i = 0; i < 10; i++)
5823 server_send_string("x");
5824 expect_data_available(req.request, 10);
5826 readex_expect_async(req.request, IRF_ASYNC, &ib, 21, "1234567890");
5827 send_response_ex_and_wait("X", FALSE, &ib, "1234567890xxxxxxxxxxX", 0, 11);
5828 async_query_data_available(req.request, &read_size);
5830 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
5831 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
5832 send_response_and_wait(NULL, TRUE, NULL, &read_size, NULL, 0, 0, 0);
5833 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5834 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5836 close_async_handle(req.session, 2);
5838 skip_receive_notification_tests = FALSE;
5841 static void test_connection_break(int port)
5843 INTERNET_BUFFERSW ib;
5844 test_request_t req;
5845 char buf[24000];
5847 if(!is_ie7plus)
5848 return;
5850 memset(&ib, 0, sizeof(ib));
5851 ib.dwStructSize = sizeof(ib);
5852 ib.lpvBuffer = buf;
5854 trace("Testing InternetReadFileExW on broken connection...\n");
5856 open_read_test_request(port, &req,
5857 "HTTP/1.1 200 OK\r\n"
5858 "Server: winetest\r\n"
5859 "Content-Length: 10000\r\n"
5860 "\r\n"
5861 "xx");
5863 /* close connection and make sure that it's closed on handle release. */
5864 close_connection();
5865 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
5866 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
5867 close_async_handle(req.session, 2);
5868 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5869 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5872 static void test_long_url(int port)
5874 char long_path[INTERNET_MAX_PATH_LENGTH*2] = "/echo_request?";
5875 char buf[sizeof(long_path)*2], url[sizeof(buf)];
5876 test_request_t req;
5877 DWORD size, len;
5878 BOOL ret;
5880 if(!is_ie7plus)
5881 return;
5883 memset(long_path+strlen(long_path), 'x', sizeof(long_path)-strlen(long_path));
5884 long_path[sizeof(long_path)-1] = 0;
5885 open_simple_request(&req, "localhost", port, NULL, long_path);
5887 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
5888 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
5889 test_status_code(req.request, 200);
5891 receive_simple_request(req.request, buf, sizeof(buf));
5892 ok(strstr(buf, long_path) != NULL, "long pathnot found in %s\n", buf);
5894 sprintf(url, "http://localhost:%u%s", port, long_path);
5896 size = sizeof(buf);
5897 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_URL, buf, &size);
5898 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %lu\n", GetLastError());
5899 len = strlen(url);
5900 ok(size == len, "size = %lu, expected %lu\n", size, len);
5901 ok(!strcmp(buf, url), "Wrong URL %s, expected %s\n", buf, url);
5903 close_request(&req);
5906 static void test_persistent_connection(int port)
5908 INTERNET_BUFFERSW ib;
5909 test_request_t req;
5910 char buf[24000];
5912 if(!is_ie7plus)
5913 return;
5915 memset(&ib, 0, sizeof(ib));
5916 ib.dwStructSize = sizeof(ib);
5917 ib.lpvBuffer = buf;
5919 skip_receive_notification_tests = TRUE;
5921 trace("Testing persistent connection...\n");
5923 open_read_test_request(port, &req,
5924 "HTTP/1.1 200 OK\r\n"
5925 "Server: winetest\r\n"
5926 "Content-Length: 2\r\n"
5927 "\r\n"
5928 "xx");
5929 readex_expect_sync_data(req.request, IRF_ASYNC, &ib, 4, "xx", 0);
5930 close_async_handle(req.session, 2);
5932 open_socket_request(port, &req, "/test_simple_chunked");
5933 server_read_request("GET /test_simple_chunked HTTP/1.1");
5935 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5936 server_send_string("HTTP/1.1 200 OK\r\n"
5937 "Server: winetest\r\n"
5938 "Transfer-Encoding: chunked\r\n"
5939 "\r\n"
5940 "2\r\nab\r\n");
5941 WaitForSingleObject(complete_event, INFINITE);
5942 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5943 ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error);
5945 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab", 0);
5946 readex_expect_async(req.request, IRF_ASYNC, &ib, sizeof(buf), NULL);
5947 send_response_ex_and_wait("3\r\nabc\r\n0\r\n\r\n", FALSE, &ib, "abc", 0, 13);
5948 close_async_handle(req.session, 2);
5950 open_socket_request(port, &req, "/chunked");
5951 server_read_request("GET /chunked HTTP/1.1");
5953 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5954 server_send_string("HTTP/1.1 200 OK\r\n"
5955 "Server: winetest\r\n"
5956 "Transfer-Encoding: chunked\r\n"
5957 "\r\n"
5958 "2\r\nab\r\n");
5959 WaitForSingleObject(complete_event, INFINITE);
5960 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5961 ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error);
5963 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, 3, "ab", 0);
5964 readex_expect_async(req.request, IRF_ASYNC, &ib, 3, NULL);
5965 send_response_ex_and_wait("3\r\nabc\r\n", FALSE, &ib, "abc", 0, 13);
5967 /* send another request on the same request handle, it must drain remaining last chunk marker */
5968 server_send_string("0\r\n\r\n");
5970 send_socket_request(&req, FALSE);
5971 server_read_request("GET /chunked HTTP/1.1");
5973 ResetEvent(complete_event);
5974 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5975 server_send_string("HTTP/1.1 201 OK\r\n"
5976 "Server: winetest\r\n"
5977 "Content-Length: 0\r\n"
5978 "Connection: keep-alive\r\n"
5979 "\r\n");
5980 WaitForSingleObject(complete_event, INFINITE);
5981 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5982 ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error);
5984 test_status_code(req.request, 201);
5985 close_async_handle(req.session, 2);
5987 /* the connection is still valid */
5988 open_socket_request(port, &req, "/another_chunked");
5989 server_read_request("GET /another_chunked HTTP/1.1");
5991 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5992 server_send_string("HTTP/1.1 200 OK\r\n"
5993 "Server: winetest\r\n"
5994 "Transfer-Encoding: chunked\r\n"
5995 "\r\n"
5996 "2\r\nab\r\n");
5997 WaitForSingleObject(complete_event, INFINITE);
5998 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5999 ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error);
6001 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab", 0);
6002 readex_expect_async(req.request, IRF_ASYNC, &ib, sizeof(buf), NULL);
6004 /* we're missing trailing '\n'; the connection can't be drained without blocking,
6005 * so it will be closed */
6006 send_response_ex_and_wait("3\r\nabc\r\n0\r\n\r", FALSE, &ib, "abc", 0, 13);
6008 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
6009 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
6010 close_async_handle(req.session, 2);
6011 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
6012 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
6014 close_connection();
6015 skip_receive_notification_tests = FALSE;
6018 static void test_redirect(int port)
6020 char buf[4000], expect_url[INTERNET_MAX_URL_LENGTH];
6021 INTERNET_BUFFERSW ib;
6022 test_request_t req;
6024 if(!is_ie7plus)
6025 return;
6027 skip_receive_notification_tests = TRUE;
6029 memset(&ib, 0, sizeof(ib));
6030 ib.dwStructSize = sizeof(ib);
6031 ib.lpvBuffer = buf;
6033 trace("Testing redirection...\n");
6035 open_socket_request(port, &req, NULL);
6037 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
6038 SET_EXPECT(INTERNET_STATUS_REDIRECT);
6039 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
6040 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
6042 server_send_string("HTTP/1.1 302 Found\r\n"
6043 "Server: winetest\r\n"
6044 "Location: test_redirection\r\n"
6045 "Connection: keep-alive\r\n"
6046 "Content-Length: 0\r\n"
6047 "\r\n");
6049 server_read_request("GET /test_redirection HTTP/1.1");
6051 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
6053 sprintf(expect_url, "http://localhost:%u/test_redirection", port);
6054 test_request_url(req.request, expect_url);
6056 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
6058 server_send_string("HTTP/1.1 200 OK\r\n"
6059 "Server: winetest\r\n"
6060 "Content-Length: 3\r\n"
6061 "\r\n"
6062 "xxx");
6064 WaitForSingleObject(complete_event, INFINITE);
6066 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
6067 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
6068 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
6069 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
6070 ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error);
6072 test_status_code(req.request, 200);
6074 close_connection();
6075 close_async_handle(req.session, 2);
6077 trace("Test redirect to non-http URL...\n");
6079 open_socket_request(port, &req, NULL);
6081 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
6083 server_send_string("HTTP/1.1 302 Found\r\n"
6084 "Server: winetest\r\n"
6085 "Location: test:non:http/url\r\n"
6086 "Connection: keep-alive\r\n"
6087 "Content-Length: 0\r\n"
6088 "\r\n");
6090 WaitForSingleObject(complete_event, INFINITE);
6092 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
6093 ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %lu\n", req_error);
6095 sprintf(expect_url, "http://localhost:%u/socket", port);
6096 test_request_url(req.request, expect_url);
6097 test_status_code(req.request, 302);
6099 close_connection();
6100 close_async_handle(req.session, 2);
6102 trace("Test redirect to http URL with no host name...\n");
6104 open_socket_request(port, &req, NULL);
6106 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
6108 server_send_string("HTTP/1.1 302 Found\r\n"
6109 "Server: winetest\r\n"
6110 "Location: http:///nohost\r\n"
6111 "Connection: keep-alive\r\n"
6112 "Content-Length: 0\r\n"
6113 "\r\n");
6115 WaitForSingleObject(complete_event, INFINITE);
6117 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
6118 ok(req_error == ERROR_INTERNET_INVALID_URL, "expected ERROR_INTERNET_INVALID_URL, got %lu\n", req_error);
6120 sprintf(expect_url, "http://localhost:%u/socket", port);
6121 test_request_url(req.request, expect_url);
6122 test_status_code(req.request, 302);
6124 close_connection();
6125 close_async_handle(req.session, 2);
6127 skip_receive_notification_tests = FALSE;
6130 static void test_remove_dot_segments(int port)
6132 test_request_t req;
6133 BOOL ret;
6135 open_simple_request(&req, "localhost", port, NULL, "/A/../B/./C/../../test_remove_dot_segments");
6137 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
6138 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
6139 test_status_code(req.request, 200);
6141 close_request(&req);
6144 struct large_test
6146 DWORD64 content_length;
6147 BOOL ret;
6150 static void test_large_content(int port)
6152 struct large_test tests[] = {
6153 { 0, TRUE },
6154 { UINT_MAX-1, TRUE },
6155 { UINT_MAX, TRUE },
6156 { (DWORD64)UINT_MAX+1, FALSE },
6157 { ~0, FALSE },
6159 test_request_t req;
6160 DWORD sizelen, len;
6161 DWORD read_size;
6162 DWORD64 len64;
6163 char buf[16];
6164 BOOL ret;
6165 size_t i;
6167 open_simple_request(&req, "localhost", port, "HEAD", "/test_large_content");
6169 for (i = 0; i < ARRAY_SIZE(tests); i++)
6171 content_length = tests[i].content_length;
6172 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
6173 ok(ret, "HttpSendRequest failed: %lu\n", GetLastError());
6175 len = ~0;
6176 sizelen = sizeof(len);
6177 SetLastError(0xdeadbeef);
6178 ret = HttpQueryInfoA(req.request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
6179 &len, &sizelen, 0);
6180 if (tests[i].ret)
6182 ok(ret, "HttpQueryInfo should have succeeded\n");
6183 ok(GetLastError() == ERROR_SUCCESS ||
6184 broken(GetLastError() == 0xdeadbeef), /* xp, 2k8, vista */
6185 "expected ERROR_SUCCESS, got %lx\n", GetLastError());
6186 ok(len == (DWORD)tests[i].content_length, "expected %lu, got %lu\n",
6187 (DWORD)tests[i].content_length, len);
6189 else
6191 ok(!ret, "HttpQueryInfo should have failed\n");
6192 ok(GetLastError() == ERROR_HTTP_INVALID_HEADER,
6193 "expected ERROR_HTTP_INVALID_HEADER, got %lx\n", GetLastError());
6194 ok(len == ~0, "expected ~0, got %lu\n", len);
6196 ok(sizelen == sizeof(DWORD), "sizelen %lu\n", sizelen);
6199 /* test argument size */
6200 len64 = ~0;
6201 sizelen = sizeof(len64);
6202 SetLastError(0xdeadbeef);
6203 ret = HttpQueryInfoA(req.request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
6204 &len64, &len, 0);
6205 ok(!ret, "HttpQueryInfo should have failed\n");
6206 ok(GetLastError() == ERROR_HTTP_INVALID_HEADER,
6207 "expected ERROR_HTTP_INVALID_HEADER, got %lx\n", GetLastError());
6208 ok(sizelen == sizeof(DWORD64), "sizelen %lu\n", sizelen);
6209 ok(len64 == ~0, "len64 %I64x\n", len64);
6211 close_request(&req);
6213 /* test internal use of HttpQueryInfo on large size */
6214 open_read_test_request(port, &req,
6215 "HTTP/1.1 200 OK\r\n"
6216 "Server: winetest\r\n"
6217 "Content-Length: 4294967296\r\n"
6218 "\r\n"
6219 "xx");
6220 read_expect_async(req.request, buf, 4, &read_size, "xx");
6221 send_response_and_wait("yy1234567890", FALSE, buf, &read_size, "xxyy", 4, 0, 2);
6222 read_expect_sync_data(req.request, buf, 10, "1234567890");
6224 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
6225 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
6226 close_async_handle(req.session, 2);
6227 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
6228 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
6229 close_connection();
6232 static void test_http_connection(void)
6234 struct server_info si;
6235 HANDLE hThread;
6236 DWORD id = 0, r;
6238 si.hEvent = CreateEventW(NULL, 0, 0, NULL);
6239 si.port = 7531;
6241 hThread = CreateThread(NULL, 0, server_thread, &si, 0, &id);
6242 ok( hThread != NULL, "create thread failed\n");
6244 r = WaitForSingleObject(si.hEvent, 10000);
6245 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
6246 if (r != WAIT_OBJECT_0)
6248 CloseHandle(hThread);
6249 return;
6252 test_basic_request(si.port, "GET", "/test1");
6253 test_proxy_indirect(si.port);
6254 test_proxy_direct(si.port);
6255 test_header_handling_order(si.port);
6256 test_basic_request(si.port, "POST", "/test5");
6257 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
6258 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
6259 test_basic_request(si.port, "GET", "/test6");
6260 test_basic_request(si.port, "GET", "/testF");
6261 test_connection_header(si.port);
6262 test_header_override(si.port);
6263 test_cookie_header(si.port);
6264 test_basic_authentication(si.port);
6265 test_invalid_response_headers(si.port);
6266 test_response_without_headers(si.port);
6267 test_HttpQueryInfo(si.port);
6268 test_HttpSendRequestW(si.port);
6269 test_options(si.port);
6270 test_no_content(si.port);
6271 test_not_modified(si.port);
6272 test_large_header(si.port);
6273 test_conn_close(si.port);
6274 test_no_cache(si.port);
6275 test_cache_read_gzipped(si.port);
6276 test_http_status(si.port);
6277 test_premature_disconnect(si.port);
6278 test_connection_closing(si.port);
6279 test_cache_control_verb(si.port);
6280 test_successive_HttpSendRequest(si.port);
6281 test_head_request(si.port);
6282 test_request_content_length(si.port);
6283 test_accept_encoding(si.port);
6284 test_basic_auth_credentials_reuse(si.port);
6285 test_basic_auth_credentials_end_session(si.port);
6286 test_basic_auth_credentials_different(si.port);
6287 test_basic_auth_credentials_manual(si.port);
6288 test_basic_auth_credentials_cached_manual(si.port);
6289 test_async_read(si.port);
6290 test_http_read(si.port);
6291 test_connection_break(si.port);
6292 test_long_url(si.port);
6293 test_redirect(si.port);
6294 test_persistent_connection(si.port);
6295 test_remove_dot_segments(si.port);
6296 test_large_content(si.port);
6298 /* send the basic request again to shutdown the server thread */
6299 test_basic_request(si.port, "GET", "/quit");
6301 r = WaitForSingleObject(hThread, 3000);
6302 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
6303 CloseHandle(hThread);
6306 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
6308 LocalFree(info->lpszSubjectInfo);
6309 LocalFree(info->lpszIssuerInfo);
6310 LocalFree(info->lpszProtocolName);
6311 LocalFree(info->lpszSignatureAlgName);
6312 LocalFree(info->lpszEncryptionAlgName);
6315 typedef struct {
6316 const char *ex_subject;
6317 const char *ex_issuer;
6318 } cert_struct_test_t;
6320 static const cert_struct_test_t test_winehq_org_cert = {
6321 "US\r\n"
6322 "Minnesota\r\n"
6323 "Saint Paul\r\n"
6324 "\"CodeWeavers, Inc.\"\r\n"
6325 "IT\r\n"
6326 "*.winehq.org",
6328 "US\r\n"
6329 "VA\r\n"
6330 "Herndon\r\n"
6331 "Network Solutions L.L.C.\r\n"
6332 "Network Solutions OV Server CA 2"
6335 static const cert_struct_test_t test_winehq_com_cert = {
6336 "US\r\n"
6337 "Minnesota\r\n"
6338 "Saint Paul\r\n"
6339 "WineHQ\r\n"
6340 "IT\r\n"
6341 "test.winehq.com\r\n"
6342 "webmaster@winehq.org",
6344 "US\r\n"
6345 "Minnesota\r\n"
6346 "Saint Paul\r\n"
6347 "WineHQ\r\n"
6348 "IT\r\n"
6349 "test.winehq.com\r\n"
6350 "webmaster@winehq.org"
6353 static const char *cert_string_fmt =
6354 "Subject:\r\n%s\r\n"
6355 "Issuer:\r\n%s\r\n"
6356 "Effective Date:\t%s %s\r\n"
6357 "Expiration Date:\t%s %s\r\n"
6358 "Security Protocol:\t%s\r\n"
6359 "Signature Type:\t%s\r\n"
6360 "Encryption Type:\t%s\r\n"
6361 "Privacy Strength:\t%s (%u bits)";
6363 static void test_cert_struct_string(HINTERNET req, const INTERNET_CERTIFICATE_INFOA *info)
6365 SYSTEMTIME start, expiry;
6366 char expiry_date[32];
6367 char expiry_time[32];
6368 char start_date[32];
6369 char start_time[32];
6370 char expect[512];
6371 char actual[512];
6372 DWORD size;
6373 BOOL res;
6375 size = sizeof(actual);
6376 SetLastError(0xdeadbeef);
6377 memset(actual, 0x55, sizeof(actual));
6378 res = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE, actual, &size);
6379 ok(res, "InternetQueryOption failed: %lu\n", GetLastError());
6381 FileTimeToSystemTime(&info->ftStart, &start);
6382 FileTimeToSystemTime(&info->ftExpiry, &expiry);
6384 GetDateFormatA(LOCALE_USER_DEFAULT, 0, &start, NULL, start_date, sizeof(start_date));
6385 GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &start, NULL, start_time, sizeof(start_time));
6386 GetDateFormatA(LOCALE_USER_DEFAULT, 0, &expiry, NULL, expiry_date, sizeof(expiry_date));
6387 GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &expiry, NULL, expiry_time, sizeof(expiry_time));
6389 snprintf(expect, sizeof(expect), cert_string_fmt, info->lpszSubjectInfo, info->lpszIssuerInfo,
6390 start_date, start_time, expiry_date, expiry_time,
6391 info->lpszSignatureAlgName, info->lpszEncryptionAlgName, info->lpszProtocolName,
6392 info->dwKeySize >= 128 ? "High" : "Low", info->dwKeySize);
6393 ok(size == strlen(actual), "size = %lu\n", size);
6394 ok(!strcmp(actual, expect), "expected:\n%s\nactual:\n%s\n", expect, actual);
6396 --size;
6397 SetLastError(0xdeadbeef);
6398 memset(actual, 0x55, sizeof(actual));
6399 res = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE, actual, &size);
6400 ok(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %ld\n", GetLastError());
6401 ok(size == 1, "unexpected size: %lu\n", size);
6402 ok(actual[0] == 0x55, "unexpected byte: %02x\n", actual[0]);
6405 static void test_cert_struct(HINTERNET req, const cert_struct_test_t *test)
6407 INTERNET_CERTIFICATE_INFOA info;
6408 DWORD size;
6409 BOOL res;
6411 memset(&info, 0x5, sizeof(info));
6413 size = sizeof(info);
6414 res = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, &info, &size);
6415 if (!res)
6417 win_skip("Querying INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT failed, skipping tests\n");
6418 return;
6421 ok(res, "InternetQueryOption failed: %lu\n", GetLastError());
6422 ok(size == sizeof(info), "size = %lu\n", size);
6424 ok(!strcmp(info.lpszSubjectInfo, test->ex_subject), "lpszSubjectInfo = %s\n", info.lpszSubjectInfo);
6425 ok(!strcmp(info.lpszIssuerInfo, test->ex_issuer), "lpszIssuerInfo = %s\n", info.lpszIssuerInfo);
6426 ok(!info.lpszSignatureAlgName, "lpszSignatureAlgName = %s\n", info.lpszSignatureAlgName);
6427 ok(!info.lpszEncryptionAlgName, "lpszEncryptionAlgName = %s\n", info.lpszEncryptionAlgName);
6428 ok(!info.lpszProtocolName, "lpszProtocolName = %s\n", info.lpszProtocolName);
6429 ok(info.dwKeySize >= 128 && info.dwKeySize <= 256, "dwKeySize = %lu\n", info.dwKeySize);
6431 if (is_lang_english())
6432 test_cert_struct_string(req, &info);
6433 else
6434 skip("Skipping tests that are English-only\n");
6435 release_cert_info(&info);
6438 #define test_security_info(a,b,c) _test_security_info(__LINE__,a,b,c)
6439 static void _test_security_info(unsigned line, const char *urlc, DWORD error, DWORD ex_flags)
6441 char url[INTERNET_MAX_URL_LENGTH];
6442 const CERT_CHAIN_CONTEXT *chain;
6443 DWORD flags;
6444 BOOL res;
6446 if(!pInternetGetSecurityInfoByURLA) {
6447 win_skip("pInternetGetSecurityInfoByURLA not available\n");
6448 return;
6451 strcpy(url, urlc);
6452 chain = (void*)0xdeadbeef;
6453 flags = 0xdeadbeef;
6454 res = pInternetGetSecurityInfoByURLA(url, &chain, &flags);
6455 if(error == ERROR_SUCCESS) {
6456 ok_(__FILE__,line)(res, "InternetGetSecurityInfoByURLA failed: %lu\n", GetLastError());
6457 ok_(__FILE__,line)(chain != NULL, "chain = NULL\n");
6458 ok_(__FILE__,line)(flags == ex_flags, "flags = %lx\n", flags);
6459 CertFreeCertificateChain(chain);
6461 SetLastError(0xdeadbeef);
6462 res = pInternetGetSecurityInfoByURLA(url, NULL, NULL);
6463 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INVALID_PARAMETER,
6464 "InternetGetSecurityInfoByURLA returned: %x(%lu)\n", res, GetLastError());
6466 res = pInternetGetSecurityInfoByURLA(url, &chain, NULL);
6467 ok_(__FILE__,line)(res, "InternetGetSecurityInfoByURLA failed: %lu\n", GetLastError());
6468 CertFreeCertificateChain(chain);
6470 res = pInternetGetSecurityInfoByURLA(url, NULL, &flags);
6471 ok_(__FILE__,line)(res, "InternetGetSecurityInfoByURLA failed: %lu\n", GetLastError());
6472 }else {
6473 ok_(__FILE__,line)(!res && GetLastError() == error,
6474 "InternetGetSecurityInfoByURLA returned: %x(%lu), expected %lu\n", res, GetLastError(), error);
6478 #define test_secflags_option(a,b,c) _test_secflags_option(__LINE__,a,b,c)
6479 static void _test_secflags_option(unsigned line, HINTERNET req, DWORD ex_flags, DWORD opt_flags)
6481 DWORD flags, size;
6482 BOOL res;
6484 flags = 0xdeadbeef;
6485 size = sizeof(flags);
6486 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
6487 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_SECURITY_FLAGS) failed: %lu\n", GetLastError());
6488 ok_(__FILE__,line)((flags & ~opt_flags) == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS flags = %lx, expected %lx\n",
6489 flags, ex_flags);
6491 /* Option 98 is undocumented and seems to be the same as INTERNET_OPTION_SECURITY_FLAGS */
6492 flags = 0xdeadbeef;
6493 size = sizeof(flags);
6494 res = InternetQueryOptionW(req, 98, &flags, &size);
6495 ok_(__FILE__,line)(res, "InternetQueryOptionW(98) failed: %lu\n", GetLastError());
6496 ok_(__FILE__,line)((flags & ~opt_flags) == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS(98) flags = %lx, expected %lx\n",
6497 flags, ex_flags);
6500 #define set_secflags(a,b,c) _set_secflags(__LINE__,a,b,c)
6501 static void _set_secflags(unsigned line, HINTERNET req, BOOL use_undoc, DWORD flags)
6503 BOOL res;
6505 res = InternetSetOptionW(req, use_undoc ? 99 : INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
6506 ok_(__FILE__,line)(res, "InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed: %lu\n", GetLastError());
6509 static void test_security_flags(void)
6511 INTERNET_CERTIFICATE_INFOA *cert;
6512 HINTERNET ses, conn, req;
6513 DWORD size, flags;
6514 char buf[512];
6515 BOOL res;
6517 if (!https_support)
6519 win_skip("Can't make https connections, skipping security flags test\n");
6520 return;
6523 trace("Testing security flags...\n");
6524 reset_events();
6526 ses = InternetOpenA("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
6527 ok(ses != NULL, "InternetOpen failed\n");
6529 pInternetSetStatusCallbackA(ses, &callback);
6531 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
6532 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
6533 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
6534 ok(conn != NULL, "InternetConnect failed with error %lu\n", GetLastError());
6535 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
6537 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
6538 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
6539 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
6540 0xdeadbeef);
6541 ok(req != NULL, "HttpOpenRequest failed\n");
6542 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
6544 flags = 0xdeadbeef;
6545 size = sizeof(flags);
6546 res = InternetQueryOptionW(req, 98, &flags, &size);
6547 if(!res && GetLastError() == ERROR_INVALID_PARAMETER) {
6548 win_skip("Incomplete security flags support, skipping\n");
6550 close_async_handle(ses, 2);
6551 return;
6554 test_secflags_option(req, 0, 0);
6555 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
6557 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_REVOCATION);
6558 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION, 0);
6560 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
6561 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID, 0);
6563 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
6564 test_secflags_option(req, SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID, 0);
6566 flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_SECURE;
6567 res = InternetSetOptionW(req, 99, &flags, sizeof(flags));
6568 ok(!res && GetLastError() == ERROR_INTERNET_OPTION_NOT_SETTABLE, "InternetSetOption(99) failed: %lu\n", GetLastError());
6570 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
6571 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
6572 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
6573 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
6574 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
6575 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
6576 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
6577 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
6578 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
6579 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
6580 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
6581 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
6582 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
6583 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
6584 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
6586 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
6587 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %lu\n", GetLastError());
6589 WaitForSingleObject(complete_event, INFINITE);
6590 ok(req_error == ERROR_SUCCESS, "req_error = %ld\n", req_error);
6592 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
6593 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
6594 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
6595 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
6596 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
6597 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
6598 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
6599 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
6600 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
6601 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
6602 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
6603 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
6604 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
6606 test_request_flags(req, 0);
6607 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA
6608 |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_STRENGTH_STRONG, 0);
6610 res = InternetReadFile(req, buf, sizeof(buf), &size);
6611 ok(res, "InternetReadFile failed: %lu\n", GetLastError());
6612 ok(size, "size = 0\n");
6614 /* Collect all existing persistent connections */
6615 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
6616 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %lu\n", GetLastError());
6618 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
6619 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
6620 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
6621 0xdeadbeef);
6622 ok(req != NULL, "HttpOpenRequest failed\n");
6623 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
6625 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT|INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY;
6626 res = InternetSetOptionA(req, INTERNET_OPTION_ERROR_MASK, (void*)&flags, sizeof(flags));
6627 ok(res, "InternetQueryOption(INTERNET_OPTION_ERROR_MASK failed: %lu\n", GetLastError());
6629 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
6630 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
6631 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
6632 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
6633 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
6634 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
6635 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
6636 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
6637 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
6638 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
6639 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
6641 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
6642 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %lu\n", GetLastError());
6644 WaitForSingleObject(complete_event, INFINITE);
6645 todo_wine
6646 ok(req_error == ERROR_INTERNET_SEC_CERT_ERRORS,
6647 "req_error = %ld\n", req_error);
6649 size = 0;
6650 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
6651 ok(res || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %ld\n", GetLastError());
6652 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %lu\n", size);
6653 cert = HeapAlloc(GetProcessHeap(), 0, size);
6654 cert->lpszSubjectInfo = NULL;
6655 cert->lpszIssuerInfo = NULL;
6656 cert->lpszSignatureAlgName = (char *)0xdeadbeef;
6657 cert->lpszEncryptionAlgName = (char *)0xdeadbeef;
6658 cert->lpszProtocolName = (char *)0xdeadbeef;
6659 cert->dwKeySize = 0xdeadbeef;
6660 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, cert, &size);
6661 ok(res, "InternetQueryOption failed: %lu\n", GetLastError());
6662 if (res)
6664 ok(cert->lpszSubjectInfo && strlen(cert->lpszSubjectInfo) > 1, "expected a non-empty subject name\n");
6665 ok(cert->lpszIssuerInfo && strlen(cert->lpszIssuerInfo) > 1, "expected a non-empty issuer name\n");
6666 ok(!cert->lpszSignatureAlgName, "unexpected signature algorithm name\n");
6667 ok(!cert->lpszEncryptionAlgName, "unexpected encryption algorithm name\n");
6668 ok(!cert->lpszProtocolName, "unexpected protocol name\n");
6669 ok(cert->dwKeySize != 0xdeadbeef, "unexpected key size\n");
6671 LocalFree(cert->lpszSubjectInfo);
6672 LocalFree(cert->lpszIssuerInfo);
6674 HeapFree(GetProcessHeap(), 0, cert);
6676 SetLastError(0xdeadbeef);
6677 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE, NULL, NULL);
6678 ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "InternetQueryOption failed: %ld\n", GetLastError());
6680 size = 0;
6681 SetLastError(0xdeadbeef);
6682 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE, NULL, &size);
6683 ok(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %ld\n", GetLastError());
6684 ok(size == 1, "unexpected size: %lu\n", size);
6686 size = 42;
6687 SetLastError(0xdeadbeef);
6688 memset(buf, 0x55, sizeof(buf));
6689 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE, buf, &size);
6690 ok(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %ld\n", GetLastError());
6691 ok(size == 1, "unexpected size: %lu\n", size);
6692 ok(buf[0] == 0x55, "unexpected byte: %02x\n", buf[0]);
6694 size = sizeof(buf);
6695 SetLastError(0xdeadbeef);
6696 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE, buf, &size);
6697 ok(res && GetLastError() == ERROR_SUCCESS, "InternetQueryOption failed: %ld\n", GetLastError());
6698 ok(size < sizeof(buf), "unexpected size: %lu\n", size);
6700 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
6701 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
6702 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
6703 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
6704 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
6705 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
6706 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
6708 if(req_error != ERROR_INTERNET_SEC_CERT_ERRORS) {
6709 skip("Unexpected cert errors %lu, skipping security flags tests\n", req_error);
6711 close_async_handle(ses, 3);
6712 return;
6715 size = sizeof(buf);
6716 res = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
6717 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfoA(HTTP_QUERY_CONTENT_ENCODING) failed: %lu\n", GetLastError());
6719 test_request_flags(req, 8);
6720 /* IE11 finds both rev failure and invalid CA. Previous versions required rev failure
6721 to be ignored before invalid CA was reported. */
6722 test_secflags_option(req, _SECURITY_FLAG_CERT_INVALID_CA, _SECURITY_FLAG_CERT_REV_FAILED);
6724 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_REVOCATION);
6725 test_secflags_option(req, _SECURITY_FLAG_CERT_INVALID_CA|SECURITY_FLAG_IGNORE_REVOCATION, _SECURITY_FLAG_CERT_REV_FAILED);
6727 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
6728 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
6729 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
6730 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
6731 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
6732 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
6733 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
6735 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
6736 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %lu\n", GetLastError());
6738 WaitForSingleObject(complete_event, INFINITE);
6739 ok(req_error == ERROR_INTERNET_INVALID_CA || req_error == ERROR_INTERNET_SEC_CERT_ERRORS, "req_error = %ld\n", req_error);
6741 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
6742 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
6743 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
6744 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
6745 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
6746 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
6747 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
6749 test_request_flags(req, INTERNET_REQFLAG_NO_HEADERS);
6750 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_INVALID_CA, 0);
6751 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
6753 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
6754 test_secflags_option(req, _SECURITY_FLAG_CERT_INVALID_CA
6755 |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_UNKNOWN_CA, 0);
6756 test_http_version(req);
6758 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
6759 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
6760 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
6761 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
6762 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
6763 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
6764 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
6765 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
6766 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
6767 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
6768 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
6769 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
6770 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
6772 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
6773 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %lu\n", GetLastError());
6775 WaitForSingleObject(complete_event, INFINITE);
6776 ok(req_error == ERROR_SUCCESS, "req_error = %ld\n", req_error);
6778 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
6779 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
6780 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
6781 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
6782 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
6783 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
6784 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
6785 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
6786 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
6787 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
6788 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
6790 test_request_flags(req, 0);
6791 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION
6792 |SECURITY_FLAG_STRENGTH_STRONG|_SECURITY_FLAG_CERT_INVALID_CA, 0);
6794 test_cert_struct(req, &test_winehq_com_cert);
6795 test_security_info("https://test.winehq.com/data/some_file.html?q", 0,
6796 _SECURITY_FLAG_CERT_INVALID_CA);
6798 res = InternetReadFile(req, buf, sizeof(buf), &size);
6799 ok(res, "InternetReadFile failed: %lu\n", GetLastError());
6800 ok(size, "size = 0\n");
6802 close_async_handle(ses, 3);
6804 /* Collect all existing persistent connections */
6805 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
6806 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %lu\n", GetLastError());
6808 /* Make another request, without setting security flags */
6810 ses = InternetOpenA("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
6811 ok(ses != NULL, "InternetOpen failed\n");
6813 pInternetSetStatusCallbackA(ses, &callback);
6815 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
6816 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
6817 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
6818 ok(conn != NULL, "InternetConnect failed with error %lu\n", GetLastError());
6819 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
6821 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
6822 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
6823 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
6824 0xdeadbeef);
6825 ok(req != NULL, "HttpOpenRequest failed\n");
6826 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
6828 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
6829 |SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_INVALID_CA, 0);
6830 test_http_version(req);
6832 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
6833 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
6834 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
6835 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
6836 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
6837 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
6838 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
6839 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
6840 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
6841 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
6842 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
6843 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
6845 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
6846 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %lu\n", GetLastError());
6848 WaitForSingleObject(complete_event, INFINITE);
6849 ok(req_error == ERROR_SUCCESS, "req_error = %ld\n", req_error);
6851 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
6852 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
6853 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
6854 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
6855 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
6856 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
6857 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
6858 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
6859 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
6860 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
6862 test_request_flags(req, 0);
6863 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
6864 |SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_INVALID_CA, 0);
6866 res = InternetReadFile(req, buf, sizeof(buf), &size);
6867 ok(res, "InternetReadFile failed: %lu\n", GetLastError());
6868 ok(size, "size = 0\n");
6870 close_async_handle(ses, 2);
6872 test_security_info("http://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
6873 test_security_info("file:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
6874 test_security_info("xxx:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
6877 static void test_secure_connection(void)
6879 static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
6880 static const WCHAR testsite[] = {'t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',0};
6881 static const WCHAR get[] = {'G','E','T',0};
6882 static const WCHAR testpage[] = {'/','t','e','s','t','s','/','h','e','l','l','o','.','h','t','m','l',0};
6883 HINTERNET ses, con, req;
6884 DWORD size, size2, flags, err;
6885 INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
6886 INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
6887 char certstr1[512], certstr2[512];
6888 BOOL ret;
6889 PCCERT_CHAIN_CONTEXT chain;
6891 ses = InternetOpenA("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
6892 ok(ses != NULL, "InternetOpen failed\n");
6894 con = InternetConnectA(ses, "test.winehq.org",
6895 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
6896 INTERNET_SERVICE_HTTP, 0, 0);
6897 ok(con != NULL, "InternetConnect failed\n");
6899 req = HttpOpenRequestA(con, "GET", "/tests/hello.html", NULL, NULL, NULL,
6900 INTERNET_FLAG_SECURE, 0);
6901 ok(req != NULL, "HttpOpenRequest failed\n");
6903 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
6904 err = GetLastError();
6905 ok(ret || broken(err == ERROR_INTERNET_CANNOT_CONNECT) ||
6906 broken(err == ERROR_INTERNET_SECURITY_CHANNEL_ERROR), "HttpSendRequest failed: %lu\n", err);
6907 if (!ret)
6909 win_skip("Cannot connect to https.\n");
6910 if (err == ERROR_INTERNET_SECURITY_CHANNEL_ERROR) https_support = FALSE;
6911 goto done;
6914 size = sizeof(flags);
6915 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
6916 ok(ret, "InternetQueryOption failed: %ld\n", GetLastError());
6917 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
6919 test_cert_struct(req, &test_winehq_org_cert);
6921 size = sizeof(chain);
6922 SetLastError(0xdeadbeef);
6923 ret = InternetQueryOptionA(req, INTERNET_OPTION_SERVER_CERT_CHAIN_CONTEXT, &chain, &size);
6924 ok(ret || GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE /* < IE8 */,
6925 "InternetQueryOption failed: %lu\n", GetLastError());
6926 if (ret) CertFreeCertificateChain(chain);
6928 /* Querying the same option through InternetQueryOptionW still results in
6929 * ANSI strings being returned.
6931 size = 0;
6932 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
6933 NULL, &size);
6934 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %ld\n", GetLastError());
6935 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %ld\n", size);
6936 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
6937 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
6938 certificate_structW, &size);
6939 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
6940 ok(ret, "InternetQueryOption failed: %ld\n", GetLastError());
6941 if (ret)
6943 ok(certificate_structA->lpszSubjectInfo &&
6944 strlen(certificate_structA->lpszSubjectInfo) > 1,
6945 "expected a non-empty subject name\n");
6946 ok(certificate_structA->lpszIssuerInfo &&
6947 strlen(certificate_structA->lpszIssuerInfo) > 1,
6948 "expected a non-empty issuer name\n");
6949 ok(!certificate_structA->lpszSignatureAlgName,
6950 "unexpected signature algorithm name\n");
6951 ok(!certificate_structA->lpszEncryptionAlgName,
6952 "unexpected encryption algorithm name\n");
6953 ok(!certificate_structA->lpszProtocolName,
6954 "unexpected protocol name\n");
6955 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
6956 release_cert_info(certificate_structA);
6958 HeapFree(GetProcessHeap(), 0, certificate_structW);
6960 SetLastError(0xdeadbeef);
6961 size = sizeof(certstr1);
6962 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE, certstr1, &size);
6963 ok(ret && GetLastError() == ERROR_SUCCESS, "InternetQueryOption failed: %ld\n", GetLastError());
6965 SetLastError(0xdeadbeef);
6966 size2 = sizeof(certstr2);
6967 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE, certstr2, &size2);
6968 ok(ret && GetLastError() == ERROR_SUCCESS, "InternetQueryOption failed: %ld\n", GetLastError());
6970 ok(size == size2, "expected same size\n");
6971 ok(!strcmp(certstr1, certstr2), "expected same string\n");
6973 InternetCloseHandle(req);
6974 InternetCloseHandle(con);
6975 InternetCloseHandle(ses);
6977 /* Repeating the tests with the W functions has the same result: */
6978 ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
6979 ok(ses != NULL, "InternetOpen failed\n");
6981 con = InternetConnectW(ses, testsite,
6982 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
6983 INTERNET_SERVICE_HTTP, 0, 0);
6984 ok(con != NULL, "InternetConnect failed\n");
6986 req = HttpOpenRequestW(con, get, testpage, NULL, NULL, NULL,
6987 INTERNET_FLAG_SECURE|INTERNET_FLAG_RELOAD, 0);
6988 ok(req != NULL, "HttpOpenRequest failed\n");
6990 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
6991 ok(ret, "HttpSendRequest failed: %ld\n", GetLastError());
6993 size = sizeof(flags);
6994 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
6995 ok(ret, "InternetQueryOption failed: %ld\n", GetLastError());
6996 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set, got %lx\n", flags);
6998 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
6999 NULL, &size);
7000 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %ld\n", GetLastError());
7001 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %ld\n", size);
7002 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
7003 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
7004 certificate_structA, &size);
7005 ok(ret, "InternetQueryOption failed: %ld\n", GetLastError());
7006 if (ret)
7008 ok(certificate_structA->lpszSubjectInfo &&
7009 strlen(certificate_structA->lpszSubjectInfo) > 1,
7010 "expected a non-empty subject name\n");
7011 ok(certificate_structA->lpszIssuerInfo &&
7012 strlen(certificate_structA->lpszIssuerInfo) > 1,
7013 "expected a non-empty issuer name\n");
7014 ok(!certificate_structA->lpszSignatureAlgName,
7015 "unexpected signature algorithm name\n");
7016 ok(!certificate_structA->lpszEncryptionAlgName,
7017 "unexpected encryption algorithm name\n");
7018 ok(!certificate_structA->lpszProtocolName,
7019 "unexpected protocol name\n");
7020 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
7021 release_cert_info(certificate_structA);
7023 HeapFree(GetProcessHeap(), 0, certificate_structA);
7025 /* Again, querying the same option through InternetQueryOptionW still
7026 * results in ASCII strings being returned.
7028 size = 0;
7029 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
7030 NULL, &size);
7031 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %ld\n", GetLastError());
7032 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %ld\n", size);
7033 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
7034 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
7035 certificate_structW, &size);
7036 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
7037 ok(ret, "InternetQueryOption failed: %ld\n", GetLastError());
7038 if (ret)
7040 ok(certificate_structA->lpszSubjectInfo &&
7041 strlen(certificate_structA->lpszSubjectInfo) > 1,
7042 "expected a non-empty subject name\n");
7043 ok(certificate_structA->lpszIssuerInfo &&
7044 strlen(certificate_structA->lpszIssuerInfo) > 1,
7045 "expected a non-empty issuer name\n");
7046 ok(!certificate_structA->lpszSignatureAlgName,
7047 "unexpected signature algorithm name\n");
7048 ok(!certificate_structA->lpszEncryptionAlgName,
7049 "unexpected encryption algorithm name\n");
7050 ok(!certificate_structA->lpszProtocolName,
7051 "unexpected protocol name\n");
7052 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
7053 release_cert_info(certificate_structA);
7055 HeapFree(GetProcessHeap(), 0, certificate_structW);
7057 done:
7058 InternetCloseHandle(req);
7059 InternetCloseHandle(con);
7060 InternetCloseHandle(ses);
7063 static void test_user_agent_header(void)
7065 HINTERNET ses, con, req;
7066 DWORD size, err;
7067 char buffer[64];
7068 BOOL ret;
7070 ses = InternetOpenA("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
7071 ok(ses != NULL, "InternetOpen failed\n");
7073 con = InternetConnectA(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
7074 ok(con != NULL, "InternetConnect failed\n");
7076 req = HttpOpenRequestA(con, "GET", "/tests/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
7077 ok(req != NULL, "HttpOpenRequest failed\n");
7079 size = sizeof(buffer);
7080 ret = HttpQueryInfoA(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
7081 err = GetLastError();
7082 ok(!ret, "HttpQueryInfo succeeded\n");
7083 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %lu\n", err);
7085 ret = HttpAddRequestHeadersA(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
7086 ok(ret, "HttpAddRequestHeaders succeeded\n");
7088 size = sizeof(buffer);
7089 ret = HttpQueryInfoA(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
7090 err = GetLastError();
7091 ok(ret, "HttpQueryInfo failed\n");
7093 InternetCloseHandle(req);
7095 req = HttpOpenRequestA(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
7096 ok(req != NULL, "HttpOpenRequest failed\n");
7098 size = sizeof(buffer);
7099 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
7100 err = GetLastError();
7101 ok(!ret, "HttpQueryInfo succeeded\n");
7102 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %lu\n", err);
7104 ret = HttpAddRequestHeadersA(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
7105 ok(ret, "HttpAddRequestHeaders failed\n");
7107 buffer[0] = 0;
7108 size = sizeof(buffer);
7109 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
7110 ok(ret, "HttpQueryInfo failed: %lu\n", GetLastError());
7111 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
7113 InternetCloseHandle(req);
7114 InternetCloseHandle(con);
7115 InternetCloseHandle(ses);
7118 static void test_bogus_accept_types_array(void)
7120 HINTERNET ses, con, req;
7121 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
7122 DWORD size, error;
7123 char buffer[32];
7124 BOOL ret;
7126 ses = InternetOpenA("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
7127 con = InternetConnectA(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
7128 req = HttpOpenRequestA(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
7130 ok(req != NULL, "HttpOpenRequest failed: %lu\n", GetLastError());
7132 buffer[0] = 0;
7133 size = sizeof(buffer);
7134 SetLastError(0xdeadbeef);
7135 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
7136 error = GetLastError();
7137 ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
7138 if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %lu\n", error);
7139 ok(broken(!strcmp(buffer, ", */*, %p, , , */*")) /* IE6 */ ||
7140 broken(!strcmp(buffer, "*/*, %p, */*")) /* IE7/8 */ ||
7141 !strcmp(buffer, ""), "got '%s' expected ''\n", buffer);
7143 InternetCloseHandle(req);
7144 InternetCloseHandle(con);
7145 InternetCloseHandle(ses);
7148 struct context
7150 HANDLE event;
7151 HINTERNET req;
7154 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
7156 INTERNET_ASYNC_RESULT *result = info;
7157 struct context *ctx = (struct context *)context;
7159 if(winetest_debug > 1)
7160 trace("%p 0x%08Ix %lu %p 0x%08lx\n", handle, context, status, info, size);
7162 switch(status) {
7163 case INTERNET_STATUS_REQUEST_COMPLETE:
7164 trace("request handle: 0x%08Ix\n", result->dwResult);
7165 ctx->req = (HINTERNET)result->dwResult;
7166 SetEvent(ctx->event);
7167 break;
7168 case INTERNET_STATUS_HANDLE_CLOSING: {
7169 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
7171 if (InternetQueryOptionA(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
7172 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
7173 SetEvent(ctx->event);
7174 break;
7176 case INTERNET_STATUS_NAME_RESOLVED:
7177 case INTERNET_STATUS_CONNECTING_TO_SERVER:
7178 case INTERNET_STATUS_CONNECTED_TO_SERVER: {
7179 char *str = info;
7180 ok(str[0] && str[1], "Got string: %s\n", str);
7181 ok(size == strlen(str)+1, "unexpected size %lu\n", size);
7186 static void test_open_url_async(void)
7188 BOOL ret;
7189 HINTERNET ses, req;
7190 DWORD size, error;
7191 struct context ctx;
7192 ULONG type;
7194 /* Collect all existing persistent connections */
7195 ret = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
7196 ok(ret, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %lu\n", GetLastError());
7199 * Some versions of IE6 fail those tests. They pass some notification data as UNICODE string, while
7200 * other versions never do. They also hang of following tests. We disable it for everything older
7201 * than IE7.
7203 if(!is_ie7plus)
7204 return;
7206 ctx.req = NULL;
7207 ctx.event = CreateEventA(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
7209 ses = InternetOpenA("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
7210 ok(ses != NULL, "InternetOpen failed\n");
7212 SetLastError(0xdeadbeef);
7213 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
7214 error = GetLastError();
7215 ok(!ret, "InternetSetOptionA succeeded\n");
7216 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %lu expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
7218 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
7219 error = GetLastError();
7220 ok(!ret, "InternetSetOptionA failed\n");
7221 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %lu expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
7223 pInternetSetStatusCallbackW(ses, cb);
7224 ResetEvent(ctx.event);
7226 req = InternetOpenUrlA(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
7227 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
7229 WaitForSingleObject(ctx.event, INFINITE);
7231 type = 0;
7232 size = sizeof(type);
7233 ret = InternetQueryOptionA(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
7234 ok(ret, "InternetQueryOption failed: %lu\n", GetLastError());
7235 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
7236 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %lu\n", type);
7238 size = 0;
7239 ret = HttpQueryInfoA(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
7240 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
7241 ok(size > 0, "expected size > 0\n");
7243 ResetEvent(ctx.event);
7244 InternetCloseHandle(ctx.req);
7245 WaitForSingleObject(ctx.event, INFINITE);
7247 InternetCloseHandle(ses);
7248 CloseHandle(ctx.event);
7251 enum api
7253 internet_connect = 1,
7254 http_open_request,
7255 http_send_request_ex,
7256 internet_writefile,
7257 http_end_request,
7258 internet_close_handle
7261 struct notification
7263 enum api function; /* api responsible for notification */
7264 unsigned int status; /* status received */
7265 BOOL async; /* delivered from another thread? */
7266 BOOL todo;
7267 BOOL optional;
7270 struct info
7272 enum api function;
7273 const struct notification *test;
7274 unsigned int count;
7275 unsigned int index;
7276 HANDLE wait;
7277 DWORD thread;
7278 unsigned int line;
7279 DWORD expect_result;
7280 BOOL is_aborted;
7283 static CRITICAL_SECTION notification_cs;
7285 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
7287 BOOL status_ok, function_ok;
7288 struct info *info = (struct info *)context;
7289 unsigned int i;
7291 EnterCriticalSection( &notification_cs );
7293 if(info->is_aborted) {
7294 LeaveCriticalSection(&notification_cs);
7295 return;
7298 if (status == INTERNET_STATUS_HANDLE_CREATED)
7300 DWORD size = sizeof(struct info *);
7301 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
7302 }else if(status == INTERNET_STATUS_REQUEST_COMPLETE) {
7303 INTERNET_ASYNC_RESULT *ar = (INTERNET_ASYNC_RESULT*)buffer;
7305 ok(buflen == sizeof(*ar), "unexpected buflen = %ld\n", buflen);
7306 if(info->expect_result == ERROR_SUCCESS) {
7307 ok(ar->dwResult == 1, "ar->dwResult = %Id, expected 1\n", ar->dwResult);
7308 }else {
7309 ok(!ar->dwResult, "ar->dwResult = %Id, expected 1\n", ar->dwResult);
7310 ok(ar->dwError == info->expect_result, "ar->dwError = %ld, expected %ld\n", ar->dwError, info->expect_result);
7314 i = info->index;
7315 if (i >= info->count)
7317 LeaveCriticalSection( &notification_cs );
7318 return;
7321 while (info->test[i].status != status &&
7322 (info->test[i].optional || info->test[i].todo) &&
7323 i < info->count - 1 &&
7324 info->test[i].function == info->test[i + 1].function)
7326 i++;
7329 status_ok = (info->test[i].status == status);
7330 function_ok = (info->test[i].function == info->function);
7332 if (!info->test[i].todo)
7334 ok( status_ok, "%u: expected status %u got %lu\n", info->line, info->test[i].status, status );
7335 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
7337 if (info->test[i].async)
7338 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %lu got %lu\n",
7339 info->line, info->thread, GetCurrentThreadId());
7341 else
7343 todo_wine ok( status_ok, "%u: expected status %u got %lu\n", info->line, info->test[i].status, status );
7344 if (status_ok)
7345 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
7347 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
7348 info->index = i+1;
7350 LeaveCriticalSection( &notification_cs );
7353 static void setup_test( struct info *info, enum api function, unsigned int line, DWORD expect_result )
7355 info->function = function;
7356 info->line = line;
7357 info->expect_result = expect_result;
7360 struct notification_data
7362 const struct notification *test;
7363 const unsigned int count;
7364 const char *method;
7365 const char *host;
7366 const char *path;
7367 const char *data;
7368 BOOL expect_conn_failure;
7371 static const struct notification async_send_request_ex_test[] =
7373 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
7374 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
7375 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
7376 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
7377 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
7378 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
7379 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE },
7380 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE },
7381 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
7382 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
7383 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
7384 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, FALSE },
7385 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, FALSE },
7386 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
7387 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
7388 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
7389 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
7390 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
7391 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
7392 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
7395 static const struct notification async_send_request_ex_test2[] =
7397 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
7398 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
7399 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
7400 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
7401 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
7402 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
7403 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE },
7404 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE },
7405 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
7406 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
7407 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
7408 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
7409 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
7410 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
7411 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
7412 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
7413 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
7414 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
7417 static const struct notification async_send_request_ex_resolve_failure_test[] =
7419 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
7420 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
7421 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
7422 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE },
7423 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
7424 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
7425 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
7426 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
7427 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
7428 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
7429 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
7432 static const struct notification async_send_request_ex_chunked_test[] =
7434 { internet_connect, INTERNET_STATUS_HANDLE_CREATED },
7435 { http_open_request, INTERNET_STATUS_HANDLE_CREATED },
7436 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
7437 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
7438 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
7439 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
7440 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE },
7441 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE },
7442 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
7443 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
7444 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
7445 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
7446 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
7447 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
7448 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
7449 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
7450 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING },
7451 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING }
7454 static const struct notification_data notification_data[] = {
7456 async_send_request_ex_chunked_test,
7457 ARRAY_SIZE(async_send_request_ex_chunked_test),
7458 "GET",
7459 "test.winehq.org",
7460 "tests/data.php"
7463 async_send_request_ex_test,
7464 ARRAY_SIZE(async_send_request_ex_test),
7465 "POST",
7466 "test.winehq.org",
7467 "tests/post.php",
7468 "Public ID=codeweavers"
7471 async_send_request_ex_test2,
7472 ARRAY_SIZE(async_send_request_ex_test2),
7473 "POST",
7474 "test.winehq.org",
7475 "tests/post.php"
7478 async_send_request_ex_resolve_failure_test,
7479 ARRAY_SIZE(async_send_request_ex_resolve_failure_test),
7480 "GET",
7481 "brokenhost",
7482 "index.html",
7483 NULL,
7484 TRUE
7488 static void test_async_HttpSendRequestEx(const struct notification_data *nd)
7490 BOOL ret;
7491 HINTERNET ses, req, con;
7492 struct info info;
7493 DWORD size, written, error;
7494 INTERNET_BUFFERSA b;
7495 static const char *accept[2] = {"*/*", NULL};
7496 char buffer[32];
7498 trace("Async HttpSendRequestEx test (%s %s)\n", nd->method, nd->host);
7500 /* Collect all existing persistent connections */
7501 ret = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
7502 ok(ret, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %lu\n", GetLastError());
7504 InitializeCriticalSection( &notification_cs );
7506 info.test = nd->test;
7507 info.count = nd->count;
7508 info.index = 0;
7509 info.wait = CreateEventW( NULL, FALSE, FALSE, NULL );
7510 info.thread = GetCurrentThreadId();
7511 info.is_aborted = FALSE;
7513 ses = InternetOpenA( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
7514 ok( ses != NULL, "InternetOpen failed\n" );
7516 pInternetSetStatusCallbackA( ses, check_notification );
7518 setup_test( &info, internet_connect, __LINE__, ERROR_SUCCESS );
7519 con = InternetConnectA( ses, nd->host, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
7520 ok( con != NULL, "InternetConnect failed %lu\n", GetLastError() );
7522 WaitForSingleObject( info.wait, 10000 );
7524 setup_test( &info, http_open_request, __LINE__, ERROR_SUCCESS );
7525 req = HttpOpenRequestA( con, nd->method, nd->path, NULL, NULL, accept, 0, (DWORD_PTR)&info );
7526 ok( req != NULL, "HttpOpenRequest failed %lu\n", GetLastError() );
7528 WaitForSingleObject( info.wait, 10000 );
7530 if(nd->data) {
7531 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
7532 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
7533 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
7534 b.dwHeadersLength = strlen( b.lpcszHeader );
7535 b.dwBufferTotal = nd->data ? strlen( nd->data ) : 0;
7538 setup_test( &info, http_send_request_ex, __LINE__,
7539 nd->expect_conn_failure ? ERROR_INTERNET_NAME_NOT_RESOLVED : ERROR_SUCCESS );
7540 ret = HttpSendRequestExA( req, nd->data ? &b : NULL, NULL, 0x28, 0 );
7541 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %lu\n", ret, GetLastError() );
7543 error = WaitForSingleObject( info.wait, 10000 );
7544 if(error != WAIT_OBJECT_0) {
7545 skip("WaitForSingleObject returned %ld, assuming DNS problem\n", error);
7546 info.is_aborted = TRUE;
7547 goto abort;
7550 size = sizeof(buffer);
7551 SetLastError( 0xdeadbeef );
7552 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
7553 error = GetLastError();
7554 ok( !ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
7555 if(nd->expect_conn_failure) {
7556 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND got %lu\n", error );
7557 }else {
7558 todo_wine
7559 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
7560 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %lu\n", error );
7563 if (nd->data)
7565 written = 0;
7566 size = strlen( nd->data );
7567 setup_test( &info, internet_writefile, __LINE__, ERROR_SUCCESS );
7568 ret = InternetWriteFile( req, nd->data, size, &written );
7569 ok( ret, "InternetWriteFile failed %lu\n", GetLastError() );
7570 ok( written == size, "expected %lu got %lu\n", written, size );
7572 WaitForSingleObject( info.wait, 10000 );
7574 SetLastError( 0xdeadbeef );
7575 ret = HttpEndRequestA( req, (void *)nd->data, 0x28, 0 );
7576 error = GetLastError();
7577 ok( !ret, "HttpEndRequestA succeeded\n" );
7578 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %lu\n", error );
7581 SetLastError( 0xdeadbeef );
7582 setup_test( &info, http_end_request, __LINE__,
7583 nd->expect_conn_failure ? ERROR_INTERNET_OPERATION_CANCELLED : ERROR_SUCCESS);
7584 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
7585 error = GetLastError();
7586 ok( !ret, "HttpEndRequestA succeeded\n" );
7587 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %lu\n", error );
7589 WaitForSingleObject( info.wait, 10000 );
7591 setup_test( &info, internet_close_handle, __LINE__, ERROR_SUCCESS );
7592 abort:
7593 InternetCloseHandle( req );
7594 InternetCloseHandle( con );
7595 InternetCloseHandle( ses );
7597 WaitForSingleObject( info.wait, 10000 );
7598 Sleep(100);
7599 CloseHandle( info.wait );
7600 DeleteCriticalSection( &notification_cs );
7603 static HINTERNET closetest_session, closetest_req, closetest_conn;
7604 static BOOL closetest_closed;
7606 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
7607 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
7609 DWORD len, type;
7610 BOOL res;
7612 if(winetest_debug > 1)
7613 trace("closetest_callback %p: %ld\n", hInternet, dwInternetStatus);
7615 ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
7616 "Unexpected hInternet %p\n", hInternet);
7617 if(!closetest_closed)
7618 return;
7620 len = sizeof(type);
7621 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
7622 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
7623 "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %lu, expected TRUE ERROR_INVALID_HANDLE\n",
7624 closetest_req, res, GetLastError());
7627 static void test_InternetCloseHandle(void)
7629 DWORD len, flags;
7630 BOOL res;
7632 closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
7633 ok(closetest_session != NULL,"InternetOpen failed with error %lu\n", GetLastError());
7635 pInternetSetStatusCallbackA(closetest_session, closetest_callback);
7637 closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
7638 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
7639 ok(closetest_conn != NULL,"InternetConnect failed with error %lu\n", GetLastError());
7641 closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
7642 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
7644 res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
7645 ok(!res && (GetLastError() == ERROR_IO_PENDING),
7646 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
7648 test_request_flags(closetest_req, INTERNET_REQFLAG_NO_HEADERS);
7650 res = InternetCloseHandle(closetest_session);
7651 ok(res, "InternetCloseHandle failed: %lu\n", GetLastError());
7652 closetest_closed = TRUE;
7653 trace("Closed session handle\n");
7655 res = InternetCloseHandle(closetest_conn);
7656 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %lu\n",
7657 res, GetLastError());
7659 res = InternetCloseHandle(closetest_req);
7660 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %lu\n",
7661 res, GetLastError());
7663 len = sizeof(flags);
7664 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
7665 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
7666 "InternetQueryOptionA(%p INTERNET_OPTION_REQUEST_FLAGS) failed: %x %lu, expected TRUE ERROR_INVALID_HANDLE\n",
7667 closetest_req, res, GetLastError());
7670 static void test_connection_failure(void)
7672 test_request_t req;
7673 DWORD error;
7674 BOOL ret;
7676 open_simple_request(&req, "localhost", 1, NULL, "/");
7678 SetLastError(0xdeadbeef);
7679 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
7680 error = GetLastError();
7681 ok(!ret, "unexpected success\n");
7682 ok(error == ERROR_INTERNET_CANNOT_CONNECT, "wrong error %lu\n", error);
7684 close_request(&req);
7687 static void test_default_service_port(void)
7689 HINTERNET session, connect, request;
7690 DWORD size, error;
7691 char buffer[128];
7692 BOOL ret;
7694 if(!is_ie7plus)
7695 return;
7697 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
7698 ok(session != NULL, "InternetOpen failed\n");
7700 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
7701 INTERNET_SERVICE_HTTP, 0, 0);
7702 ok(connect != NULL, "InternetConnect failed\n");
7704 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
7705 ok(request != NULL, "HttpOpenRequest failed\n");
7707 SetLastError(0xdeadbeef);
7708 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
7709 error = GetLastError();
7710 ok(!ret, "HttpSendRequest succeeded\n");
7711 ok(error == ERROR_INTERNET_SECURITY_CHANNEL_ERROR || error == ERROR_INTERNET_CANNOT_CONNECT,
7712 "got %lu\n", error);
7714 size = sizeof(buffer);
7715 memset(buffer, 0, sizeof(buffer));
7716 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
7717 ok(ret, "HttpQueryInfo failed with error %lu\n", GetLastError());
7718 ok(!strcmp(buffer, "test.winehq.org:80"), "Expected test.winehg.org:80, got '%s'\n", buffer);
7720 InternetCloseHandle(request);
7721 InternetCloseHandle(connect);
7723 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
7724 INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
7725 ok(connect != NULL, "InternetConnect failed\n");
7727 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
7728 ok(request != NULL, "HttpOpenRequest failed\n");
7730 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
7731 if (!ret && GetLastError() == ERROR_INTERNET_SECURITY_CHANNEL_ERROR)
7733 win_skip("Can't make https connection\n");
7734 goto done;
7736 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
7738 size = sizeof(buffer);
7739 memset(buffer, 0, sizeof(buffer));
7740 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
7741 ok(ret, "HttpQueryInfo failed with error %lu\n", GetLastError());
7742 ok(!strcmp(buffer, "test.winehq.org"), "Expected test.winehg.org, got '%s'\n", buffer);
7744 InternetCloseHandle(request);
7745 InternetCloseHandle(connect);
7747 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
7748 INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
7749 ok(connect != NULL, "InternetConnect failed\n");
7751 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, 0, 0);
7752 ok(request != NULL, "HttpOpenRequest failed\n");
7754 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
7755 ok(ret, "HttpSendRequest failed with error %lu\n", GetLastError());
7757 size = sizeof(buffer);
7758 memset(buffer, 0, sizeof(buffer));
7759 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
7760 ok(ret, "HttpQueryInfo failed with error %lu\n", GetLastError());
7761 ok(!strcmp(buffer, "test.winehq.org:443"), "Expected test.winehg.org:443, got '%s'\n", buffer);
7763 done:
7764 InternetCloseHandle(request);
7765 InternetCloseHandle(connect);
7766 InternetCloseHandle(session);
7769 static void init_status_tests(void)
7771 memset(expect, 0, sizeof(expect));
7772 memset(optional, 0, sizeof(optional));
7773 memset(wine_allow, 0, sizeof(wine_allow));
7774 memset(notified, 0, sizeof(notified));
7775 memset(status_string, 0, sizeof(status_string));
7777 #define STATUS_STRING(status) status_string[status] = #status
7778 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
7779 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
7780 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
7781 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
7782 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
7783 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
7784 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
7785 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
7786 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
7787 STATUS_STRING(INTERNET_STATUS_PREFETCH);
7788 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
7789 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
7790 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
7791 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
7792 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
7793 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
7794 STATUS_STRING(INTERNET_STATUS_REDIRECT);
7795 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
7796 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
7797 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
7798 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
7799 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
7800 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
7801 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
7802 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
7803 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
7804 #undef STATUS_STRING
7807 static void WINAPI header_cb( HINTERNET handle, DWORD_PTR ctx, DWORD status, LPVOID info, DWORD len )
7809 BOOL ret;
7810 DWORD index, size;
7811 char buf[256];
7813 if (status == INTERNET_STATUS_SENDING_REQUEST)
7815 ret = HttpAddRequestHeadersA( handle, "winetest: winetest", ~0u, HTTP_ADDREQ_FLAG_ADD );
7816 ok( ret, "HttpAddRequestHeadersA failed %lu\n", GetLastError() );
7817 SetEvent( (HANDLE)ctx );
7819 else if (status == INTERNET_STATUS_REQUEST_SENT)
7821 index = 0;
7822 size = sizeof(buf);
7823 ret = HttpQueryInfoA( handle, HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
7824 buf, &size, &index );
7825 ok( ret, "HttpQueryInfoA failed %lu\n", GetLastError() );
7826 ok( strstr( buf, "winetest: winetest" ) != NULL, "header missing\n" );
7827 SetEvent( (HANDLE)ctx );
7831 static void test_concurrent_header_access(void)
7833 HINTERNET ses, con, req;
7834 DWORD err;
7835 BOOL ret;
7836 HANDLE wait = CreateEventW( NULL, FALSE, FALSE, NULL );
7838 ses = InternetOpenA( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
7839 ok( ses != NULL, "InternetOpenA failed\n" );
7841 con = InternetConnectA( ses, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
7842 INTERNET_SERVICE_HTTP, 0, 0 );
7843 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
7845 req = HttpOpenRequestA( con, NULL, "/", NULL, NULL, NULL, 0, (DWORD_PTR)wait );
7846 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
7848 pInternetSetStatusCallbackA( req, header_cb );
7850 SetLastError( 0xdeadbeef );
7851 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
7852 err = GetLastError();
7853 ok( !ret, "HttpSendRequestA succeeded\n" );
7854 ok( err == ERROR_IO_PENDING, "got %u\n", ERROR_IO_PENDING );
7856 WaitForSingleObject( wait, 5000 );
7857 WaitForSingleObject( wait, 5000 );
7859 InternetCloseHandle( req );
7860 InternetCloseHandle( con );
7861 InternetCloseHandle( ses );
7862 CloseHandle( wait );
7865 static void test_cert_string(void)
7867 HINTERNET ses, con, req;
7868 char actual[512];
7869 DWORD size;
7870 BOOL res;
7871 PCCERT_CHAIN_CONTEXT chain;
7873 ses = InternetOpenA( "winetest", 0, NULL, NULL, 0 );
7874 ok( ses != NULL, "InternetOpenA failed\n" );
7876 con = InternetConnectA( ses, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
7877 INTERNET_SERVICE_HTTP, 0, 0 );
7878 ok( con != NULL, "InternetConnectA failed %lu\n", GetLastError() );
7880 req = HttpOpenRequestA( con, NULL, "/", NULL, NULL, NULL, 0, 0 );
7881 ok( req != NULL, "HttpOpenRequestA failed %lu\n", GetLastError() );
7883 size = sizeof(actual);
7884 SetLastError( 0xdeadbeef );
7885 memset( actual, 0x55, sizeof(actual) );
7886 res = InternetQueryOptionA( req, INTERNET_OPTION_SECURITY_CERTIFICATE, actual, &size );
7887 ok( !res && GetLastError() == ERROR_INTERNET_INVALID_OPERATION,
7888 "InternetQueryOption failed: %lu\n", GetLastError() );
7889 ok( size == 0, "unexpected size: %lu\n", size );
7890 ok( actual[0] == 0x55, "unexpected byte: %02x\n", actual[0] );
7892 size = sizeof(chain);
7893 SetLastError(0xdeadbeef);
7894 res = InternetQueryOptionA(req, INTERNET_OPTION_SERVER_CERT_CHAIN_CONTEXT, &chain, &size);
7895 ok(!res && (GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_STATE),
7896 "InternetQueryOption failed: %lu\n", GetLastError());
7898 InternetCloseHandle( req );
7899 InternetCloseHandle( con );
7900 InternetCloseHandle( ses );
7903 START_TEST(http)
7905 HMODULE hdll;
7906 hdll = GetModuleHandleA("wininet.dll");
7908 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
7909 win_skip("Too old IE (older than 6.0)\n");
7910 return;
7913 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
7914 pInternetSetStatusCallbackW = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackW");
7915 pInternetGetSecurityInfoByURLA = (void*)GetProcAddress(hdll, "InternetGetSecurityInfoByURLA");
7917 if(!pInternetGetSecurityInfoByURLA) {
7918 is_ie7plus = FALSE;
7919 win_skip("IE6 found. It's too old for some tests.\n");
7922 init_events();
7923 init_status_tests();
7924 test_InternetCloseHandle();
7925 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
7926 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
7927 InternetReadFile_test(0, &test_data[1]);
7928 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
7929 test_secure_connection();
7930 test_security_flags();
7931 InternetReadFile_test(0, &test_data[2]);
7932 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
7933 test_open_url_async();
7934 test_async_HttpSendRequestEx(&notification_data[0]);
7935 test_async_HttpSendRequestEx(&notification_data[1]);
7936 test_async_HttpSendRequestEx(&notification_data[2]);
7937 test_async_HttpSendRequestEx(&notification_data[3]);
7938 InternetOpenRequest_test();
7939 test_http_cache();
7940 InternetLockRequestFile_test();
7941 InternetOpenUrlA_test();
7942 HttpHeaders_test();
7943 test_http_connection();
7944 test_user_agent_header();
7945 test_bogus_accept_types_array();
7946 InternetReadFile_chunked_test();
7947 HttpSendRequestEx_test();
7948 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[3]);
7949 test_connection_failure();
7950 test_default_service_port();
7951 test_concurrent_header_access();
7952 test_cert_string();
7953 free_events();