kernel32/profile: Add a few NULL checks (Coverity).
[wine] / dlls / mshtml / navigate.c
1 /*
2  * Copyright 2006 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 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "ole2.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "mshtml_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 #define CONTENT_LENGTH "Content-Length"
40
41 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
42
43 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
44
45 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
46                                                    nsQIResult result)
47 {
48     nsProtocolStream *This = NSINSTREAM_THIS(iface);
49
50     *result = NULL;
51
52     if(IsEqualGUID(&IID_nsISupports, riid)) {
53         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
54         *result  = NSINSTREAM(This);
55     }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
56         TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
57         *result  = NSINSTREAM(This);
58     }
59
60     if(*result) {
61         nsIInputStream_AddRef(NSINSTREAM(This));
62         return NS_OK;
63     }
64
65     WARN("unsupported interface %s\n", debugstr_guid(riid));
66     return NS_NOINTERFACE;
67 }
68
69 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
70 {
71     nsProtocolStream *This = NSINSTREAM_THIS(iface);
72     LONG ref = InterlockedIncrement(&This->ref);
73
74     TRACE("(%p) ref=%d\n", This, ref);
75
76     return ref;
77 }
78
79
80 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
81 {
82     nsProtocolStream *This = NSINSTREAM_THIS(iface);
83     LONG ref = InterlockedDecrement(&This->ref);
84
85     TRACE("(%p) ref=%d\n", This, ref);
86
87     if(!ref)
88         mshtml_free(This);
89
90     return ref;
91 }
92
93 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
94 {
95     nsProtocolStream *This = NSINSTREAM_THIS(iface);
96     FIXME("(%p)\n", This);
97     return NS_ERROR_NOT_IMPLEMENTED;
98 }
99
100 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
101 {
102     nsProtocolStream *This = NSINSTREAM_THIS(iface);
103     FIXME("(%p)->(%p)\n", This, _retval);
104     return NS_ERROR_NOT_IMPLEMENTED;
105 }
106
107 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
108                                          PRUint32 *_retval)
109 {
110     nsProtocolStream *This = NSINSTREAM_THIS(iface);
111
112     TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
113
114     /* Gecko always calls Read with big enough buffer */
115     if(aCount < This->buf_size)
116         FIXME("aCount < This->buf_size\n");
117
118     *_retval = This->buf_size;
119     if(This->buf_size)
120         memcpy(aBuf, This->buf, This->buf_size);
121     This->buf_size = 0;
122
123     return NS_OK;
124 }
125
126 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
127         nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
128         void *aClousure, PRUint32 aCount, PRUint32 *_retval)
129 {
130     nsProtocolStream *This = NSINSTREAM_THIS(iface);
131     PRUint32 written = 0;
132     nsresult nsres;
133
134     TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
135
136     if(!This->buf_size)
137         return S_OK;
138
139     if(This->buf_size > aCount)
140         FIXME("buf_size > aCount\n");
141
142     nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, This->buf_size, &written);
143     if(NS_FAILED(nsres))
144         TRACE("aWritter failed: %08x\n", nsres);
145     else if(written != This->buf_size)
146         FIXME("written %d != buf_size %d\n", written, This->buf_size);
147
148     This->buf_size -= written; 
149
150     *_retval = written;
151     return nsres;
152 }
153
154 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
155 {
156     nsProtocolStream *This = NSINSTREAM_THIS(iface);
157     FIXME("(%p)->(%p)\n", This, _retval);
158     return NS_ERROR_NOT_IMPLEMENTED;
159 }
160
161 #undef NSINSTREAM_THIS
162
163 static const nsIInputStreamVtbl nsInputStreamVtbl = {
164     nsInputStream_QueryInterface,
165     nsInputStream_AddRef,
166     nsInputStream_Release,
167     nsInputStream_Close,
168     nsInputStream_Available,
169     nsInputStream_Read,
170     nsInputStream_ReadSegments,
171     nsInputStream_IsNonBlocking
172 };
173
174 static nsProtocolStream *create_nsprotocol_stream(IStream *stream)
175 {
176     nsProtocolStream *ret = mshtml_alloc(sizeof(nsProtocolStream));
177
178     ret->lpInputStreamVtbl = &nsInputStreamVtbl;
179     ret->ref = 1;
180     ret->buf_size = 0;
181
182     return ret;
183 }
184
185 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
186
187 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
188         REFIID riid, void **ppv)
189 {
190     BSCallback *This = STATUSCLB_THIS(iface);
191
192     *ppv = NULL;
193     if(IsEqualGUID(&IID_IUnknown, riid)) {
194         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
195         *ppv = STATUSCLB(This);
196     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
197         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
198         *ppv = STATUSCLB(This);
199     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
200         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
201         *ppv = SERVPROV(This);
202     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
203         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
204         *ppv = HTTPNEG(This);
205     }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
206         TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
207         *ppv = HTTPNEG(This);
208     }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
209         TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
210         *ppv = BINDINFO(This);
211     }
212
213     if(*ppv) {
214         IBindStatusCallback_AddRef(STATUSCLB(This));
215         return S_OK;
216     }
217
218     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
219     return E_NOINTERFACE;
220 }
221
222 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
223 {
224     BSCallback *This = STATUSCLB_THIS(iface);
225     LONG ref = InterlockedIncrement(&This->ref);
226
227     TRACE("(%p) ref = %d\n", This, ref);
228
229     return ref;
230 }
231
232 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
233 {
234     BSCallback *This = STATUSCLB_THIS(iface);
235     LONG ref = InterlockedDecrement(&This->ref);
236
237     TRACE("(%p) ref = %d\n", This, ref);
238
239     if(!ref) {
240         if(This->post_data)
241             GlobalFree(This->post_data);
242         if(This->nschannel)
243             nsIChannel_Release(NSCHANNEL(This->nschannel));
244         if(This->nslistener)
245             nsIStreamListener_Release(This->nslistener);
246         if(This->nscontext)
247             nsISupports_Release(This->nscontext);
248         if(This->nsstream)
249             nsIInputStream_Release(NSINSTREAM(This->nsstream));
250         if(This->mon)
251             IMoniker_Release(This->mon);
252         if(This->binding)
253             IBinding_Release(This->binding);
254         mshtml_free(This->headers);
255         mshtml_free(This);
256     }
257
258     return ref;
259 }
260
261 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
262         DWORD dwReserved, IBinding *pbind)
263 {
264     BSCallback *This = STATUSCLB_THIS(iface);
265
266     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
267
268     IBinding_AddRef(pbind);
269     This->binding = pbind;
270
271     if(This->nschannel && This->nschannel->load_group) {
272         nsresult nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
273                 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
274
275         if(NS_FAILED(nsres))
276             ERR("AddRequest failed:%08x\n", nsres);
277     }
278
279     return S_OK;
280 }
281
282 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
283 {
284     BSCallback *This = STATUSCLB_THIS(iface);
285     FIXME("(%p)->(%p)\n", This, pnPriority);
286     return E_NOTIMPL;
287 }
288
289 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
290 {
291     BSCallback *This = STATUSCLB_THIS(iface);
292     FIXME("(%p)->(%d)\n", This, reserved);
293     return E_NOTIMPL;
294 }
295
296 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
297         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
298 {
299     BSCallback *This = STATUSCLB_THIS(iface);
300
301     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
302             debugstr_w(szStatusText));
303
304     switch(ulStatusCode) {
305     case BINDSTATUS_MIMETYPEAVAILABLE: {
306         int len;
307
308         if(!This->nschannel)
309             return S_OK;
310         mshtml_free(This->nschannel->content);
311
312         len = WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, NULL, 0, NULL, NULL);
313         This->nschannel->content = mshtml_alloc(len*sizeof(WCHAR));
314         WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, This->nschannel->content, -1, NULL, NULL);
315     }
316     }
317
318     return S_OK;
319 }
320
321 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
322         HRESULT hresult, LPCWSTR szError)
323 {
324     BSCallback *This = STATUSCLB_THIS(iface);
325
326     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
327
328     IBinding_Release(This->binding);
329     This->binding = NULL;
330
331     if(This->nslistener) {
332         nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
333                 This->nscontext, NS_OK);
334
335         if(This->nschannel->load_group) {
336             nsresult nsres;
337
338             nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
339                     (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
340             if(NS_FAILED(nsres))
341                 ERR("RemoveRequest failed: %08x\n", nsres);
342         }
343     }
344
345     if(This->doc) {
346         task_t *task = mshtml_alloc(sizeof(task_t));
347
348         task->doc = This->doc;
349         task->task_id = TASK_PARSECOMPLETE;
350         task->next = NULL;
351
352         /*
353          * This should be done in the worker thread that parses HTML,
354          * but we don't have such thread (Gecko parses HTML for us).
355          */
356         push_task(task);
357     }
358
359     return S_OK;
360 }
361
362 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
363         DWORD *grfBINDF, BINDINFO *pbindinfo)
364 {
365     BSCallback *This = STATUSCLB_THIS(iface);
366     DWORD size;
367
368     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
369
370     *grfBINDF = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
371
372     size = pbindinfo->cbSize;
373     memset(pbindinfo, 0, size);
374     pbindinfo->cbSize = size;
375
376     pbindinfo->cbstgmedData = This->post_data_len;
377     pbindinfo->dwCodePage = CP_UTF8;
378     pbindinfo->dwOptions = 0x80000;
379
380     if(This->post_data) {
381         pbindinfo->dwBindVerb = BINDVERB_POST;
382
383         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
384         pbindinfo->stgmedData.u.hGlobal = This->post_data;
385         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
386         IBindStatusCallback_AddRef(STATUSCLB(This));
387     }
388
389     return S_OK;
390 }
391
392 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
393         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
394 {
395     BSCallback *This = STATUSCLB_THIS(iface);
396     nsresult nsres;
397     HRESULT hres;
398
399     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
400
401     if(This->nslistener) {
402         if(!This->nsstream) {
403             This->nsstream = create_nsprotocol_stream(pstgmed->u.pstm);
404
405             nsres = nsIStreamListener_OnStartRequest(This->nslistener,
406                     (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
407             if(NS_FAILED(nsres))
408                 FIXME("OnStartRequest failed: %08x\n", nsres);
409         }
410
411         do {
412             hres = IStream_Read(pstgmed->u.pstm, This->nsstream->buf, sizeof(This->nsstream->buf),
413                          &This->nsstream->buf_size);
414             if(!This->nsstream->buf_size)
415                 break;
416
417             nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
418                     (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
419                     NSINSTREAM(This->nsstream), This->readed, This->nsstream->buf_size);
420             if(NS_FAILED(nsres))
421                 FIXME("OnDataAvailable failed: %08x\n", nsres);
422
423             if(This->nsstream->buf_size)
424                 FIXME("buffer is not empty!\n");
425
426             This->readed += This->nsstream->buf_size;
427         }while(hres == S_OK);
428     }else {
429         BYTE buf[1024];
430         DWORD read;
431         do {
432             read = 0;
433             hres = IStream_Read(pstgmed->u.pstm, buf, sizeof(buf), &read);
434         }while(hres == S_OK && read);
435     }
436
437     return S_OK;
438 }
439
440 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
441         REFIID riid, IUnknown *punk)
442 {
443     BSCallback *This = STATUSCLB_THIS(iface);
444     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
445     return E_NOTIMPL;
446 }
447
448 #undef STATUSCLB_THIS
449
450 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
451     BindStatusCallback_QueryInterface,
452     BindStatusCallback_AddRef,
453     BindStatusCallback_Release,
454     BindStatusCallback_OnStartBinding,
455     BindStatusCallback_GetPriority,
456     BindStatusCallback_OnLowResource,
457     BindStatusCallback_OnProgress,
458     BindStatusCallback_OnStopBinding,
459     BindStatusCallback_GetBindInfo,
460     BindStatusCallback_OnDataAvailable,
461     BindStatusCallback_OnObjectAvailable
462 };
463
464 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
465
466 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
467                                                    REFIID riid, void **ppv)
468 {
469     BSCallback *This = HTTPNEG_THIS(iface);
470     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
471 }
472
473 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
474 {
475     BSCallback *This = HTTPNEG_THIS(iface);
476     return IBindStatusCallback_AddRef(STATUSCLB(This));
477 }
478
479 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
480 {
481     BSCallback *This = HTTPNEG_THIS(iface);
482     return IBindStatusCallback_Release(STATUSCLB(This));
483 }
484
485 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
486         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
487 {
488     BSCallback *This = HTTPNEG_THIS(iface);
489     DWORD size;
490
491     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
492           dwReserved, pszAdditionalHeaders);
493
494     if(!This->headers) {
495         *pszAdditionalHeaders = NULL;
496         return S_OK;
497     }
498
499     size = (strlenW(This->headers)+1)*sizeof(WCHAR);
500     *pszAdditionalHeaders = CoTaskMemAlloc(size);
501     memcpy(*pszAdditionalHeaders, This->headers, size);
502
503     return S_OK;
504 }
505
506 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
507         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
508 {
509     BSCallback *This = HTTPNEG_THIS(iface);
510     FIXME("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
511           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
512     return E_NOTIMPL;
513 }
514
515 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
516         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
517 {
518     BSCallback *This = HTTPNEG_THIS(iface);
519     FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
520     return E_NOTIMPL;
521 }
522
523 #undef HTTPNEG
524
525 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
526     HttpNegotiate_QueryInterface,
527     HttpNegotiate_AddRef,
528     HttpNegotiate_Release,
529     HttpNegotiate_BeginningTransaction,
530     HttpNegotiate_OnResponse,
531     HttpNegotiate_GetRootSecurityId
532 };
533
534 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
535
536 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
537                                                       REFIID riid, void **ppv)
538 {
539     BSCallback *This = BINDINFO_THIS(iface);
540     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
541 }
542
543 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
544 {
545     BSCallback *This = BINDINFO_THIS(iface);
546     return IBindStatusCallback_AddRef(STATUSCLB(This));
547 }
548
549 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
550 {
551     BSCallback *This = BINDINFO_THIS(iface);
552     return IBindStatusCallback_Release(STATUSCLB(This));
553 }
554
555 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
556                                                    DWORD *grfBINDF, BINDINFO *pbindinfo)
557 {
558     BSCallback *This = BINDINFO_THIS(iface);
559     FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
560     return E_NOTIMPL;
561 }
562
563 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
564         ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
565 {
566     BSCallback *This = BINDINFO_THIS(iface);
567     FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
568     return E_NOTIMPL;
569 }
570
571 #undef BINDINFO_THIS
572
573 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
574     InternetBindInfo_QueryInterface,
575     InternetBindInfo_AddRef,
576     InternetBindInfo_Release,
577     InternetBindInfo_GetBindInfo,
578     InternetBindInfo_GetBindString
579 };
580
581 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
582
583 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
584                                                         REFIID riid, void **ppv)
585 {
586     BSCallback *This = SERVPROV_THIS(iface);
587     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
588 }
589
590 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
591 {
592     BSCallback *This = SERVPROV_THIS(iface);
593     return IBindStatusCallback_AddRef(STATUSCLB(This));
594 }
595
596 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
597 {
598     BSCallback *This = SERVPROV_THIS(iface);
599     return IBindStatusCallback_Release(STATUSCLB(This));
600 }
601
602 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
603         REFGUID guidService, REFIID riid, void **ppv)
604 {
605     BSCallback *This = SERVPROV_THIS(iface);
606     FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
607     return E_NOTIMPL;
608 }
609
610 #undef SERVPROV_THIS
611
612 static const IServiceProviderVtbl ServiceProviderVtbl = {
613     BSCServiceProvider_QueryInterface,
614     BSCServiceProvider_AddRef,
615     BSCServiceProvider_Release,
616     BSCServiceProvider_QueryService
617 };
618
619 BSCallback *create_bscallback(IMoniker *mon)
620 {
621     BSCallback *ret = mshtml_alloc(sizeof(BSCallback));
622
623     ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
624     ret->lpServiceProviderVtbl    = &ServiceProviderVtbl;
625     ret->lpHttpNegotiate2Vtbl     = &HttpNegotiate2Vtbl;
626     ret->lpInternetBindInfoVtbl   = &InternetBindInfoVtbl;
627     ret->ref = 1;
628     ret->post_data = NULL;
629     ret->headers = NULL;
630     ret->post_data_len = 0;
631     ret->readed = 0;
632     ret->nschannel = NULL;
633     ret->nslistener = NULL;
634     ret->nscontext = NULL;
635     ret->nsstream = NULL;
636     ret->binding = NULL;
637     ret->doc = NULL;
638
639     if(mon)
640         IMoniker_AddRef(mon);
641     ret->mon = mon;
642
643     return ret;
644 }
645
646 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
647                             HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
648 {
649     PRUint32 post_data_len = 0, available = 0;
650     HGLOBAL post_data = NULL;
651     LPWSTR headers = NULL;
652     DWORD headers_len = 0, len;
653     const char *ptr, *ptr2, *post_data_end;
654
655     nsIInputStream_Available(post_data_stream, &available);
656     post_data = GlobalAlloc(0, available+1);
657     nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
658     
659     TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
660
661     ptr = ptr2 = post_data;
662     post_data_end = (const char*)post_data+post_data_len;
663
664     while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
665         while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
666             ptr++;
667
668         if(!*ptr) {
669             FIXME("*ptr = 0\n");
670             return;
671         }
672
673         ptr += 2;
674
675         if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
676            && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
677                              CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
678                              ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
679             ptr2 = ptr;
680             continue;
681         }
682
683         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
684
685         if(headers)
686             headers = mshtml_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
687         else
688             headers = mshtml_alloc((len+1)*sizeof(WCHAR));
689
690         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, -1);
691         headers_len += len;
692
693         ptr2 = ptr;
694     }
695
696     headers[headers_len] = 0;
697     *headers_ret = headers;
698
699     if(ptr >= post_data_end-2) {
700         GlobalFree(post_data);
701         return;
702     }
703
704     ptr += 2;
705
706     if(headers_len) {
707         post_data_len -= ptr-(const char*)post_data;
708         memmove(post_data, ptr, post_data_len);
709         post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
710     }
711
712     *post_data_ret = post_data;
713     *post_data_len_ret = post_data_len;
714 }
715
716 void hlink_frame_navigate(HTMLDocument *doc, IHlinkFrame *hlink_frame,
717                           LPCWSTR uri, nsIInputStream *post_data_stream, DWORD hlnf)
718 {
719     BSCallback *callback;
720     IBindCtx *bindctx;
721     IMoniker *mon;
722     IHlink *hlink;
723
724     callback = create_bscallback(NULL);
725
726     if(post_data_stream) {
727         parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
728                         &callback->post_data_len);
729         TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
730               debugstr_an(callback->post_data, callback->post_data_len));
731     }
732
733     CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
734
735     hlink = Hlink_Create();
736
737     CreateURLMoniker(NULL, uri, &mon);
738     IHlink_SetMonikerReference(hlink, 0, mon, NULL);
739
740     if(hlnf & HLNF_OPENINNEWWINDOW) {
741         static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
742         IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
743     }
744
745     IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
746
747     IBindCtx_Release(bindctx);
748     IBindStatusCallback_Release(STATUSCLB(callback));
749     IMoniker_Release(mon);
750
751 }
752
753 HRESULT start_binding(BSCallback *bscallback)
754 {
755     IStream *str = NULL;
756     IBindCtx *bctx;
757     HRESULT hres;
758
759     hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
760     if(FAILED(hres)) {
761         WARN("CreateAsyncBindCtx failed: %08x\n", hres);
762         return hres;
763     }
764
765     hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
766     IBindCtx_Release(bctx);
767     if(FAILED(hres)) {
768         WARN("BindToStorage failed: %08x\n", hres);
769         return hres;
770     }
771
772     if(str)
773         IStream_Release(str);
774
775     IMoniker_Release(bscallback->mon);
776     bscallback->mon = NULL;
777     return S_OK;
778 }
779
780 void set_document_bscallback(HTMLDocument *doc, BSCallback *callback)
781 {
782     if(doc->bscallback) {
783         if(doc->bscallback->binding)
784             IBinding_Abort(doc->bscallback->binding);
785         doc->bscallback->doc = NULL;
786         IBindStatusCallback_Release(STATUSCLB(doc->bscallback));
787     }
788
789     doc->bscallback = callback;
790
791     if(callback) {
792         IBindStatusCallback_AddRef(STATUSCLB(callback));
793         callback->doc = doc;
794     }
795 }