webservices: Build a session dictionary for strings not present in the static dictionary.
[wine.git] / dlls / webservices / channel.c
blob29c8108055efc9ef6f57b12bd07f0819d6f94a34
1 /*
2 * Copyright 2016 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 <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "rpc.h"
25 #include "webservices.h"
27 #include "wine/debug.h"
28 #include "wine/list.h"
29 #include "wine/unicode.h"
30 #include "webservices_private.h"
31 #include "sock.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
35 static const struct prop_desc channel_props[] =
37 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE */
38 { sizeof(UINT64), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_MESSAGE_SIZE */
39 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_START_SIZE */
40 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_FLUSH_SIZE */
41 { sizeof(WS_ENCODING), TRUE }, /* WS_CHANNEL_PROPERTY_ENCODING */
42 { sizeof(WS_ENVELOPE_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ENVELOPE_VERSION */
43 { sizeof(WS_ADDRESSING_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ADDRESSING_VERSION */
44 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_SESSION_DICTIONARY_SIZE */
45 { sizeof(WS_CHANNEL_STATE), TRUE }, /* WS_CHANNEL_PROPERTY_STATE */
46 { sizeof(WS_CALLBACK_MODEL), FALSE }, /* WS_CHANNEL_PROPERTY_ASYNC_CALLBACK_MODEL */
47 { sizeof(WS_IP_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_IP_VERSION */
48 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RESOLVE_TIMEOUT */
49 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CONNECT_TIMEOUT */
50 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_TIMEOUT */
51 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_RESPONSE_TIMEOUT */
52 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_TIMEOUT */
53 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CLOSE_TIMEOUT */
54 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_TIMEOUTS */
55 { sizeof(WS_TRANSFER_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_TRANSFER_MODE */
56 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_INTERFACE */
57 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_HOPS */
58 { sizeof(WS_ENDPOINT_ADDRESS), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_ADDRESS */
59 { sizeof(SOCKADDR_STORAGE), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_IP_ADDRESS */
60 { sizeof(ULONGLONG), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_CONNECTION_ID */
61 { sizeof(WS_CUSTOM_CHANNEL_CALLBACKS), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_CALLBACKS */
62 { 0, FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_PARAMETERS */
63 { sizeof(void *), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_INSTANCE */
64 { sizeof(WS_STRING), TRUE }, /* WS_CHANNEL_PROPERTY_TRANSPORT_URL */
65 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_NO_DELAY */
66 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_KEEP_ALIVES */
67 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_TIME */
68 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_INTERVAL */
69 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_HTTP_SERVER_CONNECTIONS */
70 { sizeof(BOOL), TRUE }, /* WS_CHANNEL_PROPERTY_IS_SESSION_SHUT_DOWN */
71 { sizeof(WS_CHANNEL_TYPE), TRUE }, /* WS_CHANNEL_PROPERTY_CHANNEL_TYPE */
72 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_TRIM_BUFFERED_MESSAGE_SIZE */
73 { sizeof(WS_CHANNEL_ENCODER), FALSE }, /* WS_CHANNEL_PROPERTY_ENCODER */
74 { sizeof(WS_CHANNEL_DECODER), FALSE }, /* WS_CHANNEL_PROPERTY_DECODER */
75 { sizeof(WS_PROTECTION_LEVEL), TRUE }, /* WS_CHANNEL_PROPERTY_PROTECTION_LEVEL */
76 { sizeof(WS_COOKIE_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_COOKIE_MODE */
77 { sizeof(WS_HTTP_PROXY_SETTING_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SETTING_MODE */
78 { sizeof(WS_CUSTOM_HTTP_PROXY), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_HTTP_PROXY */
79 { sizeof(WS_HTTP_MESSAGE_MAPPING), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING */
80 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_HTTP_REDIRECT */
81 { sizeof(WS_HTTP_REDIRECT_CALLBACK_CONTEXT), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_REDIRECT_CALLBACK_CONTEXT */
82 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_FAULTS_AS_ERRORS */
83 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ALLOW_UNSECURED_FAULTS */
84 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_SERVER_SPN */
85 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SPN */
86 { sizeof(ULONG), FALSE } /* WS_CHANNEL_PROPERTY_MAX_HTTP_REQUEST_HEADERS_BUFFER_SIZE */
89 enum session_state
91 SESSION_STATE_UNINITIALIZED,
92 SESSION_STATE_SETUP_COMPLETE,
95 struct channel
97 ULONG magic;
98 CRITICAL_SECTION cs;
99 WS_CHANNEL_TYPE type;
100 WS_CHANNEL_BINDING binding;
101 WS_CHANNEL_STATE state;
102 WS_ENDPOINT_ADDRESS addr;
103 WS_XML_WRITER *writer;
104 WS_XML_READER *reader;
105 WS_MESSAGE *msg;
106 WS_ENCODING encoding;
107 enum session_state session_state;
108 struct dictionary dict;
109 union
111 struct
113 HINTERNET session;
114 HINTERNET connect;
115 HINTERNET request;
116 WCHAR *path;
117 DWORD flags;
118 } http;
119 struct
121 SOCKET socket;
122 } tcp;
123 struct
125 SOCKET socket;
126 } udp;
127 } u;
128 char *read_buf;
129 ULONG read_buflen;
130 ULONG read_size;
131 ULONG prop_count;
132 struct prop prop[sizeof(channel_props)/sizeof(channel_props[0])];
135 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
137 static struct channel *alloc_channel(void)
139 static const ULONG count = sizeof(channel_props)/sizeof(channel_props[0]);
140 struct channel *ret;
141 ULONG size = sizeof(*ret) + prop_size( channel_props, count );
143 if (!(ret = heap_alloc_zero( size ))) return NULL;
145 ret->magic = CHANNEL_MAGIC;
146 InitializeCriticalSection( &ret->cs );
147 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.cs");
149 prop_init( channel_props, count, ret->prop, &ret[1] );
150 ret->prop_count = count;
151 return ret;
154 static void reset_channel( struct channel *channel )
156 channel->state = WS_CHANNEL_STATE_CREATED;
157 heap_free( channel->addr.url.chars );
158 channel->addr.url.chars = NULL;
159 channel->addr.url.length = 0;
160 channel->msg = NULL;
161 channel->read_size = 0;
162 channel->session_state = SESSION_STATE_UNINITIALIZED;
163 clear_dict( &channel->dict );
165 switch (channel->binding)
167 case WS_HTTP_CHANNEL_BINDING:
168 WinHttpCloseHandle( channel->u.http.request );
169 channel->u.http.request = NULL;
170 WinHttpCloseHandle( channel->u.http.connect );
171 channel->u.http.connect = NULL;
172 WinHttpCloseHandle( channel->u.http.session );
173 channel->u.http.session = NULL;
174 heap_free( channel->u.http.path );
175 channel->u.http.path = NULL;
176 channel->u.http.flags = 0;
177 break;
179 case WS_TCP_CHANNEL_BINDING:
180 closesocket( channel->u.tcp.socket );
181 channel->u.tcp.socket = -1;
182 break;
184 case WS_UDP_CHANNEL_BINDING:
185 closesocket( channel->u.udp.socket );
186 channel->u.udp.socket = -1;
187 break;
189 default: break;
193 static void free_channel( struct channel *channel )
195 reset_channel( channel );
197 WsFreeWriter( channel->writer );
198 WsFreeReader( channel->reader );
200 heap_free( channel->read_buf );
202 channel->cs.DebugInfo->Spare[0] = 0;
203 DeleteCriticalSection( &channel->cs );
204 heap_free( channel );
207 static HRESULT create_channel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
208 const WS_CHANNEL_PROPERTY *properties, ULONG count, struct channel **ret )
210 struct channel *channel;
211 ULONG i, msg_size = 65536;
212 WS_ENVELOPE_VERSION env_version = WS_ENVELOPE_VERSION_SOAP_1_2;
213 WS_ADDRESSING_VERSION addr_version = WS_ADDRESSING_VERSION_1_0;
214 HRESULT hr;
216 if (!(channel = alloc_channel())) return E_OUTOFMEMORY;
218 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
219 &msg_size, sizeof(msg_size) );
220 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION,
221 &env_version, sizeof(env_version) );
222 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION,
223 &addr_version, sizeof(addr_version) );
225 channel->type = type;
226 channel->binding = binding;
228 switch (channel->binding)
230 case WS_HTTP_CHANNEL_BINDING:
231 channel->encoding = WS_ENCODING_XML_UTF8;
232 break;
234 case WS_TCP_CHANNEL_BINDING:
235 channel->u.tcp.socket = -1;
236 channel->encoding = WS_ENCODING_XML_BINARY_SESSION_1;
237 break;
239 case WS_UDP_CHANNEL_BINDING:
240 channel->u.udp.socket = -1;
241 channel->encoding = WS_ENCODING_XML_UTF8;
242 break;
244 default: break;
247 for (i = 0; i < count; i++)
249 switch (properties[i].id)
251 case WS_CHANNEL_PROPERTY_ENCODING:
252 if (!properties[i].value || properties[i].valueSize != sizeof(channel->encoding))
254 free_channel( channel );
255 return E_INVALIDARG;
257 channel->encoding = *(WS_ENCODING *)properties[i].value;
258 break;
260 default:
261 if ((hr = prop_set( channel->prop, channel->prop_count, properties[i].id, properties[i].value,
262 properties[i].valueSize )) != S_OK)
264 free_channel( channel );
265 return hr;
267 break;
271 *ret = channel;
272 return S_OK;
275 /**************************************************************************
276 * WsCreateChannel [webservices.@]
278 HRESULT WINAPI WsCreateChannel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
279 const WS_CHANNEL_PROPERTY *properties, ULONG count,
280 const WS_SECURITY_DESCRIPTION *desc, WS_CHANNEL **handle,
281 WS_ERROR *error )
283 struct channel *channel;
284 HRESULT hr;
286 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
287 if (error) FIXME( "ignoring error parameter\n" );
288 if (desc) FIXME( "ignoring security description\n" );
290 if (!handle) return E_INVALIDARG;
292 if (type != WS_CHANNEL_TYPE_REQUEST && type != WS_CHANNEL_TYPE_DUPLEX &&
293 type != WS_CHANNEL_TYPE_DUPLEX_SESSION)
295 FIXME( "channel type %u not implemented\n", type );
296 return E_NOTIMPL;
298 if (binding != WS_HTTP_CHANNEL_BINDING && binding != WS_TCP_CHANNEL_BINDING &&
299 binding != WS_UDP_CHANNEL_BINDING)
301 FIXME( "channel binding %u not implemented\n", binding );
302 return E_NOTIMPL;
305 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
307 *handle = (WS_CHANNEL *)channel;
308 return S_OK;
311 /**************************************************************************
312 * WsCreateChannelForListener [webservices.@]
314 HRESULT WINAPI WsCreateChannelForListener( WS_LISTENER *listener_handle, const WS_CHANNEL_PROPERTY *properties,
315 ULONG count, WS_CHANNEL **handle, WS_ERROR *error )
317 struct channel *channel;
318 WS_CHANNEL_TYPE type;
319 WS_CHANNEL_BINDING binding;
320 HRESULT hr;
322 TRACE( "%p %p %u %p %p\n", listener_handle, properties, count, handle, error );
323 if (error) FIXME( "ignoring error parameter\n" );
325 if (!listener_handle || !handle) return E_INVALIDARG;
327 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_TYPE, &type,
328 sizeof(type), NULL )) != S_OK) return hr;
330 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_BINDING, &binding,
331 sizeof(binding), NULL )) != S_OK) return hr;
333 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
335 *handle = (WS_CHANNEL *)channel;
336 return S_OK;
339 /**************************************************************************
340 * WsFreeChannel [webservices.@]
342 void WINAPI WsFreeChannel( WS_CHANNEL *handle )
344 struct channel *channel = (struct channel *)handle;
346 TRACE( "%p\n", handle );
348 if (!channel) return;
350 EnterCriticalSection( &channel->cs );
352 if (channel->magic != CHANNEL_MAGIC)
354 LeaveCriticalSection( &channel->cs );
355 return;
358 channel->magic = 0;
360 LeaveCriticalSection( &channel->cs );
361 free_channel( channel );
364 /**************************************************************************
365 * WsResetChannel [webservices.@]
367 HRESULT WINAPI WsResetChannel( WS_CHANNEL *handle, WS_ERROR *error )
369 struct channel *channel = (struct channel *)handle;
371 TRACE( "%p %p\n", handle, error );
372 if (error) FIXME( "ignoring error parameter\n" );
374 if (!channel) return E_INVALIDARG;
376 EnterCriticalSection( &channel->cs );
378 if (channel->magic != CHANNEL_MAGIC)
380 LeaveCriticalSection( &channel->cs );
381 return E_INVALIDARG;
384 if (channel->state != WS_CHANNEL_STATE_CREATED && channel->state != WS_CHANNEL_STATE_CLOSED)
386 LeaveCriticalSection( &channel->cs );
387 return WS_E_INVALID_OPERATION;
390 reset_channel( channel );
392 LeaveCriticalSection( &channel->cs );
393 return S_OK;
396 /**************************************************************************
397 * WsGetChannelProperty [webservices.@]
399 HRESULT WINAPI WsGetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, void *buf,
400 ULONG size, WS_ERROR *error )
402 struct channel *channel = (struct channel *)handle;
403 HRESULT hr = S_OK;
405 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
406 if (error) FIXME( "ignoring error parameter\n" );
408 if (!channel) return E_INVALIDARG;
410 EnterCriticalSection( &channel->cs );
412 if (channel->magic != CHANNEL_MAGIC)
414 LeaveCriticalSection( &channel->cs );
415 return E_INVALIDARG;
418 switch (id)
420 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE:
421 if (!buf || size != sizeof(channel->type)) hr = E_INVALIDARG;
422 else *(WS_CHANNEL_TYPE *)buf = channel->type;
423 break;
425 case WS_CHANNEL_PROPERTY_ENCODING:
426 if (!buf || size != sizeof(channel->encoding)) hr = E_INVALIDARG;
427 else *(WS_ENCODING *)buf = channel->encoding;
428 break;
430 default:
431 hr = prop_get( channel->prop, channel->prop_count, id, buf, size );
434 LeaveCriticalSection( &channel->cs );
435 return hr;
438 /**************************************************************************
439 * WsSetChannelProperty [webservices.@]
441 HRESULT WINAPI WsSetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, const void *value,
442 ULONG size, WS_ERROR *error )
444 struct channel *channel = (struct channel *)handle;
445 HRESULT hr;
447 TRACE( "%p %u %p %u\n", handle, id, value, size );
448 if (error) FIXME( "ignoring error parameter\n" );
450 if (!channel) return E_INVALIDARG;
452 EnterCriticalSection( &channel->cs );
454 if (channel->magic != CHANNEL_MAGIC)
456 LeaveCriticalSection( &channel->cs );
457 return E_INVALIDARG;
460 hr = prop_set( channel->prop, channel->prop_count, id, value, size );
462 LeaveCriticalSection( &channel->cs );
463 return hr;
466 static HRESULT open_channel( struct channel *channel, const WS_ENDPOINT_ADDRESS *endpoint )
468 if (endpoint->headers || endpoint->extensions || endpoint->identity)
470 FIXME( "headers, extensions or identity not supported\n" );
471 return E_NOTIMPL;
474 if (!(channel->addr.url.chars = heap_alloc( endpoint->url.length * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
475 memcpy( channel->addr.url.chars, endpoint->url.chars, endpoint->url.length * sizeof(WCHAR) );
476 channel->addr.url.length = endpoint->url.length;
478 channel->state = WS_CHANNEL_STATE_OPEN;
479 return S_OK;
482 /**************************************************************************
483 * WsOpenChannel [webservices.@]
485 HRESULT WINAPI WsOpenChannel( WS_CHANNEL *handle, const WS_ENDPOINT_ADDRESS *endpoint,
486 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
488 struct channel *channel = (struct channel *)handle;
489 HRESULT hr;
491 TRACE( "%p %p %p %p\n", handle, endpoint, ctx, error );
492 if (error) FIXME( "ignoring error parameter\n" );
493 if (ctx) FIXME( "ignoring ctx parameter\n" );
495 if (!channel || !endpoint) return E_INVALIDARG;
497 EnterCriticalSection( &channel->cs );
499 if (channel->magic != CHANNEL_MAGIC)
501 LeaveCriticalSection( &channel->cs );
502 return E_INVALIDARG;
505 if (channel->state != WS_CHANNEL_STATE_CREATED)
507 LeaveCriticalSection( &channel->cs );
508 return WS_E_INVALID_OPERATION;
511 hr = open_channel( channel, endpoint );
513 LeaveCriticalSection( &channel->cs );
514 return hr;
517 static void close_channel( struct channel *channel )
519 reset_channel( channel );
520 channel->state = WS_CHANNEL_STATE_CLOSED;
523 /**************************************************************************
524 * WsCloseChannel [webservices.@]
526 HRESULT WINAPI WsCloseChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
528 struct channel *channel = (struct channel *)handle;
530 TRACE( "%p %p %p\n", handle, ctx, error );
531 if (error) FIXME( "ignoring error parameter\n" );
532 if (ctx) FIXME( "ignoring ctx parameter\n" );
534 if (!channel) return E_INVALIDARG;
536 EnterCriticalSection( &channel->cs );
538 if (channel->magic != CHANNEL_MAGIC)
540 LeaveCriticalSection( &channel->cs );
541 return E_INVALIDARG;
544 close_channel( channel );
546 LeaveCriticalSection( &channel->cs );
547 return S_OK;
550 static HRESULT parse_http_url( const WCHAR *url, ULONG len, URL_COMPONENTS *uc )
552 HRESULT hr = E_OUTOFMEMORY;
553 WCHAR *tmp;
554 DWORD err;
556 memset( uc, 0, sizeof(*uc) );
557 uc->dwStructSize = sizeof(*uc);
558 uc->dwHostNameLength = 128;
559 uc->lpszHostName = heap_alloc( uc->dwHostNameLength * sizeof(WCHAR) );
560 uc->dwUrlPathLength = 128;
561 uc->lpszUrlPath = heap_alloc( uc->dwUrlPathLength * sizeof(WCHAR) );
562 uc->dwExtraInfoLength = 128;
563 uc->lpszExtraInfo = heap_alloc( uc->dwExtraInfoLength * sizeof(WCHAR) );
564 if (!uc->lpszHostName || !uc->lpszUrlPath || !uc->lpszExtraInfo) goto error;
566 if (!WinHttpCrackUrl( url, len, ICU_DECODE, uc ))
568 if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
570 hr = HRESULT_FROM_WIN32( err );
571 goto error;
573 if (!(tmp = heap_realloc( uc->lpszHostName, uc->dwHostNameLength * sizeof(WCHAR) ))) goto error;
574 uc->lpszHostName = tmp;
575 if (!(tmp = heap_realloc( uc->lpszUrlPath, uc->dwUrlPathLength * sizeof(WCHAR) ))) goto error;
576 uc->lpszUrlPath = tmp;
577 if (!(tmp = heap_realloc( uc->lpszExtraInfo, uc->dwExtraInfoLength * sizeof(WCHAR) ))) goto error;
578 uc->lpszExtraInfo = tmp;
579 WinHttpCrackUrl( url, len, ICU_DECODE, uc );
582 return S_OK;
584 error:
585 heap_free( uc->lpszHostName );
586 heap_free( uc->lpszUrlPath );
587 heap_free( uc->lpszExtraInfo );
588 return hr;
591 static HRESULT connect_channel_http( struct channel *channel )
593 static const WCHAR agentW[] =
594 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
595 HINTERNET ses = NULL, con = NULL;
596 URL_COMPONENTS uc;
597 HRESULT hr;
599 if (channel->u.http.connect) return S_OK;
601 if ((hr = parse_http_url( channel->addr.url.chars, channel->addr.url.length, &uc )) != S_OK) return hr;
602 if (!(channel->u.http.path = heap_alloc( (uc.dwUrlPathLength + uc.dwExtraInfoLength + 1) * sizeof(WCHAR) )))
604 hr = E_OUTOFMEMORY;
605 goto done;
607 else
609 strcpyW( channel->u.http.path, uc.lpszUrlPath );
610 if (uc.dwExtraInfoLength) strcatW( channel->u.http.path, uc.lpszExtraInfo );
613 channel->u.http.flags = WINHTTP_FLAG_REFRESH;
614 switch (uc.nScheme)
616 case INTERNET_SCHEME_HTTP: break;
617 case INTERNET_SCHEME_HTTPS:
618 channel->u.http.flags |= WINHTTP_FLAG_SECURE;
619 break;
621 default:
622 hr = WS_E_INVALID_ENDPOINT_URL;
623 goto done;
626 if (!(ses = WinHttpOpen( agentW, 0, NULL, NULL, 0 )))
628 hr = HRESULT_FROM_WIN32( GetLastError() );
629 goto done;
631 if (!(con = WinHttpConnect( ses, uc.lpszHostName, uc.nPort, 0 )))
633 hr = HRESULT_FROM_WIN32( GetLastError() );
634 goto done;
637 channel->u.http.session = ses;
638 channel->u.http.connect = con;
640 done:
641 if (hr != S_OK)
643 WinHttpCloseHandle( con );
644 WinHttpCloseHandle( ses );
646 heap_free( uc.lpszHostName );
647 heap_free( uc.lpszUrlPath );
648 heap_free( uc.lpszExtraInfo );
649 return hr;
652 static HRESULT connect_channel_tcp( struct channel *channel )
654 struct sockaddr_storage storage;
655 struct sockaddr *addr = (struct sockaddr *)&storage;
656 int addr_len;
657 WS_URL_SCHEME_TYPE scheme;
658 WCHAR *host;
659 USHORT port;
660 HRESULT hr;
662 if (channel->u.tcp.socket != -1) return S_OK;
664 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
665 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
667 heap_free( host );
668 return WS_E_INVALID_ENDPOINT_URL;
671 winsock_init();
673 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
674 heap_free( host );
675 if (hr != S_OK) return hr;
677 if ((channel->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
678 return HRESULT_FROM_WIN32( WSAGetLastError() );
680 if (connect( channel->u.tcp.socket, addr, addr_len ) < 0)
682 closesocket( channel->u.tcp.socket );
683 channel->u.tcp.socket = -1;
684 return HRESULT_FROM_WIN32( WSAGetLastError() );
687 return S_OK;
690 static HRESULT connect_channel_udp( struct channel *channel )
692 struct sockaddr_storage storage;
693 struct sockaddr *addr = (struct sockaddr *)&storage;
694 int addr_len;
695 WS_URL_SCHEME_TYPE scheme;
696 WCHAR *host;
697 USHORT port;
698 HRESULT hr;
700 if (channel->u.udp.socket != -1) return S_OK;
702 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
703 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
705 heap_free( host );
706 return WS_E_INVALID_ENDPOINT_URL;
709 winsock_init();
711 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
712 heap_free( host );
713 if (hr != S_OK) return hr;
715 if ((channel->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
716 return HRESULT_FROM_WIN32( WSAGetLastError() );
718 if (connect( channel->u.udp.socket, addr, addr_len ) < 0)
720 closesocket( channel->u.udp.socket );
721 channel->u.udp.socket = -1;
722 return HRESULT_FROM_WIN32( WSAGetLastError() );
725 return S_OK;
728 static HRESULT connect_channel( struct channel *channel )
730 switch (channel->binding)
732 case WS_HTTP_CHANNEL_BINDING:
733 return connect_channel_http( channel );
735 case WS_TCP_CHANNEL_BINDING:
736 return connect_channel_tcp( channel );
738 case WS_UDP_CHANNEL_BINDING:
739 return connect_channel_udp( channel );
741 default:
742 ERR( "unhandled binding %u\n", channel->binding );
743 return E_NOTIMPL;
747 static HRESULT write_message( WS_MESSAGE *handle, WS_XML_WRITER *writer, const WS_ELEMENT_DESCRIPTION *desc,
748 WS_WRITE_OPTION option, const void *body, ULONG size )
750 HRESULT hr;
751 if ((hr = WsWriteEnvelopeStart( handle, writer, NULL, NULL, NULL )) != S_OK) return hr;
752 if ((hr = WsWriteBody( handle, desc, option, body, size, NULL )) != S_OK) return hr;
753 return WsWriteEnvelopeEnd( handle, NULL );
756 static HRESULT send_message_http( HINTERNET request, BYTE *data, ULONG len )
758 if (!WinHttpSendRequest( request, NULL, 0, data, len, len, 0 ))
759 return HRESULT_FROM_WIN32( GetLastError() );
761 if (!WinHttpReceiveResponse( request, NULL ))
762 return HRESULT_FROM_WIN32( GetLastError() );
763 return S_OK;
766 static HRESULT send_byte( SOCKET socket, BYTE byte )
768 int count = send( socket, (char *)&byte, 1, 0 );
769 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
770 if (count != 1) return WS_E_OTHER;
771 return S_OK;
774 static HRESULT send_bytes( SOCKET socket, BYTE *bytes, int len )
776 int count = send( socket, (char *)bytes, len, 0 );
777 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
778 if (count != len) return WS_E_OTHER;
779 return S_OK;
782 static HRESULT send_size( SOCKET socket, ULONG size )
784 HRESULT hr;
785 if (size < 0x80) return send_byte( socket, size );
786 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
787 if ((size >>= 7) < 0x80) return send_byte( socket, size );
788 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
789 if ((size >>= 7) < 0x80) return send_byte( socket, size );
790 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
791 if ((size >>= 7) < 0x80) return send_byte( socket, size );
792 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
793 if ((size >>= 7) < 0x08) return send_byte( socket, size );
794 return E_INVALIDARG;
797 enum frame_record_type
799 FRAME_RECORD_TYPE_VERSION,
800 FRAME_RECORD_TYPE_MODE,
801 FRAME_RECORD_TYPE_VIA,
802 FRAME_RECORD_TYPE_KNOWN_ENCODING,
803 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING,
804 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE,
805 FRAME_RECORD_TYPE_SIZED_ENVELOPE,
806 FRAME_RECORD_TYPE_END,
807 FRAME_RECORD_TYPE_FAULT,
808 FRAME_RECORD_TYPE_UPGRADE_REQUEST,
809 FRAME_RECORD_TYPE_UPGRADE_RESPONSE,
810 FRAME_RECORD_TYPE_PREAMBLE_ACK,
811 FRAME_RECORD_TYPE_PREAMBLE_END,
814 static inline ULONG size_length( ULONG size )
816 if (size < 0x80) return 1;
817 if (size < 0x4000) return 2;
818 if (size < 0x200000) return 3;
819 if (size < 0x10000000) return 4;
820 return 5;
823 static ULONG string_table_size( const WS_XML_DICTIONARY *dict )
825 ULONG i, size = 0;
826 for (i = 0; i < dict->stringCount; i++)
827 size += size_length( dict->strings[i].length ) + dict->strings[i].length;
828 return size;
831 static HRESULT send_string_table( SOCKET socket, const WS_XML_DICTIONARY *dict )
833 ULONG i;
834 HRESULT hr;
835 for (i = 0; i < dict->stringCount; i++)
837 if ((hr = send_size( socket, dict->strings[i].length )) != S_OK) return hr;
838 if ((hr = send_bytes( socket, dict->strings[i].bytes, dict->strings[i].length )) != S_OK) return hr;
840 return S_OK;
843 static HRESULT string_to_utf8( const WS_STRING *str, unsigned char **ret, int *len )
845 *len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
846 if (!(*ret = heap_alloc( *len ))) return E_OUTOFMEMORY;
847 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)*ret, *len, NULL, NULL );
848 return S_OK;
851 enum session_mode
853 SESSION_MODE_INVALID = 0,
854 SESSION_MODE_SINGLETON = 1,
855 SESSION_MODE_DUPLEX = 2,
856 SESSION_MODE_SIMPLEX = 3,
859 static enum session_mode map_channel_type( struct channel *channel )
861 switch (channel->type)
863 case WS_CHANNEL_TYPE_DUPLEX_SESSION: return SESSION_MODE_DUPLEX;
864 default:
865 FIXME( "unhandled channel type %08x\n", channel->type );
866 return SESSION_MODE_INVALID;
870 enum known_encoding
872 KNOWN_ENCODING_SOAP11_UTF8 = 0x00,
873 KNOWN_ENCODING_SOAP11_UTF16 = 0x01,
874 KNOWN_ENCODING_SOAP11_UTF16LE = 0x02,
875 KNOWN_ENCODING_SOAP12_UTF8 = 0x03,
876 KNOWN_ENCODING_SOAP12_UTF16 = 0x04,
877 KNOWN_ENCODING_SOAP12_UTF16LE = 0x05,
878 KNOWN_ENCODING_SOAP12_MTOM = 0x06,
879 KNOWN_ENCODING_SOAP12_BINARY = 0x07,
880 KNOWN_ENCODING_SOAP12_BINARY_SESSION = 0x08,
883 static enum known_encoding map_channel_encoding( struct channel *channel )
885 WS_ENVELOPE_VERSION version;
887 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version, sizeof(version) );
889 switch (version)
891 case WS_ENVELOPE_VERSION_SOAP_1_1:
892 switch (channel->encoding)
894 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP11_UTF8;
895 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP11_UTF16LE;
896 default:
897 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
898 return 0;
900 case WS_ENVELOPE_VERSION_SOAP_1_2:
901 switch (channel->encoding)
903 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP12_UTF8;
904 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP12_UTF16LE;
905 case WS_ENCODING_XML_BINARY_1: return KNOWN_ENCODING_SOAP12_BINARY;
906 case WS_ENCODING_XML_BINARY_SESSION_1: return KNOWN_ENCODING_SOAP12_BINARY_SESSION;
907 default:
908 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
909 return 0;
911 default:
912 ERR( "unhandled version %u\n", version );
913 return 0;
917 #define FRAME_VERSION_MAJOR 1
918 #define FRAME_VERSION_MINOR 1
920 static HRESULT send_preamble( struct channel *channel )
922 unsigned char *url;
923 HRESULT hr;
924 int len;
926 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VERSION )) != S_OK) return hr;
927 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MAJOR )) != S_OK) return hr;
928 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MINOR )) != S_OK) return hr;
930 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_MODE )) != S_OK) return hr;
931 if ((hr = send_byte( channel->u.tcp.socket, map_channel_type(channel) )) != S_OK) return hr;
933 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VIA )) != S_OK) return hr;
934 if ((hr = string_to_utf8( &channel->addr.url, &url, &len )) != S_OK) return hr;
935 if ((hr = send_size( channel->u.tcp.socket, len )) != S_OK) goto done;
936 if ((hr = send_bytes( channel->u.tcp.socket, url, len )) != S_OK) goto done;
938 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_KNOWN_ENCODING )) != S_OK) goto done;
939 if ((hr = send_byte( channel->u.tcp.socket, map_channel_encoding(channel) )) != S_OK) goto done;
940 hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_END );
942 done:
943 heap_free( url );
944 return hr;
947 static HRESULT receive_bytes( struct channel *channel, unsigned char *bytes, int len )
949 int count = recv( channel->u.tcp.socket, (char *)bytes, len, 0 );
950 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
951 if (count != len) return WS_E_INVALID_FORMAT;
952 return S_OK;
955 static HRESULT receive_preamble_ack( struct channel *channel )
957 unsigned char byte;
958 HRESULT hr;
960 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
961 if (byte != FRAME_RECORD_TYPE_PREAMBLE_ACK) return WS_E_INVALID_FORMAT;
962 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
963 return S_OK;
966 static HRESULT send_sized_envelope( struct channel *channel, BYTE *data, ULONG len )
968 ULONG table_size = string_table_size( &channel->dict.dict );
969 HRESULT hr;
971 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_SIZED_ENVELOPE )) != S_OK) return hr;
972 if ((hr = send_size( channel->u.tcp.socket, size_length(table_size) + table_size + len )) != S_OK) return hr;
973 if ((hr = send_size( channel->u.tcp.socket, table_size )) != S_OK) return hr;
974 if ((hr = send_string_table( channel->u.tcp.socket, &channel->dict.dict )) != S_OK) return hr;
975 return send_bytes( channel->u.tcp.socket, data, len );
978 static HRESULT open_http_request( struct channel *channel, HINTERNET *req )
980 static const WCHAR postW[] = {'P','O','S','T',0};
981 if ((*req = WinHttpOpenRequest( channel->u.http.connect, postW, channel->u.http.path,
982 NULL, NULL, NULL, channel->u.http.flags ))) return S_OK;
983 return HRESULT_FROM_WIN32( GetLastError() );
986 static HRESULT send_message( struct channel *channel, WS_MESSAGE *msg )
988 WS_XML_WRITER *writer;
989 WS_BYTES buf;
990 HRESULT hr;
992 channel->msg = msg;
993 if ((hr = connect_channel( channel )) != S_OK) return hr;
995 WsGetMessageProperty( channel->msg, WS_MESSAGE_PROPERTY_BODY_WRITER, &writer, sizeof(writer), NULL );
996 WsGetWriterProperty( writer, WS_XML_WRITER_PROPERTY_BYTES, &buf, sizeof(buf), NULL );
998 switch (channel->binding)
1000 case WS_HTTP_CHANNEL_BINDING:
1001 if (channel->u.http.request)
1003 WinHttpCloseHandle( channel->u.http.request );
1004 channel->u.http.request = NULL;
1006 if ((hr = open_http_request( channel, &channel->u.http.request )) != S_OK) return hr;
1007 if ((hr = message_insert_http_headers( msg, channel->u.http.request )) != S_OK) return hr;
1008 return send_message_http( channel->u.http.request, buf.bytes, buf.length );
1010 case WS_TCP_CHANNEL_BINDING:
1011 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1013 switch (channel->session_state)
1015 case SESSION_STATE_UNINITIALIZED:
1016 if ((hr = send_preamble( channel )) != S_OK) return hr;
1017 if ((hr = receive_preamble_ack( channel )) != S_OK) return hr;
1018 /* fall through */
1020 case SESSION_STATE_SETUP_COMPLETE:
1021 return send_sized_envelope( channel, buf.bytes, buf.length );
1023 default:
1024 ERR( "unhandled session state %u\n", channel->session_state );
1025 return WS_E_OTHER;
1028 return send_bytes( channel->u.tcp.socket, buf.bytes, buf.length );
1030 case WS_UDP_CHANNEL_BINDING:
1031 return send_bytes( channel->u.udp.socket, buf.bytes, buf.length );
1033 default:
1034 ERR( "unhandled binding %u\n", channel->binding );
1035 return E_NOTIMPL;
1039 HRESULT channel_send_message( WS_CHANNEL *handle, WS_MESSAGE *msg )
1041 struct channel *channel = (struct channel *)handle;
1042 HRESULT hr;
1044 EnterCriticalSection( &channel->cs );
1046 if (channel->magic != CHANNEL_MAGIC)
1048 LeaveCriticalSection( &channel->cs );
1049 return E_INVALIDARG;
1052 hr = send_message( channel, msg );
1054 LeaveCriticalSection( &channel->cs );
1055 return hr;
1058 static HRESULT CALLBACK dict_cb( void *state, const WS_XML_STRING *str, BOOL *found, ULONG *id, WS_ERROR *error )
1060 struct dictionary *dict = state;
1061 HRESULT hr = S_OK;
1062 int index;
1064 if ((index = find_string( dict, str->bytes, str->length, id )) == -1 ||
1065 (hr = insert_string( dict, str->bytes, str->length, index, id )) == S_OK)
1067 *found = TRUE;
1068 return S_OK;
1070 *found = FALSE;
1071 return hr;
1074 static HRESULT init_writer( struct channel *channel )
1076 WS_XML_WRITER_BUFFER_OUTPUT buf = {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER}};
1077 WS_XML_WRITER_TEXT_ENCODING text = {{WS_XML_WRITER_ENCODING_TYPE_TEXT}};
1078 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1079 WS_XML_WRITER_ENCODING *encoding;
1080 HRESULT hr;
1082 if (!channel->writer && (hr = WsCreateWriter( NULL, 0, &channel->writer, NULL )) != S_OK) return hr;
1084 switch (channel->encoding)
1086 case WS_ENCODING_XML_UTF8:
1087 text.charSet = WS_CHARSET_UTF8;
1088 encoding = &text.encoding;
1089 break;
1091 case WS_ENCODING_XML_BINARY_SESSION_1:
1092 if ((hr = writer_enable_lookup( channel->writer )) != S_OK) return hr;
1093 clear_dict( &channel->dict );
1094 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1095 bin.dynamicStringCallback = dict_cb;
1096 bin.dynamicStringCallbackState = &channel->dict;
1097 encoding = &bin.encoding;
1098 break;
1100 case WS_ENCODING_XML_BINARY_1:
1101 encoding = &bin.encoding;
1102 break;
1104 default:
1105 FIXME( "unhandled encoding %u\n", channel->encoding );
1106 return WS_E_NOT_SUPPORTED;
1109 return WsSetOutput( channel->writer, encoding, &buf.output, NULL, 0, NULL );
1112 /**************************************************************************
1113 * WsSendMessage [webservices.@]
1115 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1116 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1117 WS_ERROR *error )
1119 struct channel *channel = (struct channel *)handle;
1120 HRESULT hr;
1122 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1123 if (error) FIXME( "ignoring error parameter\n" );
1124 if (ctx) FIXME( "ignoring ctx parameter\n" );
1126 if (!channel || !msg || !desc) return E_INVALIDARG;
1128 EnterCriticalSection( &channel->cs );
1130 if (channel->magic != CHANNEL_MAGIC)
1132 LeaveCriticalSection( &channel->cs );
1133 return E_INVALIDARG;
1136 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK) goto done;
1137 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1138 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1140 if ((hr = init_writer( channel )) != S_OK) goto done;
1141 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1142 goto done;
1143 hr = send_message( channel, msg );
1145 done:
1146 LeaveCriticalSection( &channel->cs );
1147 return hr;
1150 /**************************************************************************
1151 * WsSendReplyMessage [webservices.@]
1153 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1154 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1155 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1157 struct channel *channel = (struct channel *)handle;
1158 HRESULT hr;
1160 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1161 if (error) FIXME( "ignoring error parameter\n" );
1162 if (ctx) FIXME( "ignoring ctx parameter\n" );
1164 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1166 EnterCriticalSection( &channel->cs );
1168 if (channel->magic != CHANNEL_MAGIC)
1170 LeaveCriticalSection( &channel->cs );
1171 return E_INVALIDARG;
1174 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1175 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1176 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1178 if ((hr = init_writer( channel )) != S_OK) goto done;
1179 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1180 goto done;
1181 hr = send_message( channel, msg );
1183 done:
1184 LeaveCriticalSection( &channel->cs );
1185 FIXME( "returning %08x\n", hr );
1186 return hr;
1189 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1191 if (!channel->read_buf)
1193 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1194 channel->read_buflen = size;
1195 return S_OK;
1197 if (channel->read_buflen < size)
1199 char *tmp;
1200 ULONG new_size = max( size, channel->read_buflen * 2 );
1201 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1202 channel->read_buf = tmp;
1203 channel->read_buflen = new_size;
1205 return S_OK;
1208 static HRESULT init_reader( struct channel *channel )
1210 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1211 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1212 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1213 WS_XML_READER_ENCODING *encoding;
1214 HRESULT hr;
1216 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1218 switch (channel->encoding)
1220 case WS_ENCODING_XML_UTF8:
1221 text.charSet = WS_CHARSET_UTF8;
1222 encoding = &text.encoding;
1223 break;
1225 case WS_ENCODING_XML_BINARY_SESSION_1:
1226 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1227 bin.dynamicDictionary = &channel->dict.dict;
1228 /* fall through */
1230 case WS_ENCODING_XML_BINARY_1:
1231 encoding = &bin.encoding;
1232 break;
1234 default:
1235 FIXME( "unhandled encoding %u\n", channel->encoding );
1236 return WS_E_NOT_SUPPORTED;
1239 buf.encodedData = channel->read_buf;
1240 buf.encodedDataSize = channel->read_size;
1241 return WsSetInput( channel->reader, encoding, &buf.input, NULL, 0, NULL );
1244 #define INITIAL_READ_BUFFER_SIZE 4096
1245 static HRESULT receive_message_http( struct channel *channel )
1247 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1248 ULONG max_len;
1249 HRESULT hr;
1251 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1252 &max_len, sizeof(max_len) );
1254 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1255 channel->read_size = 0;
1256 for (;;)
1258 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1260 return HRESULT_FROM_WIN32( GetLastError() );
1262 if (!len) break;
1263 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1264 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1266 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1268 return HRESULT_FROM_WIN32( GetLastError() );
1270 if (!bytes_read) break;
1271 channel->read_size += bytes_read;
1272 offset += bytes_read;
1275 return init_reader( channel );
1278 static HRESULT receive_message_unsized( struct channel *channel, SOCKET socket )
1280 int bytes_read;
1281 ULONG max_len;
1282 HRESULT hr;
1284 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1285 &max_len, sizeof(max_len) );
1287 if ((hr = resize_read_buffer( channel, max_len )) != S_OK) return hr;
1289 channel->read_size = 0;
1290 if ((bytes_read = recv( socket, channel->read_buf, max_len, 0 )) < 0)
1292 return HRESULT_FROM_WIN32( WSAGetLastError() );
1294 channel->read_size = bytes_read;
1295 return S_OK;
1298 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1300 unsigned int offset = 0, to_read = size;
1301 int bytes_read;
1302 HRESULT hr;
1304 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1306 channel->read_size = 0;
1307 while (channel->read_size < size)
1309 if ((bytes_read = recv( channel->u.tcp.socket, channel->read_buf + offset, to_read, 0 )) < 0)
1311 return HRESULT_FROM_WIN32( WSAGetLastError() );
1313 if (!bytes_read) break;
1314 channel->read_size += bytes_read;
1315 to_read -= bytes_read;
1316 offset += bytes_read;
1318 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1319 return S_OK;
1322 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1324 unsigned char byte;
1325 HRESULT hr;
1327 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1328 *size = byte & 0x7f;
1329 if (!(byte & 0x80)) return S_OK;
1331 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1332 *size += (byte & 0x7f) << 7;
1333 if (!(byte & 0x80)) return S_OK;
1335 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1336 *size += (byte & 0x7f) << 14;
1337 if (!(byte & 0x80)) return S_OK;
1339 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1340 *size += (byte & 0x7f) << 21;
1341 if (!(byte & 0x80)) return S_OK;
1343 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1344 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1345 *size += byte << 28;
1346 return S_OK;
1349 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1351 switch (encoding)
1353 case KNOWN_ENCODING_SOAP11_UTF8:
1354 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1355 case KNOWN_ENCODING_SOAP11_UTF16:
1356 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1357 case KNOWN_ENCODING_SOAP11_UTF16LE:
1358 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1359 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1360 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1361 default:
1362 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1363 return WS_ENCODING_XML_UTF8;
1367 static HRESULT receive_preamble( struct channel *channel )
1369 unsigned char type;
1370 HRESULT hr;
1372 for (;;)
1374 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1375 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1376 switch (type)
1378 case FRAME_RECORD_TYPE_VERSION:
1380 unsigned char major, minor;
1381 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1382 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1383 TRACE( "major %u minor %u\n", major, major );
1384 break;
1386 case FRAME_RECORD_TYPE_MODE:
1388 unsigned char mode;
1389 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1390 TRACE( "mode %u\n", mode );
1391 break;
1393 case FRAME_RECORD_TYPE_VIA:
1395 unsigned int size;
1396 unsigned char *url;
1398 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1399 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1400 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1402 heap_free( url );
1403 return hr;
1405 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1406 heap_free( url ); /* FIXME: verify */
1407 break;
1409 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1411 unsigned char encoding;
1412 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1413 TRACE( "encoding %u\n", encoding );
1414 channel->encoding = map_known_encoding( encoding );
1415 break;
1417 default:
1418 WARN( "unhandled record type %u\n", type );
1419 return WS_E_INVALID_FORMAT;
1423 return S_OK;
1426 static HRESULT receive_sized_envelope( struct channel *channel )
1428 unsigned char type;
1429 unsigned int size;
1430 HRESULT hr;
1432 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1433 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1434 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1435 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1436 return S_OK;
1439 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1441 const BYTE *buf = *ptr;
1443 if (len < 1) return WS_E_INVALID_FORMAT;
1444 *size = buf[0] & 0x7f;
1445 if (!(buf[0] & 0x80))
1447 *ptr += 1;
1448 return S_OK;
1450 if (len < 2) return WS_E_INVALID_FORMAT;
1451 *size += (buf[1] & 0x7f) << 7;
1452 if (!(buf[1] & 0x80))
1454 *ptr += 2;
1455 return S_OK;
1457 if (len < 3) return WS_E_INVALID_FORMAT;
1458 *size += (buf[2] & 0x7f) << 14;
1459 if (!(buf[2] & 0x80))
1461 *ptr += 3;
1462 return S_OK;
1464 if (len < 4) return WS_E_INVALID_FORMAT;
1465 *size += (buf[3] & 0x7f) << 21;
1466 if (!(buf[3] & 0x80))
1468 *ptr += 4;
1469 return S_OK;
1471 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1472 *size += buf[4] << 28;
1473 *ptr += 5;
1474 return S_OK;
1477 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1479 ULONG size, strings_size, strings_offset;
1480 const BYTE *ptr = buf;
1481 BYTE *bytes;
1482 int index;
1483 HRESULT hr;
1485 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1486 strings_offset = ptr - buf;
1487 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1488 *used = strings_offset + strings_size;
1489 if (!strings_size) return S_OK;
1491 UuidCreate( &dict->dict.guid );
1492 dict->dict.isConst = FALSE;
1494 buflen -= strings_offset;
1495 ptr = buf + strings_offset;
1496 while (ptr < buf + strings_size)
1498 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1500 clear_dict( dict );
1501 return hr;
1503 if (size > buflen)
1505 clear_dict( dict );
1506 return WS_E_INVALID_FORMAT;
1508 buflen -= size;
1509 if (!(bytes = heap_alloc( size )))
1511 hr = E_OUTOFMEMORY;
1512 goto error;
1514 memcpy( bytes, ptr, size );
1515 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1517 heap_free( bytes );
1518 ptr += size;
1519 continue;
1521 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1523 clear_dict( dict );
1524 return hr;
1526 ptr += size;
1528 return S_OK;
1530 error:
1531 clear_dict( dict );
1532 return hr;
1535 static HRESULT send_preamble_ack( struct channel *channel )
1537 HRESULT hr;
1538 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1539 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1540 return S_OK;
1543 static HRESULT receive_message_session_setup( struct channel *channel )
1545 HRESULT hr;
1547 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1548 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1549 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1550 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1552 ULONG size;
1553 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict, &size )) != S_OK)
1554 return hr;
1555 channel->read_size -= size;
1556 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1559 return init_reader( channel );
1562 static HRESULT receive_message_session( struct channel *channel )
1564 HRESULT hr;
1565 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1566 return init_reader( channel );
1569 static HRESULT receive_message_sock( struct channel *channel, SOCKET socket )
1571 HRESULT hr;
1572 if ((hr = receive_message_unsized( channel, socket )) != S_OK) return hr;
1573 return init_reader( channel );
1576 static HRESULT receive_message( struct channel *channel )
1578 HRESULT hr;
1579 if ((hr = connect_channel( channel )) != S_OK) return hr;
1581 switch (channel->binding)
1583 case WS_HTTP_CHANNEL_BINDING:
1584 return receive_message_http( channel );
1586 case WS_TCP_CHANNEL_BINDING:
1587 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1589 switch (channel->session_state)
1591 case SESSION_STATE_UNINITIALIZED:
1592 return receive_message_session_setup( channel );
1594 case SESSION_STATE_SETUP_COMPLETE:
1595 return receive_message_session( channel );
1597 default:
1598 ERR( "unhandled session state %u\n", channel->session_state );
1599 return WS_E_OTHER;
1602 return receive_message_sock( channel, channel->u.tcp.socket );
1604 case WS_UDP_CHANNEL_BINDING:
1605 return receive_message_sock( channel, channel->u.udp.socket );
1607 default:
1608 ERR( "unhandled binding %u\n", channel->binding );
1609 return E_NOTIMPL;
1613 HRESULT channel_receive_message( WS_CHANNEL *handle )
1615 struct channel *channel = (struct channel *)handle;
1616 HRESULT hr;
1618 EnterCriticalSection( &channel->cs );
1620 if (channel->magic != CHANNEL_MAGIC)
1622 LeaveCriticalSection( &channel->cs );
1623 return E_INVALIDARG;
1626 hr = receive_message( channel );
1628 LeaveCriticalSection( &channel->cs );
1629 return hr;
1632 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1634 struct channel *channel = (struct channel *)handle;
1636 EnterCriticalSection( &channel->cs );
1638 if (channel->magic != CHANNEL_MAGIC)
1640 LeaveCriticalSection( &channel->cs );
1641 return E_INVALIDARG;
1644 *reader = channel->reader;
1646 LeaveCriticalSection( &channel->cs );
1647 return S_OK;
1650 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1651 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1653 HRESULT hr;
1654 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1655 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1656 return WsReadEnvelopeEnd( handle, NULL );
1659 /**************************************************************************
1660 * WsReceiveMessage [webservices.@]
1662 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1663 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1664 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1666 struct channel *channel = (struct channel *)handle;
1667 HRESULT hr;
1669 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1670 value, size, index, ctx, error );
1671 if (error) FIXME( "ignoring error parameter\n" );
1672 if (ctx) FIXME( "ignoring ctx parameter\n" );
1673 if (index)
1675 FIXME( "index parameter not supported\n" );
1676 return E_NOTIMPL;
1678 if (count != 1)
1680 FIXME( "no support for multiple descriptions\n" );
1681 return E_NOTIMPL;
1683 if (option != WS_RECEIVE_REQUIRED_MESSAGE)
1685 FIXME( "receive option %08x not supported\n", option );
1686 return E_NOTIMPL;
1689 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1691 EnterCriticalSection( &channel->cs );
1693 if (channel->magic != CHANNEL_MAGIC)
1695 LeaveCriticalSection( &channel->cs );
1696 return E_INVALIDARG;
1699 if ((hr = receive_message( channel )) != S_OK) goto done;
1700 hr = read_message( msg, channel->reader, desc[0]->bodyElementDescription, read_option, heap, value, size );
1702 done:
1703 LeaveCriticalSection( &channel->cs );
1704 return hr;
1707 /**************************************************************************
1708 * WsReadMessageStart [webservices.@]
1710 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1711 WS_ERROR *error )
1713 struct channel *channel = (struct channel *)handle;
1714 HRESULT hr;
1716 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1717 if (error) FIXME( "ignoring error parameter\n" );
1718 if (ctx) FIXME( "ignoring ctx parameter\n" );
1720 if (!channel || !msg) return E_INVALIDARG;
1722 EnterCriticalSection( &channel->cs );
1724 if (channel->magic != CHANNEL_MAGIC)
1726 LeaveCriticalSection( &channel->cs );
1727 return E_INVALIDARG;
1730 if ((hr = receive_message( channel )) == S_OK)
1732 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
1735 LeaveCriticalSection( &channel->cs );
1736 return hr;
1739 /**************************************************************************
1740 * WsReadMessageEnd [webservices.@]
1742 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1743 WS_ERROR *error )
1745 struct channel *channel = (struct channel *)handle;
1746 HRESULT hr;
1748 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1749 if (error) FIXME( "ignoring error parameter\n" );
1750 if (ctx) FIXME( "ignoring ctx parameter\n" );
1752 if (!channel || !msg) return E_INVALIDARG;
1754 EnterCriticalSection( &channel->cs );
1756 if (channel->magic != CHANNEL_MAGIC)
1758 LeaveCriticalSection( &channel->cs );
1759 return E_INVALIDARG;
1762 hr = WsReadEnvelopeEnd( msg, NULL );
1764 LeaveCriticalSection( &channel->cs );
1765 return hr;
1768 /**************************************************************************
1769 * WsWriteMessageStart [webservices.@]
1771 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1772 WS_ERROR *error )
1774 struct channel *channel = (struct channel *)handle;
1775 HRESULT hr;
1777 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1778 if (error) FIXME( "ignoring error parameter\n" );
1779 if (ctx) FIXME( "ignoring ctx parameter\n" );
1781 if (!channel || !msg) return E_INVALIDARG;
1783 EnterCriticalSection( &channel->cs );
1785 if (channel->magic != CHANNEL_MAGIC)
1787 LeaveCriticalSection( &channel->cs );
1788 return E_INVALIDARG;
1791 if ((hr = init_writer( channel )) != S_OK) goto done;
1792 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1793 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
1795 done:
1796 LeaveCriticalSection( &channel->cs );
1797 return hr;
1800 /**************************************************************************
1801 * WsWriteMessageEnd [webservices.@]
1803 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1804 WS_ERROR *error )
1806 struct channel *channel = (struct channel *)handle;
1807 HRESULT hr;
1809 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1810 if (error) FIXME( "ignoring error parameter\n" );
1811 if (ctx) FIXME( "ignoring ctx parameter\n" );
1813 if (!channel || !msg) return E_INVALIDARG;
1815 EnterCriticalSection( &channel->cs );
1817 if (channel->magic != CHANNEL_MAGIC)
1819 LeaveCriticalSection( &channel->cs );
1820 return E_INVALIDARG;
1823 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK) hr = send_message( channel, msg );
1825 LeaveCriticalSection( &channel->cs );
1826 return hr;
1829 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
1831 HANDLE handles[] = { wait, cancel };
1832 ULONG nonblocking = 0;
1833 HRESULT hr = S_OK;
1835 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
1837 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
1839 case 0:
1840 if ((*ret = accept( socket, NULL, NULL )) != -1)
1842 WSAEventSelect( *ret, NULL, 0 );
1843 ioctlsocket( *ret, FIONBIO, &nonblocking );
1844 break;
1846 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
1847 break;
1849 case 1:
1850 hr = WS_E_OPERATION_ABORTED;
1851 break;
1853 default:
1854 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
1855 break;
1858 return hr;
1861 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
1863 struct channel *channel = (struct channel *)handle;
1864 HRESULT hr;
1866 EnterCriticalSection( &channel->cs );
1868 if (channel->magic != CHANNEL_MAGIC)
1870 LeaveCriticalSection( &channel->cs );
1871 return E_INVALIDARG;
1874 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
1876 LeaveCriticalSection( &channel->cs );
1877 return hr;
1880 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
1882 HANDLE handles[] = { wait, cancel };
1883 ULONG nonblocking = 0;
1884 HRESULT hr;
1886 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
1888 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
1890 case 0:
1891 hr = S_OK;
1892 break;
1894 case 1:
1895 hr = WS_E_OPERATION_ABORTED;
1896 break;
1898 default:
1899 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
1900 break;
1903 WSAEventSelect( socket, NULL, 0 );
1904 ioctlsocket( socket, FIONBIO, &nonblocking );
1905 return hr;
1908 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
1910 struct channel *channel = (struct channel *)handle;
1911 HRESULT hr;
1913 EnterCriticalSection( &channel->cs );
1915 if (channel->magic != CHANNEL_MAGIC)
1917 LeaveCriticalSection( &channel->cs );
1918 return E_INVALIDARG;
1921 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
1923 LeaveCriticalSection( &channel->cs );
1924 return hr;