Sync with trunk (r48008)
[reactos.git] / dll / win32 / urlmon / file.c
1 /*
2 * Copyright 2005 Jacek Caban
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "urlmon_main.h"
20 #include "wine/debug.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
23
24 typedef struct {
25 const IInternetProtocolVtbl *lpIInternetProtocolVtbl;
26 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
27
28 HANDLE file;
29 LONG priority;
30
31 LONG ref;
32 } FileProtocol;
33
34 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
35
36 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, IInternetProtocol, iface)
37
38 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
39 {
40 FileProtocol *This = PROTOCOL_THIS(iface);
41
42 *ppv = NULL;
43 if(IsEqualGUID(&IID_IUnknown, riid)) {
44 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
45 *ppv = PROTOCOL(This);
46 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
47 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
48 *ppv = PROTOCOL(This);
49 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
50 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
51 *ppv = PROTOCOL(This);
52 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
53 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
54 *ppv = PRIORITY(This);
55 }
56
57 if(*ppv) {
58 IInternetProtocol_AddRef(iface);
59 return S_OK;
60 }
61
62 WARN("not supported interface %s\n", debugstr_guid(riid));
63 return E_NOINTERFACE;
64 }
65
66 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
67 {
68 FileProtocol *This = PROTOCOL_THIS(iface);
69 LONG ref = InterlockedIncrement(&This->ref);
70 TRACE("(%p) ref=%d\n", This, ref);
71 return ref;
72 }
73
74 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
75 {
76 FileProtocol *This = PROTOCOL_THIS(iface);
77 LONG ref = InterlockedDecrement(&This->ref);
78
79 TRACE("(%p) ref=%d\n", This, ref);
80
81 if(!ref) {
82 if(This->file)
83 CloseHandle(This->file);
84 heap_free(This);
85
86 URLMON_UnlockModule();
87 }
88
89 return ref;
90 }
91
92 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
93 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
94 DWORD grfPI, HANDLE_PTR dwReserved)
95 {
96 FileProtocol *This = PROTOCOL_THIS(iface);
97 BINDINFO bindinfo;
98 DWORD grfBINDF = 0;
99 LARGE_INTEGER size;
100 DWORD len;
101 LPWSTR url, mime = NULL, file_name;
102 WCHAR null_char = 0;
103 BOOL first_call = FALSE;
104 HRESULT hres;
105
106 static const WCHAR wszFile[] = {'f','i','l','e',':'};
107
108 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
109 pOIBindInfo, grfPI, dwReserved);
110
111 if(!szUrl || strlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
112 || memcmp(szUrl, wszFile, sizeof(wszFile)))
113 return E_INVALIDARG;
114
115 memset(&bindinfo, 0, sizeof(bindinfo));
116 bindinfo.cbSize = sizeof(BINDINFO);
117 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
118 if(FAILED(hres)) {
119 WARN("GetBindInfo failed: %08x\n", hres);
120 return hres;
121 }
122
123 ReleaseBindInfo(&bindinfo);
124
125 len = lstrlenW(szUrl)+16;
126 url = heap_alloc(len*sizeof(WCHAR));
127 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
128 if(FAILED(hres)) {
129 heap_free(url);
130 return hres;
131 }
132
133 if(!(grfBINDF & BINDF_FROMURLMON))
134 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
135
136 if(!This->file) {
137 WCHAR *ptr;
138
139 first_call = TRUE;
140
141 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
142
143 file_name = url+sizeof(wszFile)/sizeof(WCHAR);
144
145 /* Strip both forward and back slashes */
146 if( (file_name[0] == '/' && file_name[1] == '/') ||
147 (file_name[0] == '\\' && file_name[1] == '\\'))
148 file_name += 2;
149 if(*file_name == '/')
150 file_name++;
151
152 for(ptr = file_name; *ptr; ptr++) {
153 if(*ptr == '?' || *ptr == '#') {
154 *ptr = 0;
155 break;
156 }
157 }
158
159 if(file_name[1] == '|')
160 file_name[1] = ':';
161
162 This->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
163 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
164
165 if(This->file == INVALID_HANDLE_VALUE) {
166 This->file = NULL;
167 IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
168 GetLastError(), NULL);
169 heap_free(url);
170 return INET_E_RESOURCE_NOT_FOUND;
171 }
172
173 IInternetProtocolSink_ReportProgress(pOIProtSink,
174 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_name);
175
176 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
177 if(SUCCEEDED(hres)) {
178 IInternetProtocolSink_ReportProgress(pOIProtSink,
179 (grfBINDF & BINDF_FROMURLMON) ?
180 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
181 mime);
182 CoTaskMemFree(mime);
183 }
184 }
185
186 heap_free(url);
187
188 if(GetFileSizeEx(This->file, &size))
189 IInternetProtocolSink_ReportData(pOIProtSink,
190 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
191 size.u.LowPart, size.u.LowPart);
192
193 if(first_call)
194 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
195
196 return S_OK;
197 }
198
199 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
200 {
201 FileProtocol *This = PROTOCOL_THIS(iface);
202 FIXME("(%p)->(%p)\n", This, pProtocolData);
203 return E_NOTIMPL;
204 }
205
206 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
207 DWORD dwOptions)
208 {
209 FileProtocol *This = PROTOCOL_THIS(iface);
210 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
211 return E_NOTIMPL;
212 }
213
214 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
215 {
216 FileProtocol *This = PROTOCOL_THIS(iface);
217
218 TRACE("(%p)->(%08x)\n", This, dwOptions);
219
220 return S_OK;
221 }
222
223 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
224 {
225 FileProtocol *This = PROTOCOL_THIS(iface);
226 FIXME("(%p)\n", This);
227 return E_NOTIMPL;
228 }
229
230 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
231 {
232 FileProtocol *This = PROTOCOL_THIS(iface);
233 FIXME("(%p)\n", This);
234 return E_NOTIMPL;
235 }
236
237 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
238 ULONG cb, ULONG *pcbRead)
239 {
240 FileProtocol *This = PROTOCOL_THIS(iface);
241 DWORD read = 0;
242
243 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
244
245 if (pcbRead)
246 *pcbRead = 0;
247
248 if(!This->file)
249 return INET_E_DATA_NOT_AVAILABLE;
250
251 if (!ReadFile(This->file, pv, cb, &read, NULL))
252 return INET_E_DOWNLOAD_FAILURE;
253
254 if(pcbRead)
255 *pcbRead = read;
256
257 return cb == read ? S_OK : S_FALSE;
258 }
259
260 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
261 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
262 {
263 FileProtocol *This = PROTOCOL_THIS(iface);
264 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
265 return E_NOTIMPL;
266 }
267
268 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
269 {
270 FileProtocol *This = PROTOCOL_THIS(iface);
271
272 TRACE("(%p)->(%08x)\n", This, dwOptions);
273
274 return S_OK;
275 }
276
277 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
278 {
279 FileProtocol *This = PROTOCOL_THIS(iface);
280
281 TRACE("(%p)\n", This);
282
283 return S_OK;
284 }
285
286 #undef PROTOCOL_THIS
287
288 static const IInternetProtocolVtbl FileProtocolVtbl = {
289 FileProtocol_QueryInterface,
290 FileProtocol_AddRef,
291 FileProtocol_Release,
292 FileProtocol_Start,
293 FileProtocol_Continue,
294 FileProtocol_Abort,
295 FileProtocol_Terminate,
296 FileProtocol_Suspend,
297 FileProtocol_Resume,
298 FileProtocol_Read,
299 FileProtocol_Seek,
300 FileProtocol_LockRequest,
301 FileProtocol_UnlockRequest
302 };
303
304 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
305
306 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
307 REFIID riid, void **ppv)
308 {
309 FileProtocol *This = PRIORITY_THIS(iface);
310 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
311 }
312
313 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
314 {
315 FileProtocol *This = PRIORITY_THIS(iface);
316 return IInternetProtocol_AddRef(PROTOCOL(This));
317 }
318
319 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
320 {
321 FileProtocol *This = PRIORITY_THIS(iface);
322 return IInternetProtocol_Release(PROTOCOL(This));
323 }
324
325 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
326 {
327 FileProtocol *This = PRIORITY_THIS(iface);
328
329 TRACE("(%p)->(%d)\n", This, nPriority);
330
331 This->priority = nPriority;
332 return S_OK;
333 }
334
335 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
336 {
337 FileProtocol *This = PRIORITY_THIS(iface);
338
339 TRACE("(%p)->(%p)\n", This, pnPriority);
340
341 *pnPriority = This->priority;
342 return S_OK;
343 }
344
345 #undef PRIORITY_THIS
346
347 static const IInternetPriorityVtbl FilePriorityVtbl = {
348 FilePriority_QueryInterface,
349 FilePriority_AddRef,
350 FilePriority_Release,
351 FilePriority_SetPriority,
352 FilePriority_GetPriority
353 };
354
355 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
356 {
357 FileProtocol *ret;
358
359 TRACE("(%p %p)\n", pUnkOuter, ppobj);
360
361 URLMON_LockModule();
362
363 ret = heap_alloc(sizeof(FileProtocol));
364
365 ret->lpIInternetProtocolVtbl = &FileProtocolVtbl;
366 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
367 ret->file = NULL;
368 ret->priority = 0;
369 ret->ref = 1;
370
371 *ppobj = PROTOCOL(ret);
372
373 return S_OK;
374 }