cid#1607171 Data race condition
[LibreOffice.git] / forms / source / xforms / submission / serialization_urlencoded.cxx
blob8b2c78695d6a70d5ab189c97263af4adef767308
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/io/Pipe.hpp>
22 #include <com/sun/star/xml/xpath/XPathObjectType.hpp>
23 #include <com/sun/star/xml/dom/XNode.hpp>
24 #include <com/sun/star/xml/dom/XText.hpp>
25 #include <com/sun/star/xml/dom/XNodeList.hpp>
26 #include <com/sun/star/xml/dom/NodeType.hpp>
27 #include <rtl/character.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/strbuf.hxx>
30 #include <comphelper/processfactory.hxx>
32 #include <stdio.h>
34 #include "serialization_urlencoded.hxx"
36 using namespace css::uno;
37 using namespace css::io;
38 using namespace css::xml::dom;
40 CSerializationURLEncoded::CSerializationURLEncoded()
41 : m_aPipe(Pipe::create(comphelper::getProcessComponentContext()))
47 rfc2396
48 reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
49 "$" | ","
50 mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
51 unreserved = alphanum | mark
53 bool CSerializationURLEncoded::is_unreserved(char c)
55 if (rtl::isAsciiAlphanumeric(static_cast<unsigned char>(c)))
56 return true;
57 switch (c) {
58 case '-':
59 case '_':
60 case '.':
61 case '!':
62 case '~':
63 case '*':
64 case '\'':
65 case '(':
66 case ')':
67 return true;
69 return false;
71 void CSerializationURLEncoded::encode_and_append(
72 std::u16string_view aString, OStringBuffer& aBuffer)
74 OString utf8String = OUStringToOString(aString, RTL_TEXTENCODING_UTF8);
75 const sal_uInt8 *pString = reinterpret_cast< const sal_uInt8 * >( utf8String.getStr() );
76 char tmpChar[4];
78 while( *pString != 0)
80 if( *pString < 0x80 )
82 if ( is_unreserved(*pString) ) {
83 aBuffer.append(char(*pString));
84 } else if (*pString == 0x20) {
85 aBuffer.append('+');
86 } else if (*pString == 0x0d && *(pString+1) == 0x0a) {
87 aBuffer.append("%0D%0A");
88 pString++;
89 } else if (*pString == 0x0a) {
90 aBuffer.append("%0D%0A");
91 } else {
92 snprintf(tmpChar, 4, "%%%X", *pString % 0x100);
93 aBuffer.append(tmpChar);
95 } else {
96 snprintf(tmpChar, 4, "%%%X", *pString % 0x100);
97 aBuffer.append(tmpChar);
98 while (*pString >= 0x80) {
99 // continuation...
100 pString++;
101 snprintf(tmpChar, 4, "%%%X", *pString % 0x100);
102 aBuffer.append(tmpChar);
105 pString++;
109 void CSerializationURLEncoded::serialize_node(const Reference< XNode >& aNode)
111 // serialize recursive
112 // every element node E that has a text child T will be serialized in document order
113 // <E1>T1<E2>T2</E2></E1><E3>T3</E3> -> E1=T2&E2=T2&E3=T3 (En := local name)
115 // this node
116 Reference< XNodeList > aChildList = aNode->getChildNodes();
117 Reference< XNode > aChild;
118 // is this an element node?
119 if (aNode->getNodeType() == NodeType_ELEMENT_NODE)
121 OUString aName = aNode->getNodeName();
122 // find any text children
123 OUStringBuffer aValue;
124 Reference< XText > aText;
125 for(sal_Int32 i=0; i < aChildList->getLength(); i++)
127 aChild = aChildList->item(i);
128 if (aChild->getNodeType() == NodeType_TEXT_NODE)
130 aText.set(aChild, UNO_QUERY);
131 aValue.append(aText->getData());
135 // found anything?
136 if (!aValue.isEmpty())
138 OUString aUnencValue = aValue.makeStringAndClear();
139 OStringBuffer aEncodedBuffer;
140 encode_and_append(aName, aEncodedBuffer);
141 aEncodedBuffer.append("=");
142 encode_and_append(aUnencValue, aEncodedBuffer);
143 aEncodedBuffer.append("&");
144 sal_Int8 const *pData = reinterpret_cast<sal_Int8 const *>(aEncodedBuffer.getStr());
145 Sequence< sal_Int8 > sData(pData, aEncodedBuffer.getLength());
146 m_aPipe->writeBytes(sData);
150 // element children...
151 for(sal_Int32 i=0; i < aChildList->getLength(); i++)
153 aChild = aChildList->item(i);
154 // if this is an element node, it might be a candidate for serialization
155 if (aChild.is() && aChild->getNodeType() == NodeType_ELEMENT_NODE)
156 serialize_node(aChild);
160 void CSerializationURLEncoded::serialize()
163 // output stream to the pipe buffer
165 css::uno::Reference< css::xml::dom::XNode > cur = m_aFragment->getFirstChild();
166 while (cur.is())
168 serialize_node(cur);
169 cur = cur->getNextSibling();
171 m_aPipe->closeOutput();
174 Reference< XInputStream > CSerializationURLEncoded::getInputStream()
176 return m_aPipe;
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */