conhost: Fix handling selection boundaries in copy_selection.
[wine/zf.git] / dlls / webservices / msg.c
blobd955e173b4fa58c02cfda929ba1a5345bf2d2777
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 "rpc.h"
25 #include "webservices.h"
27 #include "wine/debug.h"
28 #include "wine/heap.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 */
47 struct header
49 WS_HEADER_TYPE type;
50 BOOL mapped;
51 WS_XML_STRING name;
52 WS_XML_STRING ns;
53 union
55 WS_XML_BUFFER *buf;
56 WS_XML_STRING *text;
57 } u;
60 struct msg
62 ULONG magic;
63 CRITICAL_SECTION cs;
64 WS_MESSAGE_INITIALIZATION init;
65 WS_MESSAGE_STATE state;
66 GUID id;
67 GUID id_req;
68 WS_ENVELOPE_VERSION version_env;
69 WS_ADDRESSING_VERSION version_addr;
70 BOOL is_addressed;
71 WS_STRING addr;
72 WS_XML_STRING *action;
73 WS_HEAP *heap;
74 WS_XML_BUFFER *buf;
75 WS_XML_WRITER *writer;
76 WS_XML_WRITER *writer_body;
77 WS_XML_READER *reader;
78 WS_XML_READER *reader_body;
79 ULONG header_count;
80 ULONG header_size;
81 struct header **header;
82 WS_PROXY_MESSAGE_CALLBACK_CONTEXT ctx_send;
83 WS_PROXY_MESSAGE_CALLBACK_CONTEXT ctx_receive;
84 ULONG prop_count;
85 struct prop prop[ARRAY_SIZE( msg_props )];
88 #define MSG_MAGIC (('M' << 24) | ('E' << 16) | ('S' << 8) | 'S')
89 #define HEADER_ARRAY_SIZE 2
91 static struct msg *alloc_msg(void)
93 static const ULONG count = ARRAY_SIZE( msg_props );
94 struct msg *ret;
95 ULONG size = sizeof(*ret) + prop_size( msg_props, count );
97 if (!(ret = heap_alloc_zero( size ))) return NULL;
98 if (!(ret->header = heap_alloc( HEADER_ARRAY_SIZE * sizeof(struct header *) )))
100 heap_free( ret );
101 return NULL;
103 ret->magic = MSG_MAGIC;
104 ret->state = WS_MESSAGE_STATE_EMPTY;
105 ret->header_size = HEADER_ARRAY_SIZE;
107 InitializeCriticalSection( &ret->cs );
108 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": msg.cs");
110 prop_init( msg_props, count, ret->prop, &ret[1] );
111 ret->prop_count = count;
112 return ret;
115 static void free_header( struct header *header )
117 heap_free( header->name.bytes );
118 heap_free( header->ns.bytes );
119 if (header->mapped) free_xml_string( header->u.text );
120 heap_free( header );
123 static void reset_msg( struct msg *msg )
125 ULONG i;
127 msg->state = WS_MESSAGE_STATE_EMPTY;
128 msg->init = 0;
129 UuidCreate( &msg->id );
130 memset( &msg->id_req, 0, sizeof(msg->id_req) );
131 msg->is_addressed = FALSE;
132 heap_free( msg->addr.chars );
133 msg->addr.chars = NULL;
134 msg->addr.length = 0;
136 free_xml_string( msg->action );
137 msg->action = NULL;
139 WsResetHeap( msg->heap, NULL );
140 msg->buf = NULL; /* allocated on msg->heap */
141 msg->writer_body = NULL; /* owned by caller */
142 msg->reader_body = NULL; /* owned by caller */
144 for (i = 0; i < msg->header_count; i++)
146 free_header( msg->header[i] );
147 msg->header[i] = NULL;
149 msg->header_count = 0;
151 memset( &msg->ctx_send, 0, sizeof(msg->ctx_send) );
152 memset( &msg->ctx_receive, 0, sizeof(msg->ctx_receive) );
155 static void free_msg( struct msg *msg )
157 reset_msg( msg );
159 WsFreeWriter( msg->writer );
160 WsFreeReader( msg->reader );
161 WsFreeHeap( msg->heap );
162 heap_free( msg->header );
164 msg->cs.DebugInfo->Spare[0] = 0;
165 DeleteCriticalSection( &msg->cs );
166 heap_free( msg );
169 #define HEAP_MAX_SIZE (1 << 16)
170 static HRESULT create_msg( WS_ENVELOPE_VERSION env_version, WS_ADDRESSING_VERSION addr_version,
171 const WS_MESSAGE_PROPERTY *properties, ULONG count, WS_MESSAGE **handle )
173 struct msg *msg;
174 HRESULT hr;
175 ULONG i;
177 if (!(msg = alloc_msg())) return E_OUTOFMEMORY;
179 for (i = 0; i < count; i++)
181 if (properties[i].id == WS_MESSAGE_PROPERTY_ENVELOPE_VERSION ||
182 properties[i].id == WS_MESSAGE_PROPERTY_ADDRESSING_VERSION)
184 free_msg( msg );
185 return E_INVALIDARG;
187 hr = prop_set( msg->prop, msg->prop_count, properties[i].id, properties[i].value,
188 properties[i].valueSize );
189 if (hr != S_OK)
191 free_msg( msg );
192 return hr;
196 if ((hr = WsCreateHeap( HEAP_MAX_SIZE, 0, NULL, 0, &msg->heap, NULL )) != S_OK)
198 free_msg( msg );
199 return hr;
202 UuidCreate( &msg->id );
203 msg->version_env = env_version;
204 msg->version_addr = addr_version;
206 *handle = (WS_MESSAGE *)msg;
207 return S_OK;
210 /**************************************************************************
211 * WsCreateMessage [webservices.@]
213 HRESULT WINAPI WsCreateMessage( WS_ENVELOPE_VERSION env_version, WS_ADDRESSING_VERSION addr_version,
214 const WS_MESSAGE_PROPERTY *properties, ULONG count, WS_MESSAGE **handle,
215 WS_ERROR *error )
217 HRESULT hr;
219 TRACE( "%u %u %p %u %p %p\n", env_version, addr_version, properties, count, handle, error );
220 if (error) FIXME( "ignoring error parameter\n" );
222 if (!handle || !env_version || !addr_version ||
223 (env_version == WS_ENVELOPE_VERSION_NONE && addr_version != WS_ADDRESSING_VERSION_TRANSPORT))
225 return E_INVALIDARG;
228 if ((hr = create_msg( env_version, addr_version, properties, count, handle )) != S_OK) return hr;
229 TRACE( "created %p\n", *handle );
230 return S_OK;
233 /**************************************************************************
234 * WsCreateMessageForChannel [webservices.@]
236 HRESULT WINAPI WsCreateMessageForChannel( WS_CHANNEL *channel_handle, const WS_MESSAGE_PROPERTY *properties,
237 ULONG count, WS_MESSAGE **handle, WS_ERROR *error )
239 WS_ENVELOPE_VERSION version_env;
240 WS_ADDRESSING_VERSION version_addr;
241 HRESULT hr;
243 TRACE( "%p %p %u %p %p\n", channel_handle, properties, count, handle, error );
244 if (error) FIXME( "ignoring error parameter\n" );
246 if (!channel_handle || !handle) return E_INVALIDARG;
248 if ((hr = WsGetChannelProperty( channel_handle, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version_env,
249 sizeof(version_env), NULL )) != S_OK || !version_env)
250 version_env = WS_ENVELOPE_VERSION_SOAP_1_2;
252 if ((hr = WsGetChannelProperty( channel_handle, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION, &version_addr,
253 sizeof(version_addr), NULL )) != S_OK || !version_addr)
254 version_addr = WS_ADDRESSING_VERSION_1_0;
256 if ((hr = create_msg( version_env, version_addr, properties, count, handle )) != S_OK) return hr;
257 TRACE( "created %p\n", *handle );
258 return S_OK;
261 /**************************************************************************
262 * WsFreeMessage [webservices.@]
264 void WINAPI WsFreeMessage( WS_MESSAGE *handle )
266 struct msg *msg = (struct msg *)handle;
268 TRACE( "%p\n", handle );
270 if (!msg) return;
272 EnterCriticalSection( &msg->cs );
274 if (msg->magic != MSG_MAGIC)
276 LeaveCriticalSection( &msg->cs );
277 return;
280 msg->magic = 0;
282 LeaveCriticalSection( &msg->cs );
283 free_msg( msg );
286 /**************************************************************************
287 * WsResetMessage [webservices.@]
289 HRESULT WINAPI WsResetMessage( WS_MESSAGE *handle, WS_ERROR *error )
291 struct msg *msg = (struct msg *)handle;
292 HRESULT hr = S_OK;
294 TRACE( "%p %p\n", handle, error );
295 if (error) FIXME( "ignoring error parameter\n" );
297 if (!msg) return E_INVALIDARG;
299 EnterCriticalSection( &msg->cs );
301 if (msg->magic != MSG_MAGIC)
303 LeaveCriticalSection( &msg->cs );
304 return E_INVALIDARG;
307 reset_msg( msg );
309 LeaveCriticalSection( &msg->cs );
310 TRACE( "returning %08x\n", hr );
311 return hr;
314 /**************************************************************************
315 * WsGetMessageProperty [webservices.@]
317 HRESULT WINAPI WsGetMessageProperty( WS_MESSAGE *handle, WS_MESSAGE_PROPERTY_ID id, void *buf,
318 ULONG size, WS_ERROR *error )
320 struct msg *msg = (struct msg *)handle;
321 HRESULT hr = S_OK;
323 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
324 if (error) FIXME( "ignoring error parameter\n" );
326 if (!msg) return E_INVALIDARG;
328 EnterCriticalSection( &msg->cs );
330 if (msg->magic != MSG_MAGIC)
332 LeaveCriticalSection( &msg->cs );
333 return E_INVALIDARG;
336 switch (id)
338 case WS_MESSAGE_PROPERTY_STATE:
339 if (!buf || size != sizeof(msg->state)) hr = E_INVALIDARG;
340 else *(WS_MESSAGE_STATE *)buf = msg->state;
341 break;
343 case WS_MESSAGE_PROPERTY_HEAP:
344 if (!buf || size != sizeof(msg->heap)) hr = E_INVALIDARG;
345 else *(WS_HEAP **)buf = msg->heap;
346 break;
348 case WS_MESSAGE_PROPERTY_ENVELOPE_VERSION:
349 if (!buf || size != sizeof(msg->version_env)) hr = E_INVALIDARG;
350 else *(WS_ENVELOPE_VERSION *)buf = msg->version_env;
351 break;
353 case WS_MESSAGE_PROPERTY_ADDRESSING_VERSION:
354 if (!buf || size != sizeof(msg->version_addr)) hr = E_INVALIDARG;
355 else *(WS_ADDRESSING_VERSION *)buf = msg->version_addr;
356 break;
358 case WS_MESSAGE_PROPERTY_HEADER_BUFFER:
359 if (!buf || size != sizeof(msg->buf)) hr = E_INVALIDARG;
360 else *(WS_XML_BUFFER **)buf = msg->buf;
361 break;
363 case WS_MESSAGE_PROPERTY_BODY_READER:
364 if (!buf || size != sizeof(msg->reader_body)) hr = E_INVALIDARG;
365 else *(WS_XML_READER **)buf = msg->reader_body;
366 break;
368 case WS_MESSAGE_PROPERTY_BODY_WRITER:
369 if (!buf || size != sizeof(msg->writer_body)) hr = E_INVALIDARG;
370 else *(WS_XML_WRITER **)buf = msg->writer_body;
371 break;
373 case WS_MESSAGE_PROPERTY_IS_ADDRESSED:
374 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
375 else *(BOOL *)buf = msg->is_addressed;
376 break;
378 default:
379 hr = prop_get( msg->prop, msg->prop_count, id, buf, size );
382 LeaveCriticalSection( &msg->cs );
383 TRACE( "returning %08x\n", hr );
384 return hr;
387 /**************************************************************************
388 * WsSetMessageProperty [webservices.@]
390 HRESULT WINAPI WsSetMessageProperty( WS_MESSAGE *handle, WS_MESSAGE_PROPERTY_ID id, const void *value,
391 ULONG size, WS_ERROR *error )
393 struct msg *msg = (struct msg *)handle;
394 HRESULT hr;
396 TRACE( "%p %u %p %u\n", handle, id, value, size );
397 if (error) FIXME( "ignoring error parameter\n" );
399 if (!msg) return E_INVALIDARG;
401 EnterCriticalSection( &msg->cs );
403 if (msg->magic != MSG_MAGIC)
405 LeaveCriticalSection( &msg->cs );
406 return E_INVALIDARG;
409 switch (id)
411 case WS_MESSAGE_PROPERTY_STATE:
412 case WS_MESSAGE_PROPERTY_ENVELOPE_VERSION:
413 case WS_MESSAGE_PROPERTY_ADDRESSING_VERSION:
414 case WS_MESSAGE_PROPERTY_IS_ADDRESSED:
415 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
416 else hr = E_INVALIDARG;
417 break;
419 default:
420 hr = prop_set( msg->prop, msg->prop_count, id, value, size );
423 LeaveCriticalSection( &msg->cs );
424 TRACE( "returning %08x\n", hr );
425 return hr;
428 /**************************************************************************
429 * WsAddressMessage [webservices.@]
431 HRESULT WINAPI WsAddressMessage( WS_MESSAGE *handle, const WS_ENDPOINT_ADDRESS *addr, WS_ERROR *error )
433 struct msg *msg = (struct msg *)handle;
434 HRESULT hr = S_OK;
436 TRACE( "%p %p %p\n", handle, addr, error );
437 if (error) FIXME( "ignoring error parameter\n" );
438 if (addr && (addr->headers || addr->extensions || addr->identity))
440 FIXME( "headers, extensions or identity not supported\n" );
441 return E_NOTIMPL;
444 if (!msg) return E_INVALIDARG;
446 EnterCriticalSection( &msg->cs );
448 if (msg->magic != MSG_MAGIC)
450 LeaveCriticalSection( &msg->cs );
451 return E_INVALIDARG;
454 if (msg->state < WS_MESSAGE_STATE_INITIALIZED || msg->is_addressed) hr = WS_E_INVALID_OPERATION;
455 else if (addr && addr->url.length)
457 if (!(msg->addr.chars = heap_alloc( addr->url.length * sizeof(WCHAR) ))) hr = E_OUTOFMEMORY;
458 else
460 memcpy( msg->addr.chars, addr->url.chars, addr->url.length * sizeof(WCHAR) );
461 msg->addr.length = addr->url.length;
464 if (hr == S_OK) msg->is_addressed = TRUE;
466 LeaveCriticalSection( &msg->cs );
467 TRACE( "returning %08x\n", hr );
468 return hr;
471 static const WS_XML_STRING *get_env_namespace( WS_ENVELOPE_VERSION version )
473 static const WS_XML_STRING namespaces[] =
475 {41, (BYTE *)"http://schemas.xmlsoap.org/soap/envelope/"},
476 {39, (BYTE *)"http://www.w3.org/2003/05/soap-envelope"},
477 {0, NULL},
480 if (version < WS_ENVELOPE_VERSION_SOAP_1_1 || version > WS_ENVELOPE_VERSION_NONE)
482 ERR( "unknown version %u\n", version );
483 return NULL;
486 return &namespaces[version - 1];
489 static const WS_XML_STRING *get_addr_namespace( WS_ADDRESSING_VERSION version )
491 static const WS_XML_STRING namespaces[] =
493 {48, (BYTE *)"http://schemas.xmlsoap.org/ws/2004/08/addressing"},
494 {36, (BYTE *)"http://www.w3.org/2005/08/addressing"},
495 {55, (BYTE *)"http://schemas.microsoft.com/ws/2005/05/addressing/none"},
498 if (version < WS_ADDRESSING_VERSION_0_9 || version > WS_ADDRESSING_VERSION_TRANSPORT)
500 ERR( "unknown version %u\n", version );
501 return NULL;
504 return &namespaces[version - 1];
507 static const WS_XML_STRING *get_header_name( WS_HEADER_TYPE type )
509 static const WS_XML_STRING headers[] =
511 {6, (BYTE *)"Action"},
512 {2, (BYTE *)"To"},
513 {9, (BYTE *)"MessageID"},
514 {9, (BYTE *)"RelatesTo"},
515 {4, (BYTE *)"From"},
516 {7, (BYTE *)"ReplyTo"},
517 {7, (BYTE *)"FaultTo"},
520 if (type < WS_ACTION_HEADER || type > WS_FAULT_TO_HEADER)
522 ERR( "unknown type %u\n", type );
523 return NULL;
526 return &headers[type - 1];
529 static HRESULT write_must_understand( WS_XML_WRITER *writer, const WS_XML_STRING *prefix, const WS_XML_STRING *ns )
531 static const WS_XML_STRING understand = {14, (BYTE *)"mustUnderstand"};
532 WS_XML_INT32_TEXT one = {{WS_XML_TEXT_TYPE_INT32}, 1};
533 HRESULT hr;
535 if ((hr = WsWriteStartAttribute( writer, prefix, &understand, ns, FALSE, NULL )) != S_OK) return hr;
536 if ((hr = WsWriteText( writer, &one.text, NULL )) != S_OK) return hr;
537 return WsWriteEndAttribute( writer, NULL );
540 static HRESULT write_action_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
541 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
542 const WS_XML_STRING *ns_addr, const WS_XML_STRING *text )
544 WS_XML_UTF8_TEXT utf8 = {{WS_XML_TEXT_TYPE_UTF8}};
545 const WS_XML_STRING *action = get_header_name( WS_ACTION_HEADER );
546 HRESULT hr;
548 if (!text || !text->length) return S_OK;
549 utf8.value.length = text->length;
550 utf8.value.bytes = text->bytes;
552 if ((hr = WsWriteStartElement( writer, prefix_addr, action, ns_addr, NULL )) != S_OK) return hr;
553 if ((hr = write_must_understand( writer, prefix_env, ns_env )) != S_OK) return hr;
554 if ((hr = WsWriteText( writer, &utf8.text, NULL )) != S_OK) return hr;
555 return WsWriteEndElement( writer, NULL ); /* </a:Action> */
558 static HRESULT write_to_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
559 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
560 const WS_XML_STRING *ns_addr, const WS_STRING *addr )
562 WS_XML_UTF16_TEXT utf16 = {{WS_XML_TEXT_TYPE_UTF16}, (BYTE *)addr->chars, addr->length * sizeof(WCHAR)};
563 const WS_XML_STRING *to = get_header_name( WS_TO_HEADER );
564 HRESULT hr;
566 if ((hr = WsWriteStartElement( writer, prefix_addr, to, 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, &utf16.text, NULL )) != S_OK) return hr;
569 return WsWriteEndElement( writer, NULL ); /* </a:To> */
572 static HRESULT write_replyto_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 )
576 static const char anonymous[] = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
577 WS_XML_UTF8_TEXT utf8 = {{WS_XML_TEXT_TYPE_UTF8}, {sizeof(anonymous) - 1, (BYTE *)anonymous}};
578 const WS_XML_STRING address = {7, (BYTE *)"Address"}, *replyto = get_header_name( WS_REPLY_TO_HEADER );
579 HRESULT hr;
581 if ((hr = WsWriteStartElement( writer, prefix_addr, replyto, ns_addr, NULL )) != S_OK) return hr;
582 if ((hr = WsWriteStartElement( writer, prefix_addr, &address, ns_addr, NULL )) != S_OK) return hr;
583 if ((hr = WsWriteText( writer, &utf8.text, NULL )) != S_OK) return hr;
584 if ((hr = WsWriteEndElement( writer, NULL )) != S_OK) return hr; /* </a:Address> */
585 return WsWriteEndElement( writer, NULL ); /* </a:ReplyTo> */
588 static HRESULT write_msgid_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
589 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
590 const WS_XML_STRING *ns_addr, const GUID *guid )
592 WS_XML_UNIQUE_ID_TEXT id = {{WS_XML_TEXT_TYPE_UNIQUE_ID}, *guid};
593 const WS_XML_STRING *msgid = get_header_name( WS_MESSAGE_ID_HEADER );
594 HRESULT hr;
596 if ((hr = WsWriteStartElement( writer, prefix_addr, msgid, ns_addr, NULL )) != S_OK) return hr;
597 if ((hr = WsWriteText( writer, &id.text, NULL )) != S_OK) return hr;
598 return WsWriteEndElement( writer, NULL ); /* </a:MessageID> */
601 static HRESULT write_relatesto_header( WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
602 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
603 const WS_XML_STRING *ns_addr, const GUID *guid )
605 WS_XML_UNIQUE_ID_TEXT id = {{WS_XML_TEXT_TYPE_UNIQUE_ID}, *guid};
606 const WS_XML_STRING *relatesto = get_header_name( WS_RELATES_TO_HEADER );
607 HRESULT hr;
609 if ((hr = WsWriteStartElement( writer, prefix_addr, relatesto, ns_addr, NULL )) != S_OK) return hr;
610 if ((hr = WsWriteText( writer, &id.text, NULL )) != S_OK) return hr;
611 return WsWriteEndElement( writer, NULL ); /* </a:RelatesTo> */
614 static HRESULT write_headers( struct msg *msg, WS_XML_WRITER *writer, const WS_XML_STRING *prefix_env,
615 const WS_XML_STRING *ns_env, const WS_XML_STRING *prefix_addr,
616 const WS_XML_STRING *ns_addr )
618 static const WS_XML_STRING header = {6, (BYTE *)"Header"};
619 HRESULT hr;
620 ULONG i;
622 if ((hr = WsWriteXmlnsAttribute( writer, prefix_addr, ns_addr, FALSE, NULL )) != S_OK) return hr;
623 if ((hr = WsWriteStartElement( writer, prefix_env, &header, ns_env, NULL )) != S_OK) return hr;
625 if ((hr = write_action_header( writer, prefix_env, ns_env, prefix_addr, ns_addr, msg->action )) != S_OK)
626 return hr;
628 if (msg->init == WS_REPLY_MESSAGE)
630 if ((hr = write_relatesto_header( writer, prefix_env, ns_env, prefix_addr, ns_addr, &msg->id_req )) != S_OK)
631 return hr;
633 else if (msg->addr.length)
635 if ((hr = write_to_header( writer, prefix_env, ns_env, prefix_addr, ns_addr, &msg->addr )) != S_OK)
636 return hr;
638 else
640 if ((hr = write_msgid_header( writer, prefix_env, ns_env, prefix_addr, ns_addr, &msg->id )) != S_OK)
641 return hr;
642 if (msg->version_addr == WS_ADDRESSING_VERSION_0_9 &&
643 (hr = write_replyto_header( writer, prefix_env, ns_env, prefix_addr, ns_addr )) != S_OK) return hr;
646 for (i = 0; i < msg->header_count; i++)
648 if (msg->header[i]->mapped) continue;
649 if ((hr = WsWriteXmlBuffer( writer, msg->header[i]->u.buf, NULL )) != S_OK) return hr;
652 return WsWriteEndElement( writer, NULL ); /* </s:Header> */
655 static ULONG count_envelope_headers( struct msg *msg )
657 ULONG i, ret = 0;
658 for (i = 0; i < msg->header_count; i++)
660 if (!msg->header[i]->mapped) ret++;
662 return ret;
665 static HRESULT write_headers_transport( struct msg *msg, WS_XML_WRITER *writer, const WS_XML_STRING *prefix,
666 const WS_XML_STRING *ns )
668 static const WS_XML_STRING header = {6, (BYTE *)"Header"};
669 HRESULT hr = S_OK;
670 ULONG i;
672 if (count_envelope_headers( msg ) || !msg->action)
674 if ((hr = WsWriteStartElement( writer, prefix, &header, ns, NULL )) != S_OK) return hr;
676 for (i = 0; i < msg->header_count; i++)
678 if (msg->header[i]->mapped) continue;
679 if ((hr = WsWriteXmlBuffer( writer, msg->header[i]->u.buf, NULL )) != S_OK) return hr;
682 hr = WsWriteEndElement( writer, NULL ); /* </s:Header> */
685 return hr;
688 static HRESULT write_envelope_start( struct msg *msg, WS_XML_WRITER *writer )
690 static const WS_XML_STRING envelope = {8, (BYTE *)"Envelope"}, body = {4, (BYTE *)"Body"};
691 static const WS_XML_STRING prefix_s = {1, (BYTE *)"s"}, prefix_a = {1, (BYTE *)"a"};
692 const WS_XML_STRING *prefix_env = (msg->version_env == WS_ENVELOPE_VERSION_NONE) ? NULL : &prefix_s;
693 const WS_XML_STRING *prefix_addr = (msg->version_addr == WS_ADDRESSING_VERSION_TRANSPORT) ? NULL : &prefix_a;
694 const WS_XML_STRING *ns_env = get_env_namespace( msg->version_env );
695 const WS_XML_STRING *ns_addr = get_addr_namespace( msg->version_addr );
696 HRESULT hr;
698 if ((hr = WsWriteStartElement( writer, prefix_env, &envelope, ns_env, NULL )) != S_OK) return hr;
700 if (msg->version_addr == WS_ADDRESSING_VERSION_TRANSPORT)
701 hr = write_headers_transport( msg, writer, prefix_env, ns_env );
702 else
703 hr = write_headers( msg, writer, prefix_env, ns_env, prefix_addr, ns_addr );
704 if (hr != S_OK) return hr;
706 return WsWriteStartElement( writer, prefix_env, &body, ns_env, NULL ); /* <s:Body> */
709 static HRESULT write_envelope_end( WS_XML_WRITER *writer )
711 HRESULT hr;
712 if ((hr = WsWriteEndElement( writer, NULL )) != S_OK) return hr; /* </s:Body> */
713 return WsWriteEndElement( writer, NULL ); /* </s:Envelope> */
716 static HRESULT write_envelope( struct msg *msg )
718 HRESULT hr;
719 if (!msg->writer && (hr = WsCreateWriter( NULL, 0, &msg->writer, NULL )) != S_OK) return hr;
720 if (!msg->buf && (hr = WsCreateXmlBuffer( msg->heap, NULL, 0, &msg->buf, NULL )) != S_OK) return hr;
721 if ((hr = WsSetOutputToBuffer( msg->writer, msg->buf, NULL, 0, NULL )) != S_OK) return hr;
722 if ((hr = write_envelope_start( msg, msg->writer )) != S_OK) return hr;
723 return write_envelope_end( msg->writer );
726 /**************************************************************************
727 * WsWriteEnvelopeStart [webservices.@]
729 HRESULT WINAPI WsWriteEnvelopeStart( WS_MESSAGE *handle, WS_XML_WRITER *writer,
730 WS_MESSAGE_DONE_CALLBACK cb, void *state, WS_ERROR *error )
732 struct msg *msg = (struct msg *)handle;
733 HRESULT hr;
735 TRACE( "%p %p %p %p %p\n", handle, writer, cb, state, error );
736 if (error) FIXME( "ignoring error parameter\n" );
737 if (cb)
739 FIXME( "callback not supported\n" );
740 return E_NOTIMPL;
743 if (!msg || !writer) return E_INVALIDARG;
745 EnterCriticalSection( &msg->cs );
747 if (msg->magic != MSG_MAGIC)
749 LeaveCriticalSection( &msg->cs );
750 return E_INVALIDARG;
753 if (msg->state != WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
754 else if ((hr = write_envelope( msg )) == S_OK &&
755 (hr = write_envelope_start( msg, writer )) == S_OK)
757 msg->writer_body = writer;
758 msg->state = WS_MESSAGE_STATE_WRITING;
761 LeaveCriticalSection( &msg->cs );
762 TRACE( "returning %08x\n", hr );
763 return hr;
766 /**************************************************************************
767 * WsWriteEnvelopeEnd [webservices.@]
769 HRESULT WINAPI WsWriteEnvelopeEnd( WS_MESSAGE *handle, WS_ERROR *error )
771 struct msg *msg = (struct msg *)handle;
772 HRESULT hr;
774 TRACE( "%p %p\n", handle, error );
775 if (error) FIXME( "ignoring error parameter\n" );
777 if (!msg) return E_INVALIDARG;
779 EnterCriticalSection( &msg->cs );
781 if (msg->magic != MSG_MAGIC)
783 LeaveCriticalSection( &msg->cs );
784 return E_INVALIDARG;
787 if (msg->state != WS_MESSAGE_STATE_WRITING) hr = WS_E_INVALID_OPERATION;
788 else if ((hr = write_envelope_end( msg->writer_body )) == S_OK)
789 msg->state = WS_MESSAGE_STATE_DONE;
791 LeaveCriticalSection( &msg->cs );
792 TRACE( "returning %08x\n", hr );
793 return hr;
796 /**************************************************************************
797 * WsWriteBody [webservices.@]
799 HRESULT WINAPI WsWriteBody( WS_MESSAGE *handle, const WS_ELEMENT_DESCRIPTION *desc, WS_WRITE_OPTION option,
800 const void *value, ULONG size, WS_ERROR *error )
802 struct msg *msg = (struct msg *)handle;
803 HRESULT hr;
805 TRACE( "%p %p %08x %p %u %p\n", handle, desc, option, value, size, error );
806 if (error) FIXME( "ignoring error parameter\n" );
808 if (!msg || !desc) return E_INVALIDARG;
810 EnterCriticalSection( &msg->cs );
812 if (msg->magic != MSG_MAGIC)
814 LeaveCriticalSection( &msg->cs );
815 return E_INVALIDARG;
818 if (msg->state != WS_MESSAGE_STATE_WRITING)
820 hr = WS_E_INVALID_OPERATION;
821 goto done;
824 if (desc->elementLocalName &&
825 (hr = WsWriteStartElement( msg->writer_body, NULL, desc->elementLocalName, desc->elementNs,
826 NULL )) != S_OK) goto done;
828 if ((hr = WsWriteType( msg->writer_body, WS_ANY_ELEMENT_TYPE_MAPPING, desc->type, desc->typeDescription,
829 option, value, size, NULL )) != S_OK) goto done;
831 if (desc->elementLocalName) hr = WsWriteEndElement( msg->writer_body, NULL );
833 done:
834 LeaveCriticalSection( &msg->cs );
835 TRACE( "returning %08x\n", hr );
836 return hr;
839 /**************************************************************************
840 * WsFlushBody [webservices.@]
842 HRESULT WINAPI WsFlushBody( WS_MESSAGE *handle, ULONG size, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
844 struct msg *msg = (struct msg *)handle;
845 HRESULT hr;
847 TRACE( "%p %u %p %p\n", handle, size, ctx, error );
849 if (!msg) return E_INVALIDARG;
851 EnterCriticalSection( &msg->cs );
853 if (msg->magic != MSG_MAGIC)
855 LeaveCriticalSection( &msg->cs );
856 return E_INVALIDARG;
859 hr = WsFlushWriter( msg->writer_body, size, ctx, error );
861 LeaveCriticalSection( &msg->cs );
862 TRACE( "returning %08x\n", hr );
863 return hr;
866 static BOOL match_current_element( WS_XML_READER *reader, const WS_XML_STRING *localname )
868 const WS_XML_NODE *node;
869 const WS_XML_ELEMENT_NODE *elem;
871 if (WsGetReaderNode( reader, &node, NULL ) != S_OK) return FALSE;
872 if (node->nodeType != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
873 elem = (const WS_XML_ELEMENT_NODE *)node;
874 return WsXmlStringEquals( elem->localName, localname, NULL ) == S_OK;
877 static HRESULT read_message_id( WS_XML_READER *reader, GUID *ret )
879 const WS_XML_NODE *node;
880 const WS_XML_TEXT_NODE *text;
881 HRESULT hr;
883 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
884 if ((hr = WsGetReaderNode( reader, &node, NULL )) != S_OK) return hr;
885 if (node->nodeType != WS_XML_NODE_TYPE_TEXT) return WS_E_INVALID_FORMAT;
886 text = (const WS_XML_TEXT_NODE *)node;
888 switch (text->text->textType)
890 case WS_XML_TEXT_TYPE_UTF8:
892 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text->text;
893 if (utf8->value.length != 45 || memcmp( utf8->value.bytes, "urn:uuid:", 9 ))
894 return WS_E_INVALID_FORMAT;
896 return str_to_guid( utf8->value.bytes + 9, utf8->value.length - 9, ret );
898 case WS_XML_TEXT_TYPE_UNIQUE_ID:
900 const WS_XML_UNIQUE_ID_TEXT *id = (const WS_XML_UNIQUE_ID_TEXT *)text->text;
901 *ret = id->value;
902 return S_OK;
904 default:
905 FIXME( "unhandled text type %u\n", text->text->textType );
906 return E_NOTIMPL;
910 static HRESULT read_envelope_start( struct msg *msg, WS_XML_READER *reader )
912 static const WS_XML_STRING envelope = {8, (BYTE *)"Envelope"}, body = {4, (BYTE *)"Body"};
913 static const WS_XML_STRING header = {6, (BYTE *)"Header"}, msgid = {9, (BYTE *)"MessageID"};
914 HRESULT hr;
916 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
917 if (!match_current_element( reader, &envelope )) return WS_E_INVALID_FORMAT;
918 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
919 if (match_current_element( reader, &header ))
921 for (;;)
923 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
924 if (match_current_element( reader, &msgid ) && (hr = read_message_id( reader, &msg->id_req )) != S_OK)
925 return hr;
926 if (match_current_element( reader, &body )) break;
929 if (!match_current_element( reader, &body )) return WS_E_INVALID_FORMAT;
930 return WsReadNode( reader, NULL );
933 /**************************************************************************
934 * WsReadEnvelopeStart [webservices.@]
936 HRESULT WINAPI WsReadEnvelopeStart( WS_MESSAGE *handle, WS_XML_READER *reader, WS_MESSAGE_DONE_CALLBACK cb,
937 void *state, WS_ERROR *error )
939 struct msg *msg = (struct msg *)handle;
940 HRESULT hr;
942 TRACE( "%p %p %p %p %p\n", handle, reader, cb, state, error );
943 if (error) FIXME( "ignoring error parameter\n" );
944 if (cb)
946 FIXME( "callback not supported\n" );
947 return E_NOTIMPL;
950 if (!msg || !reader) return E_INVALIDARG;
952 EnterCriticalSection( &msg->cs );
954 if (msg->magic != MSG_MAGIC)
956 LeaveCriticalSection( &msg->cs );
957 return E_INVALIDARG;
960 if (msg->state != WS_MESSAGE_STATE_EMPTY) hr = WS_E_INVALID_OPERATION;
961 else if ((hr = read_envelope_start( msg, reader )) == S_OK &&
962 (hr = create_header_buffer( reader, msg->heap, &msg->buf )) == S_OK)
964 msg->reader_body = reader;
965 msg->state = WS_MESSAGE_STATE_READING;
968 LeaveCriticalSection( &msg->cs );
969 TRACE( "returning %08x\n", hr );
970 return hr;
973 /**************************************************************************
974 * WsReadEnvelopeEnd [webservices.@]
976 HRESULT WINAPI WsReadEnvelopeEnd( WS_MESSAGE *handle, WS_ERROR *error )
978 struct msg *msg = (struct msg *)handle;
979 HRESULT hr;
981 TRACE( "%p %p\n", handle, error );
982 if (error) FIXME( "ignoring error parameter\n" );
984 if (!msg) return E_INVALIDARG;
986 EnterCriticalSection( &msg->cs );
988 if (msg->magic != MSG_MAGIC)
990 LeaveCriticalSection( &msg->cs );
991 return E_INVALIDARG;
994 if (msg->state != WS_MESSAGE_STATE_READING) hr = WS_E_INVALID_OPERATION;
995 else if ((hr = WsReadEndElement( msg->reader_body, NULL )) == S_OK)
996 msg->state = WS_MESSAGE_STATE_DONE;
998 LeaveCriticalSection( &msg->cs );
999 TRACE( "returning %08x\n", hr );
1000 return hr;
1003 /**************************************************************************
1004 * WsReadBody [webservices.@]
1006 HRESULT WINAPI WsReadBody( WS_MESSAGE *handle, const WS_ELEMENT_DESCRIPTION *desc, WS_READ_OPTION option,
1007 WS_HEAP *heap, void *value, ULONG size, WS_ERROR *error )
1009 struct msg *msg = (struct msg *)handle;
1010 HRESULT hr;
1012 TRACE( "%p %p %08x %p %p %u %p\n", handle, desc, option, heap, value, size, error );
1013 if (error) FIXME( "ignoring error parameter\n" );
1015 if (!msg || !desc) return E_INVALIDARG;
1017 EnterCriticalSection( &msg->cs );
1019 if (msg->magic != MSG_MAGIC)
1021 LeaveCriticalSection( &msg->cs );
1022 return E_INVALIDARG;
1025 if (msg->state != WS_MESSAGE_STATE_READING) hr = WS_E_INVALID_OPERATION;
1026 else hr = WsReadElement( msg->reader_body, desc, option, heap, value, size, NULL );
1028 LeaveCriticalSection( &msg->cs );
1029 TRACE( "returning %08x\n", hr );
1030 return hr;
1033 /**************************************************************************
1034 * WsFillBody [webservices.@]
1036 HRESULT WINAPI WsFillBody( WS_MESSAGE *handle, ULONG size, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1038 struct msg *msg = (struct msg *)handle;
1039 HRESULT hr;
1041 TRACE( "%p %u %p %p\n", handle, size, ctx, error );
1043 if (!msg) return E_INVALIDARG;
1045 EnterCriticalSection( &msg->cs );
1047 if (msg->magic != MSG_MAGIC)
1049 LeaveCriticalSection( &msg->cs );
1050 return E_INVALIDARG;
1053 hr = WsFillReader( msg->reader_body, size, ctx, error );
1055 LeaveCriticalSection( &msg->cs );
1056 TRACE( "returning %08x\n", hr );
1057 return hr;
1060 /**************************************************************************
1061 * WsInitializeMessage [webservices.@]
1063 HRESULT WINAPI WsInitializeMessage( WS_MESSAGE *handle, WS_MESSAGE_INITIALIZATION init,
1064 WS_MESSAGE *src_handle, WS_ERROR *error )
1066 struct msg *msg = (struct msg *)handle;
1067 HRESULT hr;
1069 TRACE( "%p %u %p %p\n", handle, init, src_handle, error );
1070 if (error) FIXME( "ignoring error parameter\n" );
1071 if (src_handle)
1073 FIXME( "src message not supported\n" );
1074 return E_NOTIMPL;
1077 if (!msg || init > WS_FAULT_MESSAGE) return E_INVALIDARG;
1079 EnterCriticalSection( &msg->cs );
1081 if (msg->magic != MSG_MAGIC)
1083 LeaveCriticalSection( &msg->cs );
1084 return E_INVALIDARG;
1087 if (msg->state >= WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1088 else if ((hr = write_envelope( msg )) == S_OK)
1090 msg->init = init;
1091 msg->state = WS_MESSAGE_STATE_INITIALIZED;
1094 LeaveCriticalSection( &msg->cs );
1095 TRACE( "returning %08x\n", hr );
1096 return hr;
1099 static HRESULT grow_header_array( struct msg *msg, ULONG size )
1101 struct header **tmp;
1102 if (size <= msg->header_size) return S_OK;
1103 if (!(tmp = heap_realloc( msg->header, 2 * msg->header_size * sizeof(struct header *) )))
1104 return E_OUTOFMEMORY;
1105 msg->header = tmp;
1106 msg->header_size *= 2;
1107 return S_OK;
1110 static struct header *alloc_header( WS_HEADER_TYPE type, BOOL mapped, const WS_XML_STRING *name,
1111 const WS_XML_STRING *ns )
1113 struct header *ret;
1114 if (!(ret = heap_alloc_zero( sizeof(*ret) ))) return NULL;
1115 if (name && name->length)
1117 if (!(ret->name.bytes = heap_alloc( name->length )))
1119 free_header( ret );
1120 return NULL;
1122 memcpy( ret->name.bytes, name->bytes, name->length );
1123 ret->name.length = name->length;
1125 if (ns && ns->length)
1127 if (!(ret->ns.bytes = heap_alloc( ns->length )))
1129 free_header( ret );
1130 return NULL;
1132 memcpy( ret->ns.bytes, ns->bytes, ns->length );
1133 ret->ns.length = ns->length;
1135 ret->type = type;
1136 ret->mapped = mapped;
1137 return ret;
1140 static HRESULT write_standard_header( struct msg *msg, WS_HEADER_TYPE type, WS_TYPE value_type,
1141 WS_WRITE_OPTION option, const void *value, ULONG size )
1143 static const WS_XML_STRING ns = {0, NULL};
1144 static const WS_XML_STRING prefix_s = {1, (BYTE *)"s"}, prefix_a = {1, (BYTE *)"a"};
1145 const WS_XML_STRING *prefix_env = (msg->version_env == WS_ENVELOPE_VERSION_NONE) ? NULL : &prefix_s;
1146 const WS_XML_STRING *prefix_addr = (msg->version_addr == WS_ADDRESSING_VERSION_TRANSPORT) ? NULL : &prefix_a;
1147 const WS_XML_STRING *localname = get_header_name( type );
1148 HRESULT hr;
1150 if ((hr = WsWriteStartElement( msg->writer, prefix_addr, localname, &ns, NULL )) != S_OK) return hr;
1151 if ((hr = write_must_understand( msg->writer, prefix_env, &ns )) != S_OK) return hr;
1152 if (msg->version_addr == WS_ADDRESSING_VERSION_TRANSPORT)
1154 const WS_XML_STRING *ns_addr = get_addr_namespace( WS_ADDRESSING_VERSION_TRANSPORT );
1155 if ((hr = WsWriteXmlnsAttribute( msg->writer, NULL, ns_addr, FALSE, NULL )) != S_OK) return hr;
1157 if ((hr = WsWriteType( msg->writer, WS_ELEMENT_CONTENT_TYPE_MAPPING, value_type, NULL, option, value, size,
1158 NULL )) != S_OK) return hr;
1159 return WsWriteEndElement( msg->writer, NULL );
1162 static HRESULT build_standard_header( struct msg *msg, WS_HEADER_TYPE type, WS_TYPE value_type,
1163 WS_WRITE_OPTION option, const void *value, ULONG size,
1164 struct header **ret )
1166 struct header *header;
1167 WS_XML_BUFFER *buf;
1168 HRESULT hr;
1170 if (!(header = alloc_header( type, FALSE, get_header_name(type), NULL ))) return E_OUTOFMEMORY;
1172 if (!msg->writer && (hr = WsCreateWriter( NULL, 0, &msg->writer, NULL )) != S_OK) goto done;
1173 if ((hr = WsCreateXmlBuffer( msg->heap, NULL, 0, &buf, NULL )) != S_OK) goto done;
1174 if ((hr = WsSetOutputToBuffer( msg->writer, buf, NULL, 0, NULL )) != S_OK) goto done;
1175 if ((hr = write_standard_header( msg, type, value_type, option, value, size )) != S_OK) goto done;
1177 header->u.buf = buf;
1179 done:
1180 if (hr != S_OK) free_header( header );
1181 else *ret = header;
1182 return hr;
1185 /**************************************************************************
1186 * WsSetHeader [webservices.@]
1188 HRESULT WINAPI WsSetHeader( WS_MESSAGE *handle, WS_HEADER_TYPE type, WS_TYPE value_type,
1189 WS_WRITE_OPTION option, const void *value, ULONG size, WS_ERROR *error )
1191 struct msg *msg = (struct msg *)handle;
1192 struct header *header;
1193 BOOL found = FALSE;
1194 HRESULT hr;
1195 ULONG i;
1197 TRACE( "%p %u %u %08x %p %u %p\n", handle, type, value_type, option, value, size, error );
1198 if (error) FIXME( "ignoring error parameter\n" );
1200 if (!msg || type < WS_ACTION_HEADER || type > WS_FAULT_TO_HEADER) return E_INVALIDARG;
1202 EnterCriticalSection( &msg->cs );
1204 if (msg->magic != MSG_MAGIC)
1206 LeaveCriticalSection( &msg->cs );
1207 return E_INVALIDARG;
1210 if (msg->state < WS_MESSAGE_STATE_INITIALIZED)
1212 hr = WS_E_INVALID_OPERATION;
1213 goto done;
1216 for (i = 0; i < msg->header_count; i++)
1218 if (msg->header[i]->type == type)
1220 found = TRUE;
1221 break;
1225 if (!found)
1227 if ((hr = grow_header_array( msg, msg->header_count + 1 )) != S_OK) goto done;
1228 i = msg->header_count;
1231 if ((hr = build_standard_header( msg, type, value_type, option, value, size, &header )) != S_OK)
1232 goto done;
1234 if (!found) msg->header_count++;
1235 else free_header( msg->header[i] );
1237 msg->header[i] = header;
1238 hr = write_envelope( msg );
1240 done:
1241 LeaveCriticalSection( &msg->cs );
1242 TRACE( "returning %08x\n", hr );
1243 return hr;
1246 static HRESULT find_header( WS_XML_READER *reader, const WS_XML_STRING *localname, const WS_XML_STRING *ns )
1248 const WS_XML_NODE *node;
1249 const WS_XML_ELEMENT_NODE *elem;
1250 HRESULT hr;
1252 for (;;)
1254 if ((hr = WsReadNode( reader, NULL )) != S_OK) return hr;
1255 if ((hr = WsGetReaderNode( reader, &node, NULL )) != S_OK) return hr;
1256 if (node->nodeType == WS_XML_NODE_TYPE_EOF) return WS_E_INVALID_FORMAT;
1257 if (node->nodeType != WS_XML_NODE_TYPE_ELEMENT) continue;
1259 elem = (const WS_XML_ELEMENT_NODE *)node;
1260 if (WsXmlStringEquals( elem->localName, localname, NULL ) == S_OK &&
1261 WsXmlStringEquals( elem->ns, ns, NULL ) == S_OK) break;
1264 return S_OK;
1267 static HRESULT get_standard_header( struct msg *msg, WS_HEADER_TYPE type, WS_TYPE value_type,
1268 WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size )
1270 const WS_XML_STRING *localname = get_header_name( type );
1271 const WS_XML_STRING *ns = get_addr_namespace( msg->version_addr );
1272 HRESULT hr;
1274 if (!heap) heap = msg->heap;
1275 if (!msg->reader && (hr = WsCreateReader( NULL, 0, &msg->reader, NULL )) != S_OK) return hr;
1276 if ((hr = WsSetInputToBuffer( msg->reader, msg->buf, NULL, 0, NULL )) != S_OK) return hr;
1278 if ((hr = find_header( msg->reader, localname, ns )) != S_OK) return hr;
1279 return read_header( msg->reader, localname, ns, value_type, NULL, option, heap, value, size );
1282 /**************************************************************************
1283 * WsGetHeader [webservices.@]
1285 HRESULT WINAPI WsGetHeader( WS_MESSAGE *handle, WS_HEADER_TYPE type, WS_TYPE value_type, WS_READ_OPTION option,
1286 WS_HEAP *heap, void *value, ULONG size, WS_ERROR *error )
1288 struct msg *msg = (struct msg *)handle;
1289 HRESULT hr;
1291 TRACE( "%p %u %u %08x %p %p %u %p\n", handle, type, value_type, option, heap, value, size, error );
1292 if (error) FIXME( "ignoring error parameter\n" );
1294 if (!msg || type < WS_ACTION_HEADER || type > WS_FAULT_TO_HEADER || option < WS_READ_REQUIRED_VALUE ||
1295 option > WS_READ_OPTIONAL_POINTER) return E_INVALIDARG;
1297 EnterCriticalSection( &msg->cs );
1299 if (msg->magic != MSG_MAGIC)
1301 LeaveCriticalSection( &msg->cs );
1302 return E_INVALIDARG;
1305 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1306 else hr = get_standard_header( msg, type, value_type, option, heap, value, size );
1308 LeaveCriticalSection( &msg->cs );
1309 TRACE( "returning %08x\n", hr );
1310 return hr;
1313 static void remove_header( struct msg *msg, ULONG i )
1315 free_header( msg->header[i] );
1316 memmove( &msg->header[i], &msg->header[i + 1], (msg->header_count - i - 1) * sizeof(struct header *) );
1317 msg->header_count--;
1320 /**************************************************************************
1321 * WsRemoveHeader [webservices.@]
1323 HRESULT WINAPI WsRemoveHeader( WS_MESSAGE *handle, WS_HEADER_TYPE type, WS_ERROR *error )
1325 struct msg *msg = (struct msg *)handle;
1326 BOOL removed = FALSE;
1327 HRESULT hr = S_OK;
1328 ULONG i;
1330 TRACE( "%p %u %p\n", handle, type, error );
1331 if (error) FIXME( "ignoring error parameter\n" );
1333 if (!msg) return E_INVALIDARG;
1335 EnterCriticalSection( &msg->cs );
1337 if (msg->magic != MSG_MAGIC)
1339 LeaveCriticalSection( &msg->cs );
1340 return E_INVALIDARG;
1343 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1344 else if (type < WS_ACTION_HEADER || type > WS_FAULT_TO_HEADER) hr = E_INVALIDARG;
1345 else
1347 for (i = 0; i < msg->header_count; i++)
1349 if (msg->header[i]->type == type)
1351 remove_header( msg, i );
1352 removed = TRUE;
1353 break;
1356 if (removed) hr = write_envelope( msg );
1359 LeaveCriticalSection( &msg->cs );
1360 TRACE( "returning %08x\n", hr );
1361 return hr;
1364 static HRESULT build_mapped_header( const WS_XML_STRING *name, WS_TYPE type, WS_WRITE_OPTION option,
1365 const void *value, ULONG size, struct header **ret )
1367 struct header *header;
1369 if (!(header = alloc_header( 0, TRUE, name, NULL ))) return E_OUTOFMEMORY;
1370 switch (type)
1372 case WS_WSZ_TYPE:
1374 int len;
1375 const WCHAR *src;
1377 if (option != WS_WRITE_REQUIRED_POINTER || size != sizeof(WCHAR *))
1379 free_header( header );
1380 return E_INVALIDARG;
1382 src = *(const WCHAR **)value;
1383 len = WideCharToMultiByte( CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL ) - 1;
1384 if (!(header->u.text = alloc_xml_string( NULL, len )))
1386 free_header( header );
1387 return E_OUTOFMEMORY;
1389 WideCharToMultiByte( CP_UTF8, 0, src, -1, (char *)header->u.text->bytes, len, NULL, NULL );
1390 break;
1392 case WS_XML_STRING_TYPE:
1394 const WS_XML_STRING *str = value;
1396 if (option != WS_WRITE_REQUIRED_VALUE)
1398 FIXME( "unhandled write option %u\n", option );
1399 free_header( header );
1400 return E_NOTIMPL;
1402 if (size != sizeof(*str))
1404 free_header( header );
1405 return E_INVALIDARG;
1407 if (!(header->u.text = alloc_xml_string( NULL, str->length )))
1409 free_header( header );
1410 return E_OUTOFMEMORY;
1412 memcpy( header->u.text->bytes, str->bytes, str->length );
1413 break;
1415 case WS_STRING_TYPE:
1417 int len;
1418 const WS_STRING *str = value;
1420 if (option != WS_WRITE_REQUIRED_VALUE)
1422 FIXME( "unhandled write option %u\n", option );
1423 free_header( header );
1424 return E_NOTIMPL;
1426 if (size != sizeof(*str))
1428 free_header( header );
1429 return E_INVALIDARG;
1431 len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
1432 if (!(header->u.text = alloc_xml_string( NULL, len )))
1434 free_header( header );
1435 return E_OUTOFMEMORY;
1437 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)header->u.text->bytes,
1438 len, NULL, NULL );
1439 break;
1441 default:
1442 FIXME( "unhandled type %u\n", type );
1443 free_header( header );
1444 return E_NOTIMPL;
1447 *ret = header;
1448 return S_OK;
1451 static HRESULT add_mapped_header( struct msg *msg, const WS_XML_STRING *name, WS_TYPE type,
1452 WS_WRITE_OPTION option, const void *value, ULONG size )
1454 struct header *header;
1455 BOOL found = FALSE;
1456 HRESULT hr;
1457 ULONG i;
1459 for (i = 0; i < msg->header_count; i++)
1461 if (msg->header[i]->type || !msg->header[i]->mapped) continue;
1462 if (WsXmlStringEquals( name, &msg->header[i]->name, NULL ) == S_OK)
1464 found = TRUE;
1465 break;
1469 if (!found)
1471 if ((hr = grow_header_array( msg, msg->header_count + 1 )) != S_OK) return hr;
1472 i = msg->header_count;
1475 if ((hr = build_mapped_header( name, type, option, value, size, &header )) != S_OK) return hr;
1477 if (!found) msg->header_count++;
1478 else free_header( msg->header[i] );
1480 msg->header[i] = header;
1481 return S_OK;
1484 /**************************************************************************
1485 * WsAddMappedHeader [webservices.@]
1487 HRESULT WINAPI WsAddMappedHeader( WS_MESSAGE *handle, const WS_XML_STRING *name, WS_TYPE type,
1488 WS_WRITE_OPTION option, const void *value, ULONG size, WS_ERROR *error )
1490 struct msg *msg = (struct msg *)handle;
1491 HRESULT hr;
1493 TRACE( "%p %s %u %08x %p %u %p\n", handle, debugstr_xmlstr(name), type, option, value, size, error );
1494 if (error) FIXME( "ignoring error parameter\n" );
1496 if (!msg || !name) return E_INVALIDARG;
1498 EnterCriticalSection( &msg->cs );
1500 if (msg->magic != MSG_MAGIC)
1502 LeaveCriticalSection( &msg->cs );
1503 return E_INVALIDARG;
1506 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1507 else hr = add_mapped_header( msg, name, type, option, value, size );
1509 LeaveCriticalSection( &msg->cs );
1510 TRACE( "returning %08x\n", hr );
1511 return hr;
1514 /**************************************************************************
1515 * WsRemoveMappedHeader [webservices.@]
1517 HRESULT WINAPI WsRemoveMappedHeader( WS_MESSAGE *handle, const WS_XML_STRING *name, WS_ERROR *error )
1519 struct msg *msg = (struct msg *)handle;
1520 HRESULT hr = S_OK;
1521 ULONG i;
1523 TRACE( "%p %s %p\n", handle, debugstr_xmlstr(name), error );
1524 if (error) FIXME( "ignoring error parameter\n" );
1526 if (!msg || !name) return E_INVALIDARG;
1528 EnterCriticalSection( &msg->cs );
1530 if (msg->magic != MSG_MAGIC)
1532 LeaveCriticalSection( &msg->cs );
1533 return E_INVALIDARG;
1536 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1537 else
1539 for (i = 0; i < msg->header_count; i++)
1541 if (msg->header[i]->type || !msg->header[i]->mapped) continue;
1542 if (WsXmlStringEquals( name, &msg->header[i]->name, NULL ) == S_OK)
1544 remove_header( msg, i );
1545 break;
1550 LeaveCriticalSection( &msg->cs );
1551 TRACE( "returning %08x\n", hr );
1552 return hr;
1555 static HRESULT xmlstring_to_wsz( const WS_XML_STRING *str, WS_HEAP *heap, WCHAR **ret, int *len )
1557 *len = MultiByteToWideChar( CP_UTF8, 0, (char *)str->bytes, str->length, NULL, 0 );
1558 if (!(*ret = ws_alloc( heap, (*len + 1) * sizeof(WCHAR) ))) return WS_E_QUOTA_EXCEEDED;
1559 MultiByteToWideChar( CP_UTF8, 0, (char *)str->bytes, str->length, *ret, *len );
1560 (*ret)[*len] = 0;
1561 return S_OK;
1564 static HRESULT get_header_value_wsz( struct header *header, WS_READ_OPTION option, WS_HEAP *heap, WCHAR **ret,
1565 ULONG size )
1567 WCHAR *str = NULL;
1568 int len = 0;
1569 HRESULT hr;
1571 if (header && (hr = xmlstring_to_wsz( header->u.text, heap, &str, &len )) != S_OK) return hr;
1573 switch (option)
1575 case WS_READ_REQUIRED_POINTER:
1576 if (!str && !(str = ws_alloc_zero( heap, sizeof(*str) ))) return WS_E_QUOTA_EXCEEDED;
1577 /* fall through */
1579 case WS_READ_OPTIONAL_POINTER:
1580 case WS_READ_NILLABLE_POINTER:
1581 if (size != sizeof(str))
1583 ws_free( heap, str, (len + 1) * sizeof(WCHAR) );
1584 return E_INVALIDARG;
1586 *ret = str;
1587 break;
1589 default:
1590 FIXME( "read option %u not supported\n", option );
1591 ws_free( heap, str, (len + 1) * sizeof(WCHAR) );
1592 return E_NOTIMPL;
1595 return S_OK;
1598 static HRESULT get_mapped_header( struct msg *msg, const WS_XML_STRING *name, WS_TYPE type, WS_READ_OPTION option,
1599 WS_HEAP *heap, void *value, ULONG size )
1601 struct header *header = NULL;
1602 HRESULT hr;
1603 ULONG i;
1605 for (i = 0; i < msg->header_count; i++)
1607 if (msg->header[i]->type || !msg->header[i]->mapped) continue;
1608 if (WsXmlStringEquals( name, &msg->header[i]->name, NULL ) == S_OK)
1610 header = msg->header[i];
1611 break;
1615 switch (type)
1617 case WS_WSZ_TYPE:
1618 hr = get_header_value_wsz( header, option, heap, value, size );
1619 break;
1621 default:
1622 FIXME( "type %u not supported\n", option );
1623 return WS_E_NOT_SUPPORTED;
1626 return hr;
1629 /**************************************************************************
1630 * WsGetMappedHeader [webservices.@]
1632 HRESULT WINAPI WsGetMappedHeader( WS_MESSAGE *handle, const WS_XML_STRING *name, WS_REPEATING_HEADER_OPTION option,
1633 ULONG index, WS_TYPE type, WS_READ_OPTION read_option, WS_HEAP *heap, void *value,
1634 ULONG size, WS_ERROR *error )
1636 struct msg *msg = (struct msg *)handle;
1637 HRESULT hr;
1639 TRACE( "%p %s %u %u %u %u %p %p %u %p\n", handle, debugstr_xmlstr(name), option, index, type, read_option,
1640 heap, value, size, error );
1641 if (error) FIXME( "ignoring error parameter\n" );
1642 if (option != WS_SINGLETON_HEADER)
1644 FIXME( "option %u not supported\n", option );
1645 return E_NOTIMPL;
1648 if (!msg || !name) return E_INVALIDARG;
1650 EnterCriticalSection( &msg->cs );
1652 if (msg->magic != MSG_MAGIC)
1654 LeaveCriticalSection( &msg->cs );
1655 return E_INVALIDARG;
1658 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1659 else hr = get_mapped_header( msg, name, type, read_option, heap, value, size );
1661 LeaveCriticalSection( &msg->cs );
1662 TRACE( "returning %08x\n", hr );
1663 return hr;
1666 static HRESULT write_custom_header( WS_XML_WRITER *writer, const WS_XML_STRING *name, const WS_XML_STRING *ns,
1667 WS_TYPE type, const void *desc, WS_WRITE_OPTION option, const void *value,
1668 ULONG size )
1670 HRESULT hr;
1671 if ((hr = WsWriteStartElement( writer, NULL, name, ns, NULL )) != S_OK) return hr;
1672 if ((hr = WsWriteType( writer, WS_ELEMENT_CONTENT_TYPE_MAPPING, type, desc, option, value, size,
1673 NULL )) != S_OK) return hr;
1674 return WsWriteEndElement( writer, NULL );
1677 static HRESULT build_custom_header( struct msg *msg, const WS_XML_STRING *name, const WS_XML_STRING *ns,
1678 WS_TYPE type, const void *desc, WS_WRITE_OPTION option, const void *value,
1679 ULONG size, struct header **ret )
1681 struct header *header;
1682 WS_XML_BUFFER *buf;
1683 HRESULT hr;
1685 if (!(header = alloc_header( 0, FALSE, name, ns ))) return E_OUTOFMEMORY;
1687 if (!msg->writer && (hr = WsCreateWriter( NULL, 0, &msg->writer, NULL )) != S_OK) goto done;
1688 if ((hr = WsCreateXmlBuffer( msg->heap, NULL, 0, &buf, NULL )) != S_OK) goto done;
1689 if ((hr = WsSetOutputToBuffer( msg->writer, buf, NULL, 0, NULL )) != S_OK) goto done;
1690 if ((hr = write_custom_header( msg->writer, name, ns, type, desc, option, value, size )) != S_OK) goto done;
1692 header->u.buf = buf;
1694 done:
1695 if (hr != S_OK) free_header( header );
1696 else *ret = header;
1697 return hr;
1700 /**************************************************************************
1701 * WsAddCustomHeader [webservices.@]
1703 HRESULT WINAPI WsAddCustomHeader( WS_MESSAGE *handle, const WS_ELEMENT_DESCRIPTION *desc, WS_WRITE_OPTION option,
1704 const void *value, ULONG size, ULONG attrs, WS_ERROR *error )
1706 struct msg *msg = (struct msg *)handle;
1707 struct header *header;
1708 HRESULT hr;
1710 TRACE( "%p %p %08x %p %u %08x %p\n", handle, desc, option, value, size, attrs, error );
1711 if (error) FIXME( "ignoring error parameter\n" );
1713 if (!msg || !desc) return E_INVALIDARG;
1715 EnterCriticalSection( &msg->cs );
1717 if (msg->magic != MSG_MAGIC)
1719 LeaveCriticalSection( &msg->cs );
1720 return E_INVALIDARG;
1723 if (msg->state < WS_MESSAGE_STATE_INITIALIZED)
1725 hr = WS_E_INVALID_OPERATION;
1726 goto done;
1729 if ((hr = grow_header_array( msg, msg->header_count + 1 )) != S_OK) goto done;
1730 if ((hr = build_custom_header( msg, desc->elementLocalName, desc->elementNs, desc->type,
1731 desc->typeDescription, option, value, size, &header )) != S_OK) goto done;
1732 msg->header[msg->header_count++] = header;
1733 hr = write_envelope( msg );
1735 done:
1736 LeaveCriticalSection( &msg->cs );
1737 TRACE( "returning %08x\n", hr );
1738 return hr;
1741 static HRESULT get_custom_header( struct msg *msg, const WS_ELEMENT_DESCRIPTION *desc, WS_READ_OPTION option,
1742 WS_HEAP *heap, void *value, ULONG size )
1744 HRESULT hr;
1745 if (!heap) heap = msg->heap;
1746 if (!msg->reader && (hr = WsCreateReader( NULL, 0, &msg->reader, NULL )) != S_OK) return hr;
1747 if ((hr = WsSetInputToBuffer( msg->reader, msg->buf, NULL, 0, NULL )) != S_OK) return hr;
1749 if ((hr = find_header( msg->reader, desc->elementLocalName, desc->elementNs )) != S_OK) return hr;
1750 return read_header( msg->reader, desc->elementLocalName, desc->elementNs, desc->type, desc->typeDescription,
1751 option, heap, value, size );
1754 /**************************************************************************
1755 * WsGetCustomHeader [webservices.@]
1757 HRESULT WINAPI WsGetCustomHeader( WS_MESSAGE *handle, const WS_ELEMENT_DESCRIPTION *desc,
1758 WS_REPEATING_HEADER_OPTION repeat_option, ULONG index, WS_READ_OPTION option,
1759 WS_HEAP *heap, void *value, ULONG size, ULONG *attrs, WS_ERROR *error )
1761 struct msg *msg = (struct msg *)handle;
1762 HRESULT hr;
1764 TRACE( "%p %p %08x %u %08x %p %p %u %p %p\n", handle, desc, repeat_option, index, option, heap, value,
1765 size, attrs, error );
1766 if (error) FIXME( "ignoring error parameter\n" );
1768 if (!msg || !desc || repeat_option < WS_REPEATING_HEADER || repeat_option > WS_SINGLETON_HEADER ||
1769 (repeat_option == WS_SINGLETON_HEADER && index)) return E_INVALIDARG;
1771 if (repeat_option == WS_REPEATING_HEADER)
1773 FIXME( "repeating header not supported\n" );
1774 return E_NOTIMPL;
1776 if (attrs)
1778 FIXME( "attributes not supported\n" );
1779 return E_NOTIMPL;
1782 EnterCriticalSection( &msg->cs );
1784 if (msg->magic != MSG_MAGIC)
1786 LeaveCriticalSection( &msg->cs );
1787 return E_INVALIDARG;
1790 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1791 else hr = get_custom_header( msg, desc, option, heap, value, size );
1793 LeaveCriticalSection( &msg->cs );
1794 TRACE( "returning %08x\n", hr );
1795 return hr;
1798 /**************************************************************************
1799 * WsRemoveCustomHeader [webservices.@]
1801 HRESULT WINAPI WsRemoveCustomHeader( WS_MESSAGE *handle, const WS_XML_STRING *name, const WS_XML_STRING *ns,
1802 WS_ERROR *error )
1804 struct msg *msg = (struct msg *)handle;
1805 BOOL removed = FALSE;
1806 HRESULT hr = S_OK;
1807 ULONG i;
1809 TRACE( "%p %s %s %p\n", handle, debugstr_xmlstr(name), debugstr_xmlstr(ns), error );
1810 if (error) FIXME( "ignoring error parameter\n" );
1812 if (!msg || !name || !ns) return E_INVALIDARG;
1814 EnterCriticalSection( &msg->cs );
1816 if (msg->magic != MSG_MAGIC)
1818 LeaveCriticalSection( &msg->cs );
1819 return E_INVALIDARG;
1822 if (msg->state < WS_MESSAGE_STATE_INITIALIZED) hr = WS_E_INVALID_OPERATION;
1823 else
1825 for (i = 0; i < msg->header_count; i++)
1827 if (msg->header[i]->type || msg->header[i]->mapped) continue;
1828 if (WsXmlStringEquals( name, &msg->header[i]->name, NULL ) == S_OK &&
1829 WsXmlStringEquals( ns, &msg->header[i]->ns, NULL ) == S_OK)
1831 remove_header( msg, i );
1832 removed = TRUE;
1833 i--;
1836 if (removed) hr = write_envelope( msg );
1839 LeaveCriticalSection( &msg->cs );
1840 TRACE( "returning %08x\n", hr );
1841 return hr;
1844 static WCHAR *build_http_header( const WCHAR *name, const WCHAR *value, ULONG *ret_len )
1846 int len_name = lstrlenW( name ), len_value = lstrlenW( value );
1847 WCHAR *ret = heap_alloc( (len_name + len_value + 2) * sizeof(WCHAR) );
1849 if (!ret) return NULL;
1850 memcpy( ret, name, len_name * sizeof(WCHAR) );
1851 ret[len_name++] = ':';
1852 ret[len_name++] = ' ';
1853 memcpy( ret + len_name, value, len_value * sizeof(WCHAR) );
1854 *ret_len = len_name + len_value;
1855 return ret;
1858 static inline HRESULT insert_http_header( HINTERNET req, const WCHAR *header, ULONG len, ULONG flags )
1860 if (WinHttpAddRequestHeaders( req, header, len, flags )) return S_OK;
1861 return HRESULT_FROM_WIN32( GetLastError() );
1864 static WCHAR *from_xml_string( const WS_XML_STRING *str )
1866 WCHAR *ret;
1867 int len = MultiByteToWideChar( CP_UTF8, 0, (char *)str->bytes, str->length, NULL, 0 );
1868 if (!(ret = heap_alloc( (len + 1) * sizeof(*ret) ))) return NULL;
1869 MultiByteToWideChar( CP_UTF8, 0, (char *)str->bytes, str->length, ret, len );
1870 ret[len] = 0;
1871 return ret;
1874 static HRESULT insert_mapped_headers( struct msg *msg, HINTERNET req )
1876 WCHAR *name, *value, *header;
1877 ULONG i, len;
1878 HRESULT hr;
1880 for (i = 0; i < msg->header_count; i++)
1882 if (!msg->header[i]->mapped) continue;
1884 if (!(name = from_xml_string( &msg->header[i]->name ))) return E_OUTOFMEMORY;
1885 if (!(value = from_xml_string( msg->header[i]->u.text )))
1887 heap_free( name );
1888 return E_OUTOFMEMORY;
1890 header = build_http_header( name, value, &len );
1891 heap_free( name );
1892 heap_free( value );
1893 if (!header) return E_OUTOFMEMORY;
1895 hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_ADD|WINHTTP_ADDREQ_FLAG_REPLACE );
1896 heap_free( header );
1897 if (hr != S_OK) return hr;
1899 return S_OK;
1902 HRESULT message_insert_http_headers( WS_MESSAGE *handle, HINTERNET req )
1904 struct msg *msg = (struct msg *)handle;
1905 HRESULT hr = E_OUTOFMEMORY;
1906 WCHAR *header = NULL, *buf;
1907 ULONG len;
1909 EnterCriticalSection( &msg->cs );
1911 if (msg->magic != MSG_MAGIC)
1913 LeaveCriticalSection( &msg->cs );
1914 return E_INVALIDARG;
1917 switch (msg->version_env)
1919 case WS_ENVELOPE_VERSION_SOAP_1_1:
1920 header = build_http_header( L"Content-Type", L"text/xml", &len );
1921 break;
1923 case WS_ENVELOPE_VERSION_SOAP_1_2:
1924 header = build_http_header( L"Content-Type", L"application/soap+xml", &len );
1925 break;
1927 default:
1928 FIXME( "unhandled envelope version %u\n", msg->version_env );
1929 hr = E_NOTIMPL;
1931 if (!header) goto done;
1933 if ((hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_ADD )) != S_OK) goto done;
1934 heap_free( header );
1936 hr = E_OUTOFMEMORY;
1937 if (!(header = build_http_header( L"Content-Type", L"charset=utf-8", &len ))) goto done;
1938 if ((hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON )) != S_OK)
1939 goto done;
1940 heap_free( header );
1941 header = NULL;
1943 switch (msg->version_env)
1945 case WS_ENVELOPE_VERSION_SOAP_1_1:
1947 if (!(len = MultiByteToWideChar( CP_UTF8, 0, (char *)msg->action->bytes, msg->action->length, NULL, 0 )))
1948 break;
1950 hr = E_OUTOFMEMORY;
1951 if (!(buf = heap_alloc( (len + 3) * sizeof(WCHAR) ))) goto done;
1952 buf[0] = '"';
1953 MultiByteToWideChar( CP_UTF8, 0, (char *)msg->action->bytes, msg->action->length, buf + 1, len );
1954 buf[len + 1] = '"';
1955 buf[len + 2] = 0;
1957 header = build_http_header( L"SOAPAction", buf, &len );
1958 heap_free( buf );
1959 if (!header) goto done;
1961 hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_ADD );
1962 break;
1964 case WS_ENVELOPE_VERSION_SOAP_1_2:
1966 ULONG len_action = ARRAY_SIZE( L"action=\"" ) - 1;
1968 if (!(len = MultiByteToWideChar( CP_UTF8, 0, (char *)msg->action->bytes, msg->action->length, NULL, 0 )))
1969 break;
1971 hr = E_OUTOFMEMORY;
1972 if (!(buf = heap_alloc( (len + len_action + 2) * sizeof(WCHAR) ))) goto done;
1973 memcpy( buf, L"action=\"", len_action * sizeof(WCHAR) );
1974 MultiByteToWideChar( CP_UTF8, 0, (char *)msg->action->bytes, msg->action->length, buf + len_action, len );
1975 len += len_action;
1976 buf[len++] = '"';
1977 buf[len] = 0;
1979 header = build_http_header( L"Content-Type", buf, &len );
1980 heap_free( buf );
1981 if (!header) goto done;
1983 hr = insert_http_header( req, header, len, WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON );
1984 break;
1986 default:
1987 FIXME( "unhandled envelope version %u\n", msg->version_env );
1988 hr = E_NOTIMPL;
1991 if (hr == S_OK) hr = insert_mapped_headers( msg, req );
1993 done:
1994 heap_free( header );
1995 LeaveCriticalSection( &msg->cs );
1996 TRACE( "returning %08x\n", hr );
1997 return hr;
2000 static HRESULT map_http_response_headers( struct msg *msg, HINTERNET req, const WS_HTTP_MESSAGE_MAPPING *mapping )
2002 ULONG i;
2003 for (i = 0; i < mapping->responseHeaderMappingCount; i++)
2005 WCHAR *name, *value;
2006 DWORD size;
2008 if (!(name = from_xml_string( &mapping->responseHeaderMappings[i]->headerName ))) return E_OUTOFMEMORY;
2010 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_CUSTOM, name, NULL, &size, NULL ) &&
2011 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2013 HRESULT hr;
2014 if (!(value = heap_alloc( size )))
2016 heap_free( name );
2017 return E_OUTOFMEMORY;
2019 if (!WinHttpQueryHeaders( req, WINHTTP_QUERY_CUSTOM, name, value, &size, NULL ))
2021 heap_free( name );
2022 return HRESULT_FROM_WIN32( GetLastError() );
2024 hr = add_mapped_header( msg, &mapping->responseHeaderMappings[i]->headerName, WS_WSZ_TYPE,
2025 WS_WRITE_REQUIRED_POINTER, &value, sizeof(value) );
2026 heap_free( value );
2027 if (hr != S_OK)
2029 heap_free( name );
2030 return hr;
2033 heap_free( name );
2035 return S_OK;
2038 HRESULT message_map_http_response_headers( WS_MESSAGE *handle, HINTERNET req, const WS_HTTP_MESSAGE_MAPPING *mapping )
2040 struct msg *msg = (struct msg *)handle;
2041 HRESULT hr;
2043 EnterCriticalSection( &msg->cs );
2045 if (msg->magic != MSG_MAGIC)
2047 LeaveCriticalSection( &msg->cs );
2048 return E_INVALIDARG;
2051 hr = map_http_response_headers( msg, req, mapping );
2053 LeaveCriticalSection( &msg->cs );
2054 TRACE( "returning %08x\n", hr );
2055 return hr;
2058 void message_set_send_context( WS_MESSAGE *handle, const WS_PROXY_MESSAGE_CALLBACK_CONTEXT *ctx )
2060 struct msg *msg = (struct msg *)handle;
2062 EnterCriticalSection( &msg->cs );
2064 if (msg->magic != MSG_MAGIC)
2066 LeaveCriticalSection( &msg->cs );
2067 return;
2070 msg->ctx_send.callback = ctx->callback;
2071 msg->ctx_send.state = ctx->state;
2073 LeaveCriticalSection( &msg->cs );
2076 void message_set_receive_context( WS_MESSAGE *handle, const WS_PROXY_MESSAGE_CALLBACK_CONTEXT *ctx )
2078 struct msg *msg = (struct msg *)handle;
2080 EnterCriticalSection( &msg->cs );
2082 if (msg->magic != MSG_MAGIC)
2084 LeaveCriticalSection( &msg->cs );
2085 return;
2088 msg->ctx_receive.callback = ctx->callback;
2089 msg->ctx_receive.state = ctx->state;
2091 LeaveCriticalSection( &msg->cs );
2094 void message_do_send_callback( WS_MESSAGE *handle )
2096 struct msg *msg = (struct msg *)handle;
2098 EnterCriticalSection( &msg->cs );
2100 if (msg->magic != MSG_MAGIC)
2102 LeaveCriticalSection( &msg->cs );
2103 return;
2106 if (msg->ctx_send.callback)
2108 HRESULT hr;
2109 TRACE( "executing callback %p\n", msg->ctx_send.callback );
2110 hr = msg->ctx_send.callback( handle, msg->heap, msg->ctx_send.state, NULL );
2111 TRACE( "callback %p returned %08x\n", msg->ctx_send.callback, hr );
2114 LeaveCriticalSection( &msg->cs );
2117 void message_do_receive_callback( WS_MESSAGE *handle )
2119 struct msg *msg = (struct msg *)handle;
2121 EnterCriticalSection( &msg->cs );
2123 if (msg->magic != MSG_MAGIC)
2125 LeaveCriticalSection( &msg->cs );
2126 return;
2129 if (msg->ctx_receive.callback)
2131 HRESULT hr;
2132 TRACE( "executing callback %p\n", msg->ctx_receive.callback );
2133 hr = msg->ctx_receive.callback( handle, msg->heap, msg->ctx_receive.state, NULL );
2134 TRACE( "callback %p returned %08x\n", msg->ctx_receive.callback, hr );
2137 LeaveCriticalSection( &msg->cs );
2140 HRESULT message_set_action( WS_MESSAGE *handle, const WS_XML_STRING *action )
2142 struct msg *msg = (struct msg *)handle;
2143 HRESULT hr = S_OK;
2145 EnterCriticalSection( &msg->cs );
2147 if (msg->magic != MSG_MAGIC)
2149 LeaveCriticalSection( &msg->cs );
2150 return E_INVALIDARG;
2153 if (!action || !action->length)
2155 free_xml_string( msg->action );
2156 msg->action = NULL;
2158 else
2160 WS_XML_STRING *str;
2161 if (!(str = dup_xml_string( action, FALSE ))) hr = E_OUTOFMEMORY;
2162 else
2164 free_xml_string( msg->action );
2165 msg->action = str;
2169 LeaveCriticalSection( &msg->cs );
2170 return hr;
2173 HRESULT message_get_id( WS_MESSAGE *handle, GUID *id )
2175 struct msg *msg = (struct msg *)handle;
2176 HRESULT hr = S_OK;
2178 EnterCriticalSection( &msg->cs );
2180 if (msg->magic != MSG_MAGIC)
2182 LeaveCriticalSection( &msg->cs );
2183 return E_INVALIDARG;
2186 *id = msg->id_req;
2188 LeaveCriticalSection( &msg->cs );
2189 return hr;
2192 HRESULT message_set_request_id( WS_MESSAGE *handle, const GUID *id )
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 msg->id_req = *id;
2207 LeaveCriticalSection( &msg->cs );
2208 return hr;