BaseHttpServer.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "BaseHttpServer.h"
  9. #include "DataCache.h"
  10. #include <sstream>
  11. using namespace Metastream;
  12. HttpResponse BaseHttpServer::GetDataTables() const
  13. {
  14. HttpResponse response;
  15. response.code = 200;
  16. response.body = m_cache->GetDatabasesJSON().c_str();
  17. return response;
  18. }
  19. HttpResponse BaseHttpServer::GetDataKeys(const std::string& tableName) const
  20. {
  21. int code = 404;
  22. std::string body (m_cache->GetTableKeysJSON(tableName) );
  23. if (!body.empty())
  24. {
  25. code = 200;
  26. }
  27. HttpResponse response;
  28. response.code = code;
  29. response.body = body.c_str();
  30. return response;
  31. }
  32. HttpResponse BaseHttpServer::GetDataValue(const std::string& tableName, const std::string& key) const
  33. {
  34. std::vector<std::string> keys;
  35. keys.push_back(key);
  36. return GetDataValues(tableName, keys);
  37. }
  38. Metastream::HttpResponse Metastream::BaseHttpServer::GetDataValues(const std::string& tableName, const std::vector<std::string>& keys) const
  39. {
  40. int code = 404;
  41. std::string body(m_cache->GetTableKeyValuesJSON(tableName, keys));
  42. if (!body.empty())
  43. {
  44. code = 200;
  45. }
  46. HttpResponse response;
  47. response.code = code;
  48. response.body = body.c_str();
  49. return response;
  50. }
  51. std::map<std::string, std::string> BaseHttpServer::TokenizeQuery(const char* queryString)
  52. {
  53. std::map<std::string, std::string> queryMap;
  54. std::stringstream query(queryString);
  55. std::string pair;
  56. while (std::getline(query, pair, '&'))
  57. {
  58. auto pos = pair.find("=");
  59. if (pos != std::string::npos)
  60. {
  61. queryMap[pair.substr(0, pos)] = pair.substr(pos + 1, std::string::npos);
  62. }
  63. }
  64. return queryMap;
  65. }
  66. std::string BaseHttpServer::StrReplace(std::string srcString, const std::string & query, const std::string & replacement)
  67. {
  68. if (!query.empty())
  69. {
  70. size_t pos = 0;
  71. while ((pos = srcString.find(query, pos)) != std::string::npos)
  72. {
  73. srcString.replace(pos, query.length(), replacement);
  74. pos += replacement.length();
  75. }
  76. }
  77. return srcString;
  78. }
  79. std::vector<std::string> Metastream::BaseHttpServer::SplitValueList(const std::string& value, char separator)
  80. {
  81. std::vector<std::string> ret;
  82. size_t start = 0;
  83. size_t split;
  84. do
  85. {
  86. split = value.find(separator, start);
  87. ret.push_back(std::string(value, start, split-start));
  88. start = split + 1;
  89. } while (split != std::string::npos);
  90. return ret;
  91. }
  92. std::string BaseHttpServer::SerializeHeaders(const std::map<std::string, std::string>& headers)
  93. {
  94. std::stringstream serializedHeaders;
  95. for (auto it = headers.begin(); it != headers.end(); it++)
  96. {
  97. serializedHeaders << it->first << ": " << it->second << "\r\n";
  98. }
  99. serializedHeaders << "\r\n";
  100. return std::string(serializedHeaders.str());
  101. }
  102. std::string BaseHttpServer::HttpStatus(int code)
  103. {
  104. const char* description;
  105. switch (code)
  106. {
  107. case 100: description = "Continue"; break;
  108. case 101: description = "Switching Protocols"; break;
  109. case 200: description = "OK"; break;
  110. case 201: description = "Created"; break;
  111. case 202: description = "Accepted"; break;
  112. case 203: description = "Non-Authoritative Information"; break;
  113. case 204: description = "No Content"; break;
  114. case 205: description = "Reset Content"; break;
  115. case 206: description = "Partial Content"; break;
  116. case 300: description = "Multiple Choices"; break;
  117. case 301: description = "Moved Permanently"; break;
  118. case 302: description = "Moved Temporarily"; break;
  119. case 303: description = "See Other"; break;
  120. case 304: description = "Not Modified"; break;
  121. case 305: description = "Use Proxy"; break;
  122. case 400: description = "Bad Request"; break;
  123. case 401: description = "Unauthorized"; break;
  124. case 402: description = "Payment Required"; break;
  125. case 403: description = "Forbidden"; break;
  126. case 404: description = "Not Found"; break;
  127. case 405: description = "Method Not Allowed"; break;
  128. case 406: description = "Not Acceptable"; break;
  129. case 407: description = "Proxy Authentication Required"; break;
  130. case 408: description = "Request Time-out"; break;
  131. case 409: description = "Conflict"; break;
  132. case 410: description = "Gone"; break;
  133. case 411: description = "Length Required"; break;
  134. case 412: description = "Precondition Failed"; break;
  135. case 413: description = "Request Entity Too Large"; break;
  136. case 414: description = "Request-URI Too Large"; break;
  137. case 415: description = "Unsupported Media Type"; break;
  138. case 500: description = "Internal Server Error"; break;
  139. case 501: description = "Not Implemented"; break;
  140. case 502: description = "Bad Gateway"; break;
  141. case 503: description = "Service Unavailable"; break;
  142. case 504: description = "Gateway Time-out"; break;
  143. case 505: description = "HTTP Version not supported"; break;
  144. default:
  145. description = "";
  146. }
  147. std::stringstream httpStatus;
  148. httpStatus << "HTTP/1.1 " << code << " " << description << "\r\n";
  149. return std::string(httpStatus.str());
  150. }