* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
-/*
- * TODO:
- * - Handle redirects as native.
- */
-
#include "urlmon_main.h"
#include "wininet.h"
WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
-/* Flags are needed for, among other things, return HRESULTs from the Read function
- * to conform to native. For example, Read returns:
- *
- * 1. E_PENDING if called before the request has completed,
- * (flags = 0)
- * 2. S_FALSE after all data has been read and S_OK has been reported,
- * (flags = FLAG_REQUEST_COMPLETE | FLAG_ALL_DATA_READ | FLAG_RESULT_REPORTED)
- * 3. INET_E_DATA_NOT_AVAILABLE if InternetQueryDataAvailable fails. The first time
- * this occurs, INET_E_DATA_NOT_AVAILABLE will also be reported to the sink,
- * (flags = FLAG_REQUEST_COMPLETE)
- * but upon subsequent calls to Read no reporting will take place, yet
- * InternetQueryDataAvailable will still be called, and, on failure,
- * INET_E_DATA_NOT_AVAILABLE will still be returned.
- * (flags = FLAG_REQUEST_COMPLETE | FLAG_RESULT_REPORTED)
- *
- * FLAG_FIRST_DATA_REPORTED and FLAG_LAST_DATA_REPORTED are needed for proper
- * ReportData reporting. For example, if OnResponse returns S_OK, Continue will
- * report BSCF_FIRSTDATANOTIFICATION, and when all data has been read Read will
- * report BSCF_INTERMEDIATEDATANOTIFICATION|BSCF_LASTDATANOTIFICATION. However,
- * if OnResponse does not return S_OK, Continue will not report data, and Read
- * will report BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION when all
- * data has been read.
- */
-#define FLAG_REQUEST_COMPLETE 0x1
-#define FLAG_FIRST_CONTINUE_COMPLETE 0x2
-#define FLAG_FIRST_DATA_REPORTED 0x4
-#define FLAG_ALL_DATA_READ 0x8
-#define FLAG_LAST_DATA_REPORTED 0x10
-#define FLAG_RESULT_REPORTED 0x20
-
typedef struct {
- const IInternetProtocolVtbl *lpInternetProtocolVtbl;
+ Protocol base;
+
+ const IInternetProtocolVtbl *lpIInternetProtocolVtbl;
const IInternetPriorityVtbl *lpInternetPriorityVtbl;
+ const IWinInetHttpInfoVtbl *lpWinInetHttpInfoVtbl;
- DWORD flags, grfBINDF;
- BINDINFO bind_info;
- IInternetProtocolSink *protocol_sink;
+ BOOL https;
IHttpNegotiate *http_negotiate;
- HINTERNET internet, connect, request;
LPWSTR full_header;
- HANDLE lock;
- ULONG current_position, content_length, available_bytes;
- LONG priority;
LONG ref;
} HttpProtocol;
+#define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
+#define INETHTTPINFO(x) ((IWinInetHttpInfo*) &(x)->lpWinInetHttpInfoVtbl)
+
/* Default headers from native */
static const WCHAR wszHeaders[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',
':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
-/*
- * Helpers
- */
-
-static void HTTPPROTOCOL_ReportResult(HttpProtocol *This, HRESULT hres)
+static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
{
- if (!(This->flags & FLAG_RESULT_REPORTED) &&
- This->protocol_sink)
- {
- This->flags |= FLAG_RESULT_REPORTED;
- IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
+ LPWSTR ret = NULL;
+ DWORD len = 0;
+ BOOL res;
+
+ res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
+ if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
+ ret = heap_alloc(len);
+ res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
}
+ if(!res) {
+ TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
+ heap_free(ret);
+ return NULL;
+ }
+
+ return ret;
}
-static void HTTPPROTOCOL_ReportData(HttpProtocol *This)
+#define ASYNCPROTOCOL_THIS(iface) DEFINE_THIS2(HttpProtocol, base, iface)
+
+static HRESULT HttpProtocol_open_request(Protocol *prot, LPCWSTR url, DWORD request_flags,
+ HINTERNET internet_session, IInternetBindInfo *bind_info)
{
- DWORD bscf;
- if (!(This->flags & FLAG_LAST_DATA_REPORTED) &&
- This->protocol_sink)
- {
- if (This->flags & FLAG_FIRST_DATA_REPORTED)
- {
- bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
- }
- else
- {
- This->flags |= FLAG_FIRST_DATA_REPORTED;
- bscf = BSCF_FIRSTDATANOTIFICATION;
- }
- if (This->flags & FLAG_ALL_DATA_READ &&
- !(This->flags & FLAG_LAST_DATA_REPORTED))
- {
- This->flags |= FLAG_LAST_DATA_REPORTED;
- bscf |= BSCF_LASTDATANOTIFICATION;
- }
- IInternetProtocolSink_ReportData(This->protocol_sink, bscf,
- This->current_position+This->available_bytes,
- This->content_length);
+ HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
+ LPWSTR addl_header = NULL, post_cookie = NULL, optional = NULL;
+ IServiceProvider *service_provider = NULL;
+ IHttpNegotiate2 *http_negotiate2 = NULL;
+ LPWSTR host, user, pass, path;
+ LPOLESTR accept_mimes[257];
+ URL_COMPONENTSW url_comp;
+ BYTE security_id[512];
+ DWORD len = 0;
+ ULONG num;
+ BOOL res, b;
+ HRESULT hres;
+
+ static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
+ {{'G','E','T',0},
+ {'P','O','S','T',0},
+ {'P','U','T',0}};
+
+ memset(&url_comp, 0, sizeof(url_comp));
+ url_comp.dwStructSize = sizeof(url_comp);
+ url_comp.dwSchemeLength = url_comp.dwHostNameLength = url_comp.dwUrlPathLength = url_comp.dwExtraInfoLength =
+ url_comp.dwUserNameLength = url_comp.dwPasswordLength = 1;
+ if (!InternetCrackUrlW(url, 0, 0, &url_comp))
+ return MK_E_SYNTAX;
+
+ if(!url_comp.nPort)
+ url_comp.nPort = This->https ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
+
+ host = heap_strndupW(url_comp.lpszHostName, url_comp.dwHostNameLength);
+ user = heap_strndupW(url_comp.lpszUserName, url_comp.dwUserNameLength);
+ pass = heap_strndupW(url_comp.lpszPassword, url_comp.dwPasswordLength);
+ This->base.connection = InternetConnectW(internet_session, host, url_comp.nPort, user, pass,
+ INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
+ heap_free(pass);
+ heap_free(user);
+ heap_free(host);
+ if(!This->base.connection) {
+ WARN("InternetConnect failed: %d\n", GetLastError());
+ return INET_E_CANNOT_CONNECT;
}
-}
-static void HTTPPROTOCOL_AllDataRead(HttpProtocol *This)
-{
- if (!(This->flags & FLAG_ALL_DATA_READ))
- This->flags |= FLAG_ALL_DATA_READ;
- HTTPPROTOCOL_ReportData(This);
- HTTPPROTOCOL_ReportResult(This, S_OK);
-}
+ num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
+ hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
+ if(hres != S_OK) {
+ WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
+ return INET_E_NO_VALID_MEDIA;
+ }
+ accept_mimes[num] = 0;
-static void HTTPPROTOCOL_Close(HttpProtocol *This)
-{
- if (This->http_negotiate)
- {
- IHttpNegotiate_Release(This->http_negotiate);
- This->http_negotiate = 0;
+ path = heap_alloc((url_comp.dwUrlPathLength+url_comp.dwExtraInfoLength+1)*sizeof(WCHAR));
+ if(url_comp.dwUrlPathLength)
+ memcpy(path, url_comp.lpszUrlPath, url_comp.dwUrlPathLength*sizeof(WCHAR));
+ if(url_comp.dwExtraInfoLength)
+ memcpy(path+url_comp.dwUrlPathLength, url_comp.lpszExtraInfo, url_comp.dwExtraInfoLength*sizeof(WCHAR));
+ path[url_comp.dwUrlPathLength+url_comp.dwExtraInfoLength] = 0;
+ if(This->https)
+ request_flags |= INTERNET_FLAG_SECURE;
+ This->base.request = HttpOpenRequestW(This->base.connection,
+ This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
+ ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
+ path, NULL, NULL, (LPCWSTR *)accept_mimes, request_flags, (DWORD_PTR)&This->base);
+ heap_free(path);
+ while(num--)
+ CoTaskMemFree(accept_mimes[num]);
+ if (!This->base.request) {
+ WARN("HttpOpenRequest failed: %d\n", GetLastError());
+ return INET_E_RESOURCE_NOT_FOUND;
}
- if (This->request)
- InternetCloseHandle(This->request);
- if (This->connect)
- InternetCloseHandle(This->connect);
- if (This->internet)
- {
- InternetCloseHandle(This->internet);
- This->internet = 0;
+
+ hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
+ (void **)&service_provider);
+ if (hres != S_OK) {
+ WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
+ return hres;
}
- if (This->full_header)
- {
- if (This->full_header != wszHeaders)
- heap_free(This->full_header);
- This->full_header = 0;
+
+ hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
+ &IID_IHttpNegotiate, (void **)&This->http_negotiate);
+ if (hres != S_OK) {
+ WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
+ return hres;
+ }
+
+ hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, wszHeaders,
+ 0, &addl_header);
+ if(hres != S_OK) {
+ WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
+ IServiceProvider_Release(service_provider);
+ return hres;
+ }
+
+ if(addl_header) {
+ int len_addl_header = strlenW(addl_header);
+
+ This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
+
+ lstrcpyW(This->full_header, addl_header);
+ lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
+ CoTaskMemFree(addl_header);
+ }else {
+ This->full_header = (LPWSTR)wszHeaders;
+ }
+
+ hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
+ &IID_IHttpNegotiate2, (void **)&http_negotiate2);
+ IServiceProvider_Release(service_provider);
+ if(hres != S_OK) {
+ WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
+ /* No goto done as per native */
+ }else {
+ len = sizeof(security_id)/sizeof(security_id[0]);
+ hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
+ IHttpNegotiate2_Release(http_negotiate2);
+ if (hres != S_OK)
+ WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
+ }
+
+ /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
+
+ if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
+ num = 0;
+ hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
+ if(hres == S_OK && num) {
+ if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
+ post_cookie, lstrlenW(post_cookie)))
+ WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
+ CoTaskMemFree(post_cookie);
+ }
+ }
+
+ if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
+ /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
+ if (This->base.bind_info.stgmedData.tymed != TYMED_HGLOBAL)
+ WARN("Expected This->base.bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
+ This->base.bind_info.stgmedData.tymed);
+ else
+ optional = (LPWSTR)This->base.bind_info.stgmedData.u.hGlobal;
}
- This->flags = 0;
+
+ b = TRUE;
+ res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
+ if(!res)
+ WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %08x\n", GetLastError());
+
+ res = HttpSendRequestW(This->base.request, This->full_header, lstrlenW(This->full_header),
+ optional, optional ? This->base.bind_info.cbstgmedData : 0);
+ if(!res && GetLastError() != ERROR_IO_PENDING) {
+ WARN("HttpSendRequest failed: %d\n", GetLastError());
+ return INET_E_DOWNLOAD_FAILURE;
+ }
+
+ return S_OK;
}
-static void CALLBACK HTTPPROTOCOL_InternetStatusCallback(
- HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
- LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
+static HRESULT HttpProtocol_start_downloading(Protocol *prot)
{
- HttpProtocol *This = (HttpProtocol *)dwContext;
- PROTOCOLDATA data;
- ULONG ulStatusCode;
-
- switch (dwInternetStatus)
- {
- case INTERNET_STATUS_RESOLVING_NAME:
- ulStatusCode = BINDSTATUS_FINDINGRESOURCE;
- break;
- case INTERNET_STATUS_CONNECTING_TO_SERVER:
- ulStatusCode = BINDSTATUS_CONNECTING;
- break;
- case INTERNET_STATUS_SENDING_REQUEST:
- ulStatusCode = BINDSTATUS_SENDINGREQUEST;
- break;
- case INTERNET_STATUS_REQUEST_COMPLETE:
- This->flags |= FLAG_REQUEST_COMPLETE;
- /* PROTOCOLDATA same as native */
- memset(&data, 0, sizeof(data));
- data.dwState = 0xf1000000;
- if (This->flags & FLAG_FIRST_CONTINUE_COMPLETE)
- data.pData = (LPVOID)BINDSTATUS_ENDDOWNLOADCOMPONENTS;
- else
- data.pData = (LPVOID)BINDSTATUS_DOWNLOADINGDATA;
- if (This->grfBINDF & BINDF_FROMURLMON)
- IInternetProtocolSink_Switch(This->protocol_sink, &data);
- else
- IInternetProtocol_Continue((IInternetProtocol *)This, &data);
- return;
- case INTERNET_STATUS_HANDLE_CREATED:
- IInternetProtocol_AddRef((IInternetProtocol *)This);
- return;
- case INTERNET_STATUS_HANDLE_CLOSING:
- if (*(HINTERNET *)lpvStatusInformation == This->connect)
- {
- This->connect = 0;
- }
- else if (*(HINTERNET *)lpvStatusInformation == This->request)
- {
- This->request = 0;
- if (This->protocol_sink)
- {
- IInternetProtocolSink_Release(This->protocol_sink);
- This->protocol_sink = 0;
- }
- if (This->bind_info.cbSize)
- {
- ReleaseBindInfo(&This->bind_info);
- memset(&This->bind_info, 0, sizeof(This->bind_info));
+ HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
+ LPWSTR content_type, content_length, ranges;
+ DWORD len = sizeof(DWORD);
+ DWORD status_code;
+ BOOL res;
+ HRESULT hres;
+
+ static const WCHAR wszDefaultContentType[] =
+ {'t','e','x','t','/','h','t','m','l',0};
+
+ if(!This->http_negotiate) {
+ WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
+ return S_OK;
+ }
+
+ res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
+ &status_code, &len, NULL);
+ if(res) {
+ LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
+ if(response_headers) {
+ hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
+ NULL, NULL);
+ heap_free(response_headers);
+ if (hres != S_OK) {
+ WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
+ return S_OK;
}
}
- IInternetProtocol_Release((IInternetProtocol *)This);
- return;
- default:
- WARN("Unhandled Internet status callback %d\n", dwInternetStatus);
- return;
+ }else {
+ WARN("HttpQueryInfo failed: %d\n", GetLastError());
+ }
+
+ ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
+ if(ranges) {
+ IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
+ heap_free(ranges);
+ }
+
+ content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
+ if(content_type) {
+ /* remove the charset, if present */
+ LPWSTR p = strchrW(content_type, ';');
+ if (p) *p = '\0';
+
+ IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
+ (This->base.bindf & BINDF_FROMURLMON)
+ ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
+ content_type);
+ heap_free(content_type);
+ }else {
+ WARN("HttpQueryInfo failed: %d\n", GetLastError());
+ IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
+ (This->base.bindf & BINDF_FROMURLMON)
+ ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
+ wszDefaultContentType);
+ }
+
+ content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
+ if(content_length) {
+ This->base.content_length = atoiW(content_length);
+ heap_free(content_length);
}
- IInternetProtocolSink_ReportProgress(This->protocol_sink, ulStatusCode, (LPWSTR)lpvStatusInformation);
+ return S_OK;
}
-static inline LPWSTR strndupW(LPCWSTR string, int len)
+static void HttpProtocol_close_connection(Protocol *prot)
{
- LPWSTR ret = NULL;
- if (string &&
- (ret = heap_alloc((len+1)*sizeof(WCHAR))) != NULL)
- {
- memcpy(ret, string, len*sizeof(WCHAR));
- ret[len] = 0;
+ HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
+
+ if(This->http_negotiate) {
+ IHttpNegotiate_Release(This->http_negotiate);
+ This->http_negotiate = 0;
+ }
+
+ if(This->full_header) {
+ if(This->full_header != wszHeaders)
+ heap_free(This->full_header);
+ This->full_header = 0;
}
- return ret;
}
-/*
- * Interface implementations
- */
+#undef ASYNCPROTOCOL_THIS
-#define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
-#define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
+static const ProtocolVtbl AsyncProtocolVtbl = {
+ HttpProtocol_open_request,
+ HttpProtocol_start_downloading,
+ HttpProtocol_close_connection
+};
-#define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, InternetProtocol, iface)
+#define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, IInternetProtocol, iface)
static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
{
}else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
*ppv = PRIORITY(This);
+ }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
+ TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
+ *ppv = INETHTTPINFO(This);
+ }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
+ TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
+ *ppv = INETHTTPINFO(This);
}
if(*ppv) {
TRACE("(%p) ref=%d\n", This, ref);
if(!ref) {
- HTTPPROTOCOL_Close(This);
+ protocol_close_connection(&This->base);
heap_free(This);
URLMON_UnlockModule();
static HRESULT WINAPI HttpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
- DWORD grfPI, DWORD dwReserved)
+ DWORD grfPI, HANDLE_PTR dwReserved)
{
HttpProtocol *This = PROTOCOL_THIS(iface);
- URL_COMPONENTSW url;
- DWORD len = 0, request_flags = INTERNET_FLAG_KEEP_CONNECTION;
- ULONG num = 0;
- IServiceProvider *service_provider = 0;
- IHttpNegotiate2 *http_negotiate2 = 0;
- LPWSTR host = 0, path = 0, user = 0, pass = 0, addl_header = 0,
- post_cookie = 0, optional = 0;
- BYTE security_id[512];
- LPOLESTR user_agent = NULL, accept_mimes[257];
- HRESULT hres;
- static const WCHAR wszHttp[] = {'h','t','t','p',':'};
- static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
- {{'G','E','T',0},
- {'P','O','S','T',0},
- {'P','U','T',0}};
+ static const WCHAR httpW[] = {'h','t','t','p',':'};
+ static const WCHAR httpsW[] = {'h','t','t','p','s',':'};
- TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
+ TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
pOIBindInfo, grfPI, dwReserved);
- IInternetProtocolSink_AddRef(pOIProtSink);
- This->protocol_sink = pOIProtSink;
-
- memset(&This->bind_info, 0, sizeof(This->bind_info));
- This->bind_info.cbSize = sizeof(BINDINFO);
- hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &This->grfBINDF, &This->bind_info);
- if (hres != S_OK)
- {
- WARN("GetBindInfo failed: %08x\n", hres);
- goto done;
- }
-
- if (lstrlenW(szUrl) < sizeof(wszHttp)/sizeof(WCHAR)
- || memcmp(szUrl, wszHttp, sizeof(wszHttp)))
- {
- hres = MK_E_SYNTAX;
- goto done;
- }
-
- memset(&url, 0, sizeof(url));
- url.dwStructSize = sizeof(url);
- url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength =
- url.dwPasswordLength = 1;
- if (!InternetCrackUrlW(szUrl, 0, 0, &url))
- {
- hres = MK_E_SYNTAX;
- goto done;
- }
- host = strndupW(url.lpszHostName, url.dwHostNameLength);
- path = strndupW(url.lpszUrlPath, url.dwUrlPathLength);
- user = strndupW(url.lpszUserName, url.dwUserNameLength);
- pass = strndupW(url.lpszPassword, url.dwPasswordLength);
- if (!url.nPort)
- url.nPort = INTERNET_DEFAULT_HTTP_PORT;
-
- if(!(This->grfBINDF & BINDF_FROMURLMON))
- IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_DIRECTBIND, NULL);
-
- hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT, &user_agent,
- 1, &num);
- if (hres != S_OK || !num)
- {
- CHAR null_char = 0;
- LPSTR user_agenta = NULL;
- len = 0;
- if ((hres = ObtainUserAgentString(0, &null_char, &len)) != E_OUTOFMEMORY)
- {
- WARN("ObtainUserAgentString failed: %08x\n", hres);
- }
- else if (!(user_agenta = heap_alloc(len*sizeof(CHAR))))
- {
- WARN("Out of memory\n");
- }
- else if ((hres = ObtainUserAgentString(0, user_agenta, &len)) != S_OK)
- {
- WARN("ObtainUserAgentString failed: %08x\n", hres);
- }
- else
- {
- if (!(user_agent = CoTaskMemAlloc((len)*sizeof(WCHAR))))
- WARN("Out of memory\n");
- else
- MultiByteToWideChar(CP_ACP, 0, user_agenta, -1, user_agent, len);
- }
- heap_free(user_agenta);
- }
-
- This->internet = InternetOpenW(user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC);
- if (!This->internet)
- {
- WARN("InternetOpen failed: %d\n", GetLastError());
- hres = INET_E_NO_SESSION;
- goto done;
- }
-
- /* Native does not check for success of next call, so we won't either */
- InternetSetStatusCallbackW(This->internet, HTTPPROTOCOL_InternetStatusCallback);
-
- This->connect = InternetConnectW(This->internet, host, url.nPort, user,
- pass, INTERNET_SERVICE_HTTP, 0, (DWORD)This);
- if (!This->connect)
- {
- WARN("InternetConnect failed: %d\n", GetLastError());
- hres = INET_E_CANNOT_CONNECT;
- goto done;
- }
-
- num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
- hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
- accept_mimes,
- num, &num);
- if (hres != S_OK)
- {
- WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
- hres = INET_E_NO_VALID_MEDIA;
- goto done;
- }
- accept_mimes[num] = 0;
-
- if (This->grfBINDF & BINDF_NOWRITECACHE)
- request_flags |= INTERNET_FLAG_NO_CACHE_WRITE;
- if (This->grfBINDF & BINDF_NEEDFILE)
- request_flags |= INTERNET_FLAG_NEED_FILE;
- This->request = HttpOpenRequestW(This->connect, This->bind_info.dwBindVerb < BINDVERB_CUSTOM ?
- wszBindVerb[This->bind_info.dwBindVerb] :
- This->bind_info.szCustomVerb,
- path, NULL, NULL, (LPCWSTR *)accept_mimes,
- request_flags, (DWORD)This);
- if (!This->request)
- {
- WARN("HttpOpenRequest failed: %d\n", GetLastError());
- hres = INET_E_RESOURCE_NOT_FOUND;
- goto done;
- }
-
- hres = IInternetProtocolSink_QueryInterface(This->protocol_sink, &IID_IServiceProvider,
- (void **)&service_provider);
- if (hres != S_OK)
- {
- WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
- goto done;
- }
-
- hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
- &IID_IHttpNegotiate, (void **)&This->http_negotiate);
- if (hres != S_OK)
- {
- WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
- goto done;
- }
-
- hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, szUrl, wszHeaders,
- 0, &addl_header);
- if (hres != S_OK)
- {
- WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
- goto done;
- }
- else if (addl_header == NULL)
- {
- This->full_header = (LPWSTR)wszHeaders;
- }
- else
- {
- int len_addl_header = lstrlenW(addl_header);
- This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
- if (!This->full_header)
- {
- WARN("Out of memory\n");
- hres = E_OUTOFMEMORY;
- goto done;
- }
- lstrcpyW(This->full_header, addl_header);
- lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
- }
-
- hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
- &IID_IHttpNegotiate2, (void **)&http_negotiate2);
- if (hres != S_OK)
- {
- WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
- /* No goto done as per native */
- }
- else
- {
- len = sizeof(security_id)/sizeof(security_id[0]);
- hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
- if (hres != S_OK)
- {
- WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
- /* No goto done as per native */
- }
- }
-
- /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
-
- if (This->bind_info.dwBindVerb == BINDVERB_POST)
- {
- num = 0;
- hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_POST_COOKIE, &post_cookie,
- 1, &num);
- if (hres == S_OK && num &&
- !InternetSetOptionW(This->request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
- post_cookie, lstrlenW(post_cookie)))
- {
- WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n",
- GetLastError());
- }
- }
-
- if (This->bind_info.dwBindVerb != BINDVERB_GET)
- {
- /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
- if (This->bind_info.stgmedData.tymed != TYMED_HGLOBAL)
- WARN("Expected This->bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
- This->bind_info.stgmedData.tymed);
- else
- optional = (LPWSTR)This->bind_info.stgmedData.u.hGlobal;
- }
- if (!HttpSendRequestW(This->request, This->full_header, lstrlenW(This->full_header),
- optional,
- optional ? This->bind_info.cbstgmedData : 0) &&
- GetLastError() != ERROR_IO_PENDING)
- {
- WARN("HttpSendRequest failed: %d\n", GetLastError());
- hres = INET_E_DOWNLOAD_FAILURE;
- goto done;
- }
-
- hres = S_OK;
-done:
- if (hres != S_OK)
- {
- IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
- HTTPPROTOCOL_Close(This);
- }
-
- CoTaskMemFree(post_cookie);
- CoTaskMemFree(addl_header);
- if (http_negotiate2)
- IHttpNegotiate2_Release(http_negotiate2);
- if (service_provider)
- IServiceProvider_Release(service_provider);
-
- while (num<sizeof(accept_mimes)/sizeof(accept_mimes[0]) &&
- accept_mimes[num])
- CoTaskMemFree(accept_mimes[num++]);
- CoTaskMemFree(user_agent);
-
- heap_free(pass);
- heap_free(user);
- heap_free(path);
- heap_free(host);
+ if(This->https
+ ? strncmpW(szUrl, httpsW, sizeof(httpsW)/sizeof(WCHAR))
+ : strncmpW(szUrl, httpW, sizeof(httpW)/sizeof(WCHAR)))
+ return MK_E_SYNTAX;
- return hres;
+ return protocol_start(&This->base, PROTOCOL(This), szUrl, pOIProtSink, pOIBindInfo);
}
static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
{
HttpProtocol *This = PROTOCOL_THIS(iface);
- DWORD len = sizeof(DWORD), status_code;
- LPWSTR response_headers = 0, content_type = 0, content_length = 0;
-
- static const WCHAR wszDefaultContentType[] =
- {'t','e','x','t','/','h','t','m','l',0};
TRACE("(%p)->(%p)\n", This, pProtocolData);
- if (!pProtocolData)
- {
- WARN("Expected pProtocolData to be non-NULL\n");
- return S_OK;
- }
- else if (!This->request)
- {
- WARN("Expected request to be non-NULL\n");
- return S_OK;
- }
- else if (!This->http_negotiate)
- {
- WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
- return S_OK;
- }
- else if (!This->protocol_sink)
- {
- WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
- return S_OK;
- }
-
- if (pProtocolData->pData == (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
- {
- if (!HttpQueryInfoW(This->request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
- &status_code, &len, NULL))
- {
- WARN("HttpQueryInfo failed: %d\n", GetLastError());
- }
- else
- {
- len = 0;
- if ((!HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
- NULL) &&
- GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
- !(response_headers = heap_alloc(len)) ||
- !HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
- NULL))
- {
- WARN("HttpQueryInfo failed: %d\n", GetLastError());
- }
- else
- {
- HRESULT hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code,
- response_headers, NULL, NULL);
- if (hres != S_OK)
- {
- WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
- goto done;
- }
- }
- }
-
- len = 0;
- if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL) &&
- GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
- !(content_type = heap_alloc(len)) ||
- !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL))
- {
- WARN("HttpQueryInfo failed: %d\n", GetLastError());
- IInternetProtocolSink_ReportProgress(This->protocol_sink,
- (This->grfBINDF & BINDF_FROMURLMON) ?
- BINDSTATUS_MIMETYPEAVAILABLE :
- BINDSTATUS_RAWMIMETYPE,
- wszDefaultContentType);
- }
- else
- {
- /* remove the charset, if present */
- LPWSTR p = strchrW(content_type, ';');
- if (p) *p = '\0';
-
- IInternetProtocolSink_ReportProgress(This->protocol_sink,
- (This->grfBINDF & BINDF_FROMURLMON) ?
- BINDSTATUS_MIMETYPEAVAILABLE :
- BINDSTATUS_RAWMIMETYPE,
- content_type);
- }
-
- len = 0;
- if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL) &&
- GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
- !(content_length = heap_alloc(len)) ||
- !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL))
- {
- WARN("HttpQueryInfo failed: %d\n", GetLastError());
- This->content_length = 0;
- }
- else
- {
- This->content_length = atoiW(content_length);
- }
-
- if(This->grfBINDF & BINDF_NEEDFILE) {
- WCHAR cache_file[MAX_PATH];
- DWORD buflen = sizeof(cache_file);
-
- if(InternetQueryOptionW(This->request, INTERNET_OPTION_DATAFILE_NAME,
- cache_file, &buflen))
- {
- IInternetProtocolSink_ReportProgress(This->protocol_sink,
- BINDSTATUS_CACHEFILENAMEAVAILABLE,
- cache_file);
- }else {
- FIXME("Could not get cache file\n");
- }
- }
-
- This->flags |= FLAG_FIRST_CONTINUE_COMPLETE;
- }
-
- if (pProtocolData->pData >= (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
- {
- /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
- * read, so clear the flag _before_ calling so it does not incorrectly get cleared
- * after the status callback is called */
- This->flags &= ~FLAG_REQUEST_COMPLETE;
- if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
- {
- if (GetLastError() != ERROR_IO_PENDING)
- {
- This->flags |= FLAG_REQUEST_COMPLETE;
- WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
- HTTPPROTOCOL_ReportResult(This, INET_E_DATA_NOT_AVAILABLE);
- }
- }
- else
- {
- This->flags |= FLAG_REQUEST_COMPLETE;
- HTTPPROTOCOL_ReportData(This);
- }
- }
-
-done:
- heap_free(response_headers);
- heap_free(content_type);
- heap_free(content_length);
-
- /* Returns S_OK on native */
- return S_OK;
+ return protocol_continue(&This->base, pProtocolData);
}
static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
HttpProtocol *This = PROTOCOL_THIS(iface);
TRACE("(%p)->(%08x)\n", This, dwOptions);
- HTTPPROTOCOL_Close(This);
+ protocol_close_connection(&This->base);
return S_OK;
}
ULONG cb, ULONG *pcbRead)
{
HttpProtocol *This = PROTOCOL_THIS(iface);
- ULONG read = 0, len = 0;
- HRESULT hres = S_FALSE;
TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
- if (!(This->flags & FLAG_REQUEST_COMPLETE))
- {
- hres = E_PENDING;
- }
- else while (!(This->flags & FLAG_ALL_DATA_READ) &&
- read < cb)
- {
- if (This->available_bytes == 0)
- {
- /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
- * read, so clear the flag _before_ calling so it does not incorrectly get cleared
- * after the status callback is called */
- This->flags &= ~FLAG_REQUEST_COMPLETE;
- if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
- {
- if (GetLastError() == ERROR_IO_PENDING)
- {
- hres = E_PENDING;
- }
- else
- {
- WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
- hres = INET_E_DATA_NOT_AVAILABLE;
- HTTPPROTOCOL_ReportResult(This, hres);
- }
- goto done;
- }
- else if (This->available_bytes == 0)
- {
- HTTPPROTOCOL_AllDataRead(This);
- }
- }
- else
- {
- if (!InternetReadFile(This->request, ((BYTE *)pv)+read,
- This->available_bytes > cb-read ?
- cb-read : This->available_bytes, &len))
- {
- WARN("InternetReadFile failed: %d\n", GetLastError());
- hres = INET_E_DOWNLOAD_FAILURE;
- HTTPPROTOCOL_ReportResult(This, hres);
- goto done;
- }
- else if (len == 0)
- {
- HTTPPROTOCOL_AllDataRead(This);
- }
- else
- {
- read += len;
- This->current_position += len;
- This->available_bytes -= len;
- }
- }
- }
-
- /* Per MSDN this should be if (read == cb), but native returns S_OK
- * if any bytes were read, so we will too */
- if (read)
- hres = S_OK;
-
-done:
- if (pcbRead)
- *pcbRead = read;
-
- if (hres != E_PENDING)
- This->flags |= FLAG_REQUEST_COMPLETE;
-
- return hres;
+ return protocol_read(&This->base, pv, cb, pcbRead);
}
static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
TRACE("(%p)->(%08x)\n", This, dwOptions);
- if (!InternetLockRequestFile(This->request, &This->lock))
- WARN("InternetLockRequest failed: %d\n", GetLastError());
-
- return S_OK;
+ return protocol_lock_request(&This->base);
}
static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocol *iface)
TRACE("(%p)\n", This);
- if (This->lock)
- {
- if (!InternetUnlockRequestFile(This->lock))
- WARN("InternetUnlockRequest failed: %d\n", GetLastError());
- This->lock = 0;
- }
-
- return S_OK;
+ return protocol_unlock_request(&This->base);
}
#undef PROTOCOL_THIS
+static const IInternetProtocolVtbl HttpProtocolVtbl = {
+ HttpProtocol_QueryInterface,
+ HttpProtocol_AddRef,
+ HttpProtocol_Release,
+ HttpProtocol_Start,
+ HttpProtocol_Continue,
+ HttpProtocol_Abort,
+ HttpProtocol_Terminate,
+ HttpProtocol_Suspend,
+ HttpProtocol_Resume,
+ HttpProtocol_Read,
+ HttpProtocol_Seek,
+ HttpProtocol_LockRequest,
+ HttpProtocol_UnlockRequest
+};
+
#define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
TRACE("(%p)->(%d)\n", This, nPriority);
- This->priority = nPriority;
+ This->base.priority = nPriority;
return S_OK;
}
TRACE("(%p)->(%p)\n", This, pnPriority);
- *pnPriority = This->priority;
+ *pnPriority = This->base.priority;
return S_OK;
}
HttpPriority_GetPriority
};
-static const IInternetProtocolVtbl HttpProtocolVtbl = {
- HttpProtocol_QueryInterface,
- HttpProtocol_AddRef,
- HttpProtocol_Release,
- HttpProtocol_Start,
- HttpProtocol_Continue,
- HttpProtocol_Abort,
- HttpProtocol_Terminate,
- HttpProtocol_Suspend,
- HttpProtocol_Resume,
- HttpProtocol_Read,
- HttpProtocol_Seek,
- HttpProtocol_LockRequest,
- HttpProtocol_UnlockRequest
+#define INETINFO_THIS(iface) DEFINE_THIS(HttpProtocol, WinInetHttpInfo, iface)
+
+static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
+{
+ HttpProtocol *This = INETINFO_THIS(iface);
+ return IBinding_QueryInterface(PROTOCOL(This), riid, ppv);
+}
+
+static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
+{
+ HttpProtocol *This = INETINFO_THIS(iface);
+ return IBinding_AddRef(PROTOCOL(This));
+}
+
+static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
+{
+ HttpProtocol *This = INETINFO_THIS(iface);
+ return IBinding_Release(PROTOCOL(This));
+}
+
+static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
+ void *pBuffer, DWORD *pcbBuffer)
+{
+ HttpProtocol *This = INETINFO_THIS(iface);
+ FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
+ return E_NOTIMPL;
+}
+
+static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
+ void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
+{
+ HttpProtocol *This = INETINFO_THIS(iface);
+ FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
+ return E_NOTIMPL;
+}
+
+#undef INETINFO_THIS
+
+static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
+ HttpInfo_QueryInterface,
+ HttpInfo_AddRef,
+ HttpInfo_Release,
+ HttpInfo_QueryOption,
+ HttpInfo_QueryInfo
};
-HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
+static HRESULT create_http_protocol(BOOL https, void **ppobj)
{
HttpProtocol *ret;
- TRACE("(%p %p)\n", pUnkOuter, ppobj);
+ ret = heap_alloc_zero(sizeof(HttpProtocol));
+ if(!ret)
+ return E_OUTOFMEMORY;
- URLMON_LockModule();
+ ret->base.vtbl = &AsyncProtocolVtbl;
+ ret->lpIInternetProtocolVtbl = &HttpProtocolVtbl;
+ ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
+ ret->lpWinInetHttpInfoVtbl = &WinInetHttpInfoVtbl;
- ret = heap_alloc(sizeof(HttpProtocol));
-
- ret->lpInternetProtocolVtbl = &HttpProtocolVtbl;
- ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
- ret->flags = ret->grfBINDF = 0;
- memset(&ret->bind_info, 0, sizeof(ret->bind_info));
- ret->protocol_sink = 0;
- ret->http_negotiate = 0;
- ret->internet = ret->connect = ret->request = 0;
- ret->full_header = 0;
- ret->lock = 0;
- ret->current_position = ret->content_length = ret->available_bytes = 0;
- ret->priority = 0;
+ ret->https = https;
ret->ref = 1;
*ppobj = PROTOCOL(ret);
+ URLMON_LockModule();
return S_OK;
}
+HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
+{
+ TRACE("(%p %p)\n", pUnkOuter, ppobj);
+
+ return create_http_protocol(FALSE, ppobj);
+}
+
HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
{
- FIXME("(%p %p)\n", pUnkOuter, ppobj);
- return E_NOINTERFACE;
+ TRACE("(%p %p)\n", pUnkOuter, ppobj);
+
+ return create_http_protocol(TRUE, ppobj);
}