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 "webservices_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(webservices
);
35 static const struct prop_desc channel_props
[] =
37 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE */
38 { sizeof(UINT64
), FALSE
}, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_MESSAGE_SIZE */
39 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_START_SIZE */
40 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_FLUSH_SIZE */
41 { sizeof(WS_ENCODING
), TRUE
}, /* WS_CHANNEL_PROPERTY_ENCODING */
42 { sizeof(WS_ENVELOPE_VERSION
), FALSE
}, /* WS_CHANNEL_PROPERTY_ENVELOPE_VERSION */
43 { sizeof(WS_ADDRESSING_VERSION
), FALSE
}, /* WS_CHANNEL_PROPERTY_ADDRESSING_VERSION */
44 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_MAX_SESSION_DICTIONARY_SIZE */
45 { sizeof(WS_CHANNEL_STATE
), TRUE
}, /* WS_CHANNEL_PROPERTY_STATE */
46 { sizeof(WS_CALLBACK_MODEL
), FALSE
}, /* WS_CHANNEL_PROPERTY_ASYNC_CALLBACK_MODEL */
47 { sizeof(WS_IP_VERSION
), FALSE
}, /* WS_CHANNEL_PROPERTY_IP_VERSION */
48 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_RESOLVE_TIMEOUT */
49 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_CONNECT_TIMEOUT */
50 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_SEND_TIMEOUT */
51 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_RECEIVE_RESPONSE_TIMEOUT */
52 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_RECEIVE_TIMEOUT */
53 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_CLOSE_TIMEOUT */
54 { sizeof(BOOL
), FALSE
}, /* WS_CHANNEL_PROPERTY_ENABLE_TIMEOUTS */
55 { sizeof(WS_TRANSFER_MODE
), FALSE
}, /* WS_CHANNEL_PROPERTY_TRANSFER_MODE */
56 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_MULTICAST_INTERFACE */
57 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_MULTICAST_HOPS */
58 { sizeof(WS_ENDPOINT_ADDRESS
), TRUE
}, /* WS_CHANNEL_PROPERTY_REMOTE_ADDRESS */
59 { sizeof(SOCKADDR_STORAGE
), TRUE
}, /* WS_CHANNEL_PROPERTY_REMOTE_IP_ADDRESS */
60 { sizeof(ULONGLONG
), TRUE
}, /* WS_CHANNEL_PROPERTY_HTTP_CONNECTION_ID */
61 { sizeof(WS_CUSTOM_CHANNEL_CALLBACKS
), FALSE
}, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_CALLBACKS */
62 { 0, FALSE
}, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_PARAMETERS */
63 { sizeof(void *), FALSE
}, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_INSTANCE */
64 { sizeof(WS_STRING
), TRUE
}, /* WS_CHANNEL_PROPERTY_TRANSPORT_URL */
65 { sizeof(BOOL
), FALSE
}, /* WS_CHANNEL_PROPERTY_NO_DELAY */
66 { sizeof(BOOL
), FALSE
}, /* WS_CHANNEL_PROPERTY_SEND_KEEP_ALIVES */
67 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_TIME */
68 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_INTERVAL */
69 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_MAX_HTTP_SERVER_CONNECTIONS */
70 { sizeof(BOOL
), TRUE
}, /* WS_CHANNEL_PROPERTY_IS_SESSION_SHUT_DOWN */
71 { sizeof(WS_CHANNEL_TYPE
), TRUE
}, /* WS_CHANNEL_PROPERTY_CHANNEL_TYPE */
72 { sizeof(ULONG
), FALSE
}, /* WS_CHANNEL_PROPERTY_TRIM_BUFFERED_MESSAGE_SIZE */
73 { sizeof(WS_CHANNEL_ENCODER
), FALSE
}, /* WS_CHANNEL_PROPERTY_ENCODER */
74 { sizeof(WS_CHANNEL_DECODER
), FALSE
}, /* WS_CHANNEL_PROPERTY_DECODER */
75 { sizeof(WS_PROTECTION_LEVEL
), TRUE
}, /* WS_CHANNEL_PROPERTY_PROTECTION_LEVEL */
76 { sizeof(WS_COOKIE_MODE
), FALSE
}, /* WS_CHANNEL_PROPERTY_COOKIE_MODE */
77 { sizeof(WS_HTTP_PROXY_SETTING_MODE
), FALSE
}, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SETTING_MODE */
78 { sizeof(WS_CUSTOM_HTTP_PROXY
), FALSE
}, /* WS_CHANNEL_PROPERTY_CUSTOM_HTTP_PROXY */
79 { sizeof(WS_HTTP_MESSAGE_MAPPING
), FALSE
}, /* WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING */
80 { sizeof(BOOL
), FALSE
}, /* WS_CHANNEL_PROPERTY_ENABLE_HTTP_REDIRECT */
81 { sizeof(WS_HTTP_REDIRECT_CALLBACK_CONTEXT
), FALSE
}, /* WS_CHANNEL_PROPERTY_HTTP_REDIRECT_CALLBACK_CONTEXT */
82 { sizeof(BOOL
), FALSE
}, /* WS_CHANNEL_PROPERTY_FAULTS_AS_ERRORS */
83 { sizeof(BOOL
), FALSE
}, /* WS_CHANNEL_PROPERTY_ALLOW_UNSECURED_FAULTS */
84 { sizeof(WCHAR
*), TRUE
}, /* WS_CHANNEL_PROPERTY_HTTP_SERVER_SPN */
85 { sizeof(WCHAR
*), TRUE
}, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SPN */
86 { sizeof(ULONG
), FALSE
} /* WS_CHANNEL_PROPERTY_MAX_HTTP_REQUEST_HEADERS_BUFFER_SIZE */
92 void (*proc
)( struct task
* );
104 static struct task
*dequeue_task( struct queue
*queue
)
108 EnterCriticalSection( &queue
->cs
);
109 TRACE( "%u tasks queued\n", list_count( &queue
->tasks
) );
110 task
= LIST_ENTRY( list_head( &queue
->tasks
), struct task
, entry
);
111 if (task
) list_remove( &task
->entry
);
112 LeaveCriticalSection( &queue
->cs
);
114 TRACE( "returning task %p\n", task
);
118 static void CALLBACK
queue_runner( TP_CALLBACK_INSTANCE
*instance
, void *ctx
)
120 struct queue
*queue
= ctx
;
121 HANDLE handles
[] = { queue
->wait
, queue
->cancel
};
123 SetEvent( queue
->ready
);
126 DWORD err
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
132 while ((task
= dequeue_task( queue
)))
139 case WAIT_OBJECT_0
+ 1:
140 TRACE( "cancelled\n" );
141 SetEvent( queue
->ready
);
145 ERR( "wait failed %u\n", err
);
151 static HRESULT
start_queue( struct queue
*queue
)
153 HRESULT hr
= E_OUTOFMEMORY
;
155 if (queue
->wait
) return S_OK
;
156 list_init( &queue
->tasks
);
157 if (!(queue
->wait
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
))) goto error
;
158 if (!(queue
->cancel
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
))) goto error
;
159 if (!(queue
->ready
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
))) goto error
;
160 if (!TrySubmitThreadpoolCallback( queue_runner
, queue
, NULL
)) hr
= HRESULT_FROM_WIN32( GetLastError() );
163 WaitForSingleObject( queue
->ready
, INFINITE
);
168 CloseHandle( queue
->wait
);
170 CloseHandle( queue
->cancel
);
171 queue
->cancel
= NULL
;
172 CloseHandle( queue
->ready
);
177 static HRESULT
queue_task( struct queue
*queue
, struct task
*task
)
180 if ((hr
= start_queue( queue
)) != S_OK
) return hr
;
182 EnterCriticalSection( &queue
->cs
);
183 TRACE( "queueing task %p\n", task
);
184 list_add_tail( &queue
->tasks
, &task
->entry
);
185 LeaveCriticalSection( &queue
->cs
);
187 SetEvent( queue
->wait
);
193 SESSION_STATE_UNINITIALIZED
,
194 SESSION_STATE_SETUP_COMPLETE
,
195 SESSION_STATE_SHUTDOWN
,
202 WS_CHANNEL_TYPE type
;
203 WS_CHANNEL_BINDING binding
;
204 WS_CHANNEL_STATE state
;
205 WS_ENDPOINT_ADDRESS addr
;
206 WS_XML_WRITER
*writer
;
207 WS_XML_READER
*reader
;
209 WS_ENCODING encoding
;
210 enum session_state session_state
;
211 struct dictionary dict_send
;
212 struct dictionary dict_recv
;
238 struct prop prop
[ARRAY_SIZE( channel_props
)];
241 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
243 static struct channel
*alloc_channel(void)
245 static const ULONG count
= ARRAY_SIZE( channel_props
);
247 ULONG size
= sizeof(*ret
) + prop_size( channel_props
, count
);
249 if (!(ret
= heap_alloc_zero( size
))) return NULL
;
251 ret
->magic
= CHANNEL_MAGIC
;
252 InitializeCriticalSection( &ret
->cs
);
253 InitializeCriticalSection( &ret
->send_q
.cs
);
254 InitializeCriticalSection( &ret
->recv_q
.cs
);
255 ret
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": channel.cs");
256 ret
->send_q
.cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": channel.send_q.cs");
257 ret
->recv_q
.cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": channel.recv_q.cs");
259 prop_init( channel_props
, count
, ret
->prop
, &ret
[1] );
260 ret
->prop_count
= count
;
264 static void clear_addr( WS_ENDPOINT_ADDRESS
*addr
)
266 heap_free( addr
->url
.chars
);
267 addr
->url
.chars
= NULL
;
268 addr
->url
.length
= 0;
271 static void clear_queue( struct queue
*queue
)
275 SetEvent( queue
->cancel
);
276 WaitForSingleObject( queue
->ready
, INFINITE
);
278 while ((ptr
= list_head( &queue
->tasks
)))
280 struct task
*task
= LIST_ENTRY( ptr
, struct task
, entry
);
281 list_remove( &task
->entry
);
285 CloseHandle( queue
->wait
);
287 CloseHandle( queue
->cancel
);
288 queue
->cancel
= NULL
;
289 CloseHandle( queue
->ready
);
293 static void reset_channel( struct channel
*channel
)
295 clear_queue( &channel
->send_q
);
296 clear_queue( &channel
->recv_q
);
297 channel
->state
= WS_CHANNEL_STATE_CREATED
;
298 channel
->session_state
= SESSION_STATE_UNINITIALIZED
;
299 clear_addr( &channel
->addr
);
300 clear_dict( &channel
->dict_send
);
301 clear_dict( &channel
->dict_recv
);
303 channel
->read_size
= 0;
305 switch (channel
->binding
)
307 case WS_HTTP_CHANNEL_BINDING
:
308 WinHttpCloseHandle( channel
->u
.http
.request
);
309 channel
->u
.http
.request
= NULL
;
310 WinHttpCloseHandle( channel
->u
.http
.connect
);
311 channel
->u
.http
.connect
= NULL
;
312 WinHttpCloseHandle( channel
->u
.http
.session
);
313 channel
->u
.http
.session
= NULL
;
314 heap_free( channel
->u
.http
.path
);
315 channel
->u
.http
.path
= NULL
;
316 channel
->u
.http
.flags
= 0;
319 case WS_TCP_CHANNEL_BINDING
:
320 closesocket( channel
->u
.tcp
.socket
);
321 channel
->u
.tcp
.socket
= -1;
324 case WS_UDP_CHANNEL_BINDING
:
325 closesocket( channel
->u
.udp
.socket
);
326 channel
->u
.udp
.socket
= -1;
333 static void free_header_mappings( WS_HTTP_HEADER_MAPPING
**mappings
, ULONG count
)
336 for (i
= 0; i
< count
; i
++) heap_free( mappings
[i
] );
337 heap_free( mappings
);
340 static void free_message_mapping( const WS_HTTP_MESSAGE_MAPPING
*mapping
)
342 free_header_mappings( mapping
->requestHeaderMappings
, mapping
->requestHeaderMappingCount
);
343 free_header_mappings( mapping
->responseHeaderMappings
, mapping
->responseHeaderMappingCount
);
346 static void free_props( struct channel
*channel
)
348 struct prop
*prop
= &channel
->prop
[WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING
];
349 WS_HTTP_MESSAGE_MAPPING
*mapping
= (WS_HTTP_MESSAGE_MAPPING
*)prop
->value
;
350 free_message_mapping( mapping
);
353 static void free_channel( struct channel
*channel
)
355 reset_channel( channel
);
357 WsFreeWriter( channel
->writer
);
358 WsFreeReader( channel
->reader
);
360 heap_free( channel
->read_buf
);
361 free_props( channel
);
363 channel
->send_q
.cs
.DebugInfo
->Spare
[0] = 0;
364 channel
->recv_q
.cs
.DebugInfo
->Spare
[0] = 0;
365 channel
->cs
.DebugInfo
->Spare
[0] = 0;
366 DeleteCriticalSection( &channel
->send_q
.cs
);
367 DeleteCriticalSection( &channel
->recv_q
.cs
);
368 DeleteCriticalSection( &channel
->cs
);
369 heap_free( channel
);
372 static WS_HTTP_HEADER_MAPPING
*dup_header_mapping( const WS_HTTP_HEADER_MAPPING
*src
)
374 WS_HTTP_HEADER_MAPPING
*dst
;
376 if (!(dst
= heap_alloc( sizeof(*dst
) + src
->headerName
.length
))) return NULL
;
378 dst
->headerName
.bytes
= (BYTE
*)(dst
+ 1);
379 memcpy( dst
->headerName
.bytes
, src
->headerName
.bytes
, src
->headerName
.length
);
380 dst
->headerName
.length
= src
->headerName
.length
;
381 dst
->headerMappingOptions
= src
->headerMappingOptions
;
385 static HRESULT
dup_message_mapping( const WS_HTTP_MESSAGE_MAPPING
*src
, WS_HTTP_MESSAGE_MAPPING
*dst
)
389 size
= src
->requestHeaderMappingCount
* sizeof(*dst
->responseHeaderMappings
);
390 if (!(dst
->requestHeaderMappings
= heap_alloc( size
))) return E_OUTOFMEMORY
;
392 for (i
= 0; i
< src
->requestHeaderMappingCount
; i
++)
394 if (!(dst
->requestHeaderMappings
[i
] = dup_header_mapping( src
->requestHeaderMappings
[i
] )))
396 free_header_mappings( dst
->requestHeaderMappings
, i
);
397 return E_OUTOFMEMORY
;
401 size
= src
->responseHeaderMappingCount
* sizeof(*dst
->responseHeaderMappings
);
402 if (!(dst
->responseHeaderMappings
= heap_alloc( size
)))
404 heap_free( dst
->responseHeaderMappings
);
405 return E_OUTOFMEMORY
;
408 for (i
= 0; i
< src
->responseHeaderMappingCount
; i
++)
410 if (!(dst
->responseHeaderMappings
[i
] = dup_header_mapping( src
->responseHeaderMappings
[i
] )))
412 free_header_mappings( dst
->responseHeaderMappings
, i
);
413 return E_OUTOFMEMORY
;
417 dst
->requestMappingOptions
= src
->requestMappingOptions
;
418 dst
->responseMappingOptions
= src
->responseMappingOptions
;
419 dst
->requestHeaderMappingCount
= src
->requestHeaderMappingCount
;
420 dst
->responseHeaderMappingCount
= src
->responseHeaderMappingCount
;
424 static HRESULT
create_channel( WS_CHANNEL_TYPE type
, WS_CHANNEL_BINDING binding
,
425 const WS_CHANNEL_PROPERTY
*properties
, ULONG count
, struct channel
**ret
)
427 struct channel
*channel
;
428 ULONG i
, msg_size
= 65536;
429 WS_ENVELOPE_VERSION env_version
= WS_ENVELOPE_VERSION_SOAP_1_2
;
430 WS_ADDRESSING_VERSION addr_version
= WS_ADDRESSING_VERSION_1_0
;
433 if (!(channel
= alloc_channel())) return E_OUTOFMEMORY
;
435 prop_set( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE
,
436 &msg_size
, sizeof(msg_size
) );
437 prop_set( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION
,
438 &env_version
, sizeof(env_version
) );
439 prop_set( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION
,
440 &addr_version
, sizeof(addr_version
) );
442 channel
->type
= type
;
443 channel
->binding
= binding
;
445 switch (channel
->binding
)
447 case WS_HTTP_CHANNEL_BINDING
:
448 channel
->encoding
= WS_ENCODING_XML_UTF8
;
451 case WS_TCP_CHANNEL_BINDING
:
452 channel
->u
.tcp
.socket
= -1;
453 channel
->encoding
= WS_ENCODING_XML_BINARY_SESSION_1
;
456 case WS_UDP_CHANNEL_BINDING
:
457 channel
->u
.udp
.socket
= -1;
458 channel
->encoding
= WS_ENCODING_XML_UTF8
;
464 for (i
= 0; i
< count
; i
++)
466 const WS_CHANNEL_PROPERTY
*prop
= &properties
[i
];
468 TRACE( "property id %u value %p size %u\n", prop
->id
, prop
->value
, prop
->valueSize
);
469 if (prop
->valueSize
== sizeof(ULONG
) && prop
->value
) TRACE( " value %08x\n", *(ULONG
*)prop
->value
);
473 case WS_CHANNEL_PROPERTY_ENCODING
:
474 if (!prop
->value
|| prop
->valueSize
!= sizeof(channel
->encoding
))
476 free_channel( channel
);
479 channel
->encoding
= *(WS_ENCODING
*)prop
->value
;
482 case WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING
:
484 const WS_HTTP_MESSAGE_MAPPING
*src
= (WS_HTTP_MESSAGE_MAPPING
*)prop
->value
;
485 WS_HTTP_MESSAGE_MAPPING dst
;
487 if (!prop
->value
|| prop
->valueSize
!= sizeof(*src
))
489 free_channel( channel
);
493 if ((hr
= dup_message_mapping( src
, &dst
)) != S_OK
) return hr
;
495 if ((hr
= prop_set( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING
, &dst
,
496 sizeof(dst
) )) != S_OK
)
498 free_message_mapping( &dst
);
499 free_channel( channel
);
506 if ((hr
= prop_set( channel
->prop
, channel
->prop_count
, prop
->id
, prop
->value
, prop
->valueSize
)) != S_OK
)
508 free_channel( channel
);
519 /**************************************************************************
520 * WsCreateChannel [webservices.@]
522 HRESULT WINAPI
WsCreateChannel( WS_CHANNEL_TYPE type
, WS_CHANNEL_BINDING binding
,
523 const WS_CHANNEL_PROPERTY
*properties
, ULONG count
,
524 const WS_SECURITY_DESCRIPTION
*desc
, WS_CHANNEL
**handle
,
527 struct channel
*channel
;
530 TRACE( "%u %u %p %u %p %p %p\n", type
, binding
, properties
, count
, desc
, handle
, error
);
531 if (error
) FIXME( "ignoring error parameter\n" );
532 if (desc
) FIXME( "ignoring security description\n" );
534 if (!handle
) return E_INVALIDARG
;
536 if (type
!= WS_CHANNEL_TYPE_REQUEST
&& type
!= WS_CHANNEL_TYPE_DUPLEX
&&
537 type
!= WS_CHANNEL_TYPE_DUPLEX_SESSION
)
539 FIXME( "channel type %u not implemented\n", type
);
542 if (binding
!= WS_HTTP_CHANNEL_BINDING
&& binding
!= WS_TCP_CHANNEL_BINDING
&&
543 binding
!= WS_UDP_CHANNEL_BINDING
)
545 FIXME( "channel binding %u not implemented\n", binding
);
549 if ((hr
= create_channel( type
, binding
, properties
, count
, &channel
)) != S_OK
) return hr
;
551 TRACE( "created %p\n", channel
);
552 *handle
= (WS_CHANNEL
*)channel
;
556 /**************************************************************************
557 * WsCreateChannelForListener [webservices.@]
559 HRESULT WINAPI
WsCreateChannelForListener( WS_LISTENER
*listener_handle
, const WS_CHANNEL_PROPERTY
*properties
,
560 ULONG count
, WS_CHANNEL
**handle
, WS_ERROR
*error
)
562 struct channel
*channel
;
563 WS_CHANNEL_TYPE type
;
564 WS_CHANNEL_BINDING binding
;
567 TRACE( "%p %p %u %p %p\n", listener_handle
, properties
, count
, handle
, error
);
568 if (error
) FIXME( "ignoring error parameter\n" );
570 if (!listener_handle
|| !handle
) return E_INVALIDARG
;
572 if ((hr
= WsGetListenerProperty( listener_handle
, WS_LISTENER_PROPERTY_CHANNEL_TYPE
, &type
,
573 sizeof(type
), NULL
)) != S_OK
) return hr
;
575 if ((hr
= WsGetListenerProperty( listener_handle
, WS_LISTENER_PROPERTY_CHANNEL_BINDING
, &binding
,
576 sizeof(binding
), NULL
)) != S_OK
) return hr
;
578 if ((hr
= create_channel( type
, binding
, properties
, count
, &channel
)) != S_OK
) return hr
;
580 TRACE( "created %p\n", channel
);
581 *handle
= (WS_CHANNEL
*)channel
;
585 /**************************************************************************
586 * WsFreeChannel [webservices.@]
588 void WINAPI
WsFreeChannel( WS_CHANNEL
*handle
)
590 struct channel
*channel
= (struct channel
*)handle
;
592 TRACE( "%p\n", handle
);
594 if (!channel
) return;
596 EnterCriticalSection( &channel
->cs
);
598 if (channel
->magic
!= CHANNEL_MAGIC
)
600 LeaveCriticalSection( &channel
->cs
);
606 LeaveCriticalSection( &channel
->cs
);
607 free_channel( channel
);
610 /**************************************************************************
611 * WsResetChannel [webservices.@]
613 HRESULT WINAPI
WsResetChannel( WS_CHANNEL
*handle
, WS_ERROR
*error
)
615 struct channel
*channel
= (struct channel
*)handle
;
618 TRACE( "%p %p\n", handle
, error
);
619 if (error
) FIXME( "ignoring error parameter\n" );
621 if (!channel
) return E_INVALIDARG
;
623 EnterCriticalSection( &channel
->cs
);
625 if (channel
->magic
!= CHANNEL_MAGIC
)
627 LeaveCriticalSection( &channel
->cs
);
631 if (channel
->state
!= WS_CHANNEL_STATE_CREATED
&& channel
->state
!= WS_CHANNEL_STATE_CLOSED
)
632 hr
= WS_E_INVALID_OPERATION
;
634 reset_channel( channel
);
636 LeaveCriticalSection( &channel
->cs
);
637 TRACE( "returning %08x\n", hr
);
641 /**************************************************************************
642 * WsGetChannelProperty [webservices.@]
644 HRESULT WINAPI
WsGetChannelProperty( WS_CHANNEL
*handle
, WS_CHANNEL_PROPERTY_ID id
, void *buf
,
645 ULONG size
, WS_ERROR
*error
)
647 struct channel
*channel
= (struct channel
*)handle
;
650 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
651 if (error
) FIXME( "ignoring error parameter\n" );
653 if (!channel
) return E_INVALIDARG
;
655 EnterCriticalSection( &channel
->cs
);
657 if (channel
->magic
!= CHANNEL_MAGIC
)
659 LeaveCriticalSection( &channel
->cs
);
665 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE
:
666 if (!buf
|| size
!= sizeof(channel
->type
)) hr
= E_INVALIDARG
;
667 else *(WS_CHANNEL_TYPE
*)buf
= channel
->type
;
670 case WS_CHANNEL_PROPERTY_ENCODING
:
671 if (!buf
|| size
!= sizeof(channel
->encoding
)) hr
= E_INVALIDARG
;
672 else *(WS_ENCODING
*)buf
= channel
->encoding
;
676 hr
= prop_get( channel
->prop
, channel
->prop_count
, id
, buf
, size
);
679 LeaveCriticalSection( &channel
->cs
);
680 TRACE( "returning %08x\n", hr
);
684 /**************************************************************************
685 * WsSetChannelProperty [webservices.@]
687 HRESULT WINAPI
WsSetChannelProperty( WS_CHANNEL
*handle
, WS_CHANNEL_PROPERTY_ID id
, const void *value
,
688 ULONG size
, WS_ERROR
*error
)
690 struct channel
*channel
= (struct channel
*)handle
;
693 TRACE( "%p %u %p %u\n", handle
, id
, value
, size
);
694 if (error
) FIXME( "ignoring error parameter\n" );
696 if (!channel
) return E_INVALIDARG
;
698 EnterCriticalSection( &channel
->cs
);
700 if (channel
->magic
!= CHANNEL_MAGIC
)
702 LeaveCriticalSection( &channel
->cs
);
706 hr
= prop_set( channel
->prop
, channel
->prop_count
, id
, value
, size
);
708 LeaveCriticalSection( &channel
->cs
);
709 TRACE( "returning %08x\n", hr
);
713 static HRESULT
open_channel( struct channel
*channel
, const WS_ENDPOINT_ADDRESS
*endpoint
)
715 if (endpoint
->headers
|| endpoint
->extensions
|| endpoint
->identity
)
717 FIXME( "headers, extensions or identity not supported\n" );
721 TRACE( "endpoint %s\n", debugstr_wn(endpoint
->url
.chars
, endpoint
->url
.length
) );
723 if (!(channel
->addr
.url
.chars
= heap_alloc( endpoint
->url
.length
* sizeof(WCHAR
) ))) return E_OUTOFMEMORY
;
724 memcpy( channel
->addr
.url
.chars
, endpoint
->url
.chars
, endpoint
->url
.length
* sizeof(WCHAR
) );
725 channel
->addr
.url
.length
= endpoint
->url
.length
;
727 channel
->state
= WS_CHANNEL_STATE_OPEN
;
731 /**************************************************************************
732 * WsOpenChannel [webservices.@]
734 HRESULT WINAPI
WsOpenChannel( WS_CHANNEL
*handle
, const WS_ENDPOINT_ADDRESS
*endpoint
,
735 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
737 struct channel
*channel
= (struct channel
*)handle
;
740 TRACE( "%p %p %p %p\n", handle
, endpoint
, ctx
, error
);
741 if (error
) FIXME( "ignoring error parameter\n" );
742 if (ctx
) FIXME( "ignoring ctx parameter\n" );
744 if (!channel
|| !endpoint
) return E_INVALIDARG
;
746 EnterCriticalSection( &channel
->cs
);
748 if (channel
->magic
!= CHANNEL_MAGIC
)
750 LeaveCriticalSection( &channel
->cs
);
754 if (channel
->state
!= WS_CHANNEL_STATE_CREATED
) hr
= WS_E_INVALID_OPERATION
;
755 else hr
= open_channel( channel
, endpoint
);
757 LeaveCriticalSection( &channel
->cs
);
758 TRACE( "returning %08x\n", hr
);
762 enum frame_record_type
764 FRAME_RECORD_TYPE_VERSION
,
765 FRAME_RECORD_TYPE_MODE
,
766 FRAME_RECORD_TYPE_VIA
,
767 FRAME_RECORD_TYPE_KNOWN_ENCODING
,
768 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING
,
769 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE
,
770 FRAME_RECORD_TYPE_SIZED_ENVELOPE
,
771 FRAME_RECORD_TYPE_END
,
772 FRAME_RECORD_TYPE_FAULT
,
773 FRAME_RECORD_TYPE_UPGRADE_REQUEST
,
774 FRAME_RECORD_TYPE_UPGRADE_RESPONSE
,
775 FRAME_RECORD_TYPE_PREAMBLE_ACK
,
776 FRAME_RECORD_TYPE_PREAMBLE_END
,
779 static HRESULT
send_byte( SOCKET socket
, BYTE byte
)
781 int count
= send( socket
, (char *)&byte
, 1, 0 );
782 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
783 if (count
!= 1) return WS_E_OTHER
;
787 static HRESULT
shutdown_session( struct channel
*channel
)
791 if (channel
->state
!= WS_CHANNEL_STATE_OPEN
||
792 (channel
->type
!= WS_CHANNEL_TYPE_OUTPUT_SESSION
&&
793 channel
->type
!= WS_CHANNEL_TYPE_DUPLEX_SESSION
) ||
794 channel
->session_state
>= SESSION_STATE_SHUTDOWN
) return WS_E_INVALID_OPERATION
;
796 switch (channel
->binding
)
798 case WS_TCP_CHANNEL_BINDING
:
799 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_END
)) != S_OK
) return hr
;
800 channel
->session_state
= SESSION_STATE_SHUTDOWN
;
804 FIXME( "unhandled binding %u\n", channel
->binding
);
809 HRESULT WINAPI
WsShutdownSessionChannel( WS_CHANNEL
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
811 struct channel
*channel
= (struct channel
*)handle
;
814 TRACE( "%p %p %p\n", handle
, ctx
, error
);
815 if (error
) FIXME( "ignoring error parameter\n" );
816 if (ctx
) FIXME( "ignoring ctx parameter\n" );
818 if (!channel
) return E_INVALIDARG
;
820 EnterCriticalSection( &channel
->cs
);
822 if (channel
->magic
!= CHANNEL_MAGIC
)
824 LeaveCriticalSection( &channel
->cs
);
828 hr
= shutdown_session( channel
);
830 LeaveCriticalSection( &channel
->cs
);
831 TRACE( "returning %08x\n", hr
);
835 static void close_channel( struct channel
*channel
)
837 reset_channel( channel
);
838 channel
->state
= WS_CHANNEL_STATE_CLOSED
;
841 /**************************************************************************
842 * WsCloseChannel [webservices.@]
844 HRESULT WINAPI
WsCloseChannel( WS_CHANNEL
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
846 struct channel
*channel
= (struct channel
*)handle
;
849 TRACE( "%p %p %p\n", handle
, ctx
, error
);
850 if (error
) FIXME( "ignoring error parameter\n" );
851 if (ctx
) FIXME( "ignoring ctx parameter\n" );
853 if (!channel
) return E_INVALIDARG
;
855 EnterCriticalSection( &channel
->cs
);
857 if (channel
->magic
!= CHANNEL_MAGIC
)
859 LeaveCriticalSection( &channel
->cs
);
863 close_channel( channel
);
865 LeaveCriticalSection( &channel
->cs
);
866 TRACE( "returning %08x\n", hr
);
870 static HRESULT
parse_http_url( const WCHAR
*url
, ULONG len
, URL_COMPONENTS
*uc
)
872 HRESULT hr
= E_OUTOFMEMORY
;
876 memset( uc
, 0, sizeof(*uc
) );
877 uc
->dwStructSize
= sizeof(*uc
);
878 uc
->dwHostNameLength
= 128;
879 uc
->lpszHostName
= heap_alloc( uc
->dwHostNameLength
* sizeof(WCHAR
) );
880 uc
->dwUrlPathLength
= 128;
881 uc
->lpszUrlPath
= heap_alloc( uc
->dwUrlPathLength
* sizeof(WCHAR
) );
882 uc
->dwExtraInfoLength
= 128;
883 uc
->lpszExtraInfo
= heap_alloc( uc
->dwExtraInfoLength
* sizeof(WCHAR
) );
884 if (!uc
->lpszHostName
|| !uc
->lpszUrlPath
|| !uc
->lpszExtraInfo
) goto error
;
886 if (!WinHttpCrackUrl( url
, len
, ICU_DECODE
, uc
))
888 if ((err
= GetLastError()) != ERROR_INSUFFICIENT_BUFFER
)
890 hr
= HRESULT_FROM_WIN32( err
);
893 if (!(tmp
= heap_realloc( uc
->lpszHostName
, uc
->dwHostNameLength
* sizeof(WCHAR
) ))) goto error
;
894 uc
->lpszHostName
= tmp
;
895 if (!(tmp
= heap_realloc( uc
->lpszUrlPath
, uc
->dwUrlPathLength
* sizeof(WCHAR
) ))) goto error
;
896 uc
->lpszUrlPath
= tmp
;
897 if (!(tmp
= heap_realloc( uc
->lpszExtraInfo
, uc
->dwExtraInfoLength
* sizeof(WCHAR
) ))) goto error
;
898 uc
->lpszExtraInfo
= tmp
;
899 WinHttpCrackUrl( url
, len
, ICU_DECODE
, uc
);
905 heap_free( uc
->lpszHostName
);
906 heap_free( uc
->lpszUrlPath
);
907 heap_free( uc
->lpszExtraInfo
);
911 static HRESULT
connect_channel_http( struct channel
*channel
)
913 HINTERNET ses
= NULL
, con
= NULL
;
917 if (channel
->u
.http
.connect
) return S_OK
;
919 if ((hr
= parse_http_url( channel
->addr
.url
.chars
, channel
->addr
.url
.length
, &uc
)) != S_OK
) return hr
;
920 if (!(channel
->u
.http
.path
= heap_alloc( (uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
+ 1) * sizeof(WCHAR
) )))
927 lstrcpyW( channel
->u
.http
.path
, uc
.lpszUrlPath
);
928 if (uc
.dwExtraInfoLength
) lstrcatW( channel
->u
.http
.path
, uc
.lpszExtraInfo
);
931 channel
->u
.http
.flags
= WINHTTP_FLAG_REFRESH
;
934 case INTERNET_SCHEME_HTTP
: break;
935 case INTERNET_SCHEME_HTTPS
:
936 channel
->u
.http
.flags
|= WINHTTP_FLAG_SECURE
;
940 hr
= WS_E_INVALID_ENDPOINT_URL
;
944 if (!(ses
= WinHttpOpen( L
"MS-WebServices/1.0", 0, NULL
, NULL
, 0 )))
946 hr
= HRESULT_FROM_WIN32( GetLastError() );
949 if (!(con
= WinHttpConnect( ses
, uc
.lpszHostName
, uc
.nPort
, 0 )))
951 hr
= HRESULT_FROM_WIN32( GetLastError() );
955 channel
->u
.http
.session
= ses
;
956 channel
->u
.http
.connect
= con
;
961 WinHttpCloseHandle( con
);
962 WinHttpCloseHandle( ses
);
964 heap_free( uc
.lpszHostName
);
965 heap_free( uc
.lpszUrlPath
);
966 heap_free( uc
.lpszExtraInfo
);
970 static HRESULT
connect_channel_tcp( struct channel
*channel
)
972 struct sockaddr_storage storage
;
973 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
975 WS_URL_SCHEME_TYPE scheme
;
980 if (channel
->u
.tcp
.socket
!= -1) return S_OK
;
982 if ((hr
= parse_url( &channel
->addr
.url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
983 if (scheme
!= WS_URL_NETTCP_SCHEME_TYPE
)
986 return WS_E_INVALID_ENDPOINT_URL
;
991 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, 0 );
993 if (hr
!= S_OK
) return hr
;
995 if ((channel
->u
.tcp
.socket
= socket( addr
->sa_family
, SOCK_STREAM
, 0 )) == -1)
996 return HRESULT_FROM_WIN32( WSAGetLastError() );
998 if (connect( channel
->u
.tcp
.socket
, addr
, addr_len
) < 0)
1000 closesocket( channel
->u
.tcp
.socket
);
1001 channel
->u
.tcp
.socket
= -1;
1002 return HRESULT_FROM_WIN32( WSAGetLastError() );
1008 static HRESULT
connect_channel_udp( struct channel
*channel
)
1010 struct sockaddr_storage storage
;
1011 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
1013 WS_URL_SCHEME_TYPE scheme
;
1018 if (channel
->u
.udp
.socket
!= -1) return S_OK
;
1020 if ((hr
= parse_url( &channel
->addr
.url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
1021 if (scheme
!= WS_URL_SOAPUDP_SCHEME_TYPE
)
1024 return WS_E_INVALID_ENDPOINT_URL
;
1029 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, 0 );
1031 if (hr
!= S_OK
) return hr
;
1033 if ((channel
->u
.udp
.socket
= socket( addr
->sa_family
, SOCK_DGRAM
, 0 )) == -1)
1034 return HRESULT_FROM_WIN32( WSAGetLastError() );
1036 if (connect( channel
->u
.udp
.socket
, addr
, addr_len
) < 0)
1038 closesocket( channel
->u
.udp
.socket
);
1039 channel
->u
.udp
.socket
= -1;
1040 return HRESULT_FROM_WIN32( WSAGetLastError() );
1046 static HRESULT
connect_channel( struct channel
*channel
)
1048 switch (channel
->binding
)
1050 case WS_HTTP_CHANNEL_BINDING
:
1051 return connect_channel_http( channel
);
1053 case WS_TCP_CHANNEL_BINDING
:
1054 return connect_channel_tcp( channel
);
1056 case WS_UDP_CHANNEL_BINDING
:
1057 return connect_channel_udp( channel
);
1060 ERR( "unhandled binding %u\n", channel
->binding
);
1065 static HRESULT
send_message_http( HINTERNET request
, BYTE
*data
, ULONG len
)
1067 if (!WinHttpSendRequest( request
, NULL
, 0, data
, len
, len
, 0 ))
1068 return HRESULT_FROM_WIN32( GetLastError() );
1070 if (!WinHttpReceiveResponse( request
, NULL
))
1071 return HRESULT_FROM_WIN32( GetLastError() );
1075 static HRESULT
send_bytes( SOCKET socket
, BYTE
*bytes
, int len
)
1077 int count
= send( socket
, (char *)bytes
, len
, 0 );
1078 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1079 if (count
!= len
) return WS_E_OTHER
;
1083 static HRESULT
send_size( SOCKET socket
, ULONG size
)
1086 if (size
< 0x80) return send_byte( socket
, size
);
1087 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
1088 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
1089 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
1090 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
1091 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
1092 if ((size
>>= 7) < 0x80) return send_byte( socket
, size
);
1093 if ((hr
= send_byte( socket
, (size
& 0x7f) | 0x80 )) != S_OK
) return hr
;
1094 if ((size
>>= 7) < 0x08) return send_byte( socket
, size
);
1095 return E_INVALIDARG
;
1098 static inline ULONG
size_length( ULONG size
)
1100 if (size
< 0x80) return 1;
1101 if (size
< 0x4000) return 2;
1102 if (size
< 0x200000) return 3;
1103 if (size
< 0x10000000) return 4;
1107 static ULONG
string_table_size( const struct dictionary
*dict
)
1110 for (i
= 0; i
< dict
->dict
.stringCount
; i
++)
1112 if (dict
->sequence
[i
] == dict
->current_sequence
)
1113 size
+= size_length( dict
->dict
.strings
[i
].length
) + dict
->dict
.strings
[i
].length
;
1118 static HRESULT
send_string_table( SOCKET socket
, const struct dictionary
*dict
)
1122 for (i
= 0; i
< dict
->dict
.stringCount
; i
++)
1124 if (dict
->sequence
[i
] != dict
->current_sequence
) continue;
1125 if ((hr
= send_size( socket
, dict
->dict
.strings
[i
].length
)) != S_OK
) return hr
;
1126 if ((hr
= send_bytes( socket
, dict
->dict
.strings
[i
].bytes
, dict
->dict
.strings
[i
].length
)) != S_OK
) return hr
;
1131 static HRESULT
string_to_utf8( const WS_STRING
*str
, unsigned char **ret
, int *len
)
1133 *len
= WideCharToMultiByte( CP_UTF8
, 0, str
->chars
, str
->length
, NULL
, 0, NULL
, NULL
);
1134 if (!(*ret
= heap_alloc( *len
))) return E_OUTOFMEMORY
;
1135 WideCharToMultiByte( CP_UTF8
, 0, str
->chars
, str
->length
, (char *)*ret
, *len
, NULL
, NULL
);
1141 SESSION_MODE_INVALID
= 0,
1142 SESSION_MODE_SINGLETON
= 1,
1143 SESSION_MODE_DUPLEX
= 2,
1144 SESSION_MODE_SIMPLEX
= 3,
1147 static enum session_mode
map_channel_type( struct channel
*channel
)
1149 switch (channel
->type
)
1151 case WS_CHANNEL_TYPE_DUPLEX_SESSION
: return SESSION_MODE_DUPLEX
;
1153 FIXME( "unhandled channel type %08x\n", channel
->type
);
1154 return SESSION_MODE_INVALID
;
1160 KNOWN_ENCODING_SOAP11_UTF8
= 0x00,
1161 KNOWN_ENCODING_SOAP11_UTF16
= 0x01,
1162 KNOWN_ENCODING_SOAP11_UTF16LE
= 0x02,
1163 KNOWN_ENCODING_SOAP12_UTF8
= 0x03,
1164 KNOWN_ENCODING_SOAP12_UTF16
= 0x04,
1165 KNOWN_ENCODING_SOAP12_UTF16LE
= 0x05,
1166 KNOWN_ENCODING_SOAP12_MTOM
= 0x06,
1167 KNOWN_ENCODING_SOAP12_BINARY
= 0x07,
1168 KNOWN_ENCODING_SOAP12_BINARY_SESSION
= 0x08,
1171 static enum known_encoding
map_channel_encoding( struct channel
*channel
)
1173 WS_ENVELOPE_VERSION version
;
1175 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION
, &version
, sizeof(version
) );
1179 case WS_ENVELOPE_VERSION_SOAP_1_1
:
1180 switch (channel
->encoding
)
1182 case WS_ENCODING_XML_UTF8
: return KNOWN_ENCODING_SOAP11_UTF8
;
1183 case WS_ENCODING_XML_UTF16LE
: return KNOWN_ENCODING_SOAP11_UTF16LE
;
1185 FIXME( "unhandled version/encoding %u/%u\n", version
, channel
->encoding
);
1188 case WS_ENVELOPE_VERSION_SOAP_1_2
:
1189 switch (channel
->encoding
)
1191 case WS_ENCODING_XML_UTF8
: return KNOWN_ENCODING_SOAP12_UTF8
;
1192 case WS_ENCODING_XML_UTF16LE
: return KNOWN_ENCODING_SOAP12_UTF16LE
;
1193 case WS_ENCODING_XML_BINARY_1
: return KNOWN_ENCODING_SOAP12_BINARY
;
1194 case WS_ENCODING_XML_BINARY_SESSION_1
: return KNOWN_ENCODING_SOAP12_BINARY_SESSION
;
1196 FIXME( "unhandled version/encoding %u/%u\n", version
, channel
->encoding
);
1200 ERR( "unhandled version %u\n", version
);
1205 #define FRAME_VERSION_MAJOR 1
1206 #define FRAME_VERSION_MINOR 1
1208 static HRESULT
send_preamble( struct channel
*channel
)
1214 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_VERSION
)) != S_OK
) return hr
;
1215 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_VERSION_MAJOR
)) != S_OK
) return hr
;
1216 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_VERSION_MINOR
)) != S_OK
) return hr
;
1218 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_MODE
)) != S_OK
) return hr
;
1219 if ((hr
= send_byte( channel
->u
.tcp
.socket
, map_channel_type(channel
) )) != S_OK
) return hr
;
1221 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_VIA
)) != S_OK
) return hr
;
1222 if ((hr
= string_to_utf8( &channel
->addr
.url
, &url
, &len
)) != S_OK
) return hr
;
1223 if ((hr
= send_size( channel
->u
.tcp
.socket
, len
)) != S_OK
) goto done
;
1224 if ((hr
= send_bytes( channel
->u
.tcp
.socket
, url
, len
)) != S_OK
) goto done
;
1226 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_KNOWN_ENCODING
)) != S_OK
) goto done
;
1227 if ((hr
= send_byte( channel
->u
.tcp
.socket
, map_channel_encoding(channel
) )) != S_OK
) goto done
;
1228 hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_PREAMBLE_END
);
1235 static HRESULT
receive_bytes( struct channel
*channel
, unsigned char *bytes
, int len
)
1237 int count
= recv( channel
->u
.tcp
.socket
, (char *)bytes
, len
, 0 );
1238 if (count
< 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
1239 if (count
!= len
) return WS_E_INVALID_FORMAT
;
1243 static HRESULT
receive_preamble_ack( struct channel
*channel
)
1248 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1249 if (byte
!= FRAME_RECORD_TYPE_PREAMBLE_ACK
) return WS_E_INVALID_FORMAT
;
1250 channel
->session_state
= SESSION_STATE_SETUP_COMPLETE
;
1254 static HRESULT
send_sized_envelope( struct channel
*channel
, BYTE
*data
, ULONG len
)
1256 ULONG table_size
= string_table_size( &channel
->dict_send
);
1259 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_SIZED_ENVELOPE
)) != S_OK
) return hr
;
1260 if ((hr
= send_size( channel
->u
.tcp
.socket
, size_length(table_size
) + table_size
+ len
)) != S_OK
) return hr
;
1261 if ((hr
= send_size( channel
->u
.tcp
.socket
, table_size
)) != S_OK
) return hr
;
1262 if ((hr
= send_string_table( channel
->u
.tcp
.socket
, &channel
->dict_send
)) != S_OK
) return hr
;
1263 return send_bytes( channel
->u
.tcp
.socket
, data
, len
);
1266 static HRESULT
open_http_request( struct channel
*channel
, HINTERNET
*req
)
1268 if ((*req
= WinHttpOpenRequest( channel
->u
.http
.connect
, L
"POST", channel
->u
.http
.path
,
1269 NULL
, NULL
, NULL
, channel
->u
.http
.flags
))) return S_OK
;
1270 return HRESULT_FROM_WIN32( GetLastError() );
1273 static HRESULT
send_message( struct channel
*channel
, WS_MESSAGE
*msg
)
1275 WS_XML_WRITER
*writer
;
1280 WsGetMessageProperty( channel
->msg
, WS_MESSAGE_PROPERTY_BODY_WRITER
, &writer
, sizeof(writer
), NULL
);
1281 WsGetWriterProperty( writer
, WS_XML_WRITER_PROPERTY_BYTES
, &buf
, sizeof(buf
), NULL
);
1283 switch (channel
->binding
)
1285 case WS_HTTP_CHANNEL_BINDING
:
1286 if (channel
->u
.http
.request
)
1288 WinHttpCloseHandle( channel
->u
.http
.request
);
1289 channel
->u
.http
.request
= NULL
;
1291 if ((hr
= open_http_request( channel
, &channel
->u
.http
.request
)) != S_OK
) return hr
;
1292 if ((hr
= message_insert_http_headers( msg
, channel
->u
.http
.request
)) != S_OK
) return hr
;
1293 return send_message_http( channel
->u
.http
.request
, buf
.bytes
, buf
.length
);
1295 case WS_TCP_CHANNEL_BINDING
:
1296 if (channel
->type
& WS_CHANNEL_TYPE_SESSION
)
1298 switch (channel
->session_state
)
1300 case SESSION_STATE_UNINITIALIZED
:
1301 if ((hr
= send_preamble( channel
)) != S_OK
) return hr
;
1302 if ((hr
= receive_preamble_ack( channel
)) != S_OK
) return hr
;
1305 case SESSION_STATE_SETUP_COMPLETE
:
1306 return send_sized_envelope( channel
, buf
.bytes
, buf
.length
);
1309 ERR( "unhandled session state %u\n", channel
->session_state
);
1315 case WS_UDP_CHANNEL_BINDING
:
1316 return WsFlushWriter( writer
, 0, NULL
, NULL
);
1319 ERR( "unhandled binding %u\n", channel
->binding
);
1324 HRESULT
channel_send_message( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
)
1326 struct channel
*channel
= (struct channel
*)handle
;
1329 EnterCriticalSection( &channel
->cs
);
1331 if (channel
->magic
!= CHANNEL_MAGIC
)
1333 LeaveCriticalSection( &channel
->cs
);
1334 return E_INVALIDARG
;
1337 if ((hr
= connect_channel( channel
)) == S_OK
) hr
= send_message( channel
, msg
);
1339 LeaveCriticalSection( &channel
->cs
);
1343 static HRESULT CALLBACK
dict_cb( void *state
, const WS_XML_STRING
*str
, BOOL
*found
, ULONG
*id
, WS_ERROR
*error
)
1345 struct dictionary
*dict
= state
;
1350 if ((index
= find_string( dict
, str
->bytes
, str
->length
, id
)) == -1)
1356 if (!(bytes
= heap_alloc( str
->length
))) return E_OUTOFMEMORY
;
1357 memcpy( bytes
, str
->bytes
, str
->length
);
1358 if ((hr
= insert_string( dict
, bytes
, str
->length
, index
, id
)) == S_OK
)
1369 static CALLBACK HRESULT
write_callback( void *state
, const WS_BYTES
*buf
, ULONG count
,
1370 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1372 SOCKET socket
= *(SOCKET
*)state
;
1373 if (send( socket
, (const char *)buf
->bytes
, buf
->length
, 0 ) < 0)
1375 TRACE( "send failed %u\n", WSAGetLastError() );
1380 static HRESULT
init_writer( struct channel
*channel
)
1382 WS_XML_WRITER_BUFFER_OUTPUT buf
= {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER
}};
1383 WS_XML_WRITER_STREAM_OUTPUT stream
= {{WS_XML_WRITER_OUTPUT_TYPE_STREAM
}};
1384 WS_XML_WRITER_TEXT_ENCODING text
= {{WS_XML_WRITER_ENCODING_TYPE_TEXT
}, WS_CHARSET_UTF8
};
1385 WS_XML_WRITER_BINARY_ENCODING bin
= {{WS_XML_WRITER_ENCODING_TYPE_BINARY
}};
1386 const WS_XML_WRITER_ENCODING
*encoding
;
1387 const WS_XML_WRITER_OUTPUT
*output
;
1388 WS_XML_WRITER_PROPERTY prop
;
1389 ULONG max_size
= (1 << 17);
1392 prop
.id
= WS_XML_WRITER_PROPERTY_BUFFER_MAX_SIZE
;
1393 prop
.value
= &max_size
;
1394 prop
.valueSize
= sizeof(max_size
);
1395 if (!channel
->writer
&& (hr
= WsCreateWriter( &prop
, 1, &channel
->writer
, NULL
)) != S_OK
) return hr
;
1397 switch (channel
->encoding
)
1399 case WS_ENCODING_XML_UTF8
:
1400 encoding
= &text
.encoding
;
1401 if (channel
->binding
== WS_UDP_CHANNEL_BINDING
||
1402 (channel
->binding
== WS_TCP_CHANNEL_BINDING
&& !(channel
->type
& WS_CHANNEL_TYPE_SESSION
)))
1404 stream
.writeCallback
= write_callback
;
1405 stream
.writeCallbackState
= (channel
->binding
== WS_UDP_CHANNEL_BINDING
) ?
1406 &channel
->u
.udp
.socket
: &channel
->u
.tcp
.socket
;
1407 output
= &stream
.output
;
1409 else output
= &buf
.output
;
1412 case WS_ENCODING_XML_BINARY_SESSION_1
:
1413 bin
.staticDictionary
= (WS_XML_DICTIONARY
*)&dict_builtin_static
.dict
;
1416 case WS_ENCODING_XML_BINARY_1
:
1417 encoding
= &bin
.encoding
;
1418 output
= &buf
.output
;
1422 FIXME( "unhandled encoding %u\n", channel
->encoding
);
1423 return WS_E_NOT_SUPPORTED
;
1426 return WsSetOutput( channel
->writer
, encoding
, output
, NULL
, 0, NULL
);
1429 static HRESULT
write_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_ELEMENT_DESCRIPTION
*desc
,
1430 WS_WRITE_OPTION option
, const void *body
, ULONG size
)
1433 if ((hr
= writer_set_lookup( channel
->writer
, TRUE
)) != S_OK
) return hr
;
1434 if ((hr
= WsWriteEnvelopeStart( msg
, channel
->writer
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
1435 if ((hr
= writer_set_lookup( channel
->writer
, FALSE
)) != S_OK
) return hr
;
1436 channel
->dict_send
.current_sequence
++;
1437 if ((hr
= writer_set_dict_callback( channel
->writer
, dict_cb
, &channel
->dict_send
)) != S_OK
) return hr
;
1438 if ((hr
= WsWriteBody( msg
, desc
, option
, body
, size
, NULL
)) != S_OK
) return hr
;
1439 return WsWriteEnvelopeEnd( msg
, NULL
);
1442 /**************************************************************************
1443 * WsSendMessage [webservices.@]
1445 HRESULT WINAPI
WsSendMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
*desc
,
1446 WS_WRITE_OPTION option
, const void *body
, ULONG size
, const WS_ASYNC_CONTEXT
*ctx
,
1449 struct channel
*channel
= (struct channel
*)handle
;
1452 TRACE( "%p %p %p %08x %p %u %p %p\n", handle
, msg
, desc
, option
, body
, size
, ctx
, error
);
1453 if (error
) FIXME( "ignoring error parameter\n" );
1454 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1456 if (!channel
|| !msg
|| !desc
) return E_INVALIDARG
;
1458 EnterCriticalSection( &channel
->cs
);
1460 if (channel
->magic
!= CHANNEL_MAGIC
)
1462 LeaveCriticalSection( &channel
->cs
);
1463 return E_INVALIDARG
;
1466 if ((hr
= WsInitializeMessage( msg
, WS_REQUEST_MESSAGE
, NULL
, NULL
)) != S_OK
) goto done
;
1467 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
1468 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) goto done
;
1470 if ((hr
= connect_channel( channel
)) != S_OK
) goto done
;
1471 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
1472 if ((hr
= write_message( channel
, msg
, desc
->bodyElementDescription
, option
, body
, size
)) != S_OK
) goto done
;
1473 hr
= send_message( channel
, msg
);
1476 LeaveCriticalSection( &channel
->cs
);
1477 TRACE( "returning %08x\n", hr
);
1481 /**************************************************************************
1482 * WsSendReplyMessage [webservices.@]
1484 HRESULT WINAPI
WsSendReplyMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
*desc
,
1485 WS_WRITE_OPTION option
, const void *body
, ULONG size
, WS_MESSAGE
*request
,
1486 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1488 struct channel
*channel
= (struct channel
*)handle
;
1492 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle
, msg
, desc
, option
, body
, size
, request
, ctx
, error
);
1493 if (error
) FIXME( "ignoring error parameter\n" );
1494 if (ctx
) FIXME( "ignoring ctx parameter\n" );
1496 if (!channel
|| !msg
|| !desc
|| !request
) return E_INVALIDARG
;
1498 EnterCriticalSection( &channel
->cs
);
1500 if (channel
->magic
!= CHANNEL_MAGIC
)
1502 LeaveCriticalSection( &channel
->cs
);
1503 return E_INVALIDARG
;
1506 if ((hr
= WsInitializeMessage( msg
, WS_REPLY_MESSAGE
, NULL
, NULL
)) != S_OK
) goto done
;
1507 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
1508 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) goto done
;
1509 if ((hr
= message_get_id( request
, &req_id
)) != S_OK
) goto done
;
1510 if ((hr
= message_set_request_id( msg
, &req_id
)) != S_OK
) goto done
;
1512 if ((hr
= connect_channel( channel
)) != S_OK
) goto done
;
1513 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
1514 if ((hr
= write_message( channel
, msg
, desc
->bodyElementDescription
, option
, body
, size
)) != S_OK
) goto done
;
1515 hr
= send_message( channel
, msg
);
1518 LeaveCriticalSection( &channel
->cs
);
1519 TRACE( "returning %08x\n", hr
);
1523 static HRESULT
resize_read_buffer( struct channel
*channel
, ULONG size
)
1525 if (!channel
->read_buf
)
1527 if (!(channel
->read_buf
= heap_alloc( size
))) return E_OUTOFMEMORY
;
1528 channel
->read_buflen
= size
;
1531 if (channel
->read_buflen
< size
)
1534 ULONG new_size
= max( size
, channel
->read_buflen
* 2 );
1535 if (!(tmp
= heap_realloc( channel
->read_buf
, new_size
))) return E_OUTOFMEMORY
;
1536 channel
->read_buf
= tmp
;
1537 channel
->read_buflen
= new_size
;
1542 static CALLBACK HRESULT
read_callback( void *state
, void *buf
, ULONG buflen
, ULONG
*retlen
,
1543 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
1545 SOCKET socket
= *(SOCKET
*)state
;
1548 if ((ret
= recv( socket
, buf
, buflen
, 0 )) >= 0) *retlen
= ret
;
1551 TRACE( "recv failed %u\n", WSAGetLastError() );
1557 static HRESULT
init_reader( struct channel
*channel
)
1559 WS_XML_READER_BUFFER_INPUT buf
= {{WS_XML_READER_INPUT_TYPE_BUFFER
}};
1560 WS_XML_READER_STREAM_INPUT stream
= {{WS_XML_READER_INPUT_TYPE_STREAM
}};
1561 WS_XML_READER_TEXT_ENCODING text
= {{WS_XML_READER_ENCODING_TYPE_TEXT
}};
1562 WS_XML_READER_BINARY_ENCODING bin
= {{WS_XML_READER_ENCODING_TYPE_BINARY
}};
1563 const WS_XML_READER_ENCODING
*encoding
;
1564 const WS_XML_READER_INPUT
*input
;
1567 if (!channel
->reader
&& (hr
= WsCreateReader( NULL
, 0, &channel
->reader
, NULL
)) != S_OK
) return hr
;
1569 switch (channel
->encoding
)
1571 case WS_ENCODING_XML_UTF8
:
1572 text
.charSet
= WS_CHARSET_UTF8
;
1573 encoding
= &text
.encoding
;
1575 if (channel
->binding
== WS_UDP_CHANNEL_BINDING
||
1576 (channel
->binding
== WS_TCP_CHANNEL_BINDING
&& !(channel
->type
& WS_CHANNEL_TYPE_SESSION
)))
1578 stream
.readCallback
= read_callback
;
1579 stream
.readCallbackState
= (channel
->binding
== WS_UDP_CHANNEL_BINDING
) ?
1580 &channel
->u
.udp
.socket
: &channel
->u
.tcp
.socket
;
1581 input
= &stream
.input
;
1585 buf
.encodedData
= channel
->read_buf
;
1586 buf
.encodedDataSize
= channel
->read_size
;
1591 case WS_ENCODING_XML_BINARY_SESSION_1
:
1592 bin
.staticDictionary
= (WS_XML_DICTIONARY
*)&dict_builtin_static
.dict
;
1593 bin
.dynamicDictionary
= &channel
->dict_recv
.dict
;
1596 case WS_ENCODING_XML_BINARY_1
:
1597 encoding
= &bin
.encoding
;
1599 buf
.encodedData
= channel
->read_buf
;
1600 buf
.encodedDataSize
= channel
->read_size
;
1605 FIXME( "unhandled encoding %u\n", channel
->encoding
);
1606 return WS_E_NOT_SUPPORTED
;
1609 return WsSetInput( channel
->reader
, encoding
, input
, NULL
, 0, NULL
);
1612 static const WS_HTTP_MESSAGE_MAPPING
*get_http_message_mapping( struct channel
*channel
)
1614 const struct prop
*prop
= &channel
->prop
[WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING
];
1615 return (const WS_HTTP_MESSAGE_MAPPING
*)prop
->value
;
1618 static HRESULT
map_http_response_headers( struct channel
*channel
, WS_MESSAGE
*msg
)
1620 const WS_HTTP_MESSAGE_MAPPING
*mapping
= get_http_message_mapping( channel
);
1621 return message_map_http_response_headers( msg
, channel
->u
.http
.request
, mapping
);
1624 #define INITIAL_READ_BUFFER_SIZE 4096
1625 static HRESULT
receive_message_http( struct channel
*channel
, WS_MESSAGE
*msg
)
1627 DWORD len
, bytes_read
, offset
= 0, size
= INITIAL_READ_BUFFER_SIZE
;
1631 if ((hr
= map_http_response_headers( channel
, msg
)) != S_OK
) return hr
;
1633 prop_get( channel
->prop
, channel
->prop_count
, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE
,
1634 &max_len
, sizeof(max_len
) );
1636 if ((hr
= resize_read_buffer( channel
, size
)) != S_OK
) return hr
;
1637 channel
->read_size
= 0;
1640 if (!WinHttpQueryDataAvailable( channel
->u
.http
.request
, &len
))
1642 return HRESULT_FROM_WIN32( GetLastError() );
1645 if (channel
->read_size
+ len
> max_len
) return WS_E_QUOTA_EXCEEDED
;
1646 if ((hr
= resize_read_buffer( channel
, channel
->read_size
+ len
)) != S_OK
) return hr
;
1648 if (!WinHttpReadData( channel
->u
.http
.request
, channel
->read_buf
+ offset
, len
, &bytes_read
))
1650 return HRESULT_FROM_WIN32( GetLastError() );
1652 if (!bytes_read
) break;
1653 channel
->read_size
+= bytes_read
;
1654 offset
+= bytes_read
;
1660 static HRESULT
receive_message_sized( struct channel
*channel
, unsigned int size
)
1662 unsigned int offset
= 0, to_read
= size
;
1666 if ((hr
= resize_read_buffer( channel
, size
)) != S_OK
) return hr
;
1668 channel
->read_size
= 0;
1669 while (channel
->read_size
< size
)
1671 if ((bytes_read
= recv( channel
->u
.tcp
.socket
, channel
->read_buf
+ offset
, to_read
, 0 )) < 0)
1673 return HRESULT_FROM_WIN32( WSAGetLastError() );
1675 if (!bytes_read
) break;
1676 channel
->read_size
+= bytes_read
;
1677 to_read
-= bytes_read
;
1678 offset
+= bytes_read
;
1680 if (channel
->read_size
!= size
) return WS_E_INVALID_FORMAT
;
1684 static HRESULT
receive_size( struct channel
*channel
, unsigned int *size
)
1689 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1690 *size
= byte
& 0x7f;
1691 if (!(byte
& 0x80)) return S_OK
;
1693 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1694 *size
+= (byte
& 0x7f) << 7;
1695 if (!(byte
& 0x80)) return S_OK
;
1697 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1698 *size
+= (byte
& 0x7f) << 14;
1699 if (!(byte
& 0x80)) return S_OK
;
1701 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1702 *size
+= (byte
& 0x7f) << 21;
1703 if (!(byte
& 0x80)) return S_OK
;
1705 if ((hr
= receive_bytes( channel
, &byte
, 1 )) != S_OK
) return hr
;
1706 if (byte
& ~0x0f) return WS_E_INVALID_FORMAT
;
1707 *size
+= byte
<< 28;
1711 static WS_ENCODING
map_known_encoding( enum known_encoding encoding
)
1715 case KNOWN_ENCODING_SOAP11_UTF8
:
1716 case KNOWN_ENCODING_SOAP12_UTF8
: return WS_ENCODING_XML_UTF8
;
1717 case KNOWN_ENCODING_SOAP11_UTF16
:
1718 case KNOWN_ENCODING_SOAP12_UTF16
: return WS_ENCODING_XML_UTF16BE
;
1719 case KNOWN_ENCODING_SOAP11_UTF16LE
:
1720 case KNOWN_ENCODING_SOAP12_UTF16LE
: return WS_ENCODING_XML_UTF16LE
;
1721 case KNOWN_ENCODING_SOAP12_BINARY
: return WS_ENCODING_XML_BINARY_1
;
1722 case KNOWN_ENCODING_SOAP12_BINARY_SESSION
: return WS_ENCODING_XML_BINARY_SESSION_1
;
1724 WARN( "unhandled encoding %u, assuming UTF8\n", encoding
);
1725 return WS_ENCODING_XML_UTF8
;
1729 static HRESULT
receive_preamble( struct channel
*channel
)
1736 if ((hr
= receive_bytes( channel
, &type
, 1 )) != S_OK
) return hr
;
1737 if (type
== FRAME_RECORD_TYPE_PREAMBLE_END
) break;
1740 case FRAME_RECORD_TYPE_VERSION
:
1742 unsigned char major
, minor
;
1743 if ((hr
= receive_bytes( channel
, &major
, 1 )) != S_OK
) return hr
;
1744 if ((hr
= receive_bytes( channel
, &minor
, 1 )) != S_OK
) return hr
;
1745 TRACE( "major %u minor %u\n", major
, major
);
1748 case FRAME_RECORD_TYPE_MODE
:
1751 if ((hr
= receive_bytes( channel
, &mode
, 1 )) != S_OK
) return hr
;
1752 TRACE( "mode %u\n", mode
);
1755 case FRAME_RECORD_TYPE_VIA
:
1760 if ((hr
= receive_size( channel
, &size
)) != S_OK
) return hr
;
1761 if (!(url
= heap_alloc( size
))) return E_OUTOFMEMORY
;
1762 if ((hr
= receive_bytes( channel
, url
, size
)) != S_OK
)
1767 TRACE( "transport URL %s\n", debugstr_an((char *)url
, size
) );
1768 heap_free( url
); /* FIXME: verify */
1771 case FRAME_RECORD_TYPE_KNOWN_ENCODING
:
1773 unsigned char encoding
;
1774 if ((hr
= receive_bytes( channel
, &encoding
, 1 )) != S_OK
) return hr
;
1775 TRACE( "encoding %u\n", encoding
);
1776 channel
->encoding
= map_known_encoding( encoding
);
1780 WARN( "unhandled record type %u\n", type
);
1781 return WS_E_INVALID_FORMAT
;
1788 static HRESULT
receive_sized_envelope( struct channel
*channel
)
1794 if ((hr
= receive_bytes( channel
, &type
, 1 )) != S_OK
) return hr
;
1795 if (type
!= FRAME_RECORD_TYPE_SIZED_ENVELOPE
) return WS_E_INVALID_FORMAT
;
1796 if ((hr
= receive_size( channel
, &size
)) != S_OK
) return hr
;
1797 if ((hr
= receive_message_sized( channel
, size
)) != S_OK
) return hr
;
1801 static HRESULT
read_size( const BYTE
**ptr
, ULONG len
, ULONG
*size
)
1803 const BYTE
*buf
= *ptr
;
1805 if (len
< 1) return WS_E_INVALID_FORMAT
;
1806 *size
= buf
[0] & 0x7f;
1807 if (!(buf
[0] & 0x80))
1812 if (len
< 2) return WS_E_INVALID_FORMAT
;
1813 *size
+= (buf
[1] & 0x7f) << 7;
1814 if (!(buf
[1] & 0x80))
1819 if (len
< 3) return WS_E_INVALID_FORMAT
;
1820 *size
+= (buf
[2] & 0x7f) << 14;
1821 if (!(buf
[2] & 0x80))
1826 if (len
< 4) return WS_E_INVALID_FORMAT
;
1827 *size
+= (buf
[3] & 0x7f) << 21;
1828 if (!(buf
[3] & 0x80))
1833 if (len
< 5 || (buf
[4] & ~0x07)) return WS_E_INVALID_FORMAT
;
1834 *size
+= buf
[4] << 28;
1839 static HRESULT
build_dict( const BYTE
*buf
, ULONG buflen
, struct dictionary
*dict
, ULONG
*used
)
1841 ULONG size
, strings_size
, strings_offset
;
1842 const BYTE
*ptr
= buf
;
1847 if ((hr
= read_size( &ptr
, buflen
, &strings_size
)) != S_OK
) return hr
;
1848 strings_offset
= ptr
- buf
;
1849 if (buflen
< strings_offset
+ strings_size
) return WS_E_INVALID_FORMAT
;
1850 *used
= strings_offset
+ strings_size
;
1851 if (!strings_size
) return S_OK
;
1853 UuidCreate( &dict
->dict
.guid
);
1854 dict
->dict
.isConst
= FALSE
;
1856 buflen
-= strings_offset
;
1857 ptr
= buf
+ strings_offset
;
1858 while (ptr
< buf
+ strings_size
)
1860 if ((hr
= read_size( &ptr
, buflen
, &size
)) != S_OK
)
1868 return WS_E_INVALID_FORMAT
;
1871 if (!(bytes
= heap_alloc( size
)))
1876 memcpy( bytes
, ptr
, size
);
1877 if ((index
= find_string( dict
, bytes
, size
, NULL
)) == -1) /* duplicate */
1883 if ((hr
= insert_string( dict
, bytes
, size
, index
, NULL
)) != S_OK
)
1898 static HRESULT
send_preamble_ack( struct channel
*channel
)
1901 if ((hr
= send_byte( channel
->u
.tcp
.socket
, FRAME_RECORD_TYPE_PREAMBLE_ACK
)) != S_OK
) return hr
;
1902 channel
->session_state
= SESSION_STATE_SETUP_COMPLETE
;
1906 static HRESULT
receive_message_session( struct channel
*channel
)
1910 if ((hr
= receive_sized_envelope( channel
)) != S_OK
) return hr
;
1911 if (channel
->encoding
== WS_ENCODING_XML_BINARY_SESSION_1
)
1914 if ((hr
= build_dict( (const BYTE
*)channel
->read_buf
, channel
->read_size
, &channel
->dict_recv
,
1915 &size
)) != S_OK
) return hr
;
1916 channel
->read_size
-= size
;
1917 memmove( channel
->read_buf
, channel
->read_buf
+ size
, channel
->read_size
);
1923 static HRESULT
receive_message_bytes( struct channel
*channel
, WS_MESSAGE
*msg
)
1926 if ((hr
= connect_channel( channel
)) != S_OK
) return hr
;
1928 switch (channel
->binding
)
1930 case WS_HTTP_CHANNEL_BINDING
:
1931 return receive_message_http( channel
, msg
);
1933 case WS_TCP_CHANNEL_BINDING
:
1934 if (channel
->type
& WS_CHANNEL_TYPE_SESSION
)
1936 switch (channel
->session_state
)
1938 case SESSION_STATE_UNINITIALIZED
:
1939 if ((hr
= receive_preamble( channel
)) != S_OK
) return hr
;
1940 if ((hr
= send_preamble_ack( channel
)) != S_OK
) return hr
;
1943 case SESSION_STATE_SETUP_COMPLETE
:
1944 return receive_message_session( channel
);
1947 ERR( "unhandled session state %u\n", channel
->session_state
);
1951 return S_OK
; /* nothing to do, data is read through stream callback */
1953 case WS_UDP_CHANNEL_BINDING
:
1957 ERR( "unhandled binding %u\n", channel
->binding
);
1962 HRESULT
channel_receive_message( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
)
1964 struct channel
*channel
= (struct channel
*)handle
;
1967 EnterCriticalSection( &channel
->cs
);
1969 if (channel
->magic
!= CHANNEL_MAGIC
)
1971 LeaveCriticalSection( &channel
->cs
);
1972 return E_INVALIDARG
;
1975 if ((hr
= receive_message_bytes( channel
, msg
)) == S_OK
) hr
= init_reader( channel
);
1977 LeaveCriticalSection( &channel
->cs
);
1981 HRESULT
channel_get_reader( WS_CHANNEL
*handle
, WS_XML_READER
**reader
)
1983 struct channel
*channel
= (struct channel
*)handle
;
1985 EnterCriticalSection( &channel
->cs
);
1987 if (channel
->magic
!= CHANNEL_MAGIC
)
1989 LeaveCriticalSection( &channel
->cs
);
1990 return E_INVALIDARG
;
1993 *reader
= channel
->reader
;
1995 LeaveCriticalSection( &channel
->cs
);
1999 static HRESULT
read_message( WS_MESSAGE
*handle
, WS_XML_READER
*reader
, const WS_ELEMENT_DESCRIPTION
*desc
,
2000 WS_READ_OPTION option
, WS_HEAP
*heap
, void *body
, ULONG size
)
2003 if ((hr
= WsReadEnvelopeStart( handle
, reader
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
2004 if ((hr
= WsReadBody( handle
, desc
, option
, heap
, body
, size
, NULL
)) != S_OK
) return hr
;
2005 return WsReadEnvelopeEnd( handle
, NULL
);
2008 static HRESULT
receive_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
2009 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
, WS_HEAP
*heap
,
2010 void *value
, ULONG size
, ULONG
*index
)
2015 if ((hr
= receive_message_bytes( channel
, msg
)) != S_OK
) return hr
;
2016 if ((hr
= init_reader( channel
)) != S_OK
) return hr
;
2018 for (i
= 0; i
< count
; i
++)
2020 const WS_ELEMENT_DESCRIPTION
*body
= desc
[i
]->bodyElementDescription
;
2021 if ((hr
= read_message( msg
, channel
->reader
, body
, read_option
, heap
, value
, size
)) == S_OK
)
2023 if (index
) *index
= i
;
2026 if ((hr
= WsResetMessage( msg
, NULL
)) != S_OK
) return hr
;
2027 if ((hr
= init_reader( channel
)) != S_OK
) return hr
;
2029 return (i
== count
) ? WS_E_INVALID_FORMAT
: S_OK
;
2032 struct receive_message
2035 struct channel
*channel
;
2037 const WS_MESSAGE_DESCRIPTION
**desc
;
2039 WS_RECEIVE_OPTION option
;
2040 WS_READ_OPTION read_option
;
2045 WS_ASYNC_CONTEXT ctx
;
2048 static void receive_message_proc( struct task
*task
)
2050 struct receive_message
*r
= (struct receive_message
*)task
;
2053 hr
= receive_message( r
->channel
, r
->msg
, r
->desc
, r
->count
, r
->option
, r
->read_option
, r
->heap
, r
->value
,
2054 r
->size
, r
->index
);
2056 TRACE( "calling %p(%08x)\n", r
->ctx
.callback
, hr
);
2057 r
->ctx
.callback( hr
, WS_LONG_CALLBACK
, r
->ctx
.callbackState
);
2058 TRACE( "%p returned\n", r
->ctx
.callback
);
2061 static HRESULT
queue_receive_message( struct channel
*channel
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
2062 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
,
2063 WS_HEAP
*heap
, void *value
, ULONG size
, ULONG
*index
,
2064 const WS_ASYNC_CONTEXT
*ctx
)
2066 struct receive_message
*r
;
2068 if (!(r
= heap_alloc( sizeof(*r
) ))) return E_OUTOFMEMORY
;
2069 r
->task
.proc
= receive_message_proc
;
2070 r
->channel
= channel
;
2075 r
->read_option
= read_option
;
2081 return queue_task( &channel
->recv_q
, &r
->task
);
2084 /**************************************************************************
2085 * WsReceiveMessage [webservices.@]
2087 HRESULT WINAPI
WsReceiveMessage( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_MESSAGE_DESCRIPTION
**desc
,
2088 ULONG count
, WS_RECEIVE_OPTION option
, WS_READ_OPTION read_option
, WS_HEAP
*heap
,
2089 void *value
, ULONG size
, ULONG
*index
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
2091 struct channel
*channel
= (struct channel
*)handle
;
2094 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle
, msg
, desc
, count
, option
, read_option
, heap
,
2095 value
, size
, index
, ctx
, error
);
2096 if (error
) FIXME( "ignoring error parameter\n" );
2098 if (!channel
|| !msg
|| !desc
|| !count
) return E_INVALIDARG
;
2100 EnterCriticalSection( &channel
->cs
);
2102 if (channel
->magic
!= CHANNEL_MAGIC
)
2104 LeaveCriticalSection( &channel
->cs
);
2105 return E_INVALIDARG
;
2109 hr
= queue_receive_message( channel
, msg
, desc
, count
, option
, read_option
, heap
, value
, size
, index
, ctx
);
2111 hr
= receive_message( channel
, msg
, desc
, count
, option
, read_option
, heap
, value
, size
, index
);
2113 LeaveCriticalSection( &channel
->cs
);
2114 TRACE( "returning %08x\n", hr
);
2118 static HRESULT
request_reply( struct channel
*channel
, WS_MESSAGE
*request
,
2119 const WS_MESSAGE_DESCRIPTION
*request_desc
, WS_WRITE_OPTION write_option
,
2120 const void *request_body
, ULONG request_size
, WS_MESSAGE
*reply
,
2121 const WS_MESSAGE_DESCRIPTION
*reply_desc
, WS_READ_OPTION read_option
,
2122 WS_HEAP
*heap
, void *value
, ULONG size
)
2125 WsInitializeMessage( request
, WS_REQUEST_MESSAGE
, NULL
, NULL
);
2126 if ((hr
= WsAddressMessage( request
, &channel
->addr
, NULL
)) != S_OK
) return hr
;
2127 if ((hr
= message_set_action( request
, request_desc
->action
)) != S_OK
) return hr
;
2129 if ((hr
= connect_channel( channel
)) != S_OK
) return hr
;
2130 if ((hr
= init_writer( channel
)) != S_OK
) return hr
;
2131 if ((hr
= write_message( channel
, request
, request_desc
->bodyElementDescription
, write_option
, request_body
,
2132 request_size
)) != S_OK
) return hr
;
2133 if ((hr
= send_message( channel
, request
)) != S_OK
) return hr
;
2135 return receive_message( channel
, reply
, &reply_desc
, 1, WS_RECEIVE_OPTIONAL_MESSAGE
, read_option
, heap
,
2136 value
, size
, NULL
);
2139 struct request_reply
2142 struct channel
*channel
;
2143 WS_MESSAGE
*request
;
2144 const WS_MESSAGE_DESCRIPTION
*request_desc
;
2145 WS_WRITE_OPTION write_option
;
2146 const void *request_body
;
2149 const WS_MESSAGE_DESCRIPTION
*reply_desc
;
2150 WS_READ_OPTION read_option
;
2154 WS_ASYNC_CONTEXT ctx
;
2157 static void request_reply_proc( struct task
*task
)
2159 struct request_reply
*r
= (struct request_reply
*)task
;
2162 hr
= request_reply( r
->channel
, r
->request
, r
->request_desc
, r
->write_option
, r
->request_body
, r
->request_size
,
2163 r
->reply
, r
->reply_desc
, r
->read_option
, r
->heap
, r
->value
, r
->size
);
2165 TRACE( "calling %p(%08x)\n", r
->ctx
.callback
, hr
);
2166 r
->ctx
.callback( hr
, WS_LONG_CALLBACK
, r
->ctx
.callbackState
);
2167 TRACE( "%p returned\n", r
->ctx
.callback
);
2170 static HRESULT
queue_request_reply( struct channel
*channel
, WS_MESSAGE
*request
,
2171 const WS_MESSAGE_DESCRIPTION
*request_desc
, WS_WRITE_OPTION write_option
,
2172 const void *request_body
, ULONG request_size
, WS_MESSAGE
*reply
,
2173 const WS_MESSAGE_DESCRIPTION
*reply_desc
, WS_READ_OPTION read_option
,
2174 WS_HEAP
*heap
, void *value
, ULONG size
, const WS_ASYNC_CONTEXT
*ctx
)
2176 struct request_reply
*r
;
2178 if (!(r
= heap_alloc( sizeof(*r
) ))) return E_OUTOFMEMORY
;
2179 r
->task
.proc
= request_reply_proc
;
2180 r
->channel
= channel
;
2181 r
->request
= request
;
2182 r
->request_desc
= request_desc
;
2183 r
->write_option
= write_option
;
2184 r
->request_body
= request_body
;
2185 r
->request_size
= request_size
;
2187 r
->reply_desc
= reply_desc
;
2188 r
->read_option
= read_option
;
2193 return queue_task( &channel
->recv_q
, &r
->task
);
2196 /**************************************************************************
2197 * WsRequestReply [webservices.@]
2199 HRESULT WINAPI
WsRequestReply( WS_CHANNEL
*handle
, WS_MESSAGE
*request
, const WS_MESSAGE_DESCRIPTION
*request_desc
,
2200 WS_WRITE_OPTION write_option
, const void *request_body
, ULONG request_size
,
2201 WS_MESSAGE
*reply
, const WS_MESSAGE_DESCRIPTION
*reply_desc
, WS_READ_OPTION read_option
,
2202 WS_HEAP
*heap
, void *value
, ULONG size
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
2204 struct channel
*channel
= (struct channel
*)handle
;
2207 TRACE( "%p %p %p %08x %p %u %p %p %08x %p %p %u %p %p\n", handle
, request
, request_desc
, write_option
,
2208 request_body
, request_size
, reply
, reply_desc
, read_option
, heap
, value
, size
, ctx
, error
);
2209 if (error
) FIXME( "ignoring error parameter\n" );
2210 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2212 if (!channel
|| !request
|| !reply
) return E_INVALIDARG
;
2214 EnterCriticalSection( &channel
->cs
);
2216 if (channel
->magic
!= CHANNEL_MAGIC
)
2218 LeaveCriticalSection( &channel
->cs
);
2219 return E_INVALIDARG
;
2223 hr
= queue_request_reply( channel
, request
, request_desc
, write_option
, request_body
, request_size
, reply
,
2224 reply_desc
, read_option
, heap
, value
, size
, ctx
);
2226 hr
= request_reply( channel
, request
, request_desc
, write_option
, request_body
, request_size
, reply
,
2227 reply_desc
, read_option
, heap
, value
, size
);
2229 LeaveCriticalSection( &channel
->cs
);
2230 TRACE( "returning %08x\n", hr
);
2234 /**************************************************************************
2235 * WsReadMessageStart [webservices.@]
2237 HRESULT WINAPI
WsReadMessageStart( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2240 struct channel
*channel
= (struct channel
*)handle
;
2243 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2244 if (error
) FIXME( "ignoring error parameter\n" );
2245 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2247 if (!channel
|| !msg
) return E_INVALIDARG
;
2249 EnterCriticalSection( &channel
->cs
);
2251 if (channel
->magic
!= CHANNEL_MAGIC
)
2253 LeaveCriticalSection( &channel
->cs
);
2254 return E_INVALIDARG
;
2257 if ((hr
= receive_message_bytes( channel
, msg
)) == S_OK
)
2259 if ((hr
= init_reader( channel
)) == S_OK
)
2260 hr
= WsReadEnvelopeStart( msg
, channel
->reader
, NULL
, NULL
, NULL
);
2263 LeaveCriticalSection( &channel
->cs
);
2264 TRACE( "returning %08x\n", hr
);
2268 /**************************************************************************
2269 * WsReadMessageEnd [webservices.@]
2271 HRESULT WINAPI
WsReadMessageEnd( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2274 struct channel
*channel
= (struct channel
*)handle
;
2277 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2278 if (error
) FIXME( "ignoring error parameter\n" );
2279 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2281 if (!channel
|| !msg
) return E_INVALIDARG
;
2283 EnterCriticalSection( &channel
->cs
);
2285 if (channel
->magic
!= CHANNEL_MAGIC
)
2287 LeaveCriticalSection( &channel
->cs
);
2288 return E_INVALIDARG
;
2291 hr
= WsReadEnvelopeEnd( msg
, NULL
);
2293 LeaveCriticalSection( &channel
->cs
);
2294 TRACE( "returning %08x\n", hr
);
2298 /**************************************************************************
2299 * WsWriteMessageStart [webservices.@]
2301 HRESULT WINAPI
WsWriteMessageStart( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2304 struct channel
*channel
= (struct channel
*)handle
;
2307 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2308 if (error
) FIXME( "ignoring error parameter\n" );
2309 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2311 if (!channel
|| !msg
) return E_INVALIDARG
;
2313 EnterCriticalSection( &channel
->cs
);
2315 if (channel
->magic
!= CHANNEL_MAGIC
)
2317 LeaveCriticalSection( &channel
->cs
);
2318 return E_INVALIDARG
;
2321 if ((hr
= connect_channel( channel
)) != S_OK
) goto done
;
2322 if ((hr
= init_writer( channel
)) != S_OK
) goto done
;
2323 if ((hr
= WsAddressMessage( msg
, &channel
->addr
, NULL
)) != S_OK
) goto done
;
2324 hr
= WsWriteEnvelopeStart( msg
, channel
->writer
, NULL
, NULL
, NULL
);
2327 LeaveCriticalSection( &channel
->cs
);
2328 TRACE( "returning %08x\n", hr
);
2332 /**************************************************************************
2333 * WsWriteMessageEnd [webservices.@]
2335 HRESULT WINAPI
WsWriteMessageEnd( WS_CHANNEL
*handle
, WS_MESSAGE
*msg
, const WS_ASYNC_CONTEXT
*ctx
,
2338 struct channel
*channel
= (struct channel
*)handle
;
2341 TRACE( "%p %p %p %p\n", handle
, msg
, ctx
, error
);
2342 if (error
) FIXME( "ignoring error parameter\n" );
2343 if (ctx
) FIXME( "ignoring ctx parameter\n" );
2345 if (!channel
|| !msg
) return E_INVALIDARG
;
2347 EnterCriticalSection( &channel
->cs
);
2349 if (channel
->magic
!= CHANNEL_MAGIC
)
2351 LeaveCriticalSection( &channel
->cs
);
2352 return E_INVALIDARG
;
2355 if ((hr
= WsWriteEnvelopeEnd( msg
, NULL
)) == S_OK
&& (hr
= connect_channel( channel
)) == S_OK
)
2356 hr
= send_message( channel
, msg
);
2358 LeaveCriticalSection( &channel
->cs
);
2359 TRACE( "returning %08x\n", hr
);
2363 static void set_blocking( SOCKET socket
, BOOL blocking
)
2365 ULONG state
= !blocking
;
2366 ioctlsocket( socket
, FIONBIO
, &state
);
2369 static HRESULT
sock_accept( SOCKET socket
, HANDLE wait
, HANDLE cancel
, SOCKET
*ret
)
2371 HANDLE handles
[] = { wait
, cancel
};
2374 if (WSAEventSelect( socket
, handles
[0], FD_ACCEPT
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
2376 switch (WSAWaitForMultipleEvents( 2, handles
, FALSE
, WSA_INFINITE
, FALSE
))
2379 if ((*ret
= accept( socket
, NULL
, NULL
)) != -1)
2381 WSAEventSelect( *ret
, NULL
, 0 );
2382 set_blocking( *ret
, TRUE
);
2385 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2389 hr
= WS_E_OPERATION_ABORTED
;
2393 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2400 HRESULT
channel_accept_tcp( SOCKET socket
, HANDLE wait
, HANDLE cancel
, WS_CHANNEL
*handle
)
2402 struct channel
*channel
= (struct channel
*)handle
;
2405 EnterCriticalSection( &channel
->cs
);
2407 if (channel
->magic
!= CHANNEL_MAGIC
)
2409 LeaveCriticalSection( &channel
->cs
);
2410 return E_INVALIDARG
;
2413 hr
= sock_accept( socket
, wait
, cancel
, &channel
->u
.tcp
.socket
);
2415 LeaveCriticalSection( &channel
->cs
);
2419 static HRESULT
sock_wait( SOCKET socket
, HANDLE wait
, HANDLE cancel
)
2421 HANDLE handles
[] = { wait
, cancel
};
2424 if (WSAEventSelect( socket
, handles
[0], FD_READ
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
2426 switch (WSAWaitForMultipleEvents( 2, handles
, FALSE
, WSA_INFINITE
, FALSE
))
2433 hr
= WS_E_OPERATION_ABORTED
;
2437 hr
= HRESULT_FROM_WIN32( WSAGetLastError() );
2441 WSAEventSelect( socket
, NULL
, 0 );
2442 set_blocking( socket
, TRUE
);
2446 HRESULT
channel_accept_udp( SOCKET socket
, HANDLE wait
, HANDLE cancel
, WS_CHANNEL
*handle
)
2448 struct channel
*channel
= (struct channel
*)handle
;
2451 EnterCriticalSection( &channel
->cs
);
2453 if (channel
->magic
!= CHANNEL_MAGIC
)
2455 LeaveCriticalSection( &channel
->cs
);
2456 return E_INVALIDARG
;
2459 if ((hr
= sock_wait( socket
, wait
, cancel
)) == S_OK
) channel
->u
.udp
.socket
= socket
;
2461 LeaveCriticalSection( &channel
->cs
);
2465 /**************************************************************************
2466 * WsAbortChannel [webservices.@]
2468 HRESULT WINAPI
WsAbortChannel( WS_CHANNEL
*handle
, WS_ERROR
*error
)
2470 FIXME( "%p %p: stub!\n", handle
, error
);