PageRenderTime 188ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmessage/llhttpnode.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 389 lines | 147 code | 63 blank | 179 comment | 0 complexity | bc4fca9ba870137ab5fe8a7db128bac2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llhttpnode.h
  3. * @brief Declaration 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. #ifndef LL_LLHTTPNODE_H
  27. #define LL_LLHTTPNODE_H
  28. #include "llpointer.h"
  29. #include "llrefcount.h"
  30. #include "llsd.h"
  31. class LLChainIOFactory;
  32. /**
  33. * These classes represent the HTTP framework: The URL tree, and the LLSD
  34. * REST interface that such nodes implement.
  35. *
  36. * To implement a service, in most cases, subclass LLHTTPNode, implement
  37. * get() or post(), and create a global instance of LLHTTPRegistration<>.
  38. * This can all be done in a .cpp file, with no publically declared parts.
  39. *
  40. * To implement a server see lliohttpserver.h
  41. * @see LLHTTPWireServer
  42. */
  43. /**
  44. * @class LLHTTPNode
  45. * @brief Base class which handles url traversal, response routing
  46. * and support for standard LLSD services
  47. *
  48. * Users of the HTTP responder will typically derive a class from this
  49. * one, implement the get(), put() and/or post() methods, and then
  50. * use LLHTTPRegistration to insert it into the URL tree.
  51. *
  52. * The default implementation handles servicing the request and creating
  53. * the pipe fittings needed to read the headers, manage them, convert
  54. * to and from LLSD, etc.
  55. */
  56. class LLHTTPNode
  57. {
  58. public:
  59. LLHTTPNode();
  60. virtual ~LLHTTPNode();
  61. /** @name Responses
  62. Most subclasses override one or more of these methods to provide
  63. the service. By default, the rest of the LLHTTPNode architecture
  64. will handle requests, create the needed LLIOPump, parse the input
  65. to LLSD, and format the LLSD result to the output.
  66. The default implementation of each of these is to call
  67. response->methodNotAllowed(); The "simple" versions can be
  68. overridden instead in those cases where the service can return
  69. an immediately computed response.
  70. */
  71. //@{
  72. public:
  73. virtual LLSD simpleGet() const;
  74. virtual LLSD simplePut(const LLSD& input) const;
  75. virtual LLSD simplePost(const LLSD& input) const;
  76. virtual LLSD simpleDel(const LLSD& context) const;
  77. /**
  78. * @brief Abstract Base Class declaring Response interface.
  79. */
  80. class Response : public LLRefCount
  81. {
  82. protected:
  83. virtual ~Response();
  84. public:
  85. /**
  86. * @brief Return the LLSD content and a 200 OK.
  87. */
  88. virtual void result(const LLSD&) = 0;
  89. /**
  90. * @brief return status code and message with headers.
  91. */
  92. virtual void extendedResult(S32 code, const std::string& message, const LLSD& headers) = 0;
  93. /**
  94. * @brief return status code and reason string on http header,
  95. * but do not return a payload.
  96. */
  97. virtual void status(S32 code, const std::string& message) = 0;
  98. /**
  99. * @brief Return no body, just status code and 'UNKNOWN ERROR'.
  100. */
  101. virtual void statusUnknownError(S32 code);
  102. virtual void notFound(const std::string& message);
  103. virtual void notFound();
  104. virtual void methodNotAllowed();
  105. /**
  106. * @breif Add a name: value http header.
  107. *
  108. * No effort is made to ensure the response is a valid http
  109. * header.
  110. * The headers are stored as a map of header name : value.
  111. * Though HTTP allows the same header name to be transmitted
  112. * more than once, this implementation only stores a header
  113. * name once.
  114. * @param name The name of the header, eg, "Content-Encoding"
  115. * @param value The value of the header, eg, "gzip"
  116. */
  117. virtual void addHeader(const std::string& name, const std::string& value);
  118. protected:
  119. /**
  120. * @brief Headers to be sent back with the HTTP response.
  121. *
  122. * Protected class membership since derived classes are
  123. * expected to use it and there is no use case yet for other
  124. * uses. If such a use case arises, I suggest making a
  125. * headers() public method, and moving this member data into
  126. * private.
  127. */
  128. LLSD mHeaders;
  129. };
  130. typedef LLPointer<Response> ResponsePtr;
  131. virtual void get(ResponsePtr, const LLSD& context) const;
  132. virtual void put(
  133. ResponsePtr,
  134. const LLSD& context,
  135. const LLSD& input) const;
  136. virtual void post(
  137. ResponsePtr,
  138. const LLSD& context,
  139. const LLSD& input) const;
  140. virtual void del(ResponsePtr, const LLSD& context) const;
  141. virtual void options(ResponsePtr, const LLSD& context) const;
  142. //@}
  143. /** @name URL traversal
  144. The tree is traversed by calling getChild() with successive
  145. path components, on successive results. When getChild() returns
  146. null, or there are no more components, the last child responds to
  147. the request.
  148. The default behavior is generally correct, though wildcard nodes
  149. will want to implement validate().
  150. */
  151. //@{
  152. public:
  153. virtual LLHTTPNode* getChild(const std::string& name, LLSD& context) const;
  154. /**< returns a child node, if any, at the given name
  155. default looks at children and wildcard child (see below)
  156. */
  157. virtual bool handles(const LLSD& remainder, LLSD& context) const;
  158. /**< return true if this node can service the remaining components;
  159. default returns true if there are no remaining components
  160. */
  161. virtual bool validate(const std::string& name, LLSD& context) const;
  162. /**< called only on wildcard nodes, to check if they will handle
  163. the name; default is false; overrides will want to check
  164. name, and return true if the name will construct to a valid url.
  165. For convenience, the <code>getChild()</code> method above will
  166. automatically insert the name in
  167. context["request"]["wildcard"][key] if this method returns true.
  168. For example, the node "agent/<agent_id>/detail" will set
  169. context["request"]["wildcard"]["agent_id"] eqaul to the value
  170. found during traversal.
  171. */
  172. const LLHTTPNode* traverse(const std::string& path, LLSD& context) const;
  173. /**< find a node, if any, that can service this path
  174. set up context["request"] information
  175. */
  176. //@}
  177. /** @name Child Nodes
  178. The standard node can have any number of child nodes under
  179. fixed names, and optionally one "wildcard" node that can
  180. handle all other names.
  181. Usually, child nodes are add through LLHTTPRegistration, not
  182. by calling this interface directly.
  183. The added node will be now owned by the parent node.
  184. */
  185. //@{
  186. virtual void addNode(const std::string& path, LLHTTPNode* nodeToAdd);
  187. LLSD allNodePaths() const;
  188. ///< Returns an arrary of node paths at and under this node
  189. const LLHTTPNode* rootNode() const;
  190. const LLHTTPNode* findNode(const std::string& name) const;
  191. enum EHTTPNodeContentType
  192. {
  193. CONTENT_TYPE_LLSD,
  194. CONTENT_TYPE_TEXT
  195. };
  196. virtual EHTTPNodeContentType getContentType() const { return CONTENT_TYPE_LLSD; }
  197. //@}
  198. /* @name Description system
  199. The Description object contains information about a service.
  200. All subclasses of LLHTTPNode should override describe() and use
  201. the methods of the Description class to set the various properties.
  202. */
  203. //@{
  204. class Description
  205. {
  206. public:
  207. void shortInfo(const std::string& s){ mInfo["description"] = s; }
  208. void longInfo(const std::string& s) { mInfo["details"] = s; }
  209. // Call this method when the service supports the specified verb.
  210. void getAPI() { mInfo["api"].append("GET"); }
  211. void putAPI() { mInfo["api"].append("PUT"); }
  212. void postAPI() { mInfo["api"].append("POST"); }
  213. void delAPI() { mInfo["api"].append("DELETE"); }
  214. void input(const std::string& s) { mInfo["input"] = s; }
  215. void output(const std::string& s) { mInfo["output"] = s; }
  216. void source(const char* f, int l) { mInfo["__file__"] = f;
  217. mInfo["__line__"] = l; }
  218. LLSD getInfo() const { return mInfo; }
  219. private:
  220. LLSD mInfo;
  221. };
  222. virtual void describe(Description&) const;
  223. //@}
  224. virtual const LLChainIOFactory* getProtocolHandler() const;
  225. /**< Return a factory object for handling wire protocols.
  226. * The base class returns NULL, as it doesn't know about
  227. * wire protocols at all. This is okay for most nodes
  228. * as LLIOHTTPServer is smart enough to use a default
  229. * wire protocol for HTTP for such nodes. Specialized
  230. * subclasses that handle things like XML-RPC will want
  231. * to implement this. (See LLXMLSDRPCServerFactory.)
  232. */
  233. private:
  234. class Impl;
  235. Impl& impl;
  236. };
  237. class LLSimpleResponse : public LLHTTPNode::Response
  238. {
  239. public:
  240. static LLPointer<LLSimpleResponse> create();
  241. void result(const LLSD& result);
  242. void extendedResult(S32 code, const std::string& body, const LLSD& headers);
  243. void status(S32 code, const std::string& message);
  244. void print(std::ostream& out) const;
  245. S32 mCode;
  246. std::string mMessage;
  247. protected:
  248. ~LLSimpleResponse();
  249. private:
  250. LLSimpleResponse() : mCode(0) {} // Must be accessed through LLPointer.
  251. };
  252. std::ostream& operator<<(std::ostream& out, const LLSimpleResponse& resp);
  253. /**
  254. * @name Automatic LLHTTPNode registration
  255. *
  256. * To register a node type at a particular url path, construct a global instance
  257. * of LLHTTPRegistration:
  258. *
  259. * LLHTTPRegistration<LLMyNodeType> gHTTPServiceAlphaBeta("/alpha/beta");
  260. *
  261. * (Note the naming convention carefully.) This object must be global and not
  262. * static. However, it needn't be declared in your .h file. It can exist
  263. * solely in the .cpp file. The same is true of your subclass of LLHTTPNode:
  264. * it can be declared and defined wholly within the .cpp file.
  265. *
  266. * When constructing a web server, use LLHTTPRegistrar to add all the registered
  267. * nodes to the url tree:
  268. *
  269. * LLHTTPRegistrar::buidlAllServices(mRootNode);
  270. */
  271. //@{
  272. class LLHTTPRegistrar
  273. {
  274. public:
  275. class NodeFactory
  276. {
  277. public:
  278. virtual ~NodeFactory();
  279. virtual LLHTTPNode* build() const = 0;
  280. };
  281. static void buildAllServices(LLHTTPNode& root);
  282. static void registerFactory(const std::string& path, NodeFactory& factory);
  283. ///< construct an LLHTTPRegistration below to call this
  284. };
  285. template < class NodeType >
  286. class LLHTTPRegistration
  287. {
  288. public:
  289. LLHTTPRegistration(const std::string& path)
  290. {
  291. LLHTTPRegistrar::registerFactory(path, mFactory);
  292. }
  293. private:
  294. class ThisNodeFactory : public LLHTTPRegistrar::NodeFactory
  295. {
  296. public:
  297. virtual LLHTTPNode* build() const { return new NodeType; }
  298. };
  299. ThisNodeFactory mFactory;
  300. };
  301. template < class NodeType>
  302. class LLHTTPParamRegistration
  303. {
  304. public:
  305. LLHTTPParamRegistration(const std::string& path, LLSD params) :
  306. mFactory(params)
  307. {
  308. LLHTTPRegistrar::registerFactory(path, mFactory);
  309. }
  310. private:
  311. class ThisNodeFactory : public LLHTTPRegistrar::NodeFactory
  312. {
  313. public:
  314. ThisNodeFactory(LLSD params) : mParams(params) {}
  315. virtual LLHTTPNode* build() const { return new NodeType(mParams); }
  316. private:
  317. LLSD mParams;
  318. };
  319. ThisNodeFactory mFactory;
  320. };
  321. //@}
  322. #endif // LL_LLHTTPNODE_H