wininet: Send the correct callbacks during InternetReadFileExA calls.
[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 HANDLE hCompleteEvent;
37 static BOOL bResponseReceived;
38 static BOOL bReceivingResponse;
39
40 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
41 static BOOL (WINAPI *pInternetTimeFromSystemTimeA)(CONST SYSTEMTIME *,DWORD ,LPSTR ,DWORD);
42 static BOOL (WINAPI *pInternetTimeFromSystemTimeW)(CONST SYSTEMTIME *,DWORD ,LPWSTR ,DWORD);
43 static BOOL (WINAPI *pInternetTimeToSystemTimeA)(LPCSTR ,SYSTEMTIME *,DWORD);
44 static BOOL (WINAPI *pInternetTimeToSystemTimeW)(LPCWSTR ,SYSTEMTIME *,DWORD);
45
46
47 static VOID WINAPI callback(
48      HINTERNET hInternet,
49      DWORD_PTR dwContext,
50      DWORD dwInternetStatus,
51      LPVOID lpvStatusInformation,
52      DWORD dwStatusInformationLength
53 )
54 {
55     switch (dwInternetStatus)
56     {
57         case INTERNET_STATUS_RESOLVING_NAME:
58             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
59                 GetCurrentThreadId(), hInternet, dwContext,
60                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
61             *(LPSTR)lpvStatusInformation = '\0';
62             break;
63         case INTERNET_STATUS_NAME_RESOLVED:
64             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
65                 GetCurrentThreadId(), hInternet, dwContext,
66                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
67             *(LPSTR)lpvStatusInformation = '\0';
68             break;
69         case INTERNET_STATUS_CONNECTING_TO_SERVER:
70             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
71                 GetCurrentThreadId(), hInternet, dwContext,
72                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
73             *(LPSTR)lpvStatusInformation = '\0';
74             break;
75         case INTERNET_STATUS_CONNECTED_TO_SERVER:
76             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
77                 GetCurrentThreadId(), hInternet, dwContext,
78                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
79             *(LPSTR)lpvStatusInformation = '\0';
80             break;
81         case INTERNET_STATUS_SENDING_REQUEST:
82             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
83                 GetCurrentThreadId(), hInternet, dwContext,
84                 lpvStatusInformation,dwStatusInformationLength);
85             break;
86         case INTERNET_STATUS_REQUEST_SENT:
87             ok(dwStatusInformationLength == sizeof(DWORD),
88                 "info length should be sizeof(DWORD) instead of %d\n",
89                 dwStatusInformationLength);
90             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
91                 GetCurrentThreadId(), hInternet, dwContext,
92                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
93             break;
94         case INTERNET_STATUS_RECEIVING_RESPONSE:
95             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
96                 GetCurrentThreadId(), hInternet, dwContext,
97                 lpvStatusInformation,dwStatusInformationLength);
98             bReceivingResponse = TRUE;
99             break;
100         case INTERNET_STATUS_RESPONSE_RECEIVED:
101             ok(dwStatusInformationLength == sizeof(DWORD),
102                 "info length should be sizeof(DWORD) instead of %d\n",
103                 dwStatusInformationLength);
104             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
105                 GetCurrentThreadId(), hInternet, dwContext,
106                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
107             bResponseReceived = TRUE;
108             break;
109         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
110             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
111                 GetCurrentThreadId(), hInternet,dwContext,
112                 lpvStatusInformation,dwStatusInformationLength);
113             break;
114         case INTERNET_STATUS_PREFETCH:
115             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
116                 GetCurrentThreadId(), hInternet, dwContext,
117                 lpvStatusInformation,dwStatusInformationLength);
118             break;
119         case INTERNET_STATUS_CLOSING_CONNECTION:
120             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
121                 GetCurrentThreadId(), hInternet, dwContext,
122                 lpvStatusInformation,dwStatusInformationLength);
123             break;
124         case INTERNET_STATUS_CONNECTION_CLOSED:
125             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
126                 GetCurrentThreadId(), hInternet, dwContext,
127                 lpvStatusInformation,dwStatusInformationLength);
128             break;
129         case INTERNET_STATUS_HANDLE_CREATED:
130             ok(dwStatusInformationLength == sizeof(HINTERNET),
131                 "info length should be sizeof(HINTERNET) instead of %d\n",
132                 dwStatusInformationLength);
133             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
134                 GetCurrentThreadId(), hInternet, dwContext,
135                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
136             break;
137         case INTERNET_STATUS_HANDLE_CLOSING:
138             ok(dwStatusInformationLength == sizeof(HINTERNET),
139                 "info length should be sizeof(HINTERNET) instead of %d\n",
140                 dwStatusInformationLength);
141             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
142                 GetCurrentThreadId(), hInternet, dwContext,
143                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
144             break;
145         case INTERNET_STATUS_REQUEST_COMPLETE:
146         {
147             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
148             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
149                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
150                 dwStatusInformationLength);
151             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%d,%d} %d\n",
152                 GetCurrentThreadId(), hInternet, dwContext,
153                 iar->dwResult,iar->dwError,dwStatusInformationLength);
154             SetEvent(hCompleteEvent);
155             break;
156         }
157         case INTERNET_STATUS_REDIRECT:
158             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
159                 GetCurrentThreadId(), hInternet, dwContext,
160                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
161             *(LPSTR)lpvStatusInformation = '\0';
162             break;
163         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
164             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
165                 GetCurrentThreadId(), hInternet, dwContext,
166                 lpvStatusInformation, dwStatusInformationLength);
167             break;
168         default:
169             trace("%04x:Callback %p 0x%lx %d %p %d\n",
170                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
171                 lpvStatusInformation, dwStatusInformationLength);
172     }
173 }
174
175 static void InternetReadFile_test(int flags)
176 {
177     DWORD rc;
178     CHAR buffer[4000];
179     DWORD length;
180     DWORD out;
181     const char *types[2] = { "*", NULL };
182     HINTERNET hi, hic = 0, hor = 0;
183
184     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
185
186     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
187
188     trace("InternetOpenA <--\n");
189     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
190     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
191     trace("InternetOpenA -->\n");
192
193     if (hi == 0x0) goto abort;
194
195     pInternetSetStatusCallbackA(hi,&callback);
196
197     trace("InternetConnectA <--\n");
198     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
199                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
200     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
201     trace("InternetConnectA -->\n");
202
203     if (hic == 0x0) goto abort;
204
205     trace("HttpOpenRequestA <--\n");
206     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
207                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
208                            0xdeadbead);
209     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
210         /*
211          * If the internet name can't be resolved we are probably behind
212          * a firewall or in some other way not directly connected to the
213          * Internet. Not enough reason to fail the test. Just ignore and
214          * abort.
215          */
216     } else  {
217         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
218     }
219     trace("HttpOpenRequestA -->\n");
220
221     if (hor == 0x0) goto abort;
222
223     trace("HttpSendRequestA -->\n");
224     SetLastError(0xdeadbeef);
225     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
226     if (flags & INTERNET_FLAG_ASYNC)
227         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
228             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
229     else
230         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
231            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
232     trace("HttpSendRequestA <--\n");
233
234     if (flags & INTERNET_FLAG_ASYNC)
235         WaitForSingleObject(hCompleteEvent, INFINITE);
236
237     length = 4;
238     rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
239     trace("Option 0x17 -> %i  %i\n",rc,out);
240
241     length = 100;
242     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
243     trace("Option 0x22 -> %i  %s\n",rc,buffer);
244
245     length = 4000;
246     rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
247     buffer[length]=0;
248     trace("Option 0x16 -> %i  %s\n",rc,buffer);
249
250     length = 4000;
251     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
252     buffer[length]=0;
253     trace("Option 0x22 -> %i  %s\n",rc,buffer);
254
255     length = 16;
256     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
257     trace("Option 0x5 -> %i  %s  (%u)\n",rc,buffer,GetLastError());
258
259     length = 100;
260     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
261     buffer[length]=0;
262     trace("Option 0x1 -> %i  %s\n",rc,buffer);
263
264     SetLastError(0xdeadbeef);
265     rc = InternetReadFile(NULL, buffer, 100, &length);
266     ok(!rc, "InternetReadFile should have failed\n");
267     ok(GetLastError() == ERROR_INVALID_HANDLE,
268         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
269         GetLastError());
270
271     length = 100;
272     trace("Entering Query loop\n");
273
274     while (length)
275     {
276         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
277         ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed\n");
278
279         if (length)
280         {
281             char *buffer;
282             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
283
284             rc = InternetReadFile(hor,buffer,length,&length);
285
286             buffer[length]=0;
287
288             trace("ReadFile -> %i %i\n",rc,length);
289
290             HeapFree(GetProcessHeap(),0,buffer);
291         }
292     }
293 abort:
294     if (hor != 0x0) {
295         SetLastError(0xdeadbeef);
296         rc = InternetCloseHandle(hor);
297         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
298         SetLastError(0xdeadbeef);
299         rc = InternetCloseHandle(hor);
300         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
301         ok (GetLastError() == ERROR_INVALID_HANDLE,
302             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
303             GetLastError());
304     }
305     if (hic != 0x0) {
306         rc = InternetCloseHandle(hic);
307         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
308     }
309     if (hi != 0x0) {
310       rc = InternetCloseHandle(hi);
311       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
312       if (flags & INTERNET_FLAG_ASYNC)
313           Sleep(100);
314     }
315     CloseHandle(hCompleteEvent);
316 }
317
318 static void InternetReadFileExA_test(int flags)
319 {
320     DWORD rc;
321     DWORD length;
322     const char *types[2] = { "*", NULL };
323     HINTERNET hi, hic = 0, hor = 0;
324     INTERNET_BUFFERS inetbuffers;
325
326     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
327
328     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
329
330     trace("InternetOpenA <--\n");
331     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
332     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
333     trace("InternetOpenA -->\n");
334
335     if (hi == 0x0) goto abort;
336
337     pInternetSetStatusCallbackA(hi,&callback);
338
339     trace("InternetConnectA <--\n");
340     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
341                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
342     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
343     trace("InternetConnectA -->\n");
344
345     if (hic == 0x0) goto abort;
346
347     trace("HttpOpenRequestA <--\n");
348     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
349                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
350                            0xdeadbead);
351     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
352         /*
353          * If the internet name can't be resolved we are probably behind
354          * a firewall or in some other way not directly connected to the
355          * Internet. Not enough reason to fail the test. Just ignore and
356          * abort.
357          */
358     } else  {
359         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
360     }
361     trace("HttpOpenRequestA -->\n");
362
363     if (hor == 0x0) goto abort;
364
365     trace("HttpSendRequestA -->\n");
366     SetLastError(0xdeadbeef);
367     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
368     if (flags & INTERNET_FLAG_ASYNC)
369         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
370             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
371     else
372         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
373            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
374     trace("HttpSendRequestA <--\n");
375
376     if (!rc && (GetLastError() == ERROR_IO_PENDING))
377         WaitForSingleObject(hCompleteEvent, INFINITE);
378
379     /* tests invalid dwStructSize */
380     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
381     inetbuffers.lpcszHeader = NULL;
382     inetbuffers.dwHeadersLength = 0;
383     inetbuffers.dwBufferLength = 10;
384     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
385     inetbuffers.dwOffsetHigh = 1234;
386     inetbuffers.dwOffsetLow = 5678;
387     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
388     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
389         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
390         rc ? "TRUE" : "FALSE", GetLastError());
391     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
392
393     /* tests to see whether lpcszHeader is used - it isn't */
394     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
395     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
396     inetbuffers.dwHeadersLength = 255;
397     inetbuffers.dwBufferLength = 0;
398     inetbuffers.lpvBuffer = NULL;
399     inetbuffers.dwOffsetHigh = 1234;
400     inetbuffers.dwOffsetLow = 5678;
401     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
402     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
403
404     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
405     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
406         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
407         rc ? "TRUE" : "FALSE", GetLastError());
408
409     length = 0;
410     trace("Entering Query loop\n");
411
412     while (TRUE)
413     {
414         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
415         inetbuffers.dwBufferLength = 1024;
416         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
417         inetbuffers.dwOffsetHigh = 1234;
418         inetbuffers.dwOffsetLow = 5678;
419
420         bReceivingResponse = FALSE;
421         bResponseReceived = FALSE;
422         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
423         if (!rc)
424         {
425             if (GetLastError() == ERROR_IO_PENDING)
426             {
427                 trace("InternetReadFileEx -> PENDING\n");
428                 ok(bReceivingResponse, "INTERNET_STATUS_RECEIVING_RESPONSE should have been sent to callback function\n");
429                 WaitForSingleObject(hCompleteEvent, INFINITE);
430                 ok(!bResponseReceived, "INTERNET_STATUS_RESPONSE_RECEIVED should not have been sent to callback function\n");
431             }
432             else
433             {
434                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
435                 break;
436             }
437         }
438         else
439         {
440             trace("InternetReadFileEx -> SUCCEEDED\n");
441             ok(bReceivingResponse, "INTERNET_STATUS_RECEIVING_RESPONSE should have been sent to callback function\n");
442             ok(bResponseReceived, "INTERNET_STATUS_RESPONSE_RECEIVED should have been sent to callback function\n");
443         }
444
445         trace("read %i bytes\n", inetbuffers.dwBufferLength);
446         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
447
448         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
449             "InternetReadFileEx sets offsets to 0x%x%08x\n",
450             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
451
452         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
453
454         if (!inetbuffers.dwBufferLength)
455             break;
456
457         length += inetbuffers.dwBufferLength;
458     }
459     ok(length > 0, "failed to read any of the document\n");
460     trace("Finished. Read %d bytes\n", length);
461
462 abort:
463     if (hor) {
464         rc = InternetCloseHandle(hor);
465         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
466         rc = InternetCloseHandle(hor);
467         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
468     }
469     if (hic) {
470         rc = InternetCloseHandle(hic);
471         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
472     }
473     if (hi) {
474       rc = InternetCloseHandle(hi);
475       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
476       if (flags & INTERNET_FLAG_ASYNC)
477           Sleep(100);
478     }
479     CloseHandle(hCompleteEvent);
480 }
481
482 static void InternetOpenUrlA_test(void)
483 {
484   HINTERNET myhinternet, myhttp;
485   char buffer[0x400];
486   DWORD size, readbytes, totalbytes=0;
487   BOOL ret;
488   
489   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
490   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
491   size = 0x400;
492   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
493   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
494
495   SetLastError(0);
496   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
497                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
498   if (GetLastError() == 12007)
499     return; /* WinXP returns this when not connected to the net */
500   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
501   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
502   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
503   totalbytes += readbytes;
504   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
505     totalbytes += readbytes;
506   trace("read 0x%08x bytes\n",totalbytes);
507 }
508
509 static void InternetTimeFromSystemTimeA_test(void)
510 {
511     BOOL ret;
512     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
513     char string[INTERNET_RFC1123_BUFSIZE];
514     static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
515
516     ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
517     ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
518
519     ok( !memcmp( string, expect, sizeof(expect) ),
520         "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
521 }
522
523 static void InternetTimeFromSystemTimeW_test(void)
524 {
525     BOOL ret;
526     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
527     WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
528     static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
529                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
530
531     ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
532     ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
533
534     ok( !memcmp( string, expect, sizeof(expect) ),
535         "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
536 }
537
538 static void InternetTimeToSystemTimeA_test(void)
539 {
540     BOOL ret;
541     SYSTEMTIME time;
542     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
543     static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
544     static const char string2[] = " fri 7 jan 2005 12 06 35";
545
546     ret = pInternetTimeToSystemTimeA( string, &time, 0 );
547     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
548     ok( !memcmp( &time, &expect, sizeof(expect) ),
549         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
550
551     ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
552     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
553     ok( !memcmp( &time, &expect, sizeof(expect) ),
554         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
555 }
556
557 static void InternetTimeToSystemTimeW_test(void)
558 {
559     BOOL ret;
560     SYSTEMTIME time;
561     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
562     static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
563                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
564     static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
565                                      '1','2',' ','0','6',' ','3','5',0 };
566     static const WCHAR string3[] = { 'F','r',0 };
567
568     ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
569     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
570
571     ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
572     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
573
574     ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
575     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
576
577     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
578     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
579
580     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
581     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
582     ok( !memcmp( &time, &expect, sizeof(expect) ),
583         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
584
585     ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
586     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
587     ok( !memcmp( &time, &expect, sizeof(expect) ),
588         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
589
590     ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
591     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
592 }
593
594 static void HttpSendRequestEx_test(void)
595 {
596     HINTERNET hSession;
597     HINTERNET hConnect;
598     HINTERNET hRequest;
599
600     INTERNET_BUFFERS BufferIn;
601     DWORD dwBytesWritten;
602     DWORD dwBytesRead;
603     CHAR szBuffer[256];
604     int i;
605     BOOL ret;
606
607     static char szPostData[] = "mode=Test";
608     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
609
610     hSession = InternetOpen("Wine Regression Test",
611             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
612     ok( hSession != NULL ,"Unable to open Internet session\n");
613     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
614             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
615             0);
616     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
617     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
618             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
619     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
620     {
621         trace( "Network unreachable, skipping test\n" );
622         goto done;
623     }
624     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
625
626
627     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
628     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
629     BufferIn.lpcszHeader = szContentType;
630     BufferIn.dwHeadersLength = sizeof(szContentType);
631     BufferIn.dwHeadersTotal = sizeof(szContentType);
632     BufferIn.lpvBuffer = szPostData;
633     BufferIn.dwBufferLength = 3;
634     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
635     BufferIn.dwOffsetLow = 0;
636     BufferIn.dwOffsetHigh = 0;
637
638     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
639     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
640
641     for (i = 3; szPostData[i]; i++)
642         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
643                 "InternetWriteFile failed\n");
644
645     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
646
647     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
648             "Unable to read response\n");
649     szBuffer[dwBytesRead] = 0;
650
651     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
652     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
653
654     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
655 done:
656     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
657     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
658 }
659
660 static void InternetOpenRequest_test(void)
661 {
662     HINTERNET session, connect, request;
663     static const char *types[] = { "*", "", NULL };
664     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
665     static const WCHAR *typesW[] = { any, empty, NULL };
666     BOOL ret;
667
668     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
669     ok(session != NULL ,"Unable to open Internet session\n");
670
671     connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
672                               INTERNET_SERVICE_HTTP, 0, 0);
673     ok(connect != NULL, "Unable to connect to http://winehq.org\n");
674
675     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
676     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
677     {
678         trace( "Network unreachable, skipping test\n" );
679         goto done;
680     }
681     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
682
683     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
684     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
685     ok(InternetCloseHandle(request), "Close request handle failed\n");
686
687     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
688     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
689
690     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
691     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
692     ok(InternetCloseHandle(request), "Close request handle failed\n");
693
694 done:
695     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
696     ok(InternetCloseHandle(session), "Close session handle failed\n");
697 }
698
699 static void HttpHeaders_test(void)
700 {
701     HINTERNET hSession;
702     HINTERNET hConnect;
703     HINTERNET hRequest;
704     CHAR      buffer[256];
705     DWORD     len = 256;
706     DWORD     index = 0;
707
708     hSession = InternetOpen("Wine Regression Test",
709             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
710     ok( hSession != NULL ,"Unable to open Internet session\n");
711     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
712             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
713             0);
714     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
715     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
716             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
717     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
718     {
719         trace( "Network unreachable, skipping test\n" );
720         goto done;
721     }
722     ok( hRequest != NULL, "Failed to open request handle\n");
723
724     index = 0;
725     len = sizeof(buffer);
726     strcpy(buffer,"Warning");
727     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
728                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
729     
730     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
731             "Failed to add new header\n");
732
733     index = 0;
734     len = sizeof(buffer);
735     strcpy(buffer,"Warning");
736     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
737                 buffer,&len,&index),"Unable to query header\n");
738     ok(index == 1, "Index was not incremented\n");
739     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
740     len = sizeof(buffer);
741     strcpy(buffer,"Warning");
742     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
743                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
744
745     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
746             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
747
748     index = 0;
749     len = sizeof(buffer);
750     strcpy(buffer,"Warning");
751     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
752                 buffer,&len,&index),"Unable to query header\n");
753     ok(index == 1, "Index was not incremented\n");
754     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
755     len = sizeof(buffer);
756     strcpy(buffer,"Warning");
757     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
758                 buffer,&len,&index),"Failed to get second header\n");
759     ok(index == 2, "Index was not incremented\n");
760     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
761     len = sizeof(buffer);
762     strcpy(buffer,"Warning");
763     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
764                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
765
766     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
767
768     index = 0;
769     len = sizeof(buffer);
770     strcpy(buffer,"Warning");
771     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
772                 buffer,&len,&index),"Unable to query header\n");
773     ok(index == 1, "Index was not incremented\n");
774     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
775     len = sizeof(buffer);
776     strcpy(buffer,"Warning");
777     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
778                 buffer,&len,&index),"Failed to get second header\n");
779     ok(index == 2, "Index was not incremented\n");
780     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
781     len = sizeof(buffer);
782     strcpy(buffer,"Warning");
783     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
784                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
785     
786     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
787
788     index = 0;
789     len = sizeof(buffer);
790     strcpy(buffer,"Warning");
791     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
792                 buffer,&len,&index),"Unable to query header\n");
793     ok(index == 1, "Index was not incremented\n");
794     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
795     len = sizeof(buffer);
796     strcpy(buffer,"Warning");
797     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
798                 buffer,&len,&index),"Failed to get second header\n");
799     ok(index == 2, "Index was not incremented\n");
800     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
801     len = sizeof(buffer);
802     strcpy(buffer,"Warning");
803     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
804                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
805
806     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
807
808     index = 0;
809     len = sizeof(buffer);
810     strcpy(buffer,"Warning");
811     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
812                 buffer,&len,&index),"Unable to query header\n");
813     ok(index == 1, "Index was not incremented\n");
814     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
815     len = sizeof(buffer);
816     strcpy(buffer,"Warning");
817     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
818     ok(index == 2, "Index was not incremented\n");
819     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
820     len = sizeof(buffer);
821     strcpy(buffer,"Warning");
822     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
823
824     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
825
826     index = 0;
827     len = sizeof(buffer);
828     strcpy(buffer,"Warning");
829     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
830     ok(index == 1, "Index was not incremented\n");
831     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
832     len = sizeof(buffer);
833     strcpy(buffer,"Warning");
834     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
835     ok(index == 2, "Index was not incremented\n");
836     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
837     len = sizeof(buffer);
838     strcpy(buffer,"Warning");
839     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
840
841     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
842
843     index = 0;
844     len = sizeof(buffer);
845     strcpy(buffer,"Warning");
846     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
847     ok(index == 1, "Index was not incremented\n");
848     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
849     len = sizeof(buffer);
850     strcpy(buffer,"Warning");
851     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
852     ok(index == 2, "Index was not incremented\n");
853     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
854     len = sizeof(buffer);
855     strcpy(buffer,"Warning");
856     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
857
858     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");
859
860     index = 0;
861     len = sizeof(buffer);
862     strcpy(buffer,"Warning");
863     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
864     ok(index == 1, "Index was not incremented\n");
865     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
866     len = sizeof(buffer);
867     strcpy(buffer,"Warning");
868     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
869     ok(index == 2, "Index was not incremented\n");
870     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
871     len = sizeof(buffer);
872     strcpy(buffer,"Warning");
873     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
874
875     
876     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
877 done:
878     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
879     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
880 }
881
882 static const char okmsg[] =
883 "HTTP/1.0 200 OK\r\n"
884 "Server: winetest\r\n"
885 "\r\n";
886
887 static const char proxymsg[] =
888 "HTTP/1.1 407 Proxy Authentication Required\r\n"
889 "Server: winetest\r\n"
890 "Proxy-Connection: close\r\n"
891 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
892 "\r\n";
893
894 static const char page1[] =
895 "<HTML>\r\n"
896 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
897 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
898 "</HTML>\r\n\r\n";
899
900 struct server_info {
901     HANDLE hEvent;
902     int port;
903 };
904
905 static DWORD CALLBACK server_thread(LPVOID param)
906 {
907     struct server_info *si = param;
908     int r, s, c, i, on;
909     struct sockaddr_in sa;
910     char buffer[0x100];
911     WSADATA wsaData;
912     int last_request = 0;
913
914     WSAStartup(MAKEWORD(1,1), &wsaData);
915
916     s = socket(AF_INET, SOCK_STREAM, 0);
917     if (s<0)
918         return 1;
919
920     on = 1;
921     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
922
923     memset(&sa, 0, sizeof sa);
924     sa.sin_family = AF_INET;
925     sa.sin_port = htons(si->port);
926     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
927
928     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
929     if (r<0)
930         return 1;
931
932     listen(s, 0);
933
934     SetEvent(si->hEvent);
935
936     do
937     {
938         c = accept(s, NULL, NULL);
939
940         memset(buffer, 0, sizeof buffer);
941         for(i=0; i<(sizeof buffer-1); i++)
942         {
943             r = recv(c, &buffer[i], 1, 0);
944             if (r != 1)
945                 break;
946             if (i<4) continue;
947             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
948                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
949                 break;
950         }
951
952         if (strstr(buffer, "GET /test1"))
953         {
954             send(c, okmsg, sizeof okmsg-1, 0);
955             send(c, page1, sizeof page1-1, 0);
956         }
957
958         if (strstr(buffer, "/test2"))
959         {
960             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
961             {
962                 send(c, okmsg, sizeof okmsg-1, 0);
963                 send(c, page1, sizeof page1-1, 0);
964             }
965             else
966                 send(c, proxymsg, sizeof proxymsg-1, 0);
967         }
968
969         if (strstr(buffer, "/quit"))
970         {
971             send(c, okmsg, sizeof okmsg-1, 0);
972             send(c, page1, sizeof page1-1, 0);
973             last_request = 1;
974         }
975
976         shutdown(c, 2);
977         closesocket(c);
978     } while (!last_request);
979
980     closesocket(s);
981
982     return 0;
983 }
984
985 static void test_basic_request(int port, const char *url)
986 {
987     HINTERNET hi, hc, hr;
988     DWORD r, count;
989     char buffer[0x100];
990
991     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
992     ok(hi != NULL, "open failed\n");
993
994     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
995     ok(hc != NULL, "connect failed\n");
996
997     hr = HttpOpenRequest(hc, NULL, url, NULL, NULL, NULL, 0, 0);
998     ok(hr != NULL, "HttpOpenRequest failed\n");
999
1000     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1001     ok(r, "HttpSendRequest failed\n");
1002
1003     count = 0;
1004     memset(buffer, 0, sizeof buffer);
1005     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1006     ok(r, "InternetReadFile failed\n");
1007     ok(count == sizeof page1 - 1, "count was wrong\n");
1008     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1009
1010     InternetCloseHandle(hr);
1011     InternetCloseHandle(hc);
1012     InternetCloseHandle(hi);
1013 }
1014
1015 static void test_proxy_indirect(int port)
1016 {
1017     HINTERNET hi, hc, hr;
1018     DWORD r, sz, val;
1019     char buffer[0x40];
1020
1021     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1022     ok(hi != NULL, "open failed\n");
1023
1024     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1025     ok(hc != NULL, "connect failed\n");
1026
1027     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1028     ok(hr != NULL, "HttpOpenRequest failed\n");
1029
1030     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1031     ok(r, "HttpSendRequest failed\n");
1032
1033     sz = sizeof buffer;
1034     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1035     ok(r, "HttpQueryInfo failed\n");
1036     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1037
1038     sz = sizeof buffer;
1039     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1040     ok(r, "HttpQueryInfo failed\n");
1041     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1042
1043     sz = sizeof val;
1044     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1045     ok(r, "HttpQueryInfo failed\n");
1046     ok(val == 407, "proxy code wrong\n");
1047
1048     sz = sizeof buffer;
1049     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1050     ok(r, "HttpQueryInfo failed\n");
1051     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1052
1053     sz = sizeof buffer;
1054     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1055     ok(r, "HttpQueryInfo failed\n");
1056     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1057
1058     sz = sizeof buffer;
1059     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1060     ok(r, "HttpQueryInfo failed\n");
1061     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1062
1063     sz = sizeof buffer;
1064     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1065     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1066     ok(r == FALSE, "HttpQueryInfo failed\n");
1067
1068     InternetCloseHandle(hr);
1069     InternetCloseHandle(hc);
1070     InternetCloseHandle(hi);
1071 }
1072
1073 static void test_proxy_direct(int port)
1074 {
1075     HINTERNET hi, hc, hr;
1076     DWORD r, sz;
1077     char buffer[0x40];
1078     static CHAR username[] = "mike",
1079                 password[] = "1101";
1080
1081     sprintf(buffer, "localhost:%d\n", port);
1082     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1083     ok(hi != NULL, "open failed\n");
1084
1085     /* try connect without authorization */
1086     hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1087     ok(hc != NULL, "connect failed\n");
1088
1089     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1090     ok(hr != NULL, "HttpOpenRequest failed\n");
1091
1092     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1093     ok(r, "HttpSendRequest failed\n");
1094
1095     sz = sizeof buffer;
1096     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1097     ok(r, "HttpQueryInfo failed\n");
1098     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1099
1100
1101     /* set the user + password then try again */
1102     todo_wine {
1103     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1104     ok(r, "failed to set user\n");
1105
1106     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1107     ok(r, "failed to set password\n");
1108     }
1109
1110     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1111     ok(r, "HttpSendRequest failed\n");
1112     sz = sizeof buffer;
1113     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1114     ok(r, "HttpQueryInfo failed\n");
1115     todo_wine {
1116     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1117     }
1118
1119
1120     InternetCloseHandle(hr);
1121     InternetCloseHandle(hc);
1122     InternetCloseHandle(hi);
1123 }
1124
1125 static void test_http_connection(void)
1126 {
1127     struct server_info si;
1128     HANDLE hThread;
1129     DWORD id = 0, r;
1130
1131     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1132     si.port = 7531;
1133
1134     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1135     ok( hThread != NULL, "create thread failed\n");
1136
1137     r = WaitForSingleObject(si.hEvent, 10000);
1138     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1139     if (r != WAIT_OBJECT_0)
1140         return;
1141
1142     test_basic_request(si.port, "/test1");
1143     test_proxy_indirect(si.port);
1144     test_proxy_direct(si.port);
1145
1146     /* send the basic request again to shutdown the server thread */
1147     test_basic_request(si.port, "/quit");
1148
1149     r = WaitForSingleObject(hThread, 3000);
1150     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1151     CloseHandle(hThread);
1152 }
1153
1154 START_TEST(http)
1155 {
1156     HMODULE hdll;
1157     hdll = GetModuleHandleA("wininet.dll");
1158     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
1159     pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
1160     pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
1161     pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
1162     pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
1163
1164     if (!pInternetSetStatusCallbackA)
1165         skip("skipping the InternetReadFile tests\n");
1166     else
1167     {
1168         InternetReadFile_test(INTERNET_FLAG_ASYNC);
1169         InternetReadFile_test(0);
1170         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
1171     }
1172     InternetOpenRequest_test();
1173     InternetOpenUrlA_test();
1174     if (!pInternetTimeFromSystemTimeA)
1175         skip("skipping the InternetTime tests\n");
1176     else
1177     {
1178         InternetTimeFromSystemTimeA_test();
1179         InternetTimeFromSystemTimeW_test();
1180         InternetTimeToSystemTimeA_test();
1181         InternetTimeToSystemTimeW_test();
1182     }
1183     HttpSendRequestEx_test();
1184     HttpHeaders_test();
1185     test_http_connection();
1186 }