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/list.h"
28 #include "webservices_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(webservices
);
32 static const struct prop_desc proxy_props
[] =
34 { sizeof(ULONG
), FALSE
, TRUE
}, /* WS_PROXY_PROPERTY_CALL_TIMEOUT */
35 { sizeof(WS_MESSAGE_PROPERTIES
), FALSE
}, /* WS_PROXY_PROPERTY_MESSAGE_PROPERTIES */
36 { sizeof(USHORT
), FALSE
, TRUE
}, /* WS_PROXY_PROPERTY_MAX_CALL_POOL_SIZE */
37 { sizeof(WS_SERVICE_PROXY_STATE
), TRUE
}, /* WS_PROXY_PROPERTY_STATE */
38 { sizeof(ULONG
), FALSE
, TRUE
}, /* WS_PROXY_PROPERTY_MAX_PENDING_CALLS */
39 { sizeof(ULONG
), FALSE
, TRUE
}, /* WS_PROXY_PROPERTY_MAX_CLOSE_TIMEOUT */
40 { sizeof(LANGID
), FALSE
, TRUE
}, /* WS_PROXY_FAULT_LANG_ID */
47 WS_SERVICE_PROXY_STATE state
;
50 struct prop prop
[sizeof(proxy_props
)/sizeof(proxy_props
[0])];
53 #define PROXY_MAGIC (('P' << 24) | ('R' << 16) | ('O' << 8) | 'X')
55 static struct proxy
*alloc_proxy(void)
57 static const ULONG count
= sizeof(proxy_props
)/sizeof(proxy_props
[0]);
59 ULONG size
= sizeof(*ret
) + prop_size( proxy_props
, count
);
61 if (!(ret
= heap_alloc_zero( size
))) return NULL
;
63 ret
->magic
= PROXY_MAGIC
;
64 InitializeCriticalSection( &ret
->cs
);
65 ret
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": proxy.cs");
67 prop_init( proxy_props
, count
, ret
->prop
, &ret
[1] );
68 ret
->prop_count
= count
;
72 static void reset_proxy( struct proxy
*proxy
)
74 WsResetChannel( proxy
->channel
, NULL
);
75 proxy
->state
= WS_SERVICE_PROXY_STATE_CREATED
;
78 static void free_proxy( struct proxy
*proxy
)
81 WsFreeChannel( proxy
->channel
);
83 proxy
->cs
.DebugInfo
->Spare
[0] = 0;
84 DeleteCriticalSection( &proxy
->cs
);
88 static HRESULT
create_proxy( WS_CHANNEL
*channel
, const WS_PROXY_PROPERTY
*properties
, ULONG count
,
89 WS_SERVICE_PROXY
**handle
)
95 if (!(proxy
= alloc_proxy())) return E_OUTOFMEMORY
;
97 for (i
= 0; i
< count
; i
++)
99 hr
= prop_set( proxy
->prop
, proxy
->prop_count
, properties
[i
].id
, properties
[i
].value
,
100 properties
[i
].valueSize
);
108 proxy
->channel
= channel
;
110 *handle
= (WS_SERVICE_PROXY
*)proxy
;
114 /**************************************************************************
115 * WsCreateServiceProxy [webservices.@]
117 HRESULT WINAPI
WsCreateServiceProxy( const WS_CHANNEL_TYPE type
, const WS_CHANNEL_BINDING binding
,
118 const WS_SECURITY_DESCRIPTION
*desc
,
119 const WS_PROXY_PROPERTY
*proxy_props
, ULONG proxy_props_count
,
120 const WS_CHANNEL_PROPERTY
*channel_props
,
121 const ULONG channel_props_count
, WS_SERVICE_PROXY
**handle
,
127 TRACE( "%u %u %p %p %u %p %u %p %p\n", type
, binding
, desc
, proxy_props
, proxy_props_count
,
128 channel_props
, channel_props_count
, handle
, error
);
129 if (error
) FIXME( "ignoring error parameter\n" );
130 if (desc
) FIXME( "ignoring security description\n" );
132 if (!handle
) return E_INVALIDARG
;
134 if ((hr
= WsCreateChannel( type
, binding
, channel_props
, channel_props_count
, NULL
, &channel
,
135 NULL
)) != S_OK
) return hr
;
137 if ((hr
= create_proxy( channel
, proxy_props
, proxy_props_count
, handle
)) != S_OK
)
139 WsFreeChannel( channel
);
143 TRACE( "created %p\n", *handle
);
147 /**************************************************************************
148 * WsCreateServiceProxyFromTemplate [webservices.@]
150 HRESULT WINAPI
WsCreateServiceProxyFromTemplate( WS_CHANNEL_TYPE channel_type
,
151 const WS_PROXY_PROPERTY
*properties
, const ULONG count
,
152 WS_BINDING_TEMPLATE_TYPE type
, void *value
, ULONG size
,
153 const void *desc
, ULONG desc_size
, WS_SERVICE_PROXY
**handle
,
156 const WS_CHANNEL_PROPERTY
*channel_props
= NULL
;
157 ULONG channel_props_count
= 0;
158 WS_CHANNEL_BINDING binding
;
162 TRACE( "%u %p %u %u %p %u %p %u %p %p\n", channel_type
, properties
, count
, type
, value
, size
, desc
,
163 desc_size
, handle
, error
);
164 if (error
) FIXME( "ignoring error parameter\n" );
166 if (!desc
|| !handle
) return E_INVALIDARG
;
167 FIXME( "ignoring description\n" );
171 case WS_HTTP_BINDING_TEMPLATE_TYPE
:
173 WS_HTTP_BINDING_TEMPLATE
*http
= value
;
176 channel_props
= http
->channelProperties
.properties
;
177 channel_props_count
= http
->channelProperties
.propertyCount
;
179 binding
= WS_HTTP_CHANNEL_BINDING
;
182 case WS_HTTP_SSL_BINDING_TEMPLATE_TYPE
:
184 WS_HTTP_SSL_BINDING_TEMPLATE
*https
= value
;
187 channel_props
= https
->channelProperties
.properties
;
188 channel_props_count
= https
->channelProperties
.propertyCount
;
190 binding
= WS_HTTP_CHANNEL_BINDING
;
194 FIXME( "template type %u not implemented\n", type
);
198 if ((hr
= WsCreateChannel( channel_type
, binding
, channel_props
, channel_props_count
, NULL
,
199 &channel
, NULL
)) != S_OK
) return hr
;
201 if ((hr
= create_proxy( channel
, properties
, count
, handle
)) != S_OK
)
203 WsFreeChannel( channel
);
207 TRACE( "created %p\n", *handle
);
211 /**************************************************************************
212 * WsResetServiceProxy [webservices.@]
214 HRESULT WINAPI
WsResetServiceProxy( WS_SERVICE_PROXY
*handle
, WS_ERROR
*error
)
216 struct proxy
*proxy
= (struct proxy
*)handle
;
218 TRACE( "%p %p\n", handle
, error
);
219 if (error
) FIXME( "ignoring error parameter\n" );
221 if (!proxy
) return E_INVALIDARG
;
223 EnterCriticalSection( &proxy
->cs
);
225 if (proxy
->magic
!= PROXY_MAGIC
)
227 LeaveCriticalSection( &proxy
->cs
);
231 if (proxy
->state
!= WS_SERVICE_PROXY_STATE_CREATED
&& proxy
->state
!= WS_SERVICE_PROXY_STATE_CLOSED
)
233 LeaveCriticalSection( &proxy
->cs
);
234 return WS_E_INVALID_OPERATION
;
237 reset_proxy( proxy
);
239 LeaveCriticalSection( &proxy
->cs
);
243 /**************************************************************************
244 * WsFreeServiceProxy [webservices.@]
246 void WINAPI
WsFreeServiceProxy( WS_SERVICE_PROXY
*handle
)
248 struct proxy
*proxy
= (struct proxy
*)handle
;
250 TRACE( "%p\n", handle
);
254 EnterCriticalSection( &proxy
->cs
);
256 if (proxy
->magic
!= PROXY_MAGIC
)
258 LeaveCriticalSection( &proxy
->cs
);
264 LeaveCriticalSection( &proxy
->cs
);
268 /**************************************************************************
269 * WsGetServiceProxyProperty [webservices.@]
271 HRESULT WINAPI
WsGetServiceProxyProperty( WS_SERVICE_PROXY
*handle
, WS_PROXY_PROPERTY_ID id
,
272 void *buf
, ULONG size
, WS_ERROR
*error
)
274 struct proxy
*proxy
= (struct proxy
*)handle
;
277 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
278 if (error
) FIXME( "ignoring error parameter\n" );
280 if (!proxy
) return E_INVALIDARG
;
282 EnterCriticalSection( &proxy
->cs
);
284 if (proxy
->magic
!= PROXY_MAGIC
)
286 LeaveCriticalSection( &proxy
->cs
);
292 case WS_PROXY_PROPERTY_STATE
:
293 if (!buf
|| size
!= sizeof(proxy
->state
)) hr
= E_INVALIDARG
;
294 else *(WS_SERVICE_PROXY_STATE
*)buf
= proxy
->state
;
298 hr
= prop_get( proxy
->prop
, proxy
->prop_count
, id
, buf
, size
);
301 LeaveCriticalSection( &proxy
->cs
);
305 /**************************************************************************
306 * WsOpenServiceProxy [webservices.@]
308 HRESULT WINAPI
WsOpenServiceProxy( WS_SERVICE_PROXY
*handle
, const WS_ENDPOINT_ADDRESS
*endpoint
,
309 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
311 struct proxy
*proxy
= (struct proxy
*)handle
;
314 TRACE( "%p %p %p %p\n", handle
, endpoint
, ctx
, error
);
315 if (error
) FIXME( "ignoring error parameter\n" );
316 if (ctx
) FIXME( "ignoring ctx parameter\n" );
318 if (!proxy
|| !endpoint
) return E_INVALIDARG
;
320 EnterCriticalSection( &proxy
->cs
);
322 if (proxy
->magic
!= PROXY_MAGIC
)
324 LeaveCriticalSection( &proxy
->cs
);
328 if ((hr
= WsOpenChannel( proxy
->channel
, endpoint
, NULL
, NULL
)) == S_OK
)
329 proxy
->state
= WS_SERVICE_PROXY_STATE_OPEN
;
331 LeaveCriticalSection( &proxy
->cs
);
335 /**************************************************************************
336 * WsCloseServiceProxy [webservices.@]
338 HRESULT WINAPI
WsCloseServiceProxy( WS_SERVICE_PROXY
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
340 struct proxy
*proxy
= (struct proxy
*)handle
;
343 TRACE( "%p %p %p\n", handle
, ctx
, error
);
344 if (error
) FIXME( "ignoring error parameter\n" );
345 if (ctx
) FIXME( "ignoring ctx parameter\n" );
347 if (!proxy
) return E_INVALIDARG
;
349 EnterCriticalSection( &proxy
->cs
);
351 if (proxy
->magic
!= PROXY_MAGIC
)
353 LeaveCriticalSection( &proxy
->cs
);
357 if ((hr
= WsCloseChannel( proxy
->channel
, NULL
, NULL
)) == S_OK
)
358 proxy
->state
= WS_SERVICE_PROXY_STATE_CLOSED
;
360 LeaveCriticalSection( &proxy
->cs
);
364 /**************************************************************************
365 * WsAbortServiceProxy [webservices.@]
367 HRESULT WINAPI
WsAbortServiceProxy( WS_SERVICE_PROXY
*handle
, WS_ERROR
*error
)
369 FIXME( "%p %p\n", handle
, error
);
373 static HRESULT
set_send_context( WS_MESSAGE
*msg
, const WS_CALL_PROPERTY
*props
, ULONG count
)
376 for (i
= 0; i
< count
; i
++)
378 if (props
[i
].id
== WS_CALL_PROPERTY_SEND_MESSAGE_CONTEXT
)
380 if (props
[i
].valueSize
!= sizeof(WS_PROXY_MESSAGE_CALLBACK_CONTEXT
)) return E_INVALIDARG
;
381 message_set_send_context( msg
, props
[i
].value
);
388 static HRESULT
set_receive_context( WS_MESSAGE
*msg
, const WS_CALL_PROPERTY
*props
, ULONG count
)
391 for (i
= 0; i
< count
; i
++)
393 if (props
[i
].id
== WS_CALL_PROPERTY_RECEIVE_MESSAGE_CONTEXT
)
395 if (props
[i
].valueSize
!= sizeof(WS_PROXY_MESSAGE_CALLBACK_CONTEXT
)) return E_INVALIDARG
;
396 message_set_receive_context( msg
, props
[i
].value
);
403 static HRESULT
write_message( WS_MESSAGE
*msg
, WS_XML_WRITER
*writer
, const WS_ELEMENT_DESCRIPTION
*desc
,
404 const WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, const void **args
)
407 message_do_send_callback( msg
);
408 if ((hr
= WsWriteEnvelopeStart( msg
, writer
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
409 if ((hr
= write_input_params( writer
, desc
, params
, count
, args
)) != S_OK
) return hr
;
410 return WsWriteEnvelopeEnd( msg
, NULL
);
413 static HRESULT
set_output( WS_XML_WRITER
*writer
)
415 WS_XML_WRITER_TEXT_ENCODING text
= { {WS_XML_WRITER_ENCODING_TYPE_TEXT
}, WS_CHARSET_UTF8
};
416 WS_XML_WRITER_BUFFER_OUTPUT buf
= { {WS_XML_WRITER_OUTPUT_TYPE_BUFFER
} };
417 return WsSetOutput( writer
, &text
.encoding
, &buf
.output
, NULL
, 0, NULL
);
420 static HRESULT
send_message( WS_CHANNEL
*channel
, WS_MESSAGE
*msg
, WS_MESSAGE_DESCRIPTION
*desc
,
421 WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, const void **args
)
423 WS_XML_WRITER
*writer
;
426 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) return hr
;
427 if ((hr
= WsCreateWriter( NULL
, 0, &writer
, NULL
)) != S_OK
) return hr
;
428 if ((hr
= set_output( writer
)) != S_OK
) goto done
;
429 if ((hr
= write_message( msg
, writer
, desc
->bodyElementDescription
, params
, count
, args
)) != S_OK
) goto done
;
430 hr
= channel_send_message( channel
, msg
);
433 WsFreeWriter( writer
);
437 static HRESULT
read_message( WS_MESSAGE
*msg
, WS_XML_READER
*reader
, WS_HEAP
*heap
,
438 const WS_ELEMENT_DESCRIPTION
*desc
, const WS_PARAMETER_DESCRIPTION
*params
,
439 ULONG count
, const void **args
)
442 if ((hr
= WsReadEnvelopeStart( msg
, reader
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
443 message_do_receive_callback( msg
);
444 if ((hr
= read_output_params( reader
, heap
, desc
, params
, count
, args
)) != S_OK
) return hr
;
445 return WsReadEnvelopeEnd( msg
, NULL
);
448 static HRESULT
receive_message( WS_CHANNEL
*channel
, WS_MESSAGE
*msg
, WS_MESSAGE_DESCRIPTION
*desc
,
449 WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, WS_HEAP
*heap
, const void **args
)
451 WS_XML_READER
*reader
;
454 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) return hr
;
455 if ((hr
= channel_receive_message( channel
)) != S_OK
) return hr
;
456 if ((hr
= channel_get_reader( channel
, &reader
)) != S_OK
) return hr
;
457 return read_message( msg
, reader
, heap
, desc
->bodyElementDescription
, params
, count
, args
);
460 static HRESULT
create_input_message( WS_CHANNEL
*channel
, const WS_CALL_PROPERTY
*properties
,
461 ULONG count
, WS_MESSAGE
**ret
)
466 if ((hr
= WsCreateMessageForChannel( channel
, NULL
, 0, &msg
, NULL
)) != S_OK
) return hr
;
467 if ((hr
= WsInitializeMessage( msg
, WS_REQUEST_MESSAGE
, NULL
, NULL
)) != S_OK
||
468 (hr
= set_send_context( msg
, properties
, count
)) != S_OK
)
470 WsFreeMessage( msg
);
477 static HRESULT
create_output_message( WS_CHANNEL
*channel
, const WS_CALL_PROPERTY
*properties
,
478 ULONG count
, WS_MESSAGE
**ret
)
483 if ((hr
= WsCreateMessageForChannel( channel
, NULL
, 0, &msg
, NULL
)) != S_OK
) return hr
;
484 if ((hr
= set_receive_context( msg
, properties
, count
)) != S_OK
)
486 WsFreeMessage( msg
);
493 /**************************************************************************
494 * WsCall [webservices.@]
496 HRESULT WINAPI
WsCall( WS_SERVICE_PROXY
*handle
, const WS_OPERATION_DESCRIPTION
*desc
, const void **args
,
497 WS_HEAP
*heap
, const WS_CALL_PROPERTY
*properties
, const ULONG count
,
498 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
500 struct proxy
*proxy
= (struct proxy
*)handle
;
501 WS_MESSAGE
*msg
= NULL
;
505 TRACE( "%p %p %p %p %p %u %p %p\n", handle
, desc
, args
, heap
, properties
, count
, ctx
, error
);
506 if (error
) FIXME( "ignoring error parameter\n" );
507 if (ctx
) FIXME( "ignoring ctx parameter\n" );
508 for (i
= 0; i
< count
; i
++)
510 if (properties
[i
].id
!= WS_CALL_PROPERTY_SEND_MESSAGE_CONTEXT
&&
511 properties
[i
].id
!= WS_CALL_PROPERTY_RECEIVE_MESSAGE_CONTEXT
)
513 FIXME( "unimplemented call property %u\n", properties
[i
].id
);
518 if (!proxy
|| !desc
|| (desc
->parameterCount
&& !args
)) return E_INVALIDARG
;
520 EnterCriticalSection( &proxy
->cs
);
522 if (proxy
->magic
!= PROXY_MAGIC
)
524 LeaveCriticalSection( &proxy
->cs
);
528 if ((hr
= create_input_message( proxy
->channel
, properties
, count
, &msg
)) != S_OK
) goto done
;
529 if ((hr
= send_message( proxy
->channel
, msg
, desc
->inputMessageDescription
, desc
->parameterDescription
,
530 desc
->parameterCount
, args
)) != S_OK
) goto done
;
532 WsFreeMessage( msg
);
535 if ((hr
= create_output_message( proxy
->channel
, properties
, count
, &msg
)) != S_OK
) goto done
;
536 hr
= receive_message( proxy
->channel
, msg
, desc
->outputMessageDescription
, desc
->parameterDescription
,
537 desc
->parameterCount
, heap
, args
);
540 WsFreeMessage( msg
);
541 LeaveCriticalSection( &proxy
->cs
);