winhttp: Add stub implementations for WinHttpGetDefaultProxyConfiguration, WinHttpGet...
[wine] / dlls / winhttp / session.c
1 /*
2  * Copyright 2008 Hans Leidekker for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wine/debug.h"
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winhttp.h"
29
30 #include "winhttp_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
33
34 static void set_last_error( DWORD error )
35 {
36     /* FIXME */
37     SetLastError( error );
38 }
39
40 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
41 {
42     FIXME("%p, %u, %p, %u\n", hdr, status, info, buflen);
43 }
44
45 /***********************************************************************
46  *          WinHttpCheckPlatform (winhttp.@)
47  */
48 BOOL WINAPI WinHttpCheckPlatform( void )
49 {
50     TRACE("\n");
51     return TRUE;
52 }
53
54 /***********************************************************************
55  *          session_destroy (internal)
56  */
57 static void session_destroy( object_header_t *hdr )
58 {
59     session_t *session = (session_t *)hdr;
60
61     TRACE("%p\n", session);
62
63     heap_free( session->agent );
64     heap_free( session->proxy_server );
65     heap_free( session->proxy_bypass );
66     heap_free( session->proxy_username );
67     heap_free( session->proxy_password );
68     heap_free( session );
69 }
70
71 static const object_vtbl_t session_vtbl =
72 {
73     session_destroy,
74     NULL,
75     NULL
76 };
77
78 /***********************************************************************
79  *          WinHttpOpen (winhttp.@)
80  */
81 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
82 {
83     session_t *session;
84     HINTERNET handle = NULL;
85
86     TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
87
88     if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
89
90     session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
91     session->hdr.vtbl = &session_vtbl;
92     session->hdr.flags = flags;
93     session->hdr.refs = 1;
94     session->access = access;
95
96     if (agent && !(session->agent = strdupW( agent ))) goto end;
97     if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
98     if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
99
100     if (!(handle = alloc_handle( &session->hdr ))) goto end;
101     session->hdr.handle = handle;
102
103 end:
104     release_object( &session->hdr );
105     TRACE("returning %p\n", handle);
106     return handle;
107 }
108
109 /***********************************************************************
110  *          connect_destroy (internal)
111  */
112 static void connect_destroy( object_header_t *hdr )
113 {
114     connect_t *connect = (connect_t *)hdr;
115
116     TRACE("%p\n", connect);
117
118     release_object( &connect->session->hdr );
119
120     heap_free( connect->hostname );
121     heap_free( connect->servername );
122     heap_free( connect->username );
123     heap_free( connect->password );
124     heap_free( connect );
125 }
126
127 static const object_vtbl_t connect_vtbl =
128 {
129     connect_destroy,
130     NULL,
131     NULL
132 };
133
134 /***********************************************************************
135  *          WinHttpConnect (winhttp.@)
136  */
137 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
138 {
139     connect_t *connect;
140     session_t *session;
141     HINTERNET hconnect = NULL;
142
143     TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
144
145     if (!server)
146     {
147         set_last_error( ERROR_INVALID_PARAMETER );
148         return NULL;
149     }
150     if (!(session = (session_t *)grab_object( hsession )))
151     {
152         set_last_error( ERROR_INVALID_HANDLE );
153         return NULL;
154     }
155     if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
156     {
157         release_object( &session->hdr );
158         set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
159         return NULL;
160     }
161     if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
162     {
163         release_object( &session->hdr );
164         return NULL;
165     }
166     connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
167     connect->hdr.vtbl = &connect_vtbl;
168     connect->hdr.refs = 1;
169     connect->hdr.flags = session->hdr.flags;
170     connect->hdr.callback = session->hdr.callback;
171     connect->hdr.notify_mask = session->hdr.notify_mask;
172
173     addref_object( &session->hdr );
174     connect->session = session;
175     list_add_head( &session->hdr.children, &connect->hdr.entry );
176
177     if (server && !(connect->hostname = strdupW( server ))) goto end;
178     connect->hostport = port;
179
180     if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
181     connect->hdr.handle = hconnect;
182
183     send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
184
185 end:
186     release_object( &connect->hdr );
187
188     TRACE("returning %p\n", hconnect);
189     return hconnect;
190 }
191
192 /***********************************************************************
193  *          request_destroy (internal)
194  */
195 static void request_destroy( object_header_t *hdr )
196 {
197     request_t *request = (request_t *)hdr;
198     int i;
199
200     TRACE("%p\n", request);
201
202     release_object( &request->connect->hdr );
203
204     heap_free( request->verb );
205     heap_free( request->path );
206     heap_free( request->version );
207     heap_free( request->raw_headers );
208     heap_free( request->status_text );
209     for (i = 0; i < request->num_headers; i++)
210     {
211         heap_free( request->headers[i].field );
212         heap_free( request->headers[i].value );
213     }
214     heap_free( request->headers );
215     heap_free( request );
216 }
217
218 static const object_vtbl_t request_vtbl =
219 {
220     request_destroy,
221     NULL,
222     NULL
223 };
224
225 /***********************************************************************
226  *          WinHttpOpenRequest (winhttp.@)
227  */
228 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
229                                      LPCWSTR referrer, LPCWSTR *types, DWORD flags )
230 {
231     request_t *request;
232     connect_t *connect;
233     HINTERNET hrequest = NULL;
234
235     TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
236           debugstr_w(version), debugstr_w(referrer), types, flags);
237
238     if (!(connect = (connect_t *)grab_object( hconnect )))
239     {
240         set_last_error( ERROR_INVALID_HANDLE );
241         return NULL;
242     }
243     if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
244     {
245         release_object( &connect->hdr );
246         set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
247         return NULL;
248     }
249     if (!(request = heap_alloc_zero( sizeof(request_t) )))
250     {
251         release_object( &connect->hdr );
252         return NULL;
253     }
254     request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
255     request->hdr.vtbl = &request_vtbl;
256     request->hdr.refs = 1;
257     request->hdr.flags = flags;
258     request->hdr.callback = connect->hdr.callback;
259     request->hdr.notify_mask = connect->hdr.notify_mask;
260
261     addref_object( &connect->hdr );
262     request->connect = connect;
263     list_add_head( &connect->hdr.children, &request->hdr.entry );
264
265     if (verb && !(request->verb = strdupW( verb ))) goto end;
266     if (object && !(request->path = strdupW( object ))) goto end;
267     if (version && !(request->version = strdupW( version ))) goto end;
268
269     if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
270     request->hdr.handle = hrequest;
271
272     send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
273
274 end:
275     release_object( &request->hdr );
276
277     TRACE("returning %p\n", hrequest);
278     return hrequest;
279 }
280
281 /***********************************************************************
282  *          WinHttpCloseHandle (winhttp.@)
283  */
284 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
285 {
286     object_header_t *hdr;
287
288     TRACE("%p\n", handle);
289
290     if (!(hdr = grab_object( handle )))
291     {
292         set_last_error( ERROR_INVALID_HANDLE );
293         return FALSE;
294     }
295     release_object( hdr );
296     free_handle( handle );
297     return TRUE;
298 }
299
300 /***********************************************************************
301  *          WinHttpDetectAutoProxyConfigUrl (winhttp.@)
302  */
303 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
304 {
305     FIXME("0x%08x, %p\n", flags, url);
306
307     set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
308     return FALSE;
309 }
310
311 /***********************************************************************
312  *          WinHttpGetDefaultProxyConfiguration (winhttp.@)
313  */
314 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
315 {
316     FIXME("%p\n", info);
317
318     info->dwAccessType    = WINHTTP_ACCESS_TYPE_NO_PROXY;
319     info->lpszProxy       = NULL;
320     info->lpszProxyBypass = NULL;
321
322     return TRUE;
323 }
324
325 /***********************************************************************
326  *          WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
327  */
328 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
329 {
330     TRACE("%p\n", config);
331
332     if (!config)
333     {
334         set_last_error( ERROR_INVALID_PARAMETER );
335         return FALSE;
336     }
337
338     /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
339
340     FIXME("returning no proxy used\n");
341     config->fAutoDetect       = FALSE;
342     config->lpszAutoConfigUrl = NULL;
343     config->lpszProxy         = NULL;
344     config->lpszProxyBypass   = NULL;
345
346     return TRUE;
347 }
348
349 /***********************************************************************
350  *          WinHttpGetProxyForUrl (winhttp.@)
351  */
352 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
353                                    WINHTTP_PROXY_INFO *info )
354 {
355     FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
356
357     set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
358     return FALSE;
359 }
360
361 /***********************************************************************
362  *          WinHttpSetDefaultProxyConfiguration (winhttp.@)
363  */
364 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
365 {
366     FIXME("%p [%u, %s, %s]\n", info, info->dwAccessType, debugstr_w(info->lpszProxy),
367           debugstr_w(info->lpszProxyBypass));
368     return TRUE;
369 }
370
371 /***********************************************************************
372  *          WinHttpSetStatusCallback (winhttp.@)
373  */
374 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
375                                                          DWORD flags, DWORD_PTR reserved )
376 {
377     object_header_t *hdr;
378     WINHTTP_STATUS_CALLBACK ret;
379
380     TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
381
382     if (!(hdr = grab_object( handle )))
383     {
384         set_last_error( ERROR_INVALID_HANDLE );
385         return WINHTTP_INVALID_STATUS_CALLBACK;
386     }
387     ret = hdr->callback;
388     hdr->callback = callback;
389     hdr->notify_mask = flags;
390
391     release_object( hdr );
392     return ret;
393 }