3
0

BaseHttpServer.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #pragma once
  9. #include <map>
  10. #include <string>
  11. #include <vector>
  12. namespace Metastream
  13. {
  14. class DataCache;
  15. // Currently supported HTTP methods
  16. enum class HttpMethod
  17. {
  18. GET
  19. };
  20. typedef struct
  21. {
  22. HttpMethod method;
  23. std::map<std::string, std::string> headers;
  24. std::string uri;
  25. std::map<std::string, std::string> query;
  26. std::string body;
  27. } HttpRequest;
  28. typedef struct
  29. {
  30. int code;
  31. std::map<std::string, std::string> headers;
  32. std::string body;
  33. } HttpResponse;
  34. class BaseHttpServer
  35. {
  36. public:
  37. BaseHttpServer(const DataCache* cache)
  38. : m_cache(cache)
  39. {
  40. }
  41. virtual ~BaseHttpServer() {}
  42. // Start the HTTP server with the given options
  43. virtual bool Start(const std::string & civetOptions) = 0;
  44. // Stop the HTTP Server
  45. virtual void Stop() = 0;
  46. // Return a JSON list of all data tables that are exposed.
  47. HttpResponse GetDataTables() const;
  48. // Return a JSON list of all data keys that are exposed for a specific table.
  49. HttpResponse GetDataKeys(const std::string& tableName) const;
  50. // Return a JSON object containing a particular value.
  51. HttpResponse GetDataValue(const std::string& tableName, const std::string& key) const;
  52. // Return a JSON object containing a set of values.
  53. HttpResponse GetDataValues(const std::string& tableName, const std::vector<std::string>& keys) const;
  54. //---------------------------------------------------------------------
  55. // Helper functions
  56. // Split query into key-value pairs
  57. static std::map<std::string, std::string> TokenizeQuery(const char* queryString);
  58. // simple string substitution
  59. static std::string StrReplace(std::string srcString, const std::string & query, const std::string & replacement);
  60. // Split a query value if it is a comma separated list (e.g. key=value1,value2,...)
  61. static std::vector<std::string> SplitValueList(const std::string& value, char separator);
  62. // Serialize header name-value pairs into a string for writing back to response
  63. static std::string SerializeHeaders(const std::map<std::string, std::string>& headers);
  64. // Prepare HTTP status for writing back to response (e.g. "HTTP/1.1 200 OK")
  65. static std::string HttpStatus(int code);
  66. private:
  67. const DataCache* m_cache;
  68. };
  69. } // namespace Metastream