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