comctl32: Fix listview redraw when deleting items.
[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             break;
196         case INTERNET_STATUS_HANDLE_CLOSING:
197             ok(dwStatusInformationLength == sizeof(HINTERNET),
198                 "info length should be sizeof(HINTERNET) instead of %d\n",
199                 dwStatusInformationLength);
200             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
201                 GetCurrentThreadId(), hInternet, dwContext,
202                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
203             break;
204         case INTERNET_STATUS_REQUEST_COMPLETE:
205         {
206             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
207             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
208                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
209                 dwStatusInformationLength);
210             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
211                 GetCurrentThreadId(), hInternet, dwContext,
212                 iar->dwResult,iar->dwError,dwStatusInformationLength);
213             SetEvent(hCompleteEvent);
214             break;
215         }
216         case INTERNET_STATUS_REDIRECT:
217             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
218                 GetCurrentThreadId(), hInternet, dwContext,
219                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
220             *(LPSTR)lpvStatusInformation = '\0';
221             break;
222         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
223             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
224                 GetCurrentThreadId(), hInternet, dwContext,
225                 lpvStatusInformation, dwStatusInformationLength);
226             break;
227         default:
228             trace("%04x:Callback %p 0x%lx %d %p %d\n",
229                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
230                 lpvStatusInformation, dwStatusInformationLength);
231     }
232 }
233
234 static void InternetReadFile_test(int flags)
235 {
236     BOOL res;
237     DWORD rc;
238     CHAR buffer[4000];
239     DWORD length;
240     DWORD out;
241     const char *types[2] = { "*", NULL };
242     HINTERNET hi, hic = 0, hor = 0;
243
244     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
245
246     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
247
248     trace("InternetOpenA <--\n");
249     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
250     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
251     trace("InternetOpenA -->\n");
252
253     if (hi == 0x0) goto abort;
254
255     pInternetSetStatusCallbackA(hi,&callback);
256
257     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
258
259     trace("InternetConnectA <--\n");
260     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
261                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
262     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
263     trace("InternetConnectA -->\n");
264
265     if (hic == 0x0) goto abort;
266
267     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
268     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
269     SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
270     SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
271
272     trace("HttpOpenRequestA <--\n");
273     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
274                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
275                            0xdeadbead);
276     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
277         /*
278          * If the internet name can't be resolved we are probably behind
279          * a firewall or in some other way not directly connected to the
280          * Internet. Not enough reason to fail the test. Just ignore and
281          * abort.
282          */
283     } else  {
284         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
285     }
286     trace("HttpOpenRequestA -->\n");
287
288     if (hor == 0x0) goto abort;
289
290     length = sizeof(buffer);
291     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
292     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
293     ok(!strcmp(buffer, "http://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
294
295     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
296     todo_wine
297     {
298         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
299         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
300     }
301     if (first_connection_to_test_url)
302     {
303         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
304         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
305     }
306     else
307     {
308         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
309         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
310     }
311     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
312     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
313     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
314     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
315     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
316     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
317     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
318     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
319     SET_EXPECT(INTERNET_STATUS_REDIRECT);
320     if (flags & INTERNET_FLAG_ASYNC)
321         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
322     else
323         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
324
325     trace("HttpSendRequestA -->\n");
326     SetLastError(0xdeadbeef);
327     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
328     if (flags & INTERNET_FLAG_ASYNC)
329         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
330             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
331     else
332         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
333            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
334     trace("HttpSendRequestA <--\n");
335
336     if (flags & INTERNET_FLAG_ASYNC)
337         WaitForSingleObject(hCompleteEvent, INFINITE);
338
339     if (first_connection_to_test_url)
340     {
341         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
342         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
343     }
344     else todo_wine
345     {
346         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
347         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
348     }
349     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
350     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
351     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
352     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
353     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
354     if (flags & INTERNET_FLAG_ASYNC)
355         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
356     else
357         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
358     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
359     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
360     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
361
362     length = 4;
363     rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
364     trace("Option 0x17 -> %i  %i\n",rc,out);
365
366     length = 100;
367     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
368     trace("Option 0x22 -> %i  %s\n",rc,buffer);
369
370     length = 4000;
371     rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
372     buffer[length]=0;
373     trace("Option 0x16 -> %i  %s\n",rc,buffer);
374
375     length = sizeof(buffer);
376     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
377     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
378     ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
379
380     length = 16;
381     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
382     trace("Option 0x5 -> %i  %s  (%u)\n",rc,buffer,GetLastError());
383
384     length = 100;
385     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
386     buffer[length]=0;
387     trace("Option 0x1 -> %i  %s\n",rc,buffer);
388
389     SetLastError(0xdeadbeef);
390     rc = InternetReadFile(NULL, buffer, 100, &length);
391     ok(!rc, "InternetReadFile should have failed\n");
392     ok(GetLastError() == ERROR_INVALID_HANDLE,
393         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
394         GetLastError());
395
396     length = 100;
397     trace("Entering Query loop\n");
398
399     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
400     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
401     while (TRUE)
402     {
403         if (flags & INTERNET_FLAG_ASYNC)
404             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
405         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
406         ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
407         ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
408            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
409         if (flags & INTERNET_FLAG_ASYNC)
410         {
411             if (rc != 0)
412             {
413                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
414             }
415             else if (GetLastError() == ERROR_IO_PENDING)
416             {
417                 WaitForSingleObject(hCompleteEvent, INFINITE);
418                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
419                 continue;
420             }
421         }
422         if (length)
423         {
424             char *buffer;
425             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
426
427             rc = InternetReadFile(hor,buffer,length,&length);
428
429             buffer[length]=0;
430
431             trace("ReadFile -> %i %i\n",rc,length);
432
433             HeapFree(GetProcessHeap(),0,buffer);
434         }
435         if (length == 0)
436             break;
437     }
438     /* WinXP does not send, but Win98 does */
439     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
440     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
441 abort:
442     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
443     if (hor != 0x0) {
444         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
445         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
446         SetLastError(0xdeadbeef);
447         rc = InternetCloseHandle(hor);
448         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
449         SetLastError(0xdeadbeef);
450         rc = InternetCloseHandle(hor);
451         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
452         ok (GetLastError() == ERROR_INVALID_HANDLE,
453             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
454             GetLastError());
455     }
456     /* We intentionally do not close the handle opened by InternetConnectA as this
457      * tickles bug #9479: native closes child internet handles when the parent handles
458      * are closed. This is verified below by checking that the number of
459      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
460     if (hi != 0x0) {
461       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
462       rc = InternetCloseHandle(hi);
463       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
464       if (flags & INTERNET_FLAG_ASYNC)
465           Sleep(100);
466     }
467     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
468     if (hor != 0x0) todo_wine
469     {
470         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
471         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
472     }
473     else
474     {
475         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
476         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
477     }
478     CloseHandle(hCompleteEvent);
479     first_connection_to_test_url = FALSE;
480 }
481
482 static void InternetReadFileExA_test(int flags)
483 {
484     DWORD rc;
485     DWORD length;
486     const char *types[2] = { "*", NULL };
487     HINTERNET hi, hic = 0, hor = 0;
488     INTERNET_BUFFERS inetbuffers;
489
490     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
491
492     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
493
494     trace("InternetOpenA <--\n");
495     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
496     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
497     trace("InternetOpenA -->\n");
498
499     if (hi == 0x0) goto abort;
500
501     pInternetSetStatusCallbackA(hi,&callback);
502
503     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
504
505     trace("InternetConnectA <--\n");
506     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
507                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
508     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
509     trace("InternetConnectA -->\n");
510
511     if (hic == 0x0) goto abort;
512
513     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
514     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
515     SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
516     SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
517
518     trace("HttpOpenRequestA <--\n");
519     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
520                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
521                            0xdeadbead);
522     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
523         /*
524          * If the internet name can't be resolved we are probably behind
525          * a firewall or in some other way not directly connected to the
526          * Internet. Not enough reason to fail the test. Just ignore and
527          * abort.
528          */
529     } else  {
530         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
531     }
532     trace("HttpOpenRequestA -->\n");
533
534     if (hor == 0x0) goto abort;
535
536     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
537     todo_wine
538     {
539         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
540         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
541     }
542     if (first_connection_to_test_url)
543     {
544         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
545         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
546     }
547     else
548     {
549         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
550         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
551     }
552     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
553     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
554     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
555     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
556     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
557     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
558     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
559     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
560     SET_EXPECT(INTERNET_STATUS_REDIRECT);
561     if (flags & INTERNET_FLAG_ASYNC)
562         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
563     else
564         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
565
566     trace("HttpSendRequestA -->\n");
567     SetLastError(0xdeadbeef);
568     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
569     if (flags & INTERNET_FLAG_ASYNC)
570         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
571             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
572     else
573         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
574            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
575     trace("HttpSendRequestA <--\n");
576
577     if (!rc && (GetLastError() == ERROR_IO_PENDING))
578         WaitForSingleObject(hCompleteEvent, INFINITE);
579
580     if (first_connection_to_test_url)
581     {
582         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
583         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
584     }
585     else todo_wine
586     {
587         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
588         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
589     }
590     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
591     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
592     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
593     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
594     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
595     if (flags & INTERNET_FLAG_ASYNC)
596         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
597     else
598         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
599     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
600     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
601     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
602
603     /* tests invalid dwStructSize */
604     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
605     inetbuffers.lpcszHeader = NULL;
606     inetbuffers.dwHeadersLength = 0;
607     inetbuffers.dwBufferLength = 10;
608     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
609     inetbuffers.dwOffsetHigh = 1234;
610     inetbuffers.dwOffsetLow = 5678;
611     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
612     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
613         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
614         rc ? "TRUE" : "FALSE", GetLastError());
615     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
616
617     /* tests to see whether lpcszHeader is used - it isn't */
618     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
619     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
620     inetbuffers.dwHeadersLength = 255;
621     inetbuffers.dwBufferLength = 0;
622     inetbuffers.lpvBuffer = NULL;
623     inetbuffers.dwOffsetHigh = 1234;
624     inetbuffers.dwOffsetLow = 5678;
625     SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
626     SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
627     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
628     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
629     todo_wine
630     {
631         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
632         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
633     }
634
635     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
636     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
637         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
638         rc ? "TRUE" : "FALSE", GetLastError());
639
640     length = 0;
641     trace("Entering Query loop\n");
642
643     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
644     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
645     while (TRUE)
646     {
647         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
648         inetbuffers.dwBufferLength = 1024;
649         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
650         inetbuffers.dwOffsetHigh = 1234;
651         inetbuffers.dwOffsetLow = 5678;
652
653         SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
654         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
655         SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
656         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
657         if (!rc)
658         {
659             if (GetLastError() == ERROR_IO_PENDING)
660             {
661                 trace("InternetReadFileEx -> PENDING\n");
662                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
663                 WaitForSingleObject(hCompleteEvent, INFINITE);
664                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
665                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
666             }
667             else
668             {
669                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
670                 break;
671             }
672         }
673         else
674         {
675             trace("InternetReadFileEx -> SUCCEEDED\n");
676             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
677             if (inetbuffers.dwBufferLength)
678             {
679                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
680                 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
681             }
682             else
683             {
684                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
685                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
686                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
687             }
688         }
689
690         trace("read %i bytes\n", inetbuffers.dwBufferLength);
691         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
692
693         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
694             "InternetReadFileEx sets offsets to 0x%x%08x\n",
695             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
696
697         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
698
699         if (!inetbuffers.dwBufferLength)
700             break;
701
702         length += inetbuffers.dwBufferLength;
703     }
704     ok(length > 0, "failed to read any of the document\n");
705     trace("Finished. Read %d bytes\n", length);
706
707     /* WinXP does not send, but Win98 does */
708     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
709     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
710 abort:
711     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
712     if (hor) {
713         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
714         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
715         rc = InternetCloseHandle(hor);
716         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
717         rc = InternetCloseHandle(hor);
718         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
719     }
720     if (hic) {
721         rc = InternetCloseHandle(hic);
722         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
723     }
724     if (hi) {
725       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
726       rc = InternetCloseHandle(hi);
727       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
728       if (flags & INTERNET_FLAG_ASYNC)
729           Sleep(100);
730       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
731     }
732     if (hor != 0x0) todo_wine
733     {
734         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
735         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
736     }
737     else
738     {
739         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
740         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
741     }
742     CloseHandle(hCompleteEvent);
743     first_connection_to_test_url = FALSE;
744 }
745
746 static void InternetOpenUrlA_test(void)
747 {
748   HINTERNET myhinternet, myhttp;
749   char buffer[0x400];
750   DWORD size, readbytes, totalbytes=0;
751   BOOL ret;
752   
753   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
754   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
755   size = 0x400;
756   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
757   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
758
759   SetLastError(0);
760   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
761                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
762   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
763     return; /* WinXP returns this when not connected to the net */
764   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
765   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
766   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
767   totalbytes += readbytes;
768   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
769     totalbytes += readbytes;
770   trace("read 0x%08x bytes\n",totalbytes);
771
772   InternetCloseHandle(myhttp);
773   InternetCloseHandle(myhinternet);
774 }
775
776 static void InternetTimeFromSystemTimeA_test(void)
777 {
778     BOOL ret;
779     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
780     char string[INTERNET_RFC1123_BUFSIZE];
781     static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
782
783     ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
784     ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
785
786     ok( !memcmp( string, expect, sizeof(expect) ),
787         "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
788 }
789
790 static void InternetTimeFromSystemTimeW_test(void)
791 {
792     BOOL ret;
793     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
794     WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
795     static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
796                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
797
798     ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
799     ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
800
801     ok( !memcmp( string, expect, sizeof(expect) ),
802         "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
803 }
804
805 static void InternetTimeToSystemTimeA_test(void)
806 {
807     BOOL ret;
808     SYSTEMTIME time;
809     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
810     static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
811     static const char string2[] = " fri 7 jan 2005 12 06 35";
812
813     ret = pInternetTimeToSystemTimeA( string, &time, 0 );
814     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
815     ok( !memcmp( &time, &expect, sizeof(expect) ),
816         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
817
818     ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
819     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
820     ok( !memcmp( &time, &expect, sizeof(expect) ),
821         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
822 }
823
824 static void InternetTimeToSystemTimeW_test(void)
825 {
826     BOOL ret;
827     SYSTEMTIME time;
828     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
829     static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
830                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
831     static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
832                                      '1','2',' ','0','6',' ','3','5',0 };
833     static const WCHAR string3[] = { 'F','r',0 };
834
835     ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
836     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
837
838     ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
839     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
840
841     ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
842     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
843
844     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
845     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
846
847     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
848     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
849     ok( !memcmp( &time, &expect, sizeof(expect) ),
850         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
851
852     ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
853     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
854     ok( !memcmp( &time, &expect, sizeof(expect) ),
855         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
856
857     ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
858     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
859 }
860
861 static void HttpSendRequestEx_test(void)
862 {
863     HINTERNET hSession;
864     HINTERNET hConnect;
865     HINTERNET hRequest;
866
867     INTERNET_BUFFERS BufferIn;
868     DWORD dwBytesWritten;
869     DWORD dwBytesRead;
870     CHAR szBuffer[256];
871     int i;
872     BOOL ret;
873
874     static char szPostData[] = "mode=Test";
875     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
876
877     hSession = InternetOpen("Wine Regression Test",
878             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
879     ok( hSession != NULL ,"Unable to open Internet session\n");
880     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
881             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
882             0);
883     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
884     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
885             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
886     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
887     {
888         trace( "Network unreachable, skipping test\n" );
889         goto done;
890     }
891     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
892
893
894     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
895     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
896     BufferIn.lpcszHeader = szContentType;
897     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
898     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
899     BufferIn.lpvBuffer = szPostData;
900     BufferIn.dwBufferLength = 3;
901     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
902     BufferIn.dwOffsetLow = 0;
903     BufferIn.dwOffsetHigh = 0;
904
905     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
906     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
907
908     for (i = 3; szPostData[i]; i++)
909         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
910                 "InternetWriteFile failed\n");
911
912     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
913
914     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
915             "Unable to read response\n");
916     szBuffer[dwBytesRead] = 0;
917
918     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
919     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
920
921     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
922 done:
923     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
924     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
925 }
926
927 static void InternetOpenRequest_test(void)
928 {
929     HINTERNET session, connect, request;
930     static const char *types[] = { "*", "", NULL };
931     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
932     static const WCHAR *typesW[] = { any, empty, NULL };
933     BOOL ret;
934
935     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
936     ok(session != NULL ,"Unable to open Internet session\n");
937
938     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
939                               INTERNET_SERVICE_HTTP, 0, 0);
940     ok(connect == NULL, "InternetConnectA should have failed\n");
941     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
942
943     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
944                               INTERNET_SERVICE_HTTP, 0, 0);
945     ok(connect == NULL, "InternetConnectA should have failed\n");
946     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
947
948     connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
949                               INTERNET_SERVICE_HTTP, 0, 0);
950     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
951
952     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
953     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
954     {
955         trace( "Network unreachable, skipping test\n" );
956         goto done;
957     }
958     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
959
960     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
961     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
962     ok(InternetCloseHandle(request), "Close request handle failed\n");
963
964     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
965     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
966
967     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
968     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
969     ok(InternetCloseHandle(request), "Close request handle failed\n");
970
971 done:
972     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
973     ok(InternetCloseHandle(session), "Close session handle failed\n");
974 }
975
976 static void test_http_cache(void)
977 {
978     HINTERNET session, connect, request;
979     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
980     DWORD size, file_size;
981     BYTE buf[100];
982     HANDLE file;
983     BOOL ret;
984
985     static const char *types[] = { "*", "", NULL };
986
987     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
988     ok(session != NULL ,"Unable to open Internet session\n");
989
990     connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
991                               INTERNET_SERVICE_HTTP, 0, 0);
992     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
993
994     request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
995     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
996     {
997         trace( "Network unreachable, skipping test\n" );
998
999         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1000         ok(InternetCloseHandle(session), "Close session handle failed\n");
1001
1002         return;
1003     }
1004     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1005
1006     size = sizeof(url);
1007     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1008     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
1009     ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
1010
1011     size = sizeof(file_name);
1012     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1013     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1014     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1015     ok(!size, "size = %d\n", size);
1016
1017     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1018     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1019
1020     size = sizeof(file_name);
1021     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1022     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1023
1024     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1025                       FILE_ATTRIBUTE_NORMAL, NULL);
1026     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1027     file_size = GetFileSize(file, NULL);
1028     ok(file_size == 0, "file size=%d\n", file_size);
1029
1030     ret = InternetReadFile(request, buf, sizeof(buf), &size);
1031     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1032     ok(size == sizeof(buf), "size=%d\n", size);
1033
1034     file_size = GetFileSize(file, NULL);
1035     ok(file_size == sizeof(buf), "file size=%d\n", file_size);
1036     CloseHandle(file);
1037
1038     ok(InternetCloseHandle(request), "Close request handle failed\n");
1039
1040     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1041                       FILE_ATTRIBUTE_NORMAL, NULL);
1042     ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1043     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1044
1045     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1046     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1047
1048     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1049     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1050     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1051     ok(!size, "size = %d\n", size);
1052
1053     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1054     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1055
1056     size = sizeof(file_name);
1057     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1058     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1059     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1060     ok(!size, "size = %d\n", size);
1061
1062     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1063                       FILE_ATTRIBUTE_NORMAL, NULL);
1064     ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1065     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1066
1067     ok(InternetCloseHandle(request), "Close request handle failed\n");
1068
1069     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1070     ok(InternetCloseHandle(session), "Close session handle failed\n");
1071 }
1072
1073 static void HttpHeaders_test(void)
1074 {
1075     HINTERNET hSession;
1076     HINTERNET hConnect;
1077     HINTERNET hRequest;
1078     CHAR      buffer[256];
1079     DWORD     len = 256;
1080     DWORD     index = 0;
1081
1082     hSession = InternetOpen("Wine Regression Test",
1083             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1084     ok( hSession != NULL ,"Unable to open Internet session\n");
1085     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1086             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1087             0);
1088     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1089     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1090             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1091     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1092     {
1093         trace( "Network unreachable, skipping test\n" );
1094         goto done;
1095     }
1096     ok( hRequest != NULL, "Failed to open request handle\n");
1097
1098     index = 0;
1099     len = sizeof(buffer);
1100     strcpy(buffer,"Warning");
1101     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1102                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1103     
1104     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1105             "Failed to add new header\n");
1106
1107     index = 0;
1108     len = sizeof(buffer);
1109     strcpy(buffer,"Warning");
1110     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1111                 buffer,&len,&index),"Unable to query header\n");
1112     ok(index == 1, "Index was not incremented\n");
1113     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1114     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1115     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1116     len = sizeof(buffer);
1117     strcpy(buffer,"Warning");
1118     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1119                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1120
1121     index = 0;
1122     len = 5; /* could store the string but not the NULL terminator */
1123     strcpy(buffer,"Warning");
1124     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1125                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1126     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1127     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1128
1129     /* a call with NULL will fail but will return the length */
1130     index = 0;
1131     len = sizeof(buffer);
1132     SetLastError(0xdeadbeef);
1133     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1134                 NULL,&len,&index) == FALSE,"Query worked\n");
1135     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1136     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1137     ok(index == 0, "Index was incremented\n");
1138
1139     /* even for a len that is too small */
1140     index = 0;
1141     len = 15;
1142     SetLastError(0xdeadbeef);
1143     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1144                 NULL,&len,&index) == FALSE,"Query worked\n");
1145     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1146     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1147     ok(index == 0, "Index was incremented\n");
1148
1149     index = 0;
1150     len = 0;
1151     SetLastError(0xdeadbeef);
1152     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1153                 NULL,&len,&index) == FALSE,"Query worked\n");
1154     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1155     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1156     ok(index == 0, "Index was incremented\n");
1157
1158
1159     /* a working query */
1160     index = 0;
1161     len = sizeof(buffer);
1162     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1163                 buffer,&len,&index),"Unable to query header\n");
1164     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1165     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1166     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1167     ok(index == 0, "Index was incremented\n");
1168
1169
1170
1171     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1172             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1173
1174     index = 0;
1175     len = sizeof(buffer);
1176     strcpy(buffer,"Warning");
1177     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1178                 buffer,&len,&index),"Unable to query header\n");
1179     ok(index == 1, "Index was not incremented\n");
1180     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1181     len = sizeof(buffer);
1182     strcpy(buffer,"Warning");
1183     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1184                 buffer,&len,&index),"Failed to get second header\n");
1185     ok(index == 2, "Index was not incremented\n");
1186     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1187     len = sizeof(buffer);
1188     strcpy(buffer,"Warning");
1189     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1190                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1191
1192     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1193
1194     index = 0;
1195     len = sizeof(buffer);
1196     strcpy(buffer,"Warning");
1197     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1198                 buffer,&len,&index),"Unable to query header\n");
1199     ok(index == 1, "Index was not incremented\n");
1200     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1201     len = sizeof(buffer);
1202     strcpy(buffer,"Warning");
1203     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1204                 buffer,&len,&index),"Failed to get second header\n");
1205     ok(index == 2, "Index was not incremented\n");
1206     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1207     len = sizeof(buffer);
1208     strcpy(buffer,"Warning");
1209     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1210                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1211     
1212     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1213
1214     index = 0;
1215     len = sizeof(buffer);
1216     strcpy(buffer,"Warning");
1217     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1218                 buffer,&len,&index),"Unable to query header\n");
1219     ok(index == 1, "Index was not incremented\n");
1220     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1221     len = sizeof(buffer);
1222     strcpy(buffer,"Warning");
1223     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1224                 buffer,&len,&index),"Failed to get second header\n");
1225     ok(index == 2, "Index was not incremented\n");
1226     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1227     len = sizeof(buffer);
1228     strcpy(buffer,"Warning");
1229     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1230                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1231
1232     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1233
1234     index = 0;
1235     len = sizeof(buffer);
1236     strcpy(buffer,"Warning");
1237     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1238                 buffer,&len,&index),"Unable to query header\n");
1239     ok(index == 1, "Index was not incremented\n");
1240     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1241     len = sizeof(buffer);
1242     strcpy(buffer,"Warning");
1243     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1244     ok(index == 2, "Index was not incremented\n");
1245     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1246     len = sizeof(buffer);
1247     strcpy(buffer,"Warning");
1248     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1249
1250     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1251
1252     index = 0;
1253     len = sizeof(buffer);
1254     strcpy(buffer,"Warning");
1255     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1256     ok(index == 1, "Index was not incremented\n");
1257     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1258     len = sizeof(buffer);
1259     strcpy(buffer,"Warning");
1260     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1261     ok(index == 2, "Index was not incremented\n");
1262     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1263     len = sizeof(buffer);
1264     strcpy(buffer,"Warning");
1265     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1266
1267     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1268
1269     index = 0;
1270     len = sizeof(buffer);
1271     strcpy(buffer,"Warning");
1272     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1273     ok(index == 1, "Index was not incremented\n");
1274     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1275     len = sizeof(buffer);
1276     strcpy(buffer,"Warning");
1277     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1278     ok(index == 2, "Index was not incremented\n");
1279     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1280     len = sizeof(buffer);
1281     strcpy(buffer,"Warning");
1282     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1283
1284     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");
1285
1286     index = 0;
1287     len = sizeof(buffer);
1288     strcpy(buffer,"Warning");
1289     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1290     ok(index == 1, "Index was not incremented\n");
1291     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1292     len = sizeof(buffer);
1293     strcpy(buffer,"Warning");
1294     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1295     ok(index == 2, "Index was not incremented\n");
1296     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1297     len = sizeof(buffer);
1298     strcpy(buffer,"Warning");
1299     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1300
1301     
1302     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1303 done:
1304     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1305     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1306 }
1307
1308 static const char contmsg[] =
1309 "HTTP/1.1 100 Continue\r\n";
1310
1311 static const char okmsg[] =
1312 "HTTP/1.1 200 OK\r\n"
1313 "Server: winetest\r\n"
1314 "\r\n";
1315
1316 static const char notokmsg[] =
1317 "HTTP/1.1 400 Bad Request\r\n"
1318 "Server: winetest\r\n"
1319 "\r\n";
1320
1321 static const char noauthmsg[] =
1322 "HTTP/1.1 401 Unauthorized\r\n"
1323 "Server: winetest\r\n"
1324 "\r\n";
1325
1326 static const char proxymsg[] =
1327 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1328 "Server: winetest\r\n"
1329 "Proxy-Connection: close\r\n"
1330 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1331 "\r\n";
1332
1333 static const char page1[] =
1334 "<HTML>\r\n"
1335 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1336 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1337 "</HTML>\r\n\r\n";
1338
1339 struct server_info {
1340     HANDLE hEvent;
1341     int port;
1342 };
1343
1344 static DWORD CALLBACK server_thread(LPVOID param)
1345 {
1346     struct server_info *si = param;
1347     int r, c, i, on;
1348     SOCKET s;
1349     struct sockaddr_in sa;
1350     char buffer[0x100];
1351     WSADATA wsaData;
1352     int last_request = 0;
1353
1354     WSAStartup(MAKEWORD(1,1), &wsaData);
1355
1356     s = socket(AF_INET, SOCK_STREAM, 0);
1357     if (s == INVALID_SOCKET)
1358         return 1;
1359
1360     on = 1;
1361     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1362
1363     memset(&sa, 0, sizeof sa);
1364     sa.sin_family = AF_INET;
1365     sa.sin_port = htons(si->port);
1366     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1367
1368     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1369     if (r<0)
1370         return 1;
1371
1372     listen(s, 0);
1373
1374     SetEvent(si->hEvent);
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
1392         if (strstr(buffer, "GET /test1"))
1393         {
1394             if (!strstr(buffer, "Content-Length: 0"))
1395             {
1396                 send(c, okmsg, sizeof okmsg-1, 0);
1397                 send(c, page1, sizeof page1-1, 0);
1398             }
1399             else
1400                 send(c, notokmsg, sizeof notokmsg-1, 0);
1401         }
1402
1403         if (strstr(buffer, "/test2"))
1404         {
1405             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1406             {
1407                 send(c, okmsg, sizeof okmsg-1, 0);
1408                 send(c, page1, sizeof page1-1, 0);
1409             }
1410             else
1411                 send(c, proxymsg, sizeof proxymsg-1, 0);
1412         }
1413
1414         if (strstr(buffer, "/test3"))
1415         {
1416             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1417                 send(c, okmsg, sizeof okmsg-1, 0);
1418             else
1419                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1420         }
1421
1422         if (strstr(buffer, "/test4"))
1423         {
1424             if (strstr(buffer, "Connection: Close"))
1425                 send(c, okmsg, sizeof okmsg-1, 0);
1426             else
1427                 send(c, notokmsg, sizeof notokmsg-1, 0);
1428         }
1429
1430         if (strstr(buffer, "POST /test5"))
1431         {
1432             if (strstr(buffer, "Content-Length: 0"))
1433             {
1434                 send(c, okmsg, sizeof okmsg-1, 0);
1435                 send(c, page1, sizeof page1-1, 0);
1436             }
1437             else
1438                 send(c, notokmsg, sizeof notokmsg-1, 0);
1439         }
1440
1441         if (strstr(buffer, "GET /test6"))
1442         {
1443             send(c, contmsg, sizeof contmsg-1, 0);
1444             send(c, contmsg, sizeof contmsg-1, 0);
1445             send(c, okmsg, sizeof okmsg-1, 0);
1446             send(c, page1, sizeof page1-1, 0);
1447         }
1448
1449         if (strstr(buffer, "GET /quit"))
1450         {
1451             send(c, okmsg, sizeof okmsg-1, 0);
1452             send(c, page1, sizeof page1-1, 0);
1453             last_request = 1;
1454         }
1455
1456         shutdown(c, 2);
1457         closesocket(c);
1458     } while (!last_request);
1459
1460     closesocket(s);
1461
1462     return 0;
1463 }
1464
1465 static void test_basic_request(int port, const char *verb, const char *url)
1466 {
1467     HINTERNET hi, hc, hr;
1468     DWORD r, count;
1469     char buffer[0x100];
1470
1471     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1472     ok(hi != NULL, "open failed\n");
1473
1474     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1475     ok(hc != NULL, "connect failed\n");
1476
1477     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1478     ok(hr != NULL, "HttpOpenRequest failed\n");
1479
1480     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1481     ok(r, "HttpSendRequest failed\n");
1482
1483     count = 0;
1484     memset(buffer, 0, sizeof buffer);
1485     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1486     ok(r, "InternetReadFile failed\n");
1487     ok(count == sizeof page1 - 1, "count was wrong\n");
1488     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1489
1490     InternetCloseHandle(hr);
1491     InternetCloseHandle(hc);
1492     InternetCloseHandle(hi);
1493 }
1494
1495 static void test_proxy_indirect(int port)
1496 {
1497     HINTERNET hi, hc, hr;
1498     DWORD r, sz, val;
1499     char buffer[0x40];
1500
1501     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1502     ok(hi != NULL, "open failed\n");
1503
1504     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1505     ok(hc != NULL, "connect failed\n");
1506
1507     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1508     ok(hr != NULL, "HttpOpenRequest failed\n");
1509
1510     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1511     ok(r, "HttpSendRequest failed\n");
1512
1513     sz = sizeof buffer;
1514     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1515     ok(r, "HttpQueryInfo failed\n");
1516     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1517
1518     sz = sizeof buffer;
1519     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1520     ok(r, "HttpQueryInfo failed\n");
1521     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1522
1523     sz = sizeof val;
1524     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1525     ok(r, "HttpQueryInfo failed\n");
1526     ok(val == 407, "proxy code wrong\n");
1527
1528     sz = sizeof buffer;
1529     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1530     ok(r, "HttpQueryInfo failed\n");
1531     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1532
1533     sz = sizeof buffer;
1534     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1535     ok(r, "HttpQueryInfo failed\n");
1536     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1537
1538     sz = sizeof buffer;
1539     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1540     ok(r, "HttpQueryInfo failed\n");
1541     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1542
1543     sz = sizeof buffer;
1544     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1545     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1546     ok(r == FALSE, "HttpQueryInfo failed\n");
1547
1548     InternetCloseHandle(hr);
1549     InternetCloseHandle(hc);
1550     InternetCloseHandle(hi);
1551 }
1552
1553 static void test_proxy_direct(int port)
1554 {
1555     HINTERNET hi, hc, hr;
1556     DWORD r, sz;
1557     char buffer[0x40];
1558     static CHAR username[] = "mike",
1559                 password[] = "1101";
1560
1561     sprintf(buffer, "localhost:%d\n", port);
1562     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1563     ok(hi != NULL, "open failed\n");
1564
1565     /* try connect without authorization */
1566     hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1567     ok(hc != NULL, "connect failed\n");
1568
1569     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1570     ok(hr != NULL, "HttpOpenRequest failed\n");
1571
1572     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1573     ok(r, "HttpSendRequest failed\n");
1574
1575     sz = sizeof buffer;
1576     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1577     ok(r, "HttpQueryInfo failed\n");
1578     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1579
1580
1581     /* set the user + password then try again */
1582     todo_wine {
1583     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1584     ok(r, "failed to set user\n");
1585
1586     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1587     ok(r, "failed to set password\n");
1588     }
1589
1590     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1591     ok(r, "HttpSendRequest failed\n");
1592     sz = sizeof buffer;
1593     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1594     ok(r, "HttpQueryInfo failed\n");
1595     todo_wine {
1596     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1597     }
1598
1599
1600     InternetCloseHandle(hr);
1601     InternetCloseHandle(hc);
1602     InternetCloseHandle(hi);
1603 }
1604
1605 static void test_header_handling_order(int port)
1606 {
1607     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1608     static char connection[]    = "Connection: Close";
1609
1610     static const char *types[2] = { "*", NULL };
1611     HINTERNET session, connect, request;
1612     DWORD size, status;
1613     BOOL ret;
1614
1615     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1616     ok(session != NULL, "InternetOpen failed\n");
1617
1618     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1619     ok(connect != NULL, "InternetConnect failed\n");
1620
1621     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1622     ok(request != NULL, "HttpOpenRequest failed\n");
1623
1624     ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1625     ok(ret, "HttpAddRequestHeaders failed\n");
1626
1627     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1628     ok(ret, "HttpSendRequest failed\n");
1629
1630     status = 0;
1631     size = sizeof(status);
1632     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1633     ok(ret, "HttpQueryInfo failed\n");
1634     ok(status == 200, "request failed with status %u\n", status);
1635
1636     InternetCloseHandle(request);
1637
1638     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1639     ok(request != NULL, "HttpOpenRequest failed\n");
1640
1641     ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1642     ok(ret, "HttpSendRequest failed\n");
1643
1644     status = 0;
1645     size = sizeof(status);
1646     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1647     ok(ret, "HttpQueryInfo failed\n");
1648     ok(status == 200, "request failed with status %u\n", status);
1649
1650     InternetCloseHandle(request);
1651     InternetCloseHandle(connect);
1652     InternetCloseHandle(session);
1653 }
1654
1655 static void test_http_connection(void)
1656 {
1657     struct server_info si;
1658     HANDLE hThread;
1659     DWORD id = 0, r;
1660
1661     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1662     si.port = 7531;
1663
1664     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1665     ok( hThread != NULL, "create thread failed\n");
1666
1667     r = WaitForSingleObject(si.hEvent, 10000);
1668     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1669     if (r != WAIT_OBJECT_0)
1670         return;
1671
1672     test_basic_request(si.port, "GET", "/test1");
1673     test_proxy_indirect(si.port);
1674     test_proxy_direct(si.port);
1675     test_header_handling_order(si.port);
1676     test_basic_request(si.port, "POST", "/test5");
1677     test_basic_request(si.port, "GET", "/test6");
1678
1679     /* send the basic request again to shutdown the server thread */
1680     test_basic_request(si.port, "GET", "/quit");
1681
1682     r = WaitForSingleObject(hThread, 3000);
1683     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1684     CloseHandle(hThread);
1685 }
1686
1687 #define STATUS_STRING(status) \
1688     memcpy(status_string[status], #status, sizeof(CHAR) * \
1689            (strlen(#status) < MAX_STATUS_NAME ? \
1690             strlen(#status) : \
1691             MAX_STATUS_NAME - 1))
1692 static void init_status_tests(void)
1693 {
1694     memset(expect, 0, sizeof(expect));
1695     memset(wine_allow, 0, sizeof(wine_allow));
1696     memset(notified, 0, sizeof(notified));
1697     memset(status_string, 0, sizeof(status_string));
1698     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
1699     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
1700     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
1701     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
1702     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
1703     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
1704     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
1705     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
1706     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
1707     STATUS_STRING(INTERNET_STATUS_PREFETCH);
1708     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
1709     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
1710     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
1711     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
1712     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
1713     STATUS_STRING(INTERNET_STATUS_REDIRECT);
1714     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
1715     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
1716     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
1717     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
1718     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
1719     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
1720     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
1721     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
1722     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
1723 }
1724 #undef STATUS_STRING
1725
1726 START_TEST(http)
1727 {
1728     HMODULE hdll;
1729     hdll = GetModuleHandleA("wininet.dll");
1730     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
1731     pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
1732     pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
1733     pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
1734     pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
1735
1736     if (!pInternetSetStatusCallbackA)
1737         skip("skipping the InternetReadFile tests\n");
1738     else
1739     {
1740         init_status_tests();
1741         InternetReadFile_test(INTERNET_FLAG_ASYNC);
1742         InternetReadFile_test(0);
1743         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
1744     }
1745     InternetOpenRequest_test();
1746     test_http_cache();
1747     InternetOpenUrlA_test();
1748     if (!pInternetTimeFromSystemTimeA)
1749         skip("skipping the InternetTime tests\n");
1750     else
1751     {
1752         InternetTimeFromSystemTimeA_test();
1753         InternetTimeFromSystemTimeW_test();
1754         InternetTimeToSystemTimeA_test();
1755         InternetTimeToSystemTimeW_test();
1756     }
1757     HttpSendRequestEx_test();
1758     HttpHeaders_test();
1759     test_http_connection();
1760 }