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