PageRenderTime 74ms CodeModel.GetById 63ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/llsdhttpserver.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 150 lines | 89 code | 27 blank | 34 comment | 6 complexity | 228c5312e783d344ad0637d7d526a923 MD5 | raw file
  1/** 
  2 * @file llsdhttpserver.cpp
  3 * @brief Standard LLSD services
  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
 27#include "linden_common.h"
 28#include "llsdhttpserver.h"
 29
 30#include "llhttpnode.h"
 31
 32/** 
 33 * This module implements common services that should be included
 34 * in all server URL trees.  These services facilitate debugging and
 35 * introsepction.
 36 */
 37
 38void LLHTTPStandardServices::useServices()
 39{
 40	/*
 41		Having this function body here, causes the classes and globals in this
 42		file to be linked into any program that uses the llmessage library.
 43	*/
 44}
 45
 46
 47
 48class LLHTTPHelloService : public LLHTTPNode
 49{
 50public:
 51	virtual void describe(Description& desc) const
 52	{
 53		desc.shortInfo("says hello");
 54		desc.getAPI();
 55		desc.output("\"hello\"");
 56		desc.source(__FILE__, __LINE__);
 57	}
 58    
 59	virtual LLSD simpleGet() const
 60	{
 61		LLSD result = "hello";
 62		return result;
 63	}
 64};
 65
 66LLHTTPRegistration<LLHTTPHelloService>
 67	gHTTPRegistrationWebHello("/web/hello");
 68
 69
 70
 71class LLHTTPEchoService : public LLHTTPNode
 72{
 73public:
 74	virtual void describe(Description& desc) const
 75	{
 76		desc.shortInfo("echo input");
 77		desc.postAPI();
 78		desc.input("<any>");
 79		desc.output("<the input>");
 80		desc.source(__FILE__, __LINE__);
 81	}
 82	
 83	virtual LLSD simplePost(const LLSD& params) const
 84	{
 85		return params;
 86	}
 87};
 88
 89LLHTTPRegistration<LLHTTPEchoService>
 90	gHTTPRegistrationWebEcho("/web/echo");
 91
 92
 93
 94class LLAPIService : public LLHTTPNode
 95{
 96public:
 97	virtual void describe(Description& desc) const
 98	{
 99		desc.shortInfo("information about the URLs this server supports");
100		desc.getAPI();
101		desc.output("a list of URLs supported");
102		desc.source(__FILE__, __LINE__);
103	}
104
105	virtual bool handles(const LLSD& remainder, LLSD& context) const
106	{
107		return followRemainder(remainder) != NULL;
108	}
109
110    virtual void get(ResponsePtr response, const LLSD& context) const
111	{
112		const LLSD& remainder = context["request"]["remainder"];
113		
114		if (remainder.size() > 0)
115		{
116			const LLHTTPNode* node = followRemainder(remainder);
117			if (!node)
118			{
119				response->notFound();
120				return;
121			}
122			
123			Description desc;
124			node->describe(desc);
125			response->result(desc.getInfo());
126			return;
127		}
128
129		response->result(rootNode()->allNodePaths());
130	}
131
132private:
133	const LLHTTPNode* followRemainder(const LLSD& remainder) const
134	{
135		const LLHTTPNode* node = rootNode();
136		
137		LLSD::array_const_iterator i = remainder.beginArray();
138		LLSD::array_const_iterator end = remainder.endArray();
139		for (; node  &&  i != end; ++i)
140		{
141			node = node->findNode(*i);
142		}
143		
144		return node;
145	}
146};
147
148LLHTTPRegistration<LLAPIService>
149	gHTTPRegistrationWebServerApi("/web/server/api");
150