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