/*
 * Wininet
 *
 * Copyright 1999 Corel Corporation
 * Copyright 2002 CodeWeavers Inc.
 * Copyright 2002 Jaco Greeff
 * Copyright 2002 TransGaming Technologies Inc.
 *
 * Ulrich Czekalla
 * Aric Stewart
 * David Hammerton
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"

#define MAXHOSTNAME 100 /* from http.c */

#include <string.h>
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <stdlib.h>
#include <ctype.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "wininet.h"
#include "winnls.h"
#include "wine/debug.h"
#include "winerror.h"
#define NO_SHLWAPI_STREAM
#include "shlwapi.h"

#include "wine/exception.h"
#include "msvcrt/excpt.h"

#include "internet.h"

WINE_DEFAULT_DEBUG_CHANNEL(wininet);

#define MAX_IDLE_WORKER 1000*60*1
#define MAX_WORKER_THREADS 10
#define RESPONSE_TIMEOUT        30

#define GET_HWININET_FROM_LPWININETFINDNEXT(lpwh) \
(LPWININETAPPINFOA)(((LPWININETFTPSESSIONA)(lpwh->hdr.lpwhparent))->hdr.lpwhparent)

/* filter for page-fault exceptions */
static WINE_EXCEPTION_FILTER(page_fault)
{
    if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
        GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
        return EXCEPTION_EXECUTE_HANDLER;
    return EXCEPTION_CONTINUE_SEARCH;
}

typedef struct
{
    DWORD  dwError;
    CHAR   response[MAX_REPLY_LEN];
} WITHREADERROR, *LPWITHREADERROR;

INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp);
BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData);
VOID INTERNET_ExecuteWork();

DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
DWORD dwNumThreads;
DWORD dwNumIdleThreads;
HANDLE hEventArray[2];
#define hQuitEvent hEventArray[0]
#define hWorkEvent hEventArray[1]
CRITICAL_SECTION csQueue;
LPWORKREQUEST lpHeadWorkQueue;
LPWORKREQUEST lpWorkQueueTail;

/***********************************************************************
 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
 *
 * PARAMS
 *     hinstDLL    [I] handle to the DLL's instance
 *     fdwReason   [I]
 *     lpvReserved [I] reserved, must be NULL
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
 */

BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    TRACE("%i,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);

    switch (fdwReason) {
        case DLL_PROCESS_ATTACH:

            g_dwTlsErrIndex = TlsAlloc();

	    if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
		return FALSE;

	    hQuitEvent = CreateEventA(0, TRUE, FALSE, NULL);
	    hWorkEvent = CreateEventA(0, FALSE, FALSE, NULL);
	    InitializeCriticalSection(&csQueue);

            dwNumThreads = 0;
            dwNumIdleThreads = 0;

        case DLL_THREAD_ATTACH:
	    {
                LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(WITHREADERROR));
		if (NULL == lpwite)
                    return FALSE;

                TlsSetValue(g_dwTlsErrIndex, (LPVOID)lpwite);
	    }
	    break;

        case DLL_THREAD_DETACH:
	    if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
			{
				LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
				if (lpwite)
                   HeapFree(GetProcessHeap(), 0, lpwite);
			}
	    break;

        case DLL_PROCESS_DETACH:

	    if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
	    {
	        HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
	        TlsFree(g_dwTlsErrIndex);
	    }

	    SetEvent(hQuitEvent);

	    CloseHandle(hQuitEvent);
	    CloseHandle(hWorkEvent);
	    DeleteCriticalSection(&csQueue);
            break;
    }

    return TRUE;
}


/***********************************************************************
 *           InternetInitializeAutoProxyDll   (WININET.@)
 *
 * Setup the internal proxy
 *
 * PARAMETERS
 *     dwReserved
 *
 * RETURNS
 *     FALSE on failure
 *
 */
BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
{
    FIXME("STUB\n");
    INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return FALSE;
}


/***********************************************************************
 *           InternetOpenA   (WININET.@)
 *
 * Per-application initialization of wininet
 *
 * RETURNS
 *    HINTERNET on success
 *    NULL on failure
 *
 */
HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
    LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
{
    LPWININETAPPINFOA lpwai = NULL;

    TRACE("(%s, %li, %s, %s, %li)\n", debugstr_a(lpszAgent), dwAccessType,
	 debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);

    /* Clear any error information */
    INTERNET_SetLastError(0);

    lpwai = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETAPPINFOA));
    if (NULL == lpwai)
        INTERNET_SetLastError(ERROR_OUTOFMEMORY);
    else
    {
        memset(lpwai, 0, sizeof(WININETAPPINFOA));
        lpwai->hdr.htype = WH_HINIT;
        lpwai->hdr.lpwhparent = NULL;
        lpwai->hdr.dwFlags = dwFlags;
        if (NULL != lpszAgent)
        {
            if ((lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,strlen(lpszAgent)+1)))
                strcpy( lpwai->lpszAgent, lpszAgent );
        }
        if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
        {
            HKEY key;
            if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", &key))
            {
                DWORD keytype, len, enabled;
                RegQueryValueExA(key, "ProxyEnable", NULL, NULL, (BYTE*)&enabled, NULL);
                if(enabled)
                {
                    if(!RegQueryValueExA(key, "ProxyServer", NULL, &keytype, NULL, &len) && len && keytype == REG_SZ)
                    {
                        lpwai->lpszProxy=HeapAlloc( GetProcessHeap(), 0, len+1 );
                        RegQueryValueExA(key, "ProxyServer", NULL, &keytype, (BYTE*)lpwai->lpszProxy, &len);
						TRACE("Proxy = %s\n", lpwai->lpszProxy);
                        dwAccessType = INTERNET_OPEN_TYPE_PROXY;
                    }
                }
                else
                {
                    TRACE("Proxy is not enabled.\n");
                }
                RegCloseKey(key);
            }
        }
        else if (NULL != lpszProxy)
        {
            if ((lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxy)+1 )))
                strcpy( lpwai->lpszProxy, lpszProxy );
        }
        if (NULL != lpszProxyBypass)
        {
            if ((lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0, strlen(lpszProxyBypass)+1)))
                strcpy( lpwai->lpszProxyBypass, lpszProxyBypass );
        }
        lpwai->dwAccessType = dwAccessType;
    }

    TRACE("returning %p\n", (HINTERNET)lpwai);
    return (HINTERNET)lpwai;
}


/***********************************************************************
 *           InternetOpenW   (WININET.@)
 *
 * Per-application initialization of wininet
 *
 * RETURNS
 *    HINTERNET on success
 *    NULL on failure
 *
 */
HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
    LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
{
    CHAR *szAgent = NULL;
    CHAR *szProxy = NULL;
    CHAR *szBypass = NULL;
    HINTERNET rc = (HINTERNET)NULL;
    if (lpszAgent)
    {
        INT lenAgent = lstrlenW(lpszAgent)+1;
        szAgent = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenAgent*sizeof(CHAR));
        if (!szAgent)
            goto end;
        WideCharToMultiByte(CP_ACP, -1, lpszAgent, -1, szAgent, lenAgent,
            NULL, NULL);
    }
    if (lpszProxy)
    {
        INT lenProxy = lstrlenW(lpszProxy)+1;
        szProxy = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenProxy*sizeof(CHAR));
        if (!szProxy)
            goto end;
        WideCharToMultiByte(CP_ACP, -1, lpszProxy, -1, szProxy, lenProxy,
            NULL, NULL);
    }
    if (lpszProxyBypass)
    {
        INT lenBypass = lstrlenW(lpszProxyBypass)+1;
        szBypass = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenBypass*sizeof(CHAR));
        if (!szBypass)
            goto end;
        WideCharToMultiByte(CP_ACP, -1, lpszProxyBypass, -1, szBypass, lenBypass,
            NULL, NULL);
    }


    rc = InternetOpenA(szAgent, dwAccessType, szProxy, szBypass, dwFlags);

end:
    if (szAgent) HeapFree(GetProcessHeap(), 0, szAgent);
    if (szProxy) HeapFree(GetProcessHeap(), 0, szProxy);
    if (szBypass) HeapFree(GetProcessHeap(), 0, szBypass);
    return rc;
}

/***********************************************************************
 *           InternetGetLastResponseInfoA (WININET.@)
 *
 * Return last wininet error description on the calling thread
 *
 * RETURNS
 *    TRUE on success of writting to buffer
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
    LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
{
    LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);

    TRACE("\n");

    *lpdwError = lpwite->dwError;
    if (lpwite->dwError)
    {
        strncpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
	*lpdwBufferLength = strlen(lpszBuffer);
    }
    else
        *lpdwBufferLength = 0;

    return TRUE;
}


/***********************************************************************
 *           InternetGetConnectedState (WININET.@)
 *
 * Return connected state
 *
 * RETURNS
 *    TRUE if connected
 *    if lpdwStatus is not null, return the status (off line,
 *    modem, lan...) in it.
 *    FALSE if not connected
 */
BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
{
    if (lpdwStatus) {
	FIXME("always returning LAN connection.\n");
	*lpdwStatus = INTERNET_CONNECTION_LAN;
    }
    return TRUE;
}


/***********************************************************************
 *           InternetConnectA (WININET.@)
 *
 * Open a ftp, gopher or http session
 *
 * RETURNS
 *    HINTERNET a session handle on success
 *    NULL on failure
 *
 */
HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
    LPCSTR lpszServerName, INTERNET_PORT nServerPort,
    LPCSTR lpszUserName, LPCSTR lpszPassword,
    DWORD dwService, DWORD dwFlags, DWORD dwContext)
{
    HINTERNET rc = (HINTERNET) NULL;

    TRACE("(%p, %s, %i, %s, %s, %li, %li, %li)\n", hInternet, debugstr_a(lpszServerName),
	  nServerPort, debugstr_a(lpszUserName), debugstr_a(lpszPassword),
	  dwService, dwFlags, dwContext);

    /* Clear any error information */
    INTERNET_SetLastError(0);

    switch (dwService)
    {
        case INTERNET_SERVICE_FTP:
            rc = FTP_Connect(hInternet, lpszServerName, nServerPort,
            lpszUserName, lpszPassword, dwFlags, dwContext);
            break;

        case INTERNET_SERVICE_HTTP:
	    rc = HTTP_Connect(hInternet, lpszServerName, nServerPort,
            lpszUserName, lpszPassword, dwFlags, dwContext);
            break;

        case INTERNET_SERVICE_GOPHER:
        default:
            TRACE( "Not implemented for service %ld\n", dwService );
            break;
    }

    TRACE("returning %p\n", rc);
    return rc;
}


/***********************************************************************
 *           InternetConnectW (WININET.@)
 *
 * Open a ftp, gopher or http session
 *
 * RETURNS
 *    HINTERNET a session handle on success
 *    NULL on failure
 *
 */
HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
    LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
    LPCWSTR lpszUserName, LPCWSTR lpszPassword,
    DWORD dwService, DWORD dwFlags, DWORD dwContext)
{
    HINTERNET rc = (HINTERNET)NULL;
    INT lenServer = 0;
    INT lenUser = 0;
    INT lenPass = 0;
    CHAR *szServerName = NULL;
    CHAR *szUserName = NULL;
    CHAR *szPassword = NULL;

    if (lpszServerName)
    {
	lenServer = lstrlenW(lpszServerName)+1;
        szServerName = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenServer*sizeof(CHAR));
	WideCharToMultiByte(CP_ACP, -1, lpszServerName, -1, szServerName, lenServer,
			    NULL, NULL);
    }
    if (lpszUserName)
    {
	lenUser = lstrlenW(lpszUserName)+1;
        szUserName = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenUser*sizeof(CHAR));
	WideCharToMultiByte(CP_ACP, -1, lpszUserName, -1, szUserName, lenUser,
			    NULL, NULL);
    }
    if (lpszPassword)
    {
	lenPass = lstrlenW(lpszPassword)+1;
        szPassword = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenPass*sizeof(CHAR));
	WideCharToMultiByte(CP_ACP, -1, lpszPassword, -1, szPassword, lenPass,
			    NULL, NULL);
 }


    rc = InternetConnectA(hInternet, szServerName, nServerPort,
        szUserName, szPassword, dwService, dwFlags, dwContext);

    if (szServerName) HeapFree(GetProcessHeap(), 0, szServerName);
    if (szUserName) HeapFree(GetProcessHeap(), 0, szUserName);
    if (szPassword) HeapFree(GetProcessHeap(), 0, szPassword);
    return rc;
}


/***********************************************************************
 *           InternetFindNextFileA (WININET.@)
 *
 * Continues a file search from a previous call to FindFirstFile
 *
 * RETURNS
 *    TRUE on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
{
    LPWININETAPPINFOA hIC = NULL;
    LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;

    TRACE("\n");

    if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
    {
        INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
        return FALSE;
    }

    hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
    if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
    {
        WORKREQUEST workRequest;

        workRequest.asyncall = INTERNETFINDNEXTA;
	workRequest.HFTPSESSION = (DWORD)hFind;
	workRequest.LPFINDFILEDATA = (DWORD)lpvFindData;

	return INTERNET_AsyncCall(&workRequest);
    }
    else
    {
        return INTERNET_FindNextFileA(hFind, lpvFindData);
    }
}

/***********************************************************************
 *           INTERNET_FindNextFileA (Internal)
 *
 * Continues a file search from a previous call to FindFirstFile
 *
 * RETURNS
 *    TRUE on success
 *    FALSE on failure
 *
 */
BOOL WINAPI INTERNET_FindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
{
    BOOL bSuccess = TRUE;
    LPWININETAPPINFOA hIC = NULL;
    LPWIN32_FIND_DATAA lpFindFileData;
    LPWININETFINDNEXTA lpwh = (LPWININETFINDNEXTA) hFind;

    TRACE("\n");

    if (NULL == lpwh || lpwh->hdr.htype != WH_HFINDNEXT)
    {
        INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
        return FALSE;
    }

    /* Clear any error information */
    INTERNET_SetLastError(0);

    if (lpwh->hdr.lpwhparent->htype != WH_HFTPSESSION)
    {
        FIXME("Only FTP find next supported\n");
        INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
	return FALSE;
    }

    TRACE("index(%d) size(%ld)\n", lpwh->index, lpwh->size);

    lpFindFileData = (LPWIN32_FIND_DATAA) lpvFindData;
    ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));

    if (lpwh->index >= lpwh->size)
    {
        INTERNET_SetLastError(ERROR_NO_MORE_FILES);
        bSuccess = FALSE;
	goto lend;
    }

    FTP_ConvertFileProp(&lpwh->lpafp[lpwh->index], lpFindFileData);
    lpwh->index++;

    TRACE("\nName: %s\nSize: %ld\n", lpFindFileData->cFileName, lpFindFileData->nFileSizeLow);

lend:

    hIC = GET_HWININET_FROM_LPWININETFINDNEXT(lpwh);
    if (hIC->hdr.lpfnStatusCB)
    {
        INTERNET_ASYNC_RESULT iar;

        iar.dwResult = (DWORD)bSuccess;
        iar.dwError = iar.dwError = bSuccess ? ERROR_SUCCESS :
                                               INTERNET_GetLastError();

        SendAsyncCallback(&lpwh->hdr, hIC, hFind, lpwh->hdr.dwContext,
                      INTERNET_STATUS_REQUEST_COMPLETE, &iar,
                       sizeof(INTERNET_ASYNC_RESULT));
    }

    return bSuccess;
}


/***********************************************************************
 *           INTERNET_CloseHandle (internal)
 *
 * Close internet handle
 *
 * RETURNS
 *    Void
 *
 */
VOID INTERNET_CloseHandle(LPWININETAPPINFOA lpwai)
{
    TRACE("%p\n",lpwai);

    SendAsyncCallback(&lpwai->hdr, lpwai, lpwai, lpwai->hdr.dwContext,
                      INTERNET_STATUS_HANDLE_CLOSING, lpwai,
                      sizeof(HINTERNET));

    if (lpwai->lpszAgent)
        HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);

    if (lpwai->lpszProxy)
        HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);

    if (lpwai->lpszProxyBypass)
        HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);

    HeapFree(GetProcessHeap(), 0, lpwai);
}


/***********************************************************************
 *           InternetCloseHandle (WININET.@)
 *
 * Generic close handle function
 *
 * RETURNS
 *    TRUE on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
{
    BOOL retval;
    LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hInternet;

    TRACE("%p\n",hInternet);
    if (NULL == lpwh)
        return FALSE;

    __TRY {
	/* Clear any error information */
	INTERNET_SetLastError(0);
        retval = FALSE;

	switch (lpwh->htype)
	{
	    case WH_HINIT:
		INTERNET_CloseHandle((LPWININETAPPINFOA) lpwh);
		retval = TRUE;
		break;

	    case WH_HHTTPSESSION:
		HTTP_CloseHTTPSessionHandle((LPWININETHTTPSESSIONA) lpwh);
		retval = TRUE;
		break;

	    case WH_HHTTPREQ:
		HTTP_CloseHTTPRequestHandle((LPWININETHTTPREQA) lpwh);
		retval = TRUE;
		break;

	    case WH_HFTPSESSION:
		retval = FTP_CloseSessionHandle((LPWININETFTPSESSIONA) lpwh);
		break;

	    case WH_HFINDNEXT:
		retval = FTP_CloseFindNextHandle((LPWININETFINDNEXTA) lpwh);
		break;

	    default:
		break;
	}
    } __EXCEPT(page_fault) {
	INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
	return FALSE;
    }
    __ENDTRY

    return retval;
}


/***********************************************************************
 *           SetUrlComponentValue (Internal)
 *
 * Helper function for InternetCrackUrlA
 *
 * RETURNS
 *    TRUE on success
 *    FALSE on failure
 *
 */
BOOL SetUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen, 
    LPCSTR lpszStart, INT len)
{
    TRACE("%s (%d)\n", lpszStart, len);

    if (*dwComponentLen != 0 || *lppszComponent == NULL)
    {
	if (*lppszComponent == NULL)
	{
            *lppszComponent = (LPSTR)lpszStart;
	    *dwComponentLen = len;
	}
	else
	{
            INT ncpylen = min((*dwComponentLen)-1, len);
            strncpy(*lppszComponent, lpszStart, ncpylen);
            (*lppszComponent)[ncpylen] = '\0';
	    *dwComponentLen = ncpylen;
	}
    }

    return TRUE;
}

/***********************************************************************
 *           InternetCrackUrlW (WININET.@)
 *        Wraps to InternetCrackUrlA
 *        FIXME: A should call W.
 *        FIXME: - tidy this up, maybe we should use some helper functions
 *                  to make this easier.
 *               - Check for Failed memory allocations, and free nicely
 *               - Is this all correct?
 */
BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
                              LPURL_COMPONENTSW lpUrlComponents)
{
    CHAR *szUrl;
    INT len;
    BOOL rc = FALSE;
    URL_COMPONENTSA UrlComponents;

    TRACE("(%s, %li, %08lx, %p)\n", debugstr_w(lpszUrl), dwUrlLength,
	  dwFlags, lpUrlComponents);

    len = lstrlenW(lpszUrl)+1;
    if (!(szUrl = (CHAR *)HeapAlloc(GetProcessHeap(), 0, len*sizeof(CHAR))))
	return FALSE;
    WideCharToMultiByte(CP_ACP, -1, lpszUrl, -1, szUrl, len, NULL, NULL);

    UrlComponents.dwStructSize = sizeof(UrlComponents);
    UrlComponents.lpszScheme = UrlComponents.lpszHostName =
        UrlComponents.lpszUserName = UrlComponents.lpszPassword =
	UrlComponents.lpszUrlPath = UrlComponents.lpszExtraInfo = NULL;

    UrlComponents.dwSchemeLength = lpUrlComponents->dwSchemeLength *
	sizeof(CHAR) / sizeof(WCHAR);
    if (lpUrlComponents->lpszScheme)
    {
	UrlComponents.lpszScheme = HeapAlloc(GetProcessHeap(), 0,
					     UrlComponents.dwSchemeLength + 1);
	WideCharToMultiByte(CP_ACP, -1, lpUrlComponents->lpszScheme, -1,
			    UrlComponents.lpszScheme, len, NULL, NULL);
    }

    UrlComponents.dwHostNameLength = lpUrlComponents->dwHostNameLength *
	sizeof(CHAR) / sizeof(WCHAR);
    if (lpUrlComponents->lpszHostName)
    {
	UrlComponents.lpszHostName = HeapAlloc(GetProcessHeap(), 0,
					     UrlComponents.dwHostNameLength + 1);
	WideCharToMultiByte(CP_ACP, -1, lpUrlComponents->lpszHostName, -1,
			    UrlComponents.lpszHostName, len, NULL, NULL);
    }

    UrlComponents.dwUserNameLength = lpUrlComponents->dwUserNameLength *
	sizeof(CHAR) / sizeof(WCHAR);
    if (lpUrlComponents->lpszUserName)
    {
	UrlComponents.lpszUserName = HeapAlloc(GetProcessHeap(), 0,
					     UrlComponents.dwUserNameLength + 1);
	WideCharToMultiByte(CP_ACP, -1, lpUrlComponents->lpszUserName, -1,
			    UrlComponents.lpszUserName, len, NULL, NULL);
    }

    UrlComponents.dwPasswordLength = lpUrlComponents->dwPasswordLength *
	sizeof(CHAR) / sizeof(WCHAR);
    if (lpUrlComponents->lpszPassword)
    {
	UrlComponents.lpszPassword = HeapAlloc(GetProcessHeap(), 0,
					     UrlComponents.dwPasswordLength + 1);
	WideCharToMultiByte(CP_ACP, -1, lpUrlComponents->lpszPassword, -1,
			    UrlComponents.lpszPassword, len, NULL, NULL);
    }

    UrlComponents.dwUrlPathLength = lpUrlComponents->dwUrlPathLength *
	sizeof(CHAR) / sizeof(WCHAR);
    if (lpUrlComponents->lpszUrlPath)
    {
	UrlComponents.lpszUrlPath = HeapAlloc(GetProcessHeap(), 0,
					     UrlComponents.dwUrlPathLength + 1);
	WideCharToMultiByte(CP_ACP, -1, lpUrlComponents->lpszUrlPath, -1,
			    UrlComponents.lpszUrlPath, len, NULL, NULL);
    }

    UrlComponents.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength *
	sizeof(CHAR) / sizeof(WCHAR);
    if (lpUrlComponents->lpszExtraInfo)
    {
	UrlComponents.lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0,
					     UrlComponents.dwExtraInfoLength + 1);
	WideCharToMultiByte(CP_ACP, -1, lpUrlComponents->lpszExtraInfo, -1,
			    UrlComponents.lpszExtraInfo, len, NULL, NULL);
    }

    rc = InternetCrackUrlA((LPCSTR)szUrl, dwUrlLength, dwFlags,
                           &UrlComponents);
    lpUrlComponents->nPort = UrlComponents.nPort;
    memcpy (&lpUrlComponents->nScheme, &UrlComponents.nScheme, sizeof(INTERNET_SCHEME));

    if (lpUrlComponents->lpszScheme)
    {
	MultiByteToWideChar(CP_ACP, -1, UrlComponents.lpszScheme,
			    UrlComponents.dwSchemeLength, lpUrlComponents->lpszScheme,
			    lpUrlComponents->dwSchemeLength);
	HeapFree(GetProcessHeap(), 0, UrlComponents.lpszScheme);
    }
    else
    {
	lpUrlComponents->dwSchemeLength = UrlComponents.dwSchemeLength;
	lpUrlComponents->lpszScheme = (WCHAR *)lpszUrl +
	    ((UrlComponents.lpszScheme - szUrl));
    }

    if (lpUrlComponents->lpszHostName)
    {
	MultiByteToWideChar(CP_ACP, -1, UrlComponents.lpszHostName,
			    UrlComponents.dwHostNameLength, lpUrlComponents->lpszHostName,
                            lpUrlComponents->dwHostNameLength);
        HeapFree(GetProcessHeap(), 0, UrlComponents.lpszHostName);
    }
    else
    {
	lpUrlComponents->dwHostNameLength = UrlComponents.dwHostNameLength;
	lpUrlComponents->lpszHostName = (WCHAR *)lpszUrl +
	    ((UrlComponents.lpszHostName - szUrl));
    }

    if (lpUrlComponents->lpszUserName)
    {
	MultiByteToWideChar(CP_ACP, -1, UrlComponents.lpszUserName,
			    UrlComponents.dwUserNameLength, lpUrlComponents->lpszUserName,
                            lpUrlComponents->dwUserNameLength);
	HeapFree(GetProcessHeap(), 0, UrlComponents.lpszUserName);
    }
    else
    {
	lpUrlComponents->dwUserNameLength = UrlComponents.dwUserNameLength;
	lpUrlComponents->lpszUserName = (WCHAR *)lpszUrl +
	    ((UrlComponents.lpszUserName - szUrl));
    }

    if (lpUrlComponents->lpszPassword)
    {
	MultiByteToWideChar(CP_ACP, -1, UrlComponents.lpszPassword,
			    UrlComponents.dwPasswordLength, lpUrlComponents->lpszPassword,
                            lpUrlComponents->dwPasswordLength);
        HeapFree(GetProcessHeap(), 0, UrlComponents.lpszPassword);
    }
    else
    {
	lpUrlComponents->dwPasswordLength = UrlComponents.dwPasswordLength;
	lpUrlComponents->lpszPassword = (WCHAR *)lpszUrl +
	    ((UrlComponents.lpszPassword - szUrl));
    }

    if (lpUrlComponents->lpszUrlPath)
    {
	MultiByteToWideChar(CP_ACP, -1, UrlComponents.lpszUrlPath,
			    UrlComponents.dwUrlPathLength, lpUrlComponents->lpszUrlPath,
                            lpUrlComponents->dwUrlPathLength);
        HeapFree(GetProcessHeap(), 0, UrlComponents.lpszUrlPath);
    }
    else
    {
	lpUrlComponents->dwUrlPathLength = UrlComponents.dwUrlPathLength;
	lpUrlComponents->lpszUrlPath = (WCHAR *)lpszUrl +
	    ((UrlComponents.lpszUrlPath - szUrl));
    }

    if (lpUrlComponents->lpszExtraInfo)
    {
	MultiByteToWideChar(CP_ACP, -1, UrlComponents.lpszExtraInfo,
			    UrlComponents.dwExtraInfoLength, lpUrlComponents->lpszExtraInfo,
                            lpUrlComponents->dwExtraInfoLength);
        HeapFree(GetProcessHeap(), 0, UrlComponents.lpszExtraInfo);
    }
    else
    {
	lpUrlComponents->dwExtraInfoLength = UrlComponents.dwExtraInfoLength;
	lpUrlComponents->lpszExtraInfo = (WCHAR *)lpszUrl +
	    ((UrlComponents.lpszExtraInfo - szUrl));
    }

    HeapFree(GetProcessHeap(), 0, szUrl);
    return rc;
}


/***********************************************************************
 *           InternetCrackUrlA (WININET.@)
 *
 * Break up URL into its components
 *
 * TODO: Handle dwFlags
 *
 * RETURNS
 *    TRUE on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
    LPURL_COMPONENTSA lpUrlComponents)
{
  /*
   * RFC 1808
   * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
   *
   */
    LPSTR lpszParam    = NULL;
    BOOL  bIsAbsolute = FALSE;
    LPSTR lpszap = (char*)lpszUrl;
    LPSTR lpszcp = NULL;

    TRACE("%s\n", debugstr_a(lpszUrl));

    /* Determine if the URI is absolute. */
    while (*lpszap != '\0')
    {
        if (isalnum(*lpszap))
        {
            lpszap++;
            continue;
        }
        if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
        {
            bIsAbsolute = TRUE;
            lpszcp = lpszap;
        }
	else
	{
	    lpszcp = (LPSTR)lpszUrl; /* Relative url */
        }

        break;
    }

    /* Parse <params> */
    lpszParam = strpbrk(lpszap, ";?");
    if (lpszParam != NULL)
    {
        if (!SetUrlComponentValue(&lpUrlComponents->lpszExtraInfo,
	     &lpUrlComponents->dwExtraInfoLength, lpszParam+1, strlen(lpszParam+1)))
        {
            return FALSE;
        }
    }


    if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
        {
	LPSTR lpszNetLoc;

        /* Get scheme first. */
	lpUrlComponents->nScheme = GetInternetScheme(lpszUrl, lpszcp - lpszUrl);
        if (!SetUrlComponentValue(&lpUrlComponents->lpszScheme,
		    &lpUrlComponents->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
	    return FALSE;

        /* Eat ':' in protocol. */
        lpszcp++;

        /* Skip over slashes. */
        if (*lpszcp == '/')
        {
            lpszcp++;
            if (*lpszcp == '/')
            {
                lpszcp++;
                if (*lpszcp == '/')
                    lpszcp++;
            }
        }

	lpszNetLoc = strpbrk(lpszcp, "/");
	if (lpszParam)
        {
	    if (lpszNetLoc)
		lpszNetLoc = min(lpszNetLoc, lpszParam);
	    else
		lpszNetLoc = lpszParam;
	}
	else if (!lpszNetLoc)
	    lpszNetLoc = lpszcp + strlen(lpszcp);

        /* Parse net-loc */
        if (lpszNetLoc)
        {
	    LPSTR lpszHost;
	    LPSTR lpszPort;

                /* [<user>[<:password>]@]<host>[:<port>] */
            /* First find the user and password if they exist */

            lpszHost = strchr(lpszcp, '@');
            if (lpszHost == NULL || lpszHost > lpszNetLoc)
            {
                /* username and password not specified. */
		SetUrlComponentValue(&lpUrlComponents->lpszUserName,
                                     &lpUrlComponents->dwUserNameLength, NULL, 0);
		SetUrlComponentValue(&lpUrlComponents->lpszPassword,
                                     &lpUrlComponents->dwPasswordLength, NULL, 0);
            }
            else /* Parse out username and password */
            {
		LPSTR lpszUser = lpszcp;
                LPSTR lpszPasswd = lpszPasswd;

		while (lpszcp < lpszHost)
                {
		   if (*lpszcp == ':')
		       lpszPasswd = lpszcp;

		   lpszcp++;
                }

		SetUrlComponentValue(&lpUrlComponents->lpszUserName,
			&lpUrlComponents->dwUserNameLength, lpszUser, lpszPasswd - lpszUser);

                if (lpszPasswd != lpszHost)
                    lpszPasswd++;
		SetUrlComponentValue(&lpUrlComponents->lpszPassword,
			&lpUrlComponents->dwPasswordLength,
			lpszPasswd == lpszHost ? NULL : lpszPasswd,
			lpszHost - lpszPasswd);

		lpszcp++; /* Advance to beginning of host */
                }

            /* Parse <host><:port> */

	    lpszHost = lpszcp;
	    lpszPort = lpszNetLoc;

	    while (lpszcp < lpszNetLoc)
	    {
		if (*lpszcp == ':')
		    lpszPort = lpszcp;

		lpszcp++;
	    }

            SetUrlComponentValue(&lpUrlComponents->lpszHostName,
               &lpUrlComponents->dwHostNameLength, lpszHost, lpszPort - lpszHost);
	    if (lpszPort != lpszNetLoc)
                lpUrlComponents->nPort = atoi(++lpszPort);
            else
                lpUrlComponents->nPort = 0;
            }
        }

    /* Here lpszcp points to:
     *
     * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
     *                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     */
    if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
    {
        INT len;

        /* Only truncate the parameter list if it's already been saved
         * in lpUrlComponents->lpszExtraInfo.
         */
        if (lpszParam && lpUrlComponents->dwExtraInfoLength)
            len = lpszParam - lpszcp;
        else
        {
            /* Leave the parameter list in lpszUrlPath.  Strip off any trailing
             * newlines if necessary.
             */
            LPSTR lpsznewline = strchr (lpszcp, '\n');
            if (lpsznewline != NULL)
                len = lpsznewline - lpszcp;
            else
                len = strlen(lpszcp);
        }

        if (!SetUrlComponentValue(&lpUrlComponents->lpszUrlPath,
				  &lpUrlComponents->dwUrlPathLength, lpszcp, len))
         return FALSE;
    }
    else
    {
        if (!SetUrlComponentValue(&lpUrlComponents->lpszUrlPath,
				  &lpUrlComponents->dwUrlPathLength, NULL, 0))
         return FALSE;
    }

    TRACE("%s: host(%s) path(%s) extra(%s)\n", lpszUrl, lpUrlComponents->lpszHostName,
          lpUrlComponents->lpszUrlPath, lpUrlComponents->lpszExtraInfo);

    return TRUE;
}


/***********************************************************************
 *           GetUrlCacheEntryInfoA (WININET.@)
 *
 */
BOOL WINAPI GetUrlCacheEntryInfoA(LPCSTR lpszUrl,
  LPINTERNET_CACHE_ENTRY_INFOA lpCacheEntry,
  LPDWORD lpCacheEntrySize)
{
    FIXME("stub\n");
    return FALSE;
}

/***********************************************************************
 *           CommitUrlCacheEntryA (WININET.@)
 *
 */
BOOL WINAPI CommitUrlCacheEntryA(LPCSTR lpszUrl, LPCSTR lpszLocalName,
    FILETIME ExpireTime, FILETIME lastModified, DWORD cacheEntryType,
    LPBYTE lpHeaderInfo, DWORD headerSize, LPCSTR fileExtension,
    DWORD originalUrl)
{
    FIXME("stub\n");
    return FALSE;
}

/***********************************************************************
 *           InternetAttemptConnect (WININET.@)
 *
 * Attempt to make a connection to the internet
 *
 * RETURNS
 *    ERROR_SUCCESS on success
 *    Error value   on failure
 *
 */
DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
{
    FIXME("Stub\n");
    return ERROR_SUCCESS;
}


/***********************************************************************
 *           InternetCanonicalizeUrlA (WININET.@)
 *
 * Escape unsafe characters and spaces
 *
 * RETURNS
 *    TRUE on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
	LPDWORD lpdwBufferLength, DWORD dwFlags)
{
    HRESULT hr;
    TRACE("%s %p %p %08lx\n",debugstr_a(lpszUrl), lpszBuffer,
	  lpdwBufferLength, dwFlags);

    /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
    dwFlags ^= ICU_NO_ENCODE;

    dwFlags |= 0x80000000; /* Don't know what this means */

    hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);

    return (hr == S_OK) ? TRUE : FALSE;
}

/***********************************************************************
 *           InternetCanonicalizeUrlW (WININET.@)
 *
 * Escape unsafe characters and spaces
 *
 * RETURNS
 *    TRUE on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
    LPDWORD lpdwBufferLength, DWORD dwFlags)
{
    HRESULT hr;
    TRACE("%s %p %p %08lx\n", debugstr_w(lpszUrl), lpszBuffer,
        lpdwBufferLength, dwFlags);

    /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
    dwFlags ^= ICU_NO_ENCODE;

    dwFlags |= 0x80000000; /* Don't know what this means */

    hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);

    return (hr == S_OK) ? TRUE : FALSE;
}


/***********************************************************************
 *           InternetSetStatusCallback (WININET.@)
 *
 * Sets up a callback function which is called as progress is made
 * during an operation.
 *
 * RETURNS
 *    Previous callback or NULL 	on success
 *    INTERNET_INVALID_STATUS_CALLBACK  on failure
 *
 */
INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
	HINTERNET hInternet, INTERNET_STATUS_CALLBACK lpfnIntCB)
{
    INTERNET_STATUS_CALLBACK retVal;

    LPWININETAPPINFOA lpwai = (LPWININETAPPINFOA)hInternet;

    TRACE("0x%08lx\n", (ULONG)hInternet);

   /* FIXME: Need to verify there are no outstanding requests on these
    * handles before changing the callback.
    */

   /* Should checking for HTTP, FTP and Gopher be pushed down into respective
    * files? */

   if( !lpwai ) goto bad_handle;

   /* Is this a handle created with InternetOpen? */
   if( ( lpwai->hdr.htype == WH_HINIT ) ||
       ( lpwai->hdr.htype == WH_HHTTPSESSION ) ||
       ( lpwai->hdr.htype == WH_HFTPSESSION ) ||
       ( lpwai->hdr.htype == WH_HGOPHERSESSION ) ||
       ( lpwai->hdr.htype == WH_HHTTPREQ )
     )
   {
       retVal = lpwai->hdr.lpfnStatusCB;
       lpwai->hdr.lpfnStatusCB = lpfnIntCB;
       return retVal;
   }

bad_handle:
   WARN( "Invalid handle provided\n" );
   return INTERNET_INVALID_STATUS_CALLBACK;
}


/***********************************************************************
 *           InternetWriteFile (WININET.@)
 *
 * Write data to an open internet file
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer ,
	DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
{
    BOOL retval = FALSE;
    int nSocket = -1;
    LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;

    TRACE("\n");
    if (NULL == lpwh)
        return FALSE;

    /* FIXME: this should use NETCON functions! */
    switch (lpwh->htype)
    {
        case WH_HHTTPREQ:
	    FIXME("This shouldn't be here! We don't support this kind"
		  " of connection anymore. Must use NETCON functions,"
		  " especially if using SSL\n");
	    nSocket = ((LPWININETHTTPREQA)hFile)->netConnection.socketFD;
            break;

        case WH_HFILE:
            nSocket = ((LPWININETFILE)hFile)->nDataSocket;
            break;

        default:
            break;
    }

    if (nSocket != -1)
    {
        int res = send(nSocket, lpBuffer, dwNumOfBytesToWrite, 0);
        retval = (res >= 0);
        *lpdwNumOfBytesWritten = retval ? res : 0;
    }

    return retval;
}


/***********************************************************************
 *           InternetReadFile (WININET.@)
 *
 * Read data from an open internet file
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
	DWORD dwNumOfBytesToRead, LPDWORD dwNumOfBytesRead)
{
    BOOL retval = FALSE;
    int nSocket = -1;
    LPWININETHANDLEHEADER lpwh = (LPWININETHANDLEHEADER) hFile;

    TRACE("\n");

    if (NULL == lpwh)
        return FALSE;

    /* FIXME: FTP should also use NETCON functions */
    switch (lpwh->htype)
    {
        case WH_HHTTPREQ:
	    *dwNumOfBytesRead = 0;
	    do {
		LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA)lpwh;
		int bytes_read;
		if (!NETCON_recv(&((LPWININETHTTPREQA)hFile)->netConnection, lpBuffer,
				 dwNumOfBytesToRead, 0, &bytes_read))
		{
		    retval = TRUE; /* Under windows, it seems to return 0 even if nothing was read... */
		    break;
		}
		*dwNumOfBytesRead += bytes_read;
		lpBuffer += bytes_read;
		dwNumOfBytesToRead -= bytes_read;
		if (lpwhr->dwContentLeft >= bytes_read)
		    lpwhr->dwContentLeft -= bytes_read;
		else
		    lpwhr->dwContentLeft = 0;
		if (!(dwNumOfBytesToRead && lpwhr->dwContentLeft)) {
		    retval = TRUE;
		    break;
		}
	    } while (TRUE);
            break;

        case WH_HFILE:
	    /* FIXME: FTP should use NETCON_ stuff */
	    nSocket = ((LPWININETFILE)hFile)->nDataSocket;
	    if (nSocket != -1)
	    {
		int res = recv(nSocket, lpBuffer, dwNumOfBytesToRead, 0);
		retval = (res >= 0);
		*dwNumOfBytesRead = retval ? res : 0;
	    }
            break;

        default:
            break;
    }
    TRACE("read %li: %s\n", *dwNumOfBytesRead, debugstr_a(lpBuffer));

    return retval;
}
/***********************************************************************
 *           InternetReadFileExA (WININET.@)
 *
 * Read data from an open internet file
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffer,
	DWORD dwFlags, DWORD dwContext)
{
  FIXME("stub\n");
  return FALSE;
}

/***********************************************************************
 *           InternetReadFileExW (WININET.@)
 *
 * Read data from an open internet file
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
	DWORD dwFlags, DWORD dwContext)
{
  FIXME("stub\n");

  INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
 *           InternetQueryOptionA (WININET.@)
 *
 * Queries an options on the specified handle
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
	LPVOID lpBuffer, LPDWORD lpdwBufferLength)
{
    LPWININETHANDLEHEADER lpwhh;
    BOOL bSuccess = FALSE;

    TRACE("0x%08lx\n", dwOption);

    if (NULL == hInternet)
    {
        INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
	return FALSE;
    }

    lpwhh = (LPWININETHANDLEHEADER) hInternet;

    switch (dwOption)
    {
        case INTERNET_OPTION_HANDLE_TYPE:
        {
            ULONG type = lpwhh->htype;
            TRACE("INTERNET_OPTION_HANDLE_TYPE: %ld\n", type);

            if (*lpdwBufferLength < sizeof(ULONG))
                INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
            else
            {
                memcpy(lpBuffer, &type, sizeof(ULONG));
                    *lpdwBufferLength = sizeof(ULONG);
                bSuccess = TRUE;
            }
            break;
        }

        case INTERNET_OPTION_REQUEST_FLAGS:
        {
            ULONG flags = 4;
            TRACE("INTERNET_OPTION_REQUEST_FLAGS: %ld\n", flags);
            if (*lpdwBufferLength < sizeof(ULONG))
                INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
            else
            {
                memcpy(lpBuffer, &flags, sizeof(ULONG));
                    *lpdwBufferLength = sizeof(ULONG);
                bSuccess = TRUE;
            }
            break;
        }

        case INTERNET_OPTION_URL:
        case INTERNET_OPTION_DATAFILE_NAME:
        {
            ULONG type = lpwhh->htype;
            if (type == WH_HHTTPREQ)
            {
                LPWININETHTTPREQA lpreq = hInternet;
                char url[1023];

                sprintf(url,"http://%s%s",lpreq->lpszHostName,lpreq->lpszPath);
                TRACE("INTERNET_OPTION_URL: %s\n",url);
                if (*lpdwBufferLength < strlen(url)+1)
                    INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
                else
                {
                    memcpy(lpBuffer, url, strlen(url)+1);
                        *lpdwBufferLength = strlen(url)+1;
                    bSuccess = TRUE;
                }
            }
            break;
        }
       case INTERNET_OPTION_HTTP_VERSION:
       {
            /*
             * Presently hardcoded to 1.1
             */
            ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
            ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
            bSuccess = TRUE;
        break;
       }

       default:
         FIXME("Stub! %ld \n",dwOption);
         break;
    }

    return bSuccess;
}


/***********************************************************************
 *           InternetSetOptionW (WININET.@)
 *
 * Sets an options on the specified handle
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
                           LPVOID lpBuffer, DWORD dwBufferLength)
{
    LPWININETHANDLEHEADER lpwhh;
    BOOL bSuccess = FALSE;

    TRACE("(%p, 0x%08lx, %p, %li)\n", hInternet, dwOption, lpBuffer, dwBufferLength);

    if (NULL == hInternet)
    {
        INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
	return FALSE;
    }

    lpwhh = (LPWININETHANDLEHEADER) hInternet;

    switch (dwOption)
    {
    default:
        INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
	FIXME("STUB (%p, 0x%08lx, %p, %li)\n",
	     hInternet, dwOption, lpBuffer, dwBufferLength);
        break;
    }

    return bSuccess;
}


/***********************************************************************
 *           InternetSetOptionA (WININET.@)
 *
 * Sets an options on the specified handle.
 *
 * RETURNS
 *    TRUE  on success
 *    FALSE on failure
 *
 */
BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
                           LPVOID lpBuffer, DWORD dwBufferLength)
{
    /* FIXME!!! implement if lpBuffer is a string, dwBufferLength is
       in TCHARs */
    return InternetSetOptionW(hInternet,dwOption, lpBuffer,
                              dwBufferLength);
}


/***********************************************************************
 *           GetInternetScheme (internal)
 *
 * Get scheme of url
 *
 * RETURNS
 *    scheme on success
 *    INTERNET_SCHEME_UNKNOWN on failure
 *
 */
INTERNET_SCHEME GetInternetScheme(LPCSTR lpszScheme, INT nMaxCmp)
{
    TRACE("\n");
    if(lpszScheme==NULL)
        return INTERNET_SCHEME_UNKNOWN;

    if (!strncasecmp("ftp", lpszScheme, nMaxCmp))
        return INTERNET_SCHEME_FTP;
    else if (!strncasecmp("gopher", lpszScheme, nMaxCmp))
        return INTERNET_SCHEME_GOPHER;
    else if (!strncasecmp("http", lpszScheme, nMaxCmp))
        return INTERNET_SCHEME_HTTP;
    else if (!strncasecmp("https", lpszScheme, nMaxCmp))
        return INTERNET_SCHEME_HTTPS;
    else if (!strncasecmp("file", lpszScheme, nMaxCmp))
        return INTERNET_SCHEME_FILE;
    else if (!strncasecmp("news", lpszScheme, nMaxCmp))
        return INTERNET_SCHEME_NEWS;
    else if (!strncasecmp("mailto", lpszScheme, nMaxCmp))
        return INTERNET_SCHEME_MAILTO;
    else
        return INTERNET_SCHEME_UNKNOWN;
}

/***********************************************************************
 *	InternetCheckConnectionA (WININET.@)
 *
 * Pings a requested host to check internet connection
 *
 * RETURNS
 *   TRUE on success and FALSE on failure. If a failure then
 *   ERROR_NOT_CONNECTED is placesd into GetLastError
 *
 */
BOOL WINAPI InternetCheckConnectionA( LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
{
/*
 * this is a kludge which runs the resident ping program and reads the output.
 *
 * Anyone have a better idea?
 */

  BOOL   rc = FALSE;
  char command[1024];
  char host[1024];
  int status = -1;

  FIXME("\n");

  /*
   * Crack or set the Address
   */
  if (lpszUrl == NULL)
  {
     /*
      * According to the doc we are supost to use the ip for the next
      * server in the WnInet internal server database. I have
      * no idea what that is or how to get it.
      *
      * So someone needs to implement this.
      */
     FIXME("Unimplemented with URL of NULL\n");
     return TRUE;
  }
  else
  {
     URL_COMPONENTSA componets;

     ZeroMemory(&componets,sizeof(URL_COMPONENTSA));
     componets.lpszHostName = (LPSTR)&host;
     componets.dwHostNameLength = 1024;

     if (!InternetCrackUrlA(lpszUrl,0,0,&componets))
       goto End;

     TRACE("host name : %s\n",componets.lpszHostName);
  }

  /*
   * Build our ping command
   */
  strcpy(command,"ping -w 1 ");
  strcat(command,host);
  strcat(command," >/dev/null 2>/dev/null");

  TRACE("Ping command is : %s\n",command);

  status = system(command);

  TRACE("Ping returned a code of %i \n",status);

  /* Ping return code of 0 indicates success */
  if (status == 0)
     rc = TRUE;

End:

  if (rc == FALSE)
    SetLastError(ERROR_NOT_CONNECTED);

  return rc;
}


/***********************************************************************
 *	InternetCheckConnectionW (WININET.@)
 *
 * Pings a requested host to check internet connection
 *
 * RETURNS
 *   TRUE on success and FALSE on failure. If a failure then
 *   ERROR_NOT_CONNECTED is placed into GetLastError
 *
 */
BOOL WINAPI InternetCheckConnectionW(LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
{
    CHAR *szUrl;
    INT len;
    BOOL rc;

    len = lstrlenW(lpszUrl)+1;
    if (!(szUrl = (CHAR *)HeapAlloc(GetProcessHeap(), 0, len*sizeof(CHAR))))
        return FALSE;
    WideCharToMultiByte(CP_ACP, -1, lpszUrl, -1, szUrl, len, NULL, NULL);
    rc = InternetCheckConnectionA((LPCSTR)szUrl, dwFlags, dwReserved);
    HeapFree(GetProcessHeap(), 0, szUrl);
    
    return rc;
}


/**********************************************************
 *	InternetOpenUrlA (WININET.@)
 *
 * Opens an URL
 *
 * RETURNS
 *   handle of connection or NULL on failure
 */
HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
    LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
{
  URL_COMPONENTSA urlComponents;
  char protocol[32], hostName[MAXHOSTNAME], userName[1024];
  char password[1024], path[2048], extra[1024];
  HINTERNET client = NULL, client1 = NULL;

  TRACE("(%p, %s, %s, %08lx, %08lx, %08lx\n", hInternet, debugstr_a(lpszUrl), debugstr_a(lpszHeaders),
       dwHeadersLength, dwFlags, dwContext);

  urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
  urlComponents.lpszScheme = protocol;
  urlComponents.dwSchemeLength = 32;
  urlComponents.lpszHostName = hostName;
  urlComponents.dwHostNameLength = MAXHOSTNAME;
  urlComponents.lpszUserName = userName;
  urlComponents.dwUserNameLength = 1024;
  urlComponents.lpszPassword = password;
  urlComponents.dwPasswordLength = 1024;
  urlComponents.lpszUrlPath = path;
  urlComponents.dwUrlPathLength = 2048;
  urlComponents.lpszExtraInfo = extra;
  urlComponents.dwExtraInfoLength = 1024;
  if(!InternetCrackUrlA(lpszUrl, strlen(lpszUrl), 0, &urlComponents))
    return NULL;
  switch(urlComponents.nScheme) {
  case INTERNET_SCHEME_FTP:
    TRACE("using INTERNET_SCHEME_FTP\n");
    if(urlComponents.nPort == 0)
      urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
    client = InternetConnectA(hInternet, hostName, urlComponents.nPort,
        userName, password, INTERNET_SERVICE_FTP, dwFlags, dwContext);
    return FtpOpenFileA(client, path, GENERIC_READ, dwFlags, dwContext);
    break;
  case INTERNET_SCHEME_HTTPS:
    FIXME("set SSL flags\n");
  case INTERNET_SCHEME_HTTP:
  {
    LPCSTR accept[2] = { "*/*", NULL };
    TRACE("using INTERNET_SCHEME_HTTP\n");
    if(urlComponents.nPort == 0) {
      if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
        urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
      else
	urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
    }
    client = InternetConnectA(hInternet, hostName, urlComponents.nPort, userName,
        password, INTERNET_SERVICE_HTTP, dwFlags, dwContext);
    if(client == NULL)
      return NULL;
    client1 = HttpOpenRequestA(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
    if(client1 == NULL) {
      InternetCloseHandle(client);
      return NULL;
    }
    if (lpszHeaders) HttpAddRequestHeadersA(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
    if(!HttpSendRequestA(client1, NULL, 0, NULL, 0)) {
      InternetCloseHandle(client1);
      InternetCloseHandle(client);
      return NULL;
    }
    TRACE("returning %p\n", client1);
    return client1;
    break;
  }
  case INTERNET_SCHEME_GOPHER:
    /* gopher doesn't seem to be implemented in wine, but it's supposed
     * to be supported by InternetOpenUrlA. */
  default:
    return NULL;
  }
  if(client != NULL)
    InternetCloseHandle(client);
}


/**********************************************************
 *	InternetOpenUrlW (WININET.@)
 *
 * Opens an URL
 *
 * RETURNS
 *   handle of connection or NULL on failure
 */
HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
    LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD dwContext)
{
    HINTERNET rc = (HINTERNET)NULL;

    INT lenUrl = lstrlenW(lpszUrl)+1;
    INT lenHeaders = lstrlenW(lpszHeaders)+1;
    CHAR *szUrl = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(CHAR));
    CHAR *szHeaders = (CHAR *)HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(CHAR));

    if (!szUrl || !szHeaders)
    {
        if (szUrl)
            HeapFree(GetProcessHeap(), 0, szUrl);
        if (szHeaders)
            HeapFree(GetProcessHeap(), 0, szHeaders);
        return (HINTERNET)NULL;
    }

    WideCharToMultiByte(CP_ACP, -1, lpszUrl, -1, szUrl, lenUrl,
        NULL, NULL);
    WideCharToMultiByte(CP_ACP, -1, lpszHeaders, -1, szHeaders, lenHeaders,
        NULL, NULL);

    rc = InternetOpenUrlA(hInternet, szUrl, szHeaders,
        dwHeadersLength, dwFlags, dwContext);

    HeapFree(GetProcessHeap(), 0, szUrl);
    HeapFree(GetProcessHeap(), 0, szHeaders);

    return rc;
}


/***********************************************************************
 *           INTERNET_SetLastError (internal)
 *
 * Set last thread specific error
 *
 * RETURNS
 *
 */
void INTERNET_SetLastError(DWORD dwError)
{
    LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);

    SetLastError(dwError);
    lpwite->dwError = dwError;
}


/***********************************************************************
 *           INTERNET_GetLastError (internal)
 *
 * Get last thread specific error
 *
 * RETURNS
 *
 */
DWORD INTERNET_GetLastError()
{
    LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
    return lpwite->dwError;
}


/***********************************************************************
 *           INTERNET_WorkerThreadFunc (internal)
 *
 * Worker thread execution function
 *
 * RETURNS
 *
 */
DWORD INTERNET_WorkerThreadFunc(LPVOID *lpvParam)
{
    DWORD dwWaitRes;

    while (1)
    {
        dwWaitRes = WaitForMultipleObjects(2, hEventArray, FALSE, MAX_IDLE_WORKER);

        if (dwWaitRes == WAIT_OBJECT_0 + 1)
            INTERNET_ExecuteWork();
        else
            break;

        InterlockedIncrement(&dwNumIdleThreads);
    }

    InterlockedDecrement(&dwNumIdleThreads);
    InterlockedDecrement(&dwNumThreads);
    TRACE("Worker thread exiting\n");
    return TRUE;
}


/***********************************************************************
 *           INTERNET_InsertWorkRequest (internal)
 *
 * Insert work request into queue
 *
 * RETURNS
 *
 */
BOOL INTERNET_InsertWorkRequest(LPWORKREQUEST lpWorkRequest)
{
    BOOL bSuccess = FALSE;
    LPWORKREQUEST lpNewRequest;

    TRACE("\n");

    lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
    if (lpNewRequest)
    {
        memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
	lpNewRequest->prev = NULL;

        EnterCriticalSection(&csQueue);

        lpNewRequest->next = lpWorkQueueTail;
	if (lpWorkQueueTail)
            lpWorkQueueTail->prev = lpNewRequest;
        lpWorkQueueTail = lpNewRequest;
	if (!lpHeadWorkQueue)
            lpHeadWorkQueue = lpWorkQueueTail;

        LeaveCriticalSection(&csQueue);

	bSuccess = TRUE;
    }

    return bSuccess;
}


/***********************************************************************
 *           INTERNET_GetWorkRequest (internal)
 *
 * Retrieves work request from queue
 *
 * RETURNS
 *
 */
static BOOL INTERNET_GetWorkRequest(LPWORKREQUEST* lplpWorkRequest)
{
    BOOL bSuccess = FALSE;
    LPWORKREQUEST lpRequest = NULL;

    TRACE("\n");

    EnterCriticalSection(&csQueue);

    if (lpHeadWorkQueue)
    {
        lpRequest = lpHeadWorkQueue;
        lpHeadWorkQueue = lpHeadWorkQueue->prev;
	if (lpRequest == lpWorkQueueTail)
            lpWorkQueueTail = lpHeadWorkQueue;
    }

    LeaveCriticalSection(&csQueue);

    *lplpWorkRequest = lpRequest;

    if (lpRequest)
    {
	bSuccess = TRUE;
    }

    return bSuccess;
}


/***********************************************************************
 *           INTERNET_AsyncCall (internal)
 *
 * Retrieves work request from queue
 *
 * RETURNS
 *
 */
BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
{
    HANDLE hThread;
    DWORD dwTID;
    BOOL bSuccess = FALSE;

    TRACE("\n");

    if (InterlockedDecrement(&dwNumIdleThreads) < 0)
    {
        InterlockedIncrement(&dwNumIdleThreads);

	if (InterlockedIncrement(&dwNumThreads) > MAX_WORKER_THREADS ||
	    !(hThread = CreateThread(NULL, 0,
            (LPTHREAD_START_ROUTINE)INTERNET_WorkerThreadFunc, NULL, 0, &dwTID)))
	{
            InterlockedDecrement(&dwNumThreads);
            INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
	    goto lerror;
	}

	TRACE("Created new thread\n");
        CloseHandle(hThread);
    }

    bSuccess = TRUE;
    INTERNET_InsertWorkRequest(lpWorkRequest);
    SetEvent(hWorkEvent);

lerror:

    return bSuccess;
}


/***********************************************************************
 *           INTERNET_ExecuteWork (internal)
 *
 * RETURNS
 *
 */
VOID INTERNET_ExecuteWork()
{
    LPWORKREQUEST lpWorkRequest;

    TRACE("\n");

    while (INTERNET_GetWorkRequest(&lpWorkRequest))
    {
	switch (lpWorkRequest->asyncall)
	{
            case FTPPUTFILEA:
		FTP_FtpPutFileA((HINTERNET)lpWorkRequest->HFTPSESSION, (LPCSTR)lpWorkRequest->LPSZLOCALFILE,
                    (LPCSTR)lpWorkRequest->LPSZNEWREMOTEFILE, lpWorkRequest->DWFLAGS, lpWorkRequest->DWCONTEXT);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZLOCALFILE);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZNEWREMOTEFILE);
		break;

            case FTPSETCURRENTDIRECTORYA:
		FTP_FtpSetCurrentDirectoryA((HINTERNET)lpWorkRequest->HFTPSESSION,
			(LPCSTR)lpWorkRequest->LPSZDIRECTORY);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZDIRECTORY);
		break;

            case FTPCREATEDIRECTORYA:
		FTP_FtpCreateDirectoryA((HINTERNET)lpWorkRequest->HFTPSESSION,
			(LPCSTR)lpWorkRequest->LPSZDIRECTORY);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZDIRECTORY);
		break;

            case FTPFINDFIRSTFILEA:
                FTP_FtpFindFirstFileA((HINTERNET)lpWorkRequest->HFTPSESSION,
			(LPCSTR)lpWorkRequest->LPSZSEARCHFILE,
	           (LPWIN32_FIND_DATAA)lpWorkRequest->LPFINDFILEDATA, lpWorkRequest->DWFLAGS,
		   lpWorkRequest->DWCONTEXT);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZSEARCHFILE);
		break;

            case FTPGETCURRENTDIRECTORYA:
                FTP_FtpGetCurrentDirectoryA((HINTERNET)lpWorkRequest->HFTPSESSION,
			(LPSTR)lpWorkRequest->LPSZDIRECTORY, (LPDWORD)lpWorkRequest->LPDWDIRECTORY);
		break;

            case FTPOPENFILEA:
                 FTP_FtpOpenFileA((HINTERNET)lpWorkRequest->HFTPSESSION,
                    (LPCSTR)lpWorkRequest->LPSZFILENAME,
                    lpWorkRequest->FDWACCESS,
                    lpWorkRequest->DWFLAGS,
                    lpWorkRequest->DWCONTEXT);
                 HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZFILENAME);
                 break;

            case FTPGETFILEA:
                FTP_FtpGetFileA((HINTERNET)lpWorkRequest->HFTPSESSION,
                    (LPCSTR)lpWorkRequest->LPSZREMOTEFILE,
                    (LPCSTR)lpWorkRequest->LPSZNEWFILE,
                    (BOOL)lpWorkRequest->FFAILIFEXISTS,
                    lpWorkRequest->DWLOCALFLAGSATTRIBUTE,
                    lpWorkRequest->DWFLAGS,
                    lpWorkRequest->DWCONTEXT);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZREMOTEFILE);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZNEWFILE);
		break;

            case FTPDELETEFILEA:
                FTP_FtpDeleteFileA((HINTERNET)lpWorkRequest->HFTPSESSION,
			(LPCSTR)lpWorkRequest->LPSZFILENAME);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZFILENAME);
		break;

            case FTPREMOVEDIRECTORYA:
                FTP_FtpRemoveDirectoryA((HINTERNET)lpWorkRequest->HFTPSESSION,
			(LPCSTR)lpWorkRequest->LPSZDIRECTORY);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZDIRECTORY);
		break;

            case FTPRENAMEFILEA:
                FTP_FtpRenameFileA((HINTERNET)lpWorkRequest->HFTPSESSION,
			(LPCSTR)lpWorkRequest->LPSZSRCFILE,
			(LPCSTR)lpWorkRequest->LPSZDESTFILE);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZSRCFILE);
		HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZDESTFILE);
		break;

            case INTERNETFINDNEXTA:
		INTERNET_FindNextFileA((HINTERNET)lpWorkRequest->HFTPSESSION,
                    (LPWIN32_FIND_DATAA)lpWorkRequest->LPFINDFILEDATA);
		break;

            case HTTPSENDREQUESTA:
               HTTP_HttpSendRequestA((HINTERNET)lpWorkRequest->HFTPSESSION,
                       (LPCSTR)lpWorkRequest->LPSZHEADER,
                       lpWorkRequest->DWHEADERLENGTH,
                       (LPVOID)lpWorkRequest->LPOPTIONAL,
                       lpWorkRequest->DWOPTIONALLENGTH);
               HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZHEADER);
               break;

            case HTTPOPENREQUESTA:
               HTTP_HttpOpenRequestA((HINTERNET)lpWorkRequest->HFTPSESSION,
                       (LPCSTR)lpWorkRequest->LPSZVERB,
                       (LPCSTR)lpWorkRequest->LPSZOBJECTNAME,
                       (LPCSTR)lpWorkRequest->LPSZVERSION,
                       (LPCSTR)lpWorkRequest->LPSZREFERRER,
                       (LPCSTR*)lpWorkRequest->LPSZACCEPTTYPES,
                       lpWorkRequest->DWFLAGS,
                       lpWorkRequest->DWCONTEXT);
               HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZVERB);
               HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZOBJECTNAME);
               HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZVERSION);
               HeapFree(GetProcessHeap(), 0, (LPVOID)lpWorkRequest->LPSZREFERRER);
                break;

            case SENDCALLBACK:
               SendAsyncCallbackInt((LPWININETHANDLEHEADER)lpWorkRequest->param1,
                       (HINTERNET)lpWorkRequest->param2, lpWorkRequest->param3,
                        lpWorkRequest->param4, (LPVOID)lpWorkRequest->param5,
                        lpWorkRequest->param6);
               break;
	}

        HeapFree(GetProcessHeap(), 0, lpWorkRequest);
    }
}


/***********************************************************************
 *          INTERNET_GetResponseBuffer
 *
 * RETURNS
 *
 */
LPSTR INTERNET_GetResponseBuffer()
{
    LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
    TRACE("\n");
    return lpwite->response;
}

/***********************************************************************
 *           INTERNET_GetNextLine  (internal)
 *
 * Parse next line in directory string listing
 *
 * RETURNS
 *   Pointer to beginning of next line
 *   NULL on failure
 *
 */

LPSTR INTERNET_GetNextLine(INT nSocket, LPSTR lpszBuffer, LPDWORD dwBuffer)
{
    struct timeval tv;
    fd_set infd;
    BOOL bSuccess = FALSE;
    INT nRecv = 0;

    TRACE("\n");

    FD_ZERO(&infd);
    FD_SET(nSocket, &infd);
    tv.tv_sec=RESPONSE_TIMEOUT;
    tv.tv_usec=0;

    while (nRecv < *dwBuffer)
    {
        if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
        {
            if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
            {
                INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
                goto lend;
	    }

            if (lpszBuffer[nRecv] == '\n')
	    {
		bSuccess = TRUE;
                break;
	    }
            if (lpszBuffer[nRecv] != '\r')
                nRecv++;
        }
	else
	{
            INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
            goto lend;
        }
    }

lend:
    if (bSuccess)
    {
        lpszBuffer[nRecv] = '\0';
	*dwBuffer = nRecv - 1;
        TRACE(":%d %s\n", nRecv, lpszBuffer);
        return lpszBuffer;
    }
    else
    {
        return NULL;
    }
}

/***********************************************************************
 *
 */
BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
                                LPDWORD lpdwNumberOfBytesAvailble,
                                DWORD dwFlags, DWORD dwConext)
{
    LPWININETHTTPREQA lpwhr = (LPWININETHTTPREQA) hFile;
    INT retval = -1;
    char buffer[4048];


    if (NULL == lpwhr)
    {
        SetLastError(ERROR_NO_MORE_FILES);
        return FALSE;
    }

    TRACE("-->  %p %i\n",lpwhr,lpwhr->hdr.htype);

    switch (lpwhr->hdr.htype)
    {
        case WH_HHTTPREQ:
	    if (!NETCON_recv(&((LPWININETHTTPREQA)hFile)->netConnection, buffer,
			     4048, MSG_PEEK, (int *)lpdwNumberOfBytesAvailble))
	    {
                SetLastError(ERROR_NO_MORE_FILES);
                retval = FALSE;
	    }
	    else
		retval = TRUE;
            break;

        default:
            FIXME("unsuported file type\n");
	    break;
    }

    TRACE("<-- %i\n",retval);
    return (retval+1);
}


/***********************************************************************
 *
 */
BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
*lphLockReqHandle)
{
    FIXME("STUB\n");
    return FALSE;
}

BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
{
    FIXME("STUB\n");
    return FALSE;
}


/***********************************************************************
 *           InternetAutoDial
 *
 * On windows this function is supposed to dial the default internet
 * connection. We don't want to have Wine dial out to the internet so
 * we return TRUE by default. It might be nice to check if we are connected.
 *
 * RETURNS
 *   TRUE on success
 *   FALSE on failure
 *
 */
BOOL WINAPI InternetAutoDial(DWORD dwFlags, HWND hwndParent)
{
    FIXME("STUB\n");

    /* Tell that we are connected to the internet. */
    return TRUE;
}

/***********************************************************************
 *           InternetAutoDialHangup
 *
 * Hangs up an connection made with InternetAutoDial
 *
 * PARAM
 *    dwReserved
 * RETURNS
 *   TRUE on success
 *   FALSE on failure
 *
 */
BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
{
    FIXME("STUB\n");

    /* we didn't dial, we don't disconnect */
    return TRUE;
}

/***********************************************************************
 *         InternetCombineUrlA
 *
 */
BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
                                LPSTR lpszBuffer, LPDWORD lpdwBufferLength, DWORD dwFlags)
{
    DWORD flags = 0;
    HRESULT ret;

    if (dwFlags & ICU_BROWSER_MODE)
        FIXME("ICU_BROWSER_MODE\n");
    if (dwFlags & ICU_DECODE)
        flags |= URL_UNESCAPE;
    if (dwFlags & ICU_ENCODE_PERCENT)
        flags |= URL_ESCAPE_PERCENT;
    if (dwFlags & ICU_ENCODE_SPACES_ONLY)
        flags |= URL_ESCAPE_SPACES_ONLY;
    /* if (dwFlags & ICU_NO_ENCODE) ; - think this is default? */
    if (dwFlags & ICU_NO_META)
        flags |= URL_DONT_SIMPLIFY;

    ret = UrlCombineA(lpszBaseUrl, lpszRelativeUrl, lpszBuffer, lpdwBufferLength, flags);

    if (ret == S_OK)
        return TRUE;
    if (ret == E_POINTER)
    {
        SetLastError(ERROR_INSUFFICIENT_BUFFER);
        return FALSE;
    }
    FIXME("failed due to other reason. need to SetLastError\n");
    return FALSE;
}

/***********************************************************************
 *         InternetCombineUrlW
 *
 */
BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
                                LPWSTR lpszBuffer, LPDWORD lpdwBufferLength, DWORD dwFlags)
{
    DWORD flags = 0;
    HRESULT ret;

    if (dwFlags & ICU_BROWSER_MODE)
        FIXME("ICU_BROWSER_MODE\n");
    if (dwFlags & ICU_DECODE)
        flags |= URL_UNESCAPE;
    if (dwFlags & ICU_ENCODE_PERCENT)
        flags |= URL_ESCAPE_PERCENT;
    if (dwFlags & ICU_ENCODE_SPACES_ONLY)
        flags |= URL_ESCAPE_SPACES_ONLY;
    /* if (dwFlags & ICU_NO_ENCODE) ; - think this is default? */
    if (dwFlags & ICU_NO_META)
        flags |= URL_DONT_SIMPLIFY;

    ret = UrlCombineW(lpszBaseUrl, lpszRelativeUrl, lpszBuffer, lpdwBufferLength, flags);

    if (ret == S_OK)
        return TRUE;
    if (ret == E_POINTER)
    {
        SetLastError(ERROR_INSUFFICIENT_BUFFER);
        return FALSE;
    }
    FIXME("failed due to other reason. need to SetLastError\n");
    return FALSE;
}
