diff options
author | Adam <Adam@anope.org> | 2016-10-30 18:58:11 -0400 |
---|---|---|
committer | Adam <Adam@anope.org> | 2016-10-30 18:58:11 -0400 |
commit | be4083106b245053992e2b15c4c75aa74f5d39de (patch) | |
tree | f2257da53d82139a7f09a2211abb49ffa1a46f01 /modules/httpd.cpp | |
parent | c06cc40d27e5cb83424d89cb19571eb42392abe6 (diff) |
Add new RESTful JSON API and documentation
Diffstat (limited to 'modules/httpd.cpp')
-rw-r--r-- | modules/httpd.cpp | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/modules/httpd.cpp b/modules/httpd.cpp index 11f8318e7..41cd4a4ec 100644 --- a/modules/httpd.cpp +++ b/modules/httpd.cpp @@ -35,12 +35,16 @@ static Anope::string GetStatusFromCode(HTTPError err) { case HTTP_ERROR_OK: return "200 OK"; + case HTTP_CREATED: + return "201 Created"; case HTTP_FOUND: return "302 Found"; case HTTP_BAD_REQUEST: return "400 Bad Request"; case HTTP_PAGE_NOT_FOUND: return "404 Not Found"; + case HTTP_INTERNAL_SERVER_ERROR: + return "500 Internal Server Error"; case HTTP_NOT_SUPPORTED: return "505 HTTP Version Not Supported"; } @@ -59,13 +63,6 @@ class MyHTTPClient : public HTTPClient unsigned content_length; - enum - { - ACTION_NONE, - ACTION_GET, - ACTION_POST - } action; - void Serve() { if (this->served) @@ -105,7 +102,7 @@ class MyHTTPClient : public HTTPClient public: time_t created; - MyHTTPClient(HTTPProvider *l, int f, const sockaddrs &a) : Socket(f, l->IsIPv6()), HTTPClient(l, f, a), provider(l), header_done(false), served(false), ip(a.addr()), content_length(0), action(ACTION_NONE), created(Anope::CurTime) + MyHTTPClient(HTTPProvider *l, int f, const sockaddrs &a) : Socket(f, l->IsIPv6()), HTTPClient(l, f, a), provider(l), header_done(false), served(false), ip(a.addr()), content_length(0), created(Anope::CurTime) { Log(LOG_DEBUG, "httpd") << "Accepted connection " << f << " from " << a.addr(); } @@ -168,12 +165,13 @@ class MyHTTPClient : public HTTPClient { Log(LOG_DEBUG_2) << "HTTP from " << this->clientaddr.addr() << ": " << buf; - if (this->action == ACTION_NONE) + if (message.method == httpd::Method::NONE) { std::vector<Anope::string> params; spacesepstream(buf).GetTokens(params); - if (params.empty() || (params[0] != "GET" && params[0] != "POST")) + if (params.empty() || (params[0] != "GET" && params[0] != "POST" && params[0] != "PUT" + && params[0] != "DELETE" && params[0] != "OPTIONS" && params[0] != "HEAD")) { this->SendError(HTTP_BAD_REQUEST, "Unknown operation"); return true; @@ -186,9 +184,17 @@ class MyHTTPClient : public HTTPClient } if (params[0] == "GET") - this->action = ACTION_GET; + message.method = httpd::Method::GET; else if (params[0] == "POST") - this->action = ACTION_POST; + message.method = httpd::Method::POST; + else if (params[0] == "PUT") + message.method = httpd::Method::PUT; + else if (params[0] == "DELETE") + message.method = httpd::Method::DELETE; + else if (params[0] == "OPTIONS") + message.method = httpd::Method::OPTIONS; + else if (params[0] == "HEAD") + message.method = httpd::Method::HEAD; Anope::string targ = params[1]; size_t q = targ.find('?'); @@ -339,9 +345,19 @@ class MyHTTPProvider : public HTTPProvider, public Timer HTTPPage* FindPage(const Anope::string &pname) override { - if (this->pages.count(pname) == 0) - return NULL; - return this->pages[pname]; + Anope::string resource = pname; + + while (this->pages.count(resource) == 0) + { + size_t s = resource.find_last_of('/'); + + if (s == Anope::string::npos) + return nullptr; + + resource = resource.substr(0, s); + } + + return this->pages[resource]; } }; |