Esempio n. 1
0
xml::element* make_fault(const string& what)
{
	xml::element* fault(new xml::element("env:Fault"));
	
	xml::element* faultCode(new xml::element("faultcode"));
	faultCode->content("env:Server");
	fault->append(faultCode);
	
	xml::element* faultString(new xml::element("faultstring"));
	faultString->content(what);
	fault->append(faultString);

	return make_envelope(fault);
}
void server::handle_request(const http::request& req, http::reply& rep)
{
	string action;
	
	try
	{
		xml::element* response;
		
		if (req.method == "POST")	// must be a SOAP call
		{
			xml::document doc;
			doc.read(req.payload);
			envelope env(doc);
			xml::element* request = env.request();
			
			action = request->name();
			log() << action << ' ';
			response = make_envelope(dispatch(action, env.request()));
		}
		else if (req.method == "GET")
		{
			// start by sanitizing the request's URI
			string uri = req.uri;
	
			// strip off the http part including hostname and such
			if (ba::starts_with(uri, "http://"))
			{
				string::size_type s = uri.find_first_of('/', 7);
				if (s != string::npos)
					uri.erase(0, s);
			}
			
			// now make the path relative to the root
			while (uri.length() > 0 and uri[0] == '/')
				uri.erase(uri.begin());
	
			fs::path path(uri);
			fs::path::iterator p = path.begin();
			
			if (p == path.end())
				throw http::bad_request;
			
			string root = (*p++).string();
			
			if (root == "rest")
			{
				action = (*p++).string();
				
				xml::element* request(new xml::element(action));
				while (p != path.end())
				{
					string name = http::decode_url((*p++).string());
					if (p == path.end())
						break;
					xml::element* param(new xml::element(name));
					string value = http::decode_url((*p++).string());
					param->content(value);
					request->append(param);
				}
				
				log() << action << ' ';
				response = make_envelope(dispatch(action, request));
			}
			else if (root == "wsdl")
			{
				log() << "wsdl";
				response = make_wsdl(m_location);
			}
			else
			{
				log() << req.uri;
				throw http::not_found;
			}
		}
		else
			throw http::bad_request;
		
		rep.set_content(response);
	}
	catch (std::exception& e)
	{
		rep.set_content(make_fault(e));
	}
	catch (http::status_type& s)
	{
		rep = http::reply::stock_reply(s);
	}
}