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