reshuffling of dlls
[reactos.git] / reactos / dll / win32 / wininet / internet.c
1 /*
2 * Wininet
3 *
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
9 *
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #define MAXHOSTNAME 100 /* from http.c */
33
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #endif
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
43 #endif
44 #include <stdlib.h>
45 #include <ctype.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <assert.h>
50
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winreg.h"
54 #include "winuser.h"
55 #include "wininet.h"
56 #include "winnls.h"
57 #include "wine/debug.h"
58 #include "winerror.h"
59 #define NO_SHLWAPI_STREAM
60 #include "shlwapi.h"
61
62 #include "wine/exception.h"
63 #include "excpt.h"
64
65 #include "internet.h"
66 #include "resource.h"
67
68 #include "wine/unicode.h"
69
70 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
71
72 #define MAX_IDLE_WORKER 1000*60*1
73 #define MAX_WORKER_THREADS 10
74 #define RESPONSE_TIMEOUT 30
75
76 #define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
77 (LPWININETAPPINFOW)(((LPWININETFTPSESSIONW)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)
78
79
80 typedef struct
81 {
82 DWORD dwError;
83 CHAR response[MAX_REPLY_LEN];
84 } WITHREADERROR, *LPWITHREADERROR;
85
86 static VOID INTERNET_CloseHandle(LPWININETHANDLEHEADER hdr);
87 BOOL WINAPI INTERNET_FindNextFileW(LPWININETFINDNEXTW lpwh, LPVOID lpvFindData);
88 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
89 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext);
90 static VOID INTERNET_ExecuteWork(void);
91
92 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
93 static LONG dwNumThreads;
94 static LONG dwNumIdleThreads;
95 static LONG dwNumJobs;
96 static HANDLE hEventArray[2];
97 #define hQuitEvent hEventArray[0]
98 #define hWorkEvent hEventArray[1]
99 static CRITICAL_SECTION csQueue;
100 static LPWORKREQUEST lpHeadWorkQueue;
101 static LPWORKREQUEST lpWorkQueueTail;
102 static HMODULE WININET_hModule;
103
104 #define HANDLE_CHUNK_SIZE 0x10
105
106 static CRITICAL_SECTION WININET_cs;
107 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
108 {
109 0, 0, &WININET_cs,
110 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
111 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
112 };
113 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
114
115 static LPWININETHANDLEHEADER *WININET_Handles;
116 static UINT WININET_dwNextHandle;
117 static UINT WININET_dwMaxHandles;
118
119 HINTERNET WININET_AllocHandle( LPWININETHANDLEHEADER info )
120 {
121 LPWININETHANDLEHEADER *p;
122 UINT handle = 0, num;
123
124 EnterCriticalSection( &WININET_cs );
125 if( !WININET_dwMaxHandles )
126 {
127 num = HANDLE_CHUNK_SIZE;
128 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
129 sizeof (UINT)* num);
130 if( !p )
131 goto end;
132 WININET_Handles = p;
133 WININET_dwMaxHandles = num;
134 }
135 if( WININET_dwMaxHandles == WININET_dwNextHandle )
136 {
137 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
138 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
139 WININET_Handles, sizeof (UINT)* num);
140 if( !p )
141 goto end;
142 WININET_Handles = p;
143 WININET_dwMaxHandles = num;
144 }
145
146 handle = WININET_dwNextHandle;
147 if( WININET_Handles[handle] )
148 ERR("handle isn't free but should be\n");
149 WININET_Handles[handle] = WININET_AddRef( info );
150
151 while( WININET_Handles[WININET_dwNextHandle] &&
152 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
153 WININET_dwNextHandle++;
154
155 end:
156 LeaveCriticalSection( &WININET_cs );
157
158 return (HINTERNET) (handle+1);
159 }
160
161 HINTERNET WININET_FindHandle( LPWININETHANDLEHEADER info )
162 {
163 UINT i, handle = 0;
164
165 EnterCriticalSection( &WININET_cs );
166 for( i=0; i<WININET_dwMaxHandles; i++ )
167 {
168 if( info == WININET_Handles[i] )
169 {
170 WININET_AddRef( info );
171 handle = i+1;
172 break;
173 }
174 }
175 LeaveCriticalSection( &WININET_cs );
176
177 return (HINTERNET) handle;
178 }
179
180 LPWININETHANDLEHEADER WININET_AddRef( LPWININETHANDLEHEADER info )
181 {
182 info->dwRefCount++;
183 TRACE("%p -> refcount = %ld\n", info, info->dwRefCount );
184 return info;
185 }
186
187 LPWININETHANDLEHEADER WININET_GetObject( HINTERNET hinternet )
188 {
189 LPWININETHANDLEHEADER info = NULL;
190 UINT handle = (UINT) hinternet;
191
192 EnterCriticalSection( &WININET_cs );
193
194 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
195 WININET_Handles[handle-1] )
196 info = WININET_AddRef( WININET_Handles[handle-1] );
197
198 LeaveCriticalSection( &WININET_cs );
199
200 TRACE("handle %d -> %p\n", handle, info);
201
202 return info;
203 }
204
205 BOOL WININET_Release( LPWININETHANDLEHEADER info )
206 {
207 info->dwRefCount--;
208 TRACE( "object %p refcount = %ld\n", info, info->dwRefCount );
209 if( !info->dwRefCount )
210 {
211 TRACE( "destroying object %p\n", info);
212 info->destroy( info );
213 }
214 return TRUE;
215 }
216
217 BOOL WININET_FreeHandle( HINTERNET hinternet )
218 {
219 BOOL ret = FALSE;
220 UINT handle = (UINT) hinternet;
221 LPWININETHANDLEHEADER info = NULL;
222
223 EnterCriticalSection( &WININET_cs );
224
225 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
226 {
227 handle--;
228 if( WININET_Handles[handle] )
229 {
230 info = WININET_Handles[handle];
231 TRACE( "destroying handle %d for object %p\n", handle+1, info);
232 WININET_Handles[handle] = NULL;
233 ret = TRUE;
234 if( WININET_dwNextHandle > handle )
235 WININET_dwNextHandle = handle;
236 }
237 }
238
239 LeaveCriticalSection( &WININET_cs );
240
241 if( info )
242 WININET_Release( info );
243
244 return ret;
245 }
246
247 /***********************************************************************
248 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
249 *
250 * PARAMS
251 * hinstDLL [I] handle to the DLL's instance
252 * fdwReason [I]
253 * lpvReserved [I] reserved, must be NULL
254 *
255 * RETURNS
256 * Success: TRUE
257 * Failure: FALSE
258 */
259
260 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
261 {
262 WSADATA wsaData;
263
264 TRACE("%p,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
265
266 switch (fdwReason) {
267 case DLL_PROCESS_ATTACH:
268
269 if (0 != WSAStartup(MAKEWORD(1, 1), &wsaData)) {
270 return FALSE;
271 }
272
273 g_dwTlsErrIndex = TlsAlloc();
274
275 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES) {
276 WSACleanup();
277 return FALSE;
278 }
279
280 hQuitEvent = CreateEventW(0, TRUE, FALSE, NULL);
281 hWorkEvent = CreateEventW(0, FALSE, FALSE, NULL);
282 InitializeCriticalSection(&csQueue);
283
284 URLCacheContainers_CreateDefaults();
285
286 dwNumThreads = 0;
287 dwNumIdleThreads = 0;
288 dwNumJobs = 0;
289
290 WININET_hModule = (HMODULE)hinstDLL;
291
292 case DLL_THREAD_ATTACH:
293 {
294 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
295 if (NULL == lpwite)
296 return FALSE;
297
298 TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
299 }
300 break;
301
302 case DLL_THREAD_DETACH:
303 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
304 {
305 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
306 HeapFree(GetProcessHeap(), 0, lpwite);
307 }
308 break;
309
310 case DLL_PROCESS_DETACH:
311
312 URLCacheContainers_DeleteAll();
313
314 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
315 {
316 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
317 TlsFree(g_dwTlsErrIndex);
318 }
319
320 SetEvent(hQuitEvent);
321
322 CloseHandle(hQuitEvent);
323 CloseHandle(hWorkEvent);
324 DeleteCriticalSection(&csQueue);
325 WSACleanup();
326 break;
327 }
328
329 return TRUE;
330 }
331
332
333 /***********************************************************************
334 * InternetInitializeAutoProxyDll (WININET.@)
335 *
336 * Setup the internal proxy
337 *
338 * PARAMETERS
339 * dwReserved
340 *
341 * RETURNS
342 * FALSE on failure
343 *
344 */
345 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
346 {
347 FIXME("STUB\n");
348 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
349 return FALSE;
350 }
351
352 /***********************************************************************
353 * DetectAutoProxyUrl (WININET.@)
354 *
355 * Auto detect the proxy url
356 *
357 * RETURNS
358 * FALSE on failure
359 *
360 */
361 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
362 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
363 {
364 FIXME("STUB\n");
365 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
366 return FALSE;
367 }
368
369
370 /***********************************************************************
371 * INTERNET_ConfigureProxyFromReg
372 *
373 * FIXME:
374 * The proxy may be specified in the form 'http=proxy.my.org'
375 * Presumably that means there can be ftp=ftpproxy.my.org too.
376 */
377 static BOOL INTERNET_ConfigureProxyFromReg( LPWININETAPPINFOW lpwai )
378 {
379 HKEY key;
380 DWORD r, keytype, len, enabled;
381 LPCSTR lpszInternetSettings =
382 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
383 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
384
385 r = RegOpenKeyA(HKEY_CURRENT_USER, lpszInternetSettings, &key);
386 if ( r != ERROR_SUCCESS )
387 return FALSE;
388
389 len = sizeof enabled;
390 r = RegQueryValueExA( key, "ProxyEnable", NULL, &keytype,
391 (BYTE*)&enabled, &len);
392 if( (r == ERROR_SUCCESS) && enabled )
393 {
394 TRACE("Proxy is enabled.\n");
395
396 /* figure out how much memory the proxy setting takes */
397 r = RegQueryValueExW( key, szProxyServer, NULL, &keytype,
398 NULL, &len);
399 if( (r == ERROR_SUCCESS) && len && (keytype == REG_SZ) )
400 {
401 LPWSTR szProxy, p;
402 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
403
404 szProxy=HeapAlloc( GetProcessHeap(), 0, len );
405 RegQueryValueExW( key, szProxyServer, NULL, &keytype,
406 (BYTE*)szProxy, &len);
407
408 /* find the http proxy, and strip away everything else */
409 p = strstrW( szProxy, szHttp );
410 if( p )
411 {
412 p += lstrlenW(szHttp);
413 lstrcpyW( szProxy, p );
414 }
415 p = strchrW( szProxy, ' ' );
416 if( p )
417 *p = 0;
418
419 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
420 lpwai->lpszProxy = szProxy;
421
422 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
423 }
424 else
425 ERR("Couldn't read proxy server settings.\n");
426 }
427 else
428 TRACE("Proxy is not enabled.\n");
429 RegCloseKey(key);
430
431 return enabled;
432 }
433
434 /***********************************************************************
435 * dump_INTERNET_FLAGS
436 *
437 * Helper function to TRACE the internet flags.
438 *
439 * RETURNS
440 * None
441 *
442 */
443 static void dump_INTERNET_FLAGS(DWORD dwFlags)
444 {
445 #define FE(x) { x, #x }
446 static const wininet_flag_info flag[] = {
447 FE(INTERNET_FLAG_RELOAD),
448 FE(INTERNET_FLAG_RAW_DATA),
449 FE(INTERNET_FLAG_EXISTING_CONNECT),
450 FE(INTERNET_FLAG_ASYNC),
451 FE(INTERNET_FLAG_PASSIVE),
452 FE(INTERNET_FLAG_NO_CACHE_WRITE),
453 FE(INTERNET_FLAG_MAKE_PERSISTENT),
454 FE(INTERNET_FLAG_FROM_CACHE),
455 FE(INTERNET_FLAG_SECURE),
456 FE(INTERNET_FLAG_KEEP_CONNECTION),
457 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
458 FE(INTERNET_FLAG_READ_PREFETCH),
459 FE(INTERNET_FLAG_NO_COOKIES),
460 FE(INTERNET_FLAG_NO_AUTH),
461 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
462 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
463 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
464 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
465 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
466 FE(INTERNET_FLAG_RESYNCHRONIZE),
467 FE(INTERNET_FLAG_HYPERLINK),
468 FE(INTERNET_FLAG_NO_UI),
469 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
470 FE(INTERNET_FLAG_CACHE_ASYNC),
471 FE(INTERNET_FLAG_FORMS_SUBMIT),
472 FE(INTERNET_FLAG_NEED_FILE),
473 FE(INTERNET_FLAG_TRANSFER_ASCII),
474 FE(INTERNET_FLAG_TRANSFER_BINARY)
475 };
476 #undef FE
477 int i;
478
479 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
480 if (flag[i].val & dwFlags) {
481 TRACE(" %s", flag[i].name);
482 dwFlags &= ~flag[i].val;
483 }
484 }
485 if (dwFlags)
486 TRACE(" Unknown flags (%08lx)\n", dwFlags);
487 else
488 TRACE("\n");
489 }
490
491 /***********************************************************************
492 * InternetOpenW (WININET.@)
493 *
494 * Per-application initialization of wininet
495 *
496 * RETURNS
497 * HINTERNET on success
498 * NULL on failure
499 *
500 */
501 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
502 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
503 {
504 LPWININETAPPINFOW lpwai = NULL;
505 HINTERNET handle = NULL;
506
507 if (TRACE_ON(wininet)) {
508 #define FE(x) { x, #x }
509 static const wininet_flag_info access_type[] = {
510 FE(INTERNET_OPEN_TYPE_PRECONFIG),
511 FE(INTERNET_OPEN_TYPE_DIRECT),
512 FE(INTERNET_OPEN_TYPE_PROXY),
513 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
514 };
515 #undef FE
516 DWORD i;
517 const char *access_type_str = "Unknown";
518
519 TRACE("(%s, %li, %s, %s, %li)\n", debugstr_w(lpszAgent), dwAccessType,
520 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
521 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
522 if (access_type[i].val == dwAccessType) {
523 access_type_str = access_type[i].name;
524 break;
525 }
526 }
527 TRACE(" access type : %s\n", access_type_str);
528 TRACE(" flags :");
529 dump_INTERNET_FLAGS(dwFlags);
530 }
531
532 /* Clear any error information */
533 INTERNET_SetLastError(0);
534
535 lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOW));
536 if (NULL == lpwai)
537 {
538 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
539 goto lend;
540 }
541
542 memset(lpwai, 0, sizeof(WININETAPPINFOW));
543 lpwai->hdr.htype = WH_HINIT;
544 lpwai->hdr.lpwhparent = NULL;
545 lpwai->hdr.dwFlags = dwFlags;
546 lpwai->hdr.dwRefCount = 1;
547 lpwai->hdr.destroy = INTERNET_CloseHandle;
548 lpwai->dwAccessType = dwAccessType;
549 lpwai->lpszProxyUsername = NULL;
550 lpwai->lpszProxyPassword = NULL;
551
552 handle = WININET_AllocHandle( &lpwai->hdr );
553 if( !handle )
554 {
555 HeapFree( GetProcessHeap(), 0, lpwai );
556 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
557 goto lend;
558 }
559
560 if (NULL != lpszAgent)
561 {
562 lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,
563 (strlenW(lpszAgent)+1)*sizeof(WCHAR));
564 if (lpwai->lpszAgent)
565 lstrcpyW( lpwai->lpszAgent, lpszAgent );
566 }
567 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
568 INTERNET_ConfigureProxyFromReg( lpwai );
569 else if (NULL != lpszProxy)
570 {
571 lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0,
572 (strlenW(lpszProxy)+1)*sizeof(WCHAR));
573 if (lpwai->lpszProxy)
574 lstrcpyW( lpwai->lpszProxy, lpszProxy );
575 }
576
577 if (NULL != lpszProxyBypass)
578 {
579 lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0,
580 (strlenW(lpszProxyBypass)+1)*sizeof(WCHAR));
581 if (lpwai->lpszProxyBypass)
582 lstrcpyW( lpwai->lpszProxyBypass, lpszProxyBypass );
583 }
584
585 lend:
586 if( lpwai )
587 WININET_Release( &lpwai->hdr );
588
589 TRACE("returning %p\n", lpwai);
590
591 return handle;
592 }
593
594
595 /***********************************************************************
596 * InternetOpenA (WININET.@)
597 *
598 * Per-application initialization of wininet
599 *
600 * RETURNS
601 * HINTERNET on success
602 * NULL on failure
603 *
604 */
605 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
606 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
607 {
608 HINTERNET rc = (HINTERNET)NULL;
609 INT len;
610 WCHAR *szAgent = NULL, *szProxy = NULL, *szBypass = NULL;
611
612 TRACE("(%s, 0x%08lx, %s, %s, 0x%08lx)\n", debugstr_a(lpszAgent),
613 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
614
615 if( lpszAgent )
616 {
617 len = MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, NULL, 0);
618 szAgent = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
619 MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, szAgent, len);
620 }
621
622 if( lpszProxy )
623 {
624 len = MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, NULL, 0);
625 szProxy = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
626 MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, szProxy, len);
627 }
628
629 if( lpszProxyBypass )
630 {
631 len = MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, NULL, 0);
632 szBypass = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
633 MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, szBypass, len);
634 }
635
636 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
637
638 HeapFree(GetProcessHeap(), 0, szAgent);
639 HeapFree(GetProcessHeap(), 0, szProxy);
640 HeapFree(GetProcessHeap(), 0, szBypass);
641
642 return rc;
643 }
644
645 /***********************************************************************
646 * InternetGetLastResponseInfoA (WININET.@)
647 *
648 * Return last wininet error description on the calling thread
649 *
650 * RETURNS
651 * TRUE on success of writing to buffer
652 * FALSE on failure
653 *
654 */
655 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
656 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
657 {
658 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
659
660 TRACE("\n");
661
662 *lpdwError = lpwite->dwError;
663 if (lpwite->dwError)
664 {
665 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
666 *lpdwBufferLength = strlen(lpszBuffer);
667 }
668 else
669 *lpdwBufferLength = 0;
670
671 return TRUE;
672 }
673
674 /***********************************************************************
675 * InternetGetLastResponseInfoW (WININET.@)
676 *
677 * Return last wininet error description on the calling thread
678 *
679 * RETURNS
680 * TRUE on success of writing to buffer
681 * FALSE on failure
682 *
683 */
684 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
685 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
686 {
687 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
688
689 TRACE("\n");
690
691 *lpdwError = lpwite->dwError;
692 if (lpwite->dwError)
693 {
694 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
695 *lpdwBufferLength = lstrlenW(lpszBuffer);
696 }
697 else
698 *lpdwBufferLength = 0;
699
700 return TRUE;
701 }
702
703 /***********************************************************************
704 * InternetGetConnectedState (WININET.@)
705 *
706 * Return connected state
707 *
708 * RETURNS
709 * TRUE if connected
710 * if lpdwStatus is not null, return the status (off line,
711 * modem, lan...) in it.
712 * FALSE if not connected
713 */
714 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
715 {
716 TRACE("(%p, 0x%08lx)\n", lpdwStatus, dwReserved);
717
718 if (lpdwStatus) {
719 FIXME("always returning LAN connection.\n");
720 *lpdwStatus = INTERNET_CONNECTION_LAN;
721 }
722 return TRUE;
723 }
724
725
726 /***********************************************************************
727 * InternetGetConnectedStateExW (WININET.@)
728 *
729 * Return connected state
730 *
731 * PARAMS
732 *
733 * lpdwStatus [O] Flags specifying the status of the internet connection.
734 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
735 * dwNameLen [I] Size of the buffer, in characters.
736 * dwReserved [I] Reserved. Must be set to 0.
737 *
738 * RETURNS
739 * TRUE if connected
740 * if lpdwStatus is not null, return the status (off line,
741 * modem, lan...) in it.
742 * FALSE if not connected
743 *
744 * NOTES
745 * If the system has no available network connections, an empty string is
746 * stored in lpszConnectionName. If there is a LAN connection, a localized
747 * "LAN Connection" string is stored. Presumably, if only a dial-up
748 * connection is available then the name of the dial-up connection is
749 * returned. Why any application, other than the "Internet Settings" CPL,
750 * would want to use this function instead of the simpler InternetGetConnectedStateW
751 * function is beyond me.
752 */
753 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
754 DWORD dwNameLen, DWORD dwReserved)
755 {
756 TRACE("(%p, %p, %ld, 0x%08lx)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
757
758 /* Must be zero */
759 if(dwReserved)
760 return FALSE;
761
762 if (lpdwStatus) {
763 FIXME("always returning LAN connection.\n");
764 *lpdwStatus = INTERNET_CONNECTION_LAN;
765 }
766 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
767 }
768
769
770 /***********************************************************************
771 * InternetGetConnectedStateExA (WININET.@)
772 */
773 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
774 DWORD dwNameLen, DWORD dwReserved)
775 {
776 LPWSTR lpwszConnectionName = NULL;
777 BOOL rc;
778
779 TRACE("(%p, %p, %ld, 0x%08lx)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
780
781 if (lpszConnectionName && dwNameLen > 0)
782 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
783
784 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
785 dwReserved);
786 if (rc && lpwszConnectionName)
787 {
788 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
789 dwNameLen, NULL, NULL);
790
791 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
792 }
793
794 return rc;
795 }
796
797
798 /***********************************************************************
799 * InternetConnectW (WININET.@)
800 *
801 * Open a ftp, gopher or http session
802 *
803 * RETURNS
804 * HINTERNET a session handle on success
805 * NULL on failure
806 *
807 */
808 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
809 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
810 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
811 DWORD dwService, DWORD dwFlags, DWORD dwContext)
812 {
813 LPWININETAPPINFOW hIC;
814 HINTERNET rc = (HINTERNET) NULL;
815
816 TRACE("(%p, %s, %i, %s, %s, %li, %li, %li)\n", hInternet, debugstr_w(lpszServerName),
817 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
818 dwService, dwFlags, dwContext);
819
820 /* Clear any error information */
821 INTERNET_SetLastError(0);
822 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
823 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
824 goto lend;
825
826 switch (dwService)
827 {
828 case INTERNET_SERVICE_FTP:
829 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
830 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
831 break;
832
833 case INTERNET_SERVICE_HTTP:
834 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
835 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
836 break;
837
838 case INTERNET_SERVICE_GOPHER:
839 default:
840 break;
841 }
842 lend:
843 if( hIC )
844 WININET_Release( &hIC->hdr );
845
846 TRACE("returning %p\n", rc);
847 return rc;
848 }
849
850
851 /***********************************************************************
852 * InternetConnectA (WININET.@)
853 *
854 * Open a ftp, gopher or http session
855 *
856 * RETURNS
857 * HINTERNET a session handle on success
858 * NULL on failure
859 *
860 */
861 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
862 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
863 LPCSTR lpszUserName, LPCSTR lpszPassword,
864 DWORD dwService, DWORD dwFlags, DWORD dwContext)
865 {
866 HINTERNET rc = (HINTERNET)NULL;
867 INT len = 0;
868 LPWSTR szServerName = NULL;
869 LPWSTR szUserName = NULL;
870 LPWSTR szPassword = NULL;
871
872 if (lpszServerName)
873 {
874 len = MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, NULL, 0);
875 szServerName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
876 MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, szServerName, len);
877 }
878 if (lpszUserName)
879 {
880 len = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
881 szUserName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
882 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, szUserName, len);
883 }
884 if (lpszPassword)
885 {
886 len = MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, NULL, 0);
887 szPassword = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
888 MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, szPassword, len);
889 }
890
891
892 rc = InternetConnectW(hInternet, szServerName, nServerPort,
893 szUserName, szPassword, dwService, dwFlags, dwContext);
894
895 HeapFree(GetProcessHeap(), 0, szServerName);
896 HeapFree(GetProcessHeap(), 0, szUserName);
897 HeapFree(GetProcessHeap(), 0, szPassword);
898 return rc;
899 }
900
901
902 /***********************************************************************
903 * InternetFindNextFileA (WININET.@)
904 *
905 * Continues a file search from a previous call to FindFirstFile
906 *
907 * RETURNS
908 * TRUE on success
909 * FALSE on failure
910 *
911 */
912 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
913 {
914 BOOL ret;
915 WIN32_FIND_DATAW fd;
916
917 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
918 if(lpvFindData)
919 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
920 return ret;
921 }
922
923 /***********************************************************************
924 * InternetFindNextFileW (WININET.@)
925 *
926 * Continues a file search from a previous call to FindFirstFile
927 *
928 * RETURNS
929 * TRUE on success
930 * FALSE on failure
931 *
932 */
933 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
934 {
935 LPWININETAPPINFOW hIC = NULL;
936 LPWININETFINDNEXTW lpwh;
937 BOOL bSuccess = FALSE;
938
939 TRACE("\n");
940
941 lpwh = (LPWININETFINDNEXTW) WININET_GetObject( hFind );
942 if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
943 {
944 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
945 goto lend;
946 }
947
948 hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
949 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
950 {
951 WORKREQUEST workRequest;
952 struct WORKREQ_INTERNETFINDNEXTW *req;
953
954 workRequest.asyncall = INTERNETFINDNEXTW;
955 workRequest.hdr = WININET_AddRef( &lpwh->hdr );
956 req = &workRequest.u.InternetFindNextW;
957 req->lpFindFileData = lpvFindData;
958
959 bSuccess = INTERNET_AsyncCall(&workRequest);
960 }
961 else
962 {
963 bSuccess = INTERNET_FindNextFileW(lpwh, lpvFindData);
964 }
965 lend:
966 if( lpwh )
967 WININET_Release( &lpwh->hdr );
968 return bSuccess;
969 }
970
971 /***********************************************************************
972 * INTERNET_FindNextFileW (Internal)
973 *
974 * Continues a file search from a previous call to FindFirstFile
975 *
976 * RETURNS
977 * TRUE on success
978 * FALSE on failure
979 *
980 */
981 BOOL WINAPI INTERNET_FindNextFileW(LPWININETFINDNEXTW lpwh, LPVOID lpvFindData)
982 {
983 BOOL bSuccess = TRUE;
984 LPWIN32_FIND_DATAW lpFindFileData;
985
986 TRACE("\n");
987
988 assert (lpwh->hdr.htype == WH_HFINDNEXT);
989
990 /* Clear any error information */
991 INTERNET_SetLastError(0);
992
993 if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
994 {
995 FIXME("Only FTP find next supported\n");
996 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
997 return FALSE;
998 }
999
1000 TRACE("index(%ld) size(%ld)\n", lpwh->index, lpwh->size);
1001
1002 lpFindFileData = (LPWIN32_FIND_DATAW) lpvFindData;
1003 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
1004
1005 if (lpwh->index >= lpwh->size)
1006 {
1007 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
1008 bSuccess = FALSE;
1009 goto lend;
1010 }
1011
1012 FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
1013 lpwh->index++;
1014
1015 TRACE("\nName: %s\nSize: %ld\n", debugstr_w(lpFindFileData->cFileName), lpFindFileData->nFileSizeLow);
1016
1017 lend:
1018
1019 if (lpwh->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1020 {
1021 INTERNET_ASYNC_RESULT iar;
1022
1023 iar.dwResult = (DWORD)bSuccess;
1024 iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
1025 INTERNET_GetLastError();
1026
1027 INTERNET_SendCallback(&lpwh->hdr, lpwh->hdr.dwContext,
1028 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1029 sizeof(INTERNET_ASYNC_RESULT));
1030 }
1031
1032 return bSuccess;
1033 }
1034
1035
1036 /***********************************************************************
1037 * INTERNET_CloseHandle (internal)
1038 *
1039 * Close internet handle
1040 *
1041 * RETURNS
1042 * Void
1043 *
1044 */
1045 static VOID INTERNET_CloseHandle(LPWININETHANDLEHEADER hdr)
1046 {
1047 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW) hdr;
1048
1049 TRACE("%p\n",lpwai);
1050
1051 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
1052 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
1053 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
1054 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
1055 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
1056 HeapFree(GetProcessHeap(), 0, lpwai);
1057 }
1058
1059
1060 /***********************************************************************
1061 * InternetCloseHandle (WININET.@)
1062 *
1063 * Generic close handle function
1064 *
1065 * RETURNS
1066 * TRUE on success
1067 * FALSE on failure
1068 *
1069 */
1070 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1071 {
1072 LPWININETHANDLEHEADER lpwh;
1073
1074 TRACE("%p\n",hInternet);
1075
1076 lpwh = WININET_GetObject( hInternet );
1077 if (NULL == lpwh)
1078 {
1079 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1080 return FALSE;
1081 }
1082
1083 /* FIXME: native appears to send this from the equivalent of
1084 * WININET_Release */
1085 INTERNET_SendCallback(lpwh, lpwh->dwContext,
1086 INTERNET_STATUS_HANDLE_CLOSING, &hInternet,
1087 sizeof(HINTERNET));
1088
1089 if( lpwh->lpwhparent )
1090 WININET_Release( lpwh->lpwhparent );
1091 WININET_FreeHandle( hInternet );
1092 WININET_Release( lpwh );
1093
1094 return TRUE;
1095 }
1096
1097
1098 /***********************************************************************
1099 * ConvertUrlComponentValue (Internal)
1100 *
1101 * Helper function for InternetCrackUrlW
1102 *
1103 */
1104 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1105 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1106 LPCSTR lpszStart, LPCWSTR lpwszStart)
1107 {
1108 TRACE("%p %p %p %ld %p %p\n", lppszComponent, dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1109 if (*dwComponentLen != 0)
1110 {
1111 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1112 if (*lppszComponent == NULL)
1113 {
1114 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1115 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1116 *dwComponentLen = nASCIILength;
1117 }
1118 else
1119 {
1120 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1121 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1122 (*lppszComponent)[ncpylen]=0;
1123 *dwComponentLen = ncpylen;
1124 }
1125 }
1126 }
1127
1128
1129 /***********************************************************************
1130 * InternetCrackUrlA (WININET.@)
1131 *
1132 * Break up URL into its components
1133 *
1134 * TODO: Handle dwFlags
1135 *
1136 * RETURNS
1137 * TRUE on success
1138 * FALSE on failure
1139 *
1140 */
1141 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1142 LPURL_COMPONENTSA lpUrlComponents)
1143 {
1144 DWORD nLength;
1145 URL_COMPONENTSW UCW;
1146 WCHAR* lpwszUrl;
1147
1148 TRACE("(%s %lu %lx %p)\n", debugstr_a(lpszUrl), dwUrlLength, dwFlags, lpUrlComponents);
1149 if(dwUrlLength<=0)
1150 dwUrlLength=-1;
1151 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1152
1153 /* if dwUrlLength=-1 then nLength includes null but length to
1154 InternetCrackUrlW should not include it */
1155 if (dwUrlLength == -1) nLength--;
1156
1157 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1158 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1159
1160 memset(&UCW,0,sizeof(UCW));
1161 if(lpUrlComponents->dwHostNameLength!=0)
1162 UCW.dwHostNameLength= lpUrlComponents->dwHostNameLength;
1163 if(lpUrlComponents->dwUserNameLength!=0)
1164 UCW.dwUserNameLength=lpUrlComponents->dwUserNameLength;
1165 if(lpUrlComponents->dwPasswordLength!=0)
1166 UCW.dwPasswordLength=lpUrlComponents->dwPasswordLength;
1167 if(lpUrlComponents->dwUrlPathLength!=0)
1168 UCW.dwUrlPathLength=lpUrlComponents->dwUrlPathLength;
1169 if(lpUrlComponents->dwSchemeLength!=0)
1170 UCW.dwSchemeLength=lpUrlComponents->dwSchemeLength;
1171 if(lpUrlComponents->dwExtraInfoLength!=0)
1172 UCW.dwExtraInfoLength=lpUrlComponents->dwExtraInfoLength;
1173 if(!InternetCrackUrlW(lpwszUrl,nLength,dwFlags,&UCW))
1174 {
1175 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1176 return FALSE;
1177 }
1178
1179 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1180 UCW.lpszHostName, UCW.dwHostNameLength,
1181 lpszUrl, lpwszUrl);
1182 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1183 UCW.lpszUserName, UCW.dwUserNameLength,
1184 lpszUrl, lpwszUrl);
1185 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1186 UCW.lpszPassword, UCW.dwPasswordLength,
1187 lpszUrl, lpwszUrl);
1188 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1189 UCW.lpszUrlPath, UCW.dwUrlPathLength,
1190 lpszUrl, lpwszUrl);
1191 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1192 UCW.lpszScheme, UCW.dwSchemeLength,
1193 lpszUrl, lpwszUrl);
1194 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1195 UCW.lpszExtraInfo, UCW.dwExtraInfoLength,
1196 lpszUrl, lpwszUrl);
1197 lpUrlComponents->nScheme=UCW.nScheme;
1198 lpUrlComponents->nPort=UCW.nPort;
1199 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1200
1201 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1202 debugstr_an(lpUrlComponents->lpszScheme,lpUrlComponents->dwSchemeLength),
1203 debugstr_an(lpUrlComponents->lpszHostName,lpUrlComponents->dwHostNameLength),
1204 debugstr_an(lpUrlComponents->lpszUrlPath,lpUrlComponents->dwUrlPathLength),
1205 debugstr_an(lpUrlComponents->lpszExtraInfo,lpUrlComponents->dwExtraInfoLength));
1206
1207 return TRUE;
1208 }
1209
1210 /***********************************************************************
1211 * GetInternetSchemeW (internal)
1212 *
1213 * Get scheme of url
1214 *
1215 * RETURNS
1216 * scheme on success
1217 * INTERNET_SCHEME_UNKNOWN on failure
1218 *
1219 */
1220 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1221 {
1222 INTERNET_SCHEME iScheme=INTERNET_SCHEME_UNKNOWN;
1223 static const WCHAR lpszFtp[]={'f','t','p',0};
1224 static const WCHAR lpszGopher[]={'g','o','p','h','e','r',0};
1225 static const WCHAR lpszHttp[]={'h','t','t','p',0};
1226 static const WCHAR lpszHttps[]={'h','t','t','p','s',0};
1227 static const WCHAR lpszFile[]={'f','i','l','e',0};
1228 static const WCHAR lpszNews[]={'n','e','w','s',0};
1229 static const WCHAR lpszMailto[]={'m','a','i','l','t','o',0};
1230 static const WCHAR lpszRes[]={'r','e','s',0};
1231 WCHAR* tempBuffer=NULL;
1232 TRACE("%s %ld\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1233 if(lpszScheme==NULL)
1234 return INTERNET_SCHEME_UNKNOWN;
1235
1236 tempBuffer=HeapAlloc(GetProcessHeap(),0,(nMaxCmp+1)*sizeof(WCHAR));
1237 lstrcpynW(tempBuffer,lpszScheme,nMaxCmp+1);
1238 strlwrW(tempBuffer);
1239 if (nMaxCmp==strlenW(lpszFtp) && !strncmpW(lpszFtp, tempBuffer, nMaxCmp))
1240 iScheme=INTERNET_SCHEME_FTP;
1241 else if (nMaxCmp==strlenW(lpszGopher) && !strncmpW(lpszGopher, tempBuffer, nMaxCmp))
1242 iScheme=INTERNET_SCHEME_GOPHER;
1243 else if (nMaxCmp==strlenW(lpszHttp) && !strncmpW(lpszHttp, tempBuffer, nMaxCmp))
1244 iScheme=INTERNET_SCHEME_HTTP;
1245 else if (nMaxCmp==strlenW(lpszHttps) && !strncmpW(lpszHttps, tempBuffer, nMaxCmp))
1246 iScheme=INTERNET_SCHEME_HTTPS;
1247 else if (nMaxCmp==strlenW(lpszFile) && !strncmpW(lpszFile, tempBuffer, nMaxCmp))
1248 iScheme=INTERNET_SCHEME_FILE;
1249 else if (nMaxCmp==strlenW(lpszNews) && !strncmpW(lpszNews, tempBuffer, nMaxCmp))
1250 iScheme=INTERNET_SCHEME_NEWS;
1251 else if (nMaxCmp==strlenW(lpszMailto) && !strncmpW(lpszMailto, tempBuffer, nMaxCmp))
1252 iScheme=INTERNET_SCHEME_MAILTO;
1253 else if (nMaxCmp==strlenW(lpszRes) && !strncmpW(lpszRes, tempBuffer, nMaxCmp))
1254 iScheme=INTERNET_SCHEME_RES;
1255 HeapFree(GetProcessHeap(),0,tempBuffer);
1256 return iScheme;
1257 }
1258
1259 /***********************************************************************
1260 * SetUrlComponentValueW (Internal)
1261 *
1262 * Helper function for InternetCrackUrlW
1263 *
1264 * PARAMS
1265 * lppszComponent [O] Holds the returned string
1266 * dwComponentLen [I] Holds the size of lppszComponent
1267 * [O] Holds the length of the string in lppszComponent without '\0'
1268 * lpszStart [I] Holds the string to copy from
1269 * len [I] Holds the length of lpszStart without '\0'
1270 *
1271 * RETURNS
1272 * TRUE on success
1273 * FALSE on failure
1274 *
1275 */
1276 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1277 {
1278 TRACE("%s (%ld)\n", debugstr_wn(lpszStart,len), len);
1279
1280 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1281 return FALSE;
1282
1283 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1284 {
1285 if (*lppszComponent == NULL)
1286 {
1287 *lppszComponent = (LPWSTR)lpszStart;
1288 *dwComponentLen = len;
1289 }
1290 else
1291 {
1292 DWORD ncpylen = min((*dwComponentLen)-1, len);
1293 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1294 (*lppszComponent)[ncpylen] = '\0';
1295 *dwComponentLen = ncpylen;
1296 }
1297 }
1298
1299 return TRUE;
1300 }
1301
1302 /***********************************************************************
1303 * InternetCrackUrlW (WININET.@)
1304 */
1305 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1306 LPURL_COMPONENTSW lpUC)
1307 {
1308 /*
1309 * RFC 1808
1310 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1311 *
1312 */
1313 LPCWSTR lpszParam = NULL;
1314 BOOL bIsAbsolute = FALSE;
1315 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1316 LPCWSTR lpszcp = NULL;
1317 LPWSTR lpszUrl_decode = NULL;
1318 DWORD dwUrlLength = dwUrlLength_orig;
1319 const WCHAR lpszSeparators[3]={';','?',0};
1320 const WCHAR lpszSlash[2]={'/',0};
1321 if(dwUrlLength==0)
1322 dwUrlLength=strlenW(lpszUrl);
1323
1324 TRACE("(%s %lu %lx %p)\n", debugstr_w(lpszUrl), dwUrlLength, dwFlags, lpUC);
1325 if (dwFlags & ICU_DECODE)
1326 {
1327 lpszUrl_decode=HeapAlloc( GetProcessHeap(), 0, dwUrlLength * sizeof (WCHAR) );
1328 if( InternetCanonicalizeUrlW(lpszUrl_orig, lpszUrl_decode, &dwUrlLength, dwFlags))
1329 {
1330 lpszUrl = lpszUrl_decode;
1331 }
1332 }
1333 lpszap = lpszUrl;
1334
1335 /* Determine if the URI is absolute. */
1336 while (*lpszap != '\0')
1337 {
1338 if (isalnumW(*lpszap))
1339 {
1340 lpszap++;
1341 continue;
1342 }
1343 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1344 {
1345 bIsAbsolute = TRUE;
1346 lpszcp = lpszap;
1347 }
1348 else
1349 {
1350 lpszcp = lpszUrl; /* Relative url */
1351 }
1352
1353 break;
1354 }
1355
1356 /* Parse <params> */
1357 lpszParam = strpbrkW(lpszap, lpszSeparators);
1358 if (lpszParam != NULL)
1359 {
1360 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1361 lpszParam, dwUrlLength-(lpszParam-lpszUrl));
1362 }
1363
1364 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1365 {
1366 LPCWSTR lpszNetLoc;
1367 static const WCHAR wszAbout[]={'a','b','o','u','t',':',0};
1368
1369 /* Get scheme first. */
1370 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1371 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1372 lpszUrl, lpszcp - lpszUrl);
1373
1374 /* Eat ':' in protocol. */
1375 lpszcp++;
1376
1377 /* if the scheme is "about", there is no host */
1378 if(strncmpW(wszAbout,lpszUrl, lpszcp - lpszUrl)==0)
1379 {
1380 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1381 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1382 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1383 lpUC->nPort = 0;
1384 }
1385 else
1386 {
1387 /* Skip over slashes. */
1388 if (*lpszcp == '/')
1389 {
1390 lpszcp++;
1391 if (*lpszcp == '/')
1392 {
1393 lpszcp++;
1394 if (*lpszcp == '/')
1395 lpszcp++;
1396 }
1397 }
1398
1399 lpszNetLoc = strpbrkW(lpszcp, lpszSlash);
1400 if (lpszParam)
1401 {
1402 if (lpszNetLoc)
1403 lpszNetLoc = min(lpszNetLoc, lpszParam);
1404 else
1405 lpszNetLoc = lpszParam;
1406 }
1407 else if (!lpszNetLoc)
1408 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1409
1410 /* Parse net-loc */
1411 if (lpszNetLoc)
1412 {
1413 LPCWSTR lpszHost;
1414 LPCWSTR lpszPort;
1415
1416 /* [<user>[<:password>]@]<host>[:<port>] */
1417 /* First find the user and password if they exist */
1418
1419 lpszHost = strchrW(lpszcp, '@');
1420 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1421 {
1422 /* username and password not specified. */
1423 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1424 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1425 }
1426 else /* Parse out username and password */
1427 {
1428 LPCWSTR lpszUser = lpszcp;
1429 LPCWSTR lpszPasswd = lpszHost;
1430
1431 while (lpszcp < lpszHost)
1432 {
1433 if (*lpszcp == ':')
1434 lpszPasswd = lpszcp;
1435
1436 lpszcp++;
1437 }
1438
1439 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1440 lpszUser, lpszPasswd - lpszUser);
1441
1442 if (lpszPasswd != lpszHost)
1443 lpszPasswd++;
1444 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1445 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1446 lpszHost - lpszPasswd);
1447
1448 lpszcp++; /* Advance to beginning of host */
1449 }
1450
1451 /* Parse <host><:port> */
1452
1453 lpszHost = lpszcp;
1454 lpszPort = lpszNetLoc;
1455
1456 /* special case for res:// URLs: there is no port here, so the host is the
1457 entire string up to the first '/' */
1458 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1459 {
1460 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1461 lpszHost, lpszPort - lpszHost);
1462 lpUC->nPort = 0;
1463 lpszcp=lpszNetLoc;
1464 }
1465 else
1466 {
1467 while (lpszcp < lpszNetLoc)
1468 {
1469 if (*lpszcp == ':')
1470 lpszPort = lpszcp;
1471
1472 lpszcp++;
1473 }
1474
1475 /* If the scheme is "file" and the host is just one letter, it's not a host */
1476 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1477 {
1478 lpszcp=lpszHost;
1479 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1480 NULL, 0);
1481 lpUC->nPort = 0;
1482 }
1483 else
1484 {
1485 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1486 lpszHost, lpszPort - lpszHost);
1487 if (lpszPort != lpszNetLoc)
1488 lpUC->nPort = atoiW(++lpszPort);
1489 else
1490 lpUC->nPort = 0;
1491 }
1492 }
1493 }
1494 }
1495 }
1496
1497 /* Here lpszcp points to:
1498 *
1499 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1500 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1501 */
1502 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
1503 {
1504 INT len;
1505
1506 /* Only truncate the parameter list if it's already been saved
1507 * in lpUC->lpszExtraInfo.
1508 */
1509 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1510 len = lpszParam - lpszcp;
1511 else
1512 {
1513 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1514 * newlines if necessary.
1515 */
1516 LPWSTR lpsznewline = strchrW(lpszcp, '\n');
1517 if (lpsznewline != NULL)
1518 len = lpsznewline - lpszcp;
1519 else
1520 len = dwUrlLength-(lpszcp-lpszUrl);
1521 }
1522 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1523 lpszcp, len);
1524 }
1525 else
1526 {
1527 lpUC->dwUrlPathLength = 0;
1528 }
1529
1530 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1531 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1532 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1533 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1534 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1535
1536 if (lpszUrl_decode)
1537 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1538 return TRUE;
1539 }
1540
1541 /***********************************************************************
1542 * InternetAttemptConnect (WININET.@)
1543 *
1544 * Attempt to make a connection to the internet
1545 *
1546 * RETURNS
1547 * ERROR_SUCCESS on success
1548 * Error value on failure
1549 *
1550 */
1551 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1552 {
1553 FIXME("Stub\n");
1554 return ERROR_SUCCESS;
1555 }
1556
1557
1558 /***********************************************************************
1559 * InternetCanonicalizeUrlA (WININET.@)
1560 *
1561 * Escape unsafe characters and spaces
1562 *
1563 * RETURNS
1564 * TRUE on success
1565 * FALSE on failure
1566 *
1567 */
1568 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1569 LPDWORD lpdwBufferLength, DWORD dwFlags)
1570 {
1571 HRESULT hr;
1572 DWORD dwURLFlags= 0x80000000; /* Don't know what this means */
1573 if(dwFlags & ICU_DECODE)
1574 {
1575 dwURLFlags |= URL_UNESCAPE;
1576 dwFlags &= ~ICU_DECODE;
1577 }
1578
1579 if(dwFlags & ICU_ESCAPE)
1580 {
1581 dwURLFlags |= URL_UNESCAPE;
1582 dwFlags &= ~ICU_ESCAPE;
1583 }
1584 if(dwFlags & ICU_BROWSER_MODE)
1585 {
1586 dwURLFlags |= URL_BROWSER_MODE;
1587 dwFlags &= ~ICU_BROWSER_MODE;
1588 }
1589 if(dwFlags)
1590 FIXME("Unhandled flags 0x%08lx\n", dwFlags);
1591 TRACE("%s %p %p %08lx\n", debugstr_a(lpszUrl), lpszBuffer,
1592 lpdwBufferLength, dwURLFlags);
1593
1594 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1595 dwFlags ^= ICU_NO_ENCODE;
1596
1597 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1598
1599 return (hr == S_OK) ? TRUE : FALSE;
1600 }
1601
1602 /***********************************************************************
1603 * InternetCanonicalizeUrlW (WININET.@)
1604 *
1605 * Escape unsafe characters and spaces
1606 *
1607 * RETURNS
1608 * TRUE on success
1609 * FALSE on failure
1610 *
1611 */
1612 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1613 LPDWORD lpdwBufferLength, DWORD dwFlags)
1614 {
1615 HRESULT hr;
1616 DWORD dwURLFlags= 0x80000000; /* Don't know what this means */
1617 if(dwFlags & ICU_DECODE)
1618 {
1619 dwURLFlags |= URL_UNESCAPE;
1620 dwFlags &= ~ICU_DECODE;
1621 }
1622
1623 if(dwFlags & ICU_ESCAPE)
1624 {
1625 dwURLFlags |= URL_UNESCAPE;
1626 dwFlags &= ~ICU_ESCAPE;
1627 }
1628 if(dwFlags & ICU_BROWSER_MODE)
1629 {
1630 dwURLFlags |= URL_BROWSER_MODE;
1631 dwFlags &= ~ICU_BROWSER_MODE;
1632 }
1633 if(dwFlags)
1634 FIXME("Unhandled flags 0x%08lx\n", dwFlags);
1635 TRACE("%s %p %p %08lx\n", debugstr_w(lpszUrl), lpszBuffer,
1636 lpdwBufferLength, dwURLFlags);
1637
1638 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1639 dwFlags ^= ICU_NO_ENCODE;
1640
1641 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1642
1643 return (hr == S_OK) ? TRUE : FALSE;
1644 }
1645
1646
1647 /***********************************************************************
1648 * InternetSetStatusCallbackA (WININET.@)
1649 *
1650 * Sets up a callback function which is called as progress is made
1651 * during an operation.
1652 *
1653 * RETURNS
1654 * Previous callback or NULL on success
1655 * INTERNET_INVALID_STATUS_CALLBACK on failure
1656 *
1657 */
1658 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1659 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1660 {
1661 INTERNET_STATUS_CALLBACK retVal;
1662 LPWININETHANDLEHEADER lpwh;
1663
1664 TRACE("0x%08lx\n", (ULONG)hInternet);
1665
1666 lpwh = WININET_GetObject(hInternet);
1667 if (!lpwh)
1668 return INTERNET_INVALID_STATUS_CALLBACK;
1669
1670 lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1671 retVal = lpwh->lpfnStatusCB;
1672 lpwh->lpfnStatusCB = lpfnIntCB;
1673
1674 WININET_Release( lpwh );
1675
1676 return retVal;
1677 }
1678
1679 /***********************************************************************
1680 * InternetSetStatusCallbackW (WININET.@)
1681 *
1682 * Sets up a callback function which is called as progress is made
1683 * during an operation.
1684 *
1685 * RETURNS
1686 * Previous callback or NULL on success
1687 * INTERNET_INVALID_STATUS_CALLBACK on failure
1688 *
1689 */
1690 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1691 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1692 {
1693 INTERNET_STATUS_CALLBACK retVal;
1694 LPWININETHANDLEHEADER lpwh;
1695
1696 TRACE("0x%08lx\n", (ULONG)hInternet);
1697
1698 lpwh = WININET_GetObject(hInternet);
1699 if (!lpwh)
1700 return INTERNET_INVALID_STATUS_CALLBACK;
1701
1702 lpwh->dwInternalFlags |= INET_CALLBACKW;
1703 retVal = lpwh->lpfnStatusCB;
1704 lpwh->lpfnStatusCB = lpfnIntCB;
1705
1706 WININET_Release( lpwh );
1707
1708 return retVal;
1709 }
1710
1711 /***********************************************************************
1712 * InternetSetFilePointer (WININET.@)
1713 */
1714 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1715 PVOID pReserved, DWORD dwMoveContext, DWORD dwContext)
1716 {
1717 FIXME("stub\n");
1718 return FALSE;
1719 }
1720
1721 /***********************************************************************
1722 * InternetWriteFile (WININET.@)
1723 *
1724 * Write data to an open internet file
1725 *
1726 * RETURNS
1727 * TRUE on success
1728 * FALSE on failure
1729 *
1730 */
1731 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
1732 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1733 {
1734 BOOL retval = FALSE;
1735 int nSocket = -1;
1736 LPWININETHANDLEHEADER lpwh;
1737
1738 TRACE("\n");
1739 lpwh = (LPWININETHANDLEHEADER) WININET_GetObject( hFile );
1740 if (NULL == lpwh)
1741 return FALSE;
1742
1743 switch (lpwh->htype)
1744 {
1745 case WH_HHTTPREQ:
1746 {
1747 LPWININETHTTPREQW lpwhr;
1748 lpwhr = (LPWININETHTTPREQW)lpwh;
1749
1750 TRACE("HTTPREQ %li\n",dwNumOfBytesToWrite);
1751 retval = NETCON_send(&lpwhr->netConnection, lpBuffer,
1752 dwNumOfBytesToWrite, 0, (LPINT)lpdwNumOfBytesWritten);
1753
1754 WININET_Release( lpwh );
1755 return retval;
1756 }
1757 break;
1758
1759 case WH_HFILE:
1760 nSocket = ((LPWININETFILE)lpwh)->nDataSocket;
1761 break;
1762
1763 default:
1764 break;
1765 }
1766
1767 if (nSocket != -1)
1768 {
1769 int res = send(nSocket, lpBuffer, dwNumOfBytesToWrite, 0);
1770 retval = (res >= 0);
1771 *lpdwNumOfBytesWritten = retval ? res : 0;
1772 }
1773 WININET_Release( lpwh );
1774
1775 return retval;
1776 }
1777
1778
1779 static BOOL INTERNET_ReadFile(LPWININETHANDLEHEADER lpwh, LPVOID lpBuffer,
1780 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead,
1781 BOOL bWait, BOOL bSendCompletionStatus)
1782 {
1783 BOOL retval = FALSE;
1784 int nSocket = -1;
1785
1786 /* FIXME: this should use NETCON functions! */
1787 switch (lpwh->htype)
1788 {
1789 case WH_HHTTPREQ:
1790 if (!NETCON_recv(&((LPWININETHTTPREQW)lpwh)->netConnection, lpBuffer,
1791 dwNumOfBytesToRead, bWait ? MSG_WAITALL : 0, (int *)pdwNumOfBytesRead))
1792 {
1793 *pdwNumOfBytesRead = 0;
1794 retval = TRUE; /* Under windows, it seems to return 0 even if nothing was read... */
1795 }
1796 else
1797 retval = TRUE;
1798 break;
1799
1800 case WH_HFILE:
1801 /* FIXME: FTP should use NETCON_ stuff */
1802 nSocket = ((LPWININETFILE)lpwh)->nDataSocket;
1803 if (nSocket != -1)
1804 {
1805 int res = recv(nSocket, lpBuffer, dwNumOfBytesToRead, bWait ? MSG_WAITALL : 0);
1806 retval = (res >= 0);
1807 *pdwNumOfBytesRead = retval ? res : 0;
1808 }
1809 break;
1810
1811 default:
1812 break;
1813 }
1814
1815 if (bSendCompletionStatus)
1816 {
1817 INTERNET_ASYNC_RESULT iar;
1818
1819 iar.dwResult = retval;
1820 iar.dwError = iar.dwError = retval ? ERROR_SUCCESS :
1821 INTERNET_GetLastError();
1822
1823 INTERNET_SendCallback(lpwh, lpwh->dwContext,
1824 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1825 sizeof(INTERNET_ASYNC_RESULT));
1826 }
1827 return retval;
1828 }
1829
1830 /***********************************************************************
1831 * InternetReadFile (WININET.@)
1832 *
1833 * Read data from an open internet file
1834 *
1835 * RETURNS
1836 * TRUE on success
1837 * FALSE on failure
1838 *
1839 */
1840 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1841 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1842 {
1843 LPWININETHANDLEHEADER lpwh;
1844 BOOL retval;
1845
1846 TRACE("%p %p %ld %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1847
1848 lpwh = WININET_GetObject( hFile );
1849 if (!lpwh)
1850 {
1851 SetLastError(ERROR_INVALID_HANDLE);
1852 return FALSE;
1853 }
1854
1855 retval = INTERNET_ReadFile(lpwh, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead, TRUE, FALSE);
1856 WININET_Release( lpwh );
1857
1858 TRACE("-- %s (bytes read: %ld)\n", retval ? "TRUE": "FALSE", pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1859 return retval;
1860 }
1861
1862 /***********************************************************************
1863 * InternetReadFileExA (WININET.@)
1864 *
1865 * Read data from an open internet file
1866 *
1867 * PARAMS
1868 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1869 * lpBuffersOut [I/O] Buffer.
1870 * dwFlags [I] Flags. See notes.
1871 * dwContext [I] Context for callbacks.
1872 *
1873 * RETURNS
1874 * TRUE on success
1875 * FALSE on failure
1876 *
1877 * NOTES
1878 * The parameter dwFlags include zero or more of the following flags:
1879 *|IRF_ASYNC - Makes the call asynchronous.
1880 *|IRF_SYNC - Makes the call synchronous.
1881 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1882 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1883 *
1884 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1885 *
1886 * SEE
1887 * InternetOpenUrlA(), HttpOpenRequestA()
1888 */
1889 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1890 DWORD dwFlags, DWORD dwContext)
1891 {
1892 BOOL retval = FALSE;
1893 LPWININETHANDLEHEADER lpwh;
1894
1895 TRACE("(%p %p 0x%lx 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1896
1897 if (dwFlags & ~(IRF_ASYNC|IRF_NO_WAIT))
1898 FIXME("these dwFlags aren't implemented: 0x%lx\n", dwFlags & ~(IRF_ASYNC|IRF_NO_WAIT));
1899
1900 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut))
1901 {
1902 SetLastError(ERROR_INVALID_PARAMETER);
1903 return FALSE;
1904 }
1905
1906 lpwh = (LPWININETHANDLEHEADER) WININET_GetObject( hFile );
1907 if (!lpwh)
1908 {
1909 SetLastError(ERROR_INVALID_HANDLE);
1910 return FALSE;
1911 }
1912
1913 /* FIXME: native only does it asynchronously if the amount of data
1914 * requested isn't available. See NtReadFile. */
1915 /* FIXME: IRF_ASYNC may not be the right thing to test here;
1916 * hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC is probably better, but
1917 * we should implement the above first */
1918 if (dwFlags & IRF_ASYNC)
1919 {
1920 WORKREQUEST workRequest;
1921 struct WORKREQ_INTERNETREADFILEEXA *req;
1922
1923 workRequest.asyncall = INTERNETREADFILEEXA;
1924 workRequest.hdr = WININET_AddRef( lpwh );
1925 req = &workRequest.u.InternetReadFileExA;
1926 req->lpBuffersOut = lpBuffersOut;
1927
1928 retval = INTERNET_AsyncCall(&workRequest);
1929 if (!retval) return FALSE;
1930
1931 SetLastError(ERROR_IO_PENDING);
1932 return FALSE;
1933 }
1934
1935 retval = INTERNET_ReadFile(lpwh, lpBuffersOut->lpvBuffer,
1936 lpBuffersOut->dwBufferLength, &lpBuffersOut->dwBufferLength,
1937 !(dwFlags & IRF_NO_WAIT), FALSE);
1938
1939 WININET_Release( lpwh );
1940
1941 TRACE("-- %s (bytes read: %ld)\n", retval ? "TRUE": "FALSE", lpBuffersOut->dwBufferLength);
1942 return retval;
1943 }
1944
1945 /***********************************************************************
1946 * InternetReadFileExW (WININET.@)
1947 *
1948 * Read data from an open internet file.
1949 *
1950 * PARAMS
1951 * hFile [I] Handle returned by InternetOpenUrl() or HttpOpenRequest().
1952 * lpBuffersOut [I/O] Buffer.
1953 * dwFlags [I] Flags.
1954 * dwContext [I] Context for callbacks.
1955 *
1956 * RETURNS
1957 * FALSE, last error is set to ERROR_CALL_NOT_IMPLEMENTED
1958 *
1959 * NOTES
1960 * Not implemented in Wine or native either (as of IE6 SP2).
1961 *
1962 */
1963 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1964 DWORD dwFlags, DWORD dwContext)
1965 {
1966 ERR("(%p, %p, 0x%lx, 0x%lx): not implemented in native\n", hFile, lpBuffer, dwFlags, dwContext);
1967
1968 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1969 return FALSE;
1970 }
1971
1972 /***********************************************************************
1973 * INET_QueryOptionHelper (internal)
1974 */
1975 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
1976 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1977 {
1978 LPWININETHANDLEHEADER lpwhh;
1979 BOOL bSuccess = FALSE;
1980
1981 TRACE("(%p, 0x%08lx, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
1982
1983 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
1984
1985 switch (dwOption)
1986 {
1987 case INTERNET_OPTION_HANDLE_TYPE:
1988 {
1989 ULONG type;
1990
1991 if (!lpwhh)
1992 {
1993 WARN("Invalid hInternet handle\n");
1994 SetLastError(ERROR_INVALID_HANDLE);
1995 return FALSE;
1996 }
1997
1998 type = lpwhh->htype;
1999
2000 TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);
2001
2002 if (*lpdwBufferLength < sizeof(ULONG))
2003 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2004 else
2005 {
2006 memcpy(lpBuffer, &type, sizeof(ULONG));
2007 bSuccess = TRUE;
2008 }
2009 *lpdwBufferLength = sizeof(ULONG);
2010 break;
2011 }
2012
2013 case INTERNET_OPTION_REQUEST_FLAGS:
2014 {
2015 ULONG flags = 4;
2016 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %ld\n", flags);
2017 if (*lpdwBufferLength < sizeof(ULONG))
2018 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2019 else
2020 {
2021 memcpy(lpBuffer, &flags, sizeof(ULONG));
2022 bSuccess = TRUE;
2023 }
2024 *lpdwBufferLength = sizeof(ULONG);
2025 break;
2026 }
2027
2028 case INTERNET_OPTION_URL:
2029 case INTERNET_OPTION_DATAFILE_NAME:
2030 {
2031 if (!lpwhh)
2032 {
2033 WARN("Invalid hInternet handle\n");
2034 SetLastError(ERROR_INVALID_HANDLE);
2035 return FALSE;
2036 }
2037 if (lpwhh->htype == WH_HHTTPREQ)
2038 {
2039 LPWININETHTTPREQW lpreq = (LPWININETHTTPREQW) lpwhh;
2040 WCHAR url[1023];
2041 static const WCHAR szFmt[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
2042 static const WCHAR szHost[] = {'H','o','s','t',0};
2043 DWORD sizeRequired;
2044 LPHTTPHEADERW Host;
2045
2046 Host = HTTP_GetHeader(lpreq,szHost);
2047 sprintfW(url,szFmt,Host->lpszValue,lpreq->lpszPath);
2048 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
2049 if(!bIsUnicode)
2050 {
2051 sizeRequired = WideCharToMultiByte(CP_ACP,0,url,-1,
2052 lpBuffer,*lpdwBufferLength,NULL,NULL);
2053 if (sizeRequired > *lpdwBufferLength)
2054 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2055 else
2056 bSuccess = TRUE;
2057 *lpdwBufferLength = sizeRequired;
2058 }
2059 else
2060 {
2061 sizeRequired = (lstrlenW(url)+1) * sizeof(WCHAR);
2062 if (*lpdwBufferLength < sizeRequired)
2063 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2064 else
2065 {
2066 strcpyW(lpBuffer, url);
2067 bSuccess = TRUE;
2068 }
2069 *lpdwBufferLength = sizeRequired;
2070 }
2071 }
2072 break;
2073 }
2074 case INTERNET_OPTION_HTTP_VERSION:
2075 {
2076 if (*lpdwBufferLength < sizeof(HTTP_VERSION_INFO))
2077 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2078 else
2079 {
2080 /*
2081 * Presently hardcoded to 1.1
2082 */
2083 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
2084 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
2085 bSuccess = TRUE;
2086 }
2087 *lpdwBufferLength = sizeof(HTTP_VERSION_INFO);
2088 break;
2089 }
2090 case INTERNET_OPTION_CONNECTED_STATE:
2091 {
2092 DWORD *pdwConnectedState = (DWORD *)lpBuffer;
2093 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2094
2095 if (*lpdwBufferLength < sizeof(*pdwConnectedState))
2096 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2097 else
2098 {
2099 *pdwConnectedState = INTERNET_STATE_CONNECTED;
2100 bSuccess = TRUE;
2101 }
2102 *lpdwBufferLength = sizeof(*pdwConnectedState);
2103 break;
2104 }
2105 case INTERNET_OPTION_PROXY:
2106 {
2107 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW)lpwhh;
2108 WININETAPPINFOW wai;
2109
2110 if (lpwai == NULL)
2111 {
2112 TRACE("Getting global proxy info\n");
2113 memset(&wai, 0, sizeof(WININETAPPINFOW));
2114 INTERNET_ConfigureProxyFromReg( &wai );
2115 lpwai = &wai;
2116 }
2117
2118 if (bIsUnicode)
2119 {
2120 INTERNET_PROXY_INFOW *pPI = (INTERNET_PROXY_INFOW *)lpBuffer;
2121 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
2122
2123 if (lpwai->lpszProxy)
2124 proxyBytesRequired = (lstrlenW(lpwai->lpszProxy) + 1) *
2125 sizeof(WCHAR);
2126 if (lpwai->lpszProxyBypass)
2127 proxyBypassBytesRequired =
2128 (lstrlenW(lpwai->lpszProxyBypass) + 1) * sizeof(WCHAR);
2129 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOW) +
2130 proxyBytesRequired + proxyBypassBytesRequired)
2131 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2132 else
2133 {
2134 pPI->dwAccessType = lpwai->dwAccessType;
2135 if (lpwai->lpszProxy)
2136 {
2137 pPI->lpszProxy = (LPWSTR)((LPBYTE)lpBuffer +
2138 sizeof(INTERNET_PROXY_INFOW));
2139 lstrcpyW((LPWSTR)pPI->lpszProxy, lpwai->lpszProxy);
2140 }
2141 else
2142 {
2143 pPI->lpszProxy = (LPWSTR)((LPBYTE)lpBuffer +
2144 sizeof(INTERNET_PROXY_INFOW));
2145 *((LPWSTR)(pPI->lpszProxy)) = 0;
2146 }
2147
2148 if (lpwai->lpszProxyBypass)
2149 {
2150 pPI->lpszProxyBypass = (LPWSTR)((LPBYTE)lpBuffer +
2151 sizeof(INTERNET_PROXY_INFOW) +
2152 proxyBytesRequired);
2153 lstrcpyW((LPWSTR)pPI->lpszProxyBypass,
2154 lpwai->lpszProxyBypass);
2155 }
2156 else
2157 {
2158 pPI->lpszProxyBypass = (LPWSTR)((LPBYTE)lpBuffer +
2159 sizeof(INTERNET_PROXY_INFOW) +
2160 proxyBytesRequired);
2161 *((LPWSTR)(pPI->lpszProxyBypass)) = 0;
2162 }
2163 bSuccess = TRUE;
2164 }
2165 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOW) +
2166 proxyBytesRequired + proxyBypassBytesRequired;
2167 }
2168 else
2169 {
2170 INTERNET_PROXY_INFOA *pPI = (INTERNET_PROXY_INFOA *)lpBuffer;
2171 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
2172
2173 if (lpwai->lpszProxy)
2174 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0,
2175 lpwai->lpszProxy, -1, NULL, 0, NULL, NULL);
2176 if (lpwai->lpszProxyBypass)
2177 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0,
2178 lpwai->lpszProxyBypass, -1, NULL, 0, NULL, NULL);
2179 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOA) +
2180 proxyBytesRequired + proxyBypassBytesRequired)
2181 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2182 else
2183 {
2184 pPI->dwAccessType = lpwai->dwAccessType;
2185 if (lpwai->lpszProxy)
2186 {
2187 pPI->lpszProxy = (LPSTR)((LPBYTE)lpBuffer +
2188 sizeof(INTERNET_PROXY_INFOA));
2189 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxy, -1,
2190 (LPSTR)pPI->lpszProxy, proxyBytesRequired, NULL, NULL);
2191 }
2192 else
2193 {
2194 pPI->lpszProxy = (LPSTR)((LPBYTE)lpBuffer +
2195 sizeof(INTERNET_PROXY_INFOA));
2196 *((LPSTR)(pPI->lpszProxy)) = '\0';
2197 }
2198
2199 if (lpwai->lpszProxyBypass)
2200 {
2201 pPI->lpszProxyBypass = (LPSTR)((LPBYTE)lpBuffer +
2202 sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
2203 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxyBypass,
2204 -1, (LPSTR)pPI->lpszProxyBypass,
2205 proxyBypassBytesRequired,
2206 NULL, NULL);
2207 }
2208 else
2209 {
2210 pPI->lpszProxyBypass = (LPSTR)((LPBYTE)lpBuffer +
2211 sizeof(INTERNET_PROXY_INFOA) +
2212 proxyBytesRequired);
2213 *((LPSTR)(pPI->lpszProxyBypass)) = '\0';
2214 }
2215 bSuccess = TRUE;
2216 }
2217 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOA) +
2218 proxyBytesRequired + proxyBypassBytesRequired;
2219 }
2220 break;
2221 }
2222 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2223 {
2224 ULONG conn = 2;
2225 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER: %ld\n", conn);
2226 if (*lpdwBufferLength < sizeof(ULONG))
2227 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2228 else
2229 {
2230 memcpy(lpBuffer, &conn, sizeof(ULONG));
2231 bSuccess = TRUE;
2232 }
2233 *lpdwBufferLength = sizeof(ULONG);
2234 break;
2235 }
2236 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2237 {
2238 ULONG conn = 4;
2239 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER: %ld\n", conn);
2240 if (*lpdwBufferLength < sizeof(ULONG))
2241 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2242 else
2243 {
2244 memcpy(lpBuffer, &conn, sizeof(ULONG));
2245 bSuccess = TRUE;
2246 }
2247 *lpdwBufferLength = sizeof(ULONG);
2248 break;
2249 }
2250 case INTERNET_OPTION_SECURITY_FLAGS:
2251 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2252 break;
2253
2254 default:
2255 FIXME("Stub! %ld\n", dwOption);
2256 break;
2257 }
2258 if (lpwhh)
2259 WININET_Release( lpwhh );
2260
2261 return bSuccess;
2262 }
2263
2264 /***********************************************************************
2265 * InternetQueryOptionW (WININET.@)
2266 *
2267 * Queries an options on the specified handle
2268 *
2269 * RETURNS
2270 * TRUE on success
2271 * FALSE on failure
2272 *
2273 */
2274 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2275 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2276 {
2277 return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2278 }
2279
2280 /***********************************************************************
2281 * InternetQueryOptionA (WININET.@)
2282 *
2283 * Queries an options on the specified handle
2284 *
2285 * RETURNS
2286 * TRUE on success
2287 * FALSE on failure
2288 *
2289 */
2290 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2291 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2292 {
2293 return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2294 }
2295
2296
2297 /***********************************************************************
2298 * InternetSetOptionW (WININET.@)
2299 *
2300 * Sets an options on the specified handle
2301 *
2302 * RETURNS
2303 * TRUE on success
2304 * FALSE on failure
2305 *
2306 */
2307 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2308 LPVOID lpBuffer, DWORD dwBufferLength)
2309 {
2310 LPWININETHANDLEHEADER lpwhh;
2311 BOOL ret = TRUE;
2312
2313 TRACE("0x%08lx\n", dwOption);
2314
2315 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
2316 if( !lpwhh )
2317 return FALSE;
2318
2319 switch (dwOption)
2320 {
2321 case INTERNET_OPTION_HTTP_VERSION:
2322 {
2323 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2324 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%ld,%ld): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2325 }
2326 break;
2327 case INTERNET_OPTION_ERROR_MASK:
2328 {
2329 unsigned long flags=*(unsigned long*)lpBuffer;
2330 FIXME("Option INTERNET_OPTION_ERROR_MASK(%ld): STUB\n",flags);
2331 }
2332 break;
2333 case INTERNET_OPTION_CODEPAGE:
2334 {
2335 unsigned long codepage=*(unsigned long*)lpBuffer;
2336 FIXME("Option INTERNET_OPTION_CODEPAGE (%ld): STUB\n",codepage);
2337 }
2338 break;
2339 case INTERNET_OPTION_REQUEST_PRIORITY:
2340 {
2341 unsigned long priority=*(unsigned long*)lpBuffer;
2342 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%ld): STUB\n",priority);
2343 }
2344 break;
2345 case INTERNET_OPTION_CONNECT_TIMEOUT:
2346 {
2347 unsigned long connecttimeout=*(unsigned long*)lpBuffer;
2348 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%ld): STUB\n",connecttimeout);
2349 }
2350 break;
2351 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2352 {
2353 unsigned long receivetimeout=*(unsigned long*)lpBuffer;
2354 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%ld): STUB\n",receivetimeout);
2355 }
2356 break;
2357 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2358 {
2359 unsigned long conns=*(unsigned long*)lpBuffer;
2360 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%ld): STUB\n",conns);
2361 }
2362 break;
2363 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2364 {
2365 unsigned long conns=*(unsigned long*)lpBuffer;
2366 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%ld): STUB\n",conns);
2367 }
2368 break;
2369 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2370 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2371 break;
2372 case INTERNET_OPTION_END_BROWSER_SESSION:
2373 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2374 break;
2375 case INTERNET_OPTION_CONNECTED_STATE:
2376 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2377 break;
2378 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2379 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2380 break;
2381 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2382 FIXME("Option INTERNET_OPTION_RECEIVE_TIMEOUT: STUB\n");
2383 break;
2384 case INTERNET_OPTION_SEND_TIMEOUT:
2385 FIXME("Option INTERNET_OPTION_SEND_TIMEOUT: STUB\n");
2386 break;
2387 case INTERNET_OPTION_CONNECT_RETRIES:
2388 FIXME("Option INTERNET_OPTION_CONNECT_RETRIES: STUB\n");
2389 break;
2390 default:
2391 FIXME("Option %ld STUB\n",dwOption);
2392 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2393 ret = FALSE;
2394 break;
2395 }
2396 WININET_Release( lpwhh );
2397
2398 return ret;
2399 }
2400
2401
2402 /***********************************************************************
2403 * InternetSetOptionA (WININET.@)
2404 *
2405 * Sets an options on the specified handle.
2406 *
2407 * RETURNS
2408 * TRUE on success
2409 * FALSE on failure
2410 *
2411 */
2412 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2413 LPVOID lpBuffer, DWORD dwBufferLength)
2414 {
2415 LPVOID wbuffer;
2416 DWORD wlen;
2417 BOOL r;
2418
2419 switch( dwOption )
2420 {
2421 case INTERNET_OPTION_PROXY:
2422 {
2423 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2424 LPINTERNET_PROXY_INFOW piw;
2425 DWORD proxlen, prbylen;
2426 LPWSTR prox, prby;
2427
2428 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2429 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2430 wlen = sizeof(*piw) + proxlen + prbylen;
2431 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2432 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2433 piw->dwAccessType = pi->dwAccessType;
2434 prox = (LPWSTR) &piw[1];
2435 prby = &prox[proxlen+1];
2436 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2437 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2438 piw->lpszProxy = prox;
2439 piw->lpszProxyBypass = prby;
2440 }
2441 break;
2442 case INTERNET_OPTION_USER_AGENT:
2443 case INTERNET_OPTION_USERNAME:
2444 case INTERNET_OPTION_PASSWORD:
2445 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2446 NULL, 0 );
2447 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2448 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2449 wbuffer, wlen );
2450 break;
2451 default:
2452 wbuffer = lpBuffer;
2453 wlen = dwBufferLength;
2454 }
2455
2456 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2457
2458 if( lpBuffer != wbuffer )
2459 HeapFree( GetProcessHeap(), 0, wbuffer );
2460
2461 return r;
2462 }
2463
2464
2465 /***********************************************************************
2466 * InternetSetOptionExA (WININET.@)
2467 */
2468 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2469 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2470 {
2471 FIXME("Flags %08lx ignored\n", dwFlags);
2472 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2473 }
2474
2475 /***********************************************************************
2476 * InternetSetOptionExW (WININET.@)
2477 */
2478 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2479 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2480 {
2481 FIXME("Flags %08lx ignored\n", dwFlags);
2482 if( dwFlags & ~ISO_VALID_FLAGS )
2483 {
2484 SetLastError( ERROR_INVALID_PARAMETER );
2485 return FALSE;
2486 }
2487 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2488 }
2489
2490 static const WCHAR WININET_wkday[7][4] =
2491 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2492 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2493 static const WCHAR WININET_month[12][4] =
2494 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2495 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2496 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2497
2498 /***********************************************************************
2499 * InternetTimeFromSystemTimeA (WININET.@)
2500 */
2501 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2502 {
2503 BOOL ret;
2504 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2505
2506 TRACE( "%p 0x%08lx %p 0x%08lx\n", time, format, string, size );
2507
2508 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2509 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2510
2511 return ret;
2512 }
2513
2514 /***********************************************************************
2515 * InternetTimeFromSystemTimeW (WININET.@)
2516 */
2517 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2518 {
2519 static const WCHAR date[] =
2520 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2521 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2522
2523 TRACE( "%p 0x%08lx %p 0x%08lx\n", time, format, string, size );
2524
2525 if (!time || !string) return FALSE;
2526
2527 if (format != INTERNET_RFC1123_FORMAT || size < INTERNET_RFC1123_BUFSIZE * sizeof(WCHAR))
2528 return FALSE;
2529
2530 sprintfW( string, date,
2531 WININET_wkday[time->wDayOfWeek],
2532 time->wDay,
2533 WININET_month[time->wMonth - 1],
2534 time->wYear,
2535 time->wHour,
2536 time->wMinute,
2537 time->wSecond );
2538
2539 return TRUE;
2540 }
2541
2542 /***********************************************************************
2543 * InternetTimeToSystemTimeA (WININET.@)
2544 */
2545 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2546 {
2547 BOOL ret = FALSE;
2548 WCHAR *stringW;
2549 int len;
2550
2551 TRACE( "%s %p 0x%08lx\n", debugstr_a(string), time, reserved );
2552
2553 len = MultiByteToWideChar( CP_ACP, 0, string, -1, NULL, 0 );
2554 stringW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2555
2556 if (stringW)
2557 {
2558 MultiByteToWideChar( CP_ACP, 0, string, -1, stringW, len );
2559 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2560 HeapFree( GetProcessHeap(), 0, stringW );
2561 }
2562 return ret;
2563 }
2564
2565 /***********************************************************************
2566 * InternetTimeToSystemTimeW (WININET.@)
2567 */
2568 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2569 {
2570 unsigned int i;
2571 WCHAR *s = (LPWSTR)string;
2572
2573 TRACE( "%s %p 0x%08lx\n", debugstr_w(string), time, reserved );
2574
2575 if (!string || !time) return FALSE;
2576
2577 /* Windows does this too */
2578 GetSystemTime( time );
2579
2580 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2581 * a SYSTEMTIME structure.
2582 */
2583
2584 while (*s && !isalphaW( *s )) s++;
2585 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2586 time->wDayOfWeek = 7;
2587
2588 for (i = 0; i < 7; i++)
2589 {
2590 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2591 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2592 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2593 {
2594 time->wDayOfWeek = i;
2595 break;
2596 }
2597 }
2598
2599 if (time->wDayOfWeek > 6) return TRUE;
2600 while (*s && !isdigitW( *s )) s++;
2601 time->wDay = strtolW( s, &s, 10 );
2602
2603 while (*s && !isalphaW( *s )) s++;
2604 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2605 time->wMonth = 0;
2606
2607 for (i = 0; i < 12; i++)
2608 {
2609 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2610 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2611 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2612 {
2613 time->wMonth = i + 1;
2614 break;
2615 }
2616 }
2617 if (time->wMonth == 0) return TRUE;
2618
2619 while (*s && !isdigitW( *s )) s++;
2620 if (*s == '\0') return TRUE;
2621 time->wYear = strtolW( s, &s, 10 );
2622
2623 while (*s && !isdigitW( *s )) s++;
2624 if (*s == '\0') return TRUE;
2625 time->wHour = strtolW( s, &s, 10 );
2626
2627 while (*s && !isdigitW( *s )) s++;
2628 if (*s == '\0') return TRUE;
2629 time->wMinute = strtolW( s, &s, 10 );
2630
2631 while (*s && !isdigitW( *s )) s++;
2632 if (*s == '\0') return TRUE;
2633 time->wSecond = strtolW( s, &s, 10 );
2634
2635 time->wMilliseconds = 0;
2636 return TRUE;
2637 }
2638
2639 /***********************************************************************
2640 * InternetCheckConnectionW (WININET.@)
2641 *
2642 * Pings a requested host to check internet connection
2643 *
2644 * RETURNS
2645 * TRUE on success and FALSE on failure. If a failure then
2646 * ERROR_NOT_CONNECTED is placed into GetLastError
2647 *
2648 */
2649 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2650 {
2651 /*
2652 * this is a kludge which runs the resident ping program and reads the output.
2653 *
2654 * Anyone have a better idea?
2655 */
2656
2657 BOOL rc = FALSE;
2658 static const CHAR ping[] = "ping -w 1 ";
2659 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2660 CHAR *command = NULL;
2661 WCHAR hostW[1024];
2662 DWORD len;
2663 int status = -1;
2664
2665 FIXME("\n");
2666
2667 /*
2668 * Crack or set the Address
2669 */
2670 if (lpszUrl == NULL)
2671 {
2672 /*
2673 * According to the doc we are supost to use the ip for the next
2674 * server in the WnInet internal server database. I have
2675 * no idea what that is or how to get it.
2676 *
2677 * So someone needs to implement this.
2678 */
2679 FIXME("Unimplemented with URL of NULL\n");
2680 return TRUE;
2681 }
2682 else
2683 {
2684 URL_COMPONENTSW components;
2685
2686 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2687 components.lpszHostName = (LPWSTR)&hostW;
2688 components.dwHostNameLength = 1024;
2689
2690 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2691 goto End;
2692
2693 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2694 }
2695
2696 /*
2697 * Build our ping command
2698 */
2699 len = WideCharToMultiByte(CP_THREAD_ACP, 0, hostW, -1, NULL, 0, NULL, NULL);
2700 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2701 strcpy(command,ping);
2702 WideCharToMultiByte(CP_THREAD_ACP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2703 strcat(command,redirect);
2704
2705 TRACE("Ping command is : %s\n",command);
2706
2707 status = system(command);
2708
2709 TRACE("Ping returned a code of %i\n",status);
2710
2711 /* Ping return code of 0 indicates success */
2712 if (status == 0)
2713 rc = TRUE;
2714
2715 End:
2716
2717 HeapFree( GetProcessHeap(), 0, command );
2718 if (rc == FALSE)
2719 SetLastError(ERROR_NOT_CONNECTED);
2720
2721 return rc;
2722 }
2723
2724
2725 /***********************************************************************
2726 * InternetCheckConnectionA (WININET.@)
2727 *
2728 * Pings a requested host to check internet connection
2729 *
2730 * RETURNS
2731 * TRUE on success and FALSE on failure. If a failure then
2732 * ERROR_NOT_CONNECTED is placed into GetLastError
2733 *
2734 */
2735 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2736 {
2737 WCHAR *szUrl;
2738 INT len;
2739 BOOL rc;
2740
2741 len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
2742 if (!(szUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR))))
2743 return FALSE;
2744 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, len);
2745 rc = InternetCheckConnectionW(szUrl, dwFlags, dwReserved);
2746 HeapFree(GetProcessHeap(), 0, szUrl);
2747
2748 return rc;
2749 }
2750
2751
2752 /**********************************************************
2753 * INTERNET_InternetOpenUrlW (internal)
2754 *
2755 * Opens an URL
2756 *
2757 * RETURNS
2758 * handle of connection or NULL on failure
2759 */
2760 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2761 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
2762 {
2763 URL_COMPONENTSW urlComponents;
2764 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2765 WCHAR password[1024], path[2048], extra[1024];
2766 HINTERNET client = NULL, client1 = NULL;
2767
2768 TRACE("(%p, %s, %s, %08lx, %08lx, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2769 dwHeadersLength, dwFlags, dwContext);
2770
2771 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2772 urlComponents.lpszScheme = protocol;
2773 urlComponents.dwSchemeLength = 32;
2774 urlComponents.lpszHostName = hostName;
2775 urlComponents.dwHostNameLength = MAXHOSTNAME;
2776 urlComponents.lpszUserName = userName;
2777 urlComponents.dwUserNameLength = 1024;
2778 urlComponents.lpszPassword = password;
2779 urlComponents.dwPasswordLength = 1024;
2780 urlComponents.lpszUrlPath = path;
2781 urlComponents.dwUrlPathLength = 2048;
2782 urlComponents.lpszExtraInfo = extra;
2783 urlComponents.dwExtraInfoLength = 1024;
2784 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2785 return NULL;
2786 switch(urlComponents.nScheme) {
2787 case INTERNET_SCHEME_FTP:
2788 if(urlComponents.nPort == 0)
2789 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2790 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2791 userName, password, dwFlags, dwContext, INET_OPENURL);
2792 if(client == NULL)
2793 break;
2794 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2795 if(client1 == NULL) {
2796 InternetCloseHandle(client);
2797 break;
2798 }
2799 break;
2800
2801 case INTERNET_SCHEME_HTTP:
2802 case INTERNET_SCHEME_HTTPS: {
2803 static const WCHAR szStars[] = { '*','/','*', 0 };
2804 LPCWSTR accept[2] = { szStars, NULL };
2805 if(urlComponents.nPort == 0) {
2806 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2807 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2808 else
2809 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2810 }
2811 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2812 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2813 userName, password, dwFlags, dwContext, INET_OPENURL);
2814 if(client == NULL)
2815 break;
2816 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2817 if(client1 == NULL) {
2818 InternetCloseHandle(client);
2819 break;
2820 }
2821 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2822 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0)) {
2823 InternetCloseHandle(client1);
2824 client1 = NULL;
2825 break;
2826 }
2827 }
2828 case INTERNET_SCHEME_GOPHER:
2829 /* gopher doesn't seem to be implemented in wine, but it's supposed
2830 * to be supported by InternetOpenUrlA. */
2831 default:
2832 break;
2833 }
2834
2835 TRACE(" %p <--\n", client1);
2836
2837 return client1;
2838 }
2839
2840 /**********************************************************
2841 * InternetOpenUrlW (WININET.@)
2842 *
2843 * Opens an URL
2844 *
2845 * RETURNS
2846 * handle of connection or NULL on failure
2847 */
2848 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2849 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
2850 {
2851 HINTERNET ret = NULL;
2852 LPWININETAPPINFOW hIC = NULL;
2853
2854 if (TRACE_ON(wininet)) {
2855 TRACE("(%p, %s, %s, %08lx, %08lx, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2856 dwHeadersLength, dwFlags, dwContext);
2857 TRACE(" flags :");
2858 dump_INTERNET_FLAGS(dwFlags);
2859 }
2860
2861 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2862 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2863 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2864 goto lend;
2865 }
2866
2867 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2868 WORKREQUEST workRequest;
2869 struct WORKREQ_INTERNETOPENURLW *req;
2870
2871 workRequest.asyncall = INTERNETOPENURLW;
2872 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2873 req = &workRequest.u.InternetOpenUrlW;
2874 if (lpszUrl)
2875 req->lpszUrl = WININET_strdupW(lpszUrl);
2876 else
2877 req->lpszUrl = 0;
2878 if (lpszHeaders)
2879 req->lpszHeaders = WININET_strdupW(lpszHeaders);
2880 else
2881 req->lpszHeaders = 0;
2882 req->dwHeadersLength = dwHeadersLength;
2883 req->dwFlags = dwFlags;
2884 req->dwContext = dwContext;
2885
2886 INTERNET_AsyncCall(&workRequest);
2887 /*
2888 * This is from windows.
2889 */
2890 SetLastError(ERROR_IO_PENDING);
2891 } else {
2892 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2893 }
2894
2895 lend:
2896 if( hIC )
2897 WININET_Release( &hIC->hdr );
2898 TRACE(" %p <--\n", ret);
2899
2900 return ret;
2901 }
2902
2903 /**********************************************************
2904 * InternetOpenUrlA (WININET.@)
2905 *
2906 * Opens an URL
2907 *
2908 * RETURNS
2909 * handle of connection or NULL on failure
2910 */
2911 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2912 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
2913 {
2914 HINTERNET rc = (HINTERNET)NULL;
2915
2916 INT lenUrl;
2917 INT lenHeaders = 0;
2918 LPWSTR szUrl = NULL;
2919 LPWSTR szHeaders = NULL;
2920
2921 TRACE("\n");
2922
2923 if(lpszUrl) {
2924 lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
2925 szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
2926 if(!szUrl)
2927 return (HINTERNET)NULL;
2928 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
2929 }
2930
2931 if(lpszHeaders) {
2932 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2933 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2934 if(!szHeaders) {
2935 HeapFree(GetProcessHeap(), 0, szUrl);
2936 return (HINTERNET)NULL;
2937 }
2938 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2939 }
2940
2941 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2942 lenHeaders, dwFlags, dwContext);
2943
2944 HeapFree(GetProcessHeap(), 0, szUrl);
2945 HeapFree(GetProcessHeap(), 0, szHeaders);
2946
2947 return rc;
2948 }
2949
2950
2951 /***********************************************************************
2952 * INTERNET_SetLastError (internal)
2953 *
2954 * Set last thread specific error
2955 *
2956 * RETURNS
2957 *
2958 */
2959 void INTERNET_SetLastError(DWORD dwError)
2960 {
2961 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2962
2963 SetLastError(dwError);
2964 if(lpwite)
2965 lpwite->dwError = dwError;
2966 }
2967
2968
2969 /***********************************************************************
2970 * INTERNET_GetLastError (internal)
2971 *
2972 * Get last thread specific error
2973 *
2974 * RETURNS
2975 *
2976 */
2977 DWORD INTERNET_GetLastError(void)
2978 {
2979 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2980 /* TlsGetValue clears last error, so set it again here */
2981 SetLastError(lpwite->dwError);
2982 return lpwite->dwError;
2983 }
2984
2985
2986 /***********************************************************************
2987 * INTERNET_WorkerThreadFunc (internal)
2988 *
2989 * Worker thread execution function
2990 *
2991 * RETURNS
2992 *
2993 */
2994 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
2995 {
2996 DWORD dwWaitRes;
2997
2998 while (1)
2999 {
3000 if(dwNumJobs > 0) {
3001 INTERNET_ExecuteWork();
3002 continue;
3003 }
3004 dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);
3005
3006 if (dwWaitRes == WAIT_OBJECT_0 + 1)
3007 INTERNET_ExecuteWork();
3008 else
3009 break;
3010
3011 InterlockedIncrement(&dwNumIdleThreads);
3012 }
3013
3014 InterlockedDecrement(&dwNumIdleThreads);
3015 InterlockedDecrement(&dwNumThreads);
3016 TRACE("Worker thread exiting\n");
3017 return TRUE;
3018 }
3019
3020
3021 /***********************************************************************
3022 * INTERNET_InsertWorkRequest (internal)
3023 *
3024 * Insert work request into queue
3025 *
3026 * RETURNS
3027 *
3028 */
3029 static BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
3030 {
3031 BOOL bSuccess = FALSE;
3032 LPWORKREQUEST lpNewRequest;
3033
3034 TRACE("\n");
3035
3036 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3037 if (lpNewRequest)
3038 {
3039 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
3040 lpNewRequest->prev = NULL;
3041
3042 EnterCriticalSection(&csQueue);
3043
3044 lpNewRequest->next = lpWorkQueueTail;
3045 if (lpWorkQueueTail)
3046 lpWorkQueueTail->prev = lpNewRequest;
3047 lpWorkQueueTail = lpNewRequest;
3048 if (!lpHeadWorkQueue)
3049 lpHeadWorkQueue = lpWorkQueueTail;
3050
3051 LeaveCriticalSection(&csQueue);
3052
3053 bSuccess = TRUE;
3054 InterlockedIncrement(&dwNumJobs);
3055 }
3056
3057 return bSuccess;
3058 }
3059
3060
3061 /***********************************************************************
3062 * INTERNET_GetWorkRequest (internal)
3063 *
3064 * Retrieves work request from queue
3065 *
3066 * RETURNS
3067 *
3068 */
3069 static BOOL INTERNET_GetWorkRequest(LPWORKREQUEST lpWorkRequest)
3070 {
3071 BOOL bSuccess = FALSE;
3072 LPWORKREQUEST lpRequest = NULL;
3073
3074 TRACE("\n");
3075
3076 EnterCriticalSection(&csQueue);
3077
3078 if (lpHeadWorkQueue)
3079 {
3080 lpRequest = lpHeadWorkQueue;
3081 lpHeadWorkQueue = lpHeadWorkQueue->prev;
3082 if (lpRequest == lpWorkQueueTail)
3083 lpWorkQueueTail = lpHeadWorkQueue;
3084 }
3085
3086 LeaveCriticalSection(&csQueue);
3087
3088 if (lpRequest)
3089 {
3090 memcpy(lpWorkRequest, lpRequest, sizeof(WORKREQUEST));
3091 HeapFree(GetProcessHeap(), 0, lpRequest);
3092 bSuccess = TRUE;
3093 InterlockedDecrement(&dwNumJobs);
3094 }
3095
3096 return bSuccess;
3097 }
3098
3099
3100 /***********************************************************************
3101 * INTERNET_AsyncCall (internal)
3102 *
3103 * Retrieves work request from queue
3104 *
3105 * RETURNS
3106 *
3107 */
3108 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3109 {
3110 HANDLE hThread;
3111 DWORD dwTID;
3112 BOOL bSuccess = FALSE;
3113
3114 TRACE("\n");
3115
3116 if (InterlockedDecrement(&dwNumIdleThreads) < 0)
3117 {
3118 InterlockedIncrement(&dwNumIdleThreads);
3119
3120 if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
3121 !(hThread = CreateThread(NULL, 0,
3122 INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
3123 {
3124 InterlockedDecrement(&dwNumThreads);
3125 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3126 goto lerror;
3127 }
3128
3129 TRACE("Created new thread\n");
3130 }
3131
3132 bSuccess = TRUE;
3133 INTERNET_InsertWorkRequest(lpWorkRequest);
3134 SetEvent(hWorkEvent);
3135
3136 lerror:
3137
3138 return bSuccess;
3139 }
3140
3141
3142 /***********************************************************************
3143 * INTERNET_ExecuteWork (internal)
3144 *
3145 * RETURNS
3146 *
3147 */
3148 static VOID INTERNET_ExecuteWork(void)
3149 {
3150 WORKREQUEST workRequest;
3151
3152 TRACE("\n");
3153
3154 if (!INTERNET_GetWorkRequest(&workRequest))
3155 return;
3156
3157 switch (workRequest.asyncall)
3158 {
3159 case FTPPUTFILEW:
3160 {
3161 struct WORKREQ_FTPPUTFILEW *req = &workRequest.u.FtpPutFileW;
3162 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3163
3164 TRACE("FTPPUTFILEW %p\n", lpwfs);
3165
3166 FTP_FtpPutFileW(lpwfs, req->lpszLocalFile,
3167 req->lpszNewRemoteFile, req->dwFlags, req->dwContext);
3168
3169 HeapFree(GetProcessHeap(), 0, req->lpszLocalFile);
3170 HeapFree(GetProcessHeap(), 0, req->lpszNewRemoteFile);
3171 }
3172 break;
3173
3174 case FTPSETCURRENTDIRECTORYW:
3175 {
3176 struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
3177 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3178
3179 TRACE("FTPSETCURRENTDIRECTORYW %p\n", lpwfs);
3180
3181 req = &workRequest.u.FtpSetCurrentDirectoryW;
3182 FTP_FtpSetCurrentDirectoryW(lpwfs, req->lpszDirectory);
3183 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
3184 }
3185 break;
3186
3187 case FTPCREATEDIRECTORYW:
3188 {
3189 struct WORKREQ_FTPCREATEDIRECTORYW *req;
3190 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3191
3192 TRACE("FTPCREATEDIRECTORYW %p\n", lpwfs);
3193
3194 req = &workRequest.u.FtpCreateDirectoryW;
3195 FTP_FtpCreateDirectoryW(lpwfs, req->lpszDirectory);
3196 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
3197 }
3198 break;
3199
3200 case FTPFINDFIRSTFILEW:
3201 {
3202 struct WORKREQ_FTPFINDFIRSTFILEW *req;
3203 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3204
3205 TRACE("FTPFINDFIRSTFILEW %p\n", lpwfs);
3206
3207 req = &workRequest.u.FtpFindFirstFileW;
3208 FTP_FtpFindFirstFileW(lpwfs, req->lpszSearchFile,
3209 req->lpFindFileData, req->dwFlags, req->dwContext);
3210 HeapFree(GetProcessHeap(), 0, req->lpszSearchFile);
3211 }
3212 break;
3213
3214 case FTPGETCURRENTDIRECTORYW:
3215 {
3216 struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
3217 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3218
3219 TRACE("FTPGETCURRENTDIRECTORYW %p\n", lpwfs);
3220
3221 req = &workRequest.u.FtpGetCurrentDirectoryW;
3222 FTP_FtpGetCurrentDirectoryW(lpwfs,
3223 req->lpszDirectory, req->lpdwDirectory);
3224 }
3225 break;
3226
3227 case FTPOPENFILEW:
3228 {
3229 struct WORKREQ_FTPOPENFILEW *req = &workRequest.u.FtpOpenFileW;
3230 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3231
3232 TRACE("FTPOPENFILEW %p\n", lpwfs);
3233
3234 FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
3235 req->dwAccess, req->dwFlags, req->dwContext);
3236 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
3237 }
3238 break;
3239
3240 case FTPGETFILEW:
3241 {
3242 struct WORKREQ_FTPGETFILEW *req = &workRequest.u.FtpGetFileW;
3243 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3244
3245 TRACE("FTPGETFILEW %p\n", lpwfs);
3246
3247 FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
3248 req->lpszNewFile, req->fFailIfExists,
3249 req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
3250 HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
3251 HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
3252 }
3253 break;
3254
3255 case FTPDELETEFILEW:
3256 {
3257 struct WORKREQ_FTPDELETEFILEW *req = &workRequest.u.FtpDeleteFileW;
3258 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3259
3260 TRACE("FTPDELETEFILEW %p\n", lpwfs);
3261
3262 FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
3263 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
3264 }
3265 break;
3266
3267 case FTPREMOVEDIRECTORYW:
3268 {
3269 struct WORKREQ_FTPREMOVEDIRECTORYW *req;
3270 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3271
3272 TRACE("FTPREMOVEDIRECTORYW %p\n", lpwfs);
3273
3274 req = &workRequest.u.FtpRemoveDirectoryW;
3275 FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
3276 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
3277 }
3278 break;
3279
3280 case FTPRENAMEFILEW:
3281 {
3282 struct WORKREQ_FTPRENAMEFILEW *req = &workRequest.u.FtpRenameFileW;
3283 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest.hdr;
3284
3285 TRACE("FTPRENAMEFILEW %p\n", lpwfs);
3286
3287 FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
3288 HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
3289 HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
3290 }
3291 break;
3292
3293 case INTERNETFINDNEXTW:
3294 {
3295 struct WORKREQ_INTERNETFINDNEXTW *req;
3296 LPWININETFINDNEXTW lpwh = (LPWININETFINDNEXTW) workRequest.hdr;
3297
3298 TRACE("INTERNETFINDNEXTW %p\n", lpwh);
3299
3300 req = &workRequest.u.InternetFindNextW;
3301 INTERNET_FindNextFileW(lpwh, req->lpFindFileData);
3302 }
3303 break;
3304
3305 case HTTPSENDREQUESTW:
3306 {
3307 struct WORKREQ_HTTPSENDREQUESTW *req = &workRequest.u.HttpSendRequestW;
3308 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest.hdr;
3309
3310 TRACE("HTTPSENDREQUESTW %p\n", lpwhr);
3311
3312 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
3313 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
3314 req->dwContentLength, req->bEndRequest);
3315
3316 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
3317 }
3318 break;
3319
3320 case HTTPOPENREQUESTW:
3321 {
3322 struct WORKREQ_HTTPOPENREQUESTW *req = &workRequest.u.HttpOpenRequestW;
3323 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) workRequest.hdr;
3324
3325 TRACE("HTTPOPENREQUESTW %p\n", lpwhs);
3326
3327 HTTP_HttpOpenRequestW(lpwhs, req->lpszVerb,
3328 req->lpszObjectName, req->lpszVersion, req->lpszReferrer,
3329 req->lpszAcceptTypes, req->dwFlags, req->dwContext);
3330
3331 HeapFree(GetProcessHeap(), 0, req->lpszVerb);
3332 HeapFree(GetProcessHeap(), 0, req->lpszObjectName);
3333 HeapFree(GetProcessHeap(), 0, req->lpszVersion);
3334 HeapFree(GetProcessHeap(), 0, req->lpszReferrer);
3335 }
3336 break;
3337
3338 case SENDCALLBACK:
3339 {
3340 struct WORKREQ_SENDCALLBACK *req = &workRequest.u.SendCallback;
3341
3342 TRACE("SENDCALLBACK %p\n", workRequest.hdr);
3343
3344 INTERNET_SendCallback(workRequest.hdr,
3345 req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
3346 req->dwStatusInfoLength);
3347
3348 /* And frees the copy of the status info */
3349 HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
3350 }
3351 break;
3352
3353 case INTERNETOPENURLW:
3354 {
3355 struct WORKREQ_INTERNETOPENURLW *req = &workRequest.u.InternetOpenUrlW;
3356 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest.hdr;
3357
3358 TRACE("INTERNETOPENURLW %p\n", hIC);
3359
3360 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
3361 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
3362 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
3363 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
3364 }
3365 break;
3366 case INTERNETREADFILEEXA:
3367 {
3368 struct WORKREQ_INTERNETREADFILEEXA *req = &workRequest.u.InternetReadFileExA;
3369
3370 TRACE("INTERNETREADFILEEXA %p\n", workRequest.hdr);
3371
3372 INTERNET_ReadFile(workRequest.hdr, req->lpBuffersOut->lpvBuffer,
3373 req->lpBuffersOut->dwBufferLength,
3374 &req->lpBuffersOut->dwBufferLength, TRUE, TRUE);
3375 }
3376 break;
3377 }
3378 WININET_Release( workRequest.hdr );
3379 }
3380
3381
3382 /***********************************************************************
3383 * INTERNET_GetResponseBuffer (internal)
3384 *
3385 * RETURNS
3386 *
3387 */
3388 LPSTR INTERNET_GetResponseBuffer(void)
3389 {
3390 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3391 TRACE("\n");
3392 return lpwite->response;
3393 }
3394
3395 /***********************************************************************
3396 * INTERNET_GetNextLine (internal)
3397 *
3398 * Parse next line in directory string listing
3399 *
3400 * RETURNS
3401 * Pointer to beginning of next line
3402 * NULL on failure
3403 *
3404 */
3405
3406 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3407 {
3408 struct timeval tv;
3409 fd_set infd;
3410 BOOL bSuccess = FALSE;
3411 INT nRecv = 0;
3412 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3413
3414 TRACE("\n");
3415
3416 FD_ZERO(&infd);
3417 FD_SET(nSocket, &infd);
3418 tv.tv_sec=RESPONSE_TIMEOUT;
3419 tv.tv_usec=0;
3420
3421 while (nRecv < MAX_REPLY_LEN)
3422 {
3423 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
3424 {
3425 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3426 {
3427 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3428 goto lend;
3429 }
3430
3431 if (lpszBuffer[nRecv] == '\n')
3432 {
3433 bSuccess = TRUE;
3434 break;
3435 }
3436 if (lpszBuffer[nRecv] != '\r')
3437 nRecv++;
3438 }
3439 else
3440 {
3441 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3442 goto lend;
3443 }
3444 }
3445
3446 lend:
3447 if (bSuccess)
3448 {
3449 lpszBuffer[nRecv] = '\0';
3450 *dwLen = nRecv - 1;
3451 TRACE(":%d %s\n", nRecv, lpszBuffer);
3452 return lpszBuffer;
3453 }
3454 else
3455 {
3456 return NULL;
3457 }
3458 }
3459
3460 /**********************************************************
3461 * InternetQueryDataAvailable (WININET.@)
3462 *
3463 * Determines how much data is available to be read.
3464 *
3465 * RETURNS
3466 * If there is data available then TRUE, otherwise if there
3467 * is not or an error occurred then FALSE. Use GetLastError() to
3468 * check for ERROR_NO_MORE_FILES to see if it was the former.
3469 */
3470 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3471 LPDWORD lpdwNumberOfBytesAvailble,
3472 DWORD dwFlags, DWORD dwConext)
3473 {
3474 LPWININETHTTPREQW lpwhr;
3475 BOOL retval = FALSE;
3476 char buffer[4048];
3477
3478 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hFile );
3479 if (NULL == lpwhr)
3480 {
3481 SetLastError(ERROR_NO_MORE_FILES);
3482 return FALSE;
3483 }
3484
3485 TRACE("--> %p %i\n",lpwhr,lpwhr->hdr.htype);
3486
3487 switch (lpwhr->hdr.htype)
3488 {
3489 case WH_HHTTPREQ:
3490 if (!NETCON_recv(&lpwhr->netConnection, buffer,
3491 4048, MSG_PEEK, (int *)lpdwNumberOfBytesAvailble))
3492 {
3493 SetLastError(ERROR_NO_MORE_FILES);
3494 retval = FALSE;
3495 }
3496 else
3497 retval = TRUE;
3498 break;
3499
3500 default:
3501 FIXME("unsupported file type\n");
3502 break;
3503 }
3504 WININET_Release( &lpwhr->hdr );
3505
3506 TRACE("<-- %i\n",retval);
3507 return retval;
3508 }
3509
3510
3511 /***********************************************************************
3512 * InternetLockRequestFile (WININET.@)
3513 */
3514 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3515 *lphLockReqHandle)
3516 {
3517 FIXME("STUB\n");
3518 return FALSE;
3519 }
3520
3521 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3522 {
3523 FIXME("STUB\n");
3524 return FALSE;
3525 }
3526
3527
3528 /***********************************************************************
3529 * InternetAutodial (WININET.@)
3530 *
3531 * On windows this function is supposed to dial the default internet
3532 * connection. We don't want to have Wine dial out to the internet so
3533 * we return TRUE by default. It might be nice to check if we are connected.
3534 *
3535 * RETURNS
3536 * TRUE on success
3537 * FALSE on failure
3538 *
3539 */
3540 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3541 {
3542 FIXME("STUB\n");
3543
3544 /* Tell that we are connected to the internet. */
3545 return TRUE;
3546 }
3547
3548 /***********************************************************************
3549 * InternetAutodialHangup (WININET.@)
3550 *
3551 * Hangs up a connection made with InternetAutodial
3552 *
3553 * PARAM
3554 * dwReserved
3555 * RETURNS
3556 * TRUE on success
3557 * FALSE on failure
3558 *
3559 */
3560 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3561 {
3562 FIXME("STUB\n");
3563
3564 /* we didn't dial, we don't disconnect */
3565 return TRUE;
3566 }
3567
3568 /***********************************************************************
3569 * InternetCombineUrlA (WININET.@)
3570 *
3571 * Combine a base URL with a relative URL
3572 *
3573 * RETURNS
3574 * TRUE on success
3575 * FALSE on failure
3576 *
3577 */
3578
3579 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3580 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3581 DWORD dwFlags)
3582 {
3583 HRESULT hr=S_OK;
3584
3585 TRACE("(%s, %s, %p, %p, 0x%08lx)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3586
3587 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3588 dwFlags ^= ICU_NO_ENCODE;
3589 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3590
3591 return (hr==S_OK);
3592 }
3593
3594 /***********************************************************************
3595 * InternetCombineUrlW (WININET.@)
3596 *
3597 * Combine a base URL with a relative URL
3598 *
3599 * RETURNS
3600 * TRUE on success
3601 * FALSE on failure
3602 *
3603 */
3604
3605 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3606 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3607 DWORD dwFlags)
3608 {
3609 HRESULT hr=S_OK;
3610
3611 TRACE("(%s, %s, %p, %p, 0x%08lx)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3612
3613 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3614 dwFlags ^= ICU_NO_ENCODE;
3615 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3616
3617 return (hr==S_OK);
3618 }
3619
3620 /* max port num is 65535 => 5 digits */
3621 #define MAX_WORD_DIGITS 5
3622
3623 /* we can calculate using ansi strings because we're just
3624 * calculating string length, not size
3625 */
3626 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3627 LPDWORD lpdwUrlLength, LPDWORD lpdwSchemeLength)
3628 {
3629 static const WCHAR httpW[] = {'h','t','t','p',0};
3630
3631 *lpdwUrlLength = 0;
3632
3633 switch (lpUrlComponents->nScheme)
3634 {
3635 case INTERNET_SCHEME_FTP:
3636 case INTERNET_SCHEME_RES:
3637 *lpdwSchemeLength = 3;
3638 break;
3639 case INTERNET_SCHEME_HTTP:
3640 case INTERNET_SCHEME_FILE:
3641 case INTERNET_SCHEME_NEWS:
3642 *lpdwSchemeLength = 4;
3643 break;
3644
3645 default:
3646 *lpdwSchemeLength = 4;
3647 break;
3648 }
3649
3650 *lpdwUrlLength += *lpdwSchemeLength;
3651 *lpdwUrlLength += strlen("://");
3652
3653 if (lpUrlComponents->lpszUserName)
3654 {
3655 *lpdwUrlLength += lpUrlComponents->dwUserNameLength;
3656 *lpdwUrlLength += strlen("@");
3657 }
3658 else
3659 {
3660 if (lpUrlComponents->lpszPassword)
3661 {
3662 SetLastError(ERROR_INVALID_PARAMETER);
3663 return FALSE;
3664 }
3665 }
3666
3667 if (lpUrlComponents->lpszPassword)
3668 {
3669 *lpdwUrlLength += strlen(":");
3670 *lpdwUrlLength += lpUrlComponents->dwPasswordLength;
3671 }
3672
3673 *lpdwUrlLength += lpUrlComponents->dwHostNameLength;
3674
3675 if (lpUrlComponents->nPort != 80 ||
3676 (lpUrlComponents->lpszScheme && strncmpW(lpUrlComponents->lpszScheme, httpW, lpUrlComponents->dwSchemeLength)))
3677 {
3678 char szPort[MAX_WORD_DIGITS];
3679
3680 sprintf(szPort, "%d", lpUrlComponents->nPort);
3681 *lpdwUrlLength += strlen(szPort);
3682 *lpdwUrlLength += strlen(":");
3683 }
3684
3685 *lpdwUrlLength += lpUrlComponents->dwUrlPathLength;
3686 return TRUE;
3687 }
3688
3689 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3690 {
3691 INT len;
3692
3693 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3694
3695 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3696 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3697 urlCompW->nScheme = lpUrlComponents->nScheme;
3698 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3699 urlCompW->nPort = lpUrlComponents->nPort;
3700 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3701 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3702 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3703 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3704
3705 if (lpUrlComponents->lpszScheme)
3706 {
3707 len = lpUrlComponents->dwSchemeLength + 1;
3708 urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3709 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3710 -1, urlCompW->lpszScheme, len);
3711 }
3712
3713 if (lpUrlComponents->lpszHostName)
3714 {
3715 len = lpUrlComponents->dwHostNameLength + 1;
3716 urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3717 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3718 -1, urlCompW->lpszHostName, len);
3719 }
3720
3721 if (lpUrlComponents->lpszUserName)
3722 {
3723 len = lpUrlComponents->dwUserNameLength + 1;
3724 urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3725 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3726 -1, urlCompW->lpszUserName, len);
3727 }
3728
3729 if (lpUrlComponents->lpszPassword)
3730 {
3731 len = lpUrlComponents->dwPasswordLength + 1;
3732 urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3733 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3734 -1, urlCompW->lpszPassword, len);
3735 }
3736
3737 if (lpUrlComponents->lpszUrlPath)
3738 {
3739 len = lpUrlComponents->dwUrlPathLength + 1;
3740 urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3741 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3742 -1, urlCompW->lpszUrlPath, len);
3743 }
3744
3745 if (lpUrlComponents->lpszExtraInfo)
3746 {
3747 len = lpUrlComponents->dwExtraInfoLength + 1;
3748 urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3749 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3750 -1, urlCompW->lpszExtraInfo, len);
3751 }
3752 }
3753
3754 /***********************************************************************
3755 * InternetCreateUrlA (WININET.@)
3756 *
3757 * RETURNS
3758 * TRUE on success
3759 * FALSE on failure
3760 *
3761 */
3762 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3763 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3764 {
3765 BOOL ret;
3766 LPWSTR urlW = NULL;
3767 URL_COMPONENTSW urlCompW;
3768
3769 TRACE("(%p,%ld,%s,%p)\n", lpUrlComponents, dwFlags, debugstr_a(lpszUrl), lpdwUrlLength);
3770
3771 if (!lpUrlComponents)
3772 return FALSE;
3773
3774 if (lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3775 {
3776 SetLastError(ERROR_INVALID_PARAMETER);
3777 return FALSE;
3778 }
3779
3780 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3781
3782 if (lpszUrl)
3783 urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3784
3785 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3786
3787 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3788 * minus one, so add one to leave room for NULL terminator
3789 */
3790 if (ret)
3791 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3792
3793 HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3794 HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3795 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
3796 HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
3797 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
3798 HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
3799 HeapFree(GetProcessHeap(), 0, urlW);
3800
3801 return ret;
3802 }
3803
3804 /***********************************************************************
3805 * InternetCreateUrlW (WININET.@)
3806 *
3807 * RETURNS
3808 * TRUE on success
3809 * FALSE on failure
3810 *
3811 */
3812 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3813 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
3814 {
3815 DWORD dwLen, dwSchemeLen;
3816
3817 static const WCHAR colonSlashW[] = {':','/','/',0};
3818 static const WCHAR httpW[] = {'h','t','t','p',0};
3819 static const WCHAR colonW[] = {':',0};
3820 static const WCHAR atW[] = {'@',0};
3821 static const WCHAR percentD[] = {'%','d',0};
3822
3823 TRACE("(%p,%ld,%s,%p)\n", lpUrlComponents, dwFlags, debugstr_w(lpszUrl), lpdwUrlLength);
3824
3825 if (!lpUrlComponents)
3826 return FALSE;
3827
3828 if (lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3829 {
3830 SetLastError(ERROR_INVALID_PARAMETER);
3831 return FALSE;
3832 }
3833
3834 if (!calc_url_length(lpUrlComponents, &dwLen, &dwSchemeLen))
3835 return FALSE;
3836
3837 if (!lpszUrl || *lpdwUrlLength < dwLen)
3838 {
3839 *lpdwUrlLength = dwLen + 1; /* terminating null */
3840 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3841 return FALSE;
3842 }
3843
3844 *lpdwUrlLength = dwLen;
3845 lpszUrl[0] = 0x00;
3846
3847 if (lpUrlComponents->lpszScheme)
3848 lstrcpynW(lpszUrl, lpUrlComponents->lpszScheme,
3849 min(lpUrlComponents->dwSchemeLength, dwSchemeLen) + 1);
3850
3851 lstrcatW(lpszUrl, colonSlashW);
3852
3853 if (lpUrlComponents->lpszUserName)
3854 {
3855 if (!*lpUrlComponents->lpszUserName)
3856 return TRUE;
3857
3858 lstrcatW(lpszUrl, lpUrlComponents->lpszUserName);
3859
3860 if (lpUrlComponents->lpszPassword)
3861 {
3862 lstrcatW(lpszUrl, colonW);
3863
3864 if (!*lpUrlComponents->lpszPassword)
3865 return TRUE;
3866 else
3867 lstrcatW(lpszUrl, lpUrlComponents->lpszPassword);
3868 }
3869
3870 lstrcatW(lpszUrl, atW);
3871 }
3872
3873 lstrcatW(lpszUrl, lpUrlComponents->lpszHostName);
3874
3875 if (lpUrlComponents->nPort != 80 || (lpUrlComponents->lpszScheme &&
3876 strncmpW(lpUrlComponents->lpszScheme, httpW, lpUrlComponents->dwSchemeLength)))
3877 {
3878 WCHAR szPort[MAX_WORD_DIGITS];
3879
3880 sprintfW(szPort, percentD, lpUrlComponents->nPort);
3881 lstrcatW(lpszUrl, colonW);
3882 lstrcatW(lpszUrl, szPort);
3883 }
3884
3885 lstrcatW(lpszUrl, lpUrlComponents->lpszUrlPath);
3886
3887 return TRUE;
3888 }
3889
3890 /***********************************************************************
3891 * InternetConfirmZoneCrossingA (WININET.@)
3892 *
3893 */
3894 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
3895 {
3896 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
3897 return ERROR_SUCCESS;
3898 }
3899
3900 /***********************************************************************
3901 * InternetConfirmZoneCrossingW (WININET.@)
3902 *
3903 */
3904 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
3905 {
3906 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
3907 return ERROR_SUCCESS;
3908 }
3909
3910 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
3911 LPDWORD lpdwConnection, DWORD dwReserved )
3912 {
3913 FIXME("(%p, %p, 0x%08lx, %p, 0x%08lx) stub\n", hwndParent, lpszConnectoid, dwFlags,
3914 lpdwConnection, dwReserved);
3915 return ERROR_SUCCESS;
3916 }
3917
3918 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
3919 LPDWORD lpdwConnection, DWORD dwReserved )
3920 {
3921 FIXME("(%p, %p, 0x%08lx, %p, 0x%08lx) stub\n", hwndParent, lpszConnectoid, dwFlags,
3922 lpdwConnection, dwReserved);
3923 return ERROR_SUCCESS;
3924 }
3925
3926 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3927 {
3928 FIXME("(%s, %p, 0x%08lx) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
3929 return TRUE;
3930 }
3931
3932 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3933 {
3934 FIXME("(%s, %p, 0x%08lx) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
3935 return TRUE;
3936 }
3937
3938 DWORD WINAPI InternetHangUp( DWORD dwConnection, DWORD dwReserved )
3939 {
3940 FIXME("(0x%08lx, 0x%08lx) stub\n", dwConnection, dwReserved);
3941 return ERROR_SUCCESS;
3942 }
3943
3944 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
3945 PBYTE pbHexHash )
3946 {
3947 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
3948 debugstr_w(pwszTarget), pbHexHash);
3949 return FALSE;
3950 }
3951
3952 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
3953 {
3954 FIXME("stub\n");
3955 return TRUE;
3956 }
3957
3958 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, unsigned long *pcSiteNameSize,
3959 unsigned long *pdwDecision, unsigned long dwIndex )
3960 {
3961 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
3962 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
3963 return FALSE;
3964 }
3965
3966 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, unsigned long *pcSiteNameSize,
3967 unsigned long *pdwDecision, unsigned long dwIndex )
3968 {
3969 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
3970 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
3971 return FALSE;
3972 }
3973
3974 BOOL WINAPI InternetGetCookieExA( LPCSTR pchURL, LPCSTR pchCookieName, LPSTR pchCookieData,
3975 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
3976 {
3977 FIXME("(%s, %s, %s, %p, 0x%08lx, %p) stub\n",
3978 debugstr_a(pchURL), debugstr_a(pchCookieName), debugstr_a(pchCookieData),
3979 pcchCookieData, dwFlags, lpReserved);
3980 return FALSE;
3981 }
3982
3983 BOOL WINAPI InternetGetCookieExW( LPCWSTR pchURL, LPCWSTR pchCookieName, LPWSTR pchCookieData,
3984 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
3985 {
3986 FIXME("(%s, %s, %s, %p, 0x%08lx, %p) stub\n",
3987 debugstr_w(pchURL), debugstr_w(pchCookieName), debugstr_w(pchCookieData),
3988 pcchCookieData, dwFlags, lpReserved);
3989 return FALSE;
3990 }
3991
3992 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, unsigned long *pResult )
3993 {
3994 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
3995 return FALSE;
3996 }
3997
3998 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, unsigned long *pResult )
3999 {
4000 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
4001 return FALSE;
4002 }
4003
4004 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
4005 {
4006 FIXME("(%s, 0x%08lx) stub\n", debugstr_a(pchHostName), dwDecision);
4007 return FALSE;
4008 }
4009
4010 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
4011 {
4012 FIXME("(%s, 0x%08lx) stub\n", debugstr_w(pchHostName), dwDecision);
4013 return FALSE;
4014 }
4015
4016 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
4017 DWORD dwFlags, DWORD_PTR dwReserved)
4018 {
4019 FIXME("(%s, %s, %s, 0x%08lx, 0x%08lx) stub\n",
4020 debugstr_a(lpszURL), debugstr_a(lpszCookieName), debugstr_a(lpszCookieData),
4021 dwFlags, dwReserved);
4022 return TRUE;
4023 }
4024
4025 DWORD WINAPI InternetSetCookieExW( LPCWSTR lpszURL, LPCWSTR lpszCookieName, LPCWSTR lpszCookieData,
4026 DWORD dwFlags, DWORD_PTR dwReserved)
4027 {
4028 FIXME("(%s, %s, %s, 0x%08lx, 0x%08lx) stub\n",
4029 debugstr_w(lpszURL), debugstr_w(lpszCookieName), debugstr_w(lpszCookieData),
4030 dwFlags, dwReserved);
4031 return TRUE;
4032 }
4033
4034 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4035 {
4036 FIXME("(%p, 0x%08lx) stub\n", hInternet, dwError);
4037 return FALSE;
4038 }