XmlRpcRequestParserStateImpl.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "XmlRpcRequestParserStateImpl.h"
  36. #include <cstring>
  37. #include "XmlRpcRequestParserStateMachine.h"
  38. #include "RecoverableException.h"
  39. #include "util.h"
  40. #include "base64.h"
  41. #include "ValueBase.h"
  42. namespace aria2 {
  43. namespace rpc {
  44. // InitialXmlRpcRequestParserState
  45. void InitialXmlRpcRequestParserState::beginElement(
  46. XmlRpcRequestParserStateMachine* psm, const char* name,
  47. const std::vector<XmlAttr>& attrs)
  48. {
  49. if (strcmp(name, "methodCall") == 0) {
  50. psm->pushMethodCallState();
  51. }
  52. else {
  53. psm->pushUnknownElementState();
  54. }
  55. }
  56. void InitialXmlRpcRequestParserState::endElement(
  57. XmlRpcRequestParserStateMachine* psm, const char* name,
  58. std::string characters)
  59. {
  60. }
  61. // UnknownElementXmlRpcRequestParserState
  62. void UnknownElementXmlRpcRequestParserState::beginElement(
  63. XmlRpcRequestParserStateMachine* psm, const char* name,
  64. const std::vector<XmlAttr>& attrs)
  65. {
  66. psm->pushUnknownElementState();
  67. }
  68. // MethodCallXmlRpcRequestParserState
  69. void MethodCallXmlRpcRequestParserState::beginElement(
  70. XmlRpcRequestParserStateMachine* psm, const char* name,
  71. const std::vector<XmlAttr>& attrs)
  72. {
  73. if (strcmp(name, "methodName") == 0) {
  74. psm->pushMethodNameState();
  75. }
  76. else if (strcmp(name, "params") == 0) {
  77. psm->setCurrentFrameValue(List::g());
  78. psm->pushParamsState();
  79. }
  80. else {
  81. psm->pushUnknownElementState();
  82. }
  83. }
  84. // MethodNameXmlRpcRequestParserState
  85. void MethodNameXmlRpcRequestParserState::beginElement(
  86. XmlRpcRequestParserStateMachine* psm, const char* name,
  87. const std::vector<XmlAttr>& attrs)
  88. {
  89. psm->pushUnknownElementState();
  90. }
  91. void MethodNameXmlRpcRequestParserState::endElement(
  92. XmlRpcRequestParserStateMachine* psm, const char* name,
  93. std::string characters)
  94. {
  95. psm->setMethodName(std::move(characters));
  96. }
  97. // ParamsXmlRpcRequestParserState
  98. void ParamsXmlRpcRequestParserState::beginElement(
  99. XmlRpcRequestParserStateMachine* psm, const char* name,
  100. const std::vector<XmlAttr>& attrs)
  101. {
  102. if (strcmp(name, "param") == 0) {
  103. psm->pushFrame();
  104. psm->pushParamState();
  105. }
  106. else {
  107. psm->pushUnknownElementState();
  108. }
  109. }
  110. // ParamXmlRpcRequestParserState
  111. void ParamXmlRpcRequestParserState::beginElement(
  112. XmlRpcRequestParserStateMachine* psm, const char* name,
  113. const std::vector<XmlAttr>& attrs)
  114. {
  115. if (strcmp(name, "value") == 0) {
  116. psm->pushValueState();
  117. }
  118. else {
  119. psm->pushUnknownElementState();
  120. }
  121. }
  122. void ParamXmlRpcRequestParserState::endElement(
  123. XmlRpcRequestParserStateMachine* psm, const char* name,
  124. std::string characters)
  125. {
  126. psm->popArrayFrame();
  127. }
  128. // ValueXmlRpcRequestParserState
  129. void ValueXmlRpcRequestParserState::beginElement(
  130. XmlRpcRequestParserStateMachine* psm, const char* name,
  131. const std::vector<XmlAttr>& attrs)
  132. {
  133. if (strcmp(name, "i4") == 0 || strcmp(name, "int") == 0) {
  134. psm->pushIntState();
  135. }
  136. else if (strcmp(name, "struct") == 0) {
  137. psm->setCurrentFrameValue(Dict::g());
  138. psm->pushStructState();
  139. }
  140. else if (strcmp(name, "array") == 0) {
  141. psm->setCurrentFrameValue(List::g());
  142. psm->pushArrayState();
  143. }
  144. else if (strcmp(name, "string") == 0 || strcmp(name, "double") == 0) {
  145. psm->pushStringState();
  146. }
  147. else if (strcmp(name, "base64") == 0) {
  148. psm->pushBase64State();
  149. }
  150. else {
  151. psm->pushUnknownElementState();
  152. }
  153. }
  154. void ValueXmlRpcRequestParserState::endElement(
  155. XmlRpcRequestParserStateMachine* psm, const char* name,
  156. std::string characters)
  157. {
  158. // XML-RPC specification says that if no data type tag is used, the
  159. // data must be treated as string. To prevent from overwriting
  160. // current frame value, we first check it is still null.
  161. if (!psm->getCurrentFrameValue() && !characters.empty()) {
  162. psm->setCurrentFrameValue(String::g(std::move(characters)));
  163. }
  164. }
  165. // IntXmlRpcRequestParserState
  166. void IntXmlRpcRequestParserState::beginElement(
  167. XmlRpcRequestParserStateMachine* psm, const char* name,
  168. const std::vector<XmlAttr>& attrs)
  169. {
  170. psm->pushUnknownElementState();
  171. }
  172. void IntXmlRpcRequestParserState::endElement(
  173. XmlRpcRequestParserStateMachine* psm, const char* name,
  174. std::string characters)
  175. {
  176. int32_t value;
  177. if (util::parseIntNoThrow(value, characters)) {
  178. psm->setCurrentFrameValue(Integer::g(value));
  179. }
  180. else {
  181. // nothing to do here: We just leave current frame value to null.
  182. }
  183. }
  184. // StringXmlRpcRequestParserState
  185. void StringXmlRpcRequestParserState::beginElement(
  186. XmlRpcRequestParserStateMachine* psm, const char* name,
  187. const std::vector<XmlAttr>& attrs)
  188. {
  189. psm->pushUnknownElementState();
  190. }
  191. void StringXmlRpcRequestParserState::endElement(
  192. XmlRpcRequestParserStateMachine* psm, const char* name,
  193. std::string characters)
  194. {
  195. psm->setCurrentFrameValue(String::g(std::move(characters)));
  196. }
  197. // Base64XmlRpcRequestParserState
  198. void Base64XmlRpcRequestParserState::beginElement(
  199. XmlRpcRequestParserStateMachine* psm, const char* name,
  200. const std::vector<XmlAttr>& attrs)
  201. {
  202. psm->pushUnknownElementState();
  203. }
  204. void Base64XmlRpcRequestParserState::endElement(
  205. XmlRpcRequestParserStateMachine* psm, const char* name,
  206. std::string characters)
  207. {
  208. psm->setCurrentFrameValue(
  209. String::g(base64::decode(characters.begin(), characters.end())));
  210. }
  211. // StructXmlRpcRequestParserState
  212. void StructXmlRpcRequestParserState::beginElement(
  213. XmlRpcRequestParserStateMachine* psm, const char* name,
  214. const std::vector<XmlAttr>& attrs)
  215. {
  216. if (strcmp(name, "member") == 0) {
  217. psm->pushFrame();
  218. psm->pushMemberState();
  219. }
  220. else {
  221. psm->pushUnknownElementState();
  222. }
  223. }
  224. // MemberXmlRpcRequestParserState
  225. void MemberXmlRpcRequestParserState::beginElement(
  226. XmlRpcRequestParserStateMachine* psm, const char* name,
  227. const std::vector<XmlAttr>& attrs)
  228. {
  229. if (strcmp(name, "name") == 0) {
  230. psm->pushNameState();
  231. }
  232. else if (strcmp(name, "value") == 0) {
  233. psm->pushValueState();
  234. }
  235. else {
  236. psm->pushUnknownElementState();
  237. }
  238. }
  239. void MemberXmlRpcRequestParserState::endElement(
  240. XmlRpcRequestParserStateMachine* psm, const char* name,
  241. std::string characters)
  242. {
  243. psm->popStructFrame();
  244. }
  245. // NameXmlRpcRequestParserState
  246. void NameXmlRpcRequestParserState::beginElement(
  247. XmlRpcRequestParserStateMachine* psm, const char* name,
  248. const std::vector<XmlAttr>& attrs)
  249. {
  250. psm->pushUnknownElementState();
  251. }
  252. void NameXmlRpcRequestParserState::endElement(
  253. XmlRpcRequestParserStateMachine* psm, const char* name,
  254. std::string characters)
  255. {
  256. psm->setCurrentFrameName(std::move(characters));
  257. }
  258. // ArrayXmlRpcRequestParserState
  259. void ArrayXmlRpcRequestParserState::beginElement(
  260. XmlRpcRequestParserStateMachine* psm, const char* name,
  261. const std::vector<XmlAttr>& attrs)
  262. {
  263. if (strcmp(name, "data") == 0) {
  264. psm->pushDataState();
  265. }
  266. else {
  267. psm->pushUnknownElementState();
  268. }
  269. }
  270. // DataXmlRpcRequestParserState
  271. void DataXmlRpcRequestParserState::beginElement(
  272. XmlRpcRequestParserStateMachine* psm, const char* name,
  273. const std::vector<XmlAttr>& attrs)
  274. {
  275. if (strcmp(name, "value") == 0) {
  276. psm->pushFrame();
  277. psm->pushArrayValueState();
  278. }
  279. else {
  280. psm->pushUnknownElementState();
  281. }
  282. }
  283. // ArrayValueXmlRpcRequestParserState
  284. void ArrayValueXmlRpcRequestParserState::endElement(
  285. XmlRpcRequestParserStateMachine* psm, const char* name,
  286. std::string characters)
  287. {
  288. ValueXmlRpcRequestParserState::endElement(psm, name, std::move(characters));
  289. psm->popArrayFrame();
  290. }
  291. } // namespace rpc
  292. } // namespace aria2