XmlRpcRequestParserStateImpl.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "XmlRpcRequestParserStateMachine.h"
  37. #include "XmlRpcElements.h"
  38. #include "RecoverableException.h"
  39. #include "util.h"
  40. #include "Base64.h"
  41. namespace aria2 {
  42. namespace rpc {
  43. // InitialXmlRpcRequestParserState
  44. void InitialXmlRpcRequestParserState::beginElement
  45. (XmlRpcRequestParserStateMachine* stm,
  46. const std::string& name,
  47. const std::map<std::string, std::string>& attrs)
  48. {
  49. if(name == elements::METHOD_CALL) {
  50. stm->pushMethodCallState();
  51. } else {
  52. stm->pushUnknownElementState();
  53. }
  54. }
  55. void InitialXmlRpcRequestParserState::endElement
  56. (XmlRpcRequestParserStateMachine* stm,
  57. const std::string& name,
  58. const std::string& characters)
  59. {}
  60. // UnknownElementXmlRpcRequestParserState
  61. void UnknownElementXmlRpcRequestParserState::beginElement
  62. (XmlRpcRequestParserStateMachine* stm,
  63. const std::string& name,
  64. const std::map<std::string, std::string>& attrs)
  65. {
  66. stm->pushUnknownElementState();
  67. }
  68. // MethodCallXmlRpcRequestParserState
  69. void MethodCallXmlRpcRequestParserState::beginElement
  70. (XmlRpcRequestParserStateMachine* stm,
  71. const std::string& name,
  72. const std::map<std::string, std::string>& attrs)
  73. {
  74. if(name == elements::METHOD_NAME) {
  75. stm->pushMethodNameState();
  76. } else if(name == elements::A2_PARAMS) {
  77. stm->setCurrentFrameValue(List::g());
  78. stm->pushParamsState();
  79. } else {
  80. stm->pushUnknownElementState();
  81. }
  82. }
  83. // MethodNameXmlRpcRequestParserState
  84. void MethodNameXmlRpcRequestParserState::beginElement
  85. (XmlRpcRequestParserStateMachine* stm,
  86. const std::string& name,
  87. const std::map<std::string, std::string>& attrs)
  88. {
  89. stm->pushUnknownElementState();
  90. }
  91. void MethodNameXmlRpcRequestParserState::endElement
  92. (XmlRpcRequestParserStateMachine* stm,
  93. const std::string& name,
  94. const std::string& characters)
  95. {
  96. stm->setMethodName(characters);
  97. }
  98. // ParamsXmlRpcRequestParserState
  99. void ParamsXmlRpcRequestParserState::beginElement
  100. (XmlRpcRequestParserStateMachine* stm,
  101. const std::string& name,
  102. const std::map<std::string, std::string>& attrs)
  103. {
  104. if(name == elements::PARAM) {
  105. stm->pushFrame();
  106. stm->pushParamState();
  107. } else {
  108. stm->pushUnknownElementState();
  109. }
  110. }
  111. // ParamXmlRpcRequestParserState
  112. void ParamXmlRpcRequestParserState::beginElement
  113. (XmlRpcRequestParserStateMachine* stm,
  114. const std::string& name,
  115. const std::map<std::string, std::string>& attrs)
  116. {
  117. if(name == elements::VALUE) {
  118. stm->pushValueState();
  119. } else {
  120. stm->pushUnknownElementState();
  121. }
  122. }
  123. void ParamXmlRpcRequestParserState::endElement
  124. (XmlRpcRequestParserStateMachine* stm,
  125. const std::string& name,
  126. const std::string& characters)
  127. {
  128. stm->popArrayFrame();
  129. }
  130. // ValueXmlRpcRequestParserState
  131. void ValueXmlRpcRequestParserState::beginElement
  132. (XmlRpcRequestParserStateMachine* stm,
  133. const std::string& name,
  134. const std::map<std::string, std::string>& attrs)
  135. {
  136. if(name == elements::I4 || name == elements::INT) {
  137. stm->pushIntState();
  138. } else if(name == elements::STRUCT) {
  139. stm->setCurrentFrameValue(Dict::g());
  140. stm->pushStructState();
  141. } else if(name == elements::ARRAY) {
  142. stm->setCurrentFrameValue(List::g());
  143. stm->pushArrayState();
  144. } else if(name == elements::STRING || name == elements::DOUBLE) {
  145. stm->pushStringState();
  146. } else if(name == elements::BASE64) {
  147. stm->pushBase64State();
  148. } else {
  149. stm->pushUnknownElementState();
  150. }
  151. }
  152. void ValueXmlRpcRequestParserState::endElement
  153. (XmlRpcRequestParserStateMachine* stm,
  154. const std::string& name,
  155. const std::string& characters)
  156. {
  157. // XML-RPC specification says that if no data type tag is used, the
  158. // data must be treated as string. To prevent from overwriting
  159. // current frame value, we first check it is still null.
  160. if(!stm->getCurrentFrameValue() && !characters.empty()) {
  161. stm->setCurrentFrameValue(String::g(characters));
  162. }
  163. }
  164. // IntXmlRpcRequestParserState
  165. void IntXmlRpcRequestParserState::beginElement
  166. (XmlRpcRequestParserStateMachine* stm,
  167. const std::string& name,
  168. const std::map<std::string, std::string>& attrs)
  169. {
  170. stm->pushUnknownElementState();
  171. }
  172. void IntXmlRpcRequestParserState::endElement
  173. (XmlRpcRequestParserStateMachine* stm,
  174. const std::string& name,
  175. const std::string& characters)
  176. {
  177. try {
  178. int64_t value = util::parseLLInt(characters);
  179. stm->setCurrentFrameValue(Integer::g(value));
  180. } catch(RecoverableException& e) {
  181. // nothing to do here: We just leave current frame value to null.
  182. }
  183. }
  184. // StringXmlRpcRequestParserState
  185. void StringXmlRpcRequestParserState::beginElement
  186. (XmlRpcRequestParserStateMachine* stm,
  187. const std::string& name,
  188. const std::map<std::string, std::string>& attrs)
  189. {
  190. stm->pushUnknownElementState();
  191. }
  192. void StringXmlRpcRequestParserState::endElement
  193. (XmlRpcRequestParserStateMachine* stm,
  194. const std::string& name,
  195. const std::string& characters)
  196. {
  197. stm->setCurrentFrameValue(String::g(characters));
  198. }
  199. // Base64XmlRpcRequestParserState
  200. void Base64XmlRpcRequestParserState::beginElement
  201. (XmlRpcRequestParserStateMachine* stm,
  202. const std::string& name,
  203. const std::map<std::string, std::string>& attrs)
  204. {
  205. stm->pushUnknownElementState();
  206. }
  207. void Base64XmlRpcRequestParserState::endElement
  208. (XmlRpcRequestParserStateMachine* stm,
  209. const std::string& name,
  210. const std::string& characters)
  211. {
  212. stm->setCurrentFrameValue(String::g(Base64::decode(characters)));
  213. }
  214. // StructXmlRpcRequestParserState
  215. void StructXmlRpcRequestParserState::beginElement
  216. (XmlRpcRequestParserStateMachine* stm,
  217. const std::string& name,
  218. const std::map<std::string, std::string>& attrs)
  219. {
  220. if(name == elements::MEMBER) {
  221. stm->pushFrame();
  222. stm->pushMemberState();
  223. } else {
  224. stm->pushUnknownElementState();
  225. }
  226. }
  227. // MemberXmlRpcRequestParserState
  228. void MemberXmlRpcRequestParserState::beginElement
  229. (XmlRpcRequestParserStateMachine* stm,
  230. const std::string& name,
  231. const std::map<std::string, std::string>& attrs)
  232. {
  233. if(name == elements::NAME) {
  234. stm->pushNameState();
  235. } else if(name == elements::VALUE) {
  236. stm->pushValueState();
  237. } else {
  238. stm->pushUnknownElementState();
  239. }
  240. }
  241. void MemberXmlRpcRequestParserState::endElement
  242. (XmlRpcRequestParserStateMachine* stm,
  243. const std::string& name,
  244. const std::string& characters)
  245. {
  246. stm->popStructFrame();
  247. }
  248. // NameXmlRpcRequestParserState
  249. void NameXmlRpcRequestParserState::beginElement
  250. (XmlRpcRequestParserStateMachine* stm,
  251. const std::string& name,
  252. const std::map<std::string, std::string>& attrs)
  253. {
  254. stm->pushUnknownElementState();
  255. }
  256. void NameXmlRpcRequestParserState::endElement
  257. (XmlRpcRequestParserStateMachine* stm,
  258. const std::string& name,
  259. const std::string& characters)
  260. {
  261. stm->setCurrentFrameName(characters);
  262. }
  263. // ArrayXmlRpcRequestParserState
  264. void ArrayXmlRpcRequestParserState::beginElement
  265. (XmlRpcRequestParserStateMachine* stm,
  266. const std::string& name,
  267. const std::map<std::string, std::string>& attrs)
  268. {
  269. if(name == elements::DATA) {
  270. stm->pushDataState();
  271. } else {
  272. stm->pushUnknownElementState();
  273. }
  274. }
  275. // DataXmlRpcRequestParserState
  276. void DataXmlRpcRequestParserState::beginElement
  277. (XmlRpcRequestParserStateMachine* stm,
  278. const std::string& name,
  279. const std::map<std::string, std::string>& attrs)
  280. {
  281. if(name == elements::VALUE) {
  282. stm->pushFrame();
  283. stm->pushArrayValueState();
  284. } else {
  285. stm->pushUnknownElementState();
  286. }
  287. }
  288. // ArrayValueXmlRpcRequestParserState
  289. void ArrayValueXmlRpcRequestParserState::endElement
  290. (XmlRpcRequestParserStateMachine* stm,
  291. const std::string& name,
  292. const std::string& characters)
  293. {
  294. ValueXmlRpcRequestParserState::endElement(stm, name, characters);
  295. stm->popArrayFrame();
  296. }
  297. } // namespace rpc
  298. } // namespace aria2