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