mshtml: Added IHTMLStyleSheet::get_rules implementation.
[wine] / dlls / urlmon / http.c
1 /*
2  * Copyright 2005 Jacek Caban
3  * Copyright 2007 Misha Koshelev
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 /*
21  * TODO:
22  * - Handle redirects as native.
23  */
24
25 #include <stdarg.h>
26
27 #define COBJMACROS
28 #define NONAMELESSUNION
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "ole2.h"
34 #include "urlmon.h"
35 #include "wininet.h"
36 #include "urlmon_main.h"
37
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
42
43 /* Flags are needed for, among other things, return HRESULTs from the Read function
44  * to conform to native. For example, Read returns:
45  *
46  * 1. E_PENDING if called before the request has completed,
47  *        (flags = 0)
48  * 2. S_FALSE after all data has been read and S_OK has been reported,
49  *        (flags = FLAG_REQUEST_COMPLETE | FLAG_ALL_DATA_READ | FLAG_RESULT_REPORTED)
50  * 3. INET_E_DATA_NOT_AVAILABLE if InternetQueryDataAvailable fails. The first time
51  *    this occurs, INET_E_DATA_NOT_AVAILABLE will also be reported to the sink,
52  *        (flags = FLAG_REQUEST_COMPLETE)
53  *    but upon subsequent calls to Read no reporting will take place, yet
54  *    InternetQueryDataAvailable will still be called, and, on failure,
55  *    INET_E_DATA_NOT_AVAILABLE will still be returned.
56  *        (flags = FLAG_REQUEST_COMPLETE | FLAG_RESULT_REPORTED)
57  *
58  * FLAG_FIRST_DATA_REPORTED and FLAG_LAST_DATA_REPORTED are needed for proper
59  * ReportData reporting. For example, if OnResponse returns S_OK, Continue will
60  * report BSCF_FIRSTDATANOTIFICATION, and when all data has been read Read will
61  * report BSCF_INTERMEDIATEDATANOTIFICATION|BSCF_LASTDATANOTIFICATION. However,
62  * if OnResponse does not return S_OK, Continue will not report data, and Read
63  * will report BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION when all
64  * data has been read.
65  */
66 #define FLAG_REQUEST_COMPLETE 0x1
67 #define FLAG_FIRST_CONTINUE_COMPLETE 0x2
68 #define FLAG_FIRST_DATA_REPORTED 0x4
69 #define FLAG_ALL_DATA_READ 0x8
70 #define FLAG_LAST_DATA_REPORTED 0x10
71 #define FLAG_RESULT_REPORTED 0x20
72
73 typedef struct {
74     const IInternetProtocolVtbl *lpInternetProtocolVtbl;
75     const IInternetPriorityVtbl *lpInternetPriorityVtbl;
76
77     DWORD flags, grfBINDF;
78     BINDINFO bind_info;
79     IInternetProtocolSink *protocol_sink;
80     IHttpNegotiate *http_negotiate;
81     HINTERNET internet, connect, request;
82     LPWSTR full_header;
83     HANDLE lock;
84     ULONG current_position, content_length, available_bytes;
85     LONG priority;
86
87     LONG ref;
88 } HttpProtocol;
89
90 /* Default headers from native */
91 static const WCHAR wszHeaders[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',
92                                    ':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
93
94 /*
95  * Helpers
96  */
97
98 static void HTTPPROTOCOL_ReportResult(HttpProtocol *This, HRESULT hres)
99 {
100     if (!(This->flags & FLAG_RESULT_REPORTED) &&
101         This->protocol_sink)
102     {
103         This->flags |= FLAG_RESULT_REPORTED;
104         IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
105     }
106 }
107
108 static void HTTPPROTOCOL_ReportData(HttpProtocol *This)
109 {
110     DWORD bscf;
111     if (!(This->flags & FLAG_LAST_DATA_REPORTED) &&
112         This->protocol_sink)
113     {
114         if (This->flags & FLAG_FIRST_DATA_REPORTED)
115         {
116             bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
117         }
118         else
119         {
120             This->flags |= FLAG_FIRST_DATA_REPORTED;
121             bscf = BSCF_FIRSTDATANOTIFICATION;
122         }
123         if (This->flags & FLAG_ALL_DATA_READ &&
124             !(This->flags & FLAG_LAST_DATA_REPORTED))
125         {
126             This->flags |= FLAG_LAST_DATA_REPORTED;
127             bscf |= BSCF_LASTDATANOTIFICATION;
128         }
129         IInternetProtocolSink_ReportData(This->protocol_sink, bscf,
130                                          This->current_position+This->available_bytes,
131                                          This->content_length);
132     }
133 }
134
135 static void HTTPPROTOCOL_AllDataRead(HttpProtocol *This)
136 {
137     if (!(This->flags & FLAG_ALL_DATA_READ))
138         This->flags |= FLAG_ALL_DATA_READ;
139     HTTPPROTOCOL_ReportData(This);
140     HTTPPROTOCOL_ReportResult(This, S_OK);
141 }
142
143 static void HTTPPROTOCOL_Close(HttpProtocol *This)
144 {
145     if (This->http_negotiate)
146     {
147         IHttpNegotiate_Release(This->http_negotiate);
148         This->http_negotiate = 0;
149     }
150     if (This->request)
151         InternetCloseHandle(This->request);
152     if (This->connect)
153         InternetCloseHandle(This->connect);
154     if (This->internet)
155     {
156         InternetCloseHandle(This->internet);
157         This->internet = 0;
158     }
159     if (This->full_header)
160     {
161         if (This->full_header != wszHeaders)
162             heap_free(This->full_header);
163         This->full_header = 0;
164     }
165     This->flags = 0;
166 }
167
168 static void CALLBACK HTTPPROTOCOL_InternetStatusCallback(
169     HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
170     LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
171 {
172     HttpProtocol *This = (HttpProtocol *)dwContext;
173     PROTOCOLDATA data;
174     ULONG ulStatusCode;
175
176     switch (dwInternetStatus)
177     {
178     case INTERNET_STATUS_RESOLVING_NAME:
179         ulStatusCode = BINDSTATUS_FINDINGRESOURCE;
180         break;
181     case INTERNET_STATUS_CONNECTING_TO_SERVER:
182         ulStatusCode = BINDSTATUS_CONNECTING;
183         break;
184     case INTERNET_STATUS_SENDING_REQUEST:
185         ulStatusCode = BINDSTATUS_SENDINGREQUEST;
186         break;
187     case INTERNET_STATUS_REQUEST_COMPLETE:
188         This->flags |= FLAG_REQUEST_COMPLETE;
189         /* PROTOCOLDATA same as native */
190         memset(&data, 0, sizeof(data));
191         data.dwState = 0xf1000000;
192         if (This->flags & FLAG_FIRST_CONTINUE_COMPLETE)
193             data.pData = (LPVOID)BINDSTATUS_ENDDOWNLOADCOMPONENTS;
194         else
195             data.pData = (LPVOID)BINDSTATUS_DOWNLOADINGDATA;
196         if (This->grfBINDF & BINDF_FROMURLMON)
197             IInternetProtocolSink_Switch(This->protocol_sink, &data);
198         else
199             IInternetProtocol_Continue((IInternetProtocol *)This, &data);
200         return;
201     case INTERNET_STATUS_HANDLE_CREATED:
202         IInternetProtocol_AddRef((IInternetProtocol *)This);
203         return;
204     case INTERNET_STATUS_HANDLE_CLOSING:
205         if (*(HINTERNET *)lpvStatusInformation == This->connect)
206         {
207             This->connect = 0;
208         }
209         else if (*(HINTERNET *)lpvStatusInformation == This->request)
210         {
211             This->request = 0;
212             if (This->protocol_sink)
213             {
214                 IInternetProtocolSink_Release(This->protocol_sink);
215                 This->protocol_sink = 0;
216             }
217             if (This->bind_info.cbSize)
218             {
219                 ReleaseBindInfo(&This->bind_info);
220                 memset(&This->bind_info, 0, sizeof(This->bind_info));
221             }
222         }
223         IInternetProtocol_Release((IInternetProtocol *)This);
224         return;
225     default:
226         WARN("Unhandled Internet status callback %d\n", dwInternetStatus);
227         return;
228     }
229
230     IInternetProtocolSink_ReportProgress(This->protocol_sink, ulStatusCode, (LPWSTR)lpvStatusInformation);
231 }
232
233 static inline LPWSTR strndupW(LPCWSTR string, int len)
234 {
235     LPWSTR ret = NULL;
236     if (string &&
237         (ret = heap_alloc((len+1)*sizeof(WCHAR))) != NULL)
238     {
239         memcpy(ret, string, len*sizeof(WCHAR));
240         ret[len] = 0;
241     }
242     return ret;
243 }
244
245 /*
246  * Interface implementations
247  */
248
249 #define PROTOCOL(x)  ((IInternetProtocol*)  &(x)->lpInternetProtocolVtbl)
250 #define PRIORITY(x)  ((IInternetPriority*)  &(x)->lpInternetPriorityVtbl)
251
252 #define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, InternetProtocol, iface)
253
254 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
255 {
256     HttpProtocol *This = PROTOCOL_THIS(iface);
257
258     *ppv = NULL;
259     if(IsEqualGUID(&IID_IUnknown, riid)) {
260         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
261         *ppv = PROTOCOL(This);
262     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
263         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
264         *ppv = PROTOCOL(This);
265     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
266         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
267         *ppv = PROTOCOL(This);
268     }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
269         TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
270         *ppv = PRIORITY(This);
271     }
272
273     if(*ppv) {
274         IInternetProtocol_AddRef(iface);
275         return S_OK;
276     }
277
278     WARN("not supported interface %s\n", debugstr_guid(riid));
279     return E_NOINTERFACE;
280 }
281
282 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocol *iface)
283 {
284     HttpProtocol *This = PROTOCOL_THIS(iface);
285     LONG ref = InterlockedIncrement(&This->ref);
286     TRACE("(%p) ref=%d\n", This, ref);
287     return ref;
288 }
289
290 static ULONG WINAPI HttpProtocol_Release(IInternetProtocol *iface)
291 {
292     HttpProtocol *This = PROTOCOL_THIS(iface);
293     LONG ref = InterlockedDecrement(&This->ref);
294
295     TRACE("(%p) ref=%d\n", This, ref);
296
297     if(!ref) {
298         HTTPPROTOCOL_Close(This);
299         heap_free(This);
300
301         URLMON_UnlockModule();
302     }
303
304     return ref;
305 }
306
307 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
308         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
309         DWORD grfPI, DWORD dwReserved)
310 {
311     HttpProtocol *This = PROTOCOL_THIS(iface);
312     URL_COMPONENTSW url;
313     DWORD len = 0, request_flags = INTERNET_FLAG_KEEP_CONNECTION;
314     ULONG num = 0;
315     IServiceProvider *service_provider = 0;
316     IHttpNegotiate2 *http_negotiate2 = 0;
317     LPWSTR host = 0, path = 0, user = 0, pass = 0, addl_header = 0,
318         post_cookie = 0, optional = 0;
319     BYTE security_id[512];
320     LPOLESTR user_agent, accept_mimes[257];
321     HRESULT hres;
322
323     static const WCHAR wszHttp[] = {'h','t','t','p',':'};
324     static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
325         {{'G','E','T',0},
326          {'P','O','S','T',0},
327          {'P','U','T',0}};
328
329     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
330             pOIBindInfo, grfPI, dwReserved);
331
332     IInternetProtocolSink_AddRef(pOIProtSink);
333     This->protocol_sink = pOIProtSink;
334
335     memset(&This->bind_info, 0, sizeof(This->bind_info));
336     This->bind_info.cbSize = sizeof(BINDINFO);
337     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &This->grfBINDF, &This->bind_info);
338     if (hres != S_OK)
339     {
340         WARN("GetBindInfo failed: %08x\n", hres);
341         goto done;
342     }
343
344     if (lstrlenW(szUrl) < sizeof(wszHttp)/sizeof(WCHAR)
345         || memcmp(szUrl, wszHttp, sizeof(wszHttp)))
346     {
347         hres = MK_E_SYNTAX;
348         goto done;
349     }
350
351     memset(&url, 0, sizeof(url));
352     url.dwStructSize = sizeof(url);
353     url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength =
354         url.dwPasswordLength = 1;
355     if (!InternetCrackUrlW(szUrl, 0, 0, &url))
356     {
357         hres = MK_E_SYNTAX;
358         goto done;
359     }
360     host = strndupW(url.lpszHostName, url.dwHostNameLength);
361     path = strndupW(url.lpszUrlPath, url.dwUrlPathLength);
362     user = strndupW(url.lpszUserName, url.dwUserNameLength);
363     pass = strndupW(url.lpszPassword, url.dwPasswordLength);
364     if (!url.nPort)
365         url.nPort = INTERNET_DEFAULT_HTTP_PORT;
366
367     if(!(This->grfBINDF & BINDF_FROMURLMON))
368         IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_DIRECTBIND, NULL);
369
370     hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT, &user_agent,
371                                            1, &num);
372     if (hres != S_OK || !num)
373     {
374         CHAR null_char = 0;
375         LPSTR user_agenta = NULL;
376         len = 0;
377         if ((hres = ObtainUserAgentString(0, &null_char, &len)) != E_OUTOFMEMORY)
378         {
379             WARN("ObtainUserAgentString failed: %08x\n", hres);
380         }
381         else if (!(user_agenta = heap_alloc(len*sizeof(CHAR))))
382         {
383             WARN("Out of memory\n");
384         }
385         else if ((hres = ObtainUserAgentString(0, user_agenta, &len)) != S_OK)
386         {
387             WARN("ObtainUserAgentString failed: %08x\n", hres);
388         }
389         else
390         {
391             if (!(user_agent = CoTaskMemAlloc((len)*sizeof(WCHAR))))
392                 WARN("Out of memory\n");
393             else
394                 MultiByteToWideChar(CP_ACP, 0, user_agenta, -1, user_agent, len*sizeof(WCHAR));
395         }
396         heap_free(user_agenta);
397     }
398
399     This->internet = InternetOpenW(user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC);
400     if (!This->internet)
401     {
402         WARN("InternetOpen failed: %d\n", GetLastError());
403         hres = INET_E_NO_SESSION;
404         goto done;
405     }
406
407     /* Native does not check for success of next call, so we won't either */
408     InternetSetStatusCallbackW(This->internet, HTTPPROTOCOL_InternetStatusCallback);
409
410     This->connect = InternetConnectW(This->internet, host, url.nPort, user,
411                                      pass, INTERNET_SERVICE_HTTP, 0, (DWORD)This);
412     if (!This->connect)
413     {
414         WARN("InternetConnect failed: %d\n", GetLastError());
415         hres = INET_E_CANNOT_CONNECT;
416         goto done;
417     }
418
419     num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
420     hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
421                                            accept_mimes,
422                                            num, &num);
423     if (hres != S_OK)
424     {
425         WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
426         hres = INET_E_NO_VALID_MEDIA;
427         goto done;
428     }
429     accept_mimes[num] = 0;
430
431     if (This->grfBINDF & BINDF_NOWRITECACHE)
432         request_flags |= INTERNET_FLAG_NO_CACHE_WRITE;
433     This->request = HttpOpenRequestW(This->connect, This->bind_info.dwBindVerb < BINDVERB_CUSTOM ?
434                                      wszBindVerb[This->bind_info.dwBindVerb] :
435                                      This->bind_info.szCustomVerb,
436                                      path, NULL, NULL, (LPCWSTR *)accept_mimes,
437                                      request_flags, (DWORD)This);
438     if (!This->request)
439     {
440         WARN("HttpOpenRequest failed: %d\n", GetLastError());
441         hres = INET_E_RESOURCE_NOT_FOUND;
442         goto done;
443     }
444
445     hres = IInternetProtocolSink_QueryInterface(This->protocol_sink, &IID_IServiceProvider,
446                                                 (void **)&service_provider);
447     if (hres != S_OK)
448     {
449         WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
450         goto done;
451     }
452
453     hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
454                                          &IID_IHttpNegotiate, (void **)&This->http_negotiate);
455     if (hres != S_OK)
456     {
457         WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
458         goto done;
459     }
460
461     hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, szUrl, wszHeaders,
462                                                0, &addl_header);
463     if (hres != S_OK)
464     {
465         WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
466         goto done;
467     }
468     else if (addl_header == NULL)
469     {
470         This->full_header = (LPWSTR)wszHeaders;
471     }
472     else
473     {
474         int len_addl_header = lstrlenW(addl_header);
475         This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
476         if (!This->full_header)
477         {
478             WARN("Out of memory\n");
479             hres = E_OUTOFMEMORY;
480             goto done;
481         }
482         lstrcpyW(This->full_header, addl_header);
483         lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
484     }
485
486     hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
487                                          &IID_IHttpNegotiate2, (void **)&http_negotiate2);
488     if (hres != S_OK)
489     {
490         WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
491         /* No goto done as per native */
492     }
493     else
494     {
495         len = sizeof(security_id)/sizeof(security_id[0]);
496         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
497         if (hres != S_OK)
498         {
499             WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
500             /* No goto done as per native */
501         }
502     }
503
504     /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
505
506     if (This->bind_info.dwBindVerb == BINDVERB_POST)
507     {
508         num = 0;
509         hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_POST_COOKIE, &post_cookie,
510                                                1, &num);
511         if (hres == S_OK && num &&
512             !InternetSetOptionW(This->request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
513                                 post_cookie, lstrlenW(post_cookie)))
514         {
515             WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n",
516                  GetLastError());
517         }
518     }
519
520     if (This->bind_info.dwBindVerb != BINDVERB_GET)
521     {
522         /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
523         if (This->bind_info.stgmedData.tymed != TYMED_HGLOBAL)
524             WARN("Expected This->bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
525                  This->bind_info.stgmedData.tymed);
526         else
527             optional = (LPWSTR)This->bind_info.stgmedData.u.hGlobal;
528     }
529     if (!HttpSendRequestW(This->request, This->full_header, lstrlenW(This->full_header),
530                           optional,
531                           optional ? This->bind_info.cbstgmedData : 0) &&
532         GetLastError() != ERROR_IO_PENDING)
533     {
534         WARN("HttpSendRequest failed: %d\n", GetLastError());
535         hres = INET_E_DOWNLOAD_FAILURE;
536         goto done;
537     }
538
539     hres = S_OK;
540 done:
541     if (hres != S_OK)
542     {
543         IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
544         HTTPPROTOCOL_Close(This);
545     }
546
547     CoTaskMemFree(post_cookie);
548     CoTaskMemFree(addl_header);
549     if (http_negotiate2)
550         IHttpNegotiate2_Release(http_negotiate2);
551     if (service_provider)
552         IServiceProvider_Release(service_provider);
553
554     while (num<sizeof(accept_mimes)/sizeof(accept_mimes[0]) &&
555            accept_mimes[num])
556         CoTaskMemFree(accept_mimes[num++]);
557     CoTaskMemFree(user_agent);
558
559     heap_free(pass);
560     heap_free(user);
561     heap_free(path);
562     heap_free(host);
563
564     return hres;
565 }
566
567 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
568 {
569     HttpProtocol *This = PROTOCOL_THIS(iface);
570     DWORD len = sizeof(DWORD), status_code;
571     LPWSTR response_headers = 0, content_type = 0, content_length = 0;
572
573     static const WCHAR wszDefaultContentType[] =
574         {'t','e','x','t','/','h','t','m','l',0};
575
576     TRACE("(%p)->(%p)\n", This, pProtocolData);
577
578     if (!pProtocolData)
579     {
580         WARN("Expected pProtocolData to be non-NULL\n");
581         return S_OK;
582     }
583     else if (!This->request)
584     {
585         WARN("Expected request to be non-NULL\n");
586         return S_OK;
587     }
588     else if (!This->http_negotiate)
589     {
590         WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
591         return S_OK;
592     }
593     else if (!This->protocol_sink)
594     {
595         WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
596         return S_OK;
597     }
598
599     if (pProtocolData->pData == (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
600     {
601         if (!HttpQueryInfoW(This->request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
602                             &status_code, &len, NULL))
603         {
604             WARN("HttpQueryInfo failed: %d\n", GetLastError());
605         }
606         else
607         {
608             len = 0;
609             if ((!HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
610                                  NULL) &&
611                  GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
612                 !(response_headers = heap_alloc(len)) ||
613                 !HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
614                                 NULL))
615             {
616                 WARN("HttpQueryInfo failed: %d\n", GetLastError());
617             }
618             else
619             {
620                 HRESULT hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code,
621                                                          response_headers, NULL, NULL);
622                 if (hres != S_OK)
623                 {
624                     WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
625                     goto done;
626                 }
627             }
628         }
629
630         len = 0;
631         if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL) &&
632              GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
633             !(content_type = heap_alloc(len)) ||
634             !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL))
635         {
636             WARN("HttpQueryInfo failed: %d\n", GetLastError());
637             IInternetProtocolSink_ReportProgress(This->protocol_sink,
638                                                  (This->grfBINDF & BINDF_FROMURLMON) ?
639                                                  BINDSTATUS_MIMETYPEAVAILABLE :
640                                                  BINDSTATUS_RAWMIMETYPE,
641                                                  wszDefaultContentType);
642         }
643         else
644         {
645             /* remove the charset, if present */
646             LPWSTR p = strchrW(content_type, ';');
647             if (p) *p = '\0';
648
649             IInternetProtocolSink_ReportProgress(This->protocol_sink,
650                                                  (This->grfBINDF & BINDF_FROMURLMON) ?
651                                                  BINDSTATUS_MIMETYPEAVAILABLE :
652                                                  BINDSTATUS_RAWMIMETYPE,
653                                                  content_type);
654         }
655
656         len = 0;
657         if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL) &&
658              GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
659             !(content_length = heap_alloc(len)) ||
660             !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL))
661         {
662             WARN("HttpQueryInfo failed: %d\n", GetLastError());
663             This->content_length = 0;
664         }
665         else
666         {
667             This->content_length = atoiW(content_length);
668         }
669
670         This->flags |= FLAG_FIRST_CONTINUE_COMPLETE;
671     }
672
673     if (pProtocolData->pData >= (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
674     {
675         /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
676          * read, so clear the flag _before_ calling so it does not incorrectly get cleared
677          * after the status callback is called */
678         This->flags &= ~FLAG_REQUEST_COMPLETE;
679         if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
680         {
681             if (GetLastError() != ERROR_IO_PENDING)
682             {
683                 This->flags |= FLAG_REQUEST_COMPLETE;
684                 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
685                 HTTPPROTOCOL_ReportResult(This, INET_E_DATA_NOT_AVAILABLE);
686             }
687         }
688         else
689         {
690             This->flags |= FLAG_REQUEST_COMPLETE;
691             HTTPPROTOCOL_ReportData(This);
692         }
693     }
694
695 done:
696     heap_free(response_headers);
697     heap_free(content_type);
698     heap_free(content_length);
699
700     /* Returns S_OK on native */
701     return S_OK;
702 }
703
704 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
705         DWORD dwOptions)
706 {
707     HttpProtocol *This = PROTOCOL_THIS(iface);
708     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
709     return E_NOTIMPL;
710 }
711
712 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
713 {
714     HttpProtocol *This = PROTOCOL_THIS(iface);
715
716     TRACE("(%p)->(%08x)\n", This, dwOptions);
717     HTTPPROTOCOL_Close(This);
718
719     return S_OK;
720 }
721
722 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocol *iface)
723 {
724     HttpProtocol *This = PROTOCOL_THIS(iface);
725     FIXME("(%p)\n", This);
726     return E_NOTIMPL;
727 }
728
729 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocol *iface)
730 {
731     HttpProtocol *This = PROTOCOL_THIS(iface);
732     FIXME("(%p)\n", This);
733     return E_NOTIMPL;
734 }
735
736 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocol *iface, void *pv,
737         ULONG cb, ULONG *pcbRead)
738 {
739     HttpProtocol *This = PROTOCOL_THIS(iface);
740     ULONG read = 0, len = 0;
741     HRESULT hres = S_FALSE;
742
743     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
744
745     if (!(This->flags & FLAG_REQUEST_COMPLETE))
746     {
747         hres = E_PENDING;
748     }
749     else while (!(This->flags & FLAG_ALL_DATA_READ) &&
750                 read < cb)
751     {
752         if (This->available_bytes == 0)
753         {
754             /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
755              * read, so clear the flag _before_ calling so it does not incorrectly get cleared
756              * after the status callback is called */
757             This->flags &= ~FLAG_REQUEST_COMPLETE;
758             if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
759             {
760                 if (GetLastError() == ERROR_IO_PENDING)
761                 {
762                     hres = E_PENDING;
763                 }
764                 else
765                 {
766                     WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
767                     hres = INET_E_DATA_NOT_AVAILABLE;
768                     HTTPPROTOCOL_ReportResult(This, hres);
769                 }
770                 goto done;
771             }
772             else if (This->available_bytes == 0)
773             {
774                 HTTPPROTOCOL_AllDataRead(This);
775             }
776         }
777         else
778         {
779             if (!InternetReadFile(This->request, ((BYTE *)pv)+read,
780                                   This->available_bytes > cb-read ?
781                                   cb-read : This->available_bytes, &len))
782             {
783                 WARN("InternetReadFile failed: %d\n", GetLastError());
784                 hres = INET_E_DOWNLOAD_FAILURE;
785                 HTTPPROTOCOL_ReportResult(This, hres);
786                 goto done;
787             }
788             else if (len == 0)
789             {
790                 HTTPPROTOCOL_AllDataRead(This);
791             }
792             else
793             {
794                 read += len;
795                 This->current_position += len;
796                 This->available_bytes -= len;
797             }
798         }
799     }
800
801     /* Per MSDN this should be if (read == cb), but native returns S_OK
802      * if any bytes were read, so we will too */
803     if (read)
804         hres = S_OK;
805
806 done:
807     if (pcbRead)
808         *pcbRead = read;
809
810     if (hres != E_PENDING)
811         This->flags |= FLAG_REQUEST_COMPLETE;
812
813     return hres;
814 }
815
816 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
817         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
818 {
819     HttpProtocol *This = PROTOCOL_THIS(iface);
820     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
821     return E_NOTIMPL;
822 }
823
824 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
825 {
826     HttpProtocol *This = PROTOCOL_THIS(iface);
827
828     TRACE("(%p)->(%08x)\n", This, dwOptions);
829
830     if (!InternetLockRequestFile(This->request, &This->lock))
831         WARN("InternetLockRequest failed: %d\n", GetLastError());
832
833     return S_OK;
834 }
835
836 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocol *iface)
837 {
838     HttpProtocol *This = PROTOCOL_THIS(iface);
839
840     TRACE("(%p)\n", This);
841
842     if (This->lock)
843     {
844         if (!InternetUnlockRequestFile(This->lock))
845             WARN("InternetUnlockRequest failed: %d\n", GetLastError());
846         This->lock = 0;
847     }
848
849     return S_OK;
850 }
851
852 #undef PROTOCOL_THIS
853
854 #define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
855
856 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
857 {
858     HttpProtocol *This = PRIORITY_THIS(iface);
859     return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
860 }
861
862 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
863 {
864     HttpProtocol *This = PRIORITY_THIS(iface);
865     return IInternetProtocol_AddRef(PROTOCOL(This));
866 }
867
868 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
869 {
870     HttpProtocol *This = PRIORITY_THIS(iface);
871     return IInternetProtocol_Release(PROTOCOL(This));
872 }
873
874 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
875 {
876     HttpProtocol *This = PRIORITY_THIS(iface);
877
878     TRACE("(%p)->(%d)\n", This, nPriority);
879
880     This->priority = nPriority;
881     return S_OK;
882 }
883
884 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
885 {
886     HttpProtocol *This = PRIORITY_THIS(iface);
887
888     TRACE("(%p)->(%p)\n", This, pnPriority);
889
890     *pnPriority = This->priority;
891     return S_OK;
892 }
893
894 #undef PRIORITY_THIS
895
896 static const IInternetPriorityVtbl HttpPriorityVtbl = {
897     HttpPriority_QueryInterface,
898     HttpPriority_AddRef,
899     HttpPriority_Release,
900     HttpPriority_SetPriority,
901     HttpPriority_GetPriority
902 };
903
904 static const IInternetProtocolVtbl HttpProtocolVtbl = {
905     HttpProtocol_QueryInterface,
906     HttpProtocol_AddRef,
907     HttpProtocol_Release,
908     HttpProtocol_Start,
909     HttpProtocol_Continue,
910     HttpProtocol_Abort,
911     HttpProtocol_Terminate,
912     HttpProtocol_Suspend,
913     HttpProtocol_Resume,
914     HttpProtocol_Read,
915     HttpProtocol_Seek,
916     HttpProtocol_LockRequest,
917     HttpProtocol_UnlockRequest
918 };
919
920 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
921 {
922     HttpProtocol *ret;
923
924     TRACE("(%p %p)\n", pUnkOuter, ppobj);
925
926     URLMON_LockModule();
927
928     ret = heap_alloc(sizeof(HttpProtocol));
929
930     ret->lpInternetProtocolVtbl = &HttpProtocolVtbl;
931     ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
932     ret->flags = ret->grfBINDF = 0;
933     memset(&ret->bind_info, 0, sizeof(ret->bind_info));
934     ret->protocol_sink = 0;
935     ret->http_negotiate = 0;
936     ret->internet = ret->connect = ret->request = 0;
937     ret->full_header = 0;
938     ret->lock = 0;
939     ret->current_position = ret->content_length = ret->available_bytes = 0;
940     ret->priority = 0;
941     ret->ref = 1;
942
943     *ppobj = PROTOCOL(ret);
944     
945     return S_OK;
946 }
947
948 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
949 {
950     FIXME("(%p %p)\n", pUnkOuter, ppobj);
951     return E_NOINTERFACE;
952 }