push d2761731c253bfe9a5961252b22d8cea093833f5
[wine/hacks.git] / dlls / winhttp / session.c
blobd2bb3407e0f7d38b8275731c055045e7adf152ae
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 "winnls.h"
28 #include "winhttp.h"
30 #include "winhttp_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
34 void set_last_error( DWORD error )
36 /* FIXME */
37 SetLastError( error );
40 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
42 TRACE("%p, %u, %p, %u\n", hdr, status, info, buflen);
44 if (hdr->notify_mask & status) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
47 /***********************************************************************
48 * WinHttpCheckPlatform (winhttp.@)
50 BOOL WINAPI WinHttpCheckPlatform( void )
52 TRACE("\n");
53 return TRUE;
56 /***********************************************************************
57 * session_destroy (internal)
59 static void session_destroy( object_header_t *hdr )
61 session_t *session = (session_t *)hdr;
63 TRACE("%p\n", session);
65 heap_free( session->agent );
66 heap_free( session->proxy_server );
67 heap_free( session->proxy_bypass );
68 heap_free( session->proxy_username );
69 heap_free( session->proxy_password );
70 heap_free( session );
73 static const object_vtbl_t session_vtbl =
75 session_destroy,
76 NULL,
77 NULL
80 /***********************************************************************
81 * WinHttpOpen (winhttp.@)
83 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
85 session_t *session;
86 HINTERNET handle = NULL;
88 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
90 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
92 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
93 session->hdr.vtbl = &session_vtbl;
94 session->hdr.flags = flags;
95 session->hdr.refs = 1;
96 session->access = access;
98 if (agent && !(session->agent = strdupW( agent ))) goto end;
99 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
100 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
102 if (!(handle = alloc_handle( &session->hdr ))) goto end;
103 session->hdr.handle = handle;
105 end:
106 release_object( &session->hdr );
107 TRACE("returning %p\n", handle);
108 return handle;
111 /***********************************************************************
112 * connect_destroy (internal)
114 static void connect_destroy( object_header_t *hdr )
116 connect_t *connect = (connect_t *)hdr;
118 TRACE("%p\n", connect);
120 release_object( &connect->session->hdr );
122 heap_free( connect->hostname );
123 heap_free( connect->servername );
124 heap_free( connect->username );
125 heap_free( connect->password );
126 heap_free( connect );
129 static const object_vtbl_t connect_vtbl =
131 connect_destroy,
132 NULL,
133 NULL
136 /***********************************************************************
137 * WinHttpConnect (winhttp.@)
139 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
141 connect_t *connect;
142 session_t *session;
143 HINTERNET hconnect = NULL;
145 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
147 if (!server)
149 set_last_error( ERROR_INVALID_PARAMETER );
150 return NULL;
152 if (!(session = (session_t *)grab_object( hsession )))
154 set_last_error( ERROR_INVALID_HANDLE );
155 return NULL;
157 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
159 release_object( &session->hdr );
160 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
161 return NULL;
163 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
165 release_object( &session->hdr );
166 return NULL;
168 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
169 connect->hdr.vtbl = &connect_vtbl;
170 connect->hdr.refs = 1;
171 connect->hdr.flags = session->hdr.flags;
172 connect->hdr.callback = session->hdr.callback;
173 connect->hdr.notify_mask = session->hdr.notify_mask;
175 addref_object( &session->hdr );
176 connect->session = session;
177 list_add_head( &session->hdr.children, &connect->hdr.entry );
179 if (server && !(connect->hostname = strdupW( server ))) goto end;
180 connect->hostport = port;
182 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
183 connect->hdr.handle = hconnect;
185 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
187 end:
188 release_object( &connect->hdr );
190 TRACE("returning %p\n", hconnect);
191 return hconnect;
194 /***********************************************************************
195 * request_destroy (internal)
197 static void request_destroy( object_header_t *hdr )
199 request_t *request = (request_t *)hdr;
200 int i;
202 TRACE("%p\n", request);
204 release_object( &request->connect->hdr );
206 heap_free( request->verb );
207 heap_free( request->path );
208 heap_free( request->version );
209 heap_free( request->raw_headers );
210 heap_free( request->status_text );
211 for (i = 0; i < request->num_headers; i++)
213 heap_free( request->headers[i].field );
214 heap_free( request->headers[i].value );
216 heap_free( request->headers );
217 heap_free( request );
220 static const object_vtbl_t request_vtbl =
222 request_destroy,
223 NULL,
224 NULL
227 /***********************************************************************
228 * WinHttpOpenRequest (winhttp.@)
230 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
231 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
233 static const WCHAR get[] = {'G','E','T',0};
234 static const WCHAR slash[] = {'/',0};
235 static const WCHAR http1_1[] = {'H','T','T','P','/','1','.','1',0};
237 request_t *request;
238 connect_t *connect;
239 HINTERNET hrequest = NULL;
241 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
242 debugstr_w(version), debugstr_w(referrer), types, flags);
244 if (!(connect = (connect_t *)grab_object( hconnect )))
246 set_last_error( ERROR_INVALID_HANDLE );
247 return NULL;
249 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
251 release_object( &connect->hdr );
252 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
253 return NULL;
255 if (!(request = heap_alloc_zero( sizeof(request_t) )))
257 release_object( &connect->hdr );
258 return NULL;
260 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
261 request->hdr.vtbl = &request_vtbl;
262 request->hdr.refs = 1;
263 request->hdr.flags = flags;
264 request->hdr.callback = connect->hdr.callback;
265 request->hdr.notify_mask = connect->hdr.notify_mask;
267 addref_object( &connect->hdr );
268 request->connect = connect;
269 list_add_head( &connect->hdr.children, &request->hdr.entry );
271 if (!verb) verb = get;
272 if (!object) object = slash;
273 if (!version) version = http1_1;
275 if (!(request->verb = strdupW( verb ))) goto end;
276 if (!(request->path = strdupW( object ))) goto end;
277 if (!(request->version = strdupW( version ))) goto end;
279 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
280 request->hdr.handle = hrequest;
282 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
284 end:
285 release_object( &request->hdr );
287 TRACE("returning %p\n", hrequest);
288 return hrequest;
291 /***********************************************************************
292 * WinHttpCloseHandle (winhttp.@)
294 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
296 object_header_t *hdr;
298 TRACE("%p\n", handle);
300 if (!(hdr = grab_object( handle )))
302 set_last_error( ERROR_INVALID_HANDLE );
303 return FALSE;
305 release_object( hdr );
306 free_handle( handle );
307 return TRUE;
310 /***********************************************************************
311 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
313 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
315 FIXME("0x%08x, %p\n", flags, url);
317 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
318 return FALSE;
321 /***********************************************************************
322 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
324 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
326 FIXME("%p\n", info);
328 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
329 info->lpszProxy = NULL;
330 info->lpszProxyBypass = NULL;
332 return TRUE;
335 /***********************************************************************
336 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
338 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
340 TRACE("%p\n", config);
342 if (!config)
344 set_last_error( ERROR_INVALID_PARAMETER );
345 return FALSE;
348 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
350 FIXME("returning no proxy used\n");
351 config->fAutoDetect = FALSE;
352 config->lpszAutoConfigUrl = NULL;
353 config->lpszProxy = NULL;
354 config->lpszProxyBypass = NULL;
356 return TRUE;
359 /***********************************************************************
360 * WinHttpGetProxyForUrl (winhttp.@)
362 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
363 WINHTTP_PROXY_INFO *info )
365 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
367 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
368 return FALSE;
371 /***********************************************************************
372 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
374 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
376 FIXME("%p [%u, %s, %s]\n", info, info->dwAccessType, debugstr_w(info->lpszProxy),
377 debugstr_w(info->lpszProxyBypass));
378 return TRUE;
381 /***********************************************************************
382 * WinHttpSetStatusCallback (winhttp.@)
384 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
385 DWORD flags, DWORD_PTR reserved )
387 object_header_t *hdr;
388 WINHTTP_STATUS_CALLBACK ret;
390 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
392 if (!(hdr = grab_object( handle )))
394 set_last_error( ERROR_INVALID_HANDLE );
395 return WINHTTP_INVALID_STATUS_CALLBACK;
397 ret = hdr->callback;
398 hdr->callback = callback;
399 hdr->notify_mask = flags;
401 release_object( hdr );
402 return ret;
405 /***********************************************************************
406 * WinHttpSetTimeouts (winhttp.@)
408 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
410 FIXME("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
411 return TRUE;
414 static const WCHAR wkday[7][4] =
415 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
416 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
417 static const WCHAR month[12][4] =
418 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
419 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
420 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
422 /***********************************************************************
423 * WinHttpTimeFromSystemTime (WININET.@)
425 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
427 static const WCHAR format[] =
428 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
429 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
431 TRACE("%p, %p\n", time, string);
433 if (!time || !string) return FALSE;
435 sprintfW( string, format,
436 wkday[time->wDayOfWeek],
437 time->wDay,
438 month[time->wMonth - 1],
439 time->wYear,
440 time->wHour,
441 time->wMinute,
442 time->wSecond );
444 return TRUE;
447 /***********************************************************************
448 * WinHttpTimeToSystemTime (WININET.@)
450 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
452 unsigned int i;
453 const WCHAR *s = string;
454 WCHAR *end;
456 TRACE("%s, %p\n", debugstr_w(string), time);
458 if (!string || !time) return FALSE;
460 /* Windows does this too */
461 GetSystemTime( time );
463 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
464 * a SYSTEMTIME structure.
467 while (*s && !isalphaW( *s )) s++;
468 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
469 time->wDayOfWeek = 7;
471 for (i = 0; i < 7; i++)
473 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
474 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
475 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
477 time->wDayOfWeek = i;
478 break;
482 if (time->wDayOfWeek > 6) return TRUE;
483 while (*s && !isdigitW( *s )) s++;
484 time->wDay = strtolW( s, &end, 10 );
485 s = end;
487 while (*s && !isalphaW( *s )) s++;
488 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
489 time->wMonth = 0;
491 for (i = 0; i < 12; i++)
493 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
494 toupperW( month[i][1]) == toupperW( s[1] ) &&
495 toupperW( month[i][2]) == toupperW( s[2] ) )
497 time->wMonth = i + 1;
498 break;
501 if (time->wMonth == 0) return TRUE;
503 while (*s && !isdigitW( *s )) s++;
504 if (*s == '\0') return TRUE;
505 time->wYear = strtolW( s, &end, 10 );
506 s = end;
508 while (*s && !isdigitW( *s )) s++;
509 if (*s == '\0') return TRUE;
510 time->wHour = strtolW( s, &end, 10 );
511 s = end;
513 while (*s && !isdigitW( *s )) s++;
514 if (*s == '\0') return TRUE;
515 time->wMinute = strtolW( s, &end, 10 );
516 s = end;
518 while (*s && !isdigitW( *s )) s++;
519 if (*s == '\0') return TRUE;
520 time->wSecond = strtolW( s, &end, 10 );
521 s = end;
523 time->wMilliseconds = 0;
524 return TRUE;