advapi32: Support registry objects in GetNamedSecurityInfo.
[wine] / dlls / mshtml / nsio.c
1 /*
2  * Copyright 2006-2010 Jacek Caban for CodeWeavers
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 "config.h"
20
21 #include <stdarg.h>
22 #include <assert.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "ole2.h"
31 #include "shlguid.h"
32 #include "wininet.h"
33 #include "shlwapi.h"
34
35 #include "wine/debug.h"
36
37 #include "mshtml_private.h"
38 #include "binding.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41
42 #define NS_IOSERVICE_CLASSNAME "nsIOService"
43 #define NS_IOSERVICE_CONTRACTID "@mozilla.org/network/io-service;1"
44
45 static const IID NS_IOSERVICE_CID =
46     {0x9ac9e770, 0x18bc, 0x11d3, {0x93, 0x37, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40}};
47 static const IID IID_nsWineURI =
48     {0x5088272e, 0x900b, 0x11da, {0xc6,0x87, 0x00,0x0f,0xea,0x57,0xf2,0x1a}};
49
50 static nsIIOService *nsio = NULL;
51 static nsINetUtil *net_util;
52
53 static const char *request_method_strings[] = {"GET", "PUT", "POST"};
54
55 struct  nsWineURI {
56     nsIFileURL nsIFileURL_iface; /* For non-file URL objects, it's just nsIURL */
57     nsIStandardURL nsIStandardURL_iface;
58
59     LONG ref;
60
61     NSContainer *container;
62     windowref_t *window_ref;
63     nsChannelBSC *channel_bsc;
64     IUri *uri;
65     IUriBuilder *uri_builder;
66     char *origin_charset;
67     BOOL is_doc_uri;
68     BOOL is_mutable;
69     DWORD scheme;
70 };
71
72 static BOOL ensure_uri(nsWineURI *This)
73 {
74     HRESULT hres;
75
76     assert(This->uri || This->uri_builder);
77
78     if(!This->uri) {
79         hres = IUriBuilder_CreateUriSimple(This->uri_builder, 0, 0, &This->uri);
80         if(FAILED(hres)) {
81             WARN("CreateUriSimple failed: %08x\n", hres);
82             return FALSE;
83         }
84     }
85
86     return TRUE;
87 }
88
89 IUri *nsuri_get_uri(nsWineURI *nsuri)
90 {
91     if(!ensure_uri(nsuri))
92         return NULL;
93
94     IUri_AddRef(nsuri->uri);
95     return nsuri->uri;
96 }
97
98 IUri *get_uri_nofrag(IUri *uri)
99 {
100     IUriBuilder *uri_builder;
101     IUri *ret;
102     BOOL b;
103     HRESULT hres;
104
105     hres = IUri_HasProperty(uri, Uri_PROPERTY_FRAGMENT, &b);
106     if(SUCCEEDED(hres) && !b) {
107         IUri_AddRef(uri);
108         return uri;
109     }
110
111     hres = CreateIUriBuilder(uri, 0, 0, &uri_builder);
112     if(FAILED(hres))
113         return NULL;
114
115     hres = IUriBuilder_RemoveProperties(uri_builder, Uri_HAS_FRAGMENT);
116     if(SUCCEEDED(hres))
117         hres = IUriBuilder_CreateUriSimple(uri_builder, 0, 0, &ret);
118     IUriBuilder_Release(uri_builder);
119     if(FAILED(hres))
120         return NULL;
121
122     return ret;
123 }
124
125 static BOOL compare_ignoring_frag(IUri *uri1, IUri *uri2)
126 {
127     IUri *uri_nofrag1, *uri_nofrag2;
128     BOOL ret = FALSE;
129
130     uri_nofrag1 = get_uri_nofrag(uri1);
131     if(!uri_nofrag1)
132         return FALSE;
133
134     uri_nofrag2 = get_uri_nofrag(uri2);
135     if(uri_nofrag2) {
136         IUri_IsEqual(uri_nofrag1, uri_nofrag2, &ret);
137         IUri_Release(uri_nofrag2);
138     }
139
140     IUri_Release(uri_nofrag1);
141     return ret;
142 }
143
144 static nsresult create_nsuri(IUri*,HTMLOuterWindow*,NSContainer*,const char*,nsWineURI**);
145
146 static const char *debugstr_nsacstr(const nsACString *nsstr)
147 {
148     const char *data;
149
150     nsACString_GetData(nsstr, &data);
151     return debugstr_a(data);
152 }
153
154 static nsresult return_wstr_nsacstr(nsACString *ret_str, const WCHAR *str, int len)
155 {
156     char *stra;
157     int lena;
158
159     TRACE("returning %s\n", debugstr_wn(str, len));
160
161     if(!*str) {
162         nsACString_SetData(ret_str, "");
163         return NS_OK;
164     }
165
166     lena = WideCharToMultiByte(CP_UTF8, 0, str, len, NULL, 0, NULL, NULL);
167     stra = heap_alloc(lena+1);
168     if(!stra)
169         return NS_ERROR_OUT_OF_MEMORY;
170
171     WideCharToMultiByte(CP_UTF8, 0, str, len, stra, lena, NULL, NULL);
172     stra[lena] = 0;
173
174     nsACString_SetData(ret_str, stra);
175     heap_free(stra);
176     return NS_OK;
177 }
178
179 HRESULT nsuri_to_url(LPCWSTR nsuri, BOOL ret_empty, BSTR *ret)
180 {
181     const WCHAR *ptr = nsuri;
182
183     static const WCHAR wine_prefixW[] = {'w','i','n','e',':'};
184
185     if(!strncmpW(nsuri, wine_prefixW, sizeof(wine_prefixW)/sizeof(WCHAR)))
186         ptr += sizeof(wine_prefixW)/sizeof(WCHAR);
187
188     if(*ptr || ret_empty) {
189         *ret = SysAllocString(ptr);
190         if(!*ret)
191             return E_OUTOFMEMORY;
192     }else {
193         *ret = NULL;
194     }
195
196     TRACE("%s -> %s\n", debugstr_w(nsuri), debugstr_w(*ret));
197     return S_OK;
198 }
199
200 static BOOL exec_shldocvw_67(HTMLDocumentObj *doc, BSTR url)
201 {
202     IOleCommandTarget *cmdtrg = NULL;
203     HRESULT hres;
204
205     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
206     if(SUCCEEDED(hres)) {
207         VARIANT varUrl, varRes;
208
209         V_VT(&varUrl) = VT_BSTR;
210         V_BSTR(&varUrl) = url;
211         V_VT(&varRes) = VT_BOOL;
212
213         hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &varUrl, &varRes);
214
215         IOleCommandTarget_Release(cmdtrg);
216
217         if(SUCCEEDED(hres) && !V_BOOL(&varRes)) {
218             TRACE("got VARIANT_FALSE, do not load\n");
219             return FALSE;
220         }
221     }
222
223     return TRUE;
224 }
225
226 static nsresult before_async_open(nsChannel *channel, NSContainer *container, BOOL *cancel)
227 {
228     HTMLDocumentObj *doc = container->doc;
229     BSTR display_uri;
230     HRESULT hres;
231
232     if(!doc->client) {
233         *cancel = TRUE;
234         return NS_OK;
235     }
236
237     hres = IUri_GetDisplayUri(channel->uri->uri, &display_uri);
238     if(FAILED(hres))
239         return NS_ERROR_FAILURE;
240
241     if(!exec_shldocvw_67(doc, display_uri)) {
242         SysFreeString(display_uri);
243         *cancel = FALSE;
244         return NS_OK;
245     }
246
247     hres = hlink_frame_navigate(&doc->basedoc, display_uri, channel, 0, cancel);
248     SysFreeString(display_uri);
249     if(FAILED(hres))
250         *cancel = TRUE;
251     return NS_OK;
252 }
253
254 HRESULT load_nsuri(HTMLOuterWindow *window, nsWineURI *uri, nsChannelBSC *channelbsc, DWORD flags)
255 {
256     nsIWebNavigation *web_navigation;
257     nsIDocShell *doc_shell;
258     HTMLDocumentNode *doc;
259     nsresult nsres;
260
261     nsres = get_nsinterface((nsISupports*)window->nswindow, &IID_nsIWebNavigation, (void**)&web_navigation);
262     if(NS_FAILED(nsres)) {
263         ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
264         return E_FAIL;
265     }
266
267     nsres = nsIWebNavigation_QueryInterface(web_navigation, &IID_nsIDocShell, (void**)&doc_shell);
268     nsIWebNavigation_Release(web_navigation);
269     if(NS_FAILED(nsres)) {
270         ERR("Could not get nsIDocShell: %08x\n", nsres);
271         return E_FAIL;
272     }
273
274     uri->channel_bsc = channelbsc;
275     doc = window->base.inner_window->doc;
276     doc->skip_mutation_notif = TRUE;
277     nsres = nsIDocShell_LoadURI(doc_shell, (nsIURI*)&uri->nsIFileURL_iface, NULL, flags, FALSE);
278     if(doc == window->base.inner_window->doc)
279         doc->skip_mutation_notif = FALSE;
280     uri->channel_bsc = NULL;
281     nsIDocShell_Release(doc_shell);
282     if(NS_FAILED(nsres)) {
283         WARN("LoadURI failed: %08x\n", nsres);
284         return E_FAIL;
285     }
286
287     return S_OK;
288 }
289
290 static void set_uri_nscontainer(nsWineURI *This, NSContainer *nscontainer)
291 {
292     if(This->container) {
293         if(This->container == nscontainer)
294             return;
295         TRACE("Changing %p -> %p\n", This->container, nscontainer);
296         nsIWebBrowserChrome_Release(&This->container->nsIWebBrowserChrome_iface);
297     }
298
299     if(nscontainer)
300         nsIWebBrowserChrome_AddRef(&nscontainer->nsIWebBrowserChrome_iface);
301     This->container = nscontainer;
302 }
303
304 static void set_uri_window(nsWineURI *This, HTMLOuterWindow *window)
305 {
306     if(This->window_ref) {
307         if(This->window_ref->window == window)
308             return;
309         TRACE("Changing %p -> %p\n", This->window_ref->window, window);
310         windowref_release(This->window_ref);
311     }
312
313     if(window) {
314         windowref_addref(window->window_ref);
315         This->window_ref = window->window_ref;
316
317         if(window->doc_obj)
318             set_uri_nscontainer(This, window->doc_obj->nscontainer);
319     }else {
320         This->window_ref = NULL;
321     }
322 }
323
324 static inline BOOL is_http_channel(nsChannel *This)
325 {
326     return This->uri->scheme == URL_SCHEME_HTTP || This->uri->scheme == URL_SCHEME_HTTPS;
327 }
328
329 static http_header_t *find_http_header(struct list *headers, const WCHAR *name, int len)
330 {
331     http_header_t *iter;
332
333     LIST_FOR_EACH_ENTRY(iter, headers, http_header_t, entry) {
334         if(!strcmpiW(iter->header, name))
335             return iter;
336     }
337
338     return NULL;
339 }
340
341 static nsresult get_channel_http_header(struct list *headers, const nsACString *header_name_str,
342         nsACString *_retval)
343 {
344     const char *header_namea;
345     http_header_t *header;
346     WCHAR *header_name;
347     char *data;
348
349     nsACString_GetData(header_name_str, &header_namea);
350     header_name = heap_strdupAtoW(header_namea);
351     if(!header_name)
352         return NS_ERROR_UNEXPECTED;
353
354     header = find_http_header(headers, header_name, strlenW(header_name));
355     heap_free(header_name);
356     if(!header)
357         return NS_ERROR_NOT_AVAILABLE;
358
359     data = heap_strdupWtoA(header->data);
360     if(!data)
361         return NS_ERROR_UNEXPECTED;
362
363     TRACE("%s -> %s\n", debugstr_a(header_namea), debugstr_a(data));
364     nsACString_SetData(_retval, data);
365     heap_free(data);
366     return NS_OK;
367 }
368
369 HRESULT set_http_header(struct list *headers, const WCHAR *name, int name_len,
370         const WCHAR *value, int value_len)
371 {
372     http_header_t *header;
373
374     TRACE("%s: %s\n", debugstr_wn(name, name_len), debugstr_wn(value, value_len));
375
376     header = find_http_header(headers, name, name_len);
377     if(header) {
378         WCHAR *new_data;
379
380         new_data = heap_strndupW(value, value_len);
381         if(!new_data)
382             return E_OUTOFMEMORY;
383
384         heap_free(header->data);
385         header->data = new_data;
386     }else {
387         header = heap_alloc(sizeof(http_header_t));
388         if(!header)
389             return E_OUTOFMEMORY;
390
391         header->header = heap_strndupW(name, name_len);
392         header->data = heap_strndupW(value, value_len);
393         if(!header->header || !header->data) {
394             heap_free(header->header);
395             heap_free(header->data);
396             heap_free(header);
397             return E_OUTOFMEMORY;
398         }
399
400         list_add_tail(headers, &header->entry);
401     }
402
403     return S_OK;
404 }
405
406 static nsresult set_channel_http_header(struct list *headers, const nsACString *name_str,
407         const nsACString *value_str)
408 {
409     const char *namea, *valuea;
410     WCHAR *name, *value;
411     HRESULT hres;
412
413     nsACString_GetData(name_str, &namea);
414     name = heap_strdupAtoW(namea);
415     if(!name)
416         return NS_ERROR_UNEXPECTED;
417
418     nsACString_GetData(value_str, &valuea);
419     value = heap_strdupAtoW(valuea);
420     if(!value) {
421         heap_free(name);
422         return NS_ERROR_UNEXPECTED;
423     }
424
425     hres = set_http_header(headers, name, strlenW(name), value, strlenW(value));
426
427     heap_free(name);
428     heap_free(value);
429     return SUCCEEDED(hres) ? NS_OK : NS_ERROR_UNEXPECTED;
430 }
431
432 static nsresult visit_http_headers(struct list *headers, nsIHttpHeaderVisitor *visitor)
433 {
434     nsACString header_str, value_str;
435     char *header, *value;
436     http_header_t *iter;
437     nsresult nsres;
438
439     LIST_FOR_EACH_ENTRY(iter, headers, http_header_t, entry) {
440         header = heap_strdupWtoA(iter->header);
441         if(!header)
442             return NS_ERROR_OUT_OF_MEMORY;
443
444         value = heap_strdupWtoA(iter->data);
445         if(!value) {
446             heap_free(header);
447             return NS_ERROR_OUT_OF_MEMORY;
448         }
449
450         nsACString_InitDepend(&header_str, header);
451         nsACString_InitDepend(&value_str, value);
452         nsres = nsIHttpHeaderVisitor_VisitHeader(visitor, &header_str, &value_str);
453         nsACString_Finish(&header_str);
454         nsACString_Finish(&value_str);
455         heap_free(header);
456         heap_free(value);
457         if(NS_FAILED(nsres))
458             break;
459     }
460
461     return NS_OK;
462 }
463
464 static void free_http_headers(struct list *list)
465 {
466     http_header_t *iter, *iter_next;
467
468     LIST_FOR_EACH_ENTRY_SAFE(iter, iter_next, list, http_header_t, entry) {
469         list_remove(&iter->entry);
470         heap_free(iter->header);
471         heap_free(iter->data);
472         heap_free(iter);
473     }
474 }
475
476 static inline nsChannel *impl_from_nsIHttpChannel(nsIHttpChannel *iface)
477 {
478     return CONTAINING_RECORD(iface, nsChannel, nsIHttpChannel_iface);
479 }
480
481 static nsresult NSAPI nsChannel_QueryInterface(nsIHttpChannel *iface, nsIIDRef riid, void **result)
482 {
483     nsChannel *This = impl_from_nsIHttpChannel(iface);
484
485     if(IsEqualGUID(&IID_nsISupports, riid)) {
486         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
487         *result = &This->nsIHttpChannel_iface;
488     }else if(IsEqualGUID(&IID_nsIRequest, riid)) {
489         TRACE("(%p)->(IID_nsIRequest %p)\n", This, result);
490         *result = &This->nsIHttpChannel_iface;
491     }else if(IsEqualGUID(&IID_nsIChannel, riid)) {
492         TRACE("(%p)->(IID_nsIChannel %p)\n", This, result);
493         *result = &This->nsIHttpChannel_iface;
494     }else if(IsEqualGUID(&IID_nsIHttpChannel, riid)) {
495         TRACE("(%p)->(IID_nsIHttpChannel %p)\n", This, result);
496         *result = is_http_channel(This) ? &This->nsIHttpChannel_iface : NULL;
497     }else if(IsEqualGUID(&IID_nsIUploadChannel, riid)) {
498         TRACE("(%p)->(IID_nsIUploadChannel %p)\n", This, result);
499         *result = &This->nsIUploadChannel_iface;
500     }else if(IsEqualGUID(&IID_nsIHttpChannelInternal, riid)) {
501         TRACE("(%p)->(IID_nsIHttpChannelInternal %p)\n", This, result);
502         *result = is_http_channel(This) ? &This->nsIHttpChannelInternal_iface : NULL;
503     }else {
504         TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
505         *result = NULL;
506     }
507
508     if(*result) {
509         nsIHttpChannel_AddRef(&This->nsIHttpChannel_iface);
510         return NS_OK;
511     }
512
513     return NS_NOINTERFACE;
514 }
515
516 static nsrefcnt NSAPI nsChannel_AddRef(nsIHttpChannel *iface)
517 {
518     nsChannel *This = impl_from_nsIHttpChannel(iface);
519     nsrefcnt ref = InterlockedIncrement(&This->ref);
520
521     TRACE("(%p) ref=%d\n", This, ref);
522
523     return ref;
524 }
525
526 static nsrefcnt NSAPI nsChannel_Release(nsIHttpChannel *iface)
527 {
528     nsChannel *This = impl_from_nsIHttpChannel(iface);
529     LONG ref = InterlockedDecrement(&This->ref);
530
531     if(!ref) {
532         nsIFileURL_Release(&This->uri->nsIFileURL_iface);
533         if(This->owner)
534             nsISupports_Release(This->owner);
535         if(This->post_data_stream)
536             nsIInputStream_Release(This->post_data_stream);
537         if(This->load_group)
538             nsILoadGroup_Release(This->load_group);
539         if(This->notif_callback)
540             nsIInterfaceRequestor_Release(This->notif_callback);
541         if(This->original_uri)
542             nsIURI_Release(This->original_uri);
543         if(This->referrer)
544             nsIURI_Release(This->referrer);
545
546         free_http_headers(&This->response_headers);
547         free_http_headers(&This->request_headers);
548
549         heap_free(This->content_type);
550         heap_free(This->charset);
551         heap_free(This);
552     }
553
554     return ref;
555 }
556
557 static nsresult NSAPI nsChannel_GetName(nsIHttpChannel *iface, nsACString *aName)
558 {
559     nsChannel *This = impl_from_nsIHttpChannel(iface);
560
561     TRACE("(%p)->(%p)\n", This, aName);
562
563     return nsIFileURL_GetSpec(&This->uri->nsIFileURL_iface, aName);
564 }
565
566 static nsresult NSAPI nsChannel_IsPending(nsIHttpChannel *iface, cpp_bool *_retval)
567 {
568     nsChannel *This = impl_from_nsIHttpChannel(iface);
569
570     FIXME("(%p)->(%p)\n", This, _retval);
571
572     return NS_ERROR_NOT_IMPLEMENTED;
573 }
574
575 static nsresult NSAPI nsChannel_GetStatus(nsIHttpChannel *iface, nsresult *aStatus)
576 {
577     nsChannel *This = impl_from_nsIHttpChannel(iface);
578
579     WARN("(%p)->(%p) returning NS_OK\n", This, aStatus);
580
581     return *aStatus = NS_OK;
582 }
583
584 static nsresult NSAPI nsChannel_Cancel(nsIHttpChannel *iface, nsresult aStatus)
585 {
586     nsChannel *This = impl_from_nsIHttpChannel(iface);
587
588     FIXME("(%p)->(%08x)\n", This, aStatus);
589
590     return NS_ERROR_NOT_IMPLEMENTED;
591 }
592
593 static nsresult NSAPI nsChannel_Suspend(nsIHttpChannel *iface)
594 {
595     nsChannel *This = impl_from_nsIHttpChannel(iface);
596
597     FIXME("(%p)\n", This);
598
599     return NS_ERROR_NOT_IMPLEMENTED;
600 }
601
602 static nsresult NSAPI nsChannel_Resume(nsIHttpChannel *iface)
603 {
604     nsChannel *This = impl_from_nsIHttpChannel(iface);
605
606     FIXME("(%p)\n", This);
607
608     return NS_ERROR_NOT_IMPLEMENTED;
609 }
610
611 static nsresult NSAPI nsChannel_GetLoadGroup(nsIHttpChannel *iface, nsILoadGroup **aLoadGroup)
612 {
613     nsChannel *This = impl_from_nsIHttpChannel(iface);
614
615     TRACE("(%p)->(%p)\n", This, aLoadGroup);
616
617     if(This->load_group)
618         nsILoadGroup_AddRef(This->load_group);
619
620     *aLoadGroup = This->load_group;
621     return NS_OK;
622 }
623
624 static nsresult NSAPI nsChannel_SetLoadGroup(nsIHttpChannel *iface, nsILoadGroup *aLoadGroup)
625 {
626     nsChannel *This = impl_from_nsIHttpChannel(iface);
627
628     TRACE("(%p)->(%p)\n", This, aLoadGroup);
629
630     if(This->load_group)
631         nsILoadGroup_Release(This->load_group);
632     if(aLoadGroup)
633         nsILoadGroup_AddRef(aLoadGroup);
634     This->load_group = aLoadGroup;
635
636     return NS_OK;
637 }
638
639 static nsresult NSAPI nsChannel_GetLoadFlags(nsIHttpChannel *iface, nsLoadFlags *aLoadFlags)
640 {
641     nsChannel *This = impl_from_nsIHttpChannel(iface);
642
643     TRACE("(%p)->(%p)\n", This, aLoadFlags);
644
645     *aLoadFlags = This->load_flags;
646     return NS_OK;
647 }
648
649 static nsresult NSAPI nsChannel_SetLoadFlags(nsIHttpChannel *iface, nsLoadFlags aLoadFlags)
650 {
651     nsChannel *This = impl_from_nsIHttpChannel(iface);
652
653     TRACE("(%p)->(%08x)\n", This, aLoadFlags);
654
655     This->load_flags = aLoadFlags;
656     return NS_OK;
657 }
658
659 static nsresult NSAPI nsChannel_GetOriginalURI(nsIHttpChannel *iface, nsIURI **aOriginalURI)
660 {
661     nsChannel *This = impl_from_nsIHttpChannel(iface);
662
663     TRACE("(%p)->(%p)\n", This, aOriginalURI);
664
665     if(This->original_uri)
666         nsIURI_AddRef(This->original_uri);
667
668     *aOriginalURI = This->original_uri;
669     return NS_OK;
670 }
671
672 static nsresult NSAPI nsChannel_SetOriginalURI(nsIHttpChannel *iface, nsIURI *aOriginalURI)
673 {
674     nsChannel *This = impl_from_nsIHttpChannel(iface);
675
676     TRACE("(%p)->(%p)\n", This, aOriginalURI);
677
678     if(This->original_uri)
679         nsIURI_Release(This->original_uri);
680
681     nsIURI_AddRef(aOriginalURI);
682     This->original_uri = aOriginalURI;
683     return NS_OK;
684 }
685
686 static nsresult NSAPI nsChannel_GetURI(nsIHttpChannel *iface, nsIURI **aURI)
687 {
688     nsChannel *This = impl_from_nsIHttpChannel(iface);
689
690     TRACE("(%p)->(%p)\n", This, aURI);
691
692     nsIFileURL_AddRef(&This->uri->nsIFileURL_iface);
693     *aURI = (nsIURI*)This->uri;
694
695     return NS_OK;
696 }
697
698 static nsresult NSAPI nsChannel_GetOwner(nsIHttpChannel *iface, nsISupports **aOwner)
699 {
700     nsChannel *This = impl_from_nsIHttpChannel(iface);
701
702     TRACE("(%p)->(%p)\n", This, aOwner);
703
704     if(This->owner)
705         nsISupports_AddRef(This->owner);
706     *aOwner = This->owner;
707
708     return NS_OK;
709 }
710
711 static nsresult NSAPI nsChannel_SetOwner(nsIHttpChannel *iface, nsISupports *aOwner)
712 {
713     nsChannel *This = impl_from_nsIHttpChannel(iface);
714
715     TRACE("(%p)->(%p)\n", This, aOwner);
716
717     if(aOwner)
718         nsISupports_AddRef(aOwner);
719     if(This->owner)
720         nsISupports_Release(This->owner);
721     This->owner = aOwner;
722
723     return NS_OK;
724 }
725
726 static nsresult NSAPI nsChannel_GetNotificationCallbacks(nsIHttpChannel *iface,
727         nsIInterfaceRequestor **aNotificationCallbacks)
728 {
729     nsChannel *This = impl_from_nsIHttpChannel(iface);
730
731     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
732
733     if(This->notif_callback)
734         nsIInterfaceRequestor_AddRef(This->notif_callback);
735     *aNotificationCallbacks = This->notif_callback;
736
737     return NS_OK;
738 }
739
740 static nsresult NSAPI nsChannel_SetNotificationCallbacks(nsIHttpChannel *iface,
741         nsIInterfaceRequestor *aNotificationCallbacks)
742 {
743     nsChannel *This = impl_from_nsIHttpChannel(iface);
744
745     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
746
747     if(This->notif_callback)
748         nsIInterfaceRequestor_Release(This->notif_callback);
749     if(aNotificationCallbacks)
750         nsIInterfaceRequestor_AddRef(aNotificationCallbacks);
751
752     This->notif_callback = aNotificationCallbacks;
753
754     return NS_OK;
755 }
756
757 static nsresult NSAPI nsChannel_GetSecurityInfo(nsIHttpChannel *iface, nsISupports **aSecurityInfo)
758 {
759     nsChannel *This = impl_from_nsIHttpChannel(iface);
760
761     TRACE("(%p)->(%p)\n", This, aSecurityInfo);
762
763     return NS_ERROR_NOT_IMPLEMENTED;
764 }
765
766 static nsresult NSAPI nsChannel_GetContentType(nsIHttpChannel *iface, nsACString *aContentType)
767 {
768     nsChannel *This = impl_from_nsIHttpChannel(iface);
769
770     TRACE("(%p)->(%p)\n", This, aContentType);
771
772     if(This->content_type) {
773         nsACString_SetData(aContentType, This->content_type);
774         return S_OK;
775     }
776
777     if(This->uri->is_doc_uri) {
778         WARN("Document channel with no MIME set. Assuming text/html\n");
779         nsACString_SetData(aContentType, "text/html");
780         return S_OK;
781     }
782
783     WARN("unknown type\n");
784     return NS_ERROR_FAILURE;
785 }
786
787 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
788                                                const nsACString *aContentType)
789 {
790     nsChannel *This = impl_from_nsIHttpChannel(iface);
791     const char *content_type;
792
793     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aContentType));
794
795     nsACString_GetData(aContentType, &content_type);
796     heap_free(This->content_type);
797     This->content_type = heap_strdupA(content_type);
798
799     return NS_OK;
800 }
801
802 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
803                                                   nsACString *aContentCharset)
804 {
805     nsChannel *This = impl_from_nsIHttpChannel(iface);
806
807     TRACE("(%p)->(%p)\n", This, aContentCharset);
808
809     if(This->charset) {
810         nsACString_SetData(aContentCharset, This->charset);
811         return NS_OK;
812     }
813
814     nsACString_SetData(aContentCharset, "");
815     return NS_OK;
816 }
817
818 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
819                                                   const nsACString *aContentCharset)
820 {
821     nsChannel *This = impl_from_nsIHttpChannel(iface);
822     const char *data;
823     char *charset;
824
825     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aContentCharset));
826
827     nsACString_GetData(aContentCharset, &data);
828     charset = heap_strdupA(data);
829     if(!charset)
830         return NS_ERROR_OUT_OF_MEMORY;
831
832     heap_free(This->charset);
833     This->charset = charset;
834     return NS_OK;
835 }
836
837 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, LONG *aContentLength)
838 {
839     nsChannel *This = impl_from_nsIHttpChannel(iface);
840
841     FIXME("(%p)->(%p)\n", This, aContentLength);
842
843     return NS_ERROR_NOT_IMPLEMENTED;
844 }
845
846 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, LONG aContentLength)
847 {
848     nsChannel *This = impl_from_nsIHttpChannel(iface);
849
850     FIXME("(%p)->(%d)\n", This, aContentLength);
851
852     return NS_ERROR_NOT_IMPLEMENTED;
853 }
854
855 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
856 {
857     nsChannel *This = impl_from_nsIHttpChannel(iface);
858
859     FIXME("(%p)->(%p)\n", This, _retval);
860
861     return NS_ERROR_NOT_IMPLEMENTED;
862 }
863
864 static HTMLOuterWindow *get_window_from_load_group(nsChannel *This)
865 {
866     HTMLOuterWindow *window;
867     nsIChannel *channel;
868     nsIRequest *req;
869     nsWineURI *wine_uri;
870     nsIURI *uri;
871     nsresult nsres;
872
873     nsres = nsILoadGroup_GetDefaultLoadRequest(This->load_group, &req);
874     if(NS_FAILED(nsres)) {
875         ERR("GetDefaultLoadRequest failed: %08x\n", nsres);
876         return NULL;
877     }
878
879     if(!req)
880         return NULL;
881
882     nsres = nsIRequest_QueryInterface(req, &IID_nsIChannel, (void**)&channel);
883     nsIRequest_Release(req);
884     if(NS_FAILED(nsres)) {
885         WARN("Could not get nsIChannel interface: %08x\n", nsres);
886         return NULL;
887     }
888
889     nsres = nsIChannel_GetURI(channel, &uri);
890     nsIChannel_Release(channel);
891     if(NS_FAILED(nsres)) {
892         ERR("GetURI failed: %08x\n", nsres);
893         return NULL;
894     }
895
896     nsres = nsIURI_QueryInterface(uri, &IID_nsWineURI, (void**)&wine_uri);
897     nsIURI_Release(uri);
898     if(NS_FAILED(nsres)) {
899         TRACE("Could not get nsWineURI: %08x\n", nsres);
900         return NULL;
901     }
902
903     window = wine_uri->window_ref ? wine_uri->window_ref->window : NULL;
904     if(window)
905         IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
906     nsIFileURL_Release(&wine_uri->nsIFileURL_iface);
907
908     return window;
909 }
910
911 static HTMLOuterWindow *get_channel_window(nsChannel *This)
912 {
913     nsIWebProgress *web_progress;
914     nsIDOMWindow *nswindow;
915     HTMLOuterWindow *window;
916     nsresult nsres;
917
918     if(This->load_group) {
919         nsIRequestObserver *req_observer;
920
921         nsres = nsILoadGroup_GetGroupObserver(This->load_group, &req_observer);
922         if(NS_FAILED(nsres) || !req_observer) {
923             ERR("GetGroupObserver failed: %08x\n", nsres);
924             return NULL;
925         }
926
927         nsres = nsIRequestObserver_QueryInterface(req_observer, &IID_nsIWebProgress, (void**)&web_progress);
928         nsIRequestObserver_Release(req_observer);
929         if(NS_FAILED(nsres)) {
930             ERR("Could not get nsIWebProgress iface: %08x\n", nsres);
931             return NULL;
932         }
933     }else if(This->notif_callback) {
934         nsres = nsIInterfaceRequestor_GetInterface(This->notif_callback, &IID_nsIWebProgress, (void**)&web_progress);
935         if(NS_FAILED(nsres)) {
936             ERR("GetInterface(IID_nsIWebProgress failed: %08x\n", nsres);
937             return NULL;
938         }
939     }else {
940         ERR("no load group nor notif callback\n");
941         return NULL;
942     }
943
944     nsres = nsIWebProgress_GetDOMWindow(web_progress, &nswindow);
945     nsIWebProgress_Release(web_progress);
946     if(NS_FAILED(nsres) || !nswindow) {
947         ERR("GetDOMWindow failed: %08x\n", nsres);
948         return NULL;
949     }
950
951     window = nswindow_to_window(nswindow);
952     nsIDOMWindow_Release(nswindow);
953
954     if(window)
955         IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
956     else
957         FIXME("NULL window for %p\n", nswindow);
958     return window;
959 }
960
961 typedef struct {
962     task_t header;
963     HTMLInnerWindow *window;
964     nsChannelBSC *bscallback;
965 } start_binding_task_t;
966
967 static void start_binding_proc(task_t *_task)
968 {
969     start_binding_task_t *task = (start_binding_task_t*)_task;
970
971     start_binding(task->window, (BSCallback*)task->bscallback, NULL);
972 }
973
974 static void start_binding_task_destr(task_t *_task)
975 {
976     start_binding_task_t *task = (start_binding_task_t*)_task;
977
978     IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
979     heap_free(task);
980 }
981
982 static nsresult async_open(nsChannel *This, HTMLOuterWindow *window, BOOL is_doc_channel, nsIStreamListener *listener,
983         nsISupports *context)
984 {
985     nsChannelBSC *bscallback;
986     IMoniker *mon = NULL;
987     HRESULT hres;
988
989     hres = CreateURLMonikerEx2(NULL, This->uri->uri, &mon, 0);
990     if(FAILED(hres)) {
991         WARN("CreateURLMoniker failed: %08x\n", hres);
992         return NS_ERROR_UNEXPECTED;
993     }
994
995     if(is_doc_channel)
996         set_current_mon(window, mon, BINDING_NAVIGATED);
997
998     hres = create_channelbsc(mon, NULL, NULL, 0, is_doc_channel, &bscallback);
999     IMoniker_Release(mon);
1000     if(FAILED(hres))
1001         return NS_ERROR_UNEXPECTED;
1002
1003     channelbsc_set_channel(bscallback, This, listener, context);
1004
1005     if(is_doc_channel) {
1006         hres = create_pending_window(window, bscallback);
1007         if(SUCCEEDED(hres))
1008             async_start_doc_binding(window, window->pending_window);
1009         IBindStatusCallback_Release(&bscallback->bsc.IBindStatusCallback_iface);
1010         if(FAILED(hres))
1011             return NS_ERROR_UNEXPECTED;
1012     }else {
1013         start_binding_task_t *task;
1014
1015         task = heap_alloc(sizeof(start_binding_task_t));
1016         if(!task) {
1017             IBindStatusCallback_Release(&bscallback->bsc.IBindStatusCallback_iface);
1018             return NS_ERROR_OUT_OF_MEMORY;
1019         }
1020
1021         task->window = window->base.inner_window;
1022         task->bscallback = bscallback;
1023         hres = push_task(&task->header, start_binding_proc, start_binding_task_destr, window->base.inner_window->task_magic);
1024         if(FAILED(hres))
1025             return NS_ERROR_OUT_OF_MEMORY;
1026     }
1027
1028     return NS_OK;
1029 }
1030
1031 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
1032                                           nsISupports *aContext)
1033 {
1034     nsChannel *This = impl_from_nsIHttpChannel(iface);
1035     HTMLOuterWindow *window = NULL;
1036     BOOL cancel = FALSE;
1037     nsresult nsres = NS_OK;
1038
1039     TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
1040
1041     if(!ensure_uri(This->uri))
1042         return NS_ERROR_FAILURE;
1043
1044     if(TRACE_ON(mshtml)) {
1045         HRESULT hres;
1046         BSTR uri_str;
1047
1048         hres = IUri_GetDisplayUri(This->uri->uri, &uri_str);
1049         if(SUCCEEDED(hres)) {
1050             TRACE("opening %s\n", debugstr_w(uri_str));
1051             SysFreeString(uri_str);
1052         }else {
1053             WARN("GetDisplayUri failed: %08x\n", hres);
1054         }
1055     }
1056
1057     if(This->uri->is_doc_uri) {
1058         window = get_channel_window(This);
1059         if(window) {
1060             set_uri_window(This->uri, window);
1061         }else if(This->uri->container) {
1062             BOOL b;
1063
1064             /* nscontainer->doc should be NULL which means navigation to a new window */
1065             if(This->uri->container->doc)
1066                 FIXME("nscontainer->doc = %p\n", This->uri->container->doc);
1067
1068             nsres = before_async_open(This, This->uri->container, &b);
1069             if(NS_FAILED(nsres))
1070                 return nsres;
1071             if(b)
1072                 FIXME("Navigation not cancelled\n");
1073             return NS_ERROR_UNEXPECTED;
1074         }
1075     }
1076
1077     if(!window) {
1078         if(This->uri->window_ref && This->uri->window_ref->window) {
1079             window = This->uri->window_ref->window;
1080             IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
1081         }else {
1082             /* FIXME: Analyze removing get_window_from_load_group call */
1083             if(This->load_group)
1084                 window = get_window_from_load_group(This);
1085             if(!window)
1086                 window = get_channel_window(This);
1087             if(window)
1088                 set_uri_window(This->uri, window);
1089         }
1090     }
1091
1092     if(!window) {
1093         ERR("window = NULL\n");
1094         return NS_ERROR_UNEXPECTED;
1095     }
1096
1097     if(This->uri->is_doc_uri && window == window->doc_obj->basedoc.window) {
1098         if(This->uri->channel_bsc) {
1099             channelbsc_set_channel(This->uri->channel_bsc, This, aListener, aContext);
1100
1101             if(window->doc_obj->mime) {
1102                 heap_free(This->content_type);
1103                 This->content_type = heap_strdupWtoA(window->doc_obj->mime);
1104             }
1105
1106             cancel = TRUE;
1107         }else {
1108             nsres = before_async_open(This, window->doc_obj->nscontainer, &cancel);
1109             if(NS_SUCCEEDED(nsres)  && cancel) {
1110                 TRACE("canceled\n");
1111                 nsres = NS_BINDING_ABORTED;
1112             }
1113         }
1114     }
1115
1116     if(!cancel)
1117         nsres = async_open(This, window, This->uri->is_doc_uri, aListener, aContext);
1118
1119     if(NS_SUCCEEDED(nsres) && This->load_group) {
1120         nsres = nsILoadGroup_AddRequest(This->load_group, (nsIRequest*)&This->nsIHttpChannel_iface,
1121                 aContext);
1122         if(NS_FAILED(nsres))
1123             ERR("AddRequest failed: %08x\n", nsres);
1124     }
1125
1126     IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
1127     return nsres;
1128 }
1129
1130 static nsresult NSAPI nsChannel_GetContentDisposition(nsIHttpChannel *iface, UINT32 *aContentDisposition)
1131 {
1132     nsChannel *This = impl_from_nsIHttpChannel(iface);
1133     FIXME("(%p)->(%p)\n", This, aContentDisposition);
1134     return NS_ERROR_NOT_IMPLEMENTED;
1135 }
1136
1137 static nsresult NSAPI nsChannel_SetContentDisposition(nsIHttpChannel *iface, UINT32 aContentDisposition)
1138 {
1139     nsChannel *This = impl_from_nsIHttpChannel(iface);
1140     FIXME("(%p)->(%u)\n", This, aContentDisposition);
1141     return NS_ERROR_NOT_IMPLEMENTED;
1142 }
1143
1144 static nsresult NSAPI nsChannel_GetContentDispositionFilename(nsIHttpChannel *iface, nsAString *aContentDispositionFilename)
1145 {
1146     nsChannel *This = impl_from_nsIHttpChannel(iface);
1147     FIXME("(%p)->(%p)\n", This, aContentDispositionFilename);
1148     return NS_ERROR_NOT_IMPLEMENTED;
1149 }
1150
1151 static nsresult NSAPI nsChannel_SetContentDispositionFilename(nsIHttpChannel *iface, const nsAString *aContentDispositionFilename)
1152 {
1153     nsChannel *This = impl_from_nsIHttpChannel(iface);
1154     FIXME("(%p)->(%p)\n", This, aContentDispositionFilename);
1155     return NS_ERROR_NOT_IMPLEMENTED;
1156 }
1157
1158 static nsresult NSAPI nsChannel_GetContentDispositionHeader(nsIHttpChannel *iface, nsACString *aContentDispositionHeader)
1159 {
1160     nsChannel *This = impl_from_nsIHttpChannel(iface);
1161     FIXME("(%p)->(%p)\n", This, aContentDispositionHeader);
1162     return NS_ERROR_NOT_IMPLEMENTED;
1163 }
1164
1165 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
1166 {
1167     nsChannel *This = impl_from_nsIHttpChannel(iface);
1168
1169     TRACE("(%p)->(%p)\n", This, aRequestMethod);
1170
1171     nsACString_SetData(aRequestMethod, request_method_strings[This->request_method]);
1172     return NS_OK;
1173 }
1174
1175 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
1176                                                  const nsACString *aRequestMethod)
1177 {
1178     nsChannel *This = impl_from_nsIHttpChannel(iface);
1179     const char *method;
1180     unsigned i;
1181
1182     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aRequestMethod));
1183
1184     nsACString_GetData(aRequestMethod, &method);
1185     for(i=0; i < sizeof(request_method_strings)/sizeof(*request_method_strings); i++) {
1186         if(!strcasecmp(method, request_method_strings[i])) {
1187             This->request_method = i;
1188             return NS_OK;
1189         }
1190     }
1191
1192     ERR("Invalid method %s\n", debugstr_a(method));
1193     return NS_ERROR_UNEXPECTED;
1194 }
1195
1196 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
1197 {
1198     nsChannel *This = impl_from_nsIHttpChannel(iface);
1199
1200     TRACE("(%p)->(%p)\n", This, aReferrer);
1201
1202     if(This->referrer)
1203         nsIURI_AddRef(This->referrer);
1204     *aReferrer = This->referrer;
1205     return NS_OK;
1206 }
1207
1208 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
1209 {
1210     nsChannel *This = impl_from_nsIHttpChannel(iface);
1211
1212     TRACE("(%p)->(%p)\n", This, aReferrer);
1213
1214     if(aReferrer)
1215         nsIURI_AddRef(aReferrer);
1216     if(This->referrer)
1217         nsIURI_Release(This->referrer);
1218     This->referrer = aReferrer;
1219     return NS_OK;
1220 }
1221
1222 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
1223          const nsACString *aHeader, nsACString *_retval)
1224 {
1225     nsChannel *This = impl_from_nsIHttpChannel(iface);
1226
1227     TRACE("(%p)->(%s %p)\n", This, debugstr_nsacstr(aHeader), _retval);
1228
1229     return get_channel_http_header(&This->request_headers, aHeader, _retval);
1230 }
1231
1232 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
1233          const nsACString *aHeader, const nsACString *aValue, cpp_bool aMerge)
1234 {
1235     nsChannel *This = impl_from_nsIHttpChannel(iface);
1236
1237     TRACE("(%p)->(%s %s %x)\n", This, debugstr_nsacstr(aHeader), debugstr_nsacstr(aValue), aMerge);
1238
1239     if(aMerge)
1240         FIXME("aMerge not supported\n");
1241
1242     return set_channel_http_header(&This->request_headers, aHeader, aValue);
1243 }
1244
1245 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
1246                                                     nsIHttpHeaderVisitor *aVisitor)
1247 {
1248     nsChannel *This = impl_from_nsIHttpChannel(iface);
1249
1250     FIXME("(%p)->(%p)\n", This, aVisitor);
1251
1252     return NS_ERROR_NOT_IMPLEMENTED;
1253 }
1254
1255 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, cpp_bool *aAllowPipelining)
1256 {
1257     nsChannel *This = impl_from_nsIHttpChannel(iface);
1258
1259     FIXME("(%p)->(%p)\n", This, aAllowPipelining);
1260
1261     return NS_ERROR_NOT_IMPLEMENTED;
1262 }
1263
1264 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, cpp_bool aAllowPipelining)
1265 {
1266     nsChannel *This = impl_from_nsIHttpChannel(iface);
1267
1268     FIXME("(%p)->(%x)\n", This, aAllowPipelining);
1269
1270     return NS_ERROR_NOT_IMPLEMENTED;
1271 }
1272
1273 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, UINT32 *aRedirectionLimit)
1274 {
1275     nsChannel *This = impl_from_nsIHttpChannel(iface);
1276
1277     FIXME("(%p)->(%p)\n", This, aRedirectionLimit);
1278
1279     return NS_ERROR_NOT_IMPLEMENTED;
1280 }
1281
1282 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, UINT32 aRedirectionLimit)
1283 {
1284     nsChannel *This = impl_from_nsIHttpChannel(iface);
1285
1286     FIXME("(%p)->(%u)\n", This, aRedirectionLimit);
1287
1288     return NS_ERROR_NOT_IMPLEMENTED;
1289 }
1290
1291 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, UINT32 *aResponseStatus)
1292 {
1293     nsChannel *This = impl_from_nsIHttpChannel(iface);
1294
1295     TRACE("(%p)->(%p)\n", This, aResponseStatus);
1296
1297     if(This->response_status) {
1298         *aResponseStatus = This->response_status;
1299         return NS_OK;
1300     }
1301
1302     WARN("No response status\n");
1303     return NS_ERROR_UNEXPECTED;
1304 }
1305
1306 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
1307                                                       nsACString *aResponseStatusText)
1308 {
1309     nsChannel *This = impl_from_nsIHttpChannel(iface);
1310
1311     FIXME("(%p)->(%p)\n", This, aResponseStatusText);
1312
1313     return NS_ERROR_NOT_IMPLEMENTED;
1314 }
1315
1316 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
1317                                                     cpp_bool *aRequestSucceeded)
1318 {
1319     nsChannel *This = impl_from_nsIHttpChannel(iface);
1320
1321     TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
1322
1323     if(!This->response_status)
1324         return NS_ERROR_NOT_AVAILABLE;
1325
1326     *aRequestSucceeded = This->response_status/100 == 2;
1327
1328     return NS_OK;
1329 }
1330
1331 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
1332          const nsACString *header, nsACString *_retval)
1333 {
1334     nsChannel *This = impl_from_nsIHttpChannel(iface);
1335
1336     TRACE("(%p)->(%s %p)\n", This, debugstr_nsacstr(header), _retval);
1337
1338     return get_channel_http_header(&This->response_headers, header, _retval);
1339 }
1340
1341 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
1342         const nsACString *header, const nsACString *value, cpp_bool merge)
1343 {
1344     nsChannel *This = impl_from_nsIHttpChannel(iface);
1345
1346     FIXME("(%p)->(%s %s %x)\n", This, debugstr_nsacstr(header), debugstr_nsacstr(value), merge);
1347
1348     return NS_ERROR_NOT_IMPLEMENTED;
1349 }
1350
1351 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
1352         nsIHttpHeaderVisitor *aVisitor)
1353 {
1354     nsChannel *This = impl_from_nsIHttpChannel(iface);
1355
1356     TRACE("(%p)->(%p)\n", This, aVisitor);
1357
1358     return visit_http_headers(&This->response_headers, aVisitor);
1359 }
1360
1361 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, cpp_bool *_retval)
1362 {
1363     nsChannel *This = impl_from_nsIHttpChannel(iface);
1364     http_header_t *header;
1365
1366     static const WCHAR cache_controlW[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l'};
1367     static const WCHAR no_storeW[] = {'n','o','-','s','t','o','r','e',0};
1368
1369     TRACE("(%p)->(%p)\n", This, _retval);
1370
1371     header = find_http_header(&This->response_headers, cache_controlW, sizeof(cache_controlW)/sizeof(WCHAR));
1372     *_retval = header && !strcmpiW(header->data, no_storeW);
1373     return NS_OK;
1374 }
1375
1376 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, cpp_bool *_retval)
1377 {
1378     nsChannel *This = impl_from_nsIHttpChannel(iface);
1379
1380     FIXME("(%p)->(%p)\n", This, _retval);
1381
1382     return NS_ERROR_NOT_IMPLEMENTED;
1383 }
1384
1385 static const nsIHttpChannelVtbl nsChannelVtbl = {
1386     nsChannel_QueryInterface,
1387     nsChannel_AddRef,
1388     nsChannel_Release,
1389     nsChannel_GetName,
1390     nsChannel_IsPending,
1391     nsChannel_GetStatus,
1392     nsChannel_Cancel,
1393     nsChannel_Suspend,
1394     nsChannel_Resume,
1395     nsChannel_GetLoadGroup,
1396     nsChannel_SetLoadGroup,
1397     nsChannel_GetLoadFlags,
1398     nsChannel_SetLoadFlags,
1399     nsChannel_GetOriginalURI,
1400     nsChannel_SetOriginalURI,
1401     nsChannel_GetURI,
1402     nsChannel_GetOwner,
1403     nsChannel_SetOwner,
1404     nsChannel_GetNotificationCallbacks,
1405     nsChannel_SetNotificationCallbacks,
1406     nsChannel_GetSecurityInfo,
1407     nsChannel_GetContentType,
1408     nsChannel_SetContentType,
1409     nsChannel_GetContentCharset,
1410     nsChannel_SetContentCharset,
1411     nsChannel_GetContentLength,
1412     nsChannel_SetContentLength,
1413     nsChannel_Open,
1414     nsChannel_AsyncOpen,
1415     nsChannel_GetContentDisposition,
1416     nsChannel_SetContentDisposition,
1417     nsChannel_GetContentDispositionFilename,
1418     nsChannel_SetContentDispositionFilename,
1419     nsChannel_GetContentDispositionHeader,
1420     nsChannel_GetRequestMethod,
1421     nsChannel_SetRequestMethod,
1422     nsChannel_GetReferrer,
1423     nsChannel_SetReferrer,
1424     nsChannel_GetRequestHeader,
1425     nsChannel_SetRequestHeader,
1426     nsChannel_VisitRequestHeaders,
1427     nsChannel_GetAllowPipelining,
1428     nsChannel_SetAllowPipelining,
1429     nsChannel_GetRedirectionLimit,
1430     nsChannel_SetRedirectionLimit,
1431     nsChannel_GetResponseStatus,
1432     nsChannel_GetResponseStatusText,
1433     nsChannel_GetRequestSucceeded,
1434     nsChannel_GetResponseHeader,
1435     nsChannel_SetResponseHeader,
1436     nsChannel_VisitResponseHeaders,
1437     nsChannel_IsNoStoreResponse,
1438     nsChannel_IsNoCacheResponse
1439 };
1440
1441 static inline nsChannel *impl_from_nsIUploadChannel(nsIUploadChannel *iface)
1442 {
1443     return CONTAINING_RECORD(iface, nsChannel, nsIUploadChannel_iface);
1444 }
1445
1446 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
1447         void **result)
1448 {
1449     nsChannel *This = impl_from_nsIUploadChannel(iface);
1450     return nsIHttpChannel_QueryInterface(&This->nsIHttpChannel_iface, riid, result);
1451 }
1452
1453 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
1454 {
1455     nsChannel *This = impl_from_nsIUploadChannel(iface);
1456     return nsIHttpChannel_AddRef(&This->nsIHttpChannel_iface);
1457 }
1458
1459 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
1460 {
1461     nsChannel *This = impl_from_nsIUploadChannel(iface);
1462     return nsIHttpChannel_Release(&This->nsIHttpChannel_iface);
1463 }
1464
1465 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
1466         nsIInputStream *aStream, const nsACString *aContentType, INT64 aContentLength)
1467 {
1468     nsChannel *This = impl_from_nsIUploadChannel(iface);
1469     const char *content_type;
1470
1471     static const WCHAR content_typeW[] =
1472         {'C','o','n','t','e','n','t','-','T','y','p','e',0};
1473
1474     TRACE("(%p)->(%p %s %s)\n", This, aStream, debugstr_nsacstr(aContentType), wine_dbgstr_longlong(aContentLength));
1475
1476     This->post_data_contains_headers = TRUE;
1477
1478     if(aContentType) {
1479         nsACString_GetData(aContentType, &content_type);
1480         if(*content_type) {
1481             WCHAR *ct;
1482
1483             ct = heap_strdupAtoW(content_type);
1484             if(!ct)
1485                 return NS_ERROR_UNEXPECTED;
1486
1487             set_http_header(&This->request_headers, content_typeW,
1488                     sizeof(content_typeW)/sizeof(WCHAR), ct, strlenW(ct));
1489             heap_free(ct);
1490             This->post_data_contains_headers = FALSE;
1491         }
1492     }
1493
1494     if(This->post_data_stream)
1495         nsIInputStream_Release(This->post_data_stream);
1496
1497     if(aContentLength != -1)
1498         FIXME("Unsupported acontentLength = %s\n", wine_dbgstr_longlong(aContentLength));
1499
1500     if(This->post_data_stream)
1501         nsIInputStream_Release(This->post_data_stream);
1502     This->post_data_stream = aStream;
1503     if(aStream)
1504         nsIInputStream_AddRef(aStream);
1505
1506     This->request_method = METHOD_POST;
1507     return NS_OK;
1508 }
1509
1510 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1511         nsIInputStream **aUploadStream)
1512 {
1513     nsChannel *This = impl_from_nsIUploadChannel(iface);
1514
1515     TRACE("(%p)->(%p)\n", This, aUploadStream);
1516
1517     if(This->post_data_stream)
1518         nsIInputStream_AddRef(This->post_data_stream);
1519
1520     *aUploadStream = This->post_data_stream;
1521     return NS_OK;
1522 }
1523
1524 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1525     nsUploadChannel_QueryInterface,
1526     nsUploadChannel_AddRef,
1527     nsUploadChannel_Release,
1528     nsUploadChannel_SetUploadStream,
1529     nsUploadChannel_GetUploadStream
1530 };
1531
1532 static inline nsChannel *impl_from_nsIHttpChannelInternal(nsIHttpChannelInternal *iface)
1533 {
1534     return CONTAINING_RECORD(iface, nsChannel, nsIHttpChannelInternal_iface);
1535 }
1536
1537 static nsresult NSAPI nsHttpChannelInternal_QueryInterface(nsIHttpChannelInternal *iface, nsIIDRef riid,
1538         void **result)
1539 {
1540     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1541     return nsIHttpChannel_QueryInterface(&This->nsIHttpChannel_iface, riid, result);
1542 }
1543
1544 static nsrefcnt NSAPI nsHttpChannelInternal_AddRef(nsIHttpChannelInternal *iface)
1545 {
1546     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1547     return nsIHttpChannel_AddRef(&This->nsIHttpChannel_iface);
1548 }
1549
1550 static nsrefcnt NSAPI nsHttpChannelInternal_Release(nsIHttpChannelInternal *iface)
1551 {
1552     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1553     return nsIHttpChannel_Release(&This->nsIHttpChannel_iface);
1554 }
1555
1556 static nsresult NSAPI nsHttpChannelInternal_GetDocumentURI(nsIHttpChannelInternal *iface, nsIURI **aDocumentURI)
1557 {
1558     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1559
1560     FIXME("(%p)->()\n", This);
1561
1562     return NS_ERROR_NOT_IMPLEMENTED;
1563 }
1564
1565 static nsresult NSAPI nsHttpChannelInternal_SetDocumentURI(nsIHttpChannelInternal *iface, nsIURI *aDocumentURI)
1566 {
1567     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1568
1569     FIXME("(%p)->()\n", This);
1570
1571     return NS_ERROR_NOT_IMPLEMENTED;
1572 }
1573
1574 static nsresult NSAPI nsHttpChannelInternal_GetRequestVersion(nsIHttpChannelInternal *iface, UINT32 *major, UINT32 *minor)
1575 {
1576     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1577
1578     FIXME("(%p)->()\n", This);
1579
1580     return NS_ERROR_NOT_IMPLEMENTED;
1581 }
1582
1583 static nsresult NSAPI nsHttpChannelInternal_GetResponseVersion(nsIHttpChannelInternal *iface, UINT32 *major, UINT32 *minor)
1584 {
1585     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1586
1587     FIXME("(%p)->()\n", This);
1588
1589     return NS_ERROR_NOT_IMPLEMENTED;
1590 }
1591
1592 static nsresult NSAPI nsHttpChannelInternal_SetCookie(nsIHttpChannelInternal *iface, const char *aCookieHeader)
1593 {
1594     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1595
1596     FIXME("(%p)->()\n", This);
1597
1598     return NS_ERROR_NOT_IMPLEMENTED;
1599 }
1600
1601 static nsresult NSAPI nsHttpChannelInternal_SetupFallbackChannel(nsIHttpChannelInternal *iface, const char *aFallbackKey)
1602 {
1603     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1604
1605     FIXME("(%p)->()\n", This);
1606
1607     return NS_ERROR_NOT_IMPLEMENTED;
1608 }
1609
1610 static nsresult NSAPI nsHttpChannelInternal_GetForceAllowThirdPartyCookie(nsIHttpChannelInternal *iface, cpp_bool *aForceThirdPartyCookie)
1611 {
1612     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1613
1614     FIXME("(%p)->()\n", This);
1615
1616     return NS_ERROR_NOT_IMPLEMENTED;
1617 }
1618
1619 static nsresult NSAPI nsHttpChannelInternal_SetForceAllowThirdPartyCookie(nsIHttpChannelInternal *iface, cpp_bool aForceThirdPartyCookie)
1620 {
1621     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1622
1623     FIXME("(%p)->()\n", This);
1624
1625     return NS_ERROR_NOT_IMPLEMENTED;
1626 }
1627
1628 static nsresult NSAPI nsHttpChannelInternal_GetCanceled(nsIHttpChannelInternal *iface, cpp_bool *aCanceled)
1629 {
1630     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1631
1632     FIXME("(%p)->(%p)\n", This, aCanceled);
1633
1634     return NS_ERROR_NOT_IMPLEMENTED;
1635 }
1636
1637 static nsresult NSAPI nsHttpChannelInternal_GetChannelIsForDownload(nsIHttpChannelInternal *iface, cpp_bool *aCanceled)
1638 {
1639     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1640
1641     FIXME("(%p)->(%p)\n", This, aCanceled);
1642
1643     return NS_ERROR_NOT_IMPLEMENTED;
1644 }
1645
1646 static nsresult NSAPI nsHttpChannelInternal_SetChannelIsForDownload(nsIHttpChannelInternal *iface, cpp_bool aCanceled)
1647 {
1648     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1649
1650     FIXME("(%p)->(%x)\n", This, aCanceled);
1651
1652     return NS_ERROR_NOT_IMPLEMENTED;
1653 }
1654
1655 static nsresult NSAPI nsHttpChannelInternal_GetLocalAddress(nsIHttpChannelInternal *iface, nsACString *aLocalAddress)
1656 {
1657     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1658
1659     FIXME("(%p)->(%p)\n", This, aLocalAddress);
1660
1661     return NS_ERROR_NOT_IMPLEMENTED;
1662 }
1663
1664 static nsresult NSAPI nsHttpChannelInternal_GetLocalPort(nsIHttpChannelInternal *iface, LONG *aLocalPort)
1665 {
1666     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1667
1668     FIXME("(%p)->(%p)\n", This, aLocalPort);
1669
1670     return NS_ERROR_NOT_IMPLEMENTED;
1671 }
1672
1673 static nsresult NSAPI nsHttpChannelInternal_GetRemoteAddress(nsIHttpChannelInternal *iface, nsACString *aRemoteAddress)
1674 {
1675     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1676
1677     FIXME("(%p)->(%p)\n", This, aRemoteAddress);
1678
1679     return NS_ERROR_NOT_IMPLEMENTED;
1680 }
1681
1682 static nsresult NSAPI nsHttpChannelInternal_GetRemotePort(nsIHttpChannelInternal *iface, LONG *aRemotePort)
1683 {
1684     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1685
1686     FIXME("(%p)->(%p)\n", This, aRemotePort);
1687
1688     return NS_ERROR_NOT_IMPLEMENTED;
1689 }
1690
1691 static nsresult NSAPI nsHttpChannelInternal_SetCacheKeysRedirectChain(nsIHttpChannelInternal *iface, void *cacheKeys)
1692 {
1693     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1694
1695     FIXME("(%p)->(%p)\n", This, cacheKeys);
1696
1697     return NS_ERROR_NOT_IMPLEMENTED;
1698 }
1699
1700 static nsresult NSAPI nsHttpChannelInternal_HTTPUpgrade(nsIHttpChannelInternal *iface,
1701         const nsACString *aProtocolName, nsIHttpUpgradeListener *aListener)
1702 {
1703     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1704     FIXME("(%p)->(%s %p)\n", This, debugstr_nsacstr(aProtocolName), aListener);
1705     return NS_ERROR_NOT_IMPLEMENTED;
1706 }
1707
1708 static nsresult NSAPI nsHttpChannelInternal_GetAllowSpdy(nsIHttpChannelInternal *iface, cpp_bool *aAllowSpdy)
1709 {
1710     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1711     FIXME("(%p)->(%p)\n", This, aAllowSpdy);
1712     return NS_ERROR_NOT_IMPLEMENTED;
1713 }
1714
1715 static nsresult NSAPI nsHttpChannelInternal_SetAllowSpdy(nsIHttpChannelInternal *iface, cpp_bool aAllowSpdy)
1716 {
1717     nsChannel *This = impl_from_nsIHttpChannelInternal(iface);
1718     FIXME("(%p)->(%x)\n", This, aAllowSpdy);
1719     return NS_ERROR_NOT_IMPLEMENTED;
1720 }
1721
1722 static const nsIHttpChannelInternalVtbl nsHttpChannelInternalVtbl = {
1723     nsHttpChannelInternal_QueryInterface,
1724     nsHttpChannelInternal_AddRef,
1725     nsHttpChannelInternal_Release,
1726     nsHttpChannelInternal_GetDocumentURI,
1727     nsHttpChannelInternal_SetDocumentURI,
1728     nsHttpChannelInternal_GetRequestVersion,
1729     nsHttpChannelInternal_GetResponseVersion,
1730     nsHttpChannelInternal_SetCookie,
1731     nsHttpChannelInternal_SetupFallbackChannel,
1732     nsHttpChannelInternal_GetForceAllowThirdPartyCookie,
1733     nsHttpChannelInternal_SetForceAllowThirdPartyCookie,
1734     nsHttpChannelInternal_GetCanceled,
1735     nsHttpChannelInternal_GetChannelIsForDownload,
1736     nsHttpChannelInternal_SetChannelIsForDownload,
1737     nsHttpChannelInternal_GetLocalAddress,
1738     nsHttpChannelInternal_GetLocalPort,
1739     nsHttpChannelInternal_GetRemoteAddress,
1740     nsHttpChannelInternal_GetRemotePort,
1741     nsHttpChannelInternal_SetCacheKeysRedirectChain,
1742     nsHttpChannelInternal_HTTPUpgrade,
1743     nsHttpChannelInternal_GetAllowSpdy,
1744     nsHttpChannelInternal_SetAllowSpdy
1745 };
1746
1747
1748 static void invalidate_uri(nsWineURI *This)
1749 {
1750     if(This->uri) {
1751         IUri_Release(This->uri);
1752         This->uri = NULL;
1753     }
1754 }
1755
1756 static BOOL ensure_uri_builder(nsWineURI *This)
1757 {
1758     if(!This->is_mutable) {
1759         WARN("Not mutable URI\n");
1760         return FALSE;
1761     }
1762
1763     if(!This->uri_builder) {
1764         HRESULT hres;
1765
1766         if(!ensure_uri(This))
1767             return FALSE;
1768
1769         hres = CreateIUriBuilder(This->uri, 0, 0, &This->uri_builder);
1770         if(FAILED(hres)) {
1771             WARN("CreateIUriBuilder failed: %08x\n", hres);
1772             return FALSE;
1773         }
1774     }
1775
1776     invalidate_uri(This);
1777     return TRUE;
1778 }
1779
1780 static nsresult get_uri_string(nsWineURI *This, Uri_PROPERTY prop, nsACString *ret)
1781 {
1782     char *vala;
1783     BSTR val;
1784     HRESULT hres;
1785
1786     if(!ensure_uri(This))
1787         return NS_ERROR_UNEXPECTED;
1788
1789     hres = IUri_GetPropertyBSTR(This->uri, prop, &val, 0);
1790     if(FAILED(hres)) {
1791         WARN("GetPropertyBSTR failed: %08x\n", hres);
1792         return NS_ERROR_UNEXPECTED;
1793     }
1794
1795     vala = heap_strdupWtoU(val);
1796     SysFreeString(val);
1797     if(!vala)
1798         return NS_ERROR_OUT_OF_MEMORY;
1799
1800     TRACE("ret %s\n", debugstr_a(vala));
1801     nsACString_SetData(ret, vala);
1802     heap_free(vala);
1803     return NS_OK;
1804 }
1805
1806 static inline nsWineURI *impl_from_nsIFileURL(nsIFileURL *iface)
1807 {
1808     return CONTAINING_RECORD(iface, nsWineURI, nsIFileURL_iface);
1809 }
1810
1811 static nsresult NSAPI nsURI_QueryInterface(nsIFileURL *iface, nsIIDRef riid, void **result)
1812 {
1813     nsWineURI *This = impl_from_nsIFileURL(iface);
1814
1815     *result = NULL;
1816
1817     if(IsEqualGUID(&IID_nsISupports, riid)) {
1818         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1819         *result = &This->nsIFileURL_iface;
1820     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1821         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1822         *result = &This->nsIFileURL_iface;
1823     }else if(IsEqualGUID(&IID_nsIURL, riid)) {
1824         TRACE("(%p)->(IID_nsIURL %p)\n", This, result);
1825         *result = &This->nsIFileURL_iface;
1826     }else if(IsEqualGUID(&IID_nsIFileURL, riid)) {
1827         TRACE("(%p)->(IID_nsIFileURL %p)\n", This, result);
1828         *result = This->scheme == URL_SCHEME_FILE ? &This->nsIFileURL_iface : NULL;
1829     }else if(IsEqualGUID(&IID_nsIMutable, riid)) {
1830         TRACE("(%p)->(IID_nsIMutable %p)\n", This, result);
1831         *result = &This->nsIStandardURL_iface;
1832     }else if(IsEqualGUID(&IID_nsIStandardURL, riid)) {
1833         TRACE("(%p)->(IID_nsIStandardURL %p)\n", This, result);
1834         *result = &This->nsIStandardURL_iface;
1835     }else if(IsEqualGUID(&IID_nsWineURI, riid)) {
1836         TRACE("(%p)->(IID_nsWineURI %p)\n", This, result);
1837         *result = This;
1838     }
1839
1840     if(*result) {
1841         nsIFileURL_AddRef(&This->nsIFileURL_iface);
1842         return NS_OK;
1843     }
1844
1845     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1846     return NS_NOINTERFACE;
1847 }
1848
1849 static nsrefcnt NSAPI nsURI_AddRef(nsIFileURL *iface)
1850 {
1851     nsWineURI *This = impl_from_nsIFileURL(iface);
1852     LONG ref = InterlockedIncrement(&This->ref);
1853
1854     TRACE("(%p) ref=%d\n", This, ref);
1855
1856     return ref;
1857 }
1858
1859 static nsrefcnt NSAPI nsURI_Release(nsIFileURL *iface)
1860 {
1861     nsWineURI *This = impl_from_nsIFileURL(iface);
1862     LONG ref = InterlockedDecrement(&This->ref);
1863
1864     TRACE("(%p) ref=%d\n", This, ref);
1865
1866     if(!ref) {
1867         if(This->window_ref)
1868             windowref_release(This->window_ref);
1869         if(This->container)
1870             nsIWebBrowserChrome_Release(&This->container->nsIWebBrowserChrome_iface);
1871         if(This->uri)
1872             IUri_Release(This->uri);
1873         heap_free(This->origin_charset);
1874         heap_free(This);
1875     }
1876
1877     return ref;
1878 }
1879
1880 static nsresult NSAPI nsURI_GetSpec(nsIFileURL *iface, nsACString *aSpec)
1881 {
1882     nsWineURI *This = impl_from_nsIFileURL(iface);
1883
1884     TRACE("(%p)->(%p)\n", This, aSpec);
1885
1886     return get_uri_string(This, Uri_PROPERTY_DISPLAY_URI, aSpec);
1887 }
1888
1889 static nsresult NSAPI nsURI_SetSpec(nsIFileURL *iface, const nsACString *aSpec)
1890 {
1891     nsWineURI *This = impl_from_nsIFileURL(iface);
1892     const char *speca;
1893     WCHAR *spec;
1894     IUri *uri;
1895     HRESULT hres;
1896
1897     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aSpec));
1898
1899     if(!This->is_mutable)
1900         return NS_ERROR_UNEXPECTED;
1901
1902     nsACString_GetData(aSpec, &speca);
1903     spec = heap_strdupUtoW(speca);
1904     if(!spec)
1905         return NS_ERROR_OUT_OF_MEMORY;
1906
1907     hres = CreateUri(spec, 0, 0, &uri);
1908     heap_free(spec);
1909     if(FAILED(hres)) {
1910         WARN("CreateUri failed: %08x\n", hres);
1911         return NS_ERROR_FAILURE;
1912     }
1913
1914     invalidate_uri(This);
1915     if(This->uri_builder) {
1916         IUriBuilder_Release(This->uri_builder);
1917         This->uri_builder = NULL;
1918     }
1919
1920     This->uri = uri;
1921     return NS_OK;
1922 }
1923
1924 static nsresult NSAPI nsURI_GetPrePath(nsIFileURL *iface, nsACString *aPrePath)
1925 {
1926     nsWineURI *This = impl_from_nsIFileURL(iface);
1927     FIXME("(%p)->(%p)\n", This, aPrePath);
1928     return NS_ERROR_NOT_IMPLEMENTED;
1929 }
1930
1931 static nsresult NSAPI nsURI_GetScheme(nsIFileURL *iface, nsACString *aScheme)
1932 {
1933     nsWineURI *This = impl_from_nsIFileURL(iface);
1934     DWORD scheme;
1935     HRESULT hres;
1936
1937     TRACE("(%p)->(%p)\n", This, aScheme);
1938
1939     if(!ensure_uri(This))
1940         return NS_ERROR_UNEXPECTED;
1941
1942     hres = IUri_GetScheme(This->uri, &scheme);
1943     if(FAILED(hres)) {
1944         WARN("GetScheme failed: %08x\n", hres);
1945         return NS_ERROR_UNEXPECTED;
1946     }
1947
1948     if(scheme == URL_SCHEME_ABOUT) {
1949         nsACString_SetData(aScheme, "wine");
1950         return NS_OK;
1951     }
1952
1953     return get_uri_string(This, Uri_PROPERTY_SCHEME_NAME, aScheme);
1954 }
1955
1956 static nsresult NSAPI nsURI_SetScheme(nsIFileURL *iface, const nsACString *aScheme)
1957 {
1958     nsWineURI *This = impl_from_nsIFileURL(iface);
1959     const char *schemea;
1960     WCHAR *scheme;
1961     HRESULT hres;
1962
1963     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aScheme));
1964
1965     if(!ensure_uri_builder(This))
1966         return NS_ERROR_UNEXPECTED;
1967
1968     nsACString_GetData(aScheme, &schemea);
1969     scheme = heap_strdupUtoW(schemea);
1970     if(!scheme)
1971         return NS_ERROR_OUT_OF_MEMORY;
1972
1973     hres = IUriBuilder_SetSchemeName(This->uri_builder, scheme);
1974     heap_free(scheme);
1975     if(FAILED(hres))
1976         return NS_ERROR_UNEXPECTED;
1977
1978     return NS_OK;
1979 }
1980
1981 static nsresult NSAPI nsURI_GetUserPass(nsIFileURL *iface, nsACString *aUserPass)
1982 {
1983     nsWineURI *This = impl_from_nsIFileURL(iface);
1984     BSTR user, pass;
1985     HRESULT hres;
1986
1987     TRACE("(%p)->(%p)\n", This, aUserPass);
1988
1989     if(!ensure_uri(This))
1990         return NS_ERROR_UNEXPECTED;
1991
1992     hres = IUri_GetUserName(This->uri, &user);
1993     if(FAILED(hres))
1994         return NS_ERROR_FAILURE;
1995
1996     hres = IUri_GetPassword(This->uri, &pass);
1997     if(FAILED(hres)) {
1998         SysFreeString(user);
1999         return NS_ERROR_FAILURE;
2000     }
2001
2002     if(*user || *pass) {
2003         FIXME("Construct user:pass string\n");
2004     }else {
2005         nsACString_SetData(aUserPass, "");
2006     }
2007
2008     SysFreeString(user);
2009     SysFreeString(pass);
2010     return NS_OK;
2011 }
2012
2013 static nsresult NSAPI nsURI_SetUserPass(nsIFileURL *iface, const nsACString *aUserPass)
2014 {
2015     nsWineURI *This = impl_from_nsIFileURL(iface);
2016     WCHAR *user = NULL, *pass = NULL, *buf = NULL;
2017     const char *user_pass;
2018     HRESULT hres;
2019
2020     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aUserPass));
2021
2022     if(!ensure_uri_builder(This))
2023         return NS_ERROR_UNEXPECTED;
2024
2025     nsACString_GetData(aUserPass, &user_pass);
2026     if(*user_pass) {
2027         WCHAR *ptr;
2028
2029         buf = heap_strdupUtoW(user_pass);
2030         if(!buf)
2031             return NS_ERROR_OUT_OF_MEMORY;
2032
2033         ptr = strchrW(buf, ':');
2034         if(!ptr) {
2035             user = buf;
2036         }else if(ptr != buf) {
2037             *ptr++ = 0;
2038             user = buf;
2039             if(*ptr)
2040                 pass = ptr;
2041         }else {
2042             pass = buf+1;
2043         }
2044     }
2045
2046     hres = IUriBuilder_SetUserName(This->uri_builder, user);
2047     if(SUCCEEDED(hres))
2048         hres = IUriBuilder_SetPassword(This->uri_builder, pass);
2049
2050     heap_free(buf);
2051     return SUCCEEDED(hres) ? NS_OK : NS_ERROR_FAILURE;
2052 }
2053
2054 static nsresult NSAPI nsURI_GetUsername(nsIFileURL *iface, nsACString *aUsername)
2055 {
2056     nsWineURI *This = impl_from_nsIFileURL(iface);
2057
2058     TRACE("(%p)->(%p)\n", This, aUsername);
2059
2060     return get_uri_string(This, Uri_PROPERTY_USER_NAME, aUsername);
2061 }
2062
2063 static nsresult NSAPI nsURI_SetUsername(nsIFileURL *iface, const nsACString *aUsername)
2064 {
2065     nsWineURI *This = impl_from_nsIFileURL(iface);
2066     const char *usera;
2067     WCHAR *user;
2068     HRESULT hres;
2069
2070     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aUsername));
2071
2072     if(!ensure_uri_builder(This))
2073         return NS_ERROR_UNEXPECTED;
2074
2075     nsACString_GetData(aUsername, &usera);
2076     user = heap_strdupUtoW(usera);
2077     if(!user)
2078         return NS_ERROR_OUT_OF_MEMORY;
2079
2080     hres = IUriBuilder_SetUserName(This->uri_builder, user);
2081     heap_free(user);
2082     if(FAILED(hres))
2083         return NS_ERROR_UNEXPECTED;
2084
2085     return NS_OK;
2086 }
2087
2088 static nsresult NSAPI nsURI_GetPassword(nsIFileURL *iface, nsACString *aPassword)
2089 {
2090     nsWineURI *This = impl_from_nsIFileURL(iface);
2091
2092     TRACE("(%p)->(%p)\n", This, aPassword);
2093
2094     return get_uri_string(This, Uri_PROPERTY_PASSWORD, aPassword);
2095 }
2096
2097 static nsresult NSAPI nsURI_SetPassword(nsIFileURL *iface, const nsACString *aPassword)
2098 {
2099     nsWineURI *This = impl_from_nsIFileURL(iface);
2100     const char *passa;
2101     WCHAR *pass;
2102     HRESULT hres;
2103
2104     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aPassword));
2105
2106     if(!ensure_uri_builder(This))
2107         return NS_ERROR_UNEXPECTED;
2108
2109     nsACString_GetData(aPassword, &passa);
2110     pass = heap_strdupUtoW(passa);
2111     if(!pass)
2112         return NS_ERROR_OUT_OF_MEMORY;
2113
2114     hres = IUriBuilder_SetPassword(This->uri_builder, pass);
2115     heap_free(pass);
2116     if(FAILED(hres))
2117         return NS_ERROR_UNEXPECTED;
2118
2119     return NS_OK;
2120 }
2121
2122 static nsresult NSAPI nsURI_GetHostPort(nsIFileURL *iface, nsACString *aHostPort)
2123 {
2124     nsWineURI *This = impl_from_nsIFileURL(iface);
2125     const WCHAR *ptr;
2126     char *vala;
2127     BSTR val;
2128     HRESULT hres;
2129
2130     TRACE("(%p)->(%p)\n", This, aHostPort);
2131
2132     if(!ensure_uri(This))
2133         return NS_ERROR_UNEXPECTED;
2134
2135     hres = IUri_GetAuthority(This->uri, &val);
2136     if(FAILED(hres)) {
2137         WARN("GetAuthority failed: %08x\n", hres);
2138         return NS_ERROR_UNEXPECTED;
2139     }
2140
2141     ptr = strchrW(val, '@');
2142     if(!ptr)
2143         ptr = val;
2144
2145     vala = heap_strdupWtoU(ptr);
2146     SysFreeString(val);
2147     if(!vala)
2148         return NS_ERROR_OUT_OF_MEMORY;
2149
2150     TRACE("ret %s\n", debugstr_a(vala));
2151     nsACString_SetData(aHostPort, vala);
2152     heap_free(vala);
2153     return NS_OK;
2154 }
2155
2156 static nsresult NSAPI nsURI_SetHostPort(nsIFileURL *iface, const nsACString *aHostPort)
2157 {
2158     nsWineURI *This = impl_from_nsIFileURL(iface);
2159
2160     WARN("(%p)->(%s)\n", This, debugstr_nsacstr(aHostPort));
2161
2162     /* Not implemented by Gecko */
2163     return NS_ERROR_NOT_IMPLEMENTED;
2164 }
2165
2166 static nsresult NSAPI nsURI_GetHost(nsIFileURL *iface, nsACString *aHost)
2167 {
2168     nsWineURI *This = impl_from_nsIFileURL(iface);
2169
2170     TRACE("(%p)->(%p)\n", This, aHost);
2171
2172     return get_uri_string(This, Uri_PROPERTY_HOST, aHost);
2173 }
2174
2175 static nsresult NSAPI nsURI_SetHost(nsIFileURL *iface, const nsACString *aHost)
2176 {
2177     nsWineURI *This = impl_from_nsIFileURL(iface);
2178     const char *hosta;
2179     WCHAR *host;
2180     HRESULT hres;
2181
2182     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aHost));
2183
2184     if(!ensure_uri_builder(This))
2185         return NS_ERROR_UNEXPECTED;
2186
2187     nsACString_GetData(aHost, &hosta);
2188     host = heap_strdupUtoW(hosta);
2189     if(!host)
2190         return NS_ERROR_OUT_OF_MEMORY;
2191
2192     hres = IUriBuilder_SetHost(This->uri_builder, host);
2193     heap_free(host);
2194     if(FAILED(hres))
2195         return NS_ERROR_UNEXPECTED;
2196
2197     return NS_OK;
2198 }
2199
2200 static nsresult NSAPI nsURI_GetPort(nsIFileURL *iface, LONG *aPort)
2201 {
2202     nsWineURI *This = impl_from_nsIFileURL(iface);
2203     DWORD port;
2204     HRESULT hres;
2205
2206     TRACE("(%p)->(%p)\n", This, aPort);
2207
2208     if(!ensure_uri(This))
2209         return NS_ERROR_UNEXPECTED;
2210
2211     hres = IUri_GetPort(This->uri, &port);
2212     if(FAILED(hres)) {
2213         WARN("GetPort failed: %08x\n", hres);
2214         return NS_ERROR_UNEXPECTED;
2215     }
2216
2217     *aPort = port ? port : -1;
2218     return NS_OK;
2219 }
2220
2221 static nsresult NSAPI nsURI_SetPort(nsIFileURL *iface, LONG aPort)
2222 {
2223     nsWineURI *This = impl_from_nsIFileURL(iface);
2224     HRESULT hres;
2225
2226     TRACE("(%p)->(%d)\n", This, aPort);
2227
2228     if(!ensure_uri_builder(This))
2229         return NS_ERROR_UNEXPECTED;
2230
2231     hres = IUriBuilder_SetPort(This->uri_builder, aPort != -1, aPort);
2232     return SUCCEEDED(hres) ? NS_OK : NS_ERROR_FAILURE;
2233 }
2234
2235 static nsresult NSAPI nsURI_GetPath(nsIFileURL *iface, nsACString *aPath)
2236 {
2237     nsWineURI *This = impl_from_nsIFileURL(iface);
2238
2239     TRACE("(%p)->(%p)\n", This, aPath);
2240
2241     return get_uri_string(This, Uri_PROPERTY_PATH, aPath);
2242 }
2243
2244 static nsresult NSAPI nsURI_SetPath(nsIFileURL *iface, const nsACString *aPath)
2245 {
2246     nsWineURI *This = impl_from_nsIFileURL(iface);
2247     const char *patha;
2248     WCHAR *path;
2249     HRESULT hres;
2250
2251     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aPath));
2252
2253     if(!ensure_uri_builder(This))
2254         return NS_ERROR_UNEXPECTED;
2255
2256     nsACString_GetData(aPath, &patha);
2257     path = heap_strdupUtoW(patha);
2258     if(!path)
2259         return NS_ERROR_OUT_OF_MEMORY;
2260
2261     hres = IUriBuilder_SetPath(This->uri_builder, path);
2262     heap_free(path);
2263     if(FAILED(hres))
2264         return NS_ERROR_UNEXPECTED;
2265
2266     return NS_OK;
2267 }
2268
2269 static nsresult NSAPI nsURI_Equals(nsIFileURL *iface, nsIURI *other, cpp_bool *_retval)
2270 {
2271     nsWineURI *This = impl_from_nsIFileURL(iface);
2272     nsWineURI *other_obj;
2273     nsresult nsres;
2274     HRESULT hres;
2275
2276     TRACE("(%p)->(%p %p)\n", This, other, _retval);
2277
2278     nsres = nsIURI_QueryInterface(other, &IID_nsWineURI, (void**)&other_obj);
2279     if(NS_FAILED(nsres)) {
2280         TRACE("Could not get nsWineURI interface\n");
2281         *_retval = FALSE;
2282         return NS_OK;
2283     }
2284
2285     if(ensure_uri(This) && ensure_uri(other_obj)) {
2286         BOOL b;
2287
2288         hres = IUri_IsEqual(This->uri, other_obj->uri, &b);
2289         if(SUCCEEDED(hres)) {
2290             *_retval = b;
2291             nsres = NS_OK;
2292         }else {
2293             nsres = NS_ERROR_FAILURE;
2294         }
2295     }else {
2296         nsres = NS_ERROR_UNEXPECTED;
2297     }
2298
2299     nsIFileURL_Release(&other_obj->nsIFileURL_iface);
2300     return nsres;
2301 }
2302
2303 static nsresult NSAPI nsURI_SchemeIs(nsIFileURL *iface, const char *scheme, cpp_bool *_retval)
2304 {
2305     nsWineURI *This = impl_from_nsIFileURL(iface);
2306     WCHAR buf[INTERNET_MAX_SCHEME_LENGTH];
2307     BSTR scheme_name;
2308     HRESULT hres;
2309
2310     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
2311
2312     if(!ensure_uri(This))
2313         return NS_ERROR_UNEXPECTED;
2314
2315     hres = IUri_GetSchemeName(This->uri, &scheme_name);
2316     if(FAILED(hres))
2317         return NS_ERROR_UNEXPECTED;
2318
2319     MultiByteToWideChar(CP_UTF8, 0, scheme, -1, buf, sizeof(buf)/sizeof(WCHAR));
2320     *_retval = !strcmpW(scheme_name, buf);
2321     SysFreeString(scheme_name);
2322     return NS_OK;
2323 }
2324
2325 static nsresult NSAPI nsURI_Clone(nsIFileURL *iface, nsIURI **_retval)
2326 {
2327     nsWineURI *This = impl_from_nsIFileURL(iface);
2328     nsWineURI *wine_uri;
2329     nsresult nsres;
2330
2331     TRACE("(%p)->(%p)\n", This, _retval);
2332
2333     if(!ensure_uri(This))
2334         return NS_ERROR_UNEXPECTED;
2335
2336     nsres = create_nsuri(This->uri, This->window_ref ? This->window_ref->window : NULL,
2337             This->container, This->origin_charset, &wine_uri);
2338     if(NS_FAILED(nsres)) {
2339         WARN("create_nsuri failed: %08x\n", nsres);
2340         return nsres;
2341     }
2342
2343     *_retval = (nsIURI*)&wine_uri->nsIFileURL_iface;
2344     return NS_OK;
2345 }
2346
2347 static nsresult NSAPI nsURI_Resolve(nsIFileURL *iface, const nsACString *aRelativePath,
2348         nsACString *_retval)
2349 {
2350     nsWineURI *This = impl_from_nsIFileURL(iface);
2351     const char *patha;
2352     IUri *new_uri;
2353     WCHAR *path;
2354     char *reta;
2355     BSTR ret;
2356     HRESULT hres;
2357
2358     TRACE("(%p)->(%s %p)\n", This, debugstr_nsacstr(aRelativePath), _retval);
2359
2360     if(!ensure_uri(This))
2361         return NS_ERROR_UNEXPECTED;
2362
2363     nsACString_GetData(aRelativePath, &patha);
2364     path = heap_strdupUtoW(patha);
2365     if(!path)
2366         return NS_ERROR_OUT_OF_MEMORY;
2367
2368     hres = CoInternetCombineUrlEx(This->uri, path, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO, &new_uri, 0);
2369     heap_free(path);
2370     if(FAILED(hres)) {
2371         ERR("CoIntenetCombineUrlEx failed: %08x\n", hres);
2372         return NS_ERROR_FAILURE;
2373     }
2374
2375     hres = IUri_GetDisplayUri(new_uri, &ret);
2376     IUri_Release(new_uri);
2377     if(FAILED(hres))
2378         return NS_ERROR_FAILURE;
2379
2380     reta = heap_strdupWtoU(ret);
2381     SysFreeString(ret);
2382     if(!reta)
2383         return NS_ERROR_OUT_OF_MEMORY;
2384
2385     TRACE("returning %s\n", debugstr_a(reta));
2386     nsACString_SetData(_retval, reta);
2387     heap_free(reta);
2388     return NS_OK;
2389 }
2390
2391 static nsresult NSAPI nsURI_GetAsciiSpec(nsIFileURL *iface, nsACString *aAsciiSpec)
2392 {
2393     nsWineURI *This = impl_from_nsIFileURL(iface);
2394
2395     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
2396
2397     return nsIFileURL_GetSpec(&This->nsIFileURL_iface, aAsciiSpec);
2398 }
2399
2400 static nsresult NSAPI nsURI_GetAsciiHost(nsIFileURL *iface, nsACString *aAsciiHost)
2401 {
2402     nsWineURI *This = impl_from_nsIFileURL(iface);
2403
2404     WARN("(%p)->(%p) FIXME: Use Uri_PUNYCODE_IDN_HOST flag\n", This, aAsciiHost);
2405
2406     return get_uri_string(This, Uri_PROPERTY_HOST, aAsciiHost);
2407 }
2408
2409 static nsresult NSAPI nsURI_GetOriginCharset(nsIFileURL *iface, nsACString *aOriginCharset)
2410 {
2411     nsWineURI *This = impl_from_nsIFileURL(iface);
2412
2413     TRACE("(%p)->(%p)\n", This, aOriginCharset);
2414
2415     nsACString_SetData(aOriginCharset, This->origin_charset);
2416     return NS_OK;
2417 }
2418
2419 static nsresult NSAPI nsURL_GetRef(nsIFileURL *iface, nsACString *aRef)
2420 {
2421     nsWineURI *This = impl_from_nsIFileURL(iface);
2422     char *refa = NULL;
2423     BSTR ref;
2424     HRESULT hres;
2425
2426     TRACE("(%p)->(%p)\n", This, aRef);
2427
2428     if(!ensure_uri(This))
2429         return NS_ERROR_UNEXPECTED;
2430
2431     hres = IUri_GetFragment(This->uri, &ref);
2432     if(FAILED(hres))
2433         return NS_ERROR_UNEXPECTED;
2434
2435     refa = heap_strdupWtoU(ref);
2436     SysFreeString(ref);
2437     if(ref && !refa)
2438         return NS_ERROR_OUT_OF_MEMORY;
2439
2440     nsACString_SetData(aRef, refa && *refa == '#' ? refa+1 : refa);
2441     heap_free(refa);
2442     return NS_OK;
2443 }
2444
2445 static nsresult NSAPI nsURL_SetRef(nsIFileURL *iface, const nsACString *aRef)
2446 {
2447     nsWineURI *This = impl_from_nsIFileURL(iface);
2448     const char *refa;
2449     WCHAR *ref;
2450     HRESULT hres;
2451
2452     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aRef));
2453
2454     if(!ensure_uri_builder(This))
2455         return NS_ERROR_UNEXPECTED;
2456
2457     nsACString_GetData(aRef, &refa);
2458     ref = heap_strdupUtoW(refa);
2459     if(!ref)
2460         return NS_ERROR_OUT_OF_MEMORY;
2461
2462     hres = IUriBuilder_SetFragment(This->uri_builder, ref);
2463     heap_free(ref);
2464     if(FAILED(hres))
2465         return NS_ERROR_UNEXPECTED;
2466
2467     return NS_OK;
2468 }
2469
2470 static nsresult NSAPI nsURI_EqualsExceptRef(nsIFileURL *iface, nsIURI *other, cpp_bool *_retval)
2471 {
2472     nsWineURI *This = impl_from_nsIFileURL(iface);
2473     nsWineURI *other_obj;
2474     nsresult nsres;
2475
2476     TRACE("(%p)->(%p %p)\n", This, other, _retval);
2477
2478     nsres = nsIURI_QueryInterface(other, &IID_nsWineURI, (void**)&other_obj);
2479     if(NS_FAILED(nsres)) {
2480         TRACE("Could not get nsWineURI interface\n");
2481         *_retval = FALSE;
2482         return NS_OK;
2483     }
2484
2485     if(ensure_uri(This) && ensure_uri(other_obj)) {
2486         *_retval = compare_ignoring_frag(This->uri, other_obj->uri);
2487         nsres = NS_OK;
2488     }else {
2489         nsres = NS_ERROR_UNEXPECTED;
2490     }
2491
2492     nsIFileURL_Release(&other_obj->nsIFileURL_iface);
2493     return nsres;
2494 }
2495
2496 static nsresult NSAPI nsURI_CloneIgnoreRef(nsIFileURL *iface, nsIURI **_retval)
2497 {
2498     nsWineURI *This = impl_from_nsIFileURL(iface);
2499     nsWineURI *wine_uri;
2500     IUri *uri;
2501     nsresult nsres;
2502
2503     TRACE("(%p)->(%p)\n", This, _retval);
2504
2505     if(!ensure_uri(This))
2506         return NS_ERROR_UNEXPECTED;
2507
2508     uri = get_uri_nofrag(This->uri);
2509     if(!uri)
2510         return NS_ERROR_FAILURE;
2511
2512     nsres = create_nsuri(uri, This->window_ref ? This->window_ref->window : NULL, This->container,
2513             This->origin_charset, &wine_uri);
2514     IUri_Release(uri);
2515     if(NS_FAILED(nsres)) {
2516         WARN("create_nsuri failed: %08x\n", nsres);
2517         return nsres;
2518     }
2519
2520     *_retval = (nsIURI*)&wine_uri->nsIFileURL_iface;
2521     return NS_OK;
2522 }
2523
2524 static nsresult NSAPI nsURI_GetSpecIgnoringRef(nsIFileURL *iface, nsACString *aSpecIgnoringRef)
2525 {
2526     nsWineURI *This = impl_from_nsIFileURL(iface);
2527
2528     FIXME("(%p)->(%p)\n", This, aSpecIgnoringRef);
2529
2530     return nsIFileURL_GetSpec(&This->nsIFileURL_iface, aSpecIgnoringRef);
2531 }
2532
2533 static nsresult NSAPI nsURI_GetHasRef(nsIFileURL *iface, cpp_bool *aHasRef)
2534 {
2535     nsWineURI *This = impl_from_nsIFileURL(iface);
2536     BOOL b;
2537     HRESULT hres;
2538
2539     TRACE("(%p)->(%p)\n", This, aHasRef);
2540
2541     if(!ensure_uri(This))
2542         return NS_ERROR_UNEXPECTED;
2543
2544     hres = IUri_HasProperty(This->uri, Uri_PROPERTY_FRAGMENT, &b);
2545     if(FAILED(hres))
2546         return NS_ERROR_FAILURE;
2547
2548     *aHasRef = b;
2549     return NS_OK;
2550 }
2551
2552 static nsresult NSAPI nsURL_GetFilePath(nsIFileURL *iface, nsACString *aFilePath)
2553 {
2554     nsWineURI *This = impl_from_nsIFileURL(iface);
2555
2556     TRACE("(%p)->(%p)\n", This, aFilePath);
2557
2558     return nsIFileURL_GetPath(&This->nsIFileURL_iface, aFilePath);
2559 }
2560
2561 static nsresult NSAPI nsURL_SetFilePath(nsIFileURL *iface, const nsACString *aFilePath)
2562 {
2563     nsWineURI *This = impl_from_nsIFileURL(iface);
2564
2565     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aFilePath));
2566
2567     if(!This->is_mutable)
2568         return NS_ERROR_UNEXPECTED;
2569
2570     return nsIFileURL_SetPath(&This->nsIFileURL_iface, aFilePath);
2571 }
2572
2573 static nsresult NSAPI nsURL_GetQuery(nsIFileURL *iface, nsACString *aQuery)
2574 {
2575     nsWineURI *This = impl_from_nsIFileURL(iface);
2576
2577     TRACE("(%p)->(%p)\n", This, aQuery);
2578
2579     return get_uri_string(This, Uri_PROPERTY_QUERY, aQuery);
2580 }
2581
2582 static nsresult NSAPI nsURL_SetQuery(nsIFileURL *iface, const nsACString *aQuery)
2583 {
2584     nsWineURI *This = impl_from_nsIFileURL(iface);
2585     const char *querya;
2586     WCHAR *query;
2587     HRESULT hres;
2588
2589     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aQuery));
2590
2591     if(!ensure_uri_builder(This))
2592         return NS_ERROR_UNEXPECTED;
2593
2594     nsACString_GetData(aQuery, &querya);
2595     query = heap_strdupUtoW(querya);
2596     if(!query)
2597         return NS_ERROR_OUT_OF_MEMORY;
2598
2599     hres = IUriBuilder_SetQuery(This->uri_builder, query);
2600     heap_free(query);
2601     if(FAILED(hres))
2602         return NS_ERROR_UNEXPECTED;
2603
2604     return NS_OK;
2605 }
2606
2607 static nsresult get_uri_path(nsWineURI *This, BSTR *path, const WCHAR **file, const WCHAR **ext)
2608 {
2609     const WCHAR *ptr;
2610     HRESULT hres;
2611
2612     if(!ensure_uri(This))
2613         return NS_ERROR_UNEXPECTED;
2614
2615     hres = IUri_GetPath(This->uri, path);
2616     if(FAILED(hres))
2617         return NS_ERROR_FAILURE;
2618
2619     for(ptr = *path + SysStringLen(*path)-1; ptr > *path && *ptr != '/' && *ptr != '\\'; ptr--);
2620     if(*ptr == '/' || *ptr == '\\')
2621         ptr++;
2622     *file = ptr;
2623
2624     if(ext) {
2625         ptr = strrchrW(ptr, '.');
2626         if(!ptr)
2627             ptr = *path + SysStringLen(*path);
2628         *ext = ptr;
2629     }
2630
2631     return NS_OK;
2632 }
2633
2634 static nsresult NSAPI nsURL_GetDirectory(nsIFileURL *iface, nsACString *aDirectory)
2635 {
2636     nsWineURI *This = impl_from_nsIFileURL(iface);
2637     const WCHAR *file;
2638     BSTR path;
2639     nsresult nsres;
2640
2641     TRACE("(%p)->(%p)\n", This, aDirectory);
2642
2643     nsres = get_uri_path(This, &path, &file, NULL);
2644     if(NS_FAILED(nsres))
2645         return nsres;
2646
2647     nsres = return_wstr_nsacstr(aDirectory, path, file-path);
2648     SysFreeString(path);
2649     return nsres;
2650 }
2651
2652 static nsresult NSAPI nsURL_SetDirectory(nsIFileURL *iface, const nsACString *aDirectory)
2653 {
2654     nsWineURI *This = impl_from_nsIFileURL(iface);
2655
2656     WARN("(%p)->(%s)\n", This, debugstr_nsacstr(aDirectory));
2657
2658     /* Not implemented by Gecko */
2659     return NS_ERROR_NOT_IMPLEMENTED;
2660 }
2661
2662 static nsresult NSAPI nsURL_GetFileName(nsIFileURL *iface, nsACString *aFileName)
2663 {
2664     nsWineURI *This = impl_from_nsIFileURL(iface);
2665     const WCHAR *file;
2666     BSTR path;
2667     nsresult nsres;
2668
2669     TRACE("(%p)->(%p)\n", This, aFileName);
2670
2671     nsres = get_uri_path(This, &path, &file, NULL);
2672     if(NS_FAILED(nsres))
2673         return nsres;
2674
2675     nsres = return_wstr_nsacstr(aFileName, file, -1);
2676     SysFreeString(path);
2677     return nsres;
2678 }
2679
2680 static nsresult NSAPI nsURL_SetFileName(nsIFileURL *iface, const nsACString *aFileName)
2681 {
2682     nsWineURI *This = impl_from_nsIFileURL(iface);
2683     FIXME("(%p)->(%s)\n", This, debugstr_nsacstr(aFileName));
2684     return NS_ERROR_NOT_IMPLEMENTED;
2685 }
2686
2687 static nsresult NSAPI nsURL_GetFileBaseName(nsIFileURL *iface, nsACString *aFileBaseName)
2688 {
2689     nsWineURI *This = impl_from_nsIFileURL(iface);
2690     const WCHAR *file, *ext;
2691     BSTR path;
2692     nsresult nsres;
2693
2694     TRACE("(%p)->(%p)\n", This, aFileBaseName);
2695
2696     nsres = get_uri_path(This, &path, &file, &ext);
2697     if(NS_FAILED(nsres))
2698         return nsres;
2699
2700     nsres = return_wstr_nsacstr(aFileBaseName, file, ext-file);
2701     SysFreeString(path);
2702     return nsres;
2703 }
2704
2705 static nsresult NSAPI nsURL_SetFileBaseName(nsIFileURL *iface, const nsACString *aFileBaseName)
2706 {
2707     nsWineURI *This = impl_from_nsIFileURL(iface);
2708     FIXME("(%p)->(%s)\n", This, debugstr_nsacstr(aFileBaseName));
2709     return NS_ERROR_NOT_IMPLEMENTED;
2710 }
2711
2712 static nsresult NSAPI nsURL_GetFileExtension(nsIFileURL *iface, nsACString *aFileExtension)
2713 {
2714     nsWineURI *This = impl_from_nsIFileURL(iface);
2715
2716     TRACE("(%p)->(%p)\n", This, aFileExtension);
2717
2718     return get_uri_string(This, Uri_PROPERTY_EXTENSION, aFileExtension);
2719 }
2720
2721 static nsresult NSAPI nsURL_SetFileExtension(nsIFileURL *iface, const nsACString *aFileExtension)
2722 {
2723     nsWineURI *This = impl_from_nsIFileURL(iface);
2724     FIXME("(%p)->(%s)\n", This, debugstr_nsacstr(aFileExtension));
2725     return NS_ERROR_NOT_IMPLEMENTED;
2726 }
2727
2728 static nsresult NSAPI nsURL_GetCommonBaseSpec(nsIFileURL *iface, nsIURI *aURIToCompare, nsACString *_retval)
2729 {
2730     nsWineURI *This = impl_from_nsIFileURL(iface);
2731     FIXME("(%p)->(%p %p)\n", This, aURIToCompare, _retval);
2732     return NS_ERROR_NOT_IMPLEMENTED;
2733 }
2734
2735 static nsresult NSAPI nsURL_GetRelativeSpec(nsIFileURL *iface, nsIURI *aURIToCompare, nsACString *_retval)
2736 {
2737     nsWineURI *This = impl_from_nsIFileURL(iface);
2738     FIXME("(%p)->(%p %p)\n", This, aURIToCompare, _retval);
2739     return NS_ERROR_NOT_IMPLEMENTED;
2740 }
2741
2742 static nsresult NSAPI nsFileURL_GetFile(nsIFileURL *iface, nsIFile **aFile)
2743 {
2744     nsWineURI *This = impl_from_nsIFileURL(iface);
2745     WCHAR path[MAX_PATH];
2746     DWORD size;
2747     HRESULT hres;
2748
2749     TRACE("(%p)->(%p)\n", This, aFile);
2750
2751     hres = CoInternetParseIUri(This->uri, PARSE_PATH_FROM_URL, 0, path, sizeof(path)/sizeof(WCHAR), &size, 0);
2752     if(FAILED(hres)) {
2753         WARN("CoInternetParseIUri failed: %08x\n", hres);
2754         return NS_ERROR_FAILURE;
2755     }
2756
2757     return create_nsfile(path, aFile);
2758 }
2759
2760 static nsresult NSAPI nsFileURL_SetFile(nsIFileURL *iface, nsIFile *aFile)
2761 {
2762     nsWineURI *This = impl_from_nsIFileURL(iface);
2763     FIXME("(%p)->(%p)\n", This, aFile);
2764     return NS_ERROR_NOT_IMPLEMENTED;
2765 }
2766
2767 static const nsIFileURLVtbl nsFileURLVtbl = {
2768     nsURI_QueryInterface,
2769     nsURI_AddRef,
2770     nsURI_Release,
2771     nsURI_GetSpec,
2772     nsURI_SetSpec,
2773     nsURI_GetPrePath,
2774     nsURI_GetScheme,
2775     nsURI_SetScheme,
2776     nsURI_GetUserPass,
2777     nsURI_SetUserPass,
2778     nsURI_GetUsername,
2779     nsURI_SetUsername,
2780     nsURI_GetPassword,
2781     nsURI_SetPassword,
2782     nsURI_GetHostPort,
2783     nsURI_SetHostPort,
2784     nsURI_GetHost,
2785     nsURI_SetHost,
2786     nsURI_GetPort,
2787     nsURI_SetPort,
2788     nsURI_GetPath,
2789     nsURI_SetPath,
2790     nsURI_Equals,
2791     nsURI_SchemeIs,
2792     nsURI_Clone,
2793     nsURI_Resolve,
2794     nsURI_GetAsciiSpec,
2795     nsURI_GetAsciiHost,
2796     nsURI_GetOriginCharset,
2797     nsURL_GetRef,
2798     nsURL_SetRef,
2799     nsURI_EqualsExceptRef,
2800     nsURI_CloneIgnoreRef,
2801     nsURI_GetSpecIgnoringRef,
2802     nsURI_GetHasRef,
2803     nsURL_GetFilePath,
2804     nsURL_SetFilePath,
2805     nsURL_GetQuery,
2806     nsURL_SetQuery,
2807     nsURL_GetDirectory,
2808     nsURL_SetDirectory,
2809     nsURL_GetFileName,
2810     nsURL_SetFileName,
2811     nsURL_GetFileBaseName,
2812     nsURL_SetFileBaseName,
2813     nsURL_GetFileExtension,
2814     nsURL_SetFileExtension,
2815     nsURL_GetCommonBaseSpec,
2816     nsURL_GetRelativeSpec,
2817     nsFileURL_GetFile,
2818     nsFileURL_SetFile
2819 };
2820
2821 static inline nsWineURI *impl_from_nsIStandardURL(nsIStandardURL *iface)
2822 {
2823     return CONTAINING_RECORD(iface, nsWineURI, nsIStandardURL_iface);
2824 }
2825
2826 static nsresult NSAPI nsStandardURL_QueryInterface(nsIStandardURL *iface, nsIIDRef riid,
2827         void **result)
2828 {
2829     nsWineURI *This = impl_from_nsIStandardURL(iface);
2830     return nsIFileURL_QueryInterface(&This->nsIFileURL_iface, riid, result);
2831 }
2832
2833 static nsrefcnt NSAPI nsStandardURL_AddRef(nsIStandardURL *iface)
2834 {
2835     nsWineURI *This = impl_from_nsIStandardURL(iface);
2836     return nsIFileURL_AddRef(&This->nsIFileURL_iface);
2837 }
2838
2839 static nsrefcnt NSAPI nsStandardURL_Release(nsIStandardURL *iface)
2840 {
2841     nsWineURI *This = impl_from_nsIStandardURL(iface);
2842     return nsIFileURL_Release(&This->nsIFileURL_iface);
2843 }
2844
2845 static nsresult NSAPI nsStandardURL_GetMutable(nsIStandardURL *iface, cpp_bool *aMutable)
2846 {
2847     nsWineURI *This = impl_from_nsIStandardURL(iface);
2848
2849     TRACE("(%p)->(%p)\n", This, aMutable);
2850
2851     *aMutable = This->is_mutable;
2852     return NS_OK;
2853 }
2854
2855 static nsresult NSAPI nsStandardURL_SetMutable(nsIStandardURL *iface, cpp_bool aMutable)
2856 {
2857     nsWineURI *This = impl_from_nsIStandardURL(iface);
2858
2859     TRACE("(%p)->(%x)\n", This, aMutable);
2860
2861     This->is_mutable = aMutable;
2862     return NS_OK;
2863 }
2864
2865 static nsresult NSAPI nsStandardURL_Init(nsIStandardURL *iface, UINT32 aUrlType, LONG aDefaultPort,
2866         const nsACString *aSpec, const char *aOriginCharset, nsIURI *aBaseURI)
2867 {
2868     nsWineURI *This = impl_from_nsIStandardURL(iface);
2869     FIXME("(%p)->(%d %d %s %s %p)\n", This, aUrlType, aDefaultPort, debugstr_nsacstr(aSpec), debugstr_a(aOriginCharset), aBaseURI);
2870     return NS_ERROR_NOT_IMPLEMENTED;
2871 }
2872
2873 static const nsIStandardURLVtbl nsStandardURLVtbl = {
2874     nsStandardURL_QueryInterface,
2875     nsStandardURL_AddRef,
2876     nsStandardURL_Release,
2877     nsStandardURL_GetMutable,
2878     nsStandardURL_SetMutable,
2879     nsStandardURL_Init
2880 };
2881
2882 static nsresult create_nsuri(IUri *iuri, HTMLOuterWindow *window, NSContainer *container,
2883         const char *origin_charset, nsWineURI **_retval)
2884 {
2885     nsWineURI *ret;
2886     HRESULT hres;
2887
2888     ret = heap_alloc_zero(sizeof(nsWineURI));
2889     if(!ret)
2890         return NS_ERROR_OUT_OF_MEMORY;
2891
2892     ret->nsIFileURL_iface.lpVtbl = &nsFileURLVtbl;
2893     ret->nsIStandardURL_iface.lpVtbl = &nsStandardURLVtbl;
2894     ret->ref = 1;
2895     ret->is_mutable = TRUE;
2896
2897     set_uri_nscontainer(ret, container);
2898     set_uri_window(ret, window);
2899
2900     IUri_AddRef(iuri);
2901     ret->uri = iuri;
2902
2903     hres = IUri_GetScheme(iuri, &ret->scheme);
2904     if(FAILED(hres))
2905         ret->scheme = URL_SCHEME_UNKNOWN;
2906
2907     if(origin_charset && *origin_charset && strcmp(origin_charset, "UTF-8")) {
2908         ret->origin_charset = heap_strdupA(origin_charset);
2909         if(!ret->origin_charset) {
2910             nsIFileURL_Release(&ret->nsIFileURL_iface);
2911             return NS_ERROR_OUT_OF_MEMORY;
2912         }
2913     }
2914
2915     TRACE("retval=%p\n", ret);
2916     *_retval = ret;
2917     return NS_OK;
2918 }
2919
2920 HRESULT create_doc_uri(HTMLOuterWindow *window, const WCHAR *url, nsWineURI **ret)
2921 {
2922     nsWineURI *uri;
2923     IUri *iuri;
2924     nsresult nsres;
2925     HRESULT hres;
2926
2927     hres = CreateUri(url, 0, 0, &iuri);
2928     if(FAILED(hres))
2929         return hres;
2930
2931     nsres = create_nsuri(iuri, window, window->doc_obj->nscontainer, NULL, &uri);
2932     IUri_Release(iuri);
2933     if(NS_FAILED(nsres))
2934         return E_FAIL;
2935
2936     uri->is_doc_uri = TRUE;
2937
2938     *ret = uri;
2939     return S_OK;
2940 }
2941
2942 static nsresult create_nschannel(nsWineURI *uri, nsChannel **ret)
2943 {
2944     nsChannel *channel;
2945
2946     if(!ensure_uri(uri))
2947         return NS_ERROR_UNEXPECTED;
2948
2949     channel = heap_alloc_zero(sizeof(nsChannel));
2950     if(!channel)
2951         return NS_ERROR_OUT_OF_MEMORY;
2952
2953     channel->nsIHttpChannel_iface.lpVtbl = &nsChannelVtbl;
2954     channel->nsIUploadChannel_iface.lpVtbl = &nsUploadChannelVtbl;
2955     channel->nsIHttpChannelInternal_iface.lpVtbl = &nsHttpChannelInternalVtbl;
2956     channel->ref = 1;
2957     channel->request_method = METHOD_GET;
2958     list_init(&channel->response_headers);
2959     list_init(&channel->request_headers);
2960
2961     nsIFileURL_AddRef(&uri->nsIFileURL_iface);
2962     channel->uri = uri;
2963
2964     *ret = channel;
2965     return NS_OK;
2966 }
2967
2968 HRESULT create_redirect_nschannel(const WCHAR *url, nsChannel *orig_channel, nsChannel **ret)
2969 {
2970     HTMLOuterWindow *window = NULL;
2971     nsChannel *channel;
2972     nsWineURI *uri;
2973     IUri *iuri;
2974     nsresult nsres;
2975     HRESULT hres;
2976
2977     hres = CreateUri(url, 0, 0, &iuri);
2978     if(FAILED(hres))
2979         return hres;
2980
2981     if(orig_channel->uri->window_ref)
2982         window = orig_channel->uri->window_ref->window;
2983     nsres = create_nsuri(iuri, window, NULL, NULL, &uri);
2984     IUri_Release(iuri);
2985     if(NS_FAILED(nsres))
2986         return E_FAIL;
2987
2988     nsres = create_nschannel(uri, &channel);
2989     nsIFileURL_Release(&uri->nsIFileURL_iface);
2990     if(NS_FAILED(nsres))
2991         return E_FAIL;
2992
2993     if(orig_channel->load_group) {
2994         nsILoadGroup_AddRef(orig_channel->load_group);
2995         channel->load_group = orig_channel->load_group;
2996     }
2997
2998     if(orig_channel->notif_callback) {
2999         nsIInterfaceRequestor_AddRef(orig_channel->notif_callback);
3000         channel->notif_callback = orig_channel->notif_callback;
3001     }
3002
3003     channel->load_flags = orig_channel->load_flags | LOAD_REPLACE;
3004
3005     if(orig_channel->request_method == METHOD_POST)
3006         FIXME("unsupported POST method\n");
3007
3008     if(orig_channel->original_uri) {
3009         nsIURI_AddRef(orig_channel->original_uri);
3010         channel->original_uri = orig_channel->original_uri;
3011     }
3012
3013     if(orig_channel->referrer) {
3014         nsIURI_AddRef(orig_channel->referrer);
3015         channel->referrer = orig_channel->referrer;
3016     }
3017
3018     *ret = channel;
3019     return S_OK;
3020 }
3021
3022 typedef struct {
3023     nsIProtocolHandler nsIProtocolHandler_iface;
3024
3025     LONG ref;
3026
3027     nsIProtocolHandler *nshandler;
3028 } nsProtocolHandler;
3029
3030 static inline nsProtocolHandler *impl_from_nsIProtocolHandler(nsIProtocolHandler *iface)
3031 {
3032     return CONTAINING_RECORD(iface, nsProtocolHandler, nsIProtocolHandler_iface);
3033 }
3034
3035 static nsresult NSAPI nsProtocolHandler_QueryInterface(nsIProtocolHandler *iface, nsIIDRef riid,
3036         void **result)
3037 {
3038     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3039
3040     *result = NULL;
3041
3042     if(IsEqualGUID(&IID_nsISupports, riid)) {
3043         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
3044         *result = &This->nsIProtocolHandler_iface;
3045     }else if(IsEqualGUID(&IID_nsIProtocolHandler, riid)) {
3046         TRACE("(%p)->(IID_nsIProtocolHandler %p)\n", This, result);
3047         *result = &This->nsIProtocolHandler_iface;
3048     }else if(IsEqualGUID(&IID_nsIExternalProtocolHandler, riid)) {
3049         TRACE("(%p)->(IID_nsIExternalProtocolHandler %p), returning NULL\n", This, result);
3050         return NS_NOINTERFACE;
3051     }
3052
3053     if(*result) {
3054         nsISupports_AddRef((nsISupports*)*result);
3055         return NS_OK;
3056     }
3057
3058     WARN("(%s %p)\n", debugstr_guid(riid), result);
3059     return NS_NOINTERFACE;
3060 }
3061
3062 static nsrefcnt NSAPI nsProtocolHandler_AddRef(nsIProtocolHandler *iface)
3063 {
3064     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3065     LONG ref = InterlockedIncrement(&This->ref);
3066
3067     TRACE("(%p) ref=%d\n", This, ref);
3068
3069     return ref;
3070 }
3071
3072 static nsrefcnt NSAPI nsProtocolHandler_Release(nsIProtocolHandler *iface)
3073 {
3074     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3075     LONG ref = InterlockedDecrement(&This->ref);
3076
3077     TRACE("(%p) ref=%d\n", This, ref);
3078
3079     if(!ref) {
3080         if(This->nshandler)
3081             nsIProtocolHandler_Release(This->nshandler);
3082         heap_free(This);
3083     }
3084
3085     return ref;
3086 }
3087
3088 static nsresult NSAPI nsProtocolHandler_GetScheme(nsIProtocolHandler *iface, nsACString *aScheme)
3089 {
3090     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3091
3092     TRACE("(%p)->(%p)\n", This, aScheme);
3093
3094     if(This->nshandler)
3095         return nsIProtocolHandler_GetScheme(This->nshandler, aScheme);
3096     return NS_ERROR_NOT_IMPLEMENTED;
3097 }
3098
3099 static nsresult NSAPI nsProtocolHandler_GetDefaultPort(nsIProtocolHandler *iface,
3100         LONG *aDefaultPort)
3101 {
3102     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3103
3104     TRACE("(%p)->(%p)\n", This, aDefaultPort);
3105
3106     if(This->nshandler)
3107         return nsIProtocolHandler_GetDefaultPort(This->nshandler, aDefaultPort);
3108     return NS_ERROR_NOT_IMPLEMENTED;
3109 }
3110
3111 static nsresult NSAPI nsProtocolHandler_GetProtocolFlags(nsIProtocolHandler *iface,
3112                                                          UINT32 *aProtocolFlags)
3113 {
3114     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3115
3116     TRACE("(%p)->(%p)\n", This, aProtocolFlags);
3117
3118     if(This->nshandler)
3119         return nsIProtocolHandler_GetProtocolFlags(This->nshandler, aProtocolFlags);
3120     return NS_ERROR_NOT_IMPLEMENTED;
3121 }
3122
3123 static nsresult NSAPI nsProtocolHandler_NewURI(nsIProtocolHandler *iface,
3124         const nsACString *aSpec, const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
3125 {
3126     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3127
3128     TRACE("((%p)->%s %s %p %p)\n", This, debugstr_nsacstr(aSpec), debugstr_a(aOriginCharset),
3129           aBaseURI, _retval);
3130
3131     if(This->nshandler)
3132         return nsIProtocolHandler_NewURI(This->nshandler, aSpec, aOriginCharset, aBaseURI, _retval);
3133     return NS_ERROR_NOT_IMPLEMENTED;
3134 }
3135
3136 static nsresult NSAPI nsProtocolHandler_NewChannel(nsIProtocolHandler *iface,
3137         nsIURI *aURI, nsIChannel **_retval)
3138 {
3139     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3140
3141     TRACE("(%p)->(%p %p)\n", This, aURI, _retval);
3142
3143     if(This->nshandler)
3144         return nsIProtocolHandler_NewChannel(This->nshandler, aURI, _retval);
3145     return NS_ERROR_NOT_IMPLEMENTED;
3146 }
3147
3148 static nsresult NSAPI nsProtocolHandler_AllowPort(nsIProtocolHandler *iface,
3149         LONG port, const char *scheme, cpp_bool *_retval)
3150 {
3151     nsProtocolHandler *This = impl_from_nsIProtocolHandler(iface);
3152
3153     TRACE("(%p)->(%d %s %p)\n", This, port, debugstr_a(scheme), _retval);
3154
3155     if(This->nshandler)
3156         return nsIProtocolHandler_AllowPort(This->nshandler, port, scheme, _retval);
3157     return NS_ERROR_NOT_IMPLEMENTED;
3158 }
3159
3160 static const nsIProtocolHandlerVtbl nsProtocolHandlerVtbl = {
3161     nsProtocolHandler_QueryInterface,
3162     nsProtocolHandler_AddRef,
3163     nsProtocolHandler_Release,
3164     nsProtocolHandler_GetScheme,
3165     nsProtocolHandler_GetDefaultPort,
3166     nsProtocolHandler_GetProtocolFlags,
3167     nsProtocolHandler_NewURI,
3168     nsProtocolHandler_NewChannel,
3169     nsProtocolHandler_AllowPort
3170 };
3171
3172 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService*,nsIIDRef,void**);
3173
3174 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
3175 {
3176     return 2;
3177 }
3178
3179 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
3180 {
3181     return 1;
3182 }
3183
3184 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
3185                                                      nsIProtocolHandler **_retval)
3186 {
3187     nsIExternalProtocolHandler *nsexthandler;
3188     nsIProtocolHandler *nshandler;
3189     nsProtocolHandler *ret;
3190     nsresult nsres;
3191
3192     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
3193
3194     nsres = nsIIOService_GetProtocolHandler(nsio, aScheme, &nshandler);
3195     if(NS_FAILED(nsres)) {
3196         WARN("GetProtocolHandler failed: %08x\n", nsres);
3197         return nsres;
3198     }
3199
3200     nsres = nsIProtocolHandler_QueryInterface(nshandler, &IID_nsIExternalProtocolHandler,
3201                                               (void**)&nsexthandler);
3202     if(NS_FAILED(nsres)) {
3203         *_retval = nshandler;
3204         return NS_OK;
3205     }
3206
3207     nsIExternalProtocolHandler_Release(nsexthandler);
3208
3209     ret = heap_alloc(sizeof(nsProtocolHandler));
3210     if(!ret)
3211         return NS_ERROR_OUT_OF_MEMORY;
3212
3213     ret->nsIProtocolHandler_iface.lpVtbl = &nsProtocolHandlerVtbl;
3214     ret->ref = 1;
3215     ret->nshandler = nshandler;
3216     *_retval = &ret->nsIProtocolHandler_iface;
3217
3218     TRACE("return %p\n", *_retval);
3219     return NS_OK;
3220 }
3221
3222 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
3223                                                     UINT32 *_retval)
3224 {
3225     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
3226     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
3227 }
3228
3229 static BOOL is_gecko_special_uri(const char *spec)
3230 {
3231     static const char *special_schemes[] = {"chrome:", "jar:", "moz-safe-about", "resource:", "javascript:", "wyciwyg:"};
3232     int i;
3233
3234     for(i=0; i < sizeof(special_schemes)/sizeof(*special_schemes); i++) {
3235         if(!strncasecmp(spec, special_schemes[i], strlen(special_schemes[i])))
3236             return TRUE;
3237     }
3238
3239     if(!strncasecmp(spec, "file:", 5)) {
3240         const char *ptr = spec+5;
3241         while(*ptr == '/')
3242             ptr++;
3243         return is_gecko_path(ptr);
3244     }
3245
3246     return FALSE;
3247 }
3248
3249 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
3250         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
3251 {
3252     nsWineURI *wine_uri, *base_wine_uri = NULL;
3253     WCHAR new_spec[INTERNET_MAX_URL_LENGTH];
3254     HTMLOuterWindow *window = NULL;
3255     const char *spec = NULL;
3256     IUri *urlmon_uri;
3257     nsresult nsres;
3258     HRESULT hres;
3259
3260     TRACE("(%s %s %p %p)\n", debugstr_nsacstr(aSpec), debugstr_a(aOriginCharset),
3261           aBaseURI, _retval);
3262
3263     nsACString_GetData(aSpec, &spec);
3264     if(is_gecko_special_uri(spec))
3265         return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
3266
3267     if(!strncmp(spec, "wine:", 5))
3268         spec += 5;
3269
3270     if(aBaseURI) {
3271         nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsWineURI, (void**)&base_wine_uri);
3272         if(NS_SUCCEEDED(nsres)) {
3273             if(!ensure_uri(base_wine_uri))
3274                 return NS_ERROR_UNEXPECTED;
3275             if(base_wine_uri->window_ref)
3276                 window = base_wine_uri->window_ref->window;
3277         }else {
3278             WARN("Could not get base nsWineURI: %08x\n", nsres);
3279         }
3280     }
3281
3282     MultiByteToWideChar(CP_ACP, 0, spec, -1, new_spec, sizeof(new_spec)/sizeof(WCHAR));
3283
3284     if(base_wine_uri) {
3285         hres = CoInternetCombineUrlEx(base_wine_uri->uri, new_spec, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
3286                 &urlmon_uri, 0);
3287         if(FAILED(hres))
3288             WARN("CoInternetCombineUrlEx failed: %08x\n", hres);
3289     }else {
3290         hres = CreateUri(new_spec, 0, 0, &urlmon_uri);
3291         if(FAILED(hres))
3292             WARN("CreateUri failed: %08x\n", hres);
3293     }
3294
3295     if(FAILED(hres))
3296         return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
3297
3298     nsres = create_nsuri(urlmon_uri, window, NULL, NULL, &wine_uri);
3299     IUri_Release(urlmon_uri);
3300     if(base_wine_uri)
3301         nsIFileURL_Release(&base_wine_uri->nsIFileURL_iface);
3302     if(NS_FAILED(nsres))
3303         return nsres;
3304
3305     *_retval = (nsIURI*)&wine_uri->nsIFileURL_iface;
3306     return nsres;
3307 }
3308
3309 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
3310                                              nsIURI **_retval)
3311 {
3312     TRACE("(%p %p)\n", aFile, _retval);
3313     return nsIIOService_NewFileURI(nsio, aFile, _retval);
3314 }
3315
3316 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
3317                                                      nsIChannel **_retval)
3318 {
3319     nsWineURI *wine_uri;
3320     nsChannel *ret;
3321     nsresult nsres;
3322
3323     TRACE("(%p %p)\n", aURI, _retval);
3324
3325     nsres = nsIURI_QueryInterface(aURI, &IID_nsWineURI, (void**)&wine_uri);
3326     if(NS_FAILED(nsres)) {
3327         TRACE("Could not get nsWineURI: %08x\n", nsres);
3328         return nsIIOService_NewChannelFromURI(nsio, aURI, _retval);
3329     }
3330
3331     nsres = create_nschannel(wine_uri, &ret);
3332     nsIFileURL_Release(&wine_uri->nsIFileURL_iface);
3333     if(NS_FAILED(nsres))
3334         return nsres;
3335
3336     nsIURI_AddRef(aURI);
3337     ret->original_uri = aURI;
3338
3339     *_retval = (nsIChannel*)&ret->nsIHttpChannel_iface;
3340     return NS_OK;
3341 }
3342
3343 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
3344         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
3345 {
3346     TRACE("(%s %s %p %p)\n", debugstr_nsacstr(aSpec), debugstr_a(aOriginCharset), aBaseURI, _retval);
3347     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
3348 }
3349
3350 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, cpp_bool *aOffline)
3351 {
3352     TRACE("(%p)\n", aOffline);
3353     return nsIIOService_GetOffline(nsio, aOffline);
3354 }
3355
3356 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, cpp_bool aOffline)
3357 {
3358     TRACE("(%x)\n", aOffline);
3359     return nsIIOService_SetOffline(nsio, aOffline);
3360 }
3361
3362 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, LONG aPort,
3363                                              const char *aScheme, cpp_bool *_retval)
3364 {
3365     TRACE("(%d %s %p)\n", aPort, debugstr_a(aScheme), _retval);
3366     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
3367 }
3368
3369 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
3370                                                  nsACString * _retval)
3371 {
3372     TRACE("(%s %p)\n", debugstr_nsacstr(urlString), _retval);
3373     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
3374 }
3375
3376 static const nsIIOServiceVtbl nsIOServiceVtbl = {
3377     nsIOService_QueryInterface,
3378     nsIOService_AddRef,
3379     nsIOService_Release,
3380     nsIOService_GetProtocolHandler,
3381     nsIOService_GetProtocolFlags,
3382     nsIOService_NewURI,
3383     nsIOService_NewFileURI,
3384     nsIOService_NewChannelFromURI,
3385     nsIOService_NewChannel,
3386     nsIOService_GetOffline,
3387     nsIOService_SetOffline,
3388     nsIOService_AllowPort,
3389     nsIOService_ExtractScheme
3390 };
3391
3392 static nsIIOService nsIOService = { &nsIOServiceVtbl };
3393
3394 static nsresult NSAPI nsNetUtil_QueryInterface(nsINetUtil *iface, nsIIDRef riid,
3395         void **result)
3396 {
3397     return nsIIOService_QueryInterface(&nsIOService, riid, result);
3398 }
3399
3400 static nsrefcnt NSAPI nsNetUtil_AddRef(nsINetUtil *iface)
3401 {
3402     return 2;
3403 }
3404
3405 static nsrefcnt NSAPI nsNetUtil_Release(nsINetUtil *iface)
3406 {
3407     return 1;
3408 }
3409
3410 static nsresult NSAPI nsNetUtil_ParseContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
3411         nsACString *aCharset, cpp_bool *aHadCharset, nsACString *aContentType)
3412 {
3413     TRACE("(%s %p %p %p)\n", debugstr_nsacstr(aTypeHeader), aCharset, aHadCharset, aContentType);
3414
3415     return nsINetUtil_ParseContentType(net_util, aTypeHeader, aCharset, aHadCharset, aContentType);
3416 }
3417
3418 static nsresult NSAPI nsNetUtil_ProtocolHasFlags(nsINetUtil *iface, nsIURI *aURI, UINT32 aFlags, cpp_bool *_retval)
3419 {
3420     TRACE("()\n");
3421
3422     return nsINetUtil_ProtocolHasFlags(net_util, aURI, aFlags, _retval);
3423 }
3424
3425 static nsresult NSAPI nsNetUtil_URIChainHasFlags(nsINetUtil *iface, nsIURI *aURI, UINT32 aFlags, cpp_bool *_retval)
3426 {
3427     TRACE("(%p %08x %p)\n", aURI, aFlags, _retval);
3428
3429     if(aFlags == (1<<11)) {
3430         *_retval = FALSE;
3431         return NS_OK;
3432     }
3433
3434     return nsINetUtil_URIChainHasFlags(net_util, aURI, aFlags, _retval);
3435 }
3436
3437 static nsresult NSAPI nsNetUtil_ToImmutableURI(nsINetUtil *iface, nsIURI *aURI, nsIURI **_retval)
3438 {
3439     TRACE("(%p %p)\n", aURI, _retval);
3440
3441     return nsINetUtil_ToImmutableURI(net_util, aURI, _retval);
3442 }
3443
3444 static nsresult NSAPI nsNetUtil_NewSimpleNestedURI(nsINetUtil *iface, nsIURI *aURI, nsIURI **_retval)
3445 {
3446     TRACE("(%p %p)\n", aURI, _retval);
3447
3448     return nsINetUtil_NewSimpleNestedURI(net_util, aURI, _retval);
3449 }
3450
3451 static nsresult NSAPI nsNetUtil_EscapeString(nsINetUtil *iface, const nsACString *aString,
3452                                              UINT32 aEscapeType, nsACString *_retval)
3453 {
3454     TRACE("(%s %x %p)\n", debugstr_nsacstr(aString), aEscapeType, _retval);
3455
3456     return nsINetUtil_EscapeString(net_util, aString, aEscapeType, _retval);
3457 }
3458
3459 static nsresult NSAPI nsNetUtil_EscapeURL(nsINetUtil *iface, const nsACString *aStr, UINT32 aFlags,
3460                                           nsACString *_retval)
3461 {
3462     TRACE("(%s %08x %p)\n", debugstr_nsacstr(aStr), aFlags, _retval);
3463
3464     return nsINetUtil_EscapeURL(net_util, aStr, aFlags, _retval);
3465 }
3466
3467 static nsresult NSAPI nsNetUtil_UnescapeString(nsINetUtil *iface, const nsACString *aStr,
3468                                                UINT32 aFlags, nsACString *_retval)
3469 {
3470     TRACE("(%s %08x %p)\n", debugstr_nsacstr(aStr), aFlags, _retval);
3471
3472     return nsINetUtil_UnescapeString(net_util, aStr, aFlags, _retval);
3473 }
3474
3475 static nsresult NSAPI nsNetUtil_ExtractCharsetFromContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
3476         nsACString *aCharset, LONG *aCharsetStart, LONG *aCharsetEnd, cpp_bool *_retval)
3477 {
3478     TRACE("(%s %p %p %p %p)\n", debugstr_nsacstr(aTypeHeader), aCharset, aCharsetStart,
3479           aCharsetEnd, _retval);
3480
3481     return nsINetUtil_ExtractCharsetFromContentType(net_util, aTypeHeader, aCharset, aCharsetStart, aCharsetEnd, _retval);
3482 }
3483
3484 static const nsINetUtilVtbl nsNetUtilVtbl = {
3485     nsNetUtil_QueryInterface,
3486     nsNetUtil_AddRef,
3487     nsNetUtil_Release,
3488     nsNetUtil_ParseContentType,
3489     nsNetUtil_ProtocolHasFlags,
3490     nsNetUtil_URIChainHasFlags,
3491     nsNetUtil_ToImmutableURI,
3492     nsNetUtil_NewSimpleNestedURI,
3493     nsNetUtil_EscapeString,
3494     nsNetUtil_EscapeURL,
3495     nsNetUtil_UnescapeString,
3496     nsNetUtil_ExtractCharsetFromContentType
3497 };
3498
3499 static nsINetUtil nsNetUtil = { &nsNetUtilVtbl };
3500
3501 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
3502         void **result)
3503 {
3504     *result = NULL;
3505
3506     if(IsEqualGUID(&IID_nsISupports, riid))
3507         *result = &nsIOService;
3508     else if(IsEqualGUID(&IID_nsIIOService, riid))
3509         *result = &nsIOService;
3510     else if(IsEqualGUID(&IID_nsINetUtil, riid))
3511         *result = &nsNetUtil;
3512
3513     if(*result) {
3514         nsISupports_AddRef((nsISupports*)*result);
3515         return NS_OK;
3516     }
3517
3518     FIXME("(%s %p)\n", debugstr_guid(riid), result);
3519     return NS_NOINTERFACE;
3520 }
3521
3522 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
3523         void **result)
3524 {
3525     *result = NULL;
3526
3527     if(IsEqualGUID(&IID_nsISupports, riid)) {
3528         TRACE("(IID_nsISupports %p)\n", result);
3529         *result = iface;
3530     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
3531         TRACE("(IID_nsIFactory %p)\n", result);
3532         *result = iface;
3533     }
3534
3535     if(*result) {
3536         nsIFactory_AddRef(iface);
3537         return NS_OK;
3538     }
3539
3540     WARN("(%s %p)\n", debugstr_guid(riid), result);
3541     return NS_NOINTERFACE;
3542 }
3543
3544 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
3545 {
3546     return 2;
3547 }
3548
3549 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
3550 {
3551     return 1;
3552 }
3553
3554 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
3555         nsISupports *aOuter, const nsIID *iid, void **result)
3556 {
3557     return nsIIOService_QueryInterface(&nsIOService, iid, result);
3558 }
3559
3560 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, cpp_bool lock)
3561 {
3562     WARN("(%x)\n", lock);
3563     return NS_OK;
3564 }
3565
3566 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
3567     nsIOServiceFactory_QueryInterface,
3568     nsIOServiceFactory_AddRef,
3569     nsIOServiceFactory_Release,
3570     nsIOServiceFactory_CreateInstance,
3571     nsIOServiceFactory_LockFactory
3572 };
3573
3574 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
3575
3576 static BOOL translate_url(HTMLDocumentObj *doc, nsWineURI *uri)
3577 {
3578     OLECHAR *new_url = NULL;
3579     WCHAR *url;
3580     BOOL ret = FALSE;
3581     HRESULT hres;
3582
3583     if(!doc->hostui || !ensure_uri(uri))
3584         return FALSE;
3585
3586     hres = IUri_GetDisplayUri(uri->uri, &url);
3587     if(FAILED(hres))
3588         return FALSE;
3589
3590     hres = IDocHostUIHandler_TranslateUrl(doc->hostui, 0, url, &new_url);
3591     if(hres == S_OK && new_url) {
3592         if(strcmpW(url, new_url)) {
3593             FIXME("TranslateUrl returned new URL %s -> %s\n", debugstr_w(url), debugstr_w(new_url));
3594             ret = TRUE;
3595         }
3596         CoTaskMemFree(new_url);
3597     }
3598
3599     SysFreeString(url);
3600     return ret;
3601 }
3602
3603 nsresult on_start_uri_open(NSContainer *nscontainer, nsIURI *uri, cpp_bool *_retval)
3604 {
3605     nsWineURI *wine_uri;
3606     nsresult nsres;
3607
3608     *_retval = FALSE;
3609
3610     nsres = nsIURI_QueryInterface(uri, &IID_nsWineURI, (void**)&wine_uri);
3611     if(NS_FAILED(nsres)) {
3612         WARN("Could not get nsWineURI: %08x\n", nsres);
3613         return NS_ERROR_NOT_IMPLEMENTED;
3614     }
3615
3616     if(!wine_uri->is_doc_uri) {
3617         wine_uri->is_doc_uri = TRUE;
3618
3619         if(!wine_uri->container) {
3620             nsIWebBrowserChrome_AddRef(&nscontainer->nsIWebBrowserChrome_iface);
3621             wine_uri->container = nscontainer;
3622         }
3623
3624         if(nscontainer->doc)
3625             *_retval = translate_url(nscontainer->doc, wine_uri);
3626     }
3627
3628     nsIFileURL_Release(&wine_uri->nsIFileURL_iface);
3629     return NS_OK;
3630 }
3631
3632 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
3633 {
3634     nsIFactory *old_factory = NULL;
3635     nsresult nsres;
3636
3637     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
3638                                                &IID_nsIFactory, (void**)&old_factory);
3639     if(NS_FAILED(nsres)) {
3640         ERR("Could not get factory: %08x\n", nsres);
3641         return;
3642     }
3643
3644     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
3645     if(NS_FAILED(nsres)) {
3646         ERR("Couldn not create nsIOService instance %08x\n", nsres);
3647         nsIFactory_Release(old_factory);
3648         return;
3649     }
3650
3651     nsres = nsIIOService_QueryInterface(nsio, &IID_nsINetUtil, (void**)&net_util);
3652     if(NS_FAILED(nsres)) {
3653         WARN("Could not get nsINetUtil interface: %08x\n", nsres);
3654         nsIIOService_Release(nsio);
3655         return;
3656     }
3657
3658     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
3659     nsIFactory_Release(old_factory);
3660     if(NS_FAILED(nsres))
3661         ERR("UnregisterFactory failed: %08x\n", nsres);
3662
3663     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
3664             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
3665     if(NS_FAILED(nsres))
3666         ERR("RegisterFactory failed: %08x\n", nsres);
3667 }
3668
3669 void release_nsio(void)
3670 {
3671     if(net_util) {
3672         nsINetUtil_Release(net_util);
3673         net_util = NULL;
3674     }
3675
3676     if(nsio) {
3677         nsIIOService_Release(nsio);
3678         nsio = NULL;
3679     }
3680 }