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