2 * Copyright 2005 Jacek Caban
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * 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
28 #include "urlmon_main.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
35 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
36 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
44 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
45 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
47 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, InternetProtocol, iface)
49 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
51 FileProtocol *This = PROTOCOL_THIS(iface);
54 if(IsEqualGUID(&IID_IUnknown, riid)) {
55 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
56 *ppv = PROTOCOL(This);
57 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
58 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
59 *ppv = PROTOCOL(This);
60 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
61 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
62 *ppv = PROTOCOL(This);
63 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
64 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
65 *ppv = PRIORITY(This);
69 IInternetProtocol_AddRef(iface);
73 WARN("not supported interface %s\n", debugstr_guid(riid));
77 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
79 FileProtocol *This = PROTOCOL_THIS(iface);
80 LONG ref = InterlockedIncrement(&This->ref);
81 TRACE("(%p) ref=%d\n", This, ref);
85 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
87 FileProtocol *This = PROTOCOL_THIS(iface);
88 LONG ref = InterlockedDecrement(&This->ref);
90 TRACE("(%p) ref=%d\n", This, ref);
94 CloseHandle(This->file);
97 URLMON_UnlockModule();
103 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
104 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
105 DWORD grfPI, DWORD dwReserved)
107 FileProtocol *This = PROTOCOL_THIS(iface);
112 LPWSTR url, mime = NULL;
115 BOOL first_call = FALSE;
118 static const WCHAR wszFile[] = {'f','i','l','e',':'};
120 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
121 pOIBindInfo, grfPI, dwReserved);
123 memset(&bindinfo, 0, sizeof(bindinfo));
124 bindinfo.cbSize = sizeof(BINDINFO);
125 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
127 WARN("GetBindInfo failed: %08x\n", hres);
131 ReleaseBindInfo(&bindinfo);
133 if(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
134 || memcmp(szUrl, wszFile, sizeof(wszFile)))
137 len = lstrlenW(szUrl)+16;
138 url = urlmon_alloc(len*sizeof(WCHAR));
139 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
145 if(!(grfBINDF & BINDF_FROMURLMON))
146 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
151 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
153 file_name = url+sizeof(wszFile)/sizeof(WCHAR);
154 if(file_name[0] == '/' && file_name[1] == '/')
156 if(*file_name == '/')
159 This->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
160 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
162 if(This->file == INVALID_HANDLE_VALUE) {
164 IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
165 GetLastError(), NULL);
167 return INET_E_RESOURCE_NOT_FOUND;
170 IInternetProtocolSink_ReportProgress(pOIProtSink,
171 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_name);
173 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
174 if(SUCCEEDED(hres)) {
175 IInternetProtocolSink_ReportProgress(pOIProtSink,
176 (grfBINDF & BINDF_FROMURLMON) ?
177 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
185 if(GetFileSizeEx(This->file, &size))
186 IInternetProtocolSink_ReportData(pOIProtSink,
187 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
188 size.u.LowPart, size.u.LowPart);
191 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
196 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
198 FileProtocol *This = PROTOCOL_THIS(iface);
199 FIXME("(%p)->(%p)\n", This, pProtocolData);
203 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
206 FileProtocol *This = PROTOCOL_THIS(iface);
207 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
211 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
213 FileProtocol *This = PROTOCOL_THIS(iface);
215 TRACE("(%p)->(%08x)\n", This, dwOptions);
220 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
222 FileProtocol *This = PROTOCOL_THIS(iface);
223 FIXME("(%p)\n", This);
227 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
229 FileProtocol *This = PROTOCOL_THIS(iface);
230 FIXME("(%p)\n", This);
234 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
235 ULONG cb, ULONG *pcbRead)
237 FileProtocol *This = PROTOCOL_THIS(iface);
240 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
243 return INET_E_DATA_NOT_AVAILABLE;
245 ReadFile(This->file, pv, cb, &read, NULL);
250 return cb == read ? S_OK : S_FALSE;
253 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
254 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
256 FileProtocol *This = PROTOCOL_THIS(iface);
257 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
261 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
263 FileProtocol *This = PROTOCOL_THIS(iface);
265 TRACE("(%p)->(%08x)\n", This, dwOptions);
270 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
272 FileProtocol *This = PROTOCOL_THIS(iface);
274 TRACE("(%p)\n", This);
281 static const IInternetProtocolVtbl FileProtocolVtbl = {
282 FileProtocol_QueryInterface,
284 FileProtocol_Release,
286 FileProtocol_Continue,
288 FileProtocol_Terminate,
289 FileProtocol_Suspend,
293 FileProtocol_LockRequest,
294 FileProtocol_UnlockRequest
297 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
299 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
300 REFIID riid, void **ppv)
302 FileProtocol *This = PRIORITY_THIS(iface);
303 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
306 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
308 FileProtocol *This = PRIORITY_THIS(iface);
309 return IInternetProtocol_AddRef(PROTOCOL(This));
312 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
314 FileProtocol *This = PRIORITY_THIS(iface);
315 return IInternetProtocol_Release(PROTOCOL(This));
318 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
320 FileProtocol *This = PRIORITY_THIS(iface);
322 TRACE("(%p)->(%d)\n", This, nPriority);
324 This->priority = nPriority;
328 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
330 FileProtocol *This = PRIORITY_THIS(iface);
332 TRACE("(%p)->(%p)\n", This, pnPriority);
334 *pnPriority = This->priority;
340 static const IInternetPriorityVtbl FilePriorityVtbl = {
341 FilePriority_QueryInterface,
343 FilePriority_Release,
344 FilePriority_SetPriority,
345 FilePriority_GetPriority
348 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
352 TRACE("(%p %p)\n", pUnkOuter, ppobj);
356 ret = urlmon_alloc(sizeof(FileProtocol));
358 ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
359 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
364 *ppobj = PROTOCOL(ret);