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
[ARRAY_SIZE( channel_props
)];
242 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
244 static struct channel
*alloc_channel(void)
246 static const ULONG count
= ARRAY_SIZE( channel_props
);
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 InitializeCriticalSection( &ret
->send_q
.cs
);
255 InitializeCriticalSection( &ret
->recv_q
.cs
);
257 ret
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": channel.cs");
258 ret
->send_q
.cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": channel.send_q.cs");
259 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
);
346 channel
->send_q
.cs
.DebugInfo
->Spare
[0] = 0;
347 channel
->recv_q
.cs
.DebugInfo
->Spare
[0] = 0;
348 channel
->cs
.DebugInfo
->Spare
[0] = 0;
350 DeleteCriticalSection( &channel
->send_q
.cs
);
351 DeleteCriticalSection( &channel
->recv_q
.cs
);
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
;
523 TRACE( "%p %p\n", handle
, error
);
524 if (error
) FIXME( "ignoring error parameter\n" );
526 if (!channel
) return E_INVALIDARG
;
528 EnterCriticalSection( &channel
->cs
);
530 if (channel
->magic
!= CHANNEL_MAGIC
)
532 LeaveCriticalSection( &channel
->cs
);
536 if (channel
->state
!= WS_CHANNEL_STATE_CREATED
&& channel
->state
!= WS_CHANNEL_STATE_CLOSED
)
537 hr
= WS_E_INVALID_OPERATION
;
539 reset_channel( channel
);
541 LeaveCriticalSection( &channel
->cs
);
542 TRACE( "returning %08x\n", hr
);
546 /**************************************************************************
547 * WsGetChannelProperty [webservices.@]
549 HRESULT WINAPI
WsGetChannelProperty( WS_CHANNEL
*handle
, WS_CHANNEL_PROPERTY_ID id
, void *buf
,
550 ULONG size
, WS_ERROR
*error
)
552 struct channel
*channel
= (struct channel
*)handle
;
555 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
556 if (error
) FIXME( "ignoring error parameter\n" );
558 if (!channel
) return E_INVALIDARG
;
560 EnterCriticalSection( &channel
->cs
);
562 if (channel
->magic
!= CHANNEL_MAGIC
)
564 LeaveCriticalSection( &channel
->cs
);
570 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE
:
571 if (!buf
|| size
!= sizeof(channel
->type
)) hr
= E_INVALIDARG
;
572 else *(WS_CHANNEL_TYPE
*)buf
= channel
->type
;
575 case WS_CHANNEL_PROPERTY_ENCODING
:
576 if (!buf
|| size
!= sizeof(channel
->encoding
)) hr
= E_INVALIDARG
;
577 else *(WS_ENCODING
*)buf
= channel
->encoding
;
581 hr
= prop_get( channel
->prop
, channel
->prop_count
, id
, buf
, size
);
584 LeaveCriticalSection( &channel
->cs
);
585 TRACE( "returning %08x\n", hr
);
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
);
614 TRACE( "returning %08x\n", hr
);
618 static HRESULT
open_channel( struct channel
*channel
, const WS_ENDPOINT_ADDRESS
*endpoint
)
620 if (endpoint
->headers
|| endpoint
->extensions
|| endpoint
->identity
)
622 FIXME( "headers, extensions or identity not supported\n" );
626 if (!(channel
->addr
.url
.chars
= heap_alloc( endpoint
->url
.length
* sizeof(WCHAR
) ))) return E_OUTOFMEMORY
;
627 memcpy( channel
->addr
.url
.chars
, endpoint
->url
.chars
, endpoint
->url
.length
* sizeof(WCHAR
) );
628 channel
->addr
.url
.length
= endpoint
->url
.length
;
630 channel
->state
= WS_CHANNEL_STATE_OPEN
;
634 /**************************************************************************
635 * WsOpenChannel [webservices.@]
637 HRESULT WINAPI
WsOpenChannel( WS_CHANNEL
*handle
, const WS_ENDPOINT_ADDRESS
*endpoint
,
638 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
640 struct channel
*channel
= (struct channel
*)handle
;
643 TRACE( "%p %p %p %p\n", handle
, endpoint
, ctx
, error
);
644 if (error
) FIXME( "ignoring error parameter\n" );
645 if (ctx
) FIXME( "ignoring ctx parameter\n" );
647 if (!channel
|| !endpoint
) return E_INVALIDARG
;
649 EnterCriticalSection( &channel
->cs
);
651 if (channel
->magic
!= CHANNEL_MAGIC
)
653 LeaveCriticalSection( &channel
->cs
);
657 if (channel
->state
!= WS_CHANNEL_STATE_CREATED
) hr
= WS_E_INVALID_OPERATION
;
658 else hr
= open_channel( channel
, endpoint
);
660 LeaveCriticalSection( &channel
->cs
);
661 TRACE( "returning %08x\n", hr
);
665 enum frame_record_type
667 FRAME_RECORD_TYPE_VERSION
,
668 FRAME_RECORD_TYPE_MODE
,
669 FRAME_RECORD_TYPE_VIA
,
670 FRAME_RECORD_TYPE_KNOWN_ENCODING
,
671 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING
,
672 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE
,
673 FRAME_RECORD_TYPE_SIZED_ENVELOPE
,
674 FRAME_RECORD_TYPE_END
,
675 FRAME_RECORD_TYPE_FAULT
,
676 FRAME_RECORD_TYPE_UPGRADE_REQUEST
,
677 FRAME_RECORD_TYPE_UPGRADE_RESPONSE
,
678 FRAME_RECORD_TYPE_PREAMBLE_ACK
,
679 FRAME_RECORD_TYPE_PREAMBLE_END
,
682 static HRESULT
send_byte( SOCKET socket
, BYTE byte
)
684 int count
= send( socket
, (char *)&byte
, 1, 0 );
685 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
686 if (count
!= 1) return WS_E_OTHER
;
690 static HRESULT
shutdown_session( struct channel
*channel
)
694 if (channel
->state
!= WS_CHANNEL_STATE_OPEN
||
695 (channel
->type
!= WS_CHANNEL_TYPE_OUTPUT_SESSION
&&
696 channel
->type
!= WS_CHANNEL_TYPE_DUPLEX_SESSION
) ||
697 channel
->session_state
>= SESSION_STATE_SHUTDOWN
) return WS_E_INVALID_OPERATION
;
699 switch (channel
->binding
)
701 case WS_TCP_CHANNEL_BINDING
:
702 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_END
)) != S_OK
) return hr
;
703 channel
->session_state
= SESSION_STATE_SHUTDOWN
;
707 FIXME( "unhandled binding %u\n", channel
->binding
);
712 HRESULT WINAPI
WsShutdownSessionChannel( WS_CHANNEL
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
714 struct channel
*channel
= (struct channel
*)handle
;
717 TRACE( "%p %p %p\n", handle
, ctx
, error
);
718 if (error
) FIXME( "ignoring error parameter\n" );
719 if (ctx
) FIXME( "ignoring ctx parameter\n" );
721 if (!channel
) return E_INVALIDARG
;
723 EnterCriticalSection( &channel
->cs
);
725 if (channel
->magic
!= CHANNEL_MAGIC
)
727 LeaveCriticalSection( &channel
->cs
);
731 hr
= shutdown_session( channel
);
733 LeaveCriticalSection( &channel
->cs
);
734 TRACE( "returning %08x\n", hr
);
738 static void close_channel( struct channel
*channel
)
740 reset_channel( channel
);
741 channel
->state
= WS_CHANNEL_STATE_CLOSED
;
744 /**************************************************************************
745 * WsCloseChannel [webservices.@]
747 HRESULT WINAPI
WsCloseChannel( WS_CHANNEL
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
749 struct channel
*channel
= (struct channel
*)handle
;
752 TRACE( "%p %p %p\n", handle
, ctx
, error
);
753 if (error
) FIXME( "ignoring error parameter\n" );
754 if (ctx
) FIXME( "ignoring ctx parameter\n" );
756 if (!channel
) return E_INVALIDARG
;
758 EnterCriticalSection( &channel
->cs
);
760 if (channel
->magic
!= CHANNEL_MAGIC
)
762 LeaveCriticalSection( &channel
->cs
);
766 close_channel( channel
);
768 LeaveCriticalSection( &channel
->cs
);
769 TRACE( "returning %08x\n", hr
);
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
);
1351 TRACE( "returning %08x\n", hr
);
1355 /**************************************************************************
1356 * WsSendReplyMessage [webservices.@]
1358 HRESULT WINAPI
WsSendReplyMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
*desc
,
1359 WS_WRITE_OPTION option
, const void *body
, ULONG size
, WS_MESSAGE
*request
,
1360 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1362 struct channel
*channel
= (struct channel
*)handle
;
1366 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle
, msg
, desc
, option
, body
, size
, request
, ctx
, error
);
1367 if (error
) FIXME( "ignoring error parameter\n" );
1368 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1370 if (!channel
|| !msg
|| !desc
|| !request
) return E_INVALIDARG
;
1372 EnterCriticalSection( &channel
->cs
);
1374 if (channel
->magic
!= CHANNEL_MAGIC
)
1376 LeaveCriticalSection( &channel
->cs
);
1377 return E_INVALIDARG
;
1380 if ((hr
= WsInitializeMessage( msg
, WS_REPLY_MESSAGE
, NULL
, NULL
)) != S_OK
) goto done
;
1381 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
1382 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) goto done
;
1383 if ((hr
= message_get_id( request
, &req_id
)) != S_OK
) goto done
;
1384 if ((hr
= message_set_request_id( msg
, &req_id
)) != S_OK
) goto done
;
1386 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
1387 if ((hr
= write_message( channel
, msg
, desc
->bodyElementDescription
, option
, body
, size
)) != S_OK
) goto done
;
1388 hr
= send_message( channel
, msg
);
1391 LeaveCriticalSection( &channel
->cs
);
1392 TRACE( "returning %08x\n", hr
);
1396 static HRESULT
resize_read_buffer( struct channel
*channel
, ULONG size
)
1398 if (!channel
->read_buf
)
1400 if (!(channel
->read_buf
= heap_alloc( size
))) return E_OUTOFMEMORY
;
1401 channel
->read_buflen
= size
;
1404 if (channel
->read_buflen
< size
)
1407 ULONG new_size
= max( size
, channel
->read_buflen
* 2 );
1408 if (!(tmp
= heap_realloc( channel
->read_buf
, new_size
))) return E_OUTOFMEMORY
;
1409 channel
->read_buf
= tmp
;
1410 channel
->read_buflen
= new_size
;
1415 static HRESULT
init_reader( struct channel
*channel
)
1417 WS_XML_READER_BUFFER_INPUT buf
= {{WS_XML_READER_INPUT_TYPE_BUFFER
}};
1418 WS_XML_READER_TEXT_ENCODING text
= {{WS_XML_READER_ENCODING_TYPE_TEXT
}};
1419 WS_XML_READER_BINARY_ENCODING bin
= {{WS_XML_READER_ENCODING_TYPE_BINARY
}};
1420 WS_XML_READER_ENCODING
*encoding
;
1423 if (!channel
->reader
&& (hr
= WsCreateReader( NULL
, 0, &channel
->reader
, NULL
)) != S_OK
) return hr
;
1425 switch (channel
->encoding
)
1427 case WS_ENCODING_XML_UTF8
:
1428 text
.charSet
= WS_CHARSET_UTF8
;
1429 encoding
= &text
.encoding
;
1432 case WS_ENCODING_XML_BINARY_SESSION_1
:
1433 bin
.staticDictionary
= (WS_XML_DICTIONARY
*)&dict_builtin_static
.dict
;
1434 bin
.dynamicDictionary
= &channel
->dict_recv
.dict
;
1437 case WS_ENCODING_XML_BINARY_1
:
1438 encoding
= &bin
.encoding
;
1442 FIXME( "unhandled encoding %u\n", channel
->encoding
);
1443 return WS_E_NOT_SUPPORTED
;
1446 buf
.encodedData
= channel
->read_buf
;
1447 buf
.encodedDataSize
= channel
->read_size
;
1448 return WsSetInput( channel
->reader
, encoding
, &buf
.input
, NULL
, 0, NULL
);
1451 #define INITIAL_READ_BUFFER_SIZE 4096
1452 static HRESULT
receive_message_http( struct channel
*channel
)
1454 DWORD len
, bytes_read
, offset
= 0, size
= INITIAL_READ_BUFFER_SIZE
;
1458 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE
,
1459 &max_len
, sizeof(max_len
) );
1461 if ((hr
= resize_read_buffer( channel
, size
)) != S_OK
) return hr
;
1462 channel
->read_size
= 0;
1465 if (!WinHttpQueryDataAvailable( channel
->u
.http
.request
, &len
))
1467 return HRESULT_FROM_WIN32( GetLastError() );
1470 if (channel
->read_size
+ len
> max_len
) return WS_E_QUOTA_EXCEEDED
;
1471 if ((hr
= resize_read_buffer( channel
, channel
->read_size
+ len
)) != S_OK
) return hr
;
1473 if (!WinHttpReadData( channel
->u
.http
.request
, channel
->read_buf
+ offset
, len
, &bytes_read
))
1475 return HRESULT_FROM_WIN32( GetLastError() );
1477 if (!bytes_read
) break;
1478 channel
->read_size
+= bytes_read
;
1479 offset
+= bytes_read
;
1482 return init_reader( channel
);
1485 static HRESULT
receive_message_unsized( struct channel
*channel
, SOCKET socket
)
1491 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE
,
1492 &max_len
, sizeof(max_len
) );
1494 if ((hr
= resize_read_buffer( channel
, max_len
)) != S_OK
) return hr
;
1496 channel
->read_size
= 0;
1497 if ((bytes_read
= recv( socket
, channel
->read_buf
, max_len
, 0 )) < 0)
1499 return HRESULT_FROM_WIN32( WSAGetLastError() );
1501 channel
->read_size
= bytes_read
;
1505 static HRESULT
receive_message_sized( struct channel
*channel
, unsigned int size
)
1507 unsigned int offset
= 0, to_read
= size
;
1511 if ((hr
= resize_read_buffer( channel
, size
)) != S_OK
) return hr
;
1513 channel
->read_size
= 0;
1514 while (channel
->read_size
< size
)
1516 if ((bytes_read
= recv( channel
->u
.tcp
.socket
, channel
->read_buf
+ offset
, to_read
, 0 )) < 0)
1518 return HRESULT_FROM_WIN32( WSAGetLastError() );
1520 if (!bytes_read
) break;
1521 channel
->read_size
+= bytes_read
;
1522 to_read
-= bytes_read
;
1523 offset
+= bytes_read
;
1525 if (channel
->read_size
!= size
) return WS_E_INVALID_FORMAT
;
1529 static HRESULT
receive_size( struct channel
*channel
, unsigned int *size
)
1534 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1535 *size
= byte
& 0x7f;
1536 if (!(byte
& 0x80)) return S_OK
;
1538 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1539 *size
+= (byte
& 0x7f) << 7;
1540 if (!(byte
& 0x80)) return S_OK
;
1542 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1543 *size
+= (byte
& 0x7f) << 14;
1544 if (!(byte
& 0x80)) return S_OK
;
1546 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1547 *size
+= (byte
& 0x7f) << 21;
1548 if (!(byte
& 0x80)) return S_OK
;
1550 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1551 if (byte
& ~0x0f) return WS_E_INVALID_FORMAT
;
1552 *size
+= byte
<< 28;
1556 static WS_ENCODING
map_known_encoding( enum known_encoding encoding
)
1560 case KNOWN_ENCODING_SOAP11_UTF8
:
1561 case KNOWN_ENCODING_SOAP12_UTF8
: return WS_ENCODING_XML_UTF8
;
1562 case KNOWN_ENCODING_SOAP11_UTF16
:
1563 case KNOWN_ENCODING_SOAP12_UTF16
: return WS_ENCODING_XML_UTF16BE
;
1564 case KNOWN_ENCODING_SOAP11_UTF16LE
:
1565 case KNOWN_ENCODING_SOAP12_UTF16LE
: return WS_ENCODING_XML_UTF16LE
;
1566 case KNOWN_ENCODING_SOAP12_BINARY
: return WS_ENCODING_XML_BINARY_1
;
1567 case KNOWN_ENCODING_SOAP12_BINARY_SESSION
: return WS_ENCODING_XML_BINARY_SESSION_1
;
1569 WARN( "unhandled encoding %u, assuming UTF8\n", encoding
);
1570 return WS_ENCODING_XML_UTF8
;
1574 static HRESULT
receive_preamble( struct channel
*channel
)
1581 if ((hr
= receive_bytes( channel
, &type
, 1 )) != S_OK
) return hr
;
1582 if (type
== FRAME_RECORD_TYPE_PREAMBLE_END
) break;
1585 case FRAME_RECORD_TYPE_VERSION
:
1587 unsigned char major
, minor
;
1588 if ((hr
= receive_bytes( channel
, &major
, 1 )) != S_OK
) return hr
;
1589 if ((hr
= receive_bytes( channel
, &minor
, 1 )) != S_OK
) return hr
;
1590 TRACE( "major %u minor %u\n", major
, major
);
1593 case FRAME_RECORD_TYPE_MODE
:
1596 if ((hr
= receive_bytes( channel
, &mode
, 1 )) != S_OK
) return hr
;
1597 TRACE( "mode %u\n", mode
);
1600 case FRAME_RECORD_TYPE_VIA
:
1605 if ((hr
= receive_size( channel
, &size
)) != S_OK
) return hr
;
1606 if (!(url
= heap_alloc( size
))) return E_OUTOFMEMORY
;
1607 if ((hr
= receive_bytes( channel
, url
, size
)) != S_OK
)
1612 TRACE( "transport URL %s\n", debugstr_an((char *)url
, size
) );
1613 heap_free( url
); /* FIXME: verify */
1616 case FRAME_RECORD_TYPE_KNOWN_ENCODING
:
1618 unsigned char encoding
;
1619 if ((hr
= receive_bytes( channel
, &encoding
, 1 )) != S_OK
) return hr
;
1620 TRACE( "encoding %u\n", encoding
);
1621 channel
->encoding
= map_known_encoding( encoding
);
1625 WARN( "unhandled record type %u\n", type
);
1626 return WS_E_INVALID_FORMAT
;
1633 static HRESULT
receive_sized_envelope( struct channel
*channel
)
1639 if ((hr
= receive_bytes( channel
, &type
, 1 )) != S_OK
) return hr
;
1640 if (type
!= FRAME_RECORD_TYPE_SIZED_ENVELOPE
) return WS_E_INVALID_FORMAT
;
1641 if ((hr
= receive_size( channel
, &size
)) != S_OK
) return hr
;
1642 if ((hr
= receive_message_sized( channel
, size
)) != S_OK
) return hr
;
1646 static HRESULT
read_size( const BYTE
**ptr
, ULONG len
, ULONG
*size
)
1648 const BYTE
*buf
= *ptr
;
1650 if (len
< 1) return WS_E_INVALID_FORMAT
;
1651 *size
= buf
[0] & 0x7f;
1652 if (!(buf
[0] & 0x80))
1657 if (len
< 2) return WS_E_INVALID_FORMAT
;
1658 *size
+= (buf
[1] & 0x7f) << 7;
1659 if (!(buf
[1] & 0x80))
1664 if (len
< 3) return WS_E_INVALID_FORMAT
;
1665 *size
+= (buf
[2] & 0x7f) << 14;
1666 if (!(buf
[2] & 0x80))
1671 if (len
< 4) return WS_E_INVALID_FORMAT
;
1672 *size
+= (buf
[3] & 0x7f) << 21;
1673 if (!(buf
[3] & 0x80))
1678 if (len
< 5 || (buf
[4] & ~0x07)) return WS_E_INVALID_FORMAT
;
1679 *size
+= buf
[4] << 28;
1684 static HRESULT
build_dict( const BYTE
*buf
, ULONG buflen
, struct dictionary
*dict
, ULONG
*used
)
1686 ULONG size
, strings_size
, strings_offset
;
1687 const BYTE
*ptr
= buf
;
1692 if ((hr
= read_size( &ptr
, buflen
, &strings_size
)) != S_OK
) return hr
;
1693 strings_offset
= ptr
- buf
;
1694 if (buflen
< strings_offset
+ strings_size
) return WS_E_INVALID_FORMAT
;
1695 *used
= strings_offset
+ strings_size
;
1696 if (!strings_size
) return S_OK
;
1698 UuidCreate( &dict
->dict
.guid
);
1699 dict
->dict
.isConst
= FALSE
;
1701 buflen
-= strings_offset
;
1702 ptr
= buf
+ strings_offset
;
1703 while (ptr
< buf
+ strings_size
)
1705 if ((hr
= read_size( &ptr
, buflen
, &size
)) != S_OK
)
1713 return WS_E_INVALID_FORMAT
;
1716 if (!(bytes
= heap_alloc( size
)))
1721 memcpy( bytes
, ptr
, size
);
1722 if ((index
= find_string( dict
, bytes
, size
, NULL
)) == -1) /* duplicate */
1728 if ((hr
= insert_string( dict
, bytes
, size
, index
, NULL
)) != S_OK
)
1743 static HRESULT
send_preamble_ack( struct channel
*channel
)
1746 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_PREAMBLE_ACK
)) != S_OK
) return hr
;
1747 channel
->session_state
= SESSION_STATE_SETUP_COMPLETE
;
1751 static HRESULT
receive_message_session( struct channel
*channel
)
1755 if ((hr
= receive_sized_envelope( channel
)) != S_OK
) return hr
;
1756 if (channel
->encoding
== WS_ENCODING_XML_BINARY_SESSION_1
)
1759 if ((hr
= build_dict( (const BYTE
*)channel
->read_buf
, channel
->read_size
, &channel
->dict_recv
,
1760 &size
)) != S_OK
) return hr
;
1761 channel
->read_size
-= size
;
1762 memmove( channel
->read_buf
, channel
->read_buf
+ size
, channel
->read_size
);
1765 return init_reader( channel
);
1768 static HRESULT
receive_message_sock( struct channel
*channel
, SOCKET socket
)
1771 if ((hr
= receive_message_unsized( channel
, socket
)) != S_OK
) return hr
;
1772 return init_reader( channel
);
1775 static HRESULT
receive_message_bytes( struct channel
*channel
)
1778 if ((hr
= connect_channel( channel
)) != S_OK
) return hr
;
1780 switch (channel
->binding
)
1782 case WS_HTTP_CHANNEL_BINDING
:
1783 return receive_message_http( channel
);
1785 case WS_TCP_CHANNEL_BINDING
:
1786 if (channel
->encoding
== WS_ENCODING_XML_BINARY_SESSION_1
)
1788 switch (channel
->session_state
)
1790 case SESSION_STATE_UNINITIALIZED
:
1791 if ((hr
= receive_preamble( channel
)) != S_OK
) return hr
;
1792 if ((hr
= send_preamble_ack( channel
)) != S_OK
) return hr
;
1795 case SESSION_STATE_SETUP_COMPLETE
:
1796 return receive_message_session( channel
);
1799 ERR( "unhandled session state %u\n", channel
->session_state
);
1803 return receive_message_sock( channel
, channel
->u
.tcp
.socket
);
1805 case WS_UDP_CHANNEL_BINDING
:
1806 return receive_message_sock( channel
, channel
->u
.udp
.socket
);
1809 ERR( "unhandled binding %u\n", channel
->binding
);
1814 HRESULT
channel_receive_message( WS_CHANNEL
*handle
)
1816 struct channel
*channel
= (struct channel
*)handle
;
1819 EnterCriticalSection( &channel
->cs
);
1821 if (channel
->magic
!= CHANNEL_MAGIC
)
1823 LeaveCriticalSection( &channel
->cs
);
1824 return E_INVALIDARG
;
1827 hr
= receive_message_bytes( channel
);
1829 LeaveCriticalSection( &channel
->cs
);
1833 HRESULT
channel_get_reader( WS_CHANNEL
*handle
, WS_XML_READER
**reader
)
1835 struct channel
*channel
= (struct channel
*)handle
;
1837 EnterCriticalSection( &channel
->cs
);
1839 if (channel
->magic
!= CHANNEL_MAGIC
)
1841 LeaveCriticalSection( &channel
->cs
);
1842 return E_INVALIDARG
;
1845 *reader
= channel
->reader
;
1847 LeaveCriticalSection( &channel
->cs
);
1851 static HRESULT
read_message( WS_MESSAGE
*handle
, WS_XML_READER
*reader
, const WS_ELEMENT_DESCRIPTION
*desc
,
1852 WS_READ_OPTION option
, WS_HEAP
*heap
, void *body
, ULONG size
)
1855 if ((hr
= WsReadEnvelopeStart( handle
, reader
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
1856 if ((hr
= WsReadBody( handle
, desc
, option
, heap
, body
, size
, NULL
)) != S_OK
) return hr
;
1857 return WsReadEnvelopeEnd( handle
, NULL
);
1860 static HRESULT
receive_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1861 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
, WS_HEAP
*heap
,
1862 void *value
, ULONG size
, ULONG
*index
)
1867 if ((hr
= receive_message_bytes( channel
)) != S_OK
) return hr
;
1868 for (i
= 0; i
< count
; i
++)
1870 const WS_ELEMENT_DESCRIPTION
*body
= desc
[i
]->bodyElementDescription
;
1871 if ((hr
= read_message( msg
, channel
->reader
, body
, read_option
, heap
, value
, size
)) == S_OK
)
1873 if (index
) *index
= i
;
1876 if ((hr
= WsResetMessage( msg
, NULL
)) != S_OK
) return hr
;
1877 if ((hr
= init_reader( channel
)) != S_OK
) return hr
;
1879 return (i
== count
) ? WS_E_INVALID_FORMAT
: S_OK
;
1882 struct receive_message
1885 struct channel
*channel
;
1887 const WS_MESSAGE_DESCRIPTION
**desc
;
1889 WS_RECEIVE_OPTION option
;
1890 WS_READ_OPTION read_option
;
1895 WS_ASYNC_CONTEXT ctx
;
1898 static void receive_message_proc( struct task
*task
)
1900 struct receive_message
*r
= (struct receive_message
*)task
;
1903 hr
= receive_message( r
->channel
, r
->msg
, r
->desc
, r
->count
, r
->option
, r
->read_option
, r
->heap
, r
->value
,
1904 r
->size
, r
->index
);
1906 TRACE( "calling %p(%08x)\n", r
->ctx
.callback
, hr
);
1907 r
->ctx
.callback( hr
, WS_LONG_CALLBACK
, r
->ctx
.callbackState
);
1908 TRACE( "%p returned\n", r
->ctx
.callback
);
1911 static HRESULT
queue_receive_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1912 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
,
1913 WS_HEAP
*heap
, void *value
, ULONG size
, ULONG
*index
,
1914 const WS_ASYNC_CONTEXT
*ctx
)
1916 struct receive_message
*r
;
1918 if (!(r
= heap_alloc( sizeof(*r
) ))) return E_OUTOFMEMORY
;
1919 r
->task
.proc
= receive_message_proc
;
1920 r
->channel
= channel
;
1925 r
->read_option
= read_option
;
1931 return queue_task( &channel
->recv_q
, &r
->task
);
1934 /**************************************************************************
1935 * WsReceiveMessage [webservices.@]
1937 HRESULT WINAPI
WsReceiveMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1938 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
, WS_HEAP
*heap
,
1939 void *value
, ULONG size
, ULONG
*index
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1941 struct channel
*channel
= (struct channel
*)handle
;
1944 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle
, msg
, desc
, count
, option
, read_option
, heap
,
1945 value
, size
, index
, ctx
, error
);
1946 if (error
) FIXME( "ignoring error parameter\n" );
1948 if (!channel
|| !msg
|| !desc
|| !count
) return E_INVALIDARG
;
1950 EnterCriticalSection( &channel
->cs
);
1952 if (channel
->magic
!= CHANNEL_MAGIC
)
1954 LeaveCriticalSection( &channel
->cs
);
1955 return E_INVALIDARG
;
1959 hr
= queue_receive_message( channel
, msg
, desc
, count
, option
, read_option
, heap
, value
, size
, index
, ctx
);
1961 hr
= receive_message( channel
, msg
, desc
, count
, option
, read_option
, heap
, value
, size
, index
);
1963 LeaveCriticalSection( &channel
->cs
);
1964 TRACE( "returning %08x\n", hr
);
1968 /**************************************************************************
1969 * WsReadMessageStart [webservices.@]
1971 HRESULT WINAPI
WsReadMessageStart( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
1974 struct channel
*channel
= (struct channel
*)handle
;
1977 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
1978 if (error
) FIXME( "ignoring error parameter\n" );
1979 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1981 if (!channel
|| !msg
) return E_INVALIDARG
;
1983 EnterCriticalSection( &channel
->cs
);
1985 if (channel
->magic
!= CHANNEL_MAGIC
)
1987 LeaveCriticalSection( &channel
->cs
);
1988 return E_INVALIDARG
;
1991 if ((hr
= receive_message_bytes( channel
)) == S_OK
)
1993 hr
= WsReadEnvelopeStart( msg
, channel
->reader
, NULL
, NULL
, NULL
);
1996 LeaveCriticalSection( &channel
->cs
);
1997 TRACE( "returning %08x\n", hr
);
2001 /**************************************************************************
2002 * WsReadMessageEnd [webservices.@]
2004 HRESULT WINAPI
WsReadMessageEnd( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2007 struct channel
*channel
= (struct channel
*)handle
;
2010 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2011 if (error
) FIXME( "ignoring error parameter\n" );
2012 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2014 if (!channel
|| !msg
) return E_INVALIDARG
;
2016 EnterCriticalSection( &channel
->cs
);
2018 if (channel
->magic
!= CHANNEL_MAGIC
)
2020 LeaveCriticalSection( &channel
->cs
);
2021 return E_INVALIDARG
;
2024 hr
= WsReadEnvelopeEnd( msg
, NULL
);
2026 LeaveCriticalSection( &channel
->cs
);
2027 TRACE( "returning %08x\n", hr
);
2031 /**************************************************************************
2032 * WsWriteMessageStart [webservices.@]
2034 HRESULT WINAPI
WsWriteMessageStart( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2037 struct channel
*channel
= (struct channel
*)handle
;
2040 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2041 if (error
) FIXME( "ignoring error parameter\n" );
2042 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2044 if (!channel
|| !msg
) return E_INVALIDARG
;
2046 EnterCriticalSection( &channel
->cs
);
2048 if (channel
->magic
!= CHANNEL_MAGIC
)
2050 LeaveCriticalSection( &channel
->cs
);
2051 return E_INVALIDARG
;
2054 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
2055 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
2056 hr
= WsWriteEnvelopeStart( msg
, channel
->writer
, NULL
, NULL
, NULL
);
2059 LeaveCriticalSection( &channel
->cs
);
2060 TRACE( "returning %08x\n", hr
);
2064 /**************************************************************************
2065 * WsWriteMessageEnd [webservices.@]
2067 HRESULT WINAPI
WsWriteMessageEnd( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2070 struct channel
*channel
= (struct channel
*)handle
;
2073 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2074 if (error
) FIXME( "ignoring error parameter\n" );
2075 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2077 if (!channel
|| !msg
) return E_INVALIDARG
;
2079 EnterCriticalSection( &channel
->cs
);
2081 if (channel
->magic
!= CHANNEL_MAGIC
)
2083 LeaveCriticalSection( &channel
->cs
);
2084 return E_INVALIDARG
;
2087 if ((hr
= WsWriteEnvelopeEnd( msg
, NULL
)) == S_OK
) hr
= send_message( channel
, msg
);
2089 LeaveCriticalSection( &channel
->cs
);
2090 TRACE( "returning %08x\n", hr
);
2094 static HRESULT
sock_accept( SOCKET socket
, HANDLE wait
, HANDLE cancel
, SOCKET
*ret
)
2096 HANDLE handles
[] = { wait
, cancel
};
2097 ULONG nonblocking
= 0;
2100 if (WSAEventSelect( socket
, handles
[0], FD_ACCEPT
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
2102 switch (WSAWaitForMultipleEvents( 2, handles
, FALSE
, WSA_INFINITE
, FALSE
))
2105 if ((*ret
= accept( socket
, NULL
, NULL
)) != -1)
2107 WSAEventSelect( *ret
, NULL
, 0 );
2108 ioctlsocket( *ret
, FIONBIO
, &nonblocking
);
2111 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2115 hr
= WS_E_OPERATION_ABORTED
;
2119 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2126 HRESULT
channel_accept_tcp( SOCKET socket
, HANDLE wait
, HANDLE cancel
, WS_CHANNEL
*handle
)
2128 struct channel
*channel
= (struct channel
*)handle
;
2131 EnterCriticalSection( &channel
->cs
);
2133 if (channel
->magic
!= CHANNEL_MAGIC
)
2135 LeaveCriticalSection( &channel
->cs
);
2136 return E_INVALIDARG
;
2139 hr
= sock_accept( socket
, wait
, cancel
, &channel
->u
.tcp
.socket
);
2141 LeaveCriticalSection( &channel
->cs
);
2145 static HRESULT
sock_wait( SOCKET socket
, HANDLE wait
, HANDLE cancel
)
2147 HANDLE handles
[] = { wait
, cancel
};
2148 ULONG nonblocking
= 0;
2151 if (WSAEventSelect( socket
, handles
[0], FD_READ
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
2153 switch (WSAWaitForMultipleEvents( 2, handles
, FALSE
, WSA_INFINITE
, FALSE
))
2160 hr
= WS_E_OPERATION_ABORTED
;
2164 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2168 WSAEventSelect( socket
, NULL
, 0 );
2169 ioctlsocket( socket
, FIONBIO
, &nonblocking
);
2173 HRESULT
channel_accept_udp( SOCKET socket
, HANDLE wait
, HANDLE cancel
, WS_CHANNEL
*handle
)
2175 struct channel
*channel
= (struct channel
*)handle
;
2178 EnterCriticalSection( &channel
->cs
);
2180 if (channel
->magic
!= CHANNEL_MAGIC
)
2182 LeaveCriticalSection( &channel
->cs
);
2183 return E_INVALIDARG
;
2186 if ((hr
= sock_wait( socket
, wait
, cancel
)) == S_OK
) channel
->u
.udp
.socket
= socket
;
2188 LeaveCriticalSection( &channel
->cs
);
2192 /**************************************************************************
2193 * WsAbortChannel [webservices.@]
2195 HRESULT WINAPI
WsAbortChannel( WS_CHANNEL
*handle
, WS_ERROR
*error
)
2197 FIXME( "%p %p: stub!\n", handle
, error
);