diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-06-23 00:37:32 +0100 | 
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-06-23 00:37:32 +0100 | 
| commit | 46f73485a46d24022db04da16f3d571e1ea54569 (patch) | |
| tree | 5eea1fc863b6e56f798b2793b17d863be65c8da5 | |
| parent | Very basic, probably buggy, incomplete [f]cgi implementation (diff) | |
| download | icespider-46f73485a46d24022db04da16f3d571e1ea54569.tar.bz2 icespider-46f73485a46d24022db04da16f3d571e1ea54569.tar.xz icespider-46f73485a46d24022db04da16f3d571e1ea54569.zip  | |
Add a status line to the output, including a 404 handler
| -rw-r--r-- | icespider/compile/routeCompiler.cpp | 3 | ||||
| -rw-r--r-- | icespider/core/core.cpp | 3 | ||||
| -rw-r--r-- | icespider/core/ihttpRequest.cpp | 8 | ||||
| -rw-r--r-- | icespider/core/ihttpRequest.h | 5 | ||||
| -rw-r--r-- | icespider/unittests/testApp.cpp | 15 | 
5 files changed, 29 insertions, 5 deletions
diff --git a/icespider/compile/routeCompiler.cpp b/icespider/compile/routeCompiler.cpp index 9addd56..4a8d9f8 100644 --- a/icespider/compile/routeCompiler.cpp +++ b/icespider/compile/routeCompiler.cpp @@ -246,6 +246,9 @@ namespace IceSpider {  					fprintbf(output, ")");  				}  				fprintbf(output, ";\n"); +				if (!o->returnsData()) { +					fprintbf(4, output, "request->response(200, \"OK\");"); +				}  				fprintbf(3, output, "}\n\n");  				fprintbf(2, output, "private:\n");  				for (const auto & p : r->params) { diff --git a/icespider/core/core.cpp b/icespider/core/core.cpp index ba236ac..fffbed9 100644 --- a/icespider/core/core.cpp +++ b/icespider/core/core.cpp @@ -36,6 +36,9 @@ namespace IceSpider {  		if (routeHandler) {  			routeHandler->execute(request);  		} +		else { +			request->response(404, "Not found"); +		}  	}  	const IRouteHandler * diff --git a/icespider/core/ihttpRequest.cpp b/icespider/core/ihttpRequest.cpp index 73367ca..a352dca 100644 --- a/icespider/core/ihttpRequest.cpp +++ b/icespider/core/ihttpRequest.cpp @@ -37,6 +37,14 @@ namespace IceSpider {  		return IceUtil::Optional<T>();  	} +	void IHttpRequest::response(short statusCode, const std::string & statusMsg) const +	{ +		getOutputStream() +			<< statusCode << " " << statusMsg << "\r\n" +			<< "\r\n"; +	} + +  #define getParams(T) \  	template<> IceUtil::Optional<T> IHttpRequest::getURLParam<T>(const std::string & key) const { \  		return optionalLexicalCast<T>(getURLParam(key)); } \ diff --git a/icespider/core/ihttpRequest.h b/icespider/core/ihttpRequest.h index da903ef..14dccda 100644 --- a/icespider/core/ihttpRequest.h +++ b/icespider/core/ihttpRequest.h @@ -39,10 +39,13 @@ namespace IceSpider {  			IceUtil::Optional<T> getQueryStringParam(const std::string & key) const;  			template<typename T>  			IceUtil::Optional<T> getHeaderParam(const std::string & key) const; +			void response(short, const std::string &) const;  			template<typename T>  			void response(const T & t) const  			{ -				Slicer::SerializeAnyWith<T>(t, getSerializer()); +				auto s = getSerializer(); +				response(200, "OK"); +				Slicer::SerializeAnyWith<T>(t, s);  			}  			const Core * core; diff --git a/icespider/unittests/testApp.cpp b/icespider/unittests/testApp.cpp index f79d331..1e9562b 100644 --- a/icespider/unittests/testApp.cpp +++ b/icespider/unittests/testApp.cpp @@ -159,28 +159,35 @@ BOOST_AUTO_TEST_CASE( testCallMethods )  	TestRequest requestGetIndex(this, HttpMethod::GET, "/");  	process(&requestGetIndex); -	BOOST_REQUIRE_EQUAL(requestGetIndex.output.str(), "{\"value\":\"index\"}"); +	BOOST_REQUIRE_EQUAL(requestGetIndex.output.str(), "200 OK\r\n\r\n{\"value\":\"index\"}");  	TestRequest requestGetItem(this, HttpMethod::GET, "/view/something/1234");  	requestGetItem.url["s"] = "something";  	requestGetItem.url["i"] = "1234";  	process(&requestGetItem); -	BOOST_REQUIRE_EQUAL(requestGetItem.output.str(), "{\"value\":\"withParams\"}"); +	BOOST_REQUIRE_EQUAL(requestGetItem.output.str(), "200 OK\r\n\r\n{\"value\":\"withParams\"}");  	TestRequest requestDeleteItem(this, HttpMethod::DELETE, "/some value");  	requestDeleteItem.url["s"] = "some value";  	process(&requestDeleteItem); -	BOOST_REQUIRE(requestDeleteItem.output.str().empty()); +	BOOST_REQUIRE_EQUAL(requestDeleteItem.output.str(), "200 OK\r\n\r\n");  	TestRequest requestUpdateItem(this, HttpMethod::POST, "/1234");  	requestUpdateItem.url["id"] = "1234";  	requestUpdateItem.hdr["Content-Type"] = "application/json";  	requestUpdateItem.input << "{\"value\": \"some value\"}";  	process(&requestUpdateItem); -	BOOST_REQUIRE(requestDeleteItem.output.str().empty()); +	BOOST_REQUIRE_EQUAL(requestDeleteItem.output.str(), "200 OK\r\n\r\n");  	adp->deactivate();  } +BOOST_AUTO_TEST_CASE( test404 ) +{ +	TestRequest requestGetIndex(this, HttpMethod::GET, "/404"); +	process(&requestGetIndex); +	BOOST_REQUIRE_EQUAL(requestGetIndex.output.str(), "404 Not found\r\n\r\n"); +} +  BOOST_AUTO_TEST_SUITE_END();  | 
