741bd0c0717e1e6a1d95395d10091cc3200f185a
[reactos.git] / rostests / winetests / wininet / http.c
1 /*
2 * Wininet - HTTP tests
3 *
4 * Copyright 2002 Aric Stewart
5 * Copyright 2004 Mike McCormack
6 * Copyright 2005 Hans Leidekker
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 //#include <stdlib.h>
26
27 #define WIN32_NO_STATUS
28 #define _INC_WINDOWS
29
30 #include <windef.h>
31 #include <winbase.h>
32 #include <winreg.h>
33 #include <winnls.h>
34 #include <wincrypt.h>
35 #include <wininet.h>
36 #include <winsock.h>
37
38 #include <wine/test.h>
39
40 /* Undocumented security flags */
41 #define _SECURITY_FLAG_CERT_REV_FAILED 0x00800000
42 #define _SECURITY_FLAG_CERT_INVALID_CA 0x01000000
43 #define _SECURITY_FLAG_CERT_INVALID_CN 0x02000000
44 #define _SECURITY_FLAG_CERT_INVALID_DATE 0x04000000
45
46 #define TEST_URL "http://test.winehq.org/tests/hello.html"
47
48 static BOOL first_connection_to_test_url = TRUE;
49
50 /* Adapted from dlls/urlmon/tests/protocol.c */
51
52 #define SET_EXPECT2(status, num) \
53 expect[status] = num
54
55 #define SET_EXPECT(status) \
56 SET_EXPECT2(status, 1)
57
58 #define SET_OPTIONAL2(status, num) \
59 optional[status] = num
60
61 #define SET_OPTIONAL(status) \
62 SET_OPTIONAL2(status, 1)
63
64 /* SET_WINE_ALLOW's should be used with an appropriate
65 * todo_wine CHECK_NOTIFIED at a later point in the code */
66 #define SET_WINE_ALLOW2(status, num) \
67 wine_allow[status] = num
68
69 #define SET_WINE_ALLOW(status) \
70 SET_WINE_ALLOW2(status, 1)
71
72 #define CHECK_EXPECT(status) \
73 do { \
74 if (!expect[status] && !optional[status] && wine_allow[status]) \
75 { \
76 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
77 status < MAX_INTERNET_STATUS && status_string[status] ? \
78 status_string[status] : "unknown"); \
79 wine_allow[status]--; \
80 } \
81 else \
82 { \
83 ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
84 status < MAX_INTERNET_STATUS && status_string[status] ? \
85 status_string[status] : "unknown"); \
86 if (expect[status]) expect[status]--; \
87 else if(optional[status]) optional[status]--; \
88 } \
89 notified[status]++; \
90 }while(0)
91
92 /* CLEAR_NOTIFIED used in cases when notification behavior
93 * differs between Windows versions */
94 #define CLEAR_NOTIFIED(status) \
95 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
96
97 #define CHECK_NOTIFIED2(status, num) \
98 do { \
99 ok(notified[status] + optional[status] == (num), \
100 "expected status %d (%s) %d times, received %d times\n", \
101 status, status < MAX_INTERNET_STATUS && status_string[status] ? \
102 status_string[status] : "unknown", (num), notified[status]); \
103 CLEAR_NOTIFIED(status); \
104 }while(0)
105
106 #define CHECK_NOTIFIED(status) \
107 CHECK_NOTIFIED2(status, 1)
108
109 #define CHECK_NOT_NOTIFIED(status) \
110 CHECK_NOTIFIED2(status, 0)
111
112 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
113 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
114 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
115 static const char *status_string[MAX_INTERNET_STATUS];
116
117 static HANDLE hCompleteEvent, conn_close_event, conn_wait_event, server_req_rec_event;
118 static DWORD req_error;
119 static BOOL is_ie7plus = TRUE;
120
121 #define TESTF_REDIRECT 0x01
122 #define TESTF_COMPRESSED 0x02
123 #define TESTF_CHUNKED 0x04
124
125 typedef struct {
126 const char *url;
127 const char *redirected_url;
128 const char *host;
129 const char *path;
130 const char *headers;
131 DWORD flags;
132 const char *post_data;
133 const char *content;
134 } test_data_t;
135
136 static const test_data_t test_data[] = {
137 {
138 "http://test.winehq.org/tests/data.php",
139 "http://test.winehq.org/tests/data.php",
140 "test.winehq.org",
141 "/tests/data.php",
142 "",
143 TESTF_CHUNKED
144 },
145 {
146 "http://test.winehq.org/tests/redirect",
147 "http://test.winehq.org/tests/hello.html",
148 "test.winehq.org",
149 "/tests/redirect",
150 "",
151 TESTF_REDIRECT
152 },
153 {
154 "http://test.winehq.org/tests/gzip.php",
155 "http://test.winehq.org/tests/gzip.php",
156 "test.winehq.org",
157 "/tests/gzip.php",
158 "Accept-Encoding: gzip, deflate",
159 TESTF_COMPRESSED
160 },
161 {
162 "http://test.winehq.org/tests/post.php",
163 "http://test.winehq.org/tests/post.php",
164 "test.winehq.org",
165 "/tests/post.php",
166 "Content-Type: application/x-www-form-urlencoded",
167 0,
168 "mode=Test",
169 "mode => Test\n"
170 }
171 };
172
173 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
174 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackW)(HINTERNET ,INTERNET_STATUS_CALLBACK);
175 static BOOL (WINAPI *pInternetGetSecurityInfoByURLA)(LPSTR,PCCERT_CHAIN_CONTEXT*,DWORD*);
176
177 static int strcmp_wa(LPCWSTR strw, const char *stra)
178 {
179 WCHAR buf[512];
180 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
181 return lstrcmpW(strw, buf);
182 }
183
184 static BOOL proxy_active(void)
185 {
186 HKEY internet_settings;
187 DWORD proxy_enable;
188 DWORD size;
189
190 if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
191 0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
192 return FALSE;
193
194 size = sizeof(DWORD);
195 if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
196 proxy_enable = 0;
197
198 RegCloseKey(internet_settings);
199
200 return proxy_enable != 0;
201 }
202
203 #define test_status_code(a,b) _test_status_code(__LINE__,a,b, FALSE)
204 #define test_status_code_todo(a,b) _test_status_code(__LINE__,a,b, TRUE)
205 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
206 {
207 DWORD code, size, index;
208 char exbuf[12], bufa[10];
209 WCHAR bufw[10];
210 BOOL res;
211
212 code = 0xdeadbeef;
213 size = sizeof(code);
214 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, NULL);
215 ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %u\n", GetLastError());
216 todo_wine_if (is_todo)
217 ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
218 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
219
220 code = 0xdeadbeef;
221 index = 0;
222 size = sizeof(code);
223 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
224 ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %u\n", GetLastError());
225 ok_(__FILE__,line)(!index, "index = %d, expected 0\n", index);
226 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
227
228 sprintf(exbuf, "%u", excode);
229
230 size = sizeof(bufa);
231 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, bufa, &size, NULL);
232 ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
233 todo_wine_if (is_todo)
234 ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
235 ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %d for \"%s\"\n", size, exbuf);
236
237 size = 0;
238 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
239 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
240 "[4] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
241 ok_(__FILE__,line)(size == strlen(exbuf)+1, "unexpected size %d for \"%s\"\n", size, exbuf);
242
243 size = sizeof(bufw);
244 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
245 ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
246 todo_wine_if (is_todo)
247 ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
248 ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
249
250 size = 0;
251 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
252 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
253 "[6] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
254 ok_(__FILE__,line)(size == (strlen(exbuf)+1)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
255
256 if(0) {
257 size = sizeof(bufw);
258 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
259 ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "HttpQueryInfo(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
260 ok(size == sizeof(bufw), "unexpected size %d\n", size);
261 }
262
263 code = 0xdeadbeef;
264 index = 1;
265 size = sizeof(code);
266 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
267 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
268 "[7] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
269
270 code = 0xdeadbeef;
271 size = sizeof(code);
272 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_REQUEST_HEADERS, &code, &size, NULL);
273 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_INVALID_QUERY_REQUEST,
274 "[8] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
275 }
276
277 #define test_request_flags(a,b) _test_request_flags(__LINE__,a,b,FALSE)
278 #define test_request_flags_todo(a,b) _test_request_flags(__LINE__,a,b,TRUE)
279 static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOOL is_todo)
280 {
281 DWORD flags, size;
282 BOOL res;
283
284 flags = 0xdeadbeef;
285 size = sizeof(flags);
286 res = InternetQueryOptionW(req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &size);
287 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_REQUEST_FLAGS) failed: %u\n", GetLastError());
288
289 /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
290 flags &= ~INTERNET_REQFLAG_CACHE_WRITE_DISABLED;
291 todo_wine_if (is_todo)
292 ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
293 }
294
295 #define test_http_version(a) _test_http_version(__LINE__,a)
296 static void _test_http_version(unsigned line, HINTERNET req)
297 {
298 HTTP_VERSION_INFO v = {0xdeadbeef, 0xdeadbeef};
299 DWORD size;
300 BOOL res;
301
302 size = sizeof(v);
303 res = InternetQueryOptionW(req, INTERNET_OPTION_HTTP_VERSION, &v, &size);
304 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_HTTP_VERSION) failed: %u\n", GetLastError());
305 ok_(__FILE__,line)(v.dwMajorVersion == 1, "dwMajorVersion = %d\n", v.dwMajorVersion);
306 ok_(__FILE__,line)(v.dwMinorVersion == 1, "dwMinorVersion = %d\n", v.dwMinorVersion);
307 }
308
309 static int close_handle_cnt;
310
311 static VOID WINAPI callback(
312 HINTERNET hInternet,
313 DWORD_PTR dwContext,
314 DWORD dwInternetStatus,
315 LPVOID lpvStatusInformation,
316 DWORD dwStatusInformationLength
317 )
318 {
319 CHECK_EXPECT(dwInternetStatus);
320 switch (dwInternetStatus)
321 {
322 case INTERNET_STATUS_RESOLVING_NAME:
323 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
324 GetCurrentThreadId(), hInternet, dwContext,
325 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
326 *(LPSTR)lpvStatusInformation = '\0';
327 break;
328 case INTERNET_STATUS_NAME_RESOLVED:
329 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
330 GetCurrentThreadId(), hInternet, dwContext,
331 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
332 *(LPSTR)lpvStatusInformation = '\0';
333 break;
334 case INTERNET_STATUS_CONNECTING_TO_SERVER:
335 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
336 GetCurrentThreadId(), hInternet, dwContext,
337 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
338 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
339 dwStatusInformationLength);
340 *(LPSTR)lpvStatusInformation = '\0';
341 break;
342 case INTERNET_STATUS_CONNECTED_TO_SERVER:
343 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
344 GetCurrentThreadId(), hInternet, dwContext,
345 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
346 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
347 dwStatusInformationLength);
348 *(LPSTR)lpvStatusInformation = '\0';
349 break;
350 case INTERNET_STATUS_SENDING_REQUEST:
351 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
352 GetCurrentThreadId(), hInternet, dwContext,
353 lpvStatusInformation,dwStatusInformationLength);
354 break;
355 case INTERNET_STATUS_REQUEST_SENT:
356 ok(dwStatusInformationLength == sizeof(DWORD),
357 "info length should be sizeof(DWORD) instead of %d\n",
358 dwStatusInformationLength);
359 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
360 GetCurrentThreadId(), hInternet, dwContext,
361 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
362 break;
363 case INTERNET_STATUS_RECEIVING_RESPONSE:
364 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
365 GetCurrentThreadId(), hInternet, dwContext,
366 lpvStatusInformation,dwStatusInformationLength);
367 break;
368 case INTERNET_STATUS_RESPONSE_RECEIVED:
369 ok(dwStatusInformationLength == sizeof(DWORD),
370 "info length should be sizeof(DWORD) instead of %d\n",
371 dwStatusInformationLength);
372 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
373 GetCurrentThreadId(), hInternet, dwContext,
374 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
375 break;
376 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
377 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
378 GetCurrentThreadId(), hInternet,dwContext,
379 lpvStatusInformation,dwStatusInformationLength);
380 break;
381 case INTERNET_STATUS_PREFETCH:
382 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
383 GetCurrentThreadId(), hInternet, dwContext,
384 lpvStatusInformation,dwStatusInformationLength);
385 break;
386 case INTERNET_STATUS_CLOSING_CONNECTION:
387 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
388 GetCurrentThreadId(), hInternet, dwContext,
389 lpvStatusInformation,dwStatusInformationLength);
390 break;
391 case INTERNET_STATUS_CONNECTION_CLOSED:
392 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
393 GetCurrentThreadId(), hInternet, dwContext,
394 lpvStatusInformation,dwStatusInformationLength);
395 break;
396 case INTERNET_STATUS_HANDLE_CREATED:
397 ok(dwStatusInformationLength == sizeof(HINTERNET),
398 "info length should be sizeof(HINTERNET) instead of %d\n",
399 dwStatusInformationLength);
400 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
401 GetCurrentThreadId(), hInternet, dwContext,
402 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
403 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
404 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
405 break;
406 case INTERNET_STATUS_HANDLE_CLOSING:
407 ok(dwStatusInformationLength == sizeof(HINTERNET),
408 "info length should be sizeof(HINTERNET) instead of %d\n",
409 dwStatusInformationLength);
410 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
411 GetCurrentThreadId(), hInternet, dwContext,
412 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
413 if(!InterlockedDecrement(&close_handle_cnt))
414 SetEvent(hCompleteEvent);
415 break;
416 case INTERNET_STATUS_REQUEST_COMPLETE:
417 {
418 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
419 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
420 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
421 dwStatusInformationLength);
422 ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %ld\n", iar->dwResult);
423 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
424 GetCurrentThreadId(), hInternet, dwContext,
425 iar->dwResult,iar->dwError,dwStatusInformationLength);
426 req_error = iar->dwError;
427 if(!close_handle_cnt)
428 SetEvent(hCompleteEvent);
429 break;
430 }
431 case INTERNET_STATUS_REDIRECT:
432 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
433 GetCurrentThreadId(), hInternet, dwContext,
434 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
435 *(LPSTR)lpvStatusInformation = '\0';
436 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
437 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
438 break;
439 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
440 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
441 GetCurrentThreadId(), hInternet, dwContext,
442 lpvStatusInformation, dwStatusInformationLength);
443 break;
444 default:
445 trace("%04x:Callback %p 0x%lx %d %p %d\n",
446 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
447 lpvStatusInformation, dwStatusInformationLength);
448 }
449 }
450
451 typedef struct {
452 HINTERNET session;
453 HINTERNET connection;
454 HINTERNET request;
455 } test_request_t;
456
457 #define open_simple_request(a,b,c,d,e) _open_simple_request(__LINE__,a,b,c,d,e)
458 static void _open_simple_request(unsigned line, test_request_t *req, const char *host,
459 int port, const char *verb, const char *url)
460 {
461 req->session = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
462 ok_(__FILE__,line)(req->session != NULL, "InternetOpenA failed: %u\n", GetLastError());
463
464 req->connection = InternetConnectA(req->session, host, port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
465 ok_(__FILE__,line)(req->connection != NULL, "InternetConnectA failed: %u\n", GetLastError());
466
467 req->request = HttpOpenRequestA(req->connection, verb, url, NULL, NULL, NULL, 0, 0);
468 ok_(__FILE__,line)(req->request != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
469 }
470
471 #define close_request(a) _close_request(__LINE__,a)
472 static void _close_request(unsigned line, test_request_t *req)
473 {
474 BOOL ret;
475
476 ret = InternetCloseHandle(req->request);
477 ok_(__FILE__,line)(ret, "InternetCloseHandle(request) failed: %u\n", GetLastError());
478 ret = InternetCloseHandle(req->connection);
479 ok_(__FILE__,line)(ret, "InternetCloseHandle(connection) failed: %u\n", GetLastError());
480 ret = InternetCloseHandle(req->session);
481 ok_(__FILE__,line)(ret, "InternetCloseHandle(session) failed: %u\n", GetLastError());
482 }
483
484 #define receive_simple_request(a,b,c) _receive_simple_request(__LINE__,a,b,c)
485 static DWORD _receive_simple_request(unsigned line, HINTERNET req, char *buf, size_t buf_size)
486 {
487 DWORD read = 0;
488 BOOL ret;
489
490 ret = InternetReadFile(req, buf, buf_size, &read);
491 ok_(__FILE__,line)(ret, "InternetReadFile failed: %u\n", GetLastError());
492
493 return read;
494 }
495
496 static void close_async_handle(HINTERNET handle, HANDLE complete_event, int handle_cnt)
497 {
498 BOOL res;
499
500 close_handle_cnt = handle_cnt;
501
502 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
503 res = InternetCloseHandle(handle);
504 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
505 WaitForSingleObject(hCompleteEvent, INFINITE);
506 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
507 }
508
509 static void InternetReadFile_test(int flags, const test_data_t *test)
510 {
511 char *post_data = NULL;
512 BOOL res, on_async = TRUE;
513 CHAR buffer[4000];
514 WCHAR wbuffer[4000];
515 DWORD length, length2, index, exlen = 0, post_len = 0;
516 const char *types[2] = { "*", NULL };
517 HINTERNET hi, hic = 0, hor = 0;
518
519 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
520
521 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
522
523 trace("InternetOpenA <--\n");
524 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
525 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
526 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
527 trace("InternetOpenA -->\n");
528
529 if (hi == 0x0) goto abort;
530
531 pInternetSetStatusCallbackA(hi,&callback);
532
533 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
534
535 trace("InternetConnectA <--\n");
536 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
537 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
538 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
539 trace("InternetConnectA -->\n");
540
541 if (hic == 0x0) goto abort;
542
543 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
544 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
545
546 trace("HttpOpenRequestA <--\n");
547 hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
548 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
549 0xdeadbead);
550 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
551 /*
552 * If the internet name can't be resolved we are probably behind
553 * a firewall or in some other way not directly connected to the
554 * Internet. Not enough reason to fail the test. Just ignore and
555 * abort.
556 */
557 } else {
558 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
559 }
560 trace("HttpOpenRequestA -->\n");
561
562 if (hor == 0x0) goto abort;
563
564 test_request_flags(hor, INTERNET_REQFLAG_NO_HEADERS);
565
566 length = sizeof(buffer);
567 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
568 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
569 ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
570
571 length = sizeof(buffer);
572 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
573 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
574 ok(length == 0 || (length == 1 && !*buffer) /* win10 */, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
575 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
576
577 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
578 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
579 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
580 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT,2);
581 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_RECEIVED,2);
582 if (first_connection_to_test_url)
583 {
584 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
585 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
586 }
587 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
588 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
589 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
590 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
591 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
592 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
593 if(test->flags & TESTF_REDIRECT) {
594 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
595 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
596 }
597 SET_EXPECT(INTERNET_STATUS_REDIRECT);
598 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
599 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
600 if (flags & INTERNET_FLAG_ASYNC)
601 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
602
603 if(test->flags & TESTF_COMPRESSED) {
604 BOOL b = TRUE;
605
606 res = InternetSetOptionA(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
607 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
608 "InternetSetOption failed: %u\n", GetLastError());
609 if(!res)
610 goto abort;
611 }
612
613 test_status_code(hor, 0);
614
615 trace("HttpSendRequestA -->\n");
616 if(test->post_data) {
617 post_len = strlen(test->post_data);
618 post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
619 memcpy(post_data, test->post_data, post_len);
620 }
621 SetLastError(0xdeadbeef);
622 res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
623 if (flags & INTERNET_FLAG_ASYNC)
624 ok(!res && (GetLastError() == ERROR_IO_PENDING),
625 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
626 else
627 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
628 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
629 trace("HttpSendRequestA <--\n");
630
631 if (flags & INTERNET_FLAG_ASYNC) {
632 WaitForSingleObject(hCompleteEvent, INFINITE);
633 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
634 }
635 HeapFree(GetProcessHeap(), 0, post_data);
636
637 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
638 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
639 if (first_connection_to_test_url)
640 {
641 if (! proxy_active())
642 {
643 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
644 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
645 }
646 else
647 {
648 CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
649 CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
650 }
651 }
652 else
653 {
654 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
655 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
656 }
657 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
658 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
659 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
660 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
661 if(test->flags & TESTF_REDIRECT)
662 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
663 if (flags & INTERNET_FLAG_ASYNC)
664 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
665 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
666 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
667 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
668
669 test_request_flags(hor, 0);
670
671 length = 100;
672 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
673 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
674
675 length = sizeof(buffer)-1;
676 memset(buffer, 0x77, sizeof(buffer));
677 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
678 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
679 /* show that the function writes data past the length returned */
680 ok(buffer[length-2], "Expected any header character, got 0x00\n");
681 ok(!buffer[length-1], "Expected 0x00, got %02X\n", buffer[length-1]);
682 ok(!buffer[length], "Expected 0x00, got %02X\n", buffer[length]);
683 ok(buffer[length+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length+1]);
684
685 length2 = length;
686 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
687 ok(!res, "Expected 0x00, got %d\n", res);
688 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
689 ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
690 /* the in length of the buffer must be +1 but the length returned does not count this */
691 length2 = length+1;
692 memset(buffer, 0x77, sizeof(buffer));
693 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
694 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
695 ok(buffer[length2] == 0x00, "Expected 0x00, got %02X\n", buffer[length2]);
696 ok(buffer[length2+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length2+1]);
697 ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
698
699 length = sizeof(wbuffer)-sizeof(WCHAR);
700 memset(wbuffer, 0x77, sizeof(wbuffer));
701 res = HttpQueryInfoW(hor, HTTP_QUERY_RAW_HEADERS, wbuffer, &length, 0x0);
702 ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
703 ok(length % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length);
704 length /= sizeof(WCHAR);
705 /* show that the function writes data past the length returned */
706 ok(wbuffer[length-2], "Expected any header character, got 0x0000\n");
707 ok(!wbuffer[length-1], "Expected 0x0000, got %04X\n", wbuffer[length-1]);
708 ok(!wbuffer[length], "Expected 0x0000, got %04X\n", wbuffer[length]);
709 ok(wbuffer[length+1] == 0x7777 || broken(wbuffer[length+1] != 0x7777),
710 "Expected 0x7777, got %04X\n", wbuffer[length+1]);
711
712 length2 = length*sizeof(WCHAR);
713 res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
714 ok(!res, "Expected 0x00, got %d\n", res);
715 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
716 ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
717 length2 /= sizeof(WCHAR);
718 ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
719 /* the in length of the buffer must be +1 but the length returned does not count this */
720 length2 = (length+1)*sizeof(WCHAR);
721 memset(wbuffer, 0x77, sizeof(wbuffer));
722 res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
723 ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
724 ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
725 length2 /= sizeof(WCHAR);
726 ok(!wbuffer[length2], "Expected 0x0000, got %04X\n", wbuffer[length2]);
727 ok(wbuffer[length2+1] == 0x7777, "Expected 0x7777, got %04X\n", wbuffer[length2+1]);
728 ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
729
730 length = sizeof(buffer);
731 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
732 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
733 ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
734
735 index = 0;
736 length = 0;
737 SetLastError(0xdeadbeef);
738 ok(HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,NULL,&length,&index) == FALSE,"Query worked\n");
739 if(test->flags & TESTF_COMPRESSED)
740 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
741 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
742 ok(index == 0, "Index was incremented\n");
743
744 index = 0;
745 length = 16;
746 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,&index);
747 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
748 if(test->flags & TESTF_COMPRESSED)
749 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
750 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
751 ok(!res || index == 1, "Index was not incremented although result is %x (index = %u)\n", res, index);
752
753 length = 100;
754 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
755 buffer[length]=0;
756 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
757
758 length = 100;
759 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
760 buffer[length]=0;
761 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
762
763 SetLastError(0xdeadbeef);
764 res = InternetReadFile(NULL, buffer, 100, &length);
765 ok(!res, "InternetReadFile should have failed\n");
766 ok(GetLastError() == ERROR_INVALID_HANDLE,
767 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
768 GetLastError());
769
770 length = 100;
771 trace("Entering Query loop\n");
772
773 while (TRUE)
774 {
775 if (flags & INTERNET_FLAG_ASYNC)
776 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
777
778 /* IE11 calls those in InternetQueryDataAvailable call. */
779 SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
780 SET_OPTIONAL(INTERNET_STATUS_RESPONSE_RECEIVED);
781
782 length = 0;
783 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
784
785 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
786
787 if (flags & INTERNET_FLAG_ASYNC)
788 {
789 if (res)
790 {
791 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
792 if(exlen) {
793 ok(length >= exlen, "length %u < exlen %u\n", length, exlen);
794 exlen = 0;
795 }
796 }
797 else if (GetLastError() == ERROR_IO_PENDING)
798 {
799 trace("PENDING\n");
800 /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
801 if(!(test->flags & TESTF_CHUNKED))
802 ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
803 WaitForSingleObject(hCompleteEvent, INFINITE);
804 exlen = length;
805 ok(exlen, "length = 0\n");
806 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
807 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
808 ok(req_error, "req_error = 0\n");
809 continue;
810 }else {
811 ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
812 }
813 }else {
814 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
815 }
816 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
817
818 trace("LENGTH %d\n", length);
819 if(test->flags & TESTF_CHUNKED)
820 ok(length <= 8192, "length = %d, expected <= 8192\n", length);
821 if (length)
822 {
823 char *buffer;
824 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
825
826 res = InternetReadFile(hor,buffer,length,&length);
827
828 buffer[length]=0;
829
830 trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
831
832 if(test->content)
833 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
834 HeapFree(GetProcessHeap(),0,buffer);
835 }else {
836 ok(!on_async, "Returned zero size in response to request complete\n");
837 break;
838 }
839 on_async = FALSE;
840 }
841 if(test->flags & TESTF_REDIRECT) {
842 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
843 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
844 }
845 abort:
846 trace("aborting\n");
847 close_async_handle(hi, hCompleteEvent, 2);
848 CloseHandle(hCompleteEvent);
849 first_connection_to_test_url = FALSE;
850 }
851
852 static void InternetReadFile_chunked_test(void)
853 {
854 BOOL res;
855 CHAR buffer[4000];
856 DWORD length, got;
857 const char *types[2] = { "*", NULL };
858 HINTERNET hi, hic = 0, hor = 0;
859
860 trace("Starting InternetReadFile chunked test\n");
861
862 trace("InternetOpenA <--\n");
863 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
864 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
865 trace("InternetOpenA -->\n");
866
867 if (hi == 0x0) goto abort;
868
869 trace("InternetConnectA <--\n");
870 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
871 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
872 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
873 trace("InternetConnectA -->\n");
874
875 if (hic == 0x0) goto abort;
876
877 trace("HttpOpenRequestA <--\n");
878 hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
879 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
880 0xdeadbead);
881 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
882 /*
883 * If the internet name can't be resolved we are probably behind
884 * a firewall or in some other way not directly connected to the
885 * Internet. Not enough reason to fail the test. Just ignore and
886 * abort.
887 */
888 } else {
889 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
890 }
891 trace("HttpOpenRequestA -->\n");
892
893 if (hor == 0x0) goto abort;
894
895 trace("HttpSendRequestA -->\n");
896 SetLastError(0xdeadbeef);
897 res = HttpSendRequestA(hor, "", -1, NULL, 0);
898 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
899 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
900 trace("HttpSendRequestA <--\n");
901
902 test_request_flags(hor, 0);
903
904 length = 100;
905 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
906 buffer[length]=0;
907 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
908
909 SetLastError( 0xdeadbeef );
910 length = 100;
911 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
912 buffer[length]=0;
913 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
914 ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
915 "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
916 ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
917 "Wrong transfer encoding '%s'\n", buffer );
918
919 SetLastError( 0xdeadbeef );
920 length = 16;
921 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
922 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
923 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
924
925 length = 100;
926 trace("Entering Query loop\n");
927
928 while (TRUE)
929 {
930 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
931 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
932 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
933 trace("got %u available\n",length);
934 if (length)
935 {
936 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
937
938 res = InternetReadFile(hor,buffer,length,&got);
939
940 buffer[got]=0;
941 trace("ReadFile -> %i %i\n",res,got);
942 ok( length == got, "only got %u of %u available\n", got, length );
943 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
944
945 HeapFree(GetProcessHeap(),0,buffer);
946 if (!got) break;
947 }
948 if (length == 0)
949 {
950 got = 0xdeadbeef;
951 res = InternetReadFile( hor, buffer, 1, &got );
952 ok( res, "InternetReadFile failed: %u\n", GetLastError() );
953 ok( !got, "got %u\n", got );
954 break;
955 }
956 }
957 abort:
958 trace("aborting\n");
959 if (hor != 0x0) {
960 res = InternetCloseHandle(hor);
961 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
962 }
963 if (hi != 0x0) {
964 res = InternetCloseHandle(hi);
965 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
966 }
967 }
968
969 static void InternetReadFileExA_test(int flags)
970 {
971 DWORD rc;
972 DWORD length;
973 const char *types[2] = { "*", NULL };
974 HINTERNET hi, hic = 0, hor = 0;
975 INTERNET_BUFFERSA inetbuffers;
976
977 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
978
979 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
980
981 trace("InternetOpenA <--\n");
982 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
983 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
984 trace("InternetOpenA -->\n");
985
986 if (hi == 0x0) goto abort;
987
988 pInternetSetStatusCallbackA(hi,&callback);
989
990 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
991
992 trace("InternetConnectA <--\n");
993 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
994 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
995 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
996 trace("InternetConnectA -->\n");
997
998 if (hic == 0x0) goto abort;
999
1000 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1001 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1002
1003 trace("HttpOpenRequestA <--\n");
1004 hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
1005 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
1006 0xdeadbead);
1007 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
1008 /*
1009 * If the internet name can't be resolved we are probably behind
1010 * a firewall or in some other way not directly connected to the
1011 * Internet. Not enough reason to fail the test. Just ignore and
1012 * abort.
1013 */
1014 } else {
1015 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
1016 }
1017 trace("HttpOpenRequestA -->\n");
1018
1019 if (hor == 0x0) goto abort;
1020
1021 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1022 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1023 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1024 if (first_connection_to_test_url)
1025 {
1026 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
1027 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
1028 }
1029 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT, 2);
1030 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
1031 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
1032 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
1033 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
1034 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
1035 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
1036 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
1037 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
1038 SET_EXPECT(INTERNET_STATUS_REDIRECT);
1039 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
1040 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
1041 if (flags & INTERNET_FLAG_ASYNC)
1042 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1043 else
1044 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1045
1046 trace("HttpSendRequestA -->\n");
1047 SetLastError(0xdeadbeef);
1048 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
1049 if (flags & INTERNET_FLAG_ASYNC)
1050 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
1051 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
1052 else
1053 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
1054 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
1055 trace("HttpSendRequestA <--\n");
1056
1057 if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
1058 WaitForSingleObject(hCompleteEvent, INFINITE);
1059 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1060 }
1061
1062 if (first_connection_to_test_url)
1063 {
1064 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1065 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1066 }
1067 else
1068 {
1069 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1070 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1071 }
1072 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
1073 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
1074 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
1075 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
1076 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
1077 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
1078 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
1079 if (flags & INTERNET_FLAG_ASYNC)
1080 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1081 else
1082 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1083 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
1084 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
1085 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1086 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1087
1088 if(is_ie7plus) {
1089 rc = InternetReadFileExW(hor, NULL, 0, 0xdeadcafe);
1090 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1091 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1092 rc ? "TRUE" : "FALSE", GetLastError());
1093 }
1094
1095 /* tests invalid dwStructSize */
1096 inetbuffers.dwStructSize = sizeof(inetbuffers)+1;
1097 inetbuffers.lpcszHeader = NULL;
1098 inetbuffers.dwHeadersLength = 0;
1099 inetbuffers.dwBufferLength = 10;
1100 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
1101 inetbuffers.dwOffsetHigh = 1234;
1102 inetbuffers.dwOffsetLow = 5678;
1103 rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1104 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1105 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1106 rc ? "TRUE" : "FALSE", GetLastError());
1107 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1108
1109 test_request_flags(hor, 0);
1110
1111 /* tests to see whether lpcszHeader is used - it isn't */
1112 inetbuffers.dwStructSize = sizeof(inetbuffers);
1113 inetbuffers.lpcszHeader = (LPCSTR)0xdeadbeef;
1114 inetbuffers.dwHeadersLength = 255;
1115 inetbuffers.dwBufferLength = 0;
1116 inetbuffers.lpvBuffer = NULL;
1117 inetbuffers.dwOffsetHigh = 1234;
1118 inetbuffers.dwOffsetLow = 5678;
1119 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
1120 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
1121 rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1122 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
1123 trace("read %i bytes\n", inetbuffers.dwBufferLength);
1124 todo_wine
1125 {
1126 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1127 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1128 }
1129
1130 rc = InternetReadFileExA(NULL, &inetbuffers, 0, 0xdeadcafe);
1131 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
1132 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
1133 rc ? "TRUE" : "FALSE", GetLastError());
1134
1135 length = 0;
1136 trace("Entering Query loop\n");
1137
1138 while (TRUE)
1139 {
1140 inetbuffers.dwStructSize = sizeof(inetbuffers);
1141 inetbuffers.dwBufferLength = 1024;
1142 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
1143 inetbuffers.dwOffsetHigh = 1234;
1144 inetbuffers.dwOffsetLow = 5678;
1145
1146 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1147 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1148 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1149 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
1150 if (!rc)
1151 {
1152 if (GetLastError() == ERROR_IO_PENDING)
1153 {
1154 trace("InternetReadFileEx -> PENDING\n");
1155 ok(flags & INTERNET_FLAG_ASYNC,
1156 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
1157 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1158 WaitForSingleObject(hCompleteEvent, INFINITE);
1159 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1160 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1161 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1162 }
1163 else
1164 {
1165 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
1166 break;
1167 }
1168 }
1169 else
1170 {
1171 trace("InternetReadFileEx -> SUCCEEDED\n");
1172 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1173 if (inetbuffers.dwBufferLength)
1174 {
1175 todo_wine {
1176 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1177 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1178 }
1179 }
1180 else
1181 {
1182 /* Win98 still sends these when 0 bytes are read, WinXP does not */
1183 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1184 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1185 }
1186 }
1187
1188 trace("read %i bytes\n", inetbuffers.dwBufferLength);
1189 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
1190
1191 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
1192 "InternetReadFileEx sets offsets to 0x%x%08x\n",
1193 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
1194
1195 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1196
1197 if (!inetbuffers.dwBufferLength)
1198 break;
1199
1200 length += inetbuffers.dwBufferLength;
1201 }
1202 ok(length > 0, "failed to read any of the document\n");
1203 trace("Finished. Read %d bytes\n", length);
1204
1205 abort:
1206 close_async_handle(hi, hCompleteEvent, 2);
1207 CloseHandle(hCompleteEvent);
1208 first_connection_to_test_url = FALSE;
1209 }
1210
1211 static void InternetOpenUrlA_test(void)
1212 {
1213 HINTERNET myhinternet, myhttp;
1214 char buffer[0x400];
1215 DWORD size, readbytes, totalbytes=0;
1216 BOOL ret;
1217
1218 ret = DeleteUrlCacheEntryA(TEST_URL);
1219 ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND,
1220 "DeleteUrlCacheEntry returned %x, GetLastError() = %d\n", ret, GetLastError());
1221
1222 myhinternet = InternetOpenA("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1223 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
1224 size = 0x400;
1225 ret = InternetCanonicalizeUrlA(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
1226 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1227
1228 SetLastError(0);
1229 myhttp = InternetOpenUrlA(myhinternet, TEST_URL, 0, 0,
1230 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1231 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1232 return; /* WinXP returns this when not connected to the net */
1233 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1234 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1235 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1236 totalbytes += readbytes;
1237 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1238 totalbytes += readbytes;
1239 trace("read 0x%08x bytes\n",totalbytes);
1240
1241 InternetCloseHandle(myhttp);
1242 InternetCloseHandle(myhinternet);
1243
1244 ret = DeleteUrlCacheEntryA(TEST_URL);
1245 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "INTERNET_FLAG_NO_CACHE_WRITE flag doesn't work\n");
1246 }
1247
1248 static void HttpSendRequestEx_test(void)
1249 {
1250 HINTERNET hSession;
1251 HINTERNET hConnect;
1252 HINTERNET hRequest;
1253
1254 INTERNET_BUFFERSA BufferIn;
1255 DWORD dwBytesWritten, dwBytesRead, error;
1256 CHAR szBuffer[256];
1257 int i;
1258 BOOL ret;
1259
1260 static char szPostData[] = "mode=Test";
1261 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1262
1263 hSession = InternetOpenA("Wine Regression Test",
1264 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1265 ok( hSession != NULL ,"Unable to open Internet session\n");
1266 hConnect = InternetConnectA(hSession, "test.winehq.org",
1267 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1268 0);
1269 ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1270 hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1271 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1272 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1273 {
1274 skip( "Network unreachable, skipping test\n" );
1275 goto done;
1276 }
1277 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1278
1279 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1280
1281 BufferIn.dwStructSize = sizeof(BufferIn);
1282 BufferIn.Next = (INTERNET_BUFFERSA*)0xdeadcab;
1283 BufferIn.lpcszHeader = szContentType;
1284 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1285 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1286 BufferIn.lpvBuffer = szPostData;
1287 BufferIn.dwBufferLength = 3;
1288 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1289 BufferIn.dwOffsetLow = 0;
1290 BufferIn.dwOffsetHigh = 0;
1291
1292 SetLastError(0xdeadbeef);
1293 ret = HttpSendRequestExA(hRequest, &BufferIn, NULL, 0 ,0);
1294 error = GetLastError();
1295 ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1296 ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1297
1298 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1299
1300 for (i = 3; szPostData[i]; i++)
1301 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1302 "InternetWriteFile failed\n");
1303
1304 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1305
1306 ok(HttpEndRequestA(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1307
1308 test_request_flags(hRequest, 0);
1309
1310 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1311 "Unable to read response\n");
1312 szBuffer[dwBytesRead] = 0;
1313
1314 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1315 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1316
1317 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1318 done:
1319 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1320 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1321 }
1322
1323 static void InternetOpenRequest_test(void)
1324 {
1325 HINTERNET session, connect, request;
1326 static const char *types[] = { "*", "", NULL };
1327 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1328 static const WCHAR *typesW[] = { any, empty, NULL };
1329 BOOL ret;
1330
1331 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1332 ok(session != NULL ,"Unable to open Internet session\n");
1333
1334 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1335 INTERNET_SERVICE_HTTP, 0, 0);
1336 ok(connect == NULL, "InternetConnectA should have failed\n");
1337 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1338
1339 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1340 INTERNET_SERVICE_HTTP, 0, 0);
1341 ok(connect == NULL, "InternetConnectA should have failed\n");
1342 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1343
1344 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1345 INTERNET_SERVICE_HTTP, 0, 0);
1346 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1347
1348 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1349 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1350 {
1351 skip( "Network unreachable, skipping test\n" );
1352 goto done;
1353 }
1354 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1355
1356 ret = HttpSendRequestW(request, NULL, 0, NULL, 0);
1357 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1358 ok(InternetCloseHandle(request), "Close request handle failed\n");
1359
1360 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1361 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1362
1363 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1364 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1365 ok(InternetCloseHandle(request), "Close request handle failed\n");
1366
1367 done:
1368 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1369 ok(InternetCloseHandle(session), "Close session handle failed\n");
1370 }
1371
1372 static void test_cache_read(void)
1373 {
1374 HINTERNET session, connection, req;
1375 FILETIME now, tomorrow, yesterday;
1376 BYTE content[1000], buf[2000];
1377 char file_path[MAX_PATH];
1378 ULARGE_INTEGER li;
1379 HANDLE file;
1380 DWORD size;
1381 unsigned i;
1382 BOOL res;
1383
1384 static const char cache_only_url[] = "http://test.winehq.org/tests/cache-only";
1385 BYTE cache_headers[] = "HTTP/1.1 200 OK\r\n\r\n";
1386
1387 trace("Testing cache read...\n");
1388
1389 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1390
1391 for(i = 0; i < sizeof(content); i++)
1392 content[i] = '0' + (i%10);
1393
1394 GetSystemTimeAsFileTime(&now);
1395 li.u.HighPart = now.dwHighDateTime;
1396 li.u.LowPart = now.dwLowDateTime;
1397 li.QuadPart += (LONGLONG)10000000 * 3600 * 24;
1398 tomorrow.dwHighDateTime = li.u.HighPart;
1399 tomorrow.dwLowDateTime = li.u.LowPart;
1400 li.QuadPart -= (LONGLONG)10000000 * 3600 * 24 * 2;
1401 yesterday.dwHighDateTime = li.u.HighPart;
1402 yesterday.dwLowDateTime = li.u.LowPart;
1403
1404 res = CreateUrlCacheEntryA(cache_only_url, sizeof(content), "", file_path, 0);
1405 ok(res, "CreateUrlCacheEntryA failed: %u\n", GetLastError());
1406
1407 file = CreateFileA(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1408 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1409
1410 WriteFile(file, content, sizeof(content), &size, NULL);
1411 CloseHandle(file);
1412
1413 res = CommitUrlCacheEntryA(cache_only_url, file_path, tomorrow, yesterday, NORMAL_CACHE_ENTRY,
1414 cache_headers, sizeof(cache_headers)-1, "", 0);
1415 ok(res, "CommitUrlCacheEntryA failed: %u\n", GetLastError());
1416
1417 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
1418 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
1419
1420 pInternetSetStatusCallbackA(session, callback);
1421
1422 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1423 connection = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT,
1424 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1425 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
1426 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1427
1428 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1429 req = HttpOpenRequestA(connection, "GET", "/tests/cache-only", NULL, NULL, NULL, 0, 0xdeadbead);
1430 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1431 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1432
1433 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
1434 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
1435 SET_WINE_ALLOW(INTERNET_STATUS_SENDING_REQUEST);
1436 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_SENT);
1437 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1438 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1439 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1440
1441 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
1442 todo_wine
1443 ok(res, "HttpSendRequest failed: %u\n", GetLastError());
1444
1445 if(res) {
1446 size = 0;
1447 res = InternetQueryDataAvailable(req, &size, 0, 0);
1448 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
1449 ok(size == sizeof(content), "size = %u\n", size);
1450
1451 size = sizeof(buf);
1452 res = InternetReadFile(req, buf, sizeof(buf), &size);
1453 ok(res, "InternetReadFile failed: %u\n", GetLastError());
1454 ok(size == sizeof(content), "size = %u\n", size);
1455 ok(!memcmp(content, buf, sizeof(content)), "unexpected content\n");
1456 }
1457
1458 close_async_handle(session, hCompleteEvent, 2);
1459
1460 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1461 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1462 CLEAR_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
1463 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
1464 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1465 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1466 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1467
1468 res = DeleteUrlCacheEntryA(cache_only_url);
1469 ok(res, "DeleteUrlCacheEntryA failed: %u\n", GetLastError());
1470
1471 CloseHandle(hCompleteEvent);
1472 }
1473
1474 static void test_http_cache(void)
1475 {
1476 HINTERNET session, connect, request;
1477 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1478 DWORD size, file_size;
1479 BYTE buf[100];
1480 HANDLE file;
1481 BOOL ret;
1482 FILETIME filetime_zero = {0};
1483
1484 static const char cached_content[] = "data read from cache";
1485 static const char *types[] = { "*", "", NULL };
1486
1487 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1488 ok(session != NULL ,"Unable to open Internet session\n");
1489
1490 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1491 INTERNET_SERVICE_HTTP, 0, 0);
1492 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1493
1494 request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1495 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1496 {
1497 skip( "Network unreachable, skipping test\n" );
1498
1499 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1500 ok(InternetCloseHandle(session), "Close session handle failed\n");
1501
1502 return;
1503 }
1504 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1505
1506 size = sizeof(url);
1507 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1508 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1509 ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1510
1511 size = sizeof(file_name);
1512 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1513 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1514 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1515 ok(!size, "size = %d\n", size);
1516
1517 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1518 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1519
1520 size = sizeof(file_name);
1521 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1522 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1523
1524 file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1525 FILE_ATTRIBUTE_NORMAL, NULL);
1526 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1527 file_size = GetFileSize(file, NULL);
1528 ok(file_size == 106, "file size = %u\n", file_size);
1529
1530 size = sizeof(buf);
1531 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1532 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1533 ok(size == 100, "size = %u\n", size);
1534
1535 file_size = GetFileSize(file, NULL);
1536 ok(file_size == 106, "file size = %u\n", file_size);
1537 CloseHandle(file);
1538
1539 ret = DeleteFileA(file_name);
1540 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1541
1542 ok(InternetCloseHandle(request), "Close request handle failed\n");
1543
1544 file = CreateFileA(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1545 FILE_ATTRIBUTE_NORMAL, NULL);
1546 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1547 ret = WriteFile(file, cached_content, sizeof(cached_content), &size, NULL);
1548 ok(ret && size, "WriteFile failed: %d, %d\n", ret, size);
1549 ret = CommitUrlCacheEntryA(url, file_name, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, NULL, 0);
1550 ok(ret, "CommitUrlCacheEntry failed: %d\n", GetLastError());
1551 CloseHandle(file);
1552
1553 /* Send the same request, requiring it to be retrieved from the cache */
1554 request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1555
1556 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1557 ok(ret, "HttpSendRequest failed\n");
1558
1559 size = sizeof(buf);
1560 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1561 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1562 ok(size == 100, "size = %u\n", size);
1563 buf[99] = 0;
1564 todo_wine ok(!strcmp((char*)buf, cached_content), "incorrect page data: %s\n", (char*)buf);
1565
1566 ok(InternetCloseHandle(request), "Close request handle failed\n");
1567
1568 DeleteUrlCacheEntryA(url);
1569 request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1570 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1571 todo_wine ok(!ret, "HttpSendRequest succeeded\n");
1572 if(!ret)
1573 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() = %d\n", GetLastError());
1574 ok(InternetCloseHandle(request), "Close request handle failed\n");
1575
1576 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1577 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1578
1579 size = sizeof(file_name);
1580 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1581 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1582 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1583 ok(!size, "size = %d\n", size);
1584
1585 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1586 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1587
1588 size = sizeof(file_name);
1589 file_name[0] = 0;
1590 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1591 if (ret)
1592 {
1593 file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1594 FILE_ATTRIBUTE_NORMAL, NULL);
1595 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1596 CloseHandle(file);
1597 }
1598 else
1599 {
1600 /* < IE8 */
1601 ok(file_name[0] == 0, "Didn't expect a file name\n");
1602 }
1603
1604 ok(InternetCloseHandle(request), "Close request handle failed\n");
1605 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1606 ok(InternetCloseHandle(session), "Close session handle failed\n");
1607
1608 test_cache_read();
1609 }
1610
1611 static void InternetLockRequestFile_test(void)
1612 {
1613 char file_name[MAX_PATH];
1614 test_request_t req;
1615 HANDLE lock, lock2;
1616 DWORD size;
1617 BOOL ret;
1618
1619 open_simple_request(&req, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, "/tests/hello.html");
1620
1621 size = sizeof(file_name);
1622 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1623 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1624 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1625 ok(!size, "size = %d\n", size);
1626
1627 lock = NULL;
1628 ret = InternetLockRequestFile(req.request, &lock);
1629 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1630
1631 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
1632 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1633
1634 size = sizeof(file_name);
1635 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1636 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1637
1638 ret = InternetLockRequestFile(req.request, &lock);
1639 ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1640 ok(lock != NULL, "lock == NULL\n");
1641
1642 ret = InternetLockRequestFile(req.request, &lock2);
1643 ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1644 ok(lock == lock2, "lock != lock2\n");
1645
1646 ret = InternetUnlockRequestFile(lock2);
1647 ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1648
1649 ret = DeleteFileA(file_name);
1650 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1651
1652 ok(InternetCloseHandle(req.request), "Close request handle failed\n");
1653
1654 ret = DeleteFileA(file_name);
1655 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1656
1657 ret = InternetUnlockRequestFile(lock);
1658 ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1659
1660 ret = DeleteFileA(file_name);
1661 ok(ret, "Deleting file returned %x(%u)\n", ret, GetLastError());
1662 }
1663
1664 static void HttpHeaders_test(void)
1665 {
1666 HINTERNET hSession;
1667 HINTERNET hConnect;
1668 HINTERNET hRequest;
1669 CHAR buffer[256];
1670 WCHAR wbuffer[256];
1671 DWORD len = 256;
1672 DWORD oldlen;
1673 DWORD index = 0;
1674 BOOL ret;
1675
1676 hSession = InternetOpenA("Wine Regression Test",
1677 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1678 ok( hSession != NULL ,"Unable to open Internet session\n");
1679 hConnect = InternetConnectA(hSession, "test.winehq.org",
1680 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1681 0);
1682 ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1683 hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1684 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1685 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1686 {
1687 skip( "Network unreachable, skipping test\n" );
1688 goto done;
1689 }
1690 ok( hRequest != NULL, "Failed to open request handle\n");
1691
1692 index = 0;
1693 len = sizeof(buffer);
1694 strcpy(buffer,"Warning");
1695 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1696 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1697
1698 ok(HttpAddRequestHeadersA(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1699 "Failed to add new header\n");
1700
1701 index = 0;
1702 len = sizeof(buffer);
1703 strcpy(buffer,"Warning");
1704 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1705 buffer,&len,&index),"Unable to query header\n");
1706 ok(index == 1, "Index was not incremented\n");
1707 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1708 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1709 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1710 len = sizeof(buffer);
1711 strcpy(buffer,"Warning");
1712 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1713 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1714
1715 index = 0;
1716 len = 5; /* could store the string but not the NULL terminator */
1717 strcpy(buffer,"Warning");
1718 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1719 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1720 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1721 ok(index == 0, "Index was incremented\n");
1722 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1723 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1724
1725 /* a call with NULL will fail but will return the length */
1726 index = 0;
1727 len = sizeof(buffer);
1728 SetLastError(0xdeadbeef);
1729 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1730 NULL,&len,&index) == FALSE,"Query worked\n");
1731 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1732 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1733 ok(index == 0, "Index was incremented\n");
1734
1735 /* even for a len that is too small */
1736 index = 0;
1737 len = 15;
1738 SetLastError(0xdeadbeef);
1739 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1740 NULL,&len,&index) == FALSE,"Query worked\n");
1741 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1742 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1743 ok(index == 0, "Index was incremented\n");
1744
1745 index = 0;
1746 len = 0;
1747 SetLastError(0xdeadbeef);
1748 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1749 NULL,&len,&index) == FALSE,"Query worked\n");
1750 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1751 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1752 ok(index == 0, "Index was incremented\n");
1753 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1754
1755
1756 /* a working query */
1757 index = 0;
1758 len = sizeof(buffer);
1759 memset(buffer, 'x', sizeof(buffer));
1760 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1761 buffer,&len,&index),"Unable to query header\n");
1762 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1763 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1764 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1765 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1766 ok(strncmp(buffer, "POST /tests/post.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1767 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1768 ok(index == 0, "Index was incremented\n");
1769
1770 /* Like above two tests, but for W version */
1771
1772 index = 0;
1773 len = 0;
1774 SetLastError(0xdeadbeef);
1775 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1776 NULL,&len,&index) == FALSE,"Query worked\n");
1777 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1778 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1779 ok(index == 0, "Index was incremented\n");
1780 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1781
1782 /* a working query */
1783 index = 0;
1784 len = sizeof(wbuffer);
1785 memset(wbuffer, 'x', sizeof(wbuffer));
1786 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1787 wbuffer,&len,&index),"Unable to query header\n");
1788 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1789 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1790 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1791 ok(index == 0, "Index was incremented\n");
1792
1793 /* end of W version tests */
1794
1795 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1796 index = 0;
1797 len = sizeof(buffer);
1798 memset(buffer, 'x', sizeof(buffer));
1799 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1800 buffer,&len,&index) == TRUE,"Query failed\n");
1801 ok(len == 2 || len == 4 /* win10 */, "Expected 2 or 4, got %d\n", len);
1802 ok(memcmp(buffer, "\r\n\r\n", len) == 0, "Expected CRLF, got '%s'\n", buffer);
1803 ok(index == 0, "Index was incremented\n");
1804
1805 ok(HttpAddRequestHeadersA(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1806 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1807
1808 index = 0;
1809 len = sizeof(buffer);
1810 strcpy(buffer,"Warning");
1811 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1812 buffer,&len,&index),"Unable to query header\n");
1813 ok(index == 1, "Index was not incremented\n");
1814 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1815 len = sizeof(buffer);
1816 strcpy(buffer,"Warning");
1817 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1818 buffer,&len,&index),"Failed to get second header\n");
1819 ok(index == 2, "Index was not incremented\n");
1820 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1821 len = sizeof(buffer);
1822 strcpy(buffer,"Warning");
1823 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1824 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1825
1826 ok(HttpAddRequestHeadersA(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1827
1828 index = 0;
1829 len = sizeof(buffer);
1830 strcpy(buffer,"Warning");
1831 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1832 buffer,&len,&index),"Unable to query header\n");
1833 ok(index == 1, "Index was not incremented\n");
1834 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1835 len = sizeof(buffer);
1836 strcpy(buffer,"Warning");
1837 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1838 buffer,&len,&index),"Failed to get second header\n");
1839 ok(index == 2, "Index was not incremented\n");
1840 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1841 len = sizeof(buffer);
1842 strcpy(buffer,"Warning");
1843 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1844 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1845
1846 ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1847
1848 index = 0;
1849 len = sizeof(buffer);
1850 strcpy(buffer,"Warning");
1851 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1852 buffer,&len,&index),"Unable to query header\n");
1853 ok(index == 1, "Index was not incremented\n");
1854 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1855 len = sizeof(buffer);
1856 strcpy(buffer,"Warning");
1857 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1858 buffer,&len,&index),"Failed to get second header\n");
1859 ok(index == 2, "Index was not incremented\n");
1860 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1861 len = sizeof(buffer);
1862 strcpy(buffer,"Warning");
1863 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1864 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1865
1866 ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1867
1868 index = 0;
1869 len = sizeof(buffer);
1870 strcpy(buffer,"Warning");
1871 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1872 buffer,&len,&index),"Unable to query header\n");
1873 ok(index == 1, "Index was not incremented\n");
1874 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1875 len = sizeof(buffer);
1876 strcpy(buffer,"Warning");
1877 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1878 ok(index == 2, "Index was not incremented\n");
1879 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1880 len = sizeof(buffer);
1881 strcpy(buffer,"Warning");
1882 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1883
1884 ok(HttpAddRequestHeadersA(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1885
1886 index = 0;
1887 len = sizeof(buffer);
1888 strcpy(buffer,"Warning");
1889 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1890 ok(index == 1, "Index was not incremented\n");
1891 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1892 len = sizeof(buffer);
1893 strcpy(buffer,"Warning");
1894 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1895 ok(index == 2, "Index was not incremented\n");
1896 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1897 len = sizeof(buffer);
1898 strcpy(buffer,"Warning");
1899 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1900
1901 ok(HttpAddRequestHeadersA(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1902
1903 index = 0;
1904 len = sizeof(buffer);
1905 strcpy(buffer,"Warning");
1906 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1907 ok(index == 1, "Index was not incremented\n");
1908 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1909 len = sizeof(buffer);
1910 strcpy(buffer,"Warning");
1911 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1912 ok(index == 2, "Index was not incremented\n");
1913 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1914 len = sizeof(buffer);
1915 strcpy(buffer,"Warning");
1916 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1917
1918 ok(HttpAddRequestHeadersA(hRequest,"Warning:test7",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE), "HTTP_ADDREQ_FLAG_ADD with HTTP_ADDREQ_FLAG_REPALCE Did not work\n");
1919
1920 index = 0;
1921 len = sizeof(buffer);
1922 strcpy(buffer,"Warning");
1923 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1924 ok(index == 1, "Index was not incremented\n");
1925 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1926 len = sizeof(buffer);
1927 strcpy(buffer,"Warning");
1928 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1929 ok(index == 2, "Index was not incremented\n");
1930 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1931 len = sizeof(buffer);
1932 strcpy(buffer,"Warning");
1933 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1934
1935 /* Ensure that blank headers are ignored and don't cause a failure */
1936 ok(HttpAddRequestHeadersA(hRequest,"\r\nBlankTest:value\r\n\r\n",-1, HTTP_ADDREQ_FLAG_ADD_IF_NEW), "Failed to add header with blank entries in list\n");
1937
1938 index = 0;
1939 len = sizeof(buffer);
1940 strcpy(buffer,"BlankTest");
1941 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1942 ok(index == 1, "Index was not incremented\n");
1943 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1944
1945 /* Ensure that malformed header separators are ignored and don't cause a failure */
1946 ok(HttpAddRequestHeadersA(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1947 "Failed to add header with malformed entries in list\n");
1948
1949 index = 0;
1950 len = sizeof(buffer);
1951 strcpy(buffer,"MalformedTest");
1952 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1953 ok(index == 1, "Index was not incremented\n");
1954 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1955 index = 0;
1956 len = sizeof(buffer);
1957 strcpy(buffer,"MalformedTestTwo");
1958 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1959 ok(index == 1, "Index was not incremented\n");
1960 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1961 index = 0;
1962 len = sizeof(buffer);
1963 strcpy(buffer,"MalformedTestThree");
1964 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1965 ok(index == 1, "Index was not incremented\n");
1966 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1967
1968 ret = HttpAddRequestHeadersA(hRequest, "Authorization: Basic\r\n", -1, HTTP_ADDREQ_FLAG_ADD);
1969 ok(ret, "unable to add header %u\n", GetLastError());
1970
1971 index = 0;
1972 buffer[0] = 0;
1973 len = sizeof(buffer);
1974 ret = HttpQueryInfoA(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index);
1975 ok(ret, "unable to query header %u\n", GetLastError());
1976 ok(index == 1, "index was not incremented\n");
1977 ok(!strcmp(buffer, "Basic"), "incorrect string was returned (%s)\n", buffer);
1978
1979 ret = HttpAddRequestHeadersA(hRequest, "Authorization:\r\n", -1, HTTP_ADDREQ_FLAG_REPLACE);
1980 ok(ret, "unable to remove header %u\n", GetLastError());
1981
1982 index = 0;
1983 len = sizeof(buffer);
1984 SetLastError(0xdeadbeef);
1985 ok(!HttpQueryInfoA(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index),
1986 "header still present\n");
1987 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "got %u\n", GetLastError());
1988
1989 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1990 done:
1991 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1992 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1993 }
1994
1995 static const char garbagemsg[] =
1996 "Garbage: Header\r\n";
1997
1998 static const char contmsg[] =
1999 "HTTP/1.1 100 Continue\r\n";
2000
2001 static const char expandcontmsg[] =
2002 "HTTP/1.1 100 Continue\r\n"
2003 "Server: winecontinue\r\n"
2004 "Tag: something witty\r\n";
2005
2006 static const char okmsg[] =
2007 "HTTP/1.1 200 OK\r\n"
2008 "Server: winetest\r\n"
2009 "\r\n";
2010
2011 static const char okmsg2[] =
2012 "HTTP/1.1 200 OK\r\n"
2013 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2014 "Server: winetest\r\n"
2015 "Content-Length: 0\r\n"
2016 "Set-Cookie: one\r\n"
2017 "Set-Cookie: two\r\n"
2018 "\r\n";
2019
2020 static const char okmsg_cookie_path[] =
2021 "HTTP/1.1 200 OK\r\n"
2022 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2023 "Server: winetest\r\n"
2024 "Content-Length: 0\r\n"
2025 "Set-Cookie: subcookie2=data; path=/test_cookie_set_path\r\n"
2026 "\r\n";
2027
2028 static const char okmsg_cookie[] =
2029 "HTTP/1.1 200 OK\r\n"
2030 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2031 "Server: winetest\r\n"
2032 "Content-Length: 0\r\n"
2033 "Set-Cookie: testcookie=testvalue\r\n"
2034 "\r\n";
2035
2036 static const char notokmsg[] =
2037 "HTTP/1.1 400 Bad Request\r\n"
2038 "Server: winetest\r\n"
2039 "\r\n";
2040
2041 static const char noauthmsg[] =
2042 "HTTP/1.1 401 Unauthorized\r\n"
2043 "Server: winetest\r\n"
2044 "Connection: close\r\n"
2045 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
2046 "\r\n";
2047
2048 static const char noauthmsg2[] =
2049 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
2050 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
2051 "\0d`0|6\n"
2052 "Server: winetest\r\n";
2053
2054 static const char proxymsg[] =
2055 "HTTP/1.1 407 Proxy Authentication Required\r\n"
2056 "Server: winetest\r\n"
2057 "Proxy-Connection: close\r\n"
2058 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
2059 "\r\n";
2060
2061 static const char page1[] =
2062 "<HTML>\r\n"
2063 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
2064 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
2065 "</HTML>\r\n\r\n";
2066
2067 static const char ok_with_length[] =
2068 "HTTP/1.1 200 OK\r\n"
2069 "Connection: Keep-Alive\r\n"
2070 "Content-Length: 18\r\n\r\n"
2071 "HTTP/1.1 211 OK\r\n\r\n";
2072
2073 static const char ok_with_length2[] =
2074 "HTTP/1.1 210 OK\r\n"
2075 "Connection: Keep-Alive\r\n"
2076 "Content-Length: 19\r\n\r\n"
2077 "HTTP/1.1 211 OK\r\n\r\n";
2078
2079 struct server_info {
2080 HANDLE hEvent;
2081 int port;
2082 };
2083
2084 static int test_cache_gzip;
2085 static const char *send_buffer;
2086 static int server_socket;
2087
2088 static DWORD CALLBACK server_thread(LPVOID param)
2089 {
2090 struct server_info *si = param;
2091 int r, c = -1, i, on, count = 0;
2092 SOCKET s;
2093 struct sockaddr_in sa;
2094 char *buffer;
2095 size_t buffer_size;
2096 WSADATA wsaData;
2097 int last_request = 0;
2098 char host_header[22];
2099 char host_header_override[30];
2100 static int test_no_cache = 0;
2101
2102 WSAStartup(MAKEWORD(1,1), &wsaData);
2103
2104 s = socket(AF_INET, SOCK_STREAM, 0);
2105 if (s == INVALID_SOCKET)
2106 return 1;
2107
2108 on = 1;
2109 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
2110
2111 memset(&sa, 0, sizeof sa);
2112 sa.sin_family = AF_INET;
2113 sa.sin_port = htons(si->port);
2114 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
2115
2116 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
2117 if (r<0)
2118 return 1;
2119
2120 listen(s, 0);
2121
2122 SetEvent(si->hEvent);
2123
2124 sprintf(host_header, "Host: localhost:%d", si->port);
2125 sprintf(host_header_override, "Host: test.local:%d\r\n", si->port);
2126 buffer = HeapAlloc(GetProcessHeap(), 0, buffer_size = 1000);
2127
2128 do
2129 {
2130 if(c == -1)
2131 c = accept(s, NULL, NULL);
2132
2133 memset(buffer, 0, buffer_size);
2134 for(i=0;; i++)
2135 {
2136 if(i == buffer_size)
2137 buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, buffer_size *= 2);
2138
2139 r = recv(c, buffer+i, 1, 0);
2140 if (r != 1)
2141 break;
2142 if (i<4) continue;
2143 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
2144 buffer[i-3] == '\r' && buffer[i-1] == '\r')
2145 break;
2146 }
2147 if (strstr(buffer, "GET /test1"))
2148 {
2149 if (!strstr(buffer, "Content-Length: 0"))
2150 {
2151 send(c, okmsg, sizeof okmsg-1, 0);
2152 send(c, page1, sizeof page1-1, 0);
2153 }
2154 else
2155 send(c, notokmsg, sizeof notokmsg-1, 0);
2156 }
2157 if (strstr(buffer, "CONNECT "))
2158 {
2159 if (!strstr(buffer, "Content-Length: 0"))
2160 send(c, notokmsg, sizeof notokmsg-1, 0);
2161 else
2162 send(c, proxymsg, sizeof proxymsg-1, 0);
2163 }
2164 if (strstr(buffer, "/test2"))
2165 {
2166 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
2167 {
2168 send(c, okmsg, sizeof okmsg-1, 0);
2169 send(c, page1, sizeof page1-1, 0);
2170 }
2171 else
2172 send(c, proxymsg, sizeof proxymsg-1, 0);
2173 }
2174 if (strstr(buffer, "/test3"))
2175 {
2176 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2177 send(c, okmsg, sizeof okmsg-1, 0);
2178 else
2179 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2180 }
2181 if (strstr(buffer, "/test4"))
2182 {
2183 if (strstr(buffer, "Connection: Close"))
2184 send(c, okmsg, sizeof okmsg-1, 0);
2185 else
2186 send(c, notokmsg, sizeof notokmsg-1, 0);
2187 }
2188 if (strstr(buffer, "POST /test5") ||
2189 strstr(buffer, "RPC_IN_DATA /test5") ||
2190 strstr(buffer, "RPC_OUT_DATA /test5"))
2191 {
2192 if (strstr(buffer, "Content-Length: 0"))
2193 {
2194 send(c, okmsg, sizeof okmsg-1, 0);
2195 send(c, page1, sizeof page1-1, 0);
2196 }
2197 else
2198 send(c, notokmsg, sizeof notokmsg-1, 0);
2199 }
2200 if (strstr(buffer, "GET /test6"))
2201 {
2202 send(c, contmsg, sizeof contmsg-1, 0);
2203 send(c, contmsg, sizeof contmsg-1, 0);
2204 send(c, okmsg, sizeof okmsg-1, 0);
2205 send(c, page1, sizeof page1-1, 0);
2206 }
2207 if (strstr(buffer, "POST /test7"))
2208 {
2209 if (strstr(buffer, "Content-Length: 100"))
2210 {
2211 if (strstr(buffer, "POST /test7b"))
2212 recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
2213 send(c, okmsg, sizeof okmsg-1, 0);
2214 send(c, page1, sizeof page1-1, 0);
2215 }
2216 else
2217 send(c, notokmsg, sizeof notokmsg-1, 0);
2218 }
2219 if (strstr(buffer, "/test8"))
2220 {
2221 if (!strstr(buffer, "Connection: Close") &&
2222 strstr(buffer, "Connection: Keep-Alive") &&
2223 !strstr(buffer, "Cache-Control: no-cache") &&
2224 !strstr(buffer, "Pragma: no-cache") &&
2225 strstr(buffer, host_header))
2226 send(c, okmsg, sizeof okmsg-1, 0);
2227 else
2228 send(c, notokmsg, sizeof notokmsg-1, 0);
2229 }
2230 if (strstr(buffer, "/test9"))
2231 {
2232 if (!strstr(buffer, "Connection: Close") &&
2233 !strstr(buffer, "Connection: Keep-Alive") &&
2234 !strstr(buffer, "Cache-Control: no-cache") &&
2235 !strstr(buffer, "Pragma: no-cache") &&
2236 strstr(buffer, host_header))
2237 send(c, okmsg, sizeof okmsg-1, 0);
2238 else
2239 send(c, notokmsg, sizeof notokmsg-1, 0);
2240 }
2241 if (strstr(buffer, "/testA"))
2242 {
2243 if (!strstr(buffer, "Connection: Close") &&
2244 !strstr(buffer, "Connection: Keep-Alive") &&
2245 (strstr(buffer, "Cache-Control: no-cache") ||
2246 strstr(buffer, "Pragma: no-cache")) &&
2247 strstr(buffer, host_header))
2248 send(c, okmsg, sizeof okmsg-1, 0);
2249 else
2250 send(c, notokmsg, sizeof notokmsg-1, 0);
2251 }
2252 if (strstr(buffer, "/testC"))
2253 {
2254 if (strstr(buffer, "cookie=biscuit"))
2255 send(c, okmsg, sizeof okmsg-1, 0);
2256 else
2257 send(c, notokmsg, sizeof notokmsg-1, 0);
2258 }
2259 if (strstr(buffer, "/testD"))
2260 {
2261 send(c, okmsg2, sizeof okmsg2-1, 0);
2262 }
2263 if (strstr(buffer, "/testE"))
2264 {
2265 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
2266 }
2267 if (strstr(buffer, "GET /quit"))
2268 {
2269 send(c, okmsg, sizeof okmsg-1, 0);
2270 send(c, page1, sizeof page1-1, 0);
2271 last_request = 1;
2272 }
2273 if (strstr(buffer, "GET /testF"))
2274 {
2275 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
2276 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2277 send(c, contmsg, sizeof contmsg-1, 0);
2278 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2279 send(c, okmsg, sizeof okmsg-1, 0);
2280 send(c, page1, sizeof page1-1, 0);
2281 }
2282 if (strstr(buffer, "GET /testG"))
2283 {
2284 send(c, page1, sizeof page1-1, 0);
2285 }
2286
2287 if (strstr(buffer, "GET /testJ"))
2288 {
2289 if (count == 0)
2290 {
2291 count++;
2292 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2293 }
2294 else
2295 {
2296 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2297 count = 0;
2298 }
2299 }
2300 if (strstr(buffer, "GET /testH"))
2301 {
2302 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2303 recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
2304 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2305 }
2306
2307 if (strstr(buffer, "GET /test_no_content"))
2308 {
2309 static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n";
2310 send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2311 }
2312 if (strstr(buffer, "GET /test_conn_close"))
2313 {
2314 static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
2315 send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
2316 WaitForSingleObject(conn_close_event, INFINITE);
2317 trace("closing connection\n");
2318 }
2319 if (strstr(buffer, "GET /test_cache_control_no_cache"))
2320 {
2321 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\n\r\nsome content";
2322 if(!test_no_cache++)
2323 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2324 else
2325 send(c, okmsg, sizeof(okmsg)-1, 0);
2326 }
2327 if (strstr(buffer, "GET /test_cache_control_no_store"))
2328 {
2329 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: junk, \t No-StOrE\r\n\r\nsome content";
2330 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2331 }
2332 if (strstr(buffer, "GET /test_cache_gzip"))
2333 {
2334 static const char gzip_response[] = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\n\r\n"
2335 "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xaf\xca\x2c\x50\x28"
2336 "\x49\x2d\x2e\xe1\x02\x00\x62\x92\xc7\x6c\x0a\x00\x00\x00";
2337 if(!test_cache_gzip++)
2338 send(c, gzip_response, sizeof(gzip_response), 0);
2339 else
2340 send(c, notokmsg, sizeof(notokmsg)-1, 0);
2341 }
2342 if (strstr(buffer, "HEAD /test_head")) {
2343 static const char head_response[] =
2344 "HTTP/1.1 200 OK\r\n"
2345 "Connection: Keep-Alive\r\n"
2346 "Content-Length: 100\r\n"
2347 "\r\n";
2348
2349 send(c, head_response, sizeof(head_response), 0);
2350 continue;
2351 }
2352 if (strstr(buffer, "GET /send_from_buffer"))
2353 send(c, send_buffer, strlen(send_buffer), 0);
2354 if (strstr(buffer, "/test_cache_control_verb"))
2355 {
2356 if (!memcmp(buffer, "GET ", sizeof("GET ")-1) &&
2357 !strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2358 else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2359 else send(c, notokmsg, sizeof(notokmsg)-1, 0);
2360 }
2361 if (strstr(buffer, "/test_request_content_length"))
2362 {
2363 static char msg[] = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\n\r\n";
2364 static int seen_content_length;
2365
2366 if (!seen_content_length)
2367 {
2368 if (strstr(buffer, "Content-Length: 0"))
2369 {
2370 seen_content_length = 1;
2371 send(c, msg, sizeof msg-1, 0);
2372 }
2373 else send(c, notokmsg, sizeof notokmsg-1, 0);
2374 WaitForSingleObject(hCompleteEvent, 5000);
2375 }
2376 else
2377 {
2378 if (strstr(buffer, "Content-Length: 0")) send(c, msg, sizeof msg-1, 0);
2379 else send(c, notokmsg, sizeof notokmsg-1, 0);
2380 WaitForSingleObject(hCompleteEvent, 5000);
2381 }
2382 }
2383 if (strstr(buffer, "GET /test_premature_disconnect"))
2384 trace("closing connection\n");
2385 if (strstr(buffer, "HEAD /upload.txt"))
2386 {
2387 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2388 send(c, okmsg, sizeof okmsg-1, 0);
2389 else
2390 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2391 }
2392 if (strstr(buffer, "PUT /upload2.txt"))
2393 {
2394 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2395 send(c, okmsg, sizeof okmsg-1, 0);
2396 else
2397 send(c, notokmsg, sizeof notokmsg-1, 0);
2398 }
2399 if (strstr(buffer, "/test_cookie_path1"))
2400 {
2401 if (strstr(buffer, "subcookie=data"))
2402 send(c, okmsg, sizeof okmsg-1, 0);
2403 else
2404 send(c, notokmsg, sizeof notokmsg-1, 0);
2405 }
2406 if (strstr(buffer, "/test_cookie_path2"))
2407 {
2408 if (strstr(buffer, "subcookie2=data"))
2409 send(c, okmsg, sizeof okmsg-1, 0);
2410 else
2411 send(c, notokmsg, sizeof notokmsg-1, 0);
2412 }
2413 if (strstr(buffer, "/test_cookie_set_path"))
2414 {
2415 send(c, okmsg_cookie_path, sizeof okmsg_cookie_path-1, 0);
2416 }
2417 if (strstr(buffer, "/test_cookie_merge"))
2418 {
2419 if (strstr(buffer, "subcookie=data") &&
2420 !strstr(buffer, "manual_cookie=test"))
2421 send(c, okmsg, sizeof okmsg-1, 0);
2422 else
2423 send(c, notokmsg, sizeof notokmsg-1, 0);
2424 }
2425 if (strstr(buffer, "/test_cookie_set_host_override"))
2426 {
2427 send(c, okmsg_cookie, sizeof okmsg_cookie-1, 0);
2428 }
2429 if (strstr(buffer, "/test_cookie_check_host_override"))
2430 {
2431 if (strstr(buffer, "Cookie:") && strstr(buffer, "testcookie=testvalue"))
2432 send(c, okmsg, sizeof okmsg-1, 0);
2433 else
2434 send(c, notokmsg, sizeof notokmsg-1, 0);
2435 }
2436 if (strstr(buffer, "/test_cookie_check_different_host"))
2437 {
2438 if (!strstr(buffer, "foo") &&
2439 strstr(buffer, "cookie=biscuit"))
2440 send(c, okmsg, sizeof okmsg-1, 0);
2441 else
2442 send(c, notokmsg, sizeof notokmsg-1, 0);
2443 }
2444 if (strstr(buffer, "/test_host_override"))
2445 {
2446 if (strstr(buffer, host_header_override))
2447 send(c, okmsg, sizeof okmsg-1, 0);
2448 else
2449 send(c, notokmsg, sizeof notokmsg-1, 0);
2450 }
2451 if (strstr(buffer, "/async_read"))
2452 {
2453 const char *page1_mid = page1 + (sizeof page1 - 1)/2;
2454 const char *page1_end = page1 + sizeof page1 - 1;
2455 send(c, okmsg, sizeof okmsg-1, 0);
2456 send(c, page1, page1_mid - page1, 0);
2457 WaitForSingleObject(conn_wait_event, INFINITE);
2458 send(c, page1_mid, page1_end - page1_mid, 0);
2459 }
2460 if (strstr(buffer, "/socket"))
2461 {
2462 server_socket = c;
2463 SetEvent(server_req_rec_event);
2464 WaitForSingleObject(conn_wait_event, INFINITE);
2465 }
2466 if (strstr(buffer, "/echo_request"))
2467 {
2468 send(c, okmsg, sizeof(okmsg)-1, 0);
2469 send(c, buffer, strlen(buffer), 0);
2470 }
2471 if (strstr(buffer, "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_len(a,b,c,d,e,f) _readex_expect_sync_data_len(__LINE__,a,b,c,d,e,f)
4908 static void _readex_expect_sync_data_len(unsigned line, HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf,
4909 DWORD buf_size, const char *exdata, DWORD len)
4910 {
4911 BOOL ret;
4912
4913 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4914
4915 memset(buf->lpvBuffer, 0xff, buf_size);
4916 buf->dwBufferLength = buf_size;
4917 ret = InternetReadFileExW(req, buf, flags, 0xdeadbeef);
4918 ok_(__FILE__,line)(ret, "InternetReadFileExW failed: %u\n", GetLastError());
4919 ok_(__FILE__,line)(buf->dwBufferLength == len, "dwBufferLength = %u, expected %u\n", buf->dwBufferLength, len);
4920 if(len && exdata)
4921 ok_(__FILE__,line)(!memcmp(buf->lpvBuffer, exdata, len), "Unexpected data\n");
4922
4923 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4924 }
4925
4926 #define readex_expect_sync_data(a,b,c,d,e) _readex_expect_sync_data(__LINE__,a,b,c,d,e)
4927 static void _readex_expect_sync_data(unsigned line, HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf,
4928 DWORD buf_size, const char *exdata)
4929 {
4930 _readex_expect_sync_data_len(line, req, flags, buf, buf_size, exdata, strlen(exdata));
4931 }
4932
4933 static void send_response_and_wait(const char *response, BOOL close_connection, INTERNET_BUFFERSW *buf)
4934 {
4935 DWORD orig_size = buf->dwBufferLength;
4936
4937 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
4938
4939 if(response)
4940 server_send_string(response);
4941
4942 if(close_connection) {
4943 char c;
4944 SetEvent(conn_wait_event);
4945 recv(server_socket, &c, 1, 0);
4946 }
4947
4948 WaitForSingleObject(hCompleteEvent, INFINITE);
4949
4950 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
4951
4952 /* If IRF_NO_WAIT is used, buffer is not changed. */
4953 ok(buf->dwBufferLength == orig_size, "dwBufferLength = %u\n", buf->dwBufferLength);
4954 ok(!*(int*)buf->lpvBuffer, "buffer data changed\n");
4955 }
4956
4957 static void send_response_len_and_wait(unsigned len, BOOL close_connection, INTERNET_BUFFERSW *buf)
4958 {
4959 char *response;
4960
4961 response = HeapAlloc(GetProcessHeap(), 0, len+1);
4962 memset(response, 'x', len);
4963 response[len] = 0;
4964 send_response_and_wait(response, close_connection, buf);
4965 HeapFree(GetProcessHeap(), 0, response);
4966 }
4967
4968 static void readex_expect_async(HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf, DWORD buf_size)
4969 {
4970 BOOL ret;
4971
4972 memset(buf->lpvBuffer, 0, buf_size);
4973 buf->dwBufferLength = buf_size;
4974 ret = InternetReadFileExW(req, buf, flags, 0xdeadbeef);
4975 ok(!ret && GetLastError() == ERROR_IO_PENDING, "InternetReadFileExW returned %x (%u)\n", ret, GetLastError());
4976 ok(buf->dwBufferLength == buf_size, "dwBufferLength = %u, expected %u\n", buf->dwBufferLength, buf_size);
4977 ok(!*(int*)buf->lpvBuffer, "buffer data changed\n");
4978 }
4979
4980 #define expect_data_available(a,b) _expect_data_available(__LINE__,a,b)
4981 static DWORD _expect_data_available(unsigned line, HINTERNET req, int exsize)
4982 {
4983 DWORD size = 0;
4984 BOOL res;
4985
4986 res = InternetQueryDataAvailable(req, &size, 0, 0);
4987 ok_(__FILE__,line)(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
4988 if(exsize != -1)
4989 ok_(__FILE__,line)(size == exsize, "size = %u, expected %u\n", size, exsize);
4990
4991 return size;
4992 }
4993
4994 static void test_http_read(int port)
4995 {
4996 INTERNET_BUFFERSW ib;
4997 test_request_t req;
4998 char buf[24000];
4999 DWORD avail;
5000
5001 if(!is_ie7plus)
5002 return;
5003
5004 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
5005 conn_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
5006 server_req_rec_event = CreateEventW(NULL, FALSE, FALSE, NULL);
5007
5008 trace("Testing InternetReadFileExW with IRF_NO_WAIT flag...\n");
5009
5010 open_read_test_request(port, &req,
5011 "HTTP/1.1 200 OK\r\n"
5012 "Server: winetest\r\n"
5013 "\r\n"
5014 "xx");
5015
5016 memset(&ib, 0, sizeof(ib));
5017 ib.dwStructSize = sizeof(ib);
5018 ib.lpvBuffer = buf;
5019
5020 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "xx");
5021
5022 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5023 send_response_and_wait("1234567890", FALSE, &ib);
5024 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, 5, "12345");
5025 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "67890");
5026
5027 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5028 send_response_and_wait("12345", TRUE, &ib);
5029
5030 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "12345");
5031 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
5032
5033 close_async_handle(req.session, hCompleteEvent, 2);
5034
5035 open_read_test_request(port, &req,
5036 "HTTP/1.1 200 OK\r\n"
5037 "Server: winetest\r\n"
5038 "Transfer-Encoding: chunked\r\n"
5039 "\r\n"
5040 "9\r\n123456789");
5041 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123456789");
5042 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5043
5044 send_response_and_wait("\r\n1\r\na\r\n1\r\nb\r", FALSE, &ib);
5045 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab");
5046 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5047
5048 send_response_and_wait("\n3\r\nab", FALSE, &ib);
5049 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab");
5050 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5051
5052 send_response_and_wait("c", FALSE, &ib);
5053 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "c");
5054 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5055
5056 send_response_and_wait("\r\n1\r\nx\r\n0\r\n\r\n", TRUE, &ib);
5057 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "x");
5058 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
5059
5060 close_async_handle(req.session, hCompleteEvent, 2);
5061
5062 open_read_test_request(port, &req,
5063 "HTTP/1.1 200 OK\r\n"
5064 "Server: winetest\r\n"
5065 "Transfer-Encoding: chunked\r\n"
5066 "\r\n"
5067 "3\r\n123\r\n");
5068 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123");
5069 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5070
5071 send_response_and_wait("0\r\n\r\n", TRUE, &ib);
5072 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
5073
5074 close_async_handle(req.session, hCompleteEvent, 2);
5075
5076 trace("Testing InternetQueryDataAvailable...\n");
5077
5078 open_read_test_request(port, &req,
5079 "HTTP/1.1 200 OK\r\n"
5080 "Server: winetest\r\n"
5081 "\r\n"
5082 "123");
5083 expect_data_available(req.request, 3);
5084 readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123");
5085 readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
5086
5087 send_response_len_and_wait(20000, TRUE, &ib);
5088 avail = expect_data_available(req.request, -1);
5089 ok(avail < 17000, "avail = %u\n", avail);
5090
5091 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
5092 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
5093 close_async_handle(req.session, hCompleteEvent, 2);
5094 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5095 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5096
5097 CloseHandle(hCompleteEvent);
5098 CloseHandle(conn_wait_event);
5099 CloseHandle(server_req_rec_event);
5100 }
5101
5102 static void test_long_url(int port)
5103 {
5104 char long_path[INTERNET_MAX_PATH_LENGTH*2] = "/echo_request?";
5105 char buf[sizeof(long_path)*2], url[sizeof(buf)];
5106 test_request_t req;
5107 DWORD size, len;
5108 BOOL ret;
5109
5110 if(!is_ie7plus)
5111 return;
5112
5113 memset(long_path+strlen(long_path), 'x', sizeof(long_path)-strlen(long_path));
5114 long_path[sizeof(long_path)-1] = 0;
5115 open_simple_request(&req, "localhost", port, NULL, long_path);
5116
5117 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
5118 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
5119 test_status_code(req.request, 200);
5120
5121 receive_simple_request(req.request, buf, sizeof(buf));
5122 ok(strstr(buf, long_path) != NULL, "long pathnot found in %s\n", buf);
5123
5124 sprintf(url, "http://localhost:%u%s", port, long_path);
5125
5126 size = sizeof(buf);
5127 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_URL, buf, &size);
5128 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
5129 len = strlen(url);
5130 ok(size == len, "size = %u, expected %u\n", size, len);
5131 ok(!strcmp(buf, url), "Wrong URL %s, expected %s\n", buf, url);
5132
5133 close_request(&req);
5134 }
5135
5136 static void test_http_connection(void)
5137 {
5138 struct server_info si;
5139 HANDLE hThread;
5140 DWORD id = 0, r;
5141
5142 si.hEvent = CreateEventW(NULL, 0, 0, NULL);
5143 si.port = 7531;
5144
5145 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
5146 ok( hThread != NULL, "create thread failed\n");
5147
5148 r = WaitForSingleObject(si.hEvent, 10000);
5149 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
5150 if (r != WAIT_OBJECT_0)
5151 return;
5152
5153 test_basic_request(si.port, "GET", "/test1");
5154 test_proxy_indirect(si.port);
5155 test_proxy_direct(si.port);
5156 test_header_handling_order(si.port);
5157 test_basic_request(si.port, "POST", "/test5");
5158 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
5159 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
5160 test_basic_request(si.port, "GET", "/test6");
5161 test_basic_request(si.port, "GET", "/testF");
5162 test_connection_header(si.port);
5163 test_header_override(si.port);
5164 test_cookie_header(si.port);
5165 test_basic_authentication(si.port);
5166 test_invalid_response_headers(si.port);
5167 test_response_without_headers(si.port);
5168 test_HttpQueryInfo(si.port);
5169 test_HttpSendRequestW(si.port);
5170 test_options(si.port);
5171 test_no_content(si.port);
5172 test_conn_close(si.port);
5173 test_no_cache(si.port);
5174 test_cache_read_gzipped(si.port);
5175 test_http_status(si.port);
5176 test_premature_disconnect(si.port);
5177 test_connection_closing(si.port);
5178 test_cache_control_verb(si.port);
5179 test_successive_HttpSendRequest(si.port);
5180 test_head_request(si.port);
5181 test_request_content_length(si.port);
5182 test_accept_encoding(si.port);
5183 test_basic_auth_credentials_reuse(si.port);
5184 test_async_read(si.port);
5185 test_http_read(si.port);
5186 test_long_url(si.port);
5187
5188 /* send the basic request again to shutdown the server thread */
5189 test_basic_request(si.port, "GET", "/quit");
5190
5191 r = WaitForSingleObject(hThread, 3000);
5192 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
5193 CloseHandle(hThread);
5194 }
5195
5196 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
5197 {
5198 LocalFree(info->lpszSubjectInfo);
5199 LocalFree(info->lpszIssuerInfo);
5200 LocalFree(info->lpszProtocolName);
5201 LocalFree(info->lpszSignatureAlgName);
5202 LocalFree(info->lpszEncryptionAlgName);
5203 }
5204
5205 typedef struct {
5206 const char *ex_subject;
5207 const char *ex_issuer;
5208 } cert_struct_test_t;
5209
5210 static const cert_struct_test_t test_winehq_org_cert = {
5211 "*.winehq.org",
5212
5213 "US\r\n"
5214 "GeoTrust Inc.\r\n"
5215 "RapidSSL SHA256 CA"
5216 };
5217
5218 static const cert_struct_test_t test_winehq_com_cert = {
5219 "US\r\n"
5220 "Minnesota\r\n"
5221 "Saint Paul\r\n"
5222 "WineHQ\r\n"
5223 "test.winehq.com\r\n"
5224 "webmaster@winehq.org",
5225
5226 "US\r\n"
5227 "Minnesota\r\n"
5228 "WineHQ\r\n"
5229 "test.winehq.com\r\n"
5230 "webmaster@winehq.org"
5231 };
5232
5233 static void test_cert_struct(HINTERNET req, const cert_struct_test_t *test)
5234 {
5235 INTERNET_CERTIFICATE_INFOA info;
5236 DWORD size;
5237 BOOL res;
5238
5239 memset(&info, 0x5, sizeof(info));
5240
5241 size = sizeof(info);
5242 res = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, &info, &size);
5243 ok(res, "InternetQueryOption failed: %u\n", GetLastError());
5244 ok(size == sizeof(info), "size = %u\n", size);
5245
5246 ok(!strcmp(info.lpszSubjectInfo, test->ex_subject), "lpszSubjectInfo = %s\n", info.lpszSubjectInfo);
5247 ok(!strcmp(info.lpszIssuerInfo, test->ex_issuer), "lpszIssuerInfo = %s\n", info.lpszIssuerInfo);
5248 ok(!info.lpszSignatureAlgName, "lpszSignatureAlgName = %s\n", info.lpszSignatureAlgName);
5249 ok(!info.lpszEncryptionAlgName, "lpszEncryptionAlgName = %s\n", info.lpszEncryptionAlgName);
5250 ok(!info.lpszProtocolName, "lpszProtocolName = %s\n", info.lpszProtocolName);
5251 ok(info.dwKeySize >= 128 && info.dwKeySize <= 256, "dwKeySize = %u\n", info.dwKeySize);
5252
5253 release_cert_info(&info);
5254 }
5255
5256 #define test_security_info(a,b,c) _test_security_info(__LINE__,a,b,c)
5257 static void _test_security_info(unsigned line, const char *urlc, DWORD error, DWORD ex_flags)
5258 {
5259 char url[INTERNET_MAX_URL_LENGTH];
5260 const CERT_CHAIN_CONTEXT *chain;
5261 DWORD flags;
5262 BOOL res;
5263
5264 if(!pInternetGetSecurityInfoByURLA) {
5265 win_skip("pInternetGetSecurityInfoByURLA not available\n");
5266 return;
5267 }
5268
5269 strcpy(url, urlc);
5270 chain = (void*)0xdeadbeef;
5271 flags = 0xdeadbeef;
5272 res = pInternetGetSecurityInfoByURLA(url, &chain, &flags);
5273 if(error == ERROR_SUCCESS) {
5274 ok_(__FILE__,line)(res, "InternetGetSecurityInfoByURLA failed: %u\n", GetLastError());
5275 ok_(__FILE__,line)(chain != NULL, "chain = NULL\n");
5276 ok_(__FILE__,line)(flags == ex_flags, "flags = %x\n", flags);
5277 CertFreeCertificateChain(chain);
5278 }else {
5279 ok_(__FILE__,line)(!res && GetLastError() == error,
5280 "InternetGetSecurityInfoByURLA returned: %x(%u), expected %u\n", res, GetLastError(), error);
5281 }
5282 }
5283
5284 #define test_secflags_option(a,b,c) _test_secflags_option(__LINE__,a,b,c)
5285 static void _test_secflags_option(unsigned line, HINTERNET req, DWORD ex_flags, DWORD opt_flags)
5286 {
5287 DWORD flags, size;
5288 BOOL res;
5289
5290 flags = 0xdeadbeef;
5291 size = sizeof(flags);
5292 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
5293 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
5294 ok_(__FILE__,line)((flags & ~opt_flags) == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS flags = %x, expected %x\n",
5295 flags, ex_flags);
5296
5297 /* Option 98 is undocumented and seems to be the same as INTERNET_OPTION_SECURITY_FLAGS */
5298 flags = 0xdeadbeef;
5299 size = sizeof(flags);
5300 res = InternetQueryOptionW(req, 98, &flags, &size);
5301 ok_(__FILE__,line)(res, "InternetQueryOptionW(98) failed: %u\n", GetLastError());
5302 ok_(__FILE__,line)((flags & ~opt_flags) == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS(98) flags = %x, expected %x\n",
5303 flags, ex_flags);
5304 }
5305
5306 #define set_secflags(a,b,c) _set_secflags(__LINE__,a,b,c)
5307 static void _set_secflags(unsigned line, HINTERNET req, BOOL use_undoc, DWORD flags)
5308 {
5309 BOOL res;
5310
5311 res = InternetSetOptionW(req, use_undoc ? 99 : INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
5312 ok_(__FILE__,line)(res, "InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
5313 }
5314
5315 static void test_security_flags(void)
5316 {
5317 INTERNET_CERTIFICATE_INFOA *cert;
5318 HINTERNET ses, conn, req;
5319 DWORD size, flags;
5320 char buf[100];
5321 BOOL res;
5322
5323 trace("Testing security flags...\n");
5324
5325 hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
5326
5327 ses = InternetOpenA("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
5328 ok(ses != NULL, "InternetOpen failed\n");
5329
5330 pInternetSetStatusCallbackA(ses, &callback);
5331
5332 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5333 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
5334 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
5335 ok(conn != NULL, "InternetConnect failed with error %u\n", GetLastError());
5336 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5337
5338 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5339 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
5340 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
5341 0xdeadbeef);
5342 ok(req != NULL, "HttpOpenRequest failed\n");
5343 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5344
5345 flags = 0xdeadbeef;
5346 size = sizeof(flags);
5347 res = InternetQueryOptionW(req, 98, &flags, &size);
5348 if(!res && GetLastError() == ERROR_INVALID_PARAMETER) {
5349 win_skip("Incomplete security flags support, skipping\n");
5350
5351 close_async_handle(ses, hCompleteEvent, 2);
5352 CloseHandle(hCompleteEvent);
5353 return;
5354 }
5355
5356 test_secflags_option(req, 0, 0);
5357 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5358
5359 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_REVOCATION);
5360 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION, 0);
5361
5362 set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
5363 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID, 0);
5364
5365 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
5366 test_secflags_option(req, SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID, 0);
5367
5368 flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_SECURE;
5369 res = InternetSetOptionW(req, 99, &flags, sizeof(flags));
5370 ok(!res && GetLastError() == ERROR_INTERNET_OPTION_NOT_SETTABLE, "InternetSetOption(99) failed: %u\n", GetLastError());
5371
5372 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
5373 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
5374 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5375 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5376 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
5377 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
5378 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
5379 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
5380 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
5381 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
5382 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5383 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5384 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5385 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5386 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5387
5388 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5389 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5390
5391 WaitForSingleObject(hCompleteEvent, INFINITE);
5392 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
5393
5394 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
5395 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
5396 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
5397 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
5398 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5399 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5400 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
5401 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
5402 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5403 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5404 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5405 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5406 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5407
5408 test_request_flags(req, 0);
5409 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA
5410 |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_STRENGTH_STRONG, 0);
5411
5412 res = InternetReadFile(req, buf, sizeof(buf), &size);
5413 ok(res, "InternetReadFile failed: %u\n", GetLastError());
5414 ok(size, "size = 0\n");
5415
5416 /* Collect all existing persistent connections */
5417 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
5418 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
5419
5420 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5421 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
5422 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
5423 0xdeadbeef);
5424 ok(req != NULL, "HttpOpenRequest failed\n");
5425 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5426
5427 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT|INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY;
5428 res = InternetSetOptionA(req, INTERNET_OPTION_ERROR_MASK, (void*)&flags, sizeof(flags));
5429 ok(res, "InternetQueryOption(INTERNET_OPTION_ERROR_MASK failed: %u\n", GetLastError());
5430
5431 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5432 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5433 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
5434 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
5435 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
5436 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
5437 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
5438 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
5439 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5440 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5441 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5442
5443 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5444 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5445
5446 WaitForSingleObject(hCompleteEvent, INFINITE);
5447 ok(req_error == ERROR_INTERNET_SEC_CERT_REV_FAILED || broken(req_error == ERROR_INTERNET_SEC_CERT_ERRORS),
5448 "req_error = %d\n", req_error);
5449
5450 size = 0;
5451 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
5452 ok(res || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
5453 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %u\n", size);
5454 cert = HeapAlloc(GetProcessHeap(), 0, size);
5455 cert->lpszSubjectInfo = NULL;
5456 cert->lpszIssuerInfo = NULL;
5457 cert->lpszSignatureAlgName = (char *)0xdeadbeef;
5458 cert->lpszEncryptionAlgName = (char *)0xdeadbeef;
5459 cert->lpszProtocolName = (char *)0xdeadbeef;
5460 cert->dwKeySize = 0xdeadbeef;
5461 res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, cert, &size);
5462 ok(res, "InternetQueryOption failed: %u\n", GetLastError());
5463 if (res)
5464 {
5465 ok(cert->lpszSubjectInfo && strlen(cert->lpszSubjectInfo) > 1, "expected a non-empty subject name\n");
5466 ok(cert->lpszIssuerInfo && strlen(cert->lpszIssuerInfo) > 1, "expected a non-empty issuer name\n");
5467 ok(!cert->lpszSignatureAlgName, "unexpected signature algorithm name\n");
5468 ok(!cert->lpszEncryptionAlgName, "unexpected encryption algorithm name\n");
5469 ok(!cert->lpszProtocolName, "unexpected protocol name\n");
5470 ok(cert->dwKeySize != 0xdeadbeef, "unexpected key size\n");
5471 }
5472 HeapFree(GetProcessHeap(), 0, cert);
5473
5474 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
5475 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
5476 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
5477 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
5478 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5479 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5480 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5481
5482 if(req_error != ERROR_INTERNET_SEC_CERT_REV_FAILED) {
5483 win_skip("Unexpected cert errors %u, skipping security flags tests\n", req_error);
5484
5485 close_async_handle(ses, hCompleteEvent, 3);
5486 CloseHandle(hCompleteEvent);
5487 return;
5488 }
5489
5490 size = sizeof(buf);
5491 res = HttpQueryInfoA(req, HTTP_QUERY_CONTENT_ENCODING, buf, &size, 0);
5492 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfoA(HTTP_QUERY_CONTENT_ENCODING) failed: %u\n", GetLastError());
5493
5494 test_request_flags(req, 8);
5495 /* IE11 finds both rev failure and invalid CA. Previous versions required rev failure
5496 to be ignored before invalid CA was reported. */
5497 test_secflags_option(req, _SECURITY_FLAG_CERT_REV_FAILED, _SECURITY_FLAG_CERT_INVALID_CA);
5498
5499 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_REVOCATION);
5500 test_secflags_option(req, _SECURITY_FLAG_CERT_REV_FAILED|SECURITY_FLAG_IGNORE_REVOCATION, _SECURITY_FLAG_CERT_INVALID_CA);
5501
5502 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5503 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5504 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
5505 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
5506 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5507 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5508 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5509
5510 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5511 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5512
5513 WaitForSingleObject(hCompleteEvent, INFINITE);
5514 ok(req_error == ERROR_INTERNET_SEC_CERT_ERRORS, "req_error = %d\n", req_error);
5515
5516 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
5517 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
5518 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5519 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5520 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5521 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5522 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5523
5524 test_request_flags(req, INTERNET_REQFLAG_NO_HEADERS);
5525 test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
5526 test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5527
5528 set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
5529 test_secflags_option(req, _SECURITY_FLAG_CERT_INVALID_CA|_SECURITY_FLAG_CERT_REV_FAILED
5530 |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_UNKNOWN_CA, 0);
5531 test_http_version(req);
5532
5533 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5534 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5535 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
5536 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
5537 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
5538 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
5539 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
5540 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
5541 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5542 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5543 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5544 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5545 SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
5546
5547 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5548 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5549
5550 WaitForSingleObject(hCompleteEvent, INFINITE);
5551 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
5552
5553 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
5554 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
5555 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5556 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5557 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
5558 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
5559 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5560 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5561 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5562 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5563 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
5564
5565 test_request_flags(req, 0);
5566 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION
5567 |SECURITY_FLAG_STRENGTH_STRONG|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
5568
5569 test_cert_struct(req, &test_winehq_com_cert);
5570 test_security_info("https://test.winehq.com/data/some_file.html?q", 0,
5571 _SECURITY_FLAG_CERT_INVALID_CA|_SECURITY_FLAG_CERT_REV_FAILED);
5572
5573 res = InternetReadFile(req, buf, sizeof(buf), &size);
5574 ok(res, "InternetReadFile failed: %u\n", GetLastError());
5575 ok(size, "size = 0\n");
5576
5577 close_async_handle(ses, hCompleteEvent, 3);
5578
5579 /* Collect all existing persistent connections */
5580 res = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
5581 ok(res, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
5582
5583 /* Make another request, without setting security flags */
5584
5585 ses = InternetOpenA("WineTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
5586 ok(ses != NULL, "InternetOpen failed\n");
5587
5588 pInternetSetStatusCallbackA(ses, &callback);
5589
5590 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5591 conn = InternetConnectA(ses, "test.winehq.com", INTERNET_DEFAULT_HTTPS_PORT,
5592 NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0xdeadbeef);
5593 ok(conn != NULL, "InternetConnect failed with error %u\n", GetLastError());
5594 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5595
5596 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
5597 req = HttpOpenRequestA(conn, "GET", "/tests/hello.html", NULL, NULL, NULL,
5598 INTERNET_FLAG_SECURE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,
5599 0xdeadbeef);
5600 ok(req != NULL, "HttpOpenRequest failed\n");
5601 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
5602
5603 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
5604 |SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
5605 test_http_version(req);
5606
5607 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
5608 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
5609 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
5610 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
5611 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
5612 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
5613 SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
5614 SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
5615 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
5616 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
5617 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
5618 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
5619
5620 res = HttpSendRequestA(req, NULL, 0, NULL, 0);
5621 ok(!res && GetLastError() == ERROR_IO_PENDING, "HttpSendRequest failed: %u\n", GetLastError());
5622
5623 WaitForSingleObject(hCompleteEvent, INFINITE);
5624 ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
5625
5626 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
5627 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
5628 CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
5629 CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
5630 CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
5631 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
5632 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
5633 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
5634 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
5635 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
5636
5637 test_request_flags(req, 0);
5638 test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
5639 |SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
5640
5641 res = InternetReadFile(req, buf, sizeof(buf), &size);
5642 ok(res, "InternetReadFile failed: %u\n", GetLastError());
5643 ok(size, "size = 0\n");
5644
5645 close_async_handle(ses, hCompleteEvent, 2);
5646
5647 CloseHandle(hCompleteEvent);
5648
5649 test_security_info("http://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5650 test_security_info("file:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5651 test_security_info("xxx:///c:/dir/file.txt", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
5652 }
5653
5654 static void test_secure_connection(void)
5655 {
5656 static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
5657 static const WCHAR testsite[] = {'t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',0};
5658 static const WCHAR get[] = {'G','E','T',0};
5659 static const WCHAR testpage[] = {'/','t','e','s','t','s','/','h','e','l','l','o','.','h','t','m','l',0};
5660 HINTERNET ses, con, req;
5661 DWORD size, flags;
5662 INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
5663 INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
5664 BOOL ret;
5665
5666 ses = InternetOpenA("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
5667 ok(ses != NULL, "InternetOpen failed\n");
5668
5669 con = InternetConnectA(ses, "test.winehq.org",
5670 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
5671 INTERNET_SERVICE_HTTP, 0, 0);
5672 ok(con != NULL, "InternetConnect failed\n");
5673
5674 req = HttpOpenRequestA(con, "GET", "/tests/hello.html", NULL, NULL, NULL,
5675 INTERNET_FLAG_SECURE, 0);
5676 ok(req != NULL, "HttpOpenRequest failed\n");
5677
5678 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
5679 ok(ret || broken(GetLastError() == ERROR_INTERNET_CANNOT_CONNECT),
5680 "HttpSendRequest failed: %d\n", GetLastError());
5681 if (!ret)
5682 {
5683 win_skip("Cannot connect to https.\n");
5684 goto done;
5685 }
5686
5687 size = sizeof(flags);
5688 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
5689 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5690 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
5691
5692 test_cert_struct(req, &test_winehq_org_cert);
5693
5694 /* Querying the same option through InternetQueryOptionW still results in
5695 * ASCII strings being returned.
5696 */
5697 size = 0;
5698 ret = InternetQueryOptionW(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_INFOW), "size = %d\n", size);
5702 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
5703 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5704 certificate_structW, &size);
5705 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
5706 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5707 if (ret)
5708 {
5709 ok(certificate_structA->lpszSubjectInfo &&
5710 strlen(certificate_structA->lpszSubjectInfo) > 1,
5711 "expected a non-empty subject name\n");
5712 ok(certificate_structA->lpszIssuerInfo &&
5713 strlen(certificate_structA->lpszIssuerInfo) > 1,
5714 "expected a non-empty issuer name\n");
5715 ok(!certificate_structA->lpszSignatureAlgName,
5716 "unexpected signature algorithm name\n");
5717 ok(!certificate_structA->lpszEncryptionAlgName,
5718 "unexpected encryption algorithm name\n");
5719 ok(!certificate_structA->lpszProtocolName,
5720 "unexpected protocol name\n");
5721 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
5722 release_cert_info(certificate_structA);
5723 }
5724 HeapFree(GetProcessHeap(), 0, certificate_structW);
5725
5726 InternetCloseHandle(req);
5727 InternetCloseHandle(con);
5728 InternetCloseHandle(ses);
5729
5730 /* Repeating the tests with the W functions has the same result: */
5731 ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
5732 ok(ses != NULL, "InternetOpen failed\n");
5733
5734 con = InternetConnectW(ses, testsite,
5735 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
5736 INTERNET_SERVICE_HTTP, 0, 0);
5737 ok(con != NULL, "InternetConnect failed\n");
5738
5739 req = HttpOpenRequestW(con, get, testpage, NULL, NULL, NULL,
5740 INTERNET_FLAG_SECURE|INTERNET_FLAG_RELOAD, 0);
5741 ok(req != NULL, "HttpOpenRequest failed\n");
5742
5743 ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
5744 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
5745
5746 size = sizeof(flags);
5747 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
5748 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5749 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set, got %x\n", flags);
5750
5751 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5752 NULL, &size);
5753 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
5754 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
5755 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
5756 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5757 certificate_structA, &size);
5758 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5759 if (ret)
5760 {
5761 ok(certificate_structA->lpszSubjectInfo &&
5762 strlen(certificate_structA->lpszSubjectInfo) > 1,
5763 "expected a non-empty subject name\n");
5764 ok(certificate_structA->lpszIssuerInfo &&
5765 strlen(certificate_structA->lpszIssuerInfo) > 1,
5766 "expected a non-empty issuer name\n");
5767 ok(!certificate_structA->lpszSignatureAlgName,
5768 "unexpected signature algorithm name\n");
5769 ok(!certificate_structA->lpszEncryptionAlgName,
5770 "unexpected encryption algorithm name\n");
5771 ok(!certificate_structA->lpszProtocolName,
5772 "unexpected protocol name\n");
5773 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
5774 release_cert_info(certificate_structA);
5775 }
5776 HeapFree(GetProcessHeap(), 0, certificate_structA);
5777
5778 /* Again, querying the same option through InternetQueryOptionW still
5779 * results in ASCII strings being returned.
5780 */
5781 size = 0;
5782 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5783 NULL, &size);
5784 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
5785 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
5786 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
5787 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
5788 certificate_structW, &size);
5789 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
5790 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
5791 if (ret)
5792 {
5793 ok(certificate_structA->lpszSubjectInfo &&
5794 strlen(certificate_structA->lpszSubjectInfo) > 1,
5795 "expected a non-empty subject name\n");
5796 ok(certificate_structA->lpszIssuerInfo &&
5797 strlen(certificate_structA->lpszIssuerInfo) > 1,
5798 "expected a non-empty issuer name\n");
5799 ok(!certificate_structA->lpszSignatureAlgName,
5800 "unexpected signature algorithm name\n");
5801 ok(!certificate_structA->lpszEncryptionAlgName,
5802 "unexpected encryption algorithm name\n");
5803 ok(!certificate_structA->lpszProtocolName,
5804 "unexpected protocol name\n");
5805 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
5806 release_cert_info(certificate_structA);
5807 }
5808 HeapFree(GetProcessHeap(), 0, certificate_structW);
5809
5810 done:
5811 InternetCloseHandle(req);
5812 InternetCloseHandle(con);
5813 InternetCloseHandle(ses);
5814 }
5815
5816 static void test_user_agent_header(void)
5817 {
5818 HINTERNET ses, con, req;
5819 DWORD size, err;
5820 char buffer[64];
5821 BOOL ret;
5822
5823 ses = InternetOpenA("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
5824 ok(ses != NULL, "InternetOpen failed\n");
5825
5826 con = InternetConnectA(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
5827 ok(con != NULL, "InternetConnect failed\n");
5828
5829 req = HttpOpenRequestA(con, "GET", "/tests/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
5830 ok(req != NULL, "HttpOpenRequest failed\n");
5831
5832 size = sizeof(buffer);
5833 ret = HttpQueryInfoA(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5834 err = GetLastError();
5835 ok(!ret, "HttpQueryInfo succeeded\n");
5836 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
5837
5838 ret = HttpAddRequestHeadersA(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
5839 ok(ret, "HttpAddRequestHeaders succeeded\n");
5840
5841 size = sizeof(buffer);
5842 ret = HttpQueryInfoA(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5843 err = GetLastError();
5844 ok(ret, "HttpQueryInfo failed\n");
5845
5846 InternetCloseHandle(req);
5847
5848 req = HttpOpenRequestA(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
5849 ok(req != NULL, "HttpOpenRequest failed\n");
5850
5851 size = sizeof(buffer);
5852 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5853 err = GetLastError();
5854 ok(!ret, "HttpQueryInfo succeeded\n");
5855 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
5856
5857 ret = HttpAddRequestHeadersA(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
5858 ok(ret, "HttpAddRequestHeaders failed\n");
5859
5860 buffer[0] = 0;
5861 size = sizeof(buffer);
5862 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5863 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
5864 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
5865
5866 InternetCloseHandle(req);
5867 InternetCloseHandle(con);
5868 InternetCloseHandle(ses);
5869 }
5870
5871 static void test_bogus_accept_types_array(void)
5872 {
5873 HINTERNET ses, con, req;
5874 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
5875 DWORD size, error;
5876 char buffer[32];
5877 BOOL ret;
5878
5879 ses = InternetOpenA("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
5880 con = InternetConnectA(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
5881 req = HttpOpenRequestA(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
5882
5883 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
5884
5885 buffer[0] = 0;
5886 size = sizeof(buffer);
5887 SetLastError(0xdeadbeef);
5888 ret = HttpQueryInfoA(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
5889 error = GetLastError();
5890 ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
5891 if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", error);
5892 ok(broken(!strcmp(buffer, ", */*, %p, , , */*")) /* IE6 */ ||
5893 broken(!strcmp(buffer, "*/*, %p, */*")) /* IE7/8 */ ||
5894 !strcmp(buffer, ""), "got '%s' expected ''\n", buffer);
5895
5896 InternetCloseHandle(req);
5897 InternetCloseHandle(con);
5898 InternetCloseHandle(ses);
5899 }
5900
5901 struct context
5902 {
5903 HANDLE event;
5904 HINTERNET req;
5905 };
5906
5907 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
5908 {
5909 INTERNET_ASYNC_RESULT *result = info;
5910 struct context *ctx = (struct context *)context;
5911
5912 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
5913
5914 switch(status) {
5915 case INTERNET_STATUS_REQUEST_COMPLETE:
5916 trace("request handle: 0x%08lx\n", result->dwResult);
5917 ctx->req = (HINTERNET)result->dwResult;
5918 SetEvent(ctx->event);
5919 break;
5920 case INTERNET_STATUS_HANDLE_CLOSING: {
5921 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
5922
5923 if (InternetQueryOptionA(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
5924 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
5925 SetEvent(ctx->event);
5926 break;
5927 }
5928 case INTERNET_STATUS_NAME_RESOLVED:
5929 case INTERNET_STATUS_CONNECTING_TO_SERVER:
5930 case INTERNET_STATUS_CONNECTED_TO_SERVER: {
5931 char *str = info;
5932 ok(str[0] && str[1], "Got string: %s\n", str);
5933 ok(size == strlen(str)+1, "unexpected size %u\n", size);
5934 }
5935 }
5936 }
5937
5938 static void test_open_url_async(void)
5939 {
5940 BOOL ret;
5941 HINTERNET ses, req;
5942 DWORD size, error;
5943 struct context ctx;
5944 ULONG type;
5945
5946 /* Collect all existing persistent connections */
5947 ret = InternetSetOptionA(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
5948 ok(ret, "InternetSetOption(INTERNET_OPTION_END_BROWSER_SESSION) failed: %u\n", GetLastError());
5949
5950 /*
5951 * Some versions of IE6 fail those tests. They pass some notification data as UNICODE string, while
5952 * other versions never do. They also hang of following tests. We disable it for everything older
5953 * than IE7.
5954 */
5955 if(!is_ie7plus)
5956 return;
5957
5958 ctx.req = NULL;
5959 ctx.event = CreateEventA(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
5960
5961 ses = InternetOpenA("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
5962 ok(ses != NULL, "InternetOpen failed\n");
5963
5964 SetLastError(0xdeadbeef);
5965 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
5966 error = GetLastError();
5967 ok(!ret, "InternetSetOptionA succeeded\n");
5968 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
5969
5970 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
5971 error = GetLastError();
5972 ok(!ret, "InternetSetOptionA failed\n");
5973 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
5974
5975 pInternetSetStatusCallbackW(ses, cb);
5976 ResetEvent(ctx.event);
5977
5978 req = InternetOpenUrlA(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
5979 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
5980
5981 WaitForSingleObject(ctx.event, INFINITE);
5982
5983 type = 0;
5984 size = sizeof(type);
5985 ret = InternetQueryOptionA(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
5986 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
5987 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
5988 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
5989
5990 size = 0;
5991 ret = HttpQueryInfoA(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
5992 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
5993 ok(size > 0, "expected size > 0\n");
5994
5995 ResetEvent(ctx.event);
5996 InternetCloseHandle(ctx.req);
5997 WaitForSingleObject(ctx.event, INFINITE);
5998
5999 InternetCloseHandle(ses);
6000 CloseHandle(ctx.event);
6001 }
6002
6003 enum api
6004 {
6005 internet_connect = 1,
6006 http_open_request,
6007 http_send_request_ex,
6008 internet_writefile,
6009 http_end_request,
6010 internet_close_handle
6011 };
6012
6013 struct notification
6014 {
6015 enum api function; /* api responsible for notification */
6016 unsigned int status; /* status received */
6017 BOOL async; /* delivered from another thread? */
6018 BOOL todo;
6019 BOOL optional;
6020 };
6021
6022 struct info
6023 {
6024 enum api function;
6025 const struct notification *test;
6026 unsigned int count;
6027 unsigned int index;
6028 HANDLE wait;
6029 DWORD thread;
6030 unsigned int line;
6031 DWORD expect_result;
6032 BOOL is_aborted;
6033 };
6034
6035 static CRITICAL_SECTION notification_cs;
6036
6037 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
6038 {
6039 BOOL status_ok, function_ok;
6040 struct info *info = (struct info *)context;
6041 unsigned int i;
6042
6043 EnterCriticalSection( &notification_cs );
6044
6045 if(info->is_aborted) {
6046 LeaveCriticalSection(&notification_cs);
6047 return;
6048 }
6049
6050 if (status == INTERNET_STATUS_HANDLE_CREATED)
6051 {
6052 DWORD size = sizeof(struct info *);
6053 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
6054 }else if(status == INTERNET_STATUS_REQUEST_COMPLETE) {
6055 INTERNET_ASYNC_RESULT *ar = (INTERNET_ASYNC_RESULT*)buffer;
6056
6057 ok(buflen == sizeof(*ar), "unexpected buflen = %d\n", buflen);
6058 if(info->expect_result == ERROR_SUCCESS) {
6059 ok(ar->dwResult == 1, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
6060 }else {
6061 ok(!ar->dwResult, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
6062 ok(ar->dwError == info->expect_result, "ar->dwError = %d, expected %d\n", ar->dwError, info->expect_result);
6063 }
6064 }
6065
6066 i = info->index;
6067 if (i >= info->count)
6068 {
6069 LeaveCriticalSection( &notification_cs );
6070 return;
6071 }
6072
6073 while (info->test[i].status != status &&
6074 (info->test[i].optional || info->test[i].todo) &&
6075 i < info->count - 1 &&
6076 info->test[i].function == info->test[i + 1].function)
6077 {
6078 i++;
6079 }
6080
6081 status_ok = (info->test[i].status == status);
6082 function_ok = (info->test[i].function == info->function);
6083
6084 if (!info->test[i].todo)
6085 {
6086 ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
6087 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
6088
6089 if (info->test[i].async)
6090 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
6091 info->line, info->thread, GetCurrentThreadId());
6092 }
6093 else
6094 {
6095 todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
6096 if (status_ok)
6097 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
6098 }
6099 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
6100 info->index = i+1;
6101
6102 LeaveCriticalSection( &notification_cs );
6103 }
6104
6105 static void setup_test( struct info *info, enum api function, unsigned int line, DWORD expect_result )
6106 {
6107 info->function = function;
6108 info->line = line;
6109 info->expect_result = expect_result;
6110 }
6111
6112 struct notification_data
6113 {
6114 const struct notification *test;
6115 const unsigned int count;
6116 const char *method;
6117 const char *host;
6118 const char *path;
6119 const char *data;
6120 BOOL expect_conn_failure;
6121 };
6122
6123 static const struct notification async_send_request_ex_test[] =
6124 {
6125 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6126 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6127 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6128 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
6129 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
6130 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
6131 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE },
6132 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE },
6133 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
6134 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
6135 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6136 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, FALSE },
6137 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, FALSE },
6138 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
6139 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
6140 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6141 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
6142 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
6143 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
6144 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
6145 };
6146
6147 static const struct notification async_send_request_ex_test2[] =
6148 {
6149 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6150 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6151 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6152 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
6153 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
6154 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
6155 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE, TRUE },
6156 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE, TRUE },
6157 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
6158 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
6159 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6160 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
6161 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
6162 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6163 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
6164 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
6165 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
6166 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
6167 };
6168
6169 static const struct notification async_send_request_ex_resolve_failure_test[] =
6170 {
6171 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6172 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, FALSE },
6173 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6174 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE },
6175 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6176 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6177 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6178 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, FALSE, FALSE, TRUE },
6179 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, FALSE, FALSE, TRUE },
6180 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, },
6181 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, FALSE, }
6182 };
6183
6184 static const struct notification async_send_request_ex_chunked_test[] =
6185 {
6186 { internet_connect, INTERNET_STATUS_HANDLE_CREATED },
6187 { http_open_request, INTERNET_STATUS_HANDLE_CREATED },
6188 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, TRUE, FALSE, TRUE },
6189 { http_send_request_ex, INTERNET_STATUS_COOKIE_SENT, TRUE, FALSE, TRUE },
6190 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, TRUE, FALSE, TRUE },
6191 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, TRUE, FALSE, TRUE },
6192 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, TRUE },
6193 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, TRUE },
6194 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, TRUE },
6195 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, TRUE },
6196 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6197 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, TRUE },
6198 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, TRUE },
6199 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, TRUE },
6200 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION },
6201 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED },
6202 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING },
6203 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING }
6204 };
6205
6206 static const struct notification_data notification_data[] = {
6207 {
6208 async_send_request_ex_chunked_test,
6209 sizeof(async_send_request_ex_chunked_test)/sizeof(async_send_request_ex_chunked_test[0]),
6210 "GET",
6211 "test.winehq.org",
6212 "tests/data.php"
6213 },
6214 {
6215 async_send_request_ex_test,
6216 sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
6217 "POST",
6218 "test.winehq.org",
6219 "tests/post.php",
6220 "Public ID=codeweavers"
6221 },
6222 {
6223 async_send_request_ex_test2,
6224 sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
6225 "POST",
6226 "test.winehq.org",
6227 "tests/post.php"
6228 },
6229 {
6230 async_send_request_ex_resolve_failure_test,
6231 sizeof(async_send_request_ex_resolve_failure_test)/sizeof(async_send_request_ex_resolve_failure_test[0]),
6232 "GET",
6233 "brokenhost",
6234 "index.html",
6235 NULL,
6236 TRUE
6237 }
6238 };
6239
6240 static void test_async_HttpSendRequestEx(const struct notification_data *nd)
6241 {
6242 BOOL ret;
6243 HINTERNET ses, req, con;
6244 struct info info;
6245 DWORD size, written, error;
6246 INTERNET_BUFFERSA b;
6247 static const char *accept[2] = {"*/*", NULL};
6248 char buffer[32];
6249
6250 trace("Async HttpSendRequestEx test (%s %s)\n", nd->method, nd->host);
6251
6252 InitializeCriticalSection( &notification_cs );
6253
6254 info.test = nd->test;
6255 info.count = nd->count;
6256 info.index = 0;
6257 info.wait = CreateEventW( NULL, FALSE, FALSE, NULL );
6258 info.thread = GetCurrentThreadId();
6259 info.is_aborted = FALSE;
6260
6261 ses = InternetOpenA( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
6262 ok( ses != NULL, "InternetOpen failed\n" );
6263
6264 pInternetSetStatusCallbackA( ses, check_notification );
6265
6266 setup_test( &info, internet_connect, __LINE__, ERROR_SUCCESS );
6267 con = InternetConnectA( ses, nd->host, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
6268 ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
6269
6270 WaitForSingleObject( info.wait, 10000 );
6271
6272 setup_test( &info, http_open_request, __LINE__, ERROR_SUCCESS );
6273 req = HttpOpenRequestA( con, nd->method, nd->path, NULL, NULL, accept, 0, (DWORD_PTR)&info );
6274 ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
6275
6276 WaitForSingleObject( info.wait, 10000 );
6277
6278 if(nd->data) {
6279 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
6280 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
6281 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
6282 b.dwHeadersLength = strlen( b.lpcszHeader );
6283 b.dwBufferTotal = nd->data ? strlen( nd->data ) : 0;
6284 }
6285
6286 setup_test( &info, http_send_request_ex, __LINE__,
6287 nd->expect_conn_failure ? ERROR_INTERNET_NAME_NOT_RESOLVED : ERROR_SUCCESS );
6288 ret = HttpSendRequestExA( req, nd->data ? &b : NULL, NULL, 0x28, 0 );
6289 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
6290
6291 error = WaitForSingleObject( info.wait, 10000 );
6292 if(error != WAIT_OBJECT_0) {
6293 skip("WaitForSingleObject returned %d, assuming DNS problem\n", error);
6294 info.is_aborted = TRUE;
6295 goto abort;
6296 }
6297
6298 size = sizeof(buffer);
6299 SetLastError( 0xdeadbeef );
6300 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
6301 error = GetLastError();
6302 ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
6303 if(nd->expect_conn_failure) {
6304 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND got %u\n", error );
6305 }else {
6306 todo_wine
6307 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
6308 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
6309 }
6310
6311 if (nd->data)
6312 {
6313 written = 0;
6314 size = strlen( nd->data );
6315 setup_test( &info, internet_writefile, __LINE__, ERROR_SUCCESS );
6316 ret = InternetWriteFile( req, nd->data, size, &written );
6317 ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
6318 ok( written == size, "expected %u got %u\n", written, size );
6319
6320 WaitForSingleObject( info.wait, 10000 );
6321
6322 SetLastError( 0xdeadbeef );
6323 ret = HttpEndRequestA( req, (void *)nd->data, 0x28, 0 );
6324 error = GetLastError();
6325 ok( !ret, "HttpEndRequestA succeeded\n" );
6326 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
6327 }
6328
6329 SetLastError( 0xdeadbeef );
6330 setup_test( &info, http_end_request, __LINE__,
6331 nd->expect_conn_failure ? ERROR_INTERNET_OPERATION_CANCELLED : ERROR_SUCCESS);
6332 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
6333 error = GetLastError();
6334 ok( !ret, "HttpEndRequestA succeeded\n" );
6335 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
6336
6337 WaitForSingleObject( info.wait, 10000 );
6338
6339 setup_test( &info, internet_close_handle, __LINE__, ERROR_SUCCESS );
6340 abort:
6341 InternetCloseHandle( req );
6342 InternetCloseHandle( con );
6343 InternetCloseHandle( ses );
6344
6345 WaitForSingleObject( info.wait, 10000 );
6346 Sleep(100);
6347 CloseHandle( info.wait );
6348 DeleteCriticalSection( &notification_cs );
6349 }
6350
6351 static HINTERNET closetest_session, closetest_req, closetest_conn;
6352 static BOOL closetest_closed;
6353
6354 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
6355 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
6356 {
6357 DWORD len, type;
6358 BOOL res;
6359
6360 trace("closetest_callback %p: %d\n", hInternet, dwInternetStatus);
6361
6362 ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
6363 "Unexpected hInternet %p\n", hInternet);
6364 if(!closetest_closed)
6365 return;
6366
6367 len = sizeof(type);
6368 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
6369 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
6370 "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
6371 closetest_req, res, GetLastError());
6372 }
6373
6374 static void test_InternetCloseHandle(void)
6375 {
6376 DWORD len, flags;
6377 BOOL res;
6378
6379 closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
6380 ok(closetest_session != NULL,"InternetOpen failed with error %u\n", GetLastError());
6381
6382 pInternetSetStatusCallbackA(closetest_session, closetest_callback);
6383
6384 closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
6385 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
6386 ok(closetest_conn != NULL,"InternetConnect failed with error %u\n", GetLastError());
6387
6388 closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
6389 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
6390
6391 res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
6392 ok(!res && (GetLastError() == ERROR_IO_PENDING),
6393 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
6394
6395 test_request_flags(closetest_req, INTERNET_REQFLAG_NO_HEADERS);
6396
6397 res = InternetCloseHandle(closetest_session);
6398 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
6399 closetest_closed = TRUE;
6400 trace("Closed session handle\n");
6401
6402 res = InternetCloseHandle(closetest_conn);
6403 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %u\n",
6404 res, GetLastError());
6405
6406 res = InternetCloseHandle(closetest_req);
6407 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %u\n",
6408 res, GetLastError());
6409
6410 len = sizeof(flags);
6411 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
6412 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
6413 "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
6414 closetest_req, res, GetLastError());
6415 }
6416
6417 static void test_connection_failure(void)
6418 {
6419 test_request_t req;
6420 DWORD error;
6421 BOOL ret;
6422
6423 open_simple_request(&req, "localhost", 1, NULL, "/");
6424
6425 SetLastError(0xdeadbeef);
6426 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
6427 error = GetLastError();
6428 ok(!ret, "unexpected success\n");
6429 ok(error == ERROR_INTERNET_CANNOT_CONNECT, "wrong error %u\n", error);
6430
6431 close_request(&req);
6432 }
6433
6434 static void test_default_service_port(void)
6435 {
6436 HINTERNET session, connect, request;
6437 DWORD size, error;
6438 char buffer[128];
6439 BOOL ret;
6440
6441 if(!is_ie7plus)
6442 return;
6443
6444 session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
6445 ok(session != NULL, "InternetOpen failed\n");
6446
6447 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
6448 INTERNET_SERVICE_HTTP, 0, 0);
6449 ok(connect != NULL, "InternetConnect failed\n");
6450
6451 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
6452 ok(request != NULL, "HttpOpenRequest failed\n");
6453
6454 SetLastError(0xdeadbeef);
6455 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
6456 error = GetLastError();
6457 ok(!ret, "HttpSendRequest succeeded\n");
6458 ok(error == ERROR_INTERNET_SECURITY_CHANNEL_ERROR || error == ERROR_INTERNET_CANNOT_CONNECT,
6459 "got %u\n", error);
6460
6461 size = sizeof(buffer);
6462 memset(buffer, 0, sizeof(buffer));
6463 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
6464 ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
6465 ok(!strcmp(buffer, "test.winehq.org:80"), "Expected test.winehg.org:80, got '%s'\n", buffer);
6466
6467 InternetCloseHandle(request);
6468 InternetCloseHandle(connect);
6469
6470 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
6471 INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
6472 ok(connect != NULL, "InternetConnect failed\n");
6473
6474 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
6475 ok(request != NULL, "HttpOpenRequest failed\n");
6476
6477 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
6478 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
6479
6480 size = sizeof(buffer);
6481 memset(buffer, 0, sizeof(buffer));
6482 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
6483 ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
6484 ok(!strcmp(buffer, "test.winehq.org"), "Expected test.winehg.org, got '%s'\n", buffer);
6485
6486 InternetCloseHandle(request);
6487 InternetCloseHandle(connect);
6488
6489 connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
6490 INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
6491 ok(connect != NULL, "InternetConnect failed\n");
6492
6493 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, 0, 0);
6494 ok(request != NULL, "HttpOpenRequest failed\n");
6495
6496 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
6497 ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
6498
6499 size = sizeof(buffer);
6500 memset(buffer, 0, sizeof(buffer));
6501 ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
6502 ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
6503 ok(!strcmp(buffer, "test.winehq.org:443"), "Expected test.winehg.org:443, got '%s'\n", buffer);
6504
6505 InternetCloseHandle(request);
6506 InternetCloseHandle(connect);
6507 InternetCloseHandle(session);
6508 }
6509
6510 static void init_status_tests(void)
6511 {
6512 memset(expect, 0, sizeof(expect));
6513 memset(optional, 0, sizeof(optional));
6514 memset(wine_allow, 0, sizeof(wine_allow));
6515 memset(notified, 0, sizeof(notified));
6516 memset(status_string, 0, sizeof(status_string));
6517
6518 #define STATUS_STRING(status) status_string[status] = #status
6519 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
6520 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
6521 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
6522 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
6523 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
6524 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
6525 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
6526 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
6527 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
6528 STATUS_STRING(INTERNET_STATUS_PREFETCH);
6529 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
6530 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
6531 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
6532 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
6533 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
6534 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
6535 STATUS_STRING(INTERNET_STATUS_REDIRECT);
6536 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
6537 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
6538 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
6539 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
6540 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
6541 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
6542 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
6543 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
6544 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
6545 #undef STATUS_STRING
6546 }
6547
6548 static void WINAPI header_cb( HINTERNET handle, DWORD_PTR ctx, DWORD status, LPVOID info, DWORD len )
6549 {
6550 BOOL ret;
6551 DWORD index, size;
6552 char buf[256];
6553
6554 if (status == INTERNET_STATUS_SENDING_REQUEST)
6555 {
6556 ret = HttpAddRequestHeadersA( handle, "winetest: winetest", ~0u, HTTP_ADDREQ_FLAG_ADD );
6557 ok( ret, "HttpAddRequestHeadersA failed %u\n", GetLastError() );
6558 SetEvent( (HANDLE)ctx );
6559 }
6560 else if (status == INTERNET_STATUS_REQUEST_SENT)
6561 {
6562 index = 0;
6563 size = sizeof(buf);
6564 ret = HttpQueryInfoA( handle, HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
6565 buf, &size, &index );
6566 ok( ret, "HttpQueryInfoA failed %u\n", GetLastError() );
6567 ok( strstr( buf, "winetest: winetest" ) != NULL, "header missing\n" );
6568 SetEvent( (HANDLE)ctx );
6569 }
6570 }
6571
6572 static void test_concurrent_header_access(void)
6573 {
6574 HINTERNET ses, con, req;
6575 DWORD err;
6576 BOOL ret;
6577 HANDLE wait = CreateEventW( NULL, FALSE, FALSE, NULL );
6578
6579 ses = InternetOpenA( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
6580 ok( ses != NULL, "InternetOpenA failed\n" );
6581
6582 con = InternetConnectA( ses, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
6583 INTERNET_SERVICE_HTTP, 0, 0 );
6584 ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
6585
6586 req = HttpOpenRequestA( con, NULL, "/", NULL, NULL, NULL, 0, (DWORD_PTR)wait );
6587 ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
6588
6589 pInternetSetStatusCallbackA( req, header_cb );
6590
6591 SetLastError( 0xdeadbeef );
6592 ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
6593 err = GetLastError();
6594 ok( !ret, "HttpSendRequestA succeeded\n" );
6595 ok( err == ERROR_IO_PENDING, "got %u\n", ERROR_IO_PENDING );
6596
6597 WaitForSingleObject( wait, 5000 );
6598 WaitForSingleObject( wait, 5000 );
6599
6600 InternetCloseHandle( req );
6601 InternetCloseHandle( con );
6602 InternetCloseHandle( ses );
6603 CloseHandle( wait );
6604 }
6605
6606 START_TEST(http)
6607 {
6608 HMODULE hdll;
6609 hdll = GetModuleHandleA("wininet.dll");
6610
6611 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
6612 win_skip("Too old IE (older than 6.0)\n");
6613 return;
6614 }
6615
6616 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
6617 pInternetSetStatusCallbackW = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackW");
6618 pInternetGetSecurityInfoByURLA = (void*)GetProcAddress(hdll, "InternetGetSecurityInfoByURLA");
6619
6620 if(!pInternetGetSecurityInfoByURLA) {
6621 is_ie7plus = FALSE;
6622 win_skip("IE6 found. It's too old for some tests.\n");
6623 }
6624
6625 init_status_tests();
6626 test_InternetCloseHandle();
6627 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
6628 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
6629 InternetReadFile_test(0, &test_data[1]);
6630 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
6631 test_security_flags();
6632 InternetReadFile_test(0, &test_data[2]);
6633 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
6634 test_open_url_async();
6635 test_async_HttpSendRequestEx(&notification_data[0]);
6636 test_async_HttpSendRequestEx(&notification_data[1]);
6637 test_async_HttpSendRequestEx(&notification_data[2]);
6638 test_async_HttpSendRequestEx(&notification_data[3]);
6639 InternetOpenRequest_test();
6640 test_http_cache();
6641 InternetLockRequestFile_test();
6642 InternetOpenUrlA_test();
6643 HttpHeaders_test();
6644 test_http_connection();
6645 test_secure_connection();
6646 test_user_agent_header();
6647 test_bogus_accept_types_array();
6648 InternetReadFile_chunked_test();
6649 HttpSendRequestEx_test();
6650 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[3]);
6651 test_connection_failure();
6652 test_default_service_port();
6653 test_concurrent_header_access();
6654 }