urlmon: URL with two slashes is valid.
[wine/wine64.git] / dlls / urlmon / file.c
blobfcdb772b89ac00898a443eebe3d0c2b375c4e1a1
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "urlmon.h"
28 #include "urlmon_main.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
34 typedef struct {
35 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
36 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
38 HANDLE file;
39 LONG priority;
41 LONG ref;
42 } FileProtocol;
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);
53 *ppv = NULL;
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);
68 if(*ppv) {
69 IInternetProtocol_AddRef(iface);
70 return S_OK;
73 WARN("not supported interface %s\n", debugstr_guid(riid));
74 return E_NOINTERFACE;
77 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
79 FileProtocol *This = PROTOCOL_THIS(iface);
80 LONG ref = InterlockedIncrement(&This->ref);
81 TRACE("(%p) ref=%ld\n", This, ref);
82 return 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=%ld\n", This, ref);
92 if(!ref) {
93 if(This->file)
94 CloseHandle(This->file);
95 HeapFree(GetProcessHeap(), 0, This);
97 URLMON_UnlockModule();
100 return ref;
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);
108 BINDINFO bindinfo;
109 DWORD grfBINDF = 0;
110 LARGE_INTEGER size;
111 DWORD len;
112 LPWSTR url, mime = NULL;
113 LPCWSTR file_name;
114 WCHAR null_char = 0;
115 BOOL first_call = FALSE;
116 HRESULT hres;
118 static const WCHAR wszFile[] = {'f','i','l','e',':'};
120 TRACE("(%p)->(%s %p %p %08lx %ld)\n", This, debugstr_w(szUrl), pOIProtSink,
121 pOIBindInfo, grfPI, dwReserved);
123 memset(&bindinfo, 0, sizeof(bindinfo));
124 bindinfo.cbSize = sizeof(BINDINFO);
125 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
127 if(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
128 || memcmp(szUrl, wszFile, sizeof(wszFile)))
129 return MK_E_SYNTAX;
131 len = lstrlenW(szUrl)+16;
132 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
133 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
134 if(FAILED(hres)) {
135 HeapFree(GetProcessHeap(), 0, url);
136 return hres;
139 if(!(grfBINDF & BINDF_FROMURLMON))
140 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
142 if(!This->file) {
143 first_call = TRUE;
145 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
147 file_name = url+sizeof(wszFile)/sizeof(WCHAR);
148 if(file_name[0] == '/' && file_name[1] == '/')
149 file_name += 2;
150 if(*file_name == '/')
151 file_name++;
153 This->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
154 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
156 if(This->file == INVALID_HANDLE_VALUE) {
157 This->file = NULL;
158 IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
159 GetLastError(), NULL);
160 HeapFree(GetProcessHeap(), 0, url);
161 return INET_E_RESOURCE_NOT_FOUND;
164 IInternetProtocolSink_ReportProgress(pOIProtSink,
165 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_name);
167 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
168 if(SUCCEEDED(hres)) {
169 IInternetProtocolSink_ReportProgress(pOIProtSink,
170 (grfBINDF & BINDF_FROMURLMON) ?
171 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
172 mime);
173 CoTaskMemFree(mime);
177 HeapFree(GetProcessHeap(), 0, url);
179 if(GetFileSizeEx(This->file, &size))
180 IInternetProtocolSink_ReportData(pOIProtSink,
181 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
182 size.u.LowPart, size.u.LowPart);
184 if(first_call)
185 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
187 return S_OK;
190 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
192 FileProtocol *This = PROTOCOL_THIS(iface);
193 FIXME("(%p)->(%p)\n", This, pProtocolData);
194 return E_NOTIMPL;
197 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
198 DWORD dwOptions)
200 FileProtocol *This = PROTOCOL_THIS(iface);
201 FIXME("(%p)->(%08lx %08lx)\n", This, hrReason, dwOptions);
202 return E_NOTIMPL;
205 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
207 FileProtocol *This = PROTOCOL_THIS(iface);
209 TRACE("(%p)->(%08lx)\n", This, dwOptions);
211 return S_OK;
214 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
216 FileProtocol *This = PROTOCOL_THIS(iface);
217 FIXME("(%p)\n", This);
218 return E_NOTIMPL;
221 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
223 FileProtocol *This = PROTOCOL_THIS(iface);
224 FIXME("(%p)\n", This);
225 return E_NOTIMPL;
228 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
229 ULONG cb, ULONG *pcbRead)
231 FileProtocol *This = PROTOCOL_THIS(iface);
232 DWORD read = 0;
234 TRACE("(%p)->(%p %lu %p)\n", This, pv, cb, pcbRead);
236 if(!This->file)
237 return INET_E_DATA_NOT_AVAILABLE;
239 ReadFile(This->file, pv, cb, &read, NULL);
241 if(pcbRead)
242 *pcbRead = read;
244 return cb == read ? S_OK : S_FALSE;
247 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
248 DWORD dwOrgin, ULARGE_INTEGER *plibNewPosition)
250 FileProtocol *This = PROTOCOL_THIS(iface);
251 FIXME("(%p)->(%ld %ld %p)\n", This, dlibMove.u.LowPart, dwOrgin, plibNewPosition);
252 return E_NOTIMPL;
255 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
257 FileProtocol *This = PROTOCOL_THIS(iface);
259 TRACE("(%p)->(%08lx)\n", This, dwOptions);
261 return S_OK;
264 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
266 FileProtocol *This = PROTOCOL_THIS(iface);
268 TRACE("(%p)\n", This);
270 return S_OK;
273 #undef PROTOCOL_THIS
275 static const IInternetProtocolVtbl FileProtocolVtbl = {
276 FileProtocol_QueryInterface,
277 FileProtocol_AddRef,
278 FileProtocol_Release,
279 FileProtocol_Start,
280 FileProtocol_Continue,
281 FileProtocol_Abort,
282 FileProtocol_Terminate,
283 FileProtocol_Suspend,
284 FileProtocol_Resume,
285 FileProtocol_Read,
286 FileProtocol_Seek,
287 FileProtocol_LockRequest,
288 FileProtocol_UnlockRequest
291 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
293 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
294 REFIID riid, void **ppv)
296 FileProtocol *This = PRIORITY_THIS(iface);
297 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
300 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
302 FileProtocol *This = PRIORITY_THIS(iface);
303 return IInternetProtocol_AddRef(PROTOCOL(This));
306 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
308 FileProtocol *This = PRIORITY_THIS(iface);
309 return IInternetProtocol_Release(PROTOCOL(This));
312 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
314 FileProtocol *This = PRIORITY_THIS(iface);
316 TRACE("(%p)->(%ld)\n", This, nPriority);
318 This->priority = nPriority;
319 return S_OK;
322 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
324 FileProtocol *This = PRIORITY_THIS(iface);
326 TRACE("(%p)->(%p)\n", This, pnPriority);
328 *pnPriority = This->priority;
329 return S_OK;
332 #undef PRIORITY_THIS
334 static const IInternetPriorityVtbl FilePriorityVtbl = {
335 FilePriority_QueryInterface,
336 FilePriority_AddRef,
337 FilePriority_Release,
338 FilePriority_SetPriority,
339 FilePriority_GetPriority
342 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
344 FileProtocol *ret;
346 TRACE("(%p %p)\n", pUnkOuter, ppobj);
348 URLMON_LockModule();
350 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(FileProtocol));
352 ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
353 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
354 ret->file = NULL;
355 ret->priority = 0;
356 ret->ref = 1;
358 *ppobj = PROTOCOL(ret);
360 return S_OK;