From d72425de59db7d94dd51cca7988c3d025426925b Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 31 Jul 2015 06:40:09 +0000 Subject: Add utility methods to HTTPRequest and HTTPResponse classes. Allow getting method name of an HTTPRequest, and connection open/closed status of an HTTPResponse. --- lib/httpserver/HTTPRequest.cpp | 26 ++++++++++++++++++++++++++ lib/httpserver/HTTPRequest.h | 1 + lib/httpserver/HTTPResponse.h | 3 ++- 3 files changed, 29 insertions(+), 1 deletion(-) (limited to 'lib/httpserver') diff --git a/lib/httpserver/HTTPRequest.cpp b/lib/httpserver/HTTPRequest.cpp index 4c5dc149..106bc30a 100644 --- a/lib/httpserver/HTTPRequest.cpp +++ b/lib/httpserver/HTTPRequest.cpp @@ -94,6 +94,32 @@ HTTPRequest::~HTTPRequest() } } +// -------------------------------------------------------------------------- +// +// Function +// Name: HTTPRequest::GetMethodName() +// Purpose: Returns the name of the request's HTTP method verb +// as a string. +// Created: 28/7/15 +// +// -------------------------------------------------------------------------- + +std::string HTTPRequest::GetMethodName() const +{ + switch(mMethod) + { + case Method_UNINITIALISED: return "uninitialised"; + case Method_UNKNOWN: return "unknown"; + case Method_GET: return "GET"; + case Method_HEAD: return "HEAD"; + case Method_POST: return "POST"; + case Method_PUT: return "PUT"; + default: + std::ostringstream oss; + oss << "unknown-" << mMethod; + return oss.str(); + }; +} // -------------------------------------------------------------------------- // diff --git a/lib/httpserver/HTTPRequest.h b/lib/httpserver/HTTPRequest.h index 25effb70..80699e3b 100644 --- a/lib/httpserver/HTTPRequest.h +++ b/lib/httpserver/HTTPRequest.h @@ -77,6 +77,7 @@ public: // // -------------------------------------------------------------------------- enum Method GetMethod() const {return mMethod;} + std::string GetMethodName() const; const std::string &GetRequestURI() const {return mRequestURI;} // Note: the HTTPRequest generates and parses the Host: header diff --git a/lib/httpserver/HTTPResponse.h b/lib/httpserver/HTTPResponse.h index 04051958..978fa91e 100644 --- a/lib/httpserver/HTTPResponse.h +++ b/lib/httpserver/HTTPResponse.h @@ -63,7 +63,7 @@ public: typedef std::pair Header; void SetResponseCode(int Code); - int GetResponseCode() { return mResponseCode; } + int GetResponseCode() const { return mResponseCode; } void SetContentType(const char *ContentType); const std::string& GetContentType() { return mContentType; } @@ -107,6 +107,7 @@ public: void SetResponseIsDynamicContent(bool IsDynamic) {mResponseIsDynamicContent = IsDynamic;} // Set keep alive control, default is to mark as to be closed void SetKeepAlive(bool KeepAlive) {mKeepAlive = KeepAlive;} + bool IsKeepAlive() {return mKeepAlive;} void SetCookie(const char *Name, const char *Value, const char *Path = "/", int ExpiresAt = 0); -- cgit v1.2.3