protocol.c 27.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2005 Jacek Caban
 *
 * 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */

#include "config.h"

#include <stdarg.h>
#include <stdio.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"

#include "wine/debug.h"

#include "mshtml_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

37
typedef struct {
38
    IUnknown          IUnknown_inner;
39 40 41 42 43 44 45 46
    IInternetProtocol IInternetProtocol_iface;

    LONG ref;

    BYTE *data;
    ULONG data_len;
    ULONG cur;

47
    IUnknown *outer;
48 49
} InternetProtocol;

50 51 52 53 54
/********************************************************************
 * common ProtocolFactory implementation
 */

typedef struct {
55 56
    IInternetProtocolInfo IInternetProtocolInfo_iface;
    IClassFactory         IClassFactory_iface;
57 58
} ProtocolFactory;

59 60 61 62
static inline ProtocolFactory *impl_from_IInternetProtocolInfo(IInternetProtocolInfo *iface)
{
    return CONTAINING_RECORD(iface, ProtocolFactory, IInternetProtocolInfo_iface);
}
63 64 65

static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface, REFIID riid, void **ppv)
{
66
    ProtocolFactory *This = impl_from_IInternetProtocolInfo(iface);
67 68 69 70

    *ppv = NULL;
    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
71
        *ppv = &This->IInternetProtocolInfo_iface;
72 73
    }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
        TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
74
        *ppv = &This->IInternetProtocolInfo_iface;
75 76
    }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
        TRACE("(%p)->(IID_IClassFactory %p)\n", This, ppv);
77
        *ppv = &This->IClassFactory_iface;
78 79 80 81 82 83 84 85 86 87 88 89 90
    }

    if(!*ppv) {
        WARN("unknown interface %s\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

    IInternetProtocolInfo_AddRef(iface);
    return S_OK;
}

static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
{
91
    TRACE("(%p)\n", iface);
92 93 94 95 96
    return 2;
}

static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
{
97
    TRACE("(%p)\n", iface);
98 99 100
    return 1;
}

101 102 103 104
static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
        LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
        DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
{
105
    TRACE("%p)->(%s %s %08x %p %d %p %d)\n", iface, debugstr_w(pwzBaseUrl),
106 107 108 109 110 111
            debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
            pcchResult, dwReserved);

    return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
}

112 113 114 115 116 117 118
static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
        LPCWSTR pwzUrl2, DWORD dwCompareFlags)
{
    TRACE("%p)->(%s %s %08x)\n", iface, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
    return E_NOTIMPL;
}

119 120 121 122
static inline ProtocolFactory *impl_from_IClassFactory(IClassFactory *iface)
{
    return CONTAINING_RECORD(iface, ProtocolFactory, IClassFactory_iface);
}
123 124 125

static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
{
126 127
    ProtocolFactory *This = impl_from_IClassFactory(iface);
    return IInternetProtocolInfo_QueryInterface(&This->IInternetProtocolInfo_iface, riid, ppv);
128 129 130 131
}

static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
{
132 133
    ProtocolFactory *This = impl_from_IClassFactory(iface);
    return IInternetProtocolInfo_AddRef(&This->IInternetProtocolInfo_iface);
134 135 136 137
}

static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
{
138 139
    ProtocolFactory *This = impl_from_IClassFactory(iface);
    return IInternetProtocolInfo_Release(&This->IInternetProtocolInfo_iface);
140 141 142 143
}

static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
{
144
    TRACE("(%p)->(%x)\n", iface, dolock);
145 146 147
    return S_OK;
}

148 149 150 151 152
static inline InternetProtocol *impl_from_IUnknown(IUnknown *iface)
{
    return CONTAINING_RECORD(iface, InternetProtocol, IUnknown_inner);
}

153
static inline InternetProtocol *impl_from_IInternetProtocol(IInternetProtocol *iface)
154
{
155
    return CONTAINING_RECORD(iface, InternetProtocol, IInternetProtocol_iface);
156
}
157

158
static HRESULT WINAPI Protocol_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
159
{
160
    InternetProtocol *This = impl_from_IUnknown(iface);
161

162
    TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
163 164

    if(IsEqualGUID(&IID_IUnknown, riid)) {
165
        *ppv = &This->IUnknown_inner;
166
    }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
167
        *ppv = &This->IInternetProtocol_iface;
168
    }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
169
        *ppv = &This->IInternetProtocol_iface;
170 171 172 173
    }else {
        if(IsEqualGUID(&IID_IServiceProvider, riid))
            FIXME("IServiceProvider is not implemented\n");
        *ppv = NULL;
174 175 176
        return E_NOINTERFACE;
    }

177
    IUnknown_AddRef((IUnknown*)*ppv);
178 179 180
    return S_OK;
}

181
static ULONG WINAPI Protocol_AddRef(IUnknown *iface)
182
{
183
    InternetProtocol *This = impl_from_IUnknown(iface);
184
    ULONG ref = InterlockedIncrement(&This->ref);
185
    TRACE("(%p) ref=%d\n", iface, ref);
186
    return ref;
187 188
}

189
static ULONG WINAPI Protocol_Release(IUnknown *iface)
190
{
191
    InternetProtocol *This = impl_from_IUnknown(iface);
192 193
    ULONG ref = InterlockedDecrement(&This->ref);

194
    TRACE("(%p) ref=%x\n", iface, ref);
195

196
    if(!ref) {
197 198
        heap_free(This->data);
        heap_free(This);
199
    }
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    return ref;
}

static const IUnknownVtbl ProtocolUnkVtbl = {
    Protocol_QueryInterface,
    Protocol_AddRef,
    Protocol_Release
};

static HRESULT WINAPI InternetProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
{
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
    return IUnknown_QueryInterface(This->outer, riid, ppv);
}

static ULONG WINAPI InternetProtocol_AddRef(IInternetProtocol *iface)
{
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
    return IUnknown_AddRef(This->outer);
}

static ULONG WINAPI InternetProtocol_Release(IInternetProtocol *iface)
{
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
    return IUnknown_Release(This->outer);
226 227
}

228
static HRESULT WINAPI InternetProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
229
{
230
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
231 232 233 234
    FIXME("(%p)->(%p)\n", This, pProtocolData);
    return E_NOTIMPL;
}

235
static HRESULT WINAPI InternetProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
236 237
        DWORD dwOptions)
{
238
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
239
    FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
240 241 242
    return E_NOTIMPL;
}

243
static HRESULT WINAPI InternetProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
244
{
245
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
246
    TRACE("(%p)->(%08x)\n", This, dwOptions);
247
    return S_OK;
248 249
}

250
static HRESULT WINAPI InternetProtocol_Suspend(IInternetProtocol *iface)
251
{
252
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
253 254 255 256
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

257
static HRESULT WINAPI InternetProtocol_Resume(IInternetProtocol *iface)
258
{
259
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
260 261 262 263
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

264
static HRESULT WINAPI InternetProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
265
{
266
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
267

268
    TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
269 270 271 272 273 274 275 276 277

    if(!This->data)
        return E_FAIL;

    *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);

    if(!*pcbRead)
        return S_FALSE;

278
    memcpy(pv, This->data+This->cur, *pcbRead);
279 280 281
    This->cur += *pcbRead;

    return S_OK;
282 283
}

284
static HRESULT WINAPI InternetProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
285 286
        DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
{
287
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
288
    FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
289 290 291
    return E_NOTIMPL;
}

292
static HRESULT WINAPI InternetProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
293
{
294
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
295

296
    TRACE("(%p)->(%d)\n", This, dwOptions);
297 298

    return S_OK;
299 300
}

301
static HRESULT WINAPI InternetProtocol_UnlockRequest(IInternetProtocol *iface)
302
{
303
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
304 305 306 307

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

    return S_OK;
308 309
}

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
static HRESULT create_protocol_instance(const IInternetProtocolVtbl *protocol_vtbl,
                                        IUnknown *outer, REFIID riid, void **ppv)
{
    InternetProtocol *protocol;
    HRESULT hres;

    if(outer && !IsEqualGUID(&IID_IUnknown, riid)) {
        *ppv = NULL;
        return E_INVALIDARG;
    }

    protocol = heap_alloc_zero(sizeof(InternetProtocol));
    protocol->IUnknown_inner.lpVtbl = &ProtocolUnkVtbl;
    protocol->IInternetProtocol_iface.lpVtbl = protocol_vtbl;
    protocol->outer = outer ? outer : &protocol->IUnknown_inner;
    protocol->ref = 1;

    hres = IUnknown_QueryInterface(&protocol->IUnknown_inner, riid, ppv);
    IUnknown_Release(&protocol->IUnknown_inner);
    return hres;
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
/********************************************************************
 * about protocol implementation
 */

static HRESULT WINAPI AboutProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
        IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
        DWORD grfPI, HANDLE_PTR dwReserved)
{
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
    BINDINFO bindinfo;
    DWORD grfBINDF = 0;
    LPCWSTR text = NULL;
    DWORD data_len;
    BYTE *data;
    HRESULT hres;

    static const WCHAR html_begin[] = {0xfeff,'<','H','T','M','L','>',0};
    static const WCHAR html_end[] = {'<','/','H','T','M','L','>',0};
    static const WCHAR wszBlank[] = {'b','l','a','n','k',0};
    static const WCHAR wszAbout[] = {'a','b','o','u','t',':'};
    static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};

    /* NOTE:
     * the about protocol seems not to work as I would expect. It creates html document
     * for a given url, eg. about:some_text -> <HTML>some_text</HTML> except for the case when
     * some_text = "blank", when document is blank (<HTML></HMTL>). The same happens
     * when the url does not have "about:" in the beginning.
     */

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

    memset(&bindinfo, 0, sizeof(bindinfo));
    bindinfo.cbSize = sizeof(BINDINFO);
    hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
    if(FAILED(hres))
        return hres;
    ReleaseBindInfo(&bindinfo);

    TRACE("bindf %x\n", grfBINDF);

373 374
    if(strlenW(szUrl) >= ARRAY_SIZE(wszAbout) && !memcmp(wszAbout, szUrl, sizeof(wszAbout))) {
        text = szUrl + ARRAY_SIZE(wszAbout);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        if(!strcmpW(wszBlank, text))
            text = NULL;
    }

    data_len = sizeof(html_begin)+sizeof(html_end)-sizeof(WCHAR)
        + (text ? strlenW(text)*sizeof(WCHAR) : 0);
    data = heap_alloc(data_len);
    if(!data)
        return E_OUTOFMEMORY;

    heap_free(This->data);
    This->data = data;
    This->data_len = data_len;

    memcpy(This->data, html_begin, sizeof(html_begin));
    if(text)
        strcatW((LPWSTR)This->data, text);
    strcatW((LPWSTR)This->data, html_end);

    This->cur = 0;

    IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);

    IInternetProtocolSink_ReportData(pOIProtSink,
            BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
            This->data_len, This->data_len);

    IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);

    return S_OK;
}

407
static const IInternetProtocolVtbl AboutProtocolVtbl = {
408 409 410
    InternetProtocol_QueryInterface,
    InternetProtocol_AddRef,
    InternetProtocol_Release,
411
    AboutProtocol_Start,
412 413 414 415 416 417 418 419 420
    InternetProtocol_Continue,
    InternetProtocol_Abort,
    InternetProtocol_Terminate,
    InternetProtocol_Suspend,
    InternetProtocol_Resume,
    InternetProtocol_Read,
    InternetProtocol_Seek,
    InternetProtocol_LockRequest,
    InternetProtocol_UnlockRequest
421 422 423 424 425
};

static HRESULT WINAPI AboutProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
        REFIID riid, void **ppv)
{
426
    TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
427

428
    return create_protocol_instance(&AboutProtocolVtbl, pUnkOuter, riid, ppv);
429 430 431 432 433 434
}

static HRESULT WINAPI AboutProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
        PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
        DWORD* pcchResult, DWORD dwReserved)
{
435
    TRACE("%p)->(%s %d %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
436
            dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
437 438

    if(ParseAction == PARSE_SECURITY_URL) {
439
        unsigned int len = strlenW(pwzUrl)+1;
440

441 442
        *pcchResult = len;
        if(len > cchResult)
443 444
            return S_FALSE;

445
        memcpy(pwzResult, pwzUrl, len*sizeof(WCHAR));
446 447 448 449
        return S_OK;
    }

    if(ParseAction == PARSE_DOMAIN) {
450 451 452 453 454 455 456 457
        if(!pcchResult)
            return E_POINTER;

        if(pwzUrl)
            *pcchResult = strlenW(pwzUrl)+1;
        else
            *pcchResult = 1;
        return E_FAIL;
458 459 460
    }

    return INET_E_DEFAULT_ACTION;
461 462 463 464 465 466
}

static HRESULT WINAPI AboutProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
        QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
        DWORD dwReserved)
{
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
          cbBuffer, pcbBuf, dwReserved);

    switch(QueryOption) {
    case QUERY_CAN_NAVIGATE:
        return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

    case QUERY_USES_NETWORK:
        if(!pBuffer || cbBuffer < sizeof(DWORD))
            return E_FAIL;

        *(DWORD*)pBuffer = 0;
        if(pcbBuf)
            *pcbBuf = sizeof(DWORD);

        break;

    case QUERY_IS_CACHED:
        FIXME("Unsupported option QUERY_IS_CACHED\n");
        return E_NOTIMPL;
    case QUERY_IS_INSTALLEDENTRY:
        FIXME("Unsupported option QUERY_IS_INSTALLEDENTRY\n");
        return E_NOTIMPL;
    case QUERY_IS_CACHED_OR_MAPPED:
        FIXME("Unsupported option QUERY_IS_CACHED_OR_MAPPED\n");
        return E_NOTIMPL;
    case QUERY_IS_SECURE:
        FIXME("Unsupported option QUERY_IS_SECURE\n");
        return E_NOTIMPL;
    case QUERY_IS_SAFE:
        FIXME("Unsupported option QUERY_IS_SAFE\n");
        return E_NOTIMPL;
499 500 501
    case QUERY_USES_HISTORYFOLDER:
        FIXME("Unsupported option QUERY_USES_HISTORYFOLDER\n");
        return E_FAIL;
502 503 504
    case QUERY_IS_CACHED_AND_USABLE_OFFLINE:
        FIXME("Unsupported option QUERY_IS_CACHED_AND_USABLE_OFFLINE\n");
        return E_NOTIMPL;
505 506 507 508 509
    default:
        return E_FAIL;
    }

    return S_OK;
510 511 512 513 514 515 516
}

static const IInternetProtocolInfoVtbl AboutProtocolInfoVtbl = {
    InternetProtocolInfo_QueryInterface,
    InternetProtocolInfo_AddRef,
    InternetProtocolInfo_Release,
    AboutProtocolInfo_ParseUrl,
517
    InternetProtocolInfo_CombineUrl,
518
    InternetProtocolInfo_CompareUrl,
519 520 521 522 523 524 525 526 527 528 529 530
    AboutProtocolInfo_QueryInfo
};

static const IClassFactoryVtbl AboutProtocolFactoryVtbl = {
    ClassFactory_QueryInterface,
    ClassFactory_AddRef,
    ClassFactory_Release,
    AboutProtocolFactory_CreateInstance,
    ClassFactory_LockServer
};

static ProtocolFactory AboutProtocolFactory = {
531 532
    { &AboutProtocolInfoVtbl },
    { &AboutProtocolFactoryVtbl }
533 534 535
};

/********************************************************************
536
 * res protocol implementation
537 538 539 540
 */

static HRESULT WINAPI ResProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
        IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
541
        DWORD grfPI, HANDLE_PTR dwReserved)
542
{
543
    InternetProtocol *This = impl_from_IInternetProtocol(iface);
544
    WCHAR *url_dll, *url_file, *url, *mime, *res_type = (LPWSTR)RT_HTML, *ptr;
545
    DWORD grfBINDF = 0, len;
Jacek Caban's avatar
Jacek Caban committed
546 547 548
    BINDINFO bindinfo;
    HMODULE hdll;
    HRSRC src;
549
    HRESULT hres;
Jacek Caban's avatar
Jacek Caban committed
550 551 552

    static const WCHAR wszRes[] = {'r','e','s',':','/','/'};

553
    TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
554
            pOIBindInfo, grfPI, dwReserved);
Jacek Caban's avatar
Jacek Caban committed
555 556 557

    memset(&bindinfo, 0, sizeof(bindinfo));
    bindinfo.cbSize = sizeof(BINDINFO);
558 559 560
    hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
    if(FAILED(hres))
        return hres;
561
    ReleaseBindInfo(&bindinfo);
Jacek Caban's avatar
Jacek Caban committed
562

563
    len = strlenW(szUrl)+16;
564
    url = heap_alloc(len*sizeof(WCHAR));
565 566
    hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
    if(FAILED(hres)) {
567
        WARN("CoInternetParseUrl failed: %08x\n", hres);
568
        heap_free(url);
569 570 571 572
        IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
        return hres;
    }

573
    if(len < ARRAY_SIZE(wszRes) || memcmp(url, wszRes, sizeof(wszRes))) {
574
        WARN("Wrong protocol of url: %s\n", debugstr_w(url));
575
        IInternetProtocolSink_ReportResult(pOIProtSink, E_INVALIDARG, 0, NULL);
576
        heap_free(url);
577
        return E_INVALIDARG;
Jacek Caban's avatar
Jacek Caban committed
578 579
    }

580
    url_dll = url + ARRAY_SIZE(wszRes);
581
    if(!(res_type = strchrW(url_dll, '/'))) {
582
        WARN("wrong url: %s\n", debugstr_w(url));
Jacek Caban's avatar
Jacek Caban committed
583
        IInternetProtocolSink_ReportResult(pOIProtSink, MK_E_SYNTAX, 0, NULL);
584
        heap_free(url);
Jacek Caban's avatar
Jacek Caban committed
585 586 587
        return MK_E_SYNTAX;
    }

588 589 590 591 592
    *res_type++ = 0;
    if ((url_file = strchrW(res_type, '/'))) {
        *url_file++ = 0;
    }else {
        url_file = res_type;
593
        res_type = (LPWSTR)RT_HTML;
594 595
    }

596 597 598
    /* Ignore query and hash parts. */
    if((ptr = strchrW(url_file, '?')))
        *ptr = 0;
599
    if(*url_file && (ptr = strchrW(url_file+1, '#')))
600 601
        *ptr = 0;

602
    hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
Jacek Caban's avatar
Jacek Caban committed
603
    if(!hdll) {
604 605 606 607
        WARN("Could not open dll: %s\n", debugstr_w(url_dll));
        IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
        heap_free(url);
        return HRESULT_FROM_WIN32(GetLastError());
Jacek Caban's avatar
Jacek Caban committed
608 609
    }

610 611 612
    TRACE("trying to find resource type %s, name %s\n", debugstr_w(res_type), debugstr_w(url_file));

    src = FindResourceW(hdll, url_file, res_type);
Jacek Caban's avatar
Jacek Caban committed
613
    if(!src) {
614 615 616
        LPWSTR endpoint = NULL;
        DWORD file_id = strtolW(url_file, &endpoint, 10);
        if(endpoint == url_file+strlenW(url_file))
617
            src = FindResourceW(hdll, MAKEINTRESOURCEW(file_id), res_type);
618 619 620 621 622

        if(!src) {
            WARN("Could not find resource\n");
            IInternetProtocolSink_ReportResult(pOIProtSink,
                    HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
623
            heap_free(url);
624 625
            return HRESULT_FROM_WIN32(GetLastError());
        }
Jacek Caban's avatar
Jacek Caban committed
626 627 628 629
    }

    if(This->data) {
        WARN("data already loaded\n");
630
        heap_free(This->data);
Jacek Caban's avatar
Jacek Caban committed
631 632 633
    }

    This->data_len = SizeofResource(hdll, src);
634
    This->data = heap_alloc(This->data_len);
Jacek Caban's avatar
Jacek Caban committed
635 636 637 638 639
    memcpy(This->data, LoadResource(hdll, src), This->data_len);
    This->cur = 0;

    FreeLibrary(hdll);

640
    hres = FindMimeFromData(NULL, url_file, This->data, This->data_len, NULL, 0, &mime, 0);
641
    heap_free(url);
642 643 644 645 646
    if(SUCCEEDED(hres)) {
        IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
        CoTaskMemFree(mime);
    }

Jacek Caban's avatar
Jacek Caban committed
647 648 649 650 651 652 653
    IInternetProtocolSink_ReportData(pOIProtSink,
            BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
            This->data_len, This->data_len);

    IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
    
    return S_OK;
654 655 656
}

static const IInternetProtocolVtbl ResProtocolVtbl = {
657 658 659
    InternetProtocol_QueryInterface,
    InternetProtocol_AddRef,
    InternetProtocol_Release,
660
    ResProtocol_Start,
661 662 663 664 665 666 667 668 669
    InternetProtocol_Continue,
    InternetProtocol_Abort,
    InternetProtocol_Terminate,
    InternetProtocol_Suspend,
    InternetProtocol_Resume,
    InternetProtocol_Read,
    InternetProtocol_Seek,
    InternetProtocol_LockRequest,
    InternetProtocol_UnlockRequest
670 671 672 673 674
};

static HRESULT WINAPI ResProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
        REFIID riid, void **ppv)
{
675
    TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
676

677
    return create_protocol_instance(&ResProtocolVtbl, pUnkOuter, riid, ppv);
678 679 680 681 682 683
}

static HRESULT WINAPI ResProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
        PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
        DWORD* pcchResult, DWORD dwReserved)
{
684
    TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
685
            dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
686 687

    if(ParseAction == PARSE_SECURITY_URL) {
688
        WCHAR file_part[MAX_PATH], full_path[MAX_PATH];
689
        WCHAR *ptr;
690
        DWORD size, len;
691 692 693 694

        static const WCHAR wszFile[] = {'f','i','l','e',':','/','/'};
        static const WCHAR wszRes[] = {'r','e','s',':','/','/'};

695
        if(strlenW(pwzUrl) <= ARRAY_SIZE(wszRes) || memcmp(pwzUrl, wszRes, sizeof(wszRes)))
696
            return E_INVALIDARG;
697

698
        ptr = strchrW(pwzUrl + ARRAY_SIZE(wszRes), '/');
699
        if(!ptr)
700
            return E_INVALIDARG;
701

702 703
        len = ptr - (pwzUrl + ARRAY_SIZE(wszRes));
        if(len >= ARRAY_SIZE(file_part)) {
704 705 706
            FIXME("Too long URL\n");
            return MK_E_SYNTAX;
        }
707

708
        memcpy(file_part, pwzUrl + ARRAY_SIZE(wszRes), len*sizeof(WCHAR));
709 710
        file_part[len] = 0;

711
        len = SearchPathW(NULL, file_part, NULL, ARRAY_SIZE(full_path), full_path, NULL);
712
        if(!len) {
713 714 715 716 717 718 719 720 721 722
            HMODULE module;

            /* SearchPath does not work well with winelib files (like our test executable),
             * so we also try to load the library here */
            module = LoadLibraryExW(file_part, NULL, LOAD_LIBRARY_AS_DATAFILE);
            if(!module) {
                WARN("Could not find file %s\n", debugstr_w(file_part));
                return MK_E_SYNTAX;
            }

723
            len = GetModuleFileNameW(module, full_path, ARRAY_SIZE(full_path));
724 725 726
            FreeLibrary(module);
            if(!len)
                return E_FAIL;
727
        }
728

729
        size = ARRAY_SIZE(wszFile) + len + 1;
730 731
        if(pcchResult)
            *pcchResult = size;
732
        if(size > cchResult)
733 734 735
            return S_FALSE;

        memcpy(pwzResult, wszFile, sizeof(wszFile));
736
        memcpy(pwzResult + ARRAY_SIZE(wszFile), full_path, (len+1)*sizeof(WCHAR));
737 738 739 740
        return S_OK;
    }

    if(ParseAction == PARSE_DOMAIN) {
741 742 743 744 745 746 747 748
        if(!pcchResult)
            return E_POINTER;

        if(pwzUrl)
            *pcchResult = strlenW(pwzUrl)+1;
        else
            *pcchResult = 1;
        return E_FAIL;
749 750 751
    }

    return INET_E_DEFAULT_ACTION;
752 753 754 755 756 757
}

static HRESULT WINAPI ResProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
        QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
        DWORD dwReserved)
{
758 759 760 761 762 763 764 765 766 767 768 769 770 771
    TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
          cbBuffer, pcbBuf, dwReserved);

    switch(QueryOption) {
    case QUERY_USES_NETWORK:
        if(!pBuffer || cbBuffer < sizeof(DWORD))
            return E_FAIL;

        *(DWORD*)pBuffer = 0;
        if(pcbBuf)
            *pcbBuf = sizeof(DWORD);
        break;

    case QUERY_IS_SECURE:
772
        FIXME("QUERY_IS_SECURE not supported\n");
773 774
        return E_NOTIMPL;
    case QUERY_IS_SAFE:
775
        FIXME("QUERY_IS_SAFE not supported\n");
776 777 778 779 780 781
        return E_NOTIMPL;
    default:
        return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
    }

    return S_OK;
782 783 784 785 786 787 788
}

static const IInternetProtocolInfoVtbl ResProtocolInfoVtbl = {
    InternetProtocolInfo_QueryInterface,
    InternetProtocolInfo_AddRef,
    InternetProtocolInfo_Release,
    ResProtocolInfo_ParseUrl,
789
    InternetProtocolInfo_CombineUrl,
790
    InternetProtocolInfo_CompareUrl,
791 792 793 794 795 796 797 798 799 800 801 802
    ResProtocolInfo_QueryInfo
};

static const IClassFactoryVtbl ResProtocolFactoryVtbl = {
    ClassFactory_QueryInterface,
    ClassFactory_AddRef,
    ClassFactory_Release,
    ResProtocolFactory_CreateInstance,
    ClassFactory_LockServer
};

static ProtocolFactory ResProtocolFactory = {
803 804
    { &ResProtocolInfoVtbl },
    { &ResProtocolFactoryVtbl }
805 806
};

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
/********************************************************************
 * JSProtocol implementation
 */

static HRESULT WINAPI JSProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
        REFIID riid, void **ppv)
{
    FIXME("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
    return E_NOTIMPL;
}

static HRESULT WINAPI JSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
        PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
        DWORD* pcchResult, DWORD dwReserved)
{
822
    TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
823
          dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
824 825 826 827 828 829 830 831 832 833 834 835 836

    switch(ParseAction) {
    case PARSE_SECURITY_URL:
        FIXME("PARSE_SECURITY_URL\n");
        return E_NOTIMPL;
    case PARSE_DOMAIN:
        FIXME("PARSE_DOMAIN\n");
        return E_NOTIMPL;
    default:
        return INET_E_DEFAULT_ACTION;
    }

    return S_OK;
837 838 839 840 841 842
}

static HRESULT WINAPI JSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
        QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
        DWORD dwReserved)
{
843
    TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
844
          cbBuffer, pcbBuf, dwReserved);
845 846 847 848 849 850 851 852 853 854 855 856

    switch(QueryOption) {
    case QUERY_USES_NETWORK:
        if(!pBuffer || cbBuffer < sizeof(DWORD))
            return E_FAIL;

        *(DWORD*)pBuffer = 0;
        if(pcbBuf)
            *pcbBuf = sizeof(DWORD);
        break;

    case QUERY_IS_SECURE:
857
        FIXME("QUERY_IS_SECURE not supported\n");
858 859 860 861 862 863 864
        return E_NOTIMPL;

    default:
        return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
    }

    return S_OK;
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
}

static const IInternetProtocolInfoVtbl JSProtocolInfoVtbl = {
    InternetProtocolInfo_QueryInterface,
    InternetProtocolInfo_AddRef,
    InternetProtocolInfo_Release,
    JSProtocolInfo_ParseUrl,
    InternetProtocolInfo_CombineUrl,
    InternetProtocolInfo_CompareUrl,
    JSProtocolInfo_QueryInfo
};

static const IClassFactoryVtbl JSProtocolFactoryVtbl = {
    ClassFactory_QueryInterface,
    ClassFactory_AddRef,
    ClassFactory_Release,
    JSProtocolFactory_CreateInstance,
    ClassFactory_LockServer
};

static ProtocolFactory JSProtocolFactory = {
886 887
    { &JSProtocolInfoVtbl },
    { &JSProtocolFactoryVtbl }
888 889
};

890 891 892 893 894 895 896 897
HRESULT ProtocolFactory_Create(REFCLSID rclsid, REFIID riid, void **ppv)
{
    ProtocolFactory *cf = NULL;

    if(IsEqualGUID(&CLSID_AboutProtocol, rclsid))
        cf = &AboutProtocolFactory;
    else if(IsEqualGUID(&CLSID_ResProtocol, rclsid))
        cf = &ResProtocolFactory;
898 899
    else if(IsEqualGUID(&CLSID_JSProtocol, rclsid))
        cf = &JSProtocolFactory;
900 901 902 903 904

    if(!cf) {
        FIXME("not implemented protocol %s\n", debugstr_guid(rclsid));
        return CLASS_E_CLASSNOTAVAILABLE;
    }
905
 
906
    return IInternetProtocolInfo_QueryInterface(&cf->IInternetProtocolInfo_iface, riid, ppv);
907
}