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
24 #include "webservices.h"
26 #include "wine/debug.h"
27 #include "wine/heap.h"
28 #include "wine/list.h"
29 #include "webservices_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(webservices
);
33 static const struct prop_desc proxy_props
[] =
35 { sizeof(ULONG
), FALSE
, TRUE
}, /* WS_PROXY_PROPERTY_CALL_TIMEOUT */
36 { sizeof(WS_MESSAGE_PROPERTIES
), FALSE
}, /* WS_PROXY_PROPERTY_MESSAGE_PROPERTIES */
37 { sizeof(USHORT
), FALSE
, TRUE
}, /* WS_PROXY_PROPERTY_MAX_CALL_POOL_SIZE */
38 { sizeof(WS_SERVICE_PROXY_STATE
), TRUE
}, /* WS_PROXY_PROPERTY_STATE */
39 { sizeof(ULONG
), FALSE
, TRUE
}, /* WS_PROXY_PROPERTY_MAX_PENDING_CALLS */
40 { sizeof(ULONG
), FALSE
, TRUE
}, /* WS_PROXY_PROPERTY_MAX_CLOSE_TIMEOUT */
41 { sizeof(LANGID
), FALSE
, TRUE
}, /* WS_PROXY_FAULT_LANG_ID */
48 WS_SERVICE_PROXY_STATE state
;
51 struct prop prop
[ARRAY_SIZE( proxy_props
)];
54 #define PROXY_MAGIC (('P' << 24) | ('R' << 16) | ('O' << 8) | 'X')
56 static struct proxy
*alloc_proxy(void)
58 static const ULONG count
= ARRAY_SIZE( proxy_props
);
60 ULONG size
= sizeof(*ret
) + prop_size( proxy_props
, count
);
62 if (!(ret
= heap_alloc_zero( size
))) return NULL
;
64 ret
->magic
= PROXY_MAGIC
;
65 InitializeCriticalSection( &ret
->cs
);
67 ret
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": proxy.cs");
70 prop_init( proxy_props
, count
, ret
->prop
, &ret
[1] );
71 ret
->prop_count
= count
;
75 static void reset_proxy( struct proxy
*proxy
)
77 WsResetChannel( proxy
->channel
, NULL
);
78 proxy
->state
= WS_SERVICE_PROXY_STATE_CREATED
;
81 static void free_proxy( struct proxy
*proxy
)
84 WsFreeChannel( proxy
->channel
);
87 proxy
->cs
.DebugInfo
->Spare
[0] = 0;
89 DeleteCriticalSection( &proxy
->cs
);
93 static HRESULT
create_proxy( WS_CHANNEL
*channel
, const WS_PROXY_PROPERTY
*properties
, ULONG count
,
94 WS_SERVICE_PROXY
**handle
)
100 if (!(proxy
= alloc_proxy())) return E_OUTOFMEMORY
;
102 for (i
= 0; i
< count
; i
++)
104 hr
= prop_set( proxy
->prop
, proxy
->prop_count
, properties
[i
].id
, properties
[i
].value
,
105 properties
[i
].valueSize
);
113 proxy
->channel
= channel
;
115 *handle
= (WS_SERVICE_PROXY
*)proxy
;
119 /**************************************************************************
120 * WsCreateServiceProxy [webservices.@]
122 HRESULT WINAPI
WsCreateServiceProxy( const WS_CHANNEL_TYPE type
, const WS_CHANNEL_BINDING binding
,
123 const WS_SECURITY_DESCRIPTION
*desc
,
124 const WS_PROXY_PROPERTY
*proxy_props
, ULONG proxy_props_count
,
125 const WS_CHANNEL_PROPERTY
*channel_props
,
126 const ULONG channel_props_count
, WS_SERVICE_PROXY
**handle
,
132 TRACE( "%u %u %p %p %u %p %u %p %p\n", type
, binding
, desc
, proxy_props
, proxy_props_count
,
133 channel_props
, channel_props_count
, handle
, error
);
134 if (error
) FIXME( "ignoring error parameter\n" );
135 if (desc
) FIXME( "ignoring security description\n" );
137 if (!handle
) return E_INVALIDARG
;
139 if ((hr
= WsCreateChannel( type
, binding
, channel_props
, channel_props_count
, NULL
, &channel
,
140 NULL
)) != S_OK
) return hr
;
142 if ((hr
= create_proxy( channel
, proxy_props
, proxy_props_count
, handle
)) != S_OK
)
144 WsFreeChannel( channel
);
148 TRACE( "created %p\n", *handle
);
152 /**************************************************************************
153 * WsCreateServiceProxyFromTemplate [webservices.@]
155 HRESULT WINAPI
WsCreateServiceProxyFromTemplate( WS_CHANNEL_TYPE channel_type
,
156 const WS_PROXY_PROPERTY
*properties
, const ULONG count
,
157 WS_BINDING_TEMPLATE_TYPE type
, void *value
, ULONG size
,
158 const void *desc
, ULONG desc_size
, WS_SERVICE_PROXY
**handle
,
161 const WS_CHANNEL_PROPERTY
*channel_props
= NULL
;
162 ULONG channel_props_count
= 0;
163 WS_CHANNEL_BINDING binding
;
167 TRACE( "%u %p %u %u %p %u %p %u %p %p\n", channel_type
, properties
, count
, type
, value
, size
, desc
,
168 desc_size
, handle
, error
);
169 if (error
) FIXME( "ignoring error parameter\n" );
171 if (!desc
|| !handle
) return E_INVALIDARG
;
172 FIXME( "ignoring description\n" );
176 case WS_HTTP_BINDING_TEMPLATE_TYPE
:
178 WS_HTTP_BINDING_TEMPLATE
*http
= value
;
181 channel_props
= http
->channelProperties
.properties
;
182 channel_props_count
= http
->channelProperties
.propertyCount
;
184 binding
= WS_HTTP_CHANNEL_BINDING
;
187 case WS_HTTP_SSL_BINDING_TEMPLATE_TYPE
:
189 WS_HTTP_SSL_BINDING_TEMPLATE
*https
= value
;
192 channel_props
= https
->channelProperties
.properties
;
193 channel_props_count
= https
->channelProperties
.propertyCount
;
195 binding
= WS_HTTP_CHANNEL_BINDING
;
199 FIXME( "template type %u not implemented\n", type
);
203 if ((hr
= WsCreateChannel( channel_type
, binding
, channel_props
, channel_props_count
, NULL
,
204 &channel
, NULL
)) != S_OK
) return hr
;
206 if ((hr
= create_proxy( channel
, properties
, count
, handle
)) != S_OK
)
208 WsFreeChannel( channel
);
212 TRACE( "created %p\n", *handle
);
216 /**************************************************************************
217 * WsResetServiceProxy [webservices.@]
219 HRESULT WINAPI
WsResetServiceProxy( WS_SERVICE_PROXY
*handle
, WS_ERROR
*error
)
221 struct proxy
*proxy
= (struct proxy
*)handle
;
224 TRACE( "%p %p\n", handle
, error
);
225 if (error
) FIXME( "ignoring error parameter\n" );
227 if (!proxy
) return E_INVALIDARG
;
229 EnterCriticalSection( &proxy
->cs
);
231 if (proxy
->magic
!= PROXY_MAGIC
)
233 LeaveCriticalSection( &proxy
->cs
);
237 if (proxy
->state
!= WS_SERVICE_PROXY_STATE_CREATED
&& proxy
->state
!= WS_SERVICE_PROXY_STATE_CLOSED
)
238 hr
= WS_E_INVALID_OPERATION
;
240 reset_proxy( proxy
);
242 LeaveCriticalSection( &proxy
->cs
);
243 TRACE( "returning %08x\n", hr
);
247 /**************************************************************************
248 * WsFreeServiceProxy [webservices.@]
250 void WINAPI
WsFreeServiceProxy( WS_SERVICE_PROXY
*handle
)
252 struct proxy
*proxy
= (struct proxy
*)handle
;
254 TRACE( "%p\n", handle
);
258 EnterCriticalSection( &proxy
->cs
);
260 if (proxy
->magic
!= PROXY_MAGIC
)
262 LeaveCriticalSection( &proxy
->cs
);
268 LeaveCriticalSection( &proxy
->cs
);
272 /**************************************************************************
273 * WsGetServiceProxyProperty [webservices.@]
275 HRESULT WINAPI
WsGetServiceProxyProperty( WS_SERVICE_PROXY
*handle
, WS_PROXY_PROPERTY_ID id
,
276 void *buf
, ULONG size
, WS_ERROR
*error
)
278 struct proxy
*proxy
= (struct proxy
*)handle
;
281 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
282 if (error
) FIXME( "ignoring error parameter\n" );
284 if (!proxy
) return E_INVALIDARG
;
286 EnterCriticalSection( &proxy
->cs
);
288 if (proxy
->magic
!= PROXY_MAGIC
)
290 LeaveCriticalSection( &proxy
->cs
);
296 case WS_PROXY_PROPERTY_STATE
:
297 if (!buf
|| size
!= sizeof(proxy
->state
)) hr
= E_INVALIDARG
;
298 else *(WS_SERVICE_PROXY_STATE
*)buf
= proxy
->state
;
302 hr
= prop_get( proxy
->prop
, proxy
->prop_count
, id
, buf
, size
);
305 LeaveCriticalSection( &proxy
->cs
);
306 TRACE( "returning %08x\n", hr
);
310 /**************************************************************************
311 * WsOpenServiceProxy [webservices.@]
313 HRESULT WINAPI
WsOpenServiceProxy( WS_SERVICE_PROXY
*handle
, const WS_ENDPOINT_ADDRESS
*endpoint
,
314 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
316 struct proxy
*proxy
= (struct proxy
*)handle
;
319 TRACE( "%p %p %p %p\n", handle
, endpoint
, ctx
, error
);
320 if (error
) FIXME( "ignoring error parameter\n" );
321 if (ctx
) FIXME( "ignoring ctx parameter\n" );
323 if (!proxy
|| !endpoint
) return E_INVALIDARG
;
325 EnterCriticalSection( &proxy
->cs
);
327 if (proxy
->magic
!= PROXY_MAGIC
)
329 LeaveCriticalSection( &proxy
->cs
);
333 if ((hr
= WsOpenChannel( proxy
->channel
, endpoint
, NULL
, NULL
)) == S_OK
)
334 proxy
->state
= WS_SERVICE_PROXY_STATE_OPEN
;
336 LeaveCriticalSection( &proxy
->cs
);
337 TRACE( "returning %08x\n", hr
);
341 /**************************************************************************
342 * WsCloseServiceProxy [webservices.@]
344 HRESULT WINAPI
WsCloseServiceProxy( WS_SERVICE_PROXY
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
346 struct proxy
*proxy
= (struct proxy
*)handle
;
349 TRACE( "%p %p %p\n", handle
, ctx
, error
);
350 if (error
) FIXME( "ignoring error parameter\n" );
351 if (ctx
) FIXME( "ignoring ctx parameter\n" );
353 if (!proxy
) return E_INVALIDARG
;
355 EnterCriticalSection( &proxy
->cs
);
357 if (proxy
->magic
!= PROXY_MAGIC
)
359 LeaveCriticalSection( &proxy
->cs
);
363 if ((hr
= WsCloseChannel( proxy
->channel
, NULL
, NULL
)) == S_OK
)
364 proxy
->state
= WS_SERVICE_PROXY_STATE_CLOSED
;
366 LeaveCriticalSection( &proxy
->cs
);
367 TRACE( "returning %08x\n", hr
);
371 /**************************************************************************
372 * WsAbortServiceProxy [webservices.@]
374 HRESULT WINAPI
WsAbortServiceProxy( WS_SERVICE_PROXY
*handle
, WS_ERROR
*error
)
376 FIXME( "%p %p\n", handle
, error
);
380 static HRESULT
set_send_context( WS_MESSAGE
*msg
, const WS_CALL_PROPERTY
*props
, ULONG count
)
383 for (i
= 0; i
< count
; i
++)
385 if (props
[i
].id
== WS_CALL_PROPERTY_SEND_MESSAGE_CONTEXT
)
387 if (props
[i
].valueSize
!= sizeof(WS_PROXY_MESSAGE_CALLBACK_CONTEXT
)) return E_INVALIDARG
;
388 message_set_send_context( msg
, props
[i
].value
);
395 static HRESULT
set_receive_context( WS_MESSAGE
*msg
, const WS_CALL_PROPERTY
*props
, ULONG count
)
398 for (i
= 0; i
< count
; i
++)
400 if (props
[i
].id
== WS_CALL_PROPERTY_RECEIVE_MESSAGE_CONTEXT
)
402 if (props
[i
].valueSize
!= sizeof(WS_PROXY_MESSAGE_CALLBACK_CONTEXT
)) return E_INVALIDARG
;
403 message_set_receive_context( msg
, props
[i
].value
);
410 static HRESULT
write_message( WS_MESSAGE
*msg
, WS_XML_WRITER
*writer
, const WS_ELEMENT_DESCRIPTION
*desc
,
411 const WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, const void **args
)
414 message_do_send_callback( msg
);
415 if ((hr
= WsWriteEnvelopeStart( msg
, writer
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
416 if ((hr
= write_input_params( writer
, desc
, params
, count
, args
)) != S_OK
) return hr
;
417 return WsWriteEnvelopeEnd( msg
, NULL
);
420 static HRESULT
set_output( WS_XML_WRITER
*writer
)
422 WS_XML_WRITER_TEXT_ENCODING text
= { {WS_XML_WRITER_ENCODING_TYPE_TEXT
}, WS_CHARSET_UTF8
};
423 WS_XML_WRITER_BUFFER_OUTPUT buf
= { {WS_XML_WRITER_OUTPUT_TYPE_BUFFER
} };
424 return WsSetOutput( writer
, &text
.encoding
, &buf
.output
, NULL
, 0, NULL
);
427 static HRESULT
send_message( WS_CHANNEL
*channel
, WS_MESSAGE
*msg
, WS_MESSAGE_DESCRIPTION
*desc
,
428 WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, const void **args
)
430 WS_XML_WRITER
*writer
;
433 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) return hr
;
434 if ((hr
= WsCreateWriter( NULL
, 0, &writer
, NULL
)) != S_OK
) return hr
;
435 if ((hr
= set_output( writer
)) != S_OK
) goto done
;
436 if ((hr
= write_message( msg
, writer
, desc
->bodyElementDescription
, params
, count
, args
)) != S_OK
) goto done
;
437 hr
= channel_send_message( channel
, msg
);
440 WsFreeWriter( writer
);
444 static HRESULT
read_message( WS_MESSAGE
*msg
, WS_XML_READER
*reader
, WS_HEAP
*heap
,
445 const WS_ELEMENT_DESCRIPTION
*desc
, const WS_PARAMETER_DESCRIPTION
*params
,
446 ULONG count
, const void **args
)
449 if ((hr
= WsReadEnvelopeStart( msg
, reader
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
450 message_do_receive_callback( msg
);
451 if ((hr
= read_output_params( reader
, heap
, desc
, params
, count
, args
)) != S_OK
) return hr
;
452 return WsReadEnvelopeEnd( msg
, NULL
);
455 static HRESULT
receive_message( WS_CHANNEL
*channel
, WS_MESSAGE
*msg
, WS_MESSAGE_DESCRIPTION
*desc
,
456 WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, WS_HEAP
*heap
, const void **args
)
458 WS_XML_READER
*reader
;
461 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) return hr
;
462 if ((hr
= channel_receive_message( channel
)) != S_OK
) return hr
;
463 if ((hr
= channel_get_reader( channel
, &reader
)) != S_OK
) return hr
;
464 return read_message( msg
, reader
, heap
, desc
->bodyElementDescription
, params
, count
, args
);
467 static HRESULT
create_input_message( WS_CHANNEL
*channel
, const WS_CALL_PROPERTY
*properties
,
468 ULONG count
, WS_MESSAGE
**ret
)
473 if ((hr
= WsCreateMessageForChannel( channel
, NULL
, 0, &msg
, NULL
)) != S_OK
) return hr
;
474 if ((hr
= WsInitializeMessage( msg
, WS_REQUEST_MESSAGE
, NULL
, NULL
)) != S_OK
||
475 (hr
= set_send_context( msg
, properties
, count
)) != S_OK
)
477 WsFreeMessage( msg
);
484 static HRESULT
create_output_message( WS_CHANNEL
*channel
, const WS_CALL_PROPERTY
*properties
,
485 ULONG count
, WS_MESSAGE
**ret
)
490 if ((hr
= WsCreateMessageForChannel( channel
, NULL
, 0, &msg
, NULL
)) != S_OK
) return hr
;
491 if ((hr
= set_receive_context( msg
, properties
, count
)) != S_OK
)
493 WsFreeMessage( msg
);
500 /**************************************************************************
501 * WsCall [webservices.@]
503 HRESULT WINAPI
WsCall( WS_SERVICE_PROXY
*handle
, const WS_OPERATION_DESCRIPTION
*desc
, const void **args
,
504 WS_HEAP
*heap
, const WS_CALL_PROPERTY
*properties
, const ULONG count
,
505 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
507 struct proxy
*proxy
= (struct proxy
*)handle
;
508 WS_MESSAGE
*msg
= NULL
;
512 TRACE( "%p %p %p %p %p %u %p %p\n", handle
, desc
, args
, heap
, properties
, count
, ctx
, error
);
513 if (error
) FIXME( "ignoring error parameter\n" );
514 if (ctx
) FIXME( "ignoring ctx parameter\n" );
515 for (i
= 0; i
< count
; i
++)
517 if (properties
[i
].id
!= WS_CALL_PROPERTY_SEND_MESSAGE_CONTEXT
&&
518 properties
[i
].id
!= WS_CALL_PROPERTY_RECEIVE_MESSAGE_CONTEXT
)
520 FIXME( "unimplemented call property %u\n", properties
[i
].id
);
525 if (!proxy
|| !desc
|| (desc
->parameterCount
&& !args
)) return E_INVALIDARG
;
527 EnterCriticalSection( &proxy
->cs
);
529 if (proxy
->magic
!= PROXY_MAGIC
)
531 LeaveCriticalSection( &proxy
->cs
);
535 if ((hr
= create_input_message( proxy
->channel
, properties
, count
, &msg
)) != S_OK
) goto done
;
536 if ((hr
= send_message( proxy
->channel
, msg
, desc
->inputMessageDescription
, desc
->parameterDescription
,
537 desc
->parameterCount
, args
)) != S_OK
) goto done
;
539 WsFreeMessage( msg
);
542 if ((hr
= create_output_message( proxy
->channel
, properties
, count
, &msg
)) != S_OK
) goto done
;
543 hr
= receive_message( proxy
->channel
, msg
, desc
->outputMessageDescription
, desc
->parameterDescription
,
544 desc
->parameterCount
, heap
, args
);
547 WsFreeMessage( msg
);
548 LeaveCriticalSection( &proxy
->cs
);
549 TRACE( "returning %08x\n", hr
);