123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- /**
- * @file llhttpnode.cpp
- * @brief Implementation of classes for generic HTTP/LSL/REST handling.
- *
- * $LicenseInfo:firstyear=2006&license=viewergpl$
- *
- * Copyright (c) 2006-2009, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- #include "linden_common.h"
- #include "boost/tokenizer.hpp"
- #include "llhttpnode.h"
- #include "llhttpconstants.h"
- #include "llstl.h"
- /**
- * LLHTTPNode
- */
- class LLHTTPNode::Impl
- {
- public:
- typedef std::map<std::string, LLHTTPNode*> ChildMap;
- ChildMap mNamedChildren;
- LLHTTPNode* mWildcardChild;
- std::string mWildcardName;
- std::string mWildcardKey;
- LLHTTPNode* mParentNode;
- Impl() : mWildcardChild(NULL), mParentNode(NULL) {}
- LLHTTPNode* findNamedChild(const std::string& name) const;
- };
- LLHTTPNode* LLHTTPNode::Impl::findNamedChild(const std::string& name) const
- {
- LLHTTPNode* child = get_ptr_in_map(mNamedChildren, name);
- if (!child && (name[0] == '*' || name == mWildcardName))
- {
- child = mWildcardChild;
- }
- return child;
- }
- LLHTTPNode::LLHTTPNode()
- : impl(*new Impl)
- {
- }
- // virtual
- LLHTTPNode::~LLHTTPNode()
- {
- std::for_each(impl.mNamedChildren.begin(), impl.mNamedChildren.end(),
- DeletePairedPointer());
- impl.mNamedChildren.clear();
- delete impl.mWildcardChild;
- delete &impl;
- }
- namespace
- {
- class NotImplemented
- {
- };
- }
- // virtual
- LLSD LLHTTPNode::simpleGet() const
- {
- throw NotImplemented();
- }
- // virtual
- LLSD LLHTTPNode::simplePut(const LLSD& input) const
- {
- throw NotImplemented();
- }
- // virtual
- LLSD LLHTTPNode::simplePost(const LLSD& input) const
- {
- throw NotImplemented();
- }
- // virtual
- void LLHTTPNode::get(LLHTTPNode::ResponsePtr response,
- const LLSD& context) const
- {
- try
- {
- response->result(simpleGet());
- }
- catch (NotImplemented)
- {
- response->methodNotAllowed();
- }
- }
- // virtual
- void LLHTTPNode::put(LLHTTPNode::ResponsePtr response, const LLSD& context,
- const LLSD& input) const
- {
- try
- {
- response->result(simplePut(input));
- }
- catch (NotImplemented)
- {
- response->methodNotAllowed();
- }
- }
- // virtual
- void LLHTTPNode::post(LLHTTPNode::ResponsePtr response, const LLSD& context,
- const LLSD& input) const
- {
- try
- {
- response->result(simplePost(input));
- }
- catch (NotImplemented)
- {
- response->methodNotAllowed();
- }
- }
- // virtual
- void LLHTTPNode::del(LLHTTPNode::ResponsePtr response,
- const LLSD& context) const
- {
- try
- {
- response->result(simpleDel(context));
- }
- catch (NotImplemented)
- {
- response->methodNotAllowed();
- }
- }
- // virtual
- LLSD LLHTTPNode::simpleDel(const LLSD&) const
- {
- throw NotImplemented();
- }
- // virtual
- void LLHTTPNode::options(ResponsePtr response, const LLSD& context) const
- {
- LL_DEBUGS("LLHTTPNode") << "context: " << context << LL_ENDL;
- // default implementation constructs an url to the documentation.
- // *TODO: Check for 'Host' header instead of 'host' header?
- std::string host;
- host = context[CONTEXT_REQUEST][CONTEXT_HEADERS][HTTP_IN_HEADER_HOST].asString();
- if (host.empty())
- {
- response->status(HTTP_BAD_REQUEST, "Bad Request -- need Host header");
- return;
- }
- std::ostringstream ostr;
- ostr << "http://" << host << "/web/server/api";
- ostr << context[CONTEXT_REQUEST][CONTEXT_PATH].asString();
- static const std::string DOC_HEADER("X-Documentation-URL");
- response->addHeader(DOC_HEADER, ostr.str());
- response->status(HTTP_OK, "OK");
- }
- // virtual
- LLHTTPNode* LLHTTPNode::getChild(const std::string& name, LLSD& context) const
- {
- LLHTTPNode* namedChild = get_ptr_in_map(impl.mNamedChildren, name);
- if (namedChild)
- {
- return namedChild;
- }
- if (impl.mWildcardChild && impl.mWildcardChild->validate(name, context))
- {
- context[CONTEXT_REQUEST][CONTEXT_WILDCARD][impl.mWildcardKey] = name;
- return impl.mWildcardChild;
- }
- return NULL;
- }
- // virtual
- bool LLHTTPNode::handles(const LLSD& remainder, LLSD& context) const
- {
- return remainder.size() == 0;
- }
- // virtual
- bool LLHTTPNode::validate(const std::string& name, LLSD& context) const
- {
- return false;
- }
- const LLHTTPNode* LLHTTPNode::traverse(const std::string& path,
- LLSD& context) const
- {
- typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
- boost::char_separator<char> sep("/", "", boost::drop_empty_tokens);
- tokenizer tokens(path, sep);
- tokenizer::iterator iter = tokens.begin();
- tokenizer::iterator end = tokens.end();
- const LLHTTPNode* node = this;
- for ( ; iter != end; ++iter)
- {
- LLHTTPNode* child = node->getChild(*iter, context);
- if (!child)
- {
- LL_DEBUGS("HTTPNode") << "LLHTTPNode::traverse: Couldn't find '"
- << *iter << "'" << LL_ENDL;
- break;
- }
- LL_DEBUGS("HTTPNode") << "LLHTTPNode::traverse: Found '" << *iter
- << "'" << LL_ENDL;
- node = child;
- }
- LLSD& remainder = context[CONTEXT_REQUEST]["remainder"];
- for ( ; iter != end; ++iter)
- {
- remainder.append(*iter);
- }
- return node->handles(remainder, context) ? node : NULL;
- }
- void LLHTTPNode::addNode(const std::string& path, LLHTTPNode* nodeToAdd)
- {
- typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
- boost::char_separator<char> sep("/", "", boost::drop_empty_tokens);
- tokenizer tokens(path, sep);
- tokenizer::iterator iter = tokens.begin();
- tokenizer::iterator end = tokens.end();
- LLHTTPNode* node = this;
- for ( ; iter != end; ++iter)
- {
- LLHTTPNode* child = node->impl.findNamedChild(*iter);
- if (!child) break;
- node = child;
- }
- if (iter == end)
- {
- llwarns << "There is already a node handling " << path << llendl;
- return;
- }
- while (true)
- {
- std::string pathPart = *iter++;
- bool lastOne = iter == end;
- LLHTTPNode* nextNode = lastOne ? nodeToAdd : new LLHTTPNode();
- switch (pathPart[0])
- {
- case '<':
- {
- // *NOTE: This should really validate that it is of
- // the proper form: <wildcardkey> so that the substr()
- // generates the correct key name.
- node->impl.mWildcardChild = nextNode;
- node->impl.mWildcardName = pathPart;
- if (node->impl.mWildcardKey.empty())
- {
- node->impl.mWildcardKey = pathPart.substr(1,
- pathPart.size() - 2);
- }
- break;
- }
- case '*':
- {
- node->impl.mWildcardChild = nextNode;
- if (node->impl.mWildcardName.empty())
- {
- node->impl.mWildcardName = pathPart;
- }
- break;
- }
- default:
- {
- node->impl.mNamedChildren[pathPart] = nextNode;
- }
- }
- nextNode->impl.mParentNode = node;
- if (lastOne) break;
- node = nextNode;
- }
- }
- static void append_node_paths(LLSD& result, const std::string& name,
- const LLHTTPNode* node)
- {
- result.append(name);
- LLSD paths = node->allNodePaths();
- for (LLSD::array_const_iterator i = paths.beginArray(),
- end = paths.endArray();
- i != end; ++i)
- {
- result.append(name + "/" + i->asString());
- }
- }
- LLSD LLHTTPNode::allNodePaths() const
- {
- LLSD result;
- Impl::ChildMap::const_iterator i = impl.mNamedChildren.begin();
- Impl::ChildMap::const_iterator end = impl.mNamedChildren.end();
- for ( ; i != end; ++i)
- {
- append_node_paths(result, i->first, i->second);
- }
- if (impl.mWildcardChild)
- {
- append_node_paths(result, impl.mWildcardName, impl.mWildcardChild);
- }
- return result;
- }
- const LLHTTPNode* LLHTTPNode::rootNode() const
- {
- const LLHTTPNode* node = this;
- while (true)
- {
- const LLHTTPNode* next = node->impl.mParentNode;
- if (!next)
- {
- return node;
- }
- node = next;
- }
- }
- const LLHTTPNode* LLHTTPNode::findNode(const std::string& name) const
- {
- return impl.findNamedChild(name);
- }
- void LLHTTPNode::Response::statusUnknownError(S32 code)
- {
- status(code, "Unknown Error");
- }
- void LLHTTPNode::Response::notFound(const std::string& message)
- {
- status(HTTP_NOT_FOUND, message);
- }
- void LLHTTPNode::Response::notFound()
- {
- status(HTTP_NOT_FOUND, "Not Found");
- }
- void LLHTTPNode::Response::methodNotAllowed()
- {
- status(HTTP_METHOD_NOT_ALLOWED, "Method Not Allowed");
- }
- void LLHTTPNode::Response::addHeader(const std::string& name,
- const std::string& value)
- {
- mHeaders[name] = value;
- }
- void LLHTTPNode::describe(Description& desc) const
- {
- desc.shortInfo("unknown service (missing describe() method)");
- }
- const LLChainIOFactory* LLHTTPNode::getProtocolHandler() const
- {
- return NULL;
- }
- namespace
- {
- typedef std::map<std::string, LLHTTPRegistrar::NodeFactory*> FactoryMap;
- FactoryMap& factoryMap()
- {
- static FactoryMap theMap;
- return theMap;
- }
- }
- void LLHTTPRegistrar::registerFactory(const std::string& path,
- NodeFactory& factory)
- {
- factoryMap()[path] = &factory;
- }
- void LLHTTPRegistrar::buildAllServices(LLHTTPNode& root)
- {
- const FactoryMap& map = factoryMap();
- for (FactoryMap::const_iterator i = map.begin(), end = map.end();
- i != end; ++i)
- {
- LL_DEBUGS("AppInit") << "Adding node for path " << i->first << LL_ENDL;
- root.addNode(i->first, i->second->build());
- }
- }
- LLPointer<LLSimpleResponse> LLSimpleResponse::create()
- {
- return new LLSimpleResponse();
- }
- void LLSimpleResponse::result(const LLSD& result)
- {
- status(HTTP_OK, "OK");
- }
- void LLSimpleResponse::extendedResult(S32 code, const std::string& body,
- const LLSD& headers)
- {
- status(code, body);
- }
- void LLSimpleResponse::extendedResult(S32 code, const LLSD& r,
- const LLSD& headers)
- {
- status(code, "(LLSD)");
- }
- void LLSimpleResponse::status(S32 code, const std::string& message)
- {
- mCode = code;
- mMessage = message;
- }
- void LLSimpleResponse::print(std::ostream& out) const
- {
- out << mCode << " " << mMessage;
- }
- std::ostream& operator<<(std::ostream& out, const LLSimpleResponse& resp)
- {
- resp.print(out);
- return out;
- }
|