webservices: Add asynchronous support for WsReceiveMessage.
[wine.git] / dlls / webservices / channel.c
blob08e04935d340b2116ac6d7415e1922b8d4cc7077
1 /*
2 * Copyright 2016 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "rpc.h"
25 #include "webservices.h"
27 #include "wine/debug.h"
28 #include "wine/list.h"
29 #include "wine/unicode.h"
30 #include "webservices_private.h"
31 #include "sock.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
35 static const struct prop_desc channel_props[] =
37 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE */
38 { sizeof(UINT64), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_MESSAGE_SIZE */
39 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_START_SIZE */
40 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_FLUSH_SIZE */
41 { sizeof(WS_ENCODING), TRUE }, /* WS_CHANNEL_PROPERTY_ENCODING */
42 { sizeof(WS_ENVELOPE_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ENVELOPE_VERSION */
43 { sizeof(WS_ADDRESSING_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ADDRESSING_VERSION */
44 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_SESSION_DICTIONARY_SIZE */
45 { sizeof(WS_CHANNEL_STATE), TRUE }, /* WS_CHANNEL_PROPERTY_STATE */
46 { sizeof(WS_CALLBACK_MODEL), FALSE }, /* WS_CHANNEL_PROPERTY_ASYNC_CALLBACK_MODEL */
47 { sizeof(WS_IP_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_IP_VERSION */
48 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RESOLVE_TIMEOUT */
49 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CONNECT_TIMEOUT */
50 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_TIMEOUT */
51 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_RESPONSE_TIMEOUT */
52 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_TIMEOUT */
53 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CLOSE_TIMEOUT */
54 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_TIMEOUTS */
55 { sizeof(WS_TRANSFER_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_TRANSFER_MODE */
56 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_INTERFACE */
57 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_HOPS */
58 { sizeof(WS_ENDPOINT_ADDRESS), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_ADDRESS */
59 { sizeof(SOCKADDR_STORAGE), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_IP_ADDRESS */
60 { sizeof(ULONGLONG), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_CONNECTION_ID */
61 { sizeof(WS_CUSTOM_CHANNEL_CALLBACKS), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_CALLBACKS */
62 { 0, FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_PARAMETERS */
63 { sizeof(void *), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_INSTANCE */
64 { sizeof(WS_STRING), TRUE }, /* WS_CHANNEL_PROPERTY_TRANSPORT_URL */
65 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_NO_DELAY */
66 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_KEEP_ALIVES */
67 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_TIME */
68 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_INTERVAL */
69 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_HTTP_SERVER_CONNECTIONS */
70 { sizeof(BOOL), TRUE }, /* WS_CHANNEL_PROPERTY_IS_SESSION_SHUT_DOWN */
71 { sizeof(WS_CHANNEL_TYPE), TRUE }, /* WS_CHANNEL_PROPERTY_CHANNEL_TYPE */
72 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_TRIM_BUFFERED_MESSAGE_SIZE */
73 { sizeof(WS_CHANNEL_ENCODER), FALSE }, /* WS_CHANNEL_PROPERTY_ENCODER */
74 { sizeof(WS_CHANNEL_DECODER), FALSE }, /* WS_CHANNEL_PROPERTY_DECODER */
75 { sizeof(WS_PROTECTION_LEVEL), TRUE }, /* WS_CHANNEL_PROPERTY_PROTECTION_LEVEL */
76 { sizeof(WS_COOKIE_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_COOKIE_MODE */
77 { sizeof(WS_HTTP_PROXY_SETTING_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SETTING_MODE */
78 { sizeof(WS_CUSTOM_HTTP_PROXY), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_HTTP_PROXY */
79 { sizeof(WS_HTTP_MESSAGE_MAPPING), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING */
80 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_HTTP_REDIRECT */
81 { sizeof(WS_HTTP_REDIRECT_CALLBACK_CONTEXT), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_REDIRECT_CALLBACK_CONTEXT */
82 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_FAULTS_AS_ERRORS */
83 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ALLOW_UNSECURED_FAULTS */
84 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_SERVER_SPN */
85 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SPN */
86 { sizeof(ULONG), FALSE } /* WS_CHANNEL_PROPERTY_MAX_HTTP_REQUEST_HEADERS_BUFFER_SIZE */
89 struct task
91 struct list entry;
92 void (*proc)( struct task * );
95 struct queue
97 CRITICAL_SECTION cs;
98 HANDLE wait;
99 HANDLE cancel;
100 HANDLE ready;
101 struct list tasks;
104 static struct task *dequeue_task( struct queue *queue )
106 struct task *task;
108 EnterCriticalSection( &queue->cs );
109 TRACE( "%u tasks queued\n", list_count( &queue->tasks ) );
110 task = LIST_ENTRY( list_head( &queue->tasks ), struct task, entry );
111 if (task) list_remove( &task->entry );
112 LeaveCriticalSection( &queue->cs );
114 TRACE( "returning task %p\n", task );
115 return task;
118 static void CALLBACK queue_runner( TP_CALLBACK_INSTANCE *instance, void *ctx )
120 struct queue *queue = ctx;
121 HANDLE handles[] = { queue->wait, queue->cancel };
123 SetEvent( queue->ready );
124 for (;;)
126 DWORD err = WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
127 switch (err)
129 case WAIT_OBJECT_0:
131 struct task *task;
132 while ((task = dequeue_task( queue )))
134 task->proc( task );
135 heap_free( task );
137 break;
139 case WAIT_OBJECT_0 + 1:
140 TRACE( "cancelled\n" );
141 SetEvent( queue->ready );
142 return;
144 default:
145 ERR( "wait failed %u\n", err );
146 return;
151 static HRESULT start_queue( struct queue *queue )
153 HRESULT hr = E_OUTOFMEMORY;
155 if (queue->wait) return S_OK;
156 list_init( &queue->tasks );
157 if (!(queue->wait = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
158 if (!(queue->cancel = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
159 if (!(queue->ready = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
160 if (!TrySubmitThreadpoolCallback( queue_runner, queue, NULL )) hr = HRESULT_FROM_WIN32( GetLastError() );
161 else
163 WaitForSingleObject( queue->ready, INFINITE );
164 return S_OK;
167 error:
168 CloseHandle( queue->wait );
169 CloseHandle( queue->cancel );
170 CloseHandle( queue->ready );
171 return hr;
174 static HRESULT queue_task( struct queue *queue, struct task *task )
176 HRESULT hr;
177 if ((hr = start_queue( queue )) != S_OK) return hr;
179 EnterCriticalSection( &queue->cs );
180 TRACE( "queueing task %p\n", task );
181 list_add_tail( &queue->tasks, &task->entry );
182 LeaveCriticalSection( &queue->cs );
184 SetEvent( queue->wait );
185 return WS_S_ASYNC;
188 enum session_state
190 SESSION_STATE_UNINITIALIZED,
191 SESSION_STATE_SETUP_COMPLETE,
194 struct channel
196 ULONG magic;
197 CRITICAL_SECTION cs;
198 WS_CHANNEL_TYPE type;
199 WS_CHANNEL_BINDING binding;
200 WS_CHANNEL_STATE state;
201 WS_ENDPOINT_ADDRESS addr;
202 WS_XML_WRITER *writer;
203 WS_XML_READER *reader;
204 WS_MESSAGE *msg;
205 WS_ENCODING encoding;
206 enum session_state session_state;
207 struct dictionary dict_send;
208 struct dictionary dict_recv;
209 struct queue send_q;
210 struct queue recv_q;
211 union
213 struct
215 HINTERNET session;
216 HINTERNET connect;
217 HINTERNET request;
218 WCHAR *path;
219 DWORD flags;
220 } http;
221 struct
223 SOCKET socket;
224 } tcp;
225 struct
227 SOCKET socket;
228 } udp;
229 } u;
230 char *read_buf;
231 ULONG read_buflen;
232 ULONG read_size;
233 ULONG prop_count;
234 struct prop prop[sizeof(channel_props)/sizeof(channel_props[0])];
237 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
239 static struct channel *alloc_channel(void)
241 static const ULONG count = sizeof(channel_props)/sizeof(channel_props[0]);
242 struct channel *ret;
243 ULONG size = sizeof(*ret) + prop_size( channel_props, count );
245 if (!(ret = heap_alloc_zero( size ))) return NULL;
247 ret->magic = CHANNEL_MAGIC;
248 InitializeCriticalSection( &ret->cs );
249 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.cs");
251 InitializeCriticalSection( &ret->send_q.cs );
252 ret->send_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.send_q.cs");
254 InitializeCriticalSection( &ret->recv_q.cs );
255 ret->recv_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.recv_q.cs");
257 prop_init( channel_props, count, ret->prop, &ret[1] );
258 ret->prop_count = count;
259 return ret;
262 static void clear_addr( WS_ENDPOINT_ADDRESS *addr )
264 heap_free( addr->url.chars );
265 addr->url.chars = NULL;
266 addr->url.length = 0;
269 static void clear_queue( struct queue *queue )
271 struct list *ptr;
273 SetEvent( queue->cancel );
274 WaitForSingleObject( queue->ready, INFINITE );
276 while ((ptr = list_head( &queue->tasks )))
278 struct task *task = LIST_ENTRY( ptr, struct task, entry );
279 list_remove( &task->entry );
280 heap_free( task );
283 CloseHandle( queue->wait );
284 queue->wait = NULL;
285 CloseHandle( queue->cancel );
286 queue->cancel = NULL;
287 CloseHandle( queue->ready );
288 queue->ready = NULL;
291 static void reset_channel( struct channel *channel )
293 clear_queue( &channel->send_q );
294 clear_queue( &channel->recv_q );
295 channel->state = WS_CHANNEL_STATE_CREATED;
296 channel->session_state = SESSION_STATE_UNINITIALIZED;
297 clear_addr( &channel->addr );
298 clear_dict( &channel->dict_send );
299 clear_dict( &channel->dict_recv );
300 channel->msg = NULL;
301 channel->read_size = 0;
303 switch (channel->binding)
305 case WS_HTTP_CHANNEL_BINDING:
306 WinHttpCloseHandle( channel->u.http.request );
307 channel->u.http.request = NULL;
308 WinHttpCloseHandle( channel->u.http.connect );
309 channel->u.http.connect = NULL;
310 WinHttpCloseHandle( channel->u.http.session );
311 channel->u.http.session = NULL;
312 heap_free( channel->u.http.path );
313 channel->u.http.path = NULL;
314 channel->u.http.flags = 0;
315 break;
317 case WS_TCP_CHANNEL_BINDING:
318 closesocket( channel->u.tcp.socket );
319 channel->u.tcp.socket = -1;
320 break;
322 case WS_UDP_CHANNEL_BINDING:
323 closesocket( channel->u.udp.socket );
324 channel->u.udp.socket = -1;
325 break;
327 default: break;
331 static void free_channel( struct channel *channel )
333 reset_channel( channel );
335 WsFreeWriter( channel->writer );
336 WsFreeReader( channel->reader );
338 heap_free( channel->read_buf );
340 channel->send_q.cs.DebugInfo->Spare[0] = 0;
341 DeleteCriticalSection( &channel->send_q.cs );
343 channel->recv_q.cs.DebugInfo->Spare[0] = 0;
344 DeleteCriticalSection( &channel->recv_q.cs );
346 channel->cs.DebugInfo->Spare[0] = 0;
347 DeleteCriticalSection( &channel->cs );
348 heap_free( channel );
351 static HRESULT create_channel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
352 const WS_CHANNEL_PROPERTY *properties, ULONG count, struct channel **ret )
354 struct channel *channel;
355 ULONG i, msg_size = 65536;
356 WS_ENVELOPE_VERSION env_version = WS_ENVELOPE_VERSION_SOAP_1_2;
357 WS_ADDRESSING_VERSION addr_version = WS_ADDRESSING_VERSION_1_0;
358 HRESULT hr;
360 if (!(channel = alloc_channel())) return E_OUTOFMEMORY;
362 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
363 &msg_size, sizeof(msg_size) );
364 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION,
365 &env_version, sizeof(env_version) );
366 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION,
367 &addr_version, sizeof(addr_version) );
369 channel->type = type;
370 channel->binding = binding;
372 switch (channel->binding)
374 case WS_HTTP_CHANNEL_BINDING:
375 channel->encoding = WS_ENCODING_XML_UTF8;
376 break;
378 case WS_TCP_CHANNEL_BINDING:
379 channel->u.tcp.socket = -1;
380 channel->encoding = WS_ENCODING_XML_BINARY_SESSION_1;
381 break;
383 case WS_UDP_CHANNEL_BINDING:
384 channel->u.udp.socket = -1;
385 channel->encoding = WS_ENCODING_XML_UTF8;
386 break;
388 default: break;
391 for (i = 0; i < count; i++)
393 switch (properties[i].id)
395 case WS_CHANNEL_PROPERTY_ENCODING:
396 if (!properties[i].value || properties[i].valueSize != sizeof(channel->encoding))
398 free_channel( channel );
399 return E_INVALIDARG;
401 channel->encoding = *(WS_ENCODING *)properties[i].value;
402 break;
404 default:
405 if ((hr = prop_set( channel->prop, channel->prop_count, properties[i].id, properties[i].value,
406 properties[i].valueSize )) != S_OK)
408 free_channel( channel );
409 return hr;
411 break;
415 *ret = channel;
416 return S_OK;
419 /**************************************************************************
420 * WsCreateChannel [webservices.@]
422 HRESULT WINAPI WsCreateChannel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
423 const WS_CHANNEL_PROPERTY *properties, ULONG count,
424 const WS_SECURITY_DESCRIPTION *desc, WS_CHANNEL **handle,
425 WS_ERROR *error )
427 struct channel *channel;
428 HRESULT hr;
430 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
431 if (error) FIXME( "ignoring error parameter\n" );
432 if (desc) FIXME( "ignoring security description\n" );
434 if (!handle) return E_INVALIDARG;
436 if (type != WS_CHANNEL_TYPE_REQUEST && type != WS_CHANNEL_TYPE_DUPLEX &&
437 type != WS_CHANNEL_TYPE_DUPLEX_SESSION)
439 FIXME( "channel type %u not implemented\n", type );
440 return E_NOTIMPL;
442 if (binding != WS_HTTP_CHANNEL_BINDING && binding != WS_TCP_CHANNEL_BINDING &&
443 binding != WS_UDP_CHANNEL_BINDING)
445 FIXME( "channel binding %u not implemented\n", binding );
446 return E_NOTIMPL;
449 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
451 TRACE( "created %p\n", channel );
452 *handle = (WS_CHANNEL *)channel;
453 return S_OK;
456 /**************************************************************************
457 * WsCreateChannelForListener [webservices.@]
459 HRESULT WINAPI WsCreateChannelForListener( WS_LISTENER *listener_handle, const WS_CHANNEL_PROPERTY *properties,
460 ULONG count, WS_CHANNEL **handle, WS_ERROR *error )
462 struct channel *channel;
463 WS_CHANNEL_TYPE type;
464 WS_CHANNEL_BINDING binding;
465 HRESULT hr;
467 TRACE( "%p %p %u %p %p\n", listener_handle, properties, count, handle, error );
468 if (error) FIXME( "ignoring error parameter\n" );
470 if (!listener_handle || !handle) return E_INVALIDARG;
472 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_TYPE, &type,
473 sizeof(type), NULL )) != S_OK) return hr;
475 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_BINDING, &binding,
476 sizeof(binding), NULL )) != S_OK) return hr;
478 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
480 TRACE( "created %p\n", channel );
481 *handle = (WS_CHANNEL *)channel;
482 return S_OK;
485 /**************************************************************************
486 * WsFreeChannel [webservices.@]
488 void WINAPI WsFreeChannel( WS_CHANNEL *handle )
490 struct channel *channel = (struct channel *)handle;
492 TRACE( "%p\n", handle );
494 if (!channel) return;
496 EnterCriticalSection( &channel->cs );
498 if (channel->magic != CHANNEL_MAGIC)
500 LeaveCriticalSection( &channel->cs );
501 return;
504 channel->magic = 0;
506 LeaveCriticalSection( &channel->cs );
507 free_channel( channel );
510 /**************************************************************************
511 * WsResetChannel [webservices.@]
513 HRESULT WINAPI WsResetChannel( WS_CHANNEL *handle, WS_ERROR *error )
515 struct channel *channel = (struct channel *)handle;
517 TRACE( "%p %p\n", handle, error );
518 if (error) FIXME( "ignoring error parameter\n" );
520 if (!channel) return E_INVALIDARG;
522 EnterCriticalSection( &channel->cs );
524 if (channel->magic != CHANNEL_MAGIC)
526 LeaveCriticalSection( &channel->cs );
527 return E_INVALIDARG;
530 if (channel->state != WS_CHANNEL_STATE_CREATED && channel->state != WS_CHANNEL_STATE_CLOSED)
532 LeaveCriticalSection( &channel->cs );
533 return WS_E_INVALID_OPERATION;
536 reset_channel( channel );
538 LeaveCriticalSection( &channel->cs );
539 return S_OK;
542 /**************************************************************************
543 * WsGetChannelProperty [webservices.@]
545 HRESULT WINAPI WsGetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, void *buf,
546 ULONG size, WS_ERROR *error )
548 struct channel *channel = (struct channel *)handle;
549 HRESULT hr = S_OK;
551 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
552 if (error) FIXME( "ignoring error parameter\n" );
554 if (!channel) return E_INVALIDARG;
556 EnterCriticalSection( &channel->cs );
558 if (channel->magic != CHANNEL_MAGIC)
560 LeaveCriticalSection( &channel->cs );
561 return E_INVALIDARG;
564 switch (id)
566 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE:
567 if (!buf || size != sizeof(channel->type)) hr = E_INVALIDARG;
568 else *(WS_CHANNEL_TYPE *)buf = channel->type;
569 break;
571 case WS_CHANNEL_PROPERTY_ENCODING:
572 if (!buf || size != sizeof(channel->encoding)) hr = E_INVALIDARG;
573 else *(WS_ENCODING *)buf = channel->encoding;
574 break;
576 default:
577 hr = prop_get( channel->prop, channel->prop_count, id, buf, size );
580 LeaveCriticalSection( &channel->cs );
581 return hr;
584 /**************************************************************************
585 * WsSetChannelProperty [webservices.@]
587 HRESULT WINAPI WsSetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, const void *value,
588 ULONG size, WS_ERROR *error )
590 struct channel *channel = (struct channel *)handle;
591 HRESULT hr;
593 TRACE( "%p %u %p %u\n", handle, id, value, size );
594 if (error) FIXME( "ignoring error parameter\n" );
596 if (!channel) return E_INVALIDARG;
598 EnterCriticalSection( &channel->cs );
600 if (channel->magic != CHANNEL_MAGIC)
602 LeaveCriticalSection( &channel->cs );
603 return E_INVALIDARG;
606 hr = prop_set( channel->prop, channel->prop_count, id, value, size );
608 LeaveCriticalSection( &channel->cs );
609 return hr;
612 static HRESULT open_channel( struct channel *channel, const WS_ENDPOINT_ADDRESS *endpoint )
614 if (endpoint->headers || endpoint->extensions || endpoint->identity)
616 FIXME( "headers, extensions or identity not supported\n" );
617 return E_NOTIMPL;
620 if (!(channel->addr.url.chars = heap_alloc( endpoint->url.length * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
621 memcpy( channel->addr.url.chars, endpoint->url.chars, endpoint->url.length * sizeof(WCHAR) );
622 channel->addr.url.length = endpoint->url.length;
624 channel->state = WS_CHANNEL_STATE_OPEN;
625 return S_OK;
628 /**************************************************************************
629 * WsOpenChannel [webservices.@]
631 HRESULT WINAPI WsOpenChannel( WS_CHANNEL *handle, const WS_ENDPOINT_ADDRESS *endpoint,
632 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
634 struct channel *channel = (struct channel *)handle;
635 HRESULT hr;
637 TRACE( "%p %p %p %p\n", handle, endpoint, ctx, error );
638 if (error) FIXME( "ignoring error parameter\n" );
639 if (ctx) FIXME( "ignoring ctx parameter\n" );
641 if (!channel || !endpoint) return E_INVALIDARG;
643 EnterCriticalSection( &channel->cs );
645 if (channel->magic != CHANNEL_MAGIC)
647 LeaveCriticalSection( &channel->cs );
648 return E_INVALIDARG;
651 if (channel->state != WS_CHANNEL_STATE_CREATED)
653 LeaveCriticalSection( &channel->cs );
654 return WS_E_INVALID_OPERATION;
657 hr = open_channel( channel, endpoint );
659 LeaveCriticalSection( &channel->cs );
660 return hr;
663 static void close_channel( struct channel *channel )
665 reset_channel( channel );
666 channel->state = WS_CHANNEL_STATE_CLOSED;
669 /**************************************************************************
670 * WsCloseChannel [webservices.@]
672 HRESULT WINAPI WsCloseChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
674 struct channel *channel = (struct channel *)handle;
676 TRACE( "%p %p %p\n", handle, ctx, error );
677 if (error) FIXME( "ignoring error parameter\n" );
678 if (ctx) FIXME( "ignoring ctx parameter\n" );
680 if (!channel) return E_INVALIDARG;
682 EnterCriticalSection( &channel->cs );
684 if (channel->magic != CHANNEL_MAGIC)
686 LeaveCriticalSection( &channel->cs );
687 return E_INVALIDARG;
690 close_channel( channel );
692 LeaveCriticalSection( &channel->cs );
693 return S_OK;
696 static HRESULT parse_http_url( const WCHAR *url, ULONG len, URL_COMPONENTS *uc )
698 HRESULT hr = E_OUTOFMEMORY;
699 WCHAR *tmp;
700 DWORD err;
702 memset( uc, 0, sizeof(*uc) );
703 uc->dwStructSize = sizeof(*uc);
704 uc->dwHostNameLength = 128;
705 uc->lpszHostName = heap_alloc( uc->dwHostNameLength * sizeof(WCHAR) );
706 uc->dwUrlPathLength = 128;
707 uc->lpszUrlPath = heap_alloc( uc->dwUrlPathLength * sizeof(WCHAR) );
708 uc->dwExtraInfoLength = 128;
709 uc->lpszExtraInfo = heap_alloc( uc->dwExtraInfoLength * sizeof(WCHAR) );
710 if (!uc->lpszHostName || !uc->lpszUrlPath || !uc->lpszExtraInfo) goto error;
712 if (!WinHttpCrackUrl( url, len, ICU_DECODE, uc ))
714 if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
716 hr = HRESULT_FROM_WIN32( err );
717 goto error;
719 if (!(tmp = heap_realloc( uc->lpszHostName, uc->dwHostNameLength * sizeof(WCHAR) ))) goto error;
720 uc->lpszHostName = tmp;
721 if (!(tmp = heap_realloc( uc->lpszUrlPath, uc->dwUrlPathLength * sizeof(WCHAR) ))) goto error;
722 uc->lpszUrlPath = tmp;
723 if (!(tmp = heap_realloc( uc->lpszExtraInfo, uc->dwExtraInfoLength * sizeof(WCHAR) ))) goto error;
724 uc->lpszExtraInfo = tmp;
725 WinHttpCrackUrl( url, len, ICU_DECODE, uc );
728 return S_OK;
730 error:
731 heap_free( uc->lpszHostName );
732 heap_free( uc->lpszUrlPath );
733 heap_free( uc->lpszExtraInfo );
734 return hr;
737 static HRESULT connect_channel_http( struct channel *channel )
739 static const WCHAR agentW[] =
740 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
741 HINTERNET ses = NULL, con = NULL;
742 URL_COMPONENTS uc;
743 HRESULT hr;
745 if (channel->u.http.connect) return S_OK;
747 if ((hr = parse_http_url( channel->addr.url.chars, channel->addr.url.length, &uc )) != S_OK) return hr;
748 if (!(channel->u.http.path = heap_alloc( (uc.dwUrlPathLength + uc.dwExtraInfoLength + 1) * sizeof(WCHAR) )))
750 hr = E_OUTOFMEMORY;
751 goto done;
753 else
755 strcpyW( channel->u.http.path, uc.lpszUrlPath );
756 if (uc.dwExtraInfoLength) strcatW( channel->u.http.path, uc.lpszExtraInfo );
759 channel->u.http.flags = WINHTTP_FLAG_REFRESH;
760 switch (uc.nScheme)
762 case INTERNET_SCHEME_HTTP: break;
763 case INTERNET_SCHEME_HTTPS:
764 channel->u.http.flags |= WINHTTP_FLAG_SECURE;
765 break;
767 default:
768 hr = WS_E_INVALID_ENDPOINT_URL;
769 goto done;
772 if (!(ses = WinHttpOpen( agentW, 0, NULL, NULL, 0 )))
774 hr = HRESULT_FROM_WIN32( GetLastError() );
775 goto done;
777 if (!(con = WinHttpConnect( ses, uc.lpszHostName, uc.nPort, 0 )))
779 hr = HRESULT_FROM_WIN32( GetLastError() );
780 goto done;
783 channel->u.http.session = ses;
784 channel->u.http.connect = con;
786 done:
787 if (hr != S_OK)
789 WinHttpCloseHandle( con );
790 WinHttpCloseHandle( ses );
792 heap_free( uc.lpszHostName );
793 heap_free( uc.lpszUrlPath );
794 heap_free( uc.lpszExtraInfo );
795 return hr;
798 static HRESULT connect_channel_tcp( struct channel *channel )
800 struct sockaddr_storage storage;
801 struct sockaddr *addr = (struct sockaddr *)&storage;
802 int addr_len;
803 WS_URL_SCHEME_TYPE scheme;
804 WCHAR *host;
805 USHORT port;
806 HRESULT hr;
808 if (channel->u.tcp.socket != -1) return S_OK;
810 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
811 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
813 heap_free( host );
814 return WS_E_INVALID_ENDPOINT_URL;
817 winsock_init();
819 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
820 heap_free( host );
821 if (hr != S_OK) return hr;
823 if ((channel->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
824 return HRESULT_FROM_WIN32( WSAGetLastError() );
826 if (connect( channel->u.tcp.socket, addr, addr_len ) < 0)
828 closesocket( channel->u.tcp.socket );
829 channel->u.tcp.socket = -1;
830 return HRESULT_FROM_WIN32( WSAGetLastError() );
833 return S_OK;
836 static HRESULT connect_channel_udp( struct channel *channel )
838 struct sockaddr_storage storage;
839 struct sockaddr *addr = (struct sockaddr *)&storage;
840 int addr_len;
841 WS_URL_SCHEME_TYPE scheme;
842 WCHAR *host;
843 USHORT port;
844 HRESULT hr;
846 if (channel->u.udp.socket != -1) return S_OK;
848 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
849 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
851 heap_free( host );
852 return WS_E_INVALID_ENDPOINT_URL;
855 winsock_init();
857 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
858 heap_free( host );
859 if (hr != S_OK) return hr;
861 if ((channel->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
862 return HRESULT_FROM_WIN32( WSAGetLastError() );
864 if (connect( channel->u.udp.socket, addr, addr_len ) < 0)
866 closesocket( channel->u.udp.socket );
867 channel->u.udp.socket = -1;
868 return HRESULT_FROM_WIN32( WSAGetLastError() );
871 return S_OK;
874 static HRESULT connect_channel( struct channel *channel )
876 switch (channel->binding)
878 case WS_HTTP_CHANNEL_BINDING:
879 return connect_channel_http( channel );
881 case WS_TCP_CHANNEL_BINDING:
882 return connect_channel_tcp( channel );
884 case WS_UDP_CHANNEL_BINDING:
885 return connect_channel_udp( channel );
887 default:
888 ERR( "unhandled binding %u\n", channel->binding );
889 return E_NOTIMPL;
893 static HRESULT write_message( WS_MESSAGE *handle, WS_XML_WRITER *writer, const WS_ELEMENT_DESCRIPTION *desc,
894 WS_WRITE_OPTION option, const void *body, ULONG size )
896 HRESULT hr;
897 if ((hr = WsWriteEnvelopeStart( handle, writer, NULL, NULL, NULL )) != S_OK) return hr;
898 if ((hr = WsWriteBody( handle, desc, option, body, size, NULL )) != S_OK) return hr;
899 return WsWriteEnvelopeEnd( handle, NULL );
902 static HRESULT send_message_http( HINTERNET request, BYTE *data, ULONG len )
904 if (!WinHttpSendRequest( request, NULL, 0, data, len, len, 0 ))
905 return HRESULT_FROM_WIN32( GetLastError() );
907 if (!WinHttpReceiveResponse( request, NULL ))
908 return HRESULT_FROM_WIN32( GetLastError() );
909 return S_OK;
912 static HRESULT send_byte( SOCKET socket, BYTE byte )
914 int count = send( socket, (char *)&byte, 1, 0 );
915 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
916 if (count != 1) return WS_E_OTHER;
917 return S_OK;
920 static HRESULT send_bytes( SOCKET socket, BYTE *bytes, int len )
922 int count = send( socket, (char *)bytes, len, 0 );
923 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
924 if (count != len) return WS_E_OTHER;
925 return S_OK;
928 static HRESULT send_size( SOCKET socket, ULONG size )
930 HRESULT hr;
931 if (size < 0x80) return send_byte( socket, size );
932 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
933 if ((size >>= 7) < 0x80) return send_byte( socket, size );
934 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
935 if ((size >>= 7) < 0x80) return send_byte( socket, size );
936 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
937 if ((size >>= 7) < 0x80) return send_byte( socket, size );
938 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
939 if ((size >>= 7) < 0x08) return send_byte( socket, size );
940 return E_INVALIDARG;
943 enum frame_record_type
945 FRAME_RECORD_TYPE_VERSION,
946 FRAME_RECORD_TYPE_MODE,
947 FRAME_RECORD_TYPE_VIA,
948 FRAME_RECORD_TYPE_KNOWN_ENCODING,
949 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING,
950 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE,
951 FRAME_RECORD_TYPE_SIZED_ENVELOPE,
952 FRAME_RECORD_TYPE_END,
953 FRAME_RECORD_TYPE_FAULT,
954 FRAME_RECORD_TYPE_UPGRADE_REQUEST,
955 FRAME_RECORD_TYPE_UPGRADE_RESPONSE,
956 FRAME_RECORD_TYPE_PREAMBLE_ACK,
957 FRAME_RECORD_TYPE_PREAMBLE_END,
960 static inline ULONG size_length( ULONG size )
962 if (size < 0x80) return 1;
963 if (size < 0x4000) return 2;
964 if (size < 0x200000) return 3;
965 if (size < 0x10000000) return 4;
966 return 5;
969 static ULONG string_table_size( const WS_XML_DICTIONARY *dict )
971 ULONG i, size = 0;
972 for (i = 0; i < dict->stringCount; i++)
973 size += size_length( dict->strings[i].length ) + dict->strings[i].length;
974 return size;
977 static HRESULT send_string_table( SOCKET socket, const WS_XML_DICTIONARY *dict )
979 ULONG i;
980 HRESULT hr;
981 for (i = 0; i < dict->stringCount; i++)
983 if ((hr = send_size( socket, dict->strings[i].length )) != S_OK) return hr;
984 if ((hr = send_bytes( socket, dict->strings[i].bytes, dict->strings[i].length )) != S_OK) return hr;
986 return S_OK;
989 static HRESULT string_to_utf8( const WS_STRING *str, unsigned char **ret, int *len )
991 *len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
992 if (!(*ret = heap_alloc( *len ))) return E_OUTOFMEMORY;
993 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)*ret, *len, NULL, NULL );
994 return S_OK;
997 enum session_mode
999 SESSION_MODE_INVALID = 0,
1000 SESSION_MODE_SINGLETON = 1,
1001 SESSION_MODE_DUPLEX = 2,
1002 SESSION_MODE_SIMPLEX = 3,
1005 static enum session_mode map_channel_type( struct channel *channel )
1007 switch (channel->type)
1009 case WS_CHANNEL_TYPE_DUPLEX_SESSION: return SESSION_MODE_DUPLEX;
1010 default:
1011 FIXME( "unhandled channel type %08x\n", channel->type );
1012 return SESSION_MODE_INVALID;
1016 enum known_encoding
1018 KNOWN_ENCODING_SOAP11_UTF8 = 0x00,
1019 KNOWN_ENCODING_SOAP11_UTF16 = 0x01,
1020 KNOWN_ENCODING_SOAP11_UTF16LE = 0x02,
1021 KNOWN_ENCODING_SOAP12_UTF8 = 0x03,
1022 KNOWN_ENCODING_SOAP12_UTF16 = 0x04,
1023 KNOWN_ENCODING_SOAP12_UTF16LE = 0x05,
1024 KNOWN_ENCODING_SOAP12_MTOM = 0x06,
1025 KNOWN_ENCODING_SOAP12_BINARY = 0x07,
1026 KNOWN_ENCODING_SOAP12_BINARY_SESSION = 0x08,
1029 static enum known_encoding map_channel_encoding( struct channel *channel )
1031 WS_ENVELOPE_VERSION version;
1033 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version, sizeof(version) );
1035 switch (version)
1037 case WS_ENVELOPE_VERSION_SOAP_1_1:
1038 switch (channel->encoding)
1040 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP11_UTF8;
1041 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP11_UTF16LE;
1042 default:
1043 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1044 return 0;
1046 case WS_ENVELOPE_VERSION_SOAP_1_2:
1047 switch (channel->encoding)
1049 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP12_UTF8;
1050 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP12_UTF16LE;
1051 case WS_ENCODING_XML_BINARY_1: return KNOWN_ENCODING_SOAP12_BINARY;
1052 case WS_ENCODING_XML_BINARY_SESSION_1: return KNOWN_ENCODING_SOAP12_BINARY_SESSION;
1053 default:
1054 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1055 return 0;
1057 default:
1058 ERR( "unhandled version %u\n", version );
1059 return 0;
1063 #define FRAME_VERSION_MAJOR 1
1064 #define FRAME_VERSION_MINOR 1
1066 static HRESULT send_preamble( struct channel *channel )
1068 unsigned char *url;
1069 HRESULT hr;
1070 int len;
1072 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VERSION )) != S_OK) return hr;
1073 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MAJOR )) != S_OK) return hr;
1074 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MINOR )) != S_OK) return hr;
1076 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_MODE )) != S_OK) return hr;
1077 if ((hr = send_byte( channel->u.tcp.socket, map_channel_type(channel) )) != S_OK) return hr;
1079 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VIA )) != S_OK) return hr;
1080 if ((hr = string_to_utf8( &channel->addr.url, &url, &len )) != S_OK) return hr;
1081 if ((hr = send_size( channel->u.tcp.socket, len )) != S_OK) goto done;
1082 if ((hr = send_bytes( channel->u.tcp.socket, url, len )) != S_OK) goto done;
1084 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_KNOWN_ENCODING )) != S_OK) goto done;
1085 if ((hr = send_byte( channel->u.tcp.socket, map_channel_encoding(channel) )) != S_OK) goto done;
1086 hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_END );
1088 done:
1089 heap_free( url );
1090 return hr;
1093 static HRESULT receive_bytes( struct channel *channel, unsigned char *bytes, int len )
1095 int count = recv( channel->u.tcp.socket, (char *)bytes, len, 0 );
1096 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1097 if (count != len) return WS_E_INVALID_FORMAT;
1098 return S_OK;
1101 static HRESULT receive_preamble_ack( struct channel *channel )
1103 unsigned char byte;
1104 HRESULT hr;
1106 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1107 if (byte != FRAME_RECORD_TYPE_PREAMBLE_ACK) return WS_E_INVALID_FORMAT;
1108 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1109 return S_OK;
1112 static HRESULT send_sized_envelope( struct channel *channel, BYTE *data, ULONG len )
1114 ULONG table_size = string_table_size( &channel->dict_send.dict );
1115 HRESULT hr;
1117 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_SIZED_ENVELOPE )) != S_OK) return hr;
1118 if ((hr = send_size( channel->u.tcp.socket, size_length(table_size) + table_size + len )) != S_OK) return hr;
1119 if ((hr = send_size( channel->u.tcp.socket, table_size )) != S_OK) return hr;
1120 if ((hr = send_string_table( channel->u.tcp.socket, &channel->dict_send.dict )) != S_OK) return hr;
1121 return send_bytes( channel->u.tcp.socket, data, len );
1124 static HRESULT open_http_request( struct channel *channel, HINTERNET *req )
1126 static const WCHAR postW[] = {'P','O','S','T',0};
1127 if ((*req = WinHttpOpenRequest( channel->u.http.connect, postW, channel->u.http.path,
1128 NULL, NULL, NULL, channel->u.http.flags ))) return S_OK;
1129 return HRESULT_FROM_WIN32( GetLastError() );
1132 static HRESULT send_message( struct channel *channel, WS_MESSAGE *msg )
1134 WS_XML_WRITER *writer;
1135 WS_BYTES buf;
1136 HRESULT hr;
1138 channel->msg = msg;
1139 if ((hr = connect_channel( channel )) != S_OK) return hr;
1141 WsGetMessageProperty( channel->msg, WS_MESSAGE_PROPERTY_BODY_WRITER, &writer, sizeof(writer), NULL );
1142 WsGetWriterProperty( writer, WS_XML_WRITER_PROPERTY_BYTES, &buf, sizeof(buf), NULL );
1144 switch (channel->binding)
1146 case WS_HTTP_CHANNEL_BINDING:
1147 if (channel->u.http.request)
1149 WinHttpCloseHandle( channel->u.http.request );
1150 channel->u.http.request = NULL;
1152 if ((hr = open_http_request( channel, &channel->u.http.request )) != S_OK) return hr;
1153 if ((hr = message_insert_http_headers( msg, channel->u.http.request )) != S_OK) return hr;
1154 return send_message_http( channel->u.http.request, buf.bytes, buf.length );
1156 case WS_TCP_CHANNEL_BINDING:
1157 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1159 switch (channel->session_state)
1161 case SESSION_STATE_UNINITIALIZED:
1162 if ((hr = send_preamble( channel )) != S_OK) return hr;
1163 if ((hr = receive_preamble_ack( channel )) != S_OK) return hr;
1164 /* fall through */
1166 case SESSION_STATE_SETUP_COMPLETE:
1167 return send_sized_envelope( channel, buf.bytes, buf.length );
1169 default:
1170 ERR( "unhandled session state %u\n", channel->session_state );
1171 return WS_E_OTHER;
1174 return send_bytes( channel->u.tcp.socket, buf.bytes, buf.length );
1176 case WS_UDP_CHANNEL_BINDING:
1177 return send_bytes( channel->u.udp.socket, buf.bytes, buf.length );
1179 default:
1180 ERR( "unhandled binding %u\n", channel->binding );
1181 return E_NOTIMPL;
1185 HRESULT channel_send_message( WS_CHANNEL *handle, WS_MESSAGE *msg )
1187 struct channel *channel = (struct channel *)handle;
1188 HRESULT hr;
1190 EnterCriticalSection( &channel->cs );
1192 if (channel->magic != CHANNEL_MAGIC)
1194 LeaveCriticalSection( &channel->cs );
1195 return E_INVALIDARG;
1198 hr = send_message( channel, msg );
1200 LeaveCriticalSection( &channel->cs );
1201 return hr;
1204 static HRESULT CALLBACK dict_cb( void *state, const WS_XML_STRING *str, BOOL *found, ULONG *id, WS_ERROR *error )
1206 struct dictionary *dict = state;
1207 HRESULT hr = S_OK;
1208 int index;
1210 if ((index = find_string( dict, str->bytes, str->length, id )) == -1 ||
1211 (hr = insert_string( dict, str->bytes, str->length, index, id )) == S_OK)
1213 *found = TRUE;
1214 return S_OK;
1216 *found = FALSE;
1217 return hr;
1220 static HRESULT init_writer( struct channel *channel )
1222 WS_XML_WRITER_BUFFER_OUTPUT buf = {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER}};
1223 WS_XML_WRITER_TEXT_ENCODING text = {{WS_XML_WRITER_ENCODING_TYPE_TEXT}};
1224 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1225 WS_XML_WRITER_ENCODING *encoding;
1226 HRESULT hr;
1228 if (!channel->writer && (hr = WsCreateWriter( NULL, 0, &channel->writer, NULL )) != S_OK) return hr;
1230 switch (channel->encoding)
1232 case WS_ENCODING_XML_UTF8:
1233 text.charSet = WS_CHARSET_UTF8;
1234 encoding = &text.encoding;
1235 break;
1237 case WS_ENCODING_XML_BINARY_SESSION_1:
1238 if ((hr = writer_enable_lookup( channel->writer )) != S_OK) return hr;
1239 clear_dict( &channel->dict_send );
1240 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1241 bin.dynamicStringCallback = dict_cb;
1242 bin.dynamicStringCallbackState = &channel->dict_send;
1243 encoding = &bin.encoding;
1244 break;
1246 case WS_ENCODING_XML_BINARY_1:
1247 encoding = &bin.encoding;
1248 break;
1250 default:
1251 FIXME( "unhandled encoding %u\n", channel->encoding );
1252 return WS_E_NOT_SUPPORTED;
1255 return WsSetOutput( channel->writer, encoding, &buf.output, NULL, 0, NULL );
1258 /**************************************************************************
1259 * WsSendMessage [webservices.@]
1261 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1262 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1263 WS_ERROR *error )
1265 struct channel *channel = (struct channel *)handle;
1266 HRESULT hr;
1268 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1269 if (error) FIXME( "ignoring error parameter\n" );
1270 if (ctx) FIXME( "ignoring ctx parameter\n" );
1272 if (!channel || !msg || !desc) return E_INVALIDARG;
1274 EnterCriticalSection( &channel->cs );
1276 if (channel->magic != CHANNEL_MAGIC)
1278 LeaveCriticalSection( &channel->cs );
1279 return E_INVALIDARG;
1282 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK) goto done;
1283 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1284 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1286 if ((hr = init_writer( channel )) != S_OK) goto done;
1287 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1288 goto done;
1289 hr = send_message( channel, msg );
1291 done:
1292 LeaveCriticalSection( &channel->cs );
1293 return hr;
1296 /**************************************************************************
1297 * WsSendReplyMessage [webservices.@]
1299 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1300 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1301 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1303 struct channel *channel = (struct channel *)handle;
1304 GUID req_id;
1305 HRESULT hr;
1307 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1308 if (error) FIXME( "ignoring error parameter\n" );
1309 if (ctx) FIXME( "ignoring ctx parameter\n" );
1311 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1313 EnterCriticalSection( &channel->cs );
1315 if (channel->magic != CHANNEL_MAGIC)
1317 LeaveCriticalSection( &channel->cs );
1318 return E_INVALIDARG;
1321 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1322 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1323 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1324 if ((hr = message_get_id( request, &req_id )) != S_OK) goto done;
1325 if ((hr = message_set_request_id( msg, &req_id )) != S_OK) goto done;
1327 if ((hr = init_writer( channel )) != S_OK) goto done;
1328 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1329 goto done;
1330 hr = send_message( channel, msg );
1332 done:
1333 LeaveCriticalSection( &channel->cs );
1334 FIXME( "returning %08x\n", hr );
1335 return hr;
1338 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1340 if (!channel->read_buf)
1342 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1343 channel->read_buflen = size;
1344 return S_OK;
1346 if (channel->read_buflen < size)
1348 char *tmp;
1349 ULONG new_size = max( size, channel->read_buflen * 2 );
1350 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1351 channel->read_buf = tmp;
1352 channel->read_buflen = new_size;
1354 return S_OK;
1357 static HRESULT init_reader( struct channel *channel )
1359 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1360 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1361 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1362 WS_XML_READER_ENCODING *encoding;
1363 HRESULT hr;
1365 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1367 switch (channel->encoding)
1369 case WS_ENCODING_XML_UTF8:
1370 text.charSet = WS_CHARSET_UTF8;
1371 encoding = &text.encoding;
1372 break;
1374 case WS_ENCODING_XML_BINARY_SESSION_1:
1375 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1376 bin.dynamicDictionary = &channel->dict_recv.dict;
1377 /* fall through */
1379 case WS_ENCODING_XML_BINARY_1:
1380 encoding = &bin.encoding;
1381 break;
1383 default:
1384 FIXME( "unhandled encoding %u\n", channel->encoding );
1385 return WS_E_NOT_SUPPORTED;
1388 buf.encodedData = channel->read_buf;
1389 buf.encodedDataSize = channel->read_size;
1390 return WsSetInput( channel->reader, encoding, &buf.input, NULL, 0, NULL );
1393 #define INITIAL_READ_BUFFER_SIZE 4096
1394 static HRESULT receive_message_http( struct channel *channel )
1396 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1397 ULONG max_len;
1398 HRESULT hr;
1400 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1401 &max_len, sizeof(max_len) );
1403 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1404 channel->read_size = 0;
1405 for (;;)
1407 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1409 return HRESULT_FROM_WIN32( GetLastError() );
1411 if (!len) break;
1412 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1413 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1415 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1417 return HRESULT_FROM_WIN32( GetLastError() );
1419 if (!bytes_read) break;
1420 channel->read_size += bytes_read;
1421 offset += bytes_read;
1424 return init_reader( channel );
1427 static HRESULT receive_message_unsized( struct channel *channel, SOCKET socket )
1429 int bytes_read;
1430 ULONG max_len;
1431 HRESULT hr;
1433 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1434 &max_len, sizeof(max_len) );
1436 if ((hr = resize_read_buffer( channel, max_len )) != S_OK) return hr;
1438 channel->read_size = 0;
1439 if ((bytes_read = recv( socket, channel->read_buf, max_len, 0 )) < 0)
1441 return HRESULT_FROM_WIN32( WSAGetLastError() );
1443 channel->read_size = bytes_read;
1444 return S_OK;
1447 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1449 unsigned int offset = 0, to_read = size;
1450 int bytes_read;
1451 HRESULT hr;
1453 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1455 channel->read_size = 0;
1456 while (channel->read_size < size)
1458 if ((bytes_read = recv( channel->u.tcp.socket, channel->read_buf + offset, to_read, 0 )) < 0)
1460 return HRESULT_FROM_WIN32( WSAGetLastError() );
1462 if (!bytes_read) break;
1463 channel->read_size += bytes_read;
1464 to_read -= bytes_read;
1465 offset += bytes_read;
1467 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1468 return S_OK;
1471 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1473 unsigned char byte;
1474 HRESULT hr;
1476 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1477 *size = byte & 0x7f;
1478 if (!(byte & 0x80)) return S_OK;
1480 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1481 *size += (byte & 0x7f) << 7;
1482 if (!(byte & 0x80)) return S_OK;
1484 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1485 *size += (byte & 0x7f) << 14;
1486 if (!(byte & 0x80)) return S_OK;
1488 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1489 *size += (byte & 0x7f) << 21;
1490 if (!(byte & 0x80)) return S_OK;
1492 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1493 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1494 *size += byte << 28;
1495 return S_OK;
1498 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1500 switch (encoding)
1502 case KNOWN_ENCODING_SOAP11_UTF8:
1503 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1504 case KNOWN_ENCODING_SOAP11_UTF16:
1505 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1506 case KNOWN_ENCODING_SOAP11_UTF16LE:
1507 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1508 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1509 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1510 default:
1511 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1512 return WS_ENCODING_XML_UTF8;
1516 static HRESULT receive_preamble( struct channel *channel )
1518 unsigned char type;
1519 HRESULT hr;
1521 for (;;)
1523 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1524 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1525 switch (type)
1527 case FRAME_RECORD_TYPE_VERSION:
1529 unsigned char major, minor;
1530 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1531 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1532 TRACE( "major %u minor %u\n", major, major );
1533 break;
1535 case FRAME_RECORD_TYPE_MODE:
1537 unsigned char mode;
1538 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1539 TRACE( "mode %u\n", mode );
1540 break;
1542 case FRAME_RECORD_TYPE_VIA:
1544 unsigned int size;
1545 unsigned char *url;
1547 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1548 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1549 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1551 heap_free( url );
1552 return hr;
1554 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1555 heap_free( url ); /* FIXME: verify */
1556 break;
1558 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1560 unsigned char encoding;
1561 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1562 TRACE( "encoding %u\n", encoding );
1563 channel->encoding = map_known_encoding( encoding );
1564 break;
1566 default:
1567 WARN( "unhandled record type %u\n", type );
1568 return WS_E_INVALID_FORMAT;
1572 return S_OK;
1575 static HRESULT receive_sized_envelope( struct channel *channel )
1577 unsigned char type;
1578 unsigned int size;
1579 HRESULT hr;
1581 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1582 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1583 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1584 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1585 return S_OK;
1588 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1590 const BYTE *buf = *ptr;
1592 if (len < 1) return WS_E_INVALID_FORMAT;
1593 *size = buf[0] & 0x7f;
1594 if (!(buf[0] & 0x80))
1596 *ptr += 1;
1597 return S_OK;
1599 if (len < 2) return WS_E_INVALID_FORMAT;
1600 *size += (buf[1] & 0x7f) << 7;
1601 if (!(buf[1] & 0x80))
1603 *ptr += 2;
1604 return S_OK;
1606 if (len < 3) return WS_E_INVALID_FORMAT;
1607 *size += (buf[2] & 0x7f) << 14;
1608 if (!(buf[2] & 0x80))
1610 *ptr += 3;
1611 return S_OK;
1613 if (len < 4) return WS_E_INVALID_FORMAT;
1614 *size += (buf[3] & 0x7f) << 21;
1615 if (!(buf[3] & 0x80))
1617 *ptr += 4;
1618 return S_OK;
1620 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1621 *size += buf[4] << 28;
1622 *ptr += 5;
1623 return S_OK;
1626 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1628 ULONG size, strings_size, strings_offset;
1629 const BYTE *ptr = buf;
1630 BYTE *bytes;
1631 int index;
1632 HRESULT hr;
1634 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1635 strings_offset = ptr - buf;
1636 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1637 *used = strings_offset + strings_size;
1638 if (!strings_size) return S_OK;
1640 UuidCreate( &dict->dict.guid );
1641 dict->dict.isConst = FALSE;
1643 buflen -= strings_offset;
1644 ptr = buf + strings_offset;
1645 while (ptr < buf + strings_size)
1647 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1649 clear_dict( dict );
1650 return hr;
1652 if (size > buflen)
1654 clear_dict( dict );
1655 return WS_E_INVALID_FORMAT;
1657 buflen -= size;
1658 if (!(bytes = heap_alloc( size )))
1660 hr = E_OUTOFMEMORY;
1661 goto error;
1663 memcpy( bytes, ptr, size );
1664 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1666 heap_free( bytes );
1667 ptr += size;
1668 continue;
1670 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1672 clear_dict( dict );
1673 return hr;
1675 ptr += size;
1677 return S_OK;
1679 error:
1680 clear_dict( dict );
1681 return hr;
1684 static HRESULT send_preamble_ack( struct channel *channel )
1686 HRESULT hr;
1687 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1688 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1689 return S_OK;
1692 static HRESULT receive_message_session( struct channel *channel )
1694 HRESULT hr;
1696 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1697 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1699 ULONG size;
1700 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict_recv,
1701 &size )) != S_OK) return hr;
1702 channel->read_size -= size;
1703 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1706 return init_reader( channel );
1709 static HRESULT receive_message_sock( struct channel *channel, SOCKET socket )
1711 HRESULT hr;
1712 if ((hr = receive_message_unsized( channel, socket )) != S_OK) return hr;
1713 return init_reader( channel );
1716 static HRESULT receive_message_bytes( struct channel *channel )
1718 HRESULT hr;
1719 if ((hr = connect_channel( channel )) != S_OK) return hr;
1721 switch (channel->binding)
1723 case WS_HTTP_CHANNEL_BINDING:
1724 return receive_message_http( channel );
1726 case WS_TCP_CHANNEL_BINDING:
1727 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1729 switch (channel->session_state)
1731 case SESSION_STATE_UNINITIALIZED:
1732 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1733 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1734 /* fall through */
1736 case SESSION_STATE_SETUP_COMPLETE:
1737 return receive_message_session( channel );
1739 default:
1740 ERR( "unhandled session state %u\n", channel->session_state );
1741 return WS_E_OTHER;
1744 return receive_message_sock( channel, channel->u.tcp.socket );
1746 case WS_UDP_CHANNEL_BINDING:
1747 return receive_message_sock( channel, channel->u.udp.socket );
1749 default:
1750 ERR( "unhandled binding %u\n", channel->binding );
1751 return E_NOTIMPL;
1755 HRESULT channel_receive_message( WS_CHANNEL *handle )
1757 struct channel *channel = (struct channel *)handle;
1758 HRESULT hr;
1760 EnterCriticalSection( &channel->cs );
1762 if (channel->magic != CHANNEL_MAGIC)
1764 LeaveCriticalSection( &channel->cs );
1765 return E_INVALIDARG;
1768 hr = receive_message_bytes( channel );
1770 LeaveCriticalSection( &channel->cs );
1771 return hr;
1774 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1776 struct channel *channel = (struct channel *)handle;
1778 EnterCriticalSection( &channel->cs );
1780 if (channel->magic != CHANNEL_MAGIC)
1782 LeaveCriticalSection( &channel->cs );
1783 return E_INVALIDARG;
1786 *reader = channel->reader;
1788 LeaveCriticalSection( &channel->cs );
1789 return S_OK;
1792 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1793 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1795 HRESULT hr;
1796 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1797 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1798 return WsReadEnvelopeEnd( handle, NULL );
1801 static HRESULT receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1802 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1803 void *value, ULONG size, ULONG *index )
1805 HRESULT hr;
1806 ULONG i;
1808 if ((hr = receive_message_bytes( channel )) != S_OK) return hr;
1809 for (i = 0; i < count; i++)
1811 const WS_ELEMENT_DESCRIPTION *body = desc[i]->bodyElementDescription;
1812 if ((hr = read_message( msg, channel->reader, body, read_option, heap, value, size )) == S_OK)
1814 if (index) *index = i;
1815 break;
1817 if ((hr = WsResetMessage( msg, NULL )) != S_OK) return hr;
1818 if ((hr = init_reader( channel )) != S_OK) return hr;
1820 return (i == count) ? WS_E_INVALID_FORMAT : S_OK;
1823 struct receive_message
1825 struct task task;
1826 struct channel *channel;
1827 WS_MESSAGE *msg;
1828 const WS_MESSAGE_DESCRIPTION **desc;
1829 ULONG count;
1830 WS_RECEIVE_OPTION option;
1831 WS_READ_OPTION read_option;
1832 WS_HEAP *heap;
1833 void *value;
1834 ULONG size;
1835 ULONG *index;
1836 const WS_ASYNC_CONTEXT *ctx;
1839 static void receive_message_proc( struct task *task )
1841 struct receive_message *r = (struct receive_message *)task;
1842 HRESULT hr;
1844 hr = receive_message( r->channel, r->msg, r->desc, r->count, r->option, r->read_option, r->heap, r->value,
1845 r->size, r->index );
1847 TRACE( "calling %p(%08x)\n", r->ctx->callback, hr );
1848 r->ctx->callback( hr, WS_LONG_CALLBACK, r->ctx->callbackState );
1849 TRACE( "%p returned\n", r->ctx->callback );
1852 static HRESULT queue_receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1853 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option,
1854 WS_HEAP *heap, void *value, ULONG size, ULONG *index,
1855 const WS_ASYNC_CONTEXT *ctx )
1857 struct receive_message *r;
1859 if (!(r = heap_alloc( sizeof(*r) ))) return E_OUTOFMEMORY;
1860 r->task.proc = receive_message_proc;
1861 r->channel = channel;
1862 r->msg = msg;
1863 r->desc = desc;
1864 r->count = count;
1865 r->option = option;
1866 r->read_option = read_option;
1867 r->heap = heap;
1868 r->value = value;
1869 r->size = size;
1870 r->index = index;
1871 r->ctx = ctx;
1872 return queue_task( &channel->recv_q, &r->task );
1875 /**************************************************************************
1876 * WsReceiveMessage [webservices.@]
1878 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1879 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1880 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1882 struct channel *channel = (struct channel *)handle;
1883 HRESULT hr;
1885 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1886 value, size, index, ctx, error );
1887 if (error) FIXME( "ignoring error parameter\n" );
1889 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1891 EnterCriticalSection( &channel->cs );
1893 if (channel->magic != CHANNEL_MAGIC)
1895 LeaveCriticalSection( &channel->cs );
1896 return E_INVALIDARG;
1899 if (ctx)
1900 hr = queue_receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index, ctx );
1901 else
1902 hr = receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index );
1904 LeaveCriticalSection( &channel->cs );
1905 return hr;
1908 /**************************************************************************
1909 * WsReadMessageStart [webservices.@]
1911 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1912 WS_ERROR *error )
1914 struct channel *channel = (struct channel *)handle;
1915 HRESULT hr;
1917 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1918 if (error) FIXME( "ignoring error parameter\n" );
1919 if (ctx) FIXME( "ignoring ctx parameter\n" );
1921 if (!channel || !msg) return E_INVALIDARG;
1923 EnterCriticalSection( &channel->cs );
1925 if (channel->magic != CHANNEL_MAGIC)
1927 LeaveCriticalSection( &channel->cs );
1928 return E_INVALIDARG;
1931 if ((hr = receive_message_bytes( channel )) == S_OK)
1933 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
1936 LeaveCriticalSection( &channel->cs );
1937 return hr;
1940 /**************************************************************************
1941 * WsReadMessageEnd [webservices.@]
1943 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1944 WS_ERROR *error )
1946 struct channel *channel = (struct channel *)handle;
1947 HRESULT hr;
1949 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1950 if (error) FIXME( "ignoring error parameter\n" );
1951 if (ctx) FIXME( "ignoring ctx parameter\n" );
1953 if (!channel || !msg) return E_INVALIDARG;
1955 EnterCriticalSection( &channel->cs );
1957 if (channel->magic != CHANNEL_MAGIC)
1959 LeaveCriticalSection( &channel->cs );
1960 return E_INVALIDARG;
1963 hr = WsReadEnvelopeEnd( msg, NULL );
1965 LeaveCriticalSection( &channel->cs );
1966 return hr;
1969 /**************************************************************************
1970 * WsWriteMessageStart [webservices.@]
1972 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1973 WS_ERROR *error )
1975 struct channel *channel = (struct channel *)handle;
1976 HRESULT hr;
1978 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1979 if (error) FIXME( "ignoring error parameter\n" );
1980 if (ctx) FIXME( "ignoring ctx parameter\n" );
1982 if (!channel || !msg) return E_INVALIDARG;
1984 EnterCriticalSection( &channel->cs );
1986 if (channel->magic != CHANNEL_MAGIC)
1988 LeaveCriticalSection( &channel->cs );
1989 return E_INVALIDARG;
1992 if ((hr = init_writer( channel )) != S_OK) goto done;
1993 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1994 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
1996 done:
1997 LeaveCriticalSection( &channel->cs );
1998 return hr;
2001 /**************************************************************************
2002 * WsWriteMessageEnd [webservices.@]
2004 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2005 WS_ERROR *error )
2007 struct channel *channel = (struct channel *)handle;
2008 HRESULT hr;
2010 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2011 if (error) FIXME( "ignoring error parameter\n" );
2012 if (ctx) FIXME( "ignoring ctx parameter\n" );
2014 if (!channel || !msg) return E_INVALIDARG;
2016 EnterCriticalSection( &channel->cs );
2018 if (channel->magic != CHANNEL_MAGIC)
2020 LeaveCriticalSection( &channel->cs );
2021 return E_INVALIDARG;
2024 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK) hr = send_message( channel, msg );
2026 LeaveCriticalSection( &channel->cs );
2027 return hr;
2030 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
2032 HANDLE handles[] = { wait, cancel };
2033 ULONG nonblocking = 0;
2034 HRESULT hr = S_OK;
2036 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2038 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2040 case 0:
2041 if ((*ret = accept( socket, NULL, NULL )) != -1)
2043 WSAEventSelect( *ret, NULL, 0 );
2044 ioctlsocket( *ret, FIONBIO, &nonblocking );
2045 break;
2047 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2048 break;
2050 case 1:
2051 hr = WS_E_OPERATION_ABORTED;
2052 break;
2054 default:
2055 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2056 break;
2059 return hr;
2062 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2064 struct channel *channel = (struct channel *)handle;
2065 HRESULT hr;
2067 EnterCriticalSection( &channel->cs );
2069 if (channel->magic != CHANNEL_MAGIC)
2071 LeaveCriticalSection( &channel->cs );
2072 return E_INVALIDARG;
2075 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
2077 LeaveCriticalSection( &channel->cs );
2078 return hr;
2081 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
2083 HANDLE handles[] = { wait, cancel };
2084 ULONG nonblocking = 0;
2085 HRESULT hr;
2087 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2089 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2091 case 0:
2092 hr = S_OK;
2093 break;
2095 case 1:
2096 hr = WS_E_OPERATION_ABORTED;
2097 break;
2099 default:
2100 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2101 break;
2104 WSAEventSelect( socket, NULL, 0 );
2105 ioctlsocket( socket, FIONBIO, &nonblocking );
2106 return hr;
2109 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2111 struct channel *channel = (struct channel *)handle;
2112 HRESULT hr;
2114 EnterCriticalSection( &channel->cs );
2116 if (channel->magic != CHANNEL_MAGIC)
2118 LeaveCriticalSection( &channel->cs );
2119 return E_INVALIDARG;
2122 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
2124 LeaveCriticalSection( &channel->cs );
2125 return hr;