XmlRpcRequestParserStateImpl.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 xmlrpc {
  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(BDE::list());
  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(BDE::dict());
  140. stm->pushStructState();
  141. } else if(name == elements::ARRAY) {
  142. stm->setCurrentFrameValue(BDE::list());
  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. // IntXmlRpcRequestParserState
  153. void IntXmlRpcRequestParserState::beginElement
  154. (XmlRpcRequestParserStateMachine* stm,
  155. const std::string& name,
  156. const std::map<std::string, std::string>& attrs)
  157. {
  158. stm->pushUnknownElementState();
  159. }
  160. void IntXmlRpcRequestParserState::endElement
  161. (XmlRpcRequestParserStateMachine* stm,
  162. const std::string& name,
  163. const std::string& characters)
  164. {
  165. try {
  166. int64_t value = util::parseLLInt(characters);
  167. stm->setCurrentFrameValue(BDE(value));
  168. } catch(RecoverableException& e) {
  169. // nothing to do here: We just leave current frame value to BDE::none
  170. }
  171. }
  172. // StringXmlRpcRequestParserState
  173. void StringXmlRpcRequestParserState::beginElement
  174. (XmlRpcRequestParserStateMachine* stm,
  175. const std::string& name,
  176. const std::map<std::string, std::string>& attrs)
  177. {
  178. stm->pushUnknownElementState();
  179. }
  180. void StringXmlRpcRequestParserState::endElement
  181. (XmlRpcRequestParserStateMachine* stm,
  182. const std::string& name,
  183. const std::string& characters)
  184. {
  185. stm->setCurrentFrameValue(BDE(characters));
  186. }
  187. // Base64XmlRpcRequestParserState
  188. void Base64XmlRpcRequestParserState::beginElement
  189. (XmlRpcRequestParserStateMachine* stm,
  190. const std::string& name,
  191. const std::map<std::string, std::string>& attrs)
  192. {
  193. stm->pushUnknownElementState();
  194. }
  195. void Base64XmlRpcRequestParserState::endElement
  196. (XmlRpcRequestParserStateMachine* stm,
  197. const std::string& name,
  198. const std::string& characters)
  199. {
  200. stm->setCurrentFrameValue(BDE(Base64::decode(characters)));
  201. }
  202. // StructXmlRpcRequestParserState
  203. void StructXmlRpcRequestParserState::beginElement
  204. (XmlRpcRequestParserStateMachine* stm,
  205. const std::string& name,
  206. const std::map<std::string, std::string>& attrs)
  207. {
  208. if(name == elements::MEMBER) {
  209. stm->pushFrame();
  210. stm->pushMemberState();
  211. } else {
  212. stm->pushUnknownElementState();
  213. }
  214. }
  215. // MemberXmlRpcRequestParserState
  216. void MemberXmlRpcRequestParserState::beginElement
  217. (XmlRpcRequestParserStateMachine* stm,
  218. const std::string& name,
  219. const std::map<std::string, std::string>& attrs)
  220. {
  221. if(name == elements::NAME) {
  222. stm->pushNameState();
  223. } else if(name == elements::VALUE) {
  224. stm->pushValueState();
  225. } else {
  226. stm->pushUnknownElementState();
  227. }
  228. }
  229. void MemberXmlRpcRequestParserState::endElement
  230. (XmlRpcRequestParserStateMachine* stm,
  231. const std::string& name,
  232. const std::string& characters)
  233. {
  234. stm->popStructFrame();
  235. }
  236. // NameXmlRpcRequestParserState
  237. void NameXmlRpcRequestParserState::beginElement
  238. (XmlRpcRequestParserStateMachine* stm,
  239. const std::string& name,
  240. const std::map<std::string, std::string>& attrs)
  241. {
  242. stm->pushUnknownElementState();
  243. }
  244. void NameXmlRpcRequestParserState::endElement
  245. (XmlRpcRequestParserStateMachine* stm,
  246. const std::string& name,
  247. const std::string& characters)
  248. {
  249. stm->setCurrentFrameName(characters);
  250. }
  251. // ArrayXmlRpcRequestParserState
  252. void ArrayXmlRpcRequestParserState::beginElement
  253. (XmlRpcRequestParserStateMachine* stm,
  254. const std::string& name,
  255. const std::map<std::string, std::string>& attrs)
  256. {
  257. if(name == elements::DATA) {
  258. stm->pushDataState();
  259. } else {
  260. stm->pushUnknownElementState();
  261. }
  262. }
  263. // DataXmlRpcRequestParserState
  264. void DataXmlRpcRequestParserState::beginElement
  265. (XmlRpcRequestParserStateMachine* stm,
  266. const std::string& name,
  267. const std::map<std::string, std::string>& attrs)
  268. {
  269. if(name == elements::VALUE) {
  270. stm->pushFrame();
  271. stm->pushArrayValueState();
  272. } else {
  273. stm->pushUnknownElementState();
  274. }
  275. }
  276. // ArrayValueXmlRpcRequestParserState
  277. void ArrayValueXmlRpcRequestParserState::endElement
  278. (XmlRpcRequestParserStateMachine* stm,
  279. const std::string& name,
  280. const std::string& characters)
  281. {
  282. stm->popArrayFrame();
  283. }
  284. } // namespace xmlrpc
  285. } // namespace aria2