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

#include "urlmon_main.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(urlmon);

typedef struct {
25 26
    Protocol base;

27 28
    IInternetProtocol IInternetProtocol_iface;
    IInternetPriority IInternetPriority_iface;
29 30 31 32

    LONG ref;
} GopherProtocol;

33 34 35 36
static inline GopherProtocol *impl_from_Protocol(Protocol *prot)
{
    return CONTAINING_RECORD(prot, GopherProtocol, base);
}
37

38
static HRESULT GopherProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
39
        HINTERNET internet_session, IInternetBindInfo *bind_info)
40
{
41
    GopherProtocol *This = impl_from_Protocol(prot);
42 43 44 45 46 47
    BSTR url;
    HRESULT hres;

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

49
    This->base.request = InternetOpenUrlW(internet_session, url, NULL, 0,
50
            request_flags, (DWORD_PTR)&This->base);
51
    SysFreeString(url);
52 53 54 55 56 57 58 59
    if (!This->base.request && GetLastError() != ERROR_IO_PENDING) {
        WARN("InternetOpenUrl failed: %d\n", GetLastError());
        return INET_E_RESOURCE_NOT_FOUND;
    }

    return S_OK;
}

60 61 62 63 64
static HRESULT GopherProtocol_end_request(Protocol *prot)
{
    return E_NOTIMPL;
}

65 66 67 68 69 70 71 72 73
static HRESULT GopherProtocol_start_downloading(Protocol *prot)
{
    return S_OK;
}

static void GopherProtocol_close_connection(Protocol *prot)
{
}

74 75 76 77 78
static void GopherProtocol_on_error(Protocol *prot, DWORD error)
{
    FIXME("(%p) %d - stub\n", prot, error);
}

79 80
static const ProtocolVtbl AsyncProtocolVtbl = {
    GopherProtocol_open_request,
81
    GopherProtocol_end_request,
82
    GopherProtocol_start_downloading,
83 84
    GopherProtocol_close_connection,
    GopherProtocol_on_error
85 86
};

87 88 89 90
static inline GopherProtocol *impl_from_IInternetProtocol(IInternetProtocol *iface)
{
    return CONTAINING_RECORD(iface, GopherProtocol, IInternetProtocol_iface);
}
91 92 93

static HRESULT WINAPI GopherProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
{
94
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
95 96 97 98

    *ppv = NULL;
    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
99
        *ppv = &This->IInternetProtocol_iface;
100 101
    }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
        TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
102
        *ppv = &This->IInternetProtocol_iface;
103 104
    }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
        TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
105
        *ppv = &This->IInternetProtocol_iface;
106 107
    }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
        TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
108
        *ppv = &This->IInternetPriority_iface;
109 110 111 112 113 114 115 116 117 118 119 120 121
    }

    if(*ppv) {
        IInternetProtocol_AddRef(iface);
        return S_OK;
    }

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

static ULONG WINAPI GopherProtocol_AddRef(IInternetProtocol *iface)
{
122
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
123 124 125 126 127 128 129
    LONG ref = InterlockedIncrement(&This->ref);
    TRACE("(%p) ref=%d\n", This, ref);
    return ref;
}

static ULONG WINAPI GopherProtocol_Release(IInternetProtocol *iface)
{
130
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    LONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
        heap_free(This);

        URLMON_UnlockModule();
    }

    return ref;
}

static HRESULT WINAPI GopherProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
        IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
146
        DWORD grfPI, HANDLE_PTR dwReserved)
147
{
148
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
149 150
    IUri *uri;
    HRESULT hres;
151

152
    TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
153
          pOIBindInfo, grfPI, dwReserved);
154

155 156 157 158
    hres = CreateUri(szUrl, 0, 0, &uri);
    if(FAILED(hres))
        return hres;

159 160
    hres = protocol_start(&This->base, &This->IInternetProtocol_iface, uri, pOIProtSink,
            pOIBindInfo);
161 162 163

    IUri_Release(uri);
    return hres;
164 165 166 167
}

static HRESULT WINAPI GopherProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
{
168
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
169 170 171 172

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

    return protocol_continue(&This->base, pProtocolData);
173 174 175 176 177
}

static HRESULT WINAPI GopherProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
        DWORD dwOptions)
{
178
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
179 180 181 182

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

    return protocol_abort(&This->base, hrReason);
183 184 185 186
}

static HRESULT WINAPI GopherProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
{
187
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
188 189 190 191 192

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

    protocol_close_connection(&This->base);
    return S_OK;
193 194 195 196
}

static HRESULT WINAPI GopherProtocol_Suspend(IInternetProtocol *iface)
{
197
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
198 199 200 201 202 203
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI GopherProtocol_Resume(IInternetProtocol *iface)
{
204
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
205 206 207 208 209 210 211
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI GopherProtocol_Read(IInternetProtocol *iface, void *pv,
        ULONG cb, ULONG *pcbRead)
{
212
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
213 214 215 216

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

    return protocol_read(&This->base, pv, cb, pcbRead);
217 218 219 220 221
}

static HRESULT WINAPI GopherProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
        DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
{
222
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
223 224 225 226 227 228
    FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
    return E_NOTIMPL;
}

static HRESULT WINAPI GopherProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
{
229
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
230 231 232 233

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

    return protocol_lock_request(&This->base);
234 235 236 237
}

static HRESULT WINAPI GopherProtocol_UnlockRequest(IInternetProtocol *iface)
{
238
    GopherProtocol *This = impl_from_IInternetProtocol(iface);
239 240 241 242

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

    return protocol_unlock_request(&This->base);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
}

static const IInternetProtocolVtbl GopherProtocolVtbl = {
    GopherProtocol_QueryInterface,
    GopherProtocol_AddRef,
    GopherProtocol_Release,
    GopherProtocol_Start,
    GopherProtocol_Continue,
    GopherProtocol_Abort,
    GopherProtocol_Terminate,
    GopherProtocol_Suspend,
    GopherProtocol_Resume,
    GopherProtocol_Read,
    GopherProtocol_Seek,
    GopherProtocol_LockRequest,
    GopherProtocol_UnlockRequest
};

261 262 263 264
static inline GopherProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
{
    return CONTAINING_RECORD(iface, GopherProtocol, IInternetPriority_iface);
}
265 266 267

static HRESULT WINAPI GopherPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
{
268 269
    GopherProtocol *This = impl_from_IInternetPriority(iface);
    return IInternetProtocol_QueryInterface(&This->IInternetProtocol_iface, riid, ppv);
270 271 272 273
}

static ULONG WINAPI GopherPriority_AddRef(IInternetPriority *iface)
{
274 275
    GopherProtocol *This = impl_from_IInternetPriority(iface);
    return IInternetProtocol_AddRef(&This->IInternetProtocol_iface);
276 277 278 279
}

static ULONG WINAPI GopherPriority_Release(IInternetPriority *iface)
{
280 281
    GopherProtocol *This = impl_from_IInternetPriority(iface);
    return IInternetProtocol_Release(&This->IInternetProtocol_iface);
282 283 284 285
}

static HRESULT WINAPI GopherPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
{
286
    GopherProtocol *This = impl_from_IInternetPriority(iface);
287 288 289 290 291 292 293 294 295

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

    This->base.priority = nPriority;
    return S_OK;
}

static HRESULT WINAPI GopherPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
{
296
    GopherProtocol *This = impl_from_IInternetPriority(iface);
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

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

    *pnPriority = This->base.priority;
    return S_OK;
}

static const IInternetPriorityVtbl GopherPriorityVtbl = {
    GopherPriority_QueryInterface,
    GopherPriority_AddRef,
    GopherPriority_Release,
    GopherPriority_SetPriority,
    GopherPriority_GetPriority
};

312 313 314 315 316 317 318 319 320 321
HRESULT GopherProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
{
    GopherProtocol *ret;

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

    URLMON_LockModule();

    ret = heap_alloc_zero(sizeof(GopherProtocol));

322
    ret->base.vtbl = &AsyncProtocolVtbl;
323 324
    ret->IInternetProtocol_iface.lpVtbl = &GopherProtocolVtbl;
    ret->IInternetPriority_iface.lpVtbl = &GopherPriorityVtbl;
325 326
    ret->ref = 1;

327
    *ppobj = &ret->IInternetProtocol_iface;
328 329 330

    return S_OK;
}