ddraw/tests: Rewrite LimitTest().
[wine.git] / dlls / webservices / channel.c
blob0765e0dc6bc3d4cabe6405ca9abe36130b6da769
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 queue->wait = NULL;
170 CloseHandle( queue->cancel );
171 queue->cancel = NULL;
172 CloseHandle( queue->ready );
173 queue->ready = NULL;
174 return hr;
177 static HRESULT queue_task( struct queue *queue, struct task *task )
179 HRESULT hr;
180 if ((hr = start_queue( queue )) != S_OK) return hr;
182 EnterCriticalSection( &queue->cs );
183 TRACE( "queueing task %p\n", task );
184 list_add_tail( &queue->tasks, &task->entry );
185 LeaveCriticalSection( &queue->cs );
187 SetEvent( queue->wait );
188 return WS_S_ASYNC;
191 enum session_state
193 SESSION_STATE_UNINITIALIZED,
194 SESSION_STATE_SETUP_COMPLETE,
197 struct channel
199 ULONG magic;
200 CRITICAL_SECTION cs;
201 WS_CHANNEL_TYPE type;
202 WS_CHANNEL_BINDING binding;
203 WS_CHANNEL_STATE state;
204 WS_ENDPOINT_ADDRESS addr;
205 WS_XML_WRITER *writer;
206 WS_XML_READER *reader;
207 WS_MESSAGE *msg;
208 WS_ENCODING encoding;
209 enum session_state session_state;
210 struct dictionary dict_send;
211 struct dictionary dict_recv;
212 struct queue send_q;
213 struct queue recv_q;
214 union
216 struct
218 HINTERNET session;
219 HINTERNET connect;
220 HINTERNET request;
221 WCHAR *path;
222 DWORD flags;
223 } http;
224 struct
226 SOCKET socket;
227 } tcp;
228 struct
230 SOCKET socket;
231 } udp;
232 } u;
233 char *read_buf;
234 ULONG read_buflen;
235 ULONG read_size;
236 ULONG prop_count;
237 struct prop prop[sizeof(channel_props)/sizeof(channel_props[0])];
240 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
242 static struct channel *alloc_channel(void)
244 static const ULONG count = sizeof(channel_props)/sizeof(channel_props[0]);
245 struct channel *ret;
246 ULONG size = sizeof(*ret) + prop_size( channel_props, count );
248 if (!(ret = heap_alloc_zero( size ))) return NULL;
250 ret->magic = CHANNEL_MAGIC;
251 InitializeCriticalSection( &ret->cs );
252 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.cs");
254 InitializeCriticalSection( &ret->send_q.cs );
255 ret->send_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.send_q.cs");
257 InitializeCriticalSection( &ret->recv_q.cs );
258 ret->recv_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.recv_q.cs");
260 prop_init( channel_props, count, ret->prop, &ret[1] );
261 ret->prop_count = count;
262 return ret;
265 static void clear_addr( WS_ENDPOINT_ADDRESS *addr )
267 heap_free( addr->url.chars );
268 addr->url.chars = NULL;
269 addr->url.length = 0;
272 static void clear_queue( struct queue *queue )
274 struct list *ptr;
276 SetEvent( queue->cancel );
277 WaitForSingleObject( queue->ready, INFINITE );
279 while ((ptr = list_head( &queue->tasks )))
281 struct task *task = LIST_ENTRY( ptr, struct task, entry );
282 list_remove( &task->entry );
283 heap_free( task );
286 CloseHandle( queue->wait );
287 queue->wait = NULL;
288 CloseHandle( queue->cancel );
289 queue->cancel = NULL;
290 CloseHandle( queue->ready );
291 queue->ready = NULL;
294 static void reset_channel( struct channel *channel )
296 clear_queue( &channel->send_q );
297 clear_queue( &channel->recv_q );
298 channel->state = WS_CHANNEL_STATE_CREATED;
299 channel->session_state = SESSION_STATE_UNINITIALIZED;
300 clear_addr( &channel->addr );
301 clear_dict( &channel->dict_send );
302 clear_dict( &channel->dict_recv );
303 channel->msg = NULL;
304 channel->read_size = 0;
306 switch (channel->binding)
308 case WS_HTTP_CHANNEL_BINDING:
309 WinHttpCloseHandle( channel->u.http.request );
310 channel->u.http.request = NULL;
311 WinHttpCloseHandle( channel->u.http.connect );
312 channel->u.http.connect = NULL;
313 WinHttpCloseHandle( channel->u.http.session );
314 channel->u.http.session = NULL;
315 heap_free( channel->u.http.path );
316 channel->u.http.path = NULL;
317 channel->u.http.flags = 0;
318 break;
320 case WS_TCP_CHANNEL_BINDING:
321 closesocket( channel->u.tcp.socket );
322 channel->u.tcp.socket = -1;
323 break;
325 case WS_UDP_CHANNEL_BINDING:
326 closesocket( channel->u.udp.socket );
327 channel->u.udp.socket = -1;
328 break;
330 default: break;
334 static void free_channel( struct channel *channel )
336 reset_channel( channel );
338 WsFreeWriter( channel->writer );
339 WsFreeReader( channel->reader );
341 heap_free( channel->read_buf );
343 channel->send_q.cs.DebugInfo->Spare[0] = 0;
344 DeleteCriticalSection( &channel->send_q.cs );
346 channel->recv_q.cs.DebugInfo->Spare[0] = 0;
347 DeleteCriticalSection( &channel->recv_q.cs );
349 channel->cs.DebugInfo->Spare[0] = 0;
350 DeleteCriticalSection( &channel->cs );
351 heap_free( channel );
354 static HRESULT create_channel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
355 const WS_CHANNEL_PROPERTY *properties, ULONG count, struct channel **ret )
357 struct channel *channel;
358 ULONG i, msg_size = 65536;
359 WS_ENVELOPE_VERSION env_version = WS_ENVELOPE_VERSION_SOAP_1_2;
360 WS_ADDRESSING_VERSION addr_version = WS_ADDRESSING_VERSION_1_0;
361 HRESULT hr;
363 if (!(channel = alloc_channel())) return E_OUTOFMEMORY;
365 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
366 &msg_size, sizeof(msg_size) );
367 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION,
368 &env_version, sizeof(env_version) );
369 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION,
370 &addr_version, sizeof(addr_version) );
372 channel->type = type;
373 channel->binding = binding;
375 switch (channel->binding)
377 case WS_HTTP_CHANNEL_BINDING:
378 channel->encoding = WS_ENCODING_XML_UTF8;
379 break;
381 case WS_TCP_CHANNEL_BINDING:
382 channel->u.tcp.socket = -1;
383 channel->encoding = WS_ENCODING_XML_BINARY_SESSION_1;
384 break;
386 case WS_UDP_CHANNEL_BINDING:
387 channel->u.udp.socket = -1;
388 channel->encoding = WS_ENCODING_XML_UTF8;
389 break;
391 default: break;
394 for (i = 0; i < count; i++)
396 switch (properties[i].id)
398 case WS_CHANNEL_PROPERTY_ENCODING:
399 if (!properties[i].value || properties[i].valueSize != sizeof(channel->encoding))
401 free_channel( channel );
402 return E_INVALIDARG;
404 channel->encoding = *(WS_ENCODING *)properties[i].value;
405 break;
407 default:
408 if ((hr = prop_set( channel->prop, channel->prop_count, properties[i].id, properties[i].value,
409 properties[i].valueSize )) != S_OK)
411 free_channel( channel );
412 return hr;
414 break;
418 *ret = channel;
419 return S_OK;
422 /**************************************************************************
423 * WsCreateChannel [webservices.@]
425 HRESULT WINAPI WsCreateChannel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
426 const WS_CHANNEL_PROPERTY *properties, ULONG count,
427 const WS_SECURITY_DESCRIPTION *desc, WS_CHANNEL **handle,
428 WS_ERROR *error )
430 struct channel *channel;
431 HRESULT hr;
433 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
434 if (error) FIXME( "ignoring error parameter\n" );
435 if (desc) FIXME( "ignoring security description\n" );
437 if (!handle) return E_INVALIDARG;
439 if (type != WS_CHANNEL_TYPE_REQUEST && type != WS_CHANNEL_TYPE_DUPLEX &&
440 type != WS_CHANNEL_TYPE_DUPLEX_SESSION)
442 FIXME( "channel type %u not implemented\n", type );
443 return E_NOTIMPL;
445 if (binding != WS_HTTP_CHANNEL_BINDING && binding != WS_TCP_CHANNEL_BINDING &&
446 binding != WS_UDP_CHANNEL_BINDING)
448 FIXME( "channel binding %u not implemented\n", binding );
449 return E_NOTIMPL;
452 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
454 TRACE( "created %p\n", channel );
455 *handle = (WS_CHANNEL *)channel;
456 return S_OK;
459 /**************************************************************************
460 * WsCreateChannelForListener [webservices.@]
462 HRESULT WINAPI WsCreateChannelForListener( WS_LISTENER *listener_handle, const WS_CHANNEL_PROPERTY *properties,
463 ULONG count, WS_CHANNEL **handle, WS_ERROR *error )
465 struct channel *channel;
466 WS_CHANNEL_TYPE type;
467 WS_CHANNEL_BINDING binding;
468 HRESULT hr;
470 TRACE( "%p %p %u %p %p\n", listener_handle, properties, count, handle, error );
471 if (error) FIXME( "ignoring error parameter\n" );
473 if (!listener_handle || !handle) return E_INVALIDARG;
475 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_TYPE, &type,
476 sizeof(type), NULL )) != S_OK) return hr;
478 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_BINDING, &binding,
479 sizeof(binding), NULL )) != S_OK) return hr;
481 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
483 TRACE( "created %p\n", channel );
484 *handle = (WS_CHANNEL *)channel;
485 return S_OK;
488 /**************************************************************************
489 * WsFreeChannel [webservices.@]
491 void WINAPI WsFreeChannel( WS_CHANNEL *handle )
493 struct channel *channel = (struct channel *)handle;
495 TRACE( "%p\n", handle );
497 if (!channel) return;
499 EnterCriticalSection( &channel->cs );
501 if (channel->magic != CHANNEL_MAGIC)
503 LeaveCriticalSection( &channel->cs );
504 return;
507 channel->magic = 0;
509 LeaveCriticalSection( &channel->cs );
510 free_channel( channel );
513 /**************************************************************************
514 * WsResetChannel [webservices.@]
516 HRESULT WINAPI WsResetChannel( WS_CHANNEL *handle, WS_ERROR *error )
518 struct channel *channel = (struct channel *)handle;
520 TRACE( "%p %p\n", handle, error );
521 if (error) FIXME( "ignoring error parameter\n" );
523 if (!channel) return E_INVALIDARG;
525 EnterCriticalSection( &channel->cs );
527 if (channel->magic != CHANNEL_MAGIC)
529 LeaveCriticalSection( &channel->cs );
530 return E_INVALIDARG;
533 if (channel->state != WS_CHANNEL_STATE_CREATED && channel->state != WS_CHANNEL_STATE_CLOSED)
535 LeaveCriticalSection( &channel->cs );
536 return WS_E_INVALID_OPERATION;
539 reset_channel( channel );
541 LeaveCriticalSection( &channel->cs );
542 return S_OK;
545 /**************************************************************************
546 * WsGetChannelProperty [webservices.@]
548 HRESULT WINAPI WsGetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, void *buf,
549 ULONG size, WS_ERROR *error )
551 struct channel *channel = (struct channel *)handle;
552 HRESULT hr = S_OK;
554 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
555 if (error) FIXME( "ignoring error parameter\n" );
557 if (!channel) return E_INVALIDARG;
559 EnterCriticalSection( &channel->cs );
561 if (channel->magic != CHANNEL_MAGIC)
563 LeaveCriticalSection( &channel->cs );
564 return E_INVALIDARG;
567 switch (id)
569 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE:
570 if (!buf || size != sizeof(channel->type)) hr = E_INVALIDARG;
571 else *(WS_CHANNEL_TYPE *)buf = channel->type;
572 break;
574 case WS_CHANNEL_PROPERTY_ENCODING:
575 if (!buf || size != sizeof(channel->encoding)) hr = E_INVALIDARG;
576 else *(WS_ENCODING *)buf = channel->encoding;
577 break;
579 default:
580 hr = prop_get( channel->prop, channel->prop_count, id, buf, size );
583 LeaveCriticalSection( &channel->cs );
584 return hr;
587 /**************************************************************************
588 * WsSetChannelProperty [webservices.@]
590 HRESULT WINAPI WsSetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, const void *value,
591 ULONG size, WS_ERROR *error )
593 struct channel *channel = (struct channel *)handle;
594 HRESULT hr;
596 TRACE( "%p %u %p %u\n", handle, id, value, size );
597 if (error) FIXME( "ignoring error parameter\n" );
599 if (!channel) return E_INVALIDARG;
601 EnterCriticalSection( &channel->cs );
603 if (channel->magic != CHANNEL_MAGIC)
605 LeaveCriticalSection( &channel->cs );
606 return E_INVALIDARG;
609 hr = prop_set( channel->prop, channel->prop_count, id, value, size );
611 LeaveCriticalSection( &channel->cs );
612 return hr;
615 static HRESULT open_channel( struct channel *channel, const WS_ENDPOINT_ADDRESS *endpoint )
617 if (endpoint->headers || endpoint->extensions || endpoint->identity)
619 FIXME( "headers, extensions or identity not supported\n" );
620 return E_NOTIMPL;
623 if (!(channel->addr.url.chars = heap_alloc( endpoint->url.length * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
624 memcpy( channel->addr.url.chars, endpoint->url.chars, endpoint->url.length * sizeof(WCHAR) );
625 channel->addr.url.length = endpoint->url.length;
627 channel->state = WS_CHANNEL_STATE_OPEN;
628 return S_OK;
631 /**************************************************************************
632 * WsOpenChannel [webservices.@]
634 HRESULT WINAPI WsOpenChannel( WS_CHANNEL *handle, const WS_ENDPOINT_ADDRESS *endpoint,
635 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
637 struct channel *channel = (struct channel *)handle;
638 HRESULT hr;
640 TRACE( "%p %p %p %p\n", handle, endpoint, ctx, error );
641 if (error) FIXME( "ignoring error parameter\n" );
642 if (ctx) FIXME( "ignoring ctx parameter\n" );
644 if (!channel || !endpoint) return E_INVALIDARG;
646 EnterCriticalSection( &channel->cs );
648 if (channel->magic != CHANNEL_MAGIC)
650 LeaveCriticalSection( &channel->cs );
651 return E_INVALIDARG;
654 if (channel->state != WS_CHANNEL_STATE_CREATED)
656 LeaveCriticalSection( &channel->cs );
657 return WS_E_INVALID_OPERATION;
660 hr = open_channel( channel, endpoint );
662 LeaveCriticalSection( &channel->cs );
663 return hr;
666 static void close_channel( struct channel *channel )
668 reset_channel( channel );
669 channel->state = WS_CHANNEL_STATE_CLOSED;
672 /**************************************************************************
673 * WsCloseChannel [webservices.@]
675 HRESULT WINAPI WsCloseChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
677 struct channel *channel = (struct channel *)handle;
679 TRACE( "%p %p %p\n", handle, ctx, error );
680 if (error) FIXME( "ignoring error parameter\n" );
681 if (ctx) FIXME( "ignoring ctx parameter\n" );
683 if (!channel) return E_INVALIDARG;
685 EnterCriticalSection( &channel->cs );
687 if (channel->magic != CHANNEL_MAGIC)
689 LeaveCriticalSection( &channel->cs );
690 return E_INVALIDARG;
693 close_channel( channel );
695 LeaveCriticalSection( &channel->cs );
696 return S_OK;
699 static HRESULT parse_http_url( const WCHAR *url, ULONG len, URL_COMPONENTS *uc )
701 HRESULT hr = E_OUTOFMEMORY;
702 WCHAR *tmp;
703 DWORD err;
705 memset( uc, 0, sizeof(*uc) );
706 uc->dwStructSize = sizeof(*uc);
707 uc->dwHostNameLength = 128;
708 uc->lpszHostName = heap_alloc( uc->dwHostNameLength * sizeof(WCHAR) );
709 uc->dwUrlPathLength = 128;
710 uc->lpszUrlPath = heap_alloc( uc->dwUrlPathLength * sizeof(WCHAR) );
711 uc->dwExtraInfoLength = 128;
712 uc->lpszExtraInfo = heap_alloc( uc->dwExtraInfoLength * sizeof(WCHAR) );
713 if (!uc->lpszHostName || !uc->lpszUrlPath || !uc->lpszExtraInfo) goto error;
715 if (!WinHttpCrackUrl( url, len, ICU_DECODE, uc ))
717 if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
719 hr = HRESULT_FROM_WIN32( err );
720 goto error;
722 if (!(tmp = heap_realloc( uc->lpszHostName, uc->dwHostNameLength * sizeof(WCHAR) ))) goto error;
723 uc->lpszHostName = tmp;
724 if (!(tmp = heap_realloc( uc->lpszUrlPath, uc->dwUrlPathLength * sizeof(WCHAR) ))) goto error;
725 uc->lpszUrlPath = tmp;
726 if (!(tmp = heap_realloc( uc->lpszExtraInfo, uc->dwExtraInfoLength * sizeof(WCHAR) ))) goto error;
727 uc->lpszExtraInfo = tmp;
728 WinHttpCrackUrl( url, len, ICU_DECODE, uc );
731 return S_OK;
733 error:
734 heap_free( uc->lpszHostName );
735 heap_free( uc->lpszUrlPath );
736 heap_free( uc->lpszExtraInfo );
737 return hr;
740 static HRESULT connect_channel_http( struct channel *channel )
742 static const WCHAR agentW[] =
743 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
744 HINTERNET ses = NULL, con = NULL;
745 URL_COMPONENTS uc;
746 HRESULT hr;
748 if (channel->u.http.connect) return S_OK;
750 if ((hr = parse_http_url( channel->addr.url.chars, channel->addr.url.length, &uc )) != S_OK) return hr;
751 if (!(channel->u.http.path = heap_alloc( (uc.dwUrlPathLength + uc.dwExtraInfoLength + 1) * sizeof(WCHAR) )))
753 hr = E_OUTOFMEMORY;
754 goto done;
756 else
758 strcpyW( channel->u.http.path, uc.lpszUrlPath );
759 if (uc.dwExtraInfoLength) strcatW( channel->u.http.path, uc.lpszExtraInfo );
762 channel->u.http.flags = WINHTTP_FLAG_REFRESH;
763 switch (uc.nScheme)
765 case INTERNET_SCHEME_HTTP: break;
766 case INTERNET_SCHEME_HTTPS:
767 channel->u.http.flags |= WINHTTP_FLAG_SECURE;
768 break;
770 default:
771 hr = WS_E_INVALID_ENDPOINT_URL;
772 goto done;
775 if (!(ses = WinHttpOpen( agentW, 0, NULL, NULL, 0 )))
777 hr = HRESULT_FROM_WIN32( GetLastError() );
778 goto done;
780 if (!(con = WinHttpConnect( ses, uc.lpszHostName, uc.nPort, 0 )))
782 hr = HRESULT_FROM_WIN32( GetLastError() );
783 goto done;
786 channel->u.http.session = ses;
787 channel->u.http.connect = con;
789 done:
790 if (hr != S_OK)
792 WinHttpCloseHandle( con );
793 WinHttpCloseHandle( ses );
795 heap_free( uc.lpszHostName );
796 heap_free( uc.lpszUrlPath );
797 heap_free( uc.lpszExtraInfo );
798 return hr;
801 static HRESULT connect_channel_tcp( struct channel *channel )
803 struct sockaddr_storage storage;
804 struct sockaddr *addr = (struct sockaddr *)&storage;
805 int addr_len;
806 WS_URL_SCHEME_TYPE scheme;
807 WCHAR *host;
808 USHORT port;
809 HRESULT hr;
811 if (channel->u.tcp.socket != -1) return S_OK;
813 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
814 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
816 heap_free( host );
817 return WS_E_INVALID_ENDPOINT_URL;
820 winsock_init();
822 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
823 heap_free( host );
824 if (hr != S_OK) return hr;
826 if ((channel->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
827 return HRESULT_FROM_WIN32( WSAGetLastError() );
829 if (connect( channel->u.tcp.socket, addr, addr_len ) < 0)
831 closesocket( channel->u.tcp.socket );
832 channel->u.tcp.socket = -1;
833 return HRESULT_FROM_WIN32( WSAGetLastError() );
836 return S_OK;
839 static HRESULT connect_channel_udp( struct channel *channel )
841 struct sockaddr_storage storage;
842 struct sockaddr *addr = (struct sockaddr *)&storage;
843 int addr_len;
844 WS_URL_SCHEME_TYPE scheme;
845 WCHAR *host;
846 USHORT port;
847 HRESULT hr;
849 if (channel->u.udp.socket != -1) return S_OK;
851 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
852 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
854 heap_free( host );
855 return WS_E_INVALID_ENDPOINT_URL;
858 winsock_init();
860 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
861 heap_free( host );
862 if (hr != S_OK) return hr;
864 if ((channel->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
865 return HRESULT_FROM_WIN32( WSAGetLastError() );
867 if (connect( channel->u.udp.socket, addr, addr_len ) < 0)
869 closesocket( channel->u.udp.socket );
870 channel->u.udp.socket = -1;
871 return HRESULT_FROM_WIN32( WSAGetLastError() );
874 return S_OK;
877 static HRESULT connect_channel( struct channel *channel )
879 switch (channel->binding)
881 case WS_HTTP_CHANNEL_BINDING:
882 return connect_channel_http( channel );
884 case WS_TCP_CHANNEL_BINDING:
885 return connect_channel_tcp( channel );
887 case WS_UDP_CHANNEL_BINDING:
888 return connect_channel_udp( channel );
890 default:
891 ERR( "unhandled binding %u\n", channel->binding );
892 return E_NOTIMPL;
896 static HRESULT write_message( WS_MESSAGE *handle, WS_XML_WRITER *writer, const WS_ELEMENT_DESCRIPTION *desc,
897 WS_WRITE_OPTION option, const void *body, ULONG size )
899 HRESULT hr;
900 if ((hr = WsWriteEnvelopeStart( handle, writer, NULL, NULL, NULL )) != S_OK) return hr;
901 if ((hr = WsWriteBody( handle, desc, option, body, size, NULL )) != S_OK) return hr;
902 return WsWriteEnvelopeEnd( handle, NULL );
905 static HRESULT send_message_http( HINTERNET request, BYTE *data, ULONG len )
907 if (!WinHttpSendRequest( request, NULL, 0, data, len, len, 0 ))
908 return HRESULT_FROM_WIN32( GetLastError() );
910 if (!WinHttpReceiveResponse( request, NULL ))
911 return HRESULT_FROM_WIN32( GetLastError() );
912 return S_OK;
915 static HRESULT send_byte( SOCKET socket, BYTE byte )
917 int count = send( socket, (char *)&byte, 1, 0 );
918 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
919 if (count != 1) return WS_E_OTHER;
920 return S_OK;
923 static HRESULT send_bytes( SOCKET socket, BYTE *bytes, int len )
925 int count = send( socket, (char *)bytes, len, 0 );
926 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
927 if (count != len) return WS_E_OTHER;
928 return S_OK;
931 static HRESULT send_size( SOCKET socket, ULONG size )
933 HRESULT hr;
934 if (size < 0x80) return send_byte( socket, size );
935 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
936 if ((size >>= 7) < 0x80) return send_byte( socket, size );
937 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
938 if ((size >>= 7) < 0x80) return send_byte( socket, size );
939 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
940 if ((size >>= 7) < 0x80) return send_byte( socket, size );
941 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
942 if ((size >>= 7) < 0x08) return send_byte( socket, size );
943 return E_INVALIDARG;
946 enum frame_record_type
948 FRAME_RECORD_TYPE_VERSION,
949 FRAME_RECORD_TYPE_MODE,
950 FRAME_RECORD_TYPE_VIA,
951 FRAME_RECORD_TYPE_KNOWN_ENCODING,
952 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING,
953 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE,
954 FRAME_RECORD_TYPE_SIZED_ENVELOPE,
955 FRAME_RECORD_TYPE_END,
956 FRAME_RECORD_TYPE_FAULT,
957 FRAME_RECORD_TYPE_UPGRADE_REQUEST,
958 FRAME_RECORD_TYPE_UPGRADE_RESPONSE,
959 FRAME_RECORD_TYPE_PREAMBLE_ACK,
960 FRAME_RECORD_TYPE_PREAMBLE_END,
963 static inline ULONG size_length( ULONG size )
965 if (size < 0x80) return 1;
966 if (size < 0x4000) return 2;
967 if (size < 0x200000) return 3;
968 if (size < 0x10000000) return 4;
969 return 5;
972 static ULONG string_table_size( const WS_XML_DICTIONARY *dict )
974 ULONG i, size = 0;
975 for (i = 0; i < dict->stringCount; i++)
976 size += size_length( dict->strings[i].length ) + dict->strings[i].length;
977 return size;
980 static HRESULT send_string_table( SOCKET socket, const WS_XML_DICTIONARY *dict )
982 ULONG i;
983 HRESULT hr;
984 for (i = 0; i < dict->stringCount; i++)
986 if ((hr = send_size( socket, dict->strings[i].length )) != S_OK) return hr;
987 if ((hr = send_bytes( socket, dict->strings[i].bytes, dict->strings[i].length )) != S_OK) return hr;
989 return S_OK;
992 static HRESULT string_to_utf8( const WS_STRING *str, unsigned char **ret, int *len )
994 *len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
995 if (!(*ret = heap_alloc( *len ))) return E_OUTOFMEMORY;
996 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)*ret, *len, NULL, NULL );
997 return S_OK;
1000 enum session_mode
1002 SESSION_MODE_INVALID = 0,
1003 SESSION_MODE_SINGLETON = 1,
1004 SESSION_MODE_DUPLEX = 2,
1005 SESSION_MODE_SIMPLEX = 3,
1008 static enum session_mode map_channel_type( struct channel *channel )
1010 switch (channel->type)
1012 case WS_CHANNEL_TYPE_DUPLEX_SESSION: return SESSION_MODE_DUPLEX;
1013 default:
1014 FIXME( "unhandled channel type %08x\n", channel->type );
1015 return SESSION_MODE_INVALID;
1019 enum known_encoding
1021 KNOWN_ENCODING_SOAP11_UTF8 = 0x00,
1022 KNOWN_ENCODING_SOAP11_UTF16 = 0x01,
1023 KNOWN_ENCODING_SOAP11_UTF16LE = 0x02,
1024 KNOWN_ENCODING_SOAP12_UTF8 = 0x03,
1025 KNOWN_ENCODING_SOAP12_UTF16 = 0x04,
1026 KNOWN_ENCODING_SOAP12_UTF16LE = 0x05,
1027 KNOWN_ENCODING_SOAP12_MTOM = 0x06,
1028 KNOWN_ENCODING_SOAP12_BINARY = 0x07,
1029 KNOWN_ENCODING_SOAP12_BINARY_SESSION = 0x08,
1032 static enum known_encoding map_channel_encoding( struct channel *channel )
1034 WS_ENVELOPE_VERSION version;
1036 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version, sizeof(version) );
1038 switch (version)
1040 case WS_ENVELOPE_VERSION_SOAP_1_1:
1041 switch (channel->encoding)
1043 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP11_UTF8;
1044 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP11_UTF16LE;
1045 default:
1046 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1047 return 0;
1049 case WS_ENVELOPE_VERSION_SOAP_1_2:
1050 switch (channel->encoding)
1052 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP12_UTF8;
1053 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP12_UTF16LE;
1054 case WS_ENCODING_XML_BINARY_1: return KNOWN_ENCODING_SOAP12_BINARY;
1055 case WS_ENCODING_XML_BINARY_SESSION_1: return KNOWN_ENCODING_SOAP12_BINARY_SESSION;
1056 default:
1057 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1058 return 0;
1060 default:
1061 ERR( "unhandled version %u\n", version );
1062 return 0;
1066 #define FRAME_VERSION_MAJOR 1
1067 #define FRAME_VERSION_MINOR 1
1069 static HRESULT send_preamble( struct channel *channel )
1071 unsigned char *url;
1072 HRESULT hr;
1073 int len;
1075 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VERSION )) != S_OK) return hr;
1076 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MAJOR )) != S_OK) return hr;
1077 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MINOR )) != S_OK) return hr;
1079 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_MODE )) != S_OK) return hr;
1080 if ((hr = send_byte( channel->u.tcp.socket, map_channel_type(channel) )) != S_OK) return hr;
1082 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VIA )) != S_OK) return hr;
1083 if ((hr = string_to_utf8( &channel->addr.url, &url, &len )) != S_OK) return hr;
1084 if ((hr = send_size( channel->u.tcp.socket, len )) != S_OK) goto done;
1085 if ((hr = send_bytes( channel->u.tcp.socket, url, len )) != S_OK) goto done;
1087 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_KNOWN_ENCODING )) != S_OK) goto done;
1088 if ((hr = send_byte( channel->u.tcp.socket, map_channel_encoding(channel) )) != S_OK) goto done;
1089 hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_END );
1091 done:
1092 heap_free( url );
1093 return hr;
1096 static HRESULT receive_bytes( struct channel *channel, unsigned char *bytes, int len )
1098 int count = recv( channel->u.tcp.socket, (char *)bytes, len, 0 );
1099 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1100 if (count != len) return WS_E_INVALID_FORMAT;
1101 return S_OK;
1104 static HRESULT receive_preamble_ack( struct channel *channel )
1106 unsigned char byte;
1107 HRESULT hr;
1109 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1110 if (byte != FRAME_RECORD_TYPE_PREAMBLE_ACK) return WS_E_INVALID_FORMAT;
1111 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1112 return S_OK;
1115 static HRESULT send_sized_envelope( struct channel *channel, BYTE *data, ULONG len )
1117 ULONG table_size = string_table_size( &channel->dict_send.dict );
1118 HRESULT hr;
1120 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_SIZED_ENVELOPE )) != S_OK) return hr;
1121 if ((hr = send_size( channel->u.tcp.socket, size_length(table_size) + table_size + len )) != S_OK) return hr;
1122 if ((hr = send_size( channel->u.tcp.socket, table_size )) != S_OK) return hr;
1123 if ((hr = send_string_table( channel->u.tcp.socket, &channel->dict_send.dict )) != S_OK) return hr;
1124 return send_bytes( channel->u.tcp.socket, data, len );
1127 static HRESULT open_http_request( struct channel *channel, HINTERNET *req )
1129 static const WCHAR postW[] = {'P','O','S','T',0};
1130 if ((*req = WinHttpOpenRequest( channel->u.http.connect, postW, channel->u.http.path,
1131 NULL, NULL, NULL, channel->u.http.flags ))) return S_OK;
1132 return HRESULT_FROM_WIN32( GetLastError() );
1135 static HRESULT send_message( struct channel *channel, WS_MESSAGE *msg )
1137 WS_XML_WRITER *writer;
1138 WS_BYTES buf;
1139 HRESULT hr;
1141 channel->msg = msg;
1142 if ((hr = connect_channel( channel )) != S_OK) return hr;
1144 WsGetMessageProperty( channel->msg, WS_MESSAGE_PROPERTY_BODY_WRITER, &writer, sizeof(writer), NULL );
1145 WsGetWriterProperty( writer, WS_XML_WRITER_PROPERTY_BYTES, &buf, sizeof(buf), NULL );
1147 switch (channel->binding)
1149 case WS_HTTP_CHANNEL_BINDING:
1150 if (channel->u.http.request)
1152 WinHttpCloseHandle( channel->u.http.request );
1153 channel->u.http.request = NULL;
1155 if ((hr = open_http_request( channel, &channel->u.http.request )) != S_OK) return hr;
1156 if ((hr = message_insert_http_headers( msg, channel->u.http.request )) != S_OK) return hr;
1157 return send_message_http( channel->u.http.request, buf.bytes, buf.length );
1159 case WS_TCP_CHANNEL_BINDING:
1160 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1162 switch (channel->session_state)
1164 case SESSION_STATE_UNINITIALIZED:
1165 if ((hr = send_preamble( channel )) != S_OK) return hr;
1166 if ((hr = receive_preamble_ack( channel )) != S_OK) return hr;
1167 /* fall through */
1169 case SESSION_STATE_SETUP_COMPLETE:
1170 return send_sized_envelope( channel, buf.bytes, buf.length );
1172 default:
1173 ERR( "unhandled session state %u\n", channel->session_state );
1174 return WS_E_OTHER;
1177 return send_bytes( channel->u.tcp.socket, buf.bytes, buf.length );
1179 case WS_UDP_CHANNEL_BINDING:
1180 return send_bytes( channel->u.udp.socket, buf.bytes, buf.length );
1182 default:
1183 ERR( "unhandled binding %u\n", channel->binding );
1184 return E_NOTIMPL;
1188 HRESULT channel_send_message( WS_CHANNEL *handle, WS_MESSAGE *msg )
1190 struct channel *channel = (struct channel *)handle;
1191 HRESULT hr;
1193 EnterCriticalSection( &channel->cs );
1195 if (channel->magic != CHANNEL_MAGIC)
1197 LeaveCriticalSection( &channel->cs );
1198 return E_INVALIDARG;
1201 hr = send_message( channel, msg );
1203 LeaveCriticalSection( &channel->cs );
1204 return hr;
1207 static HRESULT CALLBACK dict_cb( void *state, const WS_XML_STRING *str, BOOL *found, ULONG *id, WS_ERROR *error )
1209 struct dictionary *dict = state;
1210 HRESULT hr = S_OK;
1211 BYTE *bytes;
1212 int index;
1214 if ((index = find_string( dict, str->bytes, str->length, id )) == -1)
1216 *found = TRUE;
1217 return S_OK;
1220 if (!(bytes = heap_alloc( str->length ))) return E_OUTOFMEMORY;
1221 memcpy( bytes, str->bytes, str->length );
1222 if ((hr = insert_string( dict, bytes, str->length, index, id )) == S_OK)
1224 *found = TRUE;
1225 return S_OK;
1227 heap_free( bytes );
1229 *found = FALSE;
1230 return hr;
1233 static HRESULT init_writer( struct channel *channel )
1235 WS_XML_WRITER_BUFFER_OUTPUT buf = {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER}};
1236 WS_XML_WRITER_TEXT_ENCODING text = {{WS_XML_WRITER_ENCODING_TYPE_TEXT}, WS_CHARSET_UTF8};
1237 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1238 HRESULT hr;
1240 if (!channel->writer && (hr = WsCreateWriter( NULL, 0, &channel->writer, NULL )) != S_OK) return hr;
1242 switch (channel->encoding)
1244 case WS_ENCODING_XML_UTF8:
1245 return WsSetOutput( channel->writer, &text.encoding, &buf.output, NULL, 0, NULL );
1247 case WS_ENCODING_XML_BINARY_SESSION_1:
1248 clear_dict( &channel->dict_send );
1249 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1250 bin.dynamicStringCallback = dict_cb;
1251 bin.dynamicStringCallbackState = &channel->dict_send;
1252 if ((hr = WsSetOutput( channel->writer, &bin.encoding, &buf.output, NULL, 0, NULL )) != S_OK) return hr;
1253 return writer_enable_lookup( channel->writer );
1255 case WS_ENCODING_XML_BINARY_1:
1256 return WsSetOutput( channel->writer, &bin.encoding, &buf.output, NULL, 0, NULL );
1258 default:
1259 FIXME( "unhandled encoding %u\n", channel->encoding );
1260 return WS_E_NOT_SUPPORTED;
1264 /**************************************************************************
1265 * WsSendMessage [webservices.@]
1267 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1268 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1269 WS_ERROR *error )
1271 struct channel *channel = (struct channel *)handle;
1272 HRESULT hr;
1274 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1275 if (error) FIXME( "ignoring error parameter\n" );
1276 if (ctx) FIXME( "ignoring ctx parameter\n" );
1278 if (!channel || !msg || !desc) return E_INVALIDARG;
1280 EnterCriticalSection( &channel->cs );
1282 if (channel->magic != CHANNEL_MAGIC)
1284 LeaveCriticalSection( &channel->cs );
1285 return E_INVALIDARG;
1288 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK) goto done;
1289 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1290 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1292 if ((hr = init_writer( channel )) != S_OK) goto done;
1293 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1294 goto done;
1295 hr = send_message( channel, msg );
1297 done:
1298 LeaveCriticalSection( &channel->cs );
1299 return hr;
1302 /**************************************************************************
1303 * WsSendReplyMessage [webservices.@]
1305 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1306 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1307 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1309 struct channel *channel = (struct channel *)handle;
1310 GUID req_id;
1311 HRESULT hr;
1313 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1314 if (error) FIXME( "ignoring error parameter\n" );
1315 if (ctx) FIXME( "ignoring ctx parameter\n" );
1317 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1319 EnterCriticalSection( &channel->cs );
1321 if (channel->magic != CHANNEL_MAGIC)
1323 LeaveCriticalSection( &channel->cs );
1324 return E_INVALIDARG;
1327 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1328 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1329 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1330 if ((hr = message_get_id( request, &req_id )) != S_OK) goto done;
1331 if ((hr = message_set_request_id( msg, &req_id )) != S_OK) goto done;
1333 if ((hr = init_writer( channel )) != S_OK) goto done;
1334 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1335 goto done;
1336 hr = send_message( channel, msg );
1338 done:
1339 LeaveCriticalSection( &channel->cs );
1340 FIXME( "returning %08x\n", hr );
1341 return hr;
1344 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1346 if (!channel->read_buf)
1348 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1349 channel->read_buflen = size;
1350 return S_OK;
1352 if (channel->read_buflen < size)
1354 char *tmp;
1355 ULONG new_size = max( size, channel->read_buflen * 2 );
1356 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1357 channel->read_buf = tmp;
1358 channel->read_buflen = new_size;
1360 return S_OK;
1363 static HRESULT init_reader( struct channel *channel )
1365 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1366 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1367 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1368 WS_XML_READER_ENCODING *encoding;
1369 HRESULT hr;
1371 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1373 switch (channel->encoding)
1375 case WS_ENCODING_XML_UTF8:
1376 text.charSet = WS_CHARSET_UTF8;
1377 encoding = &text.encoding;
1378 break;
1380 case WS_ENCODING_XML_BINARY_SESSION_1:
1381 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1382 bin.dynamicDictionary = &channel->dict_recv.dict;
1383 /* fall through */
1385 case WS_ENCODING_XML_BINARY_1:
1386 encoding = &bin.encoding;
1387 break;
1389 default:
1390 FIXME( "unhandled encoding %u\n", channel->encoding );
1391 return WS_E_NOT_SUPPORTED;
1394 buf.encodedData = channel->read_buf;
1395 buf.encodedDataSize = channel->read_size;
1396 return WsSetInput( channel->reader, encoding, &buf.input, NULL, 0, NULL );
1399 #define INITIAL_READ_BUFFER_SIZE 4096
1400 static HRESULT receive_message_http( struct channel *channel )
1402 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1403 ULONG max_len;
1404 HRESULT hr;
1406 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1407 &max_len, sizeof(max_len) );
1409 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1410 channel->read_size = 0;
1411 for (;;)
1413 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1415 return HRESULT_FROM_WIN32( GetLastError() );
1417 if (!len) break;
1418 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1419 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1421 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1423 return HRESULT_FROM_WIN32( GetLastError() );
1425 if (!bytes_read) break;
1426 channel->read_size += bytes_read;
1427 offset += bytes_read;
1430 return init_reader( channel );
1433 static HRESULT receive_message_unsized( struct channel *channel, SOCKET socket )
1435 int bytes_read;
1436 ULONG max_len;
1437 HRESULT hr;
1439 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1440 &max_len, sizeof(max_len) );
1442 if ((hr = resize_read_buffer( channel, max_len )) != S_OK) return hr;
1444 channel->read_size = 0;
1445 if ((bytes_read = recv( socket, channel->read_buf, max_len, 0 )) < 0)
1447 return HRESULT_FROM_WIN32( WSAGetLastError() );
1449 channel->read_size = bytes_read;
1450 return S_OK;
1453 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1455 unsigned int offset = 0, to_read = size;
1456 int bytes_read;
1457 HRESULT hr;
1459 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1461 channel->read_size = 0;
1462 while (channel->read_size < size)
1464 if ((bytes_read = recv( channel->u.tcp.socket, channel->read_buf + offset, to_read, 0 )) < 0)
1466 return HRESULT_FROM_WIN32( WSAGetLastError() );
1468 if (!bytes_read) break;
1469 channel->read_size += bytes_read;
1470 to_read -= bytes_read;
1471 offset += bytes_read;
1473 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1474 return S_OK;
1477 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1479 unsigned char byte;
1480 HRESULT hr;
1482 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1483 *size = byte & 0x7f;
1484 if (!(byte & 0x80)) return S_OK;
1486 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1487 *size += (byte & 0x7f) << 7;
1488 if (!(byte & 0x80)) return S_OK;
1490 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1491 *size += (byte & 0x7f) << 14;
1492 if (!(byte & 0x80)) return S_OK;
1494 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1495 *size += (byte & 0x7f) << 21;
1496 if (!(byte & 0x80)) return S_OK;
1498 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1499 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1500 *size += byte << 28;
1501 return S_OK;
1504 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1506 switch (encoding)
1508 case KNOWN_ENCODING_SOAP11_UTF8:
1509 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1510 case KNOWN_ENCODING_SOAP11_UTF16:
1511 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1512 case KNOWN_ENCODING_SOAP11_UTF16LE:
1513 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1514 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1515 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1516 default:
1517 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1518 return WS_ENCODING_XML_UTF8;
1522 static HRESULT receive_preamble( struct channel *channel )
1524 unsigned char type;
1525 HRESULT hr;
1527 for (;;)
1529 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1530 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1531 switch (type)
1533 case FRAME_RECORD_TYPE_VERSION:
1535 unsigned char major, minor;
1536 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1537 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1538 TRACE( "major %u minor %u\n", major, major );
1539 break;
1541 case FRAME_RECORD_TYPE_MODE:
1543 unsigned char mode;
1544 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1545 TRACE( "mode %u\n", mode );
1546 break;
1548 case FRAME_RECORD_TYPE_VIA:
1550 unsigned int size;
1551 unsigned char *url;
1553 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1554 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1555 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1557 heap_free( url );
1558 return hr;
1560 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1561 heap_free( url ); /* FIXME: verify */
1562 break;
1564 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1566 unsigned char encoding;
1567 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1568 TRACE( "encoding %u\n", encoding );
1569 channel->encoding = map_known_encoding( encoding );
1570 break;
1572 default:
1573 WARN( "unhandled record type %u\n", type );
1574 return WS_E_INVALID_FORMAT;
1578 return S_OK;
1581 static HRESULT receive_sized_envelope( struct channel *channel )
1583 unsigned char type;
1584 unsigned int size;
1585 HRESULT hr;
1587 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1588 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1589 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1590 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1591 return S_OK;
1594 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1596 const BYTE *buf = *ptr;
1598 if (len < 1) return WS_E_INVALID_FORMAT;
1599 *size = buf[0] & 0x7f;
1600 if (!(buf[0] & 0x80))
1602 *ptr += 1;
1603 return S_OK;
1605 if (len < 2) return WS_E_INVALID_FORMAT;
1606 *size += (buf[1] & 0x7f) << 7;
1607 if (!(buf[1] & 0x80))
1609 *ptr += 2;
1610 return S_OK;
1612 if (len < 3) return WS_E_INVALID_FORMAT;
1613 *size += (buf[2] & 0x7f) << 14;
1614 if (!(buf[2] & 0x80))
1616 *ptr += 3;
1617 return S_OK;
1619 if (len < 4) return WS_E_INVALID_FORMAT;
1620 *size += (buf[3] & 0x7f) << 21;
1621 if (!(buf[3] & 0x80))
1623 *ptr += 4;
1624 return S_OK;
1626 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1627 *size += buf[4] << 28;
1628 *ptr += 5;
1629 return S_OK;
1632 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1634 ULONG size, strings_size, strings_offset;
1635 const BYTE *ptr = buf;
1636 BYTE *bytes;
1637 int index;
1638 HRESULT hr;
1640 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1641 strings_offset = ptr - buf;
1642 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1643 *used = strings_offset + strings_size;
1644 if (!strings_size) return S_OK;
1646 UuidCreate( &dict->dict.guid );
1647 dict->dict.isConst = FALSE;
1649 buflen -= strings_offset;
1650 ptr = buf + strings_offset;
1651 while (ptr < buf + strings_size)
1653 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1655 clear_dict( dict );
1656 return hr;
1658 if (size > buflen)
1660 clear_dict( dict );
1661 return WS_E_INVALID_FORMAT;
1663 buflen -= size;
1664 if (!(bytes = heap_alloc( size )))
1666 hr = E_OUTOFMEMORY;
1667 goto error;
1669 memcpy( bytes, ptr, size );
1670 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1672 heap_free( bytes );
1673 ptr += size;
1674 continue;
1676 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1678 heap_free( bytes );
1679 clear_dict( dict );
1680 return hr;
1682 ptr += size;
1684 return S_OK;
1686 error:
1687 clear_dict( dict );
1688 return hr;
1691 static HRESULT send_preamble_ack( struct channel *channel )
1693 HRESULT hr;
1694 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1695 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1696 return S_OK;
1699 static HRESULT receive_message_session( struct channel *channel )
1701 HRESULT hr;
1703 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1704 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1706 ULONG size;
1707 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict_recv,
1708 &size )) != S_OK) return hr;
1709 channel->read_size -= size;
1710 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1713 return init_reader( channel );
1716 static HRESULT receive_message_sock( struct channel *channel, SOCKET socket )
1718 HRESULT hr;
1719 if ((hr = receive_message_unsized( channel, socket )) != S_OK) return hr;
1720 return init_reader( channel );
1723 static HRESULT receive_message_bytes( struct channel *channel )
1725 HRESULT hr;
1726 if ((hr = connect_channel( channel )) != S_OK) return hr;
1728 switch (channel->binding)
1730 case WS_HTTP_CHANNEL_BINDING:
1731 return receive_message_http( channel );
1733 case WS_TCP_CHANNEL_BINDING:
1734 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1736 switch (channel->session_state)
1738 case SESSION_STATE_UNINITIALIZED:
1739 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1740 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1741 /* fall through */
1743 case SESSION_STATE_SETUP_COMPLETE:
1744 return receive_message_session( channel );
1746 default:
1747 ERR( "unhandled session state %u\n", channel->session_state );
1748 return WS_E_OTHER;
1751 return receive_message_sock( channel, channel->u.tcp.socket );
1753 case WS_UDP_CHANNEL_BINDING:
1754 return receive_message_sock( channel, channel->u.udp.socket );
1756 default:
1757 ERR( "unhandled binding %u\n", channel->binding );
1758 return E_NOTIMPL;
1762 HRESULT channel_receive_message( WS_CHANNEL *handle )
1764 struct channel *channel = (struct channel *)handle;
1765 HRESULT hr;
1767 EnterCriticalSection( &channel->cs );
1769 if (channel->magic != CHANNEL_MAGIC)
1771 LeaveCriticalSection( &channel->cs );
1772 return E_INVALIDARG;
1775 hr = receive_message_bytes( channel );
1777 LeaveCriticalSection( &channel->cs );
1778 return hr;
1781 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1783 struct channel *channel = (struct channel *)handle;
1785 EnterCriticalSection( &channel->cs );
1787 if (channel->magic != CHANNEL_MAGIC)
1789 LeaveCriticalSection( &channel->cs );
1790 return E_INVALIDARG;
1793 *reader = channel->reader;
1795 LeaveCriticalSection( &channel->cs );
1796 return S_OK;
1799 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1800 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1802 HRESULT hr;
1803 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1804 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1805 return WsReadEnvelopeEnd( handle, NULL );
1808 static HRESULT receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1809 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1810 void *value, ULONG size, ULONG *index )
1812 HRESULT hr;
1813 ULONG i;
1815 if ((hr = receive_message_bytes( channel )) != S_OK) return hr;
1816 for (i = 0; i < count; i++)
1818 const WS_ELEMENT_DESCRIPTION *body = desc[i]->bodyElementDescription;
1819 if ((hr = read_message( msg, channel->reader, body, read_option, heap, value, size )) == S_OK)
1821 if (index) *index = i;
1822 break;
1824 if ((hr = WsResetMessage( msg, NULL )) != S_OK) return hr;
1825 if ((hr = init_reader( channel )) != S_OK) return hr;
1827 return (i == count) ? WS_E_INVALID_FORMAT : S_OK;
1830 struct receive_message
1832 struct task task;
1833 struct channel *channel;
1834 WS_MESSAGE *msg;
1835 const WS_MESSAGE_DESCRIPTION **desc;
1836 ULONG count;
1837 WS_RECEIVE_OPTION option;
1838 WS_READ_OPTION read_option;
1839 WS_HEAP *heap;
1840 void *value;
1841 ULONG size;
1842 ULONG *index;
1843 WS_ASYNC_CONTEXT ctx;
1846 static void receive_message_proc( struct task *task )
1848 struct receive_message *r = (struct receive_message *)task;
1849 HRESULT hr;
1851 hr = receive_message( r->channel, r->msg, r->desc, r->count, r->option, r->read_option, r->heap, r->value,
1852 r->size, r->index );
1854 TRACE( "calling %p(%08x)\n", r->ctx.callback, hr );
1855 r->ctx.callback( hr, WS_LONG_CALLBACK, r->ctx.callbackState );
1856 TRACE( "%p returned\n", r->ctx.callback );
1859 static HRESULT queue_receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1860 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option,
1861 WS_HEAP *heap, void *value, ULONG size, ULONG *index,
1862 const WS_ASYNC_CONTEXT *ctx )
1864 struct receive_message *r;
1866 if (!(r = heap_alloc( sizeof(*r) ))) return E_OUTOFMEMORY;
1867 r->task.proc = receive_message_proc;
1868 r->channel = channel;
1869 r->msg = msg;
1870 r->desc = desc;
1871 r->count = count;
1872 r->option = option;
1873 r->read_option = read_option;
1874 r->heap = heap;
1875 r->value = value;
1876 r->size = size;
1877 r->index = index;
1878 r->ctx = *ctx;
1879 return queue_task( &channel->recv_q, &r->task );
1882 /**************************************************************************
1883 * WsReceiveMessage [webservices.@]
1885 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1886 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1887 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1889 struct channel *channel = (struct channel *)handle;
1890 HRESULT hr;
1892 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1893 value, size, index, ctx, error );
1894 if (error) FIXME( "ignoring error parameter\n" );
1896 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1898 EnterCriticalSection( &channel->cs );
1900 if (channel->magic != CHANNEL_MAGIC)
1902 LeaveCriticalSection( &channel->cs );
1903 return E_INVALIDARG;
1906 if (ctx)
1907 hr = queue_receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index, ctx );
1908 else
1909 hr = receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index );
1911 LeaveCriticalSection( &channel->cs );
1912 return hr;
1915 /**************************************************************************
1916 * WsReadMessageStart [webservices.@]
1918 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1919 WS_ERROR *error )
1921 struct channel *channel = (struct channel *)handle;
1922 HRESULT hr;
1924 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1925 if (error) FIXME( "ignoring error parameter\n" );
1926 if (ctx) FIXME( "ignoring ctx parameter\n" );
1928 if (!channel || !msg) return E_INVALIDARG;
1930 EnterCriticalSection( &channel->cs );
1932 if (channel->magic != CHANNEL_MAGIC)
1934 LeaveCriticalSection( &channel->cs );
1935 return E_INVALIDARG;
1938 if ((hr = receive_message_bytes( channel )) == S_OK)
1940 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
1943 LeaveCriticalSection( &channel->cs );
1944 return hr;
1947 /**************************************************************************
1948 * WsReadMessageEnd [webservices.@]
1950 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1951 WS_ERROR *error )
1953 struct channel *channel = (struct channel *)handle;
1954 HRESULT hr;
1956 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1957 if (error) FIXME( "ignoring error parameter\n" );
1958 if (ctx) FIXME( "ignoring ctx parameter\n" );
1960 if (!channel || !msg) return E_INVALIDARG;
1962 EnterCriticalSection( &channel->cs );
1964 if (channel->magic != CHANNEL_MAGIC)
1966 LeaveCriticalSection( &channel->cs );
1967 return E_INVALIDARG;
1970 hr = WsReadEnvelopeEnd( msg, NULL );
1972 LeaveCriticalSection( &channel->cs );
1973 return hr;
1976 /**************************************************************************
1977 * WsWriteMessageStart [webservices.@]
1979 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1980 WS_ERROR *error )
1982 struct channel *channel = (struct channel *)handle;
1983 HRESULT hr;
1985 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1986 if (error) FIXME( "ignoring error parameter\n" );
1987 if (ctx) FIXME( "ignoring ctx parameter\n" );
1989 if (!channel || !msg) return E_INVALIDARG;
1991 EnterCriticalSection( &channel->cs );
1993 if (channel->magic != CHANNEL_MAGIC)
1995 LeaveCriticalSection( &channel->cs );
1996 return E_INVALIDARG;
1999 if ((hr = init_writer( channel )) != S_OK) goto done;
2000 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
2001 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
2003 done:
2004 LeaveCriticalSection( &channel->cs );
2005 return hr;
2008 /**************************************************************************
2009 * WsWriteMessageEnd [webservices.@]
2011 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2012 WS_ERROR *error )
2014 struct channel *channel = (struct channel *)handle;
2015 HRESULT hr;
2017 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2018 if (error) FIXME( "ignoring error parameter\n" );
2019 if (ctx) FIXME( "ignoring ctx parameter\n" );
2021 if (!channel || !msg) return E_INVALIDARG;
2023 EnterCriticalSection( &channel->cs );
2025 if (channel->magic != CHANNEL_MAGIC)
2027 LeaveCriticalSection( &channel->cs );
2028 return E_INVALIDARG;
2031 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK) hr = send_message( channel, msg );
2033 LeaveCriticalSection( &channel->cs );
2034 return hr;
2037 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
2039 HANDLE handles[] = { wait, cancel };
2040 ULONG nonblocking = 0;
2041 HRESULT hr = S_OK;
2043 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2045 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2047 case 0:
2048 if ((*ret = accept( socket, NULL, NULL )) != -1)
2050 WSAEventSelect( *ret, NULL, 0 );
2051 ioctlsocket( *ret, FIONBIO, &nonblocking );
2052 break;
2054 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2055 break;
2057 case 1:
2058 hr = WS_E_OPERATION_ABORTED;
2059 break;
2061 default:
2062 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2063 break;
2066 return hr;
2069 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2071 struct channel *channel = (struct channel *)handle;
2072 HRESULT hr;
2074 EnterCriticalSection( &channel->cs );
2076 if (channel->magic != CHANNEL_MAGIC)
2078 LeaveCriticalSection( &channel->cs );
2079 return E_INVALIDARG;
2082 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
2084 LeaveCriticalSection( &channel->cs );
2085 return hr;
2088 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
2090 HANDLE handles[] = { wait, cancel };
2091 ULONG nonblocking = 0;
2092 HRESULT hr;
2094 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2096 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2098 case 0:
2099 hr = S_OK;
2100 break;
2102 case 1:
2103 hr = WS_E_OPERATION_ABORTED;
2104 break;
2106 default:
2107 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2108 break;
2111 WSAEventSelect( socket, NULL, 0 );
2112 ioctlsocket( socket, FIONBIO, &nonblocking );
2113 return hr;
2116 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2118 struct channel *channel = (struct channel *)handle;
2119 HRESULT hr;
2121 EnterCriticalSection( &channel->cs );
2123 if (channel->magic != CHANNEL_MAGIC)
2125 LeaveCriticalSection( &channel->cs );
2126 return E_INVALIDARG;
2129 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
2131 LeaveCriticalSection( &channel->cs );
2132 return hr;
2135 /**************************************************************************
2136 * WsAbortChannel [webservices.@]
2138 HRESULT WINAPI WsAbortChannel( WS_CHANNEL *handle, WS_ERROR *error )
2140 FIXME( "%p %p: stub!\n", handle, error );
2141 return E_NOTIMPL;