Release 4.0.
[wine.git] / dlls / webservices / channel.c
blob139e625a36a54e10c41e73d4ea29392ae291f3a3
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 HRESULT receive_bytes( struct channel *channel, unsigned char *bytes, int len )
1149 int count = recv( channel->u.tcp.socket, (char *)bytes, len, 0 );
1150 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1151 if (count != len) return WS_E_INVALID_FORMAT;
1152 return S_OK;
1155 static HRESULT receive_preamble_ack( struct channel *channel )
1157 unsigned char byte;
1158 HRESULT hr;
1160 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1161 if (byte != FRAME_RECORD_TYPE_PREAMBLE_ACK) return WS_E_INVALID_FORMAT;
1162 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1163 return S_OK;
1166 static HRESULT send_sized_envelope( struct channel *channel, BYTE *data, ULONG len )
1168 ULONG table_size = string_table_size( &channel->dict_send );
1169 HRESULT hr;
1171 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_SIZED_ENVELOPE )) != S_OK) return hr;
1172 if ((hr = send_size( channel->u.tcp.socket, size_length(table_size) + table_size + len )) != S_OK) return hr;
1173 if ((hr = send_size( channel->u.tcp.socket, table_size )) != S_OK) return hr;
1174 if ((hr = send_string_table( channel->u.tcp.socket, &channel->dict_send )) != S_OK) return hr;
1175 return send_bytes( channel->u.tcp.socket, data, len );
1178 static HRESULT open_http_request( struct channel *channel, HINTERNET *req )
1180 static const WCHAR postW[] = {'P','O','S','T',0};
1181 if ((*req = WinHttpOpenRequest( channel->u.http.connect, postW, channel->u.http.path,
1182 NULL, NULL, NULL, channel->u.http.flags ))) return S_OK;
1183 return HRESULT_FROM_WIN32( GetLastError() );
1186 static HRESULT send_message( struct channel *channel, WS_MESSAGE *msg )
1188 WS_XML_WRITER *writer;
1189 WS_BYTES buf;
1190 HRESULT hr;
1192 channel->msg = msg;
1193 WsGetMessageProperty( channel->msg, WS_MESSAGE_PROPERTY_BODY_WRITER, &writer, sizeof(writer), NULL );
1194 WsGetWriterProperty( writer, WS_XML_WRITER_PROPERTY_BYTES, &buf, sizeof(buf), NULL );
1196 switch (channel->binding)
1198 case WS_HTTP_CHANNEL_BINDING:
1199 if (channel->u.http.request)
1201 WinHttpCloseHandle( channel->u.http.request );
1202 channel->u.http.request = NULL;
1204 if ((hr = open_http_request( channel, &channel->u.http.request )) != S_OK) return hr;
1205 if ((hr = message_insert_http_headers( msg, channel->u.http.request )) != S_OK) return hr;
1206 return send_message_http( channel->u.http.request, buf.bytes, buf.length );
1208 case WS_TCP_CHANNEL_BINDING:
1209 if (channel->type & WS_CHANNEL_TYPE_SESSION)
1211 switch (channel->session_state)
1213 case SESSION_STATE_UNINITIALIZED:
1214 if ((hr = send_preamble( channel )) != S_OK) return hr;
1215 if ((hr = receive_preamble_ack( channel )) != S_OK) return hr;
1216 /* fall through */
1218 case SESSION_STATE_SETUP_COMPLETE:
1219 return send_sized_envelope( channel, buf.bytes, buf.length );
1221 default:
1222 ERR( "unhandled session state %u\n", channel->session_state );
1223 return WS_E_OTHER;
1226 /* fall through */
1228 case WS_UDP_CHANNEL_BINDING:
1229 return WsFlushWriter( writer, 0, NULL, NULL );
1231 default:
1232 ERR( "unhandled binding %u\n", channel->binding );
1233 return E_NOTIMPL;
1237 HRESULT channel_send_message( WS_CHANNEL *handle, WS_MESSAGE *msg )
1239 struct channel *channel = (struct channel *)handle;
1240 HRESULT hr;
1242 EnterCriticalSection( &channel->cs );
1244 if (channel->magic != CHANNEL_MAGIC)
1246 LeaveCriticalSection( &channel->cs );
1247 return E_INVALIDARG;
1250 if ((hr = connect_channel( channel )) == S_OK) hr = send_message( channel, msg );
1252 LeaveCriticalSection( &channel->cs );
1253 return hr;
1256 static HRESULT CALLBACK dict_cb( void *state, const WS_XML_STRING *str, BOOL *found, ULONG *id, WS_ERROR *error )
1258 struct dictionary *dict = state;
1259 HRESULT hr = S_OK;
1260 BYTE *bytes;
1261 int index;
1263 if ((index = find_string( dict, str->bytes, str->length, id )) == -1)
1265 *found = TRUE;
1266 return S_OK;
1269 if (!(bytes = heap_alloc( str->length ))) return E_OUTOFMEMORY;
1270 memcpy( bytes, str->bytes, str->length );
1271 if ((hr = insert_string( dict, bytes, str->length, index, id )) == S_OK)
1273 *found = TRUE;
1274 return S_OK;
1276 heap_free( bytes );
1278 *found = FALSE;
1279 return hr;
1282 static CALLBACK HRESULT write_callback( void *state, const WS_BYTES *buf, ULONG count,
1283 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1285 SOCKET socket = *(SOCKET *)state;
1286 if (send( socket, (const char *)buf->bytes, buf->length, 0 ) < 0)
1288 TRACE( "send failed %u\n", WSAGetLastError() );
1290 return S_OK;
1293 static HRESULT init_writer( struct channel *channel )
1295 WS_XML_WRITER_BUFFER_OUTPUT buf = {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER}};
1296 WS_XML_WRITER_STREAM_OUTPUT stream = {{WS_XML_WRITER_OUTPUT_TYPE_STREAM}};
1297 WS_XML_WRITER_TEXT_ENCODING text = {{WS_XML_WRITER_ENCODING_TYPE_TEXT}, WS_CHARSET_UTF8};
1298 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1299 const WS_XML_WRITER_ENCODING *encoding;
1300 const WS_XML_WRITER_OUTPUT *output;
1301 WS_XML_WRITER_PROPERTY prop;
1302 ULONG max_size = (1 << 17);
1303 HRESULT hr;
1305 prop.id = WS_XML_WRITER_PROPERTY_BUFFER_MAX_SIZE;
1306 prop.value = &max_size;
1307 prop.valueSize = sizeof(max_size);
1308 if (!channel->writer && (hr = WsCreateWriter( &prop, 1, &channel->writer, NULL )) != S_OK) return hr;
1310 switch (channel->encoding)
1312 case WS_ENCODING_XML_UTF8:
1313 encoding = &text.encoding;
1314 if (channel->binding == WS_UDP_CHANNEL_BINDING ||
1315 (channel->binding == WS_TCP_CHANNEL_BINDING && !(channel->type & WS_CHANNEL_TYPE_SESSION)))
1317 stream.writeCallback = write_callback;
1318 stream.writeCallbackState = (channel->binding == WS_UDP_CHANNEL_BINDING) ?
1319 &channel->u.udp.socket : &channel->u.tcp.socket;
1320 output = &stream.output;
1322 else output = &buf.output;
1323 break;
1325 case WS_ENCODING_XML_BINARY_SESSION_1:
1326 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1327 /* fall through */
1329 case WS_ENCODING_XML_BINARY_1:
1330 encoding = &bin.encoding;
1331 output = &buf.output;
1332 break;
1334 default:
1335 FIXME( "unhandled encoding %u\n", channel->encoding );
1336 return WS_E_NOT_SUPPORTED;
1339 return WsSetOutput( channel->writer, encoding, output, NULL, 0, NULL );
1342 static HRESULT write_message( struct channel *channel, WS_MESSAGE *msg, const WS_ELEMENT_DESCRIPTION *desc,
1343 WS_WRITE_OPTION option, const void *body, ULONG size )
1345 HRESULT hr;
1346 if ((hr = writer_set_lookup( channel->writer, TRUE )) != S_OK) return hr;
1347 if ((hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL )) != S_OK) return hr;
1348 if ((hr = writer_set_lookup( channel->writer, FALSE )) != S_OK) return hr;
1349 channel->dict_send.current_sequence++;
1350 if ((hr = writer_set_dict_callback( channel->writer, dict_cb, &channel->dict_send )) != S_OK) return hr;
1351 if ((hr = WsWriteBody( msg, desc, option, body, size, NULL )) != S_OK) return hr;
1352 return WsWriteEnvelopeEnd( msg, NULL );
1355 /**************************************************************************
1356 * WsSendMessage [webservices.@]
1358 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1359 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1360 WS_ERROR *error )
1362 struct channel *channel = (struct channel *)handle;
1363 HRESULT hr;
1365 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1366 if (error) FIXME( "ignoring error parameter\n" );
1367 if (ctx) FIXME( "ignoring ctx parameter\n" );
1369 if (!channel || !msg || !desc) 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_REQUEST_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;
1383 if ((hr = connect_channel( channel )) != S_OK) goto done;
1384 if ((hr = init_writer( channel )) != S_OK) goto done;
1385 if ((hr = write_message( channel, msg, desc->bodyElementDescription, option, body, size )) != S_OK) goto done;
1386 hr = send_message( channel, msg );
1388 done:
1389 LeaveCriticalSection( &channel->cs );
1390 TRACE( "returning %08x\n", hr );
1391 return hr;
1394 /**************************************************************************
1395 * WsSendReplyMessage [webservices.@]
1397 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1398 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1399 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1401 struct channel *channel = (struct channel *)handle;
1402 GUID req_id;
1403 HRESULT hr;
1405 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1406 if (error) FIXME( "ignoring error parameter\n" );
1407 if (ctx) FIXME( "ignoring ctx parameter\n" );
1409 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1411 EnterCriticalSection( &channel->cs );
1413 if (channel->magic != CHANNEL_MAGIC)
1415 LeaveCriticalSection( &channel->cs );
1416 return E_INVALIDARG;
1419 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1420 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1421 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1422 if ((hr = message_get_id( request, &req_id )) != S_OK) goto done;
1423 if ((hr = message_set_request_id( msg, &req_id )) != S_OK) goto done;
1425 if ((hr = connect_channel( channel )) != S_OK) goto done;
1426 if ((hr = init_writer( channel )) != S_OK) goto done;
1427 if ((hr = write_message( channel, msg, desc->bodyElementDescription, option, body, size )) != S_OK) goto done;
1428 hr = send_message( channel, msg );
1430 done:
1431 LeaveCriticalSection( &channel->cs );
1432 TRACE( "returning %08x\n", hr );
1433 return hr;
1436 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1438 if (!channel->read_buf)
1440 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1441 channel->read_buflen = size;
1442 return S_OK;
1444 if (channel->read_buflen < size)
1446 char *tmp;
1447 ULONG new_size = max( size, channel->read_buflen * 2 );
1448 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1449 channel->read_buf = tmp;
1450 channel->read_buflen = new_size;
1452 return S_OK;
1455 static CALLBACK HRESULT read_callback( void *state, void *buf, ULONG buflen, ULONG *retlen,
1456 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1458 SOCKET socket = *(SOCKET *)state;
1459 int ret;
1461 if ((ret = recv( socket, buf, buflen, 0 )) >= 0) *retlen = ret;
1462 else
1464 TRACE( "recv failed %u\n", WSAGetLastError() );
1465 *retlen = 0;
1467 return S_OK;
1470 static HRESULT init_reader( struct channel *channel )
1472 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1473 WS_XML_READER_STREAM_INPUT stream = {{WS_XML_READER_INPUT_TYPE_STREAM}};
1474 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1475 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1476 const WS_XML_READER_ENCODING *encoding;
1477 const WS_XML_READER_INPUT *input;
1478 HRESULT hr;
1480 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1482 switch (channel->encoding)
1484 case WS_ENCODING_XML_UTF8:
1485 text.charSet = WS_CHARSET_UTF8;
1486 encoding = &text.encoding;
1488 if (channel->binding == WS_UDP_CHANNEL_BINDING ||
1489 (channel->binding == WS_TCP_CHANNEL_BINDING && !(channel->type & WS_CHANNEL_TYPE_SESSION)))
1491 stream.readCallback = read_callback;
1492 stream.readCallbackState = (channel->binding == WS_UDP_CHANNEL_BINDING) ?
1493 &channel->u.udp.socket : &channel->u.tcp.socket;
1494 input = &stream.input;
1496 else
1498 buf.encodedData = channel->read_buf;
1499 buf.encodedDataSize = channel->read_size;
1500 input = &buf.input;
1502 break;
1504 case WS_ENCODING_XML_BINARY_SESSION_1:
1505 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1506 bin.dynamicDictionary = &channel->dict_recv.dict;
1507 /* fall through */
1509 case WS_ENCODING_XML_BINARY_1:
1510 encoding = &bin.encoding;
1512 buf.encodedData = channel->read_buf;
1513 buf.encodedDataSize = channel->read_size;
1514 input = &buf.input;
1515 break;
1517 default:
1518 FIXME( "unhandled encoding %u\n", channel->encoding );
1519 return WS_E_NOT_SUPPORTED;
1522 return WsSetInput( channel->reader, encoding, input, NULL, 0, NULL );
1525 #define INITIAL_READ_BUFFER_SIZE 4096
1526 static HRESULT receive_message_http( struct channel *channel )
1528 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1529 ULONG max_len;
1530 HRESULT hr;
1532 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1533 &max_len, sizeof(max_len) );
1535 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1536 channel->read_size = 0;
1537 for (;;)
1539 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1541 return HRESULT_FROM_WIN32( GetLastError() );
1543 if (!len) break;
1544 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1545 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1547 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1549 return HRESULT_FROM_WIN32( GetLastError() );
1551 if (!bytes_read) break;
1552 channel->read_size += bytes_read;
1553 offset += bytes_read;
1556 return S_OK;
1559 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1561 unsigned int offset = 0, to_read = size;
1562 int bytes_read;
1563 HRESULT hr;
1565 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1567 channel->read_size = 0;
1568 while (channel->read_size < size)
1570 if ((bytes_read = recv( channel->u.tcp.socket, channel->read_buf + offset, to_read, 0 )) < 0)
1572 return HRESULT_FROM_WIN32( WSAGetLastError() );
1574 if (!bytes_read) break;
1575 channel->read_size += bytes_read;
1576 to_read -= bytes_read;
1577 offset += bytes_read;
1579 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1580 return S_OK;
1583 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1585 unsigned char byte;
1586 HRESULT hr;
1588 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1589 *size = byte & 0x7f;
1590 if (!(byte & 0x80)) return S_OK;
1592 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1593 *size += (byte & 0x7f) << 7;
1594 if (!(byte & 0x80)) return S_OK;
1596 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1597 *size += (byte & 0x7f) << 14;
1598 if (!(byte & 0x80)) return S_OK;
1600 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1601 *size += (byte & 0x7f) << 21;
1602 if (!(byte & 0x80)) return S_OK;
1604 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1605 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1606 *size += byte << 28;
1607 return S_OK;
1610 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1612 switch (encoding)
1614 case KNOWN_ENCODING_SOAP11_UTF8:
1615 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1616 case KNOWN_ENCODING_SOAP11_UTF16:
1617 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1618 case KNOWN_ENCODING_SOAP11_UTF16LE:
1619 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1620 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1621 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1622 default:
1623 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1624 return WS_ENCODING_XML_UTF8;
1628 static HRESULT receive_preamble( struct channel *channel )
1630 unsigned char type;
1631 HRESULT hr;
1633 for (;;)
1635 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1636 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1637 switch (type)
1639 case FRAME_RECORD_TYPE_VERSION:
1641 unsigned char major, minor;
1642 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1643 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1644 TRACE( "major %u minor %u\n", major, major );
1645 break;
1647 case FRAME_RECORD_TYPE_MODE:
1649 unsigned char mode;
1650 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1651 TRACE( "mode %u\n", mode );
1652 break;
1654 case FRAME_RECORD_TYPE_VIA:
1656 unsigned int size;
1657 unsigned char *url;
1659 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1660 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1661 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1663 heap_free( url );
1664 return hr;
1666 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1667 heap_free( url ); /* FIXME: verify */
1668 break;
1670 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1672 unsigned char encoding;
1673 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1674 TRACE( "encoding %u\n", encoding );
1675 channel->encoding = map_known_encoding( encoding );
1676 break;
1678 default:
1679 WARN( "unhandled record type %u\n", type );
1680 return WS_E_INVALID_FORMAT;
1684 return S_OK;
1687 static HRESULT receive_sized_envelope( struct channel *channel )
1689 unsigned char type;
1690 unsigned int size;
1691 HRESULT hr;
1693 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1694 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1695 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1696 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1697 return S_OK;
1700 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1702 const BYTE *buf = *ptr;
1704 if (len < 1) return WS_E_INVALID_FORMAT;
1705 *size = buf[0] & 0x7f;
1706 if (!(buf[0] & 0x80))
1708 *ptr += 1;
1709 return S_OK;
1711 if (len < 2) return WS_E_INVALID_FORMAT;
1712 *size += (buf[1] & 0x7f) << 7;
1713 if (!(buf[1] & 0x80))
1715 *ptr += 2;
1716 return S_OK;
1718 if (len < 3) return WS_E_INVALID_FORMAT;
1719 *size += (buf[2] & 0x7f) << 14;
1720 if (!(buf[2] & 0x80))
1722 *ptr += 3;
1723 return S_OK;
1725 if (len < 4) return WS_E_INVALID_FORMAT;
1726 *size += (buf[3] & 0x7f) << 21;
1727 if (!(buf[3] & 0x80))
1729 *ptr += 4;
1730 return S_OK;
1732 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1733 *size += buf[4] << 28;
1734 *ptr += 5;
1735 return S_OK;
1738 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1740 ULONG size, strings_size, strings_offset;
1741 const BYTE *ptr = buf;
1742 BYTE *bytes;
1743 int index;
1744 HRESULT hr;
1746 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1747 strings_offset = ptr - buf;
1748 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1749 *used = strings_offset + strings_size;
1750 if (!strings_size) return S_OK;
1752 UuidCreate( &dict->dict.guid );
1753 dict->dict.isConst = FALSE;
1755 buflen -= strings_offset;
1756 ptr = buf + strings_offset;
1757 while (ptr < buf + strings_size)
1759 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1761 clear_dict( dict );
1762 return hr;
1764 if (size > buflen)
1766 clear_dict( dict );
1767 return WS_E_INVALID_FORMAT;
1769 buflen -= size;
1770 if (!(bytes = heap_alloc( size )))
1772 hr = E_OUTOFMEMORY;
1773 goto error;
1775 memcpy( bytes, ptr, size );
1776 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1778 heap_free( bytes );
1779 ptr += size;
1780 continue;
1782 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1784 heap_free( bytes );
1785 clear_dict( dict );
1786 return hr;
1788 ptr += size;
1790 return S_OK;
1792 error:
1793 clear_dict( dict );
1794 return hr;
1797 static HRESULT send_preamble_ack( struct channel *channel )
1799 HRESULT hr;
1800 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1801 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1802 return S_OK;
1805 static HRESULT receive_message_session( struct channel *channel )
1807 HRESULT hr;
1809 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1810 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1812 ULONG size;
1813 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict_recv,
1814 &size )) != S_OK) return hr;
1815 channel->read_size -= size;
1816 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1819 return S_OK;
1822 static HRESULT receive_message_bytes( struct channel *channel )
1824 HRESULT hr;
1825 if ((hr = connect_channel( channel )) != S_OK) return hr;
1827 switch (channel->binding)
1829 case WS_HTTP_CHANNEL_BINDING:
1830 return receive_message_http( channel );
1832 case WS_TCP_CHANNEL_BINDING:
1833 if (channel->type & WS_CHANNEL_TYPE_SESSION)
1835 switch (channel->session_state)
1837 case SESSION_STATE_UNINITIALIZED:
1838 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1839 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1840 /* fall through */
1842 case SESSION_STATE_SETUP_COMPLETE:
1843 return receive_message_session( channel );
1845 default:
1846 ERR( "unhandled session state %u\n", channel->session_state );
1847 return WS_E_OTHER;
1850 return S_OK; /* nothing to do, data is read through stream callback */
1852 case WS_UDP_CHANNEL_BINDING:
1853 return S_OK;
1855 default:
1856 ERR( "unhandled binding %u\n", channel->binding );
1857 return E_NOTIMPL;
1861 HRESULT channel_receive_message( WS_CHANNEL *handle )
1863 struct channel *channel = (struct channel *)handle;
1864 HRESULT hr;
1866 EnterCriticalSection( &channel->cs );
1868 if (channel->magic != CHANNEL_MAGIC)
1870 LeaveCriticalSection( &channel->cs );
1871 return E_INVALIDARG;
1874 if ((hr = receive_message_bytes( channel )) == S_OK) hr = init_reader( channel );
1876 LeaveCriticalSection( &channel->cs );
1877 return hr;
1880 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1882 struct channel *channel = (struct channel *)handle;
1884 EnterCriticalSection( &channel->cs );
1886 if (channel->magic != CHANNEL_MAGIC)
1888 LeaveCriticalSection( &channel->cs );
1889 return E_INVALIDARG;
1892 *reader = channel->reader;
1894 LeaveCriticalSection( &channel->cs );
1895 return S_OK;
1898 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1899 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1901 HRESULT hr;
1902 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1903 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1904 return WsReadEnvelopeEnd( handle, NULL );
1907 static HRESULT receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1908 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1909 void *value, ULONG size, ULONG *index )
1911 HRESULT hr;
1912 ULONG i;
1914 if ((hr = receive_message_bytes( channel )) != S_OK) return hr;
1915 if ((hr = init_reader( channel )) != S_OK) return hr;
1917 for (i = 0; i < count; i++)
1919 const WS_ELEMENT_DESCRIPTION *body = desc[i]->bodyElementDescription;
1920 if ((hr = read_message( msg, channel->reader, body, read_option, heap, value, size )) == S_OK)
1922 if (index) *index = i;
1923 break;
1925 if ((hr = WsResetMessage( msg, NULL )) != S_OK) return hr;
1926 if ((hr = init_reader( channel )) != S_OK) return hr;
1928 return (i == count) ? WS_E_INVALID_FORMAT : S_OK;
1931 struct receive_message
1933 struct task task;
1934 struct channel *channel;
1935 WS_MESSAGE *msg;
1936 const WS_MESSAGE_DESCRIPTION **desc;
1937 ULONG count;
1938 WS_RECEIVE_OPTION option;
1939 WS_READ_OPTION read_option;
1940 WS_HEAP *heap;
1941 void *value;
1942 ULONG size;
1943 ULONG *index;
1944 WS_ASYNC_CONTEXT ctx;
1947 static void receive_message_proc( struct task *task )
1949 struct receive_message *r = (struct receive_message *)task;
1950 HRESULT hr;
1952 hr = receive_message( r->channel, r->msg, r->desc, r->count, r->option, r->read_option, r->heap, r->value,
1953 r->size, r->index );
1955 TRACE( "calling %p(%08x)\n", r->ctx.callback, hr );
1956 r->ctx.callback( hr, WS_LONG_CALLBACK, r->ctx.callbackState );
1957 TRACE( "%p returned\n", r->ctx.callback );
1960 static HRESULT queue_receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1961 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option,
1962 WS_HEAP *heap, void *value, ULONG size, ULONG *index,
1963 const WS_ASYNC_CONTEXT *ctx )
1965 struct receive_message *r;
1967 if (!(r = heap_alloc( sizeof(*r) ))) return E_OUTOFMEMORY;
1968 r->task.proc = receive_message_proc;
1969 r->channel = channel;
1970 r->msg = msg;
1971 r->desc = desc;
1972 r->count = count;
1973 r->option = option;
1974 r->read_option = read_option;
1975 r->heap = heap;
1976 r->value = value;
1977 r->size = size;
1978 r->index = index;
1979 r->ctx = *ctx;
1980 return queue_task( &channel->recv_q, &r->task );
1983 /**************************************************************************
1984 * WsReceiveMessage [webservices.@]
1986 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1987 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1988 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1990 struct channel *channel = (struct channel *)handle;
1991 HRESULT hr;
1993 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1994 value, size, index, ctx, error );
1995 if (error) FIXME( "ignoring error parameter\n" );
1997 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1999 EnterCriticalSection( &channel->cs );
2001 if (channel->magic != CHANNEL_MAGIC)
2003 LeaveCriticalSection( &channel->cs );
2004 return E_INVALIDARG;
2007 if (ctx)
2008 hr = queue_receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index, ctx );
2009 else
2010 hr = receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index );
2012 LeaveCriticalSection( &channel->cs );
2013 TRACE( "returning %08x\n", hr );
2014 return hr;
2017 static HRESULT request_reply( struct channel *channel, WS_MESSAGE *request,
2018 const WS_MESSAGE_DESCRIPTION *request_desc, WS_WRITE_OPTION write_option,
2019 const void *request_body, ULONG request_size, WS_MESSAGE *reply,
2020 const WS_MESSAGE_DESCRIPTION *reply_desc, WS_READ_OPTION read_option,
2021 WS_HEAP *heap, void *value, ULONG size )
2023 HRESULT hr;
2024 WsInitializeMessage( request, WS_REQUEST_MESSAGE, NULL, NULL );
2025 if ((hr = WsAddressMessage( request, &channel->addr, NULL )) != S_OK) return hr;
2026 if ((hr = message_set_action( request, request_desc->action )) != S_OK) return hr;
2028 if ((hr = connect_channel( channel )) != S_OK) return hr;
2029 if ((hr = init_writer( channel )) != S_OK) return hr;
2030 if ((hr = write_message( channel, request, request_desc->bodyElementDescription, write_option, request_body,
2031 request_size )) != S_OK) return hr;
2032 if ((hr = send_message( channel, request )) != S_OK) return hr;
2034 return receive_message( channel, reply, &reply_desc, 1, WS_RECEIVE_OPTIONAL_MESSAGE, read_option, heap,
2035 value, size, NULL );
2038 struct request_reply
2040 struct task task;
2041 struct channel *channel;
2042 WS_MESSAGE *request;
2043 const WS_MESSAGE_DESCRIPTION *request_desc;
2044 WS_WRITE_OPTION write_option;
2045 const void *request_body;
2046 ULONG request_size;
2047 WS_MESSAGE *reply;
2048 const WS_MESSAGE_DESCRIPTION *reply_desc;
2049 WS_READ_OPTION read_option;
2050 WS_HEAP *heap;
2051 void *value;
2052 ULONG size;
2053 WS_ASYNC_CONTEXT ctx;
2056 static void request_reply_proc( struct task *task )
2058 struct request_reply *r = (struct request_reply *)task;
2059 HRESULT hr;
2061 hr = request_reply( r->channel, r->request, r->request_desc, r->write_option, r->request_body, r->request_size,
2062 r->reply, r->reply_desc, r->read_option, r->heap, r->value, r->size );
2064 TRACE( "calling %p(%08x)\n", r->ctx.callback, hr );
2065 r->ctx.callback( hr, WS_LONG_CALLBACK, r->ctx.callbackState );
2066 TRACE( "%p returned\n", r->ctx.callback );
2069 static HRESULT queue_request_reply( struct channel *channel, WS_MESSAGE *request,
2070 const WS_MESSAGE_DESCRIPTION *request_desc, WS_WRITE_OPTION write_option,
2071 const void *request_body, ULONG request_size, WS_MESSAGE *reply,
2072 const WS_MESSAGE_DESCRIPTION *reply_desc, WS_READ_OPTION read_option,
2073 WS_HEAP *heap, void *value, ULONG size, const WS_ASYNC_CONTEXT *ctx )
2075 struct request_reply *r;
2077 if (!(r = heap_alloc( sizeof(*r) ))) return E_OUTOFMEMORY;
2078 r->task.proc = request_reply_proc;
2079 r->channel = channel;
2080 r->request = request;
2081 r->request_desc = request_desc;
2082 r->write_option = write_option;
2083 r->request_body = request_body;
2084 r->request_size = request_size;
2085 r->reply = reply;
2086 r->reply_desc = reply_desc;
2087 r->read_option = read_option;
2088 r->heap = heap;
2089 r->value = value;
2090 r->size = size;
2091 r->ctx = *ctx;
2092 return queue_task( &channel->recv_q, &r->task );
2095 /**************************************************************************
2096 * WsRequestReply [webservices.@]
2098 HRESULT WINAPI WsRequestReply( WS_CHANNEL *handle, WS_MESSAGE *request, const WS_MESSAGE_DESCRIPTION *request_desc,
2099 WS_WRITE_OPTION write_option, const void *request_body, ULONG request_size,
2100 WS_MESSAGE *reply, const WS_MESSAGE_DESCRIPTION *reply_desc, WS_READ_OPTION read_option,
2101 WS_HEAP *heap, void *value, ULONG size, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
2103 struct channel *channel = (struct channel *)handle;
2104 HRESULT hr;
2106 TRACE( "%p %p %p %08x %p %u %p %p %08x %p %p %u %p %p\n", handle, request, request_desc, write_option,
2107 request_body, request_size, reply, reply_desc, read_option, heap, value, size, ctx, error );
2108 if (error) FIXME( "ignoring error parameter\n" );
2109 if (ctx) FIXME( "ignoring ctx parameter\n" );
2111 if (!channel || !request || !reply) return E_INVALIDARG;
2113 EnterCriticalSection( &channel->cs );
2115 if (channel->magic != CHANNEL_MAGIC)
2117 LeaveCriticalSection( &channel->cs );
2118 return E_INVALIDARG;
2121 if (ctx)
2122 hr = queue_request_reply( channel, request, request_desc, write_option, request_body, request_size, reply,
2123 reply_desc, read_option, heap, value, size, ctx );
2124 else
2125 hr = request_reply( channel, request, request_desc, write_option, request_body, request_size, reply,
2126 reply_desc, read_option, heap, value, size );
2128 LeaveCriticalSection( &channel->cs );
2129 TRACE( "returning %08x\n", hr );
2130 return hr;
2133 /**************************************************************************
2134 * WsReadMessageStart [webservices.@]
2136 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2137 WS_ERROR *error )
2139 struct channel *channel = (struct channel *)handle;
2140 HRESULT hr;
2142 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2143 if (error) FIXME( "ignoring error parameter\n" );
2144 if (ctx) FIXME( "ignoring ctx parameter\n" );
2146 if (!channel || !msg) return E_INVALIDARG;
2148 EnterCriticalSection( &channel->cs );
2150 if (channel->magic != CHANNEL_MAGIC)
2152 LeaveCriticalSection( &channel->cs );
2153 return E_INVALIDARG;
2156 if ((hr = receive_message_bytes( channel )) == S_OK)
2158 if ((hr = init_reader( channel )) == S_OK)
2159 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
2162 LeaveCriticalSection( &channel->cs );
2163 TRACE( "returning %08x\n", hr );
2164 return hr;
2167 /**************************************************************************
2168 * WsReadMessageEnd [webservices.@]
2170 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2171 WS_ERROR *error )
2173 struct channel *channel = (struct channel *)handle;
2174 HRESULT hr;
2176 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2177 if (error) FIXME( "ignoring error parameter\n" );
2178 if (ctx) FIXME( "ignoring ctx parameter\n" );
2180 if (!channel || !msg) return E_INVALIDARG;
2182 EnterCriticalSection( &channel->cs );
2184 if (channel->magic != CHANNEL_MAGIC)
2186 LeaveCriticalSection( &channel->cs );
2187 return E_INVALIDARG;
2190 hr = WsReadEnvelopeEnd( msg, NULL );
2192 LeaveCriticalSection( &channel->cs );
2193 TRACE( "returning %08x\n", hr );
2194 return hr;
2197 /**************************************************************************
2198 * WsWriteMessageStart [webservices.@]
2200 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2201 WS_ERROR *error )
2203 struct channel *channel = (struct channel *)handle;
2204 HRESULT hr;
2206 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2207 if (error) FIXME( "ignoring error parameter\n" );
2208 if (ctx) FIXME( "ignoring ctx parameter\n" );
2210 if (!channel || !msg) return E_INVALIDARG;
2212 EnterCriticalSection( &channel->cs );
2214 if (channel->magic != CHANNEL_MAGIC)
2216 LeaveCriticalSection( &channel->cs );
2217 return E_INVALIDARG;
2220 if ((hr = connect_channel( channel )) != S_OK) goto done;
2221 if ((hr = init_writer( channel )) != S_OK) goto done;
2222 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
2223 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
2225 done:
2226 LeaveCriticalSection( &channel->cs );
2227 TRACE( "returning %08x\n", hr );
2228 return hr;
2231 /**************************************************************************
2232 * WsWriteMessageEnd [webservices.@]
2234 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2235 WS_ERROR *error )
2237 struct channel *channel = (struct channel *)handle;
2238 HRESULT hr;
2240 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2241 if (error) FIXME( "ignoring error parameter\n" );
2242 if (ctx) FIXME( "ignoring ctx parameter\n" );
2244 if (!channel || !msg) return E_INVALIDARG;
2246 EnterCriticalSection( &channel->cs );
2248 if (channel->magic != CHANNEL_MAGIC)
2250 LeaveCriticalSection( &channel->cs );
2251 return E_INVALIDARG;
2254 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK && (hr = connect_channel( channel ) == S_OK))
2255 hr = send_message( channel, msg );
2257 LeaveCriticalSection( &channel->cs );
2258 TRACE( "returning %08x\n", hr );
2259 return hr;
2262 static void set_blocking( SOCKET socket, BOOL blocking )
2264 ULONG state = !blocking;
2265 ioctlsocket( socket, FIONBIO, &state );
2268 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
2270 HANDLE handles[] = { wait, cancel };
2271 HRESULT hr = S_OK;
2273 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2275 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2277 case 0:
2278 if ((*ret = accept( socket, NULL, NULL )) != -1)
2280 WSAEventSelect( *ret, NULL, 0 );
2281 set_blocking( *ret, TRUE );
2282 break;
2284 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2285 break;
2287 case 1:
2288 hr = WS_E_OPERATION_ABORTED;
2289 break;
2291 default:
2292 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2293 break;
2296 return hr;
2299 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2301 struct channel *channel = (struct channel *)handle;
2302 HRESULT hr;
2304 EnterCriticalSection( &channel->cs );
2306 if (channel->magic != CHANNEL_MAGIC)
2308 LeaveCriticalSection( &channel->cs );
2309 return E_INVALIDARG;
2312 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
2314 LeaveCriticalSection( &channel->cs );
2315 return hr;
2318 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
2320 HANDLE handles[] = { wait, cancel };
2321 HRESULT hr;
2323 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2325 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2327 case 0:
2328 hr = S_OK;
2329 break;
2331 case 1:
2332 hr = WS_E_OPERATION_ABORTED;
2333 break;
2335 default:
2336 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2337 break;
2340 WSAEventSelect( socket, NULL, 0 );
2341 set_blocking( socket, TRUE );
2342 return hr;
2345 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2347 struct channel *channel = (struct channel *)handle;
2348 HRESULT hr;
2350 EnterCriticalSection( &channel->cs );
2352 if (channel->magic != CHANNEL_MAGIC)
2354 LeaveCriticalSection( &channel->cs );
2355 return E_INVALIDARG;
2358 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
2360 LeaveCriticalSection( &channel->cs );
2361 return hr;
2364 /**************************************************************************
2365 * WsAbortChannel [webservices.@]
2367 HRESULT WINAPI WsAbortChannel( WS_CHANNEL *handle, WS_ERROR *error )
2369 FIXME( "%p %p: stub!\n", handle, error );
2370 return E_NOTIMPL;