File: message.hpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (99 lines) | stat: -rw-r--r-- 2,767 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once

//httpMessage: base class for httpRequest and httpResponse
//provides shared functionality

namespace nall { namespace HTTP {

struct Variable {
  string name;
  string value;
};

struct SharedVariable {
  SharedVariable(const string& name = "", const string& value = "") : shared(new Variable{name, value}) {}

  explicit operator bool() const { return (bool)shared->name; }
  auto operator()() const { return shared->value; }
  auto& operator=(const string& value) { shared->value = value; return *this; }

  auto name() const { return shared->name; }
  auto value() const { return shared->value; }

  auto& setName(const string& name) { shared->name = name; return *this; }
  auto& setValue(const string& value = "") { shared->value = value; return *this; }

  shared_pointer<Variable> shared;
};

struct Variables {
  auto operator[](const string& name) const -> SharedVariable {
    for(auto& variable : variables) {
      if(variable.shared->name.iequals(name)) return variable;
    }
    return {};
  }

  auto operator()(const string& name) -> SharedVariable {
    for(auto& variable : variables) {
      if(variable.shared->name.iequals(name)) return variable;
    }
    return append(name);
  }

  auto find(const string& name) const -> vector<SharedVariable> {
    vector<SharedVariable> result;
    for(auto& variable : variables) {
      if(variable.shared->name.iequals(name)) result.append(variable);
    }
    return result;
  }

  auto assign(const string& name, const string& value = "") -> SharedVariable {
    for(auto& variable : variables) {
      if(variable.shared->name.iequals(name)) {
        variable.shared->value = value;
        return variable;
      }
    }
    return append(name, value);
  }

  auto append(const string& name, const string& value = "") -> SharedVariable {
    SharedVariable variable{name, value};
    variables.append(variable);
    return variable;
  }

  auto remove(const string& name) -> void {
    for(auto n : rrange(variables)) {
      if(variables[n].shared->name.iequals(name)) variables.remove(n);
    }
  }

  auto size() const { return variables.size(); }
  auto begin() const { return variables.begin(); }
  auto end() const { return variables.end(); }
  auto begin() { return variables.begin(); }
  auto end() { return variables.end(); }

  vector<SharedVariable> variables;
};

struct Message {
  using type = Message;

  virtual auto head(const function<bool (const uint8_t* data, unsigned size)>& callback) const -> bool = 0;
  virtual auto setHead() -> bool = 0;

  virtual auto body(const function<bool (const uint8_t* data, unsigned size)>& callback) const -> bool = 0;
  virtual auto setBody() -> bool = 0;

  Variables header;

//private:
  string _head;
  string _body;
};

}}