From 519cee1e81135b3b7db83aaf3928a40b063edbde Mon Sep 17 00:00:00 2001
From: "Nicolas \"Pixel\" Noble" <pixel@nobis-crew.org>
Date: Tue, 24 Dec 2013 20:41:40 +0100
Subject: Cleaning up furthermore http string maps.

---
 src/Http.cc | 114 ++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 61 insertions(+), 53 deletions(-)

(limited to 'src')

diff --git a/src/Http.cc b/src/Http.cc
index 4ae240e..10c3430 100644
--- a/src/Http.cc
+++ b/src/Http.cc
@@ -2,65 +2,73 @@
 
 #include "Http.h"
 
+template<typename I>
+const char * getMap(const std::map<I, const char *> & map, I idx, const char * defStr) {
+    auto t = map.find(idx);
+    if (t == map.end())
+        return defStr;
+    return t->second;
+}
+
+
+static const std::map<int, const char *> s_statusMap {
+    std::make_pair(100, "Continue"),
+    std::make_pair(101, "Switching Protocols"),
+    std::make_pair(200, "OK"),
+    std::make_pair(201, "Created"),
+    std::make_pair(202, "Accepted"),
+    std::make_pair(203, "Non-Authoritative Information"),
+    std::make_pair(204, "No Content"),
+    std::make_pair(205, "Reset Content"),
+    std::make_pair(206, "Partial Content"),
+    std::make_pair(300, "Multiple Choices"),
+    std::make_pair(301, "Moved Permanently"),
+    std::make_pair(302, "Found"),
+    std::make_pair(303, "See Other"),
+    std::make_pair(304, "Not Modified"),
+    std::make_pair(305, "Use Proxy"),
+    std::make_pair(307, "Temporary Redirect"),
+    std::make_pair(400, "Bad Request"),
+    std::make_pair(401, "Unauthorized"),
+    std::make_pair(402, "Payment Required"),
+    std::make_pair(403, "Forbidden"),
+    std::make_pair(404, "Not Found"),
+    std::make_pair(405, "Method Not Allowed"),
+    std::make_pair(406, "Not Acceptable"),
+    std::make_pair(407, "Proxy Authentication Required"),
+    std::make_pair(408, "Request Timeout"),
+    std::make_pair(409, "Conflict"),
+    std::make_pair(410, "Gone"),
+    std::make_pair(411, "Length Required"),
+    std::make_pair(412, "Precondition Failed"),
+    std::make_pair(413, "Request Entity Too Large"),
+    std::make_pair(414, "Request-URI Too Long"),
+    std::make_pair(415, "Unsupported Media Type"),
+    std::make_pair(416, "Requested Range Not Satisfiable"),
+    std::make_pair(417, "Expectation Failed"),
+    std::make_pair(418, "I'm a teapot"),
+    std::make_pair(500, "Internal Error"),
+    std::make_pair(501, "Not Implemented"),
+    std::make_pair(502, "Bad Gateway"),
+    std::make_pair(503, "Service Unavailable"),
+    std::make_pair(504, "Gateway Timeout"),
+    std::make_pair(505, "HTTP Version Not Supported"),
+};
+
 const char * Balau::Http::getStatusMsg(int httpStatus) {
-    switch (httpStatus) {
-        case 100: return "Continue";
-        case 101: return "Switching Protocols";
-        case 200: return "OK";
-        case 201: return "Created";
-        case 202: return "Accepted";
-        case 203: return "Non-Authoritative Information";
-        case 204: return "No Content";
-        case 205: return "Reset Content";
-        case 206: return "Partial Content";
-        case 300: return "Multiple Choices";
-        case 301: return "Moved Permanently";
-        case 302: return "Found";
-        case 303: return "See Other";
-        case 304: return "Not Modified";
-        case 305: return "Use Proxy";
-        case 307: return "Temporary Redirect";
-        case 400: return "Bad Request";
-        case 401: return "Unauthorized";
-        case 402: return "Payment Required";
-        case 403: return "Forbidden";
-        case 404: return "Not Found";
-        case 405: return "Method Not Allowed";
-        case 406: return "Not Acceptable";
-        case 407: return "Proxy Authentication Required";
-        case 408: return "Request Timeout";
-        case 409: return "Conflict";
-        case 410: return "Gone";
-        case 411: return "Length Required";
-        case 412: return "Precondition Failed";
-        case 413: return "Request Entity Too Large";
-        case 414: return "Request-URI Too Long";
-        case 415: return "Unsupported Media Type";
-        case 416: return "Requested Range Not Satisfiable";
-        case 417: return "Expectation Failed";
-        case 418: return "I'm a teapot";
-        case 500: return "Internal Error";
-        case 501: return "Not Implemented";
-        case 502: return "Bad Gateway";
-        case 503: return "Service Unavailable";
-        case 504: return "Gateway Timeout";
-        case 505: return "HTTP Version Not Supported";
-    }
-    return "Unknown HTTP code";
+    return getMap(s_statusMap, httpStatus, "Unknown HTTP code");
 }
 
-static std::map<Balau::String, const char *> s_mimeMap {
-    std::make_pair("css", "text/css"),
+
+static const std::map<const Balau::String, const char *> s_mimeMap {
+    std::make_pair("css",  "text/css"),
     std::make_pair("html", "text/html"),
-    std::make_pair("js", "application/javascript"),
+    std::make_pair("js",   "application/javascript"),
     std::make_pair("json", "application/json"),
-    std::make_pair("png", "image/png"),
-    std::make_pair("gif", "image/gif"),
+    std::make_pair("png",  "image/png"),
+    std::make_pair("gif",  "image/gif"),
 };
 
 const char * Balau::Http::getContentType(const String & extension) {
-    auto t = s_mimeMap.find(extension);
-    if (t == s_mimeMap.end())
-        return "application/octet-stream";
-    return t->second;
+    return getMap<const Balau::String>(s_mimeMap, extension, "application/octet-stream");
 }
-- 
cgit v1.2.3