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