Include sys/filio.h to get the FIONREAD definition on Solaris.
[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 "winhttp.h"
28
29 #include "winhttp_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
32
33 void set_last_error( DWORD error )
34 {
35     /* FIXME */
36     SetLastError( error );
37 }
38
39 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
40 {
41     TRACE("%p, %u, %p, %u\n", hdr, status, info, buflen);
42
43     if (hdr->notify_mask & status) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
44 }
45
46 /***********************************************************************
47  *          WinHttpCheckPlatform (winhttp.@)
48  */
49 BOOL WINAPI WinHttpCheckPlatform( void )
50 {
51     TRACE("\n");
52     return TRUE;
53 }
54
55 /***********************************************************************
56  *          session_destroy (internal)
57  */
58 static void session_destroy( object_header_t *hdr )
59 {
60     session_t *session = (session_t *)hdr;
61
62     TRACE("%p\n", session);
63
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 );
70 }
71
72 static const object_vtbl_t session_vtbl =
73 {
74     session_destroy,
75     NULL,
76     NULL
77 };
78
79 /***********************************************************************
80  *          WinHttpOpen (winhttp.@)
81  */
82 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
83 {
84     session_t *session;
85     HINTERNET handle = NULL;
86
87     TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
88
89     if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
90
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;
96
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;
100
101     if (!(handle = alloc_handle( &session->hdr ))) goto end;
102     session->hdr.handle = handle;
103
104 end:
105     release_object( &session->hdr );
106     TRACE("returning %p\n", handle);
107     return handle;
108 }
109
110 /***********************************************************************
111  *          connect_destroy (internal)
112  */
113 static void connect_destroy( object_header_t *hdr )
114 {
115     connect_t *connect = (connect_t *)hdr;
116
117     TRACE("%p\n", connect);
118
119     release_object( &connect->session->hdr );
120
121     heap_free( connect->hostname );
122     heap_free( connect->servername );
123     heap_free( connect->username );
124     heap_free( connect->password );
125     heap_free( connect );
126 }
127
128 static const object_vtbl_t connect_vtbl =
129 {
130     connect_destroy,
131     NULL,
132     NULL
133 };
134
135 /***********************************************************************
136  *          WinHttpConnect (winhttp.@)
137  */
138 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
139 {
140     connect_t *connect;
141     session_t *session;
142     HINTERNET hconnect = NULL;
143
144     TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
145
146     if (!server)
147     {
148         set_last_error( ERROR_INVALID_PARAMETER );
149         return NULL;
150     }
151     if (!(session = (session_t *)grab_object( hsession )))
152     {
153         set_last_error( ERROR_INVALID_HANDLE );
154         return NULL;
155     }
156     if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
157     {
158         release_object( &session->hdr );
159         set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
160         return NULL;
161     }
162     if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
163     {
164         release_object( &session->hdr );
165         return NULL;
166     }
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;
173
174     addref_object( &session->hdr );
175     connect->session = session;
176     list_add_head( &session->hdr.children, &connect->hdr.entry );
177
178     if (server && !(connect->hostname = strdupW( server ))) goto end;
179     connect->hostport = port ? port : (connect->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
180
181     if (server && !(connect->servername = strdupW( server ))) goto end;
182     connect->serverport = port ? port : (connect->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
183
184     if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
185     connect->hdr.handle = hconnect;
186
187     send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
188
189 end:
190     release_object( &connect->hdr );
191
192     TRACE("returning %p\n", hconnect);
193     return hconnect;
194 }
195
196 /***********************************************************************
197  *          request_destroy (internal)
198  */
199 static void request_destroy( object_header_t *hdr )
200 {
201     request_t *request = (request_t *)hdr;
202     int i;
203
204     TRACE("%p\n", request);
205
206     release_object( &request->connect->hdr );
207
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++)
214     {
215         heap_free( request->headers[i].field );
216         heap_free( request->headers[i].value );
217     }
218     heap_free( request->headers );
219     heap_free( request );
220 }
221
222 static const object_vtbl_t request_vtbl =
223 {
224     request_destroy,
225     NULL,
226     NULL
227 };
228
229 /***********************************************************************
230  *          WinHttpOpenRequest (winhttp.@)
231  */
232 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
233                                      LPCWSTR referrer, LPCWSTR *types, DWORD flags )
234 {
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};
238
239     request_t *request;
240     connect_t *connect;
241     HINTERNET hrequest = NULL;
242
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);
245
246     if (!(connect = (connect_t *)grab_object( hconnect )))
247     {
248         set_last_error( ERROR_INVALID_HANDLE );
249         return NULL;
250     }
251     if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
252     {
253         release_object( &connect->hdr );
254         set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
255         return NULL;
256     }
257     if (!(request = heap_alloc_zero( sizeof(request_t) )))
258     {
259         release_object( &connect->hdr );
260         return NULL;
261     }
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;
268
269     addref_object( &connect->hdr );
270     request->connect = connect;
271     list_add_head( &connect->hdr.children, &request->hdr.entry );
272
273     if (!netconn_init( &request->netconn )) goto end;
274
275     if (!verb) verb = get;
276     if (!object) object = slash;
277     if (!version) version = http1_1;
278
279     if (!(request->verb = strdupW( verb ))) goto end;
280     if (!(request->path = strdupW( object ))) goto end;
281     if (!(request->version = strdupW( version ))) goto end;
282
283     if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
284     request->hdr.handle = hrequest;
285
286     send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
287
288 end:
289     release_object( &request->hdr );
290
291     TRACE("returning %p\n", hrequest);
292     return hrequest;
293 }
294
295 /***********************************************************************
296  *          WinHttpCloseHandle (winhttp.@)
297  */
298 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
299 {
300     object_header_t *hdr;
301
302     TRACE("%p\n", handle);
303
304     if (!(hdr = grab_object( handle )))
305     {
306         set_last_error( ERROR_INVALID_HANDLE );
307         return FALSE;
308     }
309     release_object( hdr );
310     free_handle( handle );
311     return TRUE;
312 }
313
314 /***********************************************************************
315  *          WinHttpDetectAutoProxyConfigUrl (winhttp.@)
316  */
317 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
318 {
319     FIXME("0x%08x, %p\n", flags, url);
320
321     set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
322     return FALSE;
323 }
324
325 /***********************************************************************
326  *          WinHttpGetDefaultProxyConfiguration (winhttp.@)
327  */
328 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
329 {
330     FIXME("%p\n", info);
331
332     info->dwAccessType    = WINHTTP_ACCESS_TYPE_NO_PROXY;
333     info->lpszProxy       = NULL;
334     info->lpszProxyBypass = NULL;
335
336     return TRUE;
337 }
338
339 /***********************************************************************
340  *          WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
341  */
342 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
343 {
344     TRACE("%p\n", config);
345
346     if (!config)
347     {
348         set_last_error( ERROR_INVALID_PARAMETER );
349         return FALSE;
350     }
351
352     /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
353
354     FIXME("returning no proxy used\n");
355     config->fAutoDetect       = FALSE;
356     config->lpszAutoConfigUrl = NULL;
357     config->lpszProxy         = NULL;
358     config->lpszProxyBypass   = NULL;
359
360     return TRUE;
361 }
362
363 /***********************************************************************
364  *          WinHttpGetProxyForUrl (winhttp.@)
365  */
366 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
367                                    WINHTTP_PROXY_INFO *info )
368 {
369     FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
370
371     set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
372     return FALSE;
373 }
374
375 /***********************************************************************
376  *          WinHttpSetDefaultProxyConfiguration (winhttp.@)
377  */
378 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
379 {
380     FIXME("%p [%u, %s, %s]\n", info, info->dwAccessType, debugstr_w(info->lpszProxy),
381           debugstr_w(info->lpszProxyBypass));
382     return TRUE;
383 }
384
385 /***********************************************************************
386  *          WinHttpSetStatusCallback (winhttp.@)
387  */
388 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
389                                                          DWORD flags, DWORD_PTR reserved )
390 {
391     object_header_t *hdr;
392     WINHTTP_STATUS_CALLBACK ret;
393
394     TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
395
396     if (!(hdr = grab_object( handle )))
397     {
398         set_last_error( ERROR_INVALID_HANDLE );
399         return WINHTTP_INVALID_STATUS_CALLBACK;
400     }
401     ret = hdr->callback;
402     hdr->callback = callback;
403     hdr->notify_mask = flags;
404
405     release_object( hdr );
406     return ret;
407 }
408
409 /***********************************************************************
410  *          WinHttpSetTimeouts (winhttp.@)
411  */
412 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
413 {
414     FIXME("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
415     return TRUE;
416 }
417
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}};
425
426 /***********************************************************************
427  *           WinHttpTimeFromSystemTime (WININET.@)
428  */
429 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
430 {
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};
434
435     TRACE("%p, %p\n", time, string);
436
437     if (!time || !string) return FALSE;
438
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 );
447
448     return TRUE;
449 }
450
451 /***********************************************************************
452  *           WinHttpTimeToSystemTime (WININET.@)
453  */
454 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
455 {
456     unsigned int i;
457     const WCHAR *s = string;
458     WCHAR *end;
459
460     TRACE("%s, %p\n", debugstr_w(string), time);
461
462     if (!string || !time) return FALSE;
463
464     /* Windows does this too */
465     GetSystemTime( time );
466
467     /*  Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
468      *  a SYSTEMTIME structure.
469      */
470
471     while (*s && !isalphaW( *s )) s++;
472     if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
473     time->wDayOfWeek = 7;
474
475     for (i = 0; i < 7; i++)
476     {
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] ) )
480         {
481             time->wDayOfWeek = i;
482             break;
483         }
484     }
485
486     if (time->wDayOfWeek > 6) return TRUE;
487     while (*s && !isdigitW( *s )) s++;
488     time->wDay = strtolW( s, &end, 10 );
489     s = end;
490
491     while (*s && !isalphaW( *s )) s++;
492     if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
493     time->wMonth = 0;
494
495     for (i = 0; i < 12; i++)
496     {
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] ) )
500         {
501             time->wMonth = i + 1;
502             break;
503         }
504     }
505     if (time->wMonth == 0) return TRUE;
506
507     while (*s && !isdigitW( *s )) s++;
508     if (*s == '\0') return TRUE;
509     time->wYear = strtolW( s, &end, 10 );
510     s = end;
511
512     while (*s && !isdigitW( *s )) s++;
513     if (*s == '\0') return TRUE;
514     time->wHour = strtolW( s, &end, 10 );
515     s = end;
516
517     while (*s && !isdigitW( *s )) s++;
518     if (*s == '\0') return TRUE;
519     time->wMinute = strtolW( s, &end, 10 );
520     s = end;
521
522     while (*s && !isdigitW( *s )) s++;
523     if (*s == '\0') return TRUE;
524     time->wSecond = strtolW( s, &end, 10 );
525     s = end;
526
527     time->wMilliseconds = 0;
528     return TRUE;
529 }