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
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"
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 */
93 void (*proc
)( struct task
* );
105 static struct task
*dequeue_task( struct queue
*queue
)
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
);
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
);
127 DWORD err
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
133 while ((task
= dequeue_task( queue
)))
140 case WAIT_OBJECT_0
+ 1:
141 TRACE( "cancelled\n" );
142 SetEvent( queue
->ready
);
146 ERR( "wait failed %u\n", err
);
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() );
164 WaitForSingleObject( queue
->ready
, INFINITE
);
169 CloseHandle( queue
->wait
);
171 CloseHandle( queue
->cancel
);
172 queue
->cancel
= NULL
;
173 CloseHandle( queue
->ready
);
178 static HRESULT
queue_task( struct queue
*queue
, struct task
*task
)
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
);
194 SESSION_STATE_UNINITIALIZED
,
195 SESSION_STATE_SETUP_COMPLETE
,
196 SESSION_STATE_SHUTDOWN
,
203 WS_CHANNEL_TYPE type
;
204 WS_CHANNEL_BINDING binding
;
205 WS_CHANNEL_STATE state
;
206 WS_ENDPOINT_ADDRESS addr
;
207 WS_XML_WRITER
*writer
;
208 WS_XML_READER
*reader
;
210 WS_ENCODING encoding
;
211 enum session_state session_state
;
212 struct dictionary dict_send
;
213 struct dictionary dict_recv
;
239 struct prop prop
[sizeof(channel_props
)/sizeof(channel_props
[0])];
242 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
244 static struct channel
*alloc_channel(void)
246 static const ULONG count
= sizeof(channel_props
)/sizeof(channel_props
[0]);
248 ULONG size
= sizeof(*ret
) + prop_size( channel_props
, count
);
250 if (!(ret
= heap_alloc_zero( size
))) return NULL
;
252 ret
->magic
= CHANNEL_MAGIC
;
253 InitializeCriticalSection( &ret
->cs
);
254 ret
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": channel.cs");
256 InitializeCriticalSection( &ret
->send_q
.cs
);
257 ret
->send_q
.cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": channel.send_q.cs");
259 InitializeCriticalSection( &ret
->recv_q
.cs
);
260 ret
->recv_q
.cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": channel.recv_q.cs");
262 prop_init( channel_props
, count
, ret
->prop
, &ret
[1] );
263 ret
->prop_count
= count
;
267 static void clear_addr( WS_ENDPOINT_ADDRESS
*addr
)
269 heap_free( addr
->url
.chars
);
270 addr
->url
.chars
= NULL
;
271 addr
->url
.length
= 0;
274 static void clear_queue( struct queue
*queue
)
278 SetEvent( queue
->cancel
);
279 WaitForSingleObject( queue
->ready
, INFINITE
);
281 while ((ptr
= list_head( &queue
->tasks
)))
283 struct task
*task
= LIST_ENTRY( ptr
, struct task
, entry
);
284 list_remove( &task
->entry
);
288 CloseHandle( queue
->wait
);
290 CloseHandle( queue
->cancel
);
291 queue
->cancel
= NULL
;
292 CloseHandle( queue
->ready
);
296 static void reset_channel( struct channel
*channel
)
298 clear_queue( &channel
->send_q
);
299 clear_queue( &channel
->recv_q
);
300 channel
->state
= WS_CHANNEL_STATE_CREATED
;
301 channel
->session_state
= SESSION_STATE_UNINITIALIZED
;
302 clear_addr( &channel
->addr
);
303 clear_dict( &channel
->dict_send
);
304 clear_dict( &channel
->dict_recv
);
306 channel
->read_size
= 0;
308 switch (channel
->binding
)
310 case WS_HTTP_CHANNEL_BINDING
:
311 WinHttpCloseHandle( channel
->u
.http
.request
);
312 channel
->u
.http
.request
= NULL
;
313 WinHttpCloseHandle( channel
->u
.http
.connect
);
314 channel
->u
.http
.connect
= NULL
;
315 WinHttpCloseHandle( channel
->u
.http
.session
);
316 channel
->u
.http
.session
= NULL
;
317 heap_free( channel
->u
.http
.path
);
318 channel
->u
.http
.path
= NULL
;
319 channel
->u
.http
.flags
= 0;
322 case WS_TCP_CHANNEL_BINDING
:
323 closesocket( channel
->u
.tcp
.socket
);
324 channel
->u
.tcp
.socket
= -1;
327 case WS_UDP_CHANNEL_BINDING
:
328 closesocket( channel
->u
.udp
.socket
);
329 channel
->u
.udp
.socket
= -1;
336 static void free_channel( struct channel
*channel
)
338 reset_channel( channel
);
340 WsFreeWriter( channel
->writer
);
341 WsFreeReader( channel
->reader
);
343 heap_free( channel
->read_buf
);
345 channel
->send_q
.cs
.DebugInfo
->Spare
[0] = 0;
346 DeleteCriticalSection( &channel
->send_q
.cs
);
348 channel
->recv_q
.cs
.DebugInfo
->Spare
[0] = 0;
349 DeleteCriticalSection( &channel
->recv_q
.cs
);
351 channel
->cs
.DebugInfo
->Spare
[0] = 0;
352 DeleteCriticalSection( &channel
->cs
);
353 heap_free( channel
);
356 static HRESULT
create_channel( WS_CHANNEL_TYPE type
, WS_CHANNEL_BINDING binding
,
357 const WS_CHANNEL_PROPERTY
*properties
, ULONG count
, struct channel
**ret
)
359 struct channel
*channel
;
360 ULONG i
, msg_size
= 65536;
361 WS_ENVELOPE_VERSION env_version
= WS_ENVELOPE_VERSION_SOAP_1_2
;
362 WS_ADDRESSING_VERSION addr_version
= WS_ADDRESSING_VERSION_1_0
;
365 if (!(channel
= alloc_channel())) return E_OUTOFMEMORY
;
367 prop_set( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE
,
368 &msg_size
, sizeof(msg_size
) );
369 prop_set( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION
,
370 &env_version
, sizeof(env_version
) );
371 prop_set( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION
,
372 &addr_version
, sizeof(addr_version
) );
374 channel
->type
= type
;
375 channel
->binding
= binding
;
377 switch (channel
->binding
)
379 case WS_HTTP_CHANNEL_BINDING
:
380 channel
->encoding
= WS_ENCODING_XML_UTF8
;
383 case WS_TCP_CHANNEL_BINDING
:
384 channel
->u
.tcp
.socket
= -1;
385 channel
->encoding
= WS_ENCODING_XML_BINARY_SESSION_1
;
388 case WS_UDP_CHANNEL_BINDING
:
389 channel
->u
.udp
.socket
= -1;
390 channel
->encoding
= WS_ENCODING_XML_UTF8
;
396 for (i
= 0; i
< count
; i
++)
398 switch (properties
[i
].id
)
400 case WS_CHANNEL_PROPERTY_ENCODING
:
401 if (!properties
[i
].value
|| properties
[i
].valueSize
!= sizeof(channel
->encoding
))
403 free_channel( channel
);
406 channel
->encoding
= *(WS_ENCODING
*)properties
[i
].value
;
410 if ((hr
= prop_set( channel
->prop
, channel
->prop_count
, properties
[i
].id
, properties
[i
].value
,
411 properties
[i
].valueSize
)) != S_OK
)
413 free_channel( channel
);
424 /**************************************************************************
425 * WsCreateChannel [webservices.@]
427 HRESULT WINAPI
WsCreateChannel( WS_CHANNEL_TYPE type
, WS_CHANNEL_BINDING binding
,
428 const WS_CHANNEL_PROPERTY
*properties
, ULONG count
,
429 const WS_SECURITY_DESCRIPTION
*desc
, WS_CHANNEL
**handle
,
432 struct channel
*channel
;
435 TRACE( "%u %u %p %u %p %p %p\n", type
, binding
, properties
, count
, desc
, handle
, error
);
436 if (error
) FIXME( "ignoring error parameter\n" );
437 if (desc
) FIXME( "ignoring security description\n" );
439 if (!handle
) return E_INVALIDARG
;
441 if (type
!= WS_CHANNEL_TYPE_REQUEST
&& type
!= WS_CHANNEL_TYPE_DUPLEX
&&
442 type
!= WS_CHANNEL_TYPE_DUPLEX_SESSION
)
444 FIXME( "channel type %u not implemented\n", type
);
447 if (binding
!= WS_HTTP_CHANNEL_BINDING
&& binding
!= WS_TCP_CHANNEL_BINDING
&&
448 binding
!= WS_UDP_CHANNEL_BINDING
)
450 FIXME( "channel binding %u not implemented\n", binding
);
454 if ((hr
= create_channel( type
, binding
, properties
, count
, &channel
)) != S_OK
) return hr
;
456 TRACE( "created %p\n", channel
);
457 *handle
= (WS_CHANNEL
*)channel
;
461 /**************************************************************************
462 * WsCreateChannelForListener [webservices.@]
464 HRESULT WINAPI
WsCreateChannelForListener( WS_LISTENER
*listener_handle
, const WS_CHANNEL_PROPERTY
*properties
,
465 ULONG count
, WS_CHANNEL
**handle
, WS_ERROR
*error
)
467 struct channel
*channel
;
468 WS_CHANNEL_TYPE type
;
469 WS_CHANNEL_BINDING binding
;
472 TRACE( "%p %p %u %p %p\n", listener_handle
, properties
, count
, handle
, error
);
473 if (error
) FIXME( "ignoring error parameter\n" );
475 if (!listener_handle
|| !handle
) return E_INVALIDARG
;
477 if ((hr
= WsGetListenerProperty( listener_handle
, WS_LISTENER_PROPERTY_CHANNEL_TYPE
, &type
,
478 sizeof(type
), NULL
)) != S_OK
) return hr
;
480 if ((hr
= WsGetListenerProperty( listener_handle
, WS_LISTENER_PROPERTY_CHANNEL_BINDING
, &binding
,
481 sizeof(binding
), NULL
)) != S_OK
) return hr
;
483 if ((hr
= create_channel( type
, binding
, properties
, count
, &channel
)) != S_OK
) return hr
;
485 TRACE( "created %p\n", channel
);
486 *handle
= (WS_CHANNEL
*)channel
;
490 /**************************************************************************
491 * WsFreeChannel [webservices.@]
493 void WINAPI
WsFreeChannel( WS_CHANNEL
*handle
)
495 struct channel
*channel
= (struct channel
*)handle
;
497 TRACE( "%p\n", handle
);
499 if (!channel
) return;
501 EnterCriticalSection( &channel
->cs
);
503 if (channel
->magic
!= CHANNEL_MAGIC
)
505 LeaveCriticalSection( &channel
->cs
);
511 LeaveCriticalSection( &channel
->cs
);
512 free_channel( channel
);
515 /**************************************************************************
516 * WsResetChannel [webservices.@]
518 HRESULT WINAPI
WsResetChannel( WS_CHANNEL
*handle
, WS_ERROR
*error
)
520 struct channel
*channel
= (struct channel
*)handle
;
522 TRACE( "%p %p\n", handle
, error
);
523 if (error
) FIXME( "ignoring error parameter\n" );
525 if (!channel
) return E_INVALIDARG
;
527 EnterCriticalSection( &channel
->cs
);
529 if (channel
->magic
!= CHANNEL_MAGIC
)
531 LeaveCriticalSection( &channel
->cs
);
535 if (channel
->state
!= WS_CHANNEL_STATE_CREATED
&& channel
->state
!= WS_CHANNEL_STATE_CLOSED
)
537 LeaveCriticalSection( &channel
->cs
);
538 return WS_E_INVALID_OPERATION
;
541 reset_channel( channel
);
543 LeaveCriticalSection( &channel
->cs
);
547 /**************************************************************************
548 * WsGetChannelProperty [webservices.@]
550 HRESULT WINAPI
WsGetChannelProperty( WS_CHANNEL
*handle
, WS_CHANNEL_PROPERTY_ID id
, void *buf
,
551 ULONG size
, WS_ERROR
*error
)
553 struct channel
*channel
= (struct channel
*)handle
;
556 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
557 if (error
) FIXME( "ignoring error parameter\n" );
559 if (!channel
) return E_INVALIDARG
;
561 EnterCriticalSection( &channel
->cs
);
563 if (channel
->magic
!= CHANNEL_MAGIC
)
565 LeaveCriticalSection( &channel
->cs
);
571 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE
:
572 if (!buf
|| size
!= sizeof(channel
->type
)) hr
= E_INVALIDARG
;
573 else *(WS_CHANNEL_TYPE
*)buf
= channel
->type
;
576 case WS_CHANNEL_PROPERTY_ENCODING
:
577 if (!buf
|| size
!= sizeof(channel
->encoding
)) hr
= E_INVALIDARG
;
578 else *(WS_ENCODING
*)buf
= channel
->encoding
;
582 hr
= prop_get( channel
->prop
, channel
->prop_count
, id
, buf
, size
);
585 LeaveCriticalSection( &channel
->cs
);
589 /**************************************************************************
590 * WsSetChannelProperty [webservices.@]
592 HRESULT WINAPI
WsSetChannelProperty( WS_CHANNEL
*handle
, WS_CHANNEL_PROPERTY_ID id
, const void *value
,
593 ULONG size
, WS_ERROR
*error
)
595 struct channel
*channel
= (struct channel
*)handle
;
598 TRACE( "%p %u %p %u\n", handle
, id
, value
, size
);
599 if (error
) FIXME( "ignoring error parameter\n" );
601 if (!channel
) return E_INVALIDARG
;
603 EnterCriticalSection( &channel
->cs
);
605 if (channel
->magic
!= CHANNEL_MAGIC
)
607 LeaveCriticalSection( &channel
->cs
);
611 hr
= prop_set( channel
->prop
, channel
->prop_count
, id
, value
, size
);
613 LeaveCriticalSection( &channel
->cs
);
617 static HRESULT
open_channel( struct channel
*channel
, const WS_ENDPOINT_ADDRESS
*endpoint
)
619 if (endpoint
->headers
|| endpoint
->extensions
|| endpoint
->identity
)
621 FIXME( "headers, extensions or identity not supported\n" );
625 if (!(channel
->addr
.url
.chars
= heap_alloc( endpoint
->url
.length
* sizeof(WCHAR
) ))) return E_OUTOFMEMORY
;
626 memcpy( channel
->addr
.url
.chars
, endpoint
->url
.chars
, endpoint
->url
.length
* sizeof(WCHAR
) );
627 channel
->addr
.url
.length
= endpoint
->url
.length
;
629 channel
->state
= WS_CHANNEL_STATE_OPEN
;
633 /**************************************************************************
634 * WsOpenChannel [webservices.@]
636 HRESULT WINAPI
WsOpenChannel( WS_CHANNEL
*handle
, const WS_ENDPOINT_ADDRESS
*endpoint
,
637 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
639 struct channel
*channel
= (struct channel
*)handle
;
642 TRACE( "%p %p %p %p\n", handle
, endpoint
, ctx
, error
);
643 if (error
) FIXME( "ignoring error parameter\n" );
644 if (ctx
) FIXME( "ignoring ctx parameter\n" );
646 if (!channel
|| !endpoint
) return E_INVALIDARG
;
648 EnterCriticalSection( &channel
->cs
);
650 if (channel
->magic
!= CHANNEL_MAGIC
)
652 LeaveCriticalSection( &channel
->cs
);
656 if (channel
->state
!= WS_CHANNEL_STATE_CREATED
)
658 LeaveCriticalSection( &channel
->cs
);
659 return WS_E_INVALID_OPERATION
;
662 hr
= open_channel( channel
, endpoint
);
664 LeaveCriticalSection( &channel
->cs
);
668 enum frame_record_type
670 FRAME_RECORD_TYPE_VERSION
,
671 FRAME_RECORD_TYPE_MODE
,
672 FRAME_RECORD_TYPE_VIA
,
673 FRAME_RECORD_TYPE_KNOWN_ENCODING
,
674 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING
,
675 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE
,
676 FRAME_RECORD_TYPE_SIZED_ENVELOPE
,
677 FRAME_RECORD_TYPE_END
,
678 FRAME_RECORD_TYPE_FAULT
,
679 FRAME_RECORD_TYPE_UPGRADE_REQUEST
,
680 FRAME_RECORD_TYPE_UPGRADE_RESPONSE
,
681 FRAME_RECORD_TYPE_PREAMBLE_ACK
,
682 FRAME_RECORD_TYPE_PREAMBLE_END
,
685 static HRESULT
send_byte( SOCKET socket
, BYTE byte
)
687 int count
= send( socket
, (char *)&byte
, 1, 0 );
688 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
689 if (count
!= 1) return WS_E_OTHER
;
693 static HRESULT
shutdown_session( struct channel
*channel
)
697 if (channel
->state
!= WS_CHANNEL_STATE_OPEN
||
698 (channel
->type
!= WS_CHANNEL_TYPE_OUTPUT_SESSION
&&
699 channel
->type
!= WS_CHANNEL_TYPE_DUPLEX_SESSION
) ||
700 channel
->session_state
>= SESSION_STATE_SHUTDOWN
) return WS_E_INVALID_OPERATION
;
702 switch (channel
->binding
)
704 case WS_TCP_CHANNEL_BINDING
:
705 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_END
)) != S_OK
) return hr
;
706 channel
->session_state
= SESSION_STATE_SHUTDOWN
;
710 FIXME( "unhandled binding %u\n", channel
->binding
);
715 HRESULT WINAPI
WsShutdownSessionChannel( WS_CHANNEL
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
717 struct channel
*channel
= (struct channel
*)handle
;
720 TRACE( "%p %p %p\n", handle
, ctx
, error
);
721 if (error
) FIXME( "ignoring error parameter\n" );
722 if (ctx
) FIXME( "ignoring ctx parameter\n" );
724 if (!channel
) return E_INVALIDARG
;
726 EnterCriticalSection( &channel
->cs
);
728 if (channel
->magic
!= CHANNEL_MAGIC
)
730 LeaveCriticalSection( &channel
->cs
);
734 hr
= shutdown_session( channel
);
736 LeaveCriticalSection( &channel
->cs
);
740 static void close_channel( struct channel
*channel
)
742 reset_channel( channel
);
743 channel
->state
= WS_CHANNEL_STATE_CLOSED
;
746 /**************************************************************************
747 * WsCloseChannel [webservices.@]
749 HRESULT WINAPI
WsCloseChannel( WS_CHANNEL
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
751 struct channel
*channel
= (struct channel
*)handle
;
753 TRACE( "%p %p %p\n", handle
, ctx
, error
);
754 if (error
) FIXME( "ignoring error parameter\n" );
755 if (ctx
) FIXME( "ignoring ctx parameter\n" );
757 if (!channel
) return E_INVALIDARG
;
759 EnterCriticalSection( &channel
->cs
);
761 if (channel
->magic
!= CHANNEL_MAGIC
)
763 LeaveCriticalSection( &channel
->cs
);
767 close_channel( channel
);
769 LeaveCriticalSection( &channel
->cs
);
773 static HRESULT
parse_http_url( const WCHAR
*url
, ULONG len
, URL_COMPONENTS
*uc
)
775 HRESULT hr
= E_OUTOFMEMORY
;
779 memset( uc
, 0, sizeof(*uc
) );
780 uc
->dwStructSize
= sizeof(*uc
);
781 uc
->dwHostNameLength
= 128;
782 uc
->lpszHostName
= heap_alloc( uc
->dwHostNameLength
* sizeof(WCHAR
) );
783 uc
->dwUrlPathLength
= 128;
784 uc
->lpszUrlPath
= heap_alloc( uc
->dwUrlPathLength
* sizeof(WCHAR
) );
785 uc
->dwExtraInfoLength
= 128;
786 uc
->lpszExtraInfo
= heap_alloc( uc
->dwExtraInfoLength
* sizeof(WCHAR
) );
787 if (!uc
->lpszHostName
|| !uc
->lpszUrlPath
|| !uc
->lpszExtraInfo
) goto error
;
789 if (!WinHttpCrackUrl( url
, len
, ICU_DECODE
, uc
))
791 if ((err
= GetLastError()) != ERROR_INSUFFICIENT_BUFFER
)
793 hr
= HRESULT_FROM_WIN32( err
);
796 if (!(tmp
= heap_realloc( uc
->lpszHostName
, uc
->dwHostNameLength
* sizeof(WCHAR
) ))) goto error
;
797 uc
->lpszHostName
= tmp
;
798 if (!(tmp
= heap_realloc( uc
->lpszUrlPath
, uc
->dwUrlPathLength
* sizeof(WCHAR
) ))) goto error
;
799 uc
->lpszUrlPath
= tmp
;
800 if (!(tmp
= heap_realloc( uc
->lpszExtraInfo
, uc
->dwExtraInfoLength
* sizeof(WCHAR
) ))) goto error
;
801 uc
->lpszExtraInfo
= tmp
;
802 WinHttpCrackUrl( url
, len
, ICU_DECODE
, uc
);
808 heap_free( uc
->lpszHostName
);
809 heap_free( uc
->lpszUrlPath
);
810 heap_free( uc
->lpszExtraInfo
);
814 static HRESULT
connect_channel_http( struct channel
*channel
)
816 static const WCHAR agentW
[] =
817 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
818 HINTERNET ses
= NULL
, con
= NULL
;
822 if (channel
->u
.http
.connect
) return S_OK
;
824 if ((hr
= parse_http_url( channel
->addr
.url
.chars
, channel
->addr
.url
.length
, &uc
)) != S_OK
) return hr
;
825 if (!(channel
->u
.http
.path
= heap_alloc( (uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
+ 1) * sizeof(WCHAR
) )))
832 strcpyW( channel
->u
.http
.path
, uc
.lpszUrlPath
);
833 if (uc
.dwExtraInfoLength
) strcatW( channel
->u
.http
.path
, uc
.lpszExtraInfo
);
836 channel
->u
.http
.flags
= WINHTTP_FLAG_REFRESH
;
839 case INTERNET_SCHEME_HTTP
: break;
840 case INTERNET_SCHEME_HTTPS
:
841 channel
->u
.http
.flags
|= WINHTTP_FLAG_SECURE
;
845 hr
= WS_E_INVALID_ENDPOINT_URL
;
849 if (!(ses
= WinHttpOpen( agentW
, 0, NULL
, NULL
, 0 )))
851 hr
= HRESULT_FROM_WIN32( GetLastError() );
854 if (!(con
= WinHttpConnect( ses
, uc
.lpszHostName
, uc
.nPort
, 0 )))
856 hr
= HRESULT_FROM_WIN32( GetLastError() );
860 channel
->u
.http
.session
= ses
;
861 channel
->u
.http
.connect
= con
;
866 WinHttpCloseHandle( con
);
867 WinHttpCloseHandle( ses
);
869 heap_free( uc
.lpszHostName
);
870 heap_free( uc
.lpszUrlPath
);
871 heap_free( uc
.lpszExtraInfo
);
875 static HRESULT
connect_channel_tcp( struct channel
*channel
)
877 struct sockaddr_storage storage
;
878 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
880 WS_URL_SCHEME_TYPE scheme
;
885 if (channel
->u
.tcp
.socket
!= -1) return S_OK
;
887 if ((hr
= parse_url( &channel
->addr
.url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
888 if (scheme
!= WS_URL_NETTCP_SCHEME_TYPE
)
891 return WS_E_INVALID_ENDPOINT_URL
;
896 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, 0 );
898 if (hr
!= S_OK
) return hr
;
900 if ((channel
->u
.tcp
.socket
= socket( addr
->sa_family
, SOCK_STREAM
, 0 )) == -1)
901 return HRESULT_FROM_WIN32( WSAGetLastError() );
903 if (connect( channel
->u
.tcp
.socket
, addr
, addr_len
) < 0)
905 closesocket( channel
->u
.tcp
.socket
);
906 channel
->u
.tcp
.socket
= -1;
907 return HRESULT_FROM_WIN32( WSAGetLastError() );
913 static HRESULT
connect_channel_udp( struct channel
*channel
)
915 struct sockaddr_storage storage
;
916 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
918 WS_URL_SCHEME_TYPE scheme
;
923 if (channel
->u
.udp
.socket
!= -1) return S_OK
;
925 if ((hr
= parse_url( &channel
->addr
.url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
926 if (scheme
!= WS_URL_SOAPUDP_SCHEME_TYPE
)
929 return WS_E_INVALID_ENDPOINT_URL
;
934 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, 0 );
936 if (hr
!= S_OK
) return hr
;
938 if ((channel
->u
.udp
.socket
= socket( addr
->sa_family
, SOCK_DGRAM
, 0 )) == -1)
939 return HRESULT_FROM_WIN32( WSAGetLastError() );
941 if (connect( channel
->u
.udp
.socket
, addr
, addr_len
) < 0)
943 closesocket( channel
->u
.udp
.socket
);
944 channel
->u
.udp
.socket
= -1;
945 return HRESULT_FROM_WIN32( WSAGetLastError() );
951 static HRESULT
connect_channel( struct channel
*channel
)
953 switch (channel
->binding
)
955 case WS_HTTP_CHANNEL_BINDING
:
956 return connect_channel_http( channel
);
958 case WS_TCP_CHANNEL_BINDING
:
959 return connect_channel_tcp( channel
);
961 case WS_UDP_CHANNEL_BINDING
:
962 return connect_channel_udp( channel
);
965 ERR( "unhandled binding %u\n", channel
->binding
);
970 static HRESULT
send_message_http( HINTERNET request
, BYTE
*data
, ULONG len
)
972 if (!WinHttpSendRequest( request
, NULL
, 0, data
, len
, len
, 0 ))
973 return HRESULT_FROM_WIN32( GetLastError() );
975 if (!WinHttpReceiveResponse( request
, NULL
))
976 return HRESULT_FROM_WIN32( GetLastError() );
980 static HRESULT
send_bytes( SOCKET socket
, BYTE
*bytes
, int len
)
982 int count
= send( socket
, (char *)bytes
, len
, 0 );
983 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
984 if (count
!= len
) return WS_E_OTHER
;
988 static HRESULT
send_size( SOCKET socket
, ULONG size
)
991 if (size
< 0x80) return send_byte( socket
, size
);
992 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
993 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
994 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
995 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
996 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
997 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
998 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
999 if ((size
>>= 7) < 0x08) return send_byte( socket
, size
);
1000 return E_INVALIDARG
;
1003 static inline ULONG
size_length( ULONG size
)
1005 if (size
< 0x80) return 1;
1006 if (size
< 0x4000) return 2;
1007 if (size
< 0x200000) return 3;
1008 if (size
< 0x10000000) return 4;
1012 static ULONG
string_table_size( const struct dictionary
*dict
)
1015 for (i
= 0; i
< dict
->dict
.stringCount
; i
++)
1017 if (dict
->sequence
[i
] == dict
->current_sequence
)
1018 size
+= size_length( dict
->dict
.strings
[i
].length
) + dict
->dict
.strings
[i
].length
;
1023 static HRESULT
send_string_table( SOCKET socket
, const struct dictionary
*dict
)
1027 for (i
= 0; i
< dict
->dict
.stringCount
; i
++)
1029 if (dict
->sequence
[i
] != dict
->current_sequence
) continue;
1030 if ((hr
= send_size( socket
, dict
->dict
.strings
[i
].length
)) != S_OK
) return hr
;
1031 if ((hr
= send_bytes( socket
, dict
->dict
.strings
[i
].bytes
, dict
->dict
.strings
[i
].length
)) != S_OK
) return hr
;
1036 static HRESULT
string_to_utf8( const WS_STRING
*str
, unsigned char **ret
, int *len
)
1038 *len
= WideCharToMultiByte( CP_UTF8
, 0, str
->chars
, str
->length
, NULL
, 0, NULL
, NULL
);
1039 if (!(*ret
= heap_alloc( *len
))) return E_OUTOFMEMORY
;
1040 WideCharToMultiByte( CP_UTF8
, 0, str
->chars
, str
->length
, (char *)*ret
, *len
, NULL
, NULL
);
1046 SESSION_MODE_INVALID
= 0,
1047 SESSION_MODE_SINGLETON
= 1,
1048 SESSION_MODE_DUPLEX
= 2,
1049 SESSION_MODE_SIMPLEX
= 3,
1052 static enum session_mode
map_channel_type( struct channel
*channel
)
1054 switch (channel
->type
)
1056 case WS_CHANNEL_TYPE_DUPLEX_SESSION
: return SESSION_MODE_DUPLEX
;
1058 FIXME( "unhandled channel type %08x\n", channel
->type
);
1059 return SESSION_MODE_INVALID
;
1065 KNOWN_ENCODING_SOAP11_UTF8
= 0x00,
1066 KNOWN_ENCODING_SOAP11_UTF16
= 0x01,
1067 KNOWN_ENCODING_SOAP11_UTF16LE
= 0x02,
1068 KNOWN_ENCODING_SOAP12_UTF8
= 0x03,
1069 KNOWN_ENCODING_SOAP12_UTF16
= 0x04,
1070 KNOWN_ENCODING_SOAP12_UTF16LE
= 0x05,
1071 KNOWN_ENCODING_SOAP12_MTOM
= 0x06,
1072 KNOWN_ENCODING_SOAP12_BINARY
= 0x07,
1073 KNOWN_ENCODING_SOAP12_BINARY_SESSION
= 0x08,
1076 static enum known_encoding
map_channel_encoding( struct channel
*channel
)
1078 WS_ENVELOPE_VERSION version
;
1080 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION
, &version
, sizeof(version
) );
1084 case WS_ENVELOPE_VERSION_SOAP_1_1
:
1085 switch (channel
->encoding
)
1087 case WS_ENCODING_XML_UTF8
: return KNOWN_ENCODING_SOAP11_UTF8
;
1088 case WS_ENCODING_XML_UTF16LE
: return KNOWN_ENCODING_SOAP11_UTF16LE
;
1090 FIXME( "unhandled version/encoding %u/%u\n", version
, channel
->encoding
);
1093 case WS_ENVELOPE_VERSION_SOAP_1_2
:
1094 switch (channel
->encoding
)
1096 case WS_ENCODING_XML_UTF8
: return KNOWN_ENCODING_SOAP12_UTF8
;
1097 case WS_ENCODING_XML_UTF16LE
: return KNOWN_ENCODING_SOAP12_UTF16LE
;
1098 case WS_ENCODING_XML_BINARY_1
: return KNOWN_ENCODING_SOAP12_BINARY
;
1099 case WS_ENCODING_XML_BINARY_SESSION_1
: return KNOWN_ENCODING_SOAP12_BINARY_SESSION
;
1101 FIXME( "unhandled version/encoding %u/%u\n", version
, channel
->encoding
);
1105 ERR( "unhandled version %u\n", version
);
1110 #define FRAME_VERSION_MAJOR 1
1111 #define FRAME_VERSION_MINOR 1
1113 static HRESULT
send_preamble( struct channel
*channel
)
1119 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_VERSION
)) != S_OK
) return hr
;
1120 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_VERSION_MAJOR
)) != S_OK
) return hr
;
1121 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_VERSION_MINOR
)) != S_OK
) return hr
;
1123 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_MODE
)) != S_OK
) return hr
;
1124 if ((hr
= send_byte( channel
->u
.tcp
.socket
, map_channel_type(channel
) )) != S_OK
) return hr
;
1126 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_VIA
)) != S_OK
) return hr
;
1127 if ((hr
= string_to_utf8( &channel
->addr
.url
, &url
, &len
)) != S_OK
) return hr
;
1128 if ((hr
= send_size( channel
->u
.tcp
.socket
, len
)) != S_OK
) goto done
;
1129 if ((hr
= send_bytes( channel
->u
.tcp
.socket
, url
, len
)) != S_OK
) goto done
;
1131 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_KNOWN_ENCODING
)) != S_OK
) goto done
;
1132 if ((hr
= send_byte( channel
->u
.tcp
.socket
, map_channel_encoding(channel
) )) != S_OK
) goto done
;
1133 hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_PREAMBLE_END
);
1140 static HRESULT
receive_bytes( struct channel
*channel
, unsigned char *bytes
, int len
)
1142 int count
= recv( channel
->u
.tcp
.socket
, (char *)bytes
, len
, 0 );
1143 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1144 if (count
!= len
) return WS_E_INVALID_FORMAT
;
1148 static HRESULT
receive_preamble_ack( struct channel
*channel
)
1153 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1154 if (byte
!= FRAME_RECORD_TYPE_PREAMBLE_ACK
) return WS_E_INVALID_FORMAT
;
1155 channel
->session_state
= SESSION_STATE_SETUP_COMPLETE
;
1159 static HRESULT
send_sized_envelope( struct channel
*channel
, BYTE
*data
, ULONG len
)
1161 ULONG table_size
= string_table_size( &channel
->dict_send
);
1164 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_SIZED_ENVELOPE
)) != S_OK
) return hr
;
1165 if ((hr
= send_size( channel
->u
.tcp
.socket
, size_length(table_size
) + table_size
+ len
)) != S_OK
) return hr
;
1166 if ((hr
= send_size( channel
->u
.tcp
.socket
, table_size
)) != S_OK
) return hr
;
1167 if ((hr
= send_string_table( channel
->u
.tcp
.socket
, &channel
->dict_send
)) != S_OK
) return hr
;
1168 return send_bytes( channel
->u
.tcp
.socket
, data
, len
);
1171 static HRESULT
open_http_request( struct channel
*channel
, HINTERNET
*req
)
1173 static const WCHAR postW
[] = {'P','O','S','T',0};
1174 if ((*req
= WinHttpOpenRequest( channel
->u
.http
.connect
, postW
, channel
->u
.http
.path
,
1175 NULL
, NULL
, NULL
, channel
->u
.http
.flags
))) return S_OK
;
1176 return HRESULT_FROM_WIN32( GetLastError() );
1179 static HRESULT
send_message( struct channel
*channel
, WS_MESSAGE
*msg
)
1181 WS_XML_WRITER
*writer
;
1186 if ((hr
= connect_channel( channel
)) != S_OK
) return hr
;
1188 WsGetMessageProperty( channel
->msg
, WS_MESSAGE_PROPERTY_BODY_WRITER
, &writer
, sizeof(writer
), NULL
);
1189 WsGetWriterProperty( writer
, WS_XML_WRITER_PROPERTY_BYTES
, &buf
, sizeof(buf
), NULL
);
1191 switch (channel
->binding
)
1193 case WS_HTTP_CHANNEL_BINDING
:
1194 if (channel
->u
.http
.request
)
1196 WinHttpCloseHandle( channel
->u
.http
.request
);
1197 channel
->u
.http
.request
= NULL
;
1199 if ((hr
= open_http_request( channel
, &channel
->u
.http
.request
)) != S_OK
) return hr
;
1200 if ((hr
= message_insert_http_headers( msg
, channel
->u
.http
.request
)) != S_OK
) return hr
;
1201 return send_message_http( channel
->u
.http
.request
, buf
.bytes
, buf
.length
);
1203 case WS_TCP_CHANNEL_BINDING
:
1204 if (channel
->encoding
== WS_ENCODING_XML_BINARY_SESSION_1
)
1206 switch (channel
->session_state
)
1208 case SESSION_STATE_UNINITIALIZED
:
1209 if ((hr
= send_preamble( channel
)) != S_OK
) return hr
;
1210 if ((hr
= receive_preamble_ack( channel
)) != S_OK
) return hr
;
1213 case SESSION_STATE_SETUP_COMPLETE
:
1214 return send_sized_envelope( channel
, buf
.bytes
, buf
.length
);
1217 ERR( "unhandled session state %u\n", channel
->session_state
);
1221 return send_bytes( channel
->u
.tcp
.socket
, buf
.bytes
, buf
.length
);
1223 case WS_UDP_CHANNEL_BINDING
:
1224 return send_bytes( channel
->u
.udp
.socket
, buf
.bytes
, buf
.length
);
1227 ERR( "unhandled binding %u\n", channel
->binding
);
1232 HRESULT
channel_send_message( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
)
1234 struct channel
*channel
= (struct channel
*)handle
;
1237 EnterCriticalSection( &channel
->cs
);
1239 if (channel
->magic
!= CHANNEL_MAGIC
)
1241 LeaveCriticalSection( &channel
->cs
);
1242 return E_INVALIDARG
;
1245 hr
= send_message( channel
, msg
);
1247 LeaveCriticalSection( &channel
->cs
);
1251 static HRESULT CALLBACK
dict_cb( void *state
, const WS_XML_STRING
*str
, BOOL
*found
, ULONG
*id
, WS_ERROR
*error
)
1253 struct dictionary
*dict
= state
;
1258 if ((index
= find_string( dict
, str
->bytes
, str
->length
, id
)) == -1)
1264 if (!(bytes
= heap_alloc( str
->length
))) return E_OUTOFMEMORY
;
1265 memcpy( bytes
, str
->bytes
, str
->length
);
1266 if ((hr
= insert_string( dict
, bytes
, str
->length
, index
, id
)) == S_OK
)
1277 static HRESULT
init_writer( struct channel
*channel
)
1279 WS_XML_WRITER_BUFFER_OUTPUT buf
= {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER
}};
1280 WS_XML_WRITER_TEXT_ENCODING text
= {{WS_XML_WRITER_ENCODING_TYPE_TEXT
}, WS_CHARSET_UTF8
};
1281 WS_XML_WRITER_BINARY_ENCODING bin
= {{WS_XML_WRITER_ENCODING_TYPE_BINARY
}};
1284 if (!channel
->writer
&& (hr
= WsCreateWriter( NULL
, 0, &channel
->writer
, NULL
)) != S_OK
) return hr
;
1286 switch (channel
->encoding
)
1288 case WS_ENCODING_XML_UTF8
:
1289 return WsSetOutput( channel
->writer
, &text
.encoding
, &buf
.output
, NULL
, 0, NULL
);
1291 case WS_ENCODING_XML_BINARY_SESSION_1
:
1292 bin
.staticDictionary
= (WS_XML_DICTIONARY
*)&dict_builtin_static
.dict
;
1295 case WS_ENCODING_XML_BINARY_1
:
1296 return WsSetOutput( channel
->writer
, &bin
.encoding
, &buf
.output
, NULL
, 0, NULL
);
1299 FIXME( "unhandled encoding %u\n", channel
->encoding
);
1300 return WS_E_NOT_SUPPORTED
;
1304 static HRESULT
write_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_ELEMENT_DESCRIPTION
*desc
,
1305 WS_WRITE_OPTION option
, const void *body
, ULONG size
)
1308 if ((hr
= writer_set_lookup( channel
->writer
, TRUE
)) != S_OK
) return hr
;
1309 if ((hr
= WsWriteEnvelopeStart( msg
, channel
->writer
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
1310 if ((hr
= writer_set_lookup( channel
->writer
, FALSE
)) != S_OK
) return hr
;
1311 channel
->dict_send
.current_sequence
++;
1312 if ((hr
= writer_set_dict_callback( channel
->writer
, dict_cb
, &channel
->dict_send
)) != S_OK
) return hr
;
1313 if ((hr
= WsWriteBody( msg
, desc
, option
, body
, size
, NULL
)) != S_OK
) return hr
;
1314 return WsWriteEnvelopeEnd( msg
, NULL
);
1317 /**************************************************************************
1318 * WsSendMessage [webservices.@]
1320 HRESULT WINAPI
WsSendMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
*desc
,
1321 WS_WRITE_OPTION option
, const void *body
, ULONG size
, const WS_ASYNC_CONTEXT
*ctx
,
1324 struct channel
*channel
= (struct channel
*)handle
;
1327 TRACE( "%p %p %p %08x %p %u %p %p\n", handle
, msg
, desc
, option
, body
, size
, ctx
, error
);
1328 if (error
) FIXME( "ignoring error parameter\n" );
1329 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1331 if (!channel
|| !msg
|| !desc
) return E_INVALIDARG
;
1333 EnterCriticalSection( &channel
->cs
);
1335 if (channel
->magic
!= CHANNEL_MAGIC
)
1337 LeaveCriticalSection( &channel
->cs
);
1338 return E_INVALIDARG
;
1341 if ((hr
= WsInitializeMessage( msg
, WS_REQUEST_MESSAGE
, NULL
, NULL
)) != S_OK
) goto done
;
1342 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
1343 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) goto done
;
1345 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
1346 if ((hr
= write_message( channel
, msg
, desc
->bodyElementDescription
, option
, body
, size
)) != S_OK
) goto done
;
1347 hr
= send_message( channel
, msg
);
1350 LeaveCriticalSection( &channel
->cs
);
1354 /**************************************************************************
1355 * WsSendReplyMessage [webservices.@]
1357 HRESULT WINAPI
WsSendReplyMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
*desc
,
1358 WS_WRITE_OPTION option
, const void *body
, ULONG size
, WS_MESSAGE
*request
,
1359 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1361 struct channel
*channel
= (struct channel
*)handle
;
1365 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle
, msg
, desc
, option
, body
, size
, request
, ctx
, error
);
1366 if (error
) FIXME( "ignoring error parameter\n" );
1367 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1369 if (!channel
|| !msg
|| !desc
|| !request
) return E_INVALIDARG
;
1371 EnterCriticalSection( &channel
->cs
);
1373 if (channel
->magic
!= CHANNEL_MAGIC
)
1375 LeaveCriticalSection( &channel
->cs
);
1376 return E_INVALIDARG
;
1379 if ((hr
= WsInitializeMessage( msg
, WS_REPLY_MESSAGE
, NULL
, NULL
)) != S_OK
) goto done
;
1380 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
1381 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) goto done
;
1382 if ((hr
= message_get_id( request
, &req_id
)) != S_OK
) goto done
;
1383 if ((hr
= message_set_request_id( msg
, &req_id
)) != S_OK
) goto done
;
1385 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
1386 if ((hr
= write_message( channel
, msg
, desc
->bodyElementDescription
, option
, body
, size
)) != S_OK
) goto done
;
1387 hr
= send_message( channel
, msg
);
1390 LeaveCriticalSection( &channel
->cs
);
1394 static HRESULT
resize_read_buffer( struct channel
*channel
, ULONG size
)
1396 if (!channel
->read_buf
)
1398 if (!(channel
->read_buf
= heap_alloc( size
))) return E_OUTOFMEMORY
;
1399 channel
->read_buflen
= size
;
1402 if (channel
->read_buflen
< size
)
1405 ULONG new_size
= max( size
, channel
->read_buflen
* 2 );
1406 if (!(tmp
= heap_realloc( channel
->read_buf
, new_size
))) return E_OUTOFMEMORY
;
1407 channel
->read_buf
= tmp
;
1408 channel
->read_buflen
= new_size
;
1413 static HRESULT
init_reader( struct channel
*channel
)
1415 WS_XML_READER_BUFFER_INPUT buf
= {{WS_XML_READER_INPUT_TYPE_BUFFER
}};
1416 WS_XML_READER_TEXT_ENCODING text
= {{WS_XML_READER_ENCODING_TYPE_TEXT
}};
1417 WS_XML_READER_BINARY_ENCODING bin
= {{WS_XML_READER_ENCODING_TYPE_BINARY
}};
1418 WS_XML_READER_ENCODING
*encoding
;
1421 if (!channel
->reader
&& (hr
= WsCreateReader( NULL
, 0, &channel
->reader
, NULL
)) != S_OK
) return hr
;
1423 switch (channel
->encoding
)
1425 case WS_ENCODING_XML_UTF8
:
1426 text
.charSet
= WS_CHARSET_UTF8
;
1427 encoding
= &text
.encoding
;
1430 case WS_ENCODING_XML_BINARY_SESSION_1
:
1431 bin
.staticDictionary
= (WS_XML_DICTIONARY
*)&dict_builtin_static
.dict
;
1432 bin
.dynamicDictionary
= &channel
->dict_recv
.dict
;
1435 case WS_ENCODING_XML_BINARY_1
:
1436 encoding
= &bin
.encoding
;
1440 FIXME( "unhandled encoding %u\n", channel
->encoding
);
1441 return WS_E_NOT_SUPPORTED
;
1444 buf
.encodedData
= channel
->read_buf
;
1445 buf
.encodedDataSize
= channel
->read_size
;
1446 return WsSetInput( channel
->reader
, encoding
, &buf
.input
, NULL
, 0, NULL
);
1449 #define INITIAL_READ_BUFFER_SIZE 4096
1450 static HRESULT
receive_message_http( struct channel
*channel
)
1452 DWORD len
, bytes_read
, offset
= 0, size
= INITIAL_READ_BUFFER_SIZE
;
1456 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE
,
1457 &max_len
, sizeof(max_len
) );
1459 if ((hr
= resize_read_buffer( channel
, size
)) != S_OK
) return hr
;
1460 channel
->read_size
= 0;
1463 if (!WinHttpQueryDataAvailable( channel
->u
.http
.request
, &len
))
1465 return HRESULT_FROM_WIN32( GetLastError() );
1468 if (channel
->read_size
+ len
> max_len
) return WS_E_QUOTA_EXCEEDED
;
1469 if ((hr
= resize_read_buffer( channel
, channel
->read_size
+ len
)) != S_OK
) return hr
;
1471 if (!WinHttpReadData( channel
->u
.http
.request
, channel
->read_buf
+ offset
, len
, &bytes_read
))
1473 return HRESULT_FROM_WIN32( GetLastError() );
1475 if (!bytes_read
) break;
1476 channel
->read_size
+= bytes_read
;
1477 offset
+= bytes_read
;
1480 return init_reader( channel
);
1483 static HRESULT
receive_message_unsized( struct channel
*channel
, SOCKET socket
)
1489 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE
,
1490 &max_len
, sizeof(max_len
) );
1492 if ((hr
= resize_read_buffer( channel
, max_len
)) != S_OK
) return hr
;
1494 channel
->read_size
= 0;
1495 if ((bytes_read
= recv( socket
, channel
->read_buf
, max_len
, 0 )) < 0)
1497 return HRESULT_FROM_WIN32( WSAGetLastError() );
1499 channel
->read_size
= bytes_read
;
1503 static HRESULT
receive_message_sized( struct channel
*channel
, unsigned int size
)
1505 unsigned int offset
= 0, to_read
= size
;
1509 if ((hr
= resize_read_buffer( channel
, size
)) != S_OK
) return hr
;
1511 channel
->read_size
= 0;
1512 while (channel
->read_size
< size
)
1514 if ((bytes_read
= recv( channel
->u
.tcp
.socket
, channel
->read_buf
+ offset
, to_read
, 0 )) < 0)
1516 return HRESULT_FROM_WIN32( WSAGetLastError() );
1518 if (!bytes_read
) break;
1519 channel
->read_size
+= bytes_read
;
1520 to_read
-= bytes_read
;
1521 offset
+= bytes_read
;
1523 if (channel
->read_size
!= size
) return WS_E_INVALID_FORMAT
;
1527 static HRESULT
receive_size( struct channel
*channel
, unsigned int *size
)
1532 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1533 *size
= byte
& 0x7f;
1534 if (!(byte
& 0x80)) return S_OK
;
1536 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1537 *size
+= (byte
& 0x7f) << 7;
1538 if (!(byte
& 0x80)) return S_OK
;
1540 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1541 *size
+= (byte
& 0x7f) << 14;
1542 if (!(byte
& 0x80)) return S_OK
;
1544 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1545 *size
+= (byte
& 0x7f) << 21;
1546 if (!(byte
& 0x80)) return S_OK
;
1548 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1549 if (byte
& ~0x0f) return WS_E_INVALID_FORMAT
;
1550 *size
+= byte
<< 28;
1554 static WS_ENCODING
map_known_encoding( enum known_encoding encoding
)
1558 case KNOWN_ENCODING_SOAP11_UTF8
:
1559 case KNOWN_ENCODING_SOAP12_UTF8
: return WS_ENCODING_XML_UTF8
;
1560 case KNOWN_ENCODING_SOAP11_UTF16
:
1561 case KNOWN_ENCODING_SOAP12_UTF16
: return WS_ENCODING_XML_UTF16BE
;
1562 case KNOWN_ENCODING_SOAP11_UTF16LE
:
1563 case KNOWN_ENCODING_SOAP12_UTF16LE
: return WS_ENCODING_XML_UTF16LE
;
1564 case KNOWN_ENCODING_SOAP12_BINARY
: return WS_ENCODING_XML_BINARY_1
;
1565 case KNOWN_ENCODING_SOAP12_BINARY_SESSION
: return WS_ENCODING_XML_BINARY_SESSION_1
;
1567 WARN( "unhandled encoding %u, assuming UTF8\n", encoding
);
1568 return WS_ENCODING_XML_UTF8
;
1572 static HRESULT
receive_preamble( struct channel
*channel
)
1579 if ((hr
= receive_bytes( channel
, &type
, 1 )) != S_OK
) return hr
;
1580 if (type
== FRAME_RECORD_TYPE_PREAMBLE_END
) break;
1583 case FRAME_RECORD_TYPE_VERSION
:
1585 unsigned char major
, minor
;
1586 if ((hr
= receive_bytes( channel
, &major
, 1 )) != S_OK
) return hr
;
1587 if ((hr
= receive_bytes( channel
, &minor
, 1 )) != S_OK
) return hr
;
1588 TRACE( "major %u minor %u\n", major
, major
);
1591 case FRAME_RECORD_TYPE_MODE
:
1594 if ((hr
= receive_bytes( channel
, &mode
, 1 )) != S_OK
) return hr
;
1595 TRACE( "mode %u\n", mode
);
1598 case FRAME_RECORD_TYPE_VIA
:
1603 if ((hr
= receive_size( channel
, &size
)) != S_OK
) return hr
;
1604 if (!(url
= heap_alloc( size
))) return E_OUTOFMEMORY
;
1605 if ((hr
= receive_bytes( channel
, url
, size
)) != S_OK
)
1610 TRACE( "transport URL %s\n", debugstr_an((char *)url
, size
) );
1611 heap_free( url
); /* FIXME: verify */
1614 case FRAME_RECORD_TYPE_KNOWN_ENCODING
:
1616 unsigned char encoding
;
1617 if ((hr
= receive_bytes( channel
, &encoding
, 1 )) != S_OK
) return hr
;
1618 TRACE( "encoding %u\n", encoding
);
1619 channel
->encoding
= map_known_encoding( encoding
);
1623 WARN( "unhandled record type %u\n", type
);
1624 return WS_E_INVALID_FORMAT
;
1631 static HRESULT
receive_sized_envelope( struct channel
*channel
)
1637 if ((hr
= receive_bytes( channel
, &type
, 1 )) != S_OK
) return hr
;
1638 if (type
!= FRAME_RECORD_TYPE_SIZED_ENVELOPE
) return WS_E_INVALID_FORMAT
;
1639 if ((hr
= receive_size( channel
, &size
)) != S_OK
) return hr
;
1640 if ((hr
= receive_message_sized( channel
, size
)) != S_OK
) return hr
;
1644 static HRESULT
read_size( const BYTE
**ptr
, ULONG len
, ULONG
*size
)
1646 const BYTE
*buf
= *ptr
;
1648 if (len
< 1) return WS_E_INVALID_FORMAT
;
1649 *size
= buf
[0] & 0x7f;
1650 if (!(buf
[0] & 0x80))
1655 if (len
< 2) return WS_E_INVALID_FORMAT
;
1656 *size
+= (buf
[1] & 0x7f) << 7;
1657 if (!(buf
[1] & 0x80))
1662 if (len
< 3) return WS_E_INVALID_FORMAT
;
1663 *size
+= (buf
[2] & 0x7f) << 14;
1664 if (!(buf
[2] & 0x80))
1669 if (len
< 4) return WS_E_INVALID_FORMAT
;
1670 *size
+= (buf
[3] & 0x7f) << 21;
1671 if (!(buf
[3] & 0x80))
1676 if (len
< 5 || (buf
[4] & ~0x07)) return WS_E_INVALID_FORMAT
;
1677 *size
+= buf
[4] << 28;
1682 static HRESULT
build_dict( const BYTE
*buf
, ULONG buflen
, struct dictionary
*dict
, ULONG
*used
)
1684 ULONG size
, strings_size
, strings_offset
;
1685 const BYTE
*ptr
= buf
;
1690 if ((hr
= read_size( &ptr
, buflen
, &strings_size
)) != S_OK
) return hr
;
1691 strings_offset
= ptr
- buf
;
1692 if (buflen
< strings_offset
+ strings_size
) return WS_E_INVALID_FORMAT
;
1693 *used
= strings_offset
+ strings_size
;
1694 if (!strings_size
) return S_OK
;
1696 UuidCreate( &dict
->dict
.guid
);
1697 dict
->dict
.isConst
= FALSE
;
1699 buflen
-= strings_offset
;
1700 ptr
= buf
+ strings_offset
;
1701 while (ptr
< buf
+ strings_size
)
1703 if ((hr
= read_size( &ptr
, buflen
, &size
)) != S_OK
)
1711 return WS_E_INVALID_FORMAT
;
1714 if (!(bytes
= heap_alloc( size
)))
1719 memcpy( bytes
, ptr
, size
);
1720 if ((index
= find_string( dict
, bytes
, size
, NULL
)) == -1) /* duplicate */
1726 if ((hr
= insert_string( dict
, bytes
, size
, index
, NULL
)) != S_OK
)
1741 static HRESULT
send_preamble_ack( struct channel
*channel
)
1744 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_PREAMBLE_ACK
)) != S_OK
) return hr
;
1745 channel
->session_state
= SESSION_STATE_SETUP_COMPLETE
;
1749 static HRESULT
receive_message_session( struct channel
*channel
)
1753 if ((hr
= receive_sized_envelope( channel
)) != S_OK
) return hr
;
1754 if (channel
->encoding
== WS_ENCODING_XML_BINARY_SESSION_1
)
1757 if ((hr
= build_dict( (const BYTE
*)channel
->read_buf
, channel
->read_size
, &channel
->dict_recv
,
1758 &size
)) != S_OK
) return hr
;
1759 channel
->read_size
-= size
;
1760 memmove( channel
->read_buf
, channel
->read_buf
+ size
, channel
->read_size
);
1763 return init_reader( channel
);
1766 static HRESULT
receive_message_sock( struct channel
*channel
, SOCKET socket
)
1769 if ((hr
= receive_message_unsized( channel
, socket
)) != S_OK
) return hr
;
1770 return init_reader( channel
);
1773 static HRESULT
receive_message_bytes( struct channel
*channel
)
1776 if ((hr
= connect_channel( channel
)) != S_OK
) return hr
;
1778 switch (channel
->binding
)
1780 case WS_HTTP_CHANNEL_BINDING
:
1781 return receive_message_http( channel
);
1783 case WS_TCP_CHANNEL_BINDING
:
1784 if (channel
->encoding
== WS_ENCODING_XML_BINARY_SESSION_1
)
1786 switch (channel
->session_state
)
1788 case SESSION_STATE_UNINITIALIZED
:
1789 if ((hr
= receive_preamble( channel
)) != S_OK
) return hr
;
1790 if ((hr
= send_preamble_ack( channel
)) != S_OK
) return hr
;
1793 case SESSION_STATE_SETUP_COMPLETE
:
1794 return receive_message_session( channel
);
1797 ERR( "unhandled session state %u\n", channel
->session_state
);
1801 return receive_message_sock( channel
, channel
->u
.tcp
.socket
);
1803 case WS_UDP_CHANNEL_BINDING
:
1804 return receive_message_sock( channel
, channel
->u
.udp
.socket
);
1807 ERR( "unhandled binding %u\n", channel
->binding
);
1812 HRESULT
channel_receive_message( WS_CHANNEL
*handle
)
1814 struct channel
*channel
= (struct channel
*)handle
;
1817 EnterCriticalSection( &channel
->cs
);
1819 if (channel
->magic
!= CHANNEL_MAGIC
)
1821 LeaveCriticalSection( &channel
->cs
);
1822 return E_INVALIDARG
;
1825 hr
= receive_message_bytes( channel
);
1827 LeaveCriticalSection( &channel
->cs
);
1831 HRESULT
channel_get_reader( WS_CHANNEL
*handle
, WS_XML_READER
**reader
)
1833 struct channel
*channel
= (struct channel
*)handle
;
1835 EnterCriticalSection( &channel
->cs
);
1837 if (channel
->magic
!= CHANNEL_MAGIC
)
1839 LeaveCriticalSection( &channel
->cs
);
1840 return E_INVALIDARG
;
1843 *reader
= channel
->reader
;
1845 LeaveCriticalSection( &channel
->cs
);
1849 static HRESULT
read_message( WS_MESSAGE
*handle
, WS_XML_READER
*reader
, const WS_ELEMENT_DESCRIPTION
*desc
,
1850 WS_READ_OPTION option
, WS_HEAP
*heap
, void *body
, ULONG size
)
1853 if ((hr
= WsReadEnvelopeStart( handle
, reader
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
1854 if ((hr
= WsReadBody( handle
, desc
, option
, heap
, body
, size
, NULL
)) != S_OK
) return hr
;
1855 return WsReadEnvelopeEnd( handle
, NULL
);
1858 static HRESULT
receive_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1859 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
, WS_HEAP
*heap
,
1860 void *value
, ULONG size
, ULONG
*index
)
1865 if ((hr
= receive_message_bytes( channel
)) != S_OK
) return hr
;
1866 for (i
= 0; i
< count
; i
++)
1868 const WS_ELEMENT_DESCRIPTION
*body
= desc
[i
]->bodyElementDescription
;
1869 if ((hr
= read_message( msg
, channel
->reader
, body
, read_option
, heap
, value
, size
)) == S_OK
)
1871 if (index
) *index
= i
;
1874 if ((hr
= WsResetMessage( msg
, NULL
)) != S_OK
) return hr
;
1875 if ((hr
= init_reader( channel
)) != S_OK
) return hr
;
1877 return (i
== count
) ? WS_E_INVALID_FORMAT
: S_OK
;
1880 struct receive_message
1883 struct channel
*channel
;
1885 const WS_MESSAGE_DESCRIPTION
**desc
;
1887 WS_RECEIVE_OPTION option
;
1888 WS_READ_OPTION read_option
;
1893 WS_ASYNC_CONTEXT ctx
;
1896 static void receive_message_proc( struct task
*task
)
1898 struct receive_message
*r
= (struct receive_message
*)task
;
1901 hr
= receive_message( r
->channel
, r
->msg
, r
->desc
, r
->count
, r
->option
, r
->read_option
, r
->heap
, r
->value
,
1902 r
->size
, r
->index
);
1904 TRACE( "calling %p(%08x)\n", r
->ctx
.callback
, hr
);
1905 r
->ctx
.callback( hr
, WS_LONG_CALLBACK
, r
->ctx
.callbackState
);
1906 TRACE( "%p returned\n", r
->ctx
.callback
);
1909 static HRESULT
queue_receive_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1910 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
,
1911 WS_HEAP
*heap
, void *value
, ULONG size
, ULONG
*index
,
1912 const WS_ASYNC_CONTEXT
*ctx
)
1914 struct receive_message
*r
;
1916 if (!(r
= heap_alloc( sizeof(*r
) ))) return E_OUTOFMEMORY
;
1917 r
->task
.proc
= receive_message_proc
;
1918 r
->channel
= channel
;
1923 r
->read_option
= read_option
;
1929 return queue_task( &channel
->recv_q
, &r
->task
);
1932 /**************************************************************************
1933 * WsReceiveMessage [webservices.@]
1935 HRESULT WINAPI
WsReceiveMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1936 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
, WS_HEAP
*heap
,
1937 void *value
, ULONG size
, ULONG
*index
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1939 struct channel
*channel
= (struct channel
*)handle
;
1942 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle
, msg
, desc
, count
, option
, read_option
, heap
,
1943 value
, size
, index
, ctx
, error
);
1944 if (error
) FIXME( "ignoring error parameter\n" );
1946 if (!channel
|| !msg
|| !desc
|| !count
) return E_INVALIDARG
;
1948 EnterCriticalSection( &channel
->cs
);
1950 if (channel
->magic
!= CHANNEL_MAGIC
)
1952 LeaveCriticalSection( &channel
->cs
);
1953 return E_INVALIDARG
;
1957 hr
= queue_receive_message( channel
, msg
, desc
, count
, option
, read_option
, heap
, value
, size
, index
, ctx
);
1959 hr
= receive_message( channel
, msg
, desc
, count
, option
, read_option
, heap
, value
, size
, index
);
1961 LeaveCriticalSection( &channel
->cs
);
1965 /**************************************************************************
1966 * WsReadMessageStart [webservices.@]
1968 HRESULT WINAPI
WsReadMessageStart( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
1971 struct channel
*channel
= (struct channel
*)handle
;
1974 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
1975 if (error
) FIXME( "ignoring error parameter\n" );
1976 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1978 if (!channel
|| !msg
) return E_INVALIDARG
;
1980 EnterCriticalSection( &channel
->cs
);
1982 if (channel
->magic
!= CHANNEL_MAGIC
)
1984 LeaveCriticalSection( &channel
->cs
);
1985 return E_INVALIDARG
;
1988 if ((hr
= receive_message_bytes( channel
)) == S_OK
)
1990 hr
= WsReadEnvelopeStart( msg
, channel
->reader
, NULL
, NULL
, NULL
);
1993 LeaveCriticalSection( &channel
->cs
);
1997 /**************************************************************************
1998 * WsReadMessageEnd [webservices.@]
2000 HRESULT WINAPI
WsReadMessageEnd( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2003 struct channel
*channel
= (struct channel
*)handle
;
2006 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2007 if (error
) FIXME( "ignoring error parameter\n" );
2008 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2010 if (!channel
|| !msg
) return E_INVALIDARG
;
2012 EnterCriticalSection( &channel
->cs
);
2014 if (channel
->magic
!= CHANNEL_MAGIC
)
2016 LeaveCriticalSection( &channel
->cs
);
2017 return E_INVALIDARG
;
2020 hr
= WsReadEnvelopeEnd( msg
, NULL
);
2022 LeaveCriticalSection( &channel
->cs
);
2026 /**************************************************************************
2027 * WsWriteMessageStart [webservices.@]
2029 HRESULT WINAPI
WsWriteMessageStart( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2032 struct channel
*channel
= (struct channel
*)handle
;
2035 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2036 if (error
) FIXME( "ignoring error parameter\n" );
2037 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2039 if (!channel
|| !msg
) return E_INVALIDARG
;
2041 EnterCriticalSection( &channel
->cs
);
2043 if (channel
->magic
!= CHANNEL_MAGIC
)
2045 LeaveCriticalSection( &channel
->cs
);
2046 return E_INVALIDARG
;
2049 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
2050 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
2051 hr
= WsWriteEnvelopeStart( msg
, channel
->writer
, NULL
, NULL
, NULL
);
2054 LeaveCriticalSection( &channel
->cs
);
2058 /**************************************************************************
2059 * WsWriteMessageEnd [webservices.@]
2061 HRESULT WINAPI
WsWriteMessageEnd( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2064 struct channel
*channel
= (struct channel
*)handle
;
2067 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2068 if (error
) FIXME( "ignoring error parameter\n" );
2069 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2071 if (!channel
|| !msg
) return E_INVALIDARG
;
2073 EnterCriticalSection( &channel
->cs
);
2075 if (channel
->magic
!= CHANNEL_MAGIC
)
2077 LeaveCriticalSection( &channel
->cs
);
2078 return E_INVALIDARG
;
2081 if ((hr
= WsWriteEnvelopeEnd( msg
, NULL
)) == S_OK
) hr
= send_message( channel
, msg
);
2083 LeaveCriticalSection( &channel
->cs
);
2087 static HRESULT
sock_accept( SOCKET socket
, HANDLE wait
, HANDLE cancel
, SOCKET
*ret
)
2089 HANDLE handles
[] = { wait
, cancel
};
2090 ULONG nonblocking
= 0;
2093 if (WSAEventSelect( socket
, handles
[0], FD_ACCEPT
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
2095 switch (WSAWaitForMultipleEvents( 2, handles
, FALSE
, WSA_INFINITE
, FALSE
))
2098 if ((*ret
= accept( socket
, NULL
, NULL
)) != -1)
2100 WSAEventSelect( *ret
, NULL
, 0 );
2101 ioctlsocket( *ret
, FIONBIO
, &nonblocking
);
2104 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2108 hr
= WS_E_OPERATION_ABORTED
;
2112 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2119 HRESULT
channel_accept_tcp( SOCKET socket
, HANDLE wait
, HANDLE cancel
, WS_CHANNEL
*handle
)
2121 struct channel
*channel
= (struct channel
*)handle
;
2124 EnterCriticalSection( &channel
->cs
);
2126 if (channel
->magic
!= CHANNEL_MAGIC
)
2128 LeaveCriticalSection( &channel
->cs
);
2129 return E_INVALIDARG
;
2132 hr
= sock_accept( socket
, wait
, cancel
, &channel
->u
.tcp
.socket
);
2134 LeaveCriticalSection( &channel
->cs
);
2138 static HRESULT
sock_wait( SOCKET socket
, HANDLE wait
, HANDLE cancel
)
2140 HANDLE handles
[] = { wait
, cancel
};
2141 ULONG nonblocking
= 0;
2144 if (WSAEventSelect( socket
, handles
[0], FD_READ
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
2146 switch (WSAWaitForMultipleEvents( 2, handles
, FALSE
, WSA_INFINITE
, FALSE
))
2153 hr
= WS_E_OPERATION_ABORTED
;
2157 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2161 WSAEventSelect( socket
, NULL
, 0 );
2162 ioctlsocket( socket
, FIONBIO
, &nonblocking
);
2166 HRESULT
channel_accept_udp( SOCKET socket
, HANDLE wait
, HANDLE cancel
, WS_CHANNEL
*handle
)
2168 struct channel
*channel
= (struct channel
*)handle
;
2171 EnterCriticalSection( &channel
->cs
);
2173 if (channel
->magic
!= CHANNEL_MAGIC
)
2175 LeaveCriticalSection( &channel
->cs
);
2176 return E_INVALIDARG
;
2179 if ((hr
= sock_wait( socket
, wait
, cancel
)) == S_OK
) channel
->u
.udp
.socket
= socket
;
2181 LeaveCriticalSection( &channel
->cs
);
2185 /**************************************************************************
2186 * WsAbortChannel [webservices.@]
2188 HRESULT WINAPI
WsAbortChannel( WS_CHANNEL
*handle
, WS_ERROR
*error
)
2190 FIXME( "%p %p: stub!\n", handle
, error
);