Browse Source

cleanup: somewhat similar style in all files

master
Jørgen Lien Sellæg 9 years ago
parent
commit
f26df2639e
  1. 8
      toREST/include/http.hpp
  2. 5
      toREST/include/session.hpp
  3. 10
      toREST/include/util.hpp
  4. 81
      toREST/src/http.cpp
  5. 1
      toREST/tests/include/ConfigContext.hpp
  6. 3
      toREST/tests/include/ServerContext.hpp
  7. 2
      toREST/tests/include/TorrentContext.hpp

8
toREST/include/http.hpp

@ -1,5 +1,5 @@
#ifndef _TOREST_HTTP_HPP_ #ifndef _TR_HTTP_HPP_
#define _TOREST_HTTP_HPP_ #define _TR_HTTP_HPP_
#include <json.hpp> #include <json.hpp>
#include <unordered_map> #include <unordered_map>
@ -32,6 +32,7 @@ namespace http {
class code { class code {
public: public:
code(status status); code(status status);
public: public:
status first; status first;
std::string second; std::string second;
@ -46,6 +47,7 @@ namespace http {
void add_header(const http::header &header) { response_headers.emplace(header); } void add_header(const http::header &header) { response_headers.emplace(header); }
void set_status(http::status code) { status_code = code; } void set_status(http::status code) { status_code = code; }
std::ostream &do_response(std::ostream &os) const; std::ostream &do_response(std::ostream &os) const;
protected: protected:
std::string body; std::string body;
headers response_headers; headers response_headers;
@ -53,4 +55,4 @@ namespace http {
}; };
}; };
#endif // _TOREST_HTTP_HPP_ #endif

5
toREST/include/session.hpp

@ -1,3 +1,6 @@
#ifndef _TR_SESSION_HPP_
#define _TR_SESSION_HPP_
#include <http.hpp> #include <http.hpp>
#include <util.hpp> #include <util.hpp>
@ -68,3 +71,5 @@ namespace tr{
} }
} }
} }
#endif

10
toREST/include/util.hpp

@ -1,9 +1,9 @@
#ifndef _BT_HELPERS_HPP_ #ifndef _TR_UTIL_HPP_
#define _BT_HELPERS_HPP_ #define _TR_UTIL_HPP_
#include <unordered_map>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <json.hpp> #include <json.hpp>
#include <unordered_map>
namespace util { namespace util {
class uri { class uri {
@ -48,7 +48,6 @@ namespace util {
try { try {
return nlohmann::json::parse(istream); return nlohmann::json::parse(istream);
} catch (const std::invalid_argument &exp) { } catch (const std::invalid_argument &exp) {
} }
return nlohmann::json(nullptr); return nlohmann::json(nullptr);
} }
@ -57,7 +56,6 @@ namespace util {
try { try {
return nlohmann::json::parse(string); return nlohmann::json::parse(string);
} catch (const std::invalid_argument &exp) { } catch (const std::invalid_argument &exp) {
} }
return nlohmann::json(nullptr); return nlohmann::json(nullptr);
} }
@ -81,4 +79,4 @@ namespace util {
}; };
} }
#endif // _BT_HELPERS_HPP_ #endif

81
toREST/src/http.cpp

@ -1,28 +1,5 @@
#include <http.hpp> #include <http.hpp>
http::code::code(status status):first(status){
switch(status) {
case accepted: second="Accepted"; break;
case bad_gateway: second="Bad Gateway"; break;
case bad_request: second="Bad Request"; break;
case created: second="Created"; break;
case forbidden: second="Forbidden"; break;
case gateway_timeout: second="Gateway Timeout"; break;
case http_version_not_supported: second="HTTP Version Not Supported"; break;
case internal_server_error: second="Internal Server Error"; break;
case method_not_allowed: second="Method Not Allowed"; break;
case not_acceptable: second="Not Acceptable"; break;
case no_content: second="No Content"; break;
case not_found: second="Not Found"; break;
case not_implemented: second="Not Implemented"; break;
case ok: second="OK"; break;
case payment_required: second="Payment Required"; break;
case service_unavailable: second="Service Unavailable"; break;
case unauthorized: second="Unauthorized"; break;
default: second="UNKNOWN";
}
}
std::ostream &http::response::do_response(std::ostream &os) const { std::ostream &http::response::do_response(std::ostream &os) const {
os << "HTTP/1.1" os << "HTTP/1.1"
<< " " << status_code.first << " " << status_code.second << "\r\n"; << " " << status_code.first << " " << status_code.second << "\r\n";
@ -46,3 +23,61 @@ void http::response::set_body(const nlohmann::json &json){
add_header({"Content-Type", "application/json"}); add_header({"Content-Type", "application/json"});
response::set_body(json.dump()); response::set_body(json.dump());
} }
http::code::code(status status) : first(status) {
switch (status) {
case accepted:
second = "Accepted";
break;
case bad_gateway:
second = "Bad Gateway";
break;
case bad_request:
second = "Bad Request";
break;
case created:
second = "Created";
break;
case forbidden:
second = "Forbidden";
break;
case gateway_timeout:
second = "Gateway Timeout";
break;
case http_version_not_supported:
second = "HTTP Version Not Supported";
break;
case internal_server_error:
second = "Internal Server Error";
break;
case method_not_allowed:
second = "Method Not Allowed";
break;
case not_acceptable:
second = "Not Acceptable";
break;
case no_content:
second = "No Content";
break;
case not_found:
second = "Not Found";
break;
case not_implemented:
second = "Not Implemented";
break;
case ok:
second = "OK";
break;
case payment_required:
second = "Payment Required";
break;
case service_unavailable:
second = "Service Unavailable";
break;
case unauthorized:
second = "Unauthorized";
break;
default:
second = "UNKNOWN";
}
}

1
toREST/tests/include/ConfigContext.hpp

@ -3,5 +3,4 @@
#include <config.hpp> #include <config.hpp>
#endif #endif

3
toREST/tests/include/ServerContext.hpp

@ -1,9 +1,10 @@
#ifndef _TR_TEST_SERVER_CONTEXT_HPP_ #ifndef _TR_TEST_SERVER_CONTEXT_HPP_
#define _TR_TEST_SERVER_CONTEXT_HPP_ #define _TR_TEST_SERVER_CONTEXT_HPP_
#include <Catch/fakeit.hpp> #include <Catch/fakeit.hpp>
#include <json.hpp> #include <json.hpp>
#include <unordered_map>
#include <sstream> #include <sstream>
#include <unordered_map>
class TestRequest { class TestRequest {
public: public:

2
toREST/tests/include/TorrentContext.hpp

@ -1,8 +1,8 @@
#ifndef _TR_TEST_TORRENT_CONTEXT_HPP_ #ifndef _TR_TEST_TORRENT_CONTEXT_HPP_
#define _TR_TEST_TORRENT_CONTEXT_HPP_ #define _TR_TEST_TORRENT_CONTEXT_HPP_
#include <string>
#include <libtorrent/magnet_uri.hpp> #include <libtorrent/magnet_uri.hpp>
#include <string>
class TorrentStatus { class TorrentStatus {
public: public:

Loading…
Cancel
Save