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