[WININET_WINETEST] Sync with Wine Staging 1.9.16. CORE-11866
[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[10], 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, "HEAD /test_auth_host1"))
2472 {
2473 if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzcw=="))
2474 send(c, okmsg, sizeof okmsg-1, 0);
2475 else
2476 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2477 }
2478 if (strstr(buffer, "HEAD /test_auth_host2"))
2479 {
2480 if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzczI="))
2481 send(c, okmsg, sizeof okmsg-1, 0);
2482 else
2483 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2484 }
2485 shutdown(c, 2);
2486 closesocket(c);
2487 c = -1;
2488 } while (!last_request);
2489
2490 closesocket(s);
2491 HeapFree(GetProcessHeap(), 0, buffer);
2492
2493 return 0;
2494 }
2495
2496 static void test_basic_request(int port, const char *verb, const char *url)
2497 {
2498 test_request_t req;
2499 DWORD r, count, error;
2500 char buffer[0x100];
2501
2502 trace("basic request %s %s\n", verb, url);
2503
2504 open_simple_request(&req, "localhost", port, verb, url);
2505
2506 SetLastError(0xdeadbeef);
2507 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2508 error = GetLastError();
2509 ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
2510 ok(r, "HttpSendRequest failed: %u\n", GetLastError());
2511
2512 count = 0;
2513 memset(buffer, 0, sizeof buffer);
2514 SetLastError(0xdeadbeef);
2515 r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
2516 ok(r, "InternetReadFile failed %u\n", GetLastError());
2517 ok(count == sizeof page1 - 1, "count was wrong\n");
2518 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
2519
2520 close_request(&req);
2521 }
2522
2523 static void test_proxy_indirect(int port)
2524 {
2525 test_request_t req;
2526 DWORD r, sz;
2527 char buffer[0x40];
2528
2529 open_simple_request(&req, "localhost", port, NULL, "/test2");
2530
2531 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2532 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2533
2534 sz = sizeof buffer;
2535 r = HttpQueryInfoA(req.request, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
2536 ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
2537 if (!r)
2538 {
2539 skip("missing proxy header, not testing remaining proxy headers\n");
2540 goto out;
2541 }
2542 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
2543
2544 test_status_code(req.request, 407);
2545 test_request_flags(req.request, 0);
2546
2547 sz = sizeof buffer;
2548 r = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
2549 ok(r, "HttpQueryInfo failed\n");
2550 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
2551
2552 sz = sizeof buffer;
2553 r = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &sz, NULL);
2554 ok(r, "HttpQueryInfo failed\n");
2555 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
2556
2557 sz = sizeof buffer;
2558 r = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &sz, NULL);
2559 ok(r, "HttpQueryInfo failed\n");
2560 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
2561
2562 sz = sizeof buffer;
2563 r = HttpQueryInfoA(req.request, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
2564 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
2565 ok(r == FALSE, "HttpQueryInfo failed\n");
2566
2567 out:
2568 close_request(&req);
2569 }
2570
2571 static void test_proxy_direct(int port)
2572 {
2573 HINTERNET hi, hc, hr;
2574 DWORD r, sz, error;
2575 char buffer[0x40], *url;
2576 WCHAR bufferW[0x40];
2577 static const char url_fmt[] = "http://test.winehq.org:%u/test2";
2578 static CHAR username[] = "mike",
2579 password[] = "1101",
2580 useragent[] = "winetest";
2581 static const WCHAR usernameW[] = {'m','i','k','e',0},
2582 passwordW[] = {'1','1','0','1',0},
2583 useragentW[] = {'w','i','n','e','t','e','s','t',0};
2584
2585 /* specify proxy type without the proxy and bypass */
2586 SetLastError(0xdeadbeef);
2587 hi = InternetOpenW(NULL, INTERNET_OPEN_TYPE_PROXY, NULL, NULL, 0);
2588 error = GetLastError();
2589 ok(error == ERROR_INVALID_PARAMETER ||
2590 broken(error == ERROR_SUCCESS) /* WinXPProSP2 */, "got %u\n", error);
2591 ok(hi == NULL || broken(!!hi) /* WinXPProSP2 */, "open should have failed\n");
2592
2593 sprintf(buffer, "localhost:%d\n", port);
2594 hi = InternetOpenA(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
2595 ok(hi != NULL, "open failed\n");
2596
2597 /* try connect without authorization */
2598 hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2599 ok(hc != NULL, "connect failed\n");
2600
2601 hr = HttpOpenRequestA(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
2602 ok(hr != NULL, "HttpOpenRequest failed\n");
2603
2604 sz = 0;
2605 SetLastError(0xdeadbeef);
2606 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, NULL, &sz);
2607 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2608 ok(!r, "unexpected success\n");
2609 ok(sz == 1, "got %u\n", sz);
2610
2611 sz = 0;
2612 SetLastError(0xdeadbeef);
2613 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, NULL, &sz);
2614 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2615 ok(!r, "unexpected success\n");
2616 ok(sz == 1, "got %u\n", sz);
2617
2618 sz = sizeof(buffer);
2619 SetLastError(0xdeadbeef);
2620 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2621 ok(r, "unexpected failure %u\n", GetLastError());
2622 ok(!sz, "got %u\n", sz);
2623
2624 sz = sizeof(buffer);
2625 SetLastError(0xdeadbeef);
2626 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2627 ok(r, "unexpected failure %u\n", GetLastError());
2628 ok(!sz, "got %u\n", sz);
2629
2630 sz = 0;
2631 SetLastError(0xdeadbeef);
2632 r = InternetQueryOptionA(hr, INTERNET_OPTION_PASSWORD, NULL, &sz);
2633 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2634 ok(!r, "unexpected success\n");
2635 ok(sz == 1, "got %u\n", sz);
2636
2637 sz = 0;
2638 SetLastError(0xdeadbeef);
2639 r = InternetQueryOptionA(hr, INTERNET_OPTION_USERNAME, NULL, &sz);
2640 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2641 ok(!r, "unexpected success\n");
2642 ok(sz == 1, "got %u\n", sz);
2643
2644 sz = sizeof(buffer);
2645 SetLastError(0xdeadbeef);
2646 r = InternetQueryOptionA(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2647 ok(r, "unexpected failure %u\n", GetLastError());
2648 ok(!sz, "got %u\n", sz);
2649
2650 sz = sizeof(buffer);
2651 SetLastError(0xdeadbeef);
2652 r = InternetQueryOptionA(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2653 ok(r, "unexpected failure %u\n", GetLastError());
2654 ok(!sz, "got %u\n", sz);
2655
2656 sz = 0;
2657 SetLastError(0xdeadbeef);
2658 r = InternetQueryOptionA(hr, INTERNET_OPTION_URL, NULL, &sz);
2659 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2660 ok(!r, "unexpected success\n");
2661 ok(sz == 34, "got %u\n", sz);
2662
2663 sz = sizeof(buffer);
2664 SetLastError(0xdeadbeef);
2665 r = InternetQueryOptionA(hr, INTERNET_OPTION_URL, buffer, &sz);
2666 ok(r, "unexpected failure %u\n", GetLastError());
2667 ok(sz == 33, "got %u\n", sz);
2668
2669 r = HttpSendRequestW(hr, NULL, 0, NULL, 0);
2670 ok(r || broken(!r), "HttpSendRequest failed %u\n", GetLastError());
2671 if (!r)
2672 {
2673 win_skip("skipping proxy tests on broken wininet\n");
2674 goto done;
2675 }
2676
2677 test_status_code(hr, 407);
2678
2679 /* set the user + password then try again */
2680 r = InternetSetOptionA(hi, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2681 ok(!r, "unexpected success\n");
2682
2683 r = InternetSetOptionA(hc, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2684 ok(r, "failed to set user\n");
2685
2686 r = InternetSetOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2687 ok(r, "failed to set user\n");
2688
2689 buffer[0] = 0;
2690 sz = 3;
2691 SetLastError(0xdeadbeef);
2692 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2693 ok(!r, "unexpected failure %u\n", GetLastError());
2694 ok(!buffer[0], "got %s\n", buffer);
2695 ok(sz == strlen(username) + 1, "got %u\n", sz);
2696
2697 buffer[0] = 0;
2698 sz = 0;
2699 SetLastError(0xdeadbeef);
2700 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2701 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2702 ok(!r, "unexpected success\n");
2703 ok(sz == strlen(username) + 1, "got %u\n", sz);
2704
2705 bufferW[0] = 0;
2706 sz = 0;
2707 SetLastError(0xdeadbeef);
2708 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_USERNAME, bufferW, &sz);
2709 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2710 ok(!r, "unexpected success\n");
2711 ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2712
2713 buffer[0] = 0;
2714 sz = sizeof(buffer);
2715 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2716 ok(r, "failed to get username\n");
2717 ok(!strcmp(buffer, username), "got %s\n", buffer);
2718 ok(sz == strlen(username), "got %u\n", sz);
2719
2720 buffer[0] = 0;
2721 sz = sizeof(bufferW);
2722 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_USERNAME, bufferW, &sz);
2723 ok(r, "failed to get username\n");
2724 ok(!lstrcmpW(bufferW, usernameW), "wrong username\n");
2725 ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2726
2727 r = InternetSetOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, username, 1);
2728 ok(r, "failed to set user\n");
2729
2730 buffer[0] = 0;
2731 sz = sizeof(buffer);
2732 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_USERNAME, buffer, &sz);
2733 ok(r, "failed to get username\n");
2734 ok(!strcmp(buffer, username), "got %s\n", buffer);
2735 ok(sz == strlen(username), "got %u\n", sz);
2736
2737 r = InternetSetOptionA(hi, INTERNET_OPTION_USER_AGENT, useragent, 1);
2738 ok(r, "failed to set useragent\n");
2739
2740 buffer[0] = 0;
2741 sz = 0;
2742 SetLastError(0xdeadbeef);
2743 r = InternetQueryOptionA(hi, INTERNET_OPTION_USER_AGENT, buffer, &sz);
2744 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2745 ok(!r, "unexpected success\n");
2746 ok(sz == strlen(useragent) + 1, "got %u\n", sz);
2747
2748 buffer[0] = 0;
2749 sz = sizeof(buffer);
2750 r = InternetQueryOptionA(hi, INTERNET_OPTION_USER_AGENT, buffer, &sz);
2751 ok(r, "failed to get user agent\n");
2752 ok(!strcmp(buffer, useragent), "got %s\n", buffer);
2753 ok(sz == strlen(useragent), "got %u\n", sz);
2754
2755 bufferW[0] = 0;
2756 sz = 0;
2757 SetLastError(0xdeadbeef);
2758 r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2759 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2760 ok(!r, "unexpected success\n");
2761 ok(sz == (lstrlenW(useragentW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2762
2763 bufferW[0] = 0;
2764 sz = sizeof(bufferW);
2765 r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2766 ok(r, "failed to get user agent\n");
2767 ok(!lstrcmpW(bufferW, useragentW), "wrong user agent\n");
2768 ok(sz == lstrlenW(useragentW), "got %u\n", sz);
2769
2770 r = InternetSetOptionA(hr, INTERNET_OPTION_USERNAME, username, 1);
2771 ok(r, "failed to set user\n");
2772
2773 buffer[0] = 0;
2774 sz = 0;
2775 SetLastError(0xdeadbeef);
2776 r = InternetQueryOptionA(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2777 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2778 ok(!r, "unexpected success\n");
2779 ok(sz == strlen(username) + 1, "got %u\n", sz);
2780
2781 buffer[0] = 0;
2782 sz = sizeof(buffer);
2783 r = InternetQueryOptionA(hr, INTERNET_OPTION_USERNAME, buffer, &sz);
2784 ok(r, "failed to get user\n");
2785 ok(!strcmp(buffer, username), "got %s\n", buffer);
2786 ok(sz == strlen(username), "got %u\n", sz);
2787
2788 bufferW[0] = 0;
2789 sz = 0;
2790 SetLastError(0xdeadbeef);
2791 r = InternetQueryOptionW(hr, INTERNET_OPTION_USERNAME, bufferW, &sz);
2792 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2793 ok(!r, "unexpected success\n");
2794 ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2795
2796 bufferW[0] = 0;
2797 sz = sizeof(bufferW);
2798 r = InternetQueryOptionW(hr, INTERNET_OPTION_USERNAME, bufferW, &sz);
2799 ok(r, "failed to get user\n");
2800 ok(!lstrcmpW(bufferW, usernameW), "wrong user\n");
2801 ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2802
2803 r = InternetSetOptionA(hr, INTERNET_OPTION_PASSWORD, password, 1);
2804 ok(r, "failed to set password\n");
2805
2806 buffer[0] = 0;
2807 sz = 0;
2808 SetLastError(0xdeadbeef);
2809 r = InternetQueryOptionA(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2810 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2811 ok(!r, "unexpected success\n");
2812 ok(sz == strlen(password) + 1, "got %u\n", sz);
2813
2814 buffer[0] = 0;
2815 sz = sizeof(buffer);
2816 r = InternetQueryOptionA(hr, INTERNET_OPTION_PASSWORD, buffer, &sz);
2817 ok(r, "failed to get password\n");
2818 ok(!strcmp(buffer, password), "got %s\n", buffer);
2819 ok(sz == strlen(password), "got %u\n", sz);
2820
2821 bufferW[0] = 0;
2822 sz = 0;
2823 SetLastError(0xdeadbeef);
2824 r = InternetQueryOptionW(hr, INTERNET_OPTION_PASSWORD, bufferW, &sz);
2825 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2826 ok(!r, "unexpected success\n");
2827 ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2828
2829 bufferW[0] = 0;
2830 sz = sizeof(bufferW);
2831 r = InternetQueryOptionW(hr, INTERNET_OPTION_PASSWORD, bufferW, &sz);
2832 ok(r, "failed to get password\n");
2833 ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2834 ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2835
2836 url = HeapAlloc(GetProcessHeap(), 0, strlen(url_fmt) + 11);
2837 sprintf(url, url_fmt, port);
2838 buffer[0] = 0;
2839 sz = 0;
2840 SetLastError(0xdeadbeef);
2841 r = InternetQueryOptionA(hr, INTERNET_OPTION_URL, buffer, &sz);
2842 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2843 ok(!r, "unexpected success\n");
2844 ok(sz == strlen(url) + 1, "got %u\n", sz);
2845
2846 buffer[0] = 0;
2847 sz = sizeof(buffer);
2848 r = InternetQueryOptionA(hr, INTERNET_OPTION_URL, buffer, &sz);
2849 ok(r, "failed to get url\n");
2850 ok(!strcmp(buffer, url), "got %s\n", buffer);
2851 ok(sz == strlen(url), "got %u\n", sz);
2852
2853 bufferW[0] = 0;
2854 sz = 0;
2855 SetLastError(0xdeadbeef);
2856 r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2857 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2858 ok(!r, "unexpected success\n");
2859 ok(sz == (strlen(url) + 1) * sizeof(WCHAR), "got %u\n", sz);
2860
2861 bufferW[0] = 0;
2862 sz = sizeof(bufferW);
2863 r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2864 ok(r, "failed to get url\n");
2865 ok(!strcmp_wa(bufferW, url), "wrong url\n");
2866 ok(sz == strlen(url), "got %u\n", sz);
2867 HeapFree(GetProcessHeap(), 0, url);
2868
2869 r = InternetSetOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
2870 ok(r, "failed to set password\n");
2871
2872 buffer[0] = 0;
2873 sz = 0;
2874 SetLastError(0xdeadbeef);
2875 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2876 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2877 ok(!r, "unexpected success\n");
2878 ok(sz == strlen(password) + 1, "got %u\n", sz);
2879
2880 buffer[0] = 0;
2881 sz = sizeof(buffer);
2882 r = InternetQueryOptionA(hr, INTERNET_OPTION_PROXY_PASSWORD, buffer, &sz);
2883 ok(r, "failed to get password\n");
2884 ok(!strcmp(buffer, password), "got %s\n", buffer);
2885 ok(sz == strlen(password), "got %u\n", sz);
2886
2887 bufferW[0] = 0;
2888 sz = 0;
2889 SetLastError(0xdeadbeef);
2890 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_PASSWORD, bufferW, &sz);
2891 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %u\n", GetLastError());
2892 ok(!r, "unexpected success\n");
2893 ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2894
2895 bufferW[0] = 0;
2896 sz = sizeof(bufferW);
2897 r = InternetQueryOptionW(hr, INTERNET_OPTION_PROXY_PASSWORD, bufferW, &sz);
2898 ok(r, "failed to get password\n");
2899 ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2900 ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2901
2902 r = HttpSendRequestW(hr, NULL, 0, NULL, 0);
2903 if (!r)
2904 {
2905 win_skip("skipping proxy tests on broken wininet\n");
2906 goto done;
2907 }
2908 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2909 sz = sizeof buffer;
2910 r = HttpQueryInfoA(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
2911 ok(r, "HttpQueryInfo failed\n");
2912 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
2913
2914 InternetCloseHandle(hr);
2915 InternetCloseHandle(hc);
2916 InternetCloseHandle(hi);
2917
2918 sprintf(buffer, "localhost:%d\n", port);
2919 hi = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
2920 ok(hi != NULL, "InternetOpen failed\n");
2921
2922 hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2923 ok(hc != NULL, "InternetConnect failed\n");
2924
2925 hr = HttpOpenRequestA(hc, "POST", "/test2", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
2926 ok(hr != NULL, "HttpOpenRequest failed\n");
2927
2928 r = HttpSendRequestA(hr, NULL, 0, (char *)"data", sizeof("data"));
2929 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2930
2931 test_status_code(hr, 407);
2932
2933 done:
2934 InternetCloseHandle(hr);
2935 InternetCloseHandle(hc);
2936 InternetCloseHandle(hi);
2937 }
2938
2939 static void test_header_handling_order(int port)
2940 {
2941 static const char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
2942 static const char connection[] = "Connection: Close";
2943 static const char *types[2] = { "*", NULL };
2944 char data[32];
2945 HINTERNET session, connect, request;
2946 DWORD size, status, data_len;
2947 BOOL ret;
2948
2949 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2950 ok(session != NULL, "InternetOpen failed\n");
2951
2952 connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2953 ok(connect != NULL, "InternetConnect failed\n");
2954
2955 request = HttpOpenRequestA(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2956 ok(request != NULL, "HttpOpenRequest failed\n");
2957
2958 ret = HttpAddRequestHeadersA(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
2959 ok(ret, "HttpAddRequestHeaders failed\n");
2960
2961 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
2962 ok(ret, "HttpSendRequest failed\n");
2963
2964 test_status_code(request, 200);
2965 test_request_flags(request, 0);
2966
2967 InternetCloseHandle(request);
2968
2969 request = HttpOpenRequestA(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2970 ok(request != NULL, "HttpOpenRequest failed\n");
2971
2972 ret = HttpSendRequestA(request, connection, ~0u, NULL, 0);
2973 ok(ret, "HttpSendRequest failed\n");
2974
2975 status = 0;
2976 size = sizeof(status);
2977 ret = HttpQueryInfoA( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2978 ok(ret, "HttpQueryInfo failed\n");
2979 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2980
2981 InternetCloseHandle(request);
2982 InternetCloseHandle(connect);
2983
2984 connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2985 ok(connect != NULL, "InternetConnect failed\n");
2986
2987 request = HttpOpenRequestA(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2988 ok(request != NULL, "HttpOpenRequest failed\n");
2989
2990 ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2991 ok(ret, "HttpAddRequestHeaders failed\n");
2992
2993 ret = HttpSendRequestA(request, connection, ~0u, NULL, 0);
2994 ok(ret, "HttpSendRequest failed\n");
2995
2996 status = 0;
2997 size = sizeof(status);
2998 ret = HttpQueryInfoA( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2999 ok(ret, "HttpQueryInfo failed\n");
3000 ok(status == 200, "got status %u, expected 200\n", status);
3001
3002 InternetCloseHandle(request);
3003 InternetCloseHandle(connect);
3004
3005 connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3006 ok(connect != NULL, "InternetConnect failed\n");
3007
3008 request = HttpOpenRequestA(connect, "POST", "/test7b", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
3009 ok(request != NULL, "HttpOpenRequest failed\n");
3010
3011 ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3012 ok(ret, "HttpAddRequestHeaders failed\n");
3013
3014 data_len = sizeof(data);
3015 memset(data, 'a', sizeof(data));
3016 ret = HttpSendRequestA(request, NULL, 0, data, data_len);
3017 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
3018
3019 status = 0;
3020 size = sizeof(status);
3021 ret = HttpQueryInfoA( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
3022 ok(ret, "HttpQueryInfo failed\n");
3023 ok(status == 200, "got status %u, expected 200\n", status);
3024
3025 InternetCloseHandle(request);
3026 InternetCloseHandle(connect);
3027 InternetCloseHandle(session);
3028 }
3029
3030 static void test_connection_header(int port)
3031 {
3032 HINTERNET ses, con, req;
3033 BOOL ret;
3034
3035 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3036 ok(ses != NULL, "InternetOpen failed\n");
3037
3038 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3039 ok(con != NULL, "InternetConnect failed\n");
3040
3041 req = HttpOpenRequestA(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3042 ok(req != NULL, "HttpOpenRequest failed\n");
3043
3044 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3045 ok(ret, "HttpSendRequest failed\n");
3046
3047 test_status_code(req, 200);
3048
3049 InternetCloseHandle(req);
3050
3051 req = HttpOpenRequestA(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
3052 ok(req != NULL, "HttpOpenRequest failed\n");
3053
3054 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3055 ok(ret, "HttpSendRequest failed\n");
3056
3057 test_status_code(req, 200);
3058
3059 InternetCloseHandle(req);
3060
3061 req = HttpOpenRequestA(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
3062 ok(req != NULL, "HttpOpenRequest failed\n");
3063
3064 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3065 ok(ret, "HttpSendRequest failed\n");
3066
3067 test_status_code(req, 200);
3068
3069 InternetCloseHandle(req);
3070
3071 req = HttpOpenRequestA(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
3072 ok(req != NULL, "HttpOpenRequest failed\n");
3073
3074 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3075 ok(ret, "HttpSendRequest failed\n");
3076
3077 test_status_code(req, 200);
3078
3079 InternetCloseHandle(req);
3080 InternetCloseHandle(con);
3081 InternetCloseHandle(ses);
3082 }
3083
3084 static void test_header_override(int port)
3085 {
3086 char buffer[128], host_header_override[30], full_url[128];
3087 HINTERNET ses, con, req;
3088 DWORD size, count, err;
3089 BOOL ret;
3090
3091 sprintf(host_header_override, "Host: test.local:%d\r\n", port);
3092 sprintf(full_url, "http://localhost:%d/test_host_override", port);
3093
3094 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3095 ok(ses != NULL, "InternetOpen failed\n");
3096
3097 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3098 ok(con != NULL, "InternetConnect failed\n");
3099
3100 req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3101 ok(req != NULL, "HttpOpenRequest failed\n");
3102
3103 size = sizeof(buffer) - 1;
3104 count = 0;
3105 memset(buffer, 0, sizeof(buffer));
3106 ret = HttpQueryInfoA(req, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
3107 err = GetLastError();
3108 ok(!ret, "HttpQueryInfo succeeded\n");
3109 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %d\n", err);
3110
3111 size = sizeof(buffer) - 1;
3112 memset(buffer, 0, sizeof(buffer));
3113 ret = InternetQueryOptionA(req, INTERNET_OPTION_URL, buffer, &size);
3114 ok(ret, "InternetQueryOption failed\n");
3115 ok(!strcmp(full_url, buffer), "Expected %s, got %s\n", full_url, buffer);
3116
3117 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3118 ok(ret, "HttpAddRequestHeaders failed\n");
3119
3120 size = sizeof(buffer) - 1;
3121 count = 0;
3122 memset(buffer, 0, sizeof(buffer));
3123 ret = HttpQueryInfoA(req, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
3124 ok(ret, "HttpQueryInfo failed\n");
3125
3126 size = sizeof(buffer) - 1;
3127 memset(buffer, 0, sizeof(buffer));
3128 ret = InternetQueryOptionA(req, INTERNET_OPTION_URL, buffer, &size);
3129 ok(ret, "InternetQueryOption failed\n");
3130 ok(!strcmp(full_url, buffer), "Expected %s, got %s\n", full_url, buffer);
3131
3132 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3133 ok(ret, "HttpSendRequest failed\n");
3134
3135 test_status_code(req, 200);
3136
3137 InternetCloseHandle(req);
3138 req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3139 ok(req != NULL, "HttpOpenRequest failed\n");
3140
3141 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3142 ok(ret, "HttpAddRequestHeaders failed\n");
3143
3144 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3145 ok(ret, "HttpAddRequestHeaders failed\n");
3146
3147 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3148 ok(ret, "HttpSendRequest failed\n");
3149
3150 test_status_code(req, 400);
3151
3152 InternetCloseHandle(req);
3153 req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3154 ok(req != NULL, "HttpOpenRequest failed\n");
3155
3156 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3157 ok(ret, "HttpAddRequestHeaders failed\n");
3158
3159 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3160 ok(ret, "HttpSendRequest failed\n");
3161
3162 test_status_code(req, 200);
3163
3164 InternetCloseHandle(req);
3165 req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3166 ok(req != NULL, "HttpOpenRequest failed\n");
3167
3168 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_REPLACE);
3169 if(ret) { /* win10 returns success */
3170 trace("replacing host header is supported.\n");
3171
3172 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3173 ok(ret, "HttpSendRequest failed\n");
3174
3175 test_status_code(req, 200);
3176 }else {
3177 trace("replacing host header is not supported.\n");
3178
3179 err = GetLastError();
3180 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %d\n", err);
3181
3182 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3183 ok(ret, "HttpSendRequest failed\n");
3184
3185 test_status_code(req, 400);
3186 }
3187
3188 InternetCloseHandle(req);
3189 InternetSetCookieA("http://localhost", "cookie", "biscuit");
3190 req = HttpOpenRequestA(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3191 ok(req != NULL, "HttpOpenRequest failed\n");
3192
3193 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3194 ok(ret, "HttpAddRequestHeaders failed\n");
3195
3196 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3197 ok(ret, "HttpSendRequest failed\n");
3198
3199 test_status_code(req, 200);
3200
3201 InternetCloseHandle(req);
3202 req = HttpOpenRequestA(con, NULL, "/test_cookie_set_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3203 ok(req != NULL, "HttpOpenRequest failed\n");
3204
3205 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3206 ok(ret, "HttpAddRequestHeaders failed\n");
3207
3208 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3209 ok(ret, "HttpSendRequest failed\n");
3210
3211 test_status_code(req, 200);
3212
3213 InternetCloseHandle(req);
3214 req = HttpOpenRequestA(con, NULL, "/test_cookie_check_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3215 ok(req != NULL, "HttpOpenRequest failed\n");
3216
3217 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3218 ok(ret, "HttpAddRequestHeaders failed\n");
3219
3220 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3221 ok(ret, "HttpSendRequest failed\n");
3222
3223 test_status_code(req, 200);
3224
3225 InternetCloseHandle(req);
3226 req = HttpOpenRequestA(con, NULL, "/test_cookie_check_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3227 ok(req != NULL, "HttpOpenRequest failed\n");
3228
3229 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3230 ok(ret, "HttpSendRequest failed\n");
3231
3232 test_status_code(req, 200);
3233
3234 InternetCloseHandle(req);
3235 InternetSetCookieA("http://test.local", "foo", "bar");
3236 req = HttpOpenRequestA(con, NULL, "/test_cookie_check_different_host", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3237 ok(req != NULL, "HttpOpenRequest failed\n");
3238
3239 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3240 ok(ret, "HttpSendRequest failed\n");
3241
3242 test_status_code(req, 200);
3243
3244 InternetCloseHandle(req);
3245 req = HttpOpenRequestA(con, NULL, "/test_cookie_check_different_host", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3246 ok(req != NULL, "HttpOpenRequest failed\n");
3247
3248 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3249 ok(ret, "HttpAddRequestHeaders failed\n");
3250
3251 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3252 ok(ret, "HttpSendRequest failed\n");
3253
3254 test_status_code(req, 200);
3255
3256 InternetCloseHandle(req);
3257 InternetCloseHandle(con);
3258 InternetCloseHandle(ses);
3259
3260 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3261 ok(ses != NULL, "InternetOpenA failed\n");
3262
3263 con = InternetConnectA(ses, "localhost", port, "test1", "pass", INTERNET_SERVICE_HTTP, 0, 0);
3264 ok(con != NULL, "InternetConnectA failed %u\n", GetLastError());
3265
3266 req = HttpOpenRequestA( con, "HEAD", "/test_auth_host1", NULL, NULL, NULL, 0, 0);
3267 ok(req != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
3268
3269 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3270 ok(ret, "HttpSendRequestA failed %u\n", GetLastError());
3271
3272 test_status_code(req, 200);
3273
3274 InternetCloseHandle(req);
3275 InternetCloseHandle(con);
3276 InternetCloseHandle(ses);
3277
3278 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3279 ok(ses != NULL, "InternetOpenA failed\n");
3280
3281 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3282 ok(con != NULL, "InternetConnectA failed %u\n", GetLastError());
3283
3284 req = HttpOpenRequestA(con, "HEAD", "/test_auth_host1", NULL, NULL, NULL, 0, 0);
3285 ok(req != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
3286
3287 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3288 ok(ret, "HttpAddRequestHeaders failed\n");
3289
3290 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
3291 ok( ret, "HttpSendRequestA failed %u\n", GetLastError() );
3292
3293 test_status_code(req, 200);
3294
3295 InternetCloseHandle(req);
3296 InternetCloseHandle(con);
3297 InternetCloseHandle(ses);
3298
3299 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3300 ok(ses != NULL, "InternetOpenA failed\n");
3301
3302 con = InternetConnectA(ses, "localhost", port, "test1", "pass2", INTERNET_SERVICE_HTTP, 0, 0);
3303 ok(con != NULL, "InternetConnectA failed %u\n", GetLastError());
3304
3305 req = HttpOpenRequestA(con, "HEAD", "/test_auth_host2", NULL, NULL, NULL, 0, 0);
3306 ok(req != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
3307
3308 ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3309 ok(ret, "HttpAddRequestHeaders failed\n");
3310
3311 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3312 ok(ret, "HttpSendRequestA failed %u\n", GetLastError());
3313
3314 test_status_code(req, 200);
3315
3316 InternetCloseHandle(req);
3317 InternetCloseHandle(con);
3318 InternetCloseHandle(ses);
3319
3320 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3321 ok(ses != NULL, "InternetOpenA failed\n");
3322
3323 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3324 ok(con != NULL, "InternetConnectA failed %u\n", GetLastError());
3325
3326 req = HttpOpenRequestA(con, "HEAD", "/test_auth_host2", NULL, NULL, NULL, 0, 0);
3327 ok(req != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
3328
3329 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3330 ok(ret, "HttpSendRequestA failed %u\n", GetLastError());
3331
3332 test_status_code(req, 200);
3333
3334 InternetCloseHandle(req);
3335 InternetCloseHandle(con);
3336 InternetCloseHandle(ses);
3337 }
3338
3339 static void test_connection_closing(int port)
3340 {
3341 HINTERNET session, connection, req;
3342 DWORD res;
3343
3344 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
3345
3346 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3347 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3348
3349 pInternetSetStatusCallbackA(session, callback);
3350
3351 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3352 connection = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3353 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
3354 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3355
3356 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3357 req = HttpOpenRequestA(connection, "GET", "/testJ", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0xdeadbeaf);
3358 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
3359 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3360
3361 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3362 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3363 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3364 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3365 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3366 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3367 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3368 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3369 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
3370 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
3371 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3372
3373 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3374 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3375 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3376 WaitForSingleObject(hCompleteEvent, INFINITE);
3377 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3378
3379 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3380 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3381 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3382 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3383 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3384 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3385 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3386 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3387 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3388 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3389 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3390
3391 test_status_code(req, 200);
3392
3393 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3394 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3395 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3396 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3397 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3398 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3399 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3400 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3401 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3402
3403 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3404 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3405 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3406 WaitForSingleObject(hCompleteEvent, INFINITE);
3407 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3408
3409 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3410 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3411 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3412 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3413 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3414 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3415 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3416 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3417 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3418
3419 test_status_code(req, 210);
3420
3421 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3422 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3423 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3424 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3425 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3426 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3427 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3428 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3429 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
3430 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
3431 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3432
3433 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3434 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3435 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3436 WaitForSingleObject(hCompleteEvent, INFINITE);
3437 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3438
3439 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3440 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3441 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3442 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3443 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3444 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3445 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3446 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3447 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3448 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3449 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3450
3451 test_status_code(req, 200);
3452
3453 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
3454 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
3455
3456 close_async_handle(session, hCompleteEvent, 2);
3457 CloseHandle(hCompleteEvent);
3458 }
3459
3460 static void test_successive_HttpSendRequest(int port)
3461 {
3462 HINTERNET session, connection, req;
3463 DWORD res;
3464
3465 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
3466
3467 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3468 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3469
3470 pInternetSetStatusCallbackA(session, callback);
3471
3472 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3473 connection = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3474 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
3475 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3476
3477 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3478 req = HttpOpenRequestA(connection, "GET", "/testH", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0xdeadbeaf);
3479 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
3480 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3481
3482 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3483 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3484 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3485 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3486 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3487 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3488 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3489 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3490 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3491
3492 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3493 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3494 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3495 WaitForSingleObject(hCompleteEvent, INFINITE);
3496 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3497
3498 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3499 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3500 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3501 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3502 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3503 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3504 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3505 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3506 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3507
3508 test_status_code(req, 210);
3509
3510 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3511 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
3512 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3513 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3514 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3515 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3516 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
3517 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
3518 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3519
3520 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
3521 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3522 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3523 WaitForSingleObject(hCompleteEvent, INFINITE);
3524 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3525
3526 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3527 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
3528 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3529 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3530 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3531 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3532 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3533 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3534 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3535
3536 test_status_code(req, 200);
3537
3538 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
3539 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
3540
3541 close_async_handle(session, hCompleteEvent, 2);
3542 CloseHandle(hCompleteEvent);
3543 }
3544
3545 static void test_no_content(int port)
3546 {
3547 HINTERNET session, connection, req;
3548 DWORD res;
3549
3550 trace("Testing 204 no content response...\n");
3551
3552 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
3553
3554 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3555 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3556
3557 pInternetSetStatusCallbackA(session, callback);
3558
3559 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3560 connection = InternetConnectA(session, "localhost", port,
3561 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3562 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
3563 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3564
3565 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3566 req = HttpOpenRequestA(connection, "GET", "/test_no_content", NULL, NULL, NULL,
3567 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3568 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
3569 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3570
3571 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3572 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3573 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3574 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3575 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3576 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3577 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3578 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
3579 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
3580 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3581
3582 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
3583 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3584 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3585 WaitForSingleObject(hCompleteEvent, INFINITE);
3586 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3587
3588 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3589 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3590 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3591 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3592 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3593 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3594 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3595 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3596
3597 close_async_handle(session, hCompleteEvent, 2);
3598 CloseHandle(hCompleteEvent);
3599
3600 /*
3601 * The connection should be closed before closing handle. This is true for most
3602 * wininet versions (including Wine), but some old win2k versions fail to do that.
3603 */
3604 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3605 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3606 }
3607
3608 static void test_conn_close(int port)
3609 {
3610 HINTERNET session, connection, req;
3611 DWORD res, avail, size;
3612 BYTE buf[1024];
3613
3614 trace("Testing connection close connection...\n");
3615
3616 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
3617 conn_close_event = CreateEventW(NULL, FALSE, FALSE, NULL);
3618
3619 session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3620 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3621
3622 pInternetSetStatusCallbackA(session, callback);
3623
3624 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3625 connection = InternetConnectA(session, "localhost", port,
3626 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3627 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
3628 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3629
3630 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
3631 req = HttpOpenRequestA(connection, "GET", "/test_conn_close", NULL, NULL, NULL,
3632 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3633 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
3634 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
3635
3636 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
3637 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
3638 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
3639 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
3640 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
3641 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
3642 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
3643 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3644
3645 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
3646 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3647 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3648 WaitForSingleObject(hCompleteEvent, INFINITE);
3649 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3650
3651 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
3652 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
3653 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
3654 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
3655 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
3656 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3657 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3658 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3659
3660 avail = 0;
3661 res = InternetQueryDataAvailable(req, &avail, 0, 0);
3662 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
3663 ok(avail != 0, "avail = 0\n");
3664
3665 size = 0;
3666 res = InternetReadFile(req, buf, avail, &size);
3667 ok(res, "InternetReadFile failed: %u\n", GetLastError());
3668
3669 /* IE11 calls those in InternetQueryDataAvailable call. */
3670 SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
3671 SET_OPTIONAL(INTERNET_STATUS_RESPONSE_RECEIVED);
3672
3673 res = InternetQueryDataAvailable(req, &avail, 0, 0);
3674 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3675 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3676 ok(!avail, "avail = %u, expected 0\n", avail);
3677
3678 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
3679
3680 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
3681 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
3682 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
3683 SetEvent(conn_close_event);
3684 #ifdef ROSTESTS_73_FIXED
3685 WaitForSingleObject(hCompleteEvent, INFINITE);
3686 #else /* ROSTESTS_73_FIXED */
3687 ok(WaitForSingleObject(hCompleteEvent, 5000) == WAIT_OBJECT_0, "Wait timed out\n");
3688 #endif /* ROSTESTS_73_FIXED */
3689 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
3690 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
3691 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
3692 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
3693 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
3694
3695 close_async_handle(session, hCompleteEvent, 2);
3696 CloseHandle(hCompleteEvent);
3697 }
3698
3699 static void test_no_cache(int port)
3700 {
3701 static const char cache_control_no_cache[] = "/test_cache_control_no_cache";
3702 static const char cache_control_no_store[] = "/test_cache_control_no_store";
3703 static const char cache_url_fmt[] = "http://localhost:%d%s";
3704
3705 char cache_url[256], buf[256];
3706 HINTERNET ses, con, req;
3707 DWORD read, size;
3708 BOOL ret;
3709
3710 trace("Testing no-cache header\n");
3711
3712 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3713 ok(ses != NULL,"InternetOpen failed with error %u\n", GetLastError());
3714
3715 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3716 ok(con != NULL, "InternetConnect failed with error %u\n", GetLastError());
3717
3718 req = HttpOpenRequestA(con, NULL, cache_control_no_cache, NULL, NULL, NULL, 0, 0);
3719 ok(req != NULL, "HttpOpenRequest failed\n");
3720
3721 sprintf(cache_url, cache_url_fmt, port, cache_control_no_cache);
3722 DeleteUrlCacheEntryA(cache_url);
3723
3724 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3725 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3726 size = 0;
3727 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3728 size += read;
3729 ok(size == 12, "read %d bytes of data\n", size);
3730 InternetCloseHandle(req);
3731
3732 req = HttpOpenRequestA(con, NULL, cache_control_no_cache, NULL, NULL, NULL, 0, 0);
3733 ok(req != NULL, "HttpOpenRequest failed\n");
3734
3735 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3736 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3737 size = 0;
3738 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3739 size += read;
3740 ok(size == 0, "read %d bytes of data\n", size);
3741 InternetCloseHandle(req);
3742 DeleteUrlCacheEntryA(cache_url);
3743
3744 req = HttpOpenRequestA(con, NULL, cache_control_no_store, NULL, NULL, NULL, 0, 0);
3745 ok(req != NULL, "HttpOpenRequest failed\n");
3746
3747 sprintf(cache_url, cache_url_fmt, port, cache_control_no_store);
3748 DeleteUrlCacheEntryA(cache_url);
3749
3750 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3751 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3752 size = 0;
3753 while(InternetReadFile(req, buf, sizeof(buf), &read) && read)
3754 size += read;
3755 ok(size == 12, "read %d bytes of data\n", size);
3756 InternetCloseHandle(req);
3757
3758 ret = DeleteUrlCacheEntryA(cache_url);
3759 ok(!ret && GetLastError()==ERROR_FILE_NOT_FOUND, "cache entry should not exist\n");
3760
3761 InternetCloseHandle(con);
3762 InternetCloseHandle(ses);
3763 }
3764
3765 static void test_cache_read_gzipped(int port)
3766 {
3767 static const char cache_url_fmt[] = "http://localhost:%d%s";
3768 static const char get_gzip[] = "/test_cache_gzip";
3769 static const char content[] = "gzip test\n";
3770 static const char text_html[] = "text/html";
3771 static const char raw_header[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
3772
3773 HINTERNET ses, con, req;
3774 DWORD read, size;
3775 char cache_url[256], buf[256];
3776 BOOL ret;
3777
3778 trace("Testing reading compressed content from cache\n");
3779
3780 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3781 ok(ses != NULL,"InternetOpen failed with error %u\n", GetLastError());
3782
3783 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3784 ok(con != NULL, "InternetConnect failed with error %u\n", GetLastError());
3785
3786 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3787 ok(req != NULL, "HttpOpenRequest failed\n");
3788
3789 ret = TRUE;
3790 ret = InternetSetOptionA(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3791 if(!ret && GetLastError()==ERROR_INTERNET_INVALID_OPTION) {
3792 win_skip("INTERNET_OPTION_HTTP_DECODING not supported\n");
3793 InternetCloseHandle(req);
3794 InternetCloseHandle(con);
3795 InternetCloseHandle(ses);
3796 return;
3797 }
3798 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3799
3800 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3801 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3802 size = 0;
3803 while(InternetReadFile(req, buf+size, sizeof(buf)-size, &read) && read)
3804 size += read;
3805 ok(size == 10, "read %d bytes of data\n", size);
3806 buf[size] = 0;
3807 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3808
3809 size = sizeof(buf)-1;
3810 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_TYPE, buf, &size, 0);
3811 ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3812 buf[size] = 0;
3813 ok(!strncmp(text_html, buf, size), "buf = %s\n", buf);
3814
3815 size = sizeof(buf)-1;
3816 ret = HttpQueryInfoA(req, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &size, 0);
3817 ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3818 buf[size] = 0;
3819 ok(!strncmp(raw_header, buf, size), "buf = %s\n", buf);
3820 InternetCloseHandle(req);
3821
3822 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
3823 ok(req != NULL, "HttpOpenRequest failed\n");
3824
3825 ret = TRUE;
3826 ret = InternetSetOptionA(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3827 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3828
3829 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3830 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3831 size = 0;
3832 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3833 size += read;
3834 todo_wine ok(size == 10, "read %d bytes of data\n", size);
3835 buf[size] = 0;
3836 ok(!strncmp(buf, content, size), "incorrect page content: %s\n", buf);
3837
3838 size = sizeof(buf);
3839 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
3840 ok(!ret && GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND,
3841 "HttpQueryInfo(HTTP_QUERY_CONTENT_ENCODING) returned %d, %d\n",
3842 ret, GetLastError());
3843
3844 size = sizeof(buf)-1;
3845 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_TYPE, buf, &size, 0);
3846 todo_wine ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) failed: %d\n", GetLastError());
3847 buf[size] = 0;
3848 todo_wine ok(!strncmp(text_html, buf, size), "buf = %s\n", buf);
3849 InternetCloseHandle(req);
3850
3851 /* Decompression doesn't work while reading from cache */
3852 test_cache_gzip = 0;
3853 sprintf(cache_url, cache_url_fmt, port, get_gzip);
3854 DeleteUrlCacheEntryA(cache_url);
3855
3856 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, 0, 0);
3857 ok(req != NULL, "HttpOpenRequest failed\n");
3858
3859 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3860 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3861 size = 0;
3862 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3863 size += read;
3864 ok(size == 31, "read %d bytes of data\n", size);
3865 InternetCloseHandle(req);
3866
3867 req = HttpOpenRequestA(con, NULL, get_gzip, NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
3868 ok(req != NULL, "HttpOpenRequest failed\n");
3869
3870 ret = TRUE;
3871 ret = InternetSetOptionA(req, INTERNET_OPTION_HTTP_DECODING, &ret, sizeof(ret));
3872 ok(ret, "InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %d\n", GetLastError());
3873
3874 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", -1, NULL, 0);
3875 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
3876 size = 0;
3877 while(InternetReadFile(req, buf+size, sizeof(buf)-1-size, &read) && read)
3878 size += read;
3879 todo_wine ok(size == 31, "read %d bytes of data\n", size);
3880
3881 size = sizeof(buf);
3882 ret = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
3883 todo_wine ok(ret, "HttpQueryInfo(HTTP_QUERY_CONTENT_ENCODING) failed: %d\n", GetLastError());
3884 InternetCloseHandle(req);
3885
3886 InternetCloseHandle(con);
3887 InternetCloseHandle(ses);
3888
3889 DeleteUrlCacheEntryA(cache_url);
3890 }
3891
3892 static void test_HttpSendRequestW(int port)
3893 {
3894 static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
3895 HINTERNET ses, con, req;
3896 DWORD error;
3897 BOOL ret;
3898
3899 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
3900 ok(ses != NULL, "InternetOpen failed\n");
3901
3902 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3903 ok(con != NULL, "InternetConnect failed\n");
3904
3905 req = HttpOpenRequestA(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
3906 ok(req != NULL, "HttpOpenRequest failed\n");
3907
3908 SetLastError(0xdeadbeef);
3909 ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
3910 error = GetLastError();
3911 ok(!ret, "HttpSendRequestW succeeded\n");
3912 ok(error == ERROR_IO_PENDING ||
3913 broken(error == ERROR_HTTP_HEADER_NOT_FOUND) || /* IE6 */
3914 broken(error == ERROR_INVALID_PARAMETER), /* IE5 */
3915 "got %u expected ERROR_IO_PENDING\n", error);
3916
3917 InternetCloseHandle(req);
3918 InternetCloseHandle(con);
3919 InternetCloseHandle(ses);
3920 }
3921
3922 static void test_cookie_header(int port)
3923 {
3924 HINTERNET ses, con, req;
3925 DWORD size, error;
3926 BOOL ret;
3927 char buffer[256];
3928
3929 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3930 ok(ses != NULL, "InternetOpen failed\n");
3931
3932 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3933 ok(con != NULL, "InternetConnect failed\n");
3934
3935 InternetSetCookieA("http://localhost", "cookie", "biscuit");
3936
3937 req = HttpOpenRequestA(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3938 ok(req != NULL, "HttpOpenRequest failed\n");
3939
3940 buffer[0] = 0;
3941 size = sizeof(buffer);
3942 SetLastError(0xdeadbeef);
3943 ret = HttpQueryInfoA(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3944 error = GetLastError();
3945 ok(!ret, "HttpQueryInfo succeeded\n");
3946 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
3947
3948 ret = HttpAddRequestHeadersA(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
3949 ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
3950
3951 buffer[0] = 0;
3952 size = sizeof(buffer);
3953 ret = HttpQueryInfoA(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3954 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
3955 ok(!!strstr(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
3956
3957 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3958 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
3959
3960 test_status_code(req, 200);
3961
3962 buffer[0] = 0;
3963 size = sizeof(buffer);
3964 ret = HttpQueryInfoA(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3965 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
3966 ok(!strstr(buffer, "cookie=not biscuit"), "'%s' should not contain \'cookie=not biscuit\'\n", buffer);
3967 ok(!!strstr(buffer, "cookie=biscuit"), "'%s' should contain \'cookie=biscuit\'\n", buffer);
3968
3969 InternetCloseHandle(req);
3970
3971 InternetSetCookieA("http://localhost/testCCCC", "subcookie", "data");
3972
3973 req = HttpOpenRequestA(con, NULL, "/test_cookie_path1", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3974 ok(req != NULL, "HttpOpenRequest failed\n");
3975
3976 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3977 ok(ret, "HttpSendRequest failed\n");
3978
3979 test_status_code(req, 200);
3980 InternetCloseHandle(req);
3981
3982 req = HttpOpenRequestA(con, NULL, "/test_cookie_path1/abc", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3983 ok(req != NULL, "HttpOpenRequest failed\n");
3984
3985 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3986 ok(ret, "HttpSendRequest failed\n");
3987
3988 test_status_code(req, 200);
3989 InternetCloseHandle(req);
3990
3991 req = HttpOpenRequestA(con, NULL, "/test_cookie_set_path", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3992 ok(req != NULL, "HttpOpenRequest failed\n");
3993
3994 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3995 ok(ret, "HttpSendRequest failed\n");
3996
3997 test_status_code(req, 200);
3998 InternetCloseHandle(req);
3999
4000 req = HttpOpenRequestA(con, NULL, "/test_cookie_path2", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
4001 ok(req != NULL, "HttpOpenRequest failed\n");
4002
4003 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
4004 ok(ret, "HttpSendRequest failed\n");
4005
4006 test_status_code(req, 400);
4007 InternetCloseHandle(req);
4008
4009 req = HttpOpenRequestA(con, NULL, "/test_cookie_merge", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
4010 ok(req != NULL, "HttpOpenRequest failed\n");
4011
4012 ret = HttpAddRequestHeadersA(req, "Cookie: manual_cookie=test\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
4013 ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
4014
4015 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
4016 ok(ret, "HttpSendRequest failed\n");
4017
4018 test_status_code(req, 200);
4019 InternetCloseHandle(req);
4020
4021 InternetCloseHandle(con);
4022 InternetCloseHandle(ses);
4023 }
4024
4025 static void test_basic_authentication(int port)
4026 {
4027 HINTERNET session, connect, request;
4028 BOOL ret;
4029
4030 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4031 ok(session != NULL, "InternetOpen failed\n");
4032
4033 connect = InternetConnectA(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
4034 ok(connect != NULL, "InternetConnect failed\n");
4035
4036 request = HttpOpenRequestA(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
4037 ok(request != NULL, "HttpOpenRequest failed\n");
4038
4039 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4040 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
4041
4042 test_status_code(request, 200);
4043 test_request_flags(request, 0);
4044
4045 InternetCloseHandle(request);
4046 InternetCloseHandle(connect);
4047 InternetCloseHandle(session);
4048 }
4049
4050 static void test_premature_disconnect(int port)
4051 {
4052 test_request_t req;
4053 DWORD err;
4054 BOOL ret;
4055
4056 open_simple_request(&req, "localhost", port, NULL, "/premature_disconnect");
4057
4058 SetLastError(0xdeadbeef);
4059 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4060 err = GetLastError();
4061 todo_wine ok(!ret, "HttpSendRequest succeeded\n");
4062 todo_wine ok(err == ERROR_HTTP_INVALID_SERVER_RESPONSE, "got %u\n", err);
4063
4064 close_request(&req);
4065 }
4066
4067 static void test_invalid_response_headers(int port)
4068 {
4069 test_request_t req;
4070 DWORD size;
4071 BOOL ret;
4072 char buffer[256];
4073
4074 open_simple_request(&req, "localhost", port, NULL, "/testE");
4075
4076 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4077 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
4078
4079 test_status_code(req.request, 401);
4080 test_request_flags(req.request, 0);
4081
4082 buffer[0] = 0;
4083 size = sizeof(buffer);
4084 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
4085 ok(ret, "HttpQueryInfo failed\n");
4086 ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
4087 "headers wrong \"%s\"\n", buffer);
4088
4089 buffer[0] = 0;
4090 size = sizeof(buffer);
4091 ret = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &size, NULL);
4092 ok(ret, "HttpQueryInfo failed\n");
4093 ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
4094
4095 close_request(&req);
4096 }
4097
4098 static void test_response_without_headers(int port)
4099 {
4100 test_request_t req;
4101 DWORD r, count, size;
4102 char buffer[1024];
4103
4104 open_simple_request(&req, "localhost", port, NULL, "/testG");
4105
4106 test_request_flags(req.request, INTERNET_REQFLAG_NO_HEADERS);
4107
4108 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4109 ok(r, "HttpSendRequest failed %u\n", GetLastError());
4110
4111 test_request_flags_todo(req.request, INTERNET_REQFLAG_NO_HEADERS);
4112
4113 count = 0;
4114 memset(buffer, 0, sizeof buffer);
4115 r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
4116 ok(r, "InternetReadFile failed %u\n", GetLastError());
4117 todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
4118 todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
4119
4120 test_status_code(req.request, 200);
4121 test_request_flags_todo(req.request, INTERNET_REQFLAG_NO_HEADERS);
4122
4123 buffer[0] = 0;
4124 size = sizeof(buffer);
4125 r = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
4126 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
4127 ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
4128
4129 buffer[0] = 0;
4130 size = sizeof(buffer);
4131 r = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &size, NULL);
4132 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
4133 ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
4134
4135 buffer[0] = 0;
4136 size = sizeof(buffer);
4137 r = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
4138 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
4139 ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
4140
4141 close_request(&req);
4142 }
4143
4144 static void test_head_request(int port)
4145 {
4146 DWORD len, content_length;
4147 test_request_t req;
4148 BYTE buf[100];
4149 BOOL ret;
4150
4151 open_simple_request(&req, "localhost", port, "HEAD", "/test_head");
4152
4153 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4154 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
4155
4156 len = sizeof(content_length);
4157 content_length = -1;
4158 ret = HttpQueryInfoA(req.request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH, &content_length, &len, 0);
4159 ok(ret, "HttpQueryInfo dailed: %u\n", GetLastError());
4160 ok(len == sizeof(DWORD), "len = %u\n", len);
4161 ok(content_length == 100, "content_length = %u\n", content_length);
4162
4163 len = -1;
4164 ret = InternetReadFile(req.request, buf, sizeof(buf), &len);
4165 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
4166
4167 len = -1;
4168 ret = InternetReadFile(req.request, buf, sizeof(buf), &len);
4169 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
4170
4171 close_request(&req);
4172 }
4173
4174 static void test_HttpQueryInfo(int port)
4175 {
4176 test_request_t req;
4177 DWORD size, index, error;
4178 char buffer[1024];
4179 BOOL ret;
4180
4181 open_simple_request(&req, "localhost", port, NULL, "/testD");
4182
4183 size = sizeof(buffer);
4184 ret = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
4185 error = GetLastError();
4186 ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
4187 if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
4188
4189 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4190 ok(ret, "HttpSendRequest failed\n");
4191
4192 index = 0;
4193 size = sizeof(buffer);
4194 ret = HttpQueryInfoA(req.request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
4195 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4196 ok(index == 1, "expected 1 got %u\n", index);
4197
4198 index = 0;
4199 size = sizeof(buffer);
4200 ret = HttpQueryInfoA(req.request, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
4201 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4202 ok(index == 1, "expected 1 got %u\n", index);
4203
4204 index = 0;
4205 size = sizeof(buffer);
4206 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
4207 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4208 ok(index == 0, "expected 0 got %u\n", index);
4209
4210 size = sizeof(buffer);
4211 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
4212 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4213 ok(index == 0, "expected 0 got %u\n", index);
4214
4215 index = 0xdeadbeef; /* invalid start index */
4216 size = sizeof(buffer);
4217 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
4218 todo_wine ok(!ret, "HttpQueryInfo should have failed\n");
4219 todo_wine ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
4220 "Expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
4221
4222 index = 0;
4223 size = sizeof(buffer);
4224 ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
4225 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4226 ok(index == 0, "expected 0 got %u\n", index);
4227
4228 size = sizeof(buffer);
4229 ret = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
4230 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4231 ok(index == 0, "expected 0 got %u\n", index);
4232
4233 size = sizeof(buffer);
4234 ret = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &size, &index);
4235 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4236 ok(index == 0, "expected 0 got %u\n", index);
4237
4238 test_status_code(req.request, 200);
4239
4240 index = 0xdeadbeef;
4241 size = sizeof(buffer);
4242 ret = HttpQueryInfoA(req.request, HTTP_QUERY_FORWARDED, buffer, &size, &index);
4243 ok(!ret, "HttpQueryInfo succeeded\n");
4244 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
4245
4246 index = 0;
4247 size = sizeof(buffer);
4248 ret = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &size, &index);
4249 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4250 ok(index == 1, "expected 1 got %u\n", index);
4251
4252 index = 0;
4253 size = sizeof(buffer);
4254 strcpy(buffer, "Server");
4255 ret = HttpQueryInfoA(req.request, HTTP_QUERY_CUSTOM, buffer, &size, &index);
4256 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4257 ok(index == 1, "expected 1 got %u\n", index);
4258
4259 index = 0;
4260 size = sizeof(buffer);
4261 ret = HttpQueryInfoA(req.request, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
4262 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4263 ok(index == 1, "expected 1 got %u\n", index);
4264
4265 size = sizeof(buffer);
4266 ret = HttpQueryInfoA(req.request, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
4267 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
4268 ok(index == 2, "expected 2 got %u\n", index);
4269
4270 close_request(&req);
4271 }
4272
4273 static void test_options(int port)
4274 {
4275 INTERNET_DIAGNOSTIC_SOCKET_INFO idsi;
4276 HINTERNET ses, con, req;
4277 DWORD size, error;
4278 DWORD_PTR ctx;
4279 BOOL ret;
4280
4281 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4282 ok(ses != NULL, "InternetOpen failed\n");
4283
4284 SetLastError(0xdeadbeef);
4285 ret = InternetSetOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, 0);
4286 error = GetLastError();
4287 ok(!ret, "InternetSetOption succeeded\n");
4288 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
4289
4290 SetLastError(0xdeadbeef);
4291 ret = InternetSetOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, sizeof(ctx));
4292 ok(!ret, "InternetSetOption succeeded\n");
4293 error = GetLastError();
4294 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
4295
4296 SetLastError(0xdeadbeef);
4297 ret = InternetSetOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, 0);
4298 ok(!ret, "InternetSetOption succeeded\n");
4299 error = GetLastError();
4300 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
4301
4302 ctx = 1;
4303 ret = InternetSetOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
4304 ok(ret, "InternetSetOption failed %u\n", GetLastError());
4305
4306 SetLastError(0xdeadbeef);
4307 ret = InternetQueryOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, NULL);
4308 error = GetLastError();
4309 ok(!ret, "InternetQueryOption succeeded\n");
4310 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
4311
4312 SetLastError(0xdeadbeef);
4313 ret = InternetQueryOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, NULL);
4314 error = GetLastError();
4315 ok(!ret, "InternetQueryOption succeeded\n");
4316 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
4317
4318 size = 0;
4319 SetLastError(0xdeadbeef);
4320 ret = InternetQueryOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, &size);
4321 error = GetLastError();
4322 ok(!ret, "InternetQueryOption succeeded\n");
4323 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
4324
4325 size = sizeof(ctx);
4326 SetLastError(0xdeadbeef);
4327 ret = InternetQueryOptionA(NULL, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4328 error = GetLastError();
4329 ok(!ret, "InternetQueryOption succeeded\n");
4330 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %u\n", error);
4331
4332 ctx = 0xdeadbeef;
4333 size = sizeof(ctx);
4334 ret = InternetQueryOptionA(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4335 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
4336 ok(ctx == 1, "expected 1 got %lu\n", ctx);
4337
4338 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4339 ok(con != NULL, "InternetConnect failed\n");
4340
4341 ctx = 0xdeadbeef;
4342 size = sizeof(ctx);
4343 ret = InternetQueryOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4344 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
4345 ok(ctx == 0, "expected 0 got %lu\n", ctx);
4346
4347 ctx = 2;
4348 ret = InternetSetOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
4349 ok(ret, "InternetSetOption failed %u\n", GetLastError());
4350
4351 ctx = 0xdeadbeef;
4352 size = sizeof(ctx);
4353 ret = InternetQueryOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4354 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
4355 ok(ctx == 2, "expected 2 got %lu\n", ctx);
4356
4357 req = HttpOpenRequestA(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
4358 ok(req != NULL, "HttpOpenRequest failed\n");
4359
4360 ctx = 0xdeadbeef;
4361 size = sizeof(ctx);
4362 ret = InternetQueryOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4363 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
4364 ok(ctx == 0, "expected 0 got %lu\n", ctx);
4365
4366 ctx = 3;
4367 ret = InternetSetOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
4368 ok(ret, "InternetSetOption failed %u\n", GetLastError());
4369
4370 ctx = 0xdeadbeef;
4371 size = sizeof(ctx);
4372 ret = InternetQueryOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
4373 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
4374 ok(ctx == 3, "expected 3 got %lu\n", ctx);
4375
4376 size = sizeof(idsi);
4377 ret = InternetQueryOptionA(req, INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, &idsi, &size);
4378 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
4379
4380 size = 0;
4381 SetLastError(0xdeadbeef);
4382 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
4383 error = GetLastError();
4384 ok(!ret, "InternetQueryOption succeeded\n");
4385 ok(error == ERROR_INTERNET_INVALID_OPERATION, "expected ERROR_INTERNET_INVALID_OPERATION, got %u\n", error);
4386
4387 /* INTERNET_OPTION_PROXY */
4388 SetLastError(0xdeadbeef);
4389 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, NULL);
4390 error = GetLastError();
4391 ok(!ret, "InternetQueryOption succeeded\n");
4392 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
4393
4394 SetLastError(0xdeadbeef);
4395 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, &ctx, NULL);
4396 error = GetLastError();
4397 ok(!ret, "InternetQueryOption succeeded\n");
4398 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
4399
4400 size = 0;
4401 SetLastError(0xdeadbeef);
4402 ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, &size);
4403 error = GetLastError();
4404 ok(!ret, "InternetQueryOption succeeded\n");
4405 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
4406 ok(size >= sizeof(INTERNET_PROXY_INFOA), "expected size to be greater or equal to the struct size\n");
4407
4408 InternetCloseHandle(req);
4409 InternetCloseHandle(con);
4410 InternetCloseHandle(ses);
4411 }
4412
4413 typedef struct {
4414 const char *response_text;
4415 int status_code;
4416 const char *status_text;
4417 const char *raw_headers;
4418 } http_status_test_t;
4419
4420 static const http_status_test_t http_status_tests[] = {
4421 {
4422 "HTTP/1.1 200 OK\r\n"
4423 "Content-Length: 1\r\n"
4424 "\r\nx",
4425 200,
4426 "OK"
4427 },
4428 {
4429 "HTTP/1.1 404 Fail\r\n"
4430 "Content-Length: 1\r\n"
4431 "\r\nx",
4432 404,
4433 "Fail"
4434 },
4435 {
4436 "HTTP/1.1 200\r\n"
4437 "Content-Length: 1\r\n"
4438 "\r\nx",
4439 200,
4440 ""
4441 },
4442 {
4443 "HTTP/1.1 410 \r\n"
4444 "Content-Length: 1\r\n"
4445 "\r\nx",
4446 410,
4447 ""
4448 }
4449 };
4450
4451 static void test_http_status(int port)
4452 {
4453 test_request_t req;
4454 char buf[1000];
4455 DWORD i, size;
4456 BOOL res;
4457
4458 for(i=0; i < sizeof(http_status_tests)/sizeof(*http_status_tests); i++) {
4459 send_buffer = http_status_tests[i].response_text;
4460
4461 open_simple_request(&req, "localhost", port, NULL, "/send_from_buffer");
4462
4463 res = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4464 ok(res, "HttpSendRequest failed\n");
4465
4466 test_status_code(req.request, http_status_tests[i].status_code);
4467
4468 size = sizeof(buf);
4469 res = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buf, &size, NULL);
4470 ok(res, "HttpQueryInfo failed: %u\n", GetLastError());
4471 ok(!strcmp(buf, http_status_tests[i].status_text), "[%u] Unexpected status text \"%s\", expected \"%s\"\n",
4472 i, buf, http_status_tests[i].status_text);
4473
4474 close_request(&req);
4475 }
4476 }
4477
4478 static void test_cache_control_verb(int port)
4479 {
4480 HINTERNET session, connect, request;
4481 BOOL ret;
4482
4483 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4484 ok(session != NULL, "InternetOpen failed\n");
4485
4486 connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4487 ok(connect != NULL, "InternetConnect failed\n");
4488
4489 request = HttpOpenRequestA(connect, "RPC_OUT_DATA", "/test_cache_control_verb", NULL, NULL, NULL,
4490 INTERNET_FLAG_NO_CACHE_WRITE, 0);
4491 ok(request != NULL, "HttpOpenRequest failed\n");
4492 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4493 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
4494 test_status_code(request, 200);
4495 InternetCloseHandle(request);
4496
4497 request = HttpOpenRequestA(connect, "POST", "/test_cache_control_verb", NULL, NULL, NULL,
4498 INTERNET_FLAG_NO_CACHE_WRITE, 0);
4499 ok(request != NULL, "HttpOpenRequest failed\n");
4500 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4501 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
4502 test_status_code(request, 200);
4503 InternetCloseHandle(request);
4504
4505 request = HttpOpenRequestA(connect, "HEAD", "/test_cache_control_verb", NULL, NULL, NULL,
4506 INTERNET_FLAG_NO_CACHE_WRITE, 0);
4507 ok(request != NULL, "HttpOpenRequest failed\n");
4508 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4509 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
4510 test_status_code(request, 200);
4511 InternetCloseHandle(request);
4512
4513 request = HttpOpenRequestA(connect, "GET", "/test_cache_control_verb", NULL, NULL, NULL,
4514 INTERNET_FLAG_NO_CACHE_WRITE, 0);
4515 ok(request != NULL, "HttpOpenRequest failed\n");
4516 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
4517 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
4518 test_status_code(request, 200);
4519 InternetCloseHandle(request);
4520
4521 InternetCloseHandle(connect);
4522 InternetCloseHandle(session);
4523 }
4524
4525 static void test_request_content_length(int port)
4526 {
4527 char data[] = {'t','e','s','t'};
4528 test_request_t req;
4529 BOOL ret;
4530
4531 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
4532
4533 open_simple_request(&req, "localhost", port, "POST", "/test_request_content_length");
4534
4535 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
4536 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
4537 test_status_code(req.request, 200);
4538
4539 SetEvent(hCompleteEvent);
4540
4541 ret = HttpSendRequestA(req.request, NULL, 0, data, sizeof(data));
4542 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
4543 test_status_code(req.request, 200);
4544
4545 SetEvent(hCompleteEvent);
4546
4547 close_request(&req);
4548 CloseHandle(hCompleteEvent);
4549 }
4550
4551 static void test_accept_encoding(int port)
4552 {
4553 HINTERNET ses, con, req;
4554 char buf[1000];
4555 BOOL ret;
4556
4557 ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
4558 ok(ses != NULL, "InternetOpen failed\n");
4559
4560 con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
4561 ok(con != NULL, "InternetConnect failed\n");
4562
4563 req = HttpOpenRequestA(con, "GET", "/echo_request", "HTTP/1.0", NULL, NULL, 0, 0);
4564 ok(req != NULL, "HttpOpenRequest failed\n");
4565
4566 ret = HttpAddRequestHeadersA(req, "Accept-Encoding: gzip\r\n", ~0u, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
4567 ok(ret, "HttpAddRequestHeaders failed\n");
4568
4569 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
4570 ok(ret, "HttpSendRequestA failed\n");
4571
4572 test_status_code(req, 200);
4573 receive_simple_request(req, buf, sizeof(buf));
4574 ok(strstr(buf, "Accept-Encoding: gzip") != NULL, "Accept-Encoding header not found in %s\n", buf);
4575
4576 InternetCloseHandle(req);
4577
4578 req = HttpOpenRequestA(con, "GET", "/echo_request", "HTTP/1.0", NULL, NULL, 0, 0);
4579 ok(req != NULL, "HttpOpenRequest failed\n");
4580
4581 ret = HttpSendRequestA(req, "Accept-Encoding: gzip", ~0u, NULL, 0);
4582 ok(ret, "HttpSendRequestA failed\n");
4583
4584 test_status_code(req, 200);
4585 receive_simple_request(req, buf, sizeof(buf));
4586 ok(strstr(buf, "Accept-Encoding: gzip") != NULL, "Accept-Encoding header not found in %s\n", buf);
4587
4588 InternetCloseHandle(req);
4589 InternetCloseHandle(con);
4590 InternetCloseHandle(ses);
4591 }
4592
4593 static void test_basic_auth_credentials_reuse(int port)
4594 {
4595 HINTERNET ses, con, req;
4596 DWORD status, size;
4597 BOOL ret;
4598
4599 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4600 ok( ses != NULL, "InternetOpenA failed\n" );
4601
4602 con = InternetConnectA( ses, "localhost", port, "user", "pwd",
4603 INTERNET_SERVICE_HTTP, 0, 0 );
4604 ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
4605
4606 req = HttpOpenRequestA( con, "HEAD", "/upload.txt", NULL, NULL, NULL, 0, 0 );
4607 ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
4608
4609 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4610 ok( ret, "HttpSendRequestA failed %u\n", GetLastError() );
4611
4612 status = 0xdeadbeef;
4613 size = sizeof(status);
4614 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4615 ok( ret, "HttpQueryInfoA failed %u\n", GetLastError() );
4616 ok( status == 200, "got %u\n", status );
4617
4618 InternetCloseHandle( req );
4619 InternetCloseHandle( con );
4620 InternetCloseHandle( ses );
4621
4622 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
4623 ok( ses != NULL, "InternetOpenA failed\n" );
4624
4625 con = InternetConnectA( ses, "localhost", port, NULL, NULL,
4626 INTERNET_SERVICE_HTTP, 0, 0 );
4627 ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
4628
4629 req = HttpOpenRequestA( con, "PUT", "/upload2.txt", NULL, NULL, NULL, 0, 0 );
4630 ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
4631
4632 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4633 ok( ret, "HttpSendRequestA failed %u\n", GetLastError() );
4634
4635 status = 0xdeadbeef;
4636 size = sizeof(status);
4637 ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
4638 ok( ret, "HttpQueryInfoA failed %u\n", GetLastError() );
4639 ok( status == 200, "got %u\n", status );
4640
4641 InternetCloseHandle( req );
4642 InternetCloseHandle( con );
4643 InternetCloseHandle( ses );
4644 }
4645
4646 static void test_async_read(int port)
4647 {
4648 HINTERNET ses, con, req;
4649 INTERNET_BUFFERSA ib;
4650 char buffer[0x100];
4651 DWORD pending_reads;
4652 DWORD res, count, bytes;
4653 BOOL ret;
4654
4655 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
4656 conn_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
4657
4658 /* test asynchronous InternetReadFileEx */
4659 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC );
4660 ok( ses != NULL, "InternetOpenA failed\n" );
4661 pInternetSetStatusCallbackA( ses, &callback );
4662
4663 SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
4664 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef );
4665 ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
4666 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
4667
4668 SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
4669 req = HttpOpenRequestA( con, "GET", "/async_read", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef );
4670 ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
4671 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
4672
4673 SET_OPTIONAL( INTERNET_STATUS_COOKIE_SENT );
4674 SET_OPTIONAL( INTERNET_STATUS_DETECTING_PROXY );
4675 SET_EXPECT( INTERNET_STATUS_CONNECTING_TO_SERVER );
4676 SET_EXPECT( INTERNET_STATUS_CONNECTED_TO_SERVER );
4677 SET_EXPECT( INTERNET_STATUS_SENDING_REQUEST );
4678 SET_EXPECT( INTERNET_STATUS_REQUEST_SENT );
4679 SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
4680 SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
4681 SET_OPTIONAL( INTERNET_STATUS_CLOSING_CONNECTION );
4682 SET_OPTIONAL( INTERNET_STATUS_CONNECTION_CLOSED );
4683 SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
4684
4685 SetLastError( 0xdeadbeef );
4686 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4687 ok( !ret, "HttpSendRequestA unexpectedly succeeded\n" );
4688 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
4689 WaitForSingleObject( hCompleteEvent, INFINITE );
4690 ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error );
4691
4692 CLEAR_NOTIFIED( INTERNET_STATUS_COOKIE_SENT );
4693 CLEAR_NOTIFIED( INTERNET_STATUS_DETECTING_PROXY );
4694 CHECK_NOTIFIED( INTERNET_STATUS_CONNECTING_TO_SERVER );
4695 CHECK_NOTIFIED( INTERNET_STATUS_CONNECTED_TO_SERVER );
4696 CHECK_NOTIFIED( INTERNET_STATUS_SENDING_REQUEST );
4697 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_SENT );
4698 CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
4699 CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
4700 CLEAR_NOTIFIED( INTERNET_STATUS_CLOSING_CONNECTION );
4701 CLEAR_NOTIFIED( INTERNET_STATUS_CONNECTION_CLOSED );
4702 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
4703
4704 pending_reads = 0;
4705 memset( &ib, 0, sizeof(ib) );
4706 memset( buffer, 0, sizeof(buffer) );
4707 ib.dwStructSize = sizeof(ib);
4708 for (count = 0; count < sizeof(buffer); count += ib.dwBufferLength)
4709 {
4710 ib.lpvBuffer = buffer + count;
4711 ib.dwBufferLength = min(16, sizeof(buffer) - count);
4712
4713 SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
4714 SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
4715
4716 ret = InternetReadFileExA( req, &ib, 0, 0xdeadbeef );
4717 if (!count) /* the first part should arrive immediately */
4718 ok( ret, "InternetReadFileExA failed %u\n", GetLastError() );
4719 if (!ret)
4720 {
4721 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
4722 CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
4723 SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
4724 if (!pending_reads++)
4725 {
4726 res = WaitForSingleObject( hCompleteEvent, 0 );
4727 ok( res == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %u\n", res );
4728 SetEvent( conn_wait_event );
4729 }
4730 res = WaitForSingleObject( hCompleteEvent, INFINITE );
4731 ok( res == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", res );
4732 ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error );
4733 todo_wine_if( pending_reads > 1 )
4734 ok( ib.dwBufferLength != 0, "expected ib.dwBufferLength != 0\n" );
4735 CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
4736 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
4737 }
4738
4739 CLEAR_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
4740 CLEAR_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
4741 if (!ib.dwBufferLength) break;
4742 }
4743
4744 todo_wine
4745 ok( pending_reads == 1, "expected 1 pending read, got %u\n", pending_reads );
4746 ok( !strcmp(buffer, page1), "unexpected buffer content\n" );
4747 close_async_handle( ses, hCompleteEvent, 2 );
4748 ResetEvent( conn_wait_event );
4749
4750 /* test asynchronous InternetReadFile */
4751 ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC );
4752 ok( ses != NULL, "InternetOpenA failed\n" );
4753 pInternetSetStatusCallbackA( ses, &callback );
4754
4755 SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
4756 con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef );
4757 ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
4758 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
4759
4760 SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
4761 req = HttpOpenRequestA( con, "GET", "/async_read", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef );
4762 ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
4763 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
4764
4765 SET_OPTIONAL( INTERNET_STATUS_COOKIE_SENT );
4766 SET_OPTIONAL( INTERNET_STATUS_DETECTING_PROXY );
4767 SET_EXPECT( INTERNET_STATUS_CONNECTING_TO_SERVER );
4768 SET_EXPECT( INTERNET_STATUS_CONNECTED_TO_SERVER );
4769 SET_EXPECT( INTERNET_STATUS_SENDING_REQUEST );
4770 SET_EXPECT( INTERNET_STATUS_REQUEST_SENT );
4771 SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
4772 SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
4773 SET_OPTIONAL( INTERNET_STATUS_CLOSING_CONNECTION );
4774 SET_OPTIONAL( INTERNET_STATUS_CONNECTION_CLOSED );
4775 SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
4776
4777 SetLastError( 0xdeadbeef );
4778 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
4779 ok( !ret, "HttpSendRequestA unexpectedly succeeded\n" );
4780 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
4781 WaitForSingleObject( hCompleteEvent, INFINITE );
4782 ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error );
4783
4784 CLEAR_NOTIFIED( INTERNET_STATUS_COOKIE_SENT );
4785 CLEAR_NOTIFIED( INTERNET_STATUS_DETECTING_PROXY );
4786 CHECK_NOTIFIED( INTERNET_STATUS_CONNECTING_TO_SERVER );
4787 CHECK_NOTIFIED( INTERNET_STATUS_CONNECTED_TO_SERVER );
4788 CHECK_NOTIFIED( INTERNET_STATUS_SENDING_REQUEST );
4789 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_SENT );
4790 CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
4791 CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
4792 CLEAR_NOTIFIED( INTERNET_STATUS_CLOSING_CONNECTION );
4793 CLEAR_NOTIFIED( INTERNET_STATUS_CONNECTION_CLOSED );
4794 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
4795
4796 pending_reads = 0;
4797 memset( buffer, 0, sizeof(buffer) );
4798 for (count = 0; count < sizeof(buffer); count += bytes)
4799 {
4800 SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
4801 SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
4802
4803 bytes = 0xdeadbeef;
4804 ret = InternetReadFile( req, buffer + count, min(16, sizeof(buffer) - count), &bytes );
4805 if (!count) /* the first part should arrive immediately */
4806 ok( ret, "InternetReadFile failed %u\n", GetLastError() );
4807 if (!ret)
4808 {
4809 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
4810 ok( bytes == 0, "expected 0, got %u\n", bytes );
4811 todo_wine
4812 CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
4813 SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
4814 if (!pending_reads++)
4815 {
4816 res = WaitForSingleObject( hCompleteEvent, 0 );
4817 ok( res == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %u\n", res );
4818 SetEvent( conn_wait_event );
4819 }
4820 res = WaitForSingleObject( hCompleteEvent, INFINITE );
4821 ok( res == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", res );
4822 ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error );
4823 todo_wine_if( pending_reads > 1 )
4824 ok( bytes != 0, "expected bytes != 0\n" );
4825 CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
4826 CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
4827 }
4828
4829 CLEAR_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
4830 CLEAR_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
4831 if (!bytes) break;
4832 }
4833
4834 todo_wine
4835 ok( pending_reads == 1, "expected 1 pending read, got %u\n", pending_reads );
4836 ok( !strcmp(buffer, page1), "unexpected buffer content\n" );
4837 close_async_handle( ses, hCompleteEvent, 2 );
4838
4839 CloseHandle( hCompleteEvent );
4840 CloseHandle( conn_wait_event );
4841 }
4842
4843 static void server_send_string(const char *msg)
4844 {
4845 send(server_socket, msg, strlen(msg), 0);
4846 }
4847
4848 static void WINAPI readex_callback(HINTERNET handle, DWORD_PTR context, DWORD status, void *info, DWORD info_size)
4849 {
4850 switch(status) {
4851 case INTERNET_STATUS_RECEIVING_RESPONSE:
4852 case INTERNET_STATUS_RESPONSE_RECEIVED:
4853 break;
4854 default:
4855 callback(handle, context, status, info, info_size);
4856 }
4857 }
4858
4859 static void open_read_test_request(int port, test_request_t *req, const char *response)
4860 {
4861 BOOL ret;
4862
4863 req->session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
4864 ok(req->session != NULL, "InternetOpenA failed\n");
4865 pInternetSetStatusCallbackA(req->session, readex_callback);
4866
4867 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4868 req->connection = InternetConnectA(req->session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef);
4869 ok(req->connection != NULL, "InternetConnectA failed %u\n", GetLastError());
4870 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED );
4871
4872 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
4873 req->request = HttpOpenRequestA(req->connection, "GET", "/socket", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef);
4874 ok(req->request != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
4875 CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
4876
4877 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
4878 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
4879 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
4880 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
4881 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
4882 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
4883
4884 SetLastError(0xdeadbeef);
4885 ret = HttpSendRequestA(req->request, NULL, 0, NULL, 0);
4886 ok(!ret, "HttpSendRequestA unexpectedly succeeded\n");
4887 ok(GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError());
4888 ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error);
4889
4890 WaitForSingleObject(server_req_rec_event, INFINITE);
4891
4892 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
4893 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
4894 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
4895 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
4896 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
4897
4898 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4899
4900 server_send_string(response);
4901 WaitForSingleObject(hCompleteEvent, INFINITE);
4902
4903 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
4904 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4905 }
4906
4907 #define readex_expect_sync_data(a,b,c,d,e) _readex_expect_sync_data(__LINE__,a,b,c,d,e)
4908 static void _readex_expect_sync_data(unsigned line, HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf,
4909 DWORD buf_size, const char *exdata)
4910 {
4911 DWORD len = strlen(exdata);
4912 BOOL ret;
4913
4914 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4915
4916 memset(buf->lpvBuffer, 0xff, buf_size);
4917 buf->dwBufferLength = buf_size;
4918 ret = InternetReadFileExW(req, buf, flags, 0xdeadbeef);
4919 ok_(__FILE__,line)(ret, "InternetReadFileExW failed: %u\n", GetLastError());
4920 ok_(__FILE__,line)(buf->dwBufferLength == len, "dwBufferLength = %u, expected %u\n", buf->dwBufferLength, len);
4921 if(len)
4922 ok_(__FILE__,line)(!memcmp(buf->lpvBuffer, exdata, len), "Unexpected data\n");
4923
4924 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4925 }
4926
4927 static void send_response_and_wait(const char *response, BOOL close_connection, INTERNET_BUFFERSW *buf)
4928 {
4929 DWORD orig_size = buf->dwBufferLength;
4930
4931 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4932
4933 if(response)
4934 server_send_string(response);
4935
4936 if(close_connection) {
4937 char c;
4938 SetEvent(conn_wait_event);
4939 recv(server_socket, &c, 1, 0);
4940 }
4941
4942 WaitForSingleObject(hCompleteEvent, INFINITE);
4943
4944 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4945
4946 /* If IRF_NO_WAIT is used, buffer is not changed. */
4947 ok(buf->dwBufferLength == orig_size, "dwBufferLength = %u\n", buf->dwBufferLength);
4948 ok(!*(int*)buf->lpvBuffer, "buffer data changed\n");
4949 }
4950
4951 static void readex_expect_async(HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf, DWORD buf_size)
4952 {
4953 BOOL ret;
4954
4955 memset(buf->lpvBuffer, 0, buf_size);
4956 buf->dwBufferLength = buf_size;
4957 ret = InternetReadFileExW(req, buf, flags, 0xdeadbeef);
4958 ok(!ret && GetLastError() == ERROR_IO_PENDING, "InternetReadFileExW returned %x (%u)\n", ret, GetLastError());
4959 ok(buf->dwBufferLength == buf_size, "dwBufferLength = %u, expected %u\n", buf->dwBufferLength, buf_size);
4960 ok(!*(int*)buf->lpvBuffer, "buffer data changed\n");
4961 }
4962
4963 static void test_http_read(int port)
4964 {
4965 INTERNET_BUFFERSW ib;
4966 test_request_t req;
4967 char buf[4096];
4968
4969 if(!is_ie7plus)
4970 return;
4971
4972 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
4973 conn_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
4974 server_req_rec_event = CreateEventW(NULL, FALSE, FALSE, NULL);
4975
4976 trace("Testing InternetReadFileExW with IRF_NO_WAIT flag...\n");
4977
4978 open_read_test_request(port, &req,
4979 "HTTP/1.1 200 OK\r\n"
4980 "Server: winetest\r\n"
4981 "\r\n"
4982 "xx");
4983
4984 memset(&ib, 0, sizeof(ib));
4985 ib.dwStructSize = sizeof(ib);
4986 ib.lpvBuffer = buf;
4987
4988 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "xx");
4989
4990 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
4991 send_response_and_wait("1234567890", FALSE, &ib);
4992 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, 5, "12345");
4993 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "67890");
4994
4995 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
4996 send_response_and_wait("12345", TRUE, &ib);
4997
4998 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "12345");
4999 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
5000
5001 close_async_handle(req.session, hCompleteEvent, 2);
5002
5003 open_read_test_request(port, &req,
5004 "HTTP/1.1 200 OK\r\n"
5005 "Server: winetest\r\n"
5006 "Transfer-Encoding: chunked\r\n"
5007 "\r\n"
5008 "9\r\n123456789");
5009 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123456789");
5010 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5011
5012 send_response_and_wait("\r\n1\r\na\r\n1\r\nb\r", FALSE, &ib);
5013 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab");
5014 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5015
5016 send_response_and_wait("\n3\r\nab", FALSE, &ib);
5017 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab");
5018 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5019
5020 send_response_and_wait("c", FALSE, &ib);
5021 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "c");
5022 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5023
5024 send_response_and_wait("\r\n1\r\nx\r\n0\r\n\r\n", TRUE, &ib);
5025 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "x");
5026 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
5027
5028 close_async_handle(req.session, hCompleteEvent, 2);
5029
5030 open_read_test_request(port, &req,
5031 "HTTP/1.1 200 OK\r\n"
5032 "Server: winetest\r\n"
5033 "Transfer-Encoding: chunked\r\n"
5034 "\r\n"
5035 "3\r\n123\r\n");
5036 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123");
5037 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5038
5039 send_response_and_wait("0\r\n\r\n", TRUE, &ib);
5040 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
5041
5042 close_async_handle(req.session, hCompleteEvent, 2);
5043
5044 CloseHandle(hCompleteEvent);
5045 CloseHandle(conn_wait_event);
5046 CloseHandle(server_req_rec_event);
5047 }
5048
5049 static void test_long_url(int port)
5050 {
5051 char long_path[INTERNET_MAX_PATH_LENGTH*2] = "/echo_request?";
5052 char buf[sizeof(long_path)*2], url[sizeof(buf)];
5053 test_request_t req;
5054 DWORD size, len;
5055 BOOL ret;
5056
5057 if(!is_ie7plus)
5058 return;
5059
5060 memset(long_path+strlen(long_path), 'x', sizeof(long_path)-strlen(long_path));
5061 long_path[sizeof(long_path)-1] = 0;
5062 open_simple_request(&req, "localhost", port, NULL, long_path);
5063
5064 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
5065 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
5066 test_status_code(req.request, 200);
5067
5068 receive_simple_request(req.request, buf, sizeof(buf));
5069 ok(strstr(buf, long_path) != NULL, "long pathnot found in %s\n", buf);
5070
5071 sprintf(url, "http://localhost:%u%s", port, long_path);
5072
5073 size = sizeof(buf);
5074 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_URL, buf, &size);
5075 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
5076 len = strlen(url);
5077 ok(size == len, "size = %u, expected %u\n", size, len);
5078 ok(!strcmp(buf, url), "Wrong URL %s, expected %s\n", buf, url);
5079
5080 close_request(&req);
5081 }
5082
5083 static void test_http_connection(void)
5084 {
5085 struct server_info si;
5086 HANDLE hThread;
5087 DWORD id = 0, r;
5088
5089 si.hEvent = CreateEventW(NULL, 0, 0, NULL);
5090 si.port = 7531;
5091
5092 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
5093 ok( hThread != NULL, "create thread failed\n");
5094
5095 r = WaitForSingleObject(si.hEvent, 10000);
5096 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
5097 if (r != WAIT_OBJECT_0)
5098 return;
5099
5100 test_basic_request(si.port, "GET", "/test1");
5101 test_proxy_indirect(si.port);
5102 test_proxy_direct(si.port);
5103 test_header_handling_order(si.port);
5104 test_basic_request(si.port, "POST", "/test5");
5105 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
5106 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
5107 test_basic_request(si.port, "GET", "/test6");
5108 test_basic_request(si.port, "GET", "/testF");
5109 test_connection_header(si.port);
5110 test_header_override(si.port);
5111 test_cookie_header(si.port);
5112 test_basic_authentication(si.port);
5113 test_invalid_response_headers(si.port);
5114 test_response_without_headers(si.port);
5115 test_HttpQueryInfo(si.port);
5116 test_HttpSendRequestW(si.port);
5117 test_options(si.port);
5118 test_no_content(si.port);
5119 test_conn_close(si.port);
5120 test_no_cache(si.port);
5121 test_cache_read_gzipped(si.port);
5122 test_http_status(si.port);
5123 test_premature_disconnect(si.port);
5124 test_connection_closing(si.port);
5125 test_cache_control_verb(si.port);
5126 test_successive_HttpSendRequest(si.port);
5127 test_head_request(si.port);
5128 test_request_content_length(si.port);
5129 test_accept_encoding(si.port);
5130 test_basic_auth_credentials_reuse(si.port);
5131 test_async_read(si.port);
5132 test_http_read(si.port);
5133 test_long_url(si.port);
5134
5135 /* send the basic request again to shutdown the server thread */
5136 test_basic_request(si.port, "GET", "/quit");
5137
5138 r = WaitForSingleObject(hThread, 3000);
5139 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
5140 CloseHandle(hThread);
5141 }
5142
5143 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
5144 {
5145 LocalFree(info->lpszSubjectInfo);
5146 LocalFree(info->lpszIssuerInfo);
5147 LocalFree(info->lpszProtocolName);
5148 LocalFree(info->lpszSignatureAlgName);
5149 LocalFree(info->lpszEncryptionAlgName);
5150 }
5151
5152 typedef struct {
5153 const char *ex_subject;
5154 const char *ex_issuer;
5155 } cert_struct_test_t;
5156
5157 static const cert_struct_test_t test_winehq_org_cert = {
5158 "*.winehq.org",
5159
5160 "US\r\n"
5161 "GeoTrust Inc.\r\n"
5162 "RapidSSL SHA256 CA"
5163 };
5164
5165 static const cert_struct_test_t test_winehq_com_cert = {
5166 "US\r\n"
5167 "Minnesota\r\n"
5168 "Saint Paul\r\n"
5169 "WineHQ\r\n"
5170 "test.winehq.com\r\n"
5171 "webmaster@winehq.org",
5172
5173 "US\r\n"
5174 "Minnesota\r\n"
5175 "WineHQ\r\n"
5176 "test.winehq.com\r\n"
5177 "webmaster@winehq.org"
5178 };
5179
5180 static void test_cert_struct(HINTERNET req, const cert_struct_test_t *test)
5181 {
5182 INTERNET_CERTIFICATE_INFOA info;
5183 DWORD size;
5184 BOOL res;
5185
5186 memset(&info, 0x5, sizeof(info));
5187
5188 size = sizeof(info);
5189 res = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, &info, &size);
5190 ok(res, "InternetQueryOption failed: %u\n", GetLastError());
5191 ok(size == sizeof(info), "size = %u\n", size);
5192
5193 ok(!strcmp(info.lpszSubjectInfo, test->ex_subject), "lpszSubjectInfo = %s\n", info.lpszSubjectInfo);
5194 ok(!strcmp(info.lpszIssuerInfo, test->ex_issuer), "lpszIssuerInfo = %s\n", info.lpszIssuerInfo);
5195 ok(!info.lpszSignatureAlgName, "lpszSignatureAlgName = %s\n", info.lpszSignatureAlgName);
5196 ok(!info.lpszEncryptionAlgName, "lpszEncryptionAlgName = %s\n", info.lpszEncryptionAlgName);
5197 ok(!info.lpszProtocolName, "lpszProtocolName = %s\n", info.lpszProtocolName);
5198 ok(info.dwKeySize >= 128 && info.dwKeySize <= 256, "dwKeySize = %u\n", info.dwKeySize);
5199
5200 release_cert_info(&info);
5201 }
5202
5203 #define test_security_info(a,b,c) _test_security_info(__LINE__,a,b,c)
5204 static void _test_security_info(unsigned line, const char *urlc, DWORD error, DWORD ex_flags)
5205 {
5206 char url[INTERNET_MAX_URL_LENGTH];
5207 const CERT_CHAIN_CONTEXT *chain;
5208 DWORD flags;
5209 BOOL res;
5210
5211 if(!pInternetGetSecurityInfoByURLA) {
5212 win_skip("pInternetGetSecurityInfoByURLA not available\n");
5213 return;
5214 }
5215
5216 strcpy(url, urlc);
5217 chain = (void*)0xdeadbeef;
5218 flags = 0xdeadbeef;
5219 res = pInternetGetSecurityInfoByURLA(url, &chain, &flags);
5220 if(error == ERROR_SUCCESS) {
5221 ok_(__FILE__,line)(res, "InternetGetSecurityInfoByURLA failed: %u\n", GetLastError());
5222 ok_(__FILE__,line)(chain != NULL, "chain = NULL\n");
5223 ok_(__FILE__,line)(flags == ex_flags, "flags = %x\n", flags);
5224 CertFreeCertificateChain(chain);
5225 }else {
5226 ok_(__FILE__,line)(!res && GetLastError() == error,
5227 "InternetGetSecurityInfoByURLA returned: %x(%u), expected %u\n", res, GetLastError(), error);
5228 }
5229 }
5230
5231 #define test_secflags_option(a,b,c) _test_secflags_option(__LINE__,a,b,c)
5232 static void _test_secflags_option(unsigned line, HINTERNET req, DWORD ex_flags, DWORD opt_flags)
5233 {
5234 DWORD flags, size;
5235 BOOL res;
5236
5237 flags = 0xdeadbeef;
5238 size = sizeof(flags);
5239 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
5240 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
5241 ok_(__FILE__,line)((flags & ~opt_flags) == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS flags = %x, expected %x\n",
5242 flags, ex_flags);
5243
5244 /* Option 98 is undocumented and seems to be the same as INTERNET_OPTION_SECURITY_FLAGS */
5245 flags = 0xdeadbeef;
5246 size = sizeof(flags);
5247 res = InternetQueryOptionW(req, 98, &flags, &size);
5248 ok_(__FILE__,line)(res, "InternetQueryOptionW(98) failed: %u\n", GetLastError());
5249 ok_(__FILE__,line)((flags & ~opt_flags) == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS(98) flags = %x, expected %x\n",
5250 flags, ex_flags);
5251 }
5252
5253 #define set_secflags(a,b,c) _set_secflags(__LINE__,a,b,c)
5254 static void _set_secflags(unsigned line, HINTERNET req, BOOL use_undoc, DWORD flags)
5255 {
5256 BOOL res;
5257
5258 res = InternetSetOptionW(req, use_undoc ? 99 : INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
5259 ok_(__FILE__,line)(res, "InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
5260 }
5261
5262 static void test_security_flags(void)
5263 {
5264 INTERNET_CERTIFICATE_INFOA *cert;
5265 HINTERNET ses, conn, req;
5266 DWORD size, flags;
5267 char buf[100];
5268 BOOL res;
5269
5270 trace("Testing security flags...\n");
5271
5272 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
5273
5274 ses = InternetOpenA("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
5275 ok(ses != NULL, "InternetOpen failed\n");
5276
5277 pInternetSetStatusCallbackA(ses, &callback);
5278
5279 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5280 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
5281 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
5282 ok(conn != NULL, "InternetConnect failed with error %u\n", GetLastError());
5283 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5284
5285 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5286 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
5287 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
5288 0xdeadbeef);
5289 ok(req != NULL, "HttpOpenRequest failed\n");
5290 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5291
5292 flags = 0xdeadbeef;
5293 size = sizeof(flags);
5294 res = InternetQueryOptionW(req, 98, &flags, &size);
5295 if(!res && GetLastError() == ERROR_INVALID_PARAMETER) {
5296 win_skip("Incomplete security flags support, skipping\n");
5297
5298 close_async_handle(ses, hCompleteEvent, 2);
5299 CloseHandle(hCompleteEvent);
5300 return;
5301 }
5302
5303 test_secflags_option(req, 0, 0);
5304 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5305
5306 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_REVOCATION);
5307 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION, 0);
5308
5309 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
5310 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID, 0);
5311
5312 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
5313 test_secflags_option(req, SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID, 0);
5314
5315 flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_SECURE;
5316 res = InternetSetOptionW(req, 99, &flags, sizeof(flags));
5317 ok(!res && GetLastError() == ERROR_INTERNET_OPTION_NOT_SETTABLE, "InternetSetOption(99) failed: %u\n", GetLastError());
5318
5319 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
5320 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
5321 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5322 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5323 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
5324 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
5325 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
5326 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
5327 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
5328 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
5329 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5330 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5331 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5332 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5333 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5334
5335 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5336 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5337
5338 WaitForSingleObject(hCompleteEvent, INFINITE);
5339 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
5340
5341 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
5342 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
5343 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
5344 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
5345 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5346 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5347 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
5348 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
5349 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5350 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5351 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5352 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5353 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5354
5355 test_request_flags(req, 0);
5356 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA
5357 |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_STRENGTH_STRONG, 0);
5358
5359 res = InternetReadFile(req, buf, sizeof(buf), &size);
5360 ok(res, "InternetReadFile failed: %u\n", GetLastError());
5361 ok(size, "size = 0\n");
5362
5363 /* Collect all existing persistent connections */
5364 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
5365 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
5366
5367 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5368 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
5369 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
5370 0xdeadbeef);
5371 ok(req != NULL, "HttpOpenRequest failed\n");
5372 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5373
5374 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT|INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY;
5375 res = InternetSetOptionA(req, INTERNET_OPTION_ERROR_MASK, (void*)&flags, sizeof(flags));
5376 ok(res, "InternetQueryOption(INTERNET_OPTION_ERROR_MASK failed: %u\n", GetLastError());
5377
5378 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5379 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5380 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
5381 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
5382 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
5383 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
5384 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
5385 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
5386 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5387 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5388 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5389
5390 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5391 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5392
5393 WaitForSingleObject(hCompleteEvent, INFINITE);
5394 ok(req_error == ERROR_INTERNET_SEC_CERT_REV_FAILED || broken(req_error == ERROR_INTERNET_SEC_CERT_ERRORS),
5395 "req_error = %d\n", req_error);
5396
5397 size = 0;
5398 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
5399 ok(res || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
5400 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %u\n", size);
5401 cert = HeapAlloc(GetProcessHeap(), 0, size);
5402 cert->lpszSubjectInfo = NULL;
5403 cert->lpszIssuerInfo = NULL;
5404 cert->lpszSignatureAlgName = (char *)0xdeadbeef;
5405 cert->lpszEncryptionAlgName = (char *)0xdeadbeef;
5406 cert->lpszProtocolName = (char *)0xdeadbeef;
5407 cert->dwKeySize = 0xdeadbeef;
5408 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, cert, &size);
5409 ok(res, "InternetQueryOption failed: %u\n", GetLastError());
5410 if (res)
5411 {
5412 ok(cert->lpszSubjectInfo && strlen(cert->lpszSubjectInfo) > 1, "expected a non-empty subject name\n");
5413 ok(cert->lpszIssuerInfo && strlen(cert->lpszIssuerInfo) > 1, "expected a non-empty issuer name\n");
5414 ok(!cert->lpszSignatureAlgName, "unexpected signature algorithm name\n");
5415 ok(!cert->lpszEncryptionAlgName, "unexpected encryption algorithm name\n");
5416 ok(!cert->lpszProtocolName, "unexpected protocol name\n");
5417 ok(cert->dwKeySize != 0xdeadbeef, "unexpected key size\n");
5418 }
5419 HeapFree(GetProcessHeap(), 0, cert);
5420
5421 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
5422 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
5423 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
5424 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
5425 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5426 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5427 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5428
5429 if(req_error != ERROR_INTERNET_SEC_CERT_REV_FAILED) {
5430 win_skip("Unexpected cert errors %u, skipping security flags tests\n", req_error);
5431
5432 close_async_handle(ses, hCompleteEvent, 3);
5433 CloseHandle(hCompleteEvent);
5434 return;
5435 }
5436
5437 size = sizeof(buf);
5438 res = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
5439 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfoA(HTTP_QUERY_CONTENT_ENCODING) failed: %u\n", GetLastError());
5440
5441 test_request_flags(req, 8);
5442 /* IE11 finds both rev failure and invalid CA. Previous versions required rev failure
5443 to be ignored before invalid CA was reported. */
5444 test_secflags_option(req, _SECURITY_FLAG_CERT_REV_FAILED, _SECURITY_FLAG_CERT_INVALID_CA);
5445
5446 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_REVOCATION);
5447 test_secflags_option(req, _SECURITY_FLAG_CERT_REV_FAILED|SECURITY_FLAG_IGNORE_REVOCATION, _SECURITY_FLAG_CERT_INVALID_CA);
5448
5449 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5450 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5451 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
5452 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
5453 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5454 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5455 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5456
5457 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5458 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5459
5460 WaitForSingleObject(hCompleteEvent, INFINITE);
5461 ok(req_error == ERROR_INTERNET_SEC_CERT_ERRORS, "req_error = %d\n", req_error);
5462
5463 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
5464 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
5465 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5466 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5467 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5468 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5469 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5470
5471 test_request_flags(req, INTERNET_REQFLAG_NO_HEADERS);
5472 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
5473 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5474
5475 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
5476 test_secflags_option(req, _SECURITY_FLAG_CERT_INVALID_CA|_SECURITY_FLAG_CERT_REV_FAILED
5477 |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_UNKNOWN_CA, 0);
5478 test_http_version(req);
5479
5480 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5481 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5482 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
5483 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
5484 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
5485 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
5486 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
5487 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
5488 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5489 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5490 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5491 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5492 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5493
5494 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5495 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5496
5497 WaitForSingleObject(hCompleteEvent, INFINITE);
5498 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
5499
5500 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
5501 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
5502 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5503 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5504 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
5505 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
5506 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5507 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5508 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5509 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5510 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5511
5512 test_request_flags(req, 0);
5513 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION
5514 |SECURITY_FLAG_STRENGTH_STRONG|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
5515
5516 test_cert_struct(req, &test_winehq_com_cert);
5517 test_security_info("https://test.winehq.com/data/some_file.html?q", 0,
5518 _SECURITY_FLAG_CERT_INVALID_CA|_SECURITY_FLAG_CERT_REV_FAILED);
5519
5520 res = InternetReadFile(req, buf, sizeof(buf), &size);
5521 ok(res, "InternetReadFile failed: %u\n", GetLastError());
5522 ok(size, "size = 0\n");
5523
5524 close_async_handle(ses, hCompleteEvent, 3);
5525
5526 /* Collect all existing persistent connections */
5527 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
5528 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
5529
5530 /* Make another request, without setting security flags */
5531
5532 ses = InternetOpenA("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
5533 ok(ses != NULL, "InternetOpen failed\n");
5534
5535 pInternetSetStatusCallbackA(ses, &callback);
5536
5537 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5538 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
5539 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
5540 ok(conn != NULL, "InternetConnect failed with error %u\n", GetLastError());
5541 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5542
5543 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5544 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
5545 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
5546 0xdeadbeef);
5547 ok(req != NULL, "HttpOpenRequest failed\n");
5548 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5549
5550 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
5551 |SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
5552 test_http_version(req);
5553
5554 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5555 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5556 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
5557 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
5558 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
5559 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
5560 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
5561 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
5562 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5563 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5564 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5565 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5566
5567 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5568 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5569
5570 WaitForSingleObject(hCompleteEvent, INFINITE);
5571 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
5572
5573 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
5574 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
5575 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5576 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5577 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
5578 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
5579 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5580 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5581 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5582 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5583
5584 test_request_flags(req, 0);
5585 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
5586 |SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
5587
5588 res = InternetReadFile(req, buf, sizeof(buf), &size);
5589 ok(res, "InternetReadFile failed: %u\n", GetLastError());
5590 ok(size, "size = 0\n");
5591
5592 close_async_handle(ses, hCompleteEvent, 2);
5593
5594 CloseHandle(hCompleteEvent);
5595
5596 test_security_info("http://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5597 test_security_info("file:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5598 test_security_info("xxx:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5599 }
5600
5601 static void test_secure_connection(void)
5602 {
5603 static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
5604 static const WCHAR testsite[] = {'t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',0};
5605 static const WCHAR get[] = {'G','E','T',0};
5606 static const WCHAR testpage[] = {'/','t','e','s','t','s','/','h','e','l','l','o','.','h','t','m','l',0};
5607 HINTERNET ses, con, req;
5608 DWORD size, flags;
5609 INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
5610 INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
5611 BOOL ret;
5612
5613 ses = InternetOpenA("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
5614 ok(ses != NULL, "InternetOpen failed\n");
5615
5616 con = InternetConnectA(ses, "test.winehq.org",
5617 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
5618 INTERNET_SERVICE_HTTP, 0, 0);
5619 ok(con != NULL, "InternetConnect failed\n");
5620
5621 req = HttpOpenRequestA(con, "GET", "/tests/hello.html", NULL, NULL, NULL,
5622 INTERNET_FLAG_SECURE, 0);
5623 ok(req != NULL, "HttpOpenRequest failed\n");
5624
5625 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
5626 ok(ret || broken(GetLastError() == ERROR_INTERNET_CANNOT_CONNECT),
5627 "HttpSendRequest failed: %d\n", GetLastError());
5628 if (!ret)
5629 {
5630 win_skip("Cannot connect to https.\n");
5631 goto done;
5632 }
5633
5634 size = sizeof(flags);
5635 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
5636 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5637 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
5638
5639 test_cert_struct(req, &test_winehq_org_cert);
5640
5641 /* Querying the same option through InternetQueryOptionW still results in
5642 * ASCII strings being returned.
5643 */
5644 size = 0;
5645 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5646 NULL, &size);
5647 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
5648 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
5649 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
5650 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5651 certificate_structW, &size);
5652 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
5653 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5654 if (ret)
5655 {
5656 ok(certificate_structA->lpszSubjectInfo &&
5657 strlen(certificate_structA->lpszSubjectInfo) > 1,
5658 "expected a non-empty subject name\n");
5659 ok(certificate_structA->lpszIssuerInfo &&
5660 strlen(certificate_structA->lpszIssuerInfo) > 1,
5661 "expected a non-empty issuer name\n");
5662 ok(!certificate_structA->lpszSignatureAlgName,
5663 "unexpected signature algorithm name\n");
5664 ok(!certificate_structA->lpszEncryptionAlgName,
5665 "unexpected encryption algorithm name\n");
5666 ok(!certificate_structA->lpszProtocolName,
5667 "unexpected protocol name\n");
5668 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
5669 release_cert_info(certificate_structA);
5670 }
5671 HeapFree(GetProcessHeap(), 0, certificate_structW);
5672
5673 InternetCloseHandle(req);
5674 InternetCloseHandle(con);
5675 InternetCloseHandle(ses);
5676
5677 /* Repeating the tests with the W functions has the same result: */
5678 ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
5679 ok(ses != NULL, "InternetOpen failed\n");
5680
5681 con = InternetConnectW(ses, testsite,
5682 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
5683 INTERNET_SERVICE_HTTP, 0, 0);
5684 ok(con != NULL, "InternetConnect failed\n");
5685
5686 req = HttpOpenRequestW(con, get, testpage, NULL, NULL, NULL,
5687 INTERNET_FLAG_SECURE|INTERNET_FLAG_RELOAD, 0);
5688 ok(req != NULL, "HttpOpenRequest failed\n");
5689
5690 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
5691 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
5692
5693 size = sizeof(flags);
5694 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
5695 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5696 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set, got %x\n", flags);
5697
5698 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5699 NULL, &size);
5700 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
5701 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
5702 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
5703 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5704 certificate_structA, &size);
5705 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5706 if (ret)
5707 {
5708 ok(certificate_structA->lpszSubjectInfo &&
5709 strlen(certificate_structA->lpszSubjectInfo) > 1,
5710 "expected a non-empty subject name\n");
5711 ok(certificate_structA->lpszIssuerInfo &&
5712 strlen(certificate_structA->lpszIssuerInfo) > 1,
5713 "expected a non-empty issuer name\n");
5714 ok(!certificate_structA->lpszSignatureAlgName,
5715 "unexpected signature algorithm name\n");
5716 ok(!certificate_structA->lpszEncryptionAlgName,
5717 "unexpected encryption algorithm name\n");
5718 ok(!certificate_structA->lpszProtocolName,
5719 "unexpected protocol name\n");
5720 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
5721 release_cert_info(certificate_structA);
5722 }
5723 HeapFree(GetProcessHeap(), 0, certificate_structA);
5724
5725 /* Again, querying the same option through InternetQueryOptionW still
5726 * results in ASCII strings being returned.
5727 */
5728 size = 0;
5729 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5730 NULL, &size);
5731 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
5732 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
5733 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
5734 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5735 certificate_structW, &size);
5736 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
5737 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5738 if (ret)
5739 {
5740 ok(certificate_structA->lpszSubjectInfo &&
5741 strlen(certificate_structA->lpszSubjectInfo) > 1,
5742 "expected a non-empty subject name\n");
5743 ok(certificate_structA->lpszIssuerInfo &&
5744 strlen(certificate_structA->lpszIssuerInfo) > 1,
5745 "expected a non-empty issuer name\n");
5746 ok(!certificate_structA->lpszSignatureAlgName,
5747 "unexpected signature algorithm name\n");
5748 ok(!certificate_structA->lpszEncryptionAlgName,
5749 "unexpected encryption algorithm name\n");
5750 ok(!certificate_structA->lpszProtocolName,
5751 "unexpected protocol name\n");
5752 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
5753 release_cert_info(certificate_structA);
5754 }
5755 HeapFree(GetProcessHeap(), 0, certificate_structW);
5756
5757 done:
5758 InternetCloseHandle(req);
5759 InternetCloseHandle(con);
5760 InternetCloseHandle(ses);
5761 }
5762
5763 static void test_user_agent_header(void)
5764 {
5765 HINTERNET ses, con, req;
5766 DWORD size, err;
5767 char buffer[64];
5768 BOOL ret;
5769
5770 ses = InternetOpenA("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
5771 ok(ses != NULL, "InternetOpen failed\n");
5772
5773 con = InternetConnectA(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
5774 ok(con != NULL, "InternetConnect failed\n");
5775
5776 req = HttpOpenRequestA(con, "GET", "/tests/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
5777 ok(req != NULL, "HttpOpenRequest failed\n");
5778
5779 size = sizeof(buffer);
5780 ret = HttpQueryInfoA(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5781 err = GetLastError();
5782 ok(!ret, "HttpQueryInfo succeeded\n");
5783 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
5784
5785 ret = HttpAddRequestHeadersA(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
5786 ok(ret, "HttpAddRequestHeaders succeeded\n");
5787
5788 size = sizeof(buffer);
5789 ret = HttpQueryInfoA(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5790 err = GetLastError();
5791 ok(ret, "HttpQueryInfo failed\n");
5792
5793 InternetCloseHandle(req);
5794
5795 req = HttpOpenRequestA(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
5796 ok(req != NULL, "HttpOpenRequest failed\n");
5797
5798 size = sizeof(buffer);
5799 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5800 err = GetLastError();
5801 ok(!ret, "HttpQueryInfo succeeded\n");
5802 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
5803
5804 ret = HttpAddRequestHeadersA(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
5805 ok(ret, "HttpAddRequestHeaders failed\n");
5806
5807 buffer[0] = 0;
5808 size = sizeof(buffer);
5809 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5810 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
5811 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
5812
5813 InternetCloseHandle(req);
5814 InternetCloseHandle(con);
5815 InternetCloseHandle(ses);
5816 }
5817
5818 static void test_bogus_accept_types_array(void)
5819 {
5820 HINTERNET ses, con, req;
5821 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
5822 DWORD size, error;
5823 char buffer[32];
5824 BOOL ret;
5825
5826 ses = InternetOpenA("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
5827 con = InternetConnectA(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
5828 req = HttpOpenRequestA(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
5829
5830 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
5831
5832 buffer[0] = 0;
5833 size = sizeof(buffer);
5834 SetLastError(0xdeadbeef);
5835 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5836 error = GetLastError();
5837 ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
5838 if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", error);
5839 ok(broken(!strcmp(buffer, ", */*, %p, , , */*")) /* IE6 */ ||
5840 broken(!strcmp(buffer, "*/*, %p, */*")) /* IE7/8 */ ||
5841 !strcmp(buffer, ""), "got '%s' expected ''\n", buffer);
5842
5843 InternetCloseHandle(req);
5844 InternetCloseHandle(con);
5845 InternetCloseHandle(ses);
5846 }
5847
5848 struct context
5849 {
5850 HANDLE event;
5851 HINTERNET req;
5852 };
5853
5854 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
5855 {
5856 INTERNET_ASYNC_RESULT *result = info;
5857 struct context *ctx = (struct context *)context;
5858
5859 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
5860
5861 switch(status) {
5862 case INTERNET_STATUS_REQUEST_COMPLETE:
5863 trace("request handle: 0x%08lx\n", result->dwResult);
5864 ctx->req = (HINTERNET)result->dwResult;
5865 SetEvent(ctx->event);
5866 break;
5867 case INTERNET_STATUS_HANDLE_CLOSING: {
5868 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
5869
5870 if (InternetQueryOptionA(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
5871 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
5872 SetEvent(ctx->event);
5873 break;
5874 }
5875 case INTERNET_STATUS_NAME_RESOLVED:
5876 case INTERNET_STATUS_CONNECTING_TO_SERVER:
5877 case INTERNET_STATUS_CONNECTED_TO_SERVER: {
5878 char *str = info;
5879 ok(str[0] && str[1], "Got string: %s\n", str);
5880 ok(size == strlen(str)+1, "unexpected size %u\n", size);
5881 }
5882 }
5883 }
5884
5885 static void test_open_url_async(void)
5886 {
5887 BOOL ret;
5888 HINTERNET ses, req;
5889 DWORD size, error;
5890 struct context ctx;
5891 ULONG type;
5892
5893 /* Collect all existing persistent connections */
5894 ret = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
5895 ok(ret, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
5896
5897 /*
5898 * Some versions of IE6 fail those tests. They pass some notification data as UNICODE string, while
5899 * other versions never do. They also hang of following tests. We disable it for everything older
5900 * than IE7.
5901 */
5902 if(!is_ie7plus)
5903 return;
5904
5905 ctx.req = NULL;
5906 ctx.event = CreateEventA(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
5907
5908 ses = InternetOpenA("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
5909 ok(ses != NULL, "InternetOpen failed\n");
5910
5911 SetLastError(0xdeadbeef);
5912 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
5913 error = GetLastError();
5914 ok(!ret, "InternetSetOptionA succeeded\n");
5915 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
5916
5917 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
5918 error = GetLastError();
5919 ok(!ret, "InternetSetOptionA failed\n");
5920 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
5921
5922 pInternetSetStatusCallbackW(ses, cb);
5923 ResetEvent(ctx.event);
5924
5925 req = InternetOpenUrlA(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
5926 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
5927
5928 WaitForSingleObject(ctx.event, INFINITE);
5929
5930 type = 0;
5931 size = sizeof(type);
5932 ret = InternetQueryOptionA(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
5933 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
5934 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
5935 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
5936
5937 size = 0;
5938 ret = HttpQueryInfoA(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
5939 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
5940 ok(size > 0, "expected size > 0\n");
5941
5942 ResetEvent(ctx.event);
5943 InternetCloseHandle(ctx.req);
5944 WaitForSingleObject(ctx.event, INFINITE);
5945
5946 InternetCloseHandle(ses);
5947 CloseHandle(ctx.event);
5948 }
5949
5950 enum api
5951 {
5952 internet_connect = 1,
5953 http_open_request,
5954 http_send_request_ex,
5955 internet_writefile,
5956 http_end_request,
5957 internet_close_handle
5958 };
5959
5960 struct notification
5961 {
5962 enum api function; /* api responsible for notification */
5963 unsigned int status; /* status received */
5964 BOOL async; /* delivered from another thread? */
5965 BOOL todo;
5966 BOOL optional;
5967 };
5968
5969 struct info
5970 {
5971 enum api function;
5972 const struct notification *test;
5973 unsigned int count;
5974 unsigned int index;
5975 HANDLE wait;
5976 DWORD thread;
5977 unsigned int line;
5978 DWORD expect_result;
5979 BOOL is_aborted;
5980 };
5981
5982 static CRITICAL_SECTION notification_cs;
5983
5984 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
5985 {
5986 BOOL status_ok, function_ok;
5987 struct info *info = (struct info *)context;
5988 unsigned int i;
5989
5990 EnterCriticalSection( &notification_cs );
5991
5992 if(info->is_aborted) {
5993 LeaveCriticalSection(&notification_cs);
5994 return;
5995 }
5996
5997 if (status == INTERNET_STATUS_HANDLE_CREATED)
5998 {
5999 DWORD size = sizeof(struct info *);
6000 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
6001 }else if(status == INTERNET_STATUS_REQUEST_COMPLETE) {
6002 INTERNET_ASYNC_RESULT *ar = (INTERNET_ASYNC_RESULT*)buffer;
6003
6004 ok(buflen == sizeof(*ar), "unexpected buflen = %d\n", buflen);
6005 if(info->expect_result == ERROR_SUCCESS) {
6006 ok(ar->dwResult == 1, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
6007 }else {
6008 ok(!ar->dwResult, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
6009 ok(ar->dwError == info->expect_result, "ar->dwError = %d, expected %d\n", ar->dwError, info->expect_result);
6010 }
6011 }
6012
6013 i = info->index;
6014 if (i >= info->count)
6015 {
6016 LeaveCriticalSection( &notification_cs );
6017 return;
6018 }
6019
6020 while (info->test[i].status != status &&
6021 (info->test[i].optional || info->test[i].todo) &&
6022 i < info->count - 1 &&
6023 info->test[i].function == info->test[i + 1].function)
6024 {
6025 i++;
6026 }
6027
6028 status_ok = (info->test[i].status == status);
6029 function_ok = (info->test[i].function == info->function);
6030
6031 if (!info->test[i].todo)
6032 {
6033 ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
6034 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
6035
6036 if (info->test[i].async)
6037 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
6038 info->line, info->thread, GetCurrentThreadId());
6039 }
6040 else
6041 {
6042 todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
6043 if (status_ok)
6044 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
6045 }
6046 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
6047 info->index = i+1;
6048
6049 LeaveCriticalSection( &notification_cs );
6050 }
6051
6052 static void setup_test( struct info *info, enum api function, unsigned int line, DWORD expect_result )
6053 {
6054 info->function = function;
6055 info->line = line;
6056 info->expect_result = expect_result;
6057 }
6058
6059 struct notification_data
6060 {
6061 const struct notification *test;
6062 const unsigned int count;
6063 const char *method;
6064 const char *host;
6065 const char *path;
6066 const char *data;
6067 BOOL expect_conn_failure;
6068 };
6069
6070 static const struct notification async_send_request_ex_test[] =
6071 {
6072 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6073 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6074 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6075 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
6076 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
6077 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
6078 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE },
6079 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE },
6080 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
6081 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
6082 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6083 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, FALSE },
6084 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, FALSE },
6085 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
6086 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
6087 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6088 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
6089 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
6090 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
6091 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
6092 };
6093
6094 static const struct notification async_send_request_ex_test2[] =
6095 {
6096 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6097 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6098 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6099 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
6100 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
6101 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
6102 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE, TRUE },
6103 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE, TRUE },
6104 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
6105 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
6106 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6107 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
6108 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
6109 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6110 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
6111 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
6112 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
6113 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
6114 };
6115
6116 static const struct notification async_send_request_ex_resolve_failure_test[] =
6117 {
6118 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6119 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6120 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6121 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE },
6122 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6123 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6124 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6125 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
6126 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
6127 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
6128 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
6129 };
6130
6131 static const struct notification async_send_request_ex_chunked_test[] =
6132 {
6133 { internet_connect, INTERNET_STATUS_HANDLE_CREATED },
6134 { http_open_request, INTERNET_STATUS_HANDLE_CREATED },
6135 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6136 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
6137 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
6138 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
6139 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE },
6140 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE },
6141 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
6142 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
6143 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6144 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
6145 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
6146 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6147 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION },
6148 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED },
6149 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING },
6150 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING }
6151 };
6152
6153 static const struct notification_data notification_data[] = {
6154 {
6155 async_send_request_ex_chunked_test,
6156 sizeof(async_send_request_ex_chunked_test)/sizeof(async_send_request_ex_chunked_test[0]),
6157 "GET",
6158 "test.winehq.org",
6159 "tests/data.php"
6160 },
6161 {
6162 async_send_request_ex_test,
6163 sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
6164 "POST",
6165 "test.winehq.org",
6166 "tests/post.php",
6167 "Public ID=codeweavers"
6168 },
6169 {
6170 async_send_request_ex_test2,
6171 sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
6172 "POST",
6173 "test.winehq.org",
6174 "tests/post.php"
6175 },
6176 {
6177 async_send_request_ex_resolve_failure_test,
6178 sizeof(async_send_request_ex_resolve_failure_test)/sizeof(async_send_request_ex_resolve_failure_test[0]),
6179 "GET",
6180 "brokenhost",
6181 "index.html",
6182 NULL,
6183 TRUE
6184 }
6185 };
6186
6187 static void test_async_HttpSendRequestEx(const struct notification_data *nd)
6188 {
6189 BOOL ret;
6190 HINTERNET ses, req, con;
6191 struct info info;
6192 DWORD size, written, error;
6193 INTERNET_BUFFERSA b;
6194 static const char *accept[2] = {"*/*", NULL};
6195 char buffer[32];
6196
6197 trace("Async HttpSendRequestEx test (%s %s)\n", nd->method, nd->host);
6198
6199 InitializeCriticalSection( &notification_cs );
6200
6201 info.test = nd->test;
6202 info.count = nd->count;
6203 info.index = 0;
6204 info.wait = CreateEventW( NULL, FALSE, FALSE, NULL );
6205 info.thread = GetCurrentThreadId();
6206 info.is_aborted = FALSE;
6207
6208 ses = InternetOpenA( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
6209 ok( ses != NULL, "InternetOpen failed\n" );
6210
6211 pInternetSetStatusCallbackA( ses, check_notification );
6212
6213 setup_test( &info, internet_connect, __LINE__, ERROR_SUCCESS );
6214 con = InternetConnectA( ses, nd->host, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
6215 ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
6216
6217 WaitForSingleObject( info.wait, 10000 );
6218
6219 setup_test( &info, http_open_request, __LINE__, ERROR_SUCCESS );
6220 req = HttpOpenRequestA( con, nd->method, nd->path, NULL, NULL, accept, 0, (DWORD_PTR)&info );
6221 ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
6222
6223 WaitForSingleObject( info.wait, 10000 );
6224
6225 if(nd->data) {
6226 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
6227 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
6228 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
6229 b.dwHeadersLength = strlen( b.lpcszHeader );
6230 b.dwBufferTotal = nd->data ? strlen( nd->data ) : 0;
6231 }
6232
6233 setup_test( &info, http_send_request_ex, __LINE__,
6234 nd->expect_conn_failure ? ERROR_INTERNET_NAME_NOT_RESOLVED : ERROR_SUCCESS );
6235 ret = HttpSendRequestExA( req, nd->data ? &b : NULL, NULL, 0x28, 0 );
6236 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
6237
6238 error = WaitForSingleObject( info.wait, 10000 );
6239 if(error != WAIT_OBJECT_0) {
6240 skip("WaitForSingleObject returned %d, assuming DNS problem\n", error);
6241 info.is_aborted = TRUE;
6242 goto abort;
6243 }
6244
6245 size = sizeof(buffer);
6246 SetLastError( 0xdeadbeef );
6247 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
6248 error = GetLastError();
6249 ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
6250 if(nd->expect_conn_failure) {
6251 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND got %u\n", error );
6252 }else {
6253 todo_wine
6254 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
6255 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
6256 }
6257
6258 if (nd->data)
6259 {
6260 written = 0;
6261 size = strlen( nd->data );
6262 setup_test( &info, internet_writefile, __LINE__, ERROR_SUCCESS );
6263 ret = InternetWriteFile( req, nd->data, size, &written );
6264 ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
6265 ok( written == size, "expected %u got %u\n", written, size );
6266
6267 WaitForSingleObject( info.wait, 10000 );
6268
6269 SetLastError( 0xdeadbeef );
6270 ret = HttpEndRequestA( req, (void *)nd->data, 0x28, 0 );
6271 error = GetLastError();
6272 ok( !ret, "HttpEndRequestA succeeded\n" );
6273 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
6274 }
6275
6276 SetLastError( 0xdeadbeef );
6277 setup_test( &info, http_end_request, __LINE__,
6278 nd->expect_conn_failure ? ERROR_INTERNET_OPERATION_CANCELLED : ERROR_SUCCESS);
6279 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
6280 error = GetLastError();
6281 ok( !ret, "HttpEndRequestA succeeded\n" );
6282 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
6283
6284 WaitForSingleObject( info.wait, 10000 );
6285
6286 setup_test( &info, internet_close_handle, __LINE__, ERROR_SUCCESS );
6287 abort:
6288 InternetCloseHandle( req );
6289 InternetCloseHandle( con );
6290 InternetCloseHandle( ses );
6291
6292 WaitForSingleObject( info.wait, 10000 );
6293 Sleep(100);
6294 CloseHandle( info.wait );
6295 DeleteCriticalSection( &notification_cs );
6296 }
6297
6298 static HINTERNET closetest_session, closetest_req, closetest_conn;
6299 static BOOL closetest_closed;
6300
6301 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
6302 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
6303 {
6304 DWORD len, type;
6305 BOOL res;
6306
6307 trace("closetest_callback %p: %d\n", hInternet, dwInternetStatus);
6308
6309 ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
6310 "Unexpected hInternet %p\n", hInternet);
6311 if(!closetest_closed)
6312 return;
6313
6314 len = sizeof(type);
6315 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
6316 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
6317 "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
6318 closetest_req, res, GetLastError());
6319 }
6320
6321 static void test_InternetCloseHandle(void)
6322 {
6323 DWORD len, flags;
6324 BOOL res;
6325
6326 closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
6327 ok(closetest_session != NULL,"InternetOpen failed with error %u\n", GetLastError());
6328
6329 pInternetSetStatusCallbackA(closetest_session, closetest_callback);
6330
6331 closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
6332 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
6333 ok(closetest_conn != NULL,"InternetConnect failed with error %u\n", GetLastError());
6334
6335 closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
6336 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
6337
6338 res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
6339 ok(!res && (GetLastError() == ERROR_IO_PENDING),
6340 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
6341
6342 test_request_flags(closetest_req, INTERNET_REQFLAG_NO_HEADERS);
6343
6344 res = InternetCloseHandle(closetest_session);
6345 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
6346 closetest_closed = TRUE;
6347 trace("Closed session handle\n");
6348
6349 res = InternetCloseHandle(closetest_conn);
6350 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %u\n",
6351 res, GetLastError());
6352
6353 res = InternetCloseHandle(closetest_req);
6354 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %u\n",
6355 res, GetLastError());
6356
6357 len = sizeof(flags);
6358 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
6359 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
6360 "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
6361 closetest_req, res, GetLastError());
6362 }
6363
6364 static void test_connection_failure(void)
6365 {
6366 test_request_t req;
6367 DWORD error;
6368 BOOL ret;
6369
6370 open_simple_request(&req, "localhost", 1, NULL, "/");
6371
6372 SetLastError(0xdeadbeef);
6373 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
6374 error = GetLastError();
6375 ok(!ret, "unexpected success\n");
6376 ok(error == ERROR_INTERNET_CANNOT_CONNECT, "wrong error %u\n", error);
6377
6378 close_request(&req);
6379 }
6380
6381 static void test_default_service_port(void)
6382 {
6383 HINTERNET session, connect, request;
6384 DWORD size, error;
6385 char buffer[128];
6386 BOOL ret;
6387
6388 if(!is_ie7plus)
6389 return;
6390
6391 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
6392 ok(session != NULL, "InternetOpen failed\n");
6393
6394 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
6395 INTERNET_SERVICE_HTTP, 0, 0);
6396 ok(connect != NULL, "InternetConnect failed\n");
6397
6398 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
6399 ok(request != NULL, "HttpOpenRequest failed\n");
6400
6401 SetLastError(0xdeadbeef);
6402 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
6403 error = GetLastError();
6404 ok(!ret, "HttpSendRequest succeeded\n");
6405 ok(error == ERROR_INTERNET_SECURITY_CHANNEL_ERROR || error == ERROR_INTERNET_CANNOT_CONNECT,
6406 "got %u\n", error);
6407
6408 size = sizeof(buffer);
6409 memset(buffer, 0, sizeof(buffer));
6410 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
6411 ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
6412 ok(!strcmp(buffer, "test.winehq.org:80"), "Expected test.winehg.org:80, got '%s'\n", buffer);
6413
6414 InternetCloseHandle(request);
6415 InternetCloseHandle(connect);
6416
6417 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
6418 INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
6419 ok(connect != NULL, "InternetConnect failed\n");
6420
6421 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
6422 ok(request != NULL, "HttpOpenRequest failed\n");
6423
6424 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
6425 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
6426
6427 size = sizeof(buffer);
6428 memset(buffer, 0, sizeof(buffer));
6429 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
6430 ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
6431 ok(!strcmp(buffer, "test.winehq.org"), "Expected test.winehg.org, got '%s'\n", buffer);
6432
6433 InternetCloseHandle(request);
6434 InternetCloseHandle(connect);
6435
6436 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
6437 INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
6438 ok(connect != NULL, "InternetConnect failed\n");
6439
6440 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, 0, 0);
6441 ok(request != NULL, "HttpOpenRequest failed\n");
6442
6443 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
6444 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
6445
6446 size = sizeof(buffer);
6447 memset(buffer, 0, sizeof(buffer));
6448 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
6449 ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
6450 ok(!strcmp(buffer, "test.winehq.org:443"), "Expected test.winehg.org:443, got '%s'\n", buffer);
6451
6452 InternetCloseHandle(request);
6453 InternetCloseHandle(connect);
6454 InternetCloseHandle(session);
6455 }
6456
6457 static void init_status_tests(void)
6458 {
6459 memset(expect, 0, sizeof(expect));
6460 memset(optional, 0, sizeof(optional));
6461 memset(wine_allow, 0, sizeof(wine_allow));
6462 memset(notified, 0, sizeof(notified));
6463 memset(status_string, 0, sizeof(status_string));
6464
6465 #define STATUS_STRING(status) status_string[status] = #status
6466 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
6467 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
6468 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
6469 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
6470 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
6471 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
6472 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
6473 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
6474 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
6475 STATUS_STRING(INTERNET_STATUS_PREFETCH);
6476 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
6477 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
6478 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
6479 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
6480 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
6481 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
6482 STATUS_STRING(INTERNET_STATUS_REDIRECT);
6483 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
6484 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
6485 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
6486 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
6487 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
6488 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
6489 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
6490 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
6491 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
6492 #undef STATUS_STRING
6493 }
6494
6495 static void WINAPI header_cb( HINTERNET handle, DWORD_PTR ctx, DWORD status, LPVOID info, DWORD len )
6496 {
6497 BOOL ret;
6498 DWORD index, size;
6499 char buf[256];
6500
6501 if (status == INTERNET_STATUS_SENDING_REQUEST)
6502 {
6503 ret = HttpAddRequestHeadersA( handle, "winetest: winetest", ~0u, HTTP_ADDREQ_FLAG_ADD );
6504 ok( ret, "HttpAddRequestHeadersA failed %u\n", GetLastError() );
6505 SetEvent( (HANDLE)ctx );
6506 }
6507 else if (status == INTERNET_STATUS_REQUEST_SENT)
6508 {
6509 index = 0;
6510 size = sizeof(buf);
6511 ret = HttpQueryInfoA( handle, HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
6512 buf, &size, &index );
6513 ok( ret, "HttpQueryInfoA failed %u\n", GetLastError() );
6514 ok( strstr( buf, "winetest: winetest" ) != NULL, "header missing\n" );
6515 SetEvent( (HANDLE)ctx );
6516 }
6517 }
6518
6519 static void test_concurrent_header_access(void)
6520 {
6521 HINTERNET ses, con, req;
6522 DWORD err;
6523 BOOL ret;
6524 HANDLE wait = CreateEventW( NULL, FALSE, FALSE, NULL );
6525
6526 ses = InternetOpenA( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
6527 ok( ses != NULL, "InternetOpenA failed\n" );
6528
6529 con = InternetConnectA( ses, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
6530 INTERNET_SERVICE_HTTP, 0, 0 );
6531 ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
6532
6533 req = HttpOpenRequestA( con, NULL, "/", NULL, NULL, NULL, 0, (DWORD_PTR)wait );
6534 ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
6535
6536 pInternetSetStatusCallbackA( req, header_cb );
6537
6538 SetLastError( 0xdeadbeef );
6539 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
6540 err = GetLastError();
6541 ok( !ret, "HttpSendRequestA succeeded\n" );
6542 ok( err == ERROR_IO_PENDING, "got %u\n", ERROR_IO_PENDING );
6543
6544 WaitForSingleObject( wait, 5000 );
6545 WaitForSingleObject( wait, 5000 );
6546
6547 InternetCloseHandle( req );
6548 InternetCloseHandle( con );
6549 InternetCloseHandle( ses );
6550 CloseHandle( wait );
6551 }
6552
6553 START_TEST(http)
6554 {
6555 HMODULE hdll;
6556 hdll = GetModuleHandleA("wininet.dll");
6557
6558 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
6559 win_skip("Too old IE (older than 6.0)\n");
6560 return;
6561 }
6562
6563 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
6564 pInternetSetStatusCallbackW = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackW");
6565 pInternetGetSecurityInfoByURLA = (void*)GetProcAddress(hdll, "InternetGetSecurityInfoByURLA");
6566
6567 if(!pInternetGetSecurityInfoByURLA) {
6568 is_ie7plus = FALSE;
6569 win_skip("IE6 found. It's too old for some tests.\n");
6570 }
6571
6572 init_status_tests();
6573 test_InternetCloseHandle();
6574 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
6575 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
6576 InternetReadFile_test(0, &test_data[1]);
6577 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
6578 test_security_flags();
6579 InternetReadFile_test(0, &test_data[2]);
6580 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
6581 test_open_url_async();
6582 test_async_HttpSendRequestEx(&notification_data[0]);
6583 test_async_HttpSendRequestEx(&notification_data[1]);
6584 test_async_HttpSendRequestEx(&notification_data[2]);
6585 test_async_HttpSendRequestEx(&notification_data[3]);
6586 InternetOpenRequest_test();
6587 test_http_cache();
6588 InternetLockRequestFile_test();
6589 InternetOpenUrlA_test();
6590 HttpHeaders_test();
6591 test_http_connection();
6592 test_secure_connection();
6593 test_user_agent_header();
6594 test_bogus_accept_types_array();
6595 InternetReadFile_chunked_test();
6596 HttpSendRequestEx_test();
6597 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[3]);
6598 test_connection_failure();
6599 test_default_service_port();
6600 test_concurrent_header_access();
6601 }