PageRenderTime 107ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmessage/llhttpnode.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 494 lines | 355 code | 93 blank | 46 comment | 35 complexity | 44c9e089cd35268a66719f0c436d93f7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llhttpnode.cpp
  3. * @brief Implementation of classes for generic HTTP/LSL/REST handling.
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llhttpnode.h"
  28. #include <boost/tokenizer.hpp>
  29. #include "llstl.h"
  30. #include "lliohttpserver.h" // for string constants
  31. static const std::string CONTEXT_WILDCARD("wildcard");
  32. /**
  33. * LLHTTPNode
  34. */
  35. class LLHTTPNode::Impl
  36. {
  37. public:
  38. typedef std::map<std::string, LLHTTPNode*> ChildMap;
  39. ChildMap mNamedChildren;
  40. LLHTTPNode* mWildcardChild;
  41. std::string mWildcardName;
  42. std::string mWildcardKey;
  43. LLHTTPNode* mParentNode;
  44. Impl() : mWildcardChild(NULL), mParentNode(NULL) { }
  45. LLHTTPNode* findNamedChild(const std::string& name) const;
  46. };
  47. LLHTTPNode* LLHTTPNode::Impl::findNamedChild(const std::string& name) const
  48. {
  49. LLHTTPNode* child = get_ptr_in_map(mNamedChildren, name);
  50. if (!child && ((name[0] == '*') || (name == mWildcardName)))
  51. {
  52. child = mWildcardChild;
  53. }
  54. return child;
  55. }
  56. LLHTTPNode::LLHTTPNode()
  57. : impl(* new Impl)
  58. {
  59. }
  60. // virtual
  61. LLHTTPNode::~LLHTTPNode()
  62. {
  63. std::for_each(impl.mNamedChildren.begin(), impl.mNamedChildren.end(),
  64. DeletePairedPointer());
  65. delete impl.mWildcardChild;
  66. delete &impl;
  67. }
  68. namespace {
  69. class NotImplemented
  70. {
  71. };
  72. }
  73. // virtual
  74. LLSD LLHTTPNode::simpleGet() const
  75. {
  76. throw NotImplemented();
  77. }
  78. // virtual
  79. LLSD LLHTTPNode::simplePut(const LLSD& input) const
  80. {
  81. throw NotImplemented();
  82. }
  83. // virtual
  84. LLSD LLHTTPNode::simplePost(const LLSD& input) const
  85. {
  86. throw NotImplemented();
  87. }
  88. // virtual
  89. void LLHTTPNode::get(LLHTTPNode::ResponsePtr response, const LLSD& context) const
  90. {
  91. try
  92. {
  93. response->result(simpleGet());
  94. }
  95. catch (NotImplemented)
  96. {
  97. response->methodNotAllowed();
  98. }
  99. }
  100. // virtual
  101. void LLHTTPNode::put(LLHTTPNode::ResponsePtr response, const LLSD& context, const LLSD& input) const
  102. {
  103. try
  104. {
  105. response->result(simplePut(input));
  106. }
  107. catch (NotImplemented)
  108. {
  109. response->methodNotAllowed();
  110. }
  111. }
  112. // virtual
  113. void LLHTTPNode::post(LLHTTPNode::ResponsePtr response, const LLSD& context, const LLSD& input) const
  114. {
  115. try
  116. {
  117. response->result(simplePost(input));
  118. }
  119. catch (NotImplemented)
  120. {
  121. response->methodNotAllowed();
  122. }
  123. }
  124. // virtual
  125. void LLHTTPNode::del(LLHTTPNode::ResponsePtr response, const LLSD& context) const
  126. {
  127. try
  128. {
  129. response->result(simpleDel(context));
  130. }
  131. catch (NotImplemented)
  132. {
  133. response->methodNotAllowed();
  134. }
  135. }
  136. // virtual
  137. LLSD LLHTTPNode::simpleDel(const LLSD&) const
  138. {
  139. throw NotImplemented();
  140. }
  141. // virtual
  142. void LLHTTPNode::options(ResponsePtr response, const LLSD& context) const
  143. {
  144. //llinfos << "options context: " << context << llendl;
  145. // default implementation constructs an url to the documentation.
  146. std::string host(
  147. context[CONTEXT_REQUEST][CONTEXT_HEADERS]["host"].asString());
  148. if(host.empty())
  149. {
  150. response->status(400, "Bad Request -- need Host header");
  151. return;
  152. }
  153. std::ostringstream ostr;
  154. ostr << "http://" << host << "/web/server/api";
  155. ostr << context[CONTEXT_REQUEST]["path"].asString();
  156. static const std::string DOC_HEADER("X-Documentation-URL");
  157. response->addHeader(DOC_HEADER, ostr.str());
  158. response->status(200, "OK");
  159. }
  160. // virtual
  161. LLHTTPNode* LLHTTPNode::getChild(const std::string& name, LLSD& context) const
  162. {
  163. LLHTTPNode* namedChild = get_ptr_in_map(impl.mNamedChildren, name);
  164. if (namedChild)
  165. {
  166. return namedChild;
  167. }
  168. if (impl.mWildcardChild
  169. && impl.mWildcardChild->validate(name, context))
  170. {
  171. context[CONTEXT_REQUEST][CONTEXT_WILDCARD][impl.mWildcardKey] = name;
  172. return impl.mWildcardChild;
  173. }
  174. return NULL;
  175. }
  176. // virtual
  177. bool LLHTTPNode::handles(const LLSD& remainder, LLSD& context) const
  178. {
  179. return remainder.size() == 0;
  180. }
  181. // virtual
  182. bool LLHTTPNode::validate(const std::string& name, LLSD& context) const
  183. {
  184. return false;
  185. }
  186. const LLHTTPNode* LLHTTPNode::traverse(
  187. const std::string& path, LLSD& context) const
  188. {
  189. typedef boost::tokenizer< boost::char_separator<char> > tokenizer;
  190. boost::char_separator<char> sep("/", "", boost::drop_empty_tokens);
  191. tokenizer tokens(path, sep);
  192. tokenizer::iterator iter = tokens.begin();
  193. tokenizer::iterator end = tokens.end();
  194. const LLHTTPNode* node = this;
  195. for(; iter != end; ++iter)
  196. {
  197. LLHTTPNode* child = node->getChild(*iter, context);
  198. if(!child)
  199. {
  200. lldebugs << "LLHTTPNode::traverse: Couldn't find '" << *iter << "'" << llendl;
  201. break;
  202. }
  203. lldebugs << "LLHTTPNode::traverse: Found '" << *iter << "'" << llendl;
  204. node = child;
  205. }
  206. LLSD& remainder = context[CONTEXT_REQUEST]["remainder"];
  207. for(; iter != end; ++iter)
  208. {
  209. remainder.append(*iter);
  210. }
  211. return node->handles(remainder, context) ? node : NULL;
  212. }
  213. void LLHTTPNode::addNode(const std::string& path, LLHTTPNode* nodeToAdd)
  214. {
  215. typedef boost::tokenizer< boost::char_separator<char> > tokenizer;
  216. boost::char_separator<char> sep("/", "", boost::drop_empty_tokens);
  217. tokenizer tokens(path, sep);
  218. tokenizer::iterator iter = tokens.begin();
  219. tokenizer::iterator end = tokens.end();
  220. LLHTTPNode* node = this;
  221. for(; iter != end; ++iter)
  222. {
  223. LLHTTPNode* child = node->impl.findNamedChild(*iter);
  224. if (!child) { break; }
  225. node = child;
  226. }
  227. if (iter == end)
  228. {
  229. llwarns << "LLHTTPNode::addNode: already a node that handles "
  230. << path << llendl;
  231. return;
  232. }
  233. while (true)
  234. {
  235. std::string pathPart = *iter;
  236. ++iter;
  237. bool lastOne = iter == end;
  238. LLHTTPNode* nextNode = lastOne ? nodeToAdd : new LLHTTPNode();
  239. switch (pathPart[0])
  240. {
  241. case '<':
  242. // *NOTE: This should really validate that it is of
  243. // the proper form: <wildcardkey> so that the substr()
  244. // generates the correct key name.
  245. node->impl.mWildcardChild = nextNode;
  246. node->impl.mWildcardName = pathPart;
  247. if(node->impl.mWildcardKey.empty())
  248. {
  249. node->impl.mWildcardKey = pathPart.substr(
  250. 1,
  251. pathPart.size() - 2);
  252. }
  253. break;
  254. case '*':
  255. node->impl.mWildcardChild = nextNode;
  256. if(node->impl.mWildcardName.empty())
  257. {
  258. node->impl.mWildcardName = pathPart;
  259. }
  260. break;
  261. default:
  262. node->impl.mNamedChildren[pathPart] = nextNode;
  263. }
  264. nextNode->impl.mParentNode = node;
  265. if (lastOne) break;
  266. node = nextNode;
  267. }
  268. }
  269. static void append_node_paths(LLSD& result,
  270. const std::string& name, const LLHTTPNode* node)
  271. {
  272. result.append(name);
  273. LLSD paths = node->allNodePaths();
  274. LLSD::array_const_iterator i = paths.beginArray();
  275. LLSD::array_const_iterator end = paths.endArray();
  276. for (; i != end; ++i)
  277. {
  278. result.append(name + "/" + (*i).asString());
  279. }
  280. }
  281. LLSD LLHTTPNode::allNodePaths() const
  282. {
  283. LLSD result;
  284. Impl::ChildMap::const_iterator i = impl.mNamedChildren.begin();
  285. Impl::ChildMap::const_iterator end = impl.mNamedChildren.end();
  286. for (; i != end; ++i)
  287. {
  288. append_node_paths(result, i->first, i->second);
  289. }
  290. if (impl.mWildcardChild)
  291. {
  292. append_node_paths(result, impl.mWildcardName, impl.mWildcardChild);
  293. }
  294. return result;
  295. }
  296. const LLHTTPNode* LLHTTPNode::rootNode() const
  297. {
  298. const LLHTTPNode* node = this;
  299. while (true)
  300. {
  301. const LLHTTPNode* next = node->impl.mParentNode;
  302. if (!next)
  303. {
  304. return node;
  305. }
  306. node = next;
  307. }
  308. }
  309. const LLHTTPNode* LLHTTPNode::findNode(const std::string& name) const
  310. {
  311. return impl.findNamedChild(name);
  312. }
  313. LLHTTPNode::Response::~Response()
  314. {
  315. }
  316. void LLHTTPNode::Response::statusUnknownError(S32 code)
  317. {
  318. status(code, "Unknown Error");
  319. }
  320. void LLHTTPNode::Response::notFound(const std::string& message)
  321. {
  322. status(404, message);
  323. }
  324. void LLHTTPNode::Response::notFound()
  325. {
  326. status(404, "Not Found");
  327. }
  328. void LLHTTPNode::Response::methodNotAllowed()
  329. {
  330. status(405, "Method Not Allowed");
  331. }
  332. void LLHTTPNode::Response::addHeader(
  333. const std::string& name,
  334. const std::string& value)
  335. {
  336. mHeaders[name] = value;
  337. }
  338. void LLHTTPNode::describe(Description& desc) const
  339. {
  340. desc.shortInfo("unknown service (missing describe() method)");
  341. }
  342. const LLChainIOFactory* LLHTTPNode::getProtocolHandler() const
  343. {
  344. return NULL;
  345. }
  346. namespace
  347. {
  348. typedef std::map<std::string, LLHTTPRegistrar::NodeFactory*> FactoryMap;
  349. FactoryMap& factoryMap()
  350. {
  351. static FactoryMap theMap;
  352. return theMap;
  353. }
  354. }
  355. LLHTTPRegistrar::NodeFactory::~NodeFactory() { }
  356. void LLHTTPRegistrar::registerFactory(
  357. const std::string& path, NodeFactory& factory)
  358. {
  359. factoryMap()[path] = &factory;
  360. }
  361. void LLHTTPRegistrar::buildAllServices(LLHTTPNode& root)
  362. {
  363. const FactoryMap& map = factoryMap();
  364. FactoryMap::const_iterator i = map.begin();
  365. FactoryMap::const_iterator end = map.end();
  366. for (; i != end; ++i)
  367. {
  368. LL_DEBUGS("AppInit") << "LLHTTPRegistrar::buildAllServices adding node for path "
  369. << i->first << LL_ENDL;
  370. root.addNode(i->first, i->second->build());
  371. }
  372. }
  373. LLPointer<LLSimpleResponse> LLSimpleResponse::create()
  374. {
  375. return new LLSimpleResponse();
  376. }
  377. LLSimpleResponse::~LLSimpleResponse()
  378. {
  379. }
  380. void LLSimpleResponse::result(const LLSD& result)
  381. {
  382. status(200, "OK");
  383. }
  384. void LLSimpleResponse::extendedResult(S32 code, const std::string& body, const LLSD& headers)
  385. {
  386. status(code,body);
  387. }
  388. void LLSimpleResponse::status(S32 code, const std::string& message)
  389. {
  390. mCode = code;
  391. mMessage = message;
  392. }
  393. void LLSimpleResponse::print(std::ostream& out) const
  394. {
  395. out << mCode << " " << mMessage;
  396. }
  397. std::ostream& operator<<(std::ostream& out, const LLSimpleResponse& resp)
  398. {
  399. resp.print(out);
  400. return out;
  401. }