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