conhost: Update tty output in fill_output.
[wine.git] / dlls / webservices / proxy.c
blobb6278a6354ca62491fb9be20c83e571ac6bbd58d
1 /*
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
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 */
44 struct proxy
46 ULONG magic;
47 CRITICAL_SECTION cs;
48 WS_SERVICE_PROXY_STATE state;
49 WS_CHANNEL *channel;
50 ULONG prop_count;
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 );
59 struct proxy *ret;
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 );
66 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": proxy.cs");
68 prop_init( proxy_props, count, ret->prop, &ret[1] );
69 ret->prop_count = count;
70 return ret;
73 static void reset_proxy( struct proxy *proxy )
75 WsResetChannel( proxy->channel, NULL );
76 proxy->state = WS_SERVICE_PROXY_STATE_CREATED;
79 static void free_proxy( struct proxy *proxy )
81 reset_proxy( proxy );
82 WsFreeChannel( proxy->channel );
84 proxy->cs.DebugInfo->Spare[0] = 0;
85 DeleteCriticalSection( &proxy->cs );
86 heap_free( proxy );
89 static HRESULT create_proxy( WS_CHANNEL *channel, const WS_PROXY_PROPERTY *properties, ULONG count,
90 WS_SERVICE_PROXY **handle )
92 struct proxy *proxy;
93 HRESULT hr;
94 ULONG i;
96 if (!(proxy = alloc_proxy())) return E_OUTOFMEMORY;
98 for (i = 0; i < count; i++)
100 hr = prop_set( proxy->prop, proxy->prop_count, properties[i].id, properties[i].value,
101 properties[i].valueSize );
102 if (hr != S_OK)
104 free_proxy( proxy );
105 return hr;
109 proxy->channel = channel;
111 *handle = (WS_SERVICE_PROXY *)proxy;
112 return S_OK;
115 /**************************************************************************
116 * WsCreateServiceProxy [webservices.@]
118 HRESULT WINAPI WsCreateServiceProxy( const WS_CHANNEL_TYPE type, const WS_CHANNEL_BINDING binding,
119 const WS_SECURITY_DESCRIPTION *desc,
120 const WS_PROXY_PROPERTY *proxy_props, ULONG proxy_props_count,
121 const WS_CHANNEL_PROPERTY *channel_props,
122 const ULONG channel_props_count, WS_SERVICE_PROXY **handle,
123 WS_ERROR *error )
125 WS_CHANNEL *channel;
126 HRESULT hr;
128 TRACE( "%u %u %p %p %u %p %u %p %p\n", type, binding, desc, proxy_props, proxy_props_count,
129 channel_props, channel_props_count, handle, error );
130 if (error) FIXME( "ignoring error parameter\n" );
131 if (desc) FIXME( "ignoring security description\n" );
133 if (!handle) return E_INVALIDARG;
135 if ((hr = WsCreateChannel( type, binding, channel_props, channel_props_count, NULL, &channel,
136 NULL )) != S_OK) return hr;
138 if ((hr = create_proxy( channel, proxy_props, proxy_props_count, handle )) != S_OK)
140 WsFreeChannel( channel );
141 return hr;
144 TRACE( "created %p\n", *handle );
145 return S_OK;
148 /**************************************************************************
149 * WsCreateServiceProxyFromTemplate [webservices.@]
151 HRESULT WINAPI WsCreateServiceProxyFromTemplate( WS_CHANNEL_TYPE channel_type,
152 const WS_PROXY_PROPERTY *properties, const ULONG count,
153 WS_BINDING_TEMPLATE_TYPE type, void *value, ULONG size,
154 const void *desc, ULONG desc_size, WS_SERVICE_PROXY **handle,
155 WS_ERROR *error )
157 const WS_CHANNEL_PROPERTY *channel_props = NULL;
158 ULONG channel_props_count = 0;
159 WS_CHANNEL_BINDING binding;
160 WS_CHANNEL *channel;
161 HRESULT hr;
163 TRACE( "%u %p %u %u %p %u %p %u %p %p\n", channel_type, properties, count, type, value, size, desc,
164 desc_size, handle, error );
165 if (error) FIXME( "ignoring error parameter\n" );
167 if (!desc || !handle) return E_INVALIDARG;
168 FIXME( "ignoring description\n" );
170 switch (type)
172 case WS_HTTP_BINDING_TEMPLATE_TYPE:
174 WS_HTTP_BINDING_TEMPLATE *http = value;
175 if (http)
177 channel_props = http->channelProperties.properties;
178 channel_props_count = http->channelProperties.propertyCount;
180 binding = WS_HTTP_CHANNEL_BINDING;
181 break;
183 case WS_HTTP_SSL_BINDING_TEMPLATE_TYPE:
185 WS_HTTP_SSL_BINDING_TEMPLATE *https = value;
186 if (https)
188 channel_props = https->channelProperties.properties;
189 channel_props_count = https->channelProperties.propertyCount;
191 binding = WS_HTTP_CHANNEL_BINDING;
192 break;
194 default:
195 FIXME( "template type %u not implemented\n", type );
196 return E_NOTIMPL;
199 if ((hr = WsCreateChannel( channel_type, binding, channel_props, channel_props_count, NULL,
200 &channel, NULL )) != S_OK) return hr;
202 if ((hr = create_proxy( channel, properties, count, handle )) != S_OK)
204 WsFreeChannel( channel );
205 return hr;
208 TRACE( "created %p\n", *handle );
209 return S_OK;
212 /**************************************************************************
213 * WsResetServiceProxy [webservices.@]
215 HRESULT WINAPI WsResetServiceProxy( WS_SERVICE_PROXY *handle, WS_ERROR *error )
217 struct proxy *proxy = (struct proxy *)handle;
218 HRESULT hr = S_OK;
220 TRACE( "%p %p\n", handle, error );
221 if (error) FIXME( "ignoring error parameter\n" );
223 if (!proxy) return E_INVALIDARG;
225 EnterCriticalSection( &proxy->cs );
227 if (proxy->magic != PROXY_MAGIC)
229 LeaveCriticalSection( &proxy->cs );
230 return E_INVALIDARG;
233 if (proxy->state != WS_SERVICE_PROXY_STATE_CREATED && proxy->state != WS_SERVICE_PROXY_STATE_CLOSED)
234 hr = WS_E_INVALID_OPERATION;
235 else
236 reset_proxy( proxy );
238 LeaveCriticalSection( &proxy->cs );
239 TRACE( "returning %08x\n", hr );
240 return hr;
243 /**************************************************************************
244 * WsFreeServiceProxy [webservices.@]
246 void WINAPI WsFreeServiceProxy( WS_SERVICE_PROXY *handle )
248 struct proxy *proxy = (struct proxy *)handle;
250 TRACE( "%p\n", handle );
252 if (!proxy) return;
254 EnterCriticalSection( &proxy->cs );
256 if (proxy->magic != PROXY_MAGIC)
258 LeaveCriticalSection( &proxy->cs );
259 return;
262 proxy->magic = 0;
264 LeaveCriticalSection( &proxy->cs );
265 free_proxy( proxy );
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;
275 HRESULT hr = S_OK;
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 );
287 return E_INVALIDARG;
290 switch (id)
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;
295 break;
297 default:
298 hr = prop_get( proxy->prop, proxy->prop_count, id, buf, size );
301 LeaveCriticalSection( &proxy->cs );
302 TRACE( "returning %08x\n", hr );
303 return hr;
306 /**************************************************************************
307 * WsOpenServiceProxy [webservices.@]
309 HRESULT WINAPI WsOpenServiceProxy( WS_SERVICE_PROXY *handle, const WS_ENDPOINT_ADDRESS *endpoint,
310 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
312 struct proxy *proxy = (struct proxy *)handle;
313 HRESULT hr;
315 TRACE( "%p %p %p %p\n", handle, endpoint, ctx, error );
316 if (error) FIXME( "ignoring error parameter\n" );
317 if (ctx) FIXME( "ignoring ctx parameter\n" );
319 if (!proxy || !endpoint) return E_INVALIDARG;
321 EnterCriticalSection( &proxy->cs );
323 if (proxy->magic != PROXY_MAGIC)
325 LeaveCriticalSection( &proxy->cs );
326 return E_INVALIDARG;
329 if ((hr = WsOpenChannel( proxy->channel, endpoint, NULL, NULL )) == S_OK)
330 proxy->state = WS_SERVICE_PROXY_STATE_OPEN;
332 LeaveCriticalSection( &proxy->cs );
333 TRACE( "returning %08x\n", hr );
334 return hr;
337 /**************************************************************************
338 * WsCloseServiceProxy [webservices.@]
340 HRESULT WINAPI WsCloseServiceProxy( WS_SERVICE_PROXY *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
342 struct proxy *proxy = (struct proxy *)handle;
343 HRESULT hr;
345 TRACE( "%p %p %p\n", handle, ctx, error );
346 if (error) FIXME( "ignoring error parameter\n" );
347 if (ctx) FIXME( "ignoring ctx parameter\n" );
349 if (!proxy) return E_INVALIDARG;
351 EnterCriticalSection( &proxy->cs );
353 if (proxy->magic != PROXY_MAGIC)
355 LeaveCriticalSection( &proxy->cs );
356 return E_INVALIDARG;
359 if ((hr = WsCloseChannel( proxy->channel, NULL, NULL )) == S_OK)
360 proxy->state = WS_SERVICE_PROXY_STATE_CLOSED;
362 LeaveCriticalSection( &proxy->cs );
363 TRACE( "returning %08x\n", hr );
364 return hr;
367 /**************************************************************************
368 * WsAbortServiceProxy [webservices.@]
370 HRESULT WINAPI WsAbortServiceProxy( WS_SERVICE_PROXY *handle, WS_ERROR *error )
372 FIXME( "%p %p\n", handle, error );
373 return E_NOTIMPL;
376 static HRESULT set_send_context( WS_MESSAGE *msg, const WS_CALL_PROPERTY *props, ULONG count )
378 ULONG i;
379 for (i = 0; i < count; i++)
381 if (props[i].id == WS_CALL_PROPERTY_SEND_MESSAGE_CONTEXT)
383 if (props[i].valueSize != sizeof(WS_PROXY_MESSAGE_CALLBACK_CONTEXT)) return E_INVALIDARG;
384 message_set_send_context( msg, props[i].value );
385 break;
388 return S_OK;
391 static HRESULT set_receive_context( WS_MESSAGE *msg, const WS_CALL_PROPERTY *props, ULONG count )
393 ULONG i;
394 for (i = 0; i < count; i++)
396 if (props[i].id == WS_CALL_PROPERTY_RECEIVE_MESSAGE_CONTEXT)
398 if (props[i].valueSize != sizeof(WS_PROXY_MESSAGE_CALLBACK_CONTEXT)) return E_INVALIDARG;
399 message_set_receive_context( msg, props[i].value );
400 break;
403 return S_OK;
406 static HRESULT write_message( WS_MESSAGE *msg, WS_XML_WRITER *writer, const WS_ELEMENT_DESCRIPTION *desc,
407 const WS_PARAMETER_DESCRIPTION *params, ULONG count, const void **args )
409 HRESULT hr;
410 message_do_send_callback( msg );
411 if ((hr = WsWriteEnvelopeStart( msg, writer, NULL, NULL, NULL )) != S_OK) return hr;
412 if ((hr = write_input_params( writer, desc, params, count, args )) != S_OK) return hr;
413 return WsWriteEnvelopeEnd( msg, NULL );
416 static HRESULT set_output( WS_XML_WRITER *writer )
418 WS_XML_WRITER_TEXT_ENCODING text = { {WS_XML_WRITER_ENCODING_TYPE_TEXT}, WS_CHARSET_UTF8 };
419 WS_XML_WRITER_BUFFER_OUTPUT buf = { {WS_XML_WRITER_OUTPUT_TYPE_BUFFER} };
420 return WsSetOutput( writer, &text.encoding, &buf.output, NULL, 0, NULL );
423 static HRESULT send_message( WS_CHANNEL *channel, WS_MESSAGE *msg, WS_MESSAGE_DESCRIPTION *desc,
424 WS_PARAMETER_DESCRIPTION *params, ULONG count, const void **args )
426 WS_XML_WRITER *writer;
427 HRESULT hr;
429 if ((hr = message_set_action( msg, desc->action )) != S_OK) return hr;
430 if ((hr = WsCreateWriter( NULL, 0, &writer, NULL )) != S_OK) return hr;
431 if ((hr = set_output( writer )) != S_OK) goto done;
432 if ((hr = write_message( msg, writer, desc->bodyElementDescription, params, count, args )) != S_OK) goto done;
433 hr = channel_send_message( channel, msg );
435 done:
436 WsFreeWriter( writer );
437 return hr;
440 static HRESULT read_message( WS_MESSAGE *msg, WS_XML_READER *reader, WS_HEAP *heap,
441 const WS_ELEMENT_DESCRIPTION *desc, const WS_PARAMETER_DESCRIPTION *params,
442 ULONG count, const void **args )
444 HRESULT hr;
445 if ((hr = WsReadEnvelopeStart( msg, reader, NULL, NULL, NULL )) != S_OK) return hr;
446 message_do_receive_callback( msg );
447 if ((hr = read_output_params( reader, heap, desc, params, count, args )) != S_OK) return hr;
448 return WsReadEnvelopeEnd( msg, NULL );
451 static HRESULT receive_message( WS_CHANNEL *channel, WS_MESSAGE *msg, WS_MESSAGE_DESCRIPTION *desc,
452 WS_PARAMETER_DESCRIPTION *params, ULONG count, WS_HEAP *heap, const void **args )
454 WS_XML_READER *reader;
455 HRESULT hr;
457 if ((hr = message_set_action( msg, desc->action )) != S_OK) return hr;
458 if ((hr = channel_receive_message( channel, msg )) != S_OK) return hr;
459 if ((hr = channel_get_reader( channel, &reader )) != S_OK) return hr;
460 return read_message( msg, reader, heap, desc->bodyElementDescription, params, count, args );
463 static HRESULT create_input_message( WS_CHANNEL *channel, const WS_CALL_PROPERTY *properties,
464 ULONG count, WS_MESSAGE **ret )
466 WS_MESSAGE *msg;
467 HRESULT hr;
469 if ((hr = WsCreateMessageForChannel( channel, NULL, 0, &msg, NULL )) != S_OK) return hr;
470 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK ||
471 (hr = set_send_context( msg, properties, count )) != S_OK)
473 WsFreeMessage( msg );
474 return hr;
476 *ret = msg;
477 return S_OK;
480 static HRESULT create_output_message( WS_CHANNEL *channel, const WS_CALL_PROPERTY *properties,
481 ULONG count, WS_MESSAGE **ret )
483 WS_MESSAGE *msg;
484 HRESULT hr;
486 if ((hr = WsCreateMessageForChannel( channel, NULL, 0, &msg, NULL )) != S_OK) return hr;
487 if ((hr = set_receive_context( msg, properties, count )) != S_OK)
489 WsFreeMessage( msg );
490 return hr;
492 *ret = msg;
493 return S_OK;
496 /**************************************************************************
497 * WsCall [webservices.@]
499 HRESULT WINAPI WsCall( WS_SERVICE_PROXY *handle, const WS_OPERATION_DESCRIPTION *desc, const void **args,
500 WS_HEAP *heap, const WS_CALL_PROPERTY *properties, const ULONG count,
501 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
503 struct proxy *proxy = (struct proxy *)handle;
504 WS_MESSAGE *msg = NULL;
505 HRESULT hr;
506 ULONG i;
508 TRACE( "%p %p %p %p %p %u %p %p\n", handle, desc, args, heap, properties, count, ctx, error );
509 if (error) FIXME( "ignoring error parameter\n" );
510 if (ctx) FIXME( "ignoring ctx parameter\n" );
511 for (i = 0; i < count; i++)
513 if (properties[i].id != WS_CALL_PROPERTY_SEND_MESSAGE_CONTEXT &&
514 properties[i].id != WS_CALL_PROPERTY_RECEIVE_MESSAGE_CONTEXT)
516 FIXME( "unimplemented call property %u\n", properties[i].id );
517 return E_NOTIMPL;
521 if (!proxy || !desc || (desc->parameterCount && !args)) return E_INVALIDARG;
523 EnterCriticalSection( &proxy->cs );
525 if (proxy->magic != PROXY_MAGIC)
527 LeaveCriticalSection( &proxy->cs );
528 return E_INVALIDARG;
531 if ((hr = create_input_message( proxy->channel, properties, count, &msg )) != S_OK) goto done;
532 if ((hr = send_message( proxy->channel, msg, desc->inputMessageDescription, desc->parameterDescription,
533 desc->parameterCount, args )) != S_OK) goto done;
535 WsFreeMessage( msg );
536 msg = NULL;
538 if ((hr = create_output_message( proxy->channel, properties, count, &msg )) != S_OK) goto done;
539 hr = receive_message( proxy->channel, msg, desc->outputMessageDescription, desc->parameterDescription,
540 desc->parameterCount, heap, args );
542 done:
543 WsFreeMessage( msg );
544 LeaveCriticalSection( &proxy->cs );
545 TRACE( "returning %08x\n", hr );
546 return hr;