XmlRpcRequestParserStateImpl.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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,
  47. const char* name,
  48. const std::vector<XmlAttr>& attrs)
  49. {
  50. if(strcmp(name, "methodCall") == 0) {
  51. psm->pushMethodCallState();
  52. } else {
  53. psm->pushUnknownElementState();
  54. }
  55. }
  56. void InitialXmlRpcRequestParserState::endElement
  57. (XmlRpcRequestParserStateMachine* psm,
  58. const char* name,
  59. const std::string& characters)
  60. {}
  61. // UnknownElementXmlRpcRequestParserState
  62. void UnknownElementXmlRpcRequestParserState::beginElement
  63. (XmlRpcRequestParserStateMachine* psm,
  64. const char* name,
  65. const std::vector<XmlAttr>& attrs)
  66. {
  67. psm->pushUnknownElementState();
  68. }
  69. // MethodCallXmlRpcRequestParserState
  70. void MethodCallXmlRpcRequestParserState::beginElement
  71. (XmlRpcRequestParserStateMachine* psm,
  72. const char* name,
  73. const std::vector<XmlAttr>& attrs)
  74. {
  75. if(strcmp(name, "methodName") == 0) {
  76. psm->pushMethodNameState();
  77. } else if(strcmp(name, "params") == 0) {
  78. psm->setCurrentFrameValue(List::g());
  79. psm->pushParamsState();
  80. } else {
  81. psm->pushUnknownElementState();
  82. }
  83. }
  84. // MethodNameXmlRpcRequestParserState
  85. void MethodNameXmlRpcRequestParserState::beginElement
  86. (XmlRpcRequestParserStateMachine* psm,
  87. const char* name,
  88. const std::vector<XmlAttr>& attrs)
  89. {
  90. psm->pushUnknownElementState();
  91. }
  92. void MethodNameXmlRpcRequestParserState::endElement
  93. (XmlRpcRequestParserStateMachine* psm,
  94. const char* name,
  95. const std::string& characters)
  96. {
  97. psm->setMethodName(characters);
  98. }
  99. // ParamsXmlRpcRequestParserState
  100. void ParamsXmlRpcRequestParserState::beginElement
  101. (XmlRpcRequestParserStateMachine* psm,
  102. const char* name,
  103. const std::vector<XmlAttr>& attrs)
  104. {
  105. if(strcmp(name, "param") == 0) {
  106. psm->pushFrame();
  107. psm->pushParamState();
  108. } else {
  109. psm->pushUnknownElementState();
  110. }
  111. }
  112. // ParamXmlRpcRequestParserState
  113. void ParamXmlRpcRequestParserState::beginElement
  114. (XmlRpcRequestParserStateMachine* psm,
  115. const char* name,
  116. const std::vector<XmlAttr>& attrs)
  117. {
  118. if(strcmp(name, "value") == 0) {
  119. psm->pushValueState();
  120. } else {
  121. psm->pushUnknownElementState();
  122. }
  123. }
  124. void ParamXmlRpcRequestParserState::endElement
  125. (XmlRpcRequestParserStateMachine* psm,
  126. const char* name,
  127. const std::string& characters)
  128. {
  129. psm->popArrayFrame();
  130. }
  131. // ValueXmlRpcRequestParserState
  132. void ValueXmlRpcRequestParserState::beginElement
  133. (XmlRpcRequestParserStateMachine* psm,
  134. const char* name,
  135. const std::vector<XmlAttr>& attrs)
  136. {
  137. if(strcmp(name, "i4") == 0 || strcmp(name, "int") == 0) {
  138. psm->pushIntState();
  139. } else if(strcmp(name, "struct") == 0) {
  140. psm->setCurrentFrameValue(Dict::g());
  141. psm->pushStructState();
  142. } else if(strcmp(name, "array") == 0) {
  143. psm->setCurrentFrameValue(List::g());
  144. psm->pushArrayState();
  145. } else if(strcmp(name, "string") == 0 || strcmp(name, "double") == 0) {
  146. psm->pushStringState();
  147. } else if(strcmp(name, "base64") == 0) {
  148. psm->pushBase64State();
  149. } else {
  150. psm->pushUnknownElementState();
  151. }
  152. }
  153. void ValueXmlRpcRequestParserState::endElement
  154. (XmlRpcRequestParserStateMachine* psm,
  155. const char* name,
  156. const 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(characters));
  163. }
  164. }
  165. // IntXmlRpcRequestParserState
  166. void IntXmlRpcRequestParserState::beginElement
  167. (XmlRpcRequestParserStateMachine* psm,
  168. const char* name,
  169. const std::vector<XmlAttr>& attrs)
  170. {
  171. psm->pushUnknownElementState();
  172. }
  173. void IntXmlRpcRequestParserState::endElement
  174. (XmlRpcRequestParserStateMachine* psm,
  175. const char* name,
  176. const std::string& characters)
  177. {
  178. int32_t value;
  179. if(util::parseIntNoThrow(value, characters)) {
  180. psm->setCurrentFrameValue(Integer::g(value));
  181. } else {
  182. // nothing to do here: We just leave current frame value to null.
  183. }
  184. }
  185. // StringXmlRpcRequestParserState
  186. void StringXmlRpcRequestParserState::beginElement
  187. (XmlRpcRequestParserStateMachine* psm,
  188. const char* name,
  189. const std::vector<XmlAttr>& attrs)
  190. {
  191. psm->pushUnknownElementState();
  192. }
  193. void StringXmlRpcRequestParserState::endElement
  194. (XmlRpcRequestParserStateMachine* psm,
  195. const char* name,
  196. const std::string& characters)
  197. {
  198. psm->setCurrentFrameValue(String::g(characters));
  199. }
  200. // Base64XmlRpcRequestParserState
  201. void Base64XmlRpcRequestParserState::beginElement
  202. (XmlRpcRequestParserStateMachine* psm,
  203. const char* name,
  204. const std::vector<XmlAttr>& attrs)
  205. {
  206. psm->pushUnknownElementState();
  207. }
  208. void Base64XmlRpcRequestParserState::endElement
  209. (XmlRpcRequestParserStateMachine* psm,
  210. const char* name,
  211. const std::string& characters)
  212. {
  213. psm->setCurrentFrameValue
  214. (String::g(base64::decode(characters.begin(), characters.end())));
  215. }
  216. // StructXmlRpcRequestParserState
  217. void StructXmlRpcRequestParserState::beginElement
  218. (XmlRpcRequestParserStateMachine* psm,
  219. const char* name,
  220. const std::vector<XmlAttr>& attrs)
  221. {
  222. if(strcmp(name, "member") == 0) {
  223. psm->pushFrame();
  224. psm->pushMemberState();
  225. } else {
  226. psm->pushUnknownElementState();
  227. }
  228. }
  229. // MemberXmlRpcRequestParserState
  230. void MemberXmlRpcRequestParserState::beginElement
  231. (XmlRpcRequestParserStateMachine* psm,
  232. const char* name,
  233. const std::vector<XmlAttr>& attrs)
  234. {
  235. if(strcmp(name, "name") == 0) {
  236. psm->pushNameState();
  237. } else if(strcmp(name, "value") == 0) {
  238. psm->pushValueState();
  239. } else {
  240. psm->pushUnknownElementState();
  241. }
  242. }
  243. void MemberXmlRpcRequestParserState::endElement
  244. (XmlRpcRequestParserStateMachine* psm,
  245. const char* name,
  246. const std::string& characters)
  247. {
  248. psm->popStructFrame();
  249. }
  250. // NameXmlRpcRequestParserState
  251. void NameXmlRpcRequestParserState::beginElement
  252. (XmlRpcRequestParserStateMachine* psm,
  253. const char* name,
  254. const std::vector<XmlAttr>& attrs)
  255. {
  256. psm->pushUnknownElementState();
  257. }
  258. void NameXmlRpcRequestParserState::endElement
  259. (XmlRpcRequestParserStateMachine* psm,
  260. const char* name,
  261. const std::string& characters)
  262. {
  263. psm->setCurrentFrameName(characters);
  264. }
  265. // ArrayXmlRpcRequestParserState
  266. void ArrayXmlRpcRequestParserState::beginElement
  267. (XmlRpcRequestParserStateMachine* psm,
  268. const char* name,
  269. const std::vector<XmlAttr>& attrs)
  270. {
  271. if(strcmp(name, "data") == 0) {
  272. psm->pushDataState();
  273. } else {
  274. psm->pushUnknownElementState();
  275. }
  276. }
  277. // DataXmlRpcRequestParserState
  278. void DataXmlRpcRequestParserState::beginElement
  279. (XmlRpcRequestParserStateMachine* psm,
  280. const char* name,
  281. const std::vector<XmlAttr>& attrs)
  282. {
  283. if(strcmp(name, "value") == 0) {
  284. psm->pushFrame();
  285. psm->pushArrayValueState();
  286. } else {
  287. psm->pushUnknownElementState();
  288. }
  289. }
  290. // ArrayValueXmlRpcRequestParserState
  291. void ArrayValueXmlRpcRequestParserState::endElement
  292. (XmlRpcRequestParserStateMachine* psm,
  293. const char* name,
  294. const std::string& characters)
  295. {
  296. ValueXmlRpcRequestParserState::endElement(psm, name, characters);
  297. psm->popArrayFrame();
  298. }
  299. } // namespace rpc
  300. } // namespace aria2