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