winhttp: Implement WinHttpSetStatusCallback. Start sending notifications.
[wine/multimedia.git] / dlls / winhttp / session.c
blob26f229ed308ea703e400e16e46e7d1d5e9ed4e98
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 static 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 FIXME("%p, %u, %p, %u\n", hdr, status, info, buflen);
45 /***********************************************************************
46 * WinHttpCheckPlatform (winhttp.@)
48 BOOL WINAPI WinHttpCheckPlatform( void )
50 TRACE("\n");
51 return TRUE;
54 /***********************************************************************
55 * session_destroy (internal)
57 static void session_destroy( object_header_t *hdr )
59 session_t *session = (session_t *)hdr;
61 TRACE("%p\n", session);
63 heap_free( session->agent );
64 heap_free( session->proxy_server );
65 heap_free( session->proxy_bypass );
66 heap_free( session->proxy_username );
67 heap_free( session->proxy_password );
68 heap_free( session );
71 static const object_vtbl_t session_vtbl =
73 session_destroy,
74 NULL,
75 NULL
78 /***********************************************************************
79 * WinHttpOpen (winhttp.@)
81 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
83 session_t *session;
84 HINTERNET handle = NULL;
86 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
88 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
90 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
91 session->hdr.vtbl = &session_vtbl;
92 session->hdr.flags = flags;
93 session->hdr.refs = 1;
94 session->access = access;
96 if (agent && !(session->agent = strdupW( agent ))) goto end;
97 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
98 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
100 if (!(handle = alloc_handle( &session->hdr ))) goto end;
101 session->hdr.handle = handle;
103 end:
104 release_object( &session->hdr );
105 TRACE("returning %p\n", handle);
106 return handle;
109 /***********************************************************************
110 * connect_destroy (internal)
112 static void connect_destroy( object_header_t *hdr )
114 connect_t *connect = (connect_t *)hdr;
116 TRACE("%p\n", connect);
118 release_object( &connect->session->hdr );
120 heap_free( connect->hostname );
121 heap_free( connect->servername );
122 heap_free( connect->username );
123 heap_free( connect->password );
124 heap_free( connect );
127 static const object_vtbl_t connect_vtbl =
129 connect_destroy,
130 NULL,
131 NULL
134 /***********************************************************************
135 * WinHttpConnect (winhttp.@)
137 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
139 connect_t *connect;
140 session_t *session;
141 HINTERNET hconnect = NULL;
143 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
145 if (!server)
147 set_last_error( ERROR_INVALID_PARAMETER );
148 return NULL;
150 if (!(session = (session_t *)grab_object( hsession )))
152 set_last_error( ERROR_INVALID_HANDLE );
153 return NULL;
155 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
157 release_object( &session->hdr );
158 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
159 return NULL;
161 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
163 release_object( &session->hdr );
164 return NULL;
166 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
167 connect->hdr.vtbl = &connect_vtbl;
168 connect->hdr.refs = 1;
169 connect->hdr.flags = session->hdr.flags;
170 connect->hdr.callback = session->hdr.callback;
171 connect->hdr.notify_mask = session->hdr.notify_mask;
173 addref_object( &session->hdr );
174 connect->session = session;
175 list_add_head( &session->hdr.children, &connect->hdr.entry );
177 if (server && !(connect->hostname = strdupW( server ))) goto end;
178 connect->hostport = port;
180 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
181 connect->hdr.handle = hconnect;
183 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
185 end:
186 release_object( &connect->hdr );
188 TRACE("returning %p\n", hconnect);
189 return hconnect;
192 /***********************************************************************
193 * request_destroy (internal)
195 static void request_destroy( object_header_t *hdr )
197 request_t *request = (request_t *)hdr;
198 int i;
200 TRACE("%p\n", request);
202 release_object( &request->connect->hdr );
204 heap_free( request->verb );
205 heap_free( request->path );
206 heap_free( request->version );
207 heap_free( request->raw_headers );
208 heap_free( request->status_text );
209 for (i = 0; i < request->num_headers; i++)
211 heap_free( request->headers[i].field );
212 heap_free( request->headers[i].value );
214 heap_free( request->headers );
215 heap_free( request );
218 static const object_vtbl_t request_vtbl =
220 request_destroy,
221 NULL,
222 NULL
225 /***********************************************************************
226 * WinHttpOpenRequest (winhttp.@)
228 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
229 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
231 request_t *request;
232 connect_t *connect;
233 HINTERNET hrequest = NULL;
235 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
236 debugstr_w(version), debugstr_w(referrer), types, flags);
238 if (!(connect = (connect_t *)grab_object( hconnect )))
240 set_last_error( ERROR_INVALID_HANDLE );
241 return NULL;
243 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
245 release_object( &connect->hdr );
246 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
247 return NULL;
249 if (!(request = heap_alloc_zero( sizeof(request_t) )))
251 release_object( &connect->hdr );
252 return NULL;
254 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
255 request->hdr.vtbl = &request_vtbl;
256 request->hdr.refs = 1;
257 request->hdr.flags = flags;
258 request->hdr.callback = connect->hdr.callback;
259 request->hdr.notify_mask = connect->hdr.notify_mask;
261 addref_object( &connect->hdr );
262 request->connect = connect;
263 list_add_head( &connect->hdr.children, &request->hdr.entry );
265 if (verb && !(request->verb = strdupW( verb ))) goto end;
266 if (object && !(request->path = strdupW( object ))) goto end;
267 if (version && !(request->version = strdupW( version ))) goto end;
269 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
270 request->hdr.handle = hrequest;
272 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
274 end:
275 release_object( &request->hdr );
277 TRACE("returning %p\n", hrequest);
278 return hrequest;
281 /***********************************************************************
282 * WinHttpCloseHandle (winhttp.@)
284 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
286 object_header_t *hdr;
288 TRACE("%p\n", handle);
290 if (!(hdr = grab_object( handle )))
292 set_last_error( ERROR_INVALID_HANDLE );
293 return FALSE;
295 release_object( hdr );
296 free_handle( handle );
297 return TRUE;
300 /***********************************************************************
301 * WinHttpSetStatusCallback (winhttp.@)
303 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
304 DWORD flags, DWORD_PTR reserved )
306 object_header_t *hdr;
307 WINHTTP_STATUS_CALLBACK ret;
309 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
311 if (!(hdr = grab_object( handle )))
313 set_last_error( ERROR_INVALID_HANDLE );
314 return WINHTTP_INVALID_STATUS_CALLBACK;
316 ret = hdr->callback;
317 hdr->callback = callback;
318 hdr->notify_mask = flags;
320 release_object( hdr );
321 return ret;