d3dcompiler_43: Remove DECLSPEC_HIDDEN usage.
[wine.git] / dlls / webservices / msg.c
blob7f6cd028163005979ea99a5697d8fb0a2009d8d9
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>
20 #include <stdlib.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "rpc.h"
26 #include "webservices.h"
28 #include "wine/debug.h"
29 #include "wine/list.h"
30 #include "webservices_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
34 static const struct prop_desc msg_props[] =
36 { sizeof(WS_MESSAGE_STATE), TRUE }, /* WS_MESSAGE_PROPERTY_STATE */
37 { sizeof(WS_HEAP *), TRUE }, /* WS_MESSAGE_PROPERTY_HEAP */
38 { sizeof(WS_ENVELOPE_VERSION), TRUE }, /* WS_MESSAGE_PROPERTY_ENVELOPE_VERSION */
39 { sizeof(WS_ADDRESSING_VERSION), TRUE }, /* WS_MESSAGE_PROPERTY_ADDRESSING_VERSION */
40 { sizeof(WS_XML_BUFFER *), TRUE }, /* WS_MESSAGE_PROPERTY_HEADER_BUFFER */
41 { sizeof(WS_XML_NODE_POSITION *), TRUE }, /* WS_MESSAGE_PROPERTY_HEADER_POSITION */
42 { sizeof(WS_XML_READER *), TRUE }, /* WS_MESSAGE_PROPERTY_BODY_READER */
43 { sizeof(WS_XML_WRITER *), TRUE }, /* WS_MESSAGE_PROPERTY_BODY_WRITER */
44 { sizeof(BOOL), TRUE }, /* WS_MESSAGE_PROPERTY_IS_ADDRESSED */
45 { sizeof(WS_HEAP_PROPERTIES), TRUE }, /* WS_MESSAGE_PROPERTY_HEAP_PROPERTIES */
46 { sizeof(WS_XML_READER_PROPERTIES), TRUE }, /* WS_MESSAGE_PROPERTY_XML_READER_PROPERTIES */
47 { sizeof(WS_XML_WRITER_PROPERTIES), TRUE }, /* WS_MESSAGE_PROPERTY_XML_WRITER_PROPERTIES */
48 { sizeof(BOOL), FALSE }, /* WS_MESSAGE_PROPERTY_IS_FAULT */
51 struct header
53 WS_HEADER_TYPE type;
54 BOOL mapped;
55 WS_XML_STRING name;
56 WS_XML_STRING ns;
57 union
59 WS_XML_BUFFER *buf;
60 WS_XML_STRING *text;
61 } u;
64 struct msg
66 ULONG magic;
67 CRITICAL_SECTION cs;
68 WS_MESSAGE_INITIALIZATION init;
69 WS_MESSAGE_STATE state;
70 GUID id;
71 GUID id_req;
72 WS_ENVELOPE_VERSION version_env;
73 WS_ADDRESSING_VERSION version_addr;
74 BOOL is_addressed;
75 WS_STRING addr;
76 WS_XML_STRING *action;
77 WS_HEAP *heap;
78 WS_XML_BUFFER *buf;
79 WS_XML_WRITER *writer;
80 WS_XML_WRITER *writer_body;
81 WS_XML_READER *reader;
82 WS_XML_READER *reader_body;
83 ULONG header_count;
84 ULONG header_size;
85 struct header **header;
86 WS_PROXY_MESSAGE_CALLBACK_CONTEXT ctx_send;
87 WS_PROXY_MESSAGE_CALLBACK_CONTEXT ctx_receive;
88 ULONG prop_count;
89 struct prop prop[ARRAY_SIZE( msg_props )];
92 #define MSG_MAGIC (('M' << 24) | ('E' << 16) | ('S' << 8) | 'S')
93 #define HEADER_ARRAY_SIZE 2
95 static struct msg *alloc_msg(void)
97 static const ULONG count = ARRAY_SIZE( msg_props );
98 struct msg *ret;
99 ULONG size = sizeof(*ret) + prop_size( msg_props, count );
101 if (!(ret = calloc( 1, size ))) return NULL;
102 if (!(ret->header = malloc( HEADER_ARRAY_SIZE * sizeof(struct header *) )))
104 free( ret );
105 return NULL;
107 ret->magic = MSG_MAGIC;
108 ret->state = WS_MESSAGE_STATE_EMPTY;
109 ret->header_size = HEADER_ARRAY_SIZE;
111 InitializeCriticalSection( &ret->cs );
112 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": msg.cs");
114 prop_init( msg_props, count, ret->prop, &ret[1] );
115 ret->prop_count = count;
116 return ret;
119 static void free_header( struct header *header )
121 free( header->name.bytes );
122 free( header->ns.bytes );
123 if (header->mapped) free_xml_string( header->u.text );
124 free( header );
127 static void reset_msg( struct msg *msg )
129 BOOL isfault = FALSE;
130 ULONG i;
132 msg->state = WS_MESSAGE_STATE_EMPTY;
133 msg->init = 0;
134 UuidCreate( &msg->id );
135 memset( &msg->id_req, 0, sizeof(msg->id_req) );
136 msg->is_addressed = FALSE;
137 free( msg->addr.chars );
138 msg->addr.chars = NULL;
139 msg->addr.length = 0;
141 free_xml_string( msg->action );
142 msg->action = NULL;
144 WsResetHeap( msg->heap, NULL );
145 msg->buf = NULL; /* allocated on msg->heap */
146 msg->writer_body = NULL; /* owned by caller */
147 msg->reader_body = NULL; /* owned by caller */
149 for (i = 0; i < msg->header_count; i++)
151 free_header( msg->header[i] );
152 msg->header[i] = NULL;
154 msg->header_count = 0;
156 memset( &msg->ctx_send, 0, sizeof(msg->ctx_send) );
157 memset( &msg->ctx_receive, 0, sizeof(msg->ctx_receive) );
159 prop_set( msg->prop, msg->prop_count, WS_MESSAGE_PROPERTY_IS_FAULT, &isfault, sizeof(isfault) );
162 static void free_msg( struct msg *msg )
164 reset_msg( msg );
166 WsFreeWriter( msg->writer );
167 WsFreeReader( msg->reader );
168 WsFreeHeap( msg->heap );
169 free( msg->header );
171 msg->cs.DebugInfo->Spare[0] = 0;
172 DeleteCriticalSection( &msg->cs );
173 free( msg );
176 #define HEAP_MAX_SIZE (1 << 16)
177 static HRESULT create_msg( WS_ENVELOPE_VERSION env_version, WS_ADDRESSING_VERSION addr_version,
178 const WS_MESSAGE_PROPERTY *properties, ULONG count, WS_MESSAGE **handle )
180 struct msg *msg;
181 HRESULT hr;
182 ULONG i;
184 if (!(msg = alloc_msg())) return E_OUTOFMEMORY;
186 for (i = 0; i < count; i++)
188 if (properties[i].id == WS_MESSAGE_PROPERTY_ENVELOPE_VERSION ||
189 properties[i].id == WS_MESSAGE_PROPERTY_ADDRESSING_VERSION)
191 free_msg( msg );
192 return E_INVALIDARG;
194 hr = prop_set( msg->prop, msg->prop_count, properties[i].id, properties[i].value,
195 properties[i].valueSize );
196 if (hr != S_OK)
198 free_msg( msg );
199 return hr;
203 if ((hr = WsCreateHeap( HEAP_MAX_SIZE, 0, NULL, 0, &msg->heap, NULL )) != S_OK)
205 free_msg( msg );
206 return hr;
209 UuidCreate( &msg->id );
210 msg->version_env = env_version;
211 msg->version_addr = addr_version;
213 *handle = (WS_MESSAGE *)msg;
214 return S_OK;
217 /**************************************************************************
218 * WsCreateMessage [webservices.@]
220 HRESULT WINAPI WsCreateMessage( WS_ENVELOPE_VERSION env_version, WS_ADDRESSING_VERSION addr_version,
221 const WS_MESSAGE_PROPERTY *properties, ULONG count, WS_MESSAGE **handle,
222 WS_ERROR *error )
224 HRESULT hr;
226 TRACE( "%u %u %p %lu %p %p\n", env_version, addr_version, properties, count, handle, error );
227 if (error) FIXME( "ignoring error parameter\n" );
229 if (!handle || !env_version || !addr_version ||
230 (env_version == WS_ENVELOPE_VERSION_NONE && addr_version != WS_ADDRESSING_VERSION_TRANSPORT))
232 return E_INVALIDARG;
235 if ((hr = create_msg( env_version, addr_version, properties, count, handle )) != S_OK) return hr;
236 TRACE( "created %p\n", *handle );
237 return S_OK;
240 /**************************************************************************
241 * WsCreateMessageForChannel [webservices.@]
243 HRESULT WINAPI WsCreateMessageForChannel( WS_CHANNEL *channel_handle, const WS_MESSAGE_PROPERTY *properties,
244 ULONG count, WS_MESSAGE **handle, WS_ERROR *error )
246 WS_ENVELOPE_VERSION version_env;
247 WS_ADDRESSING_VERSION version_addr;
248 HRESULT hr;
250 TRACE( "%p %p %lu %p %p\n", channel_handle, properties, count, handle, error );
251 if (error) FIXME( "ignoring error parameter\n" );
253 if (!channel_handle || !handle) return E_INVALIDARG;
255 if ((hr = WsGetChannelProperty( channel_handle, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version_env,
256 sizeof(version_env), NULL )) != S_OK || !version_env)
257 version_env = WS_ENVELOPE_VERSION_SOAP_1_2;
259 if ((hr = WsGetChannelProperty( channel_handle, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION, &version_addr,
260 sizeof(version_addr), NULL )) != S_OK || !version_addr)
261 version_addr = WS_ADDRESSING_VERSION_1_0;
263 if ((hr = create_msg( version_env, version_addr, properties, count, handle )) != S_OK) return hr;
264 TRACE( "created %p\n", *handle );
265 return S_OK;
268 /**************************************************************************
269 * WsFreeMessage [webservices.@]
271 void WINAPI WsFreeMessage( WS_MESSAGE *handle )
273 struct msg *msg = (struct msg *)handle;
275 TRACE( "%p\n", handle );
277 if (!msg) return;
279 EnterCriticalSection( &msg->cs );
281 if (msg->magic != MSG_MAGIC)
283 LeaveCriticalSection( &msg->cs );
284 return;
287 msg->magic = 0;
289 LeaveCriticalSection( &msg->cs );
290 free_msg( msg );
293 /**************************************************************************
294 * WsResetMessage [webservices.@]
296 HRESULT WINAPI WsResetMessage( WS_MESSAGE *handle, WS_ERROR *error )
298 struct msg *msg = (struct msg *)handle;
299 HRESULT hr = S_OK;
301 TRACE( "%p %p\n", handle, error );
302 if (error) FIXME( "ignoring error parameter\n" );
304 if (!msg) return E_INVALIDARG;
306 EnterCriticalSection( &msg->cs );
308 if (msg->magic != MSG_MAGIC)
310 LeaveCriticalSection( &msg->cs );
311 return E_INVALIDARG;
314 reset_msg( msg );
316 LeaveCriticalSection( &msg->cs );
317 TRACE( "returning %#lx\n", hr );
318 return hr;
321 /**************************************************************************
322 * WsGetMessageProperty [webservices.@]
324 HRESULT WINAPI WsGetMessageProperty( WS_MESSAGE *handle, WS_MESSAGE_PROPERTY_ID id, void *buf,
325 ULONG size, WS_ERROR *error )
327 struct msg *msg = (struct msg *)handle;
328 HRESULT hr = S_OK;
330 TRACE( "%p %u %p %lu %p\n", handle, id, buf, size, error );
331 if (error) FIXME( "ignoring error parameter\n" );
333 if (!msg) return E_INVALIDARG;
335 EnterCriticalSection( &msg->cs );
337 if (msg->magic != MSG_MAGIC)
339 LeaveCriticalSection( &msg->cs );
340 return E_INVALIDARG;
343 switch (id)
345 case WS_MESSAGE_PROPERTY_STATE:
346 if (!buf || size != sizeof(msg->state)) hr = E_INVALIDARG;
347 else *(WS_MESSAGE_STATE *)buf = msg->state;
348 break;
350 case WS_MESSAGE_PROPERTY_HEAP:
351 if (!buf || size != sizeof(msg->heap)) hr = E_INVALIDARG;
352 else *(WS_HEAP **)buf = msg->heap;
353 break;
355 case WS_MESSAGE_PROPERTY_ENVELOPE_VERSION:
356 if (!buf || size != sizeof(msg->version_env)) hr = E_INVALIDARG;
357 else *(WS_ENVELOPE_VERSION *)buf = msg->version_env;
358 break;
360 case WS_MESSAGE_PROPERTY_ADDRESSING_VERSION:
361 if (!buf || size != sizeof(msg->version_addr)) hr = E_INVALIDARG;
362 else *(WS_ADDRESSING_VERSION *)buf = msg->version_addr;
363 break;
365 case WS_MESSAGE_PROPERTY_HEADER_BUFFER:
366 if (!buf || size != sizeof(msg->buf)) hr = E_INVALIDARG;
367 else *(WS_XML_BUFFER **)buf = msg->buf;
368 break;
370 case WS_MESSAGE_PROPERTY_BODY_READER:
371 if (!buf || size != sizeof(msg->reader_body)) hr = E_INVALIDARG;
372 else *(WS_XML_READER **)buf = msg->reader_body;
373 break;
375 case WS_MESSAGE_PROPERTY_BODY_WRITER:
376 if (!buf || size != sizeof(msg->writer_body)) hr = E_INVALIDARG;
377 else *(WS_XML_WRITER **)buf = msg->writer_body;
378 break;
380 case WS_MESSAGE_PROPERTY_IS_ADDRESSED:
381 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
382 else *(BOOL *)buf = msg->is_addressed;
383 break;
385 case WS_MESSAGE_PROPERTY_HEAP_PROPERTIES:
386 case WS_MESSAGE_PROPERTY_XML_READER_PROPERTIES:
387 case WS_MESSAGE_PROPERTY_XML_WRITER_PROPERTIES:
388 FIXME( "property %u not supported\n", id );
389 hr = E_NOTIMPL;
390 break;
392 default:
393 hr = prop_get( msg->prop, msg->prop_count, id, buf, size );
396 LeaveCriticalSection( &msg->cs );
397 TRACE( "returning %#lx\n", hr );
398 return hr;
401 /**************************************************************************
402 * WsSetMessageProperty [webservices.@]
404 HRESULT WINAPI WsSetMessageProperty( WS_MESSAGE *handle, WS_MESSAGE_PROPERTY_ID id, const void *value,
405 ULONG size, WS_ERROR *error )
407 struct msg *msg = (struct msg *)handle;
408 HRESULT hr;
410 TRACE( "%p %u %p %lu %p\n", handle, id, value, size, error );
411 if (error) FIXME( "ignoring error parameter\n" );
413 if (!msg) return E_INVALIDARG;
415 EnterCriticalSection( &msg->cs );
417 if (msg->magic != MSG_MAGIC)
419 LeaveCriticalSection( &msg->cs );
420 return E_INVALIDARG;
423 switch (id)
425 case WS_MESSAGE_PROPERTY_STATE:
426 case WS_MESSAGE_PROPERTY_ENVELOPE_VERSION:
427 case WS_MESSAGE_PROPERTY_ADDRESSING_VERSION:
428 case WS_MESSAGE_PROPERTY_IS_ADDRESSED:
429 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
430 else hr = E_INVALIDARG;
431 break;
433 default:
434 hr = prop_set( msg->prop, msg->prop_count, id, value, size );
437 LeaveCriticalSection( &msg->cs );
438 TRACE( "returning %#lx\n", hr );
439 return hr;
442 /**************************************************************************
443 * WsAddressMessage [webservices.@]
445 HRESULT WINAPI WsAddressMessage( WS_MESSAGE *handle, const WS_ENDPOINT_ADDRESS *addr, WS_ERROR *error )
447 struct msg *msg = (struct msg *)handle;
448 HRESULT hr = S_OK;
450 TRACE( "%p %p %p\n", handle, addr, error );
451 if (error) FIXME( "ignoring error parameter\n" );
452 if (addr && (addr->headers || addr->extensions || addr->identity))
454 FIXME( "headers, extensions or identity not supported\n" );
455 return E_NOTIMPL;
458 if (!msg) return E_INVALIDARG;
460 EnterCriticalSection( &msg->cs );
462 if (msg->magic != MSG_MAGIC)
464 LeaveCriticalSection( &msg->cs );
465 return E_INVALIDARG;
468 if (msg->state < WS_MESSAGE_STATE_INITIALIZED || msg->is_addressed) hr = WS_E_INVALID_OPERATION;
469 else if (addr && addr->url.length)
471 if (!(msg->addr.chars = malloc( addr->url.length * sizeof(WCHAR) ))) hr = E_OUTOFMEMORY;
472 else
474 memcpy( msg->addr.chars, addr->url.chars, addr->url.length * sizeof(WCHAR) );
475 msg->addr.length = addr->url.length;
478 if (hr == S_OK) msg->is_addressed = TRUE;
480 LeaveCriticalSection( &msg->cs );
481 TRACE( "returning %#lx\n", hr );
482 return hr;
485 static const WS_XML_STRING *get_env_namespace( WS_ENVELOPE_VERSION version )
487 static const WS_XML_STRING namespaces[] =
489 {41, (BYTE *)"http://schemas.xmlsoap.org/soap/envelope/"},
490 {39, (BYTE *)"http://www.w3.org/2003/05/soap-envelope"},
491 {0, NULL},
494 if (version < WS_ENVELOPE_VERSION_SOAP_1_1 || version > WS_ENVELOPE_VERSION_NONE)
496 ERR( "unknown version %u\n", version );
497 return NULL;
500 return &namespaces[version - 1];
503 static const WS_XML_STRING *get_addr_namespace( WS_ADDRESSING_VERSION version )
505 static const WS_XML_STRING namespaces[] =
507 {48, (BYTE *)"http://schemas.xmlsoap.org/ws/2004/08/addressing"},
508 {36, (BYTE *)"http://www.w3.org/2005/08/addressing"},
509 {55, (BYTE *)"http://schemas.microsoft.com/ws/2005/05/addressing/none"},
512 if (version < WS_ADDRESSING_VERSION_0_9 || version > WS_ADDRESSING_VERSION_TRANSPORT)
514 ERR( "unknown version %u\n", version );
515 return NULL;
518 return &namespaces[version - 1];
521 static const WS_XML_STRING *get_header_name( WS_HEADER_TYPE type )
523 static const WS_XML_STRING headers[] =
525 {6, (BYTE *)"Action"},
526 {2, (BYTE *)"To"},
527 {9, (BYTE *)"MessageID"},
528 {9, (BYTE *)"RelatesTo"},
529 {4, (BYTE *)"From"},
530 {7, (BYTE *)"ReplyTo"},
531 {7, (BYTE *)"FaultTo"},
534 if (type < WS_ACTION_HEADER || type > WS_FAULT_TO_HEADER)
536 ERR( "unknown type %u\n", type );
537 return NULL;
540 return &headers[type - 1];
543 static HRESULT write_must_understand( WS_XML_WRITER *writer, const WS_XML_STRING *prefix, const WS_XML_STRING *ns )
545 static const WS_XML_STRING understand = {14, (BYTE *)"mustUnderstand"};
546 WS_XML_INT32_TEXT one = {{WS_XML_TEXT_TYPE_INT32}, 1};
547 HRESULT hr;
549 if ((hr = WsWriteStartAttribute( writer, prefix, &understand, ns, FALSE, NULL )) != S_OK) return hr;
550 if ((hr = WsWriteText( writer, &one.text, NULL )) != S_OK) return hr;
551 return WsWriteEndAttribute( writer, NULL );
554 static HRESULT write_action_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
555 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
556 const WS_XML_STRING *ns_addr, const WS_XML_STRING *text )
558 WS_XML_UTF8_TEXT utf8 = {{WS_XML_TEXT_TYPE_UTF8}};
559 const WS_XML_STRING *action = get_header_name( WS_ACTION_HEADER );
560 HRESULT hr;
562 if (!text || !text->length) return S_OK;
563 utf8.value.length = text->length;
564 utf8.value.bytes = text->bytes;
566 if ((hr = WsWriteStartElement( writer, prefix_addr, action, ns_addr, NULL )) != S_OK) return hr;
567 if ((hr = write_must_understand( writer, prefix_env, ns_env )) != S_OK) return hr;
568 if ((hr = WsWriteText( writer, &utf8.text, NULL )) != S_OK) return hr;
569 return WsWriteEndElement( writer, NULL ); /* </a:Action> */
572 static HRESULT write_to_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
573 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
574 const WS_XML_STRING *ns_addr, const WS_STRING *addr )
576 WS_XML_UTF16_TEXT utf16 = {{WS_XML_TEXT_TYPE_UTF16}, (BYTE *)addr->chars, addr->length * sizeof(WCHAR)};
577 const WS_XML_STRING *to = get_header_name( WS_TO_HEADER );
578 HRESULT hr;
580 if ((hr = WsWriteStartElement( writer, prefix_addr, to, ns_addr, NULL )) != S_OK) return hr;
581 if ((hr = write_must_understand( writer, prefix_env, ns_env )) != S_OK) return hr;
582 if ((hr = WsWriteText( writer, &utf16.text, NULL )) != S_OK) return hr;
583 return WsWriteEndElement( writer, NULL ); /* </a:To> */
586 static HRESULT write_replyto_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
587 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
588 const WS_XML_STRING *ns_addr )
590 static const char anonymous[] = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
591 WS_XML_UTF8_TEXT utf8 = {{WS_XML_TEXT_TYPE_UTF8}, {sizeof(anonymous) - 1, (BYTE *)anonymous}};
592 const WS_XML_STRING address = {7, (BYTE *)"Address"}, *replyto = get_header_name( WS_REPLY_TO_HEADER );
593 HRESULT hr;
595 if ((hr = WsWriteStartElement( writer, prefix_addr, replyto, ns_addr, NULL )) != S_OK) return hr;
596 if ((hr = WsWriteStartElement( writer, prefix_addr, &address, ns_addr, NULL )) != S_OK) return hr;
597 if ((hr = WsWriteText( writer, &utf8.text, NULL )) != S_OK) return hr;
598 if ((hr = WsWriteEndElement( writer, NULL )) != S_OK) return hr; /* </a:Address> */
599 return WsWriteEndElement( writer, NULL ); /* </a:ReplyTo> */
602 static HRESULT write_msgid_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
603 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
604 const WS_XML_STRING *ns_addr, const GUID *guid )
606 WS_XML_UNIQUE_ID_TEXT id = {{WS_XML_TEXT_TYPE_UNIQUE_ID}, *guid};
607 const WS_XML_STRING *msgid = get_header_name( WS_MESSAGE_ID_HEADER );
608 HRESULT hr;
610 if ((hr = WsWriteStartElement( writer, prefix_addr, msgid, ns_addr, NULL )) != S_OK) return hr;
611 if ((hr = WsWriteText( writer, &id.text, NULL )) != S_OK) return hr;
612 return WsWriteEndElement( writer, NULL ); /* </a:MessageID> */
615 static HRESULT write_relatesto_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
616 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
617 const WS_XML_STRING *ns_addr, const GUID *guid )
619 WS_XML_UNIQUE_ID_TEXT id = {{WS_XML_TEXT_TYPE_UNIQUE_ID}, *guid};
620 const WS_XML_STRING *relatesto = get_header_name( WS_RELATES_TO_HEADER );
621 HRESULT hr;
623 if ((hr = WsWriteStartElement( writer, prefix_addr, relatesto, ns_addr, NULL )) != S_OK) return hr;
624 if ((hr = WsWriteText( writer, &id.text, NULL )) != S_OK) return hr;
625 return WsWriteEndElement( writer, NULL ); /* </a:RelatesTo> */
628 static HRESULT write_headers( struct msg *msg, WS_MESSAGE_INITIALIZATION init, WS_XML_WRITER *writer,
629 const WS_XML_STRING *prefix_env, const WS_XML_STRING *ns_env,
630 const WS_XML_STRING *prefix_addr, const WS_XML_STRING *ns_addr )
632 static const WS_XML_STRING header = {6, (BYTE *)"Header"};
633 HRESULT hr;
634 ULONG i;
636 if ((hr = WsWriteXmlnsAttribute( writer, prefix_addr, ns_addr, FALSE, NULL )) != S_OK) return hr;
637 if ((hr = WsWriteStartElement( writer, prefix_env, &header, ns_env, NULL )) != S_OK) return hr;
639 if ((hr = write_action_header( writer, prefix_env, ns_env, prefix_addr, ns_addr, msg->action )) != S_OK)
640 return hr;
642 if (init == WS_REPLY_MESSAGE || init == WS_FAULT_MESSAGE)
644 if ((hr = write_relatesto_header( writer, prefix_env, ns_env, prefix_addr, ns_addr, &msg->id_req )) != S_OK)
645 return hr;
647 else
649 if (init == WS_REQUEST_MESSAGE)
651 if ((hr = write_msgid_header(writer, prefix_env, ns_env, prefix_addr, ns_addr, &msg->id)) != S_OK)
652 return hr;
653 if (msg->version_addr == WS_ADDRESSING_VERSION_0_9 &&
654 (hr = write_replyto_header(writer, prefix_env, ns_env, prefix_addr, ns_addr)) != S_OK)
655 return hr;
658 if (msg->addr.length &&
659 (hr = write_to_header(writer, prefix_env, ns_env, prefix_addr, ns_addr, &msg->addr)) != S_OK)
660 return hr;
663 for (i = 0; i < msg->header_count; i++)
665 if (msg->header[i]->mapped) continue;
666 if ((hr = WsWriteXmlBuffer( writer, msg->header[i]->u.buf, NULL )) != S_OK) return hr;
669 return WsWriteEndElement( writer, NULL ); /* </s:Header> */
672 static ULONG count_envelope_headers( struct msg *msg )
674 ULONG i, ret = 0;
675 for (i = 0; i < msg->header_count; i++)
677 if (!msg->header[i]->mapped) ret++;
679 return ret;
682 static HRESULT write_headers_transport( struct msg *msg, WS_XML_WRITER *writer, const WS_XML_STRING *prefix,
683 const WS_XML_STRING *ns )
685 static const WS_XML_STRING header = {6, (BYTE *)"Header"};
686 HRESULT hr = S_OK;
687 ULONG i;
689 if (count_envelope_headers( msg ) || !msg->action)
691 if ((hr = WsWriteStartElement( writer, prefix, &header, ns, NULL )) != S_OK) return hr;
693 for (i = 0; i < msg->header_count; i++)
695 if (msg->header[i]->mapped) continue;
696 if ((hr = WsWriteXmlBuffer( writer, msg->header[i]->u.buf, NULL )) != S_OK) return hr;
699 hr = WsWriteEndElement( writer, NULL ); /* </s:Header> */
702 return hr;
705 static HRESULT write_envelope_start( struct msg *msg, WS_MESSAGE_INITIALIZATION init, WS_XML_WRITER *writer )
707 static const WS_XML_STRING envelope = {8, (BYTE *)"Envelope"}, body = {4, (BYTE *)"Body"};
708 static const WS_XML_STRING prefix_s = {1, (BYTE *)"s"}, prefix_a = {1, (BYTE *)"a"};
709 const WS_XML_STRING *prefix_env = (msg->version_env == WS_ENVELOPE_VERSION_NONE) ? NULL : &prefix_s;
710 const WS_XML_STRING *prefix_addr = (msg->version_addr == WS_ADDRESSING_VERSION_TRANSPORT) ? NULL : &prefix_a;
711 const WS_XML_STRING *ns_env = get_env_namespace( msg->version_env );
712 const WS_XML_STRING *ns_addr = get_addr_namespace( msg->version_addr );
713 HRESULT hr;
715 if ((hr = WsWriteStartElement( writer, prefix_env, &envelope, ns_env, NULL )) != S_OK) return hr;
717 if (msg->version_addr == WS_ADDRESSING_VERSION_TRANSPORT)
718 hr = write_headers_transport( msg, writer, prefix_env, ns_env );
719 else
720 hr = write_headers( msg, init, writer, prefix_env, ns_env, prefix_addr, ns_addr );
721 if (hr != S_OK) return hr;
723 return WsWriteStartElement( writer, prefix_env, &body, ns_env, NULL ); /* <s:Body> */
726 static HRESULT write_envelope_end( WS_XML_WRITER *writer )
728 HRESULT hr;
729 if ((hr = WsWriteEndElement( writer, NULL )) != S_OK) return hr; /* </s:Body> */
730 return WsWriteEndElement( writer, NULL ); /* </s:Envelope> */
733 static HRESULT write_envelope( struct msg *msg, WS_MESSAGE_INITIALIZATION init )
735 HRESULT hr;
736 if (!msg->writer && (hr = WsCreateWriter( NULL, 0, &msg->writer, NULL )) != S_OK) return hr;
737 if (!msg->buf && (hr = WsCreateXmlBuffer( msg->heap, NULL, 0, &msg->buf, NULL )) != S_OK) return hr;
738 if ((hr = WsSetOutputToBuffer( msg->writer, msg->buf, NULL, 0, NULL )) != S_OK) return hr;
739 if ((hr = write_envelope_start( msg, init, msg->writer )) != S_OK) return hr;
740 return write_envelope_end( msg->writer );
743 /**************************************************************************
744 * WsWriteEnvelopeStart [webservices.@]
746 HRESULT WINAPI WsWriteEnvelopeStart( WS_MESSAGE *handle, WS_XML_WRITER *writer,
747 WS_MESSAGE_DONE_CALLBACK cb, void *state, WS_ERROR *error )
749 struct msg *msg = (struct msg *)handle;
750 HRESULT hr;
752 TRACE( "%p %p %p %p %p\n", handle, writer, cb, state, error );
753 if (error) FIXME( "ignoring error parameter\n" );
754 if (cb)
756 FIXME( "callback not supported\n" );
757 return E_NOTIMPL;
760 if (!msg || !writer) return E_INVALIDARG;
762 EnterCriticalSection( &msg->cs );
764 if (msg->magic != MSG_MAGIC)
766 LeaveCriticalSection( &msg->cs );
767 return E_INVALIDARG;
770 if (msg->state != WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
771 else if ((hr = write_envelope( msg, msg->init )) == S_OK &&
772 (hr = write_envelope_start( msg, msg->init, writer )) == S_OK)
774 msg->writer_body = writer;
775 msg->state = WS_MESSAGE_STATE_WRITING;
778 LeaveCriticalSection( &msg->cs );
779 TRACE( "returning %#lx\n", hr );
780 return hr;
783 /**************************************************************************
784 * WsWriteEnvelopeEnd [webservices.@]
786 HRESULT WINAPI WsWriteEnvelopeEnd( WS_MESSAGE *handle, WS_ERROR *error )
788 struct msg *msg = (struct msg *)handle;
789 HRESULT hr;
791 TRACE( "%p %p\n", handle, error );
792 if (error) FIXME( "ignoring error parameter\n" );
794 if (!msg) return E_INVALIDARG;
796 EnterCriticalSection( &msg->cs );
798 if (msg->magic != MSG_MAGIC)
800 LeaveCriticalSection( &msg->cs );
801 return E_INVALIDARG;
804 if (msg->state != WS_MESSAGE_STATE_WRITING) hr = WS_E_INVALID_OPERATION;
805 else if ((hr = write_envelope_end( msg->writer_body )) == S_OK)
806 msg->state = WS_MESSAGE_STATE_DONE;
808 LeaveCriticalSection( &msg->cs );
809 TRACE( "returning %#lx\n", hr );
810 return hr;
813 /**************************************************************************
814 * WsWriteBody [webservices.@]
816 HRESULT WINAPI WsWriteBody( WS_MESSAGE *handle, const WS_ELEMENT_DESCRIPTION *desc, WS_WRITE_OPTION option,
817 const void *value, ULONG size, WS_ERROR *error )
819 struct msg *msg = (struct msg *)handle;
820 HRESULT hr;
822 TRACE( "%p %p %u %p %lu %p\n", handle, desc, option, value, size, error );
823 if (error) FIXME( "ignoring error parameter\n" );
825 if (!msg || !desc) return E_INVALIDARG;
827 EnterCriticalSection( &msg->cs );
829 if (msg->magic != MSG_MAGIC)
831 LeaveCriticalSection( &msg->cs );
832 return E_INVALIDARG;
835 if (msg->state != WS_MESSAGE_STATE_WRITING)
837 hr = WS_E_INVALID_OPERATION;
838 goto done;
841 if (desc->elementLocalName &&
842 (hr = WsWriteStartElement( msg->writer_body, NULL, desc->elementLocalName, desc->elementNs,
843 NULL )) != S_OK) goto done;
845 if ((hr = WsWriteType( msg->writer_body, WS_ANY_ELEMENT_TYPE_MAPPING, desc->type, desc->typeDescription,
846 option, value, size, NULL )) != S_OK) goto done;
848 if (desc->elementLocalName) hr = WsWriteEndElement( msg->writer_body, NULL );
850 done:
851 LeaveCriticalSection( &msg->cs );
852 TRACE( "returning %#lx\n", hr );
853 return hr;
856 /**************************************************************************
857 * WsFlushBody [webservices.@]
859 HRESULT WINAPI WsFlushBody( WS_MESSAGE *handle, ULONG size, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
861 struct msg *msg = (struct msg *)handle;
862 HRESULT hr;
864 TRACE( "%p %lu %p %p\n", handle, size, ctx, error );
866 if (!msg) return E_INVALIDARG;
868 EnterCriticalSection( &msg->cs );
870 if (msg->magic != MSG_MAGIC)
872 LeaveCriticalSection( &msg->cs );
873 return E_INVALIDARG;
876 hr = WsFlushWriter( msg->writer_body, size, ctx, error );
878 LeaveCriticalSection( &msg->cs );
879 TRACE( "returning %#lx\n", hr );
880 return hr;
883 static BOOL match_current_element( WS_XML_READER *reader, const WS_XML_STRING *localname )
885 const WS_XML_NODE *node;
886 const WS_XML_ELEMENT_NODE *elem;
888 if (WsGetReaderNode( reader, &node, NULL ) != S_OK) return FALSE;
889 if (node->nodeType != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
890 elem = (const WS_XML_ELEMENT_NODE *)node;
891 return WsXmlStringEquals( elem->localName, localname, NULL ) == S_OK;
894 static BOOL match_current_element_with_ns( WS_XML_READER *reader, const WS_XML_STRING *localname, const WS_XML_STRING *ns )
896 const WS_XML_NODE *node;
897 const WS_XML_ELEMENT_NODE *elem;
899 if (WsGetReaderNode( reader, &node, NULL ) != S_OK) return FALSE;
900 if (node->nodeType != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
901 elem = (const WS_XML_ELEMENT_NODE *)node;
902 return WsXmlStringEquals( elem->localName, localname, NULL ) == S_OK &&
903 WsXmlStringEquals( elem->ns, ns, NULL ) == S_OK;
906 static HRESULT read_message_id( WS_XML_READER *reader, GUID *ret )
908 const WS_XML_NODE *node;
909 const WS_XML_TEXT_NODE *text;
910 HRESULT hr;
912 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
913 if ((hr = WsGetReaderNode( reader, &node, NULL )) != S_OK) return hr;
914 if (node->nodeType != WS_XML_NODE_TYPE_TEXT) return WS_E_INVALID_FORMAT;
915 text = (const WS_XML_TEXT_NODE *)node;
917 switch (text->text->textType)
919 case WS_XML_TEXT_TYPE_UTF8:
921 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text->text;
922 if (utf8->value.length != 45 || memcmp( utf8->value.bytes, "urn:uuid:", 9 ))
923 return WS_E_INVALID_FORMAT;
925 return str_to_guid( utf8->value.bytes + 9, utf8->value.length - 9, ret );
927 case WS_XML_TEXT_TYPE_UNIQUE_ID:
929 const WS_XML_UNIQUE_ID_TEXT *id = (const WS_XML_UNIQUE_ID_TEXT *)text->text;
930 *ret = id->value;
931 return S_OK;
933 default:
934 FIXME( "unhandled text type %u\n", text->text->textType );
935 return E_NOTIMPL;
939 static HRESULT read_envelope_start( struct msg *msg, WS_XML_READER *reader )
941 static const WS_XML_STRING envelope = {8, (BYTE *)"Envelope"}, body = {4, (BYTE *)"Body"};
942 static const WS_XML_STRING header = {6, (BYTE *)"Header"}, msgid = {9, (BYTE *)"MessageID"};
943 static const WS_XML_STRING fault = {5, (BYTE *)"Fault"};
944 const WS_XML_STRING *ns_env = get_env_namespace( msg->version_env );
945 BOOL isfault;
946 HRESULT hr;
948 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
949 if (!match_current_element( reader, &envelope )) return WS_E_INVALID_FORMAT;
950 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
951 if (match_current_element( reader, &header ))
953 for (;;)
955 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
956 if (match_current_element( reader, &msgid ) && (hr = read_message_id( reader, &msg->id_req )) != S_OK)
957 return hr;
958 if (match_current_element( reader, &body )) break;
961 if (!match_current_element( reader, &body )) return WS_E_INVALID_FORMAT;
962 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
964 isfault = match_current_element_with_ns( reader, &fault, ns_env );
965 return prop_set( msg->prop, msg->prop_count, WS_MESSAGE_PROPERTY_IS_FAULT, &isfault, sizeof(isfault) );
968 /**************************************************************************
969 * WsReadEnvelopeStart [webservices.@]
971 HRESULT WINAPI WsReadEnvelopeStart( WS_MESSAGE *handle, WS_XML_READER *reader, WS_MESSAGE_DONE_CALLBACK cb,
972 void *state, WS_ERROR *error )
974 struct msg *msg = (struct msg *)handle;
975 HRESULT hr;
977 TRACE( "%p %p %p %p %p\n", handle, reader, cb, state, error );
978 if (error) FIXME( "ignoring error parameter\n" );
979 if (cb)
981 FIXME( "callback not supported\n" );
982 return E_NOTIMPL;
985 if (!msg || !reader) return E_INVALIDARG;
987 EnterCriticalSection( &msg->cs );
989 if (msg->magic != MSG_MAGIC)
991 LeaveCriticalSection( &msg->cs );
992 return E_INVALIDARG;
995 if (msg->state != WS_MESSAGE_STATE_EMPTY) hr = WS_E_INVALID_OPERATION;
996 else if ((hr = read_envelope_start( msg, reader )) == S_OK &&
997 (hr = create_header_buffer( reader, msg->heap, &msg->buf )) == S_OK)
999 msg->reader_body = reader;
1000 msg->state = WS_MESSAGE_STATE_READING;
1003 LeaveCriticalSection( &msg->cs );
1004 TRACE( "returning %#lx\n", hr );
1005 return hr;
1008 /**************************************************************************
1009 * WsReadEnvelopeEnd [webservices.@]
1011 HRESULT WINAPI WsReadEnvelopeEnd( WS_MESSAGE *handle, WS_ERROR *error )
1013 struct msg *msg = (struct msg *)handle;
1014 HRESULT hr;
1016 TRACE( "%p %p\n", handle, error );
1017 if (error) FIXME( "ignoring error parameter\n" );
1019 if (!msg) return E_INVALIDARG;
1021 EnterCriticalSection( &msg->cs );
1023 if (msg->magic != MSG_MAGIC)
1025 LeaveCriticalSection( &msg->cs );
1026 return E_INVALIDARG;
1029 if (msg->state != WS_MESSAGE_STATE_READING) hr = WS_E_INVALID_OPERATION;
1030 else if ((hr = WsReadEndElement( msg->reader_body, NULL )) == S_OK)
1031 msg->state = WS_MESSAGE_STATE_DONE;
1033 LeaveCriticalSection( &msg->cs );
1034 TRACE( "returning %#lx\n", hr );
1035 return hr;
1038 /**************************************************************************
1039 * WsReadBody [webservices.@]
1041 HRESULT WINAPI WsReadBody( WS_MESSAGE *handle, const WS_ELEMENT_DESCRIPTION *desc, WS_READ_OPTION option,
1042 WS_HEAP *heap, void *value, ULONG size, WS_ERROR *error )
1044 struct msg *msg = (struct msg *)handle;
1045 WS_ELEMENT_DESCRIPTION tmp;
1046 WS_FAULT_DESCRIPTION fault_desc;
1047 HRESULT hr;
1049 TRACE( "%p %p %u %p %p %lu %p\n", handle, desc, option, heap, value, size, error );
1050 if (error) FIXME( "ignoring error parameter\n" );
1052 if (!msg || !desc) return E_INVALIDARG;
1054 EnterCriticalSection( &msg->cs );
1056 if (msg->magic != MSG_MAGIC)
1058 LeaveCriticalSection( &msg->cs );
1059 return E_INVALIDARG;
1062 if (msg->state != WS_MESSAGE_STATE_READING) hr = WS_E_INVALID_OPERATION;
1063 else
1065 if (!desc->typeDescription)
1067 if (desc->type == WS_FAULT_TYPE)
1069 memcpy( &tmp, desc, sizeof(*desc) );
1070 fault_desc.envelopeVersion = msg->version_env;
1071 tmp.typeDescription = &fault_desc;
1072 desc = &tmp;
1074 else if (desc->type == WS_ENDPOINT_ADDRESS_TYPE)
1075 FIXME( "!desc->typeDescription with WS_ENDPOINT_ADDRESS_TYPE\n" );
1078 hr = WsReadElement( msg->reader_body, desc, option, heap, value, size, NULL );
1081 LeaveCriticalSection( &msg->cs );
1082 TRACE( "returning %#lx\n", hr );
1083 return hr;
1086 /**************************************************************************
1087 * WsFillBody [webservices.@]
1089 HRESULT WINAPI WsFillBody( WS_MESSAGE *handle, ULONG size, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1091 struct msg *msg = (struct msg *)handle;
1092 HRESULT hr;
1094 TRACE( "%p %lu %p %p\n", handle, size, ctx, error );
1096 if (!msg) return E_INVALIDARG;
1098 EnterCriticalSection( &msg->cs );
1100 if (msg->magic != MSG_MAGIC)
1102 LeaveCriticalSection( &msg->cs );
1103 return E_INVALIDARG;
1106 hr = WsFillReader( msg->reader_body, size, ctx, error );
1108 LeaveCriticalSection( &msg->cs );
1109 TRACE( "returning %#lx\n", hr );
1110 return hr;
1113 /**************************************************************************
1114 * WsInitializeMessage [webservices.@]
1116 HRESULT WINAPI WsInitializeMessage( WS_MESSAGE *handle, WS_MESSAGE_INITIALIZATION init,
1117 WS_MESSAGE *src_handle, WS_ERROR *error )
1119 struct msg *msg = (struct msg *)handle;
1120 HRESULT hr;
1122 TRACE( "%p %u %p %p\n", handle, init, src_handle, error );
1123 if (error) FIXME( "ignoring error parameter\n" );
1124 if (src_handle)
1126 FIXME( "src message not supported\n" );
1127 return E_NOTIMPL;
1130 if (!msg || init > WS_FAULT_MESSAGE) return E_INVALIDARG;
1132 EnterCriticalSection( &msg->cs );
1134 if (msg->magic != MSG_MAGIC)
1136 LeaveCriticalSection( &msg->cs );
1137 return E_INVALIDARG;
1140 if (msg->state >= WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1141 else if ((hr = write_envelope( msg, init )) == S_OK)
1143 msg->init = init;
1144 msg->state = WS_MESSAGE_STATE_INITIALIZED;
1147 LeaveCriticalSection( &msg->cs );
1148 TRACE( "returning %#lx\n", hr );
1149 return hr;
1152 static HRESULT grow_header_array( struct msg *msg, ULONG size )
1154 struct header **tmp;
1155 if (size <= msg->header_size) return S_OK;
1156 if (!(tmp = realloc( msg->header, 2 * msg->header_size * sizeof(struct header *) ))) return E_OUTOFMEMORY;
1157 msg->header = tmp;
1158 msg->header_size *= 2;
1159 return S_OK;
1162 static struct header *alloc_header( WS_HEADER_TYPE type, BOOL mapped, const WS_XML_STRING *name,
1163 const WS_XML_STRING *ns )
1165 struct header *ret;
1166 if (!(ret = calloc( 1, sizeof(*ret) ))) return NULL;
1167 if (name && name->length)
1169 if (!(ret->name.bytes = malloc( name->length )))
1171 free_header( ret );
1172 return NULL;
1174 memcpy( ret->name.bytes, name->bytes, name->length );
1175 ret->name.length = name->length;
1177 if (ns && ns->length)
1179 if (!(ret->ns.bytes = malloc( ns->length )))
1181 free_header( ret );
1182 return NULL;
1184 memcpy( ret->ns.bytes, ns->bytes, ns->length );
1185 ret->ns.length = ns->length;
1187 ret->type = type;
1188 ret->mapped = mapped;
1189 return ret;
1192 static HRESULT write_standard_header( struct msg *msg, WS_HEADER_TYPE type, WS_TYPE value_type,
1193 WS_WRITE_OPTION option, const void *value, ULONG size )
1195 static const WS_XML_STRING ns = {0, NULL};
1196 static const WS_XML_STRING prefix_s = {1, (BYTE *)"s"}, prefix_a = {1, (BYTE *)"a"};
1197 const WS_XML_STRING *prefix_env = (msg->version_env == WS_ENVELOPE_VERSION_NONE) ? NULL : &prefix_s;
1198 const WS_XML_STRING *prefix_addr = (msg->version_addr == WS_ADDRESSING_VERSION_TRANSPORT) ? NULL : &prefix_a;
1199 const WS_XML_STRING *localname = get_header_name( type );
1200 HRESULT hr;
1202 if ((hr = WsWriteStartElement( msg->writer, prefix_addr, localname, &ns, NULL )) != S_OK) return hr;
1203 if ((hr = write_must_understand( msg->writer, prefix_env, &ns )) != S_OK) return hr;
1204 if (msg->version_addr == WS_ADDRESSING_VERSION_TRANSPORT)
1206 const WS_XML_STRING *ns_addr = get_addr_namespace( WS_ADDRESSING_VERSION_TRANSPORT );
1207 if ((hr = WsWriteXmlnsAttribute( msg->writer, NULL, ns_addr, FALSE, NULL )) != S_OK) return hr;
1209 if ((hr = WsWriteType( msg->writer, WS_ELEMENT_CONTENT_TYPE_MAPPING, value_type, NULL, option, value, size,
1210 NULL )) != S_OK) return hr;
1211 return WsWriteEndElement( msg->writer, NULL );
1214 static HRESULT build_standard_header( struct msg *msg, WS_HEADER_TYPE type, WS_TYPE value_type,
1215 WS_WRITE_OPTION option, const void *value, ULONG size,
1216 struct header **ret )
1218 struct header *header;
1219 WS_XML_BUFFER *buf;
1220 HRESULT hr;
1222 if (!(header = alloc_header( type, FALSE, get_header_name(type), NULL ))) return E_OUTOFMEMORY;
1224 if (!msg->writer && (hr = WsCreateWriter( NULL, 0, &msg->writer, NULL )) != S_OK) goto done;
1225 if ((hr = WsCreateXmlBuffer( msg->heap, NULL, 0, &buf, NULL )) != S_OK) goto done;
1226 if ((hr = WsSetOutputToBuffer( msg->writer, buf, NULL, 0, NULL )) != S_OK) goto done;
1227 if ((hr = write_standard_header( msg, type, value_type, option, value, size )) != S_OK) goto done;
1229 header->u.buf = buf;
1231 done:
1232 if (hr != S_OK) free_header( header );
1233 else *ret = header;
1234 return hr;
1237 /**************************************************************************
1238 * WsSetHeader [webservices.@]
1240 HRESULT WINAPI WsSetHeader( WS_MESSAGE *handle, WS_HEADER_TYPE type, WS_TYPE value_type,
1241 WS_WRITE_OPTION option, const void *value, ULONG size, WS_ERROR *error )
1243 struct msg *msg = (struct msg *)handle;
1244 struct header *header;
1245 BOOL found = FALSE;
1246 HRESULT hr;
1247 ULONG i;
1249 TRACE( "%p %u %u %u %p %lu %p\n", handle, type, value_type, option, value, size, error );
1250 if (error) FIXME( "ignoring error parameter\n" );
1252 if (!msg || type < WS_ACTION_HEADER || type > WS_FAULT_TO_HEADER) return E_INVALIDARG;
1254 EnterCriticalSection( &msg->cs );
1256 if (msg->magic != MSG_MAGIC)
1258 LeaveCriticalSection( &msg->cs );
1259 return E_INVALIDARG;
1262 if (msg->state < WS_MESSAGE_STATE_INITIALIZED)
1264 hr = WS_E_INVALID_OPERATION;
1265 goto done;
1268 for (i = 0; i < msg->header_count; i++)
1270 if (msg->header[i]->type == type)
1272 found = TRUE;
1273 break;
1277 if (!found)
1279 if ((hr = grow_header_array( msg, msg->header_count + 1 )) != S_OK) goto done;
1280 i = msg->header_count;
1283 if ((hr = build_standard_header( msg, type, value_type, option, value, size, &header )) != S_OK)
1284 goto done;
1286 if (!found) msg->header_count++;
1287 else free_header( msg->header[i] );
1289 msg->header[i] = header;
1290 hr = write_envelope( msg, msg->init );
1292 done:
1293 LeaveCriticalSection( &msg->cs );
1294 TRACE( "returning %#lx\n", hr );
1295 return hr;
1298 static HRESULT find_header( WS_XML_READER *reader, const WS_XML_STRING *localname, const WS_XML_STRING *ns )
1300 const WS_XML_NODE *node;
1301 const WS_XML_ELEMENT_NODE *elem;
1302 HRESULT hr;
1304 for (;;)
1306 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
1307 if ((hr = WsGetReaderNode( reader, &node, NULL )) != S_OK) return hr;
1308 if (node->nodeType == WS_XML_NODE_TYPE_EOF) return WS_E_INVALID_FORMAT;
1309 if (node->nodeType != WS_XML_NODE_TYPE_ELEMENT) continue;
1311 elem = (const WS_XML_ELEMENT_NODE *)node;
1312 if (WsXmlStringEquals( elem->localName, localname, NULL ) == S_OK &&
1313 WsXmlStringEquals( elem->ns, ns, NULL ) == S_OK) break;
1316 return S_OK;
1319 static HRESULT get_standard_header( struct msg *msg, WS_HEADER_TYPE type, WS_TYPE value_type,
1320 WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size )
1322 const WS_XML_STRING *localname = get_header_name( type );
1323 const WS_XML_STRING *ns = get_addr_namespace( msg->version_addr );
1324 HRESULT hr;
1326 if (!heap) heap = msg->heap;
1327 if (!msg->reader && (hr = WsCreateReader( NULL, 0, &msg->reader, NULL )) != S_OK) return hr;
1328 if ((hr = WsSetInputToBuffer( msg->reader, msg->buf, NULL, 0, NULL )) != S_OK) return hr;
1330 if ((hr = find_header( msg->reader, localname, ns )) != S_OK) return hr;
1331 return read_header( msg->reader, localname, ns, value_type, NULL, option, heap, value, size );
1334 /**************************************************************************
1335 * WsGetHeader [webservices.@]
1337 HRESULT WINAPI WsGetHeader( WS_MESSAGE *handle, WS_HEADER_TYPE type, WS_TYPE value_type, WS_READ_OPTION option,
1338 WS_HEAP *heap, void *value, ULONG size, WS_ERROR *error )
1340 struct msg *msg = (struct msg *)handle;
1341 HRESULT hr;
1343 TRACE( "%p %u %u %u %p %p %lu %p\n", handle, type, value_type, option, heap, value, size, error );
1344 if (error) FIXME( "ignoring error parameter\n" );
1346 if (!msg || type < WS_ACTION_HEADER || type > WS_FAULT_TO_HEADER || option < WS_READ_REQUIRED_VALUE ||
1347 option > WS_READ_OPTIONAL_POINTER) return E_INVALIDARG;
1349 EnterCriticalSection( &msg->cs );
1351 if (msg->magic != MSG_MAGIC)
1353 LeaveCriticalSection( &msg->cs );
1354 return E_INVALIDARG;
1357 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1358 else hr = get_standard_header( msg, type, value_type, option, heap, value, size );
1360 LeaveCriticalSection( &msg->cs );
1361 TRACE( "returning %#lx\n", hr );
1362 return hr;
1365 static void remove_header( struct msg *msg, ULONG i )
1367 free_header( msg->header[i] );
1368 memmove( &msg->header[i], &msg->header[i + 1], (msg->header_count - i - 1) * sizeof(struct header *) );
1369 msg->header_count--;
1372 /**************************************************************************
1373 * WsRemoveHeader [webservices.@]
1375 HRESULT WINAPI WsRemoveHeader( WS_MESSAGE *handle, WS_HEADER_TYPE type, WS_ERROR *error )
1377 struct msg *msg = (struct msg *)handle;
1378 BOOL removed = FALSE;
1379 HRESULT hr = S_OK;
1380 ULONG i;
1382 TRACE( "%p %u %p\n", handle, type, error );
1383 if (error) FIXME( "ignoring error parameter\n" );
1385 if (!msg) return E_INVALIDARG;
1387 EnterCriticalSection( &msg->cs );
1389 if (msg->magic != MSG_MAGIC)
1391 LeaveCriticalSection( &msg->cs );
1392 return E_INVALIDARG;
1395 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1396 else if (type < WS_ACTION_HEADER || type > WS_FAULT_TO_HEADER) hr = E_INVALIDARG;
1397 else
1399 for (i = 0; i < msg->header_count; i++)
1401 if (msg->header[i]->type == type)
1403 remove_header( msg, i );
1404 removed = TRUE;
1405 break;
1408 if (removed) hr = write_envelope( msg, msg->init );
1411 LeaveCriticalSection( &msg->cs );
1412 TRACE( "returning %#lx\n", hr );
1413 return hr;
1416 static HRESULT build_mapped_header( const WS_XML_STRING *name, WS_TYPE type, WS_WRITE_OPTION option,
1417 const void *value, ULONG size, struct header **ret )
1419 struct header *header;
1421 if (!(header = alloc_header( 0, TRUE, name, NULL ))) return E_OUTOFMEMORY;
1422 switch (type)
1424 case WS_WSZ_TYPE:
1426 int len;
1427 const WCHAR *src;
1429 if (option != WS_WRITE_REQUIRED_POINTER || size != sizeof(WCHAR *))
1431 free_header( header );
1432 return E_INVALIDARG;
1434 src = *(const WCHAR **)value;
1435 len = WideCharToMultiByte( CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL ) - 1;
1436 if (!(header->u.text = alloc_xml_string( NULL, len )))
1438 free_header( header );
1439 return E_OUTOFMEMORY;
1441 WideCharToMultiByte( CP_UTF8, 0, src, -1, (char *)header->u.text->bytes, len, NULL, NULL );
1442 break;
1444 case WS_XML_STRING_TYPE:
1446 const WS_XML_STRING *str = value;
1448 if (option != WS_WRITE_REQUIRED_VALUE)
1450 FIXME( "unhandled write option %u\n", option );
1451 free_header( header );
1452 return E_NOTIMPL;
1454 if (size != sizeof(*str))
1456 free_header( header );
1457 return E_INVALIDARG;
1459 if (!(header->u.text = alloc_xml_string( NULL, str->length )))
1461 free_header( header );
1462 return E_OUTOFMEMORY;
1464 memcpy( header->u.text->bytes, str->bytes, str->length );
1465 break;
1467 case WS_STRING_TYPE:
1469 int len;
1470 const WS_STRING *str = value;
1472 if (option != WS_WRITE_REQUIRED_VALUE)
1474 FIXME( "unhandled write option %u\n", option );
1475 free_header( header );
1476 return E_NOTIMPL;
1478 if (size != sizeof(*str))
1480 free_header( header );
1481 return E_INVALIDARG;
1483 len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
1484 if (!(header->u.text = alloc_xml_string( NULL, len )))
1486 free_header( header );
1487 return E_OUTOFMEMORY;
1489 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)header->u.text->bytes,
1490 len, NULL, NULL );
1491 break;
1493 default:
1494 FIXME( "unhandled type %u\n", type );
1495 free_header( header );
1496 return E_NOTIMPL;
1499 *ret = header;
1500 return S_OK;
1503 static HRESULT add_mapped_header( struct msg *msg, const WS_XML_STRING *name, WS_TYPE type,
1504 WS_WRITE_OPTION option, const void *value, ULONG size )
1506 struct header *header;
1507 BOOL found = FALSE;
1508 HRESULT hr;
1509 ULONG i;
1511 for (i = 0; i < msg->header_count; i++)
1513 if (msg->header[i]->type || !msg->header[i]->mapped) continue;
1514 if (WsXmlStringEquals( name, &msg->header[i]->name, NULL ) == S_OK)
1516 found = TRUE;
1517 break;
1521 if (!found)
1523 if ((hr = grow_header_array( msg, msg->header_count + 1 )) != S_OK) return hr;
1524 i = msg->header_count;
1527 if ((hr = build_mapped_header( name, type, option, value, size, &header )) != S_OK) return hr;
1529 if (!found) msg->header_count++;
1530 else free_header( msg->header[i] );
1532 msg->header[i] = header;
1533 return S_OK;
1536 /**************************************************************************
1537 * WsAddMappedHeader [webservices.@]
1539 HRESULT WINAPI WsAddMappedHeader( WS_MESSAGE *handle, const WS_XML_STRING *name, WS_TYPE type,
1540 WS_WRITE_OPTION option, const void *value, ULONG size, WS_ERROR *error )
1542 struct msg *msg = (struct msg *)handle;
1543 HRESULT hr;
1545 TRACE( "%p %s %u %u %p %lu %p\n", handle, debugstr_xmlstr(name), type, option, value, size, error );
1546 if (error) FIXME( "ignoring error parameter\n" );
1548 if (!msg || !name) return E_INVALIDARG;
1550 EnterCriticalSection( &msg->cs );
1552 if (msg->magic != MSG_MAGIC)
1554 LeaveCriticalSection( &msg->cs );
1555 return E_INVALIDARG;
1558 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1559 else hr = add_mapped_header( msg, name, type, option, value, size );
1561 LeaveCriticalSection( &msg->cs );
1562 TRACE( "returning %#lx\n", hr );
1563 return hr;
1566 /**************************************************************************
1567 * WsRemoveMappedHeader [webservices.@]
1569 HRESULT WINAPI WsRemoveMappedHeader( WS_MESSAGE *handle, const WS_XML_STRING *name, WS_ERROR *error )
1571 struct msg *msg = (struct msg *)handle;
1572 HRESULT hr = S_OK;
1573 ULONG i;
1575 TRACE( "%p %s %p\n", handle, debugstr_xmlstr(name), error );
1576 if (error) FIXME( "ignoring error parameter\n" );
1578 if (!msg || !name) return E_INVALIDARG;
1580 EnterCriticalSection( &msg->cs );
1582 if (msg->magic != MSG_MAGIC)
1584 LeaveCriticalSection( &msg->cs );
1585 return E_INVALIDARG;
1588 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1589 else
1591 for (i = 0; i < msg->header_count; i++)
1593 if (msg->header[i]->type || !msg->header[i]->mapped) continue;
1594 if (WsXmlStringEquals( name, &msg->header[i]->name, NULL ) == S_OK)
1596 remove_header( msg, i );
1597 break;
1602 LeaveCriticalSection( &msg->cs );
1603 TRACE( "returning %#lx\n", hr );
1604 return hr;
1607 static HRESULT xmlstring_to_wsz( const WS_XML_STRING *str, WS_HEAP *heap, WCHAR **ret, int *len )
1609 *len = MultiByteToWideChar( CP_UTF8, 0, (char *)str->bytes, str->length, NULL, 0 );
1610 if (!(*ret = ws_alloc( heap, (*len + 1) * sizeof(WCHAR) ))) return WS_E_QUOTA_EXCEEDED;
1611 MultiByteToWideChar( CP_UTF8, 0, (char *)str->bytes, str->length, *ret, *len );
1612 (*ret)[*len] = 0;
1613 return S_OK;
1616 static HRESULT get_header_value_wsz( struct header *header, WS_READ_OPTION option, WS_HEAP *heap, WCHAR **ret,
1617 ULONG size )
1619 WCHAR *str = NULL;
1620 int len = 0;
1621 HRESULT hr;
1623 if (header && (hr = xmlstring_to_wsz( header->u.text, heap, &str, &len )) != S_OK) return hr;
1625 switch (option)
1627 case WS_READ_REQUIRED_POINTER:
1628 if (!str && !(str = ws_alloc_zero( heap, sizeof(*str) ))) return WS_E_QUOTA_EXCEEDED;
1629 /* fall through */
1631 case WS_READ_OPTIONAL_POINTER:
1632 case WS_READ_NILLABLE_POINTER:
1633 if (size != sizeof(str))
1635 ws_free( heap, str, (len + 1) * sizeof(WCHAR) );
1636 return E_INVALIDARG;
1638 *ret = str;
1639 break;
1641 default:
1642 FIXME( "read option %u not supported\n", option );
1643 ws_free( heap, str, (len + 1) * sizeof(WCHAR) );
1644 return E_NOTIMPL;
1647 return S_OK;
1650 static HRESULT get_mapped_header( struct msg *msg, const WS_XML_STRING *name, WS_TYPE type, WS_READ_OPTION option,
1651 WS_HEAP *heap, void *value, ULONG size )
1653 struct header *header = NULL;
1654 HRESULT hr;
1655 ULONG i;
1657 for (i = 0; i < msg->header_count; i++)
1659 if (msg->header[i]->type || !msg->header[i]->mapped) continue;
1660 if (WsXmlStringEquals( name, &msg->header[i]->name, NULL ) == S_OK)
1662 header = msg->header[i];
1663 break;
1667 switch (type)
1669 case WS_WSZ_TYPE:
1670 hr = get_header_value_wsz( header, option, heap, value, size );
1671 break;
1673 default:
1674 FIXME( "type %u not supported\n", option );
1675 return WS_E_NOT_SUPPORTED;
1678 return hr;
1681 /**************************************************************************
1682 * WsGetMappedHeader [webservices.@]
1684 HRESULT WINAPI WsGetMappedHeader( WS_MESSAGE *handle, const WS_XML_STRING *name, WS_REPEATING_HEADER_OPTION option,
1685 ULONG index, WS_TYPE type, WS_READ_OPTION read_option, WS_HEAP *heap, void *value,
1686 ULONG size, WS_ERROR *error )
1688 struct msg *msg = (struct msg *)handle;
1689 HRESULT hr;
1691 TRACE( "%p %s %u %lu %u %u %p %p %lu %p\n", handle, debugstr_xmlstr(name), option, index, type, read_option,
1692 heap, value, size, error );
1693 if (error) FIXME( "ignoring error parameter\n" );
1694 if (option != WS_SINGLETON_HEADER)
1696 FIXME( "option %u not supported\n", option );
1697 return E_NOTIMPL;
1700 if (!msg || !name) return E_INVALIDARG;
1702 EnterCriticalSection( &msg->cs );
1704 if (msg->magic != MSG_MAGIC)
1706 LeaveCriticalSection( &msg->cs );
1707 return E_INVALIDARG;
1710 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1711 else hr = get_mapped_header( msg, name, type, read_option, heap, value, size );
1713 LeaveCriticalSection( &msg->cs );
1714 TRACE( "returning %#lx\n", hr );
1715 return hr;
1718 static HRESULT write_custom_header( WS_XML_WRITER *writer, const WS_XML_STRING *name, const WS_XML_STRING *ns,
1719 WS_TYPE type, const void *desc, WS_WRITE_OPTION option, const void *value,
1720 ULONG size )
1722 HRESULT hr;
1723 if ((hr = WsWriteStartElement( writer, NULL, name, ns, NULL )) != S_OK) return hr;
1724 if ((hr = WsWriteType( writer, WS_ELEMENT_CONTENT_TYPE_MAPPING, type, desc, option, value, size,
1725 NULL )) != S_OK) return hr;
1726 return WsWriteEndElement( writer, NULL );
1729 static HRESULT build_custom_header( struct msg *msg, const WS_XML_STRING *name, const WS_XML_STRING *ns,
1730 WS_TYPE type, const void *desc, WS_WRITE_OPTION option, const void *value,
1731 ULONG size, struct header **ret )
1733 struct header *header;
1734 WS_XML_BUFFER *buf;
1735 HRESULT hr;
1737 if (!(header = alloc_header( 0, FALSE, name, ns ))) return E_OUTOFMEMORY;
1739 if (!msg->writer && (hr = WsCreateWriter( NULL, 0, &msg->writer, NULL )) != S_OK) goto done;
1740 if ((hr = WsCreateXmlBuffer( msg->heap, NULL, 0, &buf, NULL )) != S_OK) goto done;
1741 if ((hr = WsSetOutputToBuffer( msg->writer, buf, NULL, 0, NULL )) != S_OK) goto done;
1742 if ((hr = write_custom_header( msg->writer, name, ns, type, desc, option, value, size )) != S_OK) goto done;
1744 header->u.buf = buf;
1746 done:
1747 if (hr != S_OK) free_header( header );
1748 else *ret = header;
1749 return hr;
1752 /**************************************************************************
1753 * WsAddCustomHeader [webservices.@]
1755 HRESULT WINAPI WsAddCustomHeader( WS_MESSAGE *handle, const WS_ELEMENT_DESCRIPTION *desc, WS_WRITE_OPTION option,
1756 const void *value, ULONG size, ULONG attrs, WS_ERROR *error )
1758 struct msg *msg = (struct msg *)handle;
1759 struct header *header;
1760 HRESULT hr;
1762 TRACE( "%p %p %u %p %lu %#lx %p\n", handle, desc, option, value, size, attrs, error );
1763 if (error) FIXME( "ignoring error parameter\n" );
1765 if (!msg || !desc) return E_INVALIDARG;
1767 EnterCriticalSection( &msg->cs );
1769 if (msg->magic != MSG_MAGIC)
1771 LeaveCriticalSection( &msg->cs );
1772 return E_INVALIDARG;
1775 if (msg->state < WS_MESSAGE_STATE_INITIALIZED)
1777 hr = WS_E_INVALID_OPERATION;
1778 goto done;
1781 if ((hr = grow_header_array( msg, msg->header_count + 1 )) != S_OK) goto done;
1782 if ((hr = build_custom_header( msg, desc->elementLocalName, desc->elementNs, desc->type,
1783 desc->typeDescription, option, value, size, &header )) != S_OK) goto done;
1784 msg->header[msg->header_count++] = header;
1785 hr = write_envelope( msg, msg->init );
1787 done:
1788 LeaveCriticalSection( &msg->cs );
1789 TRACE( "returning %#lx\n", hr );
1790 return hr;
1793 static HRESULT get_custom_header( struct msg *msg, const WS_ELEMENT_DESCRIPTION *desc, WS_READ_OPTION option,
1794 WS_HEAP *heap, void *value, ULONG size )
1796 HRESULT hr;
1797 if (!heap) heap = msg->heap;
1798 if (!msg->reader && (hr = WsCreateReader( NULL, 0, &msg->reader, NULL )) != S_OK) return hr;
1799 if ((hr = WsSetInputToBuffer( msg->reader, msg->buf, NULL, 0, NULL )) != S_OK) return hr;
1801 if ((hr = find_header( msg->reader, desc->elementLocalName, desc->elementNs )) != S_OK) return hr;
1802 return read_header( msg->reader, desc->elementLocalName, desc->elementNs, desc->type, desc->typeDescription,
1803 option, heap, value, size );
1806 /**************************************************************************
1807 * WsGetCustomHeader [webservices.@]
1809 HRESULT WINAPI WsGetCustomHeader( WS_MESSAGE *handle, const WS_ELEMENT_DESCRIPTION *desc,
1810 WS_REPEATING_HEADER_OPTION repeat_option, ULONG index, WS_READ_OPTION option,
1811 WS_HEAP *heap, void *value, ULONG size, ULONG *attrs, WS_ERROR *error )
1813 struct msg *msg = (struct msg *)handle;
1814 HRESULT hr;
1816 TRACE( "%p %p %u %lu %u %p %p %lu %p %p\n", handle, desc, repeat_option, index, option, heap, value,
1817 size, attrs, error );
1818 if (error) FIXME( "ignoring error parameter\n" );
1820 if (!msg || !desc || repeat_option < WS_REPEATING_HEADER || repeat_option > WS_SINGLETON_HEADER ||
1821 (repeat_option == WS_SINGLETON_HEADER && index)) return E_INVALIDARG;
1823 if (repeat_option == WS_REPEATING_HEADER)
1825 FIXME( "repeating header not supported\n" );
1826 return E_NOTIMPL;
1828 if (attrs)
1830 FIXME( "attributes not supported\n" );
1831 return E_NOTIMPL;
1834 EnterCriticalSection( &msg->cs );
1836 if (msg->magic != MSG_MAGIC)
1838 LeaveCriticalSection( &msg->cs );
1839 return E_INVALIDARG;
1842 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1843 else hr = get_custom_header( msg, desc, option, heap, value, size );
1845 LeaveCriticalSection( &msg->cs );
1846 TRACE( "returning %#lx\n", hr );
1847 return hr;
1850 /**************************************************************************
1851 * WsRemoveCustomHeader [webservices.@]
1853 HRESULT WINAPI WsRemoveCustomHeader( WS_MESSAGE *handle, const WS_XML_STRING *name, const WS_XML_STRING *ns,
1854 WS_ERROR *error )
1856 struct msg *msg = (struct msg *)handle;
1857 BOOL removed = FALSE;
1858 HRESULT hr = S_OK;
1859 ULONG i;
1861 TRACE( "%p %s %s %p\n", handle, debugstr_xmlstr(name), debugstr_xmlstr(ns), error );
1862 if (error) FIXME( "ignoring error parameter\n" );
1864 if (!msg || !name || !ns) return E_INVALIDARG;
1866 EnterCriticalSection( &msg->cs );
1868 if (msg->magic != MSG_MAGIC)
1870 LeaveCriticalSection( &msg->cs );
1871 return E_INVALIDARG;
1874 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1875 else
1877 for (i = 0; i < msg->header_count; i++)
1879 if (msg->header[i]->type || msg->header[i]->mapped) continue;
1880 if (WsXmlStringEquals( name, &msg->header[i]->name, NULL ) == S_OK &&
1881 WsXmlStringEquals( ns, &msg->header[i]->ns, NULL ) == S_OK)
1883 remove_header( msg, i );
1884 removed = TRUE;
1885 i--;
1888 if (removed) hr = write_envelope( msg, msg->init );
1891 LeaveCriticalSection( &msg->cs );
1892 TRACE( "returning %#lx\n", hr );
1893 return hr;
1896 static WCHAR *build_http_header( const WCHAR *name, const WCHAR *value, ULONG *ret_len )
1898 int len_name = lstrlenW( name ), len_value = lstrlenW( value );
1899 WCHAR *ret = malloc( (len_name + len_value + 2) * sizeof(WCHAR) );
1901 if (!ret) return NULL;
1902 memcpy( ret, name, len_name * sizeof(WCHAR) );
1903 ret[len_name++] = ':';
1904 ret[len_name++] = ' ';
1905 memcpy( ret + len_name, value, len_value * sizeof(WCHAR) );
1906 *ret_len = len_name + len_value;
1907 return ret;
1910 static inline HRESULT insert_http_header( HINTERNET req, const WCHAR *header, ULONG len, ULONG flags )
1912 if (WinHttpAddRequestHeaders( req, header, len, flags )) return S_OK;
1913 return HRESULT_FROM_WIN32( GetLastError() );
1916 static WCHAR *from_xml_string( const WS_XML_STRING *str )
1918 WCHAR *ret;
1919 int len = MultiByteToWideChar( CP_UTF8, 0, (char *)str->bytes, str->length, NULL, 0 );
1920 if (!(ret = malloc( (len + 1) * sizeof(*ret) ))) return NULL;
1921 MultiByteToWideChar( CP_UTF8, 0, (char *)str->bytes, str->length, ret, len );
1922 ret[len] = 0;
1923 return ret;
1926 static HRESULT insert_mapped_headers( struct msg *msg, HINTERNET req )
1928 WCHAR *name, *value, *header;
1929 ULONG i, len;
1930 HRESULT hr;
1932 for (i = 0; i < msg->header_count; i++)
1934 if (!msg->header[i]->mapped) continue;
1936 if (!(name = from_xml_string( &msg->header[i]->name ))) return E_OUTOFMEMORY;
1937 if (!(value = from_xml_string( msg->header[i]->u.text )))
1939 free( name );
1940 return E_OUTOFMEMORY;
1942 header = build_http_header( name, value, &len );
1943 free( name );
1944 free( value );
1945 if (!header) return E_OUTOFMEMORY;
1947 hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_ADD|WINHTTP_ADDREQ_FLAG_REPLACE );
1948 free( header );
1949 if (hr != S_OK) return hr;
1951 return S_OK;
1954 HRESULT message_insert_http_headers( WS_MESSAGE *handle, HINTERNET req )
1956 struct msg *msg = (struct msg *)handle;
1957 HRESULT hr = E_OUTOFMEMORY;
1958 WCHAR *header = NULL, *buf;
1959 ULONG len;
1961 EnterCriticalSection( &msg->cs );
1963 if (msg->magic != MSG_MAGIC)
1965 LeaveCriticalSection( &msg->cs );
1966 return E_INVALIDARG;
1969 switch (msg->version_env)
1971 case WS_ENVELOPE_VERSION_SOAP_1_1:
1972 header = build_http_header( L"Content-Type", L"text/xml", &len );
1973 break;
1975 case WS_ENVELOPE_VERSION_SOAP_1_2:
1976 header = build_http_header( L"Content-Type", L"application/soap+xml", &len );
1977 break;
1979 default:
1980 FIXME( "unhandled envelope version %u\n", msg->version_env );
1981 hr = E_NOTIMPL;
1983 if (!header) goto done;
1985 if ((hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_ADD )) != S_OK) goto done;
1986 free( header );
1988 hr = E_OUTOFMEMORY;
1989 if (!(header = build_http_header( L"Content-Type", L"charset=utf-8", &len ))) goto done;
1990 if ((hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON )) != S_OK)
1991 goto done;
1992 free( header );
1993 header = NULL;
1995 switch (msg->version_env)
1997 case WS_ENVELOPE_VERSION_SOAP_1_1:
1999 if (!(len = MultiByteToWideChar( CP_UTF8, 0, (char *)msg->action->bytes, msg->action->length, NULL, 0 )))
2000 break;
2002 hr = E_OUTOFMEMORY;
2003 if (!(buf = malloc( (len + 3) * sizeof(WCHAR) ))) goto done;
2004 buf[0] = '"';
2005 MultiByteToWideChar( CP_UTF8, 0, (char *)msg->action->bytes, msg->action->length, buf + 1, len );
2006 buf[len + 1] = '"';
2007 buf[len + 2] = 0;
2009 header = build_http_header( L"SOAPAction", buf, &len );
2010 free( buf );
2011 if (!header) goto done;
2013 hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_ADD );
2014 break;
2016 case WS_ENVELOPE_VERSION_SOAP_1_2:
2018 ULONG len_action = ARRAY_SIZE( L"action=\"" ) - 1;
2020 if (!(len = MultiByteToWideChar( CP_UTF8, 0, (char *)msg->action->bytes, msg->action->length, NULL, 0 )))
2021 break;
2023 hr = E_OUTOFMEMORY;
2024 if (!(buf = malloc( (len + len_action + 2) * sizeof(WCHAR) ))) goto done;
2025 memcpy( buf, L"action=\"", len_action * sizeof(WCHAR) );
2026 MultiByteToWideChar( CP_UTF8, 0, (char *)msg->action->bytes, msg->action->length, buf + len_action, len );
2027 len += len_action;
2028 buf[len++] = '"';
2029 buf[len] = 0;
2031 header = build_http_header( L"Content-Type", buf, &len );
2032 free( buf );
2033 if (!header) goto done;
2035 hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON );
2036 break;
2038 default:
2039 FIXME( "unhandled envelope version %u\n", msg->version_env );
2040 hr = E_NOTIMPL;
2043 if (hr == S_OK) hr = insert_mapped_headers( msg, req );
2045 done:
2046 free( header );
2047 LeaveCriticalSection( &msg->cs );
2048 TRACE( "returning %#lx\n", hr );
2049 return hr;
2052 static HRESULT map_http_response_headers( struct msg *msg, HINTERNET req, const WS_HTTP_MESSAGE_MAPPING *mapping )
2054 ULONG i;
2055 for (i = 0; i < mapping->responseHeaderMappingCount; i++)
2057 WCHAR *name, *value;
2058 DWORD size;
2060 if (!(name = from_xml_string( &mapping->responseHeaderMappings[i]->headerName ))) return E_OUTOFMEMORY;
2062 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_CUSTOM, name, NULL, &size, NULL ) &&
2063 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2065 HRESULT hr;
2066 if (!(value = malloc( size )))
2068 free( name );
2069 return E_OUTOFMEMORY;
2071 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_CUSTOM, name, value, &size, NULL ))
2073 free( name );
2074 return HRESULT_FROM_WIN32( GetLastError() );
2076 hr = add_mapped_header( msg, &mapping->responseHeaderMappings[i]->headerName, WS_WSZ_TYPE,
2077 WS_WRITE_REQUIRED_POINTER, &value, sizeof(value) );
2078 free( value );
2079 if (hr != S_OK)
2081 free( name );
2082 return hr;
2085 free( name );
2087 return S_OK;
2090 HRESULT message_map_http_response_headers( WS_MESSAGE *handle, HINTERNET req, const WS_HTTP_MESSAGE_MAPPING *mapping )
2092 struct msg *msg = (struct msg *)handle;
2093 HRESULT hr;
2095 EnterCriticalSection( &msg->cs );
2097 if (msg->magic != MSG_MAGIC)
2099 LeaveCriticalSection( &msg->cs );
2100 return E_INVALIDARG;
2103 hr = map_http_response_headers( msg, req, mapping );
2105 LeaveCriticalSection( &msg->cs );
2106 TRACE( "returning %#lx\n", hr );
2107 return hr;
2110 void message_set_send_context( WS_MESSAGE *handle, const WS_PROXY_MESSAGE_CALLBACK_CONTEXT *ctx )
2112 struct msg *msg = (struct msg *)handle;
2114 EnterCriticalSection( &msg->cs );
2116 if (msg->magic != MSG_MAGIC)
2118 LeaveCriticalSection( &msg->cs );
2119 return;
2122 msg->ctx_send.callback = ctx->callback;
2123 msg->ctx_send.state = ctx->state;
2125 LeaveCriticalSection( &msg->cs );
2128 void message_set_receive_context( WS_MESSAGE *handle, const WS_PROXY_MESSAGE_CALLBACK_CONTEXT *ctx )
2130 struct msg *msg = (struct msg *)handle;
2132 EnterCriticalSection( &msg->cs );
2134 if (msg->magic != MSG_MAGIC)
2136 LeaveCriticalSection( &msg->cs );
2137 return;
2140 msg->ctx_receive.callback = ctx->callback;
2141 msg->ctx_receive.state = ctx->state;
2143 LeaveCriticalSection( &msg->cs );
2146 void message_do_send_callback( WS_MESSAGE *handle )
2148 struct msg *msg = (struct msg *)handle;
2150 EnterCriticalSection( &msg->cs );
2152 if (msg->magic != MSG_MAGIC)
2154 LeaveCriticalSection( &msg->cs );
2155 return;
2158 if (msg->ctx_send.callback)
2160 HRESULT hr;
2161 TRACE( "executing callback %p\n", msg->ctx_send.callback );
2162 hr = msg->ctx_send.callback( handle, msg->heap, msg->ctx_send.state, NULL );
2163 TRACE( "callback %p returned %#lx\n", msg->ctx_send.callback, hr );
2166 LeaveCriticalSection( &msg->cs );
2169 void message_do_receive_callback( WS_MESSAGE *handle )
2171 struct msg *msg = (struct msg *)handle;
2173 EnterCriticalSection( &msg->cs );
2175 if (msg->magic != MSG_MAGIC)
2177 LeaveCriticalSection( &msg->cs );
2178 return;
2181 if (msg->ctx_receive.callback)
2183 HRESULT hr;
2184 TRACE( "executing callback %p\n", msg->ctx_receive.callback );
2185 hr = msg->ctx_receive.callback( handle, msg->heap, msg->ctx_receive.state, NULL );
2186 TRACE( "callback %p returned %#lx\n", msg->ctx_receive.callback, hr );
2189 LeaveCriticalSection( &msg->cs );
2192 HRESULT message_set_action( WS_MESSAGE *handle, const WS_XML_STRING *action )
2194 struct msg *msg = (struct msg *)handle;
2195 HRESULT hr = S_OK;
2197 EnterCriticalSection( &msg->cs );
2199 if (msg->magic != MSG_MAGIC)
2201 LeaveCriticalSection( &msg->cs );
2202 return E_INVALIDARG;
2205 if (!action || !action->length)
2207 free_xml_string( msg->action );
2208 msg->action = NULL;
2210 else
2212 WS_XML_STRING *str;
2213 if (!(str = dup_xml_string( action, FALSE ))) hr = E_OUTOFMEMORY;
2214 else
2216 free_xml_string( msg->action );
2217 msg->action = str;
2221 LeaveCriticalSection( &msg->cs );
2222 return hr;
2225 HRESULT message_get_id( WS_MESSAGE *handle, GUID *id )
2227 struct msg *msg = (struct msg *)handle;
2228 HRESULT hr = S_OK;
2230 EnterCriticalSection( &msg->cs );
2232 if (msg->magic != MSG_MAGIC)
2234 LeaveCriticalSection( &msg->cs );
2235 return E_INVALIDARG;
2238 *id = msg->id_req;
2240 LeaveCriticalSection( &msg->cs );
2241 return hr;
2244 HRESULT message_set_request_id( WS_MESSAGE *handle, const GUID *id )
2246 struct msg *msg = (struct msg *)handle;
2247 HRESULT hr = S_OK;
2249 EnterCriticalSection( &msg->cs );
2251 if (msg->magic != MSG_MAGIC)
2253 LeaveCriticalSection( &msg->cs );
2254 return E_INVALIDARG;
2257 msg->id_req = *id;
2259 LeaveCriticalSection( &msg->cs );
2260 return hr;
2263 /* Attempt to read a fault message. If the message is a fault, WS_E_ENDPOINT_FAULT_RECEIVED will be returned. */
2264 HRESULT message_read_fault( WS_MESSAGE *handle, WS_HEAP *heap, WS_ERROR *error )
2266 static const WS_ELEMENT_DESCRIPTION desc = { NULL, NULL, WS_FAULT_TYPE, NULL };
2267 BOOL isfault;
2268 WS_FAULT fault = {0};
2269 WS_XML_STRING action;
2270 HRESULT hr;
2272 if ((hr = WsGetMessageProperty( handle, WS_MESSAGE_PROPERTY_IS_FAULT, &isfault, sizeof(isfault), NULL )) != S_OK)
2273 return hr;
2274 if (!isfault)
2275 return S_OK;
2277 if ((hr = WsReadBody( handle, &desc, WS_READ_REQUIRED_VALUE, heap, &fault, sizeof(fault), NULL )) != S_OK ||
2278 (hr = WsReadEnvelopeEnd( handle, NULL )) != S_OK)
2279 goto done;
2281 if (!error) goto done;
2283 if ((hr = WsSetFaultErrorProperty( error, WS_FAULT_ERROR_PROPERTY_FAULT, &fault, sizeof(fault) )) != S_OK)
2284 goto done;
2286 if ((hr = WsGetHeader( handle, WS_ACTION_HEADER, WS_XML_STRING_TYPE, WS_READ_REQUIRED_VALUE,
2287 heap, &action, sizeof(action), NULL )) != S_OK)
2289 if (hr == WS_E_INVALID_FORMAT)
2291 memset( &action, 0, sizeof(action) );
2292 hr = S_OK;
2294 else
2295 goto done;
2297 hr = WsSetFaultErrorProperty( error, WS_FAULT_ERROR_PROPERTY_ACTION, &action, sizeof(action) );
2299 done:
2300 free_fault_fields( heap, &fault );
2301 return hr != S_OK ? hr : WS_E_ENDPOINT_FAULT_RECEIVED;