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
20 #include "wine/port.h"
21 #include "wine/debug.h"
30 #include "winhttp_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
34 void set_last_error( DWORD error
)
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 )
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
);
73 static const object_vtbl_t session_vtbl
=
80 /***********************************************************************
81 * WinHttpOpen (winhttp.@)
83 HINTERNET WINAPI
WinHttpOpen( LPCWSTR agent
, DWORD access
, LPCWSTR proxy
, LPCWSTR bypass
, DWORD flags
)
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
;
106 release_object( &session
->hdr
);
107 TRACE("returning %p\n", 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
=
136 /***********************************************************************
137 * WinHttpConnect (winhttp.@)
139 HINTERNET WINAPI
WinHttpConnect( HINTERNET hsession
, LPCWSTR server
, INTERNET_PORT port
, DWORD reserved
)
143 HINTERNET hconnect
= NULL
;
145 TRACE("%p, %s, %u, %x\n", hsession
, debugstr_w(server
), port
, reserved
);
149 set_last_error( ERROR_INVALID_PARAMETER
);
152 if (!(session
= (session_t
*)grab_object( hsession
)))
154 set_last_error( ERROR_INVALID_HANDLE
);
157 if (session
->hdr
.type
!= WINHTTP_HANDLE_TYPE_SESSION
)
159 release_object( &session
->hdr
);
160 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
163 if (!(connect
= heap_alloc_zero( sizeof(connect_t
) )))
165 release_object( &session
->hdr
);
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
? port
: (connect
->hdr
.flags
& WINHTTP_FLAG_SECURE
? 443 : 80);
182 if (server
&& !(connect
->servername
= strdupW( server
))) goto end
;
183 connect
->serverport
= port
? port
: (connect
->hdr
.flags
& WINHTTP_FLAG_SECURE
? 443 : 80);
185 if (!(hconnect
= alloc_handle( &connect
->hdr
))) goto end
;
186 connect
->hdr
.handle
= hconnect
;
188 send_callback( &session
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hconnect
, sizeof(hconnect
) );
191 release_object( &connect
->hdr
);
193 TRACE("returning %p\n", hconnect
);
197 /***********************************************************************
198 * request_destroy (internal)
200 static void request_destroy( object_header_t
*hdr
)
202 request_t
*request
= (request_t
*)hdr
;
205 TRACE("%p\n", request
);
207 release_object( &request
->connect
->hdr
);
209 heap_free( request
->verb
);
210 heap_free( request
->path
);
211 heap_free( request
->version
);
212 heap_free( request
->raw_headers
);
213 heap_free( request
->status_text
);
214 for (i
= 0; i
< request
->num_headers
; i
++)
216 heap_free( request
->headers
[i
].field
);
217 heap_free( request
->headers
[i
].value
);
219 heap_free( request
->headers
);
220 heap_free( request
);
223 static const object_vtbl_t request_vtbl
=
230 /***********************************************************************
231 * WinHttpOpenRequest (winhttp.@)
233 HINTERNET WINAPI
WinHttpOpenRequest( HINTERNET hconnect
, LPCWSTR verb
, LPCWSTR object
, LPCWSTR version
,
234 LPCWSTR referrer
, LPCWSTR
*types
, DWORD flags
)
236 static const WCHAR get
[] = {'G','E','T',0};
237 static const WCHAR slash
[] = {'/',0};
238 static const WCHAR http1_1
[] = {'H','T','T','P','/','1','.','1',0};
242 HINTERNET hrequest
= NULL
;
244 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect
, debugstr_w(verb
), debugstr_w(object
),
245 debugstr_w(version
), debugstr_w(referrer
), types
, flags
);
247 if (!(connect
= (connect_t
*)grab_object( hconnect
)))
249 set_last_error( ERROR_INVALID_HANDLE
);
252 if (connect
->hdr
.type
!= WINHTTP_HANDLE_TYPE_CONNECT
)
254 release_object( &connect
->hdr
);
255 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
258 if (!(request
= heap_alloc_zero( sizeof(request_t
) )))
260 release_object( &connect
->hdr
);
263 request
->hdr
.type
= WINHTTP_HANDLE_TYPE_REQUEST
;
264 request
->hdr
.vtbl
= &request_vtbl
;
265 request
->hdr
.refs
= 1;
266 request
->hdr
.flags
= flags
;
267 request
->hdr
.callback
= connect
->hdr
.callback
;
268 request
->hdr
.notify_mask
= connect
->hdr
.notify_mask
;
270 addref_object( &connect
->hdr
);
271 request
->connect
= connect
;
272 list_add_head( &connect
->hdr
.children
, &request
->hdr
.entry
);
274 if (!netconn_init( &request
->netconn
)) goto end
;
276 if (!verb
) verb
= get
;
277 if (!object
) object
= slash
;
278 if (!version
) version
= http1_1
;
280 if (!(request
->verb
= strdupW( verb
))) goto end
;
281 if (!(request
->path
= strdupW( object
))) goto end
;
282 if (!(request
->version
= strdupW( version
))) goto end
;
284 if (!(hrequest
= alloc_handle( &request
->hdr
))) goto end
;
285 request
->hdr
.handle
= hrequest
;
287 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
, &hrequest
, sizeof(hrequest
) );
290 release_object( &request
->hdr
);
292 TRACE("returning %p\n", hrequest
);
296 /***********************************************************************
297 * WinHttpCloseHandle (winhttp.@)
299 BOOL WINAPI
WinHttpCloseHandle( HINTERNET handle
)
301 object_header_t
*hdr
;
303 TRACE("%p\n", handle
);
305 if (!(hdr
= grab_object( handle
)))
307 set_last_error( ERROR_INVALID_HANDLE
);
310 release_object( hdr
);
311 free_handle( handle
);
315 /***********************************************************************
316 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
318 BOOL WINAPI
WinHttpDetectAutoProxyConfigUrl( DWORD flags
, LPWSTR
*url
)
320 FIXME("0x%08x, %p\n", flags
, url
);
322 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED
);
326 /***********************************************************************
327 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
329 BOOL WINAPI
WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
333 info
->dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
334 info
->lpszProxy
= NULL
;
335 info
->lpszProxyBypass
= NULL
;
340 /***********************************************************************
341 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
343 BOOL WINAPI
WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
*config
)
345 TRACE("%p\n", config
);
349 set_last_error( ERROR_INVALID_PARAMETER
);
353 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
355 FIXME("returning no proxy used\n");
356 config
->fAutoDetect
= FALSE
;
357 config
->lpszAutoConfigUrl
= NULL
;
358 config
->lpszProxy
= NULL
;
359 config
->lpszProxyBypass
= NULL
;
364 /***********************************************************************
365 * WinHttpGetProxyForUrl (winhttp.@)
367 BOOL WINAPI
WinHttpGetProxyForUrl( HINTERNET hsession
, LPCWSTR url
, WINHTTP_AUTOPROXY_OPTIONS
*options
,
368 WINHTTP_PROXY_INFO
*info
)
370 FIXME("%p, %s, %p, %p\n", hsession
, debugstr_w(url
), options
, info
);
372 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
);
376 /***********************************************************************
377 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
379 BOOL WINAPI
WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO
*info
)
381 FIXME("%p [%u, %s, %s]\n", info
, info
->dwAccessType
, debugstr_w(info
->lpszProxy
),
382 debugstr_w(info
->lpszProxyBypass
));
386 /***********************************************************************
387 * WinHttpSetStatusCallback (winhttp.@)
389 WINHTTP_STATUS_CALLBACK WINAPI
WinHttpSetStatusCallback( HINTERNET handle
, WINHTTP_STATUS_CALLBACK callback
,
390 DWORD flags
, DWORD_PTR reserved
)
392 object_header_t
*hdr
;
393 WINHTTP_STATUS_CALLBACK ret
;
395 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle
, callback
, flags
, reserved
);
397 if (!(hdr
= grab_object( handle
)))
399 set_last_error( ERROR_INVALID_HANDLE
);
400 return WINHTTP_INVALID_STATUS_CALLBACK
;
403 hdr
->callback
= callback
;
404 hdr
->notify_mask
= flags
;
406 release_object( hdr
);
410 /***********************************************************************
411 * WinHttpSetTimeouts (winhttp.@)
413 BOOL WINAPI
WinHttpSetTimeouts( HINTERNET handle
, int resolve
, int connect
, int send
, int receive
)
415 FIXME("%p, %d, %d, %d, %d\n", handle
, resolve
, connect
, send
, receive
);
419 static const WCHAR wkday
[7][4] =
420 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
421 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
422 static const WCHAR month
[12][4] =
423 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
424 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
425 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
427 /***********************************************************************
428 * WinHttpTimeFromSystemTime (WININET.@)
430 BOOL WINAPI
WinHttpTimeFromSystemTime( const SYSTEMTIME
*time
, LPWSTR string
)
432 static const WCHAR format
[] =
433 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
434 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
436 TRACE("%p, %p\n", time
, string
);
438 if (!time
|| !string
) return FALSE
;
440 sprintfW( string
, format
,
441 wkday
[time
->wDayOfWeek
],
443 month
[time
->wMonth
- 1],
452 /***********************************************************************
453 * WinHttpTimeToSystemTime (WININET.@)
455 BOOL WINAPI
WinHttpTimeToSystemTime( LPCWSTR string
, SYSTEMTIME
*time
)
458 const WCHAR
*s
= string
;
461 TRACE("%s, %p\n", debugstr_w(string
), time
);
463 if (!string
|| !time
) return FALSE
;
465 /* Windows does this too */
466 GetSystemTime( time
);
468 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
469 * a SYSTEMTIME structure.
472 while (*s
&& !isalphaW( *s
)) s
++;
473 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
474 time
->wDayOfWeek
= 7;
476 for (i
= 0; i
< 7; i
++)
478 if (toupperW( wkday
[i
][0] ) == toupperW( s
[0] ) &&
479 toupperW( wkday
[i
][1] ) == toupperW( s
[1] ) &&
480 toupperW( wkday
[i
][2] ) == toupperW( s
[2] ) )
482 time
->wDayOfWeek
= i
;
487 if (time
->wDayOfWeek
> 6) return TRUE
;
488 while (*s
&& !isdigitW( *s
)) s
++;
489 time
->wDay
= strtolW( s
, &end
, 10 );
492 while (*s
&& !isalphaW( *s
)) s
++;
493 if (s
[0] == '\0' || s
[1] == '\0' || s
[2] == '\0') return TRUE
;
496 for (i
= 0; i
< 12; i
++)
498 if (toupperW( month
[i
][0]) == toupperW( s
[0] ) &&
499 toupperW( month
[i
][1]) == toupperW( s
[1] ) &&
500 toupperW( month
[i
][2]) == toupperW( s
[2] ) )
502 time
->wMonth
= i
+ 1;
506 if (time
->wMonth
== 0) return TRUE
;
508 while (*s
&& !isdigitW( *s
)) s
++;
509 if (*s
== '\0') return TRUE
;
510 time
->wYear
= strtolW( s
, &end
, 10 );
513 while (*s
&& !isdigitW( *s
)) s
++;
514 if (*s
== '\0') return TRUE
;
515 time
->wHour
= strtolW( s
, &end
, 10 );
518 while (*s
&& !isdigitW( *s
)) s
++;
519 if (*s
== '\0') return TRUE
;
520 time
->wMinute
= strtolW( s
, &end
, 10 );
523 while (*s
&& !isdigitW( *s
)) s
++;
524 if (*s
== '\0') return TRUE
;
525 time
->wSecond
= strtolW( s
, &end
, 10 );
528 time
->wMilliseconds
= 0;