message.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. //httpMessage: base class for httpRequest and httpResponse
  3. //provides shared functionality
  4. namespace nall::HTTP {
  5. struct Variable {
  6. string name;
  7. string value;
  8. };
  9. struct SharedVariable {
  10. SharedVariable(const nall::string& name = "", const nall::string& value = "") : shared(new Variable{name, value}) {}
  11. explicit operator bool() const { return (bool)shared->name; }
  12. auto operator()() const { return shared->value; }
  13. auto& operator=(const nall::string& value) { shared->value = value; return *this; }
  14. auto name() const { return shared->name; }
  15. auto value() const { return shared->value; }
  16. auto string() const { return nall::string{shared->value}.strip().replace("\r", ""); }
  17. auto boolean() const { return string() == "true"; }
  18. auto integer() const { return string().integer(); }
  19. auto natural() const { return string().natural(); }
  20. auto real() const { return string().real(); }
  21. auto& setName(const nall::string& name) { shared->name = name; return *this; }
  22. auto& setValue(const nall::string& value = "") { shared->value = value; return *this; }
  23. shared_pointer<Variable> shared;
  24. };
  25. struct Variables {
  26. auto operator[](const string& name) const -> SharedVariable {
  27. for(auto& variable : variables) {
  28. if(variable.shared->name.iequals(name)) return variable;
  29. }
  30. return {};
  31. }
  32. auto operator()(const string& name) -> SharedVariable {
  33. for(auto& variable : variables) {
  34. if(variable.shared->name.iequals(name)) return variable;
  35. }
  36. return append(name);
  37. }
  38. auto find(const string& name) const -> vector<SharedVariable> {
  39. vector<SharedVariable> result;
  40. for(auto& variable : variables) {
  41. if(variable.shared->name.iequals(name)) result.append(variable);
  42. }
  43. return result;
  44. }
  45. auto assign(const string& name, const string& value = "") -> SharedVariable {
  46. for(auto& variable : variables) {
  47. if(variable.shared->name.iequals(name)) {
  48. variable.shared->value = value;
  49. return variable;
  50. }
  51. }
  52. return append(name, value);
  53. }
  54. auto append(const string& name, const string& value = "") -> SharedVariable {
  55. SharedVariable variable{name, value};
  56. variables.append(variable);
  57. return variable;
  58. }
  59. auto remove(const string& name) -> void {
  60. for(auto n : reverse(range(variables.size()))) {
  61. if(variables[n].shared->name.iequals(name)) variables.remove(n);
  62. }
  63. }
  64. auto size() const { return variables.size(); }
  65. auto begin() const { return variables.begin(); }
  66. auto end() const { return variables.end(); }
  67. auto begin() { return variables.begin(); }
  68. auto end() { return variables.end(); }
  69. vector<SharedVariable> variables;
  70. };
  71. struct Message {
  72. using type = Message;
  73. virtual auto head(const function<bool (const uint8_t* data, uint size)>& callback) const -> bool = 0;
  74. virtual auto setHead() -> bool = 0;
  75. virtual auto body(const function<bool (const uint8_t* data, uint size)>& callback) const -> bool = 0;
  76. virtual auto setBody() -> bool = 0;
  77. Variables header;
  78. //private:
  79. string _head;
  80. string _body;
  81. };
  82. }