winhttp: Add support for secure connections.
[wine/multimedia.git] / dlls / winhttp / session.c
blobdc8c1bfa9c35f50e7d745bf2df4c92f7dcc0c603
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
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.
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.
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
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wine/debug.h"
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winhttp.h"
29 #include "winhttp_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
33 void set_last_error( DWORD error )
35 /* FIXME */
36 SetLastError( error );
39 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
41 TRACE("%p, %u, %p, %u\n", hdr, status, info, buflen);
43 if (hdr->notify_mask & status) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
46 /***********************************************************************
47 * WinHttpCheckPlatform (winhttp.@)
49 BOOL WINAPI WinHttpCheckPlatform( void )
51 TRACE("\n");
52 return TRUE;
55 /***********************************************************************
56 * session_destroy (internal)
58 static void session_destroy( object_header_t *hdr )
60 session_t *session = (session_t *)hdr;
62 TRACE("%p\n", session);
64 heap_free( session->agent );
65 heap_free( session->proxy_server );
66 heap_free( session->proxy_bypass );
67 heap_free( session->proxy_username );
68 heap_free( session->proxy_password );
69 heap_free( session );
72 static const object_vtbl_t session_vtbl =
74 session_destroy,
75 NULL,
76 NULL
79 /***********************************************************************
80 * WinHttpOpen (winhttp.@)
82 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
84 session_t *session;
85 HINTERNET handle = NULL;
87 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
89 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
91 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
92 session->hdr.vtbl = &session_vtbl;
93 session->hdr.flags = flags;
94 session->hdr.refs = 1;
95 session->access = access;
97 if (agent && !(session->agent = strdupW( agent ))) goto end;
98 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
99 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
101 if (!(handle = alloc_handle( &session->hdr ))) goto end;
102 session->hdr.handle = handle;
104 end:
105 release_object( &session->hdr );
106 TRACE("returning %p\n", handle);
107 return handle;
110 /***********************************************************************
111 * connect_destroy (internal)
113 static void connect_destroy( object_header_t *hdr )
115 connect_t *connect = (connect_t *)hdr;
117 TRACE("%p\n", connect);
119 release_object( &connect->session->hdr );
121 heap_free( connect->hostname );
122 heap_free( connect->servername );
123 heap_free( connect->username );
124 heap_free( connect->password );
125 heap_free( connect );
128 static const object_vtbl_t connect_vtbl =
130 connect_destroy,
131 NULL,
132 NULL
135 /***********************************************************************
136 * WinHttpConnect (winhttp.@)
138 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
140 connect_t *connect;
141 session_t *session;
142 HINTERNET hconnect = NULL;
144 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
146 if (!server)
148 set_last_error( ERROR_INVALID_PARAMETER );
149 return NULL;
151 if (!(session = (session_t *)grab_object( hsession )))
153 set_last_error( ERROR_INVALID_HANDLE );
154 return NULL;
156 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
158 release_object( &session->hdr );
159 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
160 return NULL;
162 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
164 release_object( &session->hdr );
165 return NULL;
167 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
168 connect->hdr.vtbl = &connect_vtbl;
169 connect->hdr.refs = 1;
170 connect->hdr.flags = session->hdr.flags;
171 connect->hdr.callback = session->hdr.callback;
172 connect->hdr.notify_mask = session->hdr.notify_mask;
174 addref_object( &session->hdr );
175 connect->session = session;
176 list_add_head( &session->hdr.children, &connect->hdr.entry );
178 if (server && !(connect->hostname = strdupW( server ))) goto end;
179 connect->hostport = port ? port : (connect->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
181 if (server && !(connect->servername = strdupW( server ))) goto end;
182 connect->serverport = port ? port : (connect->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
184 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
185 connect->hdr.handle = hconnect;
187 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
189 end:
190 release_object( &connect->hdr );
192 TRACE("returning %p\n", hconnect);
193 return hconnect;
196 /***********************************************************************
197 * request_destroy (internal)
199 static void request_destroy( object_header_t *hdr )
201 request_t *request = (request_t *)hdr;
202 int i;
204 TRACE("%p\n", request);
206 release_object( &request->connect->hdr );
208 heap_free( request->verb );
209 heap_free( request->path );
210 heap_free( request->version );
211 heap_free( request->raw_headers );
212 heap_free( request->status_text );
213 for (i = 0; i < request->num_headers; i++)
215 heap_free( request->headers[i].field );
216 heap_free( request->headers[i].value );
218 heap_free( request->headers );
219 heap_free( request );
222 static const object_vtbl_t request_vtbl =
224 request_destroy,
225 NULL,
226 NULL
229 /***********************************************************************
230 * WinHttpOpenRequest (winhttp.@)
232 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
233 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
235 static const WCHAR get[] = {'G','E','T',0};
236 static const WCHAR slash[] = {'/',0};
237 static const WCHAR http1_1[] = {'H','T','T','P','/','1','.','1',0};
239 request_t *request;
240 connect_t *connect;
241 HINTERNET hrequest = NULL;
243 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
244 debugstr_w(version), debugstr_w(referrer), types, flags);
246 if (!(connect = (connect_t *)grab_object( hconnect )))
248 set_last_error( ERROR_INVALID_HANDLE );
249 return NULL;
251 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
253 release_object( &connect->hdr );
254 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
255 return NULL;
257 if (!(request = heap_alloc_zero( sizeof(request_t) )))
259 release_object( &connect->hdr );
260 return NULL;
262 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
263 request->hdr.vtbl = &request_vtbl;
264 request->hdr.refs = 1;
265 request->hdr.flags = flags;
266 request->hdr.callback = connect->hdr.callback;
267 request->hdr.notify_mask = connect->hdr.notify_mask;
269 addref_object( &connect->hdr );
270 request->connect = connect;
271 list_add_head( &connect->hdr.children, &request->hdr.entry );
273 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
275 if (!verb) verb = get;
276 if (!object) object = slash;
277 if (!version) version = http1_1;
279 if (!(request->verb = strdupW( verb ))) goto end;
280 if (!(request->path = strdupW( object ))) goto end;
281 if (!(request->version = strdupW( version ))) goto end;
283 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
284 request->hdr.handle = hrequest;
286 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
288 end:
289 release_object( &request->hdr );
291 TRACE("returning %p\n", hrequest);
292 return hrequest;
295 /***********************************************************************
296 * WinHttpCloseHandle (winhttp.@)
298 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
300 object_header_t *hdr;
302 TRACE("%p\n", handle);
304 if (!(hdr = grab_object( handle )))
306 set_last_error( ERROR_INVALID_HANDLE );
307 return FALSE;
309 release_object( hdr );
310 free_handle( handle );
311 return TRUE;
314 /***********************************************************************
315 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
317 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
319 FIXME("0x%08x, %p\n", flags, url);
321 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
322 return FALSE;
325 /***********************************************************************
326 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
328 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
330 FIXME("%p\n", info);
332 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
333 info->lpszProxy = NULL;
334 info->lpszProxyBypass = NULL;
336 return TRUE;
339 /***********************************************************************
340 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
342 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
344 TRACE("%p\n", config);
346 if (!config)
348 set_last_error( ERROR_INVALID_PARAMETER );
349 return FALSE;
352 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
354 FIXME("returning no proxy used\n");
355 config->fAutoDetect = FALSE;
356 config->lpszAutoConfigUrl = NULL;
357 config->lpszProxy = NULL;
358 config->lpszProxyBypass = NULL;
360 return TRUE;
363 /***********************************************************************
364 * WinHttpGetProxyForUrl (winhttp.@)
366 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
367 WINHTTP_PROXY_INFO *info )
369 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
371 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
372 return FALSE;
375 /***********************************************************************
376 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
378 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
380 FIXME("%p [%u, %s, %s]\n", info, info->dwAccessType, debugstr_w(info->lpszProxy),
381 debugstr_w(info->lpszProxyBypass));
382 return TRUE;
385 /***********************************************************************
386 * WinHttpSetStatusCallback (winhttp.@)
388 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
389 DWORD flags, DWORD_PTR reserved )
391 object_header_t *hdr;
392 WINHTTP_STATUS_CALLBACK ret;
394 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
396 if (!(hdr = grab_object( handle )))
398 set_last_error( ERROR_INVALID_HANDLE );
399 return WINHTTP_INVALID_STATUS_CALLBACK;
401 ret = hdr->callback;
402 hdr->callback = callback;
403 hdr->notify_mask = flags;
405 release_object( hdr );
406 return ret;
409 /***********************************************************************
410 * WinHttpSetTimeouts (winhttp.@)
412 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
414 FIXME("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
415 return TRUE;
418 static const WCHAR wkday[7][4] =
419 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
420 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
421 static const WCHAR month[12][4] =
422 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
423 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
424 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
426 /***********************************************************************
427 * WinHttpTimeFromSystemTime (WININET.@)
429 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
431 static const WCHAR format[] =
432 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
433 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
435 TRACE("%p, %p\n", time, string);
437 if (!time || !string) return FALSE;
439 sprintfW( string, format,
440 wkday[time->wDayOfWeek],
441 time->wDay,
442 month[time->wMonth - 1],
443 time->wYear,
444 time->wHour,
445 time->wMinute,
446 time->wSecond );
448 return TRUE;
451 /***********************************************************************
452 * WinHttpTimeToSystemTime (WININET.@)
454 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
456 unsigned int i;
457 const WCHAR *s = string;
458 WCHAR *end;
460 TRACE("%s, %p\n", debugstr_w(string), time);
462 if (!string || !time) return FALSE;
464 /* Windows does this too */
465 GetSystemTime( time );
467 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
468 * a SYSTEMTIME structure.
471 while (*s && !isalphaW( *s )) s++;
472 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
473 time->wDayOfWeek = 7;
475 for (i = 0; i < 7; i++)
477 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
478 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
479 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
481 time->wDayOfWeek = i;
482 break;
486 if (time->wDayOfWeek > 6) return TRUE;
487 while (*s && !isdigitW( *s )) s++;
488 time->wDay = strtolW( s, &end, 10 );
489 s = end;
491 while (*s && !isalphaW( *s )) s++;
492 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
493 time->wMonth = 0;
495 for (i = 0; i < 12; i++)
497 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
498 toupperW( month[i][1]) == toupperW( s[1] ) &&
499 toupperW( month[i][2]) == toupperW( s[2] ) )
501 time->wMonth = i + 1;
502 break;
505 if (time->wMonth == 0) return TRUE;
507 while (*s && !isdigitW( *s )) s++;
508 if (*s == '\0') return TRUE;
509 time->wYear = strtolW( s, &end, 10 );
510 s = end;
512 while (*s && !isdigitW( *s )) s++;
513 if (*s == '\0') return TRUE;
514 time->wHour = strtolW( s, &end, 10 );
515 s = end;
517 while (*s && !isdigitW( *s )) s++;
518 if (*s == '\0') return TRUE;
519 time->wMinute = strtolW( s, &end, 10 );
520 s = end;
522 while (*s && !isdigitW( *s )) s++;
523 if (*s == '\0') return TRUE;
524 time->wSecond = strtolW( s, &end, 10 );
525 s = end;
527 time->wMilliseconds = 0;
528 return TRUE;