[WININET_WINETEST]
[reactos.git] / rostests / winetests / wininet / http.c
1 /*
2 * Wininet - HTTP tests
3 *
4 * Copyright 2002 Aric Stewart
5 * Copyright 2004 Mike McCormack
6 * Copyright 2005 Hans Leidekker
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 //#include <stdlib.h>
26
27 #define WIN32_NO_STATUS
28 #define _INC_WINDOWS
29
30 #include <windef.h>
31 #include <winbase.h>
32 #include <winreg.h>
33 #include <winnls.h>
34 #include <wincrypt.h>
35 #include <wininet.h>
36 #include <winsock.h>
37
38 #include <wine/test.h>
39
40 #define TEST_URL "http://test.winehq.org/tests/hello.html"
41
42 static BOOL first_connection_to_test_url = TRUE;
43
44 /* Adapted from dlls/urlmon/tests/protocol.c */
45
46 #define SET_EXPECT2(status, num) \
47 expect[status] = num
48
49 #define SET_EXPECT(status) \
50 SET_EXPECT2(status, 1)
51
52 #define SET_OPTIONAL2(status, num) \
53 optional[status] = num
54
55 #define SET_OPTIONAL(status) \
56 SET_OPTIONAL2(status, 1)
57
58 /* SET_WINE_ALLOW's should be used with an appropriate
59 * todo_wine CHECK_NOTIFIED at a later point in the code */
60 #define SET_WINE_ALLOW2(status, num) \
61 wine_allow[status] = num
62
63 #define SET_WINE_ALLOW(status) \
64 SET_WINE_ALLOW2(status, 1)
65
66 #define CHECK_EXPECT(status) \
67 do { \
68 if (!expect[status] && !optional[status] && wine_allow[status]) \
69 { \
70 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
71 status < MAX_INTERNET_STATUS && status_string[status] ? \
72 status_string[status] : "unknown"); \
73 wine_allow[status]--; \
74 } \
75 else \
76 { \
77 ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
78 status < MAX_INTERNET_STATUS && status_string[status] ? \
79 status_string[status] : "unknown"); \
80 if (expect[status]) expect[status]--; \
81 else optional[status]--; \
82 } \
83 notified[status]++; \
84 }while(0)
85
86 /* CLEAR_NOTIFIED used in cases when notification behavior
87 * differs between Windows versions */
88 #define CLEAR_NOTIFIED(status) \
89 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
90
91 #define CHECK_NOTIFIED2(status, num) \
92 do { \
93 ok(notified[status] + optional[status] == (num), \
94 "expected status %d (%s) %d times, received %d times\n", \
95 status, status < MAX_INTERNET_STATUS && status_string[status] ? \
96 status_string[status] : "unknown", (num), notified[status]); \
97 CLEAR_NOTIFIED(status); \
98 }while(0)
99
100 #define CHECK_NOTIFIED(status) \
101 CHECK_NOTIFIED2(status, 1)
102
103 #define CHECK_NOT_NOTIFIED(status) \
104 CHECK_NOTIFIED2(status, 0)
105
106 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
107 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
108 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
109 static const char *status_string[MAX_INTERNET_STATUS];
110
111 static HANDLE hCompleteEvent, conn_close_event;
112 static DWORD req_error;
113
114 #define TESTF_REDIRECT 0x01
115 #define TESTF_COMPRESSED 0x02
116 #define TESTF_CHUNKED 0x04
117
118 typedef struct {
119 const char *url;
120 const char *redirected_url;
121 const char *host;
122 const char *path;
123 const char *headers;
124 DWORD flags;
125 const char *post_data;
126 const char *content;
127 } test_data_t;
128
129 static const test_data_t test_data[] = {
130 {
131 "http://test.winehq.org/tests/data.php",
132 "http://test.winehq.org/tests/data.php",
133 "test.winehq.org",
134 "/tests/data.php",
135 "",
136 TESTF_CHUNKED
137 },
138 {
139 "http://test.winehq.org/tests/redirect",
140 "http://test.winehq.org/tests/hello.html",
141 "test.winehq.org",
142 "/tests/redirect",
143 "",
144 TESTF_REDIRECT
145 },
146 {
147 "http://www.codeweavers.com/",
148 "http://www.codeweavers.com/",
149 "www.codeweavers.com",
150 "",
151 "Accept-Encoding: gzip, deflate",
152 TESTF_COMPRESSED
153 },
154 {
155 "http://test.winehq.org/tests/post.php",
156 "http://test.winehq.org/tests/post.php",
157 "test.winehq.org",
158 "/tests/post.php",
159 "Content-Type: application/x-www-form-urlencoded",
160 0,
161 "mode=Test",
162 "mode => Test\n"
163 }
164 };
165
166 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
167 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackW)(HINTERNET ,INTERNET_STATUS_CALLBACK);
168 static BOOL (WINAPI *pInternetGetSecurityInfoByURLA)(LPSTR,PCCERT_CHAIN_CONTEXT*,DWORD*);
169
170 static int strcmp_wa(LPCWSTR strw, const char *stra)
171 {
172 WCHAR buf[512];
173 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
174 return lstrcmpW(strw, buf);
175 }
176
177 static BOOL proxy_active(void)
178 {
179 HKEY internet_settings;
180 DWORD proxy_enable;
181 DWORD size;
182
183 if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
184 0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
185 return FALSE;
186
187 size = sizeof(DWORD);
188 if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
189 proxy_enable = 0;
190
191 RegCloseKey(internet_settings);
192
193 return proxy_enable != 0;
194 }
195
196 #define test_status_code(a,b) _test_status_code(__LINE__,a,b, FALSE)
197 #define test_status_code_todo(a,b) _test_status_code(__LINE__,a,b, TRUE)
198 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
199 {
200 DWORD code, size, index;
201 char exbuf[10], bufa[10];
202 WCHAR bufw[10];
203 BOOL res;
204
205 code = 0xdeadbeef;
206 size = sizeof(code);
207 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, NULL);
208 ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %u\n", GetLastError());
209 if (is_todo)
210 todo_wine ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
211 else
212 ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
213 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
214
215 code = 0xdeadbeef;
216 index = 0;
217 size = sizeof(code);
218 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
219 ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %u\n", GetLastError());
220 if (is_todo)
221 todo_wine ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
222 else
223 ok_(__FILE__,line)(!index, "index = %d, expected 0\n", code);
224 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
225
226 sprintf(exbuf, "%u", excode);
227
228 size = sizeof(bufa);
229 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, bufa, &size, NULL);
230 ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
231 if (is_todo)
232 todo_wine ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
233 else
234 ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
235 ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %d for \"%s\"\n", size, exbuf);
236
237 size = 0;
238 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
239 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
240 "[4] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
241 ok_(__FILE__,line)(size == strlen(exbuf)+1, "unexpected size %d for \"%s\"\n", size, exbuf);
242
243 size = sizeof(bufw);
244 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
245 ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
246 if (is_todo)
247 todo_wine ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
248 else
249 ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
250 ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
251
252 size = 0;
253 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
254 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
255 "[6] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
256 ok_(__FILE__,line)(size == (strlen(exbuf)+1)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
257
258 if(0) {
259 size = sizeof(bufw);
260 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
261 ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "HttpQueryInfo(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
262 ok(size == sizeof(bufw), "unexpected size %d\n", size);
263 }
264
265 code = 0xdeadbeef;
266 index = 1;
267 size = sizeof(code);
268 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
269 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
270 "[7] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
271
272 code = 0xdeadbeef;
273 size = sizeof(code);
274 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_REQUEST_HEADERS, &code, &size, NULL);
275 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_INVALID_QUERY_REQUEST,
276 "[8] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
277 }
278
279 #define test_request_flags(a,b) _test_request_flags(__LINE__,a,b,FALSE)
280 #define test_request_flags_todo(a,b) _test_request_flags(__LINE__,a,b,TRUE)
281 static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOOL is_todo)
282 {
283 DWORD flags, size;
284 BOOL res;
285
286 flags = 0xdeadbeef;
287 size = sizeof(flags);
288 res = InternetQueryOptionW(req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &size);
289 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_REQUEST_FLAGS) failed: %u\n", GetLastError());
290
291 /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
292 flags &= ~INTERNET_REQFLAG_CACHE_WRITE_DISABLED;
293 if(!is_todo)
294 ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
295 else
296 todo_wine ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
297 }
298
299 #define test_http_version(a) _test_http_version(__LINE__,a)
300 static void _test_http_version(unsigned line, HINTERNET req)
301 {
302 HTTP_VERSION_INFO v = {0xdeadbeef, 0xdeadbeef};
303 DWORD size;
304 BOOL res;
305
306 size = sizeof(v);
307 res = InternetQueryOptionW(req, INTERNET_OPTION_HTTP_VERSION, &v, &size);
308 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_HTTP_VERSION) failed: %u\n", GetLastError());
309 ok_(__FILE__,line)(v.dwMajorVersion == 1, "dwMajorVersion = %d\n", v.dwMajorVersion);
310 ok_(__FILE__,line)(v.dwMinorVersion == 1, "dwMinorVersion = %d\n", v.dwMinorVersion);
311 }
312
313 static int close_handle_cnt;
314
315 static VOID WINAPI callback(
316 HINTERNET hInternet,
317 DWORD_PTR dwContext,
318 DWORD dwInternetStatus,
319 LPVOID lpvStatusInformation,
320 DWORD dwStatusInformationLength
321 )
322 {
323 CHECK_EXPECT(dwInternetStatus);
324 switch (dwInternetStatus)
325 {
326 case INTERNET_STATUS_RESOLVING_NAME:
327 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
328 GetCurrentThreadId(), hInternet, dwContext,
329 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
330 *(LPSTR)lpvStatusInformation = '\0';
331 break;
332 case INTERNET_STATUS_NAME_RESOLVED:
333 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
334 GetCurrentThreadId(), hInternet, dwContext,
335 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
336 *(LPSTR)lpvStatusInformation = '\0';
337 break;
338 case INTERNET_STATUS_CONNECTING_TO_SERVER:
339 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
340 GetCurrentThreadId(), hInternet, dwContext,
341 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
342 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
343 dwStatusInformationLength);
344 *(LPSTR)lpvStatusInformation = '\0';
345 break;
346 case INTERNET_STATUS_CONNECTED_TO_SERVER:
347 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
348 GetCurrentThreadId(), hInternet, dwContext,
349 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
350 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
351 dwStatusInformationLength);
352 *(LPSTR)lpvStatusInformation = '\0';
353 break;
354 case INTERNET_STATUS_SENDING_REQUEST:
355 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
356 GetCurrentThreadId(), hInternet, dwContext,
357 lpvStatusInformation,dwStatusInformationLength);
358 break;
359 case INTERNET_STATUS_REQUEST_SENT:
360 ok(dwStatusInformationLength == sizeof(DWORD),
361 "info length should be sizeof(DWORD) instead of %d\n",
362 dwStatusInformationLength);
363 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
364 GetCurrentThreadId(), hInternet, dwContext,
365 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
366 break;
367 case INTERNET_STATUS_RECEIVING_RESPONSE:
368 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
369 GetCurrentThreadId(), hInternet, dwContext,
370 lpvStatusInformation,dwStatusInformationLength);
371 break;
372 case INTERNET_STATUS_RESPONSE_RECEIVED:
373 ok(dwStatusInformationLength == sizeof(DWORD),
374 "info length should be sizeof(DWORD) instead of %d\n",
375 dwStatusInformationLength);
376 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
377 GetCurrentThreadId(), hInternet, dwContext,
378 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
379 break;
380 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
381 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
382 GetCurrentThreadId(), hInternet,dwContext,
383 lpvStatusInformation,dwStatusInformationLength);
384 break;
385 case INTERNET_STATUS_PREFETCH:
386 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
387 GetCurrentThreadId(), hInternet, dwContext,
388 lpvStatusInformation,dwStatusInformationLength);
389 break;
390 case INTERNET_STATUS_CLOSING_CONNECTION:
391 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
392 GetCurrentThreadId(), hInternet, dwContext,
393 lpvStatusInformation,dwStatusInformationLength);
394 break;
395 case INTERNET_STATUS_CONNECTION_CLOSED:
396 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
397 GetCurrentThreadId(), hInternet, dwContext,
398 lpvStatusInformation,dwStatusInformationLength);
399 break;
400 case INTERNET_STATUS_HANDLE_CREATED:
401 ok(dwStatusInformationLength == sizeof(HINTERNET),
402 "info length should be sizeof(HINTERNET) instead of %d\n",
403 dwStatusInformationLength);
404 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
405 GetCurrentThreadId(), hInternet, dwContext,
406 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
407 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
408 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
409 break;
410 case INTERNET_STATUS_HANDLE_CLOSING:
411 ok(dwStatusInformationLength == sizeof(HINTERNET),
412 "info length should be sizeof(HINTERNET) instead of %d\n",
413 dwStatusInformationLength);
414 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
415 GetCurrentThreadId(), hInternet, dwContext,
416 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
417 if(!InterlockedDecrement(&close_handle_cnt))
418 SetEvent(hCompleteEvent);
419 break;
420 case INTERNET_STATUS_REQUEST_COMPLETE:
421 {
422 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
423 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
424 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
425 dwStatusInformationLength);
426 ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %ld\n", iar->dwResult);
427 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
428 GetCurrentThreadId(), hInternet, dwContext,
429 iar->dwResult,iar->dwError,dwStatusInformationLength);
430 req_error = iar->dwError;
431 if(!close_handle_cnt)
432 SetEvent(hCompleteEvent);
433 break;
434 }
435 case INTERNET_STATUS_REDIRECT:
436 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
437 GetCurrentThreadId(), hInternet, dwContext,
438 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
439 *(LPSTR)lpvStatusInformation = '\0';
440 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
441 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
442 break;
443 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
444 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
445 GetCurrentThreadId(), hInternet, dwContext,
446 lpvStatusInformation, dwStatusInformationLength);
447 break;
448 default:
449 trace("%04x:Callback %p 0x%lx %d %p %d\n",
450 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
451 lpvStatusInformation, dwStatusInformationLength);
452 }
453 }
454
455 static void close_async_handle(HINTERNET handle, HANDLE complete_event, int handle_cnt)
456 {
457 BOOL res;
458
459 close_handle_cnt = handle_cnt;
460
461 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
462 res = InternetCloseHandle(handle);
463 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
464 WaitForSingleObject(hCompleteEvent, INFINITE);
465 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
466 }
467
468 static void InternetReadFile_test(int flags, const test_data_t *test)
469 {
470 char *post_data = NULL;
471 BOOL res, on_async = TRUE;
472 CHAR buffer[4000];
473 DWORD length, exlen = 0, post_len = 0;
474 const char *types[2] = { "*", NULL };
475 HINTERNET hi, hic = 0, hor = 0;
476
477 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
478
479 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
480
481 trace("InternetOpenA <--\n");
482 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
483 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
484 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
485 trace("InternetOpenA -->\n");
486
487 if (hi == 0x0) goto abort;
488
489 pInternetSetStatusCallbackA(hi,&callback);
490
491 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
492
493 trace("InternetConnectA <--\n");
494 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
495 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
496 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
497 trace("InternetConnectA -->\n");
498
499 if (hic == 0x0) goto abort;
500
501 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
502 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
503
504 trace("HttpOpenRequestA <--\n");
505 hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
506 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
507 0xdeadbead);
508 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
509 /*
510 * If the internet name can't be resolved we are probably behind
511 * a firewall or in some other way not directly connected to the
512 * Internet. Not enough reason to fail the test. Just ignore and
513 * abort.
514 */
515 } else {
516 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
517 }
518 trace("HttpOpenRequestA -->\n");
519
520 if (hor == 0x0) goto abort;
521
522 test_request_flags(hor, INTERNET_REQFLAG_NO_HEADERS);
523
524 length = sizeof(buffer);
525 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
526 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
527 ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
528
529 length = sizeof(buffer);
530 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
531 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
532 ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
533 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
534
535 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
536 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
537 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
538 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT,2);
539 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_RECEIVED,2);
540 if (first_connection_to_test_url)
541 {
542 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
543 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
544 }
545 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
546 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
547 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
548 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
549 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
550 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
551 if(test->flags & TESTF_REDIRECT) {
552 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
553 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
554 }
555 SET_EXPECT(INTERNET_STATUS_REDIRECT);
556 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
557 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
558 if (flags & INTERNET_FLAG_ASYNC)
559 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
560
561 if(test->flags & TESTF_COMPRESSED) {
562 BOOL b = TRUE;
563
564 res = InternetSetOption(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
565 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
566 "InternetSetOption failed: %u\n", GetLastError());
567 if(!res)
568 goto abort;
569 }
570
571 test_status_code(hor, 0);
572
573 trace("HttpSendRequestA -->\n");
574 if(test->post_data) {
575 post_len = strlen(test->post_data);
576 post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
577 memcpy(post_data, test->post_data, post_len);
578 }
579 SetLastError(0xdeadbeef);
580 res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
581 if (flags & INTERNET_FLAG_ASYNC)
582 ok(!res && (GetLastError() == ERROR_IO_PENDING),
583 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
584 else
585 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
586 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
587 trace("HttpSendRequestA <--\n");
588
589 if (flags & INTERNET_FLAG_ASYNC) {
590 WaitForSingleObject(hCompleteEvent, INFINITE);
591 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
592 }
593 HeapFree(GetProcessHeap(), 0, post_data);
594
595 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
596 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
597 if (first_connection_to_test_url)
598 {
599 if (! proxy_active())
600 {
601 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
602 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
603 }
604 else
605 {
606 CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
607 CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
608 }
609 }
610 else
611 {
612 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
613 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
614 }
615 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
616 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
617 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
618 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
619 if(test->flags & TESTF_REDIRECT)
620 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
621 if (flags & INTERNET_FLAG_ASYNC)
622 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
623 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
624 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
625 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
626
627 test_request_flags(hor, 0);
628
629 length = 100;
630 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
631 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
632
633 length = sizeof(buffer)-1;
634 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
635 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
636 buffer[length]=0;
637
638 length = sizeof(buffer);
639 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
640 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
641 ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
642
643 length = 16;
644 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
645 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
646 if(test->flags & TESTF_COMPRESSED)
647 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
648 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
649
650 length = 100;
651 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
652 buffer[length]=0;
653 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
654
655 length = 100;
656 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
657 buffer[length]=0;
658 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
659
660 SetLastError(0xdeadbeef);
661 res = InternetReadFile(NULL, buffer, 100, &length);
662 ok(!res, "InternetReadFile should have failed\n");
663 ok(GetLastError() == ERROR_INVALID_HANDLE,
664 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
665 GetLastError());
666
667 length = 100;
668 trace("Entering Query loop\n");
669
670 while (TRUE)
671 {
672 if (flags & INTERNET_FLAG_ASYNC)
673 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
674 length = 0;
675 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
676 if (flags & INTERNET_FLAG_ASYNC)
677 {
678 if (res)
679 {
680 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
681 if(exlen) {
682 ok(length >= exlen, "length %u < exlen %u\n", length, exlen);
683 exlen = 0;
684 }
685 }
686 else if (GetLastError() == ERROR_IO_PENDING)
687 {
688 trace("PENDING\n");
689 /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
690 if(!(test->flags & TESTF_CHUNKED))
691 ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
692 WaitForSingleObject(hCompleteEvent, INFINITE);
693 exlen = length;
694 ok(exlen, "length = 0\n");
695 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
696 ok(req_error, "req_error = 0\n");
697 continue;
698 }else {
699 ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
700 }
701 }else {
702 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
703 }
704 trace("LENGTH %d\n", length);
705 if(test->flags & TESTF_CHUNKED)
706 ok(length <= 8192, "length = %d, expected <= 8192\n", length);
707 if (length)
708 {
709 char *buffer;
710 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
711
712 res = InternetReadFile(hor,buffer,length,&length);
713
714 buffer[length]=0;
715
716 trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
717
718 if(test->content)
719 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
720 HeapFree(GetProcessHeap(),0,buffer);
721 }else {
722 ok(!on_async, "Returned zero size in response to request complete\n");
723 break;
724 }
725 on_async = FALSE;
726 }
727 if(test->flags & TESTF_REDIRECT) {
728 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
729 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
730 }
731 abort:
732 trace("aborting\n");
733 close_async_handle(hi, hCompleteEvent, 2);
734 CloseHandle(hCompleteEvent);
735 first_connection_to_test_url = FALSE;
736 }
737
738 static void InternetReadFile_chunked_test(void)
739 {
740 BOOL res;
741 CHAR buffer[4000];
742 DWORD length;
743 const char *types[2] = { "*", NULL };
744 HINTERNET hi, hic = 0, hor = 0;
745
746 trace("Starting InternetReadFile chunked test\n");
747
748 trace("InternetOpenA <--\n");
749 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
750 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
751 trace("InternetOpenA -->\n");
752
753 if (hi == 0x0) goto abort;
754
755 trace("InternetConnectA <--\n");
756 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
757 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
758 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
759 trace("InternetConnectA -->\n");
760
761 if (hic == 0x0) goto abort;
762
763 trace("HttpOpenRequestA <--\n");
764 hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
765 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
766 0xdeadbead);
767 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
768 /*
769 * If the internet name can't be resolved we are probably behind
770 * a firewall or in some other way not directly connected to the
771 * Internet. Not enough reason to fail the test. Just ignore and
772 * abort.
773 */
774 } else {
775 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
776 }
777 trace("HttpOpenRequestA -->\n");
778
779 if (hor == 0x0) goto abort;
780
781 trace("HttpSendRequestA -->\n");
782 SetLastError(0xdeadbeef);
783 res = HttpSendRequestA(hor, "", -1, NULL, 0);
784 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
785 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
786 trace("HttpSendRequestA <--\n");
787
788 test_request_flags(hor, 0);
789
790 length = 100;
791 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
792 buffer[length]=0;
793 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
794
795 SetLastError( 0xdeadbeef );
796 length = 100;
797 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
798 buffer[length]=0;
799 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
800 ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
801 "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
802 ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
803 "Wrong transfer encoding '%s'\n", buffer );
804
805 SetLastError( 0xdeadbeef );
806 length = 16;
807 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
808 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
809 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
810
811 length = 100;
812 trace("Entering Query loop\n");
813
814 while (TRUE)
815 {
816 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
817 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
818 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
819 trace("got %u available\n",length);
820 if (length)
821 {
822 DWORD got;
823 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
824
825 res = InternetReadFile(hor,buffer,length,&got);
826
827 buffer[got]=0;
828 trace("ReadFile -> %i %i\n",res,got);
829 ok( length == got, "only got %u of %u available\n", got, length );
830 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
831
832 HeapFree(GetProcessHeap(),0,buffer);
833 if (!got) break;
834 }
835 if (length == 0)
836 break;
837 }
838 abort:
839 trace("aborting\n");
840 if (hor != 0x0) {
841 res = InternetCloseHandle(hor);
842 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
843 }
844 if (hi != 0x0) {
845 res = InternetCloseHandle(hi);
846 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
847 }
848 }
849
850 static void InternetReadFileExA_test(int flags)
851 {
852 DWORD rc;
853 DWORD length;
854 const char *types[2] = { "*", NULL };
855 HINTERNET hi, hic = 0, hor = 0;
856 INTERNET_BUFFERS inetbuffers;
857
858 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
859
860 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
861
862 trace("InternetOpenA <--\n");
863 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
864 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
865 trace("InternetOpenA -->\n");
866
867 if (hi == 0x0) goto abort;
868
869 pInternetSetStatusCallbackA(hi,&callback);
870
871 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
872
873 trace("InternetConnectA <--\n");
874 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
875 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
876 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
877 trace("InternetConnectA -->\n");
878
879 if (hic == 0x0) goto abort;
880
881 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
882 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
883
884 trace("HttpOpenRequestA <--\n");
885 hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
886 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
887 0xdeadbead);
888 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
889 /*
890 * If the internet name can't be resolved we are probably behind
891 * a firewall or in some other way not directly connected to the
892 * Internet. Not enough reason to fail the test. Just ignore and
893 * abort.
894 */
895 } else {
896 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
897 }
898 trace("HttpOpenRequestA -->\n");
899
900 if (hor == 0x0) goto abort;
901
902 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
903 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
904 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
905 if (first_connection_to_test_url)
906 {
907 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
908 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
909 }
910 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT, 2);
911 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
912 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
913 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
914 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
915 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
916 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
917 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
918 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
919 SET_EXPECT(INTERNET_STATUS_REDIRECT);
920 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
921 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
922 if (flags & INTERNET_FLAG_ASYNC)
923 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
924 else
925 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
926
927 trace("HttpSendRequestA -->\n");
928 SetLastError(0xdeadbeef);
929 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
930 if (flags & INTERNET_FLAG_ASYNC)
931 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
932 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
933 else
934 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
935 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
936 trace("HttpSendRequestA <--\n");
937
938 if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
939 WaitForSingleObject(hCompleteEvent, INFINITE);
940 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
941 }
942
943 if (first_connection_to_test_url)
944 {
945 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
946 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
947 }
948 else
949 {
950 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
951 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
952 }
953 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
954 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
955 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
956 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
957 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
958 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
959 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
960 if (flags & INTERNET_FLAG_ASYNC)
961 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
962 else
963 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
964 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
965 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
966 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
967 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
968
969 /* tests invalid dwStructSize */
970 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
971 inetbuffers.lpcszHeader = NULL;
972 inetbuffers.dwHeadersLength = 0;
973 inetbuffers.dwBufferLength = 10;
974 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
975 inetbuffers.dwOffsetHigh = 1234;
976 inetbuffers.dwOffsetLow = 5678;
977 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
978 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
979 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
980 rc ? "TRUE" : "FALSE", GetLastError());
981 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
982
983 test_request_flags(hor, 0);
984
985 /* tests to see whether lpcszHeader is used - it isn't */
986 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
987 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
988 inetbuffers.dwHeadersLength = 255;
989 inetbuffers.dwBufferLength = 0;
990 inetbuffers.lpvBuffer = NULL;
991 inetbuffers.dwOffsetHigh = 1234;
992 inetbuffers.dwOffsetLow = 5678;
993 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
994 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
995 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
996 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
997 trace("read %i bytes\n", inetbuffers.dwBufferLength);
998 todo_wine
999 {
1000 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1001 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1002 }
1003
1004 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
1005 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
1006 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
1007 rc ? "TRUE" : "FALSE", GetLastError());
1008
1009 length = 0;
1010 trace("Entering Query loop\n");
1011
1012 while (TRUE)
1013 {
1014 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
1015 inetbuffers.dwBufferLength = 1024;
1016 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
1017 inetbuffers.dwOffsetHigh = 1234;
1018 inetbuffers.dwOffsetLow = 5678;
1019
1020 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1021 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1022 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1023 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
1024 if (!rc)
1025 {
1026 if (GetLastError() == ERROR_IO_PENDING)
1027 {
1028 trace("InternetReadFileEx -> PENDING\n");
1029 ok(flags & INTERNET_FLAG_ASYNC,
1030 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
1031 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1032 WaitForSingleObject(hCompleteEvent, INFINITE);
1033 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1034 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1035 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1036 }
1037 else
1038 {
1039 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
1040 break;
1041 }
1042 }
1043 else
1044 {
1045 trace("InternetReadFileEx -> SUCCEEDED\n");
1046 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1047 if (inetbuffers.dwBufferLength)
1048 {
1049 todo_wine {
1050 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1051 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1052 }
1053 }
1054 else
1055 {
1056 /* Win98 still sends these when 0 bytes are read, WinXP does not */
1057 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1058 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1059 }
1060 }
1061
1062 trace("read %i bytes\n", inetbuffers.dwBufferLength);
1063 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
1064
1065 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
1066 "InternetReadFileEx sets offsets to 0x%x%08x\n",
1067 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
1068
1069 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1070
1071 if (!inetbuffers.dwBufferLength)
1072 break;
1073
1074 length += inetbuffers.dwBufferLength;
1075 }
1076 ok(length > 0, "failed to read any of the document\n");
1077 trace("Finished. Read %d bytes\n", length);
1078
1079 abort:
1080 close_async_handle(hi, hCompleteEvent, 2);
1081 CloseHandle(hCompleteEvent);
1082 first_connection_to_test_url = FALSE;
1083 }
1084
1085 static void InternetOpenUrlA_test(void)
1086 {
1087 HINTERNET myhinternet, myhttp;
1088 char buffer[0x400];
1089 DWORD size, readbytes, totalbytes=0;
1090 BOOL ret;
1091
1092 ret = DeleteUrlCacheEntry(TEST_URL);
1093 ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND,
1094 "DeleteUrlCacheEntry returned %x, GetLastError() = %d\n", ret, GetLastError());
1095
1096 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1097 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
1098 size = 0x400;
1099 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
1100 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1101
1102 SetLastError(0);
1103 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
1104 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1105 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1106 return; /* WinXP returns this when not connected to the net */
1107 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1108 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1109 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1110 totalbytes += readbytes;
1111 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1112 totalbytes += readbytes;
1113 trace("read 0x%08x bytes\n",totalbytes);
1114
1115 InternetCloseHandle(myhttp);
1116 InternetCloseHandle(myhinternet);
1117
1118 ret = DeleteUrlCacheEntry(TEST_URL);
1119 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "INTERNET_FLAG_NO_CACHE_WRITE flag doesn't work\n");
1120 }
1121
1122 static void HttpSendRequestEx_test(void)
1123 {
1124 HINTERNET hSession;
1125 HINTERNET hConnect;
1126 HINTERNET hRequest;
1127
1128 INTERNET_BUFFERS BufferIn;
1129 DWORD dwBytesWritten, dwBytesRead, error;
1130 CHAR szBuffer[256];
1131 int i;
1132 BOOL ret;
1133
1134 static char szPostData[] = "mode=Test";
1135 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1136
1137 hSession = InternetOpen("Wine Regression Test",
1138 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1139 ok( hSession != NULL ,"Unable to open Internet session\n");
1140 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1141 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1142 0);
1143 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1144 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1145 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1146 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1147 {
1148 skip( "Network unreachable, skipping test\n" );
1149 goto done;
1150 }
1151 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1152
1153 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1154
1155 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
1156 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
1157 BufferIn.lpcszHeader = szContentType;
1158 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1159 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1160 BufferIn.lpvBuffer = szPostData;
1161 BufferIn.dwBufferLength = 3;
1162 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1163 BufferIn.dwOffsetLow = 0;
1164 BufferIn.dwOffsetHigh = 0;
1165
1166 SetLastError(0xdeadbeef);
1167 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
1168 error = GetLastError();
1169 ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1170 ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1171
1172 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1173
1174 for (i = 3; szPostData[i]; i++)
1175 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1176 "InternetWriteFile failed\n");
1177
1178 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1179
1180 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1181
1182 test_request_flags(hRequest, 0);
1183
1184 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1185 "Unable to read response\n");
1186 szBuffer[dwBytesRead] = 0;
1187
1188 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1189 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1190
1191 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1192 done:
1193 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1194 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1195 }
1196
1197 static void InternetOpenRequest_test(void)
1198 {
1199 HINTERNET session, connect, request;
1200 static const char *types[] = { "*", "", NULL };
1201 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1202 static const WCHAR *typesW[] = { any, empty, NULL };
1203 BOOL ret;
1204
1205 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1206 ok(session != NULL ,"Unable to open Internet session\n");
1207
1208 connect = InternetConnectA(session, NULL, 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 NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1212
1213 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1214 INTERNET_SERVICE_HTTP, 0, 0);
1215 ok(connect == NULL, "InternetConnectA should have failed\n");
1216 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1217
1218 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1219 INTERNET_SERVICE_HTTP, 0, 0);
1220 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1221
1222 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1223 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1224 {
1225 skip( "Network unreachable, skipping test\n" );
1226 goto done;
1227 }
1228 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1229
1230 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1231 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1232 ok(InternetCloseHandle(request), "Close request handle failed\n");
1233
1234 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1235 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1236
1237 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1238 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1239 ok(InternetCloseHandle(request), "Close request handle failed\n");
1240
1241 done:
1242 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1243 ok(InternetCloseHandle(session), "Close session handle failed\n");
1244 }
1245
1246 static void test_cache_read(void)
1247 {
1248 HINTERNET session, connection, req;
1249 FILETIME now, tomorrow, yesterday;
1250 BYTE content[1000], buf[2000];
1251 char file_path[MAX_PATH];
1252 ULARGE_INTEGER li;
1253 HANDLE file;
1254 DWORD size;
1255 unsigned i;
1256 BOOL res;
1257
1258 static const char cache_only_url[] = "http://test.winehq.org/tests/cache-only";
1259 BYTE cache_headers[] = "HTTP/1.1 200 OK\r\n\r\n";
1260
1261 trace("Testing cache read...\n");
1262
1263 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
1264
1265 for(i = 0; i < sizeof(content); i++)
1266 content[i] = '0' + (i%10);
1267
1268 GetSystemTimeAsFileTime(&now);
1269 li.u.HighPart = now.dwHighDateTime;
1270 li.u.LowPart = now.dwLowDateTime;
1271 li.QuadPart += (LONGLONG)10000000 * 3600 * 24;
1272 tomorrow.dwHighDateTime = li.u.HighPart;
1273 tomorrow.dwLowDateTime = li.u.LowPart;
1274 li.QuadPart -= (LONGLONG)10000000 * 3600 * 24 * 2;
1275 yesterday.dwHighDateTime = li.u.HighPart;
1276 yesterday.dwLowDateTime = li.u.LowPart;
1277
1278 res = CreateUrlCacheEntryA(cache_only_url, sizeof(content), "", file_path, 0);
1279 ok(res, "CreateUrlCacheEntryA failed: %u\n", GetLastError());
1280
1281 file = CreateFileA(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1282 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1283
1284 WriteFile(file, content, sizeof(content), &size, NULL);
1285 CloseHandle(file);
1286
1287 res = CommitUrlCacheEntryA(cache_only_url, file_path, tomorrow, yesterday, NORMAL_CACHE_ENTRY,
1288 cache_headers, sizeof(cache_headers)-1, "", 0);
1289 ok(res, "CommitUrlCacheEntryA failed: %u\n", GetLastError());
1290
1291 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
1292 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
1293
1294 pInternetSetStatusCallbackA(session, callback);
1295
1296 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1297 connection = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT,
1298 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1299 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
1300 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1301
1302 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1303 req = HttpOpenRequestA(connection, "GET", "/tests/cache-only", NULL, NULL, NULL, 0, 0xdeadbead);
1304 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1305 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1306
1307 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
1308 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
1309 SET_WINE_ALLOW(INTERNET_STATUS_SENDING_REQUEST);
1310 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_SENT);
1311 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1312 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1313 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1314
1315 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
1316 todo_wine
1317 ok(res, "HttpSendRequest failed: %u\n", GetLastError());
1318
1319 if(res) {
1320 size = 0;
1321 res = InternetQueryDataAvailable(req, &size, 0, 0);
1322 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
1323 ok(size == sizeof(content), "size = %u\n", size);
1324
1325 size = sizeof(buf);
1326 res = InternetReadFile(req, buf, sizeof(buf), &size);
1327 ok(res, "InternetReadFile failed: %u\n", GetLastError());
1328 ok(size == sizeof(content), "size = %u\n", size);
1329 ok(!memcmp(content, buf, sizeof(content)), "unexpected content\n");
1330 }
1331
1332 close_async_handle(session, hCompleteEvent, 2);
1333
1334 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1335 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1336 CLEAR_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
1337 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
1338 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1339 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1340 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1341
1342 res = DeleteUrlCacheEntryA(cache_only_url);
1343 ok(res, "DeleteUrlCacheEntryA failed: %u\n", GetLastError());
1344
1345 CloseHandle(hCompleteEvent);
1346 }
1347
1348 static void test_http_cache(void)
1349 {
1350 HINTERNET session, connect, request;
1351 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1352 DWORD size, file_size;
1353 BYTE buf[100];
1354 HANDLE file;
1355 BOOL ret;
1356
1357 static const char *types[] = { "*", "", NULL };
1358
1359 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1360 ok(session != NULL ,"Unable to open Internet session\n");
1361
1362 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1363 INTERNET_SERVICE_HTTP, 0, 0);
1364 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1365
1366 request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1367 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1368 {
1369 skip( "Network unreachable, skipping test\n" );
1370
1371 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1372 ok(InternetCloseHandle(session), "Close session handle failed\n");
1373
1374 return;
1375 }
1376 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1377
1378 size = sizeof(url);
1379 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1380 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1381 ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1382
1383 size = sizeof(file_name);
1384 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1385 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1386 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1387 ok(!size, "size = %d\n", size);
1388
1389 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1390 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1391
1392 size = sizeof(file_name);
1393 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1394 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1395
1396 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1397 FILE_ATTRIBUTE_NORMAL, NULL);
1398 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1399 file_size = GetFileSize(file, NULL);
1400 ok(file_size == 106, "file size = %u\n", file_size);
1401
1402 size = sizeof(buf);
1403 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1404 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1405 ok(size == 100, "size = %u\n", size);
1406
1407 file_size = GetFileSize(file, NULL);
1408 ok(file_size == 106, "file size = %u\n", file_size);
1409 CloseHandle(file);
1410
1411 ok(InternetCloseHandle(request), "Close request handle failed\n");
1412
1413 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1414 FILE_ATTRIBUTE_NORMAL, NULL);
1415 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1416 CloseHandle(file);
1417
1418 /* Send the same request, requiring it to be retrieved from the cache */
1419 request = HttpOpenRequest(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1420
1421 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1422 ok(ret, "HttpSendRequest failed\n");
1423
1424 size = sizeof(buf);
1425 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1426 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1427 ok(size == 100, "size = %u\n", size);
1428
1429 ok(InternetCloseHandle(request), "Close request handle failed\n");
1430
1431 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1432 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1433
1434 size = sizeof(file_name);
1435 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1436 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1437 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1438 ok(!size, "size = %d\n", size);
1439
1440 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1441 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1442
1443 size = sizeof(file_name);
1444 file_name[0] = 0;
1445 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1446 if (ret)
1447 {
1448 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1449 FILE_ATTRIBUTE_NORMAL, NULL);
1450 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1451 CloseHandle(file);
1452 }
1453 else
1454 {
1455 /* < IE8 */
1456 ok(file_name[0] == 0, "Didn't expect a file name\n");
1457 }
1458
1459 ok(InternetCloseHandle(request), "Close request handle failed\n");
1460 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1461 ok(InternetCloseHandle(session), "Close session handle failed\n");
1462
1463 test_cache_read();
1464 }
1465
1466 static void HttpHeaders_test(void)
1467 {
1468 HINTERNET hSession;
1469 HINTERNET hConnect;
1470 HINTERNET hRequest;
1471 CHAR buffer[256];
1472 WCHAR wbuffer[256];
1473 DWORD len = 256;
1474 DWORD oldlen;
1475 DWORD index = 0;
1476
1477 hSession = InternetOpen("Wine Regression Test",
1478 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1479 ok( hSession != NULL ,"Unable to open Internet session\n");
1480 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1481 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1482 0);
1483 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1484 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1485 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1486 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1487 {
1488 skip( "Network unreachable, skipping test\n" );
1489 goto done;
1490 }
1491 ok( hRequest != NULL, "Failed to open request handle\n");
1492
1493 index = 0;
1494 len = sizeof(buffer);
1495 strcpy(buffer,"Warning");
1496 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1497 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1498
1499 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1500 "Failed to add new header\n");
1501
1502 index = 0;
1503 len = sizeof(buffer);
1504 strcpy(buffer,"Warning");
1505 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1506 buffer,&len,&index),"Unable to query header\n");
1507 ok(index == 1, "Index was not incremented\n");
1508 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1509 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1510 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1511 len = sizeof(buffer);
1512 strcpy(buffer,"Warning");
1513 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1514 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1515
1516 index = 0;
1517 len = 5; /* could store the string but not the NULL terminator */
1518 strcpy(buffer,"Warning");
1519 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1520 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1521 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1522 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1523
1524 /* a call with NULL will fail but will return the length */
1525 index = 0;
1526 len = sizeof(buffer);
1527 SetLastError(0xdeadbeef);
1528 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1529 NULL,&len,&index) == FALSE,"Query worked\n");
1530 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1531 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1532 ok(index == 0, "Index was incremented\n");
1533
1534 /* even for a len that is too small */
1535 index = 0;
1536 len = 15;
1537 SetLastError(0xdeadbeef);
1538 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1539 NULL,&len,&index) == FALSE,"Query worked\n");
1540 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1541 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1542 ok(index == 0, "Index was incremented\n");
1543
1544 index = 0;
1545 len = 0;
1546 SetLastError(0xdeadbeef);
1547 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1548 NULL,&len,&index) == FALSE,"Query worked\n");
1549 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1550 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1551 ok(index == 0, "Index was incremented\n");
1552 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1553
1554
1555 /* a working query */
1556 index = 0;
1557 len = sizeof(buffer);
1558 memset(buffer, 'x', sizeof(buffer));
1559 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1560 buffer,&len,&index),"Unable to query header\n");
1561 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1562 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1563 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1564 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1565 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1566 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1567 ok(index == 0, "Index was incremented\n");
1568
1569 /* Like above two tests, but for W version */
1570
1571 index = 0;
1572 len = 0;
1573 SetLastError(0xdeadbeef);
1574 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1575 NULL,&len,&index) == FALSE,"Query worked\n");
1576 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1577 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1578 ok(index == 0, "Index was incremented\n");
1579 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1580
1581 /* a working query */
1582 index = 0;
1583 len = sizeof(wbuffer);
1584 memset(wbuffer, 'x', sizeof(wbuffer));
1585 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1586 wbuffer,&len,&index),"Unable to query header\n");
1587 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1588 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1589 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1590 ok(index == 0, "Index was incremented\n");
1591
1592 /* end of W version tests */
1593
1594 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1595 index = 0;
1596 len = sizeof(buffer);
1597 memset(buffer, 'x', sizeof(buffer));
1598 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1599 buffer,&len,&index) == TRUE,"Query failed\n");
1600 ok(len == 2, "Expected 2, got %d\n", len);
1601 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1602 ok(index == 0, "Index was incremented\n");
1603
1604 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1605 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1606
1607 index = 0;
1608 len = sizeof(buffer);
1609 strcpy(buffer,"Warning");
1610 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1611 buffer,&len,&index),"Unable to query header\n");
1612 ok(index == 1, "Index was not incremented\n");
1613 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1614 len = sizeof(buffer);
1615 strcpy(buffer,"Warning");
1616 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1617 buffer,&len,&index),"Failed to get second header\n");
1618 ok(index == 2, "Index was not incremented\n");
1619 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1620 len = sizeof(buffer);
1621 strcpy(buffer,"Warning");
1622 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1623 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1624
1625 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1626
1627 index = 0;
1628 len = sizeof(buffer);
1629 strcpy(buffer,"Warning");
1630 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1631 buffer,&len,&index),"Unable to query header\n");
1632 ok(index == 1, "Index was not incremented\n");
1633 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1634 len = sizeof(buffer);
1635 strcpy(buffer,"Warning");
1636 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1637 buffer,&len,&index),"Failed to get second header\n");
1638 ok(index == 2, "Index was not incremented\n");
1639 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1640 len = sizeof(buffer);
1641 strcpy(buffer,"Warning");
1642 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1643 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1644
1645 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1646
1647 index = 0;
1648 len = sizeof(buffer);
1649 strcpy(buffer,"Warning");
1650 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1651 buffer,&len,&index),"Unable to query header\n");
1652 ok(index == 1, "Index was not incremented\n");
1653 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1654 len = sizeof(buffer);
1655 strcpy(buffer,"Warning");
1656 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1657 buffer,&len,&index),"Failed to get second header\n");
1658 ok(index == 2, "Index was not incremented\n");
1659 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1660 len = sizeof(buffer);
1661 strcpy(buffer,"Warning");
1662 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1663 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1664
1665 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1666
1667 index = 0;
1668 len = sizeof(buffer);
1669 strcpy(buffer,"Warning");
1670 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1671 buffer,&len,&index),"Unable to query header\n");
1672 ok(index == 1, "Index was not incremented\n");
1673 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1674 len = sizeof(buffer);
1675 strcpy(buffer,"Warning");
1676 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1677 ok(index == 2, "Index was not incremented\n");
1678 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1679 len = sizeof(buffer);
1680 strcpy(buffer,"Warning");
1681 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1682
1683 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1684
1685 index = 0;
1686 len = sizeof(buffer);
1687 strcpy(buffer,"Warning");
1688 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1689 ok(index == 1, "Index was not incremented\n");
1690 ok(strcmp(buffer,"test2, test4, test5")==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, buffer,&len,&index),"Failed to get second header\n");
1694 ok(index == 2, "Index was not incremented\n");
1695 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1696 len = sizeof(buffer);
1697 strcpy(buffer,"Warning");
1698 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1699
1700 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1701
1702 index = 0;
1703 len = sizeof(buffer);
1704 strcpy(buffer,"Warning");
1705 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1706 ok(index == 1, "Index was not incremented\n");
1707 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1708 len = sizeof(buffer);
1709 strcpy(buffer,"Warning");
1710 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1711 ok(index == 2, "Index was not incremented\n");
1712 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1713 len = sizeof(buffer);
1714 strcpy(buffer,"Warning");
1715 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1716
1717 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");
1718
1719 index = 0;
1720 len = sizeof(buffer);
1721 strcpy(buffer,"Warning");
1722 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1723 ok(index == 1, "Index was not incremented\n");
1724 ok(strcmp(buffer,"test3")==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, buffer,&len,&index),"Failed to get second header\n");
1728 ok(index == 2, "Index was not incremented\n");
1729 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1730 len = sizeof(buffer);
1731 strcpy(buffer,"Warning");
1732 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1733
1734 /* Ensure that blank headers are ignored and don't cause a failure */
1735 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");
1736
1737 index = 0;
1738 len = sizeof(buffer);
1739 strcpy(buffer,"BlankTest");
1740 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1741 ok(index == 1, "Index was not incremented\n");
1742 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1743
1744 /* Ensure that malformed header separators are ignored and don't cause a failure */
1745 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),
1746 "Failed to add header with malformed entries in list\n");
1747
1748 index = 0;
1749 len = sizeof(buffer);
1750 strcpy(buffer,"MalformedTest");
1751 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1752 ok(index == 1, "Index was not incremented\n");
1753 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1754 index = 0;
1755 len = sizeof(buffer);
1756 strcpy(buffer,"MalformedTestTwo");
1757 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1758 ok(index == 1, "Index was not incremented\n");
1759 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1760 index = 0;
1761 len = sizeof(buffer);
1762 strcpy(buffer,"MalformedTestThree");
1763 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1764 ok(index == 1, "Index was not incremented\n");
1765 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1766
1767 ok(HttpAddRequestHeaders(hRequest, "Authorization: Basic\r\n", -1, HTTP_ADDREQ_FLAG_ADD),
1768 "unable to add header %u\n", GetLastError());
1769
1770 index = 0;
1771 buffer[0] = 0;
1772 len = sizeof(buffer);
1773 ok(HttpQueryInfo(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index),
1774 "unable to query header %u\n", GetLastError());
1775 ok(index == 1, "index was not incremented\n");
1776 ok(!strcmp(buffer, "Basic"), "incorrect string was returned (%s)\n", buffer);
1777
1778 ok(HttpAddRequestHeaders(hRequest, "Authorization:\r\n", -1, HTTP_ADDREQ_FLAG_REPLACE),
1779 "unable to remove header %u\n", GetLastError());
1780
1781 index = 0;
1782 len = sizeof(buffer);
1783 SetLastError(0xdeadbeef);
1784 ok(!HttpQueryInfo(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index),
1785 "header still present\n");
1786 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "got %u\n", GetLastError());
1787
1788 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1789 done:
1790 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1791 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1792 }
1793
1794 static const char garbagemsg[] =
1795 "Garbage: Header\r\n";
1796
1797 static const char contmsg[] =
1798 "HTTP/1.1 100 Continue\r\n";
1799
1800 static const char expandcontmsg[] =
1801 "HTTP/1.1 100 Continue\r\n"
1802 "Server: winecontinue\r\n"
1803 "Tag: something witty\r\n"
1804 "\r\n";
1805
1806 static const char okmsg[] =
1807 "HTTP/1.1 200 OK\r\n"
1808 "Server: winetest\r\n"
1809 "\r\n";
1810
1811 static const char okmsg2[] =
1812 "HTTP/1.1 200 OK\r\n"
1813 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1814 "Server: winetest\r\n"
1815 "Content-Length: 0\r\n"
1816 "Set-Cookie: one\r\n"
1817 "Set-Cookie: two\r\n"
1818 "\r\n";
1819
1820 static const char notokmsg[] =
1821 "HTTP/1.1 400 Bad Request\r\n"
1822 "Server: winetest\r\n"
1823 "\r\n";
1824
1825 static const char noauthmsg[] =
1826 "HTTP/1.1 401 Unauthorized\r\n"
1827 "Server: winetest\r\n"
1828 "Connection: close\r\n"
1829 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1830 "\r\n";
1831
1832 static const char noauthmsg2[] =
1833 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1834 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1835 "\0d`0|6\n"
1836 "Server: winetest\r\n";
1837
1838 static const char proxymsg[] =
1839 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1840 "Server: winetest\r\n"
1841 "Proxy-Connection: close\r\n"
1842 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1843 "\r\n";
1844
1845 static const char page1[] =
1846 "<HTML>\r\n"
1847 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1848 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1849 "</HTML>\r\n\r\n";
1850
1851 static const char ok_with_length[] =
1852 "HTTP/1.1 200 OK\r\n"
1853 "Connection: Keep-Alive\r\n"
1854 "Content-Length: 23\r\n\r\n"
1855 "abc\r\nHTTP/1.1 211 OK\r\n\r\n";
1856
1857 static const char ok_with_length2[] =
1858 "HTTP/1.1 210 OK\r\n"
1859 "Connection: Keep-Alive\r\n"
1860 "Content-Length: 24\r\n\r\n"
1861 "abc\r\nHTTP/1.1 211 OK\r\n\r\n";
1862
1863 struct server_info {
1864 HANDLE hEvent;
1865 int port;
1866 };
1867
1868 static int test_cache_gzip;
1869 static const char *send_buffer;
1870
1871 static DWORD CALLBACK server_thread(LPVOID param)
1872 {
1873 struct server_info *si = param;
1874 int r, c, i, on, count = 0;
1875 SOCKET s;
1876 struct sockaddr_in sa;
1877 char buffer[0x100];
1878 WSADATA wsaData;
1879 int last_request = 0;
1880 char host_header[22];
1881 static int test_b = 0;
1882 static int test_no_cache = 0;
1883
1884 WSAStartup(MAKEWORD(1,1), &wsaData);
1885
1886 s = socket(AF_INET, SOCK_STREAM, 0);
1887 if (s == INVALID_SOCKET)
1888 return 1;
1889
1890 on = 1;
1891 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1892
1893 memset(&sa, 0, sizeof sa);
1894 sa.sin_family = AF_INET;
1895 sa.sin_port = htons(si->port);
1896 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1897
1898 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1899 if (r<0)
1900 return 1;
1901
1902 listen(s, 0);
1903
1904 SetEvent(si->hEvent);
1905
1906 sprintf(host_header, "Host: localhost:%d", si->port);
1907
1908 do
1909 {
1910 c = accept(s, NULL, NULL);
1911
1912 memset(buffer, 0, sizeof buffer);
1913 for(i=0; i<(sizeof buffer-1); i++)
1914 {
1915 r = recv(c, &buffer[i], 1, 0);
1916 if (r != 1)
1917 break;
1918 if (i<4) continue;
1919 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1920 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1921 break;
1922 }
1923 if (strstr(buffer, "GET /test1"))
1924 {
1925 if (!strstr(buffer, "Content-Length: 0"))
1926 {
1927 send(c, okmsg, sizeof okmsg-1, 0);
1928 send(c, page1, sizeof page1-1, 0);
1929 }
1930 else
1931 send(c, notokmsg, sizeof notokmsg-1, 0);
1932 }
1933 if (strstr(buffer, "/test2"))
1934 {
1935 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1936 {
1937 send(c, okmsg, sizeof okmsg-1, 0);
1938 send(c, page1, sizeof page1-1, 0);
1939 }
1940 else
1941 send(c, proxymsg, sizeof proxymsg-1, 0);
1942 }
1943 if (strstr(buffer, "/test3"))
1944 {
1945 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1946 send(c, okmsg, sizeof okmsg-1, 0);
1947 else
1948 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1949 }
1950 if (strstr(buffer, "/test4"))
1951 {
1952 if (strstr(buffer, "Connection: Close"))
1953 send(c, okmsg, sizeof okmsg-1, 0);
1954 else
1955 send(c, notokmsg, sizeof notokmsg-1, 0);
1956 }
1957 if (strstr(buffer, "POST /test5") ||
1958 strstr(buffer, "RPC_IN_DATA /test5") ||
1959 strstr(buffer, "RPC_OUT_DATA /test5"))
1960 {
1961 if (strstr(buffer, "Content-Length: 0"))
1962 {
1963 send(c, okmsg, sizeof okmsg-1, 0);
1964 send(c, page1, sizeof page1-1, 0);
1965 }
1966 else
1967 send(c, notokmsg, sizeof notokmsg-1, 0);
1968 }
1969 if (strstr(buffer, "GET /test6"))
1970 {
1971 send(c, contmsg, sizeof contmsg-1, 0);
1972 send(c, contmsg, sizeof contmsg-1, 0);
1973 send(c, okmsg, sizeof okmsg-1, 0);
1974 send(c, page1, sizeof page1-1, 0);
1975 }
1976 if (strstr(buffer, "POST /test7"))
1977 {
1978 if (strstr(buffer, "Content-Length: 100"))
1979 {
1980 send(c, okmsg, sizeof okmsg-1, 0);
1981 send(c, page1, sizeof page1-1, 0);
1982 }
1983 else
1984 send(c, notokmsg, sizeof notokmsg-1, 0);
1985 }
1986 if (strstr(buffer, "/test8"))
1987 {
1988 if (!strstr(buffer, "Connection: Close") &&
1989 strstr(buffer, "Connection: Keep-Alive") &&
1990 !strstr(buffer, "Cache-Control: no-cache") &&
1991 !strstr(buffer, "Pragma: no-cache") &&
1992 strstr(buffer, host_header))
1993 send(c, okmsg, sizeof okmsg-1, 0);
1994 else
1995 send(c, notokmsg, sizeof notokmsg-1, 0);
1996 }
1997 if (strstr(buffer, "/test9"))
1998 {
1999 if (!strstr(buffer, "Connection: Close") &&
2000 !strstr(buffer, "Connection: Keep-Alive") &&
2001 !strstr(buffer, "Cache-Control: no-cache") &&
2002 !strstr(buffer, "Pragma: no-cache") &&
2003 strstr(buffer, host_header))
2004 send(c, okmsg, sizeof okmsg-1, 0);
2005 else
2006 send(c, notokmsg, sizeof notokmsg-1, 0);
2007 }
2008 if (strstr(buffer, "/testA"))
2009 {
2010 if (!strstr(buffer, "Connection: Close") &&
2011 !strstr(buffer, "Connection: Keep-Alive") &&
2012 (strstr(buffer, "Cache-Control: no-cache") ||
2013 strstr(buffer, "Pragma: no-cache")) &&
2014 strstr(buffer, host_header))
2015 send(c, okmsg, sizeof okmsg-1, 0);
2016 else
2017 send(c, notokmsg, sizeof notokmsg-1, 0);
2018 }
2019 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
2020 {
2021 test_b = 1;
2022 send(c, okmsg, sizeof okmsg-1, 0);
2023 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
2024 send(c, okmsg, sizeof okmsg-1, 0);
2025 }
2026 if (strstr(buffer, "/testC"))
2027 {
2028 if (strstr(buffer, "Cookie: cookie=biscuit"))
2029 send(c, okmsg, sizeof okmsg-1, 0);
2030 else
2031 send(c, notokmsg, sizeof notokmsg-1, 0);
2032 }
2033 if (strstr(buffer, "/testD"))
2034 {
2035 send(c, okmsg2, sizeof okmsg2-1, 0);
2036 }
2037 if (strstr(buffer, "/testE"))
2038 {
2039 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
2040 }
2041 if (strstr(buffer, "GET /quit"))
2042 {
2043 send(c, okmsg, sizeof okmsg-1, 0);
2044 send(c, page1, sizeof page1-1, 0);
2045 last_request = 1;
2046 }
2047 if (strstr(buffer, "GET /testF"))
2048 {
2049 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
2050 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2051 send(c, contmsg, sizeof contmsg-1, 0);
2052 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2053 send(c, okmsg, sizeof okmsg-1, 0);
2054 send(c, page1, sizeof page1-1, 0);
2055 }
2056 if (strstr(buffer, "GET /testG"))
2057 {
2058 send(c, page1, sizeof page1-1, 0);
2059 }
2060
2061 if (strstr(buffer, "GET /testJ"))
2062 {
2063 if (count == 0)
2064 {
2065 count++;
2066 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2067 }
2068 else
2069 {
2070 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2071 count = 0;
2072 }
2073 }
2074
2075 if (strstr(buffer, "GET /test_no_content"))
2076 {
2077 static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n";
2078 send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2079 }
2080 if (strstr(buffer, "GET /test_conn_close"))
2081 {
2082 static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
2083 send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
2084 WaitForSingleObject(conn_close_event, INFINITE);
2085 trace("closing connection\n");
2086 }
2087 if (strstr(buffer, "GET /test_cache_control_no_cache"))
2088 {
2089 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\n\r\nsome content";
2090 if(!test_no_cache++)
2091 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2092 else
2093 send(c, okmsg, sizeof(okmsg)-1, 0);
2094 }
2095 if (strstr(buffer, "GET /test_cache_control_no_store"))
2096 {
2097 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: junk, \t No-StOrE\r\n\r\nsome content";
2098 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2099 }
2100 if (strstr(buffer, "GET /test_cache_gzip"))
2101 {
2102 static const char gzip_response[] = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\n\r\n"
2103 "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xaf\xca\x2c\x50\x28"
2104 "\x49\x2d\x2e\xe1\x02\x00\x62\x92\xc7\x6c\x0a\x00\x00\x00";
2105 if(!test_cache_gzip++)
2106 send(c, gzip_response, sizeof(gzip_response), 0);
2107 else
2108 send(c, notokmsg, sizeof(notokmsg)-1, 0);
2109 }
2110 if (strstr(buffer, "GET /send_from_buffer"))
2111 send(c, send_buffer, strlen(send_buffer), 0);
2112 if (strstr(buffer, "/test_cache_control_verb"))
2113 {
2114 if (!memcmp(buffer, "GET ", sizeof("GET ")-1) &&
2115 !strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2116 else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2117 send(c, notokmsg, sizeof(notokmsg)-1, 0);
2118 }
2119 if (strstr(buffer, "GET /test_premature_disconnect"))
2120 trace("closing connection\n");
2121
2122 shutdown(c, 2);
2123 closesocket(c);
2124 } while (!last_request);
2125
2126 closesocket(s);
2127
2128 return 0;
2129 }
2130
2131 static void test_basic_request(int port, const char *verb, const char *url)
2132 {
2133 HINTERNET hi, hc, hr;
2134 DWORD r, count;
2135 char buffer[0x100];
2136
2137 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2138 ok(hi != NULL, "open failed\n");
2139
2140 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2141 ok(hc != NULL, "connect failed\n");
2142
2143 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
2144 ok(hr != NULL, "HttpOpenRequest failed\n");
2145
2146 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2147 ok(r, "HttpSendRequest failed\n");
2148
2149 count = 0;
2150 memset(buffer, 0, sizeof buffer);
2151 SetLastError(0xdeadbeef);
2152 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2153 ok(r, "InternetReadFile failed %u\n", GetLastError());
2154 ok(count == sizeof page1 - 1, "count was wrong\n");
2155 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
2156
2157 InternetCloseHandle(hr);
2158 InternetCloseHandle(hc);
2159 InternetCloseHandle(hi);
2160 }
2161
2162 static void test_last_error(int port)
2163 {
2164 HINTERNET hi, hc, hr;
2165 DWORD error;
2166 BOOL r;
2167
2168 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2169 ok(hi != NULL, "open failed\n");
2170
2171 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2172 ok(hc != NULL, "connect failed\n");
2173
2174 hr = HttpOpenRequest(hc, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2175 ok(hr != NULL, "HttpOpenRequest failed\n");
2176
2177 SetLastError(0xdeadbeef);
2178 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2179 error = GetLastError();
2180 ok(r, "HttpSendRequest failed\n");
2181 ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
2182
2183 InternetCloseHandle(hr);
2184 InternetCloseHandle(hc);
2185 InternetCloseHandle(hi);
2186 }
2187
2188 static void test_proxy_indirect(int port)
2189 {
2190 HINTERNET hi, hc, hr;
2191 DWORD r, sz;
2192 char buffer[0x40];
2193
2194 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
2195 ok(hi != NULL, "open failed\n");
2196
2197 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2198 ok(hc != NULL, "connect failed\n");
2199
2200 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
2201 ok(hr != NULL, "HttpOpenRequest failed\n");
2202
2203 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2204 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2205
2206 sz = sizeof buffer;
2207 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
2208 ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
2209 if (!r)
2210 {
2211 skip("missing proxy header, not testing remaining proxy headers\n");
2212 goto out;
2213 }
2214 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
2215
2216 test_status_code(hr, 407);
2217 test_request_flags(hr, 0);
2218
2219 sz = sizeof buffer;
2220 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
2221 ok(r, "HttpQueryInfo failed\n");
2222 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
2223
2224 sz = sizeof buffer;
2225 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
2226 ok(r, "HttpQueryInfo failed\n");
2227 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
2228
2229 sz = sizeof buffer;
2230 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
2231 ok(r, "HttpQueryInfo failed\n");
2232 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
2233
2234 sz = sizeof buffer;
2235 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
2236 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
2237 ok(r == FALSE, "HttpQueryInfo failed\n");
2238
2239 out:
2240 InternetCloseHandle(hr);
2241 InternetCloseHandle(hc);
2242 InternetCloseHandle(hi);
2243 }
2244
2245 static void test_proxy_direct(int port)
2246 {
2247 HINTERNET hi, hc, hr;
2248 DWORD r, sz, error;
2249 char buffer[0x40], *url;
2250 WCHAR bufferW[0x40];
2251 static CHAR username[] = "mike",
2252 password[] = "1101",
2253 useragent[] = "winetest",
2254 url_fmt[] = "http://test.winehq.org:%u/test2";
2255 static WCHAR usernameW[] = {'m','i','k','e',0},
2256 passwordW[] = {'1','1','0','1',0},
2257 useragentW[] = {'w','i','n','e','t','e','s','t',0};
2258
2259 /* specify proxy type without the proxy and bypass */
2260 SetLastError(0xdeadbeef);
2261 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, NULL, NULL, 0);
2262 error = GetLastError();
2263 ok(error == ERROR_INVALID_PARAMETER ||
2264 broken(error == ERROR_SUCCESS) /* WinXPProSP2 */, "got %u\n", error);
2265 ok(hi == NULL || broken(!!hi) /* WinXPProSP2 */, "open should have failed\n");
2266
2267 sprintf(buffer, "localhost:%d\n", port);
2268 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
2269 ok(hi != NULL, "open failed\n");
2270
2271 /* try connect without authorization */
2272 hc = InternetConnect(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2273 ok(hc != NULL, "connect failed\n");
2274
2275 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
2276 ok(hr != NULL, "HttpOpenRequest failed\n");
2277
2278 sz = 0;
2279 SetLastError(0xdeadbeef);
2280 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_PASSWORD, NULL, &sz);
2281 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2282 ok(!r, "unexpected success\n");
2283 ok(sz == 1, "got %u\n", sz);
2284
2285 sz = 0;
2286 SetLastError(0xdeadbeef);
2287 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, NULL, &sz);
2288 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2289 ok(!r, "unexpected success\n");
2290 ok(sz == 1, "got %u\n", sz);
2291
2292 sz = sizeof(buffer);
2293 SetLastError(0xdeadbeef);
2294 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2295 ok(r, "unexpected failure %u\n", GetLastError());
2296 ok(!sz, "got %u\n", sz);
2297
2298 sz = sizeof(buffer);
2299 SetLastError(0xdeadbeef);
2300 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2301 ok(r, "unexpected failure %u\n", GetLastError());
2302 ok(!sz, "got %u\n", sz);
2303
2304 sz = 0;
2305 SetLastError(0xdeadbeef);
2306 r = InternetQueryOption(hr, INTERNET_OPTION_PASSWORD, NULL, &sz);
2307 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2308 ok(!r, "unexpected success\n");
2309 ok(sz == 1, "got %u\n", sz);
2310
2311 sz = 0;
2312 SetLastError(0xdeadbeef);
2313 r = InternetQueryOption(hr, INTERNET_OPTION_USERNAME, NULL, &sz);
2314 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2315 ok(!r, "unexpected success\n");
2316 ok(sz == 1, "got %u\n", sz);
2317
2318 sz = sizeof(buffer);
2319 SetLastError(0xdeadbeef);
2320 r = InternetQueryOption(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2321 ok(r, "unexpected failure %u\n", GetLastError());
2322 ok(!sz, "got %u\n", sz);
2323
2324 sz = sizeof(buffer);
2325 SetLastError(0xdeadbeef);
2326 r = InternetQueryOption(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2327 ok(r, "unexpected failure %u\n", GetLastError());
2328 ok(!sz, "got %u\n", sz);
2329
2330 sz = 0;
2331 SetLastError(0xdeadbeef);
2332 r = InternetQueryOption(hr, INTERNET_OPTION_URL, NULL, &sz);
2333 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2334 ok(!r, "unexpected success\n");
2335 ok(sz == 34, "got %u\n", sz);
2336
2337 sz = sizeof(buffer);
2338 SetLastError(0xdeadbeef);
2339 r = InternetQueryOption(hr, INTERNET_OPTION_URL, buffer, &sz);
2340 ok(r, "unexpected failure %u\n", GetLastError());
2341 ok(sz == 33, "got %u\n", sz);
2342
2343 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2344 ok(r || broken(!r), "HttpSendRequest failed %u\n", GetLastError());
2345 if (!r)
2346 {
2347 win_skip("skipping proxy tests on broken wininet\n");
2348 goto done;
2349 }
2350
2351 test_status_code(hr, 407);
2352
2353 /* set the user + password then try again */
2354 r = InternetSetOption(hi, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2355 ok(!r, "unexpected success\n");
2356
2357 r = InternetSetOption(hc, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2358 ok(r, "failed to set user\n");
2359
2360 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2361 ok(r, "failed to set user\n");
2362
2363 buffer[0] = 0;
2364 sz = 3;
2365 SetLastError(0xdeadbeef);
2366 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2367 ok(!r, "unexpected failure %u\n", GetLastError());
2368 ok(!buffer[0], "got %s\n", buffer);
2369 ok(sz == strlen(username) + 1, "got %u\n", sz);
2370
2371 buffer[0] = 0;
2372 sz = 0;
2373 SetLastError(0xdeadbeef);
2374 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2375 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2376 ok(!r, "unexpected success\n");
2377 ok(sz == strlen(username) + 1, "got %u\n", sz);
2378
2379 bufferW[0] = 0;
2380 sz = 0;
2381 SetLastError(0xdeadbeef);
2382 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_USERNAME, bufferW, &sz);
2383 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2384 ok(!r, "unexpected success\n");
2385 ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2386
2387 buffer[0] = 0;
2388 sz = sizeof(buffer);
2389 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2390 ok(r, "failed to get username\n");
2391 ok(!strcmp(buffer, username), "got %s\n", buffer);
2392 ok(sz == strlen(username), "got %u\n", sz);
2393
2394 buffer[0] = 0;
2395 sz = sizeof(bufferW);
2396 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_USERNAME, bufferW, &sz);
2397 ok(r, "failed to get username\n");
2398 ok(!lstrcmpW(bufferW, usernameW), "wrong username\n");
2399 ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2400
2401 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 1);
2402 ok(r, "failed to set user\n");
2403
2404 buffer[0] = 0;
2405 sz = sizeof(buffer);
2406 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2407 ok(r, "failed to get username\n");
2408 ok(!strcmp(buffer, username), "got %s\n", buffer);
2409 ok(sz == strlen(username), "got %u\n", sz);
2410
2411 r = InternetSetOption(hi, INTERNET_OPTION_USER_AGENT, useragent, 1);
2412 ok(r, "failed to set useragent\n");
2413
2414 buffer[0] = 0;
2415 sz = 0;
2416 SetLastError(0xdeadbeef);
2417 r = InternetQueryOption(hi, INTERNET_OPTION_USER_AGENT, buffer, &sz);
2418 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2419 ok(!r, "unexpected success\n");
2420 ok(sz == strlen(useragent) + 1, "got %u\n", sz);
2421
2422 buffer[0] = 0;
2423 sz = sizeof(buffer);
2424 r = InternetQueryOption(hi, INTERNET_OPTION_USER_AGENT, buffer, &sz);
2425 ok(r, "failed to get user agent\n");
2426 ok(!strcmp(buffer, useragent), "got %s\n", buffer);
2427 ok(sz == strlen(useragent), "got %u\n", sz);
2428
2429 bufferW[0] = 0;
2430 sz = 0;
2431 SetLastError(0xdeadbeef);
2432 r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2433 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2434 ok(!r, "unexpected success\n");
2435 ok(sz == (lstrlenW(useragentW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2436
2437 bufferW[0] = 0;
2438 sz = sizeof(bufferW);
2439 r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2440 ok(r, "failed to get user agent\n");
2441 ok(!lstrcmpW(bufferW, useragentW), "wrong user agent\n");
2442 ok(sz == lstrlenW(useragentW), "got %u\n", sz);
2443
2444 r = InternetSetOption(hr, INTERNET_OPTION_USERNAME, username, 1);
2445 ok(r, "failed to set user\n");
2446
2447 buffer[0] = 0;
2448 sz = 0;
2449 SetLastError(0xdeadbeef);
2450 r = InternetQueryOption(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2451 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2452 ok(!r, "unexpected success\n");
2453 ok(sz == strlen(username) + 1, "got %u\n", sz);
2454
2455 buffer[0] = 0;
2456 sz = sizeof(buffer);
2457 r = InternetQueryOption(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2458 ok(r, "failed to get user\n");
2459 ok(!strcmp(buffer, username), "got %s\n", buffer);
2460 ok(sz == strlen(username), "got %u\n", sz);
2461
2462 bufferW[0] = 0;
2463 sz = 0;
2464 SetLastError(0xdeadbeef);
2465 r = InternetQueryOptionW(hr, INTERNET_OPTION_USERNAME, bufferW, &sz);
2466 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2467 ok(!r, "unexpected success\n");
2468 ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2469
2470 bufferW[0] = 0;
2471 sz = sizeof(bufferW);
2472 r = InternetQueryOptionW(hr, INTERNET_OPTION_USERNAME, bufferW, &sz);
2473 ok(r, "failed to get user\n");
2474 ok(!lstrcmpW(bufferW, usernameW), "wrong user\n");
2475 ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2476
2477 r = InternetSetOption(hr, INTERNET_OPTION_PASSWORD, password, 1);
2478 ok(r, "failed to set password\n");
2479
2480 buffer[0] = 0;
2481 sz = 0;
2482 SetLastError(0xdeadbeef);
2483 r = InternetQueryOption(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2484 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2485 ok(!r, "unexpected success\n");
2486 ok(sz == strlen(password) + 1, "got %u\n", sz);
2487
2488 buffer[0] = 0;
2489 sz = sizeof(buffer);
2490 r = InternetQueryOption(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2491 ok(r, "failed to get password\n");
2492 ok(!strcmp(buffer, password), "got %s\n", buffer);
2493 ok(sz == strlen(password), "got %u\n", sz);
2494
2495 bufferW[0] = 0;
2496 sz = 0;
2497 SetLastError(0xdeadbeef);
2498 r = InternetQueryOptionW(hr, INTERNET_OPTION_PASSWORD, bufferW, &sz);
2499 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2500 ok(!r, "unexpected success\n");
2501 ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2502
2503 bufferW[0] = 0;
2504 sz = sizeof(bufferW);
2505 r = InternetQueryOptionW(hr, INTERNET_OPTION_PASSWORD, bufferW, &sz);
2506 ok(r, "failed to get password\n");
2507 ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2508 ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2509
2510 url = HeapAlloc(GetProcessHeap(), 0, strlen(url_fmt) + 11);
2511 sprintf(url, url_fmt, port);
2512 buffer[0] = 0;
2513 sz = 0;
2514 SetLastError(0xdeadbeef);
2515 r = InternetQueryOption(hr, INTERNET_OPTION_URL, buffer, &sz);
2516 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2517 ok(!r, "unexpected success\n");
2518 ok(sz == strlen(url) + 1, "got %u\n", sz);
2519
2520 buffer[0] = 0;
2521 sz = sizeof(buffer);
2522 r = InternetQueryOption(hr, INTERNET_OPTION_URL, buffer, &sz);
2523 ok(r, "failed to get url\n");
2524 ok(!strcmp(buffer, url), "got %s\n", buffer);
2525 ok(sz == strlen(url), "got %u\n", sz);
2526
2527 bufferW[0] = 0;
2528 sz = 0;
2529 SetLastError(0xdeadbeef);
2530 r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2531 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2532 ok(!r, "unexpected success\n");
2533 ok(sz == (strlen(url) + 1) * sizeof(WCHAR), "got %u\n", sz);
2534
2535 bufferW[0] = 0;
2536 sz = sizeof(bufferW);
2537 r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2538 ok(r, "failed to get url\n");
2539 ok(!strcmp_wa(bufferW, url), "wrong url\n");
2540 ok(sz == strlen(url), "got %u\n", sz);
2541 HeapFree(GetProcessHeap(), 0, url);
2542
2543 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
2544 ok(r, "failed to set password\n");
2545
2546 buffer[0] = 0;
2547 sz = 0;
2548 SetLastError(0xdeadbeef);
2549 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2550 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2551 ok(!r, "unexpected success\n");
2552 ok(sz == strlen(password) + 1, "got %u\n", sz);
2553
2554 buffer[0] = 0;
2555 sz = sizeof(buffer);
2556 r = InternetQueryOption(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2557 ok(r, "failed to get password\n");
2558 ok(!strcmp(buffer, password), "got %s\n", buffer);
2559 ok(sz == strlen(password), "got %u\n", sz);
2560
2561 bufferW[0] = 0;
2562 sz = 0;
2563 SetLastError(0xdeadbeef);
2564 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_PASSWORD, bufferW, &sz);
2565 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2566 ok(!r, "unexpected success\n");
2567 ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2568
2569 bufferW[0] = 0;
2570 sz = sizeof(bufferW);
2571 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_PASSWORD, bufferW, &sz);
2572 ok(r, "failed to get password\n");
2573 ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2574 ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2575
2576 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2577 if (!r)
2578 {
2579 win_skip("skipping proxy tests on broken wininet\n");
2580 goto done;
2581 }
2582 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2583 sz = sizeof buffer;
2584 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
2585 ok(r, "HttpQueryInfo failed\n");
2586 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
2587
2588 done:
2589 InternetCloseHandle(hr);
2590 InternetCloseHandle(hc);
2591 InternetCloseHandle(hi);
2592 }
2593
2594 static void test_header_handling_order(int port)
2595 {
2596 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
2597 static char connection[] = "Connection: Close";
2598
2599 static const char *types[2] = { "*", NULL };
2600 HINTERNET session, connect, request;
2601 DWORD size, status;
2602 BOOL ret;
2603
2604 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2605 ok(session != NULL, "InternetOpen failed\n");
2606
2607 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2608 ok(connect != NULL, "InternetConnect failed\n");
2609
2610 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2611 ok(request != NULL, "HttpOpenRequest failed\n");
2612
2613 ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
2614 ok(ret, "HttpAddRequestHeaders failed\n");
2615
2616 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2617 ok(ret, "HttpSendRequest failed\n");
2618
2619 test_status_code(request, 200);
2620 test_request_flags(request, 0);
2621
2622 InternetCloseHandle(request);
2623
2624 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2625 ok(request != NULL, "HttpOpenRequest failed\n");
2626
2627 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2628 ok(ret, "HttpSendRequest failed\n");
2629
2630 status = 0;
2631 size = sizeof(status);
2632 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2633 ok(ret, "HttpQueryInfo failed\n");
2634 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2635
2636 InternetCloseHandle(request);
2637
2638 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2639 ok(request != NULL, "HttpOpenRequest failed\n");
2640
2641 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2642 ok(ret, "HttpAddRequestHeaders failed\n");
2643
2644 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2645 ok(ret, "HttpSendRequest failed\n");
2646
2647 status = 0;
2648 size = sizeof(status);
2649 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2650 ok(ret, "HttpQueryInfo failed\n");
2651 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2652
2653 InternetCloseHandle(request);
2654 InternetCloseHandle(connect);
2655 InternetCloseHandle(session);
2656 }
2657
2658 static void test_connection_header(int port)
2659 {
2660 HINTERNET ses, con, req;
2661 BOOL ret;
2662
2663 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2664 ok(ses != NULL, "InternetOpen failed\n");
2665
2666 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2667 ok(con != NULL, "InternetConnect failed\n");
2668
2669 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2670 ok(req != NULL, "HttpOpenRequest failed\n");
2671
2672 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2673 ok(ret, "HttpSendRequest failed\n");
2674
2675 test_status_code(req, 200);
2676
2677 InternetCloseHandle(req);
2678
2679 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
2680 ok(req != NULL, "HttpOpenRequest failed\n");
2681
2682 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2683 ok(ret, "HttpSendRequest failed\n");
2684
2685 test_status_code(req, 200);
2686
2687 InternetCloseHandle(req);
2688
2689 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2690 ok(req != NULL, "HttpOpenRequest failed\n");
2691
2692 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2693 ok(ret, "HttpSendRequest failed\n");
2694
2695 test_status_code(req, 200);
2696
2697 InternetCloseHandle(req);
2698
2699 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2700 ok(req != NULL, "HttpOpenRequest failed\n");
2701
2702 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2703 ok(ret, "HttpSendRequest failed\n");
2704
2705 test_status_code(req, 200);
2706
2707 InternetCloseHandle(req);
2708 InternetCloseHandle(con);
2709 InternetCloseHandle(ses);
2710 }
2711
2712 static void test_http1_1(int port)
2713 {
2714 HINTERNET ses, con, req;
2715 BOOL ret;
2716
2717 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2718 ok(ses != NULL, "InternetOpen failed\n");
2719
2720 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2721 ok(con != NULL, "InternetConnect failed\n");
2722
2723 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2724 ok(req != NULL, "HttpOpenRequest failed\n");
2725
2726 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2727 if (ret)
2728 {
2729 InternetCloseHandle(req);
2730
2731 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2732 ok(req != NULL, "HttpOpenRequest failed\n");
2733
2734 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2735 ok(ret, "HttpSendRequest failed\n");
2736 }
2737
2738 InternetCloseHandle(req);
2739 InternetCloseHandle(con);
2740 InternetCloseHandle(ses);
2741 }
2742
2743 static void test_connection_closing(int port)
2744 {
2745 HINTERNET session, connection, req;
2746 DWORD res;
2747
2748 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2749
2750 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
2751 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
2752
2753 pInternetSetStatusCallbackA(session, callback);
2754
2755 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2756 connection = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
2757 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
2758 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2759
2760 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2761 req = HttpOpenRequestA(connection, "GET", "/testJ", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0xdeadbeaf);
2762 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2763 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2764
2765 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2766 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
2767 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2768 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2769 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2770 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2771 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2772 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2773 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
2774 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
2775 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2776
2777 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
2778 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2779 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2780 WaitForSingleObject(hCompleteEvent, INFINITE);
2781 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2782
2783 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2784 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
2785 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2786 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2787 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2788 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2789 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2790 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2791 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
2792 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
2793 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2794
2795 test_status_code(req, 200);
2796
2797 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2798 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
2799 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2800 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2801 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2802 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2803 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2804 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2805 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2806
2807 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
2808 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2809 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2810 WaitForSingleObject(hCompleteEvent, INFINITE);
2811 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2812
2813 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2814 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
2815 todo_wine CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2816 todo_wine CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2817 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2818 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2819 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2820 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2821 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2822
2823 test_status_code_todo(req, 210);
2824
2825 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2826 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
2827 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2828 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2829 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2830 SET_WINE_ALLOW(INTERNET_STATUS_SENDING_REQUEST);
2831 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2832 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2833 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2834 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
2835 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
2836 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2837
2838 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
2839 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2840 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2841 WaitForSingleObject(hCompleteEvent, INFINITE);
2842 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2843
2844 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2845 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
2846 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2847 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2848 todo_wine CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2849 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2850 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2851 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2852 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
2853 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
2854 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2855
2856 test_status_code_todo(req, 200);
2857
2858 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
2859 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
2860
2861 close_async_handle(session, hCompleteEvent, 2);
2862 CloseHandle(hCompleteEvent);
2863 }
2864
2865
2866 static void test_no_content(int port)
2867 {
2868 HINTERNET session, connection, req;
2869 DWORD res;
2870
2871 trace("Testing 204 no content response...\n");
2872
2873 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2874
2875 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
2876 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
2877
2878 pInternetSetStatusCallbackA(session, callback);
2879
2880 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2881 connection = InternetConnectA(session, "localhost", port,
2882 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
2883 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
2884 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2885
2886 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2887 req = HttpOpenRequestA(connection, "GET", "/test_no_content", NULL, NULL, NULL,
2888 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
2889 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2890 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2891
2892 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2893 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2894 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2895 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2896 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2897 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2898 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2899 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
2900 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
2901 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2902
2903 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
2904 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2905 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2906 WaitForSingleObject(hCompleteEvent, INFINITE);
2907 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2908
2909 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2910 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2911 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2912 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2913 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2914 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2915 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2916 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
2917 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
2918 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2919
2920 close_async_handle(session, hCompleteEvent, 2);
2921 CloseHandle(hCompleteEvent);
2922 }
2923
2924 static void test_conn_close(int port)
2925 {
2926 HINTERNET session, connection, req;
2927 DWORD res, avail, size;
2928 BYTE buf[1024];
2929
2930 trace("Testing connection close connection...\n");
2931
2932 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2933 conn_close_event = CreateEvent(NULL, FALSE, FALSE, NULL);
2934
2935 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
2936 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
2937
2938 pInternetSetStatusCallbackA(session, callback);
2939
2940 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2941 connection = InternetConnectA(session, "localhost", port,
2942 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
2943 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
2944 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2945
2946 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2947 req = HttpOpenRequestA(connection, "GET", "/test_conn_close", NULL, NULL, NULL,
2948 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
2949 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2950 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2951
2952 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2953 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2954 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2955 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2956 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2957 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2958 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2959 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2960
2961 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
2962 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2963 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2964 WaitForSingleObject(hCompleteEvent, INFINITE);
2965 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2966
2967 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2968 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2969 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2970 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2971 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2972 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2973 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2974 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2975
2976 avail = 0;
2977 res = InternetQueryDataAvailable(req, &avail, 0, 0);
2978 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
2979 ok(avail != 0, "avail = 0\n");
2980
2981 size = 0;
2982 res = InternetReadFile(req, buf, avail, &size);
2983 ok(res, "InternetReadFile failed: %u\n", GetLastError());
2984
2985 res = InternetQueryDataAvailable(req, &avail, 0, 0);
2986 ok(!res && (GetLastError() == ERROR_IO_PENDING),
2987 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2988 ok(!avail, "avail = %u, expected 0\n", avail);
2989
2990 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
2991 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
2992 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2993 SetEvent(conn_close_event);
2994 #ifdef ROSTESTS_73_FIXED
2995 WaitForSingleObject(hCompleteEvent, INFINITE);
2996 #else /* ROSTESTS_73_FIXED */
2997 ok(WaitForSingleObject(hCompleteEvent, 5000) == WAIT_OBJECT_0, "Wait timed out\n");
2998 #endif /* ROSTESTS_73_FIXED */
2999 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3000 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3001 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3002 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3003
3004 close_async_handle(session, hCompleteEvent, 2);
3005 CloseHandle(hCompleteEvent);
3006 }
3007
3008 static void test_no_cache(int port)
3009 {
3010 static const char cache_control_no_cache[] = "/test_cache_control_no_cache";
3011 static const char cache_control_no_store[] = "/test_cache_control_no_store";
3012 static const char cache_url_fmt[] = "http://localhost:%d%s";
3013
3014 char cache_url[256], buf[256];
3015 HINTERNET ses, con, req;
3016 DWORD read, size;
3017 BOOL ret;
3018
3019 trace("Testing no-cache header\n");
3020
3021 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3022 ok(ses != NULL,"InternetOpen failed with error %u\n", GetLastError());
3023
3024 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3025 ok(con != NULL, "InternetConnect failed with error %u\n", GetLastError());
3026
3027 req = HttpOpenRequest(con, NULL, cache_control_no_cache, NULL, NULL, NULL, 0, 0);
3028 ok(req != NULL, "HttpOpenRequest failed\n");
3029
3030 sprintf(cache_url, cache_url_fmt, port, cache_control_no_cache);
3031 DeleteUrlCacheEntry(cache_url);
3032
3033 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
3034 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3035 size = 0;
3036 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3037 size += read;
3038 ok(size == 12, "read %d bytes of data\n", size);
3039 InternetCloseHandle(req);
3040
3041 req = HttpOpenRequest(con, NULL, cache_control_no_cache, NULL, NULL, NULL, 0, 0);
3042 ok(req != NULL, "HttpOpenRequest failed\n");
3043
3044 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
3045 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3046 size = 0;
3047 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3048 size += read;
3049 ok(size == 0, "read %d bytes of data\n", size);
3050 InternetCloseHandle(req);
3051 DeleteUrlCacheEntry(cache_url);
3052
3053 req = HttpOpenRequest(con, NULL, cache_control_no_store, NULL, NULL, NULL, 0, 0);
3054 ok(req != NULL, "HttpOpenRequest failed\n");
3055
3056 sprintf(cache_url, cache_url_fmt, port, cache_control_no_store);
3057 DeleteUrlCacheEntry(cache_url);
3058
3059 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
3060 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3061 size = 0;
3062 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3063 size += read;
3064 ok(size == 12, "read %d bytes of data\n", size);
3065 InternetCloseHandle(req);
3066
3067 ret = DeleteUrlCacheEntry(cache_url);
3068 ok(!ret && GetLastError()==ERROR_FILE_NOT_FOUND, "cache entry should not exist\n");
3069
3070 InternetCloseHandle(con);
3071 InternetCloseHandle(ses);
3072 }
3073
3074 static void test_cache_read_gzipped(int port)
3075 {
3076 static const char cache_url_fmt[] = "http://localhost:%d%s";
3077 static const char get_gzip[] = "/test_cache_gzip";
3078 static const char content[] = "gzip test\n";
3079 static const char text_html[] = "text/html";
3080 static const char raw_header[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
3081
3082 HINTERNET ses, con, req;
3083 DWORD read, size;
3084 char cache_url[256], buf[256];
3085 BOOL ret;
3086
3087 trace("Testing reading compressed content from cache\n");
3088
3089 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3090 ok(ses != NULL,"InternetOpen failed with error %u\n", GetLastError());
3091
3092 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3093 ok(con != NULL, "InternetConnect failed with error %u\n", GetLastError());
3094
3095 req = HttpOpenRequest(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3096 ok(req != NULL, "HttpOpenRequest failed\n");
3097
3098 ret = TRUE;
3099 ret = InternetSetOption(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3100 if(!ret && GetLastError()==ERROR_INTERNET_INVALID_OPTION) {
3101 win_skip("INTERNET_OPTION_HTTP_DECODING not supported\n");
3102 InternetCloseHandle(req);
3103 InternetCloseHandle(con);
3104 InternetCloseHandle(ses);
3105 return;
3106 }
3107 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3108
3109 ret = HttpSendRequest(req, "Accept-Encoding: gzip", -1, NULL, 0);
3110 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3111 size = 0;
3112 while(InternetReadFile(req, buf+size, sizeof(buf)-size, &read) && read)
3113 size += read;
3114 ok(size == 10, "read %d bytes of data\n", size);
3115 buf[size] = 0;
3116 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3117
3118 size = sizeof(buf)-1;
3119 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_TYPE, buf, &size, 0);
3120 ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3121 buf[size] = 0;
3122 ok(!strncmp(text_html, buf, size), "buf = %s\n", buf);
3123
3124 size = sizeof(buf)-1;
3125 ret = HttpQueryInfoA(req, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &size, 0);
3126 ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3127 buf[size] = 0;
3128 ok(!strncmp(raw_header, buf, size), "buf = %s\n", buf);
3129 InternetCloseHandle(req);
3130
3131 req = HttpOpenRequest(con, NULL, get_gzip, NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
3132 ok(req != NULL, "HttpOpenRequest failed\n");
3133
3134 ret = TRUE;
3135 ret = InternetSetOption(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3136 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3137
3138 ret = HttpSendRequest(req, "Accept-Encoding: gzip", -1, NULL, 0);
3139 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3140 size = 0;
3141 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3142 size += read;
3143 todo_wine ok(size == 10, "read %d bytes of data\n", size);
3144 buf[size] = 0;
3145 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3146
3147 size = sizeof(buf);
3148 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
3149 ok(!ret && GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND,
3150 "HttpQueryInfo(HTTP_QUERY_CONTENT_ENCODING) returned %d, %d\n",
3151 ret, GetLastError());
3152
3153 size = sizeof(buf)-1;
3154 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_TYPE, buf, &size, 0);
3155 todo_wine ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3156 buf[size] = 0;
3157 todo_wine ok(!strncmp(text_html, buf, size), "buf = %s\n", buf);
3158 InternetCloseHandle(req);
3159
3160 /* Decompression doesn't work while reading from cache */
3161 test_cache_gzip = 0;
3162 sprintf(cache_url, cache_url_fmt, port, get_gzip);
3163 DeleteUrlCacheEntry(cache_url);
3164
3165 req = HttpOpenRequest(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3166 ok(req != NULL, "HttpOpenRequest failed\n");
3167
3168 ret = HttpSendRequest(req, "Accept-Encoding: gzip", -1, NULL, 0);
3169 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3170 size = 0;
3171 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3172 size += read;
3173 ok(size == 31, "read %d bytes of data\n", size);
3174 InternetCloseHandle(req);
3175
3176 req = HttpOpenRequest(con, NULL, get_gzip, NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
3177 ok(req != NULL, "HttpOpenRequest failed\n");
3178
3179 ret = TRUE;
3180 ret = InternetSetOption(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3181 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3182
3183 ret = HttpSendRequest(req, "Accept-Encoding: gzip", -1, NULL, 0);
3184 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3185 size = 0;
3186 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3187 size += read;
3188 todo_wine ok(size == 31, "read %d bytes of data\n", size);
3189
3190 size = sizeof(buf);
3191 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
3192 todo_wine ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_ENCODING) failed: %d\n", GetLastError());
3193 InternetCloseHandle(req);
3194
3195 InternetCloseHandle(con);
3196 InternetCloseHandle(ses);
3197
3198 DeleteUrlCacheEntry(cache_url);
3199 }
3200
3201 static void test_HttpSendRequestW(int port)
3202 {
3203 static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
3204 HINTERNET ses, con, req;
3205 DWORD error;
3206 BOOL ret;
3207
3208 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3209 ok(ses != NULL, "InternetOpen failed\n");
3210
3211 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3212 ok(con != NULL, "InternetConnect failed\n");
3213
3214 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
3215 ok(req != NULL, "HttpOpenRequest failed\n");
3216
3217 SetLastError(0xdeadbeef);
3218 ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
3219 error = GetLastError();
3220 ok(!ret, "HttpSendRequestW succeeded\n");
3221 ok(error == ERROR_IO_PENDING ||
3222 broken(error == ERROR_HTTP_HEADER_NOT_FOUND) || /* IE6 */
3223 broken(error == ERROR_INVALID_PARAMETER), /* IE5 */
3224 "got %u expected ERROR_IO_PENDING\n", error);
3225
3226 InternetCloseHandle(req);
3227 InternetCloseHandle(con);
3228 InternetCloseHandle(ses);
3229 }
3230
3231 static void test_cookie_header(int port)
3232 {
3233 HINTERNET ses, con, req;
3234 DWORD size, error;
3235 BOOL ret;
3236 char buffer[64];
3237
3238 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3239 ok(ses != NULL, "InternetOpen failed\n");
3240
3241 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3242 ok(con != NULL, "InternetConnect failed\n");
3243
3244 InternetSetCookie("http://localhost", "cookie", "biscuit");
3245
3246 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3247 ok(req != NULL, "HttpOpenRequest failed\n");
3248
3249 buffer[0] = 0;
3250 size = sizeof(buffer);
3251 SetLastError(0xdeadbeef);
3252 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3253 error = GetLastError();
3254 ok(!ret, "HttpQueryInfo succeeded\n");
3255 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
3256
3257 ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
3258 ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
3259
3260 buffer[0] = 0;
3261 size = sizeof(buffer);
3262 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3263 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
3264 ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
3265
3266 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
3267 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
3268
3269 test_status_code(req, 200);
3270
3271 buffer[0] = 0;
3272 size = sizeof(buffer);
3273 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3274 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
3275 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
3276
3277 InternetCloseHandle(req);
3278 InternetCloseHandle(con);
3279 InternetCloseHandle(ses);
3280 }
3281
3282 static void test_basic_authentication(int port)
3283 {
3284 HINTERNET session, connect, request;
3285 BOOL ret;
3286
3287 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3288 ok(session != NULL, "InternetOpen failed\n");
3289
3290 connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
3291 ok(connect != NULL, "InternetConnect failed\n");
3292
3293 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
3294 ok(request != NULL, "HttpOpenRequest failed\n");
3295
3296 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3297 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3298
3299 test_status_code(request, 200);
3300 test_request_flags(request, 0);
3301
3302 InternetCloseHandle(request);
3303 InternetCloseHandle(connect);
3304 InternetCloseHandle(session);
3305 }
3306
3307 static void test_premature_disconnect(int port)
3308 {
3309 HINTERNET session, connect, request;
3310 DWORD err;
3311 BOOL ret;
3312
3313 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3314 ok(session != NULL, "InternetOpen failed\n");
3315
3316 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3317 ok(connect != NULL, "InternetConnect failed\n");
3318
3319 request = HttpOpenRequest(connect, NULL, "/premature_disconnect", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3320 ok(request != NULL, "HttpOpenRequest failed\n");
3321
3322 SetLastError(0xdeadbeef);
3323 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3324 err = GetLastError();
3325 todo_wine ok(!ret, "HttpSendRequest succeeded\n");
3326 todo_wine ok(err == ERROR_HTTP_INVALID_SERVER_RESPONSE, "got %u\n", err);
3327
3328 InternetCloseHandle(request);
3329 InternetCloseHandle(connect);
3330 InternetCloseHandle(session);
3331 }
3332
3333 static void test_invalid_response_headers(int port)
3334 {
3335 HINTERNET session, connect, request;
3336 DWORD size;
3337 BOOL ret;
3338 char buffer[256];
3339
3340 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3341 ok(session != NULL, "InternetOpen failed\n");
3342
3343 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3344 ok(connect != NULL, "InternetConnect failed\n");
3345
3346 request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
3347 ok(request != NULL, "HttpOpenRequest failed\n");
3348
3349 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3350 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3351
3352 test_status_code(request, 401);
3353 test_request_flags(request, 0);
3354
3355 buffer[0] = 0;
3356 size = sizeof(buffer);
3357 ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
3358 ok(ret, "HttpQueryInfo failed\n");
3359 ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
3360 "headers wrong \"%s\"\n", buffer);
3361
3362 buffer[0] = 0;
3363 size = sizeof(buffer);
3364 ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
3365 ok(ret, "HttpQueryInfo failed\n");
3366 ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
3367
3368 InternetCloseHandle(request);
3369 InternetCloseHandle(connect);
3370 InternetCloseHandle(session);
3371 }
3372
3373 static void test_response_without_headers(int port)
3374 {
3375 HINTERNET hi, hc, hr;
3376 DWORD r, count, size;
3377 char buffer[1024];
3378
3379 SetLastError(0xdeadbeef);
3380 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3381 ok(hi != NULL, "open failed %u\n", GetLastError());
3382
3383 SetLastError(0xdeadbeef);
3384 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3385 ok(hc != NULL, "connect failed %u\n", GetLastError());
3386
3387 SetLastError(0xdeadbeef);
3388 hr = HttpOpenRequest(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
3389 ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
3390
3391 test_request_flags(hr, INTERNET_REQFLAG_NO_HEADERS);
3392
3393 SetLastError(0xdeadbeef);
3394 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
3395 ok(r, "HttpSendRequest failed %u\n", GetLastError());
3396
3397 test_request_flags_todo(hr, INTERNET_REQFLAG_NO_HEADERS);
3398
3399 count = 0;
3400 memset(buffer, 0, sizeof buffer);
3401 SetLastError(0xdeadbeef);
3402 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
3403 ok(r, "InternetReadFile failed %u\n", GetLastError());
3404 todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
3405 todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
3406
3407 test_status_code(hr, 200);
3408 test_request_flags_todo(hr, INTERNET_REQFLAG_NO_HEADERS);
3409
3410 buffer[0] = 0;
3411 size = sizeof(buffer);
3412 SetLastError(0xdeadbeef);
3413 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
3414 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
3415 ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
3416
3417 buffer[0] = 0;
3418 size = sizeof(buffer);
3419 SetLastError(0xdeadbeef);
3420 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
3421 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
3422 ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
3423
3424 buffer[0] = 0;
3425 size = sizeof(buffer);
3426 SetLastError(0xdeadbeef);
3427 r = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
3428 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
3429 ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
3430
3431 InternetCloseHandle(hr);
3432 InternetCloseHandle(hc);
3433 InternetCloseHandle(hi);
3434 }
3435
3436 static void test_HttpQueryInfo(int port)
3437 {
3438 HINTERNET hi, hc, hr;
3439 DWORD size, index;
3440 char buffer[1024];
3441 BOOL ret;
3442
3443 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3444 ok(hi != NULL, "InternetOpen failed\n");
3445
3446 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3447 ok(hc != NULL, "InternetConnect failed\n");
3448
3449 hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
3450 ok(hr != NULL, "HttpOpenRequest failed\n");
3451
3452 size = sizeof(buffer);
3453 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
3454 ok(!ret && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed %u\n", GetLastError());
3455
3456 ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
3457 ok(ret, "HttpSendRequest failed\n");
3458
3459 index = 0;
3460 size = sizeof(buffer);
3461 ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
3462 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3463 ok(index == 1, "expected 1 got %u\n", index);
3464
3465 index = 0;
3466 size = sizeof(buffer);
3467 ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
3468 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3469 ok(index == 1, "expected 1 got %u\n", index);
3470
3471 index = 0;
3472 size = sizeof(buffer);
3473 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
3474 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3475 ok(index == 0, "expected 0 got %u\n", index);
3476
3477 size = sizeof(buffer);
3478 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
3479 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3480 ok(index == 0, "expected 0 got %u\n", index);
3481
3482 index = 0xdeadbeef; /* invalid start index */
3483 size = sizeof(buffer);
3484 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
3485 todo_wine ok(!ret, "HttpQueryInfo should have failed\n");
3486 todo_wine ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
3487 "Expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
3488
3489 index = 0;
3490 size = sizeof(buffer);
3491 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
3492 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3493 ok(index == 0, "expected 0 got %u\n", index);
3494
3495 size = sizeof(buffer);
3496 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
3497 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3498 ok(index == 0, "expected 0 got %u\n", index);
3499
3500 size = sizeof(buffer);
3501 ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
3502 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3503 ok(index == 0, "expected 0 got %u\n", index);
3504
3505 test_status_code(hr, 200);
3506
3507 index = 0xdeadbeef;
3508 size = sizeof(buffer);
3509 ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
3510 ok(!ret, "HttpQueryInfo succeeded\n");
3511 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
3512
3513 index = 0;
3514 size = sizeof(buffer);
3515 ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
3516 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3517 ok(index == 1, "expected 1 got %u\n", index);
3518
3519 index = 0;
3520 size = sizeof(buffer);
3521 strcpy(buffer, "Server");
3522 ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
3523 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3524 ok(index == 1, "expected 1 got %u\n", index);
3525
3526 index = 0;
3527 size = sizeof(buffer);
3528 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
3529 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3530 ok(index == 1, "expected 1 got %u\n", index);
3531
3532 size = sizeof(buffer);
3533 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
3534 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
3535 ok(index == 2, "expected 2 got %u\n", index);
3536
3537 InternetCloseHandle(hr);
3538 InternetCloseHandle(hc);
3539 InternetCloseHandle(hi);
3540 }
3541
3542 static void test_options(int port)
3543 {
3544 INTERNET_DIAGNOSTIC_SOCKET_INFO idsi;
3545 HINTERNET ses, con, req;
3546 DWORD size, error;
3547 DWORD_PTR ctx;
3548 BOOL ret;
3549
3550 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3551 ok(ses != NULL, "InternetOpen failed\n");
3552
3553 SetLastError(0xdeadbeef);
3554 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, 0);
3555 error = GetLastError();
3556 ok(!ret, "InternetSetOption succeeded\n");
3557 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3558
3559 SetLastError(0xdeadbeef);
3560 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, sizeof(ctx));
3561 ok(!ret, "InternetSetOption succeeded\n");
3562 error = GetLastError();
3563 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3564
3565 SetLastError(0xdeadbeef);
3566 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, 0);
3567 ok(!ret, "InternetSetOption succeeded\n");
3568 error = GetLastError();
3569 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3570
3571 ctx = 1;
3572 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
3573 ok(ret, "InternetSetOption failed %u\n", GetLastError());
3574
3575 SetLastError(0xdeadbeef);
3576 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, NULL);
3577 error = GetLastError();
3578 ok(!ret, "InternetQueryOption succeeded\n");
3579 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3580
3581 SetLastError(0xdeadbeef);
3582 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, NULL);
3583 error = GetLastError();
3584 ok(!ret, "InternetQueryOption succeeded\n");
3585 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3586
3587 size = 0;
3588 SetLastError(0xdeadbeef);
3589 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, &size);
3590 error = GetLastError();
3591 ok(!ret, "InternetQueryOption succeeded\n");
3592 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
3593
3594 size = sizeof(ctx);
3595 SetLastError(0xdeadbeef);
3596 ret = InternetQueryOption(NULL, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3597 error = GetLastError();
3598 ok(!ret, "InternetQueryOption succeeded\n");
3599 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %u\n", error);
3600
3601 ctx = 0xdeadbeef;
3602 size = sizeof(ctx);
3603 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3604 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3605 ok(ctx == 1, "expected 1 got %lu\n", ctx);
3606
3607 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3608 ok(con != NULL, "InternetConnect failed\n");
3609
3610 ctx = 0xdeadbeef;
3611 size = sizeof(ctx);
3612 ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3613 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3614 ok(ctx == 0, "expected 0 got %lu\n", ctx);
3615
3616 ctx = 2;
3617 ret = InternetSetOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
3618 ok(ret, "InternetSetOption failed %u\n", GetLastError());
3619
3620 ctx = 0xdeadbeef;
3621 size = sizeof(ctx);
3622 ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3623 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3624 ok(ctx == 2, "expected 2 got %lu\n", ctx);
3625
3626 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
3627 ok(req != NULL, "HttpOpenRequest failed\n");
3628
3629 ctx = 0xdeadbeef;
3630 size = sizeof(ctx);
3631 ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3632 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3633 ok(ctx == 0, "expected 0 got %lu\n", ctx);
3634
3635 ctx = 3;
3636 ret = InternetSetOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
3637 ok(ret, "InternetSetOption failed %u\n", GetLastError());
3638
3639 ctx = 0xdeadbeef;
3640 size = sizeof(ctx);
3641 ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
3642 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3643 ok(ctx == 3, "expected 3 got %lu\n", ctx);
3644
3645 size = sizeof(idsi);
3646 ret = InternetQueryOption(req, INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, &idsi, &size);
3647 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
3648
3649 size = 0;
3650 SetLastError(0xdeadbeef);
3651 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
3652 error = GetLastError();
3653 ok(!ret, "InternetQueryOption succeeded\n");
3654 ok(error == ERROR_INTERNET_INVALID_OPERATION, "expected ERROR_INTERNET_INVALID_OPERATION, got %u\n", error);
3655
3656 /* INTERNET_OPTION_PROXY */
3657 SetLastError(0xdeadbeef);
3658 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, NULL);
3659 error = GetLastError();
3660 ok(!ret, "InternetQueryOption succeeded\n");
3661 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3662
3663 SetLastError(0xdeadbeef);
3664 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, &ctx, NULL);
3665 error = GetLastError();
3666 ok(!ret, "InternetQueryOption succeeded\n");
3667 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
3668
3669 size = 0;
3670 SetLastError(0xdeadbeef);
3671 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, &size);
3672 error = GetLastError();
3673 ok(!ret, "InternetQueryOption succeeded\n");
3674 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
3675 ok(size >= sizeof(INTERNET_PROXY_INFOA), "expected size to be greater or equal to the struct size\n");
3676
3677 InternetCloseHandle(req);
3678 InternetCloseHandle(con);
3679 InternetCloseHandle(ses);
3680 }
3681
3682 typedef struct {
3683 const char *response_text;
3684 int status_code;
3685 const char *status_text;
3686 const char *raw_headers;
3687 } http_status_test_t;
3688
3689 static const http_status_test_t http_status_tests[] = {
3690 {
3691 "HTTP/1.1 200 OK\r\n"
3692 "Content-Length: 1\r\n"
3693 "\r\nx",
3694 200,
3695 "OK"
3696 },
3697 {
3698 "HTTP/1.1 404 Fail\r\n"
3699 "Content-Length: 1\r\n"
3700 "\r\nx",
3701 404,
3702 "Fail"
3703 },
3704 {
3705 "HTTP/1.1 200\r\n"
3706 "Content-Length: 1\r\n"
3707 "\r\nx",
3708 200,
3709 ""
3710 },
3711 {
3712 "HTTP/1.1 410 \r\n"
3713 "Content-Length: 1\r\n"
3714 "\r\nx",
3715 410,
3716 ""
3717 }
3718 };
3719
3720 static void test_http_status(int port)
3721 {
3722 HINTERNET ses, con, req;
3723 char buf[1000];
3724 DWORD i, size;
3725 BOOL res;
3726
3727 for(i=0; i < sizeof(http_status_tests)/sizeof(*http_status_tests); i++) {
3728 send_buffer = http_status_tests[i].response_text;
3729
3730 ses = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3731 ok(ses != NULL, "InternetOpen failed\n");
3732
3733 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3734 ok(con != NULL, "InternetConnect failed\n");
3735
3736 req = HttpOpenRequest(con, NULL, "/send_from_buffer", NULL, NULL, NULL, 0, 0);
3737 ok(req != NULL, "HttpOpenRequest failed\n");
3738
3739 res = HttpSendRequest(req, NULL, 0, NULL, 0);
3740 ok(res, "HttpSendRequest failed\n");
3741
3742 test_status_code(req, http_status_tests[i].status_code);
3743
3744 size = sizeof(buf);
3745 res = HttpQueryInfo(req, HTTP_QUERY_STATUS_TEXT, buf, &size, NULL);
3746 ok(res, "HttpQueryInfo failed: %u\n", GetLastError());
3747 ok(!strcmp(buf, http_status_tests[i].status_text), "[%u] Unexpected status text \"%s\", expected \"%s\"\n",
3748 i, buf, http_status_tests[i].status_text);
3749
3750 InternetCloseHandle(req);
3751 InternetCloseHandle(con);
3752 InternetCloseHandle(ses);
3753 }
3754 }
3755
3756 static void test_cache_control_verb(int port)
3757 {
3758 HINTERNET session, connect, request;
3759 BOOL ret;
3760
3761 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3762 ok(session != NULL, "InternetOpen failed\n");
3763
3764 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3765 ok(connect != NULL, "InternetConnect failed\n");
3766
3767 request = HttpOpenRequest(connect, "RPC_OUT_DATA", "/test_cache_control_verb", NULL, NULL, NULL,
3768 INTERNET_FLAG_NO_CACHE_WRITE, 0);
3769 ok(request != NULL, "HttpOpenRequest failed\n");
3770 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3771 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3772 test_status_code(request, 200);
3773
3774 request = HttpOpenRequest(connect, "POST", "/test_cache_control_verb", NULL, NULL, NULL,
3775 INTERNET_FLAG_NO_CACHE_WRITE, 0);
3776 ok(request != NULL, "HttpOpenRequest failed\n");
3777 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3778 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3779 test_status_code(request, 200);
3780
3781 request = HttpOpenRequest(connect, "HEAD", "/test_cache_control_verb", NULL, NULL, NULL,
3782 INTERNET_FLAG_NO_CACHE_WRITE, 0);
3783 ok(request != NULL, "HttpOpenRequest failed\n");
3784 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3785 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3786 test_status_code(request, 200);
3787
3788 request = HttpOpenRequest(connect, "GET", "/test_cache_control_verb", NULL, NULL, NULL,
3789 INTERNET_FLAG_NO_CACHE_WRITE, 0);
3790 ok(request != NULL, "HttpOpenRequest failed\n");
3791 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
3792 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
3793 test_status_code(request, 200);
3794
3795 InternetCloseHandle(request);
3796 InternetCloseHandle(connect);
3797 InternetCloseHandle(session);
3798 }
3799
3800 static void test_http_connection(void)
3801 {
3802 struct server_info si;
3803 HANDLE hThread;
3804 DWORD id = 0, r;
3805
3806 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
3807 si.port = 7531;
3808
3809 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
3810 ok( hThread != NULL, "create thread failed\n");
3811
3812 r = WaitForSingleObject(si.hEvent, 10000);
3813 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
3814 if (r != WAIT_OBJECT_0)
3815 return;
3816
3817 test_basic_request(si.port, "GET", "/test1");
3818 test_proxy_indirect(si.port);
3819 test_proxy_direct(si.port);
3820 test_header_handling_order(si.port);
3821 test_basic_request(si.port, "POST", "/test5");
3822 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
3823 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
3824 test_basic_request(si.port, "GET", "/test6");
3825 test_basic_request(si.port, "GET", "/testF");
3826 test_connection_header(si.port);
3827 test_http1_1(si.port);
3828 test_cookie_header(si.port);
3829 test_basic_authentication(si.port);
3830 test_invalid_response_headers(si.port);
3831 test_response_without_headers(si.port);
3832 test_HttpQueryInfo(si.port);
3833 test_HttpSendRequestW(si.port);
3834 test_last_error(si.port);
3835 test_options(si.port);
3836 test_no_content(si.port);
3837 test_conn_close(si.port);
3838 test_no_cache(si.port);
3839 test_cache_read_gzipped(si.port);
3840 test_http_status(si.port);
3841 test_premature_disconnect(si.port);
3842 test_connection_closing(si.port);
3843 test_cache_control_verb(si.port);
3844
3845 /* send the basic request again to shutdown the server thread */
3846 test_basic_request(si.port, "GET", "/quit");
3847
3848 r = WaitForSingleObject(hThread, 3000);
3849 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
3850 CloseHandle(hThread);
3851 }
3852
3853 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
3854 {
3855 LocalFree(info->lpszSubjectInfo);
3856 LocalFree(info->lpszIssuerInfo);
3857 LocalFree(info->lpszProtocolName);
3858 LocalFree(info->lpszSignatureAlgName);
3859 LocalFree(info->lpszEncryptionAlgName);
3860 }
3861
3862 typedef struct {
3863 const char *ex_subject;
3864 const char *ex_issuer;
3865 } cert_struct_test_t;
3866
3867 static const cert_struct_test_t test_winehq_org_cert = {
3868 "0mJuv1t-1CFypQkyTZwfvjHHBAbnUndG\r\n"
3869 "GT98380011\r\n"
3870 "See www.rapidssl.com/resources/cps (c)13\r\n"
3871 "Domain Control Validated - RapidSSL(R)\r\n"
3872 "*.winehq.org",
3873
3874 "US\r\n"
3875 "\"GeoTrust, Inc.\"\r\n"
3876 "RapidSSL CA"
3877 };
3878
3879 static const cert_struct_test_t test_winehq_com_cert = {
3880 "US\r\n"
3881 "Minnesota\r\n"
3882 "Saint Paul\r\n"
3883 "WineHQ\r\n"
3884 "test.winehq.com\r\n"
3885 "webmaster@winehq.org",
3886
3887 "US\r\n"
3888 "Minnesota\r\n"
3889 "WineHQ\r\n"
3890 "test.winehq.com\r\n"
3891 "webmaster@winehq.org"
3892 };
3893
3894 static void test_cert_struct(HINTERNET req, const cert_struct_test_t *test)
3895 {
3896 INTERNET_CERTIFICATE_INFOA info;
3897 DWORD size;
3898 BOOL res;
3899
3900 memset(&info, 0x5, sizeof(info));
3901
3902 size = sizeof(info);
3903 res = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, &info, &size);
3904 ok(res, "InternetQueryOption failed: %u\n", GetLastError());
3905 ok(size == sizeof(info), "size = %u\n", size);
3906
3907 ok(!strcmp(info.lpszSubjectInfo, test->ex_subject), "lpszSubjectInfo = %s\n", info.lpszSubjectInfo);
3908 ok(!strcmp(info.lpszIssuerInfo, test->ex_issuer), "lpszIssuerInfo = %s\n", info.lpszIssuerInfo);
3909 ok(!info.lpszSignatureAlgName, "lpszSignatureAlgName = %s\n", info.lpszSignatureAlgName);
3910 ok(!info.lpszEncryptionAlgName, "lpszEncryptionAlgName = %s\n", info.lpszEncryptionAlgName);
3911 ok(!info.lpszProtocolName, "lpszProtocolName = %s\n", info.lpszProtocolName);
3912 ok(info.dwKeySize == 128, "dwKeySize = %u\n", info.dwKeySize);
3913
3914 release_cert_info(&info);
3915 }
3916
3917 #define test_security_info(a,b,c) _test_security_info(__LINE__,a,b,c)
3918 static void _test_security_info(unsigned line, const char *urlc, DWORD error, DWORD ex_flags)
3919 {
3920 char url[INTERNET_MAX_URL_LENGTH];
3921 const CERT_CHAIN_CONTEXT *chain;
3922 DWORD flags;
3923 BOOL res;
3924
3925 if(!pInternetGetSecurityInfoByURLA) {
3926 win_skip("pInternetGetSecurityInfoByURLA not available\n");
3927 return;
3928 }
3929
3930 strcpy(url, urlc);
3931 chain = (void*)0xdeadbeef;
3932 flags = 0xdeadbeef;
3933 res = pInternetGetSecurityInfoByURLA(url, &chain, &flags);
3934 if(error == ERROR_SUCCESS) {
3935 ok_(__FILE__,line)(res, "InternetGetSecurityInfoByURLA failed: %u\n", GetLastError());
3936 ok_(__FILE__,line)(chain != NULL, "chain = NULL\n");
3937 ok_(__FILE__,line)(flags == ex_flags, "flags = %x\n", flags);
3938 CertFreeCertificateChain(chain);
3939 }else {
3940 ok_(__FILE__,line)(!res && GetLastError() == error,
3941 "InternetGetSecurityInfoByURLA returned: %x(%u), exected %u\n", res, GetLastError(), error);
3942 }
3943 }
3944
3945 #define test_secflags_option(a,b) _test_secflags_option(__LINE__,a,b)
3946 static void _test_secflags_option(unsigned line, HINTERNET req, DWORD ex_flags)
3947 {
3948 DWORD flags, size;
3949 BOOL res;
3950
3951 flags = 0xdeadbeef;
3952 size = sizeof(flags);
3953 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
3954 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
3955 ok_(__FILE__,line)(flags == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS flags = %x, expected %x\n", flags, ex_flags);
3956
3957 /* Option 98 is undocumented and seems to be the same as INTERNET_OPTION_SECURITY_FLAGS */
3958 flags = 0xdeadbeef;
3959 size = sizeof(flags);
3960 res = InternetQueryOptionW(req, 98, &flags, &size);
3961 ok_(__FILE__,line)(res, "InternetQueryOptionW(98) failed: %u\n", GetLastError());
3962 ok_(__FILE__,line)(flags == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS(98) flags = %x, expected %x\n", flags, ex_flags);
3963 }
3964
3965 #define set_secflags(a,b,c) _set_secflags(__LINE__,a,b,c)
3966 static void _set_secflags(unsigned line, HINTERNET req, BOOL use_undoc, DWORD flags)
3967 {
3968 BOOL res;
3969
3970 res = InternetSetOptionW(req, use_undoc ? 99 : INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
3971 ok_(__FILE__,line)(res, "InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
3972 }
3973
3974 static void test_security_flags(void)
3975 {
3976 HINTERNET ses, conn, req;
3977 DWORD size, flags;
3978 char buf[100];
3979 BOOL res;
3980
3981 trace("Testing security flags...\n");
3982
3983 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
3984
3985 ses = InternetOpen("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
3986 ok(ses != NULL, "InternetOpen failed\n");
3987
3988 pInternetSetStatusCallbackA(ses, &callback);
3989
3990 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3991 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
3992 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
3993 ok(conn != NULL, "InternetConnect failed with error %u\n", GetLastError());
3994 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3995
3996 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3997 req = HttpOpenRequest(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
3998 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
3999 0xdeadbeef);
4000 ok(req != NULL, "HttpOpenRequest failed\n");
4001 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4002
4003 flags = 0xdeadbeef;
4004 size = sizeof(flags);
4005 res = InternetQueryOptionW(req, 98, &flags, &size);
4006 if(!res && GetLastError() == ERROR_INVALID_PARAMETER) {
4007 win_skip("Incomplete security flags support, skipping\n");
4008
4009 close_async_handle(ses, hCompleteEvent, 2);
4010 CloseHandle(hCompleteEvent);
4011 return;
4012 }
4013
4014 test_secflags_option(req, 0);
4015 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4016
4017 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_REVOCATION);
4018 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION);
4019
4020 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
4021 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
4022
4023 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
4024 test_secflags_option(req, SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
4025
4026 flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_SECURE;
4027 res = InternetSetOptionW(req, 99, &flags, sizeof(flags));
4028 ok(!res && GetLastError() == ERROR_INTERNET_OPTION_NOT_SETTABLE, "InternetSetOption(99) failed: %u\n", GetLastError());
4029
4030 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
4031 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
4032 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4033 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4034 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
4035 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
4036 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
4037 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
4038 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4039 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4040 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4041
4042 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4043 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4044
4045 WaitForSingleObject(hCompleteEvent, INFINITE);
4046 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
4047
4048 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
4049 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
4050 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4051 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4052 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
4053 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
4054 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
4055 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
4056 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4057 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4058 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4059
4060 test_request_flags(req, 0);
4061 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA
4062 |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_STRENGTH_STRONG);
4063
4064 res = InternetReadFile(req, buf, sizeof(buf), &size);
4065 ok(res, "InternetReadFile failed: %u\n", GetLastError());
4066 ok(size, "size = 0\n");
4067
4068 /* Collect all existing persistent connections */
4069 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
4070 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
4071
4072 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4073 req = HttpOpenRequest(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
4074 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
4075 0xdeadbeef);
4076 ok(req != NULL, "HttpOpenRequest failed\n");
4077 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4078
4079 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT|INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY;
4080 res = InternetSetOption(req, INTERNET_OPTION_ERROR_MASK, (void*)&flags, sizeof(flags));
4081 ok(res, "InternetQueryOption(INTERNET_OPTION_ERROR_MASK failed: %u\n", GetLastError());
4082
4083 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4084 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4085 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
4086 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
4087 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4088 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4089 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4090
4091 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4092 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4093
4094 WaitForSingleObject(hCompleteEvent, INFINITE);
4095 ok(req_error == ERROR_INTERNET_SEC_CERT_REV_FAILED || broken(req_error == ERROR_INTERNET_SEC_CERT_ERRORS),
4096 "req_error = %d\n", req_error);
4097
4098 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4099 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4100 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
4101 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
4102 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4103 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4104 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4105
4106 if(req_error != ERROR_INTERNET_SEC_CERT_REV_FAILED) {
4107 win_skip("Unexpected cert errors %u, skipping security flags tests\n", req_error);
4108
4109 close_async_handle(ses, hCompleteEvent, 3);
4110 CloseHandle(hCompleteEvent);
4111 return;
4112 }
4113
4114 size = sizeof(buf);
4115 res = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
4116 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfoA(HTTP_QUERY_CONTENT_ENCODING) failed: %u\n", GetLastError());
4117
4118 test_request_flags(req, 8);
4119 test_secflags_option(req, 0x800000);
4120
4121 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_REVOCATION);
4122 test_secflags_option(req, 0x800000|SECURITY_FLAG_IGNORE_REVOCATION);
4123
4124 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4125 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4126 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
4127 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
4128 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4129 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4130 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4131
4132 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4133 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4134
4135 WaitForSingleObject(hCompleteEvent, INFINITE);
4136 ok(req_error == ERROR_INTERNET_SEC_CERT_ERRORS, "req_error = %d\n", req_error);
4137
4138 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4139 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4140 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
4141 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
4142 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4143 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4144 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4145
4146 test_request_flags(req, INTERNET_REQFLAG_NO_HEADERS);
4147 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
4148 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4149
4150 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
4151 test_secflags_option(req, 0x1800000|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_UNKNOWN_CA
4152 |SECURITY_FLAG_IGNORE_REVOCATION);
4153 test_http_version(req);
4154
4155 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4156 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4157 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
4158 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
4159 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
4160 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
4161 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4162 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4163 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4164
4165 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4166 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4167
4168 WaitForSingleObject(hCompleteEvent, INFINITE);
4169 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
4170
4171 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4172 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4173 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
4174 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
4175 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
4176 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
4177 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4178 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4179 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4180
4181 test_request_flags(req, 0);
4182 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION
4183 |SECURITY_FLAG_STRENGTH_STRONG|0x1800000);
4184
4185 test_cert_struct(req, &test_winehq_com_cert);
4186 test_security_info("https://test.winehq.com/data/some_file.html?q", 0, 0x1800000);
4187
4188 res = InternetReadFile(req, buf, sizeof(buf), &size);
4189 ok(res, "InternetReadFile failed: %u\n", GetLastError());
4190 ok(size, "size = 0\n");
4191
4192 close_async_handle(ses, hCompleteEvent, 3);
4193
4194 /* Collect all existing persistent connections */
4195 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
4196 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
4197
4198 /* Make another request, without setting security flags */
4199
4200 ses = InternetOpen("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
4201 ok(ses != NULL, "InternetOpen failed\n");
4202
4203 pInternetSetStatusCallbackA(ses, &callback);
4204
4205 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4206 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
4207 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
4208 ok(conn != NULL, "InternetConnect failed with error %u\n", GetLastError());
4209 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4210
4211 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4212 req = HttpOpenRequest(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
4213 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
4214 0xdeadbeef);
4215 ok(req != NULL, "HttpOpenRequest failed\n");
4216 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
4217
4218 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
4219 |SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
4220 test_http_version(req);
4221
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
4231 res = HttpSendRequest(req, NULL, 0, NULL, 0);
4232 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
4233
4234 WaitForSingleObject(hCompleteEvent, INFINITE);
4235 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
4236
4237 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4238 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4239 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
4240 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
4241 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
4242 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
4243 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4244 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4245
4246 test_request_flags(req, 0);
4247 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
4248 |SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
4249
4250 res = InternetReadFile(req, buf, sizeof(buf), &size);
4251 ok(res, "InternetReadFile failed: %u\n", GetLastError());
4252 ok(size, "size = 0\n");
4253
4254 close_async_handle(ses, hCompleteEvent, 2);
4255
4256 CloseHandle(hCompleteEvent);
4257
4258 test_security_info("http://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4259 test_security_info("file:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4260 test_security_info("xxx:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
4261 }
4262
4263 static void test_secure_connection(void)
4264 {
4265 static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
4266 static const WCHAR testsite[] = {'t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',0};
4267 static const WCHAR get[] = {'G','E','T',0};
4268 static const WCHAR testpage[] = {'/','t','e','s','t','s','/','h','e','l','l','o','.','h','t','m','l',0};
4269 HINTERNET ses, con, req;
4270 DWORD size, flags;
4271 INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
4272 INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
4273 BOOL ret;
4274
4275 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
4276 ok(ses != NULL, "InternetOpen failed\n");
4277
4278 con = InternetConnect(ses, "test.winehq.org",
4279 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
4280 INTERNET_SERVICE_HTTP, 0, 0);
4281 ok(con != NULL, "InternetConnect failed\n");
4282
4283 req = HttpOpenRequest(con, "GET", "/tests/hello.html", NULL, NULL, NULL,
4284 INTERNET_FLAG_SECURE, 0);
4285 ok(req != NULL, "HttpOpenRequest failed\n");
4286
4287 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
4288 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
4289
4290 size = sizeof(flags);
4291 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
4292 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4293 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
4294
4295 if (!winetest_interactive)
4296 {
4297 skip("test_cert_struct(req, &test_winehq_org_cert), ROSTESTS-121\n");
4298 }
4299 else
4300 {
4301 test_cert_struct(req, &test_winehq_org_cert);
4302 }
4303
4304 /* Querying the same option through InternetQueryOptionW still results in
4305 * ASCII strings being returned.
4306 */
4307 size = 0;
4308 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4309 NULL, &size);
4310 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
4311 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
4312 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
4313 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4314 certificate_structW, &size);
4315 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
4316 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4317 if (ret)
4318 {
4319 ok(certificate_structA->lpszSubjectInfo &&
4320 strlen(certificate_structA->lpszSubjectInfo) > 1,
4321 "expected a non-empty subject name\n");
4322 ok(certificate_structA->lpszIssuerInfo &&
4323 strlen(certificate_structA->lpszIssuerInfo) > 1,
4324 "expected a non-empty issuer name\n");
4325 ok(!certificate_structA->lpszSignatureAlgName,
4326 "unexpected signature algorithm name\n");
4327 ok(!certificate_structA->lpszEncryptionAlgName,
4328 "unexpected encryption algorithm name\n");
4329 ok(!certificate_structA->lpszProtocolName,
4330 "unexpected protocol name\n");
4331 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
4332 release_cert_info(certificate_structA);
4333 }
4334 HeapFree(GetProcessHeap(), 0, certificate_structW);
4335
4336 InternetCloseHandle(req);
4337 InternetCloseHandle(con);
4338 InternetCloseHandle(ses);
4339
4340 /* Repeating the tests with the W functions has the same result: */
4341 ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
4342 ok(ses != NULL, "InternetOpen failed\n");
4343
4344 con = InternetConnectW(ses, testsite,
4345 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
4346 INTERNET_SERVICE_HTTP, 0, 0);
4347 ok(con != NULL, "InternetConnect failed\n");
4348
4349 req = HttpOpenRequestW(con, get, testpage, NULL, NULL, NULL,
4350 INTERNET_FLAG_SECURE|INTERNET_FLAG_RELOAD, 0);
4351 ok(req != NULL, "HttpOpenRequest failed\n");
4352
4353 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
4354 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
4355
4356 size = sizeof(flags);
4357 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
4358 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4359 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set, got %x\n", flags);
4360
4361 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4362 NULL, &size);
4363 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
4364 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
4365 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
4366 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4367 certificate_structA, &size);
4368 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4369 if (ret)
4370 {
4371 ok(certificate_structA->lpszSubjectInfo &&
4372 strlen(certificate_structA->lpszSubjectInfo) > 1,
4373 "expected a non-empty subject name\n");
4374 ok(certificate_structA->lpszIssuerInfo &&
4375 strlen(certificate_structA->lpszIssuerInfo) > 1,
4376 "expected a non-empty issuer name\n");
4377 ok(!certificate_structA->lpszSignatureAlgName,
4378 "unexpected signature algorithm name\n");
4379 ok(!certificate_structA->lpszEncryptionAlgName,
4380 "unexpected encryption algorithm name\n");
4381 ok(!certificate_structA->lpszProtocolName,
4382 "unexpected protocol name\n");
4383 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
4384 release_cert_info(certificate_structA);
4385 }
4386 HeapFree(GetProcessHeap(), 0, certificate_structA);
4387
4388 /* Again, querying the same option through InternetQueryOptionW still
4389 * results in ASCII strings being returned.
4390 */
4391 size = 0;
4392 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4393 NULL, &size);
4394 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
4395 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
4396 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
4397 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
4398 certificate_structW, &size);
4399 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
4400 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
4401 if (ret)
4402 {
4403 ok(certificate_structA->lpszSubjectInfo &&
4404 strlen(certificate_structA->lpszSubjectInfo) > 1,
4405 "expected a non-empty subject name\n");
4406 ok(certificate_structA->lpszIssuerInfo &&
4407 strlen(certificate_structA->lpszIssuerInfo) > 1,
4408 "expected a non-empty issuer name\n");
4409 ok(!certificate_structA->lpszSignatureAlgName,
4410 "unexpected signature algorithm name\n");
4411 ok(!certificate_structA->lpszEncryptionAlgName,
4412 "unexpected encryption algorithm name\n");
4413 ok(!certificate_structA->lpszProtocolName,
4414 "unexpected protocol name\n");
4415 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
4416 release_cert_info(certificate_structA);
4417 }
4418 HeapFree(GetProcessHeap(), 0, certificate_structW);
4419
4420 InternetCloseHandle(req);
4421 InternetCloseHandle(con);
4422 InternetCloseHandle(ses);
4423 }
4424
4425 static void test_user_agent_header(void)
4426 {
4427 HINTERNET ses, con, req;
4428 DWORD size, err;
4429 char buffer[64];
4430 BOOL ret;
4431
4432 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
4433 ok(ses != NULL, "InternetOpen failed\n");
4434
4435 con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4436 ok(con != NULL, "InternetConnect failed\n");
4437
4438 req = HttpOpenRequest(con, "GET", "/tests/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
4439 ok(req != NULL, "HttpOpenRequest failed\n");
4440
4441 size = sizeof(buffer);
4442 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4443 err = GetLastError();
4444 ok(!ret, "HttpQueryInfo succeeded\n");
4445 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
4446
4447 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4448 ok(ret, "HttpAddRequestHeaders succeeded\n");
4449
4450 size = sizeof(buffer);
4451 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4452 err = GetLastError();
4453 ok(ret, "HttpQueryInfo failed\n");
4454 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
4455
4456 InternetCloseHandle(req);
4457
4458 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
4459 ok(req != NULL, "HttpOpenRequest failed\n");
4460
4461 size = sizeof(buffer);
4462 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4463 err = GetLastError();
4464 ok(!ret, "HttpQueryInfo succeeded\n");
4465 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
4466
4467 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4468 ok(ret, "HttpAddRequestHeaders failed\n");
4469
4470 buffer[0] = 0;
4471 size = sizeof(buffer);
4472 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4473 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
4474 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
4475
4476 InternetCloseHandle(req);
4477 InternetCloseHandle(con);
4478 InternetCloseHandle(ses);
4479 }
4480
4481 static void test_bogus_accept_types_array(void)
4482 {
4483 HINTERNET ses, con, req;
4484 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
4485 DWORD size, error;
4486 char buffer[32];
4487 BOOL ret;
4488
4489 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
4490 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4491 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
4492
4493 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
4494
4495 buffer[0] = 0;
4496 size = sizeof(buffer);
4497 SetLastError(0xdeadbeef);
4498 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
4499 error = GetLastError();
4500 ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
4501 if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", error);
4502 ok(broken(!strcmp(buffer, ", */*, %p, , , */*")) /* IE6 */ ||
4503 broken(!strcmp(buffer, "*/*, %p, */*")) /* IE7/8 */ ||
4504 !strcmp(buffer, ""), "got '%s' expected ''\n", buffer);
4505
4506 InternetCloseHandle(req);
4507 InternetCloseHandle(con);
4508 InternetCloseHandle(ses);
4509 }
4510
4511 struct context
4512 {
4513 HANDLE event;
4514 HINTERNET req;
4515 };
4516
4517 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
4518 {
4519 INTERNET_ASYNC_RESULT *result = info;
4520 struct context *ctx = (struct context *)context;
4521
4522 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
4523
4524 switch(status) {
4525 case INTERNET_STATUS_REQUEST_COMPLETE:
4526 trace("request handle: 0x%08lx\n", result->dwResult);
4527 ctx->req = (HINTERNET)result->dwResult;
4528 SetEvent(ctx->event);
4529 break;
4530 case INTERNET_STATUS_HANDLE_CLOSING: {
4531 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
4532
4533 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
4534 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
4535 SetEvent(ctx->event);
4536 break;
4537 }
4538 case INTERNET_STATUS_NAME_RESOLVED:
4539 case INTERNET_STATUS_CONNECTING_TO_SERVER:
4540 case INTERNET_STATUS_CONNECTED_TO_SERVER: {
4541 char *str = info;
4542 ok(str[0] && str[1], "Got string: %s\n", str);
4543 ok(size == strlen(str)+1, "unexpected size %u\n", size);
4544 }
4545 }
4546 }
4547
4548 static void test_open_url_async(void)
4549 {
4550 BOOL ret;
4551 HINTERNET ses, req;
4552 DWORD size, error;
4553 struct context ctx;
4554 ULONG type;
4555
4556 /* Collect all existing persistent connections */
4557 ret = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
4558 ok(ret, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
4559
4560 /*
4561 * Some versions of IE6 fail those tests. They pass some notification data as UNICODE string, while
4562 * other versions never do. They also hang of following tests. We disable it for everything older
4563 * than IE7.
4564 */
4565 if(!pInternetGetSecurityInfoByURLA) {
4566 win_skip("Skipping async open on too old wininet version.\n");
4567 return;
4568 }
4569
4570 ctx.req = NULL;
4571 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
4572
4573 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
4574 ok(ses != NULL, "InternetOpen failed\n");
4575
4576 SetLastError(0xdeadbeef);
4577 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
4578 error = GetLastError();
4579 ok(!ret, "InternetSetOptionA succeeded\n");
4580 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
4581
4582 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
4583 error = GetLastError();
4584 ok(!ret, "InternetSetOptionA failed\n");
4585 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
4586
4587 pInternetSetStatusCallbackW(ses, cb);
4588 ResetEvent(ctx.event);
4589
4590 req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
4591 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
4592
4593 WaitForSingleObject(ctx.event, INFINITE);
4594
4595 type = 0;
4596 size = sizeof(type);
4597 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
4598 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
4599 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
4600 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
4601
4602 size = 0;
4603 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
4604 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
4605 ok(size > 0, "expected size > 0\n");
4606
4607 ResetEvent(ctx.event);
4608 InternetCloseHandle(ctx.req);
4609 WaitForSingleObject(ctx.event, INFINITE);
4610
4611 InternetCloseHandle(ses);
4612 CloseHandle(ctx.event);
4613 }
4614
4615 enum api
4616 {
4617 internet_connect = 1,
4618 http_open_request,
4619 http_send_request_ex,
4620 internet_writefile,
4621 http_end_request,
4622 internet_close_handle
4623 };
4624
4625 struct notification
4626 {
4627 enum api function; /* api responsible for notification */
4628 unsigned int status; /* status received */
4629 int async; /* delivered from another thread? */
4630 int todo;
4631 int optional;
4632 };
4633
4634 struct info
4635 {
4636 enum api function;
4637 const struct notification *test;
4638 unsigned int count;
4639 unsigned int index;
4640 HANDLE wait;
4641 DWORD thread;
4642 unsigned int line;
4643 DWORD expect_result;
4644 BOOL is_aborted;
4645 };
4646
4647 static CRITICAL_SECTION notification_cs;
4648
4649 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
4650 {
4651 BOOL status_ok, function_ok;
4652 struct info *info = (struct info *)context;
4653 unsigned int i;
4654
4655 EnterCriticalSection( &notification_cs );
4656
4657 if(info->is_aborted) {
4658 LeaveCriticalSection(&notification_cs);
4659 return;
4660 }
4661
4662 if (status == INTERNET_STATUS_HANDLE_CREATED)
4663 {
4664 DWORD size = sizeof(struct info *);
4665 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
4666 }else if(status == INTERNET_STATUS_REQUEST_COMPLETE) {
4667 INTERNET_ASYNC_RESULT *ar = (INTERNET_ASYNC_RESULT*)buffer;
4668
4669 ok(buflen == sizeof(*ar), "unexpected buflen = %d\n", buflen);
4670 if(info->expect_result == ERROR_SUCCESS) {
4671 ok(ar->dwResult == 1, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
4672 }else {
4673 ok(!ar->dwResult, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
4674 ok(ar->dwError == info->expect_result, "ar->dwError = %d, expected %d\n", ar->dwError, info->expect_result);
4675 }
4676 }
4677
4678 i = info->index;
4679 if (i >= info->count)
4680 {
4681 LeaveCriticalSection( &notification_cs );
4682 return;
4683 }
4684
4685 while (info->test[i].status != status &&
4686 (info->test[i].optional || info->test[i].todo) &&
4687 i < info->count - 1 &&
4688 info->test[i].function == info->test[i + 1].function)
4689 {
4690 i++;
4691 }
4692
4693 status_ok = (info->test[i].status == status);
4694 function_ok = (info->test[i].function == info->function);
4695
4696 if (!info->test[i].todo)
4697 {
4698 ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
4699 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
4700
4701 if (info->test[i].async)
4702 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
4703 info->line, info->thread, GetCurrentThreadId());
4704 }
4705 else
4706 {
4707 todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
4708 if (status_ok)
4709 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
4710 }
4711 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
4712 info->index = i+1;
4713
4714 LeaveCriticalSection( &notification_cs );
4715 }
4716
4717 static void setup_test( struct info *info, enum api function, unsigned int line, DWORD expect_result )
4718 {
4719 info->function = function;
4720 info->line = line;
4721 info->expect_result = expect_result;
4722 }
4723
4724 struct notification_data
4725 {
4726 const struct notification *test;
4727 const unsigned int count;
4728 const char *method;
4729 const char *host;
4730 const char *path;
4731 const char *data;
4732 BOOL expect_conn_failure;
4733 };
4734
4735 static const struct notification async_send_request_ex_test[] =
4736 {
4737 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
4738 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
4739 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4740 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, 1, 0, 1 },
4741 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
4742 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
4743 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
4744 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
4745 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
4746 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
4747 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4748 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, 0 },
4749 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, 0 },
4750 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
4751 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
4752 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4753 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
4754 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
4755 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
4756 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
4757 };
4758
4759 static const struct notification async_send_request_ex_test2[] =
4760 {
4761 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
4762 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
4763 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4764 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, 1, 0, 1 },
4765 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
4766 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
4767 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1, 1 },
4768 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1, 1 },
4769 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
4770 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
4771 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4772 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
4773 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
4774 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4775 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
4776 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
4777 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
4778 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
4779 };
4780
4781 static const struct notification async_send_request_ex_resolve_failure_test[] =
4782 {
4783 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
4784 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
4785 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4786 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1 },
4787 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
4788 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4789 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
4790 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
4791 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
4792 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
4793 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
4794 };
4795
4796 static const struct notification_data notification_data[] = {
4797 {
4798 async_send_request_ex_test,
4799 sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
4800 "POST",
4801 "test.winehq.org",
4802 "tests/posttest.php",
4803 "Public ID=codeweavers"
4804 },
4805 {
4806 async_send_request_ex_test2,
4807 sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
4808 "POST",
4809 "test.winehq.org",
4810 "tests/posttest.php"
4811 },
4812 {
4813 async_send_request_ex_resolve_failure_test,
4814 sizeof(async_send_request_ex_resolve_failure_test)/sizeof(async_send_request_ex_resolve_failure_test[0]),
4815 "GET",
4816 "brokenhost",
4817 "index.html",
4818 NULL,
4819 TRUE
4820 }
4821 };
4822
4823 static void test_async_HttpSendRequestEx(const struct notification_data *nd)
4824 {
4825 BOOL ret;
4826 HINTERNET ses, req, con;
4827 struct info info;
4828 DWORD size, written, error;
4829 INTERNET_BUFFERSA b;
4830 static const char *accept[2] = {"*/*", NULL};
4831 char buffer[32];
4832
4833 trace("Async HttpSendRequestEx test (%s %s)\n", nd->method, nd->host);
4834
4835 InitializeCriticalSection( &notification_cs );
4836
4837 info.test = nd->test;
4838 info.count = nd->count;
4839 info.index = 0;
4840 info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
4841 info.thread = GetCurrentThreadId();
4842 info.is_aborted = FALSE;
4843
4844 ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
4845 ok( ses != NULL, "InternetOpen failed\n" );
4846
4847 pInternetSetStatusCallbackA( ses, check_notification );
4848
4849 setup_test( &info, internet_connect, __LINE__, ERROR_SUCCESS );
4850 con = InternetConnect( ses, nd->host, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
4851 ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
4852
4853 WaitForSingleObject( info.wait, 10000 );
4854
4855 setup_test( &info, http_open_request, __LINE__, ERROR_SUCCESS );
4856 req = HttpOpenRequest( con, nd->method, nd->path, NULL, NULL, accept, 0, (DWORD_PTR)&info );
4857 ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
4858
4859 WaitForSingleObject( info.wait, 10000 );
4860
4861 if(nd->data) {
4862 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
4863 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
4864 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
4865 b.dwHeadersLength = strlen( b.lpcszHeader );
4866 b.dwBufferTotal = nd->data ? strlen( nd->data ) : 0;
4867 }
4868
4869 setup_test( &info, http_send_request_ex, __LINE__,
4870 nd->expect_conn_failure ? ERROR_INTERNET_NAME_NOT_RESOLVED : ERROR_SUCCESS );
4871 ret = HttpSendRequestExA( req, nd->data ? &b : NULL, NULL, 0x28, 0 );
4872 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
4873
4874 error = WaitForSingleObject( info.wait, 10000 );
4875 if(error != WAIT_OBJECT_0) {
4876 skip("WaitForSingleObject returned %d, assuming DNS problem\n", error);
4877 info.is_aborted = TRUE;
4878 goto abort;
4879 }
4880
4881 size = sizeof(buffer);
4882 SetLastError( 0xdeadbeef );
4883 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
4884 error = GetLastError();
4885 ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
4886 if(nd->expect_conn_failure) {
4887 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND got %u\n", error );
4888 }else {
4889 todo_wine
4890 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
4891 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
4892 }
4893
4894 if (nd->data)
4895 {
4896 written = 0;
4897 size = strlen( nd->data );
4898 setup_test( &info, internet_writefile, __LINE__, ERROR_SUCCESS );
4899 ret = InternetWriteFile( req, nd->data, size, &written );
4900 ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
4901 ok( written == size, "expected %u got %u\n", written, size );
4902
4903 WaitForSingleObject( info.wait, 10000 );
4904
4905 SetLastError( 0xdeadbeef );
4906 ret = HttpEndRequestA( req, (void *)nd->data, 0x28, 0 );
4907 error = GetLastError();
4908 ok( !ret, "HttpEndRequestA succeeded\n" );
4909 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
4910 }
4911
4912 SetLastError( 0xdeadbeef );
4913 setup_test( &info, http_end_request, __LINE__,
4914 nd->expect_conn_failure ? ERROR_INTERNET_OPERATION_CANCELLED : ERROR_SUCCESS);
4915 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
4916 error = GetLastError();
4917 ok( !ret, "HttpEndRequestA succeeded\n" );
4918 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
4919
4920 WaitForSingleObject( info.wait, 10000 );
4921
4922 setup_test( &info, internet_close_handle, __LINE__, ERROR_SUCCESS );
4923 abort:
4924 InternetCloseHandle( req );
4925 InternetCloseHandle( con );
4926 InternetCloseHandle( ses );
4927
4928 WaitForSingleObject( info.wait, 10000 );
4929 Sleep(100);
4930 CloseHandle( info.wait );
4931 }
4932
4933 static HINTERNET closetest_session, closetest_req, closetest_conn;
4934 static BOOL closetest_closed;
4935
4936 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
4937 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
4938 {
4939 DWORD len, type;
4940 BOOL res;
4941
4942 trace("closetest_callback %p: %d\n", hInternet, dwInternetStatus);
4943
4944 ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
4945 "Unexpected hInternet %p\n", hInternet);
4946 if(!closetest_closed)
4947 return;
4948
4949 len = sizeof(type);
4950 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
4951 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
4952 "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
4953 closetest_req, res, GetLastError());
4954 }
4955
4956 static void test_InternetCloseHandle(void)
4957 {
4958 DWORD len, flags;
4959 BOOL res;
4960
4961 closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
4962 ok(closetest_session != NULL,"InternetOpen failed with error %u\n", GetLastError());
4963
4964 pInternetSetStatusCallbackA(closetest_session, closetest_callback);
4965
4966 closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
4967 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
4968 ok(closetest_conn != NULL,"InternetConnect failed with error %u\n", GetLastError());
4969
4970 closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
4971 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
4972
4973 res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
4974 ok(!res && (GetLastError() == ERROR_IO_PENDING),
4975 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
4976
4977 test_request_flags(closetest_req, INTERNET_REQFLAG_NO_HEADERS);
4978
4979 res = InternetCloseHandle(closetest_session);
4980 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
4981 closetest_closed = TRUE;
4982 trace("Closed session handle\n");
4983
4984 res = InternetCloseHandle(closetest_conn);
4985 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %u\n",
4986 res, GetLastError());
4987
4988 res = InternetCloseHandle(closetest_req);
4989 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %u\n",
4990 res, GetLastError());
4991
4992 len = sizeof(flags);
4993 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
4994 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
4995 "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
4996 closetest_req, res, GetLastError());
4997 }
4998
4999 static void test_connection_failure(void)
5000 {
5001 HINTERNET session, connect, request;
5002 DWORD error;
5003 BOOL ret;
5004
5005 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
5006 ok(session != NULL, "failed to get session handle\n");
5007
5008 connect = InternetConnectA(session, "localhost", 1, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
5009 ok(connect != NULL, "failed to get connection handle\n");
5010
5011 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, 0, 0);
5012 ok(request != NULL, "failed to get request handle\n");
5013
5014 SetLastError(0xdeadbeef);
5015 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
5016 error = GetLastError();
5017 ok(!ret, "unexpected success\n");
5018 ok(error == ERROR_INTERNET_CANNOT_CONNECT, "wrong error %u\n", error);
5019
5020 InternetCloseHandle(request);
5021 InternetCloseHandle(connect);
5022 InternetCloseHandle(session);
5023 }
5024
5025 static void test_default_service_port(void)
5026 {
5027 HINTERNET session, connect, request;
5028 DWORD error;
5029 BOOL ret;
5030
5031 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
5032 ok(session != NULL, "InternetOpen failed\n");
5033
5034 connect = InternetConnect(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
5035 INTERNET_SERVICE_HTTP, 0, 0);
5036 ok(connect != NULL, "InternetConnect failed\n");
5037
5038 request = HttpOpenRequest(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
5039 ok(request != NULL, "HttpOpenRequest failed\n");
5040
5041 SetLastError(0xdeadbeef);
5042 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
5043 error = GetLastError();
5044 ok(!ret, "HttpSendRequest succeeded\n");
5045 ok(error == ERROR_INTERNET_SECURITY_CHANNEL_ERROR || error == ERROR_INTERNET_CANNOT_CONNECT,
5046 "got %u\n", error);
5047
5048 InternetCloseHandle(request);
5049 InternetCloseHandle(connect);
5050 InternetCloseHandle(session);
5051 }
5052
5053 static void init_status_tests(void)
5054 {
5055 memset(expect, 0, sizeof(expect));
5056 memset(optional, 0, sizeof(optional));
5057 memset(wine_allow, 0, sizeof(wine_allow));
5058 memset(notified, 0, sizeof(notified));
5059 memset(status_string, 0, sizeof(status_string));
5060
5061 #define STATUS_STRING(status) status_string[status] = #status
5062 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
5063 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
5064 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
5065 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
5066 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
5067 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
5068 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
5069 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
5070 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
5071 STATUS_STRING(INTERNET_STATUS_PREFETCH);
5072 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
5073 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
5074 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
5075 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
5076 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
5077 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
5078 STATUS_STRING(INTERNET_STATUS_REDIRECT);
5079 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
5080 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
5081 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
5082 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
5083 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
5084 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
5085 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
5086 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
5087 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
5088 #undef STATUS_STRING
5089 }
5090
5091 START_TEST(http)
5092 {
5093 HMODULE hdll;
5094 hdll = GetModuleHandleA("wininet.dll");
5095
5096 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
5097 win_skip("Too old IE (older than 6.0)\n");
5098 return;
5099 }
5100
5101 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
5102 pInternetSetStatusCallbackW = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackW");
5103 pInternetGetSecurityInfoByURLA = (void*)GetProcAddress(hdll, "InternetGetSecurityInfoByURLA");
5104
5105 init_status_tests();
5106 test_InternetCloseHandle();
5107 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
5108 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
5109 InternetReadFile_test(0, &test_data[1]);
5110 first_connection_to_test_url = TRUE;
5111 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
5112 test_security_flags();
5113 InternetReadFile_test(0, &test_data[2]);
5114 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
5115 test_open_url_async();
5116 test_async_HttpSendRequestEx(&notification_data[0]);
5117 test_async_HttpSendRequestEx(&notification_data[1]);
5118 test_async_HttpSendRequestEx(&notification_data[2]);
5119 InternetOpenRequest_test();
5120 test_http_cache();
5121 InternetOpenUrlA_test();
5122 HttpHeaders_test();
5123 test_http_connection();
5124 test_secure_connection();
5125 test_user_agent_header();
5126 test_bogus_accept_types_array();
5127 InternetReadFile_chunked_test();
5128 HttpSendRequestEx_test();
5129 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[3]);
5130 test_connection_failure();
5131 test_default_service_port();
5132 }