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 TRACE( "property id %u value ptr %p size %u\n", properties
[i
].id
, properties
[i
].value
,
399 properties
[i
].valueSize
);
400 if (properties
[i
].valueSize
== sizeof(ULONG
) && properties
[i
].value
)
401 TRACE( " value %08x\n", *(ULONG
*)properties
[i
].value
);
403 switch (properties
[i
].id
)
405 case WS_CHANNEL_PROPERTY_ENCODING
:
406 if (!properties
[i
].value
|| properties
[i
].valueSize
!= sizeof(channel
->encoding
))
408 free_channel( channel
);
411 channel
->encoding
= *(WS_ENCODING
*)properties
[i
].value
;
415 if ((hr
= prop_set( channel
->prop
, channel
->prop_count
, properties
[i
].id
, properties
[i
].value
,
416 properties
[i
].valueSize
)) != S_OK
)
418 free_channel( channel
);
429 /**************************************************************************
430 * WsCreateChannel [webservices.@]
432 HRESULT WINAPI
WsCreateChannel( WS_CHANNEL_TYPE type
, WS_CHANNEL_BINDING binding
,
433 const WS_CHANNEL_PROPERTY
*properties
, ULONG count
,
434 const WS_SECURITY_DESCRIPTION
*desc
, WS_CHANNEL
**handle
,
437 struct channel
*channel
;
440 TRACE( "%u %u %p %u %p %p %p\n", type
, binding
, properties
, count
, desc
, handle
, error
);
441 if (error
) FIXME( "ignoring error parameter\n" );
442 if (desc
) FIXME( "ignoring security description\n" );
444 if (!handle
) return E_INVALIDARG
;
446 if (type
!= WS_CHANNEL_TYPE_REQUEST
&& type
!= WS_CHANNEL_TYPE_DUPLEX
&&
447 type
!= WS_CHANNEL_TYPE_DUPLEX_SESSION
)
449 FIXME( "channel type %u not implemented\n", type
);
452 if (binding
!= WS_HTTP_CHANNEL_BINDING
&& binding
!= WS_TCP_CHANNEL_BINDING
&&
453 binding
!= WS_UDP_CHANNEL_BINDING
)
455 FIXME( "channel binding %u not implemented\n", binding
);
459 if ((hr
= create_channel( type
, binding
, properties
, count
, &channel
)) != S_OK
) return hr
;
461 TRACE( "created %p\n", channel
);
462 *handle
= (WS_CHANNEL
*)channel
;
466 /**************************************************************************
467 * WsCreateChannelForListener [webservices.@]
469 HRESULT WINAPI
WsCreateChannelForListener( WS_LISTENER
*listener_handle
, const WS_CHANNEL_PROPERTY
*properties
,
470 ULONG count
, WS_CHANNEL
**handle
, WS_ERROR
*error
)
472 struct channel
*channel
;
473 WS_CHANNEL_TYPE type
;
474 WS_CHANNEL_BINDING binding
;
477 TRACE( "%p %p %u %p %p\n", listener_handle
, properties
, count
, handle
, error
);
478 if (error
) FIXME( "ignoring error parameter\n" );
480 if (!listener_handle
|| !handle
) return E_INVALIDARG
;
482 if ((hr
= WsGetListenerProperty( listener_handle
, WS_LISTENER_PROPERTY_CHANNEL_TYPE
, &type
,
483 sizeof(type
), NULL
)) != S_OK
) return hr
;
485 if ((hr
= WsGetListenerProperty( listener_handle
, WS_LISTENER_PROPERTY_CHANNEL_BINDING
, &binding
,
486 sizeof(binding
), NULL
)) != S_OK
) return hr
;
488 if ((hr
= create_channel( type
, binding
, properties
, count
, &channel
)) != S_OK
) return hr
;
490 TRACE( "created %p\n", channel
);
491 *handle
= (WS_CHANNEL
*)channel
;
495 /**************************************************************************
496 * WsFreeChannel [webservices.@]
498 void WINAPI
WsFreeChannel( WS_CHANNEL
*handle
)
500 struct channel
*channel
= (struct channel
*)handle
;
502 TRACE( "%p\n", handle
);
504 if (!channel
) return;
506 EnterCriticalSection( &channel
->cs
);
508 if (channel
->magic
!= CHANNEL_MAGIC
)
510 LeaveCriticalSection( &channel
->cs
);
516 LeaveCriticalSection( &channel
->cs
);
517 free_channel( channel
);
520 /**************************************************************************
521 * WsResetChannel [webservices.@]
523 HRESULT WINAPI
WsResetChannel( WS_CHANNEL
*handle
, WS_ERROR
*error
)
525 struct channel
*channel
= (struct channel
*)handle
;
528 TRACE( "%p %p\n", handle
, error
);
529 if (error
) FIXME( "ignoring error parameter\n" );
531 if (!channel
) return E_INVALIDARG
;
533 EnterCriticalSection( &channel
->cs
);
535 if (channel
->magic
!= CHANNEL_MAGIC
)
537 LeaveCriticalSection( &channel
->cs
);
541 if (channel
->state
!= WS_CHANNEL_STATE_CREATED
&& channel
->state
!= WS_CHANNEL_STATE_CLOSED
)
542 hr
= WS_E_INVALID_OPERATION
;
544 reset_channel( channel
);
546 LeaveCriticalSection( &channel
->cs
);
547 TRACE( "returning %08x\n", hr
);
551 /**************************************************************************
552 * WsGetChannelProperty [webservices.@]
554 HRESULT WINAPI
WsGetChannelProperty( WS_CHANNEL
*handle
, WS_CHANNEL_PROPERTY_ID id
, void *buf
,
555 ULONG size
, WS_ERROR
*error
)
557 struct channel
*channel
= (struct channel
*)handle
;
560 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
561 if (error
) FIXME( "ignoring error parameter\n" );
563 if (!channel
) return E_INVALIDARG
;
565 EnterCriticalSection( &channel
->cs
);
567 if (channel
->magic
!= CHANNEL_MAGIC
)
569 LeaveCriticalSection( &channel
->cs
);
575 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE
:
576 if (!buf
|| size
!= sizeof(channel
->type
)) hr
= E_INVALIDARG
;
577 else *(WS_CHANNEL_TYPE
*)buf
= channel
->type
;
580 case WS_CHANNEL_PROPERTY_ENCODING
:
581 if (!buf
|| size
!= sizeof(channel
->encoding
)) hr
= E_INVALIDARG
;
582 else *(WS_ENCODING
*)buf
= channel
->encoding
;
586 hr
= prop_get( channel
->prop
, channel
->prop_count
, id
, buf
, size
);
589 LeaveCriticalSection( &channel
->cs
);
590 TRACE( "returning %08x\n", hr
);
594 /**************************************************************************
595 * WsSetChannelProperty [webservices.@]
597 HRESULT WINAPI
WsSetChannelProperty( WS_CHANNEL
*handle
, WS_CHANNEL_PROPERTY_ID id
, const void *value
,
598 ULONG size
, WS_ERROR
*error
)
600 struct channel
*channel
= (struct channel
*)handle
;
603 TRACE( "%p %u %p %u\n", handle
, id
, value
, size
);
604 if (error
) FIXME( "ignoring error parameter\n" );
606 if (!channel
) return E_INVALIDARG
;
608 EnterCriticalSection( &channel
->cs
);
610 if (channel
->magic
!= CHANNEL_MAGIC
)
612 LeaveCriticalSection( &channel
->cs
);
616 hr
= prop_set( channel
->prop
, channel
->prop_count
, id
, value
, size
);
618 LeaveCriticalSection( &channel
->cs
);
619 TRACE( "returning %08x\n", hr
);
623 static HRESULT
open_channel( struct channel
*channel
, const WS_ENDPOINT_ADDRESS
*endpoint
)
625 if (endpoint
->headers
|| endpoint
->extensions
|| endpoint
->identity
)
627 FIXME( "headers, extensions or identity not supported\n" );
631 TRACE( "endpoint %s\n", debugstr_wn(endpoint
->url
.chars
, endpoint
->url
.length
) );
633 if (!(channel
->addr
.url
.chars
= heap_alloc( endpoint
->url
.length
* sizeof(WCHAR
) ))) return E_OUTOFMEMORY
;
634 memcpy( channel
->addr
.url
.chars
, endpoint
->url
.chars
, endpoint
->url
.length
* sizeof(WCHAR
) );
635 channel
->addr
.url
.length
= endpoint
->url
.length
;
637 channel
->state
= WS_CHANNEL_STATE_OPEN
;
641 /**************************************************************************
642 * WsOpenChannel [webservices.@]
644 HRESULT WINAPI
WsOpenChannel( WS_CHANNEL
*handle
, const WS_ENDPOINT_ADDRESS
*endpoint
,
645 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
647 struct channel
*channel
= (struct channel
*)handle
;
650 TRACE( "%p %p %p %p\n", handle
, endpoint
, ctx
, error
);
651 if (error
) FIXME( "ignoring error parameter\n" );
652 if (ctx
) FIXME( "ignoring ctx parameter\n" );
654 if (!channel
|| !endpoint
) return E_INVALIDARG
;
656 EnterCriticalSection( &channel
->cs
);
658 if (channel
->magic
!= CHANNEL_MAGIC
)
660 LeaveCriticalSection( &channel
->cs
);
664 if (channel
->state
!= WS_CHANNEL_STATE_CREATED
) hr
= WS_E_INVALID_OPERATION
;
665 else hr
= open_channel( channel
, endpoint
);
667 LeaveCriticalSection( &channel
->cs
);
668 TRACE( "returning %08x\n", hr
);
672 enum frame_record_type
674 FRAME_RECORD_TYPE_VERSION
,
675 FRAME_RECORD_TYPE_MODE
,
676 FRAME_RECORD_TYPE_VIA
,
677 FRAME_RECORD_TYPE_KNOWN_ENCODING
,
678 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING
,
679 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE
,
680 FRAME_RECORD_TYPE_SIZED_ENVELOPE
,
681 FRAME_RECORD_TYPE_END
,
682 FRAME_RECORD_TYPE_FAULT
,
683 FRAME_RECORD_TYPE_UPGRADE_REQUEST
,
684 FRAME_RECORD_TYPE_UPGRADE_RESPONSE
,
685 FRAME_RECORD_TYPE_PREAMBLE_ACK
,
686 FRAME_RECORD_TYPE_PREAMBLE_END
,
689 static HRESULT
send_byte( SOCKET socket
, BYTE byte
)
691 int count
= send( socket
, (char *)&byte
, 1, 0 );
692 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
693 if (count
!= 1) return WS_E_OTHER
;
697 static HRESULT
shutdown_session( struct channel
*channel
)
701 if (channel
->state
!= WS_CHANNEL_STATE_OPEN
||
702 (channel
->type
!= WS_CHANNEL_TYPE_OUTPUT_SESSION
&&
703 channel
->type
!= WS_CHANNEL_TYPE_DUPLEX_SESSION
) ||
704 channel
->session_state
>= SESSION_STATE_SHUTDOWN
) return WS_E_INVALID_OPERATION
;
706 switch (channel
->binding
)
708 case WS_TCP_CHANNEL_BINDING
:
709 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_END
)) != S_OK
) return hr
;
710 channel
->session_state
= SESSION_STATE_SHUTDOWN
;
714 FIXME( "unhandled binding %u\n", channel
->binding
);
719 HRESULT WINAPI
WsShutdownSessionChannel( WS_CHANNEL
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
721 struct channel
*channel
= (struct channel
*)handle
;
724 TRACE( "%p %p %p\n", handle
, ctx
, error
);
725 if (error
) FIXME( "ignoring error parameter\n" );
726 if (ctx
) FIXME( "ignoring ctx parameter\n" );
728 if (!channel
) return E_INVALIDARG
;
730 EnterCriticalSection( &channel
->cs
);
732 if (channel
->magic
!= CHANNEL_MAGIC
)
734 LeaveCriticalSection( &channel
->cs
);
738 hr
= shutdown_session( channel
);
740 LeaveCriticalSection( &channel
->cs
);
741 TRACE( "returning %08x\n", hr
);
745 static void close_channel( struct channel
*channel
)
747 reset_channel( channel
);
748 channel
->state
= WS_CHANNEL_STATE_CLOSED
;
751 /**************************************************************************
752 * WsCloseChannel [webservices.@]
754 HRESULT WINAPI
WsCloseChannel( WS_CHANNEL
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
756 struct channel
*channel
= (struct channel
*)handle
;
759 TRACE( "%p %p %p\n", handle
, ctx
, error
);
760 if (error
) FIXME( "ignoring error parameter\n" );
761 if (ctx
) FIXME( "ignoring ctx parameter\n" );
763 if (!channel
) return E_INVALIDARG
;
765 EnterCriticalSection( &channel
->cs
);
767 if (channel
->magic
!= CHANNEL_MAGIC
)
769 LeaveCriticalSection( &channel
->cs
);
773 close_channel( channel
);
775 LeaveCriticalSection( &channel
->cs
);
776 TRACE( "returning %08x\n", hr
);
780 static HRESULT
parse_http_url( const WCHAR
*url
, ULONG len
, URL_COMPONENTS
*uc
)
782 HRESULT hr
= E_OUTOFMEMORY
;
786 memset( uc
, 0, sizeof(*uc
) );
787 uc
->dwStructSize
= sizeof(*uc
);
788 uc
->dwHostNameLength
= 128;
789 uc
->lpszHostName
= heap_alloc( uc
->dwHostNameLength
* sizeof(WCHAR
) );
790 uc
->dwUrlPathLength
= 128;
791 uc
->lpszUrlPath
= heap_alloc( uc
->dwUrlPathLength
* sizeof(WCHAR
) );
792 uc
->dwExtraInfoLength
= 128;
793 uc
->lpszExtraInfo
= heap_alloc( uc
->dwExtraInfoLength
* sizeof(WCHAR
) );
794 if (!uc
->lpszHostName
|| !uc
->lpszUrlPath
|| !uc
->lpszExtraInfo
) goto error
;
796 if (!WinHttpCrackUrl( url
, len
, ICU_DECODE
, uc
))
798 if ((err
= GetLastError()) != ERROR_INSUFFICIENT_BUFFER
)
800 hr
= HRESULT_FROM_WIN32( err
);
803 if (!(tmp
= heap_realloc( uc
->lpszHostName
, uc
->dwHostNameLength
* sizeof(WCHAR
) ))) goto error
;
804 uc
->lpszHostName
= tmp
;
805 if (!(tmp
= heap_realloc( uc
->lpszUrlPath
, uc
->dwUrlPathLength
* sizeof(WCHAR
) ))) goto error
;
806 uc
->lpszUrlPath
= tmp
;
807 if (!(tmp
= heap_realloc( uc
->lpszExtraInfo
, uc
->dwExtraInfoLength
* sizeof(WCHAR
) ))) goto error
;
808 uc
->lpszExtraInfo
= tmp
;
809 WinHttpCrackUrl( url
, len
, ICU_DECODE
, uc
);
815 heap_free( uc
->lpszHostName
);
816 heap_free( uc
->lpszUrlPath
);
817 heap_free( uc
->lpszExtraInfo
);
821 static HRESULT
connect_channel_http( struct channel
*channel
)
823 static const WCHAR agentW
[] =
824 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
825 HINTERNET ses
= NULL
, con
= NULL
;
829 if (channel
->u
.http
.connect
) return S_OK
;
831 if ((hr
= parse_http_url( channel
->addr
.url
.chars
, channel
->addr
.url
.length
, &uc
)) != S_OK
) return hr
;
832 if (!(channel
->u
.http
.path
= heap_alloc( (uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
+ 1) * sizeof(WCHAR
) )))
839 strcpyW( channel
->u
.http
.path
, uc
.lpszUrlPath
);
840 if (uc
.dwExtraInfoLength
) strcatW( channel
->u
.http
.path
, uc
.lpszExtraInfo
);
843 channel
->u
.http
.flags
= WINHTTP_FLAG_REFRESH
;
846 case INTERNET_SCHEME_HTTP
: break;
847 case INTERNET_SCHEME_HTTPS
:
848 channel
->u
.http
.flags
|= WINHTTP_FLAG_SECURE
;
852 hr
= WS_E_INVALID_ENDPOINT_URL
;
856 if (!(ses
= WinHttpOpen( agentW
, 0, NULL
, NULL
, 0 )))
858 hr
= HRESULT_FROM_WIN32( GetLastError() );
861 if (!(con
= WinHttpConnect( ses
, uc
.lpszHostName
, uc
.nPort
, 0 )))
863 hr
= HRESULT_FROM_WIN32( GetLastError() );
867 channel
->u
.http
.session
= ses
;
868 channel
->u
.http
.connect
= con
;
873 WinHttpCloseHandle( con
);
874 WinHttpCloseHandle( ses
);
876 heap_free( uc
.lpszHostName
);
877 heap_free( uc
.lpszUrlPath
);
878 heap_free( uc
.lpszExtraInfo
);
882 static HRESULT
connect_channel_tcp( struct channel
*channel
)
884 struct sockaddr_storage storage
;
885 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
887 WS_URL_SCHEME_TYPE scheme
;
892 if (channel
->u
.tcp
.socket
!= -1) return S_OK
;
894 if ((hr
= parse_url( &channel
->addr
.url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
895 if (scheme
!= WS_URL_NETTCP_SCHEME_TYPE
)
898 return WS_E_INVALID_ENDPOINT_URL
;
903 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, 0 );
905 if (hr
!= S_OK
) return hr
;
907 if ((channel
->u
.tcp
.socket
= socket( addr
->sa_family
, SOCK_STREAM
, 0 )) == -1)
908 return HRESULT_FROM_WIN32( WSAGetLastError() );
910 if (connect( channel
->u
.tcp
.socket
, addr
, addr_len
) < 0)
912 closesocket( channel
->u
.tcp
.socket
);
913 channel
->u
.tcp
.socket
= -1;
914 return HRESULT_FROM_WIN32( WSAGetLastError() );
920 static HRESULT
connect_channel_udp( struct channel
*channel
)
922 struct sockaddr_storage storage
;
923 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
925 WS_URL_SCHEME_TYPE scheme
;
930 if (channel
->u
.udp
.socket
!= -1) return S_OK
;
932 if ((hr
= parse_url( &channel
->addr
.url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
933 if (scheme
!= WS_URL_SOAPUDP_SCHEME_TYPE
)
936 return WS_E_INVALID_ENDPOINT_URL
;
941 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, 0 );
943 if (hr
!= S_OK
) return hr
;
945 if ((channel
->u
.udp
.socket
= socket( addr
->sa_family
, SOCK_DGRAM
, 0 )) == -1)
946 return HRESULT_FROM_WIN32( WSAGetLastError() );
948 if (connect( channel
->u
.udp
.socket
, addr
, addr_len
) < 0)
950 closesocket( channel
->u
.udp
.socket
);
951 channel
->u
.udp
.socket
= -1;
952 return HRESULT_FROM_WIN32( WSAGetLastError() );
958 static HRESULT
connect_channel( struct channel
*channel
)
960 switch (channel
->binding
)
962 case WS_HTTP_CHANNEL_BINDING
:
963 return connect_channel_http( channel
);
965 case WS_TCP_CHANNEL_BINDING
:
966 return connect_channel_tcp( channel
);
968 case WS_UDP_CHANNEL_BINDING
:
969 return connect_channel_udp( channel
);
972 ERR( "unhandled binding %u\n", channel
->binding
);
977 static HRESULT
send_message_http( HINTERNET request
, BYTE
*data
, ULONG len
)
979 if (!WinHttpSendRequest( request
, NULL
, 0, data
, len
, len
, 0 ))
980 return HRESULT_FROM_WIN32( GetLastError() );
982 if (!WinHttpReceiveResponse( request
, NULL
))
983 return HRESULT_FROM_WIN32( GetLastError() );
987 static HRESULT
send_bytes( SOCKET socket
, BYTE
*bytes
, int len
)
989 int count
= send( socket
, (char *)bytes
, len
, 0 );
990 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
991 if (count
!= len
) return WS_E_OTHER
;
995 static HRESULT
send_size( SOCKET socket
, ULONG size
)
998 if (size
< 0x80) return send_byte( socket
, size
);
999 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
1000 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
1001 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
1002 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
1003 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
1004 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
1005 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
1006 if ((size
>>= 7) < 0x08) return send_byte( socket
, size
);
1007 return E_INVALIDARG
;
1010 static inline ULONG
size_length( ULONG size
)
1012 if (size
< 0x80) return 1;
1013 if (size
< 0x4000) return 2;
1014 if (size
< 0x200000) return 3;
1015 if (size
< 0x10000000) return 4;
1019 static ULONG
string_table_size( const struct dictionary
*dict
)
1022 for (i
= 0; i
< dict
->dict
.stringCount
; i
++)
1024 if (dict
->sequence
[i
] == dict
->current_sequence
)
1025 size
+= size_length( dict
->dict
.strings
[i
].length
) + dict
->dict
.strings
[i
].length
;
1030 static HRESULT
send_string_table( SOCKET socket
, const struct dictionary
*dict
)
1034 for (i
= 0; i
< dict
->dict
.stringCount
; i
++)
1036 if (dict
->sequence
[i
] != dict
->current_sequence
) continue;
1037 if ((hr
= send_size( socket
, dict
->dict
.strings
[i
].length
)) != S_OK
) return hr
;
1038 if ((hr
= send_bytes( socket
, dict
->dict
.strings
[i
].bytes
, dict
->dict
.strings
[i
].length
)) != S_OK
) return hr
;
1043 static HRESULT
string_to_utf8( const WS_STRING
*str
, unsigned char **ret
, int *len
)
1045 *len
= WideCharToMultiByte( CP_UTF8
, 0, str
->chars
, str
->length
, NULL
, 0, NULL
, NULL
);
1046 if (!(*ret
= heap_alloc( *len
))) return E_OUTOFMEMORY
;
1047 WideCharToMultiByte( CP_UTF8
, 0, str
->chars
, str
->length
, (char *)*ret
, *len
, NULL
, NULL
);
1053 SESSION_MODE_INVALID
= 0,
1054 SESSION_MODE_SINGLETON
= 1,
1055 SESSION_MODE_DUPLEX
= 2,
1056 SESSION_MODE_SIMPLEX
= 3,
1059 static enum session_mode
map_channel_type( struct channel
*channel
)
1061 switch (channel
->type
)
1063 case WS_CHANNEL_TYPE_DUPLEX_SESSION
: return SESSION_MODE_DUPLEX
;
1065 FIXME( "unhandled channel type %08x\n", channel
->type
);
1066 return SESSION_MODE_INVALID
;
1072 KNOWN_ENCODING_SOAP11_UTF8
= 0x00,
1073 KNOWN_ENCODING_SOAP11_UTF16
= 0x01,
1074 KNOWN_ENCODING_SOAP11_UTF16LE
= 0x02,
1075 KNOWN_ENCODING_SOAP12_UTF8
= 0x03,
1076 KNOWN_ENCODING_SOAP12_UTF16
= 0x04,
1077 KNOWN_ENCODING_SOAP12_UTF16LE
= 0x05,
1078 KNOWN_ENCODING_SOAP12_MTOM
= 0x06,
1079 KNOWN_ENCODING_SOAP12_BINARY
= 0x07,
1080 KNOWN_ENCODING_SOAP12_BINARY_SESSION
= 0x08,
1083 static enum known_encoding
map_channel_encoding( struct channel
*channel
)
1085 WS_ENVELOPE_VERSION version
;
1087 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION
, &version
, sizeof(version
) );
1091 case WS_ENVELOPE_VERSION_SOAP_1_1
:
1092 switch (channel
->encoding
)
1094 case WS_ENCODING_XML_UTF8
: return KNOWN_ENCODING_SOAP11_UTF8
;
1095 case WS_ENCODING_XML_UTF16LE
: return KNOWN_ENCODING_SOAP11_UTF16LE
;
1097 FIXME( "unhandled version/encoding %u/%u\n", version
, channel
->encoding
);
1100 case WS_ENVELOPE_VERSION_SOAP_1_2
:
1101 switch (channel
->encoding
)
1103 case WS_ENCODING_XML_UTF8
: return KNOWN_ENCODING_SOAP12_UTF8
;
1104 case WS_ENCODING_XML_UTF16LE
: return KNOWN_ENCODING_SOAP12_UTF16LE
;
1105 case WS_ENCODING_XML_BINARY_1
: return KNOWN_ENCODING_SOAP12_BINARY
;
1106 case WS_ENCODING_XML_BINARY_SESSION_1
: return KNOWN_ENCODING_SOAP12_BINARY_SESSION
;
1108 FIXME( "unhandled version/encoding %u/%u\n", version
, channel
->encoding
);
1112 ERR( "unhandled version %u\n", version
);
1117 #define FRAME_VERSION_MAJOR 1
1118 #define FRAME_VERSION_MINOR 1
1120 static HRESULT
send_preamble( struct channel
*channel
)
1126 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_VERSION
)) != S_OK
) return hr
;
1127 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_VERSION_MAJOR
)) != S_OK
) return hr
;
1128 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_VERSION_MINOR
)) != S_OK
) return hr
;
1130 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_MODE
)) != S_OK
) return hr
;
1131 if ((hr
= send_byte( channel
->u
.tcp
.socket
, map_channel_type(channel
) )) != S_OK
) return hr
;
1133 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_VIA
)) != S_OK
) return hr
;
1134 if ((hr
= string_to_utf8( &channel
->addr
.url
, &url
, &len
)) != S_OK
) return hr
;
1135 if ((hr
= send_size( channel
->u
.tcp
.socket
, len
)) != S_OK
) goto done
;
1136 if ((hr
= send_bytes( channel
->u
.tcp
.socket
, url
, len
)) != S_OK
) goto done
;
1138 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_KNOWN_ENCODING
)) != S_OK
) goto done
;
1139 if ((hr
= send_byte( channel
->u
.tcp
.socket
, map_channel_encoding(channel
) )) != S_OK
) goto done
;
1140 hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_PREAMBLE_END
);
1147 static HRESULT
receive_bytes( struct channel
*channel
, unsigned char *bytes
, int len
)
1149 int count
= recv( channel
->u
.tcp
.socket
, (char *)bytes
, len
, 0 );
1150 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1151 if (count
!= len
) return WS_E_INVALID_FORMAT
;
1155 static HRESULT
receive_preamble_ack( struct channel
*channel
)
1160 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1161 if (byte
!= FRAME_RECORD_TYPE_PREAMBLE_ACK
) return WS_E_INVALID_FORMAT
;
1162 channel
->session_state
= SESSION_STATE_SETUP_COMPLETE
;
1166 static HRESULT
send_sized_envelope( struct channel
*channel
, BYTE
*data
, ULONG len
)
1168 ULONG table_size
= string_table_size( &channel
->dict_send
);
1171 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_SIZED_ENVELOPE
)) != S_OK
) return hr
;
1172 if ((hr
= send_size( channel
->u
.tcp
.socket
, size_length(table_size
) + table_size
+ len
)) != S_OK
) return hr
;
1173 if ((hr
= send_size( channel
->u
.tcp
.socket
, table_size
)) != S_OK
) return hr
;
1174 if ((hr
= send_string_table( channel
->u
.tcp
.socket
, &channel
->dict_send
)) != S_OK
) return hr
;
1175 return send_bytes( channel
->u
.tcp
.socket
, data
, len
);
1178 static HRESULT
open_http_request( struct channel
*channel
, HINTERNET
*req
)
1180 static const WCHAR postW
[] = {'P','O','S','T',0};
1181 if ((*req
= WinHttpOpenRequest( channel
->u
.http
.connect
, postW
, channel
->u
.http
.path
,
1182 NULL
, NULL
, NULL
, channel
->u
.http
.flags
))) return S_OK
;
1183 return HRESULT_FROM_WIN32( GetLastError() );
1186 static HRESULT
send_message( struct channel
*channel
, WS_MESSAGE
*msg
)
1188 WS_XML_WRITER
*writer
;
1193 WsGetMessageProperty( channel
->msg
, WS_MESSAGE_PROPERTY_BODY_WRITER
, &writer
, sizeof(writer
), NULL
);
1194 WsGetWriterProperty( writer
, WS_XML_WRITER_PROPERTY_BYTES
, &buf
, sizeof(buf
), NULL
);
1196 switch (channel
->binding
)
1198 case WS_HTTP_CHANNEL_BINDING
:
1199 if (channel
->u
.http
.request
)
1201 WinHttpCloseHandle( channel
->u
.http
.request
);
1202 channel
->u
.http
.request
= NULL
;
1204 if ((hr
= open_http_request( channel
, &channel
->u
.http
.request
)) != S_OK
) return hr
;
1205 if ((hr
= message_insert_http_headers( msg
, channel
->u
.http
.request
)) != S_OK
) return hr
;
1206 return send_message_http( channel
->u
.http
.request
, buf
.bytes
, buf
.length
);
1208 case WS_TCP_CHANNEL_BINDING
:
1209 if (channel
->type
& WS_CHANNEL_TYPE_SESSION
)
1211 switch (channel
->session_state
)
1213 case SESSION_STATE_UNINITIALIZED
:
1214 if ((hr
= send_preamble( channel
)) != S_OK
) return hr
;
1215 if ((hr
= receive_preamble_ack( channel
)) != S_OK
) return hr
;
1218 case SESSION_STATE_SETUP_COMPLETE
:
1219 return send_sized_envelope( channel
, buf
.bytes
, buf
.length
);
1222 ERR( "unhandled session state %u\n", channel
->session_state
);
1228 case WS_UDP_CHANNEL_BINDING
:
1229 return WsFlushWriter( writer
, 0, NULL
, NULL
);
1232 ERR( "unhandled binding %u\n", channel
->binding
);
1237 HRESULT
channel_send_message( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
)
1239 struct channel
*channel
= (struct channel
*)handle
;
1242 EnterCriticalSection( &channel
->cs
);
1244 if (channel
->magic
!= CHANNEL_MAGIC
)
1246 LeaveCriticalSection( &channel
->cs
);
1247 return E_INVALIDARG
;
1250 if ((hr
= connect_channel( channel
)) == S_OK
) hr
= send_message( channel
, msg
);
1252 LeaveCriticalSection( &channel
->cs
);
1256 static HRESULT CALLBACK
dict_cb( void *state
, const WS_XML_STRING
*str
, BOOL
*found
, ULONG
*id
, WS_ERROR
*error
)
1258 struct dictionary
*dict
= state
;
1263 if ((index
= find_string( dict
, str
->bytes
, str
->length
, id
)) == -1)
1269 if (!(bytes
= heap_alloc( str
->length
))) return E_OUTOFMEMORY
;
1270 memcpy( bytes
, str
->bytes
, str
->length
);
1271 if ((hr
= insert_string( dict
, bytes
, str
->length
, index
, id
)) == S_OK
)
1282 static CALLBACK HRESULT
write_callback( void *state
, const WS_BYTES
*buf
, ULONG count
,
1283 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1285 SOCKET socket
= *(SOCKET
*)state
;
1286 if (send( socket
, (const char *)buf
->bytes
, buf
->length
, 0 ) < 0)
1288 TRACE( "send failed %u\n", WSAGetLastError() );
1293 static HRESULT
init_writer( struct channel
*channel
)
1295 WS_XML_WRITER_BUFFER_OUTPUT buf
= {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER
}};
1296 WS_XML_WRITER_STREAM_OUTPUT stream
= {{WS_XML_WRITER_OUTPUT_TYPE_STREAM
}};
1297 WS_XML_WRITER_TEXT_ENCODING text
= {{WS_XML_WRITER_ENCODING_TYPE_TEXT
}, WS_CHARSET_UTF8
};
1298 WS_XML_WRITER_BINARY_ENCODING bin
= {{WS_XML_WRITER_ENCODING_TYPE_BINARY
}};
1299 const WS_XML_WRITER_ENCODING
*encoding
;
1300 const WS_XML_WRITER_OUTPUT
*output
;
1301 WS_XML_WRITER_PROPERTY prop
;
1302 ULONG max_size
= (1 << 17);
1305 prop
.id
= WS_XML_WRITER_PROPERTY_BUFFER_MAX_SIZE
;
1306 prop
.value
= &max_size
;
1307 prop
.valueSize
= sizeof(max_size
);
1308 if (!channel
->writer
&& (hr
= WsCreateWriter( &prop
, 1, &channel
->writer
, NULL
)) != S_OK
) return hr
;
1310 switch (channel
->encoding
)
1312 case WS_ENCODING_XML_UTF8
:
1313 encoding
= &text
.encoding
;
1314 if (channel
->binding
== WS_UDP_CHANNEL_BINDING
||
1315 (channel
->binding
== WS_TCP_CHANNEL_BINDING
&& !(channel
->type
& WS_CHANNEL_TYPE_SESSION
)))
1317 stream
.writeCallback
= write_callback
;
1318 stream
.writeCallbackState
= (channel
->binding
== WS_UDP_CHANNEL_BINDING
) ?
1319 &channel
->u
.udp
.socket
: &channel
->u
.tcp
.socket
;
1320 output
= &stream
.output
;
1322 else output
= &buf
.output
;
1325 case WS_ENCODING_XML_BINARY_SESSION_1
:
1326 bin
.staticDictionary
= (WS_XML_DICTIONARY
*)&dict_builtin_static
.dict
;
1329 case WS_ENCODING_XML_BINARY_1
:
1330 encoding
= &bin
.encoding
;
1331 output
= &buf
.output
;
1335 FIXME( "unhandled encoding %u\n", channel
->encoding
);
1336 return WS_E_NOT_SUPPORTED
;
1339 return WsSetOutput( channel
->writer
, encoding
, output
, NULL
, 0, NULL
);
1342 static HRESULT
write_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_ELEMENT_DESCRIPTION
*desc
,
1343 WS_WRITE_OPTION option
, const void *body
, ULONG size
)
1346 if ((hr
= writer_set_lookup( channel
->writer
, TRUE
)) != S_OK
) return hr
;
1347 if ((hr
= WsWriteEnvelopeStart( msg
, channel
->writer
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
1348 if ((hr
= writer_set_lookup( channel
->writer
, FALSE
)) != S_OK
) return hr
;
1349 channel
->dict_send
.current_sequence
++;
1350 if ((hr
= writer_set_dict_callback( channel
->writer
, dict_cb
, &channel
->dict_send
)) != S_OK
) return hr
;
1351 if ((hr
= WsWriteBody( msg
, desc
, option
, body
, size
, NULL
)) != S_OK
) return hr
;
1352 return WsWriteEnvelopeEnd( msg
, NULL
);
1355 /**************************************************************************
1356 * WsSendMessage [webservices.@]
1358 HRESULT WINAPI
WsSendMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
*desc
,
1359 WS_WRITE_OPTION option
, const void *body
, ULONG size
, const WS_ASYNC_CONTEXT
*ctx
,
1362 struct channel
*channel
= (struct channel
*)handle
;
1365 TRACE( "%p %p %p %08x %p %u %p %p\n", handle
, msg
, desc
, option
, body
, size
, ctx
, error
);
1366 if (error
) FIXME( "ignoring error parameter\n" );
1367 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1369 if (!channel
|| !msg
|| !desc
) 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_REQUEST_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
;
1383 if ((hr
= connect_channel( channel
)) != S_OK
) goto done
;
1384 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
1385 if ((hr
= write_message( channel
, msg
, desc
->bodyElementDescription
, option
, body
, size
)) != S_OK
) goto done
;
1386 hr
= send_message( channel
, msg
);
1389 LeaveCriticalSection( &channel
->cs
);
1390 TRACE( "returning %08x\n", hr
);
1394 /**************************************************************************
1395 * WsSendReplyMessage [webservices.@]
1397 HRESULT WINAPI
WsSendReplyMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
*desc
,
1398 WS_WRITE_OPTION option
, const void *body
, ULONG size
, WS_MESSAGE
*request
,
1399 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1401 struct channel
*channel
= (struct channel
*)handle
;
1405 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle
, msg
, desc
, option
, body
, size
, request
, ctx
, error
);
1406 if (error
) FIXME( "ignoring error parameter\n" );
1407 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1409 if (!channel
|| !msg
|| !desc
|| !request
) return E_INVALIDARG
;
1411 EnterCriticalSection( &channel
->cs
);
1413 if (channel
->magic
!= CHANNEL_MAGIC
)
1415 LeaveCriticalSection( &channel
->cs
);
1416 return E_INVALIDARG
;
1419 if ((hr
= WsInitializeMessage( msg
, WS_REPLY_MESSAGE
, NULL
, NULL
)) != S_OK
) goto done
;
1420 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
1421 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) goto done
;
1422 if ((hr
= message_get_id( request
, &req_id
)) != S_OK
) goto done
;
1423 if ((hr
= message_set_request_id( msg
, &req_id
)) != S_OK
) goto done
;
1425 if ((hr
= connect_channel( channel
)) != S_OK
) goto done
;
1426 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
1427 if ((hr
= write_message( channel
, msg
, desc
->bodyElementDescription
, option
, body
, size
)) != S_OK
) goto done
;
1428 hr
= send_message( channel
, msg
);
1431 LeaveCriticalSection( &channel
->cs
);
1432 TRACE( "returning %08x\n", hr
);
1436 static HRESULT
resize_read_buffer( struct channel
*channel
, ULONG size
)
1438 if (!channel
->read_buf
)
1440 if (!(channel
->read_buf
= heap_alloc( size
))) return E_OUTOFMEMORY
;
1441 channel
->read_buflen
= size
;
1444 if (channel
->read_buflen
< size
)
1447 ULONG new_size
= max( size
, channel
->read_buflen
* 2 );
1448 if (!(tmp
= heap_realloc( channel
->read_buf
, new_size
))) return E_OUTOFMEMORY
;
1449 channel
->read_buf
= tmp
;
1450 channel
->read_buflen
= new_size
;
1455 static CALLBACK HRESULT
read_callback( void *state
, void *buf
, ULONG buflen
, ULONG
*retlen
,
1456 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1458 SOCKET socket
= *(SOCKET
*)state
;
1461 if ((ret
= recv( socket
, buf
, buflen
, 0 )) >= 0) *retlen
= ret
;
1464 TRACE( "recv failed %u\n", WSAGetLastError() );
1470 static HRESULT
init_reader( struct channel
*channel
)
1472 WS_XML_READER_BUFFER_INPUT buf
= {{WS_XML_READER_INPUT_TYPE_BUFFER
}};
1473 WS_XML_READER_STREAM_INPUT stream
= {{WS_XML_READER_INPUT_TYPE_STREAM
}};
1474 WS_XML_READER_TEXT_ENCODING text
= {{WS_XML_READER_ENCODING_TYPE_TEXT
}};
1475 WS_XML_READER_BINARY_ENCODING bin
= {{WS_XML_READER_ENCODING_TYPE_BINARY
}};
1476 const WS_XML_READER_ENCODING
*encoding
;
1477 const WS_XML_READER_INPUT
*input
;
1480 if (!channel
->reader
&& (hr
= WsCreateReader( NULL
, 0, &channel
->reader
, NULL
)) != S_OK
) return hr
;
1482 switch (channel
->encoding
)
1484 case WS_ENCODING_XML_UTF8
:
1485 text
.charSet
= WS_CHARSET_UTF8
;
1486 encoding
= &text
.encoding
;
1488 if (channel
->binding
== WS_UDP_CHANNEL_BINDING
||
1489 (channel
->binding
== WS_TCP_CHANNEL_BINDING
&& !(channel
->type
& WS_CHANNEL_TYPE_SESSION
)))
1491 stream
.readCallback
= read_callback
;
1492 stream
.readCallbackState
= (channel
->binding
== WS_UDP_CHANNEL_BINDING
) ?
1493 &channel
->u
.udp
.socket
: &channel
->u
.tcp
.socket
;
1494 input
= &stream
.input
;
1498 buf
.encodedData
= channel
->read_buf
;
1499 buf
.encodedDataSize
= channel
->read_size
;
1504 case WS_ENCODING_XML_BINARY_SESSION_1
:
1505 bin
.staticDictionary
= (WS_XML_DICTIONARY
*)&dict_builtin_static
.dict
;
1506 bin
.dynamicDictionary
= &channel
->dict_recv
.dict
;
1509 case WS_ENCODING_XML_BINARY_1
:
1510 encoding
= &bin
.encoding
;
1512 buf
.encodedData
= channel
->read_buf
;
1513 buf
.encodedDataSize
= channel
->read_size
;
1518 FIXME( "unhandled encoding %u\n", channel
->encoding
);
1519 return WS_E_NOT_SUPPORTED
;
1522 return WsSetInput( channel
->reader
, encoding
, input
, NULL
, 0, NULL
);
1525 #define INITIAL_READ_BUFFER_SIZE 4096
1526 static HRESULT
receive_message_http( struct channel
*channel
)
1528 DWORD len
, bytes_read
, offset
= 0, size
= INITIAL_READ_BUFFER_SIZE
;
1532 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE
,
1533 &max_len
, sizeof(max_len
) );
1535 if ((hr
= resize_read_buffer( channel
, size
)) != S_OK
) return hr
;
1536 channel
->read_size
= 0;
1539 if (!WinHttpQueryDataAvailable( channel
->u
.http
.request
, &len
))
1541 return HRESULT_FROM_WIN32( GetLastError() );
1544 if (channel
->read_size
+ len
> max_len
) return WS_E_QUOTA_EXCEEDED
;
1545 if ((hr
= resize_read_buffer( channel
, channel
->read_size
+ len
)) != S_OK
) return hr
;
1547 if (!WinHttpReadData( channel
->u
.http
.request
, channel
->read_buf
+ offset
, len
, &bytes_read
))
1549 return HRESULT_FROM_WIN32( GetLastError() );
1551 if (!bytes_read
) break;
1552 channel
->read_size
+= bytes_read
;
1553 offset
+= bytes_read
;
1559 static HRESULT
receive_message_sized( struct channel
*channel
, unsigned int size
)
1561 unsigned int offset
= 0, to_read
= size
;
1565 if ((hr
= resize_read_buffer( channel
, size
)) != S_OK
) return hr
;
1567 channel
->read_size
= 0;
1568 while (channel
->read_size
< size
)
1570 if ((bytes_read
= recv( channel
->u
.tcp
.socket
, channel
->read_buf
+ offset
, to_read
, 0 )) < 0)
1572 return HRESULT_FROM_WIN32( WSAGetLastError() );
1574 if (!bytes_read
) break;
1575 channel
->read_size
+= bytes_read
;
1576 to_read
-= bytes_read
;
1577 offset
+= bytes_read
;
1579 if (channel
->read_size
!= size
) return WS_E_INVALID_FORMAT
;
1583 static HRESULT
receive_size( struct channel
*channel
, unsigned int *size
)
1588 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1589 *size
= byte
& 0x7f;
1590 if (!(byte
& 0x80)) return S_OK
;
1592 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1593 *size
+= (byte
& 0x7f) << 7;
1594 if (!(byte
& 0x80)) return S_OK
;
1596 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1597 *size
+= (byte
& 0x7f) << 14;
1598 if (!(byte
& 0x80)) return S_OK
;
1600 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1601 *size
+= (byte
& 0x7f) << 21;
1602 if (!(byte
& 0x80)) return S_OK
;
1604 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1605 if (byte
& ~0x0f) return WS_E_INVALID_FORMAT
;
1606 *size
+= byte
<< 28;
1610 static WS_ENCODING
map_known_encoding( enum known_encoding encoding
)
1614 case KNOWN_ENCODING_SOAP11_UTF8
:
1615 case KNOWN_ENCODING_SOAP12_UTF8
: return WS_ENCODING_XML_UTF8
;
1616 case KNOWN_ENCODING_SOAP11_UTF16
:
1617 case KNOWN_ENCODING_SOAP12_UTF16
: return WS_ENCODING_XML_UTF16BE
;
1618 case KNOWN_ENCODING_SOAP11_UTF16LE
:
1619 case KNOWN_ENCODING_SOAP12_UTF16LE
: return WS_ENCODING_XML_UTF16LE
;
1620 case KNOWN_ENCODING_SOAP12_BINARY
: return WS_ENCODING_XML_BINARY_1
;
1621 case KNOWN_ENCODING_SOAP12_BINARY_SESSION
: return WS_ENCODING_XML_BINARY_SESSION_1
;
1623 WARN( "unhandled encoding %u, assuming UTF8\n", encoding
);
1624 return WS_ENCODING_XML_UTF8
;
1628 static HRESULT
receive_preamble( struct channel
*channel
)
1635 if ((hr
= receive_bytes( channel
, &type
, 1 )) != S_OK
) return hr
;
1636 if (type
== FRAME_RECORD_TYPE_PREAMBLE_END
) break;
1639 case FRAME_RECORD_TYPE_VERSION
:
1641 unsigned char major
, minor
;
1642 if ((hr
= receive_bytes( channel
, &major
, 1 )) != S_OK
) return hr
;
1643 if ((hr
= receive_bytes( channel
, &minor
, 1 )) != S_OK
) return hr
;
1644 TRACE( "major %u minor %u\n", major
, major
);
1647 case FRAME_RECORD_TYPE_MODE
:
1650 if ((hr
= receive_bytes( channel
, &mode
, 1 )) != S_OK
) return hr
;
1651 TRACE( "mode %u\n", mode
);
1654 case FRAME_RECORD_TYPE_VIA
:
1659 if ((hr
= receive_size( channel
, &size
)) != S_OK
) return hr
;
1660 if (!(url
= heap_alloc( size
))) return E_OUTOFMEMORY
;
1661 if ((hr
= receive_bytes( channel
, url
, size
)) != S_OK
)
1666 TRACE( "transport URL %s\n", debugstr_an((char *)url
, size
) );
1667 heap_free( url
); /* FIXME: verify */
1670 case FRAME_RECORD_TYPE_KNOWN_ENCODING
:
1672 unsigned char encoding
;
1673 if ((hr
= receive_bytes( channel
, &encoding
, 1 )) != S_OK
) return hr
;
1674 TRACE( "encoding %u\n", encoding
);
1675 channel
->encoding
= map_known_encoding( encoding
);
1679 WARN( "unhandled record type %u\n", type
);
1680 return WS_E_INVALID_FORMAT
;
1687 static HRESULT
receive_sized_envelope( struct channel
*channel
)
1693 if ((hr
= receive_bytes( channel
, &type
, 1 )) != S_OK
) return hr
;
1694 if (type
!= FRAME_RECORD_TYPE_SIZED_ENVELOPE
) return WS_E_INVALID_FORMAT
;
1695 if ((hr
= receive_size( channel
, &size
)) != S_OK
) return hr
;
1696 if ((hr
= receive_message_sized( channel
, size
)) != S_OK
) return hr
;
1700 static HRESULT
read_size( const BYTE
**ptr
, ULONG len
, ULONG
*size
)
1702 const BYTE
*buf
= *ptr
;
1704 if (len
< 1) return WS_E_INVALID_FORMAT
;
1705 *size
= buf
[0] & 0x7f;
1706 if (!(buf
[0] & 0x80))
1711 if (len
< 2) return WS_E_INVALID_FORMAT
;
1712 *size
+= (buf
[1] & 0x7f) << 7;
1713 if (!(buf
[1] & 0x80))
1718 if (len
< 3) return WS_E_INVALID_FORMAT
;
1719 *size
+= (buf
[2] & 0x7f) << 14;
1720 if (!(buf
[2] & 0x80))
1725 if (len
< 4) return WS_E_INVALID_FORMAT
;
1726 *size
+= (buf
[3] & 0x7f) << 21;
1727 if (!(buf
[3] & 0x80))
1732 if (len
< 5 || (buf
[4] & ~0x07)) return WS_E_INVALID_FORMAT
;
1733 *size
+= buf
[4] << 28;
1738 static HRESULT
build_dict( const BYTE
*buf
, ULONG buflen
, struct dictionary
*dict
, ULONG
*used
)
1740 ULONG size
, strings_size
, strings_offset
;
1741 const BYTE
*ptr
= buf
;
1746 if ((hr
= read_size( &ptr
, buflen
, &strings_size
)) != S_OK
) return hr
;
1747 strings_offset
= ptr
- buf
;
1748 if (buflen
< strings_offset
+ strings_size
) return WS_E_INVALID_FORMAT
;
1749 *used
= strings_offset
+ strings_size
;
1750 if (!strings_size
) return S_OK
;
1752 UuidCreate( &dict
->dict
.guid
);
1753 dict
->dict
.isConst
= FALSE
;
1755 buflen
-= strings_offset
;
1756 ptr
= buf
+ strings_offset
;
1757 while (ptr
< buf
+ strings_size
)
1759 if ((hr
= read_size( &ptr
, buflen
, &size
)) != S_OK
)
1767 return WS_E_INVALID_FORMAT
;
1770 if (!(bytes
= heap_alloc( size
)))
1775 memcpy( bytes
, ptr
, size
);
1776 if ((index
= find_string( dict
, bytes
, size
, NULL
)) == -1) /* duplicate */
1782 if ((hr
= insert_string( dict
, bytes
, size
, index
, NULL
)) != S_OK
)
1797 static HRESULT
send_preamble_ack( struct channel
*channel
)
1800 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_PREAMBLE_ACK
)) != S_OK
) return hr
;
1801 channel
->session_state
= SESSION_STATE_SETUP_COMPLETE
;
1805 static HRESULT
receive_message_session( struct channel
*channel
)
1809 if ((hr
= receive_sized_envelope( channel
)) != S_OK
) return hr
;
1810 if (channel
->encoding
== WS_ENCODING_XML_BINARY_SESSION_1
)
1813 if ((hr
= build_dict( (const BYTE
*)channel
->read_buf
, channel
->read_size
, &channel
->dict_recv
,
1814 &size
)) != S_OK
) return hr
;
1815 channel
->read_size
-= size
;
1816 memmove( channel
->read_buf
, channel
->read_buf
+ size
, channel
->read_size
);
1822 static HRESULT
receive_message_bytes( struct channel
*channel
)
1825 if ((hr
= connect_channel( channel
)) != S_OK
) return hr
;
1827 switch (channel
->binding
)
1829 case WS_HTTP_CHANNEL_BINDING
:
1830 return receive_message_http( channel
);
1832 case WS_TCP_CHANNEL_BINDING
:
1833 if (channel
->type
& WS_CHANNEL_TYPE_SESSION
)
1835 switch (channel
->session_state
)
1837 case SESSION_STATE_UNINITIALIZED
:
1838 if ((hr
= receive_preamble( channel
)) != S_OK
) return hr
;
1839 if ((hr
= send_preamble_ack( channel
)) != S_OK
) return hr
;
1842 case SESSION_STATE_SETUP_COMPLETE
:
1843 return receive_message_session( channel
);
1846 ERR( "unhandled session state %u\n", channel
->session_state
);
1850 return S_OK
; /* nothing to do, data is read through stream callback */
1852 case WS_UDP_CHANNEL_BINDING
:
1856 ERR( "unhandled binding %u\n", channel
->binding
);
1861 HRESULT
channel_receive_message( WS_CHANNEL
*handle
)
1863 struct channel
*channel
= (struct channel
*)handle
;
1866 EnterCriticalSection( &channel
->cs
);
1868 if (channel
->magic
!= CHANNEL_MAGIC
)
1870 LeaveCriticalSection( &channel
->cs
);
1871 return E_INVALIDARG
;
1874 if ((hr
= receive_message_bytes( channel
)) == S_OK
) hr
= init_reader( channel
);
1876 LeaveCriticalSection( &channel
->cs
);
1880 HRESULT
channel_get_reader( WS_CHANNEL
*handle
, WS_XML_READER
**reader
)
1882 struct channel
*channel
= (struct channel
*)handle
;
1884 EnterCriticalSection( &channel
->cs
);
1886 if (channel
->magic
!= CHANNEL_MAGIC
)
1888 LeaveCriticalSection( &channel
->cs
);
1889 return E_INVALIDARG
;
1892 *reader
= channel
->reader
;
1894 LeaveCriticalSection( &channel
->cs
);
1898 static HRESULT
read_message( WS_MESSAGE
*handle
, WS_XML_READER
*reader
, const WS_ELEMENT_DESCRIPTION
*desc
,
1899 WS_READ_OPTION option
, WS_HEAP
*heap
, void *body
, ULONG size
)
1902 if ((hr
= WsReadEnvelopeStart( handle
, reader
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
1903 if ((hr
= WsReadBody( handle
, desc
, option
, heap
, body
, size
, NULL
)) != S_OK
) return hr
;
1904 return WsReadEnvelopeEnd( handle
, NULL
);
1907 static HRESULT
receive_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1908 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
, WS_HEAP
*heap
,
1909 void *value
, ULONG size
, ULONG
*index
)
1914 if ((hr
= receive_message_bytes( channel
)) != S_OK
) return hr
;
1915 if ((hr
= init_reader( channel
)) != S_OK
) return hr
;
1917 for (i
= 0; i
< count
; i
++)
1919 const WS_ELEMENT_DESCRIPTION
*body
= desc
[i
]->bodyElementDescription
;
1920 if ((hr
= read_message( msg
, channel
->reader
, body
, read_option
, heap
, value
, size
)) == S_OK
)
1922 if (index
) *index
= i
;
1925 if ((hr
= WsResetMessage( msg
, NULL
)) != S_OK
) return hr
;
1926 if ((hr
= init_reader( channel
)) != S_OK
) return hr
;
1928 return (i
== count
) ? WS_E_INVALID_FORMAT
: S_OK
;
1931 struct receive_message
1934 struct channel
*channel
;
1936 const WS_MESSAGE_DESCRIPTION
**desc
;
1938 WS_RECEIVE_OPTION option
;
1939 WS_READ_OPTION read_option
;
1944 WS_ASYNC_CONTEXT ctx
;
1947 static void receive_message_proc( struct task
*task
)
1949 struct receive_message
*r
= (struct receive_message
*)task
;
1952 hr
= receive_message( r
->channel
, r
->msg
, r
->desc
, r
->count
, r
->option
, r
->read_option
, r
->heap
, r
->value
,
1953 r
->size
, r
->index
);
1955 TRACE( "calling %p(%08x)\n", r
->ctx
.callback
, hr
);
1956 r
->ctx
.callback( hr
, WS_LONG_CALLBACK
, r
->ctx
.callbackState
);
1957 TRACE( "%p returned\n", r
->ctx
.callback
);
1960 static HRESULT
queue_receive_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1961 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
,
1962 WS_HEAP
*heap
, void *value
, ULONG size
, ULONG
*index
,
1963 const WS_ASYNC_CONTEXT
*ctx
)
1965 struct receive_message
*r
;
1967 if (!(r
= heap_alloc( sizeof(*r
) ))) return E_OUTOFMEMORY
;
1968 r
->task
.proc
= receive_message_proc
;
1969 r
->channel
= channel
;
1974 r
->read_option
= read_option
;
1980 return queue_task( &channel
->recv_q
, &r
->task
);
1983 /**************************************************************************
1984 * WsReceiveMessage [webservices.@]
1986 HRESULT WINAPI
WsReceiveMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
1987 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
, WS_HEAP
*heap
,
1988 void *value
, ULONG size
, ULONG
*index
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1990 struct channel
*channel
= (struct channel
*)handle
;
1993 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle
, msg
, desc
, count
, option
, read_option
, heap
,
1994 value
, size
, index
, ctx
, error
);
1995 if (error
) FIXME( "ignoring error parameter\n" );
1997 if (!channel
|| !msg
|| !desc
|| !count
) return E_INVALIDARG
;
1999 EnterCriticalSection( &channel
->cs
);
2001 if (channel
->magic
!= CHANNEL_MAGIC
)
2003 LeaveCriticalSection( &channel
->cs
);
2004 return E_INVALIDARG
;
2008 hr
= queue_receive_message( channel
, msg
, desc
, count
, option
, read_option
, heap
, value
, size
, index
, ctx
);
2010 hr
= receive_message( channel
, msg
, desc
, count
, option
, read_option
, heap
, value
, size
, index
);
2012 LeaveCriticalSection( &channel
->cs
);
2013 TRACE( "returning %08x\n", hr
);
2017 static HRESULT
request_reply( struct channel
*channel
, WS_MESSAGE
*request
,
2018 const WS_MESSAGE_DESCRIPTION
*request_desc
, WS_WRITE_OPTION write_option
,
2019 const void *request_body
, ULONG request_size
, WS_MESSAGE
*reply
,
2020 const WS_MESSAGE_DESCRIPTION
*reply_desc
, WS_READ_OPTION read_option
,
2021 WS_HEAP
*heap
, void *value
, ULONG size
)
2024 WsInitializeMessage( request
, WS_REQUEST_MESSAGE
, NULL
, NULL
);
2025 if ((hr
= WsAddressMessage( request
, &channel
->addr
, NULL
)) != S_OK
) return hr
;
2026 if ((hr
= message_set_action( request
, request_desc
->action
)) != S_OK
) return hr
;
2028 if ((hr
= connect_channel( channel
)) != S_OK
) return hr
;
2029 if ((hr
= init_writer( channel
)) != S_OK
) return hr
;
2030 if ((hr
= write_message( channel
, request
, request_desc
->bodyElementDescription
, write_option
, request_body
,
2031 request_size
)) != S_OK
) return hr
;
2032 if ((hr
= send_message( channel
, request
)) != S_OK
) return hr
;
2034 return receive_message( channel
, reply
, &reply_desc
, 1, WS_RECEIVE_OPTIONAL_MESSAGE
, read_option
, heap
,
2035 value
, size
, NULL
);
2038 struct request_reply
2041 struct channel
*channel
;
2042 WS_MESSAGE
*request
;
2043 const WS_MESSAGE_DESCRIPTION
*request_desc
;
2044 WS_WRITE_OPTION write_option
;
2045 const void *request_body
;
2048 const WS_MESSAGE_DESCRIPTION
*reply_desc
;
2049 WS_READ_OPTION read_option
;
2053 WS_ASYNC_CONTEXT ctx
;
2056 static void request_reply_proc( struct task
*task
)
2058 struct request_reply
*r
= (struct request_reply
*)task
;
2061 hr
= request_reply( r
->channel
, r
->request
, r
->request_desc
, r
->write_option
, r
->request_body
, r
->request_size
,
2062 r
->reply
, r
->reply_desc
, r
->read_option
, r
->heap
, r
->value
, r
->size
);
2064 TRACE( "calling %p(%08x)\n", r
->ctx
.callback
, hr
);
2065 r
->ctx
.callback( hr
, WS_LONG_CALLBACK
, r
->ctx
.callbackState
);
2066 TRACE( "%p returned\n", r
->ctx
.callback
);
2069 static HRESULT
queue_request_reply( struct channel
*channel
, WS_MESSAGE
*request
,
2070 const WS_MESSAGE_DESCRIPTION
*request_desc
, WS_WRITE_OPTION write_option
,
2071 const void *request_body
, ULONG request_size
, WS_MESSAGE
*reply
,
2072 const WS_MESSAGE_DESCRIPTION
*reply_desc
, WS_READ_OPTION read_option
,
2073 WS_HEAP
*heap
, void *value
, ULONG size
, const WS_ASYNC_CONTEXT
*ctx
)
2075 struct request_reply
*r
;
2077 if (!(r
= heap_alloc( sizeof(*r
) ))) return E_OUTOFMEMORY
;
2078 r
->task
.proc
= request_reply_proc
;
2079 r
->channel
= channel
;
2080 r
->request
= request
;
2081 r
->request_desc
= request_desc
;
2082 r
->write_option
= write_option
;
2083 r
->request_body
= request_body
;
2084 r
->request_size
= request_size
;
2086 r
->reply_desc
= reply_desc
;
2087 r
->read_option
= read_option
;
2092 return queue_task( &channel
->recv_q
, &r
->task
);
2095 /**************************************************************************
2096 * WsRequestReply [webservices.@]
2098 HRESULT WINAPI
WsRequestReply( WS_CHANNEL
*handle
, WS_MESSAGE
*request
, const WS_MESSAGE_DESCRIPTION
*request_desc
,
2099 WS_WRITE_OPTION write_option
, const void *request_body
, ULONG request_size
,
2100 WS_MESSAGE
*reply
, const WS_MESSAGE_DESCRIPTION
*reply_desc
, WS_READ_OPTION read_option
,
2101 WS_HEAP
*heap
, void *value
, ULONG size
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
2103 struct channel
*channel
= (struct channel
*)handle
;
2106 TRACE( "%p %p %p %08x %p %u %p %p %08x %p %p %u %p %p\n", handle
, request
, request_desc
, write_option
,
2107 request_body
, request_size
, reply
, reply_desc
, read_option
, heap
, value
, size
, ctx
, error
);
2108 if (error
) FIXME( "ignoring error parameter\n" );
2109 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2111 if (!channel
|| !request
|| !reply
) return E_INVALIDARG
;
2113 EnterCriticalSection( &channel
->cs
);
2115 if (channel
->magic
!= CHANNEL_MAGIC
)
2117 LeaveCriticalSection( &channel
->cs
);
2118 return E_INVALIDARG
;
2122 hr
= queue_request_reply( channel
, request
, request_desc
, write_option
, request_body
, request_size
, reply
,
2123 reply_desc
, read_option
, heap
, value
, size
, ctx
);
2125 hr
= request_reply( channel
, request
, request_desc
, write_option
, request_body
, request_size
, reply
,
2126 reply_desc
, read_option
, heap
, value
, size
);
2128 LeaveCriticalSection( &channel
->cs
);
2129 TRACE( "returning %08x\n", hr
);
2133 /**************************************************************************
2134 * WsReadMessageStart [webservices.@]
2136 HRESULT WINAPI
WsReadMessageStart( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2139 struct channel
*channel
= (struct channel
*)handle
;
2142 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2143 if (error
) FIXME( "ignoring error parameter\n" );
2144 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2146 if (!channel
|| !msg
) return E_INVALIDARG
;
2148 EnterCriticalSection( &channel
->cs
);
2150 if (channel
->magic
!= CHANNEL_MAGIC
)
2152 LeaveCriticalSection( &channel
->cs
);
2153 return E_INVALIDARG
;
2156 if ((hr
= receive_message_bytes( channel
)) == S_OK
)
2158 if ((hr
= init_reader( channel
)) == S_OK
)
2159 hr
= WsReadEnvelopeStart( msg
, channel
->reader
, NULL
, NULL
, NULL
);
2162 LeaveCriticalSection( &channel
->cs
);
2163 TRACE( "returning %08x\n", hr
);
2167 /**************************************************************************
2168 * WsReadMessageEnd [webservices.@]
2170 HRESULT WINAPI
WsReadMessageEnd( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2173 struct channel
*channel
= (struct channel
*)handle
;
2176 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2177 if (error
) FIXME( "ignoring error parameter\n" );
2178 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2180 if (!channel
|| !msg
) return E_INVALIDARG
;
2182 EnterCriticalSection( &channel
->cs
);
2184 if (channel
->magic
!= CHANNEL_MAGIC
)
2186 LeaveCriticalSection( &channel
->cs
);
2187 return E_INVALIDARG
;
2190 hr
= WsReadEnvelopeEnd( msg
, NULL
);
2192 LeaveCriticalSection( &channel
->cs
);
2193 TRACE( "returning %08x\n", hr
);
2197 /**************************************************************************
2198 * WsWriteMessageStart [webservices.@]
2200 HRESULT WINAPI
WsWriteMessageStart( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2203 struct channel
*channel
= (struct channel
*)handle
;
2206 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2207 if (error
) FIXME( "ignoring error parameter\n" );
2208 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2210 if (!channel
|| !msg
) return E_INVALIDARG
;
2212 EnterCriticalSection( &channel
->cs
);
2214 if (channel
->magic
!= CHANNEL_MAGIC
)
2216 LeaveCriticalSection( &channel
->cs
);
2217 return E_INVALIDARG
;
2220 if ((hr
= connect_channel( channel
)) != S_OK
) goto done
;
2221 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
2222 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
2223 hr
= WsWriteEnvelopeStart( msg
, channel
->writer
, NULL
, NULL
, NULL
);
2226 LeaveCriticalSection( &channel
->cs
);
2227 TRACE( "returning %08x\n", hr
);
2231 /**************************************************************************
2232 * WsWriteMessageEnd [webservices.@]
2234 HRESULT WINAPI
WsWriteMessageEnd( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2237 struct channel
*channel
= (struct channel
*)handle
;
2240 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2241 if (error
) FIXME( "ignoring error parameter\n" );
2242 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2244 if (!channel
|| !msg
) return E_INVALIDARG
;
2246 EnterCriticalSection( &channel
->cs
);
2248 if (channel
->magic
!= CHANNEL_MAGIC
)
2250 LeaveCriticalSection( &channel
->cs
);
2251 return E_INVALIDARG
;
2254 if ((hr
= WsWriteEnvelopeEnd( msg
, NULL
)) == S_OK
&& (hr
= connect_channel( channel
) == S_OK
))
2255 hr
= send_message( channel
, msg
);
2257 LeaveCriticalSection( &channel
->cs
);
2258 TRACE( "returning %08x\n", hr
);
2262 static void set_blocking( SOCKET socket
, BOOL blocking
)
2264 ULONG state
= !blocking
;
2265 ioctlsocket( socket
, FIONBIO
, &state
);
2268 static HRESULT
sock_accept( SOCKET socket
, HANDLE wait
, HANDLE cancel
, SOCKET
*ret
)
2270 HANDLE handles
[] = { wait
, cancel
};
2273 if (WSAEventSelect( socket
, handles
[0], FD_ACCEPT
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
2275 switch (WSAWaitForMultipleEvents( 2, handles
, FALSE
, WSA_INFINITE
, FALSE
))
2278 if ((*ret
= accept( socket
, NULL
, NULL
)) != -1)
2280 WSAEventSelect( *ret
, NULL
, 0 );
2281 set_blocking( *ret
, TRUE
);
2284 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2288 hr
= WS_E_OPERATION_ABORTED
;
2292 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2299 HRESULT
channel_accept_tcp( SOCKET socket
, HANDLE wait
, HANDLE cancel
, WS_CHANNEL
*handle
)
2301 struct channel
*channel
= (struct channel
*)handle
;
2304 EnterCriticalSection( &channel
->cs
);
2306 if (channel
->magic
!= CHANNEL_MAGIC
)
2308 LeaveCriticalSection( &channel
->cs
);
2309 return E_INVALIDARG
;
2312 hr
= sock_accept( socket
, wait
, cancel
, &channel
->u
.tcp
.socket
);
2314 LeaveCriticalSection( &channel
->cs
);
2318 static HRESULT
sock_wait( SOCKET socket
, HANDLE wait
, HANDLE cancel
)
2320 HANDLE handles
[] = { wait
, cancel
};
2323 if (WSAEventSelect( socket
, handles
[0], FD_READ
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
2325 switch (WSAWaitForMultipleEvents( 2, handles
, FALSE
, WSA_INFINITE
, FALSE
))
2332 hr
= WS_E_OPERATION_ABORTED
;
2336 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2340 WSAEventSelect( socket
, NULL
, 0 );
2341 set_blocking( socket
, TRUE
);
2345 HRESULT
channel_accept_udp( SOCKET socket
, HANDLE wait
, HANDLE cancel
, WS_CHANNEL
*handle
)
2347 struct channel
*channel
= (struct channel
*)handle
;
2350 EnterCriticalSection( &channel
->cs
);
2352 if (channel
->magic
!= CHANNEL_MAGIC
)
2354 LeaveCriticalSection( &channel
->cs
);
2355 return E_INVALIDARG
;
2358 if ((hr
= sock_wait( socket
, wait
, cancel
)) == S_OK
) channel
->u
.udp
.socket
= socket
;
2360 LeaveCriticalSection( &channel
->cs
);
2364 /**************************************************************************
2365 * WsAbortChannel [webservices.@]
2367 HRESULT WINAPI
WsAbortChannel( WS_CHANNEL
*handle
, WS_ERROR
*error
)
2369 FIXME( "%p %p: stub!\n", handle
, error
);