Sync wininet with Wine. This fixes the Download! issue.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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_POLL_H
42 #include <poll.h>
43 #endif
44 #ifdef HAVE_SYS_POLL_H
45 # include <sys/poll.h>
46 #endif
47 #ifdef HAVE_SYS_TIME_H
48 # include <sys/time.h>
49 #endif
50 #include <stdlib.h>
51 #include <ctype.h>
52 #ifdef HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif
55 #include <assert.h>
56
57 #include "windef.h"
58 #include "winbase.h"
59 #include "winreg.h"
60 #include "winuser.h"
61 #include "wininet.h"
62 #include "winineti.h"
63 #include "winnls.h"
64 #include "wine/debug.h"
65 #include "winerror.h"
66 #define NO_SHLWAPI_STREAM
67 #include "shlwapi.h"
68
69 #include "wine/exception.h"
70
71 #include "internet.h"
72 #include "resource.h"
73
74 #include "wine/unicode.h"
75
76 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
77
78 #define RESPONSE_TIMEOUT 30
79
80 typedef struct
81 {
82 DWORD dwError;
83 CHAR response[MAX_REPLY_LEN];
84 } WITHREADERROR, *LPWITHREADERROR;
85
86 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
87 static HMODULE WININET_hModule;
88
89 #define HANDLE_CHUNK_SIZE 0x10
90
91 static CRITICAL_SECTION WININET_cs;
92 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
93 {
94 0, 0, &WININET_cs,
95 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
96 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
97 };
98 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
99
100 static LPWININETHANDLEHEADER *WININET_Handles;
101 static UINT WININET_dwNextHandle;
102 static UINT WININET_dwMaxHandles;
103
104 HINTERNET WININET_AllocHandle( LPWININETHANDLEHEADER info )
105 {
106 LPWININETHANDLEHEADER *p;
107 UINT handle = 0, num;
108
109 list_init( &info->children );
110
111 EnterCriticalSection( &WININET_cs );
112 if( !WININET_dwMaxHandles )
113 {
114 num = HANDLE_CHUNK_SIZE;
115 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
116 sizeof (UINT)* num);
117 if( !p )
118 goto end;
119 WININET_Handles = p;
120 WININET_dwMaxHandles = num;
121 }
122 if( WININET_dwMaxHandles == WININET_dwNextHandle )
123 {
124 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
125 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
126 WININET_Handles, sizeof (UINT)* num);
127 if( !p )
128 goto end;
129 WININET_Handles = p;
130 WININET_dwMaxHandles = num;
131 }
132
133 handle = WININET_dwNextHandle;
134 if( WININET_Handles[handle] )
135 ERR("handle isn't free but should be\n");
136 WININET_Handles[handle] = WININET_AddRef( info );
137
138 while( WININET_Handles[WININET_dwNextHandle] &&
139 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
140 WININET_dwNextHandle++;
141
142 end:
143 LeaveCriticalSection( &WININET_cs );
144
145 return info->hInternet = (HINTERNET) (handle+1);
146 }
147
148 LPWININETHANDLEHEADER WININET_AddRef( LPWININETHANDLEHEADER info )
149 {
150 ULONG refs = InterlockedIncrement(&info->refs);
151 TRACE("%p -> refcount = %d\n", info, refs );
152 return info;
153 }
154
155 LPWININETHANDLEHEADER WININET_GetObject( HINTERNET hinternet )
156 {
157 LPWININETHANDLEHEADER info = NULL;
158 UINT handle = (UINT) hinternet;
159
160 EnterCriticalSection( &WININET_cs );
161
162 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
163 WININET_Handles[handle-1] )
164 info = WININET_AddRef( WININET_Handles[handle-1] );
165
166 LeaveCriticalSection( &WININET_cs );
167
168 TRACE("handle %d -> %p\n", handle, info);
169
170 return info;
171 }
172
173 BOOL WININET_Release( LPWININETHANDLEHEADER info )
174 {
175 ULONG refs = InterlockedDecrement(&info->refs);
176 TRACE( "object %p refcount = %d\n", info, refs );
177 if( !refs )
178 {
179 if ( info->vtbl->CloseConnection )
180 {
181 TRACE( "closing connection %p\n", info);
182 info->vtbl->CloseConnection( info );
183 }
184 INTERNET_SendCallback(info, info->dwContext,
185 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
186 sizeof(HINTERNET));
187 TRACE( "destroying object %p\n", info);
188 if ( info->htype != WH_HINIT )
189 list_remove( &info->entry );
190 info->vtbl->Destroy( info );
191 }
192 return TRUE;
193 }
194
195 BOOL WININET_FreeHandle( HINTERNET hinternet )
196 {
197 BOOL ret = FALSE;
198 UINT handle = (UINT) hinternet;
199 LPWININETHANDLEHEADER info = NULL, child, next;
200
201 EnterCriticalSection( &WININET_cs );
202
203 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
204 {
205 handle--;
206 if( WININET_Handles[handle] )
207 {
208 info = WININET_Handles[handle];
209 TRACE( "destroying handle %d for object %p\n", handle+1, info);
210 WININET_Handles[handle] = NULL;
211 ret = TRUE;
212 }
213 }
214
215 LeaveCriticalSection( &WININET_cs );
216
217 /* As on native when the equivalent of WININET_Release is called, the handle
218 * is already invalid, but if a new handle is created at this time it does
219 * not yet get assigned the freed handle number */
220 if( info )
221 {
222 /* Free all children as native does */
223 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, WININETHANDLEHEADER, entry )
224 {
225 TRACE( "freeing child handle %d for parent handle %d\n",
226 (UINT)child->hInternet, handle+1);
227 WININET_FreeHandle( child->hInternet );
228 }
229 WININET_Release( info );
230 }
231
232 EnterCriticalSection( &WININET_cs );
233
234 if( WININET_dwNextHandle > handle && !WININET_Handles[handle] )
235 WININET_dwNextHandle = handle;
236
237 LeaveCriticalSection( &WININET_cs );
238
239 return ret;
240 }
241
242 /***********************************************************************
243 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
244 *
245 * PARAMS
246 * hinstDLL [I] handle to the DLL's instance
247 * fdwReason [I]
248 * lpvReserved [I] reserved, must be NULL
249 *
250 * RETURNS
251 * Success: TRUE
252 * Failure: FALSE
253 */
254
255 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
256 {
257 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
258
259 switch (fdwReason) {
260 case DLL_PROCESS_ATTACH:
261
262 g_dwTlsErrIndex = TlsAlloc();
263
264 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
265 return FALSE;
266
267 URLCacheContainers_CreateDefaults();
268
269 WININET_hModule = hinstDLL;
270
271 case DLL_THREAD_ATTACH:
272 break;
273
274 case DLL_THREAD_DETACH:
275 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
276 {
277 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
278 HeapFree(GetProcessHeap(), 0, lpwite);
279 }
280 break;
281
282 case DLL_PROCESS_DETACH:
283
284 URLCacheContainers_DeleteAll();
285
286 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
287 {
288 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
289 TlsFree(g_dwTlsErrIndex);
290 }
291 break;
292 }
293
294 return TRUE;
295 }
296
297
298 /***********************************************************************
299 * InternetInitializeAutoProxyDll (WININET.@)
300 *
301 * Setup the internal proxy
302 *
303 * PARAMETERS
304 * dwReserved
305 *
306 * RETURNS
307 * FALSE on failure
308 *
309 */
310 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
311 {
312 FIXME("STUB\n");
313 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
314 return FALSE;
315 }
316
317 /***********************************************************************
318 * DetectAutoProxyUrl (WININET.@)
319 *
320 * Auto detect the proxy url
321 *
322 * RETURNS
323 * FALSE on failure
324 *
325 */
326 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
327 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
328 {
329 FIXME("STUB\n");
330 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
331 return FALSE;
332 }
333
334
335 /***********************************************************************
336 * INTERNET_ConfigureProxy
337 *
338 * FIXME:
339 * The proxy may be specified in the form 'http=proxy.my.org'
340 * Presumably that means there can be ftp=ftpproxy.my.org too.
341 */
342 static BOOL INTERNET_ConfigureProxy( LPWININETAPPINFOW lpwai )
343 {
344 HKEY key;
345 DWORD type, len, enabled = 0;
346 LPCSTR envproxy;
347 static const WCHAR szInternetSettings[] =
348 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
349 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
350 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
351 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
352 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
353
354 if (RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )) return FALSE;
355
356 len = sizeof enabled;
357 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&enabled, &len ) || type != REG_DWORD)
358 RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&enabled, sizeof(REG_DWORD) );
359
360 if (enabled)
361 {
362 TRACE("Proxy is enabled.\n");
363
364 /* figure out how much memory the proxy setting takes */
365 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
366 {
367 LPWSTR szProxy, p;
368 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
369
370 if (!(szProxy = HeapAlloc( GetProcessHeap(), 0, len )))
371 {
372 RegCloseKey( key );
373 return FALSE;
374 }
375 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
376
377 /* find the http proxy, and strip away everything else */
378 p = strstrW( szProxy, szHttp );
379 if (p)
380 {
381 p += lstrlenW( szHttp );
382 lstrcpyW( szProxy, p );
383 }
384 p = strchrW( szProxy, ' ' );
385 if (p) *p = 0;
386
387 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
388 lpwai->lpszProxy = szProxy;
389
390 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
391 }
392 else
393 ERR("Couldn't read proxy server settings from registry.\n");
394 }
395 else if ((envproxy = getenv( "http_proxy" )))
396 {
397 WCHAR *envproxyW;
398
399 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
400 if (!(envproxyW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
401 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
402
403 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
404 lpwai->lpszProxy = envproxyW;
405
406 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwai->lpszProxy));
407 enabled = 1;
408 }
409 if (!enabled) TRACE("Proxy is not enabled.\n");
410
411 RegCloseKey( key );
412 return (enabled > 0);
413 }
414
415 /***********************************************************************
416 * dump_INTERNET_FLAGS
417 *
418 * Helper function to TRACE the internet flags.
419 *
420 * RETURNS
421 * None
422 *
423 */
424 static void dump_INTERNET_FLAGS(DWORD dwFlags)
425 {
426 #define FE(x) { x, #x }
427 static const wininet_flag_info flag[] = {
428 FE(INTERNET_FLAG_RELOAD),
429 FE(INTERNET_FLAG_RAW_DATA),
430 FE(INTERNET_FLAG_EXISTING_CONNECT),
431 FE(INTERNET_FLAG_ASYNC),
432 FE(INTERNET_FLAG_PASSIVE),
433 FE(INTERNET_FLAG_NO_CACHE_WRITE),
434 FE(INTERNET_FLAG_MAKE_PERSISTENT),
435 FE(INTERNET_FLAG_FROM_CACHE),
436 FE(INTERNET_FLAG_SECURE),
437 FE(INTERNET_FLAG_KEEP_CONNECTION),
438 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
439 FE(INTERNET_FLAG_READ_PREFETCH),
440 FE(INTERNET_FLAG_NO_COOKIES),
441 FE(INTERNET_FLAG_NO_AUTH),
442 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
443 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
444 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
445 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
446 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
447 FE(INTERNET_FLAG_RESYNCHRONIZE),
448 FE(INTERNET_FLAG_HYPERLINK),
449 FE(INTERNET_FLAG_NO_UI),
450 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
451 FE(INTERNET_FLAG_CACHE_ASYNC),
452 FE(INTERNET_FLAG_FORMS_SUBMIT),
453 FE(INTERNET_FLAG_NEED_FILE),
454 FE(INTERNET_FLAG_TRANSFER_ASCII),
455 FE(INTERNET_FLAG_TRANSFER_BINARY)
456 };
457 #undef FE
458 int i;
459
460 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
461 if (flag[i].val & dwFlags) {
462 TRACE(" %s", flag[i].name);
463 dwFlags &= ~flag[i].val;
464 }
465 }
466 if (dwFlags)
467 TRACE(" Unknown flags (%08x)\n", dwFlags);
468 else
469 TRACE("\n");
470 }
471
472 /***********************************************************************
473 * INTERNET_CloseHandle (internal)
474 *
475 * Close internet handle
476 *
477 */
478 static VOID APPINFO_Destroy(WININETHANDLEHEADER *hdr)
479 {
480 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW) hdr;
481
482 TRACE("%p\n",lpwai);
483
484 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
485 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
486 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
487 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
488 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
489 HeapFree(GetProcessHeap(), 0, lpwai);
490 }
491
492 static DWORD APPINFO_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
493 {
494 switch(option) {
495 case INTERNET_OPTION_HANDLE_TYPE:
496 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
497
498 if (*size < sizeof(ULONG))
499 return ERROR_INSUFFICIENT_BUFFER;
500
501 *size = sizeof(DWORD);
502 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
503 return ERROR_SUCCESS;
504 }
505
506 FIXME("Not implemented option %d\n", option);
507 return ERROR_INTERNET_INVALID_OPTION;
508 }
509
510 static const HANDLEHEADERVtbl APPINFOVtbl = {
511 APPINFO_Destroy,
512 NULL,
513 APPINFO_QueryOption,
514 NULL,
515 NULL,
516 NULL,
517 NULL,
518 NULL,
519 NULL
520 };
521
522
523 /***********************************************************************
524 * InternetOpenW (WININET.@)
525 *
526 * Per-application initialization of wininet
527 *
528 * RETURNS
529 * HINTERNET on success
530 * NULL on failure
531 *
532 */
533 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
534 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
535 {
536 LPWININETAPPINFOW lpwai = NULL;
537 HINTERNET handle = NULL;
538
539 if (TRACE_ON(wininet)) {
540 #define FE(x) { x, #x }
541 static const wininet_flag_info access_type[] = {
542 FE(INTERNET_OPEN_TYPE_PRECONFIG),
543 FE(INTERNET_OPEN_TYPE_DIRECT),
544 FE(INTERNET_OPEN_TYPE_PROXY),
545 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
546 };
547 #undef FE
548 DWORD i;
549 const char *access_type_str = "Unknown";
550
551 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
552 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
553 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
554 if (access_type[i].val == dwAccessType) {
555 access_type_str = access_type[i].name;
556 break;
557 }
558 }
559 TRACE(" access type : %s\n", access_type_str);
560 TRACE(" flags :");
561 dump_INTERNET_FLAGS(dwFlags);
562 }
563
564 /* Clear any error information */
565 INTERNET_SetLastError(0);
566
567 lpwai = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETAPPINFOW));
568 if (NULL == lpwai)
569 {
570 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
571 goto lend;
572 }
573
574 lpwai->hdr.htype = WH_HINIT;
575 lpwai->hdr.vtbl = &APPINFOVtbl;
576 lpwai->hdr.dwFlags = dwFlags;
577 lpwai->hdr.refs = 1;
578 lpwai->dwAccessType = dwAccessType;
579 lpwai->lpszProxyUsername = NULL;
580 lpwai->lpszProxyPassword = NULL;
581
582 handle = WININET_AllocHandle( &lpwai->hdr );
583 if( !handle )
584 {
585 HeapFree( GetProcessHeap(), 0, lpwai );
586 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
587 goto lend;
588 }
589
590 if (NULL != lpszAgent)
591 {
592 lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,
593 (strlenW(lpszAgent)+1)*sizeof(WCHAR));
594 if (lpwai->lpszAgent)
595 lstrcpyW( lpwai->lpszAgent, lpszAgent );
596 }
597 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
598 INTERNET_ConfigureProxy( lpwai );
599 else if (NULL != lpszProxy)
600 {
601 lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0,
602 (strlenW(lpszProxy)+1)*sizeof(WCHAR));
603 if (lpwai->lpszProxy)
604 lstrcpyW( lpwai->lpszProxy, lpszProxy );
605 }
606
607 if (NULL != lpszProxyBypass)
608 {
609 lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0,
610 (strlenW(lpszProxyBypass)+1)*sizeof(WCHAR));
611 if (lpwai->lpszProxyBypass)
612 lstrcpyW( lpwai->lpszProxyBypass, lpszProxyBypass );
613 }
614
615 lend:
616 if( lpwai )
617 WININET_Release( &lpwai->hdr );
618
619 TRACE("returning %p\n", lpwai);
620
621 return handle;
622 }
623
624
625 /***********************************************************************
626 * InternetOpenA (WININET.@)
627 *
628 * Per-application initialization of wininet
629 *
630 * RETURNS
631 * HINTERNET on success
632 * NULL on failure
633 *
634 */
635 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
636 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
637 {
638 HINTERNET rc = NULL;
639 INT len;
640 WCHAR *szAgent = NULL, *szProxy = NULL, *szBypass = NULL;
641
642 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
643 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
644
645 if( lpszAgent )
646 {
647 len = MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, NULL, 0);
648 szAgent = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
649 MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, szAgent, len);
650 }
651
652 if( lpszProxy )
653 {
654 len = MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, NULL, 0);
655 szProxy = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
656 MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, szProxy, len);
657 }
658
659 if( lpszProxyBypass )
660 {
661 len = MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, NULL, 0);
662 szBypass = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
663 MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, szBypass, len);
664 }
665
666 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
667
668 HeapFree(GetProcessHeap(), 0, szAgent);
669 HeapFree(GetProcessHeap(), 0, szProxy);
670 HeapFree(GetProcessHeap(), 0, szBypass);
671
672 return rc;
673 }
674
675 /***********************************************************************
676 * InternetGetLastResponseInfoA (WININET.@)
677 *
678 * Return last wininet error description on the calling thread
679 *
680 * RETURNS
681 * TRUE on success of writing to buffer
682 * FALSE on failure
683 *
684 */
685 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
686 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
687 {
688 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
689
690 TRACE("\n");
691
692 if (lpwite)
693 {
694 *lpdwError = lpwite->dwError;
695 if (lpwite->dwError)
696 {
697 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
698 *lpdwBufferLength = strlen(lpszBuffer);
699 }
700 else
701 *lpdwBufferLength = 0;
702 }
703 else
704 {
705 *lpdwError = 0;
706 *lpdwBufferLength = 0;
707 }
708
709 return TRUE;
710 }
711
712 /***********************************************************************
713 * InternetGetLastResponseInfoW (WININET.@)
714 *
715 * Return last wininet error description on the calling thread
716 *
717 * RETURNS
718 * TRUE on success of writing to buffer
719 * FALSE on failure
720 *
721 */
722 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
723 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
724 {
725 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
726
727 TRACE("\n");
728
729 if (lpwite)
730 {
731 *lpdwError = lpwite->dwError;
732 if (lpwite->dwError)
733 {
734 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
735 *lpdwBufferLength = lstrlenW(lpszBuffer);
736 }
737 else
738 *lpdwBufferLength = 0;
739 }
740 else
741 {
742 *lpdwError = 0;
743 *lpdwBufferLength = 0;
744 }
745
746 return TRUE;
747 }
748
749 /***********************************************************************
750 * InternetGetConnectedState (WININET.@)
751 *
752 * Return connected state
753 *
754 * RETURNS
755 * TRUE if connected
756 * if lpdwStatus is not null, return the status (off line,
757 * modem, lan...) in it.
758 * FALSE if not connected
759 */
760 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
761 {
762 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
763
764 if (lpdwStatus) {
765 FIXME("always returning LAN connection.\n");
766 *lpdwStatus = INTERNET_CONNECTION_LAN;
767 }
768 return TRUE;
769 }
770
771
772 /***********************************************************************
773 * InternetGetConnectedStateExW (WININET.@)
774 *
775 * Return connected state
776 *
777 * PARAMS
778 *
779 * lpdwStatus [O] Flags specifying the status of the internet connection.
780 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
781 * dwNameLen [I] Size of the buffer, in characters.
782 * dwReserved [I] Reserved. Must be set to 0.
783 *
784 * RETURNS
785 * TRUE if connected
786 * if lpdwStatus is not null, return the status (off line,
787 * modem, lan...) in it.
788 * FALSE if not connected
789 *
790 * NOTES
791 * If the system has no available network connections, an empty string is
792 * stored in lpszConnectionName. If there is a LAN connection, a localized
793 * "LAN Connection" string is stored. Presumably, if only a dial-up
794 * connection is available then the name of the dial-up connection is
795 * returned. Why any application, other than the "Internet Settings" CPL,
796 * would want to use this function instead of the simpler InternetGetConnectedStateW
797 * function is beyond me.
798 */
799 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
800 DWORD dwNameLen, DWORD dwReserved)
801 {
802 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
803
804 /* Must be zero */
805 if(dwReserved)
806 return FALSE;
807
808 if (lpdwStatus) {
809 FIXME("always returning LAN connection.\n");
810 *lpdwStatus = INTERNET_CONNECTION_LAN;
811 }
812 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
813 }
814
815
816 /***********************************************************************
817 * InternetGetConnectedStateExA (WININET.@)
818 */
819 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
820 DWORD dwNameLen, DWORD dwReserved)
821 {
822 LPWSTR lpwszConnectionName = NULL;
823 BOOL rc;
824
825 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
826
827 if (lpszConnectionName && dwNameLen > 0)
828 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
829
830 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
831 dwReserved);
832 if (rc && lpwszConnectionName)
833 {
834 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
835 dwNameLen, NULL, NULL);
836
837 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
838 }
839
840 return rc;
841 }
842
843
844 /***********************************************************************
845 * InternetConnectW (WININET.@)
846 *
847 * Open a ftp, gopher or http session
848 *
849 * RETURNS
850 * HINTERNET a session handle on success
851 * NULL on failure
852 *
853 */
854 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
855 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
856 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
857 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
858 {
859 LPWININETAPPINFOW hIC;
860 HINTERNET rc = NULL;
861
862 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
863 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
864 dwService, dwFlags, dwContext);
865
866 if (!lpszServerName)
867 {
868 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
869 return NULL;
870 }
871
872 /* Clear any error information */
873 INTERNET_SetLastError(0);
874 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
875 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
876 {
877 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
878 goto lend;
879 }
880
881 switch (dwService)
882 {
883 case INTERNET_SERVICE_FTP:
884 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
885 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
886 break;
887
888 case INTERNET_SERVICE_HTTP:
889 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
890 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
891 break;
892
893 case INTERNET_SERVICE_GOPHER:
894 default:
895 break;
896 }
897 lend:
898 if( hIC )
899 WININET_Release( &hIC->hdr );
900
901 TRACE("returning %p\n", rc);
902 return rc;
903 }
904
905
906 /***********************************************************************
907 * InternetConnectA (WININET.@)
908 *
909 * Open a ftp, gopher or http session
910 *
911 * RETURNS
912 * HINTERNET a session handle on success
913 * NULL on failure
914 *
915 */
916 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
917 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
918 LPCSTR lpszUserName, LPCSTR lpszPassword,
919 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
920 {
921 HINTERNET rc = NULL;
922 INT len = 0;
923 LPWSTR szServerName = NULL;
924 LPWSTR szUserName = NULL;
925 LPWSTR szPassword = NULL;
926
927 if (lpszServerName)
928 {
929 len = MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, NULL, 0);
930 szServerName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
931 MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, szServerName, len);
932 }
933 if (lpszUserName)
934 {
935 len = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
936 szUserName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
937 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, szUserName, len);
938 }
939 if (lpszPassword)
940 {
941 len = MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, NULL, 0);
942 szPassword = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
943 MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, szPassword, len);
944 }
945
946
947 rc = InternetConnectW(hInternet, szServerName, nServerPort,
948 szUserName, szPassword, dwService, dwFlags, dwContext);
949
950 HeapFree(GetProcessHeap(), 0, szServerName);
951 HeapFree(GetProcessHeap(), 0, szUserName);
952 HeapFree(GetProcessHeap(), 0, szPassword);
953 return rc;
954 }
955
956
957 /***********************************************************************
958 * InternetFindNextFileA (WININET.@)
959 *
960 * Continues a file search from a previous call to FindFirstFile
961 *
962 * RETURNS
963 * TRUE on success
964 * FALSE on failure
965 *
966 */
967 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
968 {
969 BOOL ret;
970 WIN32_FIND_DATAW fd;
971
972 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
973 if(lpvFindData)
974 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
975 return ret;
976 }
977
978 /***********************************************************************
979 * InternetFindNextFileW (WININET.@)
980 *
981 * Continues a file search from a previous call to FindFirstFile
982 *
983 * RETURNS
984 * TRUE on success
985 * FALSE on failure
986 *
987 */
988 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
989 {
990 WININETHANDLEHEADER *hdr;
991 DWORD res;
992
993 TRACE("\n");
994
995 hdr = WININET_GetObject(hFind);
996 if(!hdr) {
997 WARN("Invalid handle\n");
998 SetLastError(ERROR_INVALID_HANDLE);
999 return FALSE;
1000 }
1001
1002 if(hdr->vtbl->FindNextFileW) {
1003 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1004 }else {
1005 WARN("Handle doesn't support NextFile\n");
1006 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1007 }
1008
1009 WININET_Release(hdr);
1010
1011 if(res != ERROR_SUCCESS)
1012 SetLastError(res);
1013 return res == ERROR_SUCCESS;
1014 }
1015
1016 /***********************************************************************
1017 * InternetCloseHandle (WININET.@)
1018 *
1019 * Generic close handle function
1020 *
1021 * RETURNS
1022 * TRUE on success
1023 * FALSE on failure
1024 *
1025 */
1026 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1027 {
1028 LPWININETHANDLEHEADER lpwh;
1029
1030 TRACE("%p\n",hInternet);
1031
1032 lpwh = WININET_GetObject( hInternet );
1033 if (NULL == lpwh)
1034 {
1035 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1036 return FALSE;
1037 }
1038
1039 WININET_Release( lpwh );
1040 WININET_FreeHandle( hInternet );
1041
1042 return TRUE;
1043 }
1044
1045
1046 /***********************************************************************
1047 * ConvertUrlComponentValue (Internal)
1048 *
1049 * Helper function for InternetCrackUrlW
1050 *
1051 */
1052 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1053 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1054 LPCSTR lpszStart, LPCWSTR lpwszStart)
1055 {
1056 TRACE("%p %d %p %d %p %p\n", lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1057 if (*dwComponentLen != 0)
1058 {
1059 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1060 if (*lppszComponent == NULL)
1061 {
1062 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1063 if (lpwszComponent)
1064 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1065 else
1066 *lppszComponent = NULL;
1067 *dwComponentLen = nASCIILength;
1068 }
1069 else
1070 {
1071 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1072 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1073 (*lppszComponent)[ncpylen]=0;
1074 *dwComponentLen = ncpylen;
1075 }
1076 }
1077 }
1078
1079
1080 /***********************************************************************
1081 * InternetCrackUrlA (WININET.@)
1082 *
1083 * See InternetCrackUrlW.
1084 */
1085 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1086 LPURL_COMPONENTSA lpUrlComponents)
1087 {
1088 DWORD nLength;
1089 URL_COMPONENTSW UCW;
1090 WCHAR* lpwszUrl;
1091
1092 TRACE("(%s %u %x %p)\n", debugstr_a(lpszUrl), dwUrlLength, dwFlags, lpUrlComponents);
1093
1094 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1095 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1096 {
1097 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1098 return FALSE;
1099 }
1100
1101 if(dwUrlLength<=0)
1102 dwUrlLength=-1;
1103 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1104
1105 /* if dwUrlLength=-1 then nLength includes null but length to
1106 InternetCrackUrlW should not include it */
1107 if (dwUrlLength == -1) nLength--;
1108
1109 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1110 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1111
1112 memset(&UCW,0,sizeof(UCW));
1113 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1114 if(lpUrlComponents->dwHostNameLength!=0)
1115 UCW.dwHostNameLength= lpUrlComponents->dwHostNameLength;
1116 if(lpUrlComponents->dwUserNameLength!=0)
1117 UCW.dwUserNameLength=lpUrlComponents->dwUserNameLength;
1118 if(lpUrlComponents->dwPasswordLength!=0)
1119 UCW.dwPasswordLength=lpUrlComponents->dwPasswordLength;
1120 if(lpUrlComponents->dwUrlPathLength!=0)
1121 UCW.dwUrlPathLength=lpUrlComponents->dwUrlPathLength;
1122 if(lpUrlComponents->dwSchemeLength!=0)
1123 UCW.dwSchemeLength=lpUrlComponents->dwSchemeLength;
1124 if(lpUrlComponents->dwExtraInfoLength!=0)
1125 UCW.dwExtraInfoLength=lpUrlComponents->dwExtraInfoLength;
1126 if(!InternetCrackUrlW(lpwszUrl,nLength,dwFlags,&UCW))
1127 {
1128 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1129 return FALSE;
1130 }
1131
1132 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1133 UCW.lpszHostName, UCW.dwHostNameLength,
1134 lpszUrl, lpwszUrl);
1135 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1136 UCW.lpszUserName, UCW.dwUserNameLength,
1137 lpszUrl, lpwszUrl);
1138 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1139 UCW.lpszPassword, UCW.dwPasswordLength,
1140 lpszUrl, lpwszUrl);
1141 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1142 UCW.lpszUrlPath, UCW.dwUrlPathLength,
1143 lpszUrl, lpwszUrl);
1144 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1145 UCW.lpszScheme, UCW.dwSchemeLength,
1146 lpszUrl, lpwszUrl);
1147 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1148 UCW.lpszExtraInfo, UCW.dwExtraInfoLength,
1149 lpszUrl, lpwszUrl);
1150 lpUrlComponents->nScheme=UCW.nScheme;
1151 lpUrlComponents->nPort=UCW.nPort;
1152 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1153
1154 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1155 debugstr_an(lpUrlComponents->lpszScheme,lpUrlComponents->dwSchemeLength),
1156 debugstr_an(lpUrlComponents->lpszHostName,lpUrlComponents->dwHostNameLength),
1157 debugstr_an(lpUrlComponents->lpszUrlPath,lpUrlComponents->dwUrlPathLength),
1158 debugstr_an(lpUrlComponents->lpszExtraInfo,lpUrlComponents->dwExtraInfoLength));
1159
1160 return TRUE;
1161 }
1162
1163 static const WCHAR url_schemes[][7] =
1164 {
1165 {'f','t','p',0},
1166 {'g','o','p','h','e','r',0},
1167 {'h','t','t','p',0},
1168 {'h','t','t','p','s',0},
1169 {'f','i','l','e',0},
1170 {'n','e','w','s',0},
1171 {'m','a','i','l','t','o',0},
1172 {'r','e','s',0},
1173 };
1174
1175 /***********************************************************************
1176 * GetInternetSchemeW (internal)
1177 *
1178 * Get scheme of url
1179 *
1180 * RETURNS
1181 * scheme on success
1182 * INTERNET_SCHEME_UNKNOWN on failure
1183 *
1184 */
1185 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1186 {
1187 int i;
1188
1189 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1190
1191 if(lpszScheme==NULL)
1192 return INTERNET_SCHEME_UNKNOWN;
1193
1194 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1195 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1196 return INTERNET_SCHEME_FIRST + i;
1197
1198 return INTERNET_SCHEME_UNKNOWN;
1199 }
1200
1201 /***********************************************************************
1202 * SetUrlComponentValueW (Internal)
1203 *
1204 * Helper function for InternetCrackUrlW
1205 *
1206 * PARAMS
1207 * lppszComponent [O] Holds the returned string
1208 * dwComponentLen [I] Holds the size of lppszComponent
1209 * [O] Holds the length of the string in lppszComponent without '\0'
1210 * lpszStart [I] Holds the string to copy from
1211 * len [I] Holds the length of lpszStart without '\0'
1212 *
1213 * RETURNS
1214 * TRUE on success
1215 * FALSE on failure
1216 *
1217 */
1218 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1219 {
1220 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1221
1222 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1223 return FALSE;
1224
1225 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1226 {
1227 if (*lppszComponent == NULL)
1228 {
1229 *lppszComponent = (LPWSTR)lpszStart;
1230 *dwComponentLen = len;
1231 }
1232 else
1233 {
1234 DWORD ncpylen = min((*dwComponentLen)-1, len);
1235 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1236 (*lppszComponent)[ncpylen] = '\0';
1237 *dwComponentLen = ncpylen;
1238 }
1239 }
1240
1241 return TRUE;
1242 }
1243
1244 /***********************************************************************
1245 * InternetCrackUrlW (WININET.@)
1246 *
1247 * Break up URL into its components
1248 *
1249 * RETURNS
1250 * TRUE on success
1251 * FALSE on failure
1252 */
1253 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1254 LPURL_COMPONENTSW lpUC)
1255 {
1256 /*
1257 * RFC 1808
1258 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1259 *
1260 */
1261 LPCWSTR lpszParam = NULL;
1262 BOOL bIsAbsolute = FALSE;
1263 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1264 LPCWSTR lpszcp = NULL;
1265 LPWSTR lpszUrl_decode = NULL;
1266 DWORD dwUrlLength = dwUrlLength_orig;
1267 const WCHAR lpszSeparators[3]={';','?',0};
1268 const WCHAR lpszSlash[2]={'/',0};
1269
1270 TRACE("(%s %u %x %p)\n", debugstr_w(lpszUrl), dwUrlLength, dwFlags, lpUC);
1271
1272 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1273 {
1274 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1275 return FALSE;
1276 }
1277 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1278
1279 if (dwFlags & ICU_DECODE)
1280 {
1281 lpszUrl_decode=HeapAlloc( GetProcessHeap(), 0, dwUrlLength * sizeof (WCHAR) );
1282 if( InternetCanonicalizeUrlW(lpszUrl_orig, lpszUrl_decode, &dwUrlLength, dwFlags))
1283 {
1284 lpszUrl = lpszUrl_decode;
1285 }
1286 }
1287 lpszap = lpszUrl;
1288
1289 /* Determine if the URI is absolute. */
1290 while (*lpszap != '\0')
1291 {
1292 if (isalnumW(*lpszap))
1293 {
1294 lpszap++;
1295 continue;
1296 }
1297 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1298 {
1299 bIsAbsolute = TRUE;
1300 lpszcp = lpszap;
1301 }
1302 else
1303 {
1304 lpszcp = lpszUrl; /* Relative url */
1305 }
1306
1307 break;
1308 }
1309
1310 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1311 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1312
1313 /* Parse <params> */
1314 lpszParam = strpbrkW(lpszap, lpszSeparators);
1315 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1316 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1317
1318 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1319 {
1320 LPCWSTR lpszNetLoc;
1321
1322 /* Get scheme first. */
1323 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1324 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1325 lpszUrl, lpszcp - lpszUrl);
1326
1327 /* Eat ':' in protocol. */
1328 lpszcp++;
1329
1330 /* double slash indicates the net_loc portion is present */
1331 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1332 {
1333 lpszcp += 2;
1334
1335 lpszNetLoc = strpbrkW(lpszcp, lpszSlash);
1336 if (lpszParam)
1337 {
1338 if (lpszNetLoc)
1339 lpszNetLoc = min(lpszNetLoc, lpszParam);
1340 else
1341 lpszNetLoc = lpszParam;
1342 }
1343 else if (!lpszNetLoc)
1344 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1345
1346 /* Parse net-loc */
1347 if (lpszNetLoc)
1348 {
1349 LPCWSTR lpszHost;
1350 LPCWSTR lpszPort;
1351
1352 /* [<user>[<:password>]@]<host>[:<port>] */
1353 /* First find the user and password if they exist */
1354
1355 lpszHost = strchrW(lpszcp, '@');
1356 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1357 {
1358 /* username and password not specified. */
1359 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1360 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1361 }
1362 else /* Parse out username and password */
1363 {
1364 LPCWSTR lpszUser = lpszcp;
1365 LPCWSTR lpszPasswd = lpszHost;
1366
1367 while (lpszcp < lpszHost)
1368 {
1369 if (*lpszcp == ':')
1370 lpszPasswd = lpszcp;
1371
1372 lpszcp++;
1373 }
1374
1375 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1376 lpszUser, lpszPasswd - lpszUser);
1377
1378 if (lpszPasswd != lpszHost)
1379 lpszPasswd++;
1380 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1381 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1382 lpszHost - lpszPasswd);
1383
1384 lpszcp++; /* Advance to beginning of host */
1385 }
1386
1387 /* Parse <host><:port> */
1388
1389 lpszHost = lpszcp;
1390 lpszPort = lpszNetLoc;
1391
1392 /* special case for res:// URLs: there is no port here, so the host is the
1393 entire string up to the first '/' */
1394 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1395 {
1396 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1397 lpszHost, lpszPort - lpszHost);
1398 lpszcp=lpszNetLoc;
1399 }
1400 else
1401 {
1402 while (lpszcp < lpszNetLoc)
1403 {
1404 if (*lpszcp == ':')
1405 lpszPort = lpszcp;
1406
1407 lpszcp++;
1408 }
1409
1410 /* If the scheme is "file" and the host is just one letter, it's not a host */
1411 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1412 {
1413 lpszcp=lpszHost;
1414 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1415 NULL, 0);
1416 }
1417 else
1418 {
1419 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1420 lpszHost, lpszPort - lpszHost);
1421 if (lpszPort != lpszNetLoc)
1422 lpUC->nPort = atoiW(++lpszPort);
1423 else switch (lpUC->nScheme)
1424 {
1425 case INTERNET_SCHEME_HTTP:
1426 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1427 break;
1428 case INTERNET_SCHEME_HTTPS:
1429 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1430 break;
1431 case INTERNET_SCHEME_FTP:
1432 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1433 break;
1434 case INTERNET_SCHEME_GOPHER:
1435 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1436 break;
1437 default:
1438 break;
1439 }
1440 }
1441 }
1442 }
1443 }
1444 else
1445 {
1446 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1447 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1448 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1449 }
1450 }
1451 else
1452 {
1453 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1454 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1455 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1456 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1457 }
1458
1459 /* Here lpszcp points to:
1460 *
1461 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1462 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1463 */
1464 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
1465 {
1466 INT len;
1467
1468 /* Only truncate the parameter list if it's already been saved
1469 * in lpUC->lpszExtraInfo.
1470 */
1471 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1472 len = lpszParam - lpszcp;
1473 else
1474 {
1475 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1476 * newlines if necessary.
1477 */
1478 LPWSTR lpsznewline = strchrW(lpszcp, '\n');
1479 if (lpsznewline != NULL)
1480 len = lpsznewline - lpszcp;
1481 else
1482 len = dwUrlLength-(lpszcp-lpszUrl);
1483 }
1484 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1485 lpszcp, len);
1486 }
1487 else
1488 {
1489 lpUC->dwUrlPathLength = 0;
1490 }
1491
1492 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1493 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1494 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1495 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1496 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1497
1498 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1499 return TRUE;
1500 }
1501
1502 /***********************************************************************
1503 * InternetAttemptConnect (WININET.@)
1504 *
1505 * Attempt to make a connection to the internet
1506 *
1507 * RETURNS
1508 * ERROR_SUCCESS on success
1509 * Error value on failure
1510 *
1511 */
1512 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1513 {
1514 FIXME("Stub\n");
1515 return ERROR_SUCCESS;
1516 }
1517
1518
1519 /***********************************************************************
1520 * InternetCanonicalizeUrlA (WININET.@)
1521 *
1522 * Escape unsafe characters and spaces
1523 *
1524 * RETURNS
1525 * TRUE on success
1526 * FALSE on failure
1527 *
1528 */
1529 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1530 LPDWORD lpdwBufferLength, DWORD dwFlags)
1531 {
1532 HRESULT hr;
1533 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1534
1535 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1536 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1537
1538 if(dwFlags & ICU_DECODE)
1539 {
1540 dwURLFlags |= URL_UNESCAPE;
1541 dwFlags &= ~ICU_DECODE;
1542 }
1543
1544 if(dwFlags & ICU_ESCAPE)
1545 {
1546 dwURLFlags |= URL_UNESCAPE;
1547 dwFlags &= ~ICU_ESCAPE;
1548 }
1549
1550 if(dwFlags & ICU_BROWSER_MODE)
1551 {
1552 dwURLFlags |= URL_BROWSER_MODE;
1553 dwFlags &= ~ICU_BROWSER_MODE;
1554 }
1555
1556 if(dwFlags & ICU_NO_ENCODE)
1557 {
1558 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1559 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1560 dwFlags &= ~ICU_NO_ENCODE;
1561 }
1562
1563 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1564
1565 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1566 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1567 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1568
1569 return (hr == S_OK) ? TRUE : FALSE;
1570 }
1571
1572 /***********************************************************************
1573 * InternetCanonicalizeUrlW (WININET.@)
1574 *
1575 * Escape unsafe characters and spaces
1576 *
1577 * RETURNS
1578 * TRUE on success
1579 * FALSE on failure
1580 *
1581 */
1582 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1583 LPDWORD lpdwBufferLength, DWORD dwFlags)
1584 {
1585 HRESULT hr;
1586 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1587
1588 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1589 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1590
1591 if(dwFlags & ICU_DECODE)
1592 {
1593 dwURLFlags |= URL_UNESCAPE;
1594 dwFlags &= ~ICU_DECODE;
1595 }
1596
1597 if(dwFlags & ICU_ESCAPE)
1598 {
1599 dwURLFlags |= URL_UNESCAPE;
1600 dwFlags &= ~ICU_ESCAPE;
1601 }
1602
1603 if(dwFlags & ICU_BROWSER_MODE)
1604 {
1605 dwURLFlags |= URL_BROWSER_MODE;
1606 dwFlags &= ~ICU_BROWSER_MODE;
1607 }
1608
1609 if(dwFlags & ICU_NO_ENCODE)
1610 {
1611 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1612 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1613 dwFlags &= ~ICU_NO_ENCODE;
1614 }
1615
1616 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1617
1618 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1619 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1620 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1621
1622 return (hr == S_OK) ? TRUE : FALSE;
1623 }
1624
1625 /* #################################################### */
1626
1627 static INTERNET_STATUS_CALLBACK set_status_callback(
1628 LPWININETHANDLEHEADER lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1629 {
1630 INTERNET_STATUS_CALLBACK ret;
1631
1632 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
1633 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1634
1635 ret = lpwh->lpfnStatusCB;
1636 lpwh->lpfnStatusCB = callback;
1637
1638 return ret;
1639 }
1640
1641 /***********************************************************************
1642 * InternetSetStatusCallbackA (WININET.@)
1643 *
1644 * Sets up a callback function which is called as progress is made
1645 * during an operation.
1646 *
1647 * RETURNS
1648 * Previous callback or NULL on success
1649 * INTERNET_INVALID_STATUS_CALLBACK on failure
1650 *
1651 */
1652 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1653 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1654 {
1655 INTERNET_STATUS_CALLBACK retVal;
1656 LPWININETHANDLEHEADER lpwh;
1657
1658 TRACE("0x%08x\n", (ULONG)hInternet);
1659
1660 if (!(lpwh = WININET_GetObject(hInternet)))
1661 return INTERNET_INVALID_STATUS_CALLBACK;
1662
1663 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
1664
1665 WININET_Release( lpwh );
1666 return retVal;
1667 }
1668
1669 /***********************************************************************
1670 * InternetSetStatusCallbackW (WININET.@)
1671 *
1672 * Sets up a callback function which is called as progress is made
1673 * during an operation.
1674 *
1675 * RETURNS
1676 * Previous callback or NULL on success
1677 * INTERNET_INVALID_STATUS_CALLBACK on failure
1678 *
1679 */
1680 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1681 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1682 {
1683 INTERNET_STATUS_CALLBACK retVal;
1684 LPWININETHANDLEHEADER lpwh;
1685
1686 TRACE("0x%08x\n", (ULONG)hInternet);
1687
1688 if (!(lpwh = WININET_GetObject(hInternet)))
1689 return INTERNET_INVALID_STATUS_CALLBACK;
1690
1691 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
1692
1693 WININET_Release( lpwh );
1694 return retVal;
1695 }
1696
1697 /***********************************************************************
1698 * InternetSetFilePointer (WININET.@)
1699 */
1700 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1701 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
1702 {
1703 FIXME("stub\n");
1704 return FALSE;
1705 }
1706
1707 /***********************************************************************
1708 * InternetWriteFile (WININET.@)
1709 *
1710 * Write data to an open internet file
1711 *
1712 * RETURNS
1713 * TRUE on success
1714 * FALSE on failure
1715 *
1716 */
1717 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
1718 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1719 {
1720 LPWININETHANDLEHEADER lpwh;
1721 BOOL retval = FALSE;
1722
1723 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1724
1725 lpwh = WININET_GetObject( hFile );
1726 if (!lpwh) {
1727 WARN("Invalid handle\n");
1728 SetLastError(ERROR_INVALID_HANDLE);
1729 return FALSE;
1730 }
1731
1732 if(lpwh->vtbl->WriteFile) {
1733 retval = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1734 }else {
1735 WARN("No Writefile method.\n");
1736 SetLastError(ERROR_INVALID_HANDLE);
1737 retval = FALSE;
1738 }
1739
1740 WININET_Release( lpwh );
1741
1742 return retval;
1743 }
1744
1745
1746 /***********************************************************************
1747 * InternetReadFile (WININET.@)
1748 *
1749 * Read data from an open internet file
1750 *
1751 * RETURNS
1752 * TRUE on success
1753 * FALSE on failure
1754 *
1755 */
1756 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1757 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1758 {
1759 LPWININETHANDLEHEADER hdr;
1760 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1761
1762 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1763
1764 hdr = WININET_GetObject(hFile);
1765 if (!hdr) {
1766 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1767 return FALSE;
1768 }
1769
1770 if(hdr->vtbl->ReadFile)
1771 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1772
1773 WININET_Release(hdr);
1774
1775 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
1776 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1777
1778 if(res != ERROR_SUCCESS)
1779 SetLastError(res);
1780 return res == ERROR_SUCCESS;
1781 }
1782
1783 /***********************************************************************
1784 * InternetReadFileExA (WININET.@)
1785 *
1786 * Read data from an open internet file
1787 *
1788 * PARAMS
1789 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1790 * lpBuffersOut [I/O] Buffer.
1791 * dwFlags [I] Flags. See notes.
1792 * dwContext [I] Context for callbacks.
1793 *
1794 * RETURNS
1795 * TRUE on success
1796 * FALSE on failure
1797 *
1798 * NOTES
1799 * The parameter dwFlags include zero or more of the following flags:
1800 *|IRF_ASYNC - Makes the call asynchronous.
1801 *|IRF_SYNC - Makes the call synchronous.
1802 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1803 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1804 *
1805 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1806 *
1807 * SEE
1808 * InternetOpenUrlA(), HttpOpenRequestA()
1809 */
1810 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1811 DWORD dwFlags, DWORD_PTR dwContext)
1812 {
1813 LPWININETHANDLEHEADER hdr;
1814 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1815
1816 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1817
1818 hdr = WININET_GetObject(hFile);
1819 if (!hdr) {
1820 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1821 return FALSE;
1822 }
1823
1824 if(hdr->vtbl->ReadFileExA)
1825 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
1826
1827 WININET_Release(hdr);
1828
1829 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1830 res, lpBuffersOut->dwBufferLength);
1831
1832 if(res != ERROR_SUCCESS)
1833 SetLastError(res);
1834 return res == ERROR_SUCCESS;
1835 }
1836
1837 /***********************************************************************
1838 * InternetReadFileExW (WININET.@)
1839 *
1840 * Read data from an open internet file.
1841 *
1842 * PARAMS
1843 * hFile [I] Handle returned by InternetOpenUrl() or HttpOpenRequest().
1844 * lpBuffersOut [I/O] Buffer.
1845 * dwFlags [I] Flags.
1846 * dwContext [I] Context for callbacks.
1847 *
1848 * RETURNS
1849 * FALSE, last error is set to ERROR_CALL_NOT_IMPLEMENTED
1850 *
1851 * NOTES
1852 * Not implemented in Wine or native either (as of IE6 SP2).
1853 *
1854 */
1855 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1856 DWORD dwFlags, DWORD_PTR dwContext)
1857 {
1858 ERR("(%p, %p, 0x%x, 0x%lx): not implemented in native\n", hFile, lpBuffer, dwFlags, dwContext);
1859
1860 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1861 return FALSE;
1862 }
1863
1864 /***********************************************************************
1865 * INET_QueryOptionHelper (internal)
1866 */
1867 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
1868 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1869 {
1870 LPWININETHANDLEHEADER lpwhh;
1871 BOOL bSuccess = FALSE;
1872
1873 TRACE("(%p, 0x%08x, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
1874
1875 lpwhh = WININET_GetObject( hInternet );
1876
1877 switch (dwOption)
1878 {
1879 case INTERNET_OPTION_REQUEST_FLAGS:
1880 {
1881 ULONG flags = 4;
1882 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %d\n", flags);
1883 if (*lpdwBufferLength < sizeof(ULONG))
1884 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1885 else
1886 {
1887 memcpy(lpBuffer, &flags, sizeof(ULONG));
1888 bSuccess = TRUE;
1889 }
1890 *lpdwBufferLength = sizeof(ULONG);
1891 break;
1892 }
1893
1894 case INTERNET_OPTION_USER_AGENT:
1895 {
1896 DWORD required;
1897 LPWININETAPPINFOW ai = (LPWININETAPPINFOW)lpwhh;
1898
1899 TRACE("INTERNET_OPTION_USER_AGENT\n");
1900
1901 if (lpwhh->htype != INTERNET_HANDLE_TYPE_INTERNET)
1902 {
1903 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1904 return FALSE;
1905 }
1906 if (bIsUnicode)
1907 {
1908 required = (strlenW(ai->lpszAgent) + 1) * sizeof(WCHAR);
1909 if (*lpdwBufferLength < required)
1910 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1911 else if (lpBuffer)
1912 {
1913 strcpyW(lpBuffer, ai->lpszAgent);
1914 bSuccess = TRUE;
1915 }
1916 }
1917 else
1918 {
1919 required = WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, NULL, 0, NULL, NULL);
1920 if (*lpdwBufferLength < required)
1921 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1922 else if (lpBuffer)
1923 {
1924 WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, lpBuffer, required, NULL, NULL);
1925 bSuccess = TRUE;
1926 }
1927 }
1928 *lpdwBufferLength = required;
1929 break;
1930 }
1931 case INTERNET_OPTION_HTTP_VERSION:
1932 {
1933 if (*lpdwBufferLength < sizeof(HTTP_VERSION_INFO))
1934 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1935 else
1936 {
1937 /*
1938 * Presently hardcoded to 1.1
1939 */
1940 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1941 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1942 bSuccess = TRUE;
1943 }
1944 *lpdwBufferLength = sizeof(HTTP_VERSION_INFO);
1945 break;
1946 }
1947 case INTERNET_OPTION_CONNECTED_STATE:
1948 {
1949 DWORD *pdwConnectedState = (DWORD *)lpBuffer;
1950 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
1951
1952 if (*lpdwBufferLength < sizeof(*pdwConnectedState))
1953 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1954 else
1955 {
1956 *pdwConnectedState = INTERNET_STATE_CONNECTED;
1957 bSuccess = TRUE;
1958 }
1959 *lpdwBufferLength = sizeof(*pdwConnectedState);
1960 break;
1961 }
1962 case INTERNET_OPTION_PROXY:
1963 {
1964 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW)lpwhh;
1965 WININETAPPINFOW wai;
1966
1967 if (lpwai == NULL)
1968 {
1969 TRACE("Getting global proxy info\n");
1970 memset(&wai, 0, sizeof(WININETAPPINFOW));
1971 INTERNET_ConfigureProxy( &wai );
1972 lpwai = &wai;
1973 }
1974
1975 if (bIsUnicode)
1976 {
1977 INTERNET_PROXY_INFOW *pPI = (INTERNET_PROXY_INFOW *)lpBuffer;
1978 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
1979
1980 if (lpwai->lpszProxy)
1981 proxyBytesRequired = (lstrlenW(lpwai->lpszProxy) + 1) *
1982 sizeof(WCHAR);
1983 if (lpwai->lpszProxyBypass)
1984 proxyBypassBytesRequired =
1985 (lstrlenW(lpwai->lpszProxyBypass) + 1) * sizeof(WCHAR);
1986 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOW) +
1987 proxyBytesRequired + proxyBypassBytesRequired)
1988 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1989 else
1990 {
1991 LPWSTR proxy = (LPWSTR)((LPBYTE)lpBuffer +
1992 sizeof(INTERNET_PROXY_INFOW));
1993 LPWSTR proxy_bypass = (LPWSTR)((LPBYTE)lpBuffer +
1994 sizeof(INTERNET_PROXY_INFOW) +
1995 proxyBytesRequired);
1996
1997 pPI->dwAccessType = lpwai->dwAccessType;
1998 pPI->lpszProxy = NULL;
1999 pPI->lpszProxyBypass = NULL;
2000 if (lpwai->lpszProxy)
2001 {
2002 lstrcpyW(proxy, lpwai->lpszProxy);
2003 pPI->lpszProxy = proxy;
2004 }
2005
2006 if (lpwai->lpszProxyBypass)
2007 {
2008 lstrcpyW(proxy_bypass, lpwai->lpszProxyBypass);
2009 pPI->lpszProxyBypass = proxy_bypass;
2010 }
2011 bSuccess = TRUE;
2012 }
2013 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOW) +
2014 proxyBytesRequired + proxyBypassBytesRequired;
2015 }
2016 else
2017 {
2018 INTERNET_PROXY_INFOA *pPI = (INTERNET_PROXY_INFOA *)lpBuffer;
2019 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
2020
2021 if (lpwai->lpszProxy)
2022 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0,
2023 lpwai->lpszProxy, -1, NULL, 0, NULL, NULL);
2024 if (lpwai->lpszProxyBypass)
2025 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0,
2026 lpwai->lpszProxyBypass, -1, NULL, 0, NULL, NULL);
2027 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOA) +
2028 proxyBytesRequired + proxyBypassBytesRequired)
2029 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2030 else
2031 {
2032 LPSTR proxy = (LPSTR)((LPBYTE)lpBuffer +
2033 sizeof(INTERNET_PROXY_INFOA));
2034 LPSTR proxy_bypass = (LPSTR)((LPBYTE)lpBuffer +
2035 sizeof(INTERNET_PROXY_INFOA) +
2036 proxyBytesRequired);
2037
2038 pPI->dwAccessType = lpwai->dwAccessType;
2039 pPI->lpszProxy = NULL;
2040 pPI->lpszProxyBypass = NULL;
2041 if (lpwai->lpszProxy)
2042 {
2043 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxy, -1,
2044 proxy, proxyBytesRequired, NULL, NULL);
2045 pPI->lpszProxy = proxy;
2046 }
2047
2048 if (lpwai->lpszProxyBypass)
2049 {
2050 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxyBypass,
2051 -1, proxy_bypass, proxyBypassBytesRequired,
2052 NULL, NULL);
2053 pPI->lpszProxyBypass = proxy_bypass;
2054 }
2055 bSuccess = TRUE;
2056 }
2057 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOA) +
2058 proxyBytesRequired + proxyBypassBytesRequired;
2059 }
2060 break;
2061 }
2062 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2063 {
2064 ULONG conn = 2;
2065 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER: %d\n", conn);
2066 if (*lpdwBufferLength < sizeof(ULONG))
2067 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2068 else
2069 {
2070 memcpy(lpBuffer, &conn, sizeof(ULONG));
2071 bSuccess = TRUE;
2072 }
2073 *lpdwBufferLength = sizeof(ULONG);
2074 break;
2075 }
2076 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2077 {
2078 ULONG conn = 4;
2079 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER: %d\n", conn);
2080 if (*lpdwBufferLength < sizeof(ULONG))
2081 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2082 else
2083 {
2084 memcpy(lpBuffer, &conn, sizeof(ULONG));
2085 bSuccess = TRUE;
2086 }
2087 *lpdwBufferLength = sizeof(ULONG);
2088 break;
2089 }
2090 case INTERNET_OPTION_SECURITY_FLAGS:
2091 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2092 bSuccess = TRUE;
2093 break;
2094
2095 case INTERNET_OPTION_VERSION:
2096 {
2097 TRACE("INTERNET_OPTION_VERSION\n");
2098 if (*lpdwBufferLength < sizeof(INTERNET_VERSION_INFO))
2099 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2100 else
2101 {
2102 static const INTERNET_VERSION_INFO info = { 1, 2 };
2103 memcpy(lpBuffer, &info, sizeof(info));
2104 *lpdwBufferLength = sizeof(info);
2105 bSuccess = TRUE;
2106 }
2107 break;
2108 }
2109 case INTERNET_OPTION_PER_CONNECTION_OPTION:
2110 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2111 if (*lpdwBufferLength < sizeof(INTERNET_PER_CONN_OPTION_LISTW))
2112 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2113 else
2114 {
2115 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2116 int x;
2117 bSuccess = TRUE;
2118 for (x = 0; x < con->dwOptionCount; ++x)
2119 {
2120 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + x;
2121 switch (option->dwOption)
2122 {
2123 case INTERNET_PER_CONN_FLAGS:
2124 option->Value.dwValue = PROXY_TYPE_DIRECT;
2125 break;
2126
2127 case INTERNET_PER_CONN_PROXY_SERVER:
2128 case INTERNET_PER_CONN_PROXY_BYPASS:
2129 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2130 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2131 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2132 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2133 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2134 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2135 FIXME("Unhandled dwOption %d\n", option->dwOption);
2136 option->Value.dwValue = 0;
2137 bSuccess = FALSE;
2138 break;
2139
2140 default:
2141 FIXME("Unknown dwOption %d\n", option->dwOption);
2142 bSuccess = FALSE;
2143 break;
2144 }
2145 }
2146 if (!bSuccess)
2147 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2148 }
2149 break;
2150 case 66:
2151 FIXME("66\n");
2152 bSuccess = TRUE;
2153 break;
2154 default: {
2155 if(lpwhh) {
2156 DWORD res;
2157
2158 res = lpwhh->vtbl->QueryOption(lpwhh, dwOption, lpBuffer, lpdwBufferLength, bIsUnicode);
2159 if(res == ERROR_SUCCESS)
2160 bSuccess = TRUE;
2161 else
2162 SetLastError(res);
2163 }else {
2164 FIXME("Stub! %d\n", dwOption);
2165 break;
2166 }
2167 }
2168 }
2169 if (lpwhh)
2170 WININET_Release( lpwhh );
2171
2172 return bSuccess;
2173 }
2174
2175 /***********************************************************************
2176 * InternetQueryOptionW (WININET.@)
2177 *
2178 * Queries an options on the specified handle
2179 *
2180 * RETURNS
2181 * TRUE on success
2182 * FALSE on failure
2183 *
2184 */
2185 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2186 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2187 {
2188 return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2189 }
2190
2191 /***********************************************************************
2192 * InternetQueryOptionA (WININET.@)
2193 *
2194 * Queries an options on the specified handle
2195 *
2196 * RETURNS
2197 * TRUE on success
2198 * FALSE on failure
2199 *
2200 */
2201 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2202 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2203 {
2204 return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2205 }
2206
2207
2208 /***********************************************************************
2209 * InternetSetOptionW (WININET.@)
2210 *
2211 * Sets an options on the specified handle
2212 *
2213 * RETURNS
2214 * TRUE on success
2215 * FALSE on failure
2216 *
2217 */
2218 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2219 LPVOID lpBuffer, DWORD dwBufferLength)
2220 {
2221 LPWININETHANDLEHEADER lpwhh;
2222 BOOL ret = TRUE;
2223
2224 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2225
2226 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
2227 if(lpwhh && lpwhh->vtbl->SetOption) {
2228 DWORD res;
2229
2230 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2231 if(res != ERROR_INTERNET_INVALID_OPTION) {
2232 WININET_Release( lpwhh );
2233
2234 if(res != ERROR_SUCCESS)
2235 SetLastError(res);
2236
2237 return res == ERROR_SUCCESS;
2238 }
2239 }
2240
2241 switch (dwOption)
2242 {
2243 case INTERNET_OPTION_CALLBACK:
2244 {
2245 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2246 ret = (set_status_callback(lpwhh, callback, TRUE) != INTERNET_INVALID_STATUS_CALLBACK);
2247 break;
2248 }
2249 case INTERNET_OPTION_HTTP_VERSION:
2250 {
2251 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2252 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2253 }
2254 break;
2255 case INTERNET_OPTION_ERROR_MASK:
2256 {
2257 unsigned long flags=*(unsigned long*)lpBuffer;
2258 FIXME("Option INTERNET_OPTION_ERROR_MASK(%ld): STUB\n",flags);
2259 }
2260 break;
2261 case INTERNET_OPTION_CODEPAGE:
2262 {
2263 unsigned long codepage=*(unsigned long*)lpBuffer;
2264 FIXME("Option INTERNET_OPTION_CODEPAGE (%ld): STUB\n",codepage);
2265 }
2266 break;
2267 case INTERNET_OPTION_REQUEST_PRIORITY:
2268 {
2269 unsigned long priority=*(unsigned long*)lpBuffer;
2270 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%ld): STUB\n",priority);
2271 }
2272 break;
2273 case INTERNET_OPTION_CONNECT_TIMEOUT:
2274 {
2275 unsigned long connecttimeout=*(unsigned long*)lpBuffer;
2276 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%ld): STUB\n",connecttimeout);
2277 }
2278 break;
2279 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2280 {
2281 unsigned long receivetimeout=*(unsigned long*)lpBuffer;
2282 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%ld): STUB\n",receivetimeout);
2283 }
2284 break;
2285 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2286 {
2287 unsigned long conns=*(unsigned long*)lpBuffer;
2288 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%ld): STUB\n",conns);
2289 }
2290 break;
2291 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2292 {
2293 unsigned long conns=*(unsigned long*)lpBuffer;
2294 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%ld): STUB\n",conns);
2295 }
2296 break;
2297 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2298 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2299 break;
2300 case INTERNET_OPTION_END_BROWSER_SESSION:
2301 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2302 break;
2303 case INTERNET_OPTION_CONNECTED_STATE:
2304 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2305 break;
2306 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2307 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2308 break;
2309 case INTERNET_OPTION_SEND_TIMEOUT:
2310 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2311 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
2312 break;
2313 case INTERNET_OPTION_CONNECT_RETRIES:
2314 FIXME("Option INTERNET_OPTION_CONNECT_RETRIES: STUB\n");
2315 break;
2316 case INTERNET_OPTION_CONTEXT_VALUE:
2317 FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2318 break;
2319 case INTERNET_OPTION_SECURITY_FLAGS:
2320 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2321 break;
2322 case 86:
2323 FIXME("86\n");
2324 break;
2325 default:
2326 FIXME("Option %d STUB\n",dwOption);
2327 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2328 ret = FALSE;
2329 break;
2330 }
2331
2332 if(lpwhh)
2333 WININET_Release( lpwhh );
2334
2335 return ret;
2336 }
2337
2338
2339 /***********************************************************************
2340 * InternetSetOptionA (WININET.@)
2341 *
2342 * Sets an options on the specified handle.
2343 *
2344 * RETURNS
2345 * TRUE on success
2346 * FALSE on failure
2347 *
2348 */
2349 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2350 LPVOID lpBuffer, DWORD dwBufferLength)
2351 {
2352 LPVOID wbuffer;
2353 DWORD wlen;
2354 BOOL r;
2355
2356 switch( dwOption )
2357 {
2358 case INTERNET_OPTION_CALLBACK:
2359 {
2360 LPWININETHANDLEHEADER lpwh;
2361 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2362
2363 if (!(lpwh = WININET_GetObject(hInternet))) return FALSE;
2364 r = (set_status_callback(lpwh, callback, FALSE) != INTERNET_INVALID_STATUS_CALLBACK);
2365 WININET_Release(lpwh);
2366 return r;
2367 }
2368 case INTERNET_OPTION_PROXY:
2369 {
2370 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2371 LPINTERNET_PROXY_INFOW piw;
2372 DWORD proxlen, prbylen;
2373 LPWSTR prox, prby;
2374
2375 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2376 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2377 wlen = sizeof(*piw) + proxlen + prbylen;
2378 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2379 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2380 piw->dwAccessType = pi->dwAccessType;
2381 prox = (LPWSTR) &piw[1];
2382 prby = &prox[proxlen+1];
2383 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2384 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2385 piw->lpszProxy = prox;
2386 piw->lpszProxyBypass = prby;
2387 }
2388 break;
2389 case INTERNET_OPTION_USER_AGENT:
2390 case INTERNET_OPTION_USERNAME:
2391 case INTERNET_OPTION_PASSWORD:
2392 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2393 NULL, 0 );
2394 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2395 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2396 wbuffer, wlen );
2397 break;
2398 default:
2399 wbuffer = lpBuffer;
2400 wlen = dwBufferLength;
2401 }
2402
2403 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2404
2405 if( lpBuffer != wbuffer )
2406 HeapFree( GetProcessHeap(), 0, wbuffer );
2407
2408 return r;
2409 }
2410
2411
2412 /***********************************************************************
2413 * InternetSetOptionExA (WININET.@)
2414 */
2415 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2416 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2417 {
2418 FIXME("Flags %08x ignored\n", dwFlags);
2419 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2420 }
2421
2422 /***********************************************************************
2423 * InternetSetOptionExW (WININET.@)
2424 */
2425 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2426 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2427 {
2428 FIXME("Flags %08x ignored\n", dwFlags);
2429 if( dwFlags & ~ISO_VALID_FLAGS )
2430 {
2431 INTERNET_SetLastError( ERROR_INVALID_PARAMETER );
2432 return FALSE;
2433 }
2434 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2435 }
2436
2437 static const WCHAR WININET_wkday[7][4] =
2438 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2439 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2440 static const WCHAR WININET_month[12][4] =
2441 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2442 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2443 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2444
2445 /***********************************************************************
2446 * InternetTimeFromSystemTimeA (WININET.@)
2447 */
2448 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2449 {
2450 BOOL ret;
2451 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2452
2453 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2454
2455 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2456 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2457
2458 return ret;
2459 }
2460
2461 /***********************************************************************
2462 * InternetTimeFromSystemTimeW (WININET.@)
2463 */
2464 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2465 {
2466 static const WCHAR date[] =
2467 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2468 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2469
2470 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2471
2472 if (!time || !string) return FALSE;
2473
2474 if (format != INTERNET_RFC1123_FORMAT || size < INTERNET_RFC1123_BUFSIZE * sizeof(WCHAR))
2475 return FALSE;
2476
2477 sprintfW( string, date,
2478 WININET_wkday[time->wDayOfWeek],
2479 time->wDay,
2480 WININET_month[time->wMonth - 1],
2481 time->wYear,
2482 time->wHour,
2483 time->wMinute,
2484 time->wSecond );
2485
2486 return TRUE;
2487 }
2488
2489 /***********************************************************************
2490 * InternetTimeToSystemTimeA (WININET.@)
2491 */
2492 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2493 {
2494 BOOL ret = FALSE;
2495 WCHAR *stringW;
2496 int len;
2497
2498 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
2499
2500 len = MultiByteToWideChar( CP_ACP, 0, string, -1, NULL, 0 );
2501 stringW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2502
2503 if (stringW)
2504 {
2505 MultiByteToWideChar( CP_ACP, 0, string, -1, stringW, len );
2506 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2507 HeapFree( GetProcessHeap(), 0, stringW );
2508 }
2509 return ret;
2510 }
2511
2512 /***********************************************************************
2513 * InternetTimeToSystemTimeW (WININET.@)
2514 */
2515 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2516 {
2517 unsigned int i;
2518 const WCHAR *s = string;
2519 WCHAR *end;
2520
2521 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
2522
2523 if (!string || !time) return FALSE;
2524
2525 /* Windows does this too */
2526 GetSystemTime( time );
2527
2528 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2529 * a SYSTEMTIME structure.
2530 */
2531
2532 while (*s && !isalphaW( *s )) s++;
2533 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2534 time->wDayOfWeek = 7;
2535
2536 for (i = 0; i < 7; i++)
2537 {
2538 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2539 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2540 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2541 {
2542 time->wDayOfWeek = i;
2543 break;
2544 }
2545 }
2546
2547 if (time->wDayOfWeek > 6) return TRUE;
2548 while (*s && !isdigitW( *s )) s++;
2549 time->wDay = strtolW( s, &end, 10 );
2550 s = end;
2551
2552 while (*s && !isalphaW( *s )) s++;
2553 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2554 time->wMonth = 0;
2555
2556 for (i = 0; i < 12; i++)
2557 {
2558 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2559 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2560 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2561 {
2562 time->wMonth = i + 1;
2563 break;
2564 }
2565 }
2566 if (time->wMonth == 0) return TRUE;
2567
2568 while (*s && !isdigitW( *s )) s++;
2569 if (*s == '\0') return TRUE;
2570 time->wYear = strtolW( s, &end, 10 );
2571 s = end;
2572
2573 while (*s && !isdigitW( *s )) s++;
2574 if (*s == '\0') return TRUE;
2575 time->wHour = strtolW( s, &end, 10 );
2576 s = end;
2577
2578 while (*s && !isdigitW( *s )) s++;
2579 if (*s == '\0') return TRUE;
2580 time->wMinute = strtolW( s, &end, 10 );
2581 s = end;
2582
2583 while (*s && !isdigitW( *s )) s++;
2584 if (*s == '\0') return TRUE;
2585 time->wSecond = strtolW( s, &end, 10 );
2586 s = end;
2587
2588 time->wMilliseconds = 0;
2589 return TRUE;
2590 }
2591
2592 /***********************************************************************
2593 * InternetCheckConnectionW (WININET.@)
2594 *
2595 * Pings a requested host to check internet connection
2596 *
2597 * RETURNS
2598 * TRUE on success and FALSE on failure. If a failure then
2599 * ERROR_NOT_CONNECTED is placed into GetLastError
2600 *
2601 */
2602 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2603 {
2604 /*
2605 * this is a kludge which runs the resident ping program and reads the output.
2606 *
2607 * Anyone have a better idea?
2608 */
2609
2610 BOOL rc = FALSE;
2611 static const CHAR ping[] = "ping -c 1 ";
2612 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2613 CHAR *command = NULL;
2614 WCHAR hostW[1024];
2615 DWORD len;
2616 INTERNET_PORT port;
2617 int status = -1;
2618
2619 FIXME("\n");
2620
2621 /*
2622 * Crack or set the Address
2623 */
2624 if (lpszUrl == NULL)
2625 {
2626 /*
2627 * According to the doc we are supposed to use the ip for the next
2628 * server in the WnInet internal server database. I have
2629 * no idea what that is or how to get it.
2630 *
2631 * So someone needs to implement this.
2632 */
2633 FIXME("Unimplemented with URL of NULL\n");
2634 return TRUE;
2635 }
2636 else
2637 {
2638 URL_COMPONENTSW components;
2639
2640 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2641 components.lpszHostName = (LPWSTR)&hostW;
2642 components.dwHostNameLength = 1024;
2643
2644 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2645 goto End;
2646
2647 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2648 port = components.nPort;
2649 TRACE("port: %d\n", port);
2650 }
2651
2652 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
2653 {
2654 struct sockaddr_in sin;
2655 int fd;
2656
2657 if (!GetAddress(hostW, port, &sin))
2658 goto End;
2659 fd = socket(sin.sin_family, SOCK_STREAM, 0);
2660 if (fd != -1)
2661 {
2662 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == 0)
2663 rc = TRUE;
2664 close(fd);
2665 }
2666 }
2667 else
2668 {
2669 /*
2670 * Build our ping command
2671 */
2672 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2673 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2674 strcpy(command,ping);
2675 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2676 strcat(command,redirect);
2677
2678 TRACE("Ping command is : %s\n",command);
2679
2680 status = system(command);
2681
2682 TRACE("Ping returned a code of %i\n",status);
2683
2684 /* Ping return code of 0 indicates success */
2685 if (status == 0)
2686 rc = TRUE;
2687 }
2688
2689 End:
2690
2691 HeapFree( GetProcessHeap(), 0, command );
2692 if (rc == FALSE)
2693 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
2694
2695 return rc;
2696 }
2697
2698
2699 /***********************************************************************
2700 * InternetCheckConnectionA (WININET.@)
2701 *
2702 * Pings a requested host to check internet connection
2703 *
2704 * RETURNS
2705 * TRUE on success and FALSE on failure. If a failure then
2706 * ERROR_NOT_CONNECTED is placed into GetLastError
2707 *
2708 */
2709 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2710 {
2711 WCHAR *szUrl;
2712 INT len;
2713 BOOL rc;
2714
2715 len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
2716 if (!(szUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR))))
2717 return FALSE;
2718 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, len);
2719 rc = InternetCheckConnectionW(szUrl, dwFlags, dwReserved);
2720 HeapFree(GetProcessHeap(), 0, szUrl);
2721
2722 return rc;
2723 }
2724
2725
2726 /**********************************************************
2727 * INTERNET_InternetOpenUrlW (internal)
2728 *
2729 * Opens an URL
2730 *
2731 * RETURNS
2732 * handle of connection or NULL on failure
2733 */
2734 static HINTERNET INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2735 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2736 {
2737 URL_COMPONENTSW urlComponents;
2738 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2739 WCHAR password[1024], path[2048], extra[1024];
2740 HINTERNET client = NULL, client1 = NULL;
2741
2742 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2743 dwHeadersLength, dwFlags, dwContext);
2744
2745 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2746 urlComponents.lpszScheme = protocol;
2747 urlComponents.dwSchemeLength = 32;
2748 urlComponents.lpszHostName = hostName;
2749 urlComponents.dwHostNameLength = MAXHOSTNAME;
2750 urlComponents.lpszUserName = userName;
2751 urlComponents.dwUserNameLength = 1024;
2752 urlComponents.lpszPassword = password;
2753 urlComponents.dwPasswordLength = 1024;
2754 urlComponents.lpszUrlPath = path;
2755 urlComponents.dwUrlPathLength = 2048;
2756 urlComponents.lpszExtraInfo = extra;
2757 urlComponents.dwExtraInfoLength = 1024;
2758 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2759 return NULL;
2760 switch(urlComponents.nScheme) {
2761 case INTERNET_SCHEME_FTP:
2762 if(urlComponents.nPort == 0)
2763 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2764 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2765 userName, password, dwFlags, dwContext, INET_OPENURL);
2766 if(client == NULL)
2767 break;
2768 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2769 if(client1 == NULL) {
2770 InternetCloseHandle(client);
2771 break;
2772 }
2773 break;
2774
2775 case INTERNET_SCHEME_HTTP:
2776 case INTERNET_SCHEME_HTTPS: {
2777 static const WCHAR szStars[] = { '*','/','*', 0 };
2778 LPCWSTR accept[2] = { szStars, NULL };
2779 if(urlComponents.nPort == 0) {
2780 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2781 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2782 else
2783 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2784 }
2785 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2786 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2787 userName, password, dwFlags, dwContext, INET_OPENURL);
2788 if(client == NULL)
2789 break;
2790
2791 if (urlComponents.dwExtraInfoLength) {
2792 WCHAR *path_extra;
2793 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
2794
2795 if (!(path_extra = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
2796 {
2797 InternetCloseHandle(client);
2798 break;
2799 }
2800 strcpyW(path_extra, urlComponents.lpszUrlPath);
2801 strcatW(path_extra, urlComponents.lpszExtraInfo);
2802 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
2803 HeapFree(GetProcessHeap(), 0, path_extra);
2804 }
2805 else
2806 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2807
2808 if(client1 == NULL) {
2809 InternetCloseHandle(client);
2810 break;
2811 }
2812 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2813 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
2814 GetLastError() != ERROR_IO_PENDING) {
2815 InternetCloseHandle(client1);
2816 client1 = NULL;
2817 break;
2818 }
2819 }
2820 case INTERNET_SCHEME_GOPHER:
2821 /* gopher doesn't seem to be implemented in wine, but it's supposed
2822 * to be supported by InternetOpenUrlA. */
2823 default:
2824 INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2825 break;
2826 }
2827
2828 TRACE(" %p <--\n", client1);
2829
2830 return client1;
2831 }
2832
2833 /**********************************************************
2834 * InternetOpenUrlW (WININET.@)
2835 *
2836 * Opens an URL
2837 *
2838 * RETURNS
2839 * handle of connection or NULL on failure
2840 */
2841 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
2842 {
2843 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
2844 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest->hdr;
2845
2846 TRACE("%p\n", hIC);
2847
2848 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2849 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2850 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2851 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2852 }
2853
2854 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2855 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2856 {
2857 HINTERNET ret = NULL;
2858 LPWININETAPPINFOW hIC = NULL;
2859
2860 if (TRACE_ON(wininet)) {
2861 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2862 dwHeadersLength, dwFlags, dwContext);
2863 TRACE(" flags :");
2864 dump_INTERNET_FLAGS(dwFlags);
2865 }
2866
2867 if (!lpszUrl)
2868 {
2869 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2870 goto lend;
2871 }
2872
2873 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2874 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2875 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2876 goto lend;
2877 }
2878
2879 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2880 WORKREQUEST workRequest;
2881 struct WORKREQ_INTERNETOPENURLW *req;
2882
2883 workRequest.asyncproc = AsyncInternetOpenUrlProc;
2884 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2885 req = &workRequest.u.InternetOpenUrlW;
2886 req->lpszUrl = WININET_strdupW(lpszUrl);
2887 if (lpszHeaders)
2888 req->lpszHeaders = WININET_strdupW(lpszHeaders);
2889 else
2890 req->lpszHeaders = 0;
2891 req->dwHeadersLength = dwHeadersLength;
2892 req->dwFlags = dwFlags;
2893 req->dwContext = dwContext;
2894
2895 INTERNET_AsyncCall(&workRequest);
2896 /*
2897 * This is from windows.
2898 */
2899 INTERNET_SetLastError(ERROR_IO_PENDING);
2900 } else {
2901 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2902 }
2903
2904 lend:
2905 if( hIC )
2906 WININET_Release( &hIC->hdr );
2907 TRACE(" %p <--\n", ret);
2908
2909 return ret;
2910 }
2911
2912 /**********************************************************
2913 * InternetOpenUrlA (WININET.@)
2914 *
2915 * Opens an URL
2916 *
2917 * RETURNS
2918 * handle of connection or NULL on failure
2919 */
2920 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2921 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2922 {
2923 HINTERNET rc = NULL;
2924
2925 INT lenUrl;
2926 INT lenHeaders = 0;
2927 LPWSTR szUrl = NULL;
2928 LPWSTR szHeaders = NULL;
2929
2930 TRACE("\n");
2931
2932 if(lpszUrl) {
2933 lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
2934 szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
2935 if(!szUrl)
2936 return NULL;
2937 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
2938 }
2939
2940 if(lpszHeaders) {
2941 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2942 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2943 if(!szHeaders) {
2944 HeapFree(GetProcessHeap(), 0, szUrl);
2945 return NULL;
2946 }
2947 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2948 }
2949
2950 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2951 lenHeaders, dwFlags, dwContext);
2952
2953 HeapFree(GetProcessHeap(), 0, szUrl);
2954 HeapFree(GetProcessHeap(), 0, szHeaders);
2955
2956 return rc;
2957 }
2958
2959
2960 static LPWITHREADERROR INTERNET_AllocThreadError(void)
2961 {
2962 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
2963
2964 if (lpwite)
2965 {
2966 lpwite->dwError = 0;
2967 lpwite->response[0] = '\0';
2968 }
2969
2970 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
2971 {
2972 HeapFree(GetProcessHeap(), 0, lpwite);
2973 return NULL;
2974 }
2975
2976 return lpwite;
2977 }
2978
2979
2980 /***********************************************************************
2981 * INTERNET_SetLastError (internal)
2982 *
2983 * Set last thread specific error
2984 *
2985 * RETURNS
2986 *
2987 */
2988 void INTERNET_SetLastError(DWORD dwError)
2989 {
2990 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2991
2992 if (!lpwite)
2993 lpwite = INTERNET_AllocThreadError();
2994
2995 SetLastError(dwError);
2996 if(lpwite)
2997 lpwite->dwError = dwError;
2998 }
2999
3000
3001 /***********************************************************************
3002 * INTERNET_GetLastError (internal)
3003 *
3004 * Get last thread specific error
3005 *
3006 * RETURNS
3007 *
3008 */
3009 DWORD INTERNET_GetLastError(void)
3010 {
3011 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3012 if (!lpwite) return 0;
3013 /* TlsGetValue clears last error, so set it again here */
3014 SetLastError(lpwite->dwError);
3015 return lpwite->dwError;
3016 }
3017
3018
3019 /***********************************************************************
3020 * INTERNET_WorkerThreadFunc (internal)
3021 *
3022 * Worker thread execution function
3023 *
3024 * RETURNS
3025 *
3026 */
3027 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3028 {
3029 LPWORKREQUEST lpRequest = lpvParam;
3030 WORKREQUEST workRequest;
3031
3032 TRACE("\n");
3033
3034 workRequest = *lpRequest;
3035 HeapFree(GetProcessHeap(), 0, lpRequest);
3036
3037 workRequest.asyncproc(&workRequest);
3038
3039 WININET_Release( workRequest.hdr );
3040 return TRUE;
3041 }
3042
3043
3044 /***********************************************************************
3045 * INTERNET_AsyncCall (internal)
3046 *
3047 * Retrieves work request from queue
3048 *
3049 * RETURNS
3050 *
3051 */
3052 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3053 {
3054 BOOL bSuccess;
3055 LPWORKREQUEST lpNewRequest;
3056
3057 TRACE("\n");
3058
3059 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3060 if (!lpNewRequest)
3061 return FALSE;
3062
3063 *lpNewRequest = *lpWorkRequest;
3064
3065 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3066 if (!bSuccess)
3067 {
3068 HeapFree(GetProcessHeap(), 0, lpNewRequest);
3069 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3070 }
3071
3072 return bSuccess;
3073 }
3074
3075
3076 /***********************************************************************
3077 * INTERNET_GetResponseBuffer (internal)
3078 *
3079 * RETURNS
3080 *
3081 */
3082 LPSTR INTERNET_GetResponseBuffer(void)
3083 {
3084 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3085 if (!lpwite)
3086 lpwite = INTERNET_AllocThreadError();
3087 TRACE("\n");
3088 return lpwite->response;
3089 }
3090
3091 /***********************************************************************
3092 * INTERNET_GetNextLine (internal)
3093 *
3094 * Parse next line in directory string listing
3095 *
3096 * RETURNS
3097 * Pointer to beginning of next line
3098 * NULL on failure
3099 *
3100 */
3101
3102 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3103 {
3104 struct timeval tv;
3105 fd_set infd;
3106 BOOL bSuccess = FALSE;
3107 INT nRecv = 0;
3108 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3109
3110 TRACE("\n");
3111
3112 FD_ZERO(&infd);
3113 FD_SET(nSocket, &infd);
3114 tv.tv_sec=RESPONSE_TIMEOUT;
3115 tv.tv_usec=0;
3116
3117 while (nRecv < MAX_REPLY_LEN)
3118 {
3119 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
3120 {
3121 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3122 {
3123 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3124 goto lend;
3125 }
3126
3127 if (lpszBuffer[nRecv] == '\n')
3128 {
3129 bSuccess = TRUE;
3130 break;
3131 }
3132 if (lpszBuffer[nRecv] != '\r')
3133 nRecv++;
3134 }
3135 else
3136 {
3137 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3138 goto lend;
3139 }
3140 }
3141
3142 lend:
3143 if (bSuccess)
3144 {
3145 lpszBuffer[nRecv] = '\0';
3146 *dwLen = nRecv - 1;