winhttp: Implement some more options.
[wine/multimedia.git] / dlls / winhttp / session.c
blobd429fcb22d75efb453e3854f5afaf3978666a1df
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, 0x%08x, %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 BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
74 switch (option)
76 case WINHTTP_OPTION_PROXY:
78 WINHTTP_PROXY_INFO *pi = buffer;
80 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
81 return TRUE;
83 case WINHTTP_OPTION_REDIRECT_POLICY:
85 DWORD policy = *(DWORD *)buffer;
87 TRACE("0x%x\n", policy);
88 hdr->redirect_policy = policy;
89 return TRUE;
91 default:
92 FIXME("unimplemented option %u\n", option);
93 return TRUE;
97 static const object_vtbl_t session_vtbl =
99 session_destroy,
100 NULL,
101 session_set_option
104 /***********************************************************************
105 * WinHttpOpen (winhttp.@)
107 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
109 session_t *session;
110 HINTERNET handle = NULL;
112 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
114 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
116 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
117 session->hdr.vtbl = &session_vtbl;
118 session->hdr.flags = flags;
119 session->hdr.refs = 1;
120 session->access = access;
122 if (agent && !(session->agent = strdupW( agent ))) goto end;
123 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
124 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
126 if (!(handle = alloc_handle( &session->hdr ))) goto end;
127 session->hdr.handle = handle;
129 end:
130 release_object( &session->hdr );
131 TRACE("returning %p\n", handle);
132 return handle;
135 /***********************************************************************
136 * connect_destroy (internal)
138 static void connect_destroy( object_header_t *hdr )
140 connect_t *connect = (connect_t *)hdr;
142 TRACE("%p\n", connect);
144 release_object( &connect->session->hdr );
146 heap_free( connect->hostname );
147 heap_free( connect->servername );
148 heap_free( connect->username );
149 heap_free( connect->password );
150 heap_free( connect );
153 static const object_vtbl_t connect_vtbl =
155 connect_destroy,
156 NULL,
157 NULL
160 /***********************************************************************
161 * WinHttpConnect (winhttp.@)
163 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
165 connect_t *connect;
166 session_t *session;
167 HINTERNET hconnect = NULL;
169 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
171 if (!server)
173 set_last_error( ERROR_INVALID_PARAMETER );
174 return NULL;
176 if (!(session = (session_t *)grab_object( hsession )))
178 set_last_error( ERROR_INVALID_HANDLE );
179 return NULL;
181 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
183 release_object( &session->hdr );
184 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
185 return NULL;
187 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
189 release_object( &session->hdr );
190 return NULL;
192 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
193 connect->hdr.vtbl = &connect_vtbl;
194 connect->hdr.refs = 1;
195 connect->hdr.flags = session->hdr.flags;
196 connect->hdr.callback = session->hdr.callback;
197 connect->hdr.notify_mask = session->hdr.notify_mask;
198 connect->hdr.context = session->hdr.context;
200 addref_object( &session->hdr );
201 connect->session = session;
202 list_add_head( &session->hdr.children, &connect->hdr.entry );
204 if (server && !(connect->hostname = strdupW( server ))) goto end;
205 connect->hostport = port ? port : (connect->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
207 if (server && !(connect->servername = strdupW( server ))) goto end;
208 connect->serverport = port ? port : (connect->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
210 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
211 connect->hdr.handle = hconnect;
213 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
215 end:
216 release_object( &connect->hdr );
218 TRACE("returning %p\n", hconnect);
219 return hconnect;
222 /***********************************************************************
223 * request_destroy (internal)
225 static void request_destroy( object_header_t *hdr )
227 request_t *request = (request_t *)hdr;
228 int i;
230 TRACE("%p\n", request);
232 release_object( &request->connect->hdr );
234 heap_free( request->verb );
235 heap_free( request->path );
236 heap_free( request->version );
237 heap_free( request->raw_headers );
238 heap_free( request->status_text );
239 for (i = 0; i < request->num_headers; i++)
241 heap_free( request->headers[i].field );
242 heap_free( request->headers[i].value );
244 heap_free( request->headers );
245 heap_free( request );
248 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
250 switch (option)
252 case WINHTTP_OPTION_PROXY:
254 WINHTTP_PROXY_INFO *pi = buffer;
256 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
257 return TRUE;
259 case WINHTTP_OPTION_DISABLE_FEATURE:
261 DWORD disable = *(DWORD *)buffer;
263 TRACE("0x%x\n", disable);
264 hdr->disable_flags &= disable;
265 return TRUE;
267 case WINHTTP_OPTION_AUTOLOGON_POLICY:
269 DWORD policy = *(DWORD *)buffer;
271 TRACE("0x%x\n", policy);
272 hdr->logon_policy = policy;
273 return TRUE;
275 case WINHTTP_OPTION_REDIRECT_POLICY:
277 DWORD policy = *(DWORD *)buffer;
279 TRACE("0x%x\n", policy);
280 hdr->redirect_policy = policy;
281 return TRUE;
283 default:
284 FIXME("unimplemented option %u\n", option);
285 return TRUE;
289 static const object_vtbl_t request_vtbl =
291 request_destroy,
292 NULL,
293 request_set_option
296 /***********************************************************************
297 * WinHttpOpenRequest (winhttp.@)
299 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
300 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
302 request_t *request;
303 connect_t *connect;
304 HINTERNET hrequest = NULL;
306 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
307 debugstr_w(version), debugstr_w(referrer), types, flags);
309 if (!(connect = (connect_t *)grab_object( hconnect )))
311 set_last_error( ERROR_INVALID_HANDLE );
312 return NULL;
314 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
316 release_object( &connect->hdr );
317 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
318 return NULL;
320 if (!(request = heap_alloc_zero( sizeof(request_t) )))
322 release_object( &connect->hdr );
323 return NULL;
325 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
326 request->hdr.vtbl = &request_vtbl;
327 request->hdr.refs = 1;
328 request->hdr.flags = flags;
329 request->hdr.callback = connect->hdr.callback;
330 request->hdr.notify_mask = connect->hdr.notify_mask;
331 request->hdr.context = connect->hdr.context;
333 addref_object( &connect->hdr );
334 request->connect = connect;
335 list_add_head( &connect->hdr.children, &request->hdr.entry );
337 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
339 if (verb && !(request->verb = strdupW( verb ))) goto end;
340 if (object && !(request->path = strdupW( object ))) goto end;
341 if (version && !(request->version = strdupW( version ))) goto end;
343 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
344 request->hdr.handle = hrequest;
346 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
348 end:
349 release_object( &request->hdr );
351 TRACE("returning %p\n", hrequest);
352 return hrequest;
355 /***********************************************************************
356 * WinHttpCloseHandle (winhttp.@)
358 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
360 object_header_t *hdr;
362 TRACE("%p\n", handle);
364 if (!(hdr = grab_object( handle )))
366 set_last_error( ERROR_INVALID_HANDLE );
367 return FALSE;
369 release_object( hdr );
370 free_handle( handle );
371 return TRUE;
374 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
376 BOOL ret = FALSE;
378 switch (option)
380 case WINHTTP_OPTION_CONTEXT_VALUE:
382 *(DWORD_PTR *)buffer = hdr->context;
383 *buflen = sizeof(DWORD_PTR);
384 return TRUE;
386 default:
388 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
389 else FIXME("unimplemented option %u\n", option);
392 return ret;
395 /***********************************************************************
396 * WinHttpQueryOption (winhttp.@)
398 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
400 BOOL ret = FALSE;
401 object_header_t *hdr;
403 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
405 if (!(hdr = grab_object( handle )))
407 set_last_error( ERROR_INVALID_HANDLE );
408 return FALSE;
411 ret = query_option( hdr, option, buffer, buflen );
413 release_object( hdr );
414 return ret;
417 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
419 BOOL ret = TRUE;
421 switch (option)
423 case WINHTTP_OPTION_CONTEXT_VALUE:
425 hdr->context = *(DWORD_PTR *)buffer;
426 return TRUE;
428 default:
430 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
431 else FIXME("unimplemented option %u\n", option);
434 return ret;
437 /***********************************************************************
438 * WinHttpSetOption (winhttp.@)
440 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
442 BOOL ret = FALSE;
443 object_header_t *hdr;
445 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
447 if (!(hdr = grab_object( handle )))
449 set_last_error( ERROR_INVALID_HANDLE );
450 return FALSE;
453 ret = set_option( hdr, option, buffer, buflen );
455 release_object( hdr );
456 return ret;
459 /***********************************************************************
460 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
462 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
464 FIXME("0x%08x, %p\n", flags, url);
466 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
467 return FALSE;
470 /***********************************************************************
471 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
473 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
475 FIXME("%p\n", info);
477 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
478 info->lpszProxy = NULL;
479 info->lpszProxyBypass = NULL;
481 return TRUE;
484 /***********************************************************************
485 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
487 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
489 TRACE("%p\n", config);
491 if (!config)
493 set_last_error( ERROR_INVALID_PARAMETER );
494 return FALSE;
497 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
499 FIXME("returning no proxy used\n");
500 config->fAutoDetect = FALSE;
501 config->lpszAutoConfigUrl = NULL;
502 config->lpszProxy = NULL;
503 config->lpszProxyBypass = NULL;
505 return TRUE;
508 /***********************************************************************
509 * WinHttpGetProxyForUrl (winhttp.@)
511 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
512 WINHTTP_PROXY_INFO *info )
514 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
516 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
517 return FALSE;
520 /***********************************************************************
521 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
523 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
525 FIXME("%p [%u, %s, %s]\n", info, info->dwAccessType, debugstr_w(info->lpszProxy),
526 debugstr_w(info->lpszProxyBypass));
527 return TRUE;
530 /***********************************************************************
531 * WinHttpSetStatusCallback (winhttp.@)
533 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
534 DWORD flags, DWORD_PTR reserved )
536 object_header_t *hdr;
537 WINHTTP_STATUS_CALLBACK ret;
539 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
541 if (!(hdr = grab_object( handle )))
543 set_last_error( ERROR_INVALID_HANDLE );
544 return WINHTTP_INVALID_STATUS_CALLBACK;
546 ret = hdr->callback;
547 hdr->callback = callback;
548 hdr->notify_mask = flags;
550 release_object( hdr );
551 return ret;
554 /***********************************************************************
555 * WinHttpSetTimeouts (winhttp.@)
557 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
559 FIXME("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
560 return TRUE;
563 static const WCHAR wkday[7][4] =
564 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
565 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
566 static const WCHAR month[12][4] =
567 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
568 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
569 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
571 /***********************************************************************
572 * WinHttpTimeFromSystemTime (WININET.@)
574 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
576 static const WCHAR format[] =
577 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
578 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
580 TRACE("%p, %p\n", time, string);
582 if (!time || !string) return FALSE;
584 sprintfW( string, format,
585 wkday[time->wDayOfWeek],
586 time->wDay,
587 month[time->wMonth - 1],
588 time->wYear,
589 time->wHour,
590 time->wMinute,
591 time->wSecond );
593 return TRUE;
596 /***********************************************************************
597 * WinHttpTimeToSystemTime (WININET.@)
599 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
601 unsigned int i;
602 const WCHAR *s = string;
603 WCHAR *end;
605 TRACE("%s, %p\n", debugstr_w(string), time);
607 if (!string || !time) return FALSE;
609 /* Windows does this too */
610 GetSystemTime( time );
612 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
613 * a SYSTEMTIME structure.
616 while (*s && !isalphaW( *s )) s++;
617 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
618 time->wDayOfWeek = 7;
620 for (i = 0; i < 7; i++)
622 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
623 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
624 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
626 time->wDayOfWeek = i;
627 break;
631 if (time->wDayOfWeek > 6) return TRUE;
632 while (*s && !isdigitW( *s )) s++;
633 time->wDay = strtolW( s, &end, 10 );
634 s = end;
636 while (*s && !isalphaW( *s )) s++;
637 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
638 time->wMonth = 0;
640 for (i = 0; i < 12; i++)
642 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
643 toupperW( month[i][1]) == toupperW( s[1] ) &&
644 toupperW( month[i][2]) == toupperW( s[2] ) )
646 time->wMonth = i + 1;
647 break;
650 if (time->wMonth == 0) return TRUE;
652 while (*s && !isdigitW( *s )) s++;
653 if (*s == '\0') return TRUE;
654 time->wYear = strtolW( s, &end, 10 );
655 s = end;
657 while (*s && !isdigitW( *s )) s++;
658 if (*s == '\0') return TRUE;
659 time->wHour = strtolW( s, &end, 10 );
660 s = end;
662 while (*s && !isdigitW( *s )) s++;
663 if (*s == '\0') return TRUE;
664 time->wMinute = strtolW( s, &end, 10 );
665 s = end;
667 while (*s && !isdigitW( *s )) s++;
668 if (*s == '\0') return TRUE;
669 time->wSecond = strtolW( s, &end, 10 );
670 s = end;
672 time->wMilliseconds = 0;
673 return TRUE;