wintrust: Use helper function for setting confidence in SoftpubCheckCert.
[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://www.winehq.org/site/about"
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][0] != 0 ? \
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][0] != 0 ? \
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] == (num), "expected status %d (%s) %d times, received %d times\n", \
88            status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
89            status_string[status] : "unknown", (num), notified[status]); \
90         CLEAR_NOTIFIED(status);                                         \
91     }while(0)
92
93 #define CHECK_NOTIFIED(status) \
94     CHECK_NOTIFIED2(status, 1)
95
96 #define CHECK_NOT_NOTIFIED(status) \
97     CHECK_NOTIFIED2(status, 0)
98
99 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
100 #define MAX_STATUS_NAME 50
101 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
102     wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
103 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
104
105 static HANDLE hCompleteEvent;
106
107 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
108 static BOOL (WINAPI *pInternetTimeFromSystemTimeA)(CONST SYSTEMTIME *,DWORD ,LPSTR ,DWORD);
109 static BOOL (WINAPI *pInternetTimeFromSystemTimeW)(CONST SYSTEMTIME *,DWORD ,LPWSTR ,DWORD);
110 static BOOL (WINAPI *pInternetTimeToSystemTimeA)(LPCSTR ,SYSTEMTIME *,DWORD);
111 static BOOL (WINAPI *pInternetTimeToSystemTimeW)(LPCWSTR ,SYSTEMTIME *,DWORD);
112
113
114 static VOID WINAPI callback(
115      HINTERNET hInternet,
116      DWORD_PTR dwContext,
117      DWORD dwInternetStatus,
118      LPVOID lpvStatusInformation,
119      DWORD dwStatusInformationLength
120 )
121 {
122     CHECK_EXPECT(dwInternetStatus);
123     switch (dwInternetStatus)
124     {
125         case INTERNET_STATUS_RESOLVING_NAME:
126             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
127                 GetCurrentThreadId(), hInternet, dwContext,
128                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
129             *(LPSTR)lpvStatusInformation = '\0';
130             break;
131         case INTERNET_STATUS_NAME_RESOLVED:
132             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
133                 GetCurrentThreadId(), hInternet, dwContext,
134                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
135             *(LPSTR)lpvStatusInformation = '\0';
136             break;
137         case INTERNET_STATUS_CONNECTING_TO_SERVER:
138             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
139                 GetCurrentThreadId(), hInternet, dwContext,
140                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
141             *(LPSTR)lpvStatusInformation = '\0';
142             break;
143         case INTERNET_STATUS_CONNECTED_TO_SERVER:
144             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
145                 GetCurrentThreadId(), hInternet, dwContext,
146                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
147             *(LPSTR)lpvStatusInformation = '\0';
148             break;
149         case INTERNET_STATUS_SENDING_REQUEST:
150             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
151                 GetCurrentThreadId(), hInternet, dwContext,
152                 lpvStatusInformation,dwStatusInformationLength);
153             break;
154         case INTERNET_STATUS_REQUEST_SENT:
155             ok(dwStatusInformationLength == sizeof(DWORD),
156                 "info length should be sizeof(DWORD) instead of %d\n",
157                 dwStatusInformationLength);
158             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
159                 GetCurrentThreadId(), hInternet, dwContext,
160                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
161             break;
162         case INTERNET_STATUS_RECEIVING_RESPONSE:
163             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
164                 GetCurrentThreadId(), hInternet, dwContext,
165                 lpvStatusInformation,dwStatusInformationLength);
166             break;
167         case INTERNET_STATUS_RESPONSE_RECEIVED:
168             ok(dwStatusInformationLength == sizeof(DWORD),
169                 "info length should be sizeof(DWORD) instead of %d\n",
170                 dwStatusInformationLength);
171             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
172                 GetCurrentThreadId(), hInternet, dwContext,
173                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
174             break;
175         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
176             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
177                 GetCurrentThreadId(), hInternet,dwContext,
178                 lpvStatusInformation,dwStatusInformationLength);
179             break;
180         case INTERNET_STATUS_PREFETCH:
181             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
182                 GetCurrentThreadId(), hInternet, dwContext,
183                 lpvStatusInformation,dwStatusInformationLength);
184             break;
185         case INTERNET_STATUS_CLOSING_CONNECTION:
186             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
187                 GetCurrentThreadId(), hInternet, dwContext,
188                 lpvStatusInformation,dwStatusInformationLength);
189             break;
190         case INTERNET_STATUS_CONNECTION_CLOSED:
191             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
192                 GetCurrentThreadId(), hInternet, dwContext,
193                 lpvStatusInformation,dwStatusInformationLength);
194             break;
195         case INTERNET_STATUS_HANDLE_CREATED:
196             ok(dwStatusInformationLength == sizeof(HINTERNET),
197                 "info length should be sizeof(HINTERNET) instead of %d\n",
198                 dwStatusInformationLength);
199             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
200                 GetCurrentThreadId(), hInternet, dwContext,
201                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
202             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
203             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
204             break;
205         case INTERNET_STATUS_HANDLE_CLOSING:
206             ok(dwStatusInformationLength == sizeof(HINTERNET),
207                 "info length should be sizeof(HINTERNET) instead of %d\n",
208                 dwStatusInformationLength);
209             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
210                 GetCurrentThreadId(), hInternet, dwContext,
211                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
212             break;
213         case INTERNET_STATUS_REQUEST_COMPLETE:
214         {
215             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
216             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
217                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
218                 dwStatusInformationLength);
219             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
220                 GetCurrentThreadId(), hInternet, dwContext,
221                 iar->dwResult,iar->dwError,dwStatusInformationLength);
222             SetEvent(hCompleteEvent);
223             break;
224         }
225         case INTERNET_STATUS_REDIRECT:
226             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
227                 GetCurrentThreadId(), hInternet, dwContext,
228                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
229             *(LPSTR)lpvStatusInformation = '\0';
230             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
231             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
232             break;
233         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
234             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
235                 GetCurrentThreadId(), hInternet, dwContext,
236                 lpvStatusInformation, dwStatusInformationLength);
237             break;
238         default:
239             trace("%04x:Callback %p 0x%lx %d %p %d\n",
240                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
241                 lpvStatusInformation, dwStatusInformationLength);
242     }
243 }
244
245 static void InternetReadFile_test(int flags)
246 {
247     BOOL res;
248     DWORD rc;
249     CHAR buffer[4000];
250     DWORD length;
251     DWORD out;
252     const char *types[2] = { "*", NULL };
253     HINTERNET hi, hic = 0, hor = 0;
254
255     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
256
257     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
258
259     trace("InternetOpenA <--\n");
260     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
261     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
262     trace("InternetOpenA -->\n");
263
264     if (hi == 0x0) goto abort;
265
266     pInternetSetStatusCallbackA(hi,&callback);
267
268     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
269
270     trace("InternetConnectA <--\n");
271     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
272                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
273     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
274     trace("InternetConnectA -->\n");
275
276     if (hic == 0x0) goto abort;
277
278     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
279     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
280
281     trace("HttpOpenRequestA <--\n");
282     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
283                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
284                            0xdeadbead);
285     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
286         /*
287          * If the internet name can't be resolved we are probably behind
288          * a firewall or in some other way not directly connected to the
289          * Internet. Not enough reason to fail the test. Just ignore and
290          * abort.
291          */
292     } else  {
293         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
294     }
295     trace("HttpOpenRequestA -->\n");
296
297     if (hor == 0x0) goto abort;
298
299     length = sizeof(buffer);
300     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
301     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
302     ok(!strcmp(buffer, "http://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
303
304     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
305     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
306     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
307     if (first_connection_to_test_url)
308     {
309         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
310         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
311         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
312         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
313     }
314     else
315     {
316         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
317         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
318     }
319     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
320     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
321     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
322     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
323     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
324     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
325     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
326     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
327     SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
328     SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
329     SET_EXPECT(INTERNET_STATUS_REDIRECT);
330     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
331     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
332     if (flags & INTERNET_FLAG_ASYNC)
333         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
334     else
335         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
336
337     trace("HttpSendRequestA -->\n");
338     SetLastError(0xdeadbeef);
339     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
340     if (flags & INTERNET_FLAG_ASYNC)
341         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
342             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
343     else
344         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
345            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
346     trace("HttpSendRequestA <--\n");
347
348     if (flags & INTERNET_FLAG_ASYNC)
349         WaitForSingleObject(hCompleteEvent, INFINITE);
350
351     todo_wine if (first_connection_to_test_url)
352     {
353         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
354         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
355     }
356     else
357     {
358         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
359         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
360     }
361     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
362     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
363     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
364     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
365     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
366     if (flags & INTERNET_FLAG_ASYNC)
367         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
368     else
369         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
370     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
371     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
372     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
373
374     length = 4;
375     rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
376     trace("Option 0x17 -> %i  %i\n",rc,out);
377
378     length = 100;
379     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
380     trace("Option 0x22 -> %i  %s\n",rc,buffer);
381
382     length = 4000;
383     rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
384     buffer[length]=0;
385     trace("Option 0x16 -> %i  %s\n",rc,buffer);
386
387     length = sizeof(buffer);
388     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
389     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
390     ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
391
392     length = 16;
393     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
394     trace("Option 0x5 -> %i  %s  (%u)\n",rc,buffer,GetLastError());
395
396     length = 100;
397     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
398     buffer[length]=0;
399     trace("Option 0x1 -> %i  %s\n",rc,buffer);
400
401     SetLastError(0xdeadbeef);
402     rc = InternetReadFile(NULL, buffer, 100, &length);
403     ok(!rc, "InternetReadFile should have failed\n");
404     ok(GetLastError() == ERROR_INVALID_HANDLE,
405         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
406         GetLastError());
407
408     length = 100;
409     trace("Entering Query loop\n");
410
411     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
412     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
413     while (TRUE)
414     {
415         if (flags & INTERNET_FLAG_ASYNC)
416             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
417         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
418         ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
419         ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
420            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
421         if (flags & INTERNET_FLAG_ASYNC)
422         {
423             if (rc != 0)
424             {
425                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
426             }
427             else if (GetLastError() == ERROR_IO_PENDING)
428             {
429                 WaitForSingleObject(hCompleteEvent, INFINITE);
430                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
431                 continue;
432             }
433         }
434         if (length)
435         {
436             char *buffer;
437             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
438
439             rc = InternetReadFile(hor,buffer,length,&length);
440
441             buffer[length]=0;
442
443             trace("ReadFile -> %i %i\n",rc,length);
444
445             HeapFree(GetProcessHeap(),0,buffer);
446         }
447         if (length == 0)
448             break;
449     }
450     /* WinXP does not send, but Win98 does */
451     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
452     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
453 abort:
454     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
455     if (hor != 0x0) {
456         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
457         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
458         SetLastError(0xdeadbeef);
459         rc = InternetCloseHandle(hor);
460         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
461         SetLastError(0xdeadbeef);
462         rc = InternetCloseHandle(hor);
463         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
464         ok (GetLastError() == ERROR_INVALID_HANDLE,
465             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
466             GetLastError());
467     }
468     /* We intentionally do not close the handle opened by InternetConnectA as this
469      * tickles bug #9479: native closes child internet handles when the parent handles
470      * are closed. This is verified below by checking that the number of
471      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
472     if (hi != 0x0) {
473       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
474       rc = InternetCloseHandle(hi);
475       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
476       if (flags & INTERNET_FLAG_ASYNC)
477           Sleep(100);
478     }
479     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
480     if (hor != 0x0 && (flags & INTERNET_FLAG_ASYNC)) todo_wine
481     {
482         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
483         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
484     }
485     else
486     {
487         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
488         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
489     }
490     CloseHandle(hCompleteEvent);
491     first_connection_to_test_url = FALSE;
492 }
493
494 static void InternetReadFileExA_test(int flags)
495 {
496     DWORD rc;
497     DWORD length;
498     const char *types[2] = { "*", NULL };
499     HINTERNET hi, hic = 0, hor = 0;
500     INTERNET_BUFFERS inetbuffers;
501
502     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
503
504     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
505
506     trace("InternetOpenA <--\n");
507     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
508     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
509     trace("InternetOpenA -->\n");
510
511     if (hi == 0x0) goto abort;
512
513     pInternetSetStatusCallbackA(hi,&callback);
514
515     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
516
517     trace("InternetConnectA <--\n");
518     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
519                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
520     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
521     trace("InternetConnectA -->\n");
522
523     if (hic == 0x0) goto abort;
524
525     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
526     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
527
528     trace("HttpOpenRequestA <--\n");
529     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
530                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
531                            0xdeadbead);
532     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
533         /*
534          * If the internet name can't be resolved we are probably behind
535          * a firewall or in some other way not directly connected to the
536          * Internet. Not enough reason to fail the test. Just ignore and
537          * abort.
538          */
539     } else  {
540         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
541     }
542     trace("HttpOpenRequestA -->\n");
543
544     if (hor == 0x0) goto abort;
545
546     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
547     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
548     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
549     if (first_connection_to_test_url)
550     {
551         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
552         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
553         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
554         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
555     }
556     else
557     {
558         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
559         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
560     }
561     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
562     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
563     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
564     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
565     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
566     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
567     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
568     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
569     SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
570     SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
571     SET_EXPECT(INTERNET_STATUS_REDIRECT);
572     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
573     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
574     if (flags & INTERNET_FLAG_ASYNC)
575         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
576     else
577         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
578
579     trace("HttpSendRequestA -->\n");
580     SetLastError(0xdeadbeef);
581     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
582     if (flags & INTERNET_FLAG_ASYNC)
583         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
584             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
585     else
586         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
587            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
588     trace("HttpSendRequestA <--\n");
589
590     if (!rc && (GetLastError() == ERROR_IO_PENDING))
591         WaitForSingleObject(hCompleteEvent, INFINITE);
592
593     if (first_connection_to_test_url)
594     {
595         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
596         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
597     }
598     else todo_wine
599     {
600         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
601         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
602     }
603     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
604     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
605     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
606     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
607     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
608     if (flags & INTERNET_FLAG_ASYNC)
609         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
610     else
611         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
612     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
613     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
614     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
615
616     /* tests invalid dwStructSize */
617     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
618     inetbuffers.lpcszHeader = NULL;
619     inetbuffers.dwHeadersLength = 0;
620     inetbuffers.dwBufferLength = 10;
621     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
622     inetbuffers.dwOffsetHigh = 1234;
623     inetbuffers.dwOffsetLow = 5678;
624     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
625     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
626         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
627         rc ? "TRUE" : "FALSE", GetLastError());
628     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
629
630     /* tests to see whether lpcszHeader is used - it isn't */
631     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
632     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
633     inetbuffers.dwHeadersLength = 255;
634     inetbuffers.dwBufferLength = 0;
635     inetbuffers.lpvBuffer = NULL;
636     inetbuffers.dwOffsetHigh = 1234;
637     inetbuffers.dwOffsetLow = 5678;
638     SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
639     SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
640     SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
641     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
642     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
643     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
644         trace("read %i bytes\n", inetbuffers.dwBufferLength);
645     todo_wine
646     {
647         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
648         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
649     }
650
651     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
652     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
653         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
654         rc ? "TRUE" : "FALSE", GetLastError());
655
656     length = 0;
657     trace("Entering Query loop\n");
658
659     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
660     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
661     while (TRUE)
662     {
663         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
664         inetbuffers.dwBufferLength = 1024;
665         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
666         inetbuffers.dwOffsetHigh = 1234;
667         inetbuffers.dwOffsetLow = 5678;
668
669         SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
670         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
671         SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
672         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
673         if (!rc)
674         {
675             if (GetLastError() == ERROR_IO_PENDING)
676             {
677                 trace("InternetReadFileEx -> PENDING\n");
678                 ok(flags & INTERNET_FLAG_ASYNC,
679                    "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
680                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
681                 WaitForSingleObject(hCompleteEvent, INFINITE);
682                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
683                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
684             }
685             else
686             {
687                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
688                 break;
689             }
690         }
691         else
692         {
693             trace("InternetReadFileEx -> SUCCEEDED\n");
694             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
695             if (inetbuffers.dwBufferLength)
696             {
697                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
698                 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
699             }
700             else
701             {
702                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
703                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
704                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
705             }
706         }
707
708         trace("read %i bytes\n", inetbuffers.dwBufferLength);
709         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
710
711         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
712             "InternetReadFileEx sets offsets to 0x%x%08x\n",
713             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
714
715         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
716
717         if (!inetbuffers.dwBufferLength)
718             break;
719
720         length += inetbuffers.dwBufferLength;
721     }
722     todo_wine ok(length > 0, "failed to read any of the document\n");
723     trace("Finished. Read %d bytes\n", length);
724
725     /* WinXP does not send, but Win98 does */
726     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
727     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
728 abort:
729     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
730     if (hor) {
731         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
732         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
733         rc = InternetCloseHandle(hor);
734         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
735         rc = InternetCloseHandle(hor);
736         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
737     }
738     if (hic) {
739         rc = InternetCloseHandle(hic);
740         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
741     }
742     if (hi) {
743       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
744       rc = InternetCloseHandle(hi);
745       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
746       if (flags & INTERNET_FLAG_ASYNC)
747           Sleep(100);
748       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
749     }
750     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
751     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
752     CloseHandle(hCompleteEvent);
753     first_connection_to_test_url = FALSE;
754 }
755
756 static void InternetOpenUrlA_test(void)
757 {
758   HINTERNET myhinternet, myhttp;
759   char buffer[0x400];
760   DWORD size, readbytes, totalbytes=0;
761   BOOL ret;
762   
763   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
764   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
765   size = 0x400;
766   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
767   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
768
769   SetLastError(0);
770   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
771                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
772   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
773     return; /* WinXP returns this when not connected to the net */
774   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
775   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
776   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
777   totalbytes += readbytes;
778   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
779     totalbytes += readbytes;
780   trace("read 0x%08x bytes\n",totalbytes);
781
782   InternetCloseHandle(myhttp);
783   InternetCloseHandle(myhinternet);
784 }
785
786 static void InternetTimeFromSystemTimeA_test(void)
787 {
788     BOOL ret;
789     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
790     char string[INTERNET_RFC1123_BUFSIZE];
791     static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
792
793     ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
794     ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
795
796     ok( !memcmp( string, expect, sizeof(expect) ),
797         "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
798 }
799
800 static void InternetTimeFromSystemTimeW_test(void)
801 {
802     BOOL ret;
803     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
804     WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
805     static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
806                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
807
808     ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
809     ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
810
811     ok( !memcmp( string, expect, sizeof(expect) ),
812         "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
813 }
814
815 static void InternetTimeToSystemTimeA_test(void)
816 {
817     BOOL ret;
818     SYSTEMTIME time;
819     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
820     static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
821     static const char string2[] = " fri 7 jan 2005 12 06 35";
822
823     ret = pInternetTimeToSystemTimeA( string, &time, 0 );
824     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
825     ok( !memcmp( &time, &expect, sizeof(expect) ),
826         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
827
828     ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
829     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
830     ok( !memcmp( &time, &expect, sizeof(expect) ),
831         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
832 }
833
834 static void InternetTimeToSystemTimeW_test(void)
835 {
836     BOOL ret;
837     SYSTEMTIME time;
838     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
839     static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
840                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
841     static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
842                                      '1','2',' ','0','6',' ','3','5',0 };
843     static const WCHAR string3[] = { 'F','r',0 };
844
845     ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
846     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
847
848     ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
849     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
850
851     ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
852     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
853
854     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
855     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
856
857     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
858     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
859     ok( !memcmp( &time, &expect, sizeof(expect) ),
860         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
861
862     ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
863     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
864     ok( !memcmp( &time, &expect, sizeof(expect) ),
865         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
866
867     ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
868     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
869 }
870
871 static void HttpSendRequestEx_test(void)
872 {
873     HINTERNET hSession;
874     HINTERNET hConnect;
875     HINTERNET hRequest;
876
877     INTERNET_BUFFERS BufferIn;
878     DWORD dwBytesWritten;
879     DWORD dwBytesRead;
880     CHAR szBuffer[256];
881     int i;
882     BOOL ret;
883
884     static char szPostData[] = "mode=Test";
885     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
886
887     hSession = InternetOpen("Wine Regression Test",
888             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
889     ok( hSession != NULL ,"Unable to open Internet session\n");
890     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
891             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
892             0);
893     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
894     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
895             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
896     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
897     {
898         skip( "Network unreachable, skipping test\n" );
899         goto done;
900     }
901     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
902
903
904     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
905     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
906     BufferIn.lpcszHeader = szContentType;
907     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
908     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
909     BufferIn.lpvBuffer = szPostData;
910     BufferIn.dwBufferLength = 3;
911     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
912     BufferIn.dwOffsetLow = 0;
913     BufferIn.dwOffsetHigh = 0;
914
915     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
916     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
917
918     for (i = 3; szPostData[i]; i++)
919         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
920                 "InternetWriteFile failed\n");
921
922     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
923
924     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
925             "Unable to read response\n");
926     szBuffer[dwBytesRead] = 0;
927
928     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
929     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
930
931     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
932 done:
933     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
934     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
935 }
936
937 static void InternetOpenRequest_test(void)
938 {
939     HINTERNET session, connect, request;
940     static const char *types[] = { "*", "", NULL };
941     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
942     static const WCHAR *typesW[] = { any, empty, NULL };
943     BOOL ret;
944
945     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
946     ok(session != NULL ,"Unable to open Internet session\n");
947
948     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
949                               INTERNET_SERVICE_HTTP, 0, 0);
950     ok(connect == NULL, "InternetConnectA should have failed\n");
951     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
952
953     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
954                               INTERNET_SERVICE_HTTP, 0, 0);
955     ok(connect == NULL, "InternetConnectA should have failed\n");
956     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
957
958     connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
959                               INTERNET_SERVICE_HTTP, 0, 0);
960     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
961
962     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
963     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
964     {
965         skip( "Network unreachable, skipping test\n" );
966         goto done;
967     }
968     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
969
970     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
971     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
972     ok(InternetCloseHandle(request), "Close request handle failed\n");
973
974     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
975     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
976
977     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
978     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
979     ok(InternetCloseHandle(request), "Close request handle failed\n");
980
981 done:
982     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
983     ok(InternetCloseHandle(session), "Close session handle failed\n");
984 }
985
986 static void test_http_cache(void)
987 {
988     HINTERNET session, connect, request;
989     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
990     DWORD size, file_size;
991     BYTE buf[100];
992     HANDLE file;
993     BOOL ret;
994
995     static const char *types[] = { "*", "", NULL };
996
997     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
998     ok(session != NULL ,"Unable to open Internet session\n");
999
1000     connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1001                               INTERNET_SERVICE_HTTP, 0, 0);
1002     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
1003
1004     request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1005     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1006     {
1007         skip( "Network unreachable, skipping test\n" );
1008
1009         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1010         ok(InternetCloseHandle(session), "Close session handle failed\n");
1011
1012         return;
1013     }
1014     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1015
1016     size = sizeof(url);
1017     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1018     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
1019     ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
1020
1021     size = sizeof(file_name);
1022     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1023     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1024     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1025     ok(!size, "size = %d\n", size);
1026
1027     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1028     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1029
1030     size = sizeof(file_name);
1031     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1032     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1033
1034     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1035                       FILE_ATTRIBUTE_NORMAL, NULL);
1036     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1037     file_size = GetFileSize(file, NULL);
1038     ok(file_size == 0, "file size=%d\n", file_size);
1039
1040     ret = InternetReadFile(request, buf, sizeof(buf), &size);
1041     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1042     ok(size == sizeof(buf), "size=%d\n", size);
1043
1044     file_size = GetFileSize(file, NULL);
1045     ok(file_size == sizeof(buf), "file size=%d\n", file_size);
1046     CloseHandle(file);
1047
1048     ok(InternetCloseHandle(request), "Close request handle failed\n");
1049
1050     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1051                       FILE_ATTRIBUTE_NORMAL, NULL);
1052     ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1053     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1054
1055     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1056     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1057
1058     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1059     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1060     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1061     ok(!size, "size = %d\n", size);
1062
1063     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1064     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1065
1066     size = sizeof(file_name);
1067     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1068     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1069     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1070     ok(!size, "size = %d\n", size);
1071
1072     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1073                       FILE_ATTRIBUTE_NORMAL, NULL);
1074     ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1075     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1076
1077     ok(InternetCloseHandle(request), "Close request handle failed\n");
1078
1079     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1080     ok(InternetCloseHandle(session), "Close session handle failed\n");
1081 }
1082
1083 static void HttpHeaders_test(void)
1084 {
1085     HINTERNET hSession;
1086     HINTERNET hConnect;
1087     HINTERNET hRequest;
1088     CHAR      buffer[256];
1089     WCHAR     wbuffer[256];
1090     DWORD     len = 256;
1091     DWORD     oldlen;
1092     DWORD     index = 0;
1093
1094     hSession = InternetOpen("Wine Regression Test",
1095             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1096     ok( hSession != NULL ,"Unable to open Internet session\n");
1097     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1098             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1099             0);
1100     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1101     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1102             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1103     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1104     {
1105         skip( "Network unreachable, skipping test\n" );
1106         goto done;
1107     }
1108     ok( hRequest != NULL, "Failed to open request handle\n");
1109
1110     index = 0;
1111     len = sizeof(buffer);
1112     strcpy(buffer,"Warning");
1113     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1114                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1115     
1116     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1117             "Failed to add new header\n");
1118
1119     index = 0;
1120     len = sizeof(buffer);
1121     strcpy(buffer,"Warning");
1122     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1123                 buffer,&len,&index),"Unable to query header\n");
1124     ok(index == 1, "Index was not incremented\n");
1125     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1126     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1127     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1128     len = sizeof(buffer);
1129     strcpy(buffer,"Warning");
1130     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1131                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1132
1133     index = 0;
1134     len = 5; /* could store the string but not the NULL terminator */
1135     strcpy(buffer,"Warning");
1136     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1137                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1138     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1139     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1140
1141     /* a call with NULL will fail but will return the length */
1142     index = 0;
1143     len = sizeof(buffer);
1144     SetLastError(0xdeadbeef);
1145     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1146                 NULL,&len,&index) == FALSE,"Query worked\n");
1147     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1148     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1149     ok(index == 0, "Index was incremented\n");
1150
1151     /* even for a len that is too small */
1152     index = 0;
1153     len = 15;
1154     SetLastError(0xdeadbeef);
1155     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1156                 NULL,&len,&index) == FALSE,"Query worked\n");
1157     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1158     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1159     ok(index == 0, "Index was incremented\n");
1160
1161     index = 0;
1162     len = 0;
1163     SetLastError(0xdeadbeef);
1164     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1165                 NULL,&len,&index) == FALSE,"Query worked\n");
1166     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1167     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1168     ok(index == 0, "Index was incremented\n");
1169     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1170
1171
1172     /* a working query */
1173     index = 0;
1174     len = sizeof(buffer);
1175     memset(buffer, 'x', sizeof(buffer));
1176     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1177                 buffer,&len,&index),"Unable to query header\n");
1178     ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1179     ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1180     ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1181     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1182     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1183     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1184     ok(index == 0, "Index was incremented\n");
1185
1186     /* Like above two tests, but for W version */
1187
1188     index = 0;
1189     len = 0;
1190     SetLastError(0xdeadbeef);
1191     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1192                 NULL,&len,&index) == FALSE,"Query worked\n");
1193     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1194     ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1195     ok(index == 0, "Index was incremented\n");
1196     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1197
1198     /* a working query */
1199     index = 0;
1200     len = sizeof(wbuffer);
1201     memset(wbuffer, 'x', sizeof(wbuffer));
1202     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1203                 wbuffer,&len,&index),"Unable to query header\n");
1204     ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1205     ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1206     ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1207     ok(index == 0, "Index was incremented\n");
1208
1209     /* end of W version tests */
1210
1211     /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1212     index = 0;
1213     len = sizeof(buffer);
1214     memset(buffer, 'x', sizeof(buffer));
1215     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1216                 buffer,&len,&index) == TRUE,"Query failed\n");
1217     ok(len == 2, "Expected 2, got %d\n", len);
1218     ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1219     ok(index == 0, "Index was incremented\n");
1220
1221     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1222             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1223
1224     index = 0;
1225     len = sizeof(buffer);
1226     strcpy(buffer,"Warning");
1227     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1228                 buffer,&len,&index),"Unable to query header\n");
1229     ok(index == 1, "Index was not incremented\n");
1230     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1231     len = sizeof(buffer);
1232     strcpy(buffer,"Warning");
1233     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1234                 buffer,&len,&index),"Failed to get second header\n");
1235     ok(index == 2, "Index was not incremented\n");
1236     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1237     len = sizeof(buffer);
1238     strcpy(buffer,"Warning");
1239     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1240                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1241
1242     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1243
1244     index = 0;
1245     len = sizeof(buffer);
1246     strcpy(buffer,"Warning");
1247     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1248                 buffer,&len,&index),"Unable to query header\n");
1249     ok(index == 1, "Index was not incremented\n");
1250     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1251     len = sizeof(buffer);
1252     strcpy(buffer,"Warning");
1253     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1254                 buffer,&len,&index),"Failed to get second header\n");
1255     ok(index == 2, "Index was not incremented\n");
1256     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1257     len = sizeof(buffer);
1258     strcpy(buffer,"Warning");
1259     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1260                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1261     
1262     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1263
1264     index = 0;
1265     len = sizeof(buffer);
1266     strcpy(buffer,"Warning");
1267     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1268                 buffer,&len,&index),"Unable to query header\n");
1269     ok(index == 1, "Index was not incremented\n");
1270     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1271     len = sizeof(buffer);
1272     strcpy(buffer,"Warning");
1273     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1274                 buffer,&len,&index),"Failed to get second header\n");
1275     ok(index == 2, "Index was not incremented\n");
1276     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1277     len = sizeof(buffer);
1278     strcpy(buffer,"Warning");
1279     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1280                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1281
1282     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1283
1284     index = 0;
1285     len = sizeof(buffer);
1286     strcpy(buffer,"Warning");
1287     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1288                 buffer,&len,&index),"Unable to query header\n");
1289     ok(index == 1, "Index was not incremented\n");
1290     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1291     len = sizeof(buffer);
1292     strcpy(buffer,"Warning");
1293     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1294     ok(index == 2, "Index was not incremented\n");
1295     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1296     len = sizeof(buffer);
1297     strcpy(buffer,"Warning");
1298     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1299
1300     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1301
1302     index = 0;
1303     len = sizeof(buffer);
1304     strcpy(buffer,"Warning");
1305     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1306     ok(index == 1, "Index was not incremented\n");
1307     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1308     len = sizeof(buffer);
1309     strcpy(buffer,"Warning");
1310     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1311     ok(index == 2, "Index was not incremented\n");
1312     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1313     len = sizeof(buffer);
1314     strcpy(buffer,"Warning");
1315     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1316
1317     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1318
1319     index = 0;
1320     len = sizeof(buffer);
1321     strcpy(buffer,"Warning");
1322     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1323     ok(index == 1, "Index was not incremented\n");
1324     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1325     len = sizeof(buffer);
1326     strcpy(buffer,"Warning");
1327     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1328     ok(index == 2, "Index was not incremented\n");
1329     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1330     len = sizeof(buffer);
1331     strcpy(buffer,"Warning");
1332     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1333
1334     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");
1335
1336     index = 0;
1337     len = sizeof(buffer);
1338     strcpy(buffer,"Warning");
1339     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1340     ok(index == 1, "Index was not incremented\n");
1341     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1342     len = sizeof(buffer);
1343     strcpy(buffer,"Warning");
1344     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1345     ok(index == 2, "Index was not incremented\n");
1346     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1347     len = sizeof(buffer);
1348     strcpy(buffer,"Warning");
1349     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1350
1351     
1352     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1353 done:
1354     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1355     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1356 }
1357
1358 static const char contmsg[] =
1359 "HTTP/1.1 100 Continue\r\n";
1360
1361 static const char okmsg[] =
1362 "HTTP/1.1 200 OK\r\n"
1363 "Server: winetest\r\n"
1364 "\r\n";
1365
1366 static const char notokmsg[] =
1367 "HTTP/1.1 400 Bad Request\r\n"
1368 "Server: winetest\r\n"
1369 "\r\n";
1370
1371 static const char noauthmsg[] =
1372 "HTTP/1.1 401 Unauthorized\r\n"
1373 "Server: winetest\r\n"
1374 "\r\n";
1375
1376 static const char proxymsg[] =
1377 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1378 "Server: winetest\r\n"
1379 "Proxy-Connection: close\r\n"
1380 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1381 "\r\n";
1382
1383 static const char page1[] =
1384 "<HTML>\r\n"
1385 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1386 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1387 "</HTML>\r\n\r\n";
1388
1389 struct server_info {
1390     HANDLE hEvent;
1391     int port;
1392 };
1393
1394 static DWORD CALLBACK server_thread(LPVOID param)
1395 {
1396     struct server_info *si = param;
1397     int r, c, i, on;
1398     SOCKET s;
1399     struct sockaddr_in sa;
1400     char buffer[0x100];
1401     WSADATA wsaData;
1402     int last_request = 0;
1403     char host_header[22];
1404     static int test_b = 0;
1405
1406     WSAStartup(MAKEWORD(1,1), &wsaData);
1407
1408     s = socket(AF_INET, SOCK_STREAM, 0);
1409     if (s == INVALID_SOCKET)
1410         return 1;
1411
1412     on = 1;
1413     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1414
1415     memset(&sa, 0, sizeof sa);
1416     sa.sin_family = AF_INET;
1417     sa.sin_port = htons(si->port);
1418     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1419
1420     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1421     if (r<0)
1422         return 1;
1423
1424     listen(s, 0);
1425
1426     SetEvent(si->hEvent);
1427
1428     sprintf(host_header, "Host: localhost:%d", si->port);
1429
1430     do
1431     {
1432         c = accept(s, NULL, NULL);
1433
1434         memset(buffer, 0, sizeof buffer);
1435         for(i=0; i<(sizeof buffer-1); i++)
1436         {
1437             r = recv(c, &buffer[i], 1, 0);
1438             if (r != 1)
1439                 break;
1440             if (i<4) continue;
1441             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1442                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1443                 break;
1444         }
1445         if (strstr(buffer, "GET /test1"))
1446         {
1447             if (!strstr(buffer, "Content-Length: 0"))
1448             {
1449                 send(c, okmsg, sizeof okmsg-1, 0);
1450                 send(c, page1, sizeof page1-1, 0);
1451             }
1452             else
1453                 send(c, notokmsg, sizeof notokmsg-1, 0);
1454         }
1455         if (strstr(buffer, "/test2"))
1456         {
1457             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1458             {
1459                 send(c, okmsg, sizeof okmsg-1, 0);
1460                 send(c, page1, sizeof page1-1, 0);
1461             }
1462             else
1463                 send(c, proxymsg, sizeof proxymsg-1, 0);
1464         }
1465         if (strstr(buffer, "/test3"))
1466         {
1467             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1468                 send(c, okmsg, sizeof okmsg-1, 0);
1469             else
1470                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1471         }
1472         if (strstr(buffer, "/test4"))
1473         {
1474             if (strstr(buffer, "Connection: Close"))
1475                 send(c, okmsg, sizeof okmsg-1, 0);
1476             else
1477                 send(c, notokmsg, sizeof notokmsg-1, 0);
1478         }
1479         if (strstr(buffer, "POST /test5"))
1480         {
1481             if (strstr(buffer, "Content-Length: 0"))
1482             {
1483                 send(c, okmsg, sizeof okmsg-1, 0);
1484                 send(c, page1, sizeof page1-1, 0);
1485             }
1486             else
1487                 send(c, notokmsg, sizeof notokmsg-1, 0);
1488         }
1489         if (strstr(buffer, "GET /test6"))
1490         {
1491             send(c, contmsg, sizeof contmsg-1, 0);
1492             send(c, contmsg, sizeof contmsg-1, 0);
1493             send(c, okmsg, sizeof okmsg-1, 0);
1494             send(c, page1, sizeof page1-1, 0);
1495         }
1496         if (strstr(buffer, "POST /test7"))
1497         {
1498             if (strstr(buffer, "Content-Length: 100"))
1499             {
1500                 send(c, okmsg, sizeof okmsg-1, 0);
1501                 send(c, page1, sizeof page1-1, 0);
1502             }
1503             else
1504                 send(c, notokmsg, sizeof notokmsg-1, 0);
1505         }
1506         if (strstr(buffer, "/test8"))
1507         {
1508             if (!strstr(buffer, "Connection: Close") &&
1509                  strstr(buffer, "Connection: Keep-Alive") &&
1510                 !strstr(buffer, "Cache-Control: no-cache") &&
1511                 !strstr(buffer, "Pragma: no-cache") &&
1512                  strstr(buffer, host_header))
1513                 send(c, okmsg, sizeof okmsg-1, 0);
1514             else
1515                 send(c, notokmsg, sizeof notokmsg-1, 0);
1516         }
1517         if (strstr(buffer, "/test9"))
1518         {
1519             if (!strstr(buffer, "Connection: Close") &&
1520                 !strstr(buffer, "Connection: Keep-Alive") &&
1521                 !strstr(buffer, "Cache-Control: no-cache") &&
1522                 !strstr(buffer, "Pragma: no-cache") &&
1523                  strstr(buffer, host_header))
1524                 send(c, okmsg, sizeof okmsg-1, 0);
1525             else
1526                 send(c, notokmsg, sizeof notokmsg-1, 0);
1527         }
1528         if (strstr(buffer, "/testA"))
1529         {
1530             if (!strstr(buffer, "Connection: Close") &&
1531                 !strstr(buffer, "Connection: Keep-Alive") &&
1532                 (strstr(buffer, "Cache-Control: no-cache") ||
1533                  strstr(buffer, "Pragma: no-cache")) &&
1534                  strstr(buffer, host_header))
1535                 send(c, okmsg, sizeof okmsg-1, 0);
1536             else
1537                 send(c, notokmsg, sizeof notokmsg-1, 0);
1538         }
1539         if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1540         {
1541             test_b = 1;
1542             send(c, okmsg, sizeof okmsg-1, 0);
1543             recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1544             send(c, okmsg, sizeof okmsg-1, 0);
1545         }
1546         if (strstr(buffer, "/testC"))
1547         {
1548             if (strstr(buffer, "Cookie: cookie=biscuit"))
1549                 send(c, okmsg, sizeof okmsg-1, 0);
1550             else
1551                 send(c, notokmsg, sizeof notokmsg-1, 0);
1552         }
1553         if (strstr(buffer, "GET /quit"))
1554         {
1555             send(c, okmsg, sizeof okmsg-1, 0);
1556             send(c, page1, sizeof page1-1, 0);
1557             last_request = 1;
1558         }
1559
1560         shutdown(c, 2);
1561         closesocket(c);
1562     } while (!last_request);
1563
1564     closesocket(s);
1565
1566     return 0;
1567 }
1568
1569 static void test_basic_request(int port, const char *verb, const char *url)
1570 {
1571     HINTERNET hi, hc, hr;
1572     DWORD r, count;
1573     char buffer[0x100];
1574
1575     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1576     ok(hi != NULL, "open failed\n");
1577
1578     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1579     ok(hc != NULL, "connect failed\n");
1580
1581     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1582     ok(hr != NULL, "HttpOpenRequest failed\n");
1583
1584     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1585     ok(r, "HttpSendRequest failed\n");
1586
1587     count = 0;
1588     memset(buffer, 0, sizeof buffer);
1589     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1590     ok(r, "InternetReadFile failed\n");
1591     ok(count == sizeof page1 - 1, "count was wrong\n");
1592     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1593
1594     InternetCloseHandle(hr);
1595     InternetCloseHandle(hc);
1596     InternetCloseHandle(hi);
1597 }
1598
1599 static void test_proxy_indirect(int port)
1600 {
1601     HINTERNET hi, hc, hr;
1602     DWORD r, sz, val;
1603     char buffer[0x40];
1604
1605     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1606     ok(hi != NULL, "open failed\n");
1607
1608     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1609     ok(hc != NULL, "connect failed\n");
1610
1611     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1612     ok(hr != NULL, "HttpOpenRequest failed\n");
1613
1614     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1615     ok(r, "HttpSendRequest failed\n");
1616
1617     sz = sizeof buffer;
1618     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1619     ok(r, "HttpQueryInfo failed\n");
1620     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1621
1622     sz = sizeof buffer;
1623     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1624     ok(r, "HttpQueryInfo failed\n");
1625     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1626
1627     sz = sizeof val;
1628     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1629     ok(r, "HttpQueryInfo failed\n");
1630     ok(val == 407, "proxy code wrong\n");
1631
1632     sz = sizeof buffer;
1633     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1634     ok(r, "HttpQueryInfo failed\n");
1635     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1636
1637     sz = sizeof buffer;
1638     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1639     ok(r, "HttpQueryInfo failed\n");
1640     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1641
1642     sz = sizeof buffer;
1643     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1644     ok(r, "HttpQueryInfo failed\n");
1645     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1646
1647     sz = sizeof buffer;
1648     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1649     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1650     ok(r == FALSE, "HttpQueryInfo failed\n");
1651
1652     InternetCloseHandle(hr);
1653     InternetCloseHandle(hc);
1654     InternetCloseHandle(hi);
1655 }
1656
1657 static void test_proxy_direct(int port)
1658 {
1659     HINTERNET hi, hc, hr;
1660     DWORD r, sz;
1661     char buffer[0x40];
1662     static CHAR username[] = "mike",
1663                 password[] = "1101";
1664
1665     sprintf(buffer, "localhost:%d\n", port);
1666     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1667     ok(hi != NULL, "open failed\n");
1668
1669     /* try connect without authorization */
1670     hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1671     ok(hc != NULL, "connect failed\n");
1672
1673     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1674     ok(hr != NULL, "HttpOpenRequest failed\n");
1675
1676     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1677     ok(r, "HttpSendRequest failed\n");
1678
1679     sz = sizeof buffer;
1680     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1681     ok(r, "HttpQueryInfo failed\n");
1682     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1683
1684
1685     /* set the user + password then try again */
1686     todo_wine {
1687     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1688     ok(r, "failed to set user\n");
1689
1690     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1691     ok(r, "failed to set password\n");
1692     }
1693
1694     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1695     ok(r, "HttpSendRequest failed\n");
1696     sz = sizeof buffer;
1697     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1698     ok(r, "HttpQueryInfo failed\n");
1699     todo_wine {
1700     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1701     }
1702
1703
1704     InternetCloseHandle(hr);
1705     InternetCloseHandle(hc);
1706     InternetCloseHandle(hi);
1707 }
1708
1709 static void test_header_handling_order(int port)
1710 {
1711     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1712     static char connection[]    = "Connection: Close";
1713
1714     static const char *types[2] = { "*", NULL };
1715     HINTERNET session, connect, request;
1716     DWORD size, status;
1717     BOOL ret;
1718
1719     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1720     ok(session != NULL, "InternetOpen failed\n");
1721
1722     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1723     ok(connect != NULL, "InternetConnect failed\n");
1724
1725     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1726     ok(request != NULL, "HttpOpenRequest failed\n");
1727
1728     ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1729     ok(ret, "HttpAddRequestHeaders failed\n");
1730
1731     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1732     ok(ret, "HttpSendRequest failed\n");
1733
1734     status = 0;
1735     size = sizeof(status);
1736     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1737     ok(ret, "HttpQueryInfo failed\n");
1738     ok(status == 200, "request failed with status %u\n", status);
1739
1740     InternetCloseHandle(request);
1741
1742     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1743     ok(request != NULL, "HttpOpenRequest failed\n");
1744
1745     ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1746     ok(ret, "HttpSendRequest failed\n");
1747
1748     status = 0;
1749     size = sizeof(status);
1750     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1751     ok(ret, "HttpQueryInfo failed\n");
1752     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1753
1754     InternetCloseHandle(request);
1755
1756     request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1757     ok(request != NULL, "HttpOpenRequest failed\n");
1758
1759     ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1760     ok(ret, "HttpAddRequestHeaders failed\n");
1761
1762     ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1763     ok(ret, "HttpSendRequest failed\n");
1764
1765     status = 0;
1766     size = sizeof(status);
1767     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1768     ok(ret, "HttpQueryInfo failed\n");
1769     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1770
1771     InternetCloseHandle(request);
1772     InternetCloseHandle(connect);
1773     InternetCloseHandle(session);
1774 }
1775
1776 static void test_connection_header(int port)
1777 {
1778     HINTERNET ses, con, req;
1779     DWORD size, status;
1780     BOOL ret;
1781
1782     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1783     ok(ses != NULL, "InternetOpen failed\n");
1784
1785     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1786     ok(con != NULL, "InternetConnect failed\n");
1787
1788     req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1789     ok(req != NULL, "HttpOpenRequest failed\n");
1790
1791     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1792     ok(ret, "HttpSendRequest failed\n");
1793
1794     status = 0;
1795     size = sizeof(status);
1796     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1797     ok(ret, "HttpQueryInfo failed\n");
1798     ok(status == 200, "request failed with status %u\n", status);
1799
1800     InternetCloseHandle(req);
1801
1802     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1803     ok(req != NULL, "HttpOpenRequest failed\n");
1804
1805     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1806     ok(ret, "HttpSendRequest failed\n");
1807
1808     status = 0;
1809     size = sizeof(status);
1810     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1811     ok(ret, "HttpQueryInfo failed\n");
1812     ok(status == 200, "request failed with status %u\n", status);
1813
1814     InternetCloseHandle(req);
1815
1816     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1817     ok(req != NULL, "HttpOpenRequest failed\n");
1818
1819     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1820     ok(ret, "HttpSendRequest failed\n");
1821
1822     status = 0;
1823     size = sizeof(status);
1824     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1825     ok(ret, "HttpQueryInfo failed\n");
1826     ok(status == 200, "request failed with status %u\n", status);
1827
1828     InternetCloseHandle(req);
1829
1830     req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1831     ok(req != NULL, "HttpOpenRequest failed\n");
1832
1833     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1834     ok(ret, "HttpSendRequest failed\n");
1835
1836     status = 0;
1837     size = sizeof(status);
1838     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1839     ok(ret, "HttpQueryInfo failed\n");
1840     ok(status == 200, "request failed with status %u\n", status);
1841
1842     InternetCloseHandle(req);
1843     InternetCloseHandle(con);
1844     InternetCloseHandle(ses);
1845 }
1846
1847 static void test_http1_1(int port)
1848 {
1849     HINTERNET ses, con, req;
1850     BOOL ret;
1851
1852     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1853     ok(ses != NULL, "InternetOpen failed\n");
1854
1855     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1856     ok(con != NULL, "InternetConnect failed\n");
1857
1858     req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1859     ok(req != NULL, "HttpOpenRequest failed\n");
1860
1861     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1862     if (ret)
1863     {
1864         InternetCloseHandle(req);
1865
1866         req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1867         ok(req != NULL, "HttpOpenRequest failed\n");
1868
1869         ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1870         todo_wine
1871         ok(ret, "HttpSendRequest failed\n");
1872     }
1873
1874     InternetCloseHandle(req);
1875     InternetCloseHandle(con);
1876     InternetCloseHandle(ses);
1877 }
1878
1879 static void test_cookie_header(int port)
1880 {
1881     HINTERNET ses, con, req;
1882     DWORD size, status, error;
1883     BOOL ret;
1884     char buffer[64];
1885
1886     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1887     ok(ses != NULL, "InternetOpen failed\n");
1888
1889     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1890     ok(con != NULL, "InternetConnect failed\n");
1891
1892     InternetSetCookie("http://localhost", "cookie", "biscuit");
1893
1894     req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1895     ok(req != NULL, "HttpOpenRequest failed\n");
1896
1897     buffer[0] = 0;
1898     size = sizeof(buffer);
1899     SetLastError(0xdeadbeef);
1900     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1901     error = GetLastError();
1902     ok(!ret, "HttpQueryInfo succeeded\n");
1903     ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
1904
1905     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1906     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1907
1908     status = 0;
1909     size = sizeof(status);
1910     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1911     ok(ret, "HttpQueryInfo failed\n");
1912     ok(status == 200, "request failed with status %u\n", status);
1913
1914     buffer[0] = 0;
1915     size = sizeof(buffer);
1916     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1917     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1918     ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
1919
1920     InternetCloseHandle(req);
1921     InternetCloseHandle(con);
1922     InternetCloseHandle(ses);
1923 }
1924
1925 static void test_http_connection(void)
1926 {
1927     struct server_info si;
1928     HANDLE hThread;
1929     DWORD id = 0, r;
1930
1931     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1932     si.port = 7531;
1933
1934     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1935     ok( hThread != NULL, "create thread failed\n");
1936
1937     r = WaitForSingleObject(si.hEvent, 10000);
1938     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1939     if (r != WAIT_OBJECT_0)
1940         return;
1941
1942     test_basic_request(si.port, "GET", "/test1");
1943     test_proxy_indirect(si.port);
1944     test_proxy_direct(si.port);
1945     test_header_handling_order(si.port);
1946     test_basic_request(si.port, "POST", "/test5");
1947     test_basic_request(si.port, "GET", "/test6");
1948     test_connection_header(si.port);
1949     test_http1_1(si.port);
1950     test_cookie_header(si.port);
1951
1952     /* send the basic request again to shutdown the server thread */
1953     test_basic_request(si.port, "GET", "/quit");
1954
1955     r = WaitForSingleObject(hThread, 3000);
1956     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1957     CloseHandle(hThread);
1958 }
1959
1960 static void test_user_agent_header(void)
1961 {
1962     HINTERNET ses, con, req;
1963     DWORD size, err;
1964     char buffer[64];
1965     BOOL ret;
1966
1967     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1968     ok(ses != NULL, "InternetOpen failed\n");
1969
1970     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1971     ok(con != NULL, "InternetConnect failed\n");
1972
1973     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1974     ok(req != NULL, "HttpOpenRequest failed\n");
1975
1976     size = sizeof(buffer);
1977     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1978     err = GetLastError();
1979     ok(!ret, "HttpQueryInfo succeeded\n");
1980     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1981
1982     ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1983     ok(ret, "HttpAddRequestHeaders succeeded\n");
1984
1985     size = sizeof(buffer);
1986     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1987     err = GetLastError();
1988     ok(ret, "HttpQueryInfo failed\n");
1989     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1990
1991     InternetCloseHandle(req);
1992
1993     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1994     ok(req != NULL, "HttpOpenRequest failed\n");
1995
1996     size = sizeof(buffer);
1997     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1998     err = GetLastError();
1999     ok(!ret, "HttpQueryInfo succeeded\n");
2000     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2001
2002     ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2003     ok(ret, "HttpAddRequestHeaders failed\n");
2004
2005     buffer[0] = 0;
2006     size = sizeof(buffer);
2007     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2008     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2009     ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2010
2011     InternetCloseHandle(req);
2012     InternetCloseHandle(con);
2013     InternetCloseHandle(ses);
2014 }
2015
2016 static void test_bogus_accept_types_array(void)
2017 {
2018     HINTERNET ses, con, req;
2019     static const char *types[] = { (const char *)6240, "*/*", "%p", "", "*/*", NULL };
2020     DWORD size;
2021     char buffer[32];
2022     BOOL ret;
2023
2024     ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2025     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2026     req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2027
2028     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2029
2030     buffer[0] = 0;
2031     size = sizeof(buffer);
2032     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2033     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2034     ok(!strcmp(buffer, ", */*, %p, , */*") || /* IE6 */
2035        !strcmp(buffer, "*/*, %p, */*"),
2036        "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , */*'\n", buffer);
2037
2038     InternetCloseHandle(req);
2039     InternetCloseHandle(con);
2040     InternetCloseHandle(ses);
2041 }
2042
2043 struct context
2044 {
2045     HANDLE event;
2046     HINTERNET req;
2047 };
2048
2049 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2050 {
2051     INTERNET_ASYNC_RESULT *result = info;
2052     struct context *ctx = (struct context *)context;
2053
2054     trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2055
2056     if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2057     {
2058         trace("request handle: 0x%08lx\n", result->dwResult);
2059         ctx->req = (HINTERNET)result->dwResult;
2060         SetEvent(ctx->event);
2061     }
2062     if (status == INTERNET_STATUS_HANDLE_CLOSING)
2063     {
2064         DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2065
2066         if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2067             ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2068         SetEvent(ctx->event);
2069     }
2070 }
2071
2072 static void test_open_url_async(void)
2073 {
2074     BOOL ret;
2075     HINTERNET ses, req;
2076     DWORD size;
2077     struct context ctx;
2078     ULONG type;
2079
2080     ctx.req = NULL;
2081     ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2082
2083     ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2084     ok(ses != NULL, "InternetOpen failed\n");
2085
2086     pInternetSetStatusCallbackA(ses, cb);
2087     ResetEvent(ctx.event);
2088
2089     req = InternetOpenUrl(ses, "http://www.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2090     ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2091
2092     WaitForSingleObject(ctx.event, INFINITE);
2093
2094     type = 0;
2095     size = sizeof(type);
2096     ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2097     ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2098     ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2099        "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2100
2101     size = 0;
2102     ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2103     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2104     ok(size > 0, "expected size > 0\n");
2105
2106     ResetEvent(ctx.event);
2107     InternetCloseHandle(ctx.req);
2108     WaitForSingleObject(ctx.event, INFINITE);
2109
2110     InternetCloseHandle(ses);
2111     CloseHandle(ctx.event);
2112 }
2113
2114 #define STATUS_STRING(status) \
2115     memcpy(status_string[status], #status, sizeof(CHAR) * \
2116            (strlen(#status) < MAX_STATUS_NAME ? \
2117             strlen(#status) : \
2118             MAX_STATUS_NAME - 1))
2119 static void init_status_tests(void)
2120 {
2121     memset(expect, 0, sizeof(expect));
2122     memset(optional, 0, sizeof(optional));
2123     memset(wine_allow, 0, sizeof(wine_allow));
2124     memset(notified, 0, sizeof(notified));
2125     memset(status_string, 0, sizeof(status_string));
2126     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2127     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2128     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2129     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2130     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2131     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2132     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2133     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2134     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2135     STATUS_STRING(INTERNET_STATUS_PREFETCH);
2136     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2137     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2138     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2139     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2140     STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2141     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2142     STATUS_STRING(INTERNET_STATUS_REDIRECT);
2143     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2144     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2145     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2146     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2147     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2148     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2149     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2150     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2151     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2152 }
2153 #undef STATUS_STRING
2154
2155 START_TEST(http)
2156 {
2157     HMODULE hdll;
2158     hdll = GetModuleHandleA("wininet.dll");
2159     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2160     pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
2161     pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
2162     pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
2163     pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
2164
2165     if (!pInternetSetStatusCallbackA)
2166         skip("skipping the InternetReadFile tests\n");
2167     else
2168     {
2169         init_status_tests();
2170         InternetReadFile_test(INTERNET_FLAG_ASYNC);
2171         InternetReadFile_test(0);
2172         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2173         test_open_url_async();
2174     }
2175     InternetOpenRequest_test();
2176     test_http_cache();
2177     InternetOpenUrlA_test();
2178     if (!pInternetTimeFromSystemTimeA)
2179         skip("skipping the InternetTime tests\n");
2180     else
2181     {
2182         InternetTimeFromSystemTimeA_test();
2183         InternetTimeFromSystemTimeW_test();
2184         InternetTimeToSystemTimeA_test();
2185         InternetTimeToSystemTimeW_test();
2186     }
2187     HttpSendRequestEx_test();
2188     HttpHeaders_test();
2189     test_http_connection();
2190     test_user_agent_header();
2191     test_bogus_accept_types_array();
2192 }