[MSXML3] Sync with Wine Staging 4.18. CORE-16441
[reactos.git] / dll / win32 / msxml3 / httprequest.c
index 992dad0..c6f9fdb 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include "precomp.h"
+#define COBJMACROS
+#define NONAMELESSUNION
 
-#include <wingdi.h>
-#include <wininet.h>
-#include <mshtml.h>
-#include <objsafe.h>
-#include <docobj.h>
+#include "config.h"
 
-#include <wine/list.h>
+#include <stdarg.h>
+#ifdef HAVE_LIBXML2
+# include <libxml/parser.h>
+# include <libxml/xmlerror.h>
+# include <libxml/encoding.h>
+#endif
+
+#include "windef.h"
+#include "winbase.h"
+#include "wingdi.h"
+#include "wininet.h"
+#include "winreg.h"
+#include "winuser.h"
+#include "ole2.h"
+#include "mshtml.h"
+#include "msxml6.h"
+#include "objsafe.h"
+#include "docobj.h"
+#include "shlwapi.h"
+
+#include "msxml_private.h"
+
+#include "wine/debug.h"
 
 #ifdef HAVE_LIBXML2
 
+WINE_DEFAULT_DEBUG_CHANNEL(msxml);
+
 static const WCHAR colspaceW[] = {':',' ',0};
 static const WCHAR crlfW[] = {'\r','\n',0};
 static const DWORD safety_supported_options =
@@ -52,6 +73,7 @@ typedef struct
     IXMLHTTPRequest IXMLHTTPRequest_iface;
     IObjectWithSite IObjectWithSite_iface;
     IObjectSafety   IObjectSafety_iface;
+    ISupportErrorInfo ISupportErrorInfo_iface;
     LONG ref;
 
     READYSTATE state;
@@ -93,7 +115,6 @@ typedef struct
 {
     httprequest req;
     IServerXMLHTTPRequest IServerXMLHTTPRequest_iface;
-    LONG ref;
 } serverhttp;
 
 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
@@ -111,6 +132,11 @@ static inline httprequest *impl_from_IObjectSafety(IObjectSafety *iface)
     return CONTAINING_RECORD(iface, httprequest, IObjectSafety_iface);
 }
 
+static inline httprequest *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
+{
+    return CONTAINING_RECORD(iface, httprequest, ISupportErrorInfo_iface);
+}
+
 static inline serverhttp *impl_from_IServerXMLHTTPRequest(IServerXMLHTTPRequest *iface)
 {
     return CONTAINING_RECORD(iface, serverhttp, IServerXMLHTTPRequest_iface);
@@ -155,6 +181,19 @@ static void free_response_headers(httprequest *This)
     This->raw_respheaders = NULL;
 }
 
+static void free_request_headers(httprequest *This)
+{
+    struct httpheader *header, *header2;
+
+    LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
+    {
+        list_remove(&header->entry);
+        SysFreeString(header->header);
+        SysFreeString(header->value);
+        heap_free(header);
+    }
+}
+
 struct BindStatusCallback
 {
     IBindStatusCallback IBindStatusCallback_iface;
@@ -358,7 +397,7 @@ static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
     pbindinfo->dwBindVerb = This->request->verb;
     if (This->request->verb == BINDVERB_CUSTOM)
     {
-        pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
+        pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom)+sizeof(WCHAR));
         strcpyW(pbindinfo->szCustomVerb, This->request->custom);
     }
 
@@ -436,9 +475,11 @@ static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *ifac
 {
     static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
         't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
+    static const WCHAR refererW[] = {'R','e','f','e','r','e','r',':',' ',0};
 
     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
     const struct httpheader *entry;
+    BSTR base_uri = NULL;
     WCHAR *buff, *ptr;
     int size = 0;
 
@@ -452,16 +493,39 @@ static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *ifac
     if (!list_empty(&This->request->reqheaders))
         size += This->request->reqheader_size*sizeof(WCHAR);
 
-    if (!size) return S_OK;
+    if (This->request->base_uri)
+    {
+        IUri_GetRawUri(This->request->base_uri, &base_uri);
+        size += SysStringLen(base_uri)*sizeof(WCHAR) + sizeof(refererW) + sizeof(crlfW);
+    }
+
+    if (!size)
+    {
+        SysFreeString(base_uri);
+        return S_OK;
+    }
 
     buff = CoTaskMemAlloc(size);
-    if (!buff) return E_OUTOFMEMORY;
+    if (!buff)
+    {
+        SysFreeString(base_uri);
+        return E_OUTOFMEMORY;
+    }
 
     ptr = buff;
     if (This->request->use_utf8_content)
     {
         lstrcpyW(ptr, content_type_utf8W);
-        ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
+        ptr += ARRAY_SIZE(content_type_utf8W) - 1;
+    }
+
+    if (base_uri)
+    {
+        strcpyW(ptr, refererW);
+        strcatW(ptr, base_uri);
+        strcatW(ptr, crlfW);
+        ptr += strlenW(refererW) + SysStringLen(base_uri) + strlenW(crlfW);
+        SysFreeString(base_uri);
     }
 
     /* user headers */
@@ -471,13 +535,13 @@ static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *ifac
         ptr += SysStringLen(entry->header);
 
         lstrcpyW(ptr, colspaceW);
-        ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
+        ptr += ARRAY_SIZE(colspaceW) - 1;
 
         lstrcpyW(ptr, entry->value);
         ptr += SysStringLen(entry->value);
 
         lstrcpyW(ptr, crlfW);
-        ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
+        ptr += ARRAY_SIZE(crlfW) - 1;
     }
 
     *add_headers = buff;
@@ -695,7 +759,7 @@ static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback *
         case VT_ARRAY|VT_UI1:
         {
             sa = V_ARRAY(body);
-            if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK)
+            if ((hr = SafeArrayAccessData(sa, &ptr)) != S_OK)
             {
                 heap_free(bsc);
                 return hr;
@@ -822,6 +886,7 @@ static HRESULT verify_uri(httprequest *This, IUri *uri)
 static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
         VARIANT async, VARIANT user, VARIANT password)
 {
+    static const WCHAR MethodHeadW[] = {'H','E','A','D',0};
     static const WCHAR MethodGetW[] = {'G','E','T',0};
     static const WCHAR MethodPutW[] = {'P','U','T',0};
     static const WCHAR MethodPostW[] = {'P','O','S','T',0};
@@ -842,6 +907,7 @@ static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
     SysFreeString(This->user);
     SysFreeString(This->password);
     This->user = This->password = NULL;
+    free_request_headers(This);
 
     if (!strcmpiW(method, MethodGetW))
     {
@@ -856,6 +922,7 @@ static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
         This->verb = BINDVERB_POST;
     }
     else if (!strcmpiW(method, MethodDeleteW) ||
+             !strcmpiW(method, MethodHeadW) ||
              !strcmpiW(method, MethodPropFindW))
     {
         This->verb = BINDVERB_CUSTOM;
@@ -963,8 +1030,8 @@ static HRESULT httprequest_setRequestHeader(httprequest *This, BSTR header, BSTR
     entry->value  = SysAllocString(value);
 
     /* header length including null terminator */
-    This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
-                            SysStringLen(entry->value)  + sizeof(crlfW)/sizeof(WCHAR) - 1;
+    This->reqheader_size += SysStringLen(entry->header) + ARRAY_SIZE(colspaceW) +
+        SysStringLen(entry->value) + ARRAY_SIZE(crlfW) - 1;
 
     list_add_head(&This->reqheaders, &entry->entry);
 
@@ -1119,7 +1186,7 @@ static HRESULT httprequest_get_responseXML(httprequest *This, IDispatch **body)
     if (!body) return E_INVALIDARG;
     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
 
-    hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
+    hr = DOMDocument_create(MSXML_DEFAULT, (void**)&doc);
     if (hr != S_OK) return hr;
 
     hr = httprequest_get_responseText(This, &str);
@@ -1227,8 +1294,6 @@ static HRESULT httprequest_put_onreadystatechange(httprequest *This, IDispatch *
 
 static void httprequest_release(httprequest *This)
 {
-    struct httpheader *header, *header2;
-
     if (This->site)
         IUnknown_Release( This->site );
     if (This->uri)
@@ -1240,15 +1305,8 @@ static void httprequest_release(httprequest *This)
     SysFreeString(This->user);
     SysFreeString(This->password);
 
-    /* request headers */
-    LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
-    {
-        list_remove(&header->entry);
-        SysFreeString(header->header);
-        SysFreeString(header->value);
-        heap_free(header);
-    }
-    /* response headers */
+    /* cleanup headers lists */
+    free_request_headers(This);
     free_response_headers(This);
     SysFreeString(This->status_text);
 
@@ -1277,6 +1335,10 @@ static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFI
     {
         *ppvObject = &This->IObjectSafety_iface;
     }
+    else if (IsEqualGUID(&IID_ISupportErrorInfo, riid))
+    {
+        *ppvObject = &This->ISupportErrorInfo_iface;
+    }
     else
     {
         TRACE("Unsupported interface %s\n", debugstr_guid(riid));
@@ -1284,7 +1346,7 @@ static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFI
         return E_NOINTERFACE;
     }
 
-    IXMLHTTPRequest_AddRef( iface );
+    IUnknown_AddRef((IUnknown *)*ppvObject);
 
     return S_OK;
 }
@@ -1509,19 +1571,19 @@ static HRESULT WINAPI
 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
 {
     httprequest *This = impl_from_IObjectWithSite(iface);
-    return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
+    return IXMLHTTPRequest_QueryInterface(&This->IXMLHTTPRequest_iface, riid, ppvObject);
 }
 
 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
 {
     httprequest *This = impl_from_IObjectWithSite(iface);
-    return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
+    return IXMLHTTPRequest_AddRef(&This->IXMLHTTPRequest_iface);
 }
 
 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
 {
     httprequest *This = impl_from_IObjectWithSite(iface);
-    return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
+    return IXMLHTTPRequest_Release(&This->IXMLHTTPRequest_iface);
 }
 
 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
@@ -1549,6 +1611,8 @@ static void get_base_uri(httprequest *This)
         return;
 
     hr = IServiceProvider_QueryService(provider, &SID_SContainerDispatch, &IID_IHTMLDocument2, (void**)&doc);
+    if(FAILED(hr))
+        hr = IServiceProvider_QueryService(provider, &SID_SInternetHostSecurityManager, &IID_IHTMLDocument2, (void**)&doc);
     IServiceProvider_Release(provider);
     if(FAILED(hr))
         return;
@@ -1603,19 +1667,19 @@ static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
 {
     httprequest *This = impl_from_IObjectSafety(iface);
-    return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
+    return IXMLHTTPRequest_QueryInterface(&This->IXMLHTTPRequest_iface, riid, ppv);
 }
 
 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
 {
     httprequest *This = impl_from_IObjectSafety(iface);
-    return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
+    return IXMLHTTPRequest_AddRef(&This->IXMLHTTPRequest_iface);
 }
 
 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
 {
     httprequest *This = impl_from_IObjectSafety(iface);
-    return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
+    return IXMLHTTPRequest_Release(&This->IXMLHTTPRequest_iface);
 }
 
 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
@@ -1655,6 +1719,41 @@ static const IObjectSafetyVtbl ObjectSafetyVtbl = {
     httprequest_Safety_SetInterfaceSafetyOptions
 };
 
+static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **obj)
+{
+    httprequest *This = impl_from_ISupportErrorInfo(iface);
+    return IXMLHTTPRequest_QueryInterface(&This->IXMLHTTPRequest_iface, riid, obj);
+}
+
+static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
+{
+    httprequest *This = impl_from_ISupportErrorInfo(iface);
+    return IXMLHTTPRequest_AddRef(&This->IXMLHTTPRequest_iface);
+}
+
+static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
+{
+    httprequest *This = impl_from_ISupportErrorInfo(iface);
+    return IXMLHTTPRequest_Release(&This->IXMLHTTPRequest_iface);
+}
+
+static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
+{
+    httprequest *This = impl_from_ISupportErrorInfo(iface);
+
+    FIXME("(%p)->(%s)\n", This, debugstr_guid(riid));
+
+    return E_NOTIMPL;
+}
+
+static const ISupportErrorInfoVtbl SupportErrorInfoVtbl =
+{
+    SupportErrorInfo_QueryInterface,
+    SupportErrorInfo_AddRef,
+    SupportErrorInfo_Release,
+    SupportErrorInfo_InterfaceSupportsErrorInfo,
+};
+
 /* IServerXMLHTTPRequest */
 static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest *iface, REFIID riid, void **obj)
 {
@@ -1669,6 +1768,10 @@ static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest
     {
         *obj = iface;
     }
+    else if ( IsEqualGUID( riid, &IID_ISupportErrorInfo ))
+    {
+        *obj = &This->req.ISupportErrorInfo_iface;
+    }
     else
     {
         TRACE("Unsupported interface %s\n", debugstr_guid(riid));
@@ -1676,7 +1779,7 @@ static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest
         return E_NOINTERFACE;
     }
 
-    IServerXMLHTTPRequest_AddRef( iface );
+    IUnknown_AddRef( (IUnknown *)*obj );
 
     return S_OK;
 }
@@ -1684,7 +1787,7 @@ static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest
 static ULONG WINAPI ServerXMLHTTPRequest_AddRef(IServerXMLHTTPRequest *iface)
 {
     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
-    ULONG ref = InterlockedIncrement( &This->ref );
+    ULONG ref = InterlockedIncrement( &This->req.ref );
     TRACE("(%p)->(%u)\n", This, ref );
     return ref;
 }
@@ -1692,7 +1795,7 @@ static ULONG WINAPI ServerXMLHTTPRequest_AddRef(IServerXMLHTTPRequest *iface)
 static ULONG WINAPI ServerXMLHTTPRequest_Release(IServerXMLHTTPRequest *iface)
 {
     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
-    ULONG ref = InterlockedDecrement( &This->ref );
+    ULONG ref = InterlockedDecrement( &This->req.ref );
 
     TRACE("(%p)->(%u)\n", This, ref );
 
@@ -1875,7 +1978,7 @@ static HRESULT WINAPI ServerXMLHTTPRequest_setTimeouts(IServerXMLHTTPRequest *if
 {
     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
     FIXME("(%p)->(%d %d %d %d): stub\n", This, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout);
-    return E_NOTIMPL;
+    return S_OK;
 }
 
 static HRESULT WINAPI ServerXMLHTTPRequest_waitForResponse(IServerXMLHTTPRequest *iface, VARIANT timeout, VARIANT_BOOL *isSuccessful)
@@ -1933,6 +2036,7 @@ static void init_httprequest(httprequest *req)
     req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
     req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
     req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
+    req->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
     req->ref = 1;
 
     req->async = FALSE;
@@ -1958,11 +2062,11 @@ static void init_httprequest(httprequest *req)
     req->safeopt = 0;
 }
 
-HRESULT XMLHTTPRequest_create(IUnknown *outer, void **obj)
+HRESULT XMLHTTPRequest_create(void **obj)
 {
     httprequest *req;
 
-    TRACE("(%p, %p)\n", outer, obj);
+    TRACE("(%p)\n", obj);
 
     req = heap_alloc( sizeof (*req) );
     if( !req )
@@ -1976,11 +2080,11 @@ HRESULT XMLHTTPRequest_create(IUnknown *outer, void **obj)
     return S_OK;
 }
 
-HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
+HRESULT ServerXMLHTTP_create(void **obj)
 {
     serverhttp *req;
 
-    TRACE("(%p, %p)\n", outer, obj);
+    TRACE("(%p)\n", obj);
 
     req = heap_alloc( sizeof (*req) );
     if( !req )
@@ -1988,7 +2092,6 @@ HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
 
     init_httprequest(&req->req);
     req->IServerXMLHTTPRequest_iface.lpVtbl = &ServerXMLHTTPRequestVtbl;
-    req->ref = 1;
 
     *obj = &req->IServerXMLHTTPRequest_iface;
 
@@ -1999,14 +2102,14 @@ HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
 
 #else
 
-HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
+HRESULT XMLHTTPRequest_create(void **ppObj)
 {
     MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
             "libxml2 support was not present at compile time.\n");
     return E_NOTIMPL;
 }
 
-HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
+HRESULT ServerXMLHTTP_create(void **obj)
 {
     MESSAGE("This program tried to use a ServerXMLHTTP object, but\n"
             "libxml2 support was not present at compile time.\n");