wininet: Added InternetLockRequestFile tests.
[wine.git] / dlls / wininet / tests / http.c
blob2cc4b4746cf592fc959700ba461cece47306034c
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>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30 #include "winineti.h"
31 #include "winsock.h"
33 #include "wine/test.h"
35 #define TEST_URL "http://test.winehq.org/tests/hello.html"
37 static BOOL first_connection_to_test_url = TRUE;
39 /* Adapted from dlls/urlmon/tests/protocol.c */
41 #define SET_EXPECT2(status, num) \
42 expect[status] = num
44 #define SET_EXPECT(status) \
45 SET_EXPECT2(status, 1)
47 #define SET_OPTIONAL2(status, num) \
48 optional[status] = num
50 #define SET_OPTIONAL(status) \
51 SET_OPTIONAL2(status, 1)
53 /* SET_WINE_ALLOW's should be used with an appropriate
54 * todo_wine CHECK_NOTIFIED at a later point in the code */
55 #define SET_WINE_ALLOW2(status, num) \
56 wine_allow[status] = num
58 #define SET_WINE_ALLOW(status) \
59 SET_WINE_ALLOW2(status, 1)
61 #define CHECK_EXPECT(status) \
62 do { \
63 if (!expect[status] && !optional[status] && wine_allow[status]) \
64 { \
65 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
66 status < MAX_INTERNET_STATUS && status_string[status] ? \
67 status_string[status] : "unknown"); \
68 wine_allow[status]--; \
69 } \
70 else \
71 { \
72 ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
73 status < MAX_INTERNET_STATUS && status_string[status] ? \
74 status_string[status] : "unknown"); \
75 if (expect[status]) expect[status]--; \
76 else optional[status]--; \
77 } \
78 notified[status]++; \
79 }while(0)
81 /* CLEAR_NOTIFIED used in cases when notification behavior
82 * differs between Windows versions */
83 #define CLEAR_NOTIFIED(status) \
84 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
86 #define CHECK_NOTIFIED2(status, num) \
87 do { \
88 ok(notified[status] + optional[status] == (num), \
89 "expected status %d (%s) %d times, received %d times\n", \
90 status, status < MAX_INTERNET_STATUS && status_string[status] ? \
91 status_string[status] : "unknown", (num), notified[status]); \
92 CLEAR_NOTIFIED(status); \
93 }while(0)
95 #define CHECK_NOTIFIED(status) \
96 CHECK_NOTIFIED2(status, 1)
98 #define CHECK_NOT_NOTIFIED(status) \
99 CHECK_NOTIFIED2(status, 0)
101 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
102 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
103 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
104 static const char *status_string[MAX_INTERNET_STATUS];
106 static HANDLE hCompleteEvent, conn_close_event;
107 static DWORD req_error;
109 #define TESTF_REDIRECT 0x01
110 #define TESTF_COMPRESSED 0x02
111 #define TESTF_CHUNKED 0x04
113 typedef struct {
114 const char *url;
115 const char *redirected_url;
116 const char *host;
117 const char *path;
118 const char *headers;
119 DWORD flags;
120 const char *post_data;
121 const char *content;
122 } test_data_t;
124 static const test_data_t test_data[] = {
126 "http://test.winehq.org/tests/data.php",
127 "http://test.winehq.org/tests/data.php",
128 "test.winehq.org",
129 "/tests/data.php",
131 TESTF_CHUNKED
134 "http://test.winehq.org/tests/redirect",
135 "http://test.winehq.org/tests/hello.html",
136 "test.winehq.org",
137 "/tests/redirect",
139 TESTF_REDIRECT
142 "http://www.codeweavers.com/",
143 "http://www.codeweavers.com/",
144 "www.codeweavers.com",
146 "Accept-Encoding: gzip, deflate",
147 TESTF_COMPRESSED
150 "http://test.winehq.org/tests/post.php",
151 "http://test.winehq.org/tests/post.php",
152 "test.winehq.org",
153 "/tests/post.php",
154 "Content-Type: application/x-www-form-urlencoded",
156 "mode=Test",
157 "mode => Test\n"
161 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
162 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackW)(HINTERNET ,INTERNET_STATUS_CALLBACK);
163 static BOOL (WINAPI *pInternetGetSecurityInfoByURLA)(LPSTR,PCCERT_CHAIN_CONTEXT*,DWORD*);
165 static int strcmp_wa(LPCWSTR strw, const char *stra)
167 WCHAR buf[512];
168 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
169 return lstrcmpW(strw, buf);
172 static BOOL proxy_active(void)
174 HKEY internet_settings;
175 DWORD proxy_enable;
176 DWORD size;
178 if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
179 0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
180 return FALSE;
182 size = sizeof(DWORD);
183 if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
184 proxy_enable = 0;
186 RegCloseKey(internet_settings);
188 return proxy_enable != 0;
191 #define test_status_code(a,b) _test_status_code(__LINE__,a,b, FALSE)
192 #define test_status_code_todo(a,b) _test_status_code(__LINE__,a,b, TRUE)
193 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
195 DWORD code, size, index;
196 char exbuf[10], bufa[10];
197 WCHAR bufw[10];
198 BOOL res;
200 code = 0xdeadbeef;
201 size = sizeof(code);
202 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, NULL);
203 ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %u\n", GetLastError());
204 if (is_todo)
205 todo_wine ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
206 else
207 ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
208 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
210 code = 0xdeadbeef;
211 index = 0;
212 size = sizeof(code);
213 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
214 ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %u\n", GetLastError());
215 if (is_todo)
216 todo_wine ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
217 else
218 ok_(__FILE__,line)(!index, "index = %d, expected 0\n", code);
219 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
221 sprintf(exbuf, "%u", excode);
223 size = sizeof(bufa);
224 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, bufa, &size, NULL);
225 ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
226 if (is_todo)
227 todo_wine ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
228 else
229 ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
230 ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %d for \"%s\"\n", size, exbuf);
232 size = 0;
233 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
234 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
235 "[4] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
236 ok_(__FILE__,line)(size == strlen(exbuf)+1, "unexpected size %d for \"%s\"\n", size, exbuf);
238 size = sizeof(bufw);
239 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
240 ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
241 if (is_todo)
242 todo_wine ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
243 else
244 ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
245 ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
247 size = 0;
248 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
249 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
250 "[6] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
251 ok_(__FILE__,line)(size == (strlen(exbuf)+1)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
253 if(0) {
254 size = sizeof(bufw);
255 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
256 ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "HttpQueryInfo(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
257 ok(size == sizeof(bufw), "unexpected size %d\n", size);
260 code = 0xdeadbeef;
261 index = 1;
262 size = sizeof(code);
263 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
264 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
265 "[7] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
267 code = 0xdeadbeef;
268 size = sizeof(code);
269 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_REQUEST_HEADERS, &code, &size, NULL);
270 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_INVALID_QUERY_REQUEST,
271 "[8] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
274 #define test_request_flags(a,b) _test_request_flags(__LINE__,a,b,FALSE)
275 #define test_request_flags_todo(a,b) _test_request_flags(__LINE__,a,b,TRUE)
276 static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOOL is_todo)
278 DWORD flags, size;
279 BOOL res;
281 flags = 0xdeadbeef;
282 size = sizeof(flags);
283 res = InternetQueryOptionW(req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &size);
284 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_REQUEST_FLAGS) failed: %u\n", GetLastError());
286 /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
287 flags &= ~INTERNET_REQFLAG_CACHE_WRITE_DISABLED;
288 if(!is_todo)
289 ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
290 else
291 todo_wine ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
294 #define test_http_version(a) _test_http_version(__LINE__,a)
295 static void _test_http_version(unsigned line, HINTERNET req)
297 HTTP_VERSION_INFO v = {0xdeadbeef, 0xdeadbeef};
298 DWORD size;
299 BOOL res;
301 size = sizeof(v);
302 res = InternetQueryOptionW(req, INTERNET_OPTION_HTTP_VERSION, &v, &size);
303 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_HTTP_VERSION) failed: %u\n", GetLastError());
304 ok_(__FILE__,line)(v.dwMajorVersion == 1, "dwMajorVersion = %d\n", v.dwMajorVersion);
305 ok_(__FILE__,line)(v.dwMinorVersion == 1, "dwMinorVersion = %d\n", v.dwMinorVersion);
308 static int close_handle_cnt;
310 static VOID WINAPI callback(
311 HINTERNET hInternet,
312 DWORD_PTR dwContext,
313 DWORD dwInternetStatus,
314 LPVOID lpvStatusInformation,
315 DWORD dwStatusInformationLength
318 CHECK_EXPECT(dwInternetStatus);
319 switch (dwInternetStatus)
321 case INTERNET_STATUS_RESOLVING_NAME:
322 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
323 GetCurrentThreadId(), hInternet, dwContext,
324 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
325 *(LPSTR)lpvStatusInformation = '\0';
326 break;
327 case INTERNET_STATUS_NAME_RESOLVED:
328 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
329 GetCurrentThreadId(), hInternet, dwContext,
330 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
331 *(LPSTR)lpvStatusInformation = '\0';
332 break;
333 case INTERNET_STATUS_CONNECTING_TO_SERVER:
334 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
335 GetCurrentThreadId(), hInternet, dwContext,
336 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
337 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
338 dwStatusInformationLength);
339 *(LPSTR)lpvStatusInformation = '\0';
340 break;
341 case INTERNET_STATUS_CONNECTED_TO_SERVER:
342 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
343 GetCurrentThreadId(), hInternet, dwContext,
344 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
345 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
346 dwStatusInformationLength);
347 *(LPSTR)lpvStatusInformation = '\0';
348 break;
349 case INTERNET_STATUS_SENDING_REQUEST:
350 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
351 GetCurrentThreadId(), hInternet, dwContext,
352 lpvStatusInformation,dwStatusInformationLength);
353 break;
354 case INTERNET_STATUS_REQUEST_SENT:
355 ok(dwStatusInformationLength == sizeof(DWORD),
356 "info length should be sizeof(DWORD) instead of %d\n",
357 dwStatusInformationLength);
358 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
359 GetCurrentThreadId(), hInternet, dwContext,
360 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
361 break;
362 case INTERNET_STATUS_RECEIVING_RESPONSE:
363 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
364 GetCurrentThreadId(), hInternet, dwContext,
365 lpvStatusInformation,dwStatusInformationLength);
366 break;
367 case INTERNET_STATUS_RESPONSE_RECEIVED:
368 ok(dwStatusInformationLength == sizeof(DWORD),
369 "info length should be sizeof(DWORD) instead of %d\n",
370 dwStatusInformationLength);
371 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
372 GetCurrentThreadId(), hInternet, dwContext,
373 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
374 break;
375 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
376 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
377 GetCurrentThreadId(), hInternet,dwContext,
378 lpvStatusInformation,dwStatusInformationLength);
379 break;
380 case INTERNET_STATUS_PREFETCH:
381 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
382 GetCurrentThreadId(), hInternet, dwContext,
383 lpvStatusInformation,dwStatusInformationLength);
384 break;
385 case INTERNET_STATUS_CLOSING_CONNECTION:
386 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
387 GetCurrentThreadId(), hInternet, dwContext,
388 lpvStatusInformation,dwStatusInformationLength);
389 break;
390 case INTERNET_STATUS_CONNECTION_CLOSED:
391 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
392 GetCurrentThreadId(), hInternet, dwContext,
393 lpvStatusInformation,dwStatusInformationLength);
394 break;
395 case INTERNET_STATUS_HANDLE_CREATED:
396 ok(dwStatusInformationLength == sizeof(HINTERNET),
397 "info length should be sizeof(HINTERNET) instead of %d\n",
398 dwStatusInformationLength);
399 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
400 GetCurrentThreadId(), hInternet, dwContext,
401 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
402 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
403 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
404 break;
405 case INTERNET_STATUS_HANDLE_CLOSING:
406 ok(dwStatusInformationLength == sizeof(HINTERNET),
407 "info length should be sizeof(HINTERNET) instead of %d\n",
408 dwStatusInformationLength);
409 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
410 GetCurrentThreadId(), hInternet, dwContext,
411 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
412 if(!InterlockedDecrement(&close_handle_cnt))
413 SetEvent(hCompleteEvent);
414 break;
415 case INTERNET_STATUS_REQUEST_COMPLETE:
417 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
418 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
419 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
420 dwStatusInformationLength);
421 ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %ld\n", iar->dwResult);
422 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
423 GetCurrentThreadId(), hInternet, dwContext,
424 iar->dwResult,iar->dwError,dwStatusInformationLength);
425 req_error = iar->dwError;
426 if(!close_handle_cnt)
427 SetEvent(hCompleteEvent);
428 break;
430 case INTERNET_STATUS_REDIRECT:
431 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
432 GetCurrentThreadId(), hInternet, dwContext,
433 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
434 *(LPSTR)lpvStatusInformation = '\0';
435 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
436 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
437 break;
438 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
439 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
440 GetCurrentThreadId(), hInternet, dwContext,
441 lpvStatusInformation, dwStatusInformationLength);
442 break;
443 default:
444 trace("%04x:Callback %p 0x%lx %d %p %d\n",
445 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
446 lpvStatusInformation, dwStatusInformationLength);
450 static void close_async_handle(HINTERNET handle, HANDLE complete_event, int handle_cnt)
452 BOOL res;
454 close_handle_cnt = handle_cnt;
456 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
457 res = InternetCloseHandle(handle);
458 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
459 WaitForSingleObject(hCompleteEvent, INFINITE);
460 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
463 static void InternetReadFile_test(int flags, const test_data_t *test)
465 char *post_data = NULL;
466 BOOL res, on_async = TRUE;
467 CHAR buffer[4000];
468 DWORD length, exlen = 0, post_len = 0;
469 const char *types[2] = { "*", NULL };
470 HINTERNET hi, hic = 0, hor = 0;
472 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
474 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
476 trace("InternetOpenA <--\n");
477 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
478 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
479 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
480 trace("InternetOpenA -->\n");
482 if (hi == 0x0) goto abort;
484 pInternetSetStatusCallbackA(hi,&callback);
486 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
488 trace("InternetConnectA <--\n");
489 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
490 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
491 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
492 trace("InternetConnectA -->\n");
494 if (hic == 0x0) goto abort;
496 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
497 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
499 trace("HttpOpenRequestA <--\n");
500 hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
501 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
502 0xdeadbead);
503 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
505 * If the internet name can't be resolved we are probably behind
506 * a firewall or in some other way not directly connected to the
507 * Internet. Not enough reason to fail the test. Just ignore and
508 * abort.
510 } else {
511 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
513 trace("HttpOpenRequestA -->\n");
515 if (hor == 0x0) goto abort;
517 test_request_flags(hor, INTERNET_REQFLAG_NO_HEADERS);
519 length = sizeof(buffer);
520 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
521 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
522 ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
524 length = sizeof(buffer);
525 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
526 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
527 ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
528 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
530 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
531 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
532 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
533 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT,2);
534 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_RECEIVED,2);
535 if (first_connection_to_test_url)
537 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
538 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
540 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
541 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
542 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
543 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
544 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
545 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
546 if(test->flags & TESTF_REDIRECT) {
547 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
548 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
550 SET_EXPECT(INTERNET_STATUS_REDIRECT);
551 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
552 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
553 if (flags & INTERNET_FLAG_ASYNC)
554 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
556 if(test->flags & TESTF_COMPRESSED) {
557 BOOL b = TRUE;
559 res = InternetSetOption(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
560 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
561 "InternetSetOption failed: %u\n", GetLastError());
562 if(!res)
563 goto abort;
566 test_status_code(hor, 0);
568 trace("HttpSendRequestA -->\n");
569 if(test->post_data) {
570 post_len = strlen(test->post_data);
571 post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
572 memcpy(post_data, test->post_data, post_len);
574 SetLastError(0xdeadbeef);
575 res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
576 if (flags & INTERNET_FLAG_ASYNC)
577 ok(!res && (GetLastError() == ERROR_IO_PENDING),
578 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
579 else
580 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
581 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
582 trace("HttpSendRequestA <--\n");
584 if (flags & INTERNET_FLAG_ASYNC) {
585 WaitForSingleObject(hCompleteEvent, INFINITE);
586 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
588 HeapFree(GetProcessHeap(), 0, post_data);
590 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
591 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
592 if (first_connection_to_test_url)
594 if (! proxy_active())
596 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
597 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
599 else
601 CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
602 CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
605 else
607 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
608 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
610 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
611 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
612 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
613 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
614 if(test->flags & TESTF_REDIRECT)
615 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
616 if (flags & INTERNET_FLAG_ASYNC)
617 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
618 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
619 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
620 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
622 test_request_flags(hor, 0);
624 length = 100;
625 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
626 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
628 length = sizeof(buffer)-1;
629 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
630 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
631 buffer[length]=0;
633 length = sizeof(buffer);
634 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
635 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
636 ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
638 length = 16;
639 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
640 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
641 if(test->flags & TESTF_COMPRESSED)
642 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
643 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
645 length = 100;
646 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
647 buffer[length]=0;
648 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
650 length = 100;
651 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
652 buffer[length]=0;
653 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
655 SetLastError(0xdeadbeef);
656 res = InternetReadFile(NULL, buffer, 100, &length);
657 ok(!res, "InternetReadFile should have failed\n");
658 ok(GetLastError() == ERROR_INVALID_HANDLE,
659 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
660 GetLastError());
662 length = 100;
663 trace("Entering Query loop\n");
665 while (TRUE)
667 if (flags & INTERNET_FLAG_ASYNC)
668 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
669 length = 0;
670 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
671 if (flags & INTERNET_FLAG_ASYNC)
673 if (res)
675 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
676 if(exlen) {
677 ok(length >= exlen, "length %u < exlen %u\n", length, exlen);
678 exlen = 0;
681 else if (GetLastError() == ERROR_IO_PENDING)
683 trace("PENDING\n");
684 /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
685 if(!(test->flags & TESTF_CHUNKED))
686 ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
687 WaitForSingleObject(hCompleteEvent, INFINITE);
688 exlen = length;
689 ok(exlen, "length = 0\n");
690 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
691 ok(req_error, "req_error = 0\n");
692 continue;
693 }else {
694 ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
696 }else {
697 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
699 trace("LENGTH %d\n", length);
700 if(test->flags & TESTF_CHUNKED)
701 ok(length <= 8192, "length = %d, expected <= 8192\n", length);
702 if (length)
704 char *buffer;
705 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
707 res = InternetReadFile(hor,buffer,length,&length);
709 buffer[length]=0;
711 trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
713 if(test->content)
714 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
715 HeapFree(GetProcessHeap(),0,buffer);
716 }else {
717 ok(!on_async, "Returned zero size in response to request complete\n");
718 break;
720 on_async = FALSE;
722 if(test->flags & TESTF_REDIRECT) {
723 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
724 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
726 abort:
727 trace("aborting\n");
728 close_async_handle(hi, hCompleteEvent, 2);
729 CloseHandle(hCompleteEvent);
730 first_connection_to_test_url = FALSE;
733 static void InternetReadFile_chunked_test(void)
735 BOOL res;
736 CHAR buffer[4000];
737 DWORD length;
738 const char *types[2] = { "*", NULL };
739 HINTERNET hi, hic = 0, hor = 0;
741 trace("Starting InternetReadFile chunked test\n");
743 trace("InternetOpenA <--\n");
744 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
745 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
746 trace("InternetOpenA -->\n");
748 if (hi == 0x0) goto abort;
750 trace("InternetConnectA <--\n");
751 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
752 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
753 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
754 trace("InternetConnectA -->\n");
756 if (hic == 0x0) goto abort;
758 trace("HttpOpenRequestA <--\n");
759 hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
760 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
761 0xdeadbead);
762 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
764 * If the internet name can't be resolved we are probably behind
765 * a firewall or in some other way not directly connected to the
766 * Internet. Not enough reason to fail the test. Just ignore and
767 * abort.
769 } else {
770 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
772 trace("HttpOpenRequestA -->\n");
774 if (hor == 0x0) goto abort;
776 trace("HttpSendRequestA -->\n");
777 SetLastError(0xdeadbeef);
778 res = HttpSendRequestA(hor, "", -1, NULL, 0);
779 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
780 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
781 trace("HttpSendRequestA <--\n");
783 test_request_flags(hor, 0);
785 length = 100;
786 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
787 buffer[length]=0;
788 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
790 SetLastError( 0xdeadbeef );
791 length = 100;
792 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
793 buffer[length]=0;
794 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
795 ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
796 "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
797 ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
798 "Wrong transfer encoding '%s'\n", buffer );
800 SetLastError( 0xdeadbeef );
801 length = 16;
802 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
803 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
804 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
806 length = 100;
807 trace("Entering Query loop\n");
809 while (TRUE)
811 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
812 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
813 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
814 trace("got %u available\n",length);
815 if (length)
817 DWORD got;
818 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
820 res = InternetReadFile(hor,buffer,length,&got);
822 buffer[got]=0;
823 trace("ReadFile -> %i %i\n",res,got);
824 ok( length == got, "only got %u of %u available\n", got, length );
825 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
827 HeapFree(GetProcessHeap(),0,buffer);
828 if (!got) break;
830 if (length == 0)
831 break;
833 abort:
834 trace("aborting\n");
835 if (hor != 0x0) {
836 res = InternetCloseHandle(hor);
837 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
839 if (hi != 0x0) {
840 res = InternetCloseHandle(hi);
841 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
845 static void InternetReadFileExA_test(int flags)
847 DWORD rc;
848 DWORD length;
849 const char *types[2] = { "*", NULL };
850 HINTERNET hi, hic = 0, hor = 0;
851 INTERNET_BUFFERS inetbuffers;
853 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
855 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
857 trace("InternetOpenA <--\n");
858 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
859 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
860 trace("InternetOpenA -->\n");
862 if (hi == 0x0) goto abort;
864 pInternetSetStatusCallbackA(hi,&callback);
866 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
868 trace("InternetConnectA <--\n");
869 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
870 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
871 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
872 trace("InternetConnectA -->\n");
874 if (hic == 0x0) goto abort;
876 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
877 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
879 trace("HttpOpenRequestA <--\n");
880 hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
881 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
882 0xdeadbead);
883 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
885 * If the internet name can't be resolved we are probably behind
886 * a firewall or in some other way not directly connected to the
887 * Internet. Not enough reason to fail the test. Just ignore and
888 * abort.
890 } else {
891 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
893 trace("HttpOpenRequestA -->\n");
895 if (hor == 0x0) goto abort;
897 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
898 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
899 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
900 if (first_connection_to_test_url)
902 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
903 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
905 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT, 2);
906 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
907 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
908 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
909 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
910 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
911 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
912 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
913 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
914 SET_EXPECT(INTERNET_STATUS_REDIRECT);
915 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
916 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
917 if (flags & INTERNET_FLAG_ASYNC)
918 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
919 else
920 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
922 trace("HttpSendRequestA -->\n");
923 SetLastError(0xdeadbeef);
924 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
925 if (flags & INTERNET_FLAG_ASYNC)
926 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
927 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
928 else
929 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
930 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
931 trace("HttpSendRequestA <--\n");
933 if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
934 WaitForSingleObject(hCompleteEvent, INFINITE);
935 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
938 if (first_connection_to_test_url)
940 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
941 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
943 else
945 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
946 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
948 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
949 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
950 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
951 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
952 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
953 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
954 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
955 if (flags & INTERNET_FLAG_ASYNC)
956 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
957 else
958 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
959 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
960 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
961 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
962 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
964 /* tests invalid dwStructSize */
965 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
966 inetbuffers.lpcszHeader = NULL;
967 inetbuffers.dwHeadersLength = 0;
968 inetbuffers.dwBufferLength = 10;
969 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
970 inetbuffers.dwOffsetHigh = 1234;
971 inetbuffers.dwOffsetLow = 5678;
972 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
973 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
974 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
975 rc ? "TRUE" : "FALSE", GetLastError());
976 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
978 test_request_flags(hor, 0);
980 /* tests to see whether lpcszHeader is used - it isn't */
981 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
982 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
983 inetbuffers.dwHeadersLength = 255;
984 inetbuffers.dwBufferLength = 0;
985 inetbuffers.lpvBuffer = NULL;
986 inetbuffers.dwOffsetHigh = 1234;
987 inetbuffers.dwOffsetLow = 5678;
988 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
989 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
990 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
991 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
992 trace("read %i bytes\n", inetbuffers.dwBufferLength);
993 todo_wine
995 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
996 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
999 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
1000 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
1001 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
1002 rc ? "TRUE" : "FALSE", GetLastError());
1004 length = 0;
1005 trace("Entering Query loop\n");
1007 while (TRUE)
1009 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
1010 inetbuffers.dwBufferLength = 1024;
1011 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
1012 inetbuffers.dwOffsetHigh = 1234;
1013 inetbuffers.dwOffsetLow = 5678;
1015 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1016 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1017 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1018 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
1019 if (!rc)
1021 if (GetLastError() == ERROR_IO_PENDING)
1023 trace("InternetReadFileEx -> PENDING\n");
1024 ok(flags & INTERNET_FLAG_ASYNC,
1025 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
1026 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1027 WaitForSingleObject(hCompleteEvent, INFINITE);
1028 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1029 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1030 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1032 else
1034 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
1035 break;
1038 else
1040 trace("InternetReadFileEx -> SUCCEEDED\n");
1041 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1042 if (inetbuffers.dwBufferLength)
1044 todo_wine {
1045 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1046 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1049 else
1051 /* Win98 still sends these when 0 bytes are read, WinXP does not */
1052 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1053 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1057 trace("read %i bytes\n", inetbuffers.dwBufferLength);
1058 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
1060 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
1061 "InternetReadFileEx sets offsets to 0x%x%08x\n",
1062 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
1064 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1066 if (!inetbuffers.dwBufferLength)
1067 break;
1069 length += inetbuffers.dwBufferLength;
1071 ok(length > 0, "failed to read any of the document\n");
1072 trace("Finished. Read %d bytes\n", length);
1074 abort:
1075 close_async_handle(hi, hCompleteEvent, 2);
1076 CloseHandle(hCompleteEvent);
1077 first_connection_to_test_url = FALSE;
1080 static void InternetOpenUrlA_test(void)
1082 HINTERNET myhinternet, myhttp;
1083 char buffer[0x400];
1084 DWORD size, readbytes, totalbytes=0;
1085 BOOL ret;
1087 ret = DeleteUrlCacheEntry(TEST_URL);
1088 ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND,
1089 "DeleteUrlCacheEntry returned %x, GetLastError() = %d\n", ret, GetLastError());
1091 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1092 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
1093 size = 0x400;
1094 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
1095 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1097 SetLastError(0);
1098 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
1099 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1100 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1101 return; /* WinXP returns this when not connected to the net */
1102 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1103 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1104 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1105 totalbytes += readbytes;
1106 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1107 totalbytes += readbytes;
1108 trace("read 0x%08x bytes\n",totalbytes);
1110 InternetCloseHandle(myhttp);
1111 InternetCloseHandle(myhinternet);
1113 ret = DeleteUrlCacheEntry(TEST_URL);
1114 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "INTERNET_FLAG_NO_CACHE_WRITE flag doesn't work\n");
1117 static void HttpSendRequestEx_test(void)
1119 HINTERNET hSession;
1120 HINTERNET hConnect;
1121 HINTERNET hRequest;
1123 INTERNET_BUFFERS BufferIn;
1124 DWORD dwBytesWritten, dwBytesRead, error;
1125 CHAR szBuffer[256];
1126 int i;
1127 BOOL ret;
1129 static char szPostData[] = "mode=Test";
1130 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1132 hSession = InternetOpen("Wine Regression Test",
1133 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1134 ok( hSession != NULL ,"Unable to open Internet session\n");
1135 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1136 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1138 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1139 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1140 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1141 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1143 skip( "Network unreachable, skipping test\n" );
1144 goto done;
1146 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1148 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1150 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
1151 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
1152 BufferIn.lpcszHeader = szContentType;
1153 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1154 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1155 BufferIn.lpvBuffer = szPostData;
1156 BufferIn.dwBufferLength = 3;
1157 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1158 BufferIn.dwOffsetLow = 0;
1159 BufferIn.dwOffsetHigh = 0;
1161 SetLastError(0xdeadbeef);
1162 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
1163 error = GetLastError();
1164 ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1165 ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1167 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1169 for (i = 3; szPostData[i]; i++)
1170 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1171 "InternetWriteFile failed\n");
1173 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1175 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1177 test_request_flags(hRequest, 0);
1179 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1180 "Unable to read response\n");
1181 szBuffer[dwBytesRead] = 0;
1183 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1184 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1186 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1187 done:
1188 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1189 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1192 static void InternetOpenRequest_test(void)
1194 HINTERNET session, connect, request;
1195 static const char *types[] = { "*", "", NULL };
1196 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1197 static const WCHAR *typesW[] = { any, empty, NULL };
1198 BOOL ret;
1200 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1201 ok(session != NULL ,"Unable to open Internet session\n");
1203 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1204 INTERNET_SERVICE_HTTP, 0, 0);
1205 ok(connect == NULL, "InternetConnectA should have failed\n");
1206 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1208 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1209 INTERNET_SERVICE_HTTP, 0, 0);
1210 ok(connect == NULL, "InternetConnectA should have failed\n");
1211 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1213 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1214 INTERNET_SERVICE_HTTP, 0, 0);
1215 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1217 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1218 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1220 skip( "Network unreachable, skipping test\n" );
1221 goto done;
1223 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1225 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1226 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1227 ok(InternetCloseHandle(request), "Close request handle failed\n");
1229 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1230 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1232 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1233 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1234 ok(InternetCloseHandle(request), "Close request handle failed\n");
1236 done:
1237 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1238 ok(InternetCloseHandle(session), "Close session handle failed\n");
1241 static void test_cache_read(void)
1243 HINTERNET session, connection, req;
1244 FILETIME now, tomorrow, yesterday;
1245 BYTE content[1000], buf[2000];
1246 char file_path[MAX_PATH];
1247 ULARGE_INTEGER li;
1248 HANDLE file;
1249 DWORD size;
1250 unsigned i;
1251 BOOL res;
1253 static const char cache_only_url[] = "http://test.winehq.org/tests/cache-only";
1254 BYTE cache_headers[] = "HTTP/1.1 200 OK\r\n\r\n";
1256 trace("Testing cache read...\n");
1258 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1260 for(i = 0; i < sizeof(content); i++)
1261 content[i] = '0' + (i%10);
1263 GetSystemTimeAsFileTime(&now);
1264 li.u.HighPart = now.dwHighDateTime;
1265 li.u.LowPart = now.dwLowDateTime;
1266 li.QuadPart += (LONGLONG)10000000 * 3600 * 24;
1267 tomorrow.dwHighDateTime = li.u.HighPart;
1268 tomorrow.dwLowDateTime = li.u.LowPart;
1269 li.QuadPart -= (LONGLONG)10000000 * 3600 * 24 * 2;
1270 yesterday.dwHighDateTime = li.u.HighPart;
1271 yesterday.dwLowDateTime = li.u.LowPart;
1273 res = CreateUrlCacheEntryA(cache_only_url, sizeof(content), "", file_path, 0);
1274 ok(res, "CreateUrlCacheEntryA failed: %u\n", GetLastError());
1276 file = CreateFileA(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1277 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1279 WriteFile(file, content, sizeof(content), &size, NULL);
1280 CloseHandle(file);
1282 res = CommitUrlCacheEntryA(cache_only_url, file_path, tomorrow, yesterday, NORMAL_CACHE_ENTRY,
1283 cache_headers, sizeof(cache_headers)-1, "", 0);
1284 ok(res, "CommitUrlCacheEntryA failed: %u\n", GetLastError());
1286 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
1287 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
1289 pInternetSetStatusCallbackA(session, callback);
1291 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1292 connection = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT,
1293 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1294 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
1295 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1297 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1298 req = HttpOpenRequestA(connection, "GET", "/tests/cache-only", NULL, NULL, NULL, 0, 0xdeadbead);
1299 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1300 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1302 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
1303 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
1304 SET_WINE_ALLOW(INTERNET_STATUS_SENDING_REQUEST);
1305 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_SENT);
1306 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1307 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1308 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1310 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
1311 todo_wine
1312 ok(res, "HttpSendRequest failed: %u\n", GetLastError());
1314 if(res) {
1315 size = 0;
1316 res = InternetQueryDataAvailable(req, &size, 0, 0);
1317 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
1318 ok(size == sizeof(content), "size = %u\n", size);
1320 size = sizeof(buf);
1321 res = InternetReadFile(req, buf, sizeof(buf), &size);
1322 ok(res, "InternetReadFile failed: %u\n", GetLastError());
1323 ok(size == sizeof(content), "size = %u\n", size);
1324 ok(!memcmp(content, buf, sizeof(content)), "unexpected content\n");
1327 close_async_handle(session, hCompleteEvent, 2);
1329 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1330 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1331 CLEAR_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
1332 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
1333 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1334 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1335 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1337 res = DeleteUrlCacheEntryA(cache_only_url);
1338 ok(res, "DeleteUrlCacheEntryA failed: %u\n", GetLastError());
1340 CloseHandle(hCompleteEvent);
1343 static void test_http_cache(void)
1345 HINTERNET session, connect, request;
1346 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1347 DWORD size, file_size;
1348 BYTE buf[100];
1349 HANDLE file;
1350 BOOL ret;
1352 static const char *types[] = { "*", "", NULL };
1354 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1355 ok(session != NULL ,"Unable to open Internet session\n");
1357 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1358 INTERNET_SERVICE_HTTP, 0, 0);
1359 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1361 request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1362 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1364 skip( "Network unreachable, skipping test\n" );
1366 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1367 ok(InternetCloseHandle(session), "Close session handle failed\n");
1369 return;
1371 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1373 size = sizeof(url);
1374 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1375 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1376 ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1378 size = sizeof(file_name);
1379 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1380 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1381 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1382 ok(!size, "size = %d\n", size);
1384 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1385 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1387 size = sizeof(file_name);
1388 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1389 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1391 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1392 FILE_ATTRIBUTE_NORMAL, NULL);
1393 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1394 file_size = GetFileSize(file, NULL);
1395 ok(file_size == 106, "file size = %u\n", file_size);
1397 size = sizeof(buf);
1398 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1399 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1400 ok(size == 100, "size = %u\n", size);
1402 file_size = GetFileSize(file, NULL);
1403 ok(file_size == 106, "file size = %u\n", file_size);
1404 CloseHandle(file);
1406 ret = DeleteFileA(file_name);
1407 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1409 ok(InternetCloseHandle(request), "Close request handle failed\n");
1411 file = CreateFile(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1412 FILE_ATTRIBUTE_NORMAL, NULL);
1413 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1414 CloseHandle(file);
1416 /* Send the same request, requiring it to be retrieved from the cache */
1417 request = HttpOpenRequest(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1419 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1420 ok(ret, "HttpSendRequest failed\n");
1422 size = sizeof(buf);
1423 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1424 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1425 ok(size == 100, "size = %u\n", size);
1427 ok(InternetCloseHandle(request), "Close request handle failed\n");
1429 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1430 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1432 size = sizeof(file_name);
1433 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1434 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1435 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1436 ok(!size, "size = %d\n", size);
1438 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1439 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1441 size = sizeof(file_name);
1442 file_name[0] = 0;
1443 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1444 if (ret)
1446 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1447 FILE_ATTRIBUTE_NORMAL, NULL);
1448 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1449 CloseHandle(file);
1451 else
1453 /* < IE8 */
1454 ok(file_name[0] == 0, "Didn't expect a file name\n");
1457 ok(InternetCloseHandle(request), "Close request handle failed\n");
1458 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1459 ok(InternetCloseHandle(session), "Close session handle failed\n");
1461 test_cache_read();
1464 static void InternetLockRequestFile_test(void)
1466 HINTERNET session, connect, request;
1467 char file_name[MAX_PATH];
1468 HANDLE lock, lock2;
1469 DWORD size;
1470 BOOL ret;
1472 static const char *types[] = { "*", "", NULL };
1474 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1475 ok(session != NULL ,"Unable to open Internet session\n");
1477 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1478 INTERNET_SERVICE_HTTP, 0, 0);
1479 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1481 request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_RELOAD, 0);
1482 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1484 skip( "Network unreachable, skipping test\n" );
1486 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1487 ok(InternetCloseHandle(session), "Close session handle failed\n");
1489 return;
1491 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1493 size = sizeof(file_name);
1494 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1495 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1496 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1497 ok(!size, "size = %d\n", size);
1499 lock = NULL;
1500 ret = InternetLockRequestFile(request, &lock);
1501 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1503 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1504 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1506 size = sizeof(file_name);
1507 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1508 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1510 ret = InternetLockRequestFile(request, &lock);
1511 ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1512 ok(lock != NULL, "lock == NULL\n");
1514 ret = InternetLockRequestFile(request, &lock2);
1515 ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1516 ok(lock == lock2, "lock != lock2\n");
1518 ret = InternetUnlockRequestFile(lock2);
1519 ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1521 ret = DeleteFileA(file_name);
1522 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1524 ok(InternetCloseHandle(request), "Close request handle failed\n");
1526 ret = DeleteFileA(file_name);
1527 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1529 ret = InternetUnlockRequestFile(lock);
1530 ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1532 ret = DeleteFileA(file_name);
1533 ok(ret, "Deleting file returned %x(%u)\n", ret, GetLastError());
1536 static void HttpHeaders_test(void)
1538 HINTERNET hSession;
1539 HINTERNET hConnect;
1540 HINTERNET hRequest;
1541 CHAR buffer[256];
1542 WCHAR wbuffer[256];
1543 DWORD len = 256;
1544 DWORD oldlen;
1545 DWORD index = 0;
1546 BOOL ret;
1548 hSession = InternetOpen("Wine Regression Test",
1549 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1550 ok( hSession != NULL ,"Unable to open Internet session\n");
1551 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1552 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1554 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1555 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1556 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1557 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1559 skip( "Network unreachable, skipping test\n" );
1560 goto done;
1562 ok( hRequest != NULL, "Failed to open request handle\n");
1564 index = 0;
1565 len = sizeof(buffer);
1566 strcpy(buffer,"Warning");
1567 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1568 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1570 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1571 "Failed to add new header\n");
1573 index = 0;
1574 len = sizeof(buffer);
1575 strcpy(buffer,"Warning");
1576 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1577 buffer,&len,&index),"Unable to query header\n");
1578 ok(index == 1, "Index was not incremented\n");
1579 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1580 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1581 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1582 len = sizeof(buffer);
1583 strcpy(buffer,"Warning");
1584 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1585 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1587 index = 0;
1588 len = 5; /* could store the string but not the NULL terminator */
1589 strcpy(buffer,"Warning");
1590 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1591 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1592 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1593 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1595 /* a call with NULL will fail but will return the length */
1596 index = 0;
1597 len = sizeof(buffer);
1598 SetLastError(0xdeadbeef);
1599 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1600 NULL,&len,&index) == FALSE,"Query worked\n");
1601 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1602 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1603 ok(index == 0, "Index was incremented\n");
1605 /* even for a len that is too small */
1606 index = 0;
1607 len = 15;
1608 SetLastError(0xdeadbeef);
1609 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1610 NULL,&len,&index) == FALSE,"Query worked\n");
1611 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1612 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1613 ok(index == 0, "Index was incremented\n");
1615 index = 0;
1616 len = 0;
1617 SetLastError(0xdeadbeef);
1618 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1619 NULL,&len,&index) == FALSE,"Query worked\n");
1620 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1621 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1622 ok(index == 0, "Index was incremented\n");
1623 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1626 /* a working query */
1627 index = 0;
1628 len = sizeof(buffer);
1629 memset(buffer, 'x', sizeof(buffer));
1630 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1631 buffer,&len,&index),"Unable to query header\n");
1632 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1633 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1634 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1635 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1636 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1637 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1638 ok(index == 0, "Index was incremented\n");
1640 /* Like above two tests, but for W version */
1642 index = 0;
1643 len = 0;
1644 SetLastError(0xdeadbeef);
1645 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1646 NULL,&len,&index) == FALSE,"Query worked\n");
1647 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1648 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1649 ok(index == 0, "Index was incremented\n");
1650 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1652 /* a working query */
1653 index = 0;
1654 len = sizeof(wbuffer);
1655 memset(wbuffer, 'x', sizeof(wbuffer));
1656 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1657 wbuffer,&len,&index),"Unable to query header\n");
1658 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1659 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1660 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1661 ok(index == 0, "Index was incremented\n");
1663 /* end of W version tests */
1665 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1666 index = 0;
1667 len = sizeof(buffer);
1668 memset(buffer, 'x', sizeof(buffer));
1669 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1670 buffer,&len,&index) == TRUE,"Query failed\n");
1671 ok(len == 2, "Expected 2, got %d\n", len);
1672 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1673 ok(index == 0, "Index was incremented\n");
1675 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1676 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1678 index = 0;
1679 len = sizeof(buffer);
1680 strcpy(buffer,"Warning");
1681 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1682 buffer,&len,&index),"Unable to query header\n");
1683 ok(index == 1, "Index was not incremented\n");
1684 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1685 len = sizeof(buffer);
1686 strcpy(buffer,"Warning");
1687 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1688 buffer,&len,&index),"Failed to get second header\n");
1689 ok(index == 2, "Index was not incremented\n");
1690 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1691 len = sizeof(buffer);
1692 strcpy(buffer,"Warning");
1693 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1694 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1696 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1698 index = 0;
1699 len = sizeof(buffer);
1700 strcpy(buffer,"Warning");
1701 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1702 buffer,&len,&index),"Unable to query header\n");
1703 ok(index == 1, "Index was not incremented\n");
1704 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1705 len = sizeof(buffer);
1706 strcpy(buffer,"Warning");
1707 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1708 buffer,&len,&index),"Failed to get second header\n");
1709 ok(index == 2, "Index was not incremented\n");
1710 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1711 len = sizeof(buffer);
1712 strcpy(buffer,"Warning");
1713 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1714 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1716 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1718 index = 0;
1719 len = sizeof(buffer);
1720 strcpy(buffer,"Warning");
1721 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1722 buffer,&len,&index),"Unable to query header\n");
1723 ok(index == 1, "Index was not incremented\n");
1724 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1725 len = sizeof(buffer);
1726 strcpy(buffer,"Warning");
1727 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1728 buffer,&len,&index),"Failed to get second header\n");
1729 ok(index == 2, "Index was not incremented\n");
1730 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1731 len = sizeof(buffer);
1732 strcpy(buffer,"Warning");
1733 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1734 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1736 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1738 index = 0;
1739 len = sizeof(buffer);
1740 strcpy(buffer,"Warning");
1741 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1742 buffer,&len,&index),"Unable to query header\n");
1743 ok(index == 1, "Index was not incremented\n");
1744 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1745 len = sizeof(buffer);
1746 strcpy(buffer,"Warning");
1747 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1748 ok(index == 2, "Index was not incremented\n");
1749 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1750 len = sizeof(buffer);
1751 strcpy(buffer,"Warning");
1752 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1754 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1756 index = 0;
1757 len = sizeof(buffer);
1758 strcpy(buffer,"Warning");
1759 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1760 ok(index == 1, "Index was not incremented\n");
1761 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1762 len = sizeof(buffer);
1763 strcpy(buffer,"Warning");
1764 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1765 ok(index == 2, "Index was not incremented\n");
1766 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1767 len = sizeof(buffer);
1768 strcpy(buffer,"Warning");
1769 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1771 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1773 index = 0;
1774 len = sizeof(buffer);
1775 strcpy(buffer,"Warning");
1776 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1777 ok(index == 1, "Index was not incremented\n");
1778 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1779 len = sizeof(buffer);
1780 strcpy(buffer,"Warning");
1781 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1782 ok(index == 2, "Index was not incremented\n");
1783 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1784 len = sizeof(buffer);
1785 strcpy(buffer,"Warning");
1786 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1788 ok(HttpAddRequestHeaders(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");
1790 index = 0;
1791 len = sizeof(buffer);
1792 strcpy(buffer,"Warning");
1793 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1794 ok(index == 1, "Index was not incremented\n");
1795 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1796 len = sizeof(buffer);
1797 strcpy(buffer,"Warning");
1798 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1799 ok(index == 2, "Index was not incremented\n");
1800 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1801 len = sizeof(buffer);
1802 strcpy(buffer,"Warning");
1803 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1805 /* Ensure that blank headers are ignored and don't cause a failure */
1806 ok(HttpAddRequestHeaders(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");
1808 index = 0;
1809 len = sizeof(buffer);
1810 strcpy(buffer,"BlankTest");
1811 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1812 ok(index == 1, "Index was not incremented\n");
1813 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1815 /* Ensure that malformed header separators are ignored and don't cause a failure */
1816 ok(HttpAddRequestHeaders(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1817 "Failed to add header with malformed entries in list\n");
1819 index = 0;
1820 len = sizeof(buffer);
1821 strcpy(buffer,"MalformedTest");
1822 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1823 ok(index == 1, "Index was not incremented\n");
1824 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1825 index = 0;
1826 len = sizeof(buffer);
1827 strcpy(buffer,"MalformedTestTwo");
1828 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1829 ok(index == 1, "Index was not incremented\n");
1830 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1831 index = 0;
1832 len = sizeof(buffer);
1833 strcpy(buffer,"MalformedTestThree");
1834 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1835 ok(index == 1, "Index was not incremented\n");
1836 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1838 ret = HttpAddRequestHeaders(hRequest, "Authorization: Basic\r\n", -1, HTTP_ADDREQ_FLAG_ADD);
1839 ok(ret, "unable to add header %u\n", GetLastError());
1841 index = 0;
1842 buffer[0] = 0;
1843 len = sizeof(buffer);
1844 ret = HttpQueryInfo(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index);
1845 ok(ret, "unable to query header %u\n", GetLastError());
1846 ok(index == 1, "index was not incremented\n");
1847 ok(!strcmp(buffer, "Basic"), "incorrect string was returned (%s)\n", buffer);
1849 ret = HttpAddRequestHeaders(hRequest, "Authorization:\r\n", -1, HTTP_ADDREQ_FLAG_REPLACE);
1850 ok(ret, "unable to remove header %u\n", GetLastError());
1852 index = 0;
1853 len = sizeof(buffer);
1854 SetLastError(0xdeadbeef);
1855 ok(!HttpQueryInfo(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index),
1856 "header still present\n");
1857 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "got %u\n", GetLastError());
1859 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1860 done:
1861 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1862 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1865 static const char garbagemsg[] =
1866 "Garbage: Header\r\n";
1868 static const char contmsg[] =
1869 "HTTP/1.1 100 Continue\r\n";
1871 static const char expandcontmsg[] =
1872 "HTTP/1.1 100 Continue\r\n"
1873 "Server: winecontinue\r\n"
1874 "Tag: something witty\r\n"
1875 "\r\n";
1877 static const char okmsg[] =
1878 "HTTP/1.1 200 OK\r\n"
1879 "Server: winetest\r\n"
1880 "\r\n";
1882 static const char okmsg2[] =
1883 "HTTP/1.1 200 OK\r\n"
1884 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1885 "Server: winetest\r\n"
1886 "Content-Length: 0\r\n"
1887 "Set-Cookie: one\r\n"
1888 "Set-Cookie: two\r\n"
1889 "\r\n";
1891 static const char notokmsg[] =
1892 "HTTP/1.1 400 Bad Request\r\n"
1893 "Server: winetest\r\n"
1894 "\r\n";
1896 static const char noauthmsg[] =
1897 "HTTP/1.1 401 Unauthorized\r\n"
1898 "Server: winetest\r\n"
1899 "Connection: close\r\n"
1900 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1901 "\r\n";
1903 static const char noauthmsg2[] =
1904 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1905 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1906 "\0d`0|6\n"
1907 "Server: winetest\r\n";
1909 static const char proxymsg[] =
1910 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1911 "Server: winetest\r\n"
1912 "Proxy-Connection: close\r\n"
1913 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1914 "\r\n";
1916 static const char page1[] =
1917 "<HTML>\r\n"
1918 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1919 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1920 "</HTML>\r\n\r\n";
1922 static const char ok_with_length[] =
1923 "HTTP/1.1 200 OK\r\n"
1924 "Connection: Keep-Alive\r\n"
1925 "Content-Length: 23\r\n\r\n"
1926 "abc\r\nHTTP/1.1 211 OK\r\n\r\n";
1928 static const char ok_with_length2[] =
1929 "HTTP/1.1 210 OK\r\n"
1930 "Connection: Keep-Alive\r\n"
1931 "Content-Length: 24\r\n\r\n"
1932 "abc\r\nHTTP/1.1 211 OK\r\n\r\n";
1934 struct server_info {
1935 HANDLE hEvent;
1936 int port;
1939 static int test_cache_gzip;
1940 static const char *send_buffer;
1942 static DWORD CALLBACK server_thread(LPVOID param)
1944 struct server_info *si = param;
1945 int r, c, i, on, count = 0;
1946 SOCKET s;
1947 struct sockaddr_in sa;
1948 char buffer[0x100];
1949 WSADATA wsaData;
1950 int last_request = 0;
1951 char host_header[22];
1952 static int test_b = 0;
1953 static int test_no_cache = 0;
1955 WSAStartup(MAKEWORD(1,1), &wsaData);
1957 s = socket(AF_INET, SOCK_STREAM, 0);
1958 if (s == INVALID_SOCKET)
1959 return 1;
1961 on = 1;
1962 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1964 memset(&sa, 0, sizeof sa);
1965 sa.sin_family = AF_INET;
1966 sa.sin_port = htons(si->port);
1967 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1969 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1970 if (r<0)
1971 return 1;
1973 listen(s, 0);
1975 SetEvent(si->hEvent);
1977 sprintf(host_header, "Host: localhost:%d", si->port);
1981 c = accept(s, NULL, NULL);
1983 memset(buffer, 0, sizeof buffer);
1984 for(i=0; i<(sizeof buffer-1); i++)
1986 r = recv(c, &buffer[i], 1, 0);
1987 if (r != 1)
1988 break;
1989 if (i<4) continue;
1990 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1991 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1992 break;
1994 if (strstr(buffer, "GET /test1"))
1996 if (!strstr(buffer, "Content-Length: 0"))
1998 send(c, okmsg, sizeof okmsg-1, 0);
1999 send(c, page1, sizeof page1-1, 0);
2001 else
2002 send(c, notokmsg, sizeof notokmsg-1, 0);
2004 if (strstr(buffer, "/test2"))
2006 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
2008 send(c, okmsg, sizeof okmsg-1, 0);
2009 send(c, page1, sizeof page1-1, 0);
2011 else
2012 send(c, proxymsg, sizeof proxymsg-1, 0);
2014 if (strstr(buffer, "/test3"))
2016 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2017 send(c, okmsg, sizeof okmsg-1, 0);
2018 else
2019 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2021 if (strstr(buffer, "/test4"))
2023 if (strstr(buffer, "Connection: Close"))
2024 send(c, okmsg, sizeof okmsg-1, 0);
2025 else
2026 send(c, notokmsg, sizeof notokmsg-1, 0);
2028 if (strstr(buffer, "POST /test5") ||
2029 strstr(buffer, "RPC_IN_DATA /test5") ||
2030 strstr(buffer, "RPC_OUT_DATA /test5"))
2032 if (strstr(buffer, "Content-Length: 0"))
2034 send(c, okmsg, sizeof okmsg-1, 0);
2035 send(c, page1, sizeof page1-1, 0);
2037 else
2038 send(c, notokmsg, sizeof notokmsg-1, 0);
2040 if (strstr(buffer, "GET /test6"))
2042 send(c, contmsg, sizeof contmsg-1, 0);
2043 send(c, contmsg, sizeof contmsg-1, 0);
2044 send(c, okmsg, sizeof okmsg-1, 0);
2045 send(c, page1, sizeof page1-1, 0);
2047 if (strstr(buffer, "POST /test7"))
2049 if (strstr(buffer, "Content-Length: 100"))
2051 send(c, okmsg, sizeof okmsg-1, 0);
2052 send(c, page1, sizeof page1-1, 0);
2054 else
2055 send(c, notokmsg, sizeof notokmsg-1, 0);
2057 if (strstr(buffer, "/test8"))
2059 if (!strstr(buffer, "Connection: Close") &&
2060 strstr(buffer, "Connection: Keep-Alive") &&
2061 !strstr(buffer, "Cache-Control: no-cache") &&
2062 !strstr(buffer, "Pragma: no-cache") &&
2063 strstr(buffer, host_header))
2064 send(c, okmsg, sizeof okmsg-1, 0);
2065 else
2066 send(c, notokmsg, sizeof notokmsg-1, 0);
2068 if (strstr(buffer, "/test9"))
2070 if (!strstr(buffer, "Connection: Close") &&
2071 !strstr(buffer, "Connection: Keep-Alive") &&
2072 !strstr(buffer, "Cache-Control: no-cache") &&
2073 !strstr(buffer, "Pragma: no-cache") &&
2074 strstr(buffer, host_header))
2075 send(c, okmsg, sizeof okmsg-1, 0);
2076 else
2077 send(c, notokmsg, sizeof notokmsg-1, 0);
2079 if (strstr(buffer, "/testA"))
2081 if (!strstr(buffer, "Connection: Close") &&
2082 !strstr(buffer, "Connection: Keep-Alive") &&
2083 (strstr(buffer, "Cache-Control: no-cache") ||
2084 strstr(buffer, "Pragma: no-cache")) &&
2085 strstr(buffer, host_header))
2086 send(c, okmsg, sizeof okmsg-1, 0);
2087 else
2088 send(c, notokmsg, sizeof notokmsg-1, 0);
2090 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
2092 test_b = 1;
2093 send(c, okmsg, sizeof okmsg-1, 0);
2094 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
2095 send(c, okmsg, sizeof okmsg-1, 0);
2097 if (strstr(buffer, "/testC"))
2099 if (strstr(buffer, "Cookie: cookie=biscuit"))
2100 send(c, okmsg, sizeof okmsg-1, 0);
2101 else
2102 send(c, notokmsg, sizeof notokmsg-1, 0);
2104 if (strstr(buffer, "/testD"))
2106 send(c, okmsg2, sizeof okmsg2-1, 0);
2108 if (strstr(buffer, "/testE"))
2110 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
2112 if (strstr(buffer, "GET /quit"))
2114 send(c, okmsg, sizeof okmsg-1, 0);
2115 send(c, page1, sizeof page1-1, 0);
2116 last_request = 1;
2118 if (strstr(buffer, "GET /testF"))
2120 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
2121 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2122 send(c, contmsg, sizeof contmsg-1, 0);
2123 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2124 send(c, okmsg, sizeof okmsg-1, 0);
2125 send(c, page1, sizeof page1-1, 0);
2127 if (strstr(buffer, "GET /testG"))
2129 send(c, page1, sizeof page1-1, 0);
2132 if (strstr(buffer, "GET /testJ"))
2134 if (count == 0)
2136 count++;
2137 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2139 else
2141 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2142 count = 0;
2146 if (strstr(buffer, "GET /test_no_content"))
2148 static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n";
2149 send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2151 if (strstr(buffer, "GET /test_conn_close"))
2153 static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
2154 send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
2155 WaitForSingleObject(conn_close_event, INFINITE);
2156 trace("closing connection\n");
2158 if (strstr(buffer, "GET /test_cache_control_no_cache"))
2160 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\n\r\nsome content";
2161 if(!test_no_cache++)
2162 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2163 else
2164 send(c, okmsg, sizeof(okmsg)-1, 0);
2166 if (strstr(buffer, "GET /test_cache_control_no_store"))
2168 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: junk, \t No-StOrE\r\n\r\nsome content";
2169 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2171 if (strstr(buffer, "GET /test_cache_gzip"))
2173 static const char gzip_response[] = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\n\r\n"
2174 "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xaf\xca\x2c\x50\x28"
2175 "\x49\x2d\x2e\xe1\x02\x00\x62\x92\xc7\x6c\x0a\x00\x00\x00";
2176 if(!test_cache_gzip++)
2177 send(c, gzip_response, sizeof(gzip_response), 0);
2178 else
2179 send(c, notokmsg, sizeof(notokmsg)-1, 0);
2181 if (strstr(buffer, "GET /send_from_buffer"))
2182 send(c, send_buffer, strlen(send_buffer), 0);
2183 if (strstr(buffer, "/test_cache_control_verb"))
2185 if (!memcmp(buffer, "GET ", sizeof("GET ")-1) &&
2186 !strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2187 else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2188 send(c, notokmsg, sizeof(notokmsg)-1, 0);
2190 if (strstr(buffer, "GET /test_premature_disconnect"))
2191 trace("closing connection\n");
2193 shutdown(c, 2);
2194 closesocket(c);
2195 } while (!last_request);
2197 closesocket(s);
2199 return 0;
2202 static void test_basic_request(int port, const char *verb, const char *url)
2204 HINTERNET hi, hc, hr;
2205 DWORD r, count;
2206 char buffer[0x100];
2208 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2209 ok(hi != NULL, "open failed\n");
2211 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2212 ok(hc != NULL, "connect failed\n");
2214 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
2215 ok(hr != NULL, "HttpOpenRequest failed\n");
2217 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2218 ok(r, "HttpSendRequest failed\n");
2220 count = 0;
2221 memset(buffer, 0, sizeof buffer);
2222 SetLastError(0xdeadbeef);
2223 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2224 ok(r, "InternetReadFile failed %u\n", GetLastError());
2225 ok(count == sizeof page1 - 1, "count was wrong\n");
2226 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
2228 InternetCloseHandle(hr);
2229 InternetCloseHandle(hc);
2230 InternetCloseHandle(hi);
2233 static void test_last_error(int port)
2235 HINTERNET hi, hc, hr;
2236 DWORD error;
2237 BOOL r;
2239 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2240 ok(hi != NULL, "open failed\n");
2242 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2243 ok(hc != NULL, "connect failed\n");
2245 hr = HttpOpenRequest(hc, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2246 ok(hr != NULL, "HttpOpenRequest failed\n");
2248 SetLastError(0xdeadbeef);
2249 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2250 error = GetLastError();
2251 ok(r, "HttpSendRequest failed\n");
2252 ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
2254 InternetCloseHandle(hr);
2255 InternetCloseHandle(hc);
2256 InternetCloseHandle(hi);
2259 static void test_proxy_indirect(int port)
2261 HINTERNET hi, hc, hr;
2262 DWORD r, sz;
2263 char buffer[0x40];
2265 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
2266 ok(hi != NULL, "open failed\n");
2268 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2269 ok(hc != NULL, "connect failed\n");
2271 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
2272 ok(hr != NULL, "HttpOpenRequest failed\n");
2274 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2275 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2277 sz = sizeof buffer;
2278 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
2279 ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
2280 if (!r)
2282 skip("missing proxy header, not testing remaining proxy headers\n");
2283 goto out;
2285 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
2287 test_status_code(hr, 407);
2288 test_request_flags(hr, 0);
2290 sz = sizeof buffer;
2291 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
2292 ok(r, "HttpQueryInfo failed\n");
2293 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
2295 sz = sizeof buffer;
2296 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
2297 ok(r, "HttpQueryInfo failed\n");
2298 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
2300 sz = sizeof buffer;
2301 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
2302 ok(r, "HttpQueryInfo failed\n");
2303 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
2305 sz = sizeof buffer;
2306 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
2307 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
2308 ok(r == FALSE, "HttpQueryInfo failed\n");
2310 out:
2311 InternetCloseHandle(hr);
2312 InternetCloseHandle(hc);
2313 InternetCloseHandle(hi);
2316 static void test_proxy_direct(int port)
2318 HINTERNET hi, hc, hr;
2319 DWORD r, sz, error;
2320 char buffer[0x40], *url;
2321 WCHAR bufferW[0x40];
2322 static CHAR username[] = "mike",
2323 password[] = "1101",
2324 useragent[] = "winetest",
2325 url_fmt[] = "http://test.winehq.org:%u/test2";
2326 static WCHAR usernameW[] = {'m','i','k','e',0},
2327 passwordW[] = {'1','1','0','1',0},
2328 useragentW[] = {'w','i','n','e','t','e','s','t',0};
2330 /* specify proxy type without the proxy and bypass */
2331 SetLastError(0xdeadbeef);
2332 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, NULL, NULL, 0);
2333 error = GetLastError();
2334 ok(error == ERROR_INVALID_PARAMETER ||
2335 broken(error == ERROR_SUCCESS) /* WinXPProSP2 */, "got %u\n", error);
2336 ok(hi == NULL || broken(!!hi) /* WinXPProSP2 */, "open should have failed\n");
2338 sprintf(buffer, "localhost:%d\n", port);
2339 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
2340 ok(hi != NULL, "open failed\n");
2342 /* try connect without authorization */
2343 hc = InternetConnect(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2344 ok(hc != NULL, "connect failed\n");
2346 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
2347 ok(hr != NULL, "HttpOpenRequest failed\n");
2349 sz = 0;
2350 SetLastError(0xdeadbeef);
2351 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_PASSWORD, NULL, &sz);
2352 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2353 ok(!r, "unexpected success\n");
2354 ok(sz == 1, "got %u\n", sz);
2356 sz = 0;
2357 SetLastError(0xdeadbeef);
2358 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, NULL, &sz);
2359 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2360 ok(!r, "unexpected success\n");
2361 ok(sz == 1, "got %u\n", sz);
2363 sz = sizeof(buffer);
2364 SetLastError(0xdeadbeef);
2365 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2366 ok(r, "unexpected failure %u\n", GetLastError());
2367 ok(!sz, "got %u\n", sz);
2369 sz = sizeof(buffer);
2370 SetLastError(0xdeadbeef);
2371 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2372 ok(r, "unexpected failure %u\n", GetLastError());
2373 ok(!sz, "got %u\n", sz);
2375 sz = 0;
2376 SetLastError(0xdeadbeef);
2377 r = InternetQueryOption(hr, INTERNET_OPTION_PASSWORD, NULL, &sz);
2378 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2379 ok(!r, "unexpected success\n");
2380 ok(sz == 1, "got %u\n", sz);
2382 sz = 0;
2383 SetLastError(0xdeadbeef);
2384 r = InternetQueryOption(hr, INTERNET_OPTION_USERNAME, NULL, &sz);
2385 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2386 ok(!r, "unexpected success\n");
2387 ok(sz == 1, "got %u\n", sz);
2389 sz = sizeof(buffer);
2390 SetLastError(0xdeadbeef);
2391 r = InternetQueryOption(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2392 ok(r, "unexpected failure %u\n", GetLastError());
2393 ok(!sz, "got %u\n", sz);
2395 sz = sizeof(buffer);
2396 SetLastError(0xdeadbeef);
2397 r = InternetQueryOption(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2398 ok(r, "unexpected failure %u\n", GetLastError());
2399 ok(!sz, "got %u\n", sz);
2401 sz = 0;
2402 SetLastError(0xdeadbeef);
2403 r = InternetQueryOption(hr, INTERNET_OPTION_URL, NULL, &sz);
2404 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2405 ok(!r, "unexpected success\n");
2406 ok(sz == 34, "got %u\n", sz);
2408 sz = sizeof(buffer);
2409 SetLastError(0xdeadbeef);
2410 r = InternetQueryOption(hr, INTERNET_OPTION_URL, buffer, &sz);
2411 ok(r, "unexpected failure %u\n", GetLastError());
2412 ok(sz == 33, "got %u\n", sz);
2414 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2415 ok(r || broken(!r), "HttpSendRequest failed %u\n", GetLastError());
2416 if (!r)
2418 win_skip("skipping proxy tests on broken wininet\n");
2419 goto done;
2422 test_status_code(hr, 407);
2424 /* set the user + password then try again */
2425 r = InternetSetOption(hi, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2426 ok(!r, "unexpected success\n");
2428 r = InternetSetOption(hc, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2429 ok(r, "failed to set user\n");
2431 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2432 ok(r, "failed to set user\n");
2434 buffer[0] = 0;
2435 sz = 3;
2436 SetLastError(0xdeadbeef);
2437 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2438 ok(!r, "unexpected failure %u\n", GetLastError());
2439 ok(!buffer[0], "got %s\n", buffer);
2440 ok(sz == strlen(username) + 1, "got %u\n", sz);
2442 buffer[0] = 0;
2443 sz = 0;
2444 SetLastError(0xdeadbeef);
2445 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2446 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2447 ok(!r, "unexpected success\n");
2448 ok(sz == strlen(username) + 1, "got %u\n", sz);
2450 bufferW[0] = 0;
2451 sz = 0;
2452 SetLastError(0xdeadbeef);
2453 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_USERNAME, bufferW, &sz);
2454 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2455 ok(!r, "unexpected success\n");
2456 ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2458 buffer[0] = 0;
2459 sz = sizeof(buffer);
2460 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2461 ok(r, "failed to get username\n");
2462 ok(!strcmp(buffer, username), "got %s\n", buffer);
2463 ok(sz == strlen(username), "got %u\n", sz);
2465 buffer[0] = 0;
2466 sz = sizeof(bufferW);
2467 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_USERNAME, bufferW, &sz);
2468 ok(r, "failed to get username\n");
2469 ok(!lstrcmpW(bufferW, usernameW), "wrong username\n");
2470 ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2472 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 1);
2473 ok(r, "failed to set user\n");
2475 buffer[0] = 0;
2476 sz = sizeof(buffer);
2477 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2478 ok(r, "failed to get username\n");
2479 ok(!strcmp(buffer, username), "got %s\n", buffer);
2480 ok(sz == strlen(username), "got %u\n", sz);
2482 r = InternetSetOption(hi, INTERNET_OPTION_USER_AGENT, useragent, 1);
2483 ok(r, "failed to set useragent\n");
2485 buffer[0] = 0;
2486 sz = 0;
2487 SetLastError(0xdeadbeef);
2488 r = InternetQueryOption(hi, INTERNET_OPTION_USER_AGENT, buffer, &sz);
2489 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2490 ok(!r, "unexpected success\n");
2491 ok(sz == strlen(useragent) + 1, "got %u\n", sz);
2493 buffer[0] = 0;
2494 sz = sizeof(buffer);
2495 r = InternetQueryOption(hi, INTERNET_OPTION_USER_AGENT, buffer, &sz);
2496 ok(r, "failed to get user agent\n");
2497 ok(!strcmp(buffer, useragent), "got %s\n", buffer);
2498 ok(sz == strlen(useragent), "got %u\n", sz);
2500 bufferW[0] = 0;
2501 sz = 0;
2502 SetLastError(0xdeadbeef);
2503 r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2504 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2505 ok(!r, "unexpected success\n");
2506 ok(sz == (lstrlenW(useragentW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2508 bufferW[0] = 0;
2509 sz = sizeof(bufferW);
2510 r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2511 ok(r, "failed to get user agent\n");
2512 ok(!lstrcmpW(bufferW, useragentW), "wrong user agent\n");
2513 ok(sz == lstrlenW(useragentW), "got %u\n", sz);
2515 r = InternetSetOption(hr, INTERNET_OPTION_USERNAME, username, 1);
2516 ok(r, "failed to set user\n");
2518 buffer[0] = 0;
2519 sz = 0;
2520 SetLastError(0xdeadbeef);
2521 r = InternetQueryOption(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2522 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2523 ok(!r, "unexpected success\n");
2524 ok(sz == strlen(username) + 1, "got %u\n", sz);
2526 buffer[0] = 0;
2527 sz = sizeof(buffer);
2528 r = InternetQueryOption(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2529 ok(r, "failed to get user\n");
2530 ok(!strcmp(buffer, username), "got %s\n", buffer);
2531 ok(sz == strlen(username), "got %u\n", sz);
2533 bufferW[0] = 0;
2534 sz = 0;
2535 SetLastError(0xdeadbeef);
2536 r = InternetQueryOptionW(hr, INTERNET_OPTION_USERNAME, bufferW, &sz);
2537 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2538 ok(!r, "unexpected success\n");
2539 ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2541 bufferW[0] = 0;
2542 sz = sizeof(bufferW);
2543 r = InternetQueryOptionW(hr, INTERNET_OPTION_USERNAME, bufferW, &sz);
2544 ok(r, "failed to get user\n");
2545 ok(!lstrcmpW(bufferW, usernameW), "wrong user\n");
2546 ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2548 r = InternetSetOption(hr, INTERNET_OPTION_PASSWORD, password, 1);
2549 ok(r, "failed to set password\n");
2551 buffer[0] = 0;
2552 sz = 0;
2553 SetLastError(0xdeadbeef);
2554 r = InternetQueryOption(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2555 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2556 ok(!r, "unexpected success\n");
2557 ok(sz == strlen(password) + 1, "got %u\n", sz);
2559 buffer[0] = 0;
2560 sz = sizeof(buffer);
2561 r = InternetQueryOption(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2562 ok(r, "failed to get password\n");
2563 ok(!strcmp(buffer, password), "got %s\n", buffer);
2564 ok(sz == strlen(password), "got %u\n", sz);
2566 bufferW[0] = 0;
2567 sz = 0;
2568 SetLastError(0xdeadbeef);
2569 r = InternetQueryOptionW(hr, INTERNET_OPTION_PASSWORD, bufferW, &sz);
2570 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2571 ok(!r, "unexpected success\n");
2572 ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2574 bufferW[0] = 0;
2575 sz = sizeof(bufferW);
2576 r = InternetQueryOptionW(hr, INTERNET_OPTION_PASSWORD, bufferW, &sz);
2577 ok(r, "failed to get password\n");
2578 ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2579 ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2581 url = HeapAlloc(GetProcessHeap(), 0, strlen(url_fmt) + 11);
2582 sprintf(url, url_fmt, port);
2583 buffer[0] = 0;
2584 sz = 0;
2585 SetLastError(0xdeadbeef);
2586 r = InternetQueryOption(hr, INTERNET_OPTION_URL, buffer, &sz);
2587 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2588 ok(!r, "unexpected success\n");
2589 ok(sz == strlen(url) + 1, "got %u\n", sz);
2591 buffer[0] = 0;
2592 sz = sizeof(buffer);
2593 r = InternetQueryOption(hr, INTERNET_OPTION_URL, buffer, &sz);
2594 ok(r, "failed to get url\n");
2595 ok(!strcmp(buffer, url), "got %s\n", buffer);
2596 ok(sz == strlen(url), "got %u\n", sz);
2598 bufferW[0] = 0;
2599 sz = 0;
2600 SetLastError(0xdeadbeef);
2601 r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2602 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2603 ok(!r, "unexpected success\n");
2604 ok(sz == (strlen(url) + 1) * sizeof(WCHAR), "got %u\n", sz);
2606 bufferW[0] = 0;
2607 sz = sizeof(bufferW);
2608 r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2609 ok(r, "failed to get url\n");
2610 ok(!strcmp_wa(bufferW, url), "wrong url\n");
2611 ok(sz == strlen(url), "got %u\n", sz);
2612 HeapFree(GetProcessHeap(), 0, url);
2614 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
2615 ok(r, "failed to set password\n");
2617 buffer[0] = 0;
2618 sz = 0;
2619 SetLastError(0xdeadbeef);
2620 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2621 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2622 ok(!r, "unexpected success\n");
2623 ok(sz == strlen(password) + 1, "got %u\n", sz);
2625 buffer[0] = 0;
2626 sz = sizeof(buffer);
2627 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2628 ok(r, "failed to get password\n");
2629 ok(!strcmp(buffer, password), "got %s\n", buffer);
2630 ok(sz == strlen(password), "got %u\n", sz);
2632 bufferW[0] = 0;
2633 sz = 0;
2634 SetLastError(0xdeadbeef);
2635 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_PASSWORD, bufferW, &sz);
2636 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2637 ok(!r, "unexpected success\n");
2638 ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2640 bufferW[0] = 0;
2641 sz = sizeof(bufferW);
2642 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_PASSWORD, bufferW, &sz);
2643 ok(r, "failed to get password\n");
2644 ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2645 ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2647 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2648 if (!r)
2650 win_skip("skipping proxy tests on broken wininet\n");
2651 goto done;
2653 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2654 sz = sizeof buffer;
2655 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
2656 ok(r, "HttpQueryInfo failed\n");
2657 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
2659 done:
2660 InternetCloseHandle(hr);
2661 InternetCloseHandle(hc);
2662 InternetCloseHandle(hi);
2665 static void test_header_handling_order(int port)
2667 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
2668 static char connection[] = "Connection: Close";
2670 static const char *types[2] = { "*", NULL };
2671 HINTERNET session, connect, request;
2672 DWORD size, status;
2673 BOOL ret;
2675 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2676 ok(session != NULL, "InternetOpen failed\n");
2678 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2679 ok(connect != NULL, "InternetConnect failed\n");
2681 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2682 ok(request != NULL, "HttpOpenRequest failed\n");
2684 ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
2685 ok(ret, "HttpAddRequestHeaders failed\n");
2687 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2688 ok(ret, "HttpSendRequest failed\n");
2690 test_status_code(request, 200);
2691 test_request_flags(request, 0);
2693 InternetCloseHandle(request);
2695 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2696 ok(request != NULL, "HttpOpenRequest failed\n");
2698 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2699 ok(ret, "HttpSendRequest failed\n");
2701 status = 0;
2702 size = sizeof(status);
2703 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2704 ok(ret, "HttpQueryInfo failed\n");
2705 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2707 InternetCloseHandle(request);
2709 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2710 ok(request != NULL, "HttpOpenRequest failed\n");
2712 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2713 ok(ret, "HttpAddRequestHeaders failed\n");
2715 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2716 ok(ret, "HttpSendRequest failed\n");
2718 status = 0;
2719 size = sizeof(status);
2720 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2721 ok(ret, "HttpQueryInfo failed\n");
2722 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2724 InternetCloseHandle(request);
2725 InternetCloseHandle(connect);
2726 InternetCloseHandle(session);
2729 static void test_connection_header(int port)
2731 HINTERNET ses, con, req;
2732 BOOL ret;
2734 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2735 ok(ses != NULL, "InternetOpen failed\n");
2737 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2738 ok(con != NULL, "InternetConnect failed\n");
2740 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2741 ok(req != NULL, "HttpOpenRequest failed\n");
2743 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2744 ok(ret, "HttpSendRequest failed\n");
2746 test_status_code(req, 200);
2748 InternetCloseHandle(req);
2750 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
2751 ok(req != NULL, "HttpOpenRequest failed\n");
2753 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2754 ok(ret, "HttpSendRequest failed\n");
2756 test_status_code(req, 200);
2758 InternetCloseHandle(req);
2760 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2761 ok(req != NULL, "HttpOpenRequest failed\n");
2763 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2764 ok(ret, "HttpSendRequest failed\n");
2766 test_status_code(req, 200);
2768 InternetCloseHandle(req);
2770 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2771 ok(req != NULL, "HttpOpenRequest failed\n");
2773 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2774 ok(ret, "HttpSendRequest failed\n");
2776 test_status_code(req, 200);
2778 InternetCloseHandle(req);
2779 InternetCloseHandle(con);
2780 InternetCloseHandle(ses);
2783 static void test_http1_1(int port)
2785 HINTERNET ses, con, req;
2786 BOOL ret;
2788 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2789 ok(ses != NULL, "InternetOpen failed\n");
2791 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2792 ok(con != NULL, "InternetConnect failed\n");
2794 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2795 ok(req != NULL, "HttpOpenRequest failed\n");
2797 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2798 if (ret)
2800 InternetCloseHandle(req);
2802 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2803 ok(req != NULL, "HttpOpenRequest failed\n");
2805 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2806 ok(ret, "HttpSendRequest failed\n");
2809 InternetCloseHandle(req);
2810 InternetCloseHandle(con);
2811 InternetCloseHandle(ses);
2814 static void test_connection_closing(int port)
2816 HINTERNET session, connection, req;
2817 DWORD res;
2819 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2821 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
2822 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
2824 pInternetSetStatusCallbackA(session, callback);
2826 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2827 connection = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
2828 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
2829 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2831 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2832 req = HttpOpenRequestA(connection, "GET", "/testJ", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0xdeadbeaf);
2833 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2834 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2836 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2837 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
2838 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2839 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2840 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2841 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2842 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2843 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2844 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
2845 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
2846 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2848 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
2849 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2850 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2851 WaitForSingleObject(hCompleteEvent, INFINITE);
2852 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2854 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2855 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
2856 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2857 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2858 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2859 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2860 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2861 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2862 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
2863 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
2864 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2866 test_status_code(req, 200);
2868 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2869 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
2870 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2871 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2872 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2873 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2874 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2875 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2876 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2878 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
2879 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2880 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2881 WaitForSingleObject(hCompleteEvent, INFINITE);
2882 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2884 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2885 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
2886 todo_wine CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2887 todo_wine CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2888 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2889 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2890 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2891 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2892 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2894 test_status_code_todo(req, 210);
2896 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2897 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
2898 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2899 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2900 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2901 SET_WINE_ALLOW(INTERNET_STATUS_SENDING_REQUEST);
2902 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2903 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2904 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2905 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
2906 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
2907 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2909 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
2910 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2911 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2912 WaitForSingleObject(hCompleteEvent, INFINITE);
2913 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2915 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2916 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
2917 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2918 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2919 todo_wine CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2920 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2921 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2922 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2923 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
2924 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
2925 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2927 test_status_code_todo(req, 200);
2929 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
2930 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
2932 close_async_handle(session, hCompleteEvent, 2);
2933 CloseHandle(hCompleteEvent);
2937 static void test_no_content(int port)
2939 HINTERNET session, connection, req;
2940 DWORD res;
2942 trace("Testing 204 no content response...\n");
2944 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2946 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
2947 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
2949 pInternetSetStatusCallbackA(session, callback);
2951 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2952 connection = InternetConnectA(session, "localhost", port,
2953 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
2954 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
2955 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2957 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2958 req = HttpOpenRequestA(connection, "GET", "/test_no_content", NULL, NULL, NULL,
2959 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
2960 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2961 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2963 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2964 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2965 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2966 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2967 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2968 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2969 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2970 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
2971 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
2972 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2974 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
2975 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2976 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2977 WaitForSingleObject(hCompleteEvent, INFINITE);
2978 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2980 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2981 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2982 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2983 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2984 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2985 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2986 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2987 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
2988 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
2989 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2991 close_async_handle(session, hCompleteEvent, 2);
2992 CloseHandle(hCompleteEvent);
2995 static void test_conn_close(int port)
2997 HINTERNET session, connection, req;
2998 DWORD res, avail, size;
2999 BYTE buf[1024];
3001 trace("Testing connection close connection...\n");
3003 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3004 conn_close_event = CreateEvent(NULL, FALSE, FALSE, NULL);
3006 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
3007 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3009 pInternetSetStatusCallbackA(session, callback);
3011 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3012 connection = InternetConnectA(session, "localhost", port,
3013 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3014 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
3015 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3017 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3018 req = HttpOpenRequestA(connection, "GET", "/test_conn_close", NULL, NULL, NULL,
3019 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3020 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
3021 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3023 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3024 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3025 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3026 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3027 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3028 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3029 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3030 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3032 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
3033 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3034 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3035 WaitForSingleObject(hCompleteEvent, INFINITE);
3036 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3038 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3039 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3040 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3041 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3042 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3043 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3044 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3045 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3047 avail = 0;
3048 res = InternetQueryDataAvailable(req, &avail, 0, 0);
3049 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
3050 ok(avail != 0, "avail = 0\n");
3052 size = 0;
3053 res = InternetReadFile(req, buf, avail, &size);
3054 ok(res, "InternetReadFile failed: %u\n", GetLastError());
3056 res = InternetQueryDataAvailable(req, &avail, 0, 0);
3057 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3058 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3059 ok(!avail, "avail = %u, expected 0\n", avail);
3061 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
3062 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
3063 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3064 SetEvent(conn_close_event);
3065 WaitForSingleObject(hCompleteEvent, INFINITE);
3066 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3067 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3068 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3069 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3071 close_async_handle(session, hCompleteEvent, 2);
3072 CloseHandle(hCompleteEvent);
3075 static void test_no_cache(int port)
3077 static const char cache_control_no_cache[] = "/test_cache_control_no_cache";
3078 static const char cache_control_no_store[] = "/test_cache_control_no_store";
3079 static const char cache_url_fmt[] = "http://localhost:%d%s";
3081 char cache_url[256], buf[256];
3082 HINTERNET ses, con, req;
3083 DWORD read, size;
3084 BOOL ret;
3086 trace("Testing no-cache header\n");
3088 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3089 ok(ses != NULL,"InternetOpen failed with error %u\n", GetLastError());
3091 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3092 ok(con != NULL, "InternetConnect failed with error %u\n", GetLastError());
3094 req = HttpOpenRequest(con, NULL, cache_control_no_cache, NULL, NULL, NULL, 0, 0);
3095 ok(req != NULL, "HttpOpenRequest failed\n");
3097 sprintf(cache_url, cache_url_fmt, port, cache_control_no_cache);
3098 DeleteUrlCacheEntry(cache_url);
3100 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
3101 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3102 size = 0;
3103 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3104 size += read;
3105 ok(size == 12, "read %d bytes of data\n", size);
3106 InternetCloseHandle(req);
3108 req = HttpOpenRequest(con, NULL, cache_control_no_cache, NULL, NULL, NULL, 0, 0);
3109 ok(req != NULL, "HttpOpenRequest failed\n");
3111 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
3112 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3113 size = 0;
3114 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3115 size += read;
3116 ok(size == 0, "read %d bytes of data\n", size);
3117 InternetCloseHandle(req);
3118 DeleteUrlCacheEntry(cache_url);
3120 req = HttpOpenRequest(con, NULL, cache_control_no_store, NULL, NULL, NULL, 0, 0);
3121 ok(req != NULL, "HttpOpenRequest failed\n");
3123 sprintf(cache_url, cache_url_fmt, port, cache_control_no_store);
3124 DeleteUrlCacheEntry(cache_url);
3126 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
3127 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3128 size = 0;
3129 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3130 size += read;
3131 ok(size == 12, "read %d bytes of data\n", size);
3132 InternetCloseHandle(req);
3134 ret = DeleteUrlCacheEntry(cache_url);
3135 ok(!ret && GetLastError()==ERROR_FILE_NOT_FOUND, "cache entry should not exist\n");
3137 InternetCloseHandle(con);
3138 InternetCloseHandle(ses);
3141 static void test_cache_read_gzipped(int port)
3143 static const char cache_url_fmt[] = "http://localhost:%d%s";
3144 static const char get_gzip[] = "/test_cache_gzip";
3145 static const char content[] = "gzip test\n";
3146 static const char text_html[] = "text/html";
3147 static const char raw_header[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
3149 HINTERNET ses, con, req;
3150 DWORD read, size;
3151 char cache_url[256], buf[256];
3152 BOOL ret;
3154 trace("Testing reading compressed content from cache\n");
3156 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3157 ok(ses != NULL,"InternetOpen failed with error %u\n", GetLastError());
3159 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3160 ok(con != NULL, "InternetConnect failed with error %u\n", GetLastError());
3162 req = HttpOpenRequest(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3163 ok(req != NULL, "HttpOpenRequest failed\n");
3165 ret = TRUE;
3166 ret = InternetSetOption(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3167 if(!ret && GetLastError()==ERROR_INTERNET_INVALID_OPTION) {
3168 win_skip("INTERNET_OPTION_HTTP_DECODING not supported\n");
3169 InternetCloseHandle(req);
3170 InternetCloseHandle(con);
3171 InternetCloseHandle(ses);
3172 return;
3174 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3176 ret = HttpSendRequest(req, "Accept-Encoding: gzip", -1, NULL, 0);
3177 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3178 size = 0;
3179 while(InternetReadFile(req, buf+size, sizeof(buf)-size, &read) && read)
3180 size += read;
3181 ok(size == 10, "read %d bytes of data\n", size);
3182 buf[size] = 0;
3183 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3185 size = sizeof(buf)-1;
3186 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_TYPE, buf, &size, 0);
3187 ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3188 buf[size] = 0;
3189 ok(!strncmp(text_html, buf, size), "buf = %s\n", buf);
3191 size = sizeof(buf)-1;
3192 ret = HttpQueryInfoA(req, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &size, 0);
3193 ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3194 buf[size] = 0;
3195 ok(!strncmp(raw_header, buf, size), "buf = %s\n", buf);
3196 InternetCloseHandle(req);
3198 req = HttpOpenRequest(con, NULL, get_gzip, NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
3199 ok(req != NULL, "HttpOpenRequest failed\n");
3201 ret = TRUE;
3202 ret = InternetSetOption(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3203 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3205 ret = HttpSendRequest(req, "Accept-Encoding: gzip", -1, NULL, 0);
3206 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3207 size = 0;
3208 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3209 size += read;
3210 todo_wine ok(size == 10, "read %d bytes of data\n", size);
3211 buf[size] = 0;
3212 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3214 size = sizeof(buf);
3215 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
3216 ok(!ret && GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND,
3217 "HttpQueryInfo(HTTP_QUERY_CONTENT_ENCODING) returned %d, %d\n",
3218 ret, GetLastError());
3220 size = sizeof(buf)-1;
3221 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_TYPE, buf, &size, 0);
3222 todo_wine ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3223 buf[size] = 0;
3224 todo_wine ok(!strncmp(text_html, buf, size), "buf = %s\n", buf);
3225 InternetCloseHandle(req);
3227 /* Decompression doesn't work while reading from cache */
3228 test_cache_gzip = 0;
3229 sprintf(cache_url, cache_url_fmt, port, get_gzip);
3230 DeleteUrlCacheEntry(cache_url);
3232 req = HttpOpenRequest(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3233 ok(req != NULL, "HttpOpenRequest failed\n");
3235 ret = HttpSendRequest(req, "Accept-Encoding: gzip", -1, NULL, 0);
3236 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3237 size = 0;
3238 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3239 size += read;
3240 ok(size == 31, "read %d bytes of data\n", size);
3241 InternetCloseHandle(req);
3243 req = HttpOpenRequest(con, NULL, get_gzip, NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
3244 ok(req != NULL, "HttpOpenRequest failed\n");
3246 ret = TRUE;
3247 ret = InternetSetOption(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3248 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3250 ret = HttpSendRequest(req, "Accept-Encoding: gzip", -1, NULL, 0);
3251 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3252 size = 0;
3253 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3254 size += read;
3255 todo_wine ok(size == 31, "read %d bytes of data\n", size);
3257 size = sizeof(buf);
3258 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
3259 todo_wine ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_ENCODING) failed: %d\n", GetLastError());
3260 InternetCloseHandle(req);
3262 InternetCloseHandle(con);
3263 InternetCloseHandle(ses);
3265 DeleteUrlCacheEntry(cache_url);
3268 static void test_HttpSendRequestW(int port)
3270 static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
3271 HINTERNET ses, con, req;
3272 DWORD error;
3273 BOOL ret;
3275 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3276 ok(ses != NULL, "InternetOpen failed\n");
3278 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3279 ok(con != NULL, "InternetConnect failed\n");
3281 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
3282 ok(req != NULL, "HttpOpenRequest failed\n");
3284 SetLastError(0xdeadbeef);
3285 ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
3286 error = GetLastError();
3287 ok(!ret, "HttpSendRequestW succeeded\n");
3288 ok(error == ERROR_IO_PENDING ||
3289 broken(error == ERROR_HTTP_HEADER_NOT_FOUND) || /* IE6 */
3290 broken(error == ERROR_INVALID_PARAMETER), /* IE5 */
3291 "got %u expected ERROR_IO_PENDING\n", error);
3293 InternetCloseHandle(req);
3294 InternetCloseHandle(con);
3295 InternetCloseHandle(ses);
3298 static void test_cookie_header(int port)
3300 HINTERNET ses, con, req;
3301 DWORD size, error;
3302 BOOL ret;
3303 char buffer[64];
3305 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3306 ok(ses != NULL, "InternetOpen failed\n");
3308 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3309 ok(con != NULL, "InternetConnect failed\n");
3311 InternetSetCookie("http://localhost", "cookie", "biscuit");
3313 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3314 ok(req != NULL, "HttpOpenRequest failed\n");
3316 buffer[0] = 0;
3317 size = sizeof(buffer);
3318 SetLastError(0xdeadbeef);
3319 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3320 error = GetLastError();
3321 ok(!ret, "HttpQueryInfo succeeded\n");
3322 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
3324 ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
3325 ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
3327 buffer[0] = 0;
3328 size = sizeof(buffer);
3329 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3330 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
3331 ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
3333 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
3334 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
3336 test_status_code(req, 200);
3338 buffer[0] = 0;
3339 size = sizeof(buffer);
3340 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3341 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
3342 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
3344 InternetCloseHandle(req);
3345 InternetCloseHandle(con);
3346 InternetCloseHandle(ses);
3349 static void test_basic_authentication(int port)
3351 HINTERNET session, connect, request;
3352 BOOL ret;
3354 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3355 ok(session != NULL, "InternetOpen failed\n");
3357 connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
3358 ok(connect != NULL, "InternetConnect failed\n");
3360 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
3361 ok(request != NULL, "HttpOpenRequest failed\n");
3363 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3364 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3366 test_status_code(request, 200);
3367 test_request_flags(request, 0);
3369 InternetCloseHandle(request);
3370 InternetCloseHandle(connect);
3371 InternetCloseHandle(session);
3374 static void test_premature_disconnect(int port)
3376 HINTERNET session, connect, request;
3377 DWORD err;
3378 BOOL ret;
3380 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3381 ok(session != NULL, "InternetOpen failed\n");
3383 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3384 ok(connect != NULL, "InternetConnect failed\n");
3386 request = HttpOpenRequest(connect, NULL, "/premature_disconnect", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3387 ok(request != NULL, "HttpOpenRequest failed\n");
3389 SetLastError(0xdeadbeef);
3390 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3391 err = GetLastError();
3392 todo_wine ok(!ret, "HttpSendRequest succeeded\n");
3393 todo_wine ok(err == ERROR_HTTP_INVALID_SERVER_RESPONSE, "got %u\n", err);
3395 InternetCloseHandle(request);
3396 InternetCloseHandle(connect);
3397 InternetCloseHandle(session);
3400 static void test_invalid_response_headers(int port)
3402 HINTERNET session, connect, request;
3403 DWORD size;
3404 BOOL ret;
3405 char buffer[256];
3407 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3408 ok(session != NULL, "InternetOpen failed\n");
3410 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3411 ok(connect != NULL, "InternetConnect failed\n");
3413 request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
3414 ok(request != NULL, "HttpOpenRequest failed\n");
3416 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3417 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3419 test_status_code(request, 401);
3420 test_request_flags(request, 0);
3422 buffer[0] = 0;
3423 size = sizeof(buffer);
3424 ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
3425 ok(ret, "HttpQueryInfo failed\n");
3426 ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
3427 "headers wrong \"%s\"\n", buffer);
3429 buffer[0] = 0;
3430 size = sizeof(buffer);
3431 ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
3432 ok(ret, "HttpQueryInfo failed\n");
3433 ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
3435 InternetCloseHandle(request);
3436 InternetCloseHandle(connect);
3437 InternetCloseHandle(session);
3440 static void test_response_without_headers(int port)
3442 HINTERNET hi, hc, hr;
3443 DWORD r, count, size;
3444 char buffer[1024];
3446 SetLastError(0xdeadbeef);
3447 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3448 ok(hi != NULL, "open failed %u\n", GetLastError());
3450 SetLastError(0xdeadbeef);
3451 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3452 ok(hc != NULL, "connect failed %u\n", GetLastError());
3454 SetLastError(0xdeadbeef);
3455 hr = HttpOpenRequest(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
3456 ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
3458 test_request_flags(hr, INTERNET_REQFLAG_NO_HEADERS);
3460 SetLastError(0xdeadbeef);
3461 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
3462 ok(r, "HttpSendRequest failed %u\n", GetLastError());
3464 test_request_flags_todo(hr, INTERNET_REQFLAG_NO_HEADERS);
3466 count = 0;
3467 memset(buffer, 0, sizeof buffer);
3468 SetLastError(0xdeadbeef);
3469 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
3470 ok(r, "InternetReadFile failed %u\n", GetLastError());
3471 todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
3472 todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
3474 test_status_code(hr, 200);
3475 test_request_flags_todo(hr, INTERNET_REQFLAG_NO_HEADERS);
3477 buffer[0] = 0;
3478 size = sizeof(buffer);
3479 SetLastError(0xdeadbeef);
3480 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
3481 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
3482 ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
3484 buffer[0] = 0;
3485 size = sizeof(buffer);
3486 SetLastError(0xdeadbeef);
3487 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
3488 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
3489 ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
3491 buffer[0] = 0;
3492 size = sizeof(buffer);
3493 SetLastError(0xdeadbeef);
3494 r = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
3495 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
3496 ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
3498 InternetCloseHandle(hr);
3499 InternetCloseHandle(hc);
3500 InternetCloseHandle(hi);
3503 static void test_HttpQueryInfo(int port)
3505 HINTERNET hi, hc, hr;
3506 DWORD size, index;
3507 char buffer[1024];
3508 BOOL ret;
3510 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3511 ok(hi != NULL, "InternetOpen failed\n");
3513 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3514 ok(hc != NULL, "InternetConnect failed\n");
3516 hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
3517 ok(hr != NULL, "HttpOpenRequest failed\n");
3519 size = sizeof(buffer);
3520 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
3521 ok(!ret && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed %u\n", GetLastError());
3523 ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
3524 ok(ret, "HttpSendRequest failed\n");
3526 index = 0;
3527 size = sizeof(buffer);
3528 ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
3529 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3530 ok(index == 1, "expected 1 got %u\n", index);
3532 index = 0;
3533 size = sizeof(buffer);
3534 ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
3535 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3536 ok(index == 1, "expected 1 got %u\n", index);
3538 index = 0;
3539 size = sizeof(buffer);
3540 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
3541 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3542 ok(index == 0, "expected 0 got %u\n", index);
3544 size = sizeof(buffer);
3545 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
3546 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3547 ok(index == 0, "expected 0 got %u\n", index);
3549 index = 0xdeadbeef; /* invalid start index */
3550 size = sizeof(buffer);
3551 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
3552 todo_wine ok(!ret, "HttpQueryInfo should have failed\n");
3553 todo_wine ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
3554 "Expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
3556 index = 0;
3557 size = sizeof(buffer);
3558 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
3559 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3560 ok(index == 0, "expected 0 got %u\n", index);
3562 size = sizeof(buffer);
3563 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
3564 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3565 ok(index == 0, "expected 0 got %u\n", index);
3567 size = sizeof(buffer);
3568 ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
3569 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3570 ok(index == 0, "expected 0 got %u\n", index);
3572 test_status_code(hr, 200);
3574 index = 0xdeadbeef;
3575 size = sizeof(buffer);
3576 ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
3577 ok(!ret, "HttpQueryInfo succeeded\n");
3578 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
3580 index = 0;
3581 size = sizeof(buffer);
3582 ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
3583 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3584 ok(index == 1, "expected 1 got %u\n", index);
3586 index = 0;
3587 size = sizeof(buffer);
3588 strcpy(buffer, "Server");
3589 ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
3590 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3591 ok(index == 1, "expected 1 got %u\n", index);
3593 index = 0;
3594 size = sizeof(buffer);
3595 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
3596 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3597 ok(index == 1, "expected 1 got %u\n", index);
3599 size = sizeof(buffer);
3600 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
3601 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3602 ok(index == 2, "expected 2 got %u\n", index);
3604 InternetCloseHandle(hr);
3605 InternetCloseHandle(hc);
3606 InternetCloseHandle(hi);
3609 static void test_options(int port)
3611 INTERNET_DIAGNOSTIC_SOCKET_INFO idsi;
3612 HINTERNET ses, con, req;
3613 DWORD size, error;
3614 DWORD_PTR ctx;
3615 BOOL ret;
3617 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3618 ok(ses != NULL, "InternetOpen failed\n");
3620 SetLastError(0xdeadbeef);
3621 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, 0);
3622 error = GetLastError();
3623 ok(!ret, "InternetSetOption succeeded\n");
3624 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3626 SetLastError(0xdeadbeef);
3627 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, sizeof(ctx));
3628 ok(!ret, "InternetSetOption succeeded\n");
3629 error = GetLastError();
3630 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3632 SetLastError(0xdeadbeef);
3633 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, 0);
3634 ok(!ret, "InternetSetOption succeeded\n");
3635 error = GetLastError();
3636 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3638 ctx = 1;
3639 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
3640 ok(ret, "InternetSetOption failed %u\n", GetLastError());
3642 SetLastError(0xdeadbeef);
3643 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, NULL);
3644 error = GetLastError();
3645 ok(!ret, "InternetQueryOption succeeded\n");
3646 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3648 SetLastError(0xdeadbeef);
3649 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, NULL);
3650 error = GetLastError();
3651 ok(!ret, "InternetQueryOption succeeded\n");
3652 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3654 size = 0;
3655 SetLastError(0xdeadbeef);
3656 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, &size);
3657 error = GetLastError();
3658 ok(!ret, "InternetQueryOption succeeded\n");
3659 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
3661 size = sizeof(ctx);
3662 SetLastError(0xdeadbeef);
3663 ret = InternetQueryOption(NULL, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3664 error = GetLastError();
3665 ok(!ret, "InternetQueryOption succeeded\n");
3666 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %u\n", error);
3668 ctx = 0xdeadbeef;
3669 size = sizeof(ctx);
3670 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3671 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3672 ok(ctx == 1, "expected 1 got %lu\n", ctx);
3674 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3675 ok(con != NULL, "InternetConnect failed\n");
3677 ctx = 0xdeadbeef;
3678 size = sizeof(ctx);
3679 ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3680 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3681 ok(ctx == 0, "expected 0 got %lu\n", ctx);
3683 ctx = 2;
3684 ret = InternetSetOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
3685 ok(ret, "InternetSetOption failed %u\n", GetLastError());
3687 ctx = 0xdeadbeef;
3688 size = sizeof(ctx);
3689 ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3690 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3691 ok(ctx == 2, "expected 2 got %lu\n", ctx);
3693 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
3694 ok(req != NULL, "HttpOpenRequest failed\n");
3696 ctx = 0xdeadbeef;
3697 size = sizeof(ctx);
3698 ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3699 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3700 ok(ctx == 0, "expected 0 got %lu\n", ctx);
3702 ctx = 3;
3703 ret = InternetSetOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
3704 ok(ret, "InternetSetOption failed %u\n", GetLastError());
3706 ctx = 0xdeadbeef;
3707 size = sizeof(ctx);
3708 ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3709 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3710 ok(ctx == 3, "expected 3 got %lu\n", ctx);
3712 size = sizeof(idsi);
3713 ret = InternetQueryOption(req, INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, &idsi, &size);
3714 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3716 size = 0;
3717 SetLastError(0xdeadbeef);
3718 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
3719 error = GetLastError();
3720 ok(!ret, "InternetQueryOption succeeded\n");
3721 ok(error == ERROR_INTERNET_INVALID_OPERATION, "expected ERROR_INTERNET_INVALID_OPERATION, got %u\n", error);
3723 /* INTERNET_OPTION_PROXY */
3724 SetLastError(0xdeadbeef);
3725 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, NULL);
3726 error = GetLastError();
3727 ok(!ret, "InternetQueryOption succeeded\n");
3728 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3730 SetLastError(0xdeadbeef);
3731 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, &ctx, NULL);
3732 error = GetLastError();
3733 ok(!ret, "InternetQueryOption succeeded\n");
3734 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3736 size = 0;
3737 SetLastError(0xdeadbeef);
3738 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, &size);
3739 error = GetLastError();
3740 ok(!ret, "InternetQueryOption succeeded\n");
3741 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
3742 ok(size >= sizeof(INTERNET_PROXY_INFOA), "expected size to be greater or equal to the struct size\n");
3744 InternetCloseHandle(req);
3745 InternetCloseHandle(con);
3746 InternetCloseHandle(ses);
3749 typedef struct {
3750 const char *response_text;
3751 int status_code;
3752 const char *status_text;
3753 const char *raw_headers;
3754 } http_status_test_t;
3756 static const http_status_test_t http_status_tests[] = {
3758 "HTTP/1.1 200 OK\r\n"
3759 "Content-Length: 1\r\n"
3760 "\r\nx",
3761 200,
3762 "OK"
3765 "HTTP/1.1 404 Fail\r\n"
3766 "Content-Length: 1\r\n"
3767 "\r\nx",
3768 404,
3769 "Fail"
3772 "HTTP/1.1 200\r\n"
3773 "Content-Length: 1\r\n"
3774 "\r\nx",
3775 200,
3779 "HTTP/1.1 410 \r\n"
3780 "Content-Length: 1\r\n"
3781 "\r\nx",
3782 410,
3787 static void test_http_status(int port)
3789 HINTERNET ses, con, req;
3790 char buf[1000];
3791 DWORD i, size;
3792 BOOL res;
3794 for(i=0; i < sizeof(http_status_tests)/sizeof(*http_status_tests); i++) {
3795 send_buffer = http_status_tests[i].response_text;
3797 ses = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3798 ok(ses != NULL, "InternetOpen failed\n");
3800 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3801 ok(con != NULL, "InternetConnect failed\n");
3803 req = HttpOpenRequest(con, NULL, "/send_from_buffer", NULL, NULL, NULL, 0, 0);
3804 ok(req != NULL, "HttpOpenRequest failed\n");
3806 res = HttpSendRequest(req, NULL, 0, NULL, 0);
3807 ok(res, "HttpSendRequest failed\n");
3809 test_status_code(req, http_status_tests[i].status_code);
3811 size = sizeof(buf);
3812 res = HttpQueryInfo(req, HTTP_QUERY_STATUS_TEXT, buf, &size, NULL);
3813 ok(res, "HttpQueryInfo failed: %u\n", GetLastError());
3814 ok(!strcmp(buf, http_status_tests[i].status_text), "[%u] Unexpected status text \"%s\", expected \"%s\"\n",
3815 i, buf, http_status_tests[i].status_text);
3817 InternetCloseHandle(req);
3818 InternetCloseHandle(con);
3819 InternetCloseHandle(ses);
3823 static void test_cache_control_verb(int port)
3825 HINTERNET session, connect, request;
3826 BOOL ret;
3828 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3829 ok(session != NULL, "InternetOpen failed\n");
3831 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3832 ok(connect != NULL, "InternetConnect failed\n");
3834 request = HttpOpenRequest(connect, "RPC_OUT_DATA", "/test_cache_control_verb", NULL, NULL, NULL,
3835 INTERNET_FLAG_NO_CACHE_WRITE, 0);
3836 ok(request != NULL, "HttpOpenRequest failed\n");
3837 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3838 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3839 test_status_code(request, 200);
3841 request = HttpOpenRequest(connect, "POST", "/test_cache_control_verb", NULL, NULL, NULL,
3842 INTERNET_FLAG_NO_CACHE_WRITE, 0);
3843 ok(request != NULL, "HttpOpenRequest failed\n");
3844 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3845 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3846 test_status_code(request, 200);
3848 request = HttpOpenRequest(connect, "HEAD", "/test_cache_control_verb", NULL, NULL, NULL,
3849 INTERNET_FLAG_NO_CACHE_WRITE, 0);
3850 ok(request != NULL, "HttpOpenRequest failed\n");
3851 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3852 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3853 test_status_code(request, 200);
3855 request = HttpOpenRequest(connect, "GET", "/test_cache_control_verb", NULL, NULL, NULL,
3856 INTERNET_FLAG_NO_CACHE_WRITE, 0);
3857 ok(request != NULL, "HttpOpenRequest failed\n");
3858 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3859 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3860 test_status_code(request, 200);
3862 InternetCloseHandle(request);
3863 InternetCloseHandle(connect);
3864 InternetCloseHandle(session);
3867 static void test_http_connection(void)
3869 struct server_info si;
3870 HANDLE hThread;
3871 DWORD id = 0, r;
3873 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
3874 si.port = 7531;
3876 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
3877 ok( hThread != NULL, "create thread failed\n");
3879 r = WaitForSingleObject(si.hEvent, 10000);
3880 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
3881 if (r != WAIT_OBJECT_0)
3882 return;
3884 test_basic_request(si.port, "GET", "/test1");
3885 test_proxy_indirect(si.port);
3886 test_proxy_direct(si.port);
3887 test_header_handling_order(si.port);
3888 test_basic_request(si.port, "POST", "/test5");
3889 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
3890 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
3891 test_basic_request(si.port, "GET", "/test6");
3892 test_basic_request(si.port, "GET", "/testF");
3893 test_connection_header(si.port);
3894 test_http1_1(si.port);
3895 test_cookie_header(si.port);
3896 test_basic_authentication(si.port);
3897 test_invalid_response_headers(si.port);
3898 test_response_without_headers(si.port);
3899 test_HttpQueryInfo(si.port);
3900 test_HttpSendRequestW(si.port);
3901 test_last_error(si.port);
3902 test_options(si.port);
3903 test_no_content(si.port);
3904 test_conn_close(si.port);
3905 test_no_cache(si.port);
3906 test_cache_read_gzipped(si.port);
3907 test_http_status(si.port);
3908 test_premature_disconnect(si.port);
3909 test_connection_closing(si.port);
3910 test_cache_control_verb(si.port);
3912 /* send the basic request again to shutdown the server thread */
3913 test_basic_request(si.port, "GET", "/quit");
3915 r = WaitForSingleObject(hThread, 3000);
3916 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
3917 CloseHandle(hThread);
3920 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
3922 LocalFree(info->lpszSubjectInfo);
3923 LocalFree(info->lpszIssuerInfo);
3924 LocalFree(info->lpszProtocolName);
3925 LocalFree(info->lpszSignatureAlgName);
3926 LocalFree(info->lpszEncryptionAlgName);
3929 typedef struct {
3930 const char *ex_subject;
3931 const char *ex_issuer;
3932 } cert_struct_test_t;
3934 static const cert_struct_test_t test_winehq_org_cert = {
3935 "0mJuv1t-1CFypQkyTZwfvjHHBAbnUndG\r\n"
3936 "GT98380011\r\n"
3937 "See www.rapidssl.com/resources/cps (c)13\r\n"
3938 "Domain Control Validated - RapidSSL(R)\r\n"
3939 "*.winehq.org",
3941 "US\r\n"
3942 "\"GeoTrust, Inc.\"\r\n"
3943 "RapidSSL CA"
3946 static const cert_struct_test_t test_winehq_com_cert = {
3947 "US\r\n"
3948 "Minnesota\r\n"
3949 "Saint Paul\r\n"
3950 "WineHQ\r\n"
3951 "test.winehq.com\r\n"
3952 "webmaster@winehq.org",
3954 "US\r\n"
3955 "Minnesota\r\n"
3956 "WineHQ\r\n"
3957 "test.winehq.com\r\n"
3958 "webmaster@winehq.org"
3961 static void test_cert_struct(HINTERNET req, const cert_struct_test_t *test)
3963 INTERNET_CERTIFICATE_INFOA info;
3964 DWORD size;
3965 BOOL res;
3967 memset(&info, 0x5, sizeof(info));
3969 size = sizeof(info);
3970 res = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, &info, &size);
3971 ok(res, "InternetQueryOption failed: %u\n", GetLastError());
3972 ok(size == sizeof(info), "size = %u\n", size);
3974 ok(!strcmp(info.lpszSubjectInfo, test->ex_subject), "lpszSubjectInfo = %s\n", info.lpszSubjectInfo);
3975 ok(!strcmp(info.lpszIssuerInfo, test->ex_issuer), "lpszIssuerInfo = %s\n", info.lpszIssuerInfo);
3976 ok(!info.lpszSignatureAlgName, "lpszSignatureAlgName = %s\n", info.lpszSignatureAlgName);
3977 ok(!info.lpszEncryptionAlgName, "lpszEncryptionAlgName = %s\n", info.lpszEncryptionAlgName);
3978 ok(!info.lpszProtocolName, "lpszProtocolName = %s\n", info.lpszProtocolName);
3979 ok(info.dwKeySize == 128, "dwKeySize = %u\n", info.dwKeySize);
3981 release_cert_info(&info);
3984 #define test_security_info(a,b,c) _test_security_info(__LINE__,a,b,c)
3985 static void _test_security_info(unsigned line, const char *urlc, DWORD error, DWORD ex_flags)
3987 char url[INTERNET_MAX_URL_LENGTH];
3988 const CERT_CHAIN_CONTEXT *chain;
3989 DWORD flags;
3990 BOOL res;
3992 if(!pInternetGetSecurityInfoByURLA) {
3993 win_skip("pInternetGetSecurityInfoByURLA not available\n");
3994 return;
3997 strcpy(url, urlc);
3998 chain = (void*)0xdeadbeef;
3999 flags = 0xdeadbeef;
4000 res = pInternetGetSecurityInfoByURLA(url, &chain, &flags);
4001 if(error == ERROR_SUCCESS) {
4002 ok_(__FILE__,line)(res, "InternetGetSecurityInfoByURLA failed: %u\n", GetLastError());
4003 ok_(__FILE__,line)(chain != NULL, "chain = NULL\n");
4004 ok_(__FILE__,line)(flags == ex_flags, "flags = %x\n", flags);
4005 CertFreeCertificateChain(chain);
4006 }else {
4007 ok_(__FILE__,line)(!res && GetLastError() == error,
4008 "InternetGetSecurityInfoByURLA returned: %x(%u), exected %u\n", res, GetLastError(), error);
4012 #define test_secflags_option(a,b) _test_secflags_option(__LINE__,a,b)
4013 static void _test_secflags_option(unsigned line, HINTERNET req, DWORD ex_flags)
4015 DWORD flags, size;
4016 BOOL res;
4018 flags = 0xdeadbeef;
4019 size = sizeof(flags);
4020 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
4021 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
4022 ok_(__FILE__,line)(flags == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS flags = %x, expected %x\n", flags, ex_flags);
4024 /* Option 98 is undocumented and seems to be the same as INTERNET_OPTION_SECURITY_FLAGS */
4025 flags = 0xdeadbeef;
4026 size = sizeof(flags);
4027 res = InternetQueryOptionW(req, 98, &flags, &size);
4028 ok_(__FILE__,line)(res, "InternetQueryOptionW(98) failed: %u\n", GetLastError());
4029 ok_(__FILE__,line)(flags == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS(98) flags = %x, expected %x\n", flags, ex_flags);
4032 #define set_secflags(a,b,c) _set_secflags(__LINE__,a,b,c)
4033 static void _set_secflags(unsigned line, HINTERNET req, BOOL use_undoc, DWORD flags)
4035 BOOL res;
4037 res = InternetSetOptionW(req, use_undoc ? 99 : INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
4038 ok_(__FILE__,line)(res, "InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
4041 static void test_security_flags(void)
4043 HINTERNET ses, conn, req;
4044 DWORD size, flags;
4045 char buf[100];
4046 BOOL res;
4048 trace("Testing security flags...\n");
4050 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
4052 ses = InternetOpen("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
4053 ok(ses != NULL, "InternetOpen failed\n");
4055 pInternetSetStatusCallbackA(ses, &callback);
4057 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4058 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
4059 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
4060 ok(conn != NULL, "InternetConnect failed with error %u\n", GetLastError());
4061 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4063 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4064 req = HttpOpenRequest(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
4065 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
4066 0xdeadbeef);
4067 ok(req != NULL, "HttpOpenRequest failed\n");
4068 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4070 flags = 0xdeadbeef;
4071 size = sizeof(flags);
4072 res = InternetQueryOptionW(req, 98, &flags, &size);
4073 if(!res && GetLastError() == ERROR_INVALID_PARAMETER) {
4074 win_skip("Incomplete security flags support, skipping\n");
4076 close_async_handle(ses, hCompleteEvent, 2);
4077 CloseHandle(hCompleteEvent);
4078 return;
4081 test_secflags_option(req, 0);
4082 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4084 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_REVOCATION);
4085 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION);
4087 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
4088 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
4090 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
4091 test_secflags_option(req, SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
4093 flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_SECURE;
4094 res = InternetSetOptionW(req, 99, &flags, sizeof(flags));
4095 ok(!res && GetLastError() == ERROR_INTERNET_OPTION_NOT_SETTABLE, "InternetSetOption(99) failed: %u\n", GetLastError());
4097 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
4098 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
4099 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4100 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4101 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
4102 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
4103 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
4104 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
4105 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4106 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4107 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4109 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4110 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4112 WaitForSingleObject(hCompleteEvent, INFINITE);
4113 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
4115 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
4116 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
4117 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4118 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4119 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
4120 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
4121 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
4122 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
4123 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4124 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4125 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4127 test_request_flags(req, 0);
4128 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA
4129 |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_STRENGTH_STRONG);
4131 res = InternetReadFile(req, buf, sizeof(buf), &size);
4132 ok(res, "InternetReadFile failed: %u\n", GetLastError());
4133 ok(size, "size = 0\n");
4135 /* Collect all existing persistent connections */
4136 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
4137 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
4139 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4140 req = HttpOpenRequest(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
4141 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
4142 0xdeadbeef);
4143 ok(req != NULL, "HttpOpenRequest failed\n");
4144 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4146 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT|INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY;
4147 res = InternetSetOption(req, INTERNET_OPTION_ERROR_MASK, (void*)&flags, sizeof(flags));
4148 ok(res, "InternetQueryOption(INTERNET_OPTION_ERROR_MASK failed: %u\n", GetLastError());
4150 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4151 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4152 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
4153 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
4154 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4155 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4156 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4158 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4159 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4161 WaitForSingleObject(hCompleteEvent, INFINITE);
4162 ok(req_error == ERROR_INTERNET_SEC_CERT_REV_FAILED || broken(req_error == ERROR_INTERNET_SEC_CERT_ERRORS),
4163 "req_error = %d\n", req_error);
4165 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4166 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4167 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
4168 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
4169 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4170 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4171 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4173 if(req_error != ERROR_INTERNET_SEC_CERT_REV_FAILED) {
4174 win_skip("Unexpected cert errors %u, skipping security flags tests\n", req_error);
4176 close_async_handle(ses, hCompleteEvent, 3);
4177 CloseHandle(hCompleteEvent);
4178 return;
4181 size = sizeof(buf);
4182 res = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
4183 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfoA(HTTP_QUERY_CONTENT_ENCODING) failed: %u\n", GetLastError());
4185 test_request_flags(req, 8);
4186 test_secflags_option(req, 0x800000);
4188 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_REVOCATION);
4189 test_secflags_option(req, 0x800000|SECURITY_FLAG_IGNORE_REVOCATION);
4191 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4192 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4193 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
4194 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
4195 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4196 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4197 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4199 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4200 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4202 WaitForSingleObject(hCompleteEvent, INFINITE);
4203 ok(req_error == ERROR_INTERNET_SEC_CERT_ERRORS, "req_error = %d\n", req_error);
4205 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4206 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4207 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
4208 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
4209 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4210 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4211 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4213 test_request_flags(req, INTERNET_REQFLAG_NO_HEADERS);
4214 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
4215 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4217 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
4218 test_secflags_option(req, 0x1800000|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_UNKNOWN_CA
4219 |SECURITY_FLAG_IGNORE_REVOCATION);
4220 test_http_version(req);
4222 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4223 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4224 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
4225 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
4226 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
4227 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
4228 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4229 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4230 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4232 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4233 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4235 WaitForSingleObject(hCompleteEvent, INFINITE);
4236 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
4238 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4239 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4240 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
4241 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
4242 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
4243 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
4244 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4245 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4246 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4248 test_request_flags(req, 0);
4249 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION
4250 |SECURITY_FLAG_STRENGTH_STRONG|0x1800000);
4252 test_cert_struct(req, &test_winehq_com_cert);
4253 test_security_info("https://test.winehq.com/data/some_file.html?q", 0, 0x1800000);
4255 res = InternetReadFile(req, buf, sizeof(buf), &size);
4256 ok(res, "InternetReadFile failed: %u\n", GetLastError());
4257 ok(size, "size = 0\n");
4259 close_async_handle(ses, hCompleteEvent, 3);
4261 /* Collect all existing persistent connections */
4262 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
4263 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
4265 /* Make another request, without setting security flags */
4267 ses = InternetOpen("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
4268 ok(ses != NULL, "InternetOpen failed\n");
4270 pInternetSetStatusCallbackA(ses, &callback);
4272 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4273 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
4274 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
4275 ok(conn != NULL, "InternetConnect failed with error %u\n", GetLastError());
4276 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4278 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4279 req = HttpOpenRequest(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
4280 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
4281 0xdeadbeef);
4282 ok(req != NULL, "HttpOpenRequest failed\n");
4283 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4285 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
4286 |SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
4287 test_http_version(req);
4289 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4290 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4291 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
4292 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
4293 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
4294 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
4295 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4296 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4298 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4299 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4301 WaitForSingleObject(hCompleteEvent, INFINITE);
4302 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
4304 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4305 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4306 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
4307 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
4308 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
4309 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
4310 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4311 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4313 test_request_flags(req, 0);
4314 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
4315 |SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
4317 res = InternetReadFile(req, buf, sizeof(buf), &size);
4318 ok(res, "InternetReadFile failed: %u\n", GetLastError());
4319 ok(size, "size = 0\n");
4321 close_async_handle(ses, hCompleteEvent, 2);
4323 CloseHandle(hCompleteEvent);
4325 test_security_info("http://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4326 test_security_info("file:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4327 test_security_info("xxx:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4330 static void test_secure_connection(void)
4332 static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
4333 static const WCHAR testsite[] = {'t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',0};
4334 static const WCHAR get[] = {'G','E','T',0};
4335 static const WCHAR testpage[] = {'/','t','e','s','t','s','/','h','e','l','l','o','.','h','t','m','l',0};
4336 HINTERNET ses, con, req;
4337 DWORD size, flags;
4338 INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
4339 INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
4340 BOOL ret;
4342 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
4343 ok(ses != NULL, "InternetOpen failed\n");
4345 con = InternetConnect(ses, "test.winehq.org",
4346 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
4347 INTERNET_SERVICE_HTTP, 0, 0);
4348 ok(con != NULL, "InternetConnect failed\n");
4350 req = HttpOpenRequest(con, "GET", "/tests/hello.html", NULL, NULL, NULL,
4351 INTERNET_FLAG_SECURE, 0);
4352 ok(req != NULL, "HttpOpenRequest failed\n");
4354 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
4355 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
4357 size = sizeof(flags);
4358 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
4359 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4360 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
4362 test_cert_struct(req, &test_winehq_org_cert);
4364 /* Querying the same option through InternetQueryOptionW still results in
4365 * ASCII strings being returned.
4367 size = 0;
4368 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4369 NULL, &size);
4370 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
4371 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
4372 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
4373 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4374 certificate_structW, &size);
4375 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
4376 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4377 if (ret)
4379 ok(certificate_structA->lpszSubjectInfo &&
4380 strlen(certificate_structA->lpszSubjectInfo) > 1,
4381 "expected a non-empty subject name\n");
4382 ok(certificate_structA->lpszIssuerInfo &&
4383 strlen(certificate_structA->lpszIssuerInfo) > 1,
4384 "expected a non-empty issuer name\n");
4385 ok(!certificate_structA->lpszSignatureAlgName,
4386 "unexpected signature algorithm name\n");
4387 ok(!certificate_structA->lpszEncryptionAlgName,
4388 "unexpected encryption algorithm name\n");
4389 ok(!certificate_structA->lpszProtocolName,
4390 "unexpected protocol name\n");
4391 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
4392 release_cert_info(certificate_structA);
4394 HeapFree(GetProcessHeap(), 0, certificate_structW);
4396 InternetCloseHandle(req);
4397 InternetCloseHandle(con);
4398 InternetCloseHandle(ses);
4400 /* Repeating the tests with the W functions has the same result: */
4401 ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
4402 ok(ses != NULL, "InternetOpen failed\n");
4404 con = InternetConnectW(ses, testsite,
4405 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
4406 INTERNET_SERVICE_HTTP, 0, 0);
4407 ok(con != NULL, "InternetConnect failed\n");
4409 req = HttpOpenRequestW(con, get, testpage, NULL, NULL, NULL,
4410 INTERNET_FLAG_SECURE|INTERNET_FLAG_RELOAD, 0);
4411 ok(req != NULL, "HttpOpenRequest failed\n");
4413 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
4414 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
4416 size = sizeof(flags);
4417 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
4418 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4419 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set, got %x\n", flags);
4421 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4422 NULL, &size);
4423 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
4424 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
4425 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
4426 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4427 certificate_structA, &size);
4428 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4429 if (ret)
4431 ok(certificate_structA->lpszSubjectInfo &&
4432 strlen(certificate_structA->lpszSubjectInfo) > 1,
4433 "expected a non-empty subject name\n");
4434 ok(certificate_structA->lpszIssuerInfo &&
4435 strlen(certificate_structA->lpszIssuerInfo) > 1,
4436 "expected a non-empty issuer name\n");
4437 ok(!certificate_structA->lpszSignatureAlgName,
4438 "unexpected signature algorithm name\n");
4439 ok(!certificate_structA->lpszEncryptionAlgName,
4440 "unexpected encryption algorithm name\n");
4441 ok(!certificate_structA->lpszProtocolName,
4442 "unexpected protocol name\n");
4443 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
4444 release_cert_info(certificate_structA);
4446 HeapFree(GetProcessHeap(), 0, certificate_structA);
4448 /* Again, querying the same option through InternetQueryOptionW still
4449 * results in ASCII strings being returned.
4451 size = 0;
4452 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4453 NULL, &size);
4454 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
4455 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
4456 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
4457 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4458 certificate_structW, &size);
4459 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
4460 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4461 if (ret)
4463 ok(certificate_structA->lpszSubjectInfo &&
4464 strlen(certificate_structA->lpszSubjectInfo) > 1,
4465 "expected a non-empty subject name\n");
4466 ok(certificate_structA->lpszIssuerInfo &&
4467 strlen(certificate_structA->lpszIssuerInfo) > 1,
4468 "expected a non-empty issuer name\n");
4469 ok(!certificate_structA->lpszSignatureAlgName,
4470 "unexpected signature algorithm name\n");
4471 ok(!certificate_structA->lpszEncryptionAlgName,
4472 "unexpected encryption algorithm name\n");
4473 ok(!certificate_structA->lpszProtocolName,
4474 "unexpected protocol name\n");
4475 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
4476 release_cert_info(certificate_structA);
4478 HeapFree(GetProcessHeap(), 0, certificate_structW);
4480 InternetCloseHandle(req);
4481 InternetCloseHandle(con);
4482 InternetCloseHandle(ses);
4485 static void test_user_agent_header(void)
4487 HINTERNET ses, con, req;
4488 DWORD size, err;
4489 char buffer[64];
4490 BOOL ret;
4492 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
4493 ok(ses != NULL, "InternetOpen failed\n");
4495 con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4496 ok(con != NULL, "InternetConnect failed\n");
4498 req = HttpOpenRequest(con, "GET", "/tests/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
4499 ok(req != NULL, "HttpOpenRequest failed\n");
4501 size = sizeof(buffer);
4502 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4503 err = GetLastError();
4504 ok(!ret, "HttpQueryInfo succeeded\n");
4505 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
4507 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4508 ok(ret, "HttpAddRequestHeaders succeeded\n");
4510 size = sizeof(buffer);
4511 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4512 err = GetLastError();
4513 ok(ret, "HttpQueryInfo failed\n");
4514 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
4516 InternetCloseHandle(req);
4518 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
4519 ok(req != NULL, "HttpOpenRequest failed\n");
4521 size = sizeof(buffer);
4522 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4523 err = GetLastError();
4524 ok(!ret, "HttpQueryInfo succeeded\n");
4525 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
4527 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4528 ok(ret, "HttpAddRequestHeaders failed\n");
4530 buffer[0] = 0;
4531 size = sizeof(buffer);
4532 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4533 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
4534 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
4536 InternetCloseHandle(req);
4537 InternetCloseHandle(con);
4538 InternetCloseHandle(ses);
4541 static void test_bogus_accept_types_array(void)
4543 HINTERNET ses, con, req;
4544 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
4545 DWORD size, error;
4546 char buffer[32];
4547 BOOL ret;
4549 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
4550 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4551 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
4553 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
4555 buffer[0] = 0;
4556 size = sizeof(buffer);
4557 SetLastError(0xdeadbeef);
4558 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4559 error = GetLastError();
4560 ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
4561 if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", error);
4562 ok(broken(!strcmp(buffer, ", */*, %p, , , */*")) /* IE6 */ ||
4563 broken(!strcmp(buffer, "*/*, %p, */*")) /* IE7/8 */ ||
4564 !strcmp(buffer, ""), "got '%s' expected ''\n", buffer);
4566 InternetCloseHandle(req);
4567 InternetCloseHandle(con);
4568 InternetCloseHandle(ses);
4571 struct context
4573 HANDLE event;
4574 HINTERNET req;
4577 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
4579 INTERNET_ASYNC_RESULT *result = info;
4580 struct context *ctx = (struct context *)context;
4582 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
4584 switch(status) {
4585 case INTERNET_STATUS_REQUEST_COMPLETE:
4586 trace("request handle: 0x%08lx\n", result->dwResult);
4587 ctx->req = (HINTERNET)result->dwResult;
4588 SetEvent(ctx->event);
4589 break;
4590 case INTERNET_STATUS_HANDLE_CLOSING: {
4591 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
4593 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
4594 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
4595 SetEvent(ctx->event);
4596 break;
4598 case INTERNET_STATUS_NAME_RESOLVED:
4599 case INTERNET_STATUS_CONNECTING_TO_SERVER:
4600 case INTERNET_STATUS_CONNECTED_TO_SERVER: {
4601 char *str = info;
4602 ok(str[0] && str[1], "Got string: %s\n", str);
4603 ok(size == strlen(str)+1, "unexpected size %u\n", size);
4608 static void test_open_url_async(void)
4610 BOOL ret;
4611 HINTERNET ses, req;
4612 DWORD size, error;
4613 struct context ctx;
4614 ULONG type;
4616 /* Collect all existing persistent connections */
4617 ret = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
4618 ok(ret, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
4621 * Some versions of IE6 fail those tests. They pass some notification data as UNICODE string, while
4622 * other versions never do. They also hang of following tests. We disable it for everything older
4623 * than IE7.
4625 if(!pInternetGetSecurityInfoByURLA) {
4626 win_skip("Skipping async open on too old wininet version.\n");
4627 return;
4630 ctx.req = NULL;
4631 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
4633 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
4634 ok(ses != NULL, "InternetOpen failed\n");
4636 SetLastError(0xdeadbeef);
4637 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
4638 error = GetLastError();
4639 ok(!ret, "InternetSetOptionA succeeded\n");
4640 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
4642 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
4643 error = GetLastError();
4644 ok(!ret, "InternetSetOptionA failed\n");
4645 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
4647 pInternetSetStatusCallbackW(ses, cb);
4648 ResetEvent(ctx.event);
4650 req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
4651 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
4653 WaitForSingleObject(ctx.event, INFINITE);
4655 type = 0;
4656 size = sizeof(type);
4657 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
4658 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
4659 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
4660 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
4662 size = 0;
4663 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
4664 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
4665 ok(size > 0, "expected size > 0\n");
4667 ResetEvent(ctx.event);
4668 InternetCloseHandle(ctx.req);
4669 WaitForSingleObject(ctx.event, INFINITE);
4671 InternetCloseHandle(ses);
4672 CloseHandle(ctx.event);
4675 enum api
4677 internet_connect = 1,
4678 http_open_request,
4679 http_send_request_ex,
4680 internet_writefile,
4681 http_end_request,
4682 internet_close_handle
4685 struct notification
4687 enum api function; /* api responsible for notification */
4688 unsigned int status; /* status received */
4689 int async; /* delivered from another thread? */
4690 int todo;
4691 int optional;
4694 struct info
4696 enum api function;
4697 const struct notification *test;
4698 unsigned int count;
4699 unsigned int index;
4700 HANDLE wait;
4701 DWORD thread;
4702 unsigned int line;
4703 DWORD expect_result;
4704 BOOL is_aborted;
4707 static CRITICAL_SECTION notification_cs;
4709 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
4711 BOOL status_ok, function_ok;
4712 struct info *info = (struct info *)context;
4713 unsigned int i;
4715 EnterCriticalSection( &notification_cs );
4717 if(info->is_aborted) {
4718 LeaveCriticalSection(&notification_cs);
4719 return;
4722 if (status == INTERNET_STATUS_HANDLE_CREATED)
4724 DWORD size = sizeof(struct info *);
4725 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
4726 }else if(status == INTERNET_STATUS_REQUEST_COMPLETE) {
4727 INTERNET_ASYNC_RESULT *ar = (INTERNET_ASYNC_RESULT*)buffer;
4729 ok(buflen == sizeof(*ar), "unexpected buflen = %d\n", buflen);
4730 if(info->expect_result == ERROR_SUCCESS) {
4731 ok(ar->dwResult == 1, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
4732 }else {
4733 ok(!ar->dwResult, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
4734 ok(ar->dwError == info->expect_result, "ar->dwError = %d, expected %d\n", ar->dwError, info->expect_result);
4738 i = info->index;
4739 if (i >= info->count)
4741 LeaveCriticalSection( &notification_cs );
4742 return;
4745 while (info->test[i].status != status &&
4746 (info->test[i].optional || info->test[i].todo) &&
4747 i < info->count - 1 &&
4748 info->test[i].function == info->test[i + 1].function)
4750 i++;
4753 status_ok = (info->test[i].status == status);
4754 function_ok = (info->test[i].function == info->function);
4756 if (!info->test[i].todo)
4758 ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
4759 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
4761 if (info->test[i].async)
4762 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
4763 info->line, info->thread, GetCurrentThreadId());
4765 else
4767 todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
4768 if (status_ok)
4769 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
4771 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
4772 info->index = i+1;
4774 LeaveCriticalSection( &notification_cs );
4777 static void setup_test( struct info *info, enum api function, unsigned int line, DWORD expect_result )
4779 info->function = function;
4780 info->line = line;
4781 info->expect_result = expect_result;
4784 struct notification_data
4786 const struct notification *test;
4787 const unsigned int count;
4788 const char *method;
4789 const char *host;
4790 const char *path;
4791 const char *data;
4792 BOOL expect_conn_failure;
4795 static const struct notification async_send_request_ex_test[] =
4797 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
4798 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
4799 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4800 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, 1, 0, 1 },
4801 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
4802 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
4803 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
4804 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
4805 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
4806 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
4807 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4808 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, 0 },
4809 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, 0 },
4810 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
4811 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
4812 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4813 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
4814 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
4815 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
4816 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
4819 static const struct notification async_send_request_ex_test2[] =
4821 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
4822 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
4823 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4824 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, 1, 0, 1 },
4825 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
4826 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
4827 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1, 1 },
4828 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1, 1 },
4829 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
4830 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
4831 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4832 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
4833 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
4834 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4835 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
4836 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
4837 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
4838 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
4841 static const struct notification async_send_request_ex_resolve_failure_test[] =
4843 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
4844 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
4845 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4846 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1 },
4847 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4848 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4849 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4850 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
4851 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
4852 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
4853 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
4856 static const struct notification async_send_request_ex_chunked_test[] =
4858 { internet_connect, INTERNET_STATUS_HANDLE_CREATED },
4859 { http_open_request, INTERNET_STATUS_HANDLE_CREATED },
4860 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4861 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, 1, 0, 1 },
4862 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
4863 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
4864 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
4865 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
4866 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
4867 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
4868 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4869 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
4870 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
4871 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4872 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION },
4873 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED },
4874 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING },
4875 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING }
4878 static const struct notification_data notification_data[] = {
4880 async_send_request_ex_chunked_test,
4881 sizeof(async_send_request_ex_chunked_test)/sizeof(async_send_request_ex_chunked_test[0]),
4882 "GET",
4883 "test.winehq.org",
4884 "tests/data.php"
4887 async_send_request_ex_test,
4888 sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
4889 "POST",
4890 "test.winehq.org",
4891 "tests/posttest.php",
4892 "Public ID=codeweavers"
4895 async_send_request_ex_test2,
4896 sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
4897 "POST",
4898 "test.winehq.org",
4899 "tests/posttest.php"
4902 async_send_request_ex_resolve_failure_test,
4903 sizeof(async_send_request_ex_resolve_failure_test)/sizeof(async_send_request_ex_resolve_failure_test[0]),
4904 "GET",
4905 "brokenhost",
4906 "index.html",
4907 NULL,
4908 TRUE
4912 static void test_async_HttpSendRequestEx(const struct notification_data *nd)
4914 BOOL ret;
4915 HINTERNET ses, req, con;
4916 struct info info;
4917 DWORD size, written, error;
4918 INTERNET_BUFFERSA b;
4919 static const char *accept[2] = {"*/*", NULL};
4920 char buffer[32];
4922 trace("Async HttpSendRequestEx test (%s %s)\n", nd->method, nd->host);
4924 InitializeCriticalSection( &notification_cs );
4926 info.test = nd->test;
4927 info.count = nd->count;
4928 info.index = 0;
4929 info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
4930 info.thread = GetCurrentThreadId();
4931 info.is_aborted = FALSE;
4933 ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
4934 ok( ses != NULL, "InternetOpen failed\n" );
4936 pInternetSetStatusCallbackA( ses, check_notification );
4938 setup_test( &info, internet_connect, __LINE__, ERROR_SUCCESS );
4939 con = InternetConnect( ses, nd->host, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
4940 ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
4942 WaitForSingleObject( info.wait, 10000 );
4944 setup_test( &info, http_open_request, __LINE__, ERROR_SUCCESS );
4945 req = HttpOpenRequest( con, nd->method, nd->path, NULL, NULL, accept, 0, (DWORD_PTR)&info );
4946 ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
4948 WaitForSingleObject( info.wait, 10000 );
4950 if(nd->data) {
4951 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
4952 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
4953 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
4954 b.dwHeadersLength = strlen( b.lpcszHeader );
4955 b.dwBufferTotal = nd->data ? strlen( nd->data ) : 0;
4958 setup_test( &info, http_send_request_ex, __LINE__,
4959 nd->expect_conn_failure ? ERROR_INTERNET_NAME_NOT_RESOLVED : ERROR_SUCCESS );
4960 ret = HttpSendRequestExA( req, nd->data ? &b : NULL, NULL, 0x28, 0 );
4961 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
4963 error = WaitForSingleObject( info.wait, 10000 );
4964 if(error != WAIT_OBJECT_0) {
4965 skip("WaitForSingleObject returned %d, assuming DNS problem\n", error);
4966 info.is_aborted = TRUE;
4967 goto abort;
4970 size = sizeof(buffer);
4971 SetLastError( 0xdeadbeef );
4972 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
4973 error = GetLastError();
4974 ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
4975 if(nd->expect_conn_failure) {
4976 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND got %u\n", error );
4977 }else {
4978 todo_wine
4979 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
4980 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
4983 if (nd->data)
4985 written = 0;
4986 size = strlen( nd->data );
4987 setup_test( &info, internet_writefile, __LINE__, ERROR_SUCCESS );
4988 ret = InternetWriteFile( req, nd->data, size, &written );
4989 ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
4990 ok( written == size, "expected %u got %u\n", written, size );
4992 WaitForSingleObject( info.wait, 10000 );
4994 SetLastError( 0xdeadbeef );
4995 ret = HttpEndRequestA( req, (void *)nd->data, 0x28, 0 );
4996 error = GetLastError();
4997 ok( !ret, "HttpEndRequestA succeeded\n" );
4998 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
5001 SetLastError( 0xdeadbeef );
5002 setup_test( &info, http_end_request, __LINE__,
5003 nd->expect_conn_failure ? ERROR_INTERNET_OPERATION_CANCELLED : ERROR_SUCCESS);
5004 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
5005 error = GetLastError();
5006 ok( !ret, "HttpEndRequestA succeeded\n" );
5007 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
5009 WaitForSingleObject( info.wait, 10000 );
5011 setup_test( &info, internet_close_handle, __LINE__, ERROR_SUCCESS );
5012 abort:
5013 InternetCloseHandle( req );
5014 InternetCloseHandle( con );
5015 InternetCloseHandle( ses );
5017 WaitForSingleObject( info.wait, 10000 );
5018 Sleep(100);
5019 CloseHandle( info.wait );
5022 static HINTERNET closetest_session, closetest_req, closetest_conn;
5023 static BOOL closetest_closed;
5025 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
5026 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
5028 DWORD len, type;
5029 BOOL res;
5031 trace("closetest_callback %p: %d\n", hInternet, dwInternetStatus);
5033 ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
5034 "Unexpected hInternet %p\n", hInternet);
5035 if(!closetest_closed)
5036 return;
5038 len = sizeof(type);
5039 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
5040 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
5041 "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
5042 closetest_req, res, GetLastError());
5045 static void test_InternetCloseHandle(void)
5047 DWORD len, flags;
5048 BOOL res;
5050 closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
5051 ok(closetest_session != NULL,"InternetOpen failed with error %u\n", GetLastError());
5053 pInternetSetStatusCallbackA(closetest_session, closetest_callback);
5055 closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
5056 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
5057 ok(closetest_conn != NULL,"InternetConnect failed with error %u\n", GetLastError());
5059 closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
5060 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
5062 res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
5063 ok(!res && (GetLastError() == ERROR_IO_PENDING),
5064 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
5066 test_request_flags(closetest_req, INTERNET_REQFLAG_NO_HEADERS);
5068 res = InternetCloseHandle(closetest_session);
5069 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
5070 closetest_closed = TRUE;
5071 trace("Closed session handle\n");
5073 res = InternetCloseHandle(closetest_conn);
5074 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %u\n",
5075 res, GetLastError());
5077 res = InternetCloseHandle(closetest_req);
5078 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %u\n",
5079 res, GetLastError());
5081 len = sizeof(flags);
5082 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
5083 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
5084 "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
5085 closetest_req, res, GetLastError());
5088 static void test_connection_failure(void)
5090 HINTERNET session, connect, request;
5091 DWORD error;
5092 BOOL ret;
5094 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
5095 ok(session != NULL, "failed to get session handle\n");
5097 connect = InternetConnectA(session, "localhost", 1, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
5098 ok(connect != NULL, "failed to get connection handle\n");
5100 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, 0, 0);
5101 ok(request != NULL, "failed to get request handle\n");
5103 SetLastError(0xdeadbeef);
5104 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
5105 error = GetLastError();
5106 ok(!ret, "unexpected success\n");
5107 ok(error == ERROR_INTERNET_CANNOT_CONNECT, "wrong error %u\n", error);
5109 InternetCloseHandle(request);
5110 InternetCloseHandle(connect);
5111 InternetCloseHandle(session);
5114 static void test_default_service_port(void)
5116 HINTERNET session, connect, request;
5117 DWORD error;
5118 BOOL ret;
5120 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
5121 ok(session != NULL, "InternetOpen failed\n");
5123 connect = InternetConnect(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
5124 INTERNET_SERVICE_HTTP, 0, 0);
5125 ok(connect != NULL, "InternetConnect failed\n");
5127 request = HttpOpenRequest(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
5128 ok(request != NULL, "HttpOpenRequest failed\n");
5130 SetLastError(0xdeadbeef);
5131 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
5132 error = GetLastError();
5133 ok(!ret, "HttpSendRequest succeeded\n");
5134 ok(error == ERROR_INTERNET_SECURITY_CHANNEL_ERROR || error == ERROR_INTERNET_CANNOT_CONNECT,
5135 "got %u\n", error);
5137 InternetCloseHandle(request);
5138 InternetCloseHandle(connect);
5139 InternetCloseHandle(session);
5142 static void init_status_tests(void)
5144 memset(expect, 0, sizeof(expect));
5145 memset(optional, 0, sizeof(optional));
5146 memset(wine_allow, 0, sizeof(wine_allow));
5147 memset(notified, 0, sizeof(notified));
5148 memset(status_string, 0, sizeof(status_string));
5150 #define STATUS_STRING(status) status_string[status] = #status
5151 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
5152 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
5153 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
5154 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
5155 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
5156 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
5157 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
5158 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
5159 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
5160 STATUS_STRING(INTERNET_STATUS_PREFETCH);
5161 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
5162 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
5163 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
5164 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
5165 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
5166 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
5167 STATUS_STRING(INTERNET_STATUS_REDIRECT);
5168 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
5169 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
5170 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
5171 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
5172 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
5173 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
5174 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
5175 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
5176 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
5177 #undef STATUS_STRING
5180 START_TEST(http)
5182 HMODULE hdll;
5183 hdll = GetModuleHandleA("wininet.dll");
5185 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
5186 win_skip("Too old IE (older than 6.0)\n");
5187 return;
5190 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
5191 pInternetSetStatusCallbackW = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackW");
5192 pInternetGetSecurityInfoByURLA = (void*)GetProcAddress(hdll, "InternetGetSecurityInfoByURLA");
5194 init_status_tests();
5195 test_InternetCloseHandle();
5196 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
5197 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
5198 InternetReadFile_test(0, &test_data[1]);
5199 first_connection_to_test_url = TRUE;
5200 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
5201 test_security_flags();
5202 InternetReadFile_test(0, &test_data[2]);
5203 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
5204 test_open_url_async();
5205 test_async_HttpSendRequestEx(&notification_data[0]);
5206 test_async_HttpSendRequestEx(&notification_data[1]);
5207 test_async_HttpSendRequestEx(&notification_data[2]);
5208 test_async_HttpSendRequestEx(&notification_data[3]);
5209 InternetOpenRequest_test();
5210 test_http_cache();
5211 InternetLockRequestFile_test();
5212 InternetOpenUrlA_test();
5213 HttpHeaders_test();
5214 test_http_connection();
5215 test_secure_connection();
5216 test_user_agent_header();
5217 test_bogus_accept_types_array();
5218 InternetReadFile_chunked_test();
5219 HttpSendRequestEx_test();
5220 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[3]);
5221 test_connection_failure();
5222 test_default_service_port();