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