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