webservices: Add traces.
[wine.git] / dlls / webservices / channel.c
blobb995a86c9230707ede85018bdca838476eb36673
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[ARRAY_SIZE( channel_props )];
242 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
244 static struct channel *alloc_channel(void)
246 static const ULONG count = ARRAY_SIZE( channel_props );
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 InitializeCriticalSection( &ret->send_q.cs );
255 InitializeCriticalSection( &ret->recv_q.cs );
256 #ifndef __MINGW32__
257 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.cs");
258 ret->send_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.send_q.cs");
259 ret->recv_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.recv_q.cs");
260 #endif
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 #ifndef __MINGW32__
346 channel->send_q.cs.DebugInfo->Spare[0] = 0;
347 channel->recv_q.cs.DebugInfo->Spare[0] = 0;
348 channel->cs.DebugInfo->Spare[0] = 0;
349 #endif
350 DeleteCriticalSection( &channel->send_q.cs );
351 DeleteCriticalSection( &channel->recv_q.cs );
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 TRACE( "property id %u value ptr %p size %u\n", properties[i].id, properties[i].value,
399 properties[i].valueSize );
400 if (properties[i].valueSize == sizeof(ULONG) && properties[i].value)
401 TRACE( " value %08x\n", *(ULONG *)properties[i].value );
403 switch (properties[i].id)
405 case WS_CHANNEL_PROPERTY_ENCODING:
406 if (!properties[i].value || properties[i].valueSize != sizeof(channel->encoding))
408 free_channel( channel );
409 return E_INVALIDARG;
411 channel->encoding = *(WS_ENCODING *)properties[i].value;
412 break;
414 default:
415 if ((hr = prop_set( channel->prop, channel->prop_count, properties[i].id, properties[i].value,
416 properties[i].valueSize )) != S_OK)
418 free_channel( channel );
419 return hr;
421 break;
425 *ret = channel;
426 return S_OK;
429 /**************************************************************************
430 * WsCreateChannel [webservices.@]
432 HRESULT WINAPI WsCreateChannel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
433 const WS_CHANNEL_PROPERTY *properties, ULONG count,
434 const WS_SECURITY_DESCRIPTION *desc, WS_CHANNEL **handle,
435 WS_ERROR *error )
437 struct channel *channel;
438 HRESULT hr;
440 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
441 if (error) FIXME( "ignoring error parameter\n" );
442 if (desc) FIXME( "ignoring security description\n" );
444 if (!handle) return E_INVALIDARG;
446 if (type != WS_CHANNEL_TYPE_REQUEST && type != WS_CHANNEL_TYPE_DUPLEX &&
447 type != WS_CHANNEL_TYPE_DUPLEX_SESSION)
449 FIXME( "channel type %u not implemented\n", type );
450 return E_NOTIMPL;
452 if (binding != WS_HTTP_CHANNEL_BINDING && binding != WS_TCP_CHANNEL_BINDING &&
453 binding != WS_UDP_CHANNEL_BINDING)
455 FIXME( "channel binding %u not implemented\n", binding );
456 return E_NOTIMPL;
459 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
461 TRACE( "created %p\n", channel );
462 *handle = (WS_CHANNEL *)channel;
463 return S_OK;
466 /**************************************************************************
467 * WsCreateChannelForListener [webservices.@]
469 HRESULT WINAPI WsCreateChannelForListener( WS_LISTENER *listener_handle, const WS_CHANNEL_PROPERTY *properties,
470 ULONG count, WS_CHANNEL **handle, WS_ERROR *error )
472 struct channel *channel;
473 WS_CHANNEL_TYPE type;
474 WS_CHANNEL_BINDING binding;
475 HRESULT hr;
477 TRACE( "%p %p %u %p %p\n", listener_handle, properties, count, handle, error );
478 if (error) FIXME( "ignoring error parameter\n" );
480 if (!listener_handle || !handle) return E_INVALIDARG;
482 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_TYPE, &type,
483 sizeof(type), NULL )) != S_OK) return hr;
485 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_BINDING, &binding,
486 sizeof(binding), NULL )) != S_OK) return hr;
488 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
490 TRACE( "created %p\n", channel );
491 *handle = (WS_CHANNEL *)channel;
492 return S_OK;
495 /**************************************************************************
496 * WsFreeChannel [webservices.@]
498 void WINAPI WsFreeChannel( WS_CHANNEL *handle )
500 struct channel *channel = (struct channel *)handle;
502 TRACE( "%p\n", handle );
504 if (!channel) return;
506 EnterCriticalSection( &channel->cs );
508 if (channel->magic != CHANNEL_MAGIC)
510 LeaveCriticalSection( &channel->cs );
511 return;
514 channel->magic = 0;
516 LeaveCriticalSection( &channel->cs );
517 free_channel( channel );
520 /**************************************************************************
521 * WsResetChannel [webservices.@]
523 HRESULT WINAPI WsResetChannel( WS_CHANNEL *handle, WS_ERROR *error )
525 struct channel *channel = (struct channel *)handle;
526 HRESULT hr = S_OK;
528 TRACE( "%p %p\n", handle, error );
529 if (error) FIXME( "ignoring error parameter\n" );
531 if (!channel) return E_INVALIDARG;
533 EnterCriticalSection( &channel->cs );
535 if (channel->magic != CHANNEL_MAGIC)
537 LeaveCriticalSection( &channel->cs );
538 return E_INVALIDARG;
541 if (channel->state != WS_CHANNEL_STATE_CREATED && channel->state != WS_CHANNEL_STATE_CLOSED)
542 hr = WS_E_INVALID_OPERATION;
543 else
544 reset_channel( channel );
546 LeaveCriticalSection( &channel->cs );
547 TRACE( "returning %08x\n", hr );
548 return hr;
551 /**************************************************************************
552 * WsGetChannelProperty [webservices.@]
554 HRESULT WINAPI WsGetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, void *buf,
555 ULONG size, WS_ERROR *error )
557 struct channel *channel = (struct channel *)handle;
558 HRESULT hr = S_OK;
560 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
561 if (error) FIXME( "ignoring error parameter\n" );
563 if (!channel) return E_INVALIDARG;
565 EnterCriticalSection( &channel->cs );
567 if (channel->magic != CHANNEL_MAGIC)
569 LeaveCriticalSection( &channel->cs );
570 return E_INVALIDARG;
573 switch (id)
575 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE:
576 if (!buf || size != sizeof(channel->type)) hr = E_INVALIDARG;
577 else *(WS_CHANNEL_TYPE *)buf = channel->type;
578 break;
580 case WS_CHANNEL_PROPERTY_ENCODING:
581 if (!buf || size != sizeof(channel->encoding)) hr = E_INVALIDARG;
582 else *(WS_ENCODING *)buf = channel->encoding;
583 break;
585 default:
586 hr = prop_get( channel->prop, channel->prop_count, id, buf, size );
589 LeaveCriticalSection( &channel->cs );
590 TRACE( "returning %08x\n", hr );
591 return hr;
594 /**************************************************************************
595 * WsSetChannelProperty [webservices.@]
597 HRESULT WINAPI WsSetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, const void *value,
598 ULONG size, WS_ERROR *error )
600 struct channel *channel = (struct channel *)handle;
601 HRESULT hr;
603 TRACE( "%p %u %p %u\n", handle, id, value, size );
604 if (error) FIXME( "ignoring error parameter\n" );
606 if (!channel) return E_INVALIDARG;
608 EnterCriticalSection( &channel->cs );
610 if (channel->magic != CHANNEL_MAGIC)
612 LeaveCriticalSection( &channel->cs );
613 return E_INVALIDARG;
616 hr = prop_set( channel->prop, channel->prop_count, id, value, size );
618 LeaveCriticalSection( &channel->cs );
619 TRACE( "returning %08x\n", hr );
620 return hr;
623 static HRESULT open_channel( struct channel *channel, const WS_ENDPOINT_ADDRESS *endpoint )
625 if (endpoint->headers || endpoint->extensions || endpoint->identity)
627 FIXME( "headers, extensions or identity not supported\n" );
628 return E_NOTIMPL;
631 TRACE( "endpoint %s\n", debugstr_wn(endpoint->url.chars, endpoint->url.length) );
633 if (!(channel->addr.url.chars = heap_alloc( endpoint->url.length * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
634 memcpy( channel->addr.url.chars, endpoint->url.chars, endpoint->url.length * sizeof(WCHAR) );
635 channel->addr.url.length = endpoint->url.length;
637 channel->state = WS_CHANNEL_STATE_OPEN;
638 return S_OK;
641 /**************************************************************************
642 * WsOpenChannel [webservices.@]
644 HRESULT WINAPI WsOpenChannel( WS_CHANNEL *handle, const WS_ENDPOINT_ADDRESS *endpoint,
645 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
647 struct channel *channel = (struct channel *)handle;
648 HRESULT hr;
650 TRACE( "%p %p %p %p\n", handle, endpoint, ctx, error );
651 if (error) FIXME( "ignoring error parameter\n" );
652 if (ctx) FIXME( "ignoring ctx parameter\n" );
654 if (!channel || !endpoint) return E_INVALIDARG;
656 EnterCriticalSection( &channel->cs );
658 if (channel->magic != CHANNEL_MAGIC)
660 LeaveCriticalSection( &channel->cs );
661 return E_INVALIDARG;
664 if (channel->state != WS_CHANNEL_STATE_CREATED) hr = WS_E_INVALID_OPERATION;
665 else hr = open_channel( channel, endpoint );
667 LeaveCriticalSection( &channel->cs );
668 TRACE( "returning %08x\n", hr );
669 return hr;
672 enum frame_record_type
674 FRAME_RECORD_TYPE_VERSION,
675 FRAME_RECORD_TYPE_MODE,
676 FRAME_RECORD_TYPE_VIA,
677 FRAME_RECORD_TYPE_KNOWN_ENCODING,
678 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING,
679 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE,
680 FRAME_RECORD_TYPE_SIZED_ENVELOPE,
681 FRAME_RECORD_TYPE_END,
682 FRAME_RECORD_TYPE_FAULT,
683 FRAME_RECORD_TYPE_UPGRADE_REQUEST,
684 FRAME_RECORD_TYPE_UPGRADE_RESPONSE,
685 FRAME_RECORD_TYPE_PREAMBLE_ACK,
686 FRAME_RECORD_TYPE_PREAMBLE_END,
689 static HRESULT send_byte( SOCKET socket, BYTE byte )
691 int count = send( socket, (char *)&byte, 1, 0 );
692 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
693 if (count != 1) return WS_E_OTHER;
694 return S_OK;
697 static HRESULT shutdown_session( struct channel *channel )
699 HRESULT hr;
701 if (channel->state != WS_CHANNEL_STATE_OPEN ||
702 (channel->type != WS_CHANNEL_TYPE_OUTPUT_SESSION &&
703 channel->type != WS_CHANNEL_TYPE_DUPLEX_SESSION) ||
704 channel->session_state >= SESSION_STATE_SHUTDOWN) return WS_E_INVALID_OPERATION;
706 switch (channel->binding)
708 case WS_TCP_CHANNEL_BINDING:
709 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_END )) != S_OK) return hr;
710 channel->session_state = SESSION_STATE_SHUTDOWN;
711 return S_OK;
713 default:
714 FIXME( "unhandled binding %u\n", channel->binding );
715 return E_NOTIMPL;
719 HRESULT WINAPI WsShutdownSessionChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
721 struct channel *channel = (struct channel *)handle;
722 HRESULT hr;
724 TRACE( "%p %p %p\n", handle, ctx, error );
725 if (error) FIXME( "ignoring error parameter\n" );
726 if (ctx) FIXME( "ignoring ctx parameter\n" );
728 if (!channel) return E_INVALIDARG;
730 EnterCriticalSection( &channel->cs );
732 if (channel->magic != CHANNEL_MAGIC)
734 LeaveCriticalSection( &channel->cs );
735 return E_INVALIDARG;
738 hr = shutdown_session( channel );
740 LeaveCriticalSection( &channel->cs );
741 TRACE( "returning %08x\n", hr );
742 return hr;
745 static void close_channel( struct channel *channel )
747 reset_channel( channel );
748 channel->state = WS_CHANNEL_STATE_CLOSED;
751 /**************************************************************************
752 * WsCloseChannel [webservices.@]
754 HRESULT WINAPI WsCloseChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
756 struct channel *channel = (struct channel *)handle;
757 HRESULT hr = S_OK;
759 TRACE( "%p %p %p\n", handle, ctx, error );
760 if (error) FIXME( "ignoring error parameter\n" );
761 if (ctx) FIXME( "ignoring ctx parameter\n" );
763 if (!channel) return E_INVALIDARG;
765 EnterCriticalSection( &channel->cs );
767 if (channel->magic != CHANNEL_MAGIC)
769 LeaveCriticalSection( &channel->cs );
770 return E_INVALIDARG;
773 close_channel( channel );
775 LeaveCriticalSection( &channel->cs );
776 TRACE( "returning %08x\n", hr );
777 return hr;
780 static HRESULT parse_http_url( const WCHAR *url, ULONG len, URL_COMPONENTS *uc )
782 HRESULT hr = E_OUTOFMEMORY;
783 WCHAR *tmp;
784 DWORD err;
786 memset( uc, 0, sizeof(*uc) );
787 uc->dwStructSize = sizeof(*uc);
788 uc->dwHostNameLength = 128;
789 uc->lpszHostName = heap_alloc( uc->dwHostNameLength * sizeof(WCHAR) );
790 uc->dwUrlPathLength = 128;
791 uc->lpszUrlPath = heap_alloc( uc->dwUrlPathLength * sizeof(WCHAR) );
792 uc->dwExtraInfoLength = 128;
793 uc->lpszExtraInfo = heap_alloc( uc->dwExtraInfoLength * sizeof(WCHAR) );
794 if (!uc->lpszHostName || !uc->lpszUrlPath || !uc->lpszExtraInfo) goto error;
796 if (!WinHttpCrackUrl( url, len, ICU_DECODE, uc ))
798 if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
800 hr = HRESULT_FROM_WIN32( err );
801 goto error;
803 if (!(tmp = heap_realloc( uc->lpszHostName, uc->dwHostNameLength * sizeof(WCHAR) ))) goto error;
804 uc->lpszHostName = tmp;
805 if (!(tmp = heap_realloc( uc->lpszUrlPath, uc->dwUrlPathLength * sizeof(WCHAR) ))) goto error;
806 uc->lpszUrlPath = tmp;
807 if (!(tmp = heap_realloc( uc->lpszExtraInfo, uc->dwExtraInfoLength * sizeof(WCHAR) ))) goto error;
808 uc->lpszExtraInfo = tmp;
809 WinHttpCrackUrl( url, len, ICU_DECODE, uc );
812 return S_OK;
814 error:
815 heap_free( uc->lpszHostName );
816 heap_free( uc->lpszUrlPath );
817 heap_free( uc->lpszExtraInfo );
818 return hr;
821 static HRESULT connect_channel_http( struct channel *channel )
823 static const WCHAR agentW[] =
824 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
825 HINTERNET ses = NULL, con = NULL;
826 URL_COMPONENTS uc;
827 HRESULT hr;
829 if (channel->u.http.connect) return S_OK;
831 if ((hr = parse_http_url( channel->addr.url.chars, channel->addr.url.length, &uc )) != S_OK) return hr;
832 if (!(channel->u.http.path = heap_alloc( (uc.dwUrlPathLength + uc.dwExtraInfoLength + 1) * sizeof(WCHAR) )))
834 hr = E_OUTOFMEMORY;
835 goto done;
837 else
839 strcpyW( channel->u.http.path, uc.lpszUrlPath );
840 if (uc.dwExtraInfoLength) strcatW( channel->u.http.path, uc.lpszExtraInfo );
843 channel->u.http.flags = WINHTTP_FLAG_REFRESH;
844 switch (uc.nScheme)
846 case INTERNET_SCHEME_HTTP: break;
847 case INTERNET_SCHEME_HTTPS:
848 channel->u.http.flags |= WINHTTP_FLAG_SECURE;
849 break;
851 default:
852 hr = WS_E_INVALID_ENDPOINT_URL;
853 goto done;
856 if (!(ses = WinHttpOpen( agentW, 0, NULL, NULL, 0 )))
858 hr = HRESULT_FROM_WIN32( GetLastError() );
859 goto done;
861 if (!(con = WinHttpConnect( ses, uc.lpszHostName, uc.nPort, 0 )))
863 hr = HRESULT_FROM_WIN32( GetLastError() );
864 goto done;
867 channel->u.http.session = ses;
868 channel->u.http.connect = con;
870 done:
871 if (hr != S_OK)
873 WinHttpCloseHandle( con );
874 WinHttpCloseHandle( ses );
876 heap_free( uc.lpszHostName );
877 heap_free( uc.lpszUrlPath );
878 heap_free( uc.lpszExtraInfo );
879 return hr;
882 static HRESULT connect_channel_tcp( struct channel *channel )
884 struct sockaddr_storage storage;
885 struct sockaddr *addr = (struct sockaddr *)&storage;
886 int addr_len;
887 WS_URL_SCHEME_TYPE scheme;
888 WCHAR *host;
889 USHORT port;
890 HRESULT hr;
892 if (channel->u.tcp.socket != -1) return S_OK;
894 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
895 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
897 heap_free( host );
898 return WS_E_INVALID_ENDPOINT_URL;
901 winsock_init();
903 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
904 heap_free( host );
905 if (hr != S_OK) return hr;
907 if ((channel->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
908 return HRESULT_FROM_WIN32( WSAGetLastError() );
910 if (connect( channel->u.tcp.socket, addr, addr_len ) < 0)
912 closesocket( channel->u.tcp.socket );
913 channel->u.tcp.socket = -1;
914 return HRESULT_FROM_WIN32( WSAGetLastError() );
917 return S_OK;
920 static HRESULT connect_channel_udp( struct channel *channel )
922 struct sockaddr_storage storage;
923 struct sockaddr *addr = (struct sockaddr *)&storage;
924 int addr_len;
925 WS_URL_SCHEME_TYPE scheme;
926 WCHAR *host;
927 USHORT port;
928 HRESULT hr;
930 if (channel->u.udp.socket != -1) return S_OK;
932 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
933 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
935 heap_free( host );
936 return WS_E_INVALID_ENDPOINT_URL;
939 winsock_init();
941 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
942 heap_free( host );
943 if (hr != S_OK) return hr;
945 if ((channel->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
946 return HRESULT_FROM_WIN32( WSAGetLastError() );
948 if (connect( channel->u.udp.socket, addr, addr_len ) < 0)
950 closesocket( channel->u.udp.socket );
951 channel->u.udp.socket = -1;
952 return HRESULT_FROM_WIN32( WSAGetLastError() );
955 return S_OK;
958 static HRESULT connect_channel( struct channel *channel )
960 switch (channel->binding)
962 case WS_HTTP_CHANNEL_BINDING:
963 return connect_channel_http( channel );
965 case WS_TCP_CHANNEL_BINDING:
966 return connect_channel_tcp( channel );
968 case WS_UDP_CHANNEL_BINDING:
969 return connect_channel_udp( channel );
971 default:
972 ERR( "unhandled binding %u\n", channel->binding );
973 return E_NOTIMPL;
977 static HRESULT send_message_http( HINTERNET request, BYTE *data, ULONG len )
979 if (!WinHttpSendRequest( request, NULL, 0, data, len, len, 0 ))
980 return HRESULT_FROM_WIN32( GetLastError() );
982 if (!WinHttpReceiveResponse( request, NULL ))
983 return HRESULT_FROM_WIN32( GetLastError() );
984 return S_OK;
987 static HRESULT send_bytes( SOCKET socket, BYTE *bytes, int len )
989 int count = send( socket, (char *)bytes, len, 0 );
990 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
991 if (count != len) return WS_E_OTHER;
992 return S_OK;
995 static HRESULT send_size( SOCKET socket, ULONG size )
997 HRESULT hr;
998 if (size < 0x80) return send_byte( socket, size );
999 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
1000 if ((size >>= 7) < 0x80) return send_byte( socket, size );
1001 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
1002 if ((size >>= 7) < 0x80) return send_byte( socket, size );
1003 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
1004 if ((size >>= 7) < 0x80) return send_byte( socket, size );
1005 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
1006 if ((size >>= 7) < 0x08) return send_byte( socket, size );
1007 return E_INVALIDARG;
1010 static inline ULONG size_length( ULONG size )
1012 if (size < 0x80) return 1;
1013 if (size < 0x4000) return 2;
1014 if (size < 0x200000) return 3;
1015 if (size < 0x10000000) return 4;
1016 return 5;
1019 static ULONG string_table_size( const struct dictionary *dict )
1021 ULONG i, size = 0;
1022 for (i = 0; i < dict->dict.stringCount; i++)
1024 if (dict->sequence[i] == dict->current_sequence)
1025 size += size_length( dict->dict.strings[i].length ) + dict->dict.strings[i].length;
1027 return size;
1030 static HRESULT send_string_table( SOCKET socket, const struct dictionary *dict )
1032 ULONG i;
1033 HRESULT hr;
1034 for (i = 0; i < dict->dict.stringCount; i++)
1036 if (dict->sequence[i] != dict->current_sequence) continue;
1037 if ((hr = send_size( socket, dict->dict.strings[i].length )) != S_OK) return hr;
1038 if ((hr = send_bytes( socket, dict->dict.strings[i].bytes, dict->dict.strings[i].length )) != S_OK) return hr;
1040 return S_OK;
1043 static HRESULT string_to_utf8( const WS_STRING *str, unsigned char **ret, int *len )
1045 *len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
1046 if (!(*ret = heap_alloc( *len ))) return E_OUTOFMEMORY;
1047 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)*ret, *len, NULL, NULL );
1048 return S_OK;
1051 enum session_mode
1053 SESSION_MODE_INVALID = 0,
1054 SESSION_MODE_SINGLETON = 1,
1055 SESSION_MODE_DUPLEX = 2,
1056 SESSION_MODE_SIMPLEX = 3,
1059 static enum session_mode map_channel_type( struct channel *channel )
1061 switch (channel->type)
1063 case WS_CHANNEL_TYPE_DUPLEX_SESSION: return SESSION_MODE_DUPLEX;
1064 default:
1065 FIXME( "unhandled channel type %08x\n", channel->type );
1066 return SESSION_MODE_INVALID;
1070 enum known_encoding
1072 KNOWN_ENCODING_SOAP11_UTF8 = 0x00,
1073 KNOWN_ENCODING_SOAP11_UTF16 = 0x01,
1074 KNOWN_ENCODING_SOAP11_UTF16LE = 0x02,
1075 KNOWN_ENCODING_SOAP12_UTF8 = 0x03,
1076 KNOWN_ENCODING_SOAP12_UTF16 = 0x04,
1077 KNOWN_ENCODING_SOAP12_UTF16LE = 0x05,
1078 KNOWN_ENCODING_SOAP12_MTOM = 0x06,
1079 KNOWN_ENCODING_SOAP12_BINARY = 0x07,
1080 KNOWN_ENCODING_SOAP12_BINARY_SESSION = 0x08,
1083 static enum known_encoding map_channel_encoding( struct channel *channel )
1085 WS_ENVELOPE_VERSION version;
1087 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version, sizeof(version) );
1089 switch (version)
1091 case WS_ENVELOPE_VERSION_SOAP_1_1:
1092 switch (channel->encoding)
1094 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP11_UTF8;
1095 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP11_UTF16LE;
1096 default:
1097 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1098 return 0;
1100 case WS_ENVELOPE_VERSION_SOAP_1_2:
1101 switch (channel->encoding)
1103 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP12_UTF8;
1104 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP12_UTF16LE;
1105 case WS_ENCODING_XML_BINARY_1: return KNOWN_ENCODING_SOAP12_BINARY;
1106 case WS_ENCODING_XML_BINARY_SESSION_1: return KNOWN_ENCODING_SOAP12_BINARY_SESSION;
1107 default:
1108 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1109 return 0;
1111 default:
1112 ERR( "unhandled version %u\n", version );
1113 return 0;
1117 #define FRAME_VERSION_MAJOR 1
1118 #define FRAME_VERSION_MINOR 1
1120 static HRESULT send_preamble( struct channel *channel )
1122 unsigned char *url;
1123 HRESULT hr;
1124 int len;
1126 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VERSION )) != S_OK) return hr;
1127 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MAJOR )) != S_OK) return hr;
1128 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MINOR )) != S_OK) return hr;
1130 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_MODE )) != S_OK) return hr;
1131 if ((hr = send_byte( channel->u.tcp.socket, map_channel_type(channel) )) != S_OK) return hr;
1133 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VIA )) != S_OK) return hr;
1134 if ((hr = string_to_utf8( &channel->addr.url, &url, &len )) != S_OK) return hr;
1135 if ((hr = send_size( channel->u.tcp.socket, len )) != S_OK) goto done;
1136 if ((hr = send_bytes( channel->u.tcp.socket, url, len )) != S_OK) goto done;
1138 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_KNOWN_ENCODING )) != S_OK) goto done;
1139 if ((hr = send_byte( channel->u.tcp.socket, map_channel_encoding(channel) )) != S_OK) goto done;
1140 hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_END );
1142 done:
1143 heap_free( url );
1144 return hr;
1147 static void set_blocking( SOCKET socket, BOOL blocking )
1149 ULONG state = !blocking;
1150 ioctlsocket( socket, FIONBIO, &state );
1153 static int sock_peek( SOCKET socket )
1155 int ret;
1156 char byte;
1158 set_blocking( socket, FALSE );
1159 ret = recv( socket, &byte, 1, MSG_PEEK );
1160 set_blocking( socket, TRUE );
1161 return ret;
1164 static int sock_recv( SOCKET socket, char *buf, int len )
1166 int count, ret = 0;
1167 for (;;)
1169 if ((count = recv( socket, buf + ret, len, 0 )) <= 0) break;
1170 ret += count;
1171 len -= count;
1172 if (sock_peek( socket ) != 1) break;
1174 return ret;
1177 static HRESULT receive_bytes( struct channel *channel, unsigned char *bytes, int len )
1179 int count = sock_recv( channel->u.tcp.socket, (char *)bytes, len );
1180 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1181 if (count != len) return WS_E_INVALID_FORMAT;
1182 return S_OK;
1185 static HRESULT receive_preamble_ack( struct channel *channel )
1187 unsigned char byte;
1188 HRESULT hr;
1190 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1191 if (byte != FRAME_RECORD_TYPE_PREAMBLE_ACK) return WS_E_INVALID_FORMAT;
1192 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1193 return S_OK;
1196 static HRESULT send_sized_envelope( struct channel *channel, BYTE *data, ULONG len )
1198 ULONG table_size = string_table_size( &channel->dict_send );
1199 HRESULT hr;
1201 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_SIZED_ENVELOPE )) != S_OK) return hr;
1202 if ((hr = send_size( channel->u.tcp.socket, size_length(table_size) + table_size + len )) != S_OK) return hr;
1203 if ((hr = send_size( channel->u.tcp.socket, table_size )) != S_OK) return hr;
1204 if ((hr = send_string_table( channel->u.tcp.socket, &channel->dict_send )) != S_OK) return hr;
1205 return send_bytes( channel->u.tcp.socket, data, len );
1208 static HRESULT open_http_request( struct channel *channel, HINTERNET *req )
1210 static const WCHAR postW[] = {'P','O','S','T',0};
1211 if ((*req = WinHttpOpenRequest( channel->u.http.connect, postW, channel->u.http.path,
1212 NULL, NULL, NULL, channel->u.http.flags ))) return S_OK;
1213 return HRESULT_FROM_WIN32( GetLastError() );
1216 static HRESULT send_message( struct channel *channel, WS_MESSAGE *msg )
1218 WS_XML_WRITER *writer;
1219 WS_BYTES buf;
1220 HRESULT hr;
1222 channel->msg = msg;
1223 if ((hr = connect_channel( channel )) != S_OK) return hr;
1225 WsGetMessageProperty( channel->msg, WS_MESSAGE_PROPERTY_BODY_WRITER, &writer, sizeof(writer), NULL );
1226 WsGetWriterProperty( writer, WS_XML_WRITER_PROPERTY_BYTES, &buf, sizeof(buf), NULL );
1228 switch (channel->binding)
1230 case WS_HTTP_CHANNEL_BINDING:
1231 if (channel->u.http.request)
1233 WinHttpCloseHandle( channel->u.http.request );
1234 channel->u.http.request = NULL;
1236 if ((hr = open_http_request( channel, &channel->u.http.request )) != S_OK) return hr;
1237 if ((hr = message_insert_http_headers( msg, channel->u.http.request )) != S_OK) return hr;
1238 return send_message_http( channel->u.http.request, buf.bytes, buf.length );
1240 case WS_TCP_CHANNEL_BINDING:
1241 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1243 switch (channel->session_state)
1245 case SESSION_STATE_UNINITIALIZED:
1246 if ((hr = send_preamble( channel )) != S_OK) return hr;
1247 if ((hr = receive_preamble_ack( channel )) != S_OK) return hr;
1248 /* fall through */
1250 case SESSION_STATE_SETUP_COMPLETE:
1251 return send_sized_envelope( channel, buf.bytes, buf.length );
1253 default:
1254 ERR( "unhandled session state %u\n", channel->session_state );
1255 return WS_E_OTHER;
1258 return send_bytes( channel->u.tcp.socket, buf.bytes, buf.length );
1260 case WS_UDP_CHANNEL_BINDING:
1261 return send_bytes( channel->u.udp.socket, buf.bytes, buf.length );
1263 default:
1264 ERR( "unhandled binding %u\n", channel->binding );
1265 return E_NOTIMPL;
1269 HRESULT channel_send_message( WS_CHANNEL *handle, WS_MESSAGE *msg )
1271 struct channel *channel = (struct channel *)handle;
1272 HRESULT hr;
1274 EnterCriticalSection( &channel->cs );
1276 if (channel->magic != CHANNEL_MAGIC)
1278 LeaveCriticalSection( &channel->cs );
1279 return E_INVALIDARG;
1282 hr = send_message( channel, msg );
1284 LeaveCriticalSection( &channel->cs );
1285 return hr;
1288 static HRESULT CALLBACK dict_cb( void *state, const WS_XML_STRING *str, BOOL *found, ULONG *id, WS_ERROR *error )
1290 struct dictionary *dict = state;
1291 HRESULT hr = S_OK;
1292 BYTE *bytes;
1293 int index;
1295 if ((index = find_string( dict, str->bytes, str->length, id )) == -1)
1297 *found = TRUE;
1298 return S_OK;
1301 if (!(bytes = heap_alloc( str->length ))) return E_OUTOFMEMORY;
1302 memcpy( bytes, str->bytes, str->length );
1303 if ((hr = insert_string( dict, bytes, str->length, index, id )) == S_OK)
1305 *found = TRUE;
1306 return S_OK;
1308 heap_free( bytes );
1310 *found = FALSE;
1311 return hr;
1314 static HRESULT init_writer( struct channel *channel )
1316 WS_XML_WRITER_BUFFER_OUTPUT buf = {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER}};
1317 WS_XML_WRITER_TEXT_ENCODING text = {{WS_XML_WRITER_ENCODING_TYPE_TEXT}, WS_CHARSET_UTF8};
1318 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1319 HRESULT hr;
1321 if (!channel->writer && (hr = WsCreateWriter( NULL, 0, &channel->writer, NULL )) != S_OK) return hr;
1323 switch (channel->encoding)
1325 case WS_ENCODING_XML_UTF8:
1326 return WsSetOutput( channel->writer, &text.encoding, &buf.output, NULL, 0, NULL );
1328 case WS_ENCODING_XML_BINARY_SESSION_1:
1329 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1330 /* fall through */
1332 case WS_ENCODING_XML_BINARY_1:
1333 return WsSetOutput( channel->writer, &bin.encoding, &buf.output, NULL, 0, NULL );
1335 default:
1336 FIXME( "unhandled encoding %u\n", channel->encoding );
1337 return WS_E_NOT_SUPPORTED;
1341 static HRESULT write_message( struct channel *channel, WS_MESSAGE *msg, const WS_ELEMENT_DESCRIPTION *desc,
1342 WS_WRITE_OPTION option, const void *body, ULONG size )
1344 HRESULT hr;
1345 if ((hr = writer_set_lookup( channel->writer, TRUE )) != S_OK) return hr;
1346 if ((hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL )) != S_OK) return hr;
1347 if ((hr = writer_set_lookup( channel->writer, FALSE )) != S_OK) return hr;
1348 channel->dict_send.current_sequence++;
1349 if ((hr = writer_set_dict_callback( channel->writer, dict_cb, &channel->dict_send )) != S_OK) return hr;
1350 if ((hr = WsWriteBody( msg, desc, option, body, size, NULL )) != S_OK) return hr;
1351 return WsWriteEnvelopeEnd( msg, NULL );
1354 /**************************************************************************
1355 * WsSendMessage [webservices.@]
1357 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1358 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1359 WS_ERROR *error )
1361 struct channel *channel = (struct channel *)handle;
1362 HRESULT hr;
1364 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1365 if (error) FIXME( "ignoring error parameter\n" );
1366 if (ctx) FIXME( "ignoring ctx parameter\n" );
1368 if (!channel || !msg || !desc) return E_INVALIDARG;
1370 EnterCriticalSection( &channel->cs );
1372 if (channel->magic != CHANNEL_MAGIC)
1374 LeaveCriticalSection( &channel->cs );
1375 return E_INVALIDARG;
1378 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK) goto done;
1379 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1380 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1382 if ((hr = init_writer( channel )) != S_OK) goto done;
1383 if ((hr = write_message( channel, msg, desc->bodyElementDescription, option, body, size )) != S_OK) goto done;
1384 hr = send_message( channel, msg );
1386 done:
1387 LeaveCriticalSection( &channel->cs );
1388 TRACE( "returning %08x\n", hr );
1389 return hr;
1392 /**************************************************************************
1393 * WsSendReplyMessage [webservices.@]
1395 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1396 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1397 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1399 struct channel *channel = (struct channel *)handle;
1400 GUID req_id;
1401 HRESULT hr;
1403 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1404 if (error) FIXME( "ignoring error parameter\n" );
1405 if (ctx) FIXME( "ignoring ctx parameter\n" );
1407 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1409 EnterCriticalSection( &channel->cs );
1411 if (channel->magic != CHANNEL_MAGIC)
1413 LeaveCriticalSection( &channel->cs );
1414 return E_INVALIDARG;
1417 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1418 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1419 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1420 if ((hr = message_get_id( request, &req_id )) != S_OK) goto done;
1421 if ((hr = message_set_request_id( msg, &req_id )) != S_OK) goto done;
1423 if ((hr = init_writer( channel )) != S_OK) goto done;
1424 if ((hr = write_message( channel, msg, desc->bodyElementDescription, option, body, size )) != S_OK) goto done;
1425 hr = send_message( channel, msg );
1427 done:
1428 LeaveCriticalSection( &channel->cs );
1429 TRACE( "returning %08x\n", hr );
1430 return hr;
1433 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1435 if (!channel->read_buf)
1437 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1438 channel->read_buflen = size;
1439 return S_OK;
1441 if (channel->read_buflen < size)
1443 char *tmp;
1444 ULONG new_size = max( size, channel->read_buflen * 2 );
1445 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1446 channel->read_buf = tmp;
1447 channel->read_buflen = new_size;
1449 return S_OK;
1452 static HRESULT init_reader( struct channel *channel )
1454 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1455 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1456 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1457 WS_XML_READER_ENCODING *encoding;
1458 HRESULT hr;
1460 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1462 switch (channel->encoding)
1464 case WS_ENCODING_XML_UTF8:
1465 text.charSet = WS_CHARSET_UTF8;
1466 encoding = &text.encoding;
1467 break;
1469 case WS_ENCODING_XML_BINARY_SESSION_1:
1470 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1471 bin.dynamicDictionary = &channel->dict_recv.dict;
1472 /* fall through */
1474 case WS_ENCODING_XML_BINARY_1:
1475 encoding = &bin.encoding;
1476 break;
1478 default:
1479 FIXME( "unhandled encoding %u\n", channel->encoding );
1480 return WS_E_NOT_SUPPORTED;
1483 buf.encodedData = channel->read_buf;
1484 buf.encodedDataSize = channel->read_size;
1485 return WsSetInput( channel->reader, encoding, &buf.input, NULL, 0, NULL );
1488 #define INITIAL_READ_BUFFER_SIZE 4096
1489 static HRESULT receive_message_http( struct channel *channel )
1491 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1492 ULONG max_len;
1493 HRESULT hr;
1495 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1496 &max_len, sizeof(max_len) );
1498 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1499 channel->read_size = 0;
1500 for (;;)
1502 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1504 return HRESULT_FROM_WIN32( GetLastError() );
1506 if (!len) break;
1507 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1508 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1510 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1512 return HRESULT_FROM_WIN32( GetLastError() );
1514 if (!bytes_read) break;
1515 channel->read_size += bytes_read;
1516 offset += bytes_read;
1519 return init_reader( channel );
1522 static HRESULT receive_message_unsized( struct channel *channel, SOCKET socket )
1524 int bytes_read;
1525 ULONG max_len;
1526 HRESULT hr;
1528 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1529 &max_len, sizeof(max_len) );
1531 if ((hr = resize_read_buffer( channel, max_len )) != S_OK) return hr;
1533 channel->read_size = 0;
1534 if ((bytes_read = sock_recv( socket, channel->read_buf, max_len )) < 0)
1536 return HRESULT_FROM_WIN32( WSAGetLastError() );
1538 channel->read_size = bytes_read;
1539 return S_OK;
1542 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1544 unsigned int offset = 0, to_read = size;
1545 int bytes_read;
1546 HRESULT hr;
1548 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1550 channel->read_size = 0;
1551 while (channel->read_size < size)
1553 if ((bytes_read = sock_recv( channel->u.tcp.socket, channel->read_buf + offset, to_read )) < 0)
1555 return HRESULT_FROM_WIN32( WSAGetLastError() );
1557 if (!bytes_read) break;
1558 channel->read_size += bytes_read;
1559 to_read -= bytes_read;
1560 offset += bytes_read;
1562 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1563 return S_OK;
1566 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1568 unsigned char byte;
1569 HRESULT hr;
1571 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1572 *size = byte & 0x7f;
1573 if (!(byte & 0x80)) return S_OK;
1575 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1576 *size += (byte & 0x7f) << 7;
1577 if (!(byte & 0x80)) return S_OK;
1579 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1580 *size += (byte & 0x7f) << 14;
1581 if (!(byte & 0x80)) return S_OK;
1583 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1584 *size += (byte & 0x7f) << 21;
1585 if (!(byte & 0x80)) return S_OK;
1587 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1588 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1589 *size += byte << 28;
1590 return S_OK;
1593 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1595 switch (encoding)
1597 case KNOWN_ENCODING_SOAP11_UTF8:
1598 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1599 case KNOWN_ENCODING_SOAP11_UTF16:
1600 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1601 case KNOWN_ENCODING_SOAP11_UTF16LE:
1602 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1603 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1604 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1605 default:
1606 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1607 return WS_ENCODING_XML_UTF8;
1611 static HRESULT receive_preamble( struct channel *channel )
1613 unsigned char type;
1614 HRESULT hr;
1616 for (;;)
1618 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1619 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1620 switch (type)
1622 case FRAME_RECORD_TYPE_VERSION:
1624 unsigned char major, minor;
1625 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1626 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1627 TRACE( "major %u minor %u\n", major, major );
1628 break;
1630 case FRAME_RECORD_TYPE_MODE:
1632 unsigned char mode;
1633 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1634 TRACE( "mode %u\n", mode );
1635 break;
1637 case FRAME_RECORD_TYPE_VIA:
1639 unsigned int size;
1640 unsigned char *url;
1642 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1643 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1644 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1646 heap_free( url );
1647 return hr;
1649 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1650 heap_free( url ); /* FIXME: verify */
1651 break;
1653 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1655 unsigned char encoding;
1656 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1657 TRACE( "encoding %u\n", encoding );
1658 channel->encoding = map_known_encoding( encoding );
1659 break;
1661 default:
1662 WARN( "unhandled record type %u\n", type );
1663 return WS_E_INVALID_FORMAT;
1667 return S_OK;
1670 static HRESULT receive_sized_envelope( struct channel *channel )
1672 unsigned char type;
1673 unsigned int size;
1674 HRESULT hr;
1676 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1677 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1678 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1679 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1680 return S_OK;
1683 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1685 const BYTE *buf = *ptr;
1687 if (len < 1) return WS_E_INVALID_FORMAT;
1688 *size = buf[0] & 0x7f;
1689 if (!(buf[0] & 0x80))
1691 *ptr += 1;
1692 return S_OK;
1694 if (len < 2) return WS_E_INVALID_FORMAT;
1695 *size += (buf[1] & 0x7f) << 7;
1696 if (!(buf[1] & 0x80))
1698 *ptr += 2;
1699 return S_OK;
1701 if (len < 3) return WS_E_INVALID_FORMAT;
1702 *size += (buf[2] & 0x7f) << 14;
1703 if (!(buf[2] & 0x80))
1705 *ptr += 3;
1706 return S_OK;
1708 if (len < 4) return WS_E_INVALID_FORMAT;
1709 *size += (buf[3] & 0x7f) << 21;
1710 if (!(buf[3] & 0x80))
1712 *ptr += 4;
1713 return S_OK;
1715 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1716 *size += buf[4] << 28;
1717 *ptr += 5;
1718 return S_OK;
1721 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1723 ULONG size, strings_size, strings_offset;
1724 const BYTE *ptr = buf;
1725 BYTE *bytes;
1726 int index;
1727 HRESULT hr;
1729 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1730 strings_offset = ptr - buf;
1731 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1732 *used = strings_offset + strings_size;
1733 if (!strings_size) return S_OK;
1735 UuidCreate( &dict->dict.guid );
1736 dict->dict.isConst = FALSE;
1738 buflen -= strings_offset;
1739 ptr = buf + strings_offset;
1740 while (ptr < buf + strings_size)
1742 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1744 clear_dict( dict );
1745 return hr;
1747 if (size > buflen)
1749 clear_dict( dict );
1750 return WS_E_INVALID_FORMAT;
1752 buflen -= size;
1753 if (!(bytes = heap_alloc( size )))
1755 hr = E_OUTOFMEMORY;
1756 goto error;
1758 memcpy( bytes, ptr, size );
1759 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1761 heap_free( bytes );
1762 ptr += size;
1763 continue;
1765 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1767 heap_free( bytes );
1768 clear_dict( dict );
1769 return hr;
1771 ptr += size;
1773 return S_OK;
1775 error:
1776 clear_dict( dict );
1777 return hr;
1780 static HRESULT send_preamble_ack( struct channel *channel )
1782 HRESULT hr;
1783 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1784 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1785 return S_OK;
1788 static HRESULT receive_message_session( struct channel *channel )
1790 HRESULT hr;
1792 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1793 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1795 ULONG size;
1796 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict_recv,
1797 &size )) != S_OK) return hr;
1798 channel->read_size -= size;
1799 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1802 return init_reader( channel );
1805 static HRESULT receive_message_sock( struct channel *channel, SOCKET socket )
1807 HRESULT hr;
1808 if ((hr = receive_message_unsized( channel, socket )) != S_OK) return hr;
1809 return init_reader( channel );
1812 static HRESULT receive_message_bytes( struct channel *channel )
1814 HRESULT hr;
1815 if ((hr = connect_channel( channel )) != S_OK) return hr;
1817 switch (channel->binding)
1819 case WS_HTTP_CHANNEL_BINDING:
1820 return receive_message_http( channel );
1822 case WS_TCP_CHANNEL_BINDING:
1823 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1825 switch (channel->session_state)
1827 case SESSION_STATE_UNINITIALIZED:
1828 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1829 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1830 /* fall through */
1832 case SESSION_STATE_SETUP_COMPLETE:
1833 return receive_message_session( channel );
1835 default:
1836 ERR( "unhandled session state %u\n", channel->session_state );
1837 return WS_E_OTHER;
1840 return receive_message_sock( channel, channel->u.tcp.socket );
1842 case WS_UDP_CHANNEL_BINDING:
1843 return receive_message_sock( channel, channel->u.udp.socket );
1845 default:
1846 ERR( "unhandled binding %u\n", channel->binding );
1847 return E_NOTIMPL;
1851 HRESULT channel_receive_message( WS_CHANNEL *handle )
1853 struct channel *channel = (struct channel *)handle;
1854 HRESULT hr;
1856 EnterCriticalSection( &channel->cs );
1858 if (channel->magic != CHANNEL_MAGIC)
1860 LeaveCriticalSection( &channel->cs );
1861 return E_INVALIDARG;
1864 hr = receive_message_bytes( channel );
1866 LeaveCriticalSection( &channel->cs );
1867 return hr;
1870 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1872 struct channel *channel = (struct channel *)handle;
1874 EnterCriticalSection( &channel->cs );
1876 if (channel->magic != CHANNEL_MAGIC)
1878 LeaveCriticalSection( &channel->cs );
1879 return E_INVALIDARG;
1882 *reader = channel->reader;
1884 LeaveCriticalSection( &channel->cs );
1885 return S_OK;
1888 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1889 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1891 HRESULT hr;
1892 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1893 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1894 return WsReadEnvelopeEnd( handle, NULL );
1897 static HRESULT receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1898 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1899 void *value, ULONG size, ULONG *index )
1901 HRESULT hr;
1902 ULONG i;
1904 if ((hr = receive_message_bytes( channel )) != S_OK) return hr;
1905 for (i = 0; i < count; i++)
1907 const WS_ELEMENT_DESCRIPTION *body = desc[i]->bodyElementDescription;
1908 if ((hr = read_message( msg, channel->reader, body, read_option, heap, value, size )) == S_OK)
1910 if (index) *index = i;
1911 break;
1913 if ((hr = WsResetMessage( msg, NULL )) != S_OK) return hr;
1914 if ((hr = init_reader( channel )) != S_OK) return hr;
1916 return (i == count) ? WS_E_INVALID_FORMAT : S_OK;
1919 struct receive_message
1921 struct task task;
1922 struct channel *channel;
1923 WS_MESSAGE *msg;
1924 const WS_MESSAGE_DESCRIPTION **desc;
1925 ULONG count;
1926 WS_RECEIVE_OPTION option;
1927 WS_READ_OPTION read_option;
1928 WS_HEAP *heap;
1929 void *value;
1930 ULONG size;
1931 ULONG *index;
1932 WS_ASYNC_CONTEXT ctx;
1935 static void receive_message_proc( struct task *task )
1937 struct receive_message *r = (struct receive_message *)task;
1938 HRESULT hr;
1940 hr = receive_message( r->channel, r->msg, r->desc, r->count, r->option, r->read_option, r->heap, r->value,
1941 r->size, r->index );
1943 TRACE( "calling %p(%08x)\n", r->ctx.callback, hr );
1944 r->ctx.callback( hr, WS_LONG_CALLBACK, r->ctx.callbackState );
1945 TRACE( "%p returned\n", r->ctx.callback );
1948 static HRESULT queue_receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1949 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option,
1950 WS_HEAP *heap, void *value, ULONG size, ULONG *index,
1951 const WS_ASYNC_CONTEXT *ctx )
1953 struct receive_message *r;
1955 if (!(r = heap_alloc( sizeof(*r) ))) return E_OUTOFMEMORY;
1956 r->task.proc = receive_message_proc;
1957 r->channel = channel;
1958 r->msg = msg;
1959 r->desc = desc;
1960 r->count = count;
1961 r->option = option;
1962 r->read_option = read_option;
1963 r->heap = heap;
1964 r->value = value;
1965 r->size = size;
1966 r->index = index;
1967 r->ctx = *ctx;
1968 return queue_task( &channel->recv_q, &r->task );
1971 /**************************************************************************
1972 * WsReceiveMessage [webservices.@]
1974 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1975 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1976 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1978 struct channel *channel = (struct channel *)handle;
1979 HRESULT hr;
1981 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1982 value, size, index, ctx, error );
1983 if (error) FIXME( "ignoring error parameter\n" );
1985 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1987 EnterCriticalSection( &channel->cs );
1989 if (channel->magic != CHANNEL_MAGIC)
1991 LeaveCriticalSection( &channel->cs );
1992 return E_INVALIDARG;
1995 if (ctx)
1996 hr = queue_receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index, ctx );
1997 else
1998 hr = receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index );
2000 LeaveCriticalSection( &channel->cs );
2001 TRACE( "returning %08x\n", hr );
2002 return hr;
2005 /**************************************************************************
2006 * WsReadMessageStart [webservices.@]
2008 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2009 WS_ERROR *error )
2011 struct channel *channel = (struct channel *)handle;
2012 HRESULT hr;
2014 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2015 if (error) FIXME( "ignoring error parameter\n" );
2016 if (ctx) FIXME( "ignoring ctx parameter\n" );
2018 if (!channel || !msg) return E_INVALIDARG;
2020 EnterCriticalSection( &channel->cs );
2022 if (channel->magic != CHANNEL_MAGIC)
2024 LeaveCriticalSection( &channel->cs );
2025 return E_INVALIDARG;
2028 if ((hr = receive_message_bytes( channel )) == S_OK)
2030 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
2033 LeaveCriticalSection( &channel->cs );
2034 TRACE( "returning %08x\n", hr );
2035 return hr;
2038 /**************************************************************************
2039 * WsReadMessageEnd [webservices.@]
2041 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2042 WS_ERROR *error )
2044 struct channel *channel = (struct channel *)handle;
2045 HRESULT hr;
2047 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2048 if (error) FIXME( "ignoring error parameter\n" );
2049 if (ctx) FIXME( "ignoring ctx parameter\n" );
2051 if (!channel || !msg) return E_INVALIDARG;
2053 EnterCriticalSection( &channel->cs );
2055 if (channel->magic != CHANNEL_MAGIC)
2057 LeaveCriticalSection( &channel->cs );
2058 return E_INVALIDARG;
2061 hr = WsReadEnvelopeEnd( msg, NULL );
2063 LeaveCriticalSection( &channel->cs );
2064 TRACE( "returning %08x\n", hr );
2065 return hr;
2068 /**************************************************************************
2069 * WsWriteMessageStart [webservices.@]
2071 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2072 WS_ERROR *error )
2074 struct channel *channel = (struct channel *)handle;
2075 HRESULT hr;
2077 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2078 if (error) FIXME( "ignoring error parameter\n" );
2079 if (ctx) FIXME( "ignoring ctx parameter\n" );
2081 if (!channel || !msg) return E_INVALIDARG;
2083 EnterCriticalSection( &channel->cs );
2085 if (channel->magic != CHANNEL_MAGIC)
2087 LeaveCriticalSection( &channel->cs );
2088 return E_INVALIDARG;
2091 if ((hr = init_writer( channel )) != S_OK) goto done;
2092 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
2093 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
2095 done:
2096 LeaveCriticalSection( &channel->cs );
2097 TRACE( "returning %08x\n", hr );
2098 return hr;
2101 /**************************************************************************
2102 * WsWriteMessageEnd [webservices.@]
2104 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2105 WS_ERROR *error )
2107 struct channel *channel = (struct channel *)handle;
2108 HRESULT hr;
2110 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2111 if (error) FIXME( "ignoring error parameter\n" );
2112 if (ctx) FIXME( "ignoring ctx parameter\n" );
2114 if (!channel || !msg) return E_INVALIDARG;
2116 EnterCriticalSection( &channel->cs );
2118 if (channel->magic != CHANNEL_MAGIC)
2120 LeaveCriticalSection( &channel->cs );
2121 return E_INVALIDARG;
2124 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK) hr = send_message( channel, msg );
2126 LeaveCriticalSection( &channel->cs );
2127 TRACE( "returning %08x\n", hr );
2128 return hr;
2131 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
2133 HANDLE handles[] = { wait, cancel };
2134 ULONG nonblocking = 0;
2135 HRESULT hr = S_OK;
2137 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2139 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2141 case 0:
2142 if ((*ret = accept( socket, NULL, NULL )) != -1)
2144 WSAEventSelect( *ret, NULL, 0 );
2145 ioctlsocket( *ret, FIONBIO, &nonblocking );
2146 break;
2148 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2149 break;
2151 case 1:
2152 hr = WS_E_OPERATION_ABORTED;
2153 break;
2155 default:
2156 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2157 break;
2160 return hr;
2163 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2165 struct channel *channel = (struct channel *)handle;
2166 HRESULT hr;
2168 EnterCriticalSection( &channel->cs );
2170 if (channel->magic != CHANNEL_MAGIC)
2172 LeaveCriticalSection( &channel->cs );
2173 return E_INVALIDARG;
2176 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
2178 LeaveCriticalSection( &channel->cs );
2179 return hr;
2182 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
2184 HANDLE handles[] = { wait, cancel };
2185 ULONG nonblocking = 0;
2186 HRESULT hr;
2188 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2190 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2192 case 0:
2193 hr = S_OK;
2194 break;
2196 case 1:
2197 hr = WS_E_OPERATION_ABORTED;
2198 break;
2200 default:
2201 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2202 break;
2205 WSAEventSelect( socket, NULL, 0 );
2206 ioctlsocket( socket, FIONBIO, &nonblocking );
2207 return hr;
2210 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2212 struct channel *channel = (struct channel *)handle;
2213 HRESULT hr;
2215 EnterCriticalSection( &channel->cs );
2217 if (channel->magic != CHANNEL_MAGIC)
2219 LeaveCriticalSection( &channel->cs );
2220 return E_INVALIDARG;
2223 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
2225 LeaveCriticalSection( &channel->cs );
2226 return hr;
2229 /**************************************************************************
2230 * WsAbortChannel [webservices.@]
2232 HRESULT WINAPI WsAbortChannel( WS_CHANNEL *handle, WS_ERROR *error )
2234 FIXME( "%p %p: stub!\n", handle, error );
2235 return E_NOTIMPL;