ftp.c 12.9 KB
Newer Older
1
/*
2
 * Copyright 2005-2009 Jacek Caban for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18 19
 */

#include "urlmon_main.h"
20 21 22 23

#define NO_SHLWAPI_REG
#include "shlwapi.h"

24 25 26 27 28
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(urlmon);

typedef struct {
29 30
    Protocol base;

31 32 33
    IInternetProtocolEx IInternetProtocolEx_iface;
    IInternetPriority   IInternetPriority_iface;
    IWinInetHttpInfo    IWinInetHttpInfo_iface;
34

35 36 37
    LONG ref;
} FtpProtocol;

38 39 40 41 42 43 44 45 46 47 48 49 50 51
static inline FtpProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
{
    return CONTAINING_RECORD(iface, FtpProtocol, IInternetProtocolEx_iface);
}

static inline FtpProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
{
    return CONTAINING_RECORD(iface, FtpProtocol, IInternetPriority_iface);
}
static inline FtpProtocol *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)

{
    return CONTAINING_RECORD(iface, FtpProtocol, IWinInetHttpInfo_iface);
}
52

53 54 55 56
static inline FtpProtocol *impl_from_Protocol(Protocol *prot)
{
    return CONTAINING_RECORD(prot, FtpProtocol, base);
}
57

58
static HRESULT FtpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
59
        HINTERNET internet_session, IInternetBindInfo *bind_info)
60
{
61
    FtpProtocol *This = impl_from_Protocol(prot);
62 63 64 65 66 67
    BSTR url;
    HRESULT hres;

    hres = IUri_GetAbsoluteUri(uri, &url);
    if(FAILED(hres))
        return hres;
68

69
    This->base.request = InternetOpenUrlW(internet_session, url, NULL, 0,
70 71
            request_flags|INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_PASSIVE,
            (DWORD_PTR)&This->base);
72
    SysFreeString(url);
73 74 75 76 77 78 79 80
    if (!This->base.request && GetLastError() != ERROR_IO_PENDING) {
        WARN("InternetOpenUrl failed: %d\n", GetLastError());
        return INET_E_RESOURCE_NOT_FOUND;
    }

    return S_OK;
}

81 82 83 84 85
static HRESULT FtpProtocol_end_request(Protocol *prot)
{
    return E_NOTIMPL;
}

86 87
static HRESULT FtpProtocol_start_downloading(Protocol *prot)
{
88
    FtpProtocol *This = impl_from_Protocol(prot);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    DWORD size;
    BOOL res;

    res = FtpGetFileSize(This->base.request, &size);
    if(res)
        This->base.content_length = size;
    else
        WARN("FtpGetFileSize failed: %d\n", GetLastError());

    return S_OK;
}

static void FtpProtocol_close_connection(Protocol *prot)
{
}

105 106 107 108 109
static void FtpProtocol_on_error(Protocol *prot, DWORD error)
{
    FIXME("(%p) %d - stub\n", prot, error);
}

110 111
static const ProtocolVtbl AsyncProtocolVtbl = {
    FtpProtocol_open_request,
112
    FtpProtocol_end_request,
113
    FtpProtocol_start_downloading,
114 115
    FtpProtocol_close_connection,
    FtpProtocol_on_error
116 117
};

118
static HRESULT WINAPI FtpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
119
{
120
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
121 122 123 124

    *ppv = NULL;
    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
125
        *ppv = &This->IInternetProtocolEx_iface;
126 127
    }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
        TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
128
        *ppv = &This->IInternetProtocolEx_iface;
129 130
    }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
        TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
131
        *ppv = &This->IInternetProtocolEx_iface;
132 133
    }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
        TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
134
        *ppv = &This->IInternetProtocolEx_iface;
135 136
    }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
        TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
137
        *ppv = &This->IInternetPriority_iface;
138 139
    }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
        TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
140
        *ppv = &This->IWinInetHttpInfo_iface;
141 142
    }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
        TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
143
        *ppv = &This->IWinInetHttpInfo_iface;
144 145 146
    }

    if(*ppv) {
147
        IInternetProtocolEx_AddRef(iface);
148 149 150 151 152 153 154
        return S_OK;
    }

    WARN("not supported interface %s\n", debugstr_guid(riid));
    return E_NOINTERFACE;
}

155
static ULONG WINAPI FtpProtocol_AddRef(IInternetProtocolEx *iface)
156
{
157
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
158
    LONG ref = InterlockedIncrement(&This->ref);
159
    TRACE("(%p) ref=%d\n", This, ref);
160 161 162
    return ref;
}

163
static ULONG WINAPI FtpProtocol_Release(IInternetProtocolEx *iface)
164
{
165
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
166 167
    LONG ref = InterlockedDecrement(&This->ref);

168
    TRACE("(%p) ref=%d\n", This, ref);
169 170

    if(!ref) {
171
        protocol_close_connection(&This->base);
172
        heap_free(This);
173 174 175 176 177 178 179

        URLMON_UnlockModule();
    }

    return ref;
}

180
static HRESULT WINAPI FtpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
181
        IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
182
        DWORD grfPI, HANDLE_PTR dwReserved)
183
{
184
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
185 186
    IUri *uri;
    HRESULT hres;
187

188
    TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
189 190
          pOIBindInfo, grfPI, dwReserved);

191 192 193 194
    hres = CreateUri(szUrl, 0, 0, &uri);
    if(FAILED(hres))
        return hres;

195 196
    hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
            pOIBindInfo, grfPI, (HANDLE*)dwReserved);
197 198 199

    IUri_Release(uri);
    return hres;
200 201
}

202
static HRESULT WINAPI FtpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
203
{
204
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
205 206 207 208

    TRACE("(%p)->(%p)\n", This, pProtocolData);

    return protocol_continue(&This->base, pProtocolData);
209 210
}

211
static HRESULT WINAPI FtpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
212 213
        DWORD dwOptions)
{
214
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
215 216 217 218

    TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);

    return protocol_abort(&This->base, hrReason);
219 220
}

221
static HRESULT WINAPI FtpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
222
{
223
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
224 225 226 227 228

    TRACE("(%p)->(%08x)\n", This, dwOptions);

    protocol_close_connection(&This->base);
    return S_OK;
229 230
}

231
static HRESULT WINAPI FtpProtocol_Suspend(IInternetProtocolEx *iface)
232
{
233
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
234 235 236 237
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

238
static HRESULT WINAPI FtpProtocol_Resume(IInternetProtocolEx *iface)
239
{
240
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
241 242 243 244
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

245
static HRESULT WINAPI FtpProtocol_Read(IInternetProtocolEx *iface, void *pv,
246 247
        ULONG cb, ULONG *pcbRead)
{
248
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
249 250 251 252

    TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);

    return protocol_read(&This->base, pv, cb, pcbRead);
253 254
}

255
static HRESULT WINAPI FtpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
256
        DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
257
{
258
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
259
    FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
260 261 262
    return E_NOTIMPL;
}

263
static HRESULT WINAPI FtpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
264
{
265
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
266 267 268 269

    TRACE("(%p)->(%08x)\n", This, dwOptions);

    return protocol_lock_request(&This->base);
270 271
}

272
static HRESULT WINAPI FtpProtocol_UnlockRequest(IInternetProtocolEx *iface)
273
{
274
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
275 276 277 278

    TRACE("(%p)\n", This);

    return protocol_unlock_request(&This->base);
279 280
}

281 282 283 284
static HRESULT WINAPI FtpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
        IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
        DWORD grfPI, HANDLE *dwReserved)
{
285
    FtpProtocol *This = impl_from_IInternetProtocolEx(iface);
286 287 288 289 290 291 292 293 294 295 296 297
    DWORD scheme = 0;
    HRESULT hres;

    TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
            pOIBindInfo, grfPI, dwReserved);

    hres = IUri_GetScheme(pUri, &scheme);
    if(FAILED(hres))
        return hres;
    if(scheme != URL_SCHEME_FTP)
        return MK_E_SYNTAX;

298 299
    return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
                          pOIProtSink, pOIBindInfo);
300
}
301

302
static const IInternetProtocolExVtbl FtpProtocolVtbl = {
303 304 305 306 307 308 309 310 311 312 313 314
    FtpProtocol_QueryInterface,
    FtpProtocol_AddRef,
    FtpProtocol_Release,
    FtpProtocol_Start,
    FtpProtocol_Continue,
    FtpProtocol_Abort,
    FtpProtocol_Terminate,
    FtpProtocol_Suspend,
    FtpProtocol_Resume,
    FtpProtocol_Read,
    FtpProtocol_Seek,
    FtpProtocol_LockRequest,
315 316
    FtpProtocol_UnlockRequest,
    FtpProtocol_StartEx
317 318
};

319 320
static HRESULT WINAPI FtpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
{
321 322
    FtpProtocol *This = impl_from_IInternetPriority(iface);
    return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
323 324 325 326
}

static ULONG WINAPI FtpPriority_AddRef(IInternetPriority *iface)
{
327 328
    FtpProtocol *This = impl_from_IInternetPriority(iface);
    return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
329 330 331 332
}

static ULONG WINAPI FtpPriority_Release(IInternetPriority *iface)
{
333 334
    FtpProtocol *This = impl_from_IInternetPriority(iface);
    return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
335 336 337 338
}

static HRESULT WINAPI FtpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
{
339
    FtpProtocol *This = impl_from_IInternetPriority(iface);
340 341 342 343 344

    TRACE("(%p)->(%d)\n", This, nPriority);

    This->base.priority = nPriority;
    return S_OK;
345 346 347 348
}

static HRESULT WINAPI FtpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
{
349
    FtpProtocol *This = impl_from_IInternetPriority(iface);
350 351 352 353 354

    TRACE("(%p)->(%p)\n", This, pnPriority);

    *pnPriority = This->base.priority;
    return S_OK;
355 356 357 358 359 360 361 362 363 364
}

static const IInternetPriorityVtbl FtpPriorityVtbl = {
    FtpPriority_QueryInterface,
    FtpPriority_AddRef,
    FtpPriority_Release,
    FtpPriority_SetPriority,
    FtpPriority_GetPriority
};

365 366
static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
{
367 368
    FtpProtocol *This = impl_from_IWinInetHttpInfo(iface);
    return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
369 370 371 372
}

static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
{
373 374
    FtpProtocol *This = impl_from_IWinInetHttpInfo(iface);
    return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
375 376 377 378
}

static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
{
379 380
    FtpProtocol *This = impl_from_IWinInetHttpInfo(iface);
    return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
381 382 383 384 385
}

static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
        void *pBuffer, DWORD *pcbBuffer)
{
386
    FtpProtocol *This = impl_from_IWinInetHttpInfo(iface);
387 388 389 390 391 392 393 394
    TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);

    if(!This->base.request)
        return E_FAIL;

    if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
        return S_FALSE;
    return S_OK;
395 396 397 398 399
}

static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
        void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
{
400
    FtpProtocol *This = impl_from_IWinInetHttpInfo(iface);
401 402 403 404 405 406 407 408
    TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);

    if(!This->base.request)
        return E_FAIL;

    if(!HttpQueryInfoW(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags))
        return S_FALSE;
    return S_OK;
409 410 411 412 413 414 415 416 417 418
}

static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
    HttpInfo_QueryInterface,
    HttpInfo_AddRef,
    HttpInfo_Release,
    HttpInfo_QueryOption,
    HttpInfo_QueryInfo
};

419 420 421 422 423 424 425 426
HRESULT FtpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
{
    FtpProtocol *ret;

    TRACE("(%p %p)\n", pUnkOuter, ppobj);

    URLMON_LockModule();

427
    ret = heap_alloc_zero(sizeof(FtpProtocol));
428

429
    ret->base.vtbl = &AsyncProtocolVtbl;
430 431 432
    ret->IInternetProtocolEx_iface.lpVtbl = &FtpProtocolVtbl;
    ret->IInternetPriority_iface.lpVtbl   = &FtpPriorityVtbl;
    ret->IWinInetHttpInfo_iface.lpVtbl    = &WinInetHttpInfoVtbl;
433 434
    ret->ref = 1;

435 436
    *ppobj = &ret->IInternetProtocolEx_iface;

437 438
    return S_OK;
}