ntdll: Allow explicitly specified asm.v2 namespace for "trustInfo".
[wine.git] / dlls / webservices / channel.c
blob685e13d99d744e4a40bce382e1f35981c5c017cd
1 /*
2 * Copyright 2016 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "rpc.h"
25 #include "webservices.h"
27 #include "wine/debug.h"
28 #include "wine/heap.h"
29 #include "wine/list.h"
30 #include "wine/unicode.h"
31 #include "webservices_private.h"
32 #include "sock.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
36 static const struct prop_desc channel_props[] =
38 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE */
39 { sizeof(UINT64), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_MESSAGE_SIZE */
40 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_START_SIZE */
41 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_FLUSH_SIZE */
42 { sizeof(WS_ENCODING), TRUE }, /* WS_CHANNEL_PROPERTY_ENCODING */
43 { sizeof(WS_ENVELOPE_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ENVELOPE_VERSION */
44 { sizeof(WS_ADDRESSING_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ADDRESSING_VERSION */
45 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_SESSION_DICTIONARY_SIZE */
46 { sizeof(WS_CHANNEL_STATE), TRUE }, /* WS_CHANNEL_PROPERTY_STATE */
47 { sizeof(WS_CALLBACK_MODEL), FALSE }, /* WS_CHANNEL_PROPERTY_ASYNC_CALLBACK_MODEL */
48 { sizeof(WS_IP_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_IP_VERSION */
49 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RESOLVE_TIMEOUT */
50 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CONNECT_TIMEOUT */
51 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_TIMEOUT */
52 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_RESPONSE_TIMEOUT */
53 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_TIMEOUT */
54 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CLOSE_TIMEOUT */
55 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_TIMEOUTS */
56 { sizeof(WS_TRANSFER_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_TRANSFER_MODE */
57 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_INTERFACE */
58 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_HOPS */
59 { sizeof(WS_ENDPOINT_ADDRESS), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_ADDRESS */
60 { sizeof(SOCKADDR_STORAGE), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_IP_ADDRESS */
61 { sizeof(ULONGLONG), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_CONNECTION_ID */
62 { sizeof(WS_CUSTOM_CHANNEL_CALLBACKS), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_CALLBACKS */
63 { 0, FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_PARAMETERS */
64 { sizeof(void *), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_INSTANCE */
65 { sizeof(WS_STRING), TRUE }, /* WS_CHANNEL_PROPERTY_TRANSPORT_URL */
66 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_NO_DELAY */
67 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_KEEP_ALIVES */
68 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_TIME */
69 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_INTERVAL */
70 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_HTTP_SERVER_CONNECTIONS */
71 { sizeof(BOOL), TRUE }, /* WS_CHANNEL_PROPERTY_IS_SESSION_SHUT_DOWN */
72 { sizeof(WS_CHANNEL_TYPE), TRUE }, /* WS_CHANNEL_PROPERTY_CHANNEL_TYPE */
73 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_TRIM_BUFFERED_MESSAGE_SIZE */
74 { sizeof(WS_CHANNEL_ENCODER), FALSE }, /* WS_CHANNEL_PROPERTY_ENCODER */
75 { sizeof(WS_CHANNEL_DECODER), FALSE }, /* WS_CHANNEL_PROPERTY_DECODER */
76 { sizeof(WS_PROTECTION_LEVEL), TRUE }, /* WS_CHANNEL_PROPERTY_PROTECTION_LEVEL */
77 { sizeof(WS_COOKIE_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_COOKIE_MODE */
78 { sizeof(WS_HTTP_PROXY_SETTING_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SETTING_MODE */
79 { sizeof(WS_CUSTOM_HTTP_PROXY), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_HTTP_PROXY */
80 { sizeof(WS_HTTP_MESSAGE_MAPPING), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING */
81 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_HTTP_REDIRECT */
82 { sizeof(WS_HTTP_REDIRECT_CALLBACK_CONTEXT), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_REDIRECT_CALLBACK_CONTEXT */
83 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_FAULTS_AS_ERRORS */
84 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ALLOW_UNSECURED_FAULTS */
85 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_SERVER_SPN */
86 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SPN */
87 { sizeof(ULONG), FALSE } /* WS_CHANNEL_PROPERTY_MAX_HTTP_REQUEST_HEADERS_BUFFER_SIZE */
90 struct task
92 struct list entry;
93 void (*proc)( struct task * );
96 struct queue
98 CRITICAL_SECTION cs;
99 HANDLE wait;
100 HANDLE cancel;
101 HANDLE ready;
102 struct list tasks;
105 static struct task *dequeue_task( struct queue *queue )
107 struct task *task;
109 EnterCriticalSection( &queue->cs );
110 TRACE( "%u tasks queued\n", list_count( &queue->tasks ) );
111 task = LIST_ENTRY( list_head( &queue->tasks ), struct task, entry );
112 if (task) list_remove( &task->entry );
113 LeaveCriticalSection( &queue->cs );
115 TRACE( "returning task %p\n", task );
116 return task;
119 static void CALLBACK queue_runner( TP_CALLBACK_INSTANCE *instance, void *ctx )
121 struct queue *queue = ctx;
122 HANDLE handles[] = { queue->wait, queue->cancel };
124 SetEvent( queue->ready );
125 for (;;)
127 DWORD err = WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
128 switch (err)
130 case WAIT_OBJECT_0:
132 struct task *task;
133 while ((task = dequeue_task( queue )))
135 task->proc( task );
136 heap_free( task );
138 break;
140 case WAIT_OBJECT_0 + 1:
141 TRACE( "cancelled\n" );
142 SetEvent( queue->ready );
143 return;
145 default:
146 ERR( "wait failed %u\n", err );
147 return;
152 static HRESULT start_queue( struct queue *queue )
154 HRESULT hr = E_OUTOFMEMORY;
156 if (queue->wait) return S_OK;
157 list_init( &queue->tasks );
158 if (!(queue->wait = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
159 if (!(queue->cancel = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
160 if (!(queue->ready = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
161 if (!TrySubmitThreadpoolCallback( queue_runner, queue, NULL )) hr = HRESULT_FROM_WIN32( GetLastError() );
162 else
164 WaitForSingleObject( queue->ready, INFINITE );
165 return S_OK;
168 error:
169 CloseHandle( queue->wait );
170 queue->wait = NULL;
171 CloseHandle( queue->cancel );
172 queue->cancel = NULL;
173 CloseHandle( queue->ready );
174 queue->ready = NULL;
175 return hr;
178 static HRESULT queue_task( struct queue *queue, struct task *task )
180 HRESULT hr;
181 if ((hr = start_queue( queue )) != S_OK) return hr;
183 EnterCriticalSection( &queue->cs );
184 TRACE( "queueing task %p\n", task );
185 list_add_tail( &queue->tasks, &task->entry );
186 LeaveCriticalSection( &queue->cs );
188 SetEvent( queue->wait );
189 return WS_S_ASYNC;
192 enum session_state
194 SESSION_STATE_UNINITIALIZED,
195 SESSION_STATE_SETUP_COMPLETE,
198 struct channel
200 ULONG magic;
201 CRITICAL_SECTION cs;
202 WS_CHANNEL_TYPE type;
203 WS_CHANNEL_BINDING binding;
204 WS_CHANNEL_STATE state;
205 WS_ENDPOINT_ADDRESS addr;
206 WS_XML_WRITER *writer;
207 WS_XML_READER *reader;
208 WS_MESSAGE *msg;
209 WS_ENCODING encoding;
210 enum session_state session_state;
211 struct dictionary dict_send;
212 struct dictionary dict_recv;
213 struct queue send_q;
214 struct queue recv_q;
215 union
217 struct
219 HINTERNET session;
220 HINTERNET connect;
221 HINTERNET request;
222 WCHAR *path;
223 DWORD flags;
224 } http;
225 struct
227 SOCKET socket;
228 } tcp;
229 struct
231 SOCKET socket;
232 } udp;
233 } u;
234 char *read_buf;
235 ULONG read_buflen;
236 ULONG read_size;
237 ULONG prop_count;
238 struct prop prop[sizeof(channel_props)/sizeof(channel_props[0])];
241 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
243 static struct channel *alloc_channel(void)
245 static const ULONG count = sizeof(channel_props)/sizeof(channel_props[0]);
246 struct channel *ret;
247 ULONG size = sizeof(*ret) + prop_size( channel_props, count );
249 if (!(ret = heap_alloc_zero( size ))) return NULL;
251 ret->magic = CHANNEL_MAGIC;
252 InitializeCriticalSection( &ret->cs );
253 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.cs");
255 InitializeCriticalSection( &ret->send_q.cs );
256 ret->send_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.send_q.cs");
258 InitializeCriticalSection( &ret->recv_q.cs );
259 ret->recv_q.cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.recv_q.cs");
261 prop_init( channel_props, count, ret->prop, &ret[1] );
262 ret->prop_count = count;
263 return ret;
266 static void clear_addr( WS_ENDPOINT_ADDRESS *addr )
268 heap_free( addr->url.chars );
269 addr->url.chars = NULL;
270 addr->url.length = 0;
273 static void clear_queue( struct queue *queue )
275 struct list *ptr;
277 SetEvent( queue->cancel );
278 WaitForSingleObject( queue->ready, INFINITE );
280 while ((ptr = list_head( &queue->tasks )))
282 struct task *task = LIST_ENTRY( ptr, struct task, entry );
283 list_remove( &task->entry );
284 heap_free( task );
287 CloseHandle( queue->wait );
288 queue->wait = NULL;
289 CloseHandle( queue->cancel );
290 queue->cancel = NULL;
291 CloseHandle( queue->ready );
292 queue->ready = NULL;
295 static void reset_channel( struct channel *channel )
297 clear_queue( &channel->send_q );
298 clear_queue( &channel->recv_q );
299 channel->state = WS_CHANNEL_STATE_CREATED;
300 channel->session_state = SESSION_STATE_UNINITIALIZED;
301 clear_addr( &channel->addr );
302 clear_dict( &channel->dict_send );
303 clear_dict( &channel->dict_recv );
304 channel->msg = NULL;
305 channel->read_size = 0;
307 switch (channel->binding)
309 case WS_HTTP_CHANNEL_BINDING:
310 WinHttpCloseHandle( channel->u.http.request );
311 channel->u.http.request = NULL;
312 WinHttpCloseHandle( channel->u.http.connect );
313 channel->u.http.connect = NULL;
314 WinHttpCloseHandle( channel->u.http.session );
315 channel->u.http.session = NULL;
316 heap_free( channel->u.http.path );
317 channel->u.http.path = NULL;
318 channel->u.http.flags = 0;
319 break;
321 case WS_TCP_CHANNEL_BINDING:
322 closesocket( channel->u.tcp.socket );
323 channel->u.tcp.socket = -1;
324 break;
326 case WS_UDP_CHANNEL_BINDING:
327 closesocket( channel->u.udp.socket );
328 channel->u.udp.socket = -1;
329 break;
331 default: break;
335 static void free_channel( struct channel *channel )
337 reset_channel( channel );
339 WsFreeWriter( channel->writer );
340 WsFreeReader( channel->reader );
342 heap_free( channel->read_buf );
344 channel->send_q.cs.DebugInfo->Spare[0] = 0;
345 DeleteCriticalSection( &channel->send_q.cs );
347 channel->recv_q.cs.DebugInfo->Spare[0] = 0;
348 DeleteCriticalSection( &channel->recv_q.cs );
350 channel->cs.DebugInfo->Spare[0] = 0;
351 DeleteCriticalSection( &channel->cs );
352 heap_free( channel );
355 static HRESULT create_channel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
356 const WS_CHANNEL_PROPERTY *properties, ULONG count, struct channel **ret )
358 struct channel *channel;
359 ULONG i, msg_size = 65536;
360 WS_ENVELOPE_VERSION env_version = WS_ENVELOPE_VERSION_SOAP_1_2;
361 WS_ADDRESSING_VERSION addr_version = WS_ADDRESSING_VERSION_1_0;
362 HRESULT hr;
364 if (!(channel = alloc_channel())) return E_OUTOFMEMORY;
366 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
367 &msg_size, sizeof(msg_size) );
368 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION,
369 &env_version, sizeof(env_version) );
370 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION,
371 &addr_version, sizeof(addr_version) );
373 channel->type = type;
374 channel->binding = binding;
376 switch (channel->binding)
378 case WS_HTTP_CHANNEL_BINDING:
379 channel->encoding = WS_ENCODING_XML_UTF8;
380 break;
382 case WS_TCP_CHANNEL_BINDING:
383 channel->u.tcp.socket = -1;
384 channel->encoding = WS_ENCODING_XML_BINARY_SESSION_1;
385 break;
387 case WS_UDP_CHANNEL_BINDING:
388 channel->u.udp.socket = -1;
389 channel->encoding = WS_ENCODING_XML_UTF8;
390 break;
392 default: break;
395 for (i = 0; i < count; i++)
397 switch (properties[i].id)
399 case WS_CHANNEL_PROPERTY_ENCODING:
400 if (!properties[i].value || properties[i].valueSize != sizeof(channel->encoding))
402 free_channel( channel );
403 return E_INVALIDARG;
405 channel->encoding = *(WS_ENCODING *)properties[i].value;
406 break;
408 default:
409 if ((hr = prop_set( channel->prop, channel->prop_count, properties[i].id, properties[i].value,
410 properties[i].valueSize )) != S_OK)
412 free_channel( channel );
413 return hr;
415 break;
419 *ret = channel;
420 return S_OK;
423 /**************************************************************************
424 * WsCreateChannel [webservices.@]
426 HRESULT WINAPI WsCreateChannel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
427 const WS_CHANNEL_PROPERTY *properties, ULONG count,
428 const WS_SECURITY_DESCRIPTION *desc, WS_CHANNEL **handle,
429 WS_ERROR *error )
431 struct channel *channel;
432 HRESULT hr;
434 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
435 if (error) FIXME( "ignoring error parameter\n" );
436 if (desc) FIXME( "ignoring security description\n" );
438 if (!handle) return E_INVALIDARG;
440 if (type != WS_CHANNEL_TYPE_REQUEST && type != WS_CHANNEL_TYPE_DUPLEX &&
441 type != WS_CHANNEL_TYPE_DUPLEX_SESSION)
443 FIXME( "channel type %u not implemented\n", type );
444 return E_NOTIMPL;
446 if (binding != WS_HTTP_CHANNEL_BINDING && binding != WS_TCP_CHANNEL_BINDING &&
447 binding != WS_UDP_CHANNEL_BINDING)
449 FIXME( "channel binding %u not implemented\n", binding );
450 return E_NOTIMPL;
453 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
455 TRACE( "created %p\n", channel );
456 *handle = (WS_CHANNEL *)channel;
457 return S_OK;
460 /**************************************************************************
461 * WsCreateChannelForListener [webservices.@]
463 HRESULT WINAPI WsCreateChannelForListener( WS_LISTENER *listener_handle, const WS_CHANNEL_PROPERTY *properties,
464 ULONG count, WS_CHANNEL **handle, WS_ERROR *error )
466 struct channel *channel;
467 WS_CHANNEL_TYPE type;
468 WS_CHANNEL_BINDING binding;
469 HRESULT hr;
471 TRACE( "%p %p %u %p %p\n", listener_handle, properties, count, handle, error );
472 if (error) FIXME( "ignoring error parameter\n" );
474 if (!listener_handle || !handle) return E_INVALIDARG;
476 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_TYPE, &type,
477 sizeof(type), NULL )) != S_OK) return hr;
479 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_BINDING, &binding,
480 sizeof(binding), NULL )) != S_OK) return hr;
482 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
484 TRACE( "created %p\n", channel );
485 *handle = (WS_CHANNEL *)channel;
486 return S_OK;
489 /**************************************************************************
490 * WsFreeChannel [webservices.@]
492 void WINAPI WsFreeChannel( WS_CHANNEL *handle )
494 struct channel *channel = (struct channel *)handle;
496 TRACE( "%p\n", handle );
498 if (!channel) return;
500 EnterCriticalSection( &channel->cs );
502 if (channel->magic != CHANNEL_MAGIC)
504 LeaveCriticalSection( &channel->cs );
505 return;
508 channel->magic = 0;
510 LeaveCriticalSection( &channel->cs );
511 free_channel( channel );
514 /**************************************************************************
515 * WsResetChannel [webservices.@]
517 HRESULT WINAPI WsResetChannel( WS_CHANNEL *handle, WS_ERROR *error )
519 struct channel *channel = (struct channel *)handle;
521 TRACE( "%p %p\n", handle, error );
522 if (error) FIXME( "ignoring error parameter\n" );
524 if (!channel) return E_INVALIDARG;
526 EnterCriticalSection( &channel->cs );
528 if (channel->magic != CHANNEL_MAGIC)
530 LeaveCriticalSection( &channel->cs );
531 return E_INVALIDARG;
534 if (channel->state != WS_CHANNEL_STATE_CREATED && channel->state != WS_CHANNEL_STATE_CLOSED)
536 LeaveCriticalSection( &channel->cs );
537 return WS_E_INVALID_OPERATION;
540 reset_channel( channel );
542 LeaveCriticalSection( &channel->cs );
543 return S_OK;
546 /**************************************************************************
547 * WsGetChannelProperty [webservices.@]
549 HRESULT WINAPI WsGetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, void *buf,
550 ULONG size, WS_ERROR *error )
552 struct channel *channel = (struct channel *)handle;
553 HRESULT hr = S_OK;
555 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
556 if (error) FIXME( "ignoring error parameter\n" );
558 if (!channel) return E_INVALIDARG;
560 EnterCriticalSection( &channel->cs );
562 if (channel->magic != CHANNEL_MAGIC)
564 LeaveCriticalSection( &channel->cs );
565 return E_INVALIDARG;
568 switch (id)
570 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE:
571 if (!buf || size != sizeof(channel->type)) hr = E_INVALIDARG;
572 else *(WS_CHANNEL_TYPE *)buf = channel->type;
573 break;
575 case WS_CHANNEL_PROPERTY_ENCODING:
576 if (!buf || size != sizeof(channel->encoding)) hr = E_INVALIDARG;
577 else *(WS_ENCODING *)buf = channel->encoding;
578 break;
580 default:
581 hr = prop_get( channel->prop, channel->prop_count, id, buf, size );
584 LeaveCriticalSection( &channel->cs );
585 return hr;
588 /**************************************************************************
589 * WsSetChannelProperty [webservices.@]
591 HRESULT WINAPI WsSetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, const void *value,
592 ULONG size, WS_ERROR *error )
594 struct channel *channel = (struct channel *)handle;
595 HRESULT hr;
597 TRACE( "%p %u %p %u\n", handle, id, value, size );
598 if (error) FIXME( "ignoring error parameter\n" );
600 if (!channel) return E_INVALIDARG;
602 EnterCriticalSection( &channel->cs );
604 if (channel->magic != CHANNEL_MAGIC)
606 LeaveCriticalSection( &channel->cs );
607 return E_INVALIDARG;
610 hr = prop_set( channel->prop, channel->prop_count, id, value, size );
612 LeaveCriticalSection( &channel->cs );
613 return hr;
616 static HRESULT open_channel( struct channel *channel, const WS_ENDPOINT_ADDRESS *endpoint )
618 if (endpoint->headers || endpoint->extensions || endpoint->identity)
620 FIXME( "headers, extensions or identity not supported\n" );
621 return E_NOTIMPL;
624 if (!(channel->addr.url.chars = heap_alloc( endpoint->url.length * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
625 memcpy( channel->addr.url.chars, endpoint->url.chars, endpoint->url.length * sizeof(WCHAR) );
626 channel->addr.url.length = endpoint->url.length;
628 channel->state = WS_CHANNEL_STATE_OPEN;
629 return S_OK;
632 /**************************************************************************
633 * WsOpenChannel [webservices.@]
635 HRESULT WINAPI WsOpenChannel( WS_CHANNEL *handle, const WS_ENDPOINT_ADDRESS *endpoint,
636 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
638 struct channel *channel = (struct channel *)handle;
639 HRESULT hr;
641 TRACE( "%p %p %p %p\n", handle, endpoint, ctx, error );
642 if (error) FIXME( "ignoring error parameter\n" );
643 if (ctx) FIXME( "ignoring ctx parameter\n" );
645 if (!channel || !endpoint) return E_INVALIDARG;
647 EnterCriticalSection( &channel->cs );
649 if (channel->magic != CHANNEL_MAGIC)
651 LeaveCriticalSection( &channel->cs );
652 return E_INVALIDARG;
655 if (channel->state != WS_CHANNEL_STATE_CREATED)
657 LeaveCriticalSection( &channel->cs );
658 return WS_E_INVALID_OPERATION;
661 hr = open_channel( channel, endpoint );
663 LeaveCriticalSection( &channel->cs );
664 return hr;
667 static void close_channel( struct channel *channel )
669 reset_channel( channel );
670 channel->state = WS_CHANNEL_STATE_CLOSED;
673 /**************************************************************************
674 * WsCloseChannel [webservices.@]
676 HRESULT WINAPI WsCloseChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
678 struct channel *channel = (struct channel *)handle;
680 TRACE( "%p %p %p\n", handle, ctx, error );
681 if (error) FIXME( "ignoring error parameter\n" );
682 if (ctx) FIXME( "ignoring ctx parameter\n" );
684 if (!channel) return E_INVALIDARG;
686 EnterCriticalSection( &channel->cs );
688 if (channel->magic != CHANNEL_MAGIC)
690 LeaveCriticalSection( &channel->cs );
691 return E_INVALIDARG;
694 close_channel( channel );
696 LeaveCriticalSection( &channel->cs );
697 return S_OK;
700 static HRESULT parse_http_url( const WCHAR *url, ULONG len, URL_COMPONENTS *uc )
702 HRESULT hr = E_OUTOFMEMORY;
703 WCHAR *tmp;
704 DWORD err;
706 memset( uc, 0, sizeof(*uc) );
707 uc->dwStructSize = sizeof(*uc);
708 uc->dwHostNameLength = 128;
709 uc->lpszHostName = heap_alloc( uc->dwHostNameLength * sizeof(WCHAR) );
710 uc->dwUrlPathLength = 128;
711 uc->lpszUrlPath = heap_alloc( uc->dwUrlPathLength * sizeof(WCHAR) );
712 uc->dwExtraInfoLength = 128;
713 uc->lpszExtraInfo = heap_alloc( uc->dwExtraInfoLength * sizeof(WCHAR) );
714 if (!uc->lpszHostName || !uc->lpszUrlPath || !uc->lpszExtraInfo) goto error;
716 if (!WinHttpCrackUrl( url, len, ICU_DECODE, uc ))
718 if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
720 hr = HRESULT_FROM_WIN32( err );
721 goto error;
723 if (!(tmp = heap_realloc( uc->lpszHostName, uc->dwHostNameLength * sizeof(WCHAR) ))) goto error;
724 uc->lpszHostName = tmp;
725 if (!(tmp = heap_realloc( uc->lpszUrlPath, uc->dwUrlPathLength * sizeof(WCHAR) ))) goto error;
726 uc->lpszUrlPath = tmp;
727 if (!(tmp = heap_realloc( uc->lpszExtraInfo, uc->dwExtraInfoLength * sizeof(WCHAR) ))) goto error;
728 uc->lpszExtraInfo = tmp;
729 WinHttpCrackUrl( url, len, ICU_DECODE, uc );
732 return S_OK;
734 error:
735 heap_free( uc->lpszHostName );
736 heap_free( uc->lpszUrlPath );
737 heap_free( uc->lpszExtraInfo );
738 return hr;
741 static HRESULT connect_channel_http( struct channel *channel )
743 static const WCHAR agentW[] =
744 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
745 HINTERNET ses = NULL, con = NULL;
746 URL_COMPONENTS uc;
747 HRESULT hr;
749 if (channel->u.http.connect) return S_OK;
751 if ((hr = parse_http_url( channel->addr.url.chars, channel->addr.url.length, &uc )) != S_OK) return hr;
752 if (!(channel->u.http.path = heap_alloc( (uc.dwUrlPathLength + uc.dwExtraInfoLength + 1) * sizeof(WCHAR) )))
754 hr = E_OUTOFMEMORY;
755 goto done;
757 else
759 strcpyW( channel->u.http.path, uc.lpszUrlPath );
760 if (uc.dwExtraInfoLength) strcatW( channel->u.http.path, uc.lpszExtraInfo );
763 channel->u.http.flags = WINHTTP_FLAG_REFRESH;
764 switch (uc.nScheme)
766 case INTERNET_SCHEME_HTTP: break;
767 case INTERNET_SCHEME_HTTPS:
768 channel->u.http.flags |= WINHTTP_FLAG_SECURE;
769 break;
771 default:
772 hr = WS_E_INVALID_ENDPOINT_URL;
773 goto done;
776 if (!(ses = WinHttpOpen( agentW, 0, NULL, NULL, 0 )))
778 hr = HRESULT_FROM_WIN32( GetLastError() );
779 goto done;
781 if (!(con = WinHttpConnect( ses, uc.lpszHostName, uc.nPort, 0 )))
783 hr = HRESULT_FROM_WIN32( GetLastError() );
784 goto done;
787 channel->u.http.session = ses;
788 channel->u.http.connect = con;
790 done:
791 if (hr != S_OK)
793 WinHttpCloseHandle( con );
794 WinHttpCloseHandle( ses );
796 heap_free( uc.lpszHostName );
797 heap_free( uc.lpszUrlPath );
798 heap_free( uc.lpszExtraInfo );
799 return hr;
802 static HRESULT connect_channel_tcp( struct channel *channel )
804 struct sockaddr_storage storage;
805 struct sockaddr *addr = (struct sockaddr *)&storage;
806 int addr_len;
807 WS_URL_SCHEME_TYPE scheme;
808 WCHAR *host;
809 USHORT port;
810 HRESULT hr;
812 if (channel->u.tcp.socket != -1) return S_OK;
814 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
815 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
817 heap_free( host );
818 return WS_E_INVALID_ENDPOINT_URL;
821 winsock_init();
823 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
824 heap_free( host );
825 if (hr != S_OK) return hr;
827 if ((channel->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
828 return HRESULT_FROM_WIN32( WSAGetLastError() );
830 if (connect( channel->u.tcp.socket, addr, addr_len ) < 0)
832 closesocket( channel->u.tcp.socket );
833 channel->u.tcp.socket = -1;
834 return HRESULT_FROM_WIN32( WSAGetLastError() );
837 return S_OK;
840 static HRESULT connect_channel_udp( struct channel *channel )
842 struct sockaddr_storage storage;
843 struct sockaddr *addr = (struct sockaddr *)&storage;
844 int addr_len;
845 WS_URL_SCHEME_TYPE scheme;
846 WCHAR *host;
847 USHORT port;
848 HRESULT hr;
850 if (channel->u.udp.socket != -1) return S_OK;
852 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
853 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
855 heap_free( host );
856 return WS_E_INVALID_ENDPOINT_URL;
859 winsock_init();
861 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
862 heap_free( host );
863 if (hr != S_OK) return hr;
865 if ((channel->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
866 return HRESULT_FROM_WIN32( WSAGetLastError() );
868 if (connect( channel->u.udp.socket, addr, addr_len ) < 0)
870 closesocket( channel->u.udp.socket );
871 channel->u.udp.socket = -1;
872 return HRESULT_FROM_WIN32( WSAGetLastError() );
875 return S_OK;
878 static HRESULT connect_channel( struct channel *channel )
880 switch (channel->binding)
882 case WS_HTTP_CHANNEL_BINDING:
883 return connect_channel_http( channel );
885 case WS_TCP_CHANNEL_BINDING:
886 return connect_channel_tcp( channel );
888 case WS_UDP_CHANNEL_BINDING:
889 return connect_channel_udp( channel );
891 default:
892 ERR( "unhandled binding %u\n", channel->binding );
893 return E_NOTIMPL;
897 static HRESULT write_message( WS_MESSAGE *handle, WS_XML_WRITER *writer, const WS_ELEMENT_DESCRIPTION *desc,
898 WS_WRITE_OPTION option, const void *body, ULONG size )
900 HRESULT hr;
901 if ((hr = WsWriteEnvelopeStart( handle, writer, NULL, NULL, NULL )) != S_OK) return hr;
902 if ((hr = WsWriteBody( handle, desc, option, body, size, NULL )) != S_OK) return hr;
903 return WsWriteEnvelopeEnd( handle, NULL );
906 static HRESULT send_message_http( HINTERNET request, BYTE *data, ULONG len )
908 if (!WinHttpSendRequest( request, NULL, 0, data, len, len, 0 ))
909 return HRESULT_FROM_WIN32( GetLastError() );
911 if (!WinHttpReceiveResponse( request, NULL ))
912 return HRESULT_FROM_WIN32( GetLastError() );
913 return S_OK;
916 static HRESULT send_byte( SOCKET socket, BYTE byte )
918 int count = send( socket, (char *)&byte, 1, 0 );
919 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
920 if (count != 1) return WS_E_OTHER;
921 return S_OK;
924 static HRESULT send_bytes( SOCKET socket, BYTE *bytes, int len )
926 int count = send( socket, (char *)bytes, len, 0 );
927 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
928 if (count != len) return WS_E_OTHER;
929 return S_OK;
932 static HRESULT send_size( SOCKET socket, ULONG size )
934 HRESULT hr;
935 if (size < 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) < 0x80) return send_byte( socket, size );
940 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
941 if ((size >>= 7) < 0x80) return send_byte( socket, size );
942 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
943 if ((size >>= 7) < 0x08) return send_byte( socket, size );
944 return E_INVALIDARG;
947 enum frame_record_type
949 FRAME_RECORD_TYPE_VERSION,
950 FRAME_RECORD_TYPE_MODE,
951 FRAME_RECORD_TYPE_VIA,
952 FRAME_RECORD_TYPE_KNOWN_ENCODING,
953 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING,
954 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE,
955 FRAME_RECORD_TYPE_SIZED_ENVELOPE,
956 FRAME_RECORD_TYPE_END,
957 FRAME_RECORD_TYPE_FAULT,
958 FRAME_RECORD_TYPE_UPGRADE_REQUEST,
959 FRAME_RECORD_TYPE_UPGRADE_RESPONSE,
960 FRAME_RECORD_TYPE_PREAMBLE_ACK,
961 FRAME_RECORD_TYPE_PREAMBLE_END,
964 static inline ULONG size_length( ULONG size )
966 if (size < 0x80) return 1;
967 if (size < 0x4000) return 2;
968 if (size < 0x200000) return 3;
969 if (size < 0x10000000) return 4;
970 return 5;
973 static ULONG string_table_size( const WS_XML_DICTIONARY *dict )
975 ULONG i, size = 0;
976 for (i = 0; i < dict->stringCount; i++)
977 size += size_length( dict->strings[i].length ) + dict->strings[i].length;
978 return size;
981 static HRESULT send_string_table( SOCKET socket, const WS_XML_DICTIONARY *dict )
983 ULONG i;
984 HRESULT hr;
985 for (i = 0; i < dict->stringCount; i++)
987 if ((hr = send_size( socket, dict->strings[i].length )) != S_OK) return hr;
988 if ((hr = send_bytes( socket, dict->strings[i].bytes, dict->strings[i].length )) != S_OK) return hr;
990 return S_OK;
993 static HRESULT string_to_utf8( const WS_STRING *str, unsigned char **ret, int *len )
995 *len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
996 if (!(*ret = heap_alloc( *len ))) return E_OUTOFMEMORY;
997 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)*ret, *len, NULL, NULL );
998 return S_OK;
1001 enum session_mode
1003 SESSION_MODE_INVALID = 0,
1004 SESSION_MODE_SINGLETON = 1,
1005 SESSION_MODE_DUPLEX = 2,
1006 SESSION_MODE_SIMPLEX = 3,
1009 static enum session_mode map_channel_type( struct channel *channel )
1011 switch (channel->type)
1013 case WS_CHANNEL_TYPE_DUPLEX_SESSION: return SESSION_MODE_DUPLEX;
1014 default:
1015 FIXME( "unhandled channel type %08x\n", channel->type );
1016 return SESSION_MODE_INVALID;
1020 enum known_encoding
1022 KNOWN_ENCODING_SOAP11_UTF8 = 0x00,
1023 KNOWN_ENCODING_SOAP11_UTF16 = 0x01,
1024 KNOWN_ENCODING_SOAP11_UTF16LE = 0x02,
1025 KNOWN_ENCODING_SOAP12_UTF8 = 0x03,
1026 KNOWN_ENCODING_SOAP12_UTF16 = 0x04,
1027 KNOWN_ENCODING_SOAP12_UTF16LE = 0x05,
1028 KNOWN_ENCODING_SOAP12_MTOM = 0x06,
1029 KNOWN_ENCODING_SOAP12_BINARY = 0x07,
1030 KNOWN_ENCODING_SOAP12_BINARY_SESSION = 0x08,
1033 static enum known_encoding map_channel_encoding( struct channel *channel )
1035 WS_ENVELOPE_VERSION version;
1037 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version, sizeof(version) );
1039 switch (version)
1041 case WS_ENVELOPE_VERSION_SOAP_1_1:
1042 switch (channel->encoding)
1044 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP11_UTF8;
1045 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP11_UTF16LE;
1046 default:
1047 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1048 return 0;
1050 case WS_ENVELOPE_VERSION_SOAP_1_2:
1051 switch (channel->encoding)
1053 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP12_UTF8;
1054 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP12_UTF16LE;
1055 case WS_ENCODING_XML_BINARY_1: return KNOWN_ENCODING_SOAP12_BINARY;
1056 case WS_ENCODING_XML_BINARY_SESSION_1: return KNOWN_ENCODING_SOAP12_BINARY_SESSION;
1057 default:
1058 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
1059 return 0;
1061 default:
1062 ERR( "unhandled version %u\n", version );
1063 return 0;
1067 #define FRAME_VERSION_MAJOR 1
1068 #define FRAME_VERSION_MINOR 1
1070 static HRESULT send_preamble( struct channel *channel )
1072 unsigned char *url;
1073 HRESULT hr;
1074 int len;
1076 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VERSION )) != S_OK) return hr;
1077 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MAJOR )) != S_OK) return hr;
1078 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MINOR )) != S_OK) return hr;
1080 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_MODE )) != S_OK) return hr;
1081 if ((hr = send_byte( channel->u.tcp.socket, map_channel_type(channel) )) != S_OK) return hr;
1083 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VIA )) != S_OK) return hr;
1084 if ((hr = string_to_utf8( &channel->addr.url, &url, &len )) != S_OK) return hr;
1085 if ((hr = send_size( channel->u.tcp.socket, len )) != S_OK) goto done;
1086 if ((hr = send_bytes( channel->u.tcp.socket, url, len )) != S_OK) goto done;
1088 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_KNOWN_ENCODING )) != S_OK) goto done;
1089 if ((hr = send_byte( channel->u.tcp.socket, map_channel_encoding(channel) )) != S_OK) goto done;
1090 hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_END );
1092 done:
1093 heap_free( url );
1094 return hr;
1097 static HRESULT receive_bytes( struct channel *channel, unsigned char *bytes, int len )
1099 int count = recv( channel->u.tcp.socket, (char *)bytes, len, 0 );
1100 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1101 if (count != len) return WS_E_INVALID_FORMAT;
1102 return S_OK;
1105 static HRESULT receive_preamble_ack( struct channel *channel )
1107 unsigned char byte;
1108 HRESULT hr;
1110 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1111 if (byte != FRAME_RECORD_TYPE_PREAMBLE_ACK) return WS_E_INVALID_FORMAT;
1112 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1113 return S_OK;
1116 static HRESULT send_sized_envelope( struct channel *channel, BYTE *data, ULONG len )
1118 ULONG table_size = string_table_size( &channel->dict_send.dict );
1119 HRESULT hr;
1121 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_SIZED_ENVELOPE )) != S_OK) return hr;
1122 if ((hr = send_size( channel->u.tcp.socket, size_length(table_size) + table_size + len )) != S_OK) return hr;
1123 if ((hr = send_size( channel->u.tcp.socket, table_size )) != S_OK) return hr;
1124 if ((hr = send_string_table( channel->u.tcp.socket, &channel->dict_send.dict )) != S_OK) return hr;
1125 return send_bytes( channel->u.tcp.socket, data, len );
1128 static HRESULT open_http_request( struct channel *channel, HINTERNET *req )
1130 static const WCHAR postW[] = {'P','O','S','T',0};
1131 if ((*req = WinHttpOpenRequest( channel->u.http.connect, postW, channel->u.http.path,
1132 NULL, NULL, NULL, channel->u.http.flags ))) return S_OK;
1133 return HRESULT_FROM_WIN32( GetLastError() );
1136 static HRESULT send_message( struct channel *channel, WS_MESSAGE *msg )
1138 WS_XML_WRITER *writer;
1139 WS_BYTES buf;
1140 HRESULT hr;
1142 channel->msg = msg;
1143 if ((hr = connect_channel( channel )) != S_OK) return hr;
1145 WsGetMessageProperty( channel->msg, WS_MESSAGE_PROPERTY_BODY_WRITER, &writer, sizeof(writer), NULL );
1146 WsGetWriterProperty( writer, WS_XML_WRITER_PROPERTY_BYTES, &buf, sizeof(buf), NULL );
1148 switch (channel->binding)
1150 case WS_HTTP_CHANNEL_BINDING:
1151 if (channel->u.http.request)
1153 WinHttpCloseHandle( channel->u.http.request );
1154 channel->u.http.request = NULL;
1156 if ((hr = open_http_request( channel, &channel->u.http.request )) != S_OK) return hr;
1157 if ((hr = message_insert_http_headers( msg, channel->u.http.request )) != S_OK) return hr;
1158 return send_message_http( channel->u.http.request, buf.bytes, buf.length );
1160 case WS_TCP_CHANNEL_BINDING:
1161 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1163 switch (channel->session_state)
1165 case SESSION_STATE_UNINITIALIZED:
1166 if ((hr = send_preamble( channel )) != S_OK) return hr;
1167 if ((hr = receive_preamble_ack( channel )) != S_OK) return hr;
1168 /* fall through */
1170 case SESSION_STATE_SETUP_COMPLETE:
1171 return send_sized_envelope( channel, buf.bytes, buf.length );
1173 default:
1174 ERR( "unhandled session state %u\n", channel->session_state );
1175 return WS_E_OTHER;
1178 return send_bytes( channel->u.tcp.socket, buf.bytes, buf.length );
1180 case WS_UDP_CHANNEL_BINDING:
1181 return send_bytes( channel->u.udp.socket, buf.bytes, buf.length );
1183 default:
1184 ERR( "unhandled binding %u\n", channel->binding );
1185 return E_NOTIMPL;
1189 HRESULT channel_send_message( WS_CHANNEL *handle, WS_MESSAGE *msg )
1191 struct channel *channel = (struct channel *)handle;
1192 HRESULT hr;
1194 EnterCriticalSection( &channel->cs );
1196 if (channel->magic != CHANNEL_MAGIC)
1198 LeaveCriticalSection( &channel->cs );
1199 return E_INVALIDARG;
1202 hr = send_message( channel, msg );
1204 LeaveCriticalSection( &channel->cs );
1205 return hr;
1208 static HRESULT CALLBACK dict_cb( void *state, const WS_XML_STRING *str, BOOL *found, ULONG *id, WS_ERROR *error )
1210 struct dictionary *dict = state;
1211 HRESULT hr = S_OK;
1212 BYTE *bytes;
1213 int index;
1215 if ((index = find_string( dict, str->bytes, str->length, id )) == -1)
1217 *found = TRUE;
1218 return S_OK;
1221 if (!(bytes = heap_alloc( str->length ))) return E_OUTOFMEMORY;
1222 memcpy( bytes, str->bytes, str->length );
1223 if ((hr = insert_string( dict, bytes, str->length, index, id )) == S_OK)
1225 *found = TRUE;
1226 return S_OK;
1228 heap_free( bytes );
1230 *found = FALSE;
1231 return hr;
1234 static HRESULT init_writer( struct channel *channel )
1236 WS_XML_WRITER_BUFFER_OUTPUT buf = {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER}};
1237 WS_XML_WRITER_TEXT_ENCODING text = {{WS_XML_WRITER_ENCODING_TYPE_TEXT}, WS_CHARSET_UTF8};
1238 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1239 HRESULT hr;
1241 if (!channel->writer && (hr = WsCreateWriter( NULL, 0, &channel->writer, NULL )) != S_OK) return hr;
1243 switch (channel->encoding)
1245 case WS_ENCODING_XML_UTF8:
1246 return WsSetOutput( channel->writer, &text.encoding, &buf.output, NULL, 0, NULL );
1248 case WS_ENCODING_XML_BINARY_SESSION_1:
1249 clear_dict( &channel->dict_send );
1250 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1251 bin.dynamicStringCallback = dict_cb;
1252 bin.dynamicStringCallbackState = &channel->dict_send;
1253 if ((hr = WsSetOutput( channel->writer, &bin.encoding, &buf.output, NULL, 0, NULL )) != S_OK) return hr;
1254 return writer_enable_lookup( channel->writer );
1256 case WS_ENCODING_XML_BINARY_1:
1257 return WsSetOutput( channel->writer, &bin.encoding, &buf.output, NULL, 0, NULL );
1259 default:
1260 FIXME( "unhandled encoding %u\n", channel->encoding );
1261 return WS_E_NOT_SUPPORTED;
1265 /**************************************************************************
1266 * WsSendMessage [webservices.@]
1268 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1269 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1270 WS_ERROR *error )
1272 struct channel *channel = (struct channel *)handle;
1273 HRESULT hr;
1275 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1276 if (error) FIXME( "ignoring error parameter\n" );
1277 if (ctx) FIXME( "ignoring ctx parameter\n" );
1279 if (!channel || !msg || !desc) return E_INVALIDARG;
1281 EnterCriticalSection( &channel->cs );
1283 if (channel->magic != CHANNEL_MAGIC)
1285 LeaveCriticalSection( &channel->cs );
1286 return E_INVALIDARG;
1289 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK) goto done;
1290 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1291 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1293 if ((hr = init_writer( channel )) != S_OK) goto done;
1294 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1295 goto done;
1296 hr = send_message( channel, msg );
1298 done:
1299 LeaveCriticalSection( &channel->cs );
1300 return hr;
1303 /**************************************************************************
1304 * WsSendReplyMessage [webservices.@]
1306 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1307 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1308 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1310 struct channel *channel = (struct channel *)handle;
1311 GUID req_id;
1312 HRESULT hr;
1314 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1315 if (error) FIXME( "ignoring error parameter\n" );
1316 if (ctx) FIXME( "ignoring ctx parameter\n" );
1318 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1320 EnterCriticalSection( &channel->cs );
1322 if (channel->magic != CHANNEL_MAGIC)
1324 LeaveCriticalSection( &channel->cs );
1325 return E_INVALIDARG;
1328 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1329 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1330 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1331 if ((hr = message_get_id( request, &req_id )) != S_OK) goto done;
1332 if ((hr = message_set_request_id( msg, &req_id )) != S_OK) goto done;
1334 if ((hr = init_writer( channel )) != S_OK) goto done;
1335 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1336 goto done;
1337 hr = send_message( channel, msg );
1339 done:
1340 LeaveCriticalSection( &channel->cs );
1341 FIXME( "returning %08x\n", hr );
1342 return hr;
1345 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1347 if (!channel->read_buf)
1349 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1350 channel->read_buflen = size;
1351 return S_OK;
1353 if (channel->read_buflen < size)
1355 char *tmp;
1356 ULONG new_size = max( size, channel->read_buflen * 2 );
1357 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1358 channel->read_buf = tmp;
1359 channel->read_buflen = new_size;
1361 return S_OK;
1364 static HRESULT init_reader( struct channel *channel )
1366 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1367 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1368 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1369 WS_XML_READER_ENCODING *encoding;
1370 HRESULT hr;
1372 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1374 switch (channel->encoding)
1376 case WS_ENCODING_XML_UTF8:
1377 text.charSet = WS_CHARSET_UTF8;
1378 encoding = &text.encoding;
1379 break;
1381 case WS_ENCODING_XML_BINARY_SESSION_1:
1382 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1383 bin.dynamicDictionary = &channel->dict_recv.dict;
1384 /* fall through */
1386 case WS_ENCODING_XML_BINARY_1:
1387 encoding = &bin.encoding;
1388 break;
1390 default:
1391 FIXME( "unhandled encoding %u\n", channel->encoding );
1392 return WS_E_NOT_SUPPORTED;
1395 buf.encodedData = channel->read_buf;
1396 buf.encodedDataSize = channel->read_size;
1397 return WsSetInput( channel->reader, encoding, &buf.input, NULL, 0, NULL );
1400 #define INITIAL_READ_BUFFER_SIZE 4096
1401 static HRESULT receive_message_http( struct channel *channel )
1403 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1404 ULONG max_len;
1405 HRESULT hr;
1407 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1408 &max_len, sizeof(max_len) );
1410 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1411 channel->read_size = 0;
1412 for (;;)
1414 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1416 return HRESULT_FROM_WIN32( GetLastError() );
1418 if (!len) break;
1419 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1420 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1422 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1424 return HRESULT_FROM_WIN32( GetLastError() );
1426 if (!bytes_read) break;
1427 channel->read_size += bytes_read;
1428 offset += bytes_read;
1431 return init_reader( channel );
1434 static HRESULT receive_message_unsized( struct channel *channel, SOCKET socket )
1436 int bytes_read;
1437 ULONG max_len;
1438 HRESULT hr;
1440 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1441 &max_len, sizeof(max_len) );
1443 if ((hr = resize_read_buffer( channel, max_len )) != S_OK) return hr;
1445 channel->read_size = 0;
1446 if ((bytes_read = recv( socket, channel->read_buf, max_len, 0 )) < 0)
1448 return HRESULT_FROM_WIN32( WSAGetLastError() );
1450 channel->read_size = bytes_read;
1451 return S_OK;
1454 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1456 unsigned int offset = 0, to_read = size;
1457 int bytes_read;
1458 HRESULT hr;
1460 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1462 channel->read_size = 0;
1463 while (channel->read_size < size)
1465 if ((bytes_read = recv( channel->u.tcp.socket, channel->read_buf + offset, to_read, 0 )) < 0)
1467 return HRESULT_FROM_WIN32( WSAGetLastError() );
1469 if (!bytes_read) break;
1470 channel->read_size += bytes_read;
1471 to_read -= bytes_read;
1472 offset += bytes_read;
1474 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1475 return S_OK;
1478 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1480 unsigned char byte;
1481 HRESULT hr;
1483 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1484 *size = byte & 0x7f;
1485 if (!(byte & 0x80)) return S_OK;
1487 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1488 *size += (byte & 0x7f) << 7;
1489 if (!(byte & 0x80)) return S_OK;
1491 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1492 *size += (byte & 0x7f) << 14;
1493 if (!(byte & 0x80)) return S_OK;
1495 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1496 *size += (byte & 0x7f) << 21;
1497 if (!(byte & 0x80)) return S_OK;
1499 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1500 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1501 *size += byte << 28;
1502 return S_OK;
1505 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1507 switch (encoding)
1509 case KNOWN_ENCODING_SOAP11_UTF8:
1510 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1511 case KNOWN_ENCODING_SOAP11_UTF16:
1512 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1513 case KNOWN_ENCODING_SOAP11_UTF16LE:
1514 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1515 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1516 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1517 default:
1518 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1519 return WS_ENCODING_XML_UTF8;
1523 static HRESULT receive_preamble( struct channel *channel )
1525 unsigned char type;
1526 HRESULT hr;
1528 for (;;)
1530 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1531 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1532 switch (type)
1534 case FRAME_RECORD_TYPE_VERSION:
1536 unsigned char major, minor;
1537 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1538 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1539 TRACE( "major %u minor %u\n", major, major );
1540 break;
1542 case FRAME_RECORD_TYPE_MODE:
1544 unsigned char mode;
1545 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1546 TRACE( "mode %u\n", mode );
1547 break;
1549 case FRAME_RECORD_TYPE_VIA:
1551 unsigned int size;
1552 unsigned char *url;
1554 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1555 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1556 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1558 heap_free( url );
1559 return hr;
1561 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1562 heap_free( url ); /* FIXME: verify */
1563 break;
1565 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1567 unsigned char encoding;
1568 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1569 TRACE( "encoding %u\n", encoding );
1570 channel->encoding = map_known_encoding( encoding );
1571 break;
1573 default:
1574 WARN( "unhandled record type %u\n", type );
1575 return WS_E_INVALID_FORMAT;
1579 return S_OK;
1582 static HRESULT receive_sized_envelope( struct channel *channel )
1584 unsigned char type;
1585 unsigned int size;
1586 HRESULT hr;
1588 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1589 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1590 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1591 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1592 return S_OK;
1595 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1597 const BYTE *buf = *ptr;
1599 if (len < 1) return WS_E_INVALID_FORMAT;
1600 *size = buf[0] & 0x7f;
1601 if (!(buf[0] & 0x80))
1603 *ptr += 1;
1604 return S_OK;
1606 if (len < 2) return WS_E_INVALID_FORMAT;
1607 *size += (buf[1] & 0x7f) << 7;
1608 if (!(buf[1] & 0x80))
1610 *ptr += 2;
1611 return S_OK;
1613 if (len < 3) return WS_E_INVALID_FORMAT;
1614 *size += (buf[2] & 0x7f) << 14;
1615 if (!(buf[2] & 0x80))
1617 *ptr += 3;
1618 return S_OK;
1620 if (len < 4) return WS_E_INVALID_FORMAT;
1621 *size += (buf[3] & 0x7f) << 21;
1622 if (!(buf[3] & 0x80))
1624 *ptr += 4;
1625 return S_OK;
1627 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1628 *size += buf[4] << 28;
1629 *ptr += 5;
1630 return S_OK;
1633 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1635 ULONG size, strings_size, strings_offset;
1636 const BYTE *ptr = buf;
1637 BYTE *bytes;
1638 int index;
1639 HRESULT hr;
1641 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1642 strings_offset = ptr - buf;
1643 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1644 *used = strings_offset + strings_size;
1645 if (!strings_size) return S_OK;
1647 UuidCreate( &dict->dict.guid );
1648 dict->dict.isConst = FALSE;
1650 buflen -= strings_offset;
1651 ptr = buf + strings_offset;
1652 while (ptr < buf + strings_size)
1654 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1656 clear_dict( dict );
1657 return hr;
1659 if (size > buflen)
1661 clear_dict( dict );
1662 return WS_E_INVALID_FORMAT;
1664 buflen -= size;
1665 if (!(bytes = heap_alloc( size )))
1667 hr = E_OUTOFMEMORY;
1668 goto error;
1670 memcpy( bytes, ptr, size );
1671 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1673 heap_free( bytes );
1674 ptr += size;
1675 continue;
1677 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1679 heap_free( bytes );
1680 clear_dict( dict );
1681 return hr;
1683 ptr += size;
1685 return S_OK;
1687 error:
1688 clear_dict( dict );
1689 return hr;
1692 static HRESULT send_preamble_ack( struct channel *channel )
1694 HRESULT hr;
1695 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1696 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1697 return S_OK;
1700 static HRESULT receive_message_session( struct channel *channel )
1702 HRESULT hr;
1704 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1705 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1707 ULONG size;
1708 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict_recv,
1709 &size )) != S_OK) return hr;
1710 channel->read_size -= size;
1711 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1714 return init_reader( channel );
1717 static HRESULT receive_message_sock( struct channel *channel, SOCKET socket )
1719 HRESULT hr;
1720 if ((hr = receive_message_unsized( channel, socket )) != S_OK) return hr;
1721 return init_reader( channel );
1724 static HRESULT receive_message_bytes( struct channel *channel )
1726 HRESULT hr;
1727 if ((hr = connect_channel( channel )) != S_OK) return hr;
1729 switch (channel->binding)
1731 case WS_HTTP_CHANNEL_BINDING:
1732 return receive_message_http( channel );
1734 case WS_TCP_CHANNEL_BINDING:
1735 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1737 switch (channel->session_state)
1739 case SESSION_STATE_UNINITIALIZED:
1740 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1741 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1742 /* fall through */
1744 case SESSION_STATE_SETUP_COMPLETE:
1745 return receive_message_session( channel );
1747 default:
1748 ERR( "unhandled session state %u\n", channel->session_state );
1749 return WS_E_OTHER;
1752 return receive_message_sock( channel, channel->u.tcp.socket );
1754 case WS_UDP_CHANNEL_BINDING:
1755 return receive_message_sock( channel, channel->u.udp.socket );
1757 default:
1758 ERR( "unhandled binding %u\n", channel->binding );
1759 return E_NOTIMPL;
1763 HRESULT channel_receive_message( WS_CHANNEL *handle )
1765 struct channel *channel = (struct channel *)handle;
1766 HRESULT hr;
1768 EnterCriticalSection( &channel->cs );
1770 if (channel->magic != CHANNEL_MAGIC)
1772 LeaveCriticalSection( &channel->cs );
1773 return E_INVALIDARG;
1776 hr = receive_message_bytes( channel );
1778 LeaveCriticalSection( &channel->cs );
1779 return hr;
1782 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1784 struct channel *channel = (struct channel *)handle;
1786 EnterCriticalSection( &channel->cs );
1788 if (channel->magic != CHANNEL_MAGIC)
1790 LeaveCriticalSection( &channel->cs );
1791 return E_INVALIDARG;
1794 *reader = channel->reader;
1796 LeaveCriticalSection( &channel->cs );
1797 return S_OK;
1800 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1801 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1803 HRESULT hr;
1804 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1805 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1806 return WsReadEnvelopeEnd( handle, NULL );
1809 static HRESULT receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1810 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1811 void *value, ULONG size, ULONG *index )
1813 HRESULT hr;
1814 ULONG i;
1816 if ((hr = receive_message_bytes( channel )) != S_OK) return hr;
1817 for (i = 0; i < count; i++)
1819 const WS_ELEMENT_DESCRIPTION *body = desc[i]->bodyElementDescription;
1820 if ((hr = read_message( msg, channel->reader, body, read_option, heap, value, size )) == S_OK)
1822 if (index) *index = i;
1823 break;
1825 if ((hr = WsResetMessage( msg, NULL )) != S_OK) return hr;
1826 if ((hr = init_reader( channel )) != S_OK) return hr;
1828 return (i == count) ? WS_E_INVALID_FORMAT : S_OK;
1831 struct receive_message
1833 struct task task;
1834 struct channel *channel;
1835 WS_MESSAGE *msg;
1836 const WS_MESSAGE_DESCRIPTION **desc;
1837 ULONG count;
1838 WS_RECEIVE_OPTION option;
1839 WS_READ_OPTION read_option;
1840 WS_HEAP *heap;
1841 void *value;
1842 ULONG size;
1843 ULONG *index;
1844 WS_ASYNC_CONTEXT ctx;
1847 static void receive_message_proc( struct task *task )
1849 struct receive_message *r = (struct receive_message *)task;
1850 HRESULT hr;
1852 hr = receive_message( r->channel, r->msg, r->desc, r->count, r->option, r->read_option, r->heap, r->value,
1853 r->size, r->index );
1855 TRACE( "calling %p(%08x)\n", r->ctx.callback, hr );
1856 r->ctx.callback( hr, WS_LONG_CALLBACK, r->ctx.callbackState );
1857 TRACE( "%p returned\n", r->ctx.callback );
1860 static HRESULT queue_receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1861 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option,
1862 WS_HEAP *heap, void *value, ULONG size, ULONG *index,
1863 const WS_ASYNC_CONTEXT *ctx )
1865 struct receive_message *r;
1867 if (!(r = heap_alloc( sizeof(*r) ))) return E_OUTOFMEMORY;
1868 r->task.proc = receive_message_proc;
1869 r->channel = channel;
1870 r->msg = msg;
1871 r->desc = desc;
1872 r->count = count;
1873 r->option = option;
1874 r->read_option = read_option;
1875 r->heap = heap;
1876 r->value = value;
1877 r->size = size;
1878 r->index = index;
1879 r->ctx = *ctx;
1880 return queue_task( &channel->recv_q, &r->task );
1883 /**************************************************************************
1884 * WsReceiveMessage [webservices.@]
1886 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1887 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1888 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1890 struct channel *channel = (struct channel *)handle;
1891 HRESULT hr;
1893 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1894 value, size, index, ctx, error );
1895 if (error) FIXME( "ignoring error parameter\n" );
1897 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1899 EnterCriticalSection( &channel->cs );
1901 if (channel->magic != CHANNEL_MAGIC)
1903 LeaveCriticalSection( &channel->cs );
1904 return E_INVALIDARG;
1907 if (ctx)
1908 hr = queue_receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index, ctx );
1909 else
1910 hr = receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index );
1912 LeaveCriticalSection( &channel->cs );
1913 return hr;
1916 /**************************************************************************
1917 * WsReadMessageStart [webservices.@]
1919 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1920 WS_ERROR *error )
1922 struct channel *channel = (struct channel *)handle;
1923 HRESULT hr;
1925 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1926 if (error) FIXME( "ignoring error parameter\n" );
1927 if (ctx) FIXME( "ignoring ctx parameter\n" );
1929 if (!channel || !msg) return E_INVALIDARG;
1931 EnterCriticalSection( &channel->cs );
1933 if (channel->magic != CHANNEL_MAGIC)
1935 LeaveCriticalSection( &channel->cs );
1936 return E_INVALIDARG;
1939 if ((hr = receive_message_bytes( channel )) == S_OK)
1941 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
1944 LeaveCriticalSection( &channel->cs );
1945 return hr;
1948 /**************************************************************************
1949 * WsReadMessageEnd [webservices.@]
1951 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1952 WS_ERROR *error )
1954 struct channel *channel = (struct channel *)handle;
1955 HRESULT hr;
1957 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1958 if (error) FIXME( "ignoring error parameter\n" );
1959 if (ctx) FIXME( "ignoring ctx parameter\n" );
1961 if (!channel || !msg) return E_INVALIDARG;
1963 EnterCriticalSection( &channel->cs );
1965 if (channel->magic != CHANNEL_MAGIC)
1967 LeaveCriticalSection( &channel->cs );
1968 return E_INVALIDARG;
1971 hr = WsReadEnvelopeEnd( msg, NULL );
1973 LeaveCriticalSection( &channel->cs );
1974 return hr;
1977 /**************************************************************************
1978 * WsWriteMessageStart [webservices.@]
1980 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1981 WS_ERROR *error )
1983 struct channel *channel = (struct channel *)handle;
1984 HRESULT hr;
1986 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1987 if (error) FIXME( "ignoring error parameter\n" );
1988 if (ctx) FIXME( "ignoring ctx parameter\n" );
1990 if (!channel || !msg) return E_INVALIDARG;
1992 EnterCriticalSection( &channel->cs );
1994 if (channel->magic != CHANNEL_MAGIC)
1996 LeaveCriticalSection( &channel->cs );
1997 return E_INVALIDARG;
2000 if ((hr = init_writer( channel )) != S_OK) goto done;
2001 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
2002 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
2004 done:
2005 LeaveCriticalSection( &channel->cs );
2006 return hr;
2009 /**************************************************************************
2010 * WsWriteMessageEnd [webservices.@]
2012 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2013 WS_ERROR *error )
2015 struct channel *channel = (struct channel *)handle;
2016 HRESULT hr;
2018 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2019 if (error) FIXME( "ignoring error parameter\n" );
2020 if (ctx) FIXME( "ignoring ctx parameter\n" );
2022 if (!channel || !msg) return E_INVALIDARG;
2024 EnterCriticalSection( &channel->cs );
2026 if (channel->magic != CHANNEL_MAGIC)
2028 LeaveCriticalSection( &channel->cs );
2029 return E_INVALIDARG;
2032 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK) hr = send_message( channel, msg );
2034 LeaveCriticalSection( &channel->cs );
2035 return hr;
2038 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
2040 HANDLE handles[] = { wait, cancel };
2041 ULONG nonblocking = 0;
2042 HRESULT hr = S_OK;
2044 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2046 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2048 case 0:
2049 if ((*ret = accept( socket, NULL, NULL )) != -1)
2051 WSAEventSelect( *ret, NULL, 0 );
2052 ioctlsocket( *ret, FIONBIO, &nonblocking );
2053 break;
2055 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2056 break;
2058 case 1:
2059 hr = WS_E_OPERATION_ABORTED;
2060 break;
2062 default:
2063 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2064 break;
2067 return hr;
2070 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2072 struct channel *channel = (struct channel *)handle;
2073 HRESULT hr;
2075 EnterCriticalSection( &channel->cs );
2077 if (channel->magic != CHANNEL_MAGIC)
2079 LeaveCriticalSection( &channel->cs );
2080 return E_INVALIDARG;
2083 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
2085 LeaveCriticalSection( &channel->cs );
2086 return hr;
2089 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
2091 HANDLE handles[] = { wait, cancel };
2092 ULONG nonblocking = 0;
2093 HRESULT hr;
2095 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2097 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2099 case 0:
2100 hr = S_OK;
2101 break;
2103 case 1:
2104 hr = WS_E_OPERATION_ABORTED;
2105 break;
2107 default:
2108 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2109 break;
2112 WSAEventSelect( socket, NULL, 0 );
2113 ioctlsocket( socket, FIONBIO, &nonblocking );
2114 return hr;
2117 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2119 struct channel *channel = (struct channel *)handle;
2120 HRESULT hr;
2122 EnterCriticalSection( &channel->cs );
2124 if (channel->magic != CHANNEL_MAGIC)
2126 LeaveCriticalSection( &channel->cs );
2127 return E_INVALIDARG;
2130 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
2132 LeaveCriticalSection( &channel->cs );
2133 return hr;
2136 /**************************************************************************
2137 * WsAbortChannel [webservices.@]
2139 HRESULT WINAPI WsAbortChannel( WS_CHANNEL *handle, WS_ERROR *error )
2141 FIXME( "%p %p: stub!\n", handle, error );
2142 return E_NOTIMPL;