diff options
| -rw-r--r-- | icespider/common/formatters.h | 1 | ||||
| -rw-r--r-- | icespider/common/http.ice | 18 | ||||
| -rw-r--r-- | icespider/core/core.cpp | 2 | ||||
| -rw-r--r-- | icespider/core/ihttpRequest.cpp | 15 | ||||
| -rw-r--r-- | icespider/core/irouteHandler.cpp | 9 | ||||
| -rw-r--r-- | icespider/core/xwwwFormUrlEncoded.cpp | 15 | 
6 files changed, 45 insertions, 15 deletions
| diff --git a/icespider/common/formatters.h b/icespider/common/formatters.h index 1ec6a6b..e42717e 100644 --- a/icespider/common/formatters.h +++ b/icespider/common/formatters.h @@ -6,6 +6,7 @@  namespace IceSpider {  	AdHocFormatter(StatusFmt, "Status: %? %?\r\n\r\n");  	AdHocFormatter(HdrFmt, "%?: %?\r\n"); +	AdHocFormatter(MimeTypeFmt, "%?/%?");  }  #endif diff --git a/icespider/common/http.ice b/icespider/common/http.ice index a44a492..72e1de7 100644 --- a/icespider/common/http.ice +++ b/icespider/common/http.ice @@ -2,6 +2,7 @@  #define ICESPIDER_HTTP_ICE  [["ice-prefix"]] +[["underscore"]]  module IceSpider {  	["slicer:ignore"]  	local exception HttpException { @@ -32,6 +33,23 @@ module IceSpider {  	["slicer:json:object"]  	local dictionary<string, string> StringMap; + +	module S { // Statuses +		const string OK = "OK"; +		const string MOVED = "Moved"; +	}; +	module H { // Header names +		const string ACCEPT = "Accept"; +		const string LOCATION = "Location"; +		const string SET_COOKIE = "Set-Cookie"; +		const string CONTENT_TYPE = "Content-Type"; +	}; +	module MIME { // Common MIME types +		const string TEXT_PLAIN = "text/plain"; +	}; +	module E { // Common environment vars +		const string CONTENT_TYPE = "CONTENT_TYPE"; +	};  };  #endif diff --git a/icespider/core/core.cpp b/icespider/core/core.cpp index bbb4b05..c5b5a92 100644 --- a/icespider/core/core.cpp +++ b/icespider/core/core.cpp @@ -107,7 +107,7 @@ namespace IceSpider {  	Core::defaultErrorReport(IHttpRequest * request, const std::exception & exception) const  	{  		char * buf = __cxxabiv1::__cxa_demangle(typeid(exception).name(), NULL, NULL, NULL); -		request->setHeader("Content-Type", "text/plain"); +		request->setHeader(H::CONTENT_TYPE, MIME::TEXT_PLAIN);  		request->response(500, buf);  		LogExp::write(request->getOutputStream(), buf, exception.what());  		request->dump(std::cerr); diff --git a/icespider/core/ihttpRequest.cpp b/icespider/core/ihttpRequest.cpp index 9325a5e..6afa124 100644 --- a/icespider/core/ihttpRequest.cpp +++ b/icespider/core/ihttpRequest.cpp @@ -5,6 +5,7 @@  #include "xwwwFormUrlEncoded.h"  #include <boost/lexical_cast.hpp>  #include <time.h> +#include <formatters.h>  namespace IceSpider {  	IHttpRequest::IHttpRequest(const Core * c) : @@ -23,7 +24,7 @@ namespace IceSpider {  	{  		try {  			return Slicer::StreamDeserializerFactory::createNew( -				getEnv("CONTENT_TYPE") / []() -> std::string { +				getEnv(E::CONTENT_TYPE) / []() -> std::string {  					throw Http400_BadRequest();  				}, getInputStream());  		} @@ -35,7 +36,7 @@ namespace IceSpider {  	ContentTypeSerializer  	IHttpRequest::getSerializer(const IRouteHandler * handler) const  	{ -		auto acceptHdr = getHeaderParam("Accept"); +		auto acceptHdr = getHeaderParam(H::ACCEPT);  		if (acceptHdr) {  			auto accept = acceptHdr->c_str();  			std::vector<AcceptPtr> accepts; @@ -115,7 +116,7 @@ namespace IceSpider {  		if (d) o << "; domain=" << *d;  		if (p) o << "; path=" << *p;  		if (s) o << "; secure"; -		setHeader("Set-Cookie", o.str()); +		setHeader(H::SET_COOKIE, o.str());  	}  	template <typename T> @@ -127,16 +128,16 @@ namespace IceSpider {  	void IHttpRequest::responseRedirect(const std::string & url, const Ice::optional<std::string> & statusMsg) const  	{ -		setHeader("Location", url); -		response(303, (statusMsg ? *statusMsg : "Moved")); +		setHeader(H::LOCATION, url); +		response(303, (statusMsg ? *statusMsg : S::MOVED));  	}  	void  	IHttpRequest::modelPartResponse(const IRouteHandler * route, const Slicer::ModelPartForRootPtr & mp) const  	{  		auto s = getSerializer(route); -		setHeader("Content-Type", s.first.group + "/" + s.first.type); -		response(200, "OK"); +		setHeader(H::CONTENT_TYPE, MimeTypeFmt::get(s.first.group, s.first.type)); +		response(200, S::OK);  		s.second->Serialize(mp);  	} diff --git a/icespider/core/irouteHandler.cpp b/icespider/core/irouteHandler.cpp index d4ad722..7a44fb3 100644 --- a/icespider/core/irouteHandler.cpp +++ b/icespider/core/irouteHandler.cpp @@ -1,10 +1,15 @@  #include "irouteHandler.h"  #include "core.h"  #include <factory.impl.h> +#include <formatters.h>  INSTANTIATEFACTORY(IceSpider::IRouteHandler, const IceSpider::Core *);  namespace IceSpider { +	static const std::string APPLICATION = "application"; +	static const std::string JSON = "json"; +	static const std::string APPLICATION_JSON = MimeTypeFmt::get(APPLICATION, JSON); +  	const RouteOptions IRouteHandler::defaultRouteOptions {  	};  	IRouteHandler::IRouteHandler(HttpMethod m, const std::string & p) : @@ -44,8 +49,8 @@ namespace IceSpider {  	IRouteHandler::defaultSerializer(std::ostream & strm) const  	{  		return { -			{ "application", "json" }, -			Slicer::StreamSerializerFactory::createNew("application/json", strm) +			{ APPLICATION, JSON }, +			Slicer::StreamSerializerFactory::createNew(APPLICATION_JSON, strm)  		};  	} diff --git a/icespider/core/xwwwFormUrlEncoded.cpp b/icespider/core/xwwwFormUrlEncoded.cpp index 655c839..ad68716 100644 --- a/icespider/core/xwwwFormUrlEncoded.cpp +++ b/icespider/core/xwwwFormUrlEncoded.cpp @@ -9,6 +9,11 @@ using namespace std::literals;  extern long hextable[];  namespace IceSpider { +	static const std::string AMP = "&"; +	static const std::string TRUE = "true"; +	static const std::string FALSE = "false"; +	static const std::string KEY = "key"; +	static const std::string VALUE = "value";  	XWwwFormUrlEncoded::XWwwFormUrlEncoded(std::istream & in) :  		input(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()) @@ -46,8 +51,8 @@ namespace IceSpider {  			void set(bool & t) const override  			{ -				if (s == "true") t = true; -				else if (s == "false") t = false; +				if (s == TRUE) t = true; +				else if (s == FALSE) t = false;  				else throw Http400_BadRequest();  			} @@ -150,7 +155,7 @@ namespace IceSpider {  	void  	XWwwFormUrlEncoded::iterateVars(const KVh & h)  	{ -		iterateVars(input, h, "&"sv); +		iterateVars(input, h, AMP);  	}  	void @@ -178,8 +183,8 @@ namespace IceSpider {  	{  		iterateVars([mp](auto k, auto v) {  			auto p = mp->GetAnonChild(); -			p->GetChild("key")->SetValue(SetFromString(k)); -			p->GetChild("value")->SetValue(SetFromString(v)); +			p->GetChild(KEY)->SetValue(SetFromString(k)); +			p->GetChild(VALUE)->SetValue(SetFromString(v));  			p->Complete();  		});  	} | 
