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