mshtml: Added IHTMLWindow2::focus implementation.
[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 "winsock.h"
31
32 #include "wine/test.h"
33
34 #define TEST_URL "http://test.winehq.org/hello.html"
35
36 static BOOL first_connection_to_test_url = TRUE;
37
38 /* Adapted from dlls/urlmon/tests/protocol.c */
39
40 #define SET_EXPECT2(status, num) \
41     expect[status] = num
42
43 #define SET_EXPECT(status) \
44     SET_EXPECT2(status, 1)
45
46 #define SET_OPTIONAL2(status, num) \
47     optional[status] = num
48
49 #define SET_OPTIONAL(status) \
50     SET_OPTIONAL2(status, 1)
51
52 /* SET_WINE_ALLOW's should be used with an appropriate
53  * todo_wine CHECK_NOTIFIED at a later point in the code */
54 #define SET_WINE_ALLOW2(status, num) \
55     wine_allow[status] = num
56
57 #define SET_WINE_ALLOW(status) \
58     SET_WINE_ALLOW2(status, 1)
59
60 #define CHECK_EXPECT(status) \
61     do { \
62         if (!expect[status] && !optional[status] && wine_allow[status]) \
63         { \
64             todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
65                          status < MAX_INTERNET_STATUS && status_string[status] ? \
66                          status_string[status] : "unknown");            \
67             wine_allow[status]--; \
68         } \
69         else \
70         { \
71             ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status,   \
72                status < MAX_INTERNET_STATUS && status_string[status] ? \
73                status_string[status] : "unknown");                      \
74             if (expect[status]) expect[status]--; \
75             else optional[status]--; \
76         } \
77         notified[status]++; \
78     }while(0)
79
80 /* CLEAR_NOTIFIED used in cases when notification behavior
81  * differs between Windows versions */
82 #define CLEAR_NOTIFIED(status) \
83     expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
84
85 #define CHECK_NOTIFIED2(status, num) \
86     do { \
87         ok(notified[status] + optional[status] == (num), \
88            "expected status %d (%s) %d times, received %d times\n", \
89            status, status < MAX_INTERNET_STATUS && status_string[status] ? \
90            status_string[status] : "unknown", (num), notified[status]); \
91         CLEAR_NOTIFIED(status);                                         \
92     }while(0)
93
94 #define CHECK_NOTIFIED(status) \
95     CHECK_NOTIFIED2(status, 1)
96
97 #define CHECK_NOT_NOTIFIED(status) \
98     CHECK_NOTIFIED2(status, 0)
99
100 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
101 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
102     wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
103 static const char *status_string[MAX_INTERNET_STATUS];
104
105 static HANDLE hCompleteEvent;
106
107 #define TESTF_REDIRECT      0x01
108 #define TESTF_COMPRESSED    0x02
109 #define TESTF_ALLOW_COOKIE  0x04
110
111 typedef struct {
112     const char *url;
113     const char *redirected_url;
114     const char *host;
115     const char *path;
116     const char *headers;
117     DWORD flags;
118     const char *post_data;
119     const char *content;
120 } test_data_t;
121
122 static const test_data_t test_data[] = {
123     {
124         "http://test.winehq.org/testredirect",
125         "http://test.winehq.org/hello.html",
126         "test.winehq.org",
127         "/testredirect",
128         "",
129         TESTF_REDIRECT
130     },
131     {
132         "http://www.codeweavers.com/",
133         "http://www.codeweavers.com/",
134         "www.codeweavers.com",
135         "",
136         "Accept-Encoding: gzip, deflate",
137         TESTF_COMPRESSED|TESTF_ALLOW_COOKIE
138     },
139     {
140         "http://crossover.codeweavers.com/posttest.php",
141         "http://crossover.codeweavers.com/posttest.php",
142         "crossover.codeweavers.com",
143         "/posttest.php",
144         "Content-Type: application/x-www-form-urlencoded",
145         0,
146         "mode=Test",
147         "mode => Test\n"
148     }
149 };
150
151 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
152
153 static BOOL proxy_active(void)
154 {
155     HKEY internet_settings;
156     DWORD proxy_enable;
157     DWORD size;
158
159     if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
160                       0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
161         return FALSE;
162
163     size = sizeof(DWORD);
164     if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
165         proxy_enable = 0;
166
167     RegCloseKey(internet_settings);
168
169     return proxy_enable != 0;
170 }
171
172 static VOID WINAPI callback(
173      HINTERNET hInternet,
174      DWORD_PTR dwContext,
175      DWORD dwInternetStatus,
176      LPVOID lpvStatusInformation,
177      DWORD dwStatusInformationLength
178 )
179 {
180     CHECK_EXPECT(dwInternetStatus);
181     switch (dwInternetStatus)
182     {
183         case INTERNET_STATUS_RESOLVING_NAME:
184             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
185                 GetCurrentThreadId(), hInternet, dwContext,
186                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
187             *(LPSTR)lpvStatusInformation = '\0';
188             break;
189         case INTERNET_STATUS_NAME_RESOLVED:
190             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
191                 GetCurrentThreadId(), hInternet, dwContext,
192                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
193             *(LPSTR)lpvStatusInformation = '\0';
194             break;
195         case INTERNET_STATUS_CONNECTING_TO_SERVER:
196             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
197                 GetCurrentThreadId(), hInternet, dwContext,
198                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
199             *(LPSTR)lpvStatusInformation = '\0';
200             break;
201         case INTERNET_STATUS_CONNECTED_TO_SERVER:
202             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
203                 GetCurrentThreadId(), hInternet, dwContext,
204                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
205             *(LPSTR)lpvStatusInformation = '\0';
206             break;
207         case INTERNET_STATUS_SENDING_REQUEST:
208             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
209                 GetCurrentThreadId(), hInternet, dwContext,
210                 lpvStatusInformation,dwStatusInformationLength);
211             break;
212         case INTERNET_STATUS_REQUEST_SENT:
213             ok(dwStatusInformationLength == sizeof(DWORD),
214                 "info length should be sizeof(DWORD) instead of %d\n",
215                 dwStatusInformationLength);
216             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
217                 GetCurrentThreadId(), hInternet, dwContext,
218                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
219             break;
220         case INTERNET_STATUS_RECEIVING_RESPONSE:
221             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
222                 GetCurrentThreadId(), hInternet, dwContext,
223                 lpvStatusInformation,dwStatusInformationLength);
224             break;
225         case INTERNET_STATUS_RESPONSE_RECEIVED:
226             ok(dwStatusInformationLength == sizeof(DWORD),
227                 "info length should be sizeof(DWORD) instead of %d\n",
228                 dwStatusInformationLength);
229             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
230                 GetCurrentThreadId(), hInternet, dwContext,
231                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
232             break;
233         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
234             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
235                 GetCurrentThreadId(), hInternet,dwContext,
236                 lpvStatusInformation,dwStatusInformationLength);
237             break;
238         case INTERNET_STATUS_PREFETCH:
239             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
240                 GetCurrentThreadId(), hInternet, dwContext,
241                 lpvStatusInformation,dwStatusInformationLength);
242             break;
243         case INTERNET_STATUS_CLOSING_CONNECTION:
244             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
245                 GetCurrentThreadId(), hInternet, dwContext,
246                 lpvStatusInformation,dwStatusInformationLength);
247             break;
248         case INTERNET_STATUS_CONNECTION_CLOSED:
249             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
250                 GetCurrentThreadId(), hInternet, dwContext,
251                 lpvStatusInformation,dwStatusInformationLength);
252             break;
253         case INTERNET_STATUS_HANDLE_CREATED:
254             ok(dwStatusInformationLength == sizeof(HINTERNET),
255                 "info length should be sizeof(HINTERNET) instead of %d\n",
256                 dwStatusInformationLength);
257             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
258                 GetCurrentThreadId(), hInternet, dwContext,
259                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
260             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
261             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
262             break;
263         case INTERNET_STATUS_HANDLE_CLOSING:
264             ok(dwStatusInformationLength == sizeof(HINTERNET),
265                 "info length should be sizeof(HINTERNET) instead of %d\n",
266                 dwStatusInformationLength);
267             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
268                 GetCurrentThreadId(), hInternet, dwContext,
269                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
270             break;
271         case INTERNET_STATUS_REQUEST_COMPLETE:
272         {
273             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
274             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
275                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
276                 dwStatusInformationLength);
277             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
278                 GetCurrentThreadId(), hInternet, dwContext,
279                 iar->dwResult,iar->dwError,dwStatusInformationLength);
280             SetEvent(hCompleteEvent);
281             break;
282         }
283         case INTERNET_STATUS_REDIRECT:
284             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
285                 GetCurrentThreadId(), hInternet, dwContext,
286                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
287             *(LPSTR)lpvStatusInformation = '\0';
288             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
289             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
290             break;
291         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
292             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
293                 GetCurrentThreadId(), hInternet, dwContext,
294                 lpvStatusInformation, dwStatusInformationLength);
295             break;
296         default:
297             trace("%04x:Callback %p 0x%lx %d %p %d\n",
298                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
299                 lpvStatusInformation, dwStatusInformationLength);
300     }
301 }
302
303 static void InternetReadFile_test(int flags, const test_data_t *test)
304 {
305     char *post_data = NULL;
306     BOOL res;
307     CHAR buffer[4000];
308     DWORD length, post_len = 0;
309     DWORD out;
310     const char *types[2] = { "*", NULL };
311     HINTERNET hi, hic = 0, hor = 0;
312
313     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
314
315     trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
316
317     trace("InternetOpenA <--\n");
318     hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
319             INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
320     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
321     trace("InternetOpenA -->\n");
322
323     if (hi == 0x0) goto abort;
324
325     pInternetSetStatusCallbackA(hi,&callback);
326
327     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
328
329     trace("InternetConnectA <--\n");
330     hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
331                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
332     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
333     trace("InternetConnectA -->\n");
334
335     if (hic == 0x0) goto abort;
336
337     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
338     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
339
340     trace("HttpOpenRequestA <--\n");
341     hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
342                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
343                            0xdeadbead);
344     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
345         /*
346          * If the internet name can't be resolved we are probably behind
347          * a firewall or in some other way not directly connected to the
348          * Internet. Not enough reason to fail the test. Just ignore and
349          * abort.
350          */
351     } else  {
352         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
353     }
354     trace("HttpOpenRequestA -->\n");
355
356     if (hor == 0x0) goto abort;
357
358     length = sizeof(buffer);
359     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
360     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
361     ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
362
363     length = sizeof(buffer);
364     res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
365     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
366     ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
367     ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
368
369     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
370     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
371     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
372     if(test->flags & TESTF_ALLOW_COOKIE) {
373         SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
374         SET_OPTIONAL(INTERNET_STATUS_COOKIE_RECEIVED);
375     }
376     if (first_connection_to_test_url)
377     {
378         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
379         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
380         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
381         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
382     }
383     else
384     {
385         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
386         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
387     }
388     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
389     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
390     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
391     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
392     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
393     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
394     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
395     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
396     if(test->flags & TESTF_REDIRECT) {
397         SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
398         SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
399     }
400     SET_EXPECT(INTERNET_STATUS_REDIRECT);
401     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
402     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
403     if (flags & INTERNET_FLAG_ASYNC)
404         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
405     else
406         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
407
408     if(test->flags & TESTF_COMPRESSED) {
409         BOOL b = TRUE;
410
411         res = InternetSetOption(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
412         ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
413            "InternetSetOption failed: %u\n", GetLastError());
414         if(!res)
415             goto abort;
416     }
417
418     trace("HttpSendRequestA -->\n");
419     if(test->post_data) {
420         post_len = strlen(test->post_data);
421         post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
422         memcpy(post_data, test->post_data, post_len);
423     }
424     SetLastError(0xdeadbeef);
425     res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
426     if (flags & INTERNET_FLAG_ASYNC)
427         ok(!res && (GetLastError() == ERROR_IO_PENDING),
428             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
429     else
430         ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
431            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
432     trace("HttpSendRequestA <--\n");
433
434     if (flags & INTERNET_FLAG_ASYNC)
435         WaitForSingleObject(hCompleteEvent, INFINITE);
436     HeapFree(GetProcessHeap(), 0, post_data);
437
438     if(test->flags & TESTF_ALLOW_COOKIE) {
439         CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
440         CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
441     }
442     if (first_connection_to_test_url)
443     {
444         if (! proxy_active())
445         {
446             CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
447             CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
448         }
449         else
450         {
451             CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
452             CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
453         }
454     }
455     else todo_wine
456     {
457         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
458         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
459     }
460     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
461     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
462     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
463     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
464     if(test->flags & TESTF_REDIRECT)
465         CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
466     if (flags & INTERNET_FLAG_ASYNC)
467         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
468     else
469         CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
470     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
471     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
472     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
473
474     length = 4;
475     res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
476     ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
477
478     length = 100;
479     res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
480     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
481
482     length = sizeof(buffer);
483     res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
484     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
485     buffer[length]=0;
486
487     length = sizeof(buffer);
488     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
489     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
490     ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
491
492     length = 16;
493     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
494     trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i  %s  (%u)\n",res,buffer,GetLastError());
495     if(test->flags & TESTF_COMPRESSED)
496         ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
497            "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
498
499     length = 100;
500     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
501     buffer[length]=0;
502     trace("Option HTTP_QUERY_CONTENT_TYPE -> %i  %s\n",res,buffer);
503
504     length = 100;
505     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
506     buffer[length]=0;
507     trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i  %s\n",res,buffer);
508
509     SetLastError(0xdeadbeef);
510     res = InternetReadFile(NULL, buffer, 100, &length);
511     ok(!res, "InternetReadFile should have failed\n");
512     ok(GetLastError() == ERROR_INVALID_HANDLE,
513         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
514         GetLastError());
515
516     length = 100;
517     trace("Entering Query loop\n");
518
519     while (TRUE)
520     {
521         if (flags & INTERNET_FLAG_ASYNC)
522             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
523         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
524         ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
525         ok(res || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
526            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
527         if (flags & INTERNET_FLAG_ASYNC)
528         {
529             if (res)
530             {
531                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
532             }
533             else if (GetLastError() == ERROR_IO_PENDING)
534             {
535                 WaitForSingleObject(hCompleteEvent, INFINITE);
536                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
537                 continue;
538             }
539         }
540         if (length)
541         {
542             char *buffer;
543             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
544
545             res = InternetReadFile(hor,buffer,length,&length);
546
547             buffer[length]=0;
548
549             trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
550
551             if(test->content)
552                 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
553             HeapFree(GetProcessHeap(),0,buffer);
554         }
555         if (length == 0)
556             break;
557     }
558     if(test->flags & TESTF_REDIRECT) {
559         CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
560         CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
561     }
562 abort:
563     trace("aborting\n");
564     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
565     if (hor != 0x0) {
566         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
567         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
568         SetLastError(0xdeadbeef);
569         trace("closing\n");
570         res = InternetCloseHandle(hor);
571         ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
572         SetLastError(0xdeadbeef);
573         res = InternetCloseHandle(hor);
574         ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
575         ok (GetLastError() == ERROR_INVALID_HANDLE,
576             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
577             GetLastError());
578     }
579     /* We intentionally do not close the handle opened by InternetConnectA as this
580      * tickles bug #9479: native closes child internet handles when the parent handles
581      * are closed. This is verified below by checking that the number of
582      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
583     if (hi != 0x0) {
584       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
585         trace("closing 2\n");
586       res = InternetCloseHandle(hi);
587       ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
588       if (flags & INTERNET_FLAG_ASYNC)
589           Sleep(100);
590     }
591     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
592     if (hor != 0x0) todo_wine
593     {
594         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
595         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
596     }
597     else
598     {
599         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
600         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
601     }
602     CloseHandle(hCompleteEvent);
603     first_connection_to_test_url = FALSE;
604 }
605
606 static void InternetReadFile_chunked_test(void)
607 {
608     BOOL res;
609     CHAR buffer[4000];
610     DWORD length;
611     const char *types[2] = { "*", NULL };
612     HINTERNET hi, hic = 0, hor = 0;
613
614     trace("Starting InternetReadFile chunked test\n");
615
616     trace("InternetOpenA <--\n");
617     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
618     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
619     trace("InternetOpenA -->\n");
620
621     if (hi == 0x0) goto abort;
622
623     trace("InternetConnectA <--\n");
624     hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
625                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
626     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
627     trace("InternetConnectA -->\n");
628
629     if (hic == 0x0) goto abort;
630
631     trace("HttpOpenRequestA <--\n");
632     hor = HttpOpenRequestA(hic, "GET", "/testchunked", NULL, NULL, types,
633                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
634                            0xdeadbead);
635     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
636         /*
637          * If the internet name can't be resolved we are probably behind
638          * a firewall or in some other way not directly connected to the
639          * Internet. Not enough reason to fail the test. Just ignore and
640          * abort.
641          */
642     } else  {
643         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
644     }
645     trace("HttpOpenRequestA -->\n");
646
647     if (hor == 0x0) goto abort;
648
649     trace("HttpSendRequestA -->\n");
650     SetLastError(0xdeadbeef);
651     res = HttpSendRequestA(hor, "", -1, NULL, 0);
652     ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
653        "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
654     trace("HttpSendRequestA <--\n");
655
656     length = 100;
657     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
658     buffer[length]=0;
659     trace("Option CONTENT_TYPE -> %i  %s\n",res,buffer);
660
661     SetLastError( 0xdeadbeef );
662     length = 100;
663     res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
664     buffer[length]=0;
665     trace("Option TRANSFER_ENCODING -> %i  %s\n",res,buffer);
666     ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
667         "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
668     ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
669         "Wrong transfer encoding '%s'\n", buffer );
670
671     SetLastError( 0xdeadbeef );
672     length = 16;
673     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
674     ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
675     ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
676
677     length = 100;
678     trace("Entering Query loop\n");
679
680     while (TRUE)
681     {
682         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
683         ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
684         ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
685         trace("got %u available\n",length);
686         if (length)
687         {
688             DWORD got;
689             char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
690
691             res = InternetReadFile(hor,buffer,length,&got);
692
693             buffer[got]=0;
694             trace("ReadFile -> %i %i\n",res,got);
695             ok( length == got, "only got %u of %u available\n", got, length );
696             ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
697
698             HeapFree(GetProcessHeap(),0,buffer);
699             if (!got) break;
700         }
701         if (length == 0)
702             break;
703     }
704 abort:
705     trace("aborting\n");
706     if (hor != 0x0) {
707         res = InternetCloseHandle(hor);
708         ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
709     }
710     if (hi != 0x0) {
711         res = InternetCloseHandle(hi);
712         ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
713     }
714 }
715
716 static void InternetReadFileExA_test(int flags)
717 {
718     DWORD rc;
719     DWORD length;
720     const char *types[2] = { "*", NULL };
721     HINTERNET hi, hic = 0, hor = 0;
722     INTERNET_BUFFERS inetbuffers;
723
724     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
725
726     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
727
728     trace("InternetOpenA <--\n");
729     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
730     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
731     trace("InternetOpenA -->\n");
732
733     if (hi == 0x0) goto abort;
734
735     pInternetSetStatusCallbackA(hi,&callback);
736
737     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
738
739     trace("InternetConnectA <--\n");
740     hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
741                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
742     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
743     trace("InternetConnectA -->\n");
744
745     if (hic == 0x0) goto abort;
746
747     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
748     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
749
750     trace("HttpOpenRequestA <--\n");
751     hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
752                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
753                            0xdeadbead);
754     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
755         /*
756          * If the internet name can't be resolved we are probably behind
757          * a firewall or in some other way not directly connected to the
758          * Internet. Not enough reason to fail the test. Just ignore and
759          * abort.
760          */
761     } else  {
762         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
763     }
764     trace("HttpOpenRequestA -->\n");
765
766     if (hor == 0x0) goto abort;
767
768     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
769     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
770     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
771     if (first_connection_to_test_url)
772     {
773         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
774         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
775         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
776         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
777     }
778     else
779     {
780         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
781         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
782     }
783     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
784     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
785     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
786     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
787     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
788     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
789     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
790     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
791     SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
792     SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
793     SET_EXPECT(INTERNET_STATUS_REDIRECT);
794     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
795     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
796     if (flags & INTERNET_FLAG_ASYNC)
797         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
798     else
799         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
800
801     trace("HttpSendRequestA -->\n");
802     SetLastError(0xdeadbeef);
803     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
804     if (flags & INTERNET_FLAG_ASYNC)
805         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
806             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
807     else
808         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
809            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
810     trace("HttpSendRequestA <--\n");
811
812     if (!rc && (GetLastError() == ERROR_IO_PENDING))
813         WaitForSingleObject(hCompleteEvent, INFINITE);
814
815     if (first_connection_to_test_url)
816     {
817         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
818         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
819     }
820     else todo_wine
821     {
822         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
823         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
824     }
825     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
826     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
827     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
828     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
829     CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
830     CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
831     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
832     if (flags & INTERNET_FLAG_ASYNC)
833         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
834     else
835         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
836     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
837     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
838     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
839
840     /* tests invalid dwStructSize */
841     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
842     inetbuffers.lpcszHeader = NULL;
843     inetbuffers.dwHeadersLength = 0;
844     inetbuffers.dwBufferLength = 10;
845     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
846     inetbuffers.dwOffsetHigh = 1234;
847     inetbuffers.dwOffsetLow = 5678;
848     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
849     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
850         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
851         rc ? "TRUE" : "FALSE", GetLastError());
852     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
853
854     /* tests to see whether lpcszHeader is used - it isn't */
855     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
856     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
857     inetbuffers.dwHeadersLength = 255;
858     inetbuffers.dwBufferLength = 0;
859     inetbuffers.lpvBuffer = NULL;
860     inetbuffers.dwOffsetHigh = 1234;
861     inetbuffers.dwOffsetLow = 5678;
862     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
863     SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
864     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
865     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
866     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
867     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
868         trace("read %i bytes\n", inetbuffers.dwBufferLength);
869     todo_wine
870     {
871         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
872         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
873     }
874
875     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
876     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
877         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
878         rc ? "TRUE" : "FALSE", GetLastError());
879
880     length = 0;
881     trace("Entering Query loop\n");
882
883     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
884     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
885     while (TRUE)
886     {
887         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
888         inetbuffers.dwBufferLength = 1024;
889         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
890         inetbuffers.dwOffsetHigh = 1234;
891         inetbuffers.dwOffsetLow = 5678;
892
893         SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
894         SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
895         SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
896         SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
897         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
898         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
899         if (!rc)
900         {
901             if (GetLastError() == ERROR_IO_PENDING)
902             {
903                 trace("InternetReadFileEx -> PENDING\n");
904                 ok(flags & INTERNET_FLAG_ASYNC,
905                    "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
906                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
907                 WaitForSingleObject(hCompleteEvent, INFINITE);
908                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
909                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
910             }
911             else
912             {
913                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
914                 break;
915             }
916         }
917         else
918         {
919             trace("InternetReadFileEx -> SUCCEEDED\n");
920             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
921             if (inetbuffers.dwBufferLength)
922             {
923                 todo_wine {
924                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
925                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
926                 }
927             }
928             else
929             {
930                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
931                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
932                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
933             }
934         }
935
936         trace("read %i bytes\n", inetbuffers.dwBufferLength);
937         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
938
939         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
940             "InternetReadFileEx sets offsets to 0x%x%08x\n",
941             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
942
943         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
944
945         if (!inetbuffers.dwBufferLength)
946             break;
947
948         length += inetbuffers.dwBufferLength;
949     }
950     ok(length > 0, "failed to read any of the document\n");
951     trace("Finished. Read %d bytes\n", length);
952
953     /* WinXP does not send, but Win98 does */
954     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
955     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
956 abort:
957     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
958     if (hor) {
959         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
960         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
961         rc = InternetCloseHandle(hor);
962         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
963         rc = InternetCloseHandle(hor);
964         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
965     }
966     if (hic) {
967         rc = InternetCloseHandle(hic);
968         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
969     }
970     if (hi) {
971       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
972       rc = InternetCloseHandle(hi);
973       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
974       if (flags & INTERNET_FLAG_ASYNC)
975           Sleep(100);
976       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
977     }
978     /* to enable once Wine is fixed to never send it
979     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
980     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
981     */
982     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
983     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
984     CloseHandle(hCompleteEvent);
985     first_connection_to_test_url = FALSE;
986 }
987
988 static void InternetOpenUrlA_test(void)
989 {
990   HINTERNET myhinternet, myhttp;
991   char buffer[0x400];
992   DWORD size, readbytes, totalbytes=0;
993   BOOL ret;
994
995   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
996   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
997   size = 0x400;
998   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
999   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1000
1001   SetLastError(0);
1002   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
1003                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1004   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1005     return; /* WinXP returns this when not connected to the net */
1006   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1007   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1008   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1009   totalbytes += readbytes;
1010   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1011     totalbytes += readbytes;
1012   trace("read 0x%08x bytes\n",totalbytes);
1013
1014   InternetCloseHandle(myhttp);
1015   InternetCloseHandle(myhinternet);
1016 }
1017
1018 static void HttpSendRequestEx_test(void)
1019 {
1020     HINTERNET hSession;
1021     HINTERNET hConnect;
1022     HINTERNET hRequest;
1023
1024     INTERNET_BUFFERS BufferIn;
1025     DWORD dwBytesWritten, dwBytesRead, error;
1026     CHAR szBuffer[256];
1027     int i;
1028     BOOL ret;
1029
1030     static char szPostData[] = "mode=Test";
1031     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1032
1033     hSession = InternetOpen("Wine Regression Test",
1034             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1035     ok( hSession != NULL ,"Unable to open Internet session\n");
1036     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1037             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1038             0);
1039     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1040     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1041             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1042     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1043     {
1044         skip( "Network unreachable, skipping test\n" );
1045         goto done;
1046     }
1047     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1048
1049
1050     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
1051     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
1052     BufferIn.lpcszHeader = szContentType;
1053     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1054     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1055     BufferIn.lpvBuffer = szPostData;
1056     BufferIn.dwBufferLength = 3;
1057     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1058     BufferIn.dwOffsetLow = 0;
1059     BufferIn.dwOffsetHigh = 0;
1060
1061     SetLastError(0xdeadbeef);
1062     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
1063     error = GetLastError();
1064     ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1065     ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1066
1067     for (i = 3; szPostData[i]; i++)
1068         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1069                 "InternetWriteFile failed\n");
1070
1071     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1072
1073     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1074             "Unable to read response\n");
1075     szBuffer[dwBytesRead] = 0;
1076
1077     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1078     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1079
1080     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1081 done:
1082     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1083     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1084 }
1085
1086 static void InternetOpenRequest_test(void)
1087 {
1088     HINTERNET session, connect, request;
1089     static const char *types[] = { "*", "", NULL };
1090     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1091     static const WCHAR *typesW[] = { any, empty, NULL };
1092     BOOL ret;
1093
1094     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1095     ok(session != NULL ,"Unable to open Internet session\n");
1096
1097     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1098                               INTERNET_SERVICE_HTTP, 0, 0);
1099     ok(connect == NULL, "InternetConnectA should have failed\n");
1100     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1101
1102     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1103                               INTERNET_SERVICE_HTTP, 0, 0);
1104     ok(connect == NULL, "InternetConnectA should have failed\n");
1105     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1106
1107     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1108                               INTERNET_SERVICE_HTTP, 0, 0);
1109     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1110
1111     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1112     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1113     {
1114         skip( "Network unreachable, skipping test\n" );
1115         goto done;
1116     }
1117     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1118
1119     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1120     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1121     ok(InternetCloseHandle(request), "Close request handle failed\n");
1122
1123     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1124     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1125
1126     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1127     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1128     ok(InternetCloseHandle(request), "Close request handle failed\n");
1129
1130 done:
1131     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1132     ok(InternetCloseHandle(session), "Close session handle failed\n");
1133 }
1134
1135 static void test_http_cache(void)
1136 {
1137     HINTERNET session, connect, request;
1138     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1139     DWORD size, file_size;
1140     BYTE buf[100];
1141     HANDLE file;
1142     BOOL ret;
1143
1144     static const char *types[] = { "*", "", NULL };
1145
1146     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1147     ok(session != NULL ,"Unable to open Internet session\n");
1148
1149     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1150                               INTERNET_SERVICE_HTTP, 0, 0);
1151     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1152
1153     request = HttpOpenRequestA(connect, NULL, "/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1154     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1155     {
1156         skip( "Network unreachable, skipping test\n" );
1157
1158         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1159         ok(InternetCloseHandle(session), "Close session handle failed\n");
1160
1161         return;
1162     }
1163     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1164
1165     size = sizeof(url);
1166     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1167     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1168     ok(!strcmp(url, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", url);
1169
1170     size = sizeof(file_name);
1171     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1172     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1173     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1174     ok(!size, "size = %d\n", size);
1175
1176     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1177     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1178
1179     size = sizeof(file_name);
1180     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1181     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1182
1183     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1184                       FILE_ATTRIBUTE_NORMAL, NULL);
1185     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1186     file_size = GetFileSize(file, NULL);
1187     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1188
1189     size = sizeof(buf);
1190     ret = InternetReadFile(request, buf, sizeof(buf), &size);
1191     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1192     ok(size == 100, "size = %u\n", size);
1193
1194     file_size = GetFileSize(file, NULL);
1195     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1196     CloseHandle(file);
1197
1198     ok(InternetCloseHandle(request), "Close request handle failed\n");
1199
1200     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1201                       FILE_ATTRIBUTE_NORMAL, NULL);
1202     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1203     CloseHandle(file);
1204
1205     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1206     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1207
1208     size = sizeof(file_name);
1209     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1210     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1211     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1212     ok(!size, "size = %d\n", size);
1213
1214     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1215     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1216
1217     size = sizeof(file_name);
1218     file_name[0] = 0;
1219     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1220     if (ret)
1221     {
1222         file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1223                       FILE_ATTRIBUTE_NORMAL, NULL);
1224         ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1225         CloseHandle(file);
1226     }
1227     else
1228     {
1229         /* < IE8 */
1230         ok(file_name[0] == 0, "Didn't expect a file name\n");
1231     }
1232
1233     ok(InternetCloseHandle(request), "Close request handle failed\n");
1234     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1235     ok(InternetCloseHandle(session), "Close session handle failed\n");
1236 }
1237
1238 static void HttpHeaders_test(void)
1239 {
1240     HINTERNET hSession;
1241     HINTERNET hConnect;
1242     HINTERNET hRequest;
1243     CHAR      buffer[256];
1244     WCHAR     wbuffer[256];
1245     DWORD     len = 256;
1246     DWORD     oldlen;
1247     DWORD     index = 0;
1248
1249     hSession = InternetOpen("Wine Regression Test",
1250             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1251     ok( hSession != NULL ,"Unable to open Internet session\n");
1252     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1253             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1254             0);
1255     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1256     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1257             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1258     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1259     {
1260         skip( "Network unreachable, skipping test\n" );
1261         goto done;
1262     }
1263     ok( hRequest != NULL, "Failed to open request handle\n");
1264
1265     index = 0;
1266     len = sizeof(buffer);
1267     strcpy(buffer,"Warning");
1268     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1269                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1270
1271     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1272             "Failed to add new header\n");
1273
1274     index = 0;
1275     len = sizeof(buffer);
1276     strcpy(buffer,"Warning");
1277     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1278                 buffer,&len,&index),"Unable to query header\n");
1279     ok(index == 1, "Index was not incremented\n");
1280     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1281     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1282     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1283     len = sizeof(buffer);
1284     strcpy(buffer,"Warning");
1285     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1286                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1287
1288     index = 0;
1289     len = 5; /* could store the string but not the NULL terminator */
1290     strcpy(buffer,"Warning");
1291     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1292                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1293     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1294     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1295
1296     /* a call with NULL will fail but will return the length */
1297     index = 0;
1298     len = sizeof(buffer);
1299     SetLastError(0xdeadbeef);
1300     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1301                 NULL,&len,&index) == FALSE,"Query worked\n");
1302     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1303     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1304     ok(index == 0, "Index was incremented\n");
1305
1306     /* even for a len that is too small */
1307     index = 0;
1308     len = 15;
1309     SetLastError(0xdeadbeef);
1310     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1311                 NULL,&len,&index) == FALSE,"Query worked\n");
1312     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1313     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1314     ok(index == 0, "Index was incremented\n");
1315
1316     index = 0;
1317     len = 0;
1318     SetLastError(0xdeadbeef);
1319     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1320                 NULL,&len,&index) == FALSE,"Query worked\n");
1321     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1322     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1323     ok(index == 0, "Index was incremented\n");
1324     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1325
1326
1327     /* a working query */
1328     index = 0;
1329     len = sizeof(buffer);
1330     memset(buffer, 'x', sizeof(buffer));
1331     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1332                 buffer,&len,&index),"Unable to query header\n");
1333     ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1334     ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1335     ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1336     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1337     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1338     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1339     ok(index == 0, "Index was incremented\n");
1340
1341     /* Like above two tests, but for W version */
1342
1343     index = 0;
1344     len = 0;
1345     SetLastError(0xdeadbeef);
1346     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1347                 NULL,&len,&index) == FALSE,"Query worked\n");
1348     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1349     ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1350     ok(index == 0, "Index was incremented\n");
1351     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1352
1353     /* a working query */
1354     index = 0;
1355     len = sizeof(wbuffer);
1356     memset(wbuffer, 'x', sizeof(wbuffer));
1357     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1358                 wbuffer,&len,&index),"Unable to query header\n");
1359     ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1360     ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1361     ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1362     ok(index == 0, "Index was incremented\n");
1363
1364     /* end of W version tests */
1365
1366     /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1367     index = 0;
1368     len = sizeof(buffer);
1369     memset(buffer, 'x', sizeof(buffer));
1370     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1371                 buffer,&len,&index) == TRUE,"Query failed\n");
1372     ok(len == 2, "Expected 2, got %d\n", len);
1373     ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1374     ok(index == 0, "Index was incremented\n");
1375
1376     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1377             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1378
1379     index = 0;
1380     len = sizeof(buffer);
1381     strcpy(buffer,"Warning");
1382     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1383                 buffer,&len,&index),"Unable to query header\n");
1384     ok(index == 1, "Index was not incremented\n");
1385     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1386     len = sizeof(buffer);
1387     strcpy(buffer,"Warning");
1388     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1389                 buffer,&len,&index),"Failed to get second header\n");
1390     ok(index == 2, "Index was not incremented\n");
1391     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1392     len = sizeof(buffer);
1393     strcpy(buffer,"Warning");
1394     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1395                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1396
1397     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1398
1399     index = 0;
1400     len = sizeof(buffer);
1401     strcpy(buffer,"Warning");
1402     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1403                 buffer,&len,&index),"Unable to query header\n");
1404     ok(index == 1, "Index was not incremented\n");
1405     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1406     len = sizeof(buffer);
1407     strcpy(buffer,"Warning");
1408     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1409                 buffer,&len,&index),"Failed to get second header\n");
1410     ok(index == 2, "Index was not incremented\n");
1411     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1412     len = sizeof(buffer);
1413     strcpy(buffer,"Warning");
1414     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1415                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1416
1417     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1418
1419     index = 0;
1420     len = sizeof(buffer);
1421     strcpy(buffer,"Warning");
1422     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1423                 buffer,&len,&index),"Unable to query header\n");
1424     ok(index == 1, "Index was not incremented\n");
1425     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1426     len = sizeof(buffer);
1427     strcpy(buffer,"Warning");
1428     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1429                 buffer,&len,&index),"Failed to get second header\n");
1430     ok(index == 2, "Index was not incremented\n");
1431     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1432     len = sizeof(buffer);
1433     strcpy(buffer,"Warning");
1434     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1435                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1436
1437     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1438
1439     index = 0;
1440     len = sizeof(buffer);
1441     strcpy(buffer,"Warning");
1442     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1443                 buffer,&len,&index),"Unable to query header\n");
1444     ok(index == 1, "Index was not incremented\n");
1445     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1446     len = sizeof(buffer);
1447     strcpy(buffer,"Warning");
1448     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1449     ok(index == 2, "Index was not incremented\n");
1450     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1451     len = sizeof(buffer);
1452     strcpy(buffer,"Warning");
1453     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1454
1455     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1456
1457     index = 0;
1458     len = sizeof(buffer);
1459     strcpy(buffer,"Warning");
1460     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1461     ok(index == 1, "Index was not incremented\n");
1462     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1463     len = sizeof(buffer);
1464     strcpy(buffer,"Warning");
1465     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1466     ok(index == 2, "Index was not incremented\n");
1467     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1468     len = sizeof(buffer);
1469     strcpy(buffer,"Warning");
1470     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1471
1472     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1473
1474     index = 0;
1475     len = sizeof(buffer);
1476     strcpy(buffer,"Warning");
1477     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1478     ok(index == 1, "Index was not incremented\n");
1479     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1480     len = sizeof(buffer);
1481     strcpy(buffer,"Warning");
1482     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1483     ok(index == 2, "Index was not incremented\n");
1484     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1485     len = sizeof(buffer);
1486     strcpy(buffer,"Warning");
1487     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1488
1489     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");
1490
1491     index = 0;
1492     len = sizeof(buffer);
1493     strcpy(buffer,"Warning");
1494     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1495     ok(index == 1, "Index was not incremented\n");
1496     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1497     len = sizeof(buffer);
1498     strcpy(buffer,"Warning");
1499     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1500     ok(index == 2, "Index was not incremented\n");
1501     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1502     len = sizeof(buffer);
1503     strcpy(buffer,"Warning");
1504     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1505
1506     /* Ensure that blank headers are ignored and don't cause a failure */
1507     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");
1508
1509     index = 0;
1510     len = sizeof(buffer);
1511     strcpy(buffer,"BlankTest");
1512     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1513     ok(index == 1, "Index was not incremented\n");
1514     ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1515
1516     /* Ensure that malformed header separators are ignored and don't cause a failure */
1517     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),
1518         "Failed to add header with malformed entries in list\n");
1519
1520     index = 0;
1521     len = sizeof(buffer);
1522     strcpy(buffer,"MalformedTest");
1523     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1524     ok(index == 1, "Index was not incremented\n");
1525     ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1526     index = 0;
1527     len = sizeof(buffer);
1528     strcpy(buffer,"MalformedTestTwo");
1529     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1530     ok(index == 1, "Index was not incremented\n");
1531     ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1532     index = 0;
1533     len = sizeof(buffer);
1534     strcpy(buffer,"MalformedTestThree");
1535     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1536     ok(index == 1, "Index was not incremented\n");
1537     ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1538
1539     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1540 done:
1541     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1542     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1543 }
1544
1545 static const char garbagemsg[] =
1546 "Garbage: Header\r\n";
1547
1548 static const char contmsg[] =
1549 "HTTP/1.1 100 Continue\r\n";
1550
1551 static const char expandcontmsg[] =
1552 "HTTP/1.1 100 Continue\r\n"
1553 "Server: winecontinue\r\n"
1554 "Tag: something witty\r\n"
1555 "\r\n";
1556
1557 static const char okmsg[] =
1558 "HTTP/1.1 200 OK\r\n"
1559 "Server: winetest\r\n"
1560 "\r\n";
1561
1562 static const char okmsg2[] =
1563 "HTTP/1.1 200 OK\r\n"
1564 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1565 "Server: winetest\r\n"
1566 "Content-Length: 0\r\n"
1567 "Set-Cookie: one\r\n"
1568 "Set-Cookie: two\r\n"
1569 "\r\n";
1570
1571 static const char notokmsg[] =
1572 "HTTP/1.1 400 Bad Request\r\n"
1573 "Server: winetest\r\n"
1574 "\r\n";
1575
1576 static const char noauthmsg[] =
1577 "HTTP/1.1 401 Unauthorized\r\n"
1578 "Server: winetest\r\n"
1579 "Connection: close\r\n"
1580 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1581 "\r\n";
1582
1583 static const char noauthmsg2[] =
1584 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1585 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1586 "\0d`0|6\n"
1587 "Server: winetest\r\n";
1588
1589 static const char proxymsg[] =
1590 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1591 "Server: winetest\r\n"
1592 "Proxy-Connection: close\r\n"
1593 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1594 "\r\n";
1595
1596 static const char page1[] =
1597 "<HTML>\r\n"
1598 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1599 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1600 "</HTML>\r\n\r\n";
1601
1602 struct server_info {
1603     HANDLE hEvent;
1604     int port;
1605 };
1606
1607 static DWORD CALLBACK server_thread(LPVOID param)
1608 {
1609     struct server_info *si = param;
1610     int r, c, i, on;
1611     SOCKET s;
1612     struct sockaddr_in sa;
1613     char buffer[0x100];
1614     WSADATA wsaData;
1615     int last_request = 0;
1616     char host_header[22];
1617     static int test_b = 0;
1618
1619     WSAStartup(MAKEWORD(1,1), &wsaData);
1620
1621     s = socket(AF_INET, SOCK_STREAM, 0);
1622     if (s == INVALID_SOCKET)
1623         return 1;
1624
1625     on = 1;
1626     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1627
1628     memset(&sa, 0, sizeof sa);
1629     sa.sin_family = AF_INET;
1630     sa.sin_port = htons(si->port);
1631     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1632
1633     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1634     if (r<0)
1635         return 1;
1636
1637     listen(s, 0);
1638
1639     SetEvent(si->hEvent);
1640
1641     sprintf(host_header, "Host: localhost:%d", si->port);
1642
1643     do
1644     {
1645         c = accept(s, NULL, NULL);
1646
1647         memset(buffer, 0, sizeof buffer);
1648         for(i=0; i<(sizeof buffer-1); i++)
1649         {
1650             r = recv(c, &buffer[i], 1, 0);
1651             if (r != 1)
1652                 break;
1653             if (i<4) continue;
1654             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1655                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1656                 break;
1657         }
1658         if (strstr(buffer, "GET /test1"))
1659         {
1660             if (!strstr(buffer, "Content-Length: 0"))
1661             {
1662                 send(c, okmsg, sizeof okmsg-1, 0);
1663                 send(c, page1, sizeof page1-1, 0);
1664             }
1665             else
1666                 send(c, notokmsg, sizeof notokmsg-1, 0);
1667         }
1668         if (strstr(buffer, "/test2"))
1669         {
1670             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1671             {
1672                 send(c, okmsg, sizeof okmsg-1, 0);
1673                 send(c, page1, sizeof page1-1, 0);
1674             }
1675             else
1676                 send(c, proxymsg, sizeof proxymsg-1, 0);
1677         }
1678         if (strstr(buffer, "/test3"))
1679         {
1680             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1681                 send(c, okmsg, sizeof okmsg-1, 0);
1682             else
1683                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1684         }
1685         if (strstr(buffer, "/test4"))
1686         {
1687             if (strstr(buffer, "Connection: Close"))
1688                 send(c, okmsg, sizeof okmsg-1, 0);
1689             else
1690                 send(c, notokmsg, sizeof notokmsg-1, 0);
1691         }
1692         if (strstr(buffer, "POST /test5") ||
1693             strstr(buffer, "RPC_IN_DATA /test5") ||
1694             strstr(buffer, "RPC_OUT_DATA /test5"))
1695         {
1696             if (strstr(buffer, "Content-Length: 0"))
1697             {
1698                 send(c, okmsg, sizeof okmsg-1, 0);
1699                 send(c, page1, sizeof page1-1, 0);
1700             }
1701             else
1702                 send(c, notokmsg, sizeof notokmsg-1, 0);
1703         }
1704         if (strstr(buffer, "GET /test6"))
1705         {
1706             send(c, contmsg, sizeof contmsg-1, 0);
1707             send(c, contmsg, sizeof contmsg-1, 0);
1708             send(c, okmsg, sizeof okmsg-1, 0);
1709             send(c, page1, sizeof page1-1, 0);
1710         }
1711         if (strstr(buffer, "POST /test7"))
1712         {
1713             if (strstr(buffer, "Content-Length: 100"))
1714             {
1715                 send(c, okmsg, sizeof okmsg-1, 0);
1716                 send(c, page1, sizeof page1-1, 0);
1717             }
1718             else
1719                 send(c, notokmsg, sizeof notokmsg-1, 0);
1720         }
1721         if (strstr(buffer, "/test8"))
1722         {
1723             if (!strstr(buffer, "Connection: Close") &&
1724                  strstr(buffer, "Connection: Keep-Alive") &&
1725                 !strstr(buffer, "Cache-Control: no-cache") &&
1726                 !strstr(buffer, "Pragma: no-cache") &&
1727                  strstr(buffer, host_header))
1728                 send(c, okmsg, sizeof okmsg-1, 0);
1729             else
1730                 send(c, notokmsg, sizeof notokmsg-1, 0);
1731         }
1732         if (strstr(buffer, "/test9"))
1733         {
1734             if (!strstr(buffer, "Connection: Close") &&
1735                 !strstr(buffer, "Connection: Keep-Alive") &&
1736                 !strstr(buffer, "Cache-Control: no-cache") &&
1737                 !strstr(buffer, "Pragma: no-cache") &&
1738                  strstr(buffer, host_header))
1739                 send(c, okmsg, sizeof okmsg-1, 0);
1740             else
1741                 send(c, notokmsg, sizeof notokmsg-1, 0);
1742         }
1743         if (strstr(buffer, "/testA"))
1744         {
1745             if (!strstr(buffer, "Connection: Close") &&
1746                 !strstr(buffer, "Connection: Keep-Alive") &&
1747                 (strstr(buffer, "Cache-Control: no-cache") ||
1748                  strstr(buffer, "Pragma: no-cache")) &&
1749                  strstr(buffer, host_header))
1750                 send(c, okmsg, sizeof okmsg-1, 0);
1751             else
1752                 send(c, notokmsg, sizeof notokmsg-1, 0);
1753         }
1754         if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1755         {
1756             test_b = 1;
1757             send(c, okmsg, sizeof okmsg-1, 0);
1758             recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1759             send(c, okmsg, sizeof okmsg-1, 0);
1760         }
1761         if (strstr(buffer, "/testC"))
1762         {
1763             if (strstr(buffer, "Cookie: cookie=biscuit"))
1764                 send(c, okmsg, sizeof okmsg-1, 0);
1765             else
1766                 send(c, notokmsg, sizeof notokmsg-1, 0);
1767         }
1768         if (strstr(buffer, "/testD"))
1769         {
1770             send(c, okmsg2, sizeof okmsg2-1, 0);
1771         }
1772         if (strstr(buffer, "/testE"))
1773         {
1774             send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1775         }
1776         if (strstr(buffer, "GET /quit"))
1777         {
1778             send(c, okmsg, sizeof okmsg-1, 0);
1779             send(c, page1, sizeof page1-1, 0);
1780             last_request = 1;
1781         }
1782         if (strstr(buffer, "GET /testF"))
1783         {
1784             send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
1785             send(c, garbagemsg, sizeof garbagemsg-1, 0);
1786             send(c, contmsg, sizeof contmsg-1, 0);
1787             send(c, garbagemsg, sizeof garbagemsg-1, 0);
1788             send(c, okmsg, sizeof okmsg-1, 0);
1789             send(c, page1, sizeof page1-1, 0);
1790         }
1791         if (strstr(buffer, "GET /testG"))
1792         {
1793             send(c, page1, sizeof page1-1, 0);
1794         }
1795
1796         shutdown(c, 2);
1797         closesocket(c);
1798     } while (!last_request);
1799
1800     closesocket(s);
1801
1802     return 0;
1803 }
1804
1805 static void test_basic_request(int port, const char *verb, const char *url)
1806 {
1807     HINTERNET hi, hc, hr;
1808     DWORD r, count;
1809     char buffer[0x100];
1810
1811     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1812     ok(hi != NULL, "open failed\n");
1813
1814     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1815     ok(hc != NULL, "connect failed\n");
1816
1817     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1818     ok(hr != NULL, "HttpOpenRequest failed\n");
1819
1820     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1821     ok(r, "HttpSendRequest failed\n");
1822
1823     count = 0;
1824     memset(buffer, 0, sizeof buffer);
1825     SetLastError(0xdeadbeef);
1826     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1827     ok(r, "InternetReadFile failed %u\n", GetLastError());
1828     ok(count == sizeof page1 - 1, "count was wrong\n");
1829     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1830
1831     InternetCloseHandle(hr);
1832     InternetCloseHandle(hc);
1833     InternetCloseHandle(hi);
1834 }
1835
1836 static void test_last_error(int port)
1837 {
1838     HINTERNET hi, hc, hr;
1839     DWORD error;
1840     BOOL r;
1841
1842     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1843     ok(hi != NULL, "open failed\n");
1844
1845     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1846     ok(hc != NULL, "connect failed\n");
1847
1848     hr = HttpOpenRequest(hc, NULL, "/test1", NULL, NULL, NULL, 0, 0);
1849     ok(hr != NULL, "HttpOpenRequest failed\n");
1850
1851     SetLastError(0xdeadbeef);
1852     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1853     error = GetLastError();
1854     ok(r, "HttpSendRequest failed\n");
1855     ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
1856
1857     InternetCloseHandle(hr);
1858     InternetCloseHandle(hc);
1859     InternetCloseHandle(hi);
1860 }
1861
1862 static void test_proxy_indirect(int port)
1863 {
1864     HINTERNET hi, hc, hr;
1865     DWORD r, sz, val;
1866     char buffer[0x40];
1867
1868     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1869     ok(hi != NULL, "open failed\n");
1870
1871     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1872     ok(hc != NULL, "connect failed\n");
1873
1874     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1875     ok(hr != NULL, "HttpOpenRequest failed\n");
1876
1877     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1878     ok(r, "HttpSendRequest failed\n");
1879
1880     sz = sizeof buffer;
1881     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1882     ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
1883     if (!r)
1884     {
1885         skip("missing proxy header, not testing remaining proxy headers\n");
1886         goto out;
1887     }
1888     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1889
1890     sz = sizeof buffer;
1891     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1892     ok(r, "HttpQueryInfo failed\n");
1893     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1894
1895     sz = sizeof val;
1896     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1897     ok(r, "HttpQueryInfo failed\n");
1898     ok(val == 407, "proxy code wrong\n");
1899
1900     sz = sizeof buffer;
1901     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1902     ok(r, "HttpQueryInfo failed\n");
1903     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1904
1905     sz = sizeof buffer;
1906     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1907     ok(r, "HttpQueryInfo failed\n");
1908     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1909
1910     sz = sizeof buffer;
1911     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1912     ok(r, "HttpQueryInfo failed\n");
1913     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1914
1915     sz = sizeof buffer;
1916     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1917     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1918     ok(r == FALSE, "HttpQueryInfo failed\n");
1919
1920 out:
1921     InternetCloseHandle(hr);
1922     InternetCloseHandle(hc);
1923     InternetCloseHandle(hi);
1924 }
1925
1926 static void test_proxy_direct(int port)
1927 {
1928     HINTERNET hi, hc, hr;
1929     DWORD r, sz;
1930     char buffer[0x40];
1931     static CHAR username[] = "mike",
1932                 password[] = "1101";
1933
1934     sprintf(buffer, "localhost:%d\n", port);
1935     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1936     ok(hi != NULL, "open failed\n");
1937
1938     /* try connect without authorization */
1939     hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1940     ok(hc != NULL, "connect failed\n");
1941
1942     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1943     ok(hr != NULL, "HttpOpenRequest failed\n");
1944
1945     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1946     ok(r, "HttpSendRequest failed\n");
1947
1948     sz = sizeof buffer;
1949     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1950     ok(r, "HttpQueryInfo failed\n");
1951     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1952
1953
1954     /* set the user + password then try again */
1955     todo_wine {
1956     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1957     ok(r, "failed to set user\n");
1958
1959     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1960     ok(r, "failed to set password\n");
1961     }
1962
1963     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1964     ok(r, "HttpSendRequest failed\n");
1965     sz = sizeof buffer;
1966     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1967     ok(r, "HttpQueryInfo failed\n");
1968     todo_wine {
1969     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1970     }
1971
1972
1973     InternetCloseHandle(hr);
1974     InternetCloseHandle(hc);
1975     InternetCloseHandle(hi);
1976 }
1977
1978 static void test_header_handling_order(int port)
1979 {
1980     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1981     static char connection[]    = "Connection: Close";
1982
1983     static const char *types[2] = { "*", NULL };
1984     HINTERNET session, connect, request;
1985     DWORD size, status;
1986     BOOL ret;
1987
1988     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1989     ok(session != NULL, "InternetOpen failed\n");
1990
1991     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1992     ok(connect != NULL, "InternetConnect failed\n");
1993
1994     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1995     ok(request != NULL, "HttpOpenRequest failed\n");
1996
1997     ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
1998     ok(ret, "HttpAddRequestHeaders failed\n");
1999
2000     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2001     ok(ret, "HttpSendRequest failed\n");
2002
2003     status = 0;
2004     size = sizeof(status);
2005     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2006     ok(ret, "HttpQueryInfo failed\n");
2007     ok(status == 200, "request failed with status %u\n", status);
2008
2009     InternetCloseHandle(request);
2010
2011     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2012     ok(request != NULL, "HttpOpenRequest failed\n");
2013
2014     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2015     ok(ret, "HttpSendRequest failed\n");
2016
2017     status = 0;
2018     size = sizeof(status);
2019     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2020     ok(ret, "HttpQueryInfo failed\n");
2021     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2022
2023     InternetCloseHandle(request);
2024
2025     request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2026     ok(request != NULL, "HttpOpenRequest failed\n");
2027
2028     ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2029     ok(ret, "HttpAddRequestHeaders failed\n");
2030
2031     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2032     ok(ret, "HttpSendRequest failed\n");
2033
2034     status = 0;
2035     size = sizeof(status);
2036     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2037     ok(ret, "HttpQueryInfo failed\n");
2038     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2039
2040     InternetCloseHandle(request);
2041     InternetCloseHandle(connect);
2042     InternetCloseHandle(session);
2043 }
2044
2045 static void test_connection_header(int port)
2046 {
2047     HINTERNET ses, con, req;
2048     DWORD size, status;
2049     BOOL ret;
2050
2051     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2052     ok(ses != NULL, "InternetOpen failed\n");
2053
2054     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2055     ok(con != NULL, "InternetConnect failed\n");
2056
2057     req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2058     ok(req != NULL, "HttpOpenRequest failed\n");
2059
2060     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2061     ok(ret, "HttpSendRequest failed\n");
2062
2063     status = 0;
2064     size = sizeof(status);
2065     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2066     ok(ret, "HttpQueryInfo failed\n");
2067     ok(status == 200, "request failed with status %u\n", status);
2068
2069     InternetCloseHandle(req);
2070
2071     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
2072     ok(req != NULL, "HttpOpenRequest failed\n");
2073
2074     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2075     ok(ret, "HttpSendRequest failed\n");
2076
2077     status = 0;
2078     size = sizeof(status);
2079     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2080     ok(ret, "HttpQueryInfo failed\n");
2081     ok(status == 200, "request failed with status %u\n", status);
2082
2083     InternetCloseHandle(req);
2084
2085     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2086     ok(req != NULL, "HttpOpenRequest failed\n");
2087
2088     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2089     ok(ret, "HttpSendRequest failed\n");
2090
2091     status = 0;
2092     size = sizeof(status);
2093     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2094     ok(ret, "HttpQueryInfo failed\n");
2095     ok(status == 200, "request failed with status %u\n", status);
2096
2097     InternetCloseHandle(req);
2098
2099     req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2100     ok(req != NULL, "HttpOpenRequest failed\n");
2101
2102     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2103     ok(ret, "HttpSendRequest failed\n");
2104
2105     status = 0;
2106     size = sizeof(status);
2107     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2108     ok(ret, "HttpQueryInfo failed\n");
2109     ok(status == 200, "request failed with status %u\n", status);
2110
2111     InternetCloseHandle(req);
2112     InternetCloseHandle(con);
2113     InternetCloseHandle(ses);
2114 }
2115
2116 static void test_http1_1(int port)
2117 {
2118     HINTERNET ses, con, req;
2119     BOOL ret;
2120
2121     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2122     ok(ses != NULL, "InternetOpen failed\n");
2123
2124     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2125     ok(con != NULL, "InternetConnect failed\n");
2126
2127     req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2128     ok(req != NULL, "HttpOpenRequest failed\n");
2129
2130     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2131     if (ret)
2132     {
2133         InternetCloseHandle(req);
2134
2135         req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2136         ok(req != NULL, "HttpOpenRequest failed\n");
2137
2138         ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2139         ok(ret, "HttpSendRequest failed\n");
2140     }
2141
2142     InternetCloseHandle(req);
2143     InternetCloseHandle(con);
2144     InternetCloseHandle(ses);
2145 }
2146
2147 static void test_HttpSendRequestW(int port)
2148 {
2149     static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
2150     HINTERNET ses, con, req;
2151     DWORD error;
2152     BOOL ret;
2153
2154     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
2155     ok(ses != NULL, "InternetOpen failed\n");
2156
2157     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2158     ok(con != NULL, "InternetConnect failed\n");
2159
2160     req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2161     ok(req != NULL, "HttpOpenRequest failed\n");
2162
2163     SetLastError(0xdeadbeef);
2164     ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
2165     error = GetLastError();
2166     ok(!ret, "HttpSendRequestW succeeded\n");
2167     ok(error == ERROR_IO_PENDING ||
2168        broken(error == ERROR_HTTP_HEADER_NOT_FOUND) ||  /* IE6 */
2169        broken(error == ERROR_INVALID_PARAMETER),        /* IE5 */
2170        "got %u expected ERROR_IO_PENDING\n", error);
2171
2172     InternetCloseHandle(req);
2173     InternetCloseHandle(con);
2174     InternetCloseHandle(ses);
2175 }
2176
2177 static void test_cookie_header(int port)
2178 {
2179     HINTERNET ses, con, req;
2180     DWORD size, status, error;
2181     BOOL ret;
2182     char buffer[64];
2183
2184     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2185     ok(ses != NULL, "InternetOpen failed\n");
2186
2187     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2188     ok(con != NULL, "InternetConnect failed\n");
2189
2190     InternetSetCookie("http://localhost", "cookie", "biscuit");
2191
2192     req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2193     ok(req != NULL, "HttpOpenRequest failed\n");
2194
2195     buffer[0] = 0;
2196     size = sizeof(buffer);
2197     SetLastError(0xdeadbeef);
2198     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2199     error = GetLastError();
2200     ok(!ret, "HttpQueryInfo succeeded\n");
2201     ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
2202
2203     ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
2204     ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
2205
2206     buffer[0] = 0;
2207     size = sizeof(buffer);
2208     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2209     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2210     ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
2211
2212     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2213     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
2214
2215     status = 0;
2216     size = sizeof(status);
2217     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2218     ok(ret, "HttpQueryInfo failed\n");
2219     ok(status == 200, "request failed with status %u\n", status);
2220
2221     buffer[0] = 0;
2222     size = sizeof(buffer);
2223     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2224     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2225     ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
2226
2227     InternetCloseHandle(req);
2228     InternetCloseHandle(con);
2229     InternetCloseHandle(ses);
2230 }
2231
2232 static void test_basic_authentication(int port)
2233 {
2234     HINTERNET session, connect, request;
2235     DWORD size, status;
2236     BOOL ret;
2237
2238     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2239     ok(session != NULL, "InternetOpen failed\n");
2240
2241     connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
2242     ok(connect != NULL, "InternetConnect failed\n");
2243
2244     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
2245     ok(request != NULL, "HttpOpenRequest failed\n");
2246
2247     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2248     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2249
2250     status = 0;
2251     size = sizeof(status);
2252     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2253     ok(ret, "HttpQueryInfo failed\n");
2254     ok(status == 200, "request failed with status %u\n", status);
2255
2256     InternetCloseHandle(request);
2257     InternetCloseHandle(connect);
2258     InternetCloseHandle(session);
2259 }
2260
2261 static void test_invalid_response_headers(int port)
2262 {
2263     HINTERNET session, connect, request;
2264     DWORD size, status;
2265     BOOL ret;
2266     char buffer[256];
2267
2268     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2269     ok(session != NULL, "InternetOpen failed\n");
2270
2271     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2272     ok(connect != NULL, "InternetConnect failed\n");
2273
2274     request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
2275     ok(request != NULL, "HttpOpenRequest failed\n");
2276
2277     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2278     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2279
2280     status = 0;
2281     size = sizeof(status);
2282     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2283     ok(ret, "HttpQueryInfo failed\n");
2284     ok(status == 401, "unexpected status %u\n", status);
2285
2286     buffer[0] = 0;
2287     size = sizeof(buffer);
2288     ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2289     ok(ret, "HttpQueryInfo failed\n");
2290     ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
2291        "headers wrong \"%s\"\n", buffer);
2292
2293     buffer[0] = 0;
2294     size = sizeof(buffer);
2295     ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
2296     ok(ret, "HttpQueryInfo failed\n");
2297     ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
2298
2299     InternetCloseHandle(request);
2300     InternetCloseHandle(connect);
2301     InternetCloseHandle(session);
2302 }
2303
2304 static void test_response_without_headers(int port)
2305 {
2306     HINTERNET hi, hc, hr;
2307     DWORD r, count, size, status;
2308     char buffer[1024];
2309
2310     SetLastError(0xdeadbeef);
2311     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2312     ok(hi != NULL, "open failed %u\n", GetLastError());
2313
2314     SetLastError(0xdeadbeef);
2315     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2316     ok(hc != NULL, "connect failed %u\n", GetLastError());
2317
2318     SetLastError(0xdeadbeef);
2319     hr = HttpOpenRequest(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
2320     ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
2321
2322     SetLastError(0xdeadbeef);
2323     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2324     ok(r, "HttpSendRequest failed %u\n", GetLastError());
2325
2326     count = 0;
2327     memset(buffer, 0, sizeof buffer);
2328     SetLastError(0xdeadbeef);
2329     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2330     ok(r, "InternetReadFile failed %u\n", GetLastError());
2331     todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
2332     todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2333
2334     status = 0;
2335     size = sizeof(status);
2336     SetLastError(0xdeadbeef);
2337     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2338     todo_wine ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2339     todo_wine ok(status == 200, "expected status 200 got %u\n", status);
2340
2341     buffer[0] = 0;
2342     size = sizeof(buffer);
2343     SetLastError(0xdeadbeef);
2344     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
2345     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2346     ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
2347
2348     buffer[0] = 0;
2349     size = sizeof(buffer);
2350     SetLastError(0xdeadbeef);
2351     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
2352     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2353     ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
2354
2355     buffer[0] = 0;
2356     size = sizeof(buffer);
2357     SetLastError(0xdeadbeef);
2358     r = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2359     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2360     ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
2361
2362     InternetCloseHandle(hr);
2363     InternetCloseHandle(hc);
2364     InternetCloseHandle(hi);
2365 }
2366
2367 static void test_HttpQueryInfo(int port)
2368 {
2369     HINTERNET hi, hc, hr;
2370     DWORD size, index;
2371     char buffer[1024];
2372     BOOL ret;
2373
2374     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2375     ok(hi != NULL, "InternetOpen failed\n");
2376
2377     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2378     ok(hc != NULL, "InternetConnect failed\n");
2379
2380     hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
2381     ok(hr != NULL, "HttpOpenRequest failed\n");
2382
2383     ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
2384     ok(ret, "HttpSendRequest failed\n");
2385
2386     index = 0;
2387     size = sizeof(buffer);
2388     ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
2389     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2390     ok(index == 1, "expected 1 got %u\n", index);
2391
2392     index = 0;
2393     size = sizeof(buffer);
2394     ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2395     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2396     ok(index == 1, "expected 1 got %u\n", index);
2397
2398     index = 0;
2399     size = sizeof(buffer);
2400     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2401     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2402     ok(index == 0, "expected 0 got %u\n", index);
2403
2404     size = sizeof(buffer);
2405     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2406     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2407     ok(index == 0, "expected 0 got %u\n", index);
2408
2409     size = sizeof(buffer);
2410     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2411     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2412     ok(index == 0, "expected 0 got %u\n", index);
2413
2414     size = sizeof(buffer);
2415     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2416     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2417     ok(index == 0, "expected 0 got %u\n", index);
2418
2419     size = sizeof(buffer);
2420     ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2421     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2422     ok(index == 0, "expected 0 got %u\n", index);
2423
2424     index = 0;
2425     size = sizeof(buffer);
2426     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2427     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2428     ok(index == 0, "expected 0 got %u\n", index);
2429
2430     index = 0;
2431     size = sizeof(buffer);
2432     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2433     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2434     ok(index == 0, "expected 0 got %u\n", index);
2435
2436     index = 0xdeadbeef;
2437     size = sizeof(buffer);
2438     ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2439     ok(!ret, "HttpQueryInfo succeeded\n");
2440     ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2441
2442     index = 0;
2443     size = sizeof(buffer);
2444     ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2445     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2446     ok(index == 1, "expected 1 got %u\n", index);
2447
2448     index = 0;
2449     size = sizeof(buffer);
2450     strcpy(buffer, "Server");
2451     ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2452     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2453     ok(index == 1, "expected 1 got %u\n", index);
2454
2455     index = 0;
2456     size = sizeof(buffer);
2457     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2458     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2459     ok(index == 1, "expected 1 got %u\n", index);
2460
2461     size = sizeof(buffer);
2462     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2463     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2464     ok(index == 2, "expected 2 got %u\n", index);
2465
2466     InternetCloseHandle(hr);
2467     InternetCloseHandle(hc);
2468     InternetCloseHandle(hi);
2469 }
2470
2471 static void test_options(int port)
2472 {
2473     HINTERNET ses, con, req;
2474     DWORD size, error;
2475     DWORD_PTR ctx;
2476     BOOL ret;
2477
2478     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2479     ok(ses != NULL, "InternetOpen failed\n");
2480
2481     SetLastError(0xdeadbeef);
2482     ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, 0);
2483     error = GetLastError();
2484     ok(!ret, "InternetSetOption succeeded\n");
2485     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2486
2487     SetLastError(0xdeadbeef);
2488     ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, sizeof(ctx));
2489     ok(!ret, "InternetSetOption succeeded\n");
2490     error = GetLastError();
2491     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2492
2493     SetLastError(0xdeadbeef);
2494     ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, 0);
2495     ok(!ret, "InternetSetOption succeeded\n");
2496     error = GetLastError();
2497     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2498
2499     ctx = 1;
2500     ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2501     ok(ret, "InternetSetOption failed %u\n", GetLastError());
2502
2503     SetLastError(0xdeadbeef);
2504     ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, NULL);
2505     error = GetLastError();
2506     ok(!ret, "InternetQueryOption succeeded\n");
2507     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2508
2509     SetLastError(0xdeadbeef);
2510     ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, NULL);
2511     error = GetLastError();
2512     ok(!ret, "InternetQueryOption succeeded\n");
2513     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2514
2515     size = 0;
2516     SetLastError(0xdeadbeef);
2517     ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, &size);
2518     error = GetLastError();
2519     ok(!ret, "InternetQueryOption succeeded\n");
2520     ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
2521
2522     size = sizeof(ctx);
2523     SetLastError(0xdeadbeef);
2524     ret = InternetQueryOption(NULL, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2525     error = GetLastError();
2526     ok(!ret, "InternetQueryOption succeeded\n");
2527     ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %u\n", error);
2528
2529     ctx = 0xdeadbeef;
2530     size = sizeof(ctx);
2531     ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2532     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2533     ok(ctx == 1, "expected 1 got %lu\n", ctx);
2534
2535     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2536     ok(con != NULL, "InternetConnect failed\n");
2537
2538     ctx = 0xdeadbeef;
2539     size = sizeof(ctx);
2540     ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2541     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2542     ok(ctx == 0, "expected 0 got %lu\n", ctx);
2543
2544     ctx = 2;
2545     ret = InternetSetOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2546     ok(ret, "InternetSetOption failed %u\n", GetLastError());
2547
2548     ctx = 0xdeadbeef;
2549     size = sizeof(ctx);
2550     ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2551     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2552     ok(ctx == 2, "expected 2 got %lu\n", ctx);
2553
2554     req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2555     ok(req != NULL, "HttpOpenRequest failed\n");
2556
2557     ctx = 0xdeadbeef;
2558     size = sizeof(ctx);
2559     ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2560     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2561     ok(ctx == 0, "expected 0 got %lu\n", ctx);
2562
2563     ctx = 3;
2564     ret = InternetSetOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2565     ok(ret, "InternetSetOption failed %u\n", GetLastError());
2566
2567     ctx = 0xdeadbeef;
2568     size = sizeof(ctx);
2569     ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2570     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2571     ok(ctx == 3, "expected 3 got %lu\n", ctx);
2572
2573     InternetCloseHandle(req);
2574     InternetCloseHandle(con);
2575     InternetCloseHandle(ses);
2576 }
2577
2578 static void test_http_connection(void)
2579 {
2580     struct server_info si;
2581     HANDLE hThread;
2582     DWORD id = 0, r;
2583
2584     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2585     si.port = 7531;
2586
2587     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2588     ok( hThread != NULL, "create thread failed\n");
2589
2590     r = WaitForSingleObject(si.hEvent, 10000);
2591     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2592     if (r != WAIT_OBJECT_0)
2593         return;
2594
2595     test_basic_request(si.port, "GET", "/test1");
2596     test_proxy_indirect(si.port);
2597     test_proxy_direct(si.port);
2598     test_header_handling_order(si.port);
2599     test_basic_request(si.port, "POST", "/test5");
2600     test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2601     test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2602     test_basic_request(si.port, "GET", "/test6");
2603     test_basic_request(si.port, "GET", "/testF");
2604     test_connection_header(si.port);
2605     test_http1_1(si.port);
2606     test_cookie_header(si.port);
2607     test_basic_authentication(si.port);
2608     test_invalid_response_headers(si.port);
2609     test_response_without_headers(si.port);
2610     test_HttpQueryInfo(si.port);
2611     test_HttpSendRequestW(si.port);
2612     test_last_error(si.port);
2613     test_options(si.port);
2614
2615     /* send the basic request again to shutdown the server thread */
2616     test_basic_request(si.port, "GET", "/quit");
2617
2618     r = WaitForSingleObject(hThread, 3000);
2619     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2620     CloseHandle(hThread);
2621 }
2622
2623 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
2624 {
2625     LocalFree(info->lpszSubjectInfo);
2626     LocalFree(info->lpszIssuerInfo);
2627     LocalFree(info->lpszProtocolName);
2628     LocalFree(info->lpszSignatureAlgName);
2629     LocalFree(info->lpszEncryptionAlgName);
2630 }
2631
2632 static void test_secure_connection(void)
2633 {
2634     static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
2635     static const WCHAR testbot[] = {'t','e','s','t','b','o','t','.','w','i','n','e','h','q','.','o','r','g',0};
2636     static const WCHAR get[] = {'G','E','T',0};
2637     static const WCHAR slash[] = {'/',0};
2638     HINTERNET ses, con, req;
2639     DWORD size, flags;
2640     INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
2641     INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
2642     BOOL ret;
2643
2644     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2645     ok(ses != NULL, "InternetOpen failed\n");
2646
2647     con = InternetConnect(ses, "testbot.winehq.org",
2648                           INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
2649                           INTERNET_SERVICE_HTTP, 0, 0);
2650     ok(con != NULL, "InternetConnect failed\n");
2651
2652     req = HttpOpenRequest(con, "GET", "/", NULL, NULL, NULL,
2653                           INTERNET_FLAG_SECURE, 0);
2654     ok(req != NULL, "HttpOpenRequest failed\n");
2655
2656     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2657     ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
2658
2659     size = sizeof(flags);
2660     ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
2661     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2662     ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
2663
2664     ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2665                                NULL, &size);
2666     ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2667     ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
2668     certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
2669     ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2670                               certificate_structA, &size);
2671     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2672     if (ret)
2673     {
2674         ok(certificate_structA->lpszSubjectInfo &&
2675            strlen(certificate_structA->lpszSubjectInfo) > 1,
2676            "expected a non-empty subject name\n");
2677         ok(certificate_structA->lpszIssuerInfo &&
2678            strlen(certificate_structA->lpszIssuerInfo) > 1,
2679            "expected a non-empty issuer name\n");
2680         ok(!certificate_structA->lpszSignatureAlgName,
2681            "unexpected signature algorithm name\n");
2682         ok(!certificate_structA->lpszEncryptionAlgName,
2683            "unexpected encryption algorithm name\n");
2684         ok(!certificate_structA->lpszProtocolName,
2685            "unexpected protocol name\n");
2686         ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2687         release_cert_info(certificate_structA);
2688     }
2689     HeapFree(GetProcessHeap(), 0, certificate_structA);
2690
2691     /* Querying the same option through InternetQueryOptionW still results in
2692      * ASCII strings being returned.
2693      */
2694     size = 0;
2695     ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2696                                NULL, &size);
2697     ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2698     ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
2699     certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
2700     ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2701                               certificate_structW, &size);
2702     certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
2703     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2704     if (ret)
2705     {
2706         ok(certificate_structA->lpszSubjectInfo &&
2707            strlen(certificate_structA->lpszSubjectInfo) > 1,
2708            "expected a non-empty subject name\n");
2709         ok(certificate_structA->lpszIssuerInfo &&
2710            strlen(certificate_structA->lpszIssuerInfo) > 1,
2711            "expected a non-empty issuer name\n");
2712         ok(!certificate_structA->lpszSignatureAlgName,
2713            "unexpected signature algorithm name\n");
2714         ok(!certificate_structA->lpszEncryptionAlgName,
2715            "unexpected encryption algorithm name\n");
2716         ok(!certificate_structA->lpszProtocolName,
2717            "unexpected protocol name\n");
2718         ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2719         release_cert_info(certificate_structA);
2720     }
2721     HeapFree(GetProcessHeap(), 0, certificate_structW);
2722
2723     InternetCloseHandle(req);
2724     InternetCloseHandle(con);
2725     InternetCloseHandle(ses);
2726
2727     /* Repeating the tests with the W functions has the same result: */
2728     ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2729     ok(ses != NULL, "InternetOpen failed\n");
2730
2731     con = InternetConnectW(ses, testbot,
2732                           INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
2733                           INTERNET_SERVICE_HTTP, 0, 0);
2734     ok(con != NULL, "InternetConnect failed\n");
2735
2736     req = HttpOpenRequestW(con, get, slash, NULL, NULL, NULL,
2737                           INTERNET_FLAG_SECURE, 0);
2738     ok(req != NULL, "HttpOpenRequest failed\n");
2739
2740     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2741     ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
2742
2743     size = sizeof(flags);
2744     ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
2745     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2746     ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
2747
2748     ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2749                                NULL, &size);
2750     ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2751     ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
2752     certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
2753     ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2754                                certificate_structA, &size);
2755     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2756     if (ret)
2757     {
2758         ok(certificate_structA->lpszSubjectInfo &&
2759            strlen(certificate_structA->lpszSubjectInfo) > 1,
2760            "expected a non-empty subject name\n");
2761         ok(certificate_structA->lpszIssuerInfo &&
2762            strlen(certificate_structA->lpszIssuerInfo) > 1,
2763            "expected a non-empty issuer name\n");
2764         ok(!certificate_structA->lpszSignatureAlgName,
2765            "unexpected signature algorithm name\n");
2766         ok(!certificate_structA->lpszEncryptionAlgName,
2767            "unexpected encryption algorithm name\n");
2768         ok(!certificate_structA->lpszProtocolName,
2769            "unexpected protocol name\n");
2770         ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2771         release_cert_info(certificate_structA);
2772     }
2773     HeapFree(GetProcessHeap(), 0, certificate_structA);
2774
2775     /* Again, querying the same option through InternetQueryOptionW still
2776      * results in ASCII strings being returned.
2777      */
2778     size = 0;
2779     ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2780                                NULL, &size);
2781     ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2782     ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
2783     certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
2784     ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2785                                certificate_structW, &size);
2786     certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
2787     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2788     if (ret)
2789     {
2790         ok(certificate_structA->lpszSubjectInfo &&
2791            strlen(certificate_structA->lpszSubjectInfo) > 1,
2792            "expected a non-empty subject name\n");
2793         ok(certificate_structA->lpszIssuerInfo &&
2794            strlen(certificate_structA->lpszIssuerInfo) > 1,
2795            "expected a non-empty issuer name\n");
2796         ok(!certificate_structA->lpszSignatureAlgName,
2797            "unexpected signature algorithm name\n");
2798         ok(!certificate_structA->lpszEncryptionAlgName,
2799            "unexpected encryption algorithm name\n");
2800         ok(!certificate_structA->lpszProtocolName,
2801            "unexpected protocol name\n");
2802         ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2803         release_cert_info(certificate_structA);
2804     }
2805     HeapFree(GetProcessHeap(), 0, certificate_structW);
2806
2807     InternetCloseHandle(req);
2808     InternetCloseHandle(con);
2809     InternetCloseHandle(ses);
2810 }
2811
2812 static void test_user_agent_header(void)
2813 {
2814     HINTERNET ses, con, req;
2815     DWORD size, err;
2816     char buffer[64];
2817     BOOL ret;
2818
2819     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2820     ok(ses != NULL, "InternetOpen failed\n");
2821
2822     con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2823     ok(con != NULL, "InternetConnect failed\n");
2824
2825     req = HttpOpenRequest(con, "GET", "/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
2826     ok(req != NULL, "HttpOpenRequest failed\n");
2827
2828     size = sizeof(buffer);
2829     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2830     err = GetLastError();
2831     ok(!ret, "HttpQueryInfo succeeded\n");
2832     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2833
2834     ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2835     ok(ret, "HttpAddRequestHeaders succeeded\n");
2836
2837     size = sizeof(buffer);
2838     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2839     err = GetLastError();
2840     ok(ret, "HttpQueryInfo failed\n");
2841     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2842
2843     InternetCloseHandle(req);
2844
2845     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2846     ok(req != NULL, "HttpOpenRequest failed\n");
2847
2848     size = sizeof(buffer);
2849     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2850     err = GetLastError();
2851     ok(!ret, "HttpQueryInfo succeeded\n");
2852     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2853
2854     ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2855     ok(ret, "HttpAddRequestHeaders failed\n");
2856
2857     buffer[0] = 0;
2858     size = sizeof(buffer);
2859     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2860     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2861     ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2862
2863     InternetCloseHandle(req);
2864     InternetCloseHandle(con);
2865     InternetCloseHandle(ses);
2866 }
2867
2868 static void test_bogus_accept_types_array(void)
2869 {
2870     HINTERNET ses, con, req;
2871     static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2872     DWORD size;
2873     char buffer[32];
2874     BOOL ret;
2875
2876     ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2877     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2878     req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2879
2880     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2881
2882     buffer[0] = 0;
2883     size = sizeof(buffer);
2884     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2885     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2886     ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2887        !strcmp(buffer, "*/*, %p, */*"),
2888        "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
2889
2890     InternetCloseHandle(req);
2891     InternetCloseHandle(con);
2892     InternetCloseHandle(ses);
2893 }
2894
2895 struct context
2896 {
2897     HANDLE event;
2898     HINTERNET req;
2899 };
2900
2901 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2902 {
2903     INTERNET_ASYNC_RESULT *result = info;
2904     struct context *ctx = (struct context *)context;
2905
2906     trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2907
2908     if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2909     {
2910         trace("request handle: 0x%08lx\n", result->dwResult);
2911         ctx->req = (HINTERNET)result->dwResult;
2912         SetEvent(ctx->event);
2913     }
2914     if (status == INTERNET_STATUS_HANDLE_CLOSING)
2915     {
2916         DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2917
2918         if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2919             ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2920         SetEvent(ctx->event);
2921     }
2922 }
2923
2924 static void test_open_url_async(void)
2925 {
2926     BOOL ret;
2927     HINTERNET ses, req;
2928     DWORD size, error;
2929     struct context ctx;
2930     ULONG type;
2931
2932     ctx.req = NULL;
2933     ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2934
2935     ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2936     ok(ses != NULL, "InternetOpen failed\n");
2937
2938     SetLastError(0xdeadbeef);
2939     ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2940     error = GetLastError();
2941     ok(!ret, "InternetSetOptionA succeeded\n");
2942     ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
2943
2944     ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2945     error = GetLastError();
2946     ok(!ret, "InternetSetOptionA failed\n");
2947     ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
2948
2949     pInternetSetStatusCallbackA(ses, cb);
2950     ResetEvent(ctx.event);
2951
2952     req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2953     ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2954
2955     WaitForSingleObject(ctx.event, INFINITE);
2956
2957     type = 0;
2958     size = sizeof(type);
2959     ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2960     ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2961     ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2962        "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2963
2964     size = 0;
2965     ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2966     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2967     ok(size > 0, "expected size > 0\n");
2968
2969     ResetEvent(ctx.event);
2970     InternetCloseHandle(ctx.req);
2971     WaitForSingleObject(ctx.event, INFINITE);
2972
2973     InternetCloseHandle(ses);
2974     CloseHandle(ctx.event);
2975 }
2976
2977 enum api
2978 {
2979     internet_connect = 1,
2980     http_open_request,
2981     http_send_request_ex,
2982     internet_writefile,
2983     http_end_request,
2984     internet_close_handle
2985 };
2986
2987 struct notification
2988 {
2989     enum api     function; /* api responsible for notification */
2990     unsigned int status;   /* status received */
2991     int          async;    /* delivered from another thread? */
2992     int          todo;
2993     int          optional;
2994 };
2995
2996 struct info
2997 {
2998     enum api     function;
2999     const struct notification *test;
3000     unsigned int count;
3001     unsigned int index;
3002     HANDLE       wait;
3003     DWORD        thread;
3004     unsigned int line;
3005 };
3006
3007 static CRITICAL_SECTION notification_cs;
3008
3009 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
3010 {
3011     BOOL status_ok, function_ok;
3012     struct info *info = (struct info *)context;
3013     unsigned int i;
3014
3015     EnterCriticalSection( &notification_cs );
3016
3017     if (status == INTERNET_STATUS_HANDLE_CREATED)
3018     {
3019         DWORD size = sizeof(struct info *);
3020         HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
3021     }
3022     i = info->index;
3023     if (i >= info->count)
3024     {
3025         LeaveCriticalSection( &notification_cs );
3026         return;
3027     }
3028
3029     while (info->test[i].status != status && info->test[i].optional &&
3030         i < info->count - 1 &&
3031         info->test[i].function == info->test[i + 1].function)
3032     {
3033         i++;
3034     }
3035
3036     status_ok   = (info->test[i].status == status);
3037     function_ok = (info->test[i].function == info->function);
3038
3039     if (!info->test[i].todo)
3040     {
3041         ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
3042         ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
3043
3044         if (info->test[i].async)
3045             ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
3046                info->line, info->thread, GetCurrentThreadId());
3047     }
3048     else
3049     {
3050         todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
3051         if (status_ok)
3052             todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
3053     }
3054     if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
3055     info->index = i+1;
3056
3057     LeaveCriticalSection( &notification_cs );
3058 }
3059
3060 static void setup_test( struct info *info, enum api function, unsigned int line )
3061 {
3062     info->function = function;
3063     info->line = line;
3064 }
3065
3066 static const struct notification async_send_request_ex_test[] =
3067 {
3068     { internet_connect,      INTERNET_STATUS_HANDLE_CREATED, 0 },
3069     { http_open_request,     INTERNET_STATUS_HANDLE_CREATED, 0 },
3070     { http_send_request_ex,  INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
3071     { http_send_request_ex,  INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
3072     { http_send_request_ex,  INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
3073     { http_send_request_ex,  INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
3074     { http_send_request_ex,  INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
3075     { http_send_request_ex,  INTERNET_STATUS_SENDING_REQUEST, 1 },
3076     { http_send_request_ex,  INTERNET_STATUS_REQUEST_SENT, 1 },
3077     { http_send_request_ex,  INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3078     { internet_writefile,    INTERNET_STATUS_SENDING_REQUEST, 0 },
3079     { internet_writefile,    INTERNET_STATUS_REQUEST_SENT, 0 },
3080     { http_end_request,      INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
3081     { http_end_request,      INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
3082     { http_end_request,      INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3083     { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
3084     { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
3085     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
3086     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
3087 };
3088
3089 static void test_async_HttpSendRequestEx(void)
3090 {
3091     BOOL ret;
3092     HINTERNET ses, req, con;
3093     struct info info;
3094     DWORD size, written, error;
3095     INTERNET_BUFFERSA b;
3096     static const char *accept[2] = {"*/*", NULL};
3097     static char data[] = "Public ID=codeweavers";
3098     char buffer[32];
3099
3100     InitializeCriticalSection( &notification_cs );
3101
3102     info.test  = async_send_request_ex_test;
3103     info.count = sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]);
3104     info.index = 0;
3105     info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
3106     info.thread = GetCurrentThreadId();
3107
3108     ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
3109     ok( ses != NULL, "InternetOpen failed\n" );
3110
3111     pInternetSetStatusCallbackA( ses, check_notification );
3112
3113     setup_test( &info, internet_connect, __LINE__ );
3114     con = InternetConnect( ses, "crossover.codeweavers.com", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
3115     ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
3116
3117     WaitForSingleObject( info.wait, 10000 );
3118
3119     setup_test( &info, http_open_request, __LINE__ );
3120     req = HttpOpenRequest( con, "POST", "posttest.php", NULL, NULL, accept, 0, (DWORD_PTR)&info );
3121     ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
3122
3123     WaitForSingleObject( info.wait, 10000 );
3124
3125     memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
3126     b.dwStructSize = sizeof(INTERNET_BUFFERSA);
3127     b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
3128     b.dwHeadersLength = strlen( b.lpcszHeader );
3129     b.dwBufferTotal = strlen( data );
3130
3131     setup_test( &info, http_send_request_ex, __LINE__ );
3132     ret = HttpSendRequestExA( req, &b, NULL, 0x28, 0 );
3133     ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
3134
3135     WaitForSingleObject( info.wait, 10000 );
3136
3137     size = sizeof(buffer);
3138     SetLastError( 0xdeadbeef );
3139     ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
3140     error = GetLastError();
3141     ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
3142     todo_wine
3143     ok( error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
3144         "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
3145
3146     written = 0;
3147     size = strlen( data );
3148     setup_test( &info, internet_writefile, __LINE__ );
3149     ret = InternetWriteFile( req, data, size, &written );
3150     ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
3151     ok( written == size, "expected %u got %u\n", written, size );
3152
3153     WaitForSingleObject( info.wait, 10000 );
3154
3155     SetLastError( 0xdeadbeef );
3156     ret = HttpEndRequestA( req, (void *)data, 0x28, 0 );
3157     error = GetLastError();
3158     ok( !ret, "HttpEndRequestA succeeded\n" );
3159     ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
3160
3161     SetLastError( 0xdeadbeef );
3162     setup_test( &info, http_end_request, __LINE__ );
3163     ret = HttpEndRequestA( req, NULL, 0x28, 0 );
3164     error = GetLastError();
3165     ok( !ret, "HttpEndRequestA succeeded\n" );
3166     ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
3167
3168     WaitForSingleObject( info.wait, 10000 );
3169
3170     setup_test( &info, internet_close_handle, __LINE__ );
3171     InternetCloseHandle( req );
3172     InternetCloseHandle( con );
3173     InternetCloseHandle( ses );
3174
3175     WaitForSingleObject( info.wait, 10000 );
3176     Sleep(100);
3177     CloseHandle( info.wait );
3178 }
3179
3180 static HINTERNET closetest_session, closetest_req, closetest_conn;
3181 static BOOL closetest_closed;
3182
3183 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
3184      LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
3185 {
3186     DWORD len, type;
3187     BOOL res;
3188
3189     trace("closetest_callback %p: %d\n", hInternet, dwInternetStatus);
3190
3191     ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
3192        "Unexpected hInternet %p\n", hInternet);
3193     if(!closetest_closed)
3194         return;
3195
3196     len = sizeof(type);
3197     res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
3198     ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
3199        "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
3200        closetest_req, res, GetLastError());
3201 }
3202
3203 static void test_InternetCloseHandle(void)
3204 {
3205     DWORD len, flags;
3206     BOOL res;
3207
3208     closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
3209     ok(closetest_session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3210
3211     pInternetSetStatusCallbackA(closetest_session, closetest_callback);
3212
3213     closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
3214             NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3215     ok(closetest_conn != NULL,"InternetConnect failed with error %u\n", GetLastError());
3216
3217     closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
3218             INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3219
3220     res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
3221     ok(!res && (GetLastError() == ERROR_IO_PENDING),
3222        "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3223
3224     len = sizeof(flags);
3225     res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
3226     ok(res, "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %u\n", closetest_req, GetLastError());
3227
3228     res = InternetCloseHandle(closetest_session);
3229     ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
3230     closetest_closed = TRUE;
3231     trace("Closed session handle\n");
3232
3233     res = InternetCloseHandle(closetest_conn);
3234     ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %u\n",
3235        res, GetLastError());
3236
3237     res = InternetCloseHandle(closetest_req);
3238     ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %u\n",
3239        res, GetLastError());
3240
3241     len = sizeof(flags);
3242     res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
3243     ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
3244        "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
3245        closetest_req, res, GetLastError());
3246 }
3247
3248 static void init_status_tests(void)
3249 {
3250     memset(expect, 0, sizeof(expect));
3251     memset(optional, 0, sizeof(optional));
3252     memset(wine_allow, 0, sizeof(wine_allow));
3253     memset(notified, 0, sizeof(notified));
3254     memset(status_string, 0, sizeof(status_string));
3255
3256 #define STATUS_STRING(status) status_string[status] = #status
3257     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
3258     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
3259     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
3260     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
3261     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
3262     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
3263     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
3264     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
3265     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
3266     STATUS_STRING(INTERNET_STATUS_PREFETCH);
3267     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
3268     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
3269     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
3270     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
3271     STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
3272     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
3273     STATUS_STRING(INTERNET_STATUS_REDIRECT);
3274     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
3275     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
3276     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
3277     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
3278     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
3279     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
3280     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
3281     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
3282     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
3283 #undef STATUS_STRING
3284 }
3285
3286 START_TEST(http)
3287 {
3288     HMODULE hdll;
3289     hdll = GetModuleHandleA("wininet.dll");
3290
3291     if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
3292         win_skip("Too old IE (older than 6.0)\n");
3293         return;
3294     }
3295
3296     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
3297
3298     init_status_tests();
3299     test_InternetCloseHandle();
3300     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
3301     InternetReadFile_test(0, &test_data[0]);
3302     first_connection_to_test_url = TRUE;
3303     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
3304     InternetReadFile_test(0, &test_data[1]);
3305     InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
3306     test_open_url_async();
3307     test_async_HttpSendRequestEx();
3308     InternetOpenRequest_test();
3309     test_http_cache();
3310     InternetOpenUrlA_test();
3311     HttpHeaders_test();
3312     test_http_connection();
3313     test_secure_connection();
3314     test_user_agent_header();
3315     test_bogus_accept_types_array();
3316     InternetReadFile_chunked_test();
3317     HttpSendRequestEx_test();
3318     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
3319 }