oleaut32: Remove unnecessary consts.
[wine.git] / dlls / webservices / channel.c
blob5963faf00de631b32b0ad7ad471ab80497b24ca2
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/heap.h"
29 #include "wine/list.h"
30 #include "wine/unicode.h"
31 #include "webservices_private.h"
32 #include "sock.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
36 static const struct prop_desc channel_props[] =
38 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE */
39 { sizeof(UINT64), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_MESSAGE_SIZE */
40 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_START_SIZE */
41 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_FLUSH_SIZE */
42 { sizeof(WS_ENCODING), TRUE }, /* WS_CHANNEL_PROPERTY_ENCODING */
43 { sizeof(WS_ENVELOPE_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ENVELOPE_VERSION */
44 { sizeof(WS_ADDRESSING_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ADDRESSING_VERSION */
45 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_SESSION_DICTIONARY_SIZE */
46 { sizeof(WS_CHANNEL_STATE), TRUE }, /* WS_CHANNEL_PROPERTY_STATE */
47 { sizeof(WS_CALLBACK_MODEL), FALSE }, /* WS_CHANNEL_PROPERTY_ASYNC_CALLBACK_MODEL */
48 { sizeof(WS_IP_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_IP_VERSION */
49 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RESOLVE_TIMEOUT */
50 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CONNECT_TIMEOUT */
51 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_TIMEOUT */
52 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_RESPONSE_TIMEOUT */
53 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_TIMEOUT */
54 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CLOSE_TIMEOUT */
55 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_TIMEOUTS */
56 { sizeof(WS_TRANSFER_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_TRANSFER_MODE */
57 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_INTERFACE */
58 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_HOPS */
59 { sizeof(WS_ENDPOINT_ADDRESS), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_ADDRESS */
60 { sizeof(SOCKADDR_STORAGE), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_IP_ADDRESS */
61 { sizeof(ULONGLONG), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_CONNECTION_ID */
62 { sizeof(WS_CUSTOM_CHANNEL_CALLBACKS), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_CALLBACKS */
63 { 0, FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_PARAMETERS */
64 { sizeof(void *), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_INSTANCE */
65 { sizeof(WS_STRING), TRUE }, /* WS_CHANNEL_PROPERTY_TRANSPORT_URL */
66 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_NO_DELAY */
67 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_KEEP_ALIVES */
68 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_TIME */
69 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_INTERVAL */
70 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_HTTP_SERVER_CONNECTIONS */
71 { sizeof(BOOL), TRUE }, /* WS_CHANNEL_PROPERTY_IS_SESSION_SHUT_DOWN */
72 { sizeof(WS_CHANNEL_TYPE), TRUE }, /* WS_CHANNEL_PROPERTY_CHANNEL_TYPE */
73 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_TRIM_BUFFERED_MESSAGE_SIZE */
74 { sizeof(WS_CHANNEL_ENCODER), FALSE }, /* WS_CHANNEL_PROPERTY_ENCODER */
75 { sizeof(WS_CHANNEL_DECODER), FALSE }, /* WS_CHANNEL_PROPERTY_DECODER */
76 { sizeof(WS_PROTECTION_LEVEL), TRUE }, /* WS_CHANNEL_PROPERTY_PROTECTION_LEVEL */
77 { sizeof(WS_COOKIE_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_COOKIE_MODE */
78 { sizeof(WS_HTTP_PROXY_SETTING_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SETTING_MODE */
79 { sizeof(WS_CUSTOM_HTTP_PROXY), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_HTTP_PROXY */
80 { sizeof(WS_HTTP_MESSAGE_MAPPING), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING */
81 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_HTTP_REDIRECT */
82 { sizeof(WS_HTTP_REDIRECT_CALLBACK_CONTEXT), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_REDIRECT_CALLBACK_CONTEXT */
83 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_FAULTS_AS_ERRORS */
84 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ALLOW_UNSECURED_FAULTS */
85 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_SERVER_SPN */
86 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SPN */
87 { sizeof(ULONG), FALSE } /* WS_CHANNEL_PROPERTY_MAX_HTTP_REQUEST_HEADERS_BUFFER_SIZE */
90 struct task
92 struct list entry;
93 void (*proc)( struct task * );
96 struct queue
98 CRITICAL_SECTION cs;
99 HANDLE wait;
100 HANDLE cancel;
101 HANDLE ready;
102 struct list tasks;
105 static struct task *dequeue_task( struct queue *queue )
107 struct task *task;
109 EnterCriticalSection( &queue->cs );
110 TRACE( "%u tasks queued\n", list_count( &queue->tasks ) );
111 task = LIST_ENTRY( list_head( &queue->tasks ), struct task, entry );
112 if (task) list_remove( &task->entry );
113 LeaveCriticalSection( &queue->cs );
115 TRACE( "returning task %p\n", task );
116 return task;
119 static void CALLBACK queue_runner( TP_CALLBACK_INSTANCE *instance, void *ctx )
121 struct queue *queue = ctx;
122 HANDLE handles[] = { queue->wait, queue->cancel };
124 SetEvent( queue->ready );
125 for (;;)
127 DWORD err = WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
128 switch (err)
130 case WAIT_OBJECT_0:
132 struct task *task;
133 while ((task = dequeue_task( queue )))
135 task->proc( task );
136 heap_free( task );
138 break;
140 case WAIT_OBJECT_0 + 1:
141 TRACE( "cancelled\n" );
142 SetEvent( queue->ready );
143 return;
145 default:
146 ERR( "wait failed %u\n", err );
147 return;
152 static HRESULT start_queue( struct queue *queue )
154 HRESULT hr = E_OUTOFMEMORY;
156 if (queue->wait) return S_OK;
157 list_init( &queue->tasks );
158 if (!(queue->wait = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
159 if (!(queue->cancel = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
160 if (!(queue->ready = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
161 if (!TrySubmitThreadpoolCallback( queue_runner, queue, NULL )) hr = HRESULT_FROM_WIN32( GetLastError() );
162 else
164 WaitForSingleObject( queue->ready, INFINITE );
165 return S_OK;
168 error:
169 CloseHandle( queue->wait );
170 queue->wait = NULL;
171 CloseHandle( queue->cancel );
172 queue->cancel = NULL;
173 CloseHandle( queue->ready );
174 queue->ready = NULL;
175 return hr;
178 static HRESULT queue_task( struct queue *queue, struct task *task )
180 HRESULT hr;
181 if ((hr = start_queue( queue )) != S_OK) return hr;
183 EnterCriticalSection( &queue->cs );
184 TRACE( "queueing task %p\n", task );
185 list_add_tail( &queue->tasks, &task->entry );
186 LeaveCriticalSection( &queue->cs );
188 SetEvent( queue->wait );
189 return WS_S_ASYNC;
192 enum session_state
194 SESSION_STATE_UNINITIALIZED,
195 SESSION_STATE_SETUP_COMPLETE,
196 SESSION_STATE_SHUTDOWN,
199 struct channel
201 ULONG magic;
202 CRITICAL_SECTION cs;
203 WS_CHANNEL_TYPE type;
204 WS_CHANNEL_BINDING binding;
205 WS_CHANNEL_STATE state;
206 WS_ENDPOINT_ADDRESS addr;
207 WS_XML_WRITER *writer;
208 WS_XML_READER *reader;
209 WS_MESSAGE *msg;
210 WS_ENCODING encoding;
211 enum session_state session_state;
212 struct dictionary dict_send;
213 struct dictionary dict_recv;
214 struct queue send_q;
215 struct queue recv_q;
216 union
218 struct
220 HINTERNET session;
221 HINTERNET connect;
222 HINTERNET request;
223 WCHAR *path;
224 DWORD flags;
225 } http;
226 struct
228 SOCKET socket;
229 } tcp;
230 struct
232 SOCKET socket;
233 } udp;
234 } u;
235 char *read_buf;
236 ULONG read_buflen;
237 ULONG read_size;
238 ULONG prop_count;
239 struct prop prop[sizeof(channel_props)/sizeof(channel_props[0])];
242 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
244 static struct channel *alloc_channel(void)
246 static const ULONG count = sizeof(channel_props)/sizeof(channel_props[0]);
247 struct channel *ret;
248 ULONG size = sizeof(*ret) + prop_size( channel_props, count );
250 if (!(ret = heap_alloc_zero( size ))) return NULL;
252 ret->magic = CHANNEL_MAGIC;
253 InitializeCriticalSection( &ret->cs );
254 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.cs");
256 InitializeCriticalSection( &ret->send_q.cs );
257 ret->send_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.send_q.cs");
259 InitializeCriticalSection( &ret->recv_q.cs );
260 ret->recv_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.recv_q.cs");
262 prop_init( channel_props, count, ret->prop, &ret[1] );
263 ret->prop_count = count;
264 return ret;
267 static void clear_addr( WS_ENDPOINT_ADDRESS *addr )
269 heap_free( addr->url.chars );
270 addr->url.chars = NULL;
271 addr->url.length = 0;
274 static void clear_queue( struct queue *queue )
276 struct list *ptr;
278 SetEvent( queue->cancel );
279 WaitForSingleObject( queue->ready, INFINITE );
281 while ((ptr = list_head( &queue->tasks )))
283 struct task *task = LIST_ENTRY( ptr, struct task, entry );
284 list_remove( &task->entry );
285 heap_free( task );
288 CloseHandle( queue->wait );
289 queue->wait = NULL;
290 CloseHandle( queue->cancel );
291 queue->cancel = NULL;
292 CloseHandle( queue->ready );
293 queue->ready = NULL;
296 static void reset_channel( struct channel *channel )
298 clear_queue( &channel->send_q );
299 clear_queue( &channel->recv_q );
300 channel->state = WS_CHANNEL_STATE_CREATED;
301 channel->session_state = SESSION_STATE_UNINITIALIZED;
302 clear_addr( &channel->addr );
303 clear_dict( &channel->dict_send );
304 clear_dict( &channel->dict_recv );
305 channel->msg = NULL;
306 channel->read_size = 0;
308 switch (channel->binding)
310 case WS_HTTP_CHANNEL_BINDING:
311 WinHttpCloseHandle( channel->u.http.request );
312 channel->u.http.request = NULL;
313 WinHttpCloseHandle( channel->u.http.connect );
314 channel->u.http.connect = NULL;
315 WinHttpCloseHandle( channel->u.http.session );
316 channel->u.http.session = NULL;
317 heap_free( channel->u.http.path );
318 channel->u.http.path = NULL;
319 channel->u.http.flags = 0;
320 break;
322 case WS_TCP_CHANNEL_BINDING:
323 closesocket( channel->u.tcp.socket );
324 channel->u.tcp.socket = -1;
325 break;
327 case WS_UDP_CHANNEL_BINDING:
328 closesocket( channel->u.udp.socket );
329 channel->u.udp.socket = -1;
330 break;
332 default: break;
336 static void free_channel( struct channel *channel )
338 reset_channel( channel );
340 WsFreeWriter( channel->writer );
341 WsFreeReader( channel->reader );
343 heap_free( channel->read_buf );
345 channel->send_q.cs.DebugInfo->Spare[0] = 0;
346 DeleteCriticalSection( &channel->send_q.cs );
348 channel->recv_q.cs.DebugInfo->Spare[0] = 0;
349 DeleteCriticalSection( &channel->recv_q.cs );
351 channel->cs.DebugInfo->Spare[0] = 0;
352 DeleteCriticalSection( &channel->cs );
353 heap_free( channel );
356 static HRESULT create_channel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
357 const WS_CHANNEL_PROPERTY *properties, ULONG count, struct channel **ret )
359 struct channel *channel;
360 ULONG i, msg_size = 65536;
361 WS_ENVELOPE_VERSION env_version = WS_ENVELOPE_VERSION_SOAP_1_2;
362 WS_ADDRESSING_VERSION addr_version = WS_ADDRESSING_VERSION_1_0;
363 HRESULT hr;
365 if (!(channel = alloc_channel())) return E_OUTOFMEMORY;
367 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
368 &msg_size, sizeof(msg_size) );
369 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION,
370 &env_version, sizeof(env_version) );
371 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION,
372 &addr_version, sizeof(addr_version) );
374 channel->type = type;
375 channel->binding = binding;
377 switch (channel->binding)
379 case WS_HTTP_CHANNEL_BINDING:
380 channel->encoding = WS_ENCODING_XML_UTF8;
381 break;
383 case WS_TCP_CHANNEL_BINDING:
384 channel->u.tcp.socket = -1;
385 channel->encoding = WS_ENCODING_XML_BINARY_SESSION_1;
386 break;
388 case WS_UDP_CHANNEL_BINDING:
389 channel->u.udp.socket = -1;
390 channel->encoding = WS_ENCODING_XML_UTF8;
391 break;
393 default: break;
396 for (i = 0; i < count; i++)
398 switch (properties[i].id)
400 case WS_CHANNEL_PROPERTY_ENCODING:
401 if (!properties[i].value || properties[i].valueSize != sizeof(channel->encoding))
403 free_channel( channel );
404 return E_INVALIDARG;
406 channel->encoding = *(WS_ENCODING *)properties[i].value;
407 break;
409 default:
410 if ((hr = prop_set( channel->prop, channel->prop_count, properties[i].id, properties[i].value,
411 properties[i].valueSize )) != S_OK)
413 free_channel( channel );
414 return hr;
416 break;
420 *ret = channel;
421 return S_OK;
424 /**************************************************************************
425 * WsCreateChannel [webservices.@]
427 HRESULT WINAPI WsCreateChannel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
428 const WS_CHANNEL_PROPERTY *properties, ULONG count,
429 const WS_SECURITY_DESCRIPTION *desc, WS_CHANNEL **handle,
430 WS_ERROR *error )
432 struct channel *channel;
433 HRESULT hr;
435 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
436 if (error) FIXME( "ignoring error parameter\n" );
437 if (desc) FIXME( "ignoring security description\n" );
439 if (!handle) return E_INVALIDARG;
441 if (type != WS_CHANNEL_TYPE_REQUEST && type != WS_CHANNEL_TYPE_DUPLEX &&
442 type != WS_CHANNEL_TYPE_DUPLEX_SESSION)
444 FIXME( "channel type %u not implemented\n", type );
445 return E_NOTIMPL;
447 if (binding != WS_HTTP_CHANNEL_BINDING && binding != WS_TCP_CHANNEL_BINDING &&
448 binding != WS_UDP_CHANNEL_BINDING)
450 FIXME( "channel binding %u not implemented\n", binding );
451 return E_NOTIMPL;
454 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
456 TRACE( "created %p\n", channel );
457 *handle = (WS_CHANNEL *)channel;
458 return S_OK;
461 /**************************************************************************
462 * WsCreateChannelForListener [webservices.@]
464 HRESULT WINAPI WsCreateChannelForListener( WS_LISTENER *listener_handle, const WS_CHANNEL_PROPERTY *properties,
465 ULONG count, WS_CHANNEL **handle, WS_ERROR *error )
467 struct channel *channel;
468 WS_CHANNEL_TYPE type;
469 WS_CHANNEL_BINDING binding;
470 HRESULT hr;
472 TRACE( "%p %p %u %p %p\n", listener_handle, properties, count, handle, error );
473 if (error) FIXME( "ignoring error parameter\n" );
475 if (!listener_handle || !handle) return E_INVALIDARG;
477 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_TYPE, &type,
478 sizeof(type), NULL )) != S_OK) return hr;
480 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_BINDING, &binding,
481 sizeof(binding), NULL )) != S_OK) return hr;
483 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
485 TRACE( "created %p\n", channel );
486 *handle = (WS_CHANNEL *)channel;
487 return S_OK;
490 /**************************************************************************
491 * WsFreeChannel [webservices.@]
493 void WINAPI WsFreeChannel( WS_CHANNEL *handle )
495 struct channel *channel = (struct channel *)handle;
497 TRACE( "%p\n", handle );
499 if (!channel) return;
501 EnterCriticalSection( &channel->cs );
503 if (channel->magic != CHANNEL_MAGIC)
505 LeaveCriticalSection( &channel->cs );
506 return;
509 channel->magic = 0;
511 LeaveCriticalSection( &channel->cs );
512 free_channel( channel );
515 /**************************************************************************
516 * WsResetChannel [webservices.@]
518 HRESULT WINAPI WsResetChannel( WS_CHANNEL *handle, WS_ERROR *error )
520 struct channel *channel = (struct channel *)handle;
522 TRACE( "%p %p\n", handle, error );
523 if (error) FIXME( "ignoring error parameter\n" );
525 if (!channel) return E_INVALIDARG;
527 EnterCriticalSection( &channel->cs );
529 if (channel->magic != CHANNEL_MAGIC)
531 LeaveCriticalSection( &channel->cs );
532 return E_INVALIDARG;
535 if (channel->state != WS_CHANNEL_STATE_CREATED && channel->state != WS_CHANNEL_STATE_CLOSED)
537 LeaveCriticalSection( &channel->cs );
538 return WS_E_INVALID_OPERATION;
541 reset_channel( channel );
543 LeaveCriticalSection( &channel->cs );
544 return S_OK;
547 /**************************************************************************
548 * WsGetChannelProperty [webservices.@]
550 HRESULT WINAPI WsGetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, void *buf,
551 ULONG size, WS_ERROR *error )
553 struct channel *channel = (struct channel *)handle;
554 HRESULT hr = S_OK;
556 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
557 if (error) FIXME( "ignoring error parameter\n" );
559 if (!channel) return E_INVALIDARG;
561 EnterCriticalSection( &channel->cs );
563 if (channel->magic != CHANNEL_MAGIC)
565 LeaveCriticalSection( &channel->cs );
566 return E_INVALIDARG;
569 switch (id)
571 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE:
572 if (!buf || size != sizeof(channel->type)) hr = E_INVALIDARG;
573 else *(WS_CHANNEL_TYPE *)buf = channel->type;
574 break;
576 case WS_CHANNEL_PROPERTY_ENCODING:
577 if (!buf || size != sizeof(channel->encoding)) hr = E_INVALIDARG;
578 else *(WS_ENCODING *)buf = channel->encoding;
579 break;
581 default:
582 hr = prop_get( channel->prop, channel->prop_count, id, buf, size );
585 LeaveCriticalSection( &channel->cs );
586 return hr;
589 /**************************************************************************
590 * WsSetChannelProperty [webservices.@]
592 HRESULT WINAPI WsSetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, const void *value,
593 ULONG size, WS_ERROR *error )
595 struct channel *channel = (struct channel *)handle;
596 HRESULT hr;
598 TRACE( "%p %u %p %u\n", handle, id, value, size );
599 if (error) FIXME( "ignoring error parameter\n" );
601 if (!channel) return E_INVALIDARG;
603 EnterCriticalSection( &channel->cs );
605 if (channel->magic != CHANNEL_MAGIC)
607 LeaveCriticalSection( &channel->cs );
608 return E_INVALIDARG;
611 hr = prop_set( channel->prop, channel->prop_count, id, value, size );
613 LeaveCriticalSection( &channel->cs );
614 return hr;
617 static HRESULT open_channel( struct channel *channel, const WS_ENDPOINT_ADDRESS *endpoint )
619 if (endpoint->headers || endpoint->extensions || endpoint->identity)
621 FIXME( "headers, extensions or identity not supported\n" );
622 return E_NOTIMPL;
625 if (!(channel->addr.url.chars = heap_alloc( endpoint->url.length * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
626 memcpy( channel->addr.url.chars, endpoint->url.chars, endpoint->url.length * sizeof(WCHAR) );
627 channel->addr.url.length = endpoint->url.length;
629 channel->state = WS_CHANNEL_STATE_OPEN;
630 return S_OK;
633 /**************************************************************************
634 * WsOpenChannel [webservices.@]
636 HRESULT WINAPI WsOpenChannel( WS_CHANNEL *handle, const WS_ENDPOINT_ADDRESS *endpoint,
637 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
639 struct channel *channel = (struct channel *)handle;
640 HRESULT hr;
642 TRACE( "%p %p %p %p\n", handle, endpoint, ctx, error );
643 if (error) FIXME( "ignoring error parameter\n" );
644 if (ctx) FIXME( "ignoring ctx parameter\n" );
646 if (!channel || !endpoint) return E_INVALIDARG;
648 EnterCriticalSection( &channel->cs );
650 if (channel->magic != CHANNEL_MAGIC)
652 LeaveCriticalSection( &channel->cs );
653 return E_INVALIDARG;
656 if (channel->state != WS_CHANNEL_STATE_CREATED)
658 LeaveCriticalSection( &channel->cs );
659 return WS_E_INVALID_OPERATION;
662 hr = open_channel( channel, endpoint );
664 LeaveCriticalSection( &channel->cs );
665 return hr;
668 enum frame_record_type
670 FRAME_RECORD_TYPE_VERSION,
671 FRAME_RECORD_TYPE_MODE,
672 FRAME_RECORD_TYPE_VIA,
673 FRAME_RECORD_TYPE_KNOWN_ENCODING,
674 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING,
675 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE,
676 FRAME_RECORD_TYPE_SIZED_ENVELOPE,
677 FRAME_RECORD_TYPE_END,
678 FRAME_RECORD_TYPE_FAULT,
679 FRAME_RECORD_TYPE_UPGRADE_REQUEST,
680 FRAME_RECORD_TYPE_UPGRADE_RESPONSE,
681 FRAME_RECORD_TYPE_PREAMBLE_ACK,
682 FRAME_RECORD_TYPE_PREAMBLE_END,
685 static HRESULT send_byte( SOCKET socket, BYTE byte )
687 int count = send( socket, (char *)&byte, 1, 0 );
688 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
689 if (count != 1) return WS_E_OTHER;
690 return S_OK;
693 static HRESULT shutdown_session( struct channel *channel )
695 HRESULT hr;
697 if (channel->state != WS_CHANNEL_STATE_OPEN ||
698 (channel->type != WS_CHANNEL_TYPE_OUTPUT_SESSION &&
699 channel->type != WS_CHANNEL_TYPE_DUPLEX_SESSION) ||
700 channel->session_state >= SESSION_STATE_SHUTDOWN) return WS_E_INVALID_OPERATION;
702 switch (channel->binding)
704 case WS_TCP_CHANNEL_BINDING:
705 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_END )) != S_OK) return hr;
706 channel->session_state = SESSION_STATE_SHUTDOWN;
707 return S_OK;
709 default:
710 FIXME( "unhandled binding %u\n", channel->binding );
711 return E_NOTIMPL;
715 HRESULT WINAPI WsShutdownSessionChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
717 struct channel *channel = (struct channel *)handle;
718 HRESULT hr;
720 TRACE( "%p %p %p\n", handle, ctx, error );
721 if (error) FIXME( "ignoring error parameter\n" );
722 if (ctx) FIXME( "ignoring ctx parameter\n" );
724 if (!channel) return E_INVALIDARG;
726 EnterCriticalSection( &channel->cs );
728 if (channel->magic != CHANNEL_MAGIC)
730 LeaveCriticalSection( &channel->cs );
731 return E_INVALIDARG;
734 hr = shutdown_session( channel );
736 LeaveCriticalSection( &channel->cs );
737 return hr;
740 static void close_channel( struct channel *channel )
742 reset_channel( channel );
743 channel->state = WS_CHANNEL_STATE_CLOSED;
746 /**************************************************************************
747 * WsCloseChannel [webservices.@]
749 HRESULT WINAPI WsCloseChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
751 struct channel *channel = (struct channel *)handle;
753 TRACE( "%p %p %p\n", handle, ctx, error );
754 if (error) FIXME( "ignoring error parameter\n" );
755 if (ctx) FIXME( "ignoring ctx parameter\n" );
757 if (!channel) return E_INVALIDARG;
759 EnterCriticalSection( &channel->cs );
761 if (channel->magic != CHANNEL_MAGIC)
763 LeaveCriticalSection( &channel->cs );
764 return E_INVALIDARG;
767 close_channel( channel );
769 LeaveCriticalSection( &channel->cs );
770 return S_OK;
773 static HRESULT parse_http_url( const WCHAR *url, ULONG len, URL_COMPONENTS *uc )
775 HRESULT hr = E_OUTOFMEMORY;
776 WCHAR *tmp;
777 DWORD err;
779 memset( uc, 0, sizeof(*uc) );
780 uc->dwStructSize = sizeof(*uc);
781 uc->dwHostNameLength = 128;
782 uc->lpszHostName = heap_alloc( uc->dwHostNameLength * sizeof(WCHAR) );
783 uc->dwUrlPathLength = 128;
784 uc->lpszUrlPath = heap_alloc( uc->dwUrlPathLength * sizeof(WCHAR) );
785 uc->dwExtraInfoLength = 128;
786 uc->lpszExtraInfo = heap_alloc( uc->dwExtraInfoLength * sizeof(WCHAR) );
787 if (!uc->lpszHostName || !uc->lpszUrlPath || !uc->lpszExtraInfo) goto error;
789 if (!WinHttpCrackUrl( url, len, ICU_DECODE, uc ))
791 if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
793 hr = HRESULT_FROM_WIN32( err );
794 goto error;
796 if (!(tmp = heap_realloc( uc->lpszHostName, uc->dwHostNameLength * sizeof(WCHAR) ))) goto error;
797 uc->lpszHostName = tmp;
798 if (!(tmp = heap_realloc( uc->lpszUrlPath, uc->dwUrlPathLength * sizeof(WCHAR) ))) goto error;
799 uc->lpszUrlPath = tmp;
800 if (!(tmp = heap_realloc( uc->lpszExtraInfo, uc->dwExtraInfoLength * sizeof(WCHAR) ))) goto error;
801 uc->lpszExtraInfo = tmp;
802 WinHttpCrackUrl( url, len, ICU_DECODE, uc );
805 return S_OK;
807 error:
808 heap_free( uc->lpszHostName );
809 heap_free( uc->lpszUrlPath );
810 heap_free( uc->lpszExtraInfo );
811 return hr;
814 static HRESULT connect_channel_http( struct channel *channel )
816 static const WCHAR agentW[] =
817 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
818 HINTERNET ses = NULL, con = NULL;
819 URL_COMPONENTS uc;
820 HRESULT hr;
822 if (channel->u.http.connect) return S_OK;
824 if ((hr = parse_http_url( channel->addr.url.chars, channel->addr.url.length, &uc )) != S_OK) return hr;
825 if (!(channel->u.http.path = heap_alloc( (uc.dwUrlPathLength + uc.dwExtraInfoLength + 1) * sizeof(WCHAR) )))
827 hr = E_OUTOFMEMORY;
828 goto done;
830 else
832 strcpyW( channel->u.http.path, uc.lpszUrlPath );
833 if (uc.dwExtraInfoLength) strcatW( channel->u.http.path, uc.lpszExtraInfo );
836 channel->u.http.flags = WINHTTP_FLAG_REFRESH;
837 switch (uc.nScheme)
839 case INTERNET_SCHEME_HTTP: break;
840 case INTERNET_SCHEME_HTTPS:
841 channel->u.http.flags |= WINHTTP_FLAG_SECURE;
842 break;
844 default:
845 hr = WS_E_INVALID_ENDPOINT_URL;
846 goto done;
849 if (!(ses = WinHttpOpen( agentW, 0, NULL, NULL, 0 )))
851 hr = HRESULT_FROM_WIN32( GetLastError() );
852 goto done;
854 if (!(con = WinHttpConnect( ses, uc.lpszHostName, uc.nPort, 0 )))
856 hr = HRESULT_FROM_WIN32( GetLastError() );
857 goto done;
860 channel->u.http.session = ses;
861 channel->u.http.connect = con;
863 done:
864 if (hr != S_OK)
866 WinHttpCloseHandle( con );
867 WinHttpCloseHandle( ses );
869 heap_free( uc.lpszHostName );
870 heap_free( uc.lpszUrlPath );
871 heap_free( uc.lpszExtraInfo );
872 return hr;
875 static HRESULT connect_channel_tcp( struct channel *channel )
877 struct sockaddr_storage storage;
878 struct sockaddr *addr = (struct sockaddr *)&storage;
879 int addr_len;
880 WS_URL_SCHEME_TYPE scheme;
881 WCHAR *host;
882 USHORT port;
883 HRESULT hr;
885 if (channel->u.tcp.socket != -1) return S_OK;
887 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
888 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
890 heap_free( host );
891 return WS_E_INVALID_ENDPOINT_URL;
894 winsock_init();
896 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
897 heap_free( host );
898 if (hr != S_OK) return hr;
900 if ((channel->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
901 return HRESULT_FROM_WIN32( WSAGetLastError() );
903 if (connect( channel->u.tcp.socket, addr, addr_len ) < 0)
905 closesocket( channel->u.tcp.socket );
906 channel->u.tcp.socket = -1;
907 return HRESULT_FROM_WIN32( WSAGetLastError() );
910 return S_OK;
913 static HRESULT connect_channel_udp( struct channel *channel )
915 struct sockaddr_storage storage;
916 struct sockaddr *addr = (struct sockaddr *)&storage;
917 int addr_len;
918 WS_URL_SCHEME_TYPE scheme;
919 WCHAR *host;
920 USHORT port;
921 HRESULT hr;
923 if (channel->u.udp.socket != -1) return S_OK;
925 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
926 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
928 heap_free( host );
929 return WS_E_INVALID_ENDPOINT_URL;
932 winsock_init();
934 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
935 heap_free( host );
936 if (hr != S_OK) return hr;
938 if ((channel->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
939 return HRESULT_FROM_WIN32( WSAGetLastError() );
941 if (connect( channel->u.udp.socket, addr, addr_len ) < 0)
943 closesocket( channel->u.udp.socket );
944 channel->u.udp.socket = -1;
945 return HRESULT_FROM_WIN32( WSAGetLastError() );
948 return S_OK;
951 static HRESULT connect_channel( struct channel *channel )
953 switch (channel->binding)
955 case WS_HTTP_CHANNEL_BINDING:
956 return connect_channel_http( channel );
958 case WS_TCP_CHANNEL_BINDING:
959 return connect_channel_tcp( channel );
961 case WS_UDP_CHANNEL_BINDING:
962 return connect_channel_udp( channel );
964 default:
965 ERR( "unhandled binding %u\n", channel->binding );
966 return E_NOTIMPL;
970 static HRESULT send_message_http( HINTERNET request, BYTE *data, ULONG len )
972 if (!WinHttpSendRequest( request, NULL, 0, data, len, len, 0 ))
973 return HRESULT_FROM_WIN32( GetLastError() );
975 if (!WinHttpReceiveResponse( request, NULL ))
976 return HRESULT_FROM_WIN32( GetLastError() );
977 return S_OK;
980 static HRESULT send_bytes( SOCKET socket, BYTE *bytes, int len )
982 int count = send( socket, (char *)bytes, len, 0 );
983 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
984 if (count != len) return WS_E_OTHER;
985 return S_OK;
988 static HRESULT send_size( SOCKET socket, ULONG size )
990 HRESULT hr;
991 if (size < 0x80) return send_byte( socket, size );
992 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
993 if ((size >>= 7) < 0x80) return send_byte( socket, size );
994 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
995 if ((size >>= 7) < 0x80) return send_byte( socket, size );
996 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
997 if ((size >>= 7) < 0x80) return send_byte( socket, size );
998 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
999 if ((size >>= 7) < 0x08) return send_byte( socket, size );
1000 return E_INVALIDARG;
1003 static inline ULONG size_length( ULONG size )
1005 if (size < 0x80) return 1;
1006 if (size < 0x4000) return 2;
1007 if (size < 0x200000) return 3;
1008 if (size < 0x10000000) return 4;
1009 return 5;
1012 static ULONG string_table_size( const struct dictionary *dict )
1014 ULONG i, size = 0;
1015 for (i = 0; i < dict->dict.stringCount; i++)
1017 if (dict->sequence[i] == dict->current_sequence)
1018 size += size_length( dict->dict.strings[i].length ) + dict->dict.strings[i].length;
1020 return size;
1023 static HRESULT send_string_table( SOCKET socket, const struct dictionary *dict )
1025 ULONG i;
1026 HRESULT hr;
1027 for (i = 0; i < dict->dict.stringCount; i++)
1029 if (dict->sequence[i] != dict->current_sequence) continue;
1030 if ((hr = send_size( socket, dict->dict.strings[i].length )) != S_OK) return hr;
1031 if ((hr = send_bytes( socket, dict->dict.strings[i].bytes, dict->dict.strings[i].length )) != S_OK) return hr;
1033 return S_OK;
1036 static HRESULT string_to_utf8( const WS_STRING *str, unsigned char **ret, int *len )
1038 *len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
1039 if (!(*ret = heap_alloc( *len ))) return E_OUTOFMEMORY;
1040 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)*ret, *len, NULL, NULL );
1041 return S_OK;
1044 enum session_mode
1046 SESSION_MODE_INVALID = 0,
1047 SESSION_MODE_SINGLETON = 1,
1048 SESSION_MODE_DUPLEX = 2,
1049 SESSION_MODE_SIMPLEX = 3,
1052 static enum session_mode map_channel_type( struct channel *channel )
1054 switch (channel->type)
1056 case WS_CHANNEL_TYPE_DUPLEX_SESSION: return SESSION_MODE_DUPLEX;
1057 default:
1058 FIXME( "unhandled channel type %08x\n", channel->type );
1059 return SESSION_MODE_INVALID;
1063 enum known_encoding
1065 KNOWN_ENCODING_SOAP11_UTF8 = 0x00,
1066 KNOWN_ENCODING_SOAP11_UTF16 = 0x01,
1067 KNOWN_ENCODING_SOAP11_UTF16LE = 0x02,
1068 KNOWN_ENCODING_SOAP12_UTF8 = 0x03,
1069 KNOWN_ENCODING_SOAP12_UTF16 = 0x04,
1070 KNOWN_ENCODING_SOAP12_UTF16LE = 0x05,
1071 KNOWN_ENCODING_SOAP12_MTOM = 0x06,
1072 KNOWN_ENCODING_SOAP12_BINARY = 0x07,
1073 KNOWN_ENCODING_SOAP12_BINARY_SESSION = 0x08,
1076 static enum known_encoding map_channel_encoding( struct channel *channel )
1078 WS_ENVELOPE_VERSION version;
1080 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version, sizeof(version) );
1082 switch (version)
1084 case WS_ENVELOPE_VERSION_SOAP_1_1:
1085 switch (channel->encoding)
1087 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP11_UTF8;
1088 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP11_UTF16LE;
1089 default:
1090 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1091 return 0;
1093 case WS_ENVELOPE_VERSION_SOAP_1_2:
1094 switch (channel->encoding)
1096 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP12_UTF8;
1097 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP12_UTF16LE;
1098 case WS_ENCODING_XML_BINARY_1: return KNOWN_ENCODING_SOAP12_BINARY;
1099 case WS_ENCODING_XML_BINARY_SESSION_1: return KNOWN_ENCODING_SOAP12_BINARY_SESSION;
1100 default:
1101 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1102 return 0;
1104 default:
1105 ERR( "unhandled version %u\n", version );
1106 return 0;
1110 #define FRAME_VERSION_MAJOR 1
1111 #define FRAME_VERSION_MINOR 1
1113 static HRESULT send_preamble( struct channel *channel )
1115 unsigned char *url;
1116 HRESULT hr;
1117 int len;
1119 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VERSION )) != S_OK) return hr;
1120 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MAJOR )) != S_OK) return hr;
1121 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MINOR )) != S_OK) return hr;
1123 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_MODE )) != S_OK) return hr;
1124 if ((hr = send_byte( channel->u.tcp.socket, map_channel_type(channel) )) != S_OK) return hr;
1126 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VIA )) != S_OK) return hr;
1127 if ((hr = string_to_utf8( &channel->addr.url, &url, &len )) != S_OK) return hr;
1128 if ((hr = send_size( channel->u.tcp.socket, len )) != S_OK) goto done;
1129 if ((hr = send_bytes( channel->u.tcp.socket, url, len )) != S_OK) goto done;
1131 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_KNOWN_ENCODING )) != S_OK) goto done;
1132 if ((hr = send_byte( channel->u.tcp.socket, map_channel_encoding(channel) )) != S_OK) goto done;
1133 hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_END );
1135 done:
1136 heap_free( url );
1137 return hr;
1140 static HRESULT receive_bytes( struct channel *channel, unsigned char *bytes, int len )
1142 int count = recv( channel->u.tcp.socket, (char *)bytes, len, 0 );
1143 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1144 if (count != len) return WS_E_INVALID_FORMAT;
1145 return S_OK;
1148 static HRESULT receive_preamble_ack( struct channel *channel )
1150 unsigned char byte;
1151 HRESULT hr;
1153 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1154 if (byte != FRAME_RECORD_TYPE_PREAMBLE_ACK) return WS_E_INVALID_FORMAT;
1155 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1156 return S_OK;
1159 static HRESULT send_sized_envelope( struct channel *channel, BYTE *data, ULONG len )
1161 ULONG table_size = string_table_size( &channel->dict_send );
1162 HRESULT hr;
1164 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_SIZED_ENVELOPE )) != S_OK) return hr;
1165 if ((hr = send_size( channel->u.tcp.socket, size_length(table_size) + table_size + len )) != S_OK) return hr;
1166 if ((hr = send_size( channel->u.tcp.socket, table_size )) != S_OK) return hr;
1167 if ((hr = send_string_table( channel->u.tcp.socket, &channel->dict_send )) != S_OK) return hr;
1168 return send_bytes( channel->u.tcp.socket, data, len );
1171 static HRESULT open_http_request( struct channel *channel, HINTERNET *req )
1173 static const WCHAR postW[] = {'P','O','S','T',0};
1174 if ((*req = WinHttpOpenRequest( channel->u.http.connect, postW, channel->u.http.path,
1175 NULL, NULL, NULL, channel->u.http.flags ))) return S_OK;
1176 return HRESULT_FROM_WIN32( GetLastError() );
1179 static HRESULT send_message( struct channel *channel, WS_MESSAGE *msg )
1181 WS_XML_WRITER *writer;
1182 WS_BYTES buf;
1183 HRESULT hr;
1185 channel->msg = msg;
1186 if ((hr = connect_channel( channel )) != S_OK) return hr;
1188 WsGetMessageProperty( channel->msg, WS_MESSAGE_PROPERTY_BODY_WRITER, &writer, sizeof(writer), NULL );
1189 WsGetWriterProperty( writer, WS_XML_WRITER_PROPERTY_BYTES, &buf, sizeof(buf), NULL );
1191 switch (channel->binding)
1193 case WS_HTTP_CHANNEL_BINDING:
1194 if (channel->u.http.request)
1196 WinHttpCloseHandle( channel->u.http.request );
1197 channel->u.http.request = NULL;
1199 if ((hr = open_http_request( channel, &channel->u.http.request )) != S_OK) return hr;
1200 if ((hr = message_insert_http_headers( msg, channel->u.http.request )) != S_OK) return hr;
1201 return send_message_http( channel->u.http.request, buf.bytes, buf.length );
1203 case WS_TCP_CHANNEL_BINDING:
1204 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1206 switch (channel->session_state)
1208 case SESSION_STATE_UNINITIALIZED:
1209 if ((hr = send_preamble( channel )) != S_OK) return hr;
1210 if ((hr = receive_preamble_ack( channel )) != S_OK) return hr;
1211 /* fall through */
1213 case SESSION_STATE_SETUP_COMPLETE:
1214 return send_sized_envelope( channel, buf.bytes, buf.length );
1216 default:
1217 ERR( "unhandled session state %u\n", channel->session_state );
1218 return WS_E_OTHER;
1221 return send_bytes( channel->u.tcp.socket, buf.bytes, buf.length );
1223 case WS_UDP_CHANNEL_BINDING:
1224 return send_bytes( channel->u.udp.socket, buf.bytes, buf.length );
1226 default:
1227 ERR( "unhandled binding %u\n", channel->binding );
1228 return E_NOTIMPL;
1232 HRESULT channel_send_message( WS_CHANNEL *handle, WS_MESSAGE *msg )
1234 struct channel *channel = (struct channel *)handle;
1235 HRESULT hr;
1237 EnterCriticalSection( &channel->cs );
1239 if (channel->magic != CHANNEL_MAGIC)
1241 LeaveCriticalSection( &channel->cs );
1242 return E_INVALIDARG;
1245 hr = send_message( channel, msg );
1247 LeaveCriticalSection( &channel->cs );
1248 return hr;
1251 static HRESULT CALLBACK dict_cb( void *state, const WS_XML_STRING *str, BOOL *found, ULONG *id, WS_ERROR *error )
1253 struct dictionary *dict = state;
1254 HRESULT hr = S_OK;
1255 BYTE *bytes;
1256 int index;
1258 if ((index = find_string( dict, str->bytes, str->length, id )) == -1)
1260 *found = TRUE;
1261 return S_OK;
1264 if (!(bytes = heap_alloc( str->length ))) return E_OUTOFMEMORY;
1265 memcpy( bytes, str->bytes, str->length );
1266 if ((hr = insert_string( dict, bytes, str->length, index, id )) == S_OK)
1268 *found = TRUE;
1269 return S_OK;
1271 heap_free( bytes );
1273 *found = FALSE;
1274 return hr;
1277 static HRESULT init_writer( struct channel *channel )
1279 WS_XML_WRITER_BUFFER_OUTPUT buf = {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER}};
1280 WS_XML_WRITER_TEXT_ENCODING text = {{WS_XML_WRITER_ENCODING_TYPE_TEXT}, WS_CHARSET_UTF8};
1281 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1282 HRESULT hr;
1284 if (!channel->writer && (hr = WsCreateWriter( NULL, 0, &channel->writer, NULL )) != S_OK) return hr;
1286 switch (channel->encoding)
1288 case WS_ENCODING_XML_UTF8:
1289 return WsSetOutput( channel->writer, &text.encoding, &buf.output, NULL, 0, NULL );
1291 case WS_ENCODING_XML_BINARY_SESSION_1:
1292 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1293 /* fall through */
1295 case WS_ENCODING_XML_BINARY_1:
1296 return WsSetOutput( channel->writer, &bin.encoding, &buf.output, NULL, 0, NULL );
1298 default:
1299 FIXME( "unhandled encoding %u\n", channel->encoding );
1300 return WS_E_NOT_SUPPORTED;
1304 static HRESULT write_message( struct channel *channel, WS_MESSAGE *msg, const WS_ELEMENT_DESCRIPTION *desc,
1305 WS_WRITE_OPTION option, const void *body, ULONG size )
1307 HRESULT hr;
1308 if ((hr = writer_set_lookup( channel->writer, TRUE )) != S_OK) return hr;
1309 if ((hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL )) != S_OK) return hr;
1310 if ((hr = writer_set_lookup( channel->writer, FALSE )) != S_OK) return hr;
1311 channel->dict_send.current_sequence++;
1312 if ((hr = writer_set_dict_callback( channel->writer, dict_cb, &channel->dict_send )) != S_OK) return hr;
1313 if ((hr = WsWriteBody( msg, desc, option, body, size, NULL )) != S_OK) return hr;
1314 return WsWriteEnvelopeEnd( msg, NULL );
1317 /**************************************************************************
1318 * WsSendMessage [webservices.@]
1320 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1321 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1322 WS_ERROR *error )
1324 struct channel *channel = (struct channel *)handle;
1325 HRESULT hr;
1327 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1328 if (error) FIXME( "ignoring error parameter\n" );
1329 if (ctx) FIXME( "ignoring ctx parameter\n" );
1331 if (!channel || !msg || !desc) return E_INVALIDARG;
1333 EnterCriticalSection( &channel->cs );
1335 if (channel->magic != CHANNEL_MAGIC)
1337 LeaveCriticalSection( &channel->cs );
1338 return E_INVALIDARG;
1341 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK) goto done;
1342 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1343 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1345 if ((hr = init_writer( channel )) != S_OK) goto done;
1346 if ((hr = write_message( channel, msg, desc->bodyElementDescription, option, body, size )) != S_OK) goto done;
1347 hr = send_message( channel, msg );
1349 done:
1350 LeaveCriticalSection( &channel->cs );
1351 return hr;
1354 /**************************************************************************
1355 * WsSendReplyMessage [webservices.@]
1357 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1358 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1359 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1361 struct channel *channel = (struct channel *)handle;
1362 GUID req_id;
1363 HRESULT hr;
1365 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1366 if (error) FIXME( "ignoring error parameter\n" );
1367 if (ctx) FIXME( "ignoring ctx parameter\n" );
1369 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1371 EnterCriticalSection( &channel->cs );
1373 if (channel->magic != CHANNEL_MAGIC)
1375 LeaveCriticalSection( &channel->cs );
1376 return E_INVALIDARG;
1379 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1380 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1381 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1382 if ((hr = message_get_id( request, &req_id )) != S_OK) goto done;
1383 if ((hr = message_set_request_id( msg, &req_id )) != S_OK) goto done;
1385 if ((hr = init_writer( channel )) != S_OK) goto done;
1386 if ((hr = write_message( channel, msg, desc->bodyElementDescription, option, body, size )) != S_OK) goto done;
1387 hr = send_message( channel, msg );
1389 done:
1390 LeaveCriticalSection( &channel->cs );
1391 return hr;
1394 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1396 if (!channel->read_buf)
1398 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1399 channel->read_buflen = size;
1400 return S_OK;
1402 if (channel->read_buflen < size)
1404 char *tmp;
1405 ULONG new_size = max( size, channel->read_buflen * 2 );
1406 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1407 channel->read_buf = tmp;
1408 channel->read_buflen = new_size;
1410 return S_OK;
1413 static HRESULT init_reader( struct channel *channel )
1415 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1416 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1417 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1418 WS_XML_READER_ENCODING *encoding;
1419 HRESULT hr;
1421 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1423 switch (channel->encoding)
1425 case WS_ENCODING_XML_UTF8:
1426 text.charSet = WS_CHARSET_UTF8;
1427 encoding = &text.encoding;
1428 break;
1430 case WS_ENCODING_XML_BINARY_SESSION_1:
1431 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1432 bin.dynamicDictionary = &channel->dict_recv.dict;
1433 /* fall through */
1435 case WS_ENCODING_XML_BINARY_1:
1436 encoding = &bin.encoding;
1437 break;
1439 default:
1440 FIXME( "unhandled encoding %u\n", channel->encoding );
1441 return WS_E_NOT_SUPPORTED;
1444 buf.encodedData = channel->read_buf;
1445 buf.encodedDataSize = channel->read_size;
1446 return WsSetInput( channel->reader, encoding, &buf.input, NULL, 0, NULL );
1449 #define INITIAL_READ_BUFFER_SIZE 4096
1450 static HRESULT receive_message_http( struct channel *channel )
1452 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1453 ULONG max_len;
1454 HRESULT hr;
1456 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1457 &max_len, sizeof(max_len) );
1459 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1460 channel->read_size = 0;
1461 for (;;)
1463 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1465 return HRESULT_FROM_WIN32( GetLastError() );
1467 if (!len) break;
1468 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1469 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1471 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1473 return HRESULT_FROM_WIN32( GetLastError() );
1475 if (!bytes_read) break;
1476 channel->read_size += bytes_read;
1477 offset += bytes_read;
1480 return init_reader( channel );
1483 static HRESULT receive_message_unsized( struct channel *channel, SOCKET socket )
1485 int bytes_read;
1486 ULONG max_len;
1487 HRESULT hr;
1489 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1490 &max_len, sizeof(max_len) );
1492 if ((hr = resize_read_buffer( channel, max_len )) != S_OK) return hr;
1494 channel->read_size = 0;
1495 if ((bytes_read = recv( socket, channel->read_buf, max_len, 0 )) < 0)
1497 return HRESULT_FROM_WIN32( WSAGetLastError() );
1499 channel->read_size = bytes_read;
1500 return S_OK;
1503 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1505 unsigned int offset = 0, to_read = size;
1506 int bytes_read;
1507 HRESULT hr;
1509 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1511 channel->read_size = 0;
1512 while (channel->read_size < size)
1514 if ((bytes_read = recv( channel->u.tcp.socket, channel->read_buf + offset, to_read, 0 )) < 0)
1516 return HRESULT_FROM_WIN32( WSAGetLastError() );
1518 if (!bytes_read) break;
1519 channel->read_size += bytes_read;
1520 to_read -= bytes_read;
1521 offset += bytes_read;
1523 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1524 return S_OK;
1527 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1529 unsigned char byte;
1530 HRESULT hr;
1532 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1533 *size = byte & 0x7f;
1534 if (!(byte & 0x80)) return S_OK;
1536 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1537 *size += (byte & 0x7f) << 7;
1538 if (!(byte & 0x80)) return S_OK;
1540 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1541 *size += (byte & 0x7f) << 14;
1542 if (!(byte & 0x80)) return S_OK;
1544 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1545 *size += (byte & 0x7f) << 21;
1546 if (!(byte & 0x80)) return S_OK;
1548 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1549 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1550 *size += byte << 28;
1551 return S_OK;
1554 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1556 switch (encoding)
1558 case KNOWN_ENCODING_SOAP11_UTF8:
1559 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1560 case KNOWN_ENCODING_SOAP11_UTF16:
1561 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1562 case KNOWN_ENCODING_SOAP11_UTF16LE:
1563 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1564 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1565 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1566 default:
1567 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1568 return WS_ENCODING_XML_UTF8;
1572 static HRESULT receive_preamble( struct channel *channel )
1574 unsigned char type;
1575 HRESULT hr;
1577 for (;;)
1579 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1580 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1581 switch (type)
1583 case FRAME_RECORD_TYPE_VERSION:
1585 unsigned char major, minor;
1586 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1587 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1588 TRACE( "major %u minor %u\n", major, major );
1589 break;
1591 case FRAME_RECORD_TYPE_MODE:
1593 unsigned char mode;
1594 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1595 TRACE( "mode %u\n", mode );
1596 break;
1598 case FRAME_RECORD_TYPE_VIA:
1600 unsigned int size;
1601 unsigned char *url;
1603 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1604 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1605 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1607 heap_free( url );
1608 return hr;
1610 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1611 heap_free( url ); /* FIXME: verify */
1612 break;
1614 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1616 unsigned char encoding;
1617 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1618 TRACE( "encoding %u\n", encoding );
1619 channel->encoding = map_known_encoding( encoding );
1620 break;
1622 default:
1623 WARN( "unhandled record type %u\n", type );
1624 return WS_E_INVALID_FORMAT;
1628 return S_OK;
1631 static HRESULT receive_sized_envelope( struct channel *channel )
1633 unsigned char type;
1634 unsigned int size;
1635 HRESULT hr;
1637 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1638 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1639 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1640 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1641 return S_OK;
1644 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1646 const BYTE *buf = *ptr;
1648 if (len < 1) return WS_E_INVALID_FORMAT;
1649 *size = buf[0] & 0x7f;
1650 if (!(buf[0] & 0x80))
1652 *ptr += 1;
1653 return S_OK;
1655 if (len < 2) return WS_E_INVALID_FORMAT;
1656 *size += (buf[1] & 0x7f) << 7;
1657 if (!(buf[1] & 0x80))
1659 *ptr += 2;
1660 return S_OK;
1662 if (len < 3) return WS_E_INVALID_FORMAT;
1663 *size += (buf[2] & 0x7f) << 14;
1664 if (!(buf[2] & 0x80))
1666 *ptr += 3;
1667 return S_OK;
1669 if (len < 4) return WS_E_INVALID_FORMAT;
1670 *size += (buf[3] & 0x7f) << 21;
1671 if (!(buf[3] & 0x80))
1673 *ptr += 4;
1674 return S_OK;
1676 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1677 *size += buf[4] << 28;
1678 *ptr += 5;
1679 return S_OK;
1682 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1684 ULONG size, strings_size, strings_offset;
1685 const BYTE *ptr = buf;
1686 BYTE *bytes;
1687 int index;
1688 HRESULT hr;
1690 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1691 strings_offset = ptr - buf;
1692 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1693 *used = strings_offset + strings_size;
1694 if (!strings_size) return S_OK;
1696 UuidCreate( &dict->dict.guid );
1697 dict->dict.isConst = FALSE;
1699 buflen -= strings_offset;
1700 ptr = buf + strings_offset;
1701 while (ptr < buf + strings_size)
1703 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1705 clear_dict( dict );
1706 return hr;
1708 if (size > buflen)
1710 clear_dict( dict );
1711 return WS_E_INVALID_FORMAT;
1713 buflen -= size;
1714 if (!(bytes = heap_alloc( size )))
1716 hr = E_OUTOFMEMORY;
1717 goto error;
1719 memcpy( bytes, ptr, size );
1720 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1722 heap_free( bytes );
1723 ptr += size;
1724 continue;
1726 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1728 heap_free( bytes );
1729 clear_dict( dict );
1730 return hr;
1732 ptr += size;
1734 return S_OK;
1736 error:
1737 clear_dict( dict );
1738 return hr;
1741 static HRESULT send_preamble_ack( struct channel *channel )
1743 HRESULT hr;
1744 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1745 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1746 return S_OK;
1749 static HRESULT receive_message_session( struct channel *channel )
1751 HRESULT hr;
1753 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1754 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1756 ULONG size;
1757 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict_recv,
1758 &size )) != S_OK) return hr;
1759 channel->read_size -= size;
1760 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1763 return init_reader( channel );
1766 static HRESULT receive_message_sock( struct channel *channel, SOCKET socket )
1768 HRESULT hr;
1769 if ((hr = receive_message_unsized( channel, socket )) != S_OK) return hr;
1770 return init_reader( channel );
1773 static HRESULT receive_message_bytes( struct channel *channel )
1775 HRESULT hr;
1776 if ((hr = connect_channel( channel )) != S_OK) return hr;
1778 switch (channel->binding)
1780 case WS_HTTP_CHANNEL_BINDING:
1781 return receive_message_http( channel );
1783 case WS_TCP_CHANNEL_BINDING:
1784 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1786 switch (channel->session_state)
1788 case SESSION_STATE_UNINITIALIZED:
1789 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1790 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1791 /* fall through */
1793 case SESSION_STATE_SETUP_COMPLETE:
1794 return receive_message_session( channel );
1796 default:
1797 ERR( "unhandled session state %u\n", channel->session_state );
1798 return WS_E_OTHER;
1801 return receive_message_sock( channel, channel->u.tcp.socket );
1803 case WS_UDP_CHANNEL_BINDING:
1804 return receive_message_sock( channel, channel->u.udp.socket );
1806 default:
1807 ERR( "unhandled binding %u\n", channel->binding );
1808 return E_NOTIMPL;
1812 HRESULT channel_receive_message( WS_CHANNEL *handle )
1814 struct channel *channel = (struct channel *)handle;
1815 HRESULT hr;
1817 EnterCriticalSection( &channel->cs );
1819 if (channel->magic != CHANNEL_MAGIC)
1821 LeaveCriticalSection( &channel->cs );
1822 return E_INVALIDARG;
1825 hr = receive_message_bytes( channel );
1827 LeaveCriticalSection( &channel->cs );
1828 return hr;
1831 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1833 struct channel *channel = (struct channel *)handle;
1835 EnterCriticalSection( &channel->cs );
1837 if (channel->magic != CHANNEL_MAGIC)
1839 LeaveCriticalSection( &channel->cs );
1840 return E_INVALIDARG;
1843 *reader = channel->reader;
1845 LeaveCriticalSection( &channel->cs );
1846 return S_OK;
1849 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1850 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1852 HRESULT hr;
1853 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1854 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1855 return WsReadEnvelopeEnd( handle, NULL );
1858 static HRESULT receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1859 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1860 void *value, ULONG size, ULONG *index )
1862 HRESULT hr;
1863 ULONG i;
1865 if ((hr = receive_message_bytes( channel )) != S_OK) return hr;
1866 for (i = 0; i < count; i++)
1868 const WS_ELEMENT_DESCRIPTION *body = desc[i]->bodyElementDescription;
1869 if ((hr = read_message( msg, channel->reader, body, read_option, heap, value, size )) == S_OK)
1871 if (index) *index = i;
1872 break;
1874 if ((hr = WsResetMessage( msg, NULL )) != S_OK) return hr;
1875 if ((hr = init_reader( channel )) != S_OK) return hr;
1877 return (i == count) ? WS_E_INVALID_FORMAT : S_OK;
1880 struct receive_message
1882 struct task task;
1883 struct channel *channel;
1884 WS_MESSAGE *msg;
1885 const WS_MESSAGE_DESCRIPTION **desc;
1886 ULONG count;
1887 WS_RECEIVE_OPTION option;
1888 WS_READ_OPTION read_option;
1889 WS_HEAP *heap;
1890 void *value;
1891 ULONG size;
1892 ULONG *index;
1893 WS_ASYNC_CONTEXT ctx;
1896 static void receive_message_proc( struct task *task )
1898 struct receive_message *r = (struct receive_message *)task;
1899 HRESULT hr;
1901 hr = receive_message( r->channel, r->msg, r->desc, r->count, r->option, r->read_option, r->heap, r->value,
1902 r->size, r->index );
1904 TRACE( "calling %p(%08x)\n", r->ctx.callback, hr );
1905 r->ctx.callback( hr, WS_LONG_CALLBACK, r->ctx.callbackState );
1906 TRACE( "%p returned\n", r->ctx.callback );
1909 static HRESULT queue_receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1910 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option,
1911 WS_HEAP *heap, void *value, ULONG size, ULONG *index,
1912 const WS_ASYNC_CONTEXT *ctx )
1914 struct receive_message *r;
1916 if (!(r = heap_alloc( sizeof(*r) ))) return E_OUTOFMEMORY;
1917 r->task.proc = receive_message_proc;
1918 r->channel = channel;
1919 r->msg = msg;
1920 r->desc = desc;
1921 r->count = count;
1922 r->option = option;
1923 r->read_option = read_option;
1924 r->heap = heap;
1925 r->value = value;
1926 r->size = size;
1927 r->index = index;
1928 r->ctx = *ctx;
1929 return queue_task( &channel->recv_q, &r->task );
1932 /**************************************************************************
1933 * WsReceiveMessage [webservices.@]
1935 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1936 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1937 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1939 struct channel *channel = (struct channel *)handle;
1940 HRESULT hr;
1942 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1943 value, size, index, ctx, error );
1944 if (error) FIXME( "ignoring error parameter\n" );
1946 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1948 EnterCriticalSection( &channel->cs );
1950 if (channel->magic != CHANNEL_MAGIC)
1952 LeaveCriticalSection( &channel->cs );
1953 return E_INVALIDARG;
1956 if (ctx)
1957 hr = queue_receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index, ctx );
1958 else
1959 hr = receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index );
1961 LeaveCriticalSection( &channel->cs );
1962 return hr;
1965 /**************************************************************************
1966 * WsReadMessageStart [webservices.@]
1968 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1969 WS_ERROR *error )
1971 struct channel *channel = (struct channel *)handle;
1972 HRESULT hr;
1974 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1975 if (error) FIXME( "ignoring error parameter\n" );
1976 if (ctx) FIXME( "ignoring ctx parameter\n" );
1978 if (!channel || !msg) return E_INVALIDARG;
1980 EnterCriticalSection( &channel->cs );
1982 if (channel->magic != CHANNEL_MAGIC)
1984 LeaveCriticalSection( &channel->cs );
1985 return E_INVALIDARG;
1988 if ((hr = receive_message_bytes( channel )) == S_OK)
1990 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
1993 LeaveCriticalSection( &channel->cs );
1994 return hr;
1997 /**************************************************************************
1998 * WsReadMessageEnd [webservices.@]
2000 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2001 WS_ERROR *error )
2003 struct channel *channel = (struct channel *)handle;
2004 HRESULT hr;
2006 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2007 if (error) FIXME( "ignoring error parameter\n" );
2008 if (ctx) FIXME( "ignoring ctx parameter\n" );
2010 if (!channel || !msg) return E_INVALIDARG;
2012 EnterCriticalSection( &channel->cs );
2014 if (channel->magic != CHANNEL_MAGIC)
2016 LeaveCriticalSection( &channel->cs );
2017 return E_INVALIDARG;
2020 hr = WsReadEnvelopeEnd( msg, NULL );
2022 LeaveCriticalSection( &channel->cs );
2023 return hr;
2026 /**************************************************************************
2027 * WsWriteMessageStart [webservices.@]
2029 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2030 WS_ERROR *error )
2032 struct channel *channel = (struct channel *)handle;
2033 HRESULT hr;
2035 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2036 if (error) FIXME( "ignoring error parameter\n" );
2037 if (ctx) FIXME( "ignoring ctx parameter\n" );
2039 if (!channel || !msg) return E_INVALIDARG;
2041 EnterCriticalSection( &channel->cs );
2043 if (channel->magic != CHANNEL_MAGIC)
2045 LeaveCriticalSection( &channel->cs );
2046 return E_INVALIDARG;
2049 if ((hr = init_writer( channel )) != S_OK) goto done;
2050 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
2051 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
2053 done:
2054 LeaveCriticalSection( &channel->cs );
2055 return hr;
2058 /**************************************************************************
2059 * WsWriteMessageEnd [webservices.@]
2061 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2062 WS_ERROR *error )
2064 struct channel *channel = (struct channel *)handle;
2065 HRESULT hr;
2067 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2068 if (error) FIXME( "ignoring error parameter\n" );
2069 if (ctx) FIXME( "ignoring ctx parameter\n" );
2071 if (!channel || !msg) return E_INVALIDARG;
2073 EnterCriticalSection( &channel->cs );
2075 if (channel->magic != CHANNEL_MAGIC)
2077 LeaveCriticalSection( &channel->cs );
2078 return E_INVALIDARG;
2081 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK) hr = send_message( channel, msg );
2083 LeaveCriticalSection( &channel->cs );
2084 return hr;
2087 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
2089 HANDLE handles[] = { wait, cancel };
2090 ULONG nonblocking = 0;
2091 HRESULT hr = S_OK;
2093 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2095 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2097 case 0:
2098 if ((*ret = accept( socket, NULL, NULL )) != -1)
2100 WSAEventSelect( *ret, NULL, 0 );
2101 ioctlsocket( *ret, FIONBIO, &nonblocking );
2102 break;
2104 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2105 break;
2107 case 1:
2108 hr = WS_E_OPERATION_ABORTED;
2109 break;
2111 default:
2112 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2113 break;
2116 return hr;
2119 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2121 struct channel *channel = (struct channel *)handle;
2122 HRESULT hr;
2124 EnterCriticalSection( &channel->cs );
2126 if (channel->magic != CHANNEL_MAGIC)
2128 LeaveCriticalSection( &channel->cs );
2129 return E_INVALIDARG;
2132 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
2134 LeaveCriticalSection( &channel->cs );
2135 return hr;
2138 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
2140 HANDLE handles[] = { wait, cancel };
2141 ULONG nonblocking = 0;
2142 HRESULT hr;
2144 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2146 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2148 case 0:
2149 hr = S_OK;
2150 break;
2152 case 1:
2153 hr = WS_E_OPERATION_ABORTED;
2154 break;
2156 default:
2157 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2158 break;
2161 WSAEventSelect( socket, NULL, 0 );
2162 ioctlsocket( socket, FIONBIO, &nonblocking );
2163 return hr;
2166 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2168 struct channel *channel = (struct channel *)handle;
2169 HRESULT hr;
2171 EnterCriticalSection( &channel->cs );
2173 if (channel->magic != CHANNEL_MAGIC)
2175 LeaveCriticalSection( &channel->cs );
2176 return E_INVALIDARG;
2179 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
2181 LeaveCriticalSection( &channel->cs );
2182 return hr;
2185 /**************************************************************************
2186 * WsAbortChannel [webservices.@]
2188 HRESULT WINAPI WsAbortChannel( WS_CHANNEL *handle, WS_ERROR *error )
2190 FIXME( "%p %p: stub!\n", handle, error );
2191 return E_NOTIMPL;