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
)
138 WsFreeChannel( channel
);
143 /**************************************************************************
144 * WsCreateServiceProxyFromTemplate [webservices.@]
146 HRESULT WINAPI
WsCreateServiceProxyFromTemplate( WS_CHANNEL_TYPE channel_type
,
147 const WS_PROXY_PROPERTY
*properties
, const ULONG count
,
148 WS_BINDING_TEMPLATE_TYPE type
, void *value
, ULONG size
,
149 const void *desc
, ULONG desc_size
, WS_SERVICE_PROXY
**handle
,
152 const WS_CHANNEL_PROPERTY
*channel_props
= NULL
;
153 ULONG channel_props_count
= 0;
154 WS_CHANNEL_BINDING binding
;
158 TRACE( "%u %p %u %u %p %u %p %u %p %p\n", channel_type
, properties
, count
, type
, value
, size
, desc
,
159 desc_size
, handle
, error
);
160 if (error
) FIXME( "ignoring error parameter\n" );
162 if (!desc
|| !handle
) return E_INVALIDARG
;
163 FIXME( "ignoring description\n" );
167 case WS_HTTP_BINDING_TEMPLATE_TYPE
:
169 WS_HTTP_BINDING_TEMPLATE
*http
= value
;
172 channel_props
= http
->channelProperties
.properties
;
173 channel_props_count
= http
->channelProperties
.propertyCount
;
175 binding
= WS_HTTP_CHANNEL_BINDING
;
178 case WS_HTTP_SSL_BINDING_TEMPLATE_TYPE
:
180 WS_HTTP_SSL_BINDING_TEMPLATE
*https
= value
;
183 channel_props
= https
->channelProperties
.properties
;
184 channel_props_count
= https
->channelProperties
.propertyCount
;
186 binding
= WS_HTTP_CHANNEL_BINDING
;
190 FIXME( "template type %u not implemented\n", type
);
194 if ((hr
= WsCreateChannel( channel_type
, binding
, channel_props
, channel_props_count
, NULL
,
195 &channel
, NULL
)) != S_OK
) return hr
;
197 if ((hr
= create_proxy( channel
, properties
, count
, handle
)) != S_OK
) WsFreeChannel( channel
);
201 /**************************************************************************
202 * WsResetServiceProxy [webservices.@]
204 HRESULT WINAPI
WsResetServiceProxy( WS_SERVICE_PROXY
*handle
, WS_ERROR
*error
)
206 struct proxy
*proxy
= (struct proxy
*)handle
;
208 TRACE( "%p %p\n", handle
, error
);
209 if (error
) FIXME( "ignoring error parameter\n" );
211 if (!proxy
) return E_INVALIDARG
;
213 EnterCriticalSection( &proxy
->cs
);
215 if (proxy
->magic
!= PROXY_MAGIC
)
217 LeaveCriticalSection( &proxy
->cs
);
221 if (proxy
->state
!= WS_SERVICE_PROXY_STATE_CREATED
&& proxy
->state
!= WS_SERVICE_PROXY_STATE_CLOSED
)
223 LeaveCriticalSection( &proxy
->cs
);
224 return WS_E_INVALID_OPERATION
;
227 reset_proxy( proxy
);
229 LeaveCriticalSection( &proxy
->cs
);
233 /**************************************************************************
234 * WsFreeServiceProxy [webservices.@]
236 void WINAPI
WsFreeServiceProxy( WS_SERVICE_PROXY
*handle
)
238 struct proxy
*proxy
= (struct proxy
*)handle
;
240 TRACE( "%p\n", handle
);
244 EnterCriticalSection( &proxy
->cs
);
246 if (proxy
->magic
!= PROXY_MAGIC
)
248 LeaveCriticalSection( &proxy
->cs
);
254 LeaveCriticalSection( &proxy
->cs
);
258 /**************************************************************************
259 * WsGetServiceProxyProperty [webservices.@]
261 HRESULT WINAPI
WsGetServiceProxyProperty( WS_SERVICE_PROXY
*handle
, WS_PROXY_PROPERTY_ID id
,
262 void *buf
, ULONG size
, WS_ERROR
*error
)
264 struct proxy
*proxy
= (struct proxy
*)handle
;
267 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
268 if (error
) FIXME( "ignoring error parameter\n" );
270 if (!proxy
) return E_INVALIDARG
;
272 EnterCriticalSection( &proxy
->cs
);
274 if (proxy
->magic
!= PROXY_MAGIC
)
276 LeaveCriticalSection( &proxy
->cs
);
282 case WS_PROXY_PROPERTY_STATE
:
283 if (!buf
|| size
!= sizeof(proxy
->state
)) hr
= E_INVALIDARG
;
284 else *(WS_SERVICE_PROXY_STATE
*)buf
= proxy
->state
;
288 hr
= prop_get( proxy
->prop
, proxy
->prop_count
, id
, buf
, size
);
291 LeaveCriticalSection( &proxy
->cs
);
295 /**************************************************************************
296 * WsOpenServiceProxy [webservices.@]
298 HRESULT WINAPI
WsOpenServiceProxy( WS_SERVICE_PROXY
*handle
, const WS_ENDPOINT_ADDRESS
*endpoint
,
299 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
301 struct proxy
*proxy
= (struct proxy
*)handle
;
304 TRACE( "%p %p %p %p\n", handle
, endpoint
, ctx
, error
);
305 if (error
) FIXME( "ignoring error parameter\n" );
306 if (ctx
) FIXME( "ignoring ctx parameter\n" );
308 if (!proxy
|| !endpoint
) return E_INVALIDARG
;
310 EnterCriticalSection( &proxy
->cs
);
312 if (proxy
->magic
!= PROXY_MAGIC
)
314 LeaveCriticalSection( &proxy
->cs
);
318 if ((hr
= WsOpenChannel( proxy
->channel
, endpoint
, NULL
, NULL
)) == S_OK
)
319 proxy
->state
= WS_SERVICE_PROXY_STATE_OPEN
;
321 LeaveCriticalSection( &proxy
->cs
);
325 /**************************************************************************
326 * WsCloseServiceProxy [webservices.@]
328 HRESULT WINAPI
WsCloseServiceProxy( WS_SERVICE_PROXY
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
330 struct proxy
*proxy
= (struct proxy
*)handle
;
333 TRACE( "%p %p %p\n", handle
, ctx
, error
);
334 if (error
) FIXME( "ignoring error parameter\n" );
335 if (ctx
) FIXME( "ignoring ctx parameter\n" );
337 if (!proxy
) return E_INVALIDARG
;
339 EnterCriticalSection( &proxy
->cs
);
341 if (proxy
->magic
!= PROXY_MAGIC
)
343 LeaveCriticalSection( &proxy
->cs
);
347 if ((hr
= WsCloseChannel( proxy
->channel
, NULL
, NULL
)) == S_OK
)
348 proxy
->state
= WS_SERVICE_PROXY_STATE_CLOSED
;
350 LeaveCriticalSection( &proxy
->cs
);
354 /**************************************************************************
355 * WsAbortServiceProxy [webservices.@]
357 HRESULT WINAPI
WsAbortServiceProxy( WS_SERVICE_PROXY
*handle
, WS_ERROR
*error
)
359 FIXME( "%p %p\n", handle
, error
);
363 static HRESULT
set_send_context( WS_MESSAGE
*msg
, const WS_CALL_PROPERTY
*props
, ULONG count
)
366 for (i
= 0; i
< count
; i
++)
368 if (props
[i
].id
== WS_CALL_PROPERTY_SEND_MESSAGE_CONTEXT
)
370 if (props
[i
].valueSize
!= sizeof(WS_PROXY_MESSAGE_CALLBACK_CONTEXT
)) return E_INVALIDARG
;
371 message_set_send_context( msg
, props
[i
].value
);
378 static HRESULT
set_receive_context( WS_MESSAGE
*msg
, const WS_CALL_PROPERTY
*props
, ULONG count
)
381 for (i
= 0; i
< count
; i
++)
383 if (props
[i
].id
== WS_CALL_PROPERTY_RECEIVE_MESSAGE_CONTEXT
)
385 if (props
[i
].valueSize
!= sizeof(WS_PROXY_MESSAGE_CALLBACK_CONTEXT
)) return E_INVALIDARG
;
386 message_set_receive_context( msg
, props
[i
].value
);
393 static HRESULT
write_message( WS_MESSAGE
*msg
, WS_XML_WRITER
*writer
, const WS_ELEMENT_DESCRIPTION
*desc
,
394 const WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, const void **args
)
397 message_do_send_callback( msg
);
398 if ((hr
= WsWriteEnvelopeStart( msg
, writer
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
399 if ((hr
= write_input_params( writer
, desc
, params
, count
, args
)) != S_OK
) return hr
;
400 return WsWriteEnvelopeEnd( msg
, NULL
);
403 static HRESULT
set_output( WS_XML_WRITER
*writer
)
405 WS_XML_WRITER_TEXT_ENCODING text
= { {WS_XML_WRITER_ENCODING_TYPE_TEXT
}, WS_CHARSET_UTF8
};
406 WS_XML_WRITER_BUFFER_OUTPUT buf
= { {WS_XML_WRITER_OUTPUT_TYPE_BUFFER
} };
407 return WsSetOutput( writer
, &text
.encoding
, &buf
.output
, NULL
, 0, NULL
);
410 static HRESULT
send_message( WS_CHANNEL
*channel
, WS_MESSAGE
*msg
, WS_MESSAGE_DESCRIPTION
*desc
,
411 WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, const void **args
)
413 WS_XML_WRITER
*writer
;
416 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) return hr
;
417 if ((hr
= WsCreateWriter( NULL
, 0, &writer
, NULL
)) != S_OK
) return hr
;
418 if ((hr
= set_output( writer
)) != S_OK
) goto done
;
419 if ((hr
= write_message( msg
, writer
, desc
->bodyElementDescription
, params
, count
, args
)) != S_OK
) goto done
;
420 hr
= channel_send_message( channel
, msg
);
423 WsFreeWriter( writer
);
427 static HRESULT
read_message( WS_MESSAGE
*msg
, WS_XML_READER
*reader
, WS_HEAP
*heap
,
428 const WS_ELEMENT_DESCRIPTION
*desc
, const WS_PARAMETER_DESCRIPTION
*params
,
429 ULONG count
, const void **args
)
432 if ((hr
= WsReadEnvelopeStart( msg
, reader
, NULL
, NULL
, NULL
)) != S_OK
) return hr
;
433 message_do_receive_callback( msg
);
434 if ((hr
= read_output_params( reader
, heap
, desc
, params
, count
, args
)) != S_OK
) return hr
;
435 return WsReadEnvelopeEnd( msg
, NULL
);
438 static HRESULT
receive_message( WS_CHANNEL
*channel
, WS_MESSAGE
*msg
, WS_MESSAGE_DESCRIPTION
*desc
,
439 WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, WS_HEAP
*heap
, const void **args
)
441 WS_XML_READER
*reader
;
444 if ((hr
= message_set_action( msg
, desc
->action
)) != S_OK
) return hr
;
445 if ((hr
= channel_receive_message( channel
)) != S_OK
) return hr
;
446 if ((hr
= channel_get_reader( channel
, &reader
)) != S_OK
) return hr
;
447 return read_message( msg
, reader
, heap
, desc
->bodyElementDescription
, params
, count
, args
);
450 static HRESULT
create_input_message( WS_CHANNEL
*channel
, const WS_CALL_PROPERTY
*properties
,
451 ULONG count
, WS_MESSAGE
**ret
)
456 if ((hr
= WsCreateMessageForChannel( channel
, NULL
, 0, &msg
, NULL
)) != S_OK
) return hr
;
457 if ((hr
= WsInitializeMessage( msg
, WS_REQUEST_MESSAGE
, NULL
, NULL
)) != S_OK
||
458 (hr
= set_send_context( msg
, properties
, count
)) != S_OK
)
460 WsFreeMessage( msg
);
467 static HRESULT
create_output_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
= set_receive_context( msg
, properties
, count
)) != S_OK
)
476 WsFreeMessage( msg
);
483 /**************************************************************************
484 * WsCall [webservices.@]
486 HRESULT WINAPI
WsCall( WS_SERVICE_PROXY
*handle
, const WS_OPERATION_DESCRIPTION
*desc
, const void **args
,
487 WS_HEAP
*heap
, const WS_CALL_PROPERTY
*properties
, const ULONG count
,
488 const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
490 struct proxy
*proxy
= (struct proxy
*)handle
;
491 WS_MESSAGE
*msg
= NULL
;
495 TRACE( "%p %p %p %p %p %u %p %p\n", handle
, desc
, args
, heap
, properties
, count
, ctx
, error
);
496 if (error
) FIXME( "ignoring error parameter\n" );
497 if (ctx
) FIXME( "ignoring ctx parameter\n" );
498 for (i
= 0; i
< count
; i
++)
500 if (properties
[i
].id
!= WS_CALL_PROPERTY_SEND_MESSAGE_CONTEXT
&&
501 properties
[i
].id
!= WS_CALL_PROPERTY_RECEIVE_MESSAGE_CONTEXT
)
503 FIXME( "unimplemented call property %u\n", properties
[i
].id
);
508 if (!proxy
|| !desc
|| (desc
->parameterCount
&& !args
)) return E_INVALIDARG
;
510 EnterCriticalSection( &proxy
->cs
);
512 if (proxy
->magic
!= PROXY_MAGIC
)
514 LeaveCriticalSection( &proxy
->cs
);
518 if ((hr
= create_input_message( proxy
->channel
, properties
, count
, &msg
)) != S_OK
) goto done
;
519 if ((hr
= send_message( proxy
->channel
, msg
, desc
->inputMessageDescription
, desc
->parameterDescription
,
520 desc
->parameterCount
, args
)) != S_OK
) goto done
;
522 WsFreeMessage( msg
);
525 if ((hr
= create_output_message( proxy
->channel
, properties
, count
, &msg
)) != S_OK
) goto done
;
526 hr
= receive_message( proxy
->channel
, msg
, desc
->outputMessageDescription
, desc
->parameterDescription
,
527 desc
->parameterCount
, heap
, args
);
530 WsFreeMessage( msg
);
531 LeaveCriticalSection( &proxy
->cs
);