iccvid: Added Slovenian translation.
[wine] / dlls / hlink / extserv.c
1 /*
2  * Copyright 2007 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 "hlink_private.h"
20
21 #include "wine/debug.h"
22 #include "wine/unicode.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(hlink);
25
26 #define DEFINE_THIS(cls,ifc,iface) ((cls*)((BYTE*)(iface)-offsetof(cls,lp ## ifc ## Vtbl)))
27
28 typedef struct {
29     const IUnknownVtbl        *lpIUnknownVtbl;
30     const IAuthenticateVtbl   *lpIAuthenticateVtbl;
31     const IHttpNegotiateVtbl  *lpIHttpNegotiateVtbl;
32
33     LONG ref;
34     IUnknown *outer;
35
36     HWND hwnd;
37     LPWSTR username;
38     LPWSTR password;
39     LPWSTR headers;
40 } ExtensionService;
41
42 #define EXTSERVUNK(x)    ((IUnknown*)       &(x)->lpIUnknownVtbl)
43 #define AUTHENTICATE(x)  ((IAuthenticate*)  &(x)->lpIAuthenticateVtbl)
44 #define HTTPNEGOTIATE(x) ((IHttpNegotiate*) &(x)->lpIHttpNegotiateVtbl)
45
46 #define EXTSERVUNK_THIS(iface)  DEFINE_THIS(ExtensionService, IUnknown, iface)
47
48 static HRESULT WINAPI ExtServUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
49 {
50     ExtensionService *This = EXTSERVUNK_THIS(iface);
51
52     *ppv = NULL;
53
54     if(IsEqualGUID(&IID_IUnknown, riid)) {
55         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
56         *ppv = EXTSERVUNK(This);
57     }else if(IsEqualGUID(&IID_IAuthenticate, riid)) {
58         TRACE("(%p)->(IID_IAuthenticate %p)\n", This, ppv);
59         *ppv = AUTHENTICATE(This);
60     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
61         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
62         *ppv = HTTPNEGOTIATE(This);
63     }
64
65     if(*ppv) {
66         IUnknown_AddRef((IUnknown*)*ppv);
67         return S_OK;
68     }
69
70     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
71     return E_NOINTERFACE;
72 }
73
74 static ULONG WINAPI ExtServUnk_AddRef(IUnknown *iface)
75 {
76     ExtensionService *This = EXTSERVUNK_THIS(iface);
77     LONG ref = InterlockedIncrement(&This->ref);
78
79     TRACE("(%p) ref=%d\n", This, ref);
80
81     return ref;
82 }
83
84 static ULONG WINAPI ExtServUnk_Release(IUnknown *iface)
85 {
86     ExtensionService *This = EXTSERVUNK_THIS(iface);
87     LONG ref = InterlockedDecrement(&This->ref);
88
89     TRACE("(%p) ref=%d\n", This, ref);
90
91     if(!ref) {
92         heap_free(This->username);
93         heap_free(This->password);
94         heap_free(This->headers);
95         heap_free(This);
96     }
97
98     return ref;
99 }
100
101 #undef EXTSERVUNK_THIS
102
103 static const IUnknownVtbl ExtServUnkVtbl = {
104     ExtServUnk_QueryInterface,
105     ExtServUnk_AddRef,
106     ExtServUnk_Release
107 };
108
109 #define AUTHENTICATE_THIS(iface) DEFINE_THIS(ExtensionService, IAuthenticate, iface)
110
111 static HRESULT WINAPI Authenticate_QueryInterface(IAuthenticate *iface, REFIID riid, void **ppv)
112 {
113     ExtensionService *This = AUTHENTICATE_THIS(iface);
114     return IUnknown_QueryInterface(This->outer, riid, ppv);
115 }
116
117 static ULONG WINAPI Authenticate_AddRef(IAuthenticate *iface)
118 {
119     ExtensionService *This = AUTHENTICATE_THIS(iface);
120     return IUnknown_AddRef(This->outer);
121 }
122
123 static ULONG WINAPI Authenticate_Release(IAuthenticate *iface)
124 {
125     ExtensionService *This = AUTHENTICATE_THIS(iface);
126     return IUnknown_Release(This->outer);
127 }
128
129 static HRESULT WINAPI Authenticate_Authenticate(IAuthenticate *iface,
130         HWND *phwnd, LPWSTR *pszUsername, LPWSTR *pszPassword)
131 {
132     ExtensionService *This = AUTHENTICATE_THIS(iface);
133
134     TRACE("(%p)->(%p %p %p)\n", This, phwnd, pszUsername, pszPassword);
135
136     if(!phwnd || !pszUsername || !pszPassword)
137         return E_INVALIDARG;
138
139     *phwnd = This->hwnd;
140     *pszUsername = hlink_co_strdupW(This->username);
141     *pszPassword = hlink_co_strdupW(This->password);
142
143     return S_OK;
144 }
145
146 #undef AUTHENTICATE_THIS
147
148 static const IAuthenticateVtbl AuthenticateVtbl = {
149     Authenticate_QueryInterface,
150     Authenticate_AddRef,
151     Authenticate_Release,
152     Authenticate_Authenticate
153 };
154
155 #define HTTPNEGOTIATE_THIS(iface) DEFINE_THIS(ExtensionService, IHttpNegotiate, iface)
156
157 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate *iface, REFIID riid, void **ppv)
158 {
159     ExtensionService *This = HTTPNEGOTIATE_THIS(iface);
160     return IUnknown_QueryInterface(This->outer, riid, ppv);
161 }
162
163 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate *iface)
164 {
165     ExtensionService *This = HTTPNEGOTIATE_THIS(iface);
166     return IUnknown_AddRef(This->outer);
167 }
168
169 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate *iface)
170 {
171     ExtensionService *This = HTTPNEGOTIATE_THIS(iface);
172     return IUnknown_Release(This->outer);
173 }
174
175 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
176         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
177 {
178     ExtensionService *This = HTTPNEGOTIATE_THIS(iface);
179
180     TRACE("(%p)->(%s %s %x %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders), dwReserved,
181           pszAdditionalHeaders);
182
183     if(!pszAdditionalHeaders)
184         return E_INVALIDARG;
185
186     *pszAdditionalHeaders = hlink_co_strdupW(This->headers);
187     return S_OK;
188 }
189
190 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD dwResponseCode,
191         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
192 {
193     ExtensionService *This = HTTPNEGOTIATE_THIS(iface);
194
195     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
196           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
197
198     *pszAdditionalRequestHeaders = NULL;
199     return S_OK;
200 }
201
202 #undef HTTPNEGOTIATE_THIS
203
204 static const IHttpNegotiateVtbl HttpNegotiateVtbl = {
205     HttpNegotiate_QueryInterface,
206     HttpNegotiate_AddRef,
207     HttpNegotiate_Release,
208     HttpNegotiate_BeginningTransaction,
209     HttpNegotiate_OnResponse
210 };
211
212 /***********************************************************************
213  *             HlinkCreateExtensionServices (HLINK.@)
214  */
215 HRESULT WINAPI HlinkCreateExtensionServices(LPCWSTR pwzAdditionalHeaders,
216         HWND phwnd, LPCWSTR pszUsername, LPCWSTR pszPassword,
217         IUnknown *punkOuter, REFIID riid, void** ppv)
218 {
219     ExtensionService *ret;
220     int len = 0;
221     HRESULT hres = S_OK;
222
223     TRACE("%s %p %s %s %p %s %p\n",debugstr_w(pwzAdditionalHeaders),
224             phwnd, debugstr_w(pszUsername), debugstr_w(pszPassword),
225             punkOuter, debugstr_guid(riid), ppv);
226
227     ret = heap_alloc(sizeof(*ret));
228
229     ret->lpIUnknownVtbl = &ExtServUnkVtbl;
230     ret->lpIAuthenticateVtbl = &AuthenticateVtbl;
231     ret->lpIHttpNegotiateVtbl = &HttpNegotiateVtbl;
232     ret->ref = 1;
233     ret->hwnd = phwnd;
234     ret->username = hlink_strdupW(pszUsername);
235     ret->password = hlink_strdupW(pszPassword);
236
237     if(pwzAdditionalHeaders)
238         len = strlenW(pwzAdditionalHeaders);
239
240     if(len && pwzAdditionalHeaders[len-1] != '\n' && pwzAdditionalHeaders[len-1] != '\r') {
241         static const WCHAR endlW[] = {'\r','\n',0};
242         ret->headers = heap_alloc(len*sizeof(WCHAR) + sizeof(endlW));
243         memcpy(ret->headers, pwzAdditionalHeaders, len*sizeof(WCHAR));
244         memcpy(ret->headers+len, endlW, sizeof(endlW));
245     }else {
246         ret->headers = hlink_strdupW(pwzAdditionalHeaders);
247     }
248
249     if(!punkOuter) {
250         ret->outer = EXTSERVUNK(ret);
251         hres = IUnknown_QueryInterface(EXTSERVUNK(ret), riid, ppv);
252         IUnknown_Release(EXTSERVUNK(ret));
253     }else if(IsEqualGUID(&IID_IUnknown, riid)) {
254         ret->outer = punkOuter;
255         *ppv = EXTSERVUNK(ret);
256     }else {
257         IUnknown_Release(EXTSERVUNK(ret));
258         hres = E_INVALIDARG;
259     }
260
261     return hres;
262 }