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