gphoto2.ds: Set supported groups.
[wine.git] / dlls / webservices / channel.c
blob9093a6cf674d2230dabb729a2b9121ee7b5f46fa
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}};
1237 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1238 WS_XML_WRITER_ENCODING *encoding;
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 text.charSet = WS_CHARSET_UTF8;
1247 encoding = &text.encoding;
1248 break;
1250 case WS_ENCODING_XML_BINARY_SESSION_1:
1251 if ((hr = writer_enable_lookup( channel->writer )) != S_OK) return hr;
1252 clear_dict( &channel->dict_send );
1253 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1254 bin.dynamicStringCallback = dict_cb;
1255 bin.dynamicStringCallbackState = &channel->dict_send;
1256 encoding = &bin.encoding;
1257 break;
1259 case WS_ENCODING_XML_BINARY_1:
1260 encoding = &bin.encoding;
1261 break;
1263 default:
1264 FIXME( "unhandled encoding %u\n", channel->encoding );
1265 return WS_E_NOT_SUPPORTED;
1268 return WsSetOutput( channel->writer, encoding, &buf.output, NULL, 0, NULL );
1271 /**************************************************************************
1272 * WsSendMessage [webservices.@]
1274 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1275 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1276 WS_ERROR *error )
1278 struct channel *channel = (struct channel *)handle;
1279 HRESULT hr;
1281 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1282 if (error) FIXME( "ignoring error parameter\n" );
1283 if (ctx) FIXME( "ignoring ctx parameter\n" );
1285 if (!channel || !msg || !desc) return E_INVALIDARG;
1287 EnterCriticalSection( &channel->cs );
1289 if (channel->magic != CHANNEL_MAGIC)
1291 LeaveCriticalSection( &channel->cs );
1292 return E_INVALIDARG;
1295 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK) goto done;
1296 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1297 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1299 if ((hr = init_writer( channel )) != S_OK) goto done;
1300 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1301 goto done;
1302 hr = send_message( channel, msg );
1304 done:
1305 LeaveCriticalSection( &channel->cs );
1306 return hr;
1309 /**************************************************************************
1310 * WsSendReplyMessage [webservices.@]
1312 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1313 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1314 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1316 struct channel *channel = (struct channel *)handle;
1317 GUID req_id;
1318 HRESULT hr;
1320 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1321 if (error) FIXME( "ignoring error parameter\n" );
1322 if (ctx) FIXME( "ignoring ctx parameter\n" );
1324 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1326 EnterCriticalSection( &channel->cs );
1328 if (channel->magic != CHANNEL_MAGIC)
1330 LeaveCriticalSection( &channel->cs );
1331 return E_INVALIDARG;
1334 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1335 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1336 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1337 if ((hr = message_get_id( request, &req_id )) != S_OK) goto done;
1338 if ((hr = message_set_request_id( msg, &req_id )) != S_OK) goto done;
1340 if ((hr = init_writer( channel )) != S_OK) goto done;
1341 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1342 goto done;
1343 hr = send_message( channel, msg );
1345 done:
1346 LeaveCriticalSection( &channel->cs );
1347 FIXME( "returning %08x\n", hr );
1348 return hr;
1351 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1353 if (!channel->read_buf)
1355 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1356 channel->read_buflen = size;
1357 return S_OK;
1359 if (channel->read_buflen < size)
1361 char *tmp;
1362 ULONG new_size = max( size, channel->read_buflen * 2 );
1363 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1364 channel->read_buf = tmp;
1365 channel->read_buflen = new_size;
1367 return S_OK;
1370 static HRESULT init_reader( struct channel *channel )
1372 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1373 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1374 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1375 WS_XML_READER_ENCODING *encoding;
1376 HRESULT hr;
1378 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1380 switch (channel->encoding)
1382 case WS_ENCODING_XML_UTF8:
1383 text.charSet = WS_CHARSET_UTF8;
1384 encoding = &text.encoding;
1385 break;
1387 case WS_ENCODING_XML_BINARY_SESSION_1:
1388 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1389 bin.dynamicDictionary = &channel->dict_recv.dict;
1390 /* fall through */
1392 case WS_ENCODING_XML_BINARY_1:
1393 encoding = &bin.encoding;
1394 break;
1396 default:
1397 FIXME( "unhandled encoding %u\n", channel->encoding );
1398 return WS_E_NOT_SUPPORTED;
1401 buf.encodedData = channel->read_buf;
1402 buf.encodedDataSize = channel->read_size;
1403 return WsSetInput( channel->reader, encoding, &buf.input, NULL, 0, NULL );
1406 #define INITIAL_READ_BUFFER_SIZE 4096
1407 static HRESULT receive_message_http( struct channel *channel )
1409 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1410 ULONG max_len;
1411 HRESULT hr;
1413 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1414 &max_len, sizeof(max_len) );
1416 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1417 channel->read_size = 0;
1418 for (;;)
1420 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1422 return HRESULT_FROM_WIN32( GetLastError() );
1424 if (!len) break;
1425 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1426 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1428 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1430 return HRESULT_FROM_WIN32( GetLastError() );
1432 if (!bytes_read) break;
1433 channel->read_size += bytes_read;
1434 offset += bytes_read;
1437 return init_reader( channel );
1440 static HRESULT receive_message_unsized( struct channel *channel, SOCKET socket )
1442 int bytes_read;
1443 ULONG max_len;
1444 HRESULT hr;
1446 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1447 &max_len, sizeof(max_len) );
1449 if ((hr = resize_read_buffer( channel, max_len )) != S_OK) return hr;
1451 channel->read_size = 0;
1452 if ((bytes_read = recv( socket, channel->read_buf, max_len, 0 )) < 0)
1454 return HRESULT_FROM_WIN32( WSAGetLastError() );
1456 channel->read_size = bytes_read;
1457 return S_OK;
1460 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1462 unsigned int offset = 0, to_read = size;
1463 int bytes_read;
1464 HRESULT hr;
1466 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1468 channel->read_size = 0;
1469 while (channel->read_size < size)
1471 if ((bytes_read = recv( channel->u.tcp.socket, channel->read_buf + offset, to_read, 0 )) < 0)
1473 return HRESULT_FROM_WIN32( WSAGetLastError() );
1475 if (!bytes_read) break;
1476 channel->read_size += bytes_read;
1477 to_read -= bytes_read;
1478 offset += bytes_read;
1480 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1481 return S_OK;
1484 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1486 unsigned char byte;
1487 HRESULT hr;
1489 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1490 *size = byte & 0x7f;
1491 if (!(byte & 0x80)) return S_OK;
1493 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1494 *size += (byte & 0x7f) << 7;
1495 if (!(byte & 0x80)) return S_OK;
1497 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1498 *size += (byte & 0x7f) << 14;
1499 if (!(byte & 0x80)) return S_OK;
1501 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1502 *size += (byte & 0x7f) << 21;
1503 if (!(byte & 0x80)) return S_OK;
1505 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1506 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1507 *size += byte << 28;
1508 return S_OK;
1511 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1513 switch (encoding)
1515 case KNOWN_ENCODING_SOAP11_UTF8:
1516 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1517 case KNOWN_ENCODING_SOAP11_UTF16:
1518 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1519 case KNOWN_ENCODING_SOAP11_UTF16LE:
1520 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1521 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1522 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1523 default:
1524 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1525 return WS_ENCODING_XML_UTF8;
1529 static HRESULT receive_preamble( struct channel *channel )
1531 unsigned char type;
1532 HRESULT hr;
1534 for (;;)
1536 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1537 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1538 switch (type)
1540 case FRAME_RECORD_TYPE_VERSION:
1542 unsigned char major, minor;
1543 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1544 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1545 TRACE( "major %u minor %u\n", major, major );
1546 break;
1548 case FRAME_RECORD_TYPE_MODE:
1550 unsigned char mode;
1551 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1552 TRACE( "mode %u\n", mode );
1553 break;
1555 case FRAME_RECORD_TYPE_VIA:
1557 unsigned int size;
1558 unsigned char *url;
1560 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1561 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1562 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1564 heap_free( url );
1565 return hr;
1567 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1568 heap_free( url ); /* FIXME: verify */
1569 break;
1571 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1573 unsigned char encoding;
1574 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1575 TRACE( "encoding %u\n", encoding );
1576 channel->encoding = map_known_encoding( encoding );
1577 break;
1579 default:
1580 WARN( "unhandled record type %u\n", type );
1581 return WS_E_INVALID_FORMAT;
1585 return S_OK;
1588 static HRESULT receive_sized_envelope( struct channel *channel )
1590 unsigned char type;
1591 unsigned int size;
1592 HRESULT hr;
1594 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1595 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1596 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1597 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1598 return S_OK;
1601 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1603 const BYTE *buf = *ptr;
1605 if (len < 1) return WS_E_INVALID_FORMAT;
1606 *size = buf[0] & 0x7f;
1607 if (!(buf[0] & 0x80))
1609 *ptr += 1;
1610 return S_OK;
1612 if (len < 2) return WS_E_INVALID_FORMAT;
1613 *size += (buf[1] & 0x7f) << 7;
1614 if (!(buf[1] & 0x80))
1616 *ptr += 2;
1617 return S_OK;
1619 if (len < 3) return WS_E_INVALID_FORMAT;
1620 *size += (buf[2] & 0x7f) << 14;
1621 if (!(buf[2] & 0x80))
1623 *ptr += 3;
1624 return S_OK;
1626 if (len < 4) return WS_E_INVALID_FORMAT;
1627 *size += (buf[3] & 0x7f) << 21;
1628 if (!(buf[3] & 0x80))
1630 *ptr += 4;
1631 return S_OK;
1633 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1634 *size += buf[4] << 28;
1635 *ptr += 5;
1636 return S_OK;
1639 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1641 ULONG size, strings_size, strings_offset;
1642 const BYTE *ptr = buf;
1643 BYTE *bytes;
1644 int index;
1645 HRESULT hr;
1647 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1648 strings_offset = ptr - buf;
1649 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1650 *used = strings_offset + strings_size;
1651 if (!strings_size) return S_OK;
1653 UuidCreate( &dict->dict.guid );
1654 dict->dict.isConst = FALSE;
1656 buflen -= strings_offset;
1657 ptr = buf + strings_offset;
1658 while (ptr < buf + strings_size)
1660 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1662 clear_dict( dict );
1663 return hr;
1665 if (size > buflen)
1667 clear_dict( dict );
1668 return WS_E_INVALID_FORMAT;
1670 buflen -= size;
1671 if (!(bytes = heap_alloc( size )))
1673 hr = E_OUTOFMEMORY;
1674 goto error;
1676 memcpy( bytes, ptr, size );
1677 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1679 heap_free( bytes );
1680 ptr += size;
1681 continue;
1683 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1685 heap_free( bytes );
1686 clear_dict( dict );
1687 return hr;
1689 ptr += size;
1691 return S_OK;
1693 error:
1694 clear_dict( dict );
1695 return hr;
1698 static HRESULT send_preamble_ack( struct channel *channel )
1700 HRESULT hr;
1701 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1702 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1703 return S_OK;
1706 static HRESULT receive_message_session( struct channel *channel )
1708 HRESULT hr;
1710 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1711 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1713 ULONG size;
1714 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict_recv,
1715 &size )) != S_OK) return hr;
1716 channel->read_size -= size;
1717 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1720 return init_reader( channel );
1723 static HRESULT receive_message_sock( struct channel *channel, SOCKET socket )
1725 HRESULT hr;
1726 if ((hr = receive_message_unsized( channel, socket )) != S_OK) return hr;
1727 return init_reader( channel );
1730 static HRESULT receive_message_bytes( struct channel *channel )
1732 HRESULT hr;
1733 if ((hr = connect_channel( channel )) != S_OK) return hr;
1735 switch (channel->binding)
1737 case WS_HTTP_CHANNEL_BINDING:
1738 return receive_message_http( channel );
1740 case WS_TCP_CHANNEL_BINDING:
1741 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1743 switch (channel->session_state)
1745 case SESSION_STATE_UNINITIALIZED:
1746 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1747 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1748 /* fall through */
1750 case SESSION_STATE_SETUP_COMPLETE:
1751 return receive_message_session( channel );
1753 default:
1754 ERR( "unhandled session state %u\n", channel->session_state );
1755 return WS_E_OTHER;
1758 return receive_message_sock( channel, channel->u.tcp.socket );
1760 case WS_UDP_CHANNEL_BINDING:
1761 return receive_message_sock( channel, channel->u.udp.socket );
1763 default:
1764 ERR( "unhandled binding %u\n", channel->binding );
1765 return E_NOTIMPL;
1769 HRESULT channel_receive_message( WS_CHANNEL *handle )
1771 struct channel *channel = (struct channel *)handle;
1772 HRESULT hr;
1774 EnterCriticalSection( &channel->cs );
1776 if (channel->magic != CHANNEL_MAGIC)
1778 LeaveCriticalSection( &channel->cs );
1779 return E_INVALIDARG;
1782 hr = receive_message_bytes( channel );
1784 LeaveCriticalSection( &channel->cs );
1785 return hr;
1788 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1790 struct channel *channel = (struct channel *)handle;
1792 EnterCriticalSection( &channel->cs );
1794 if (channel->magic != CHANNEL_MAGIC)
1796 LeaveCriticalSection( &channel->cs );
1797 return E_INVALIDARG;
1800 *reader = channel->reader;
1802 LeaveCriticalSection( &channel->cs );
1803 return S_OK;
1806 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1807 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1809 HRESULT hr;
1810 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1811 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1812 return WsReadEnvelopeEnd( handle, NULL );
1815 static HRESULT receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1816 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1817 void *value, ULONG size, ULONG *index )
1819 HRESULT hr;
1820 ULONG i;
1822 if ((hr = receive_message_bytes( channel )) != S_OK) return hr;
1823 for (i = 0; i < count; i++)
1825 const WS_ELEMENT_DESCRIPTION *body = desc[i]->bodyElementDescription;
1826 if ((hr = read_message( msg, channel->reader, body, read_option, heap, value, size )) == S_OK)
1828 if (index) *index = i;
1829 break;
1831 if ((hr = WsResetMessage( msg, NULL )) != S_OK) return hr;
1832 if ((hr = init_reader( channel )) != S_OK) return hr;
1834 return (i == count) ? WS_E_INVALID_FORMAT : S_OK;
1837 struct receive_message
1839 struct task task;
1840 struct channel *channel;
1841 WS_MESSAGE *msg;
1842 const WS_MESSAGE_DESCRIPTION **desc;
1843 ULONG count;
1844 WS_RECEIVE_OPTION option;
1845 WS_READ_OPTION read_option;
1846 WS_HEAP *heap;
1847 void *value;
1848 ULONG size;
1849 ULONG *index;
1850 const WS_ASYNC_CONTEXT *ctx;
1853 static void receive_message_proc( struct task *task )
1855 struct receive_message *r = (struct receive_message *)task;
1856 HRESULT hr;
1858 hr = receive_message( r->channel, r->msg, r->desc, r->count, r->option, r->read_option, r->heap, r->value,
1859 r->size, r->index );
1861 TRACE( "calling %p(%08x)\n", r->ctx->callback, hr );
1862 r->ctx->callback( hr, WS_LONG_CALLBACK, r->ctx->callbackState );
1863 TRACE( "%p returned\n", r->ctx->callback );
1866 static HRESULT queue_receive_message( struct channel *channel, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1867 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option,
1868 WS_HEAP *heap, void *value, ULONG size, ULONG *index,
1869 const WS_ASYNC_CONTEXT *ctx )
1871 struct receive_message *r;
1873 if (!(r = heap_alloc( sizeof(*r) ))) return E_OUTOFMEMORY;
1874 r->task.proc = receive_message_proc;
1875 r->channel = channel;
1876 r->msg = msg;
1877 r->desc = desc;
1878 r->count = count;
1879 r->option = option;
1880 r->read_option = read_option;
1881 r->heap = heap;
1882 r->value = value;
1883 r->size = size;
1884 r->index = index;
1885 r->ctx = ctx;
1886 return queue_task( &channel->recv_q, &r->task );
1889 /**************************************************************************
1890 * WsReceiveMessage [webservices.@]
1892 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1893 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1894 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1896 struct channel *channel = (struct channel *)handle;
1897 HRESULT hr;
1899 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1900 value, size, index, ctx, error );
1901 if (error) FIXME( "ignoring error parameter\n" );
1903 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1905 EnterCriticalSection( &channel->cs );
1907 if (channel->magic != CHANNEL_MAGIC)
1909 LeaveCriticalSection( &channel->cs );
1910 return E_INVALIDARG;
1913 if (ctx)
1914 hr = queue_receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index, ctx );
1915 else
1916 hr = receive_message( channel, msg, desc, count, option, read_option, heap, value, size, index );
1918 LeaveCriticalSection( &channel->cs );
1919 return hr;
1922 /**************************************************************************
1923 * WsReadMessageStart [webservices.@]
1925 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1926 WS_ERROR *error )
1928 struct channel *channel = (struct channel *)handle;
1929 HRESULT hr;
1931 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1932 if (error) FIXME( "ignoring error parameter\n" );
1933 if (ctx) FIXME( "ignoring ctx parameter\n" );
1935 if (!channel || !msg) return E_INVALIDARG;
1937 EnterCriticalSection( &channel->cs );
1939 if (channel->magic != CHANNEL_MAGIC)
1941 LeaveCriticalSection( &channel->cs );
1942 return E_INVALIDARG;
1945 if ((hr = receive_message_bytes( channel )) == S_OK)
1947 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
1950 LeaveCriticalSection( &channel->cs );
1951 return hr;
1954 /**************************************************************************
1955 * WsReadMessageEnd [webservices.@]
1957 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1958 WS_ERROR *error )
1960 struct channel *channel = (struct channel *)handle;
1961 HRESULT hr;
1963 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1964 if (error) FIXME( "ignoring error parameter\n" );
1965 if (ctx) FIXME( "ignoring ctx parameter\n" );
1967 if (!channel || !msg) return E_INVALIDARG;
1969 EnterCriticalSection( &channel->cs );
1971 if (channel->magic != CHANNEL_MAGIC)
1973 LeaveCriticalSection( &channel->cs );
1974 return E_INVALIDARG;
1977 hr = WsReadEnvelopeEnd( msg, NULL );
1979 LeaveCriticalSection( &channel->cs );
1980 return hr;
1983 /**************************************************************************
1984 * WsWriteMessageStart [webservices.@]
1986 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1987 WS_ERROR *error )
1989 struct channel *channel = (struct channel *)handle;
1990 HRESULT hr;
1992 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1993 if (error) FIXME( "ignoring error parameter\n" );
1994 if (ctx) FIXME( "ignoring ctx parameter\n" );
1996 if (!channel || !msg) return E_INVALIDARG;
1998 EnterCriticalSection( &channel->cs );
2000 if (channel->magic != CHANNEL_MAGIC)
2002 LeaveCriticalSection( &channel->cs );
2003 return E_INVALIDARG;
2006 if ((hr = init_writer( channel )) != S_OK) goto done;
2007 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
2008 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
2010 done:
2011 LeaveCriticalSection( &channel->cs );
2012 return hr;
2015 /**************************************************************************
2016 * WsWriteMessageEnd [webservices.@]
2018 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
2019 WS_ERROR *error )
2021 struct channel *channel = (struct channel *)handle;
2022 HRESULT hr;
2024 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
2025 if (error) FIXME( "ignoring error parameter\n" );
2026 if (ctx) FIXME( "ignoring ctx parameter\n" );
2028 if (!channel || !msg) return E_INVALIDARG;
2030 EnterCriticalSection( &channel->cs );
2032 if (channel->magic != CHANNEL_MAGIC)
2034 LeaveCriticalSection( &channel->cs );
2035 return E_INVALIDARG;
2038 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK) hr = send_message( channel, msg );
2040 LeaveCriticalSection( &channel->cs );
2041 return hr;
2044 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
2046 HANDLE handles[] = { wait, cancel };
2047 ULONG nonblocking = 0;
2048 HRESULT hr = S_OK;
2050 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2052 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2054 case 0:
2055 if ((*ret = accept( socket, NULL, NULL )) != -1)
2057 WSAEventSelect( *ret, NULL, 0 );
2058 ioctlsocket( *ret, FIONBIO, &nonblocking );
2059 break;
2061 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2062 break;
2064 case 1:
2065 hr = WS_E_OPERATION_ABORTED;
2066 break;
2068 default:
2069 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2070 break;
2073 return hr;
2076 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2078 struct channel *channel = (struct channel *)handle;
2079 HRESULT hr;
2081 EnterCriticalSection( &channel->cs );
2083 if (channel->magic != CHANNEL_MAGIC)
2085 LeaveCriticalSection( &channel->cs );
2086 return E_INVALIDARG;
2089 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
2091 LeaveCriticalSection( &channel->cs );
2092 return hr;
2095 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
2097 HANDLE handles[] = { wait, cancel };
2098 ULONG nonblocking = 0;
2099 HRESULT hr;
2101 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
2103 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
2105 case 0:
2106 hr = S_OK;
2107 break;
2109 case 1:
2110 hr = WS_E_OPERATION_ABORTED;
2111 break;
2113 default:
2114 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
2115 break;
2118 WSAEventSelect( socket, NULL, 0 );
2119 ioctlsocket( socket, FIONBIO, &nonblocking );
2120 return hr;
2123 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
2125 struct channel *channel = (struct channel *)handle;
2126 HRESULT hr;
2128 EnterCriticalSection( &channel->cs );
2130 if (channel->magic != CHANNEL_MAGIC)
2132 LeaveCriticalSection( &channel->cs );
2133 return E_INVALIDARG;
2136 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
2138 LeaveCriticalSection( &channel->cs );
2139 return hr;
2142 /**************************************************************************
2143 * WsAbortChannel [webservices.@]
2145 HRESULT WINAPI WsAbortChannel( WS_CHANNEL *handle, WS_ERROR *error )
2147 FIXME( "%p %p: stub!\n", handle, error );
2148 return E_NOTIMPL;