File: request.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 (184 lines) | stat: -rw-r--r-- 6,054 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#pragma once

#include <nall/decode/url.hpp>
#include <nall/encode/url.hpp>
#include <nall/http/message.hpp>

namespace nall { namespace HTTP {

struct Request : Message {
  using type = Request;

  enum class RequestType : unsigned { None, Head, Get, Post };

  explicit operator bool() const { return requestType() != RequestType::None; }

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

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

  auto ipv4() const -> bool { return _ipv6 == false; }
  auto ipv6() const -> bool { return _ipv6 == true; }
  auto ip() const -> string { return _ip; }

  auto requestType() const -> RequestType { return _requestType; }
  auto setRequestType(RequestType value) -> void { _requestType = value; }

  auto path() const -> string { return _path; }
  auto setPath(const string& value) -> void { _path = value; }

  Variables cookie;
  Variables get;
  Variables post;

//private:
  bool _ipv6 = false;
  string _ip;
  RequestType _requestType = RequestType::None;
  string _path;
};

auto Request::head(const function<bool (const uint8_t*, unsigned)>& callback) const -> bool {
  if(!callback) return false;
  string output;

  string request = path();
  if(get.size()) {
    request.append("?");
    for(auto& variable : get) {
      request.append(Encode::URL(variable.name()), "=", Encode::URL(variable.value()), "&");
    }
    request.rtrim("&", 1L);
  }

  switch(requestType()) {
  case RequestType::Head: output.append("HEAD ", request, " HTTP/1.1\r\n"); break;
  case RequestType::Get : output.append("GET ",  request, " HTTP/1.1\r\n"); break;
  case RequestType::Post: output.append("POST ", request, " HTTP/1.1\r\n"); break;
  default: return false;
  }

  for(auto& variable : header) {
    output.append(variable.name(), ": ", variable.value(), "\r\n");
  }
  output.append("\r\n");

  return callback(output.binary(), output.size());
}

auto Request::setHead() -> bool {
  lstring headers = _head.split("\n");
  string request = headers.takeFirst().rtrim("\r", 1L);
  string requestHost;

       if(request.iendsWith(" HTTP/1.0")) request.irtrim(" HTTP/1.0", 1L);
  else if(request.iendsWith(" HTTP/1.1")) request.irtrim(" HTTP/1.1", 1L);
  else return false;

       if(request.ibeginsWith("HEAD ")) request.iltrim("HEAD ", 1L), setRequestType(RequestType::Head);
  else if(request.ibeginsWith("GET " )) request.iltrim("GET ",  1L), setRequestType(RequestType::Get );
  else if(request.ibeginsWith("POST ")) request.iltrim("POST ", 1L), setRequestType(RequestType::Post);
  else return false;

  //decode absolute URIs
  request.strip().iltrim("http://", 1L);
  if(!request.beginsWith("/")) {
    lstring components = request.split("/", 1L);
    requestHost = components(0);
    request = {"/", components(1)};
  }

  lstring components = request.split("?", 1L);
  setPath(components(0));

  if(auto queryString = components(1)) {
    for(auto& block : queryString.split("&")) {
      auto p = block.split("=", 1L);
      auto name = Decode::URL(p(0));
      auto value = Decode::URL(p(1));
      if(name) get.append(name, value);
    }
  }

  for(auto& header : headers) {
    if(header.beginsWith(" ") || header.beginsWith("\t")) continue;
    auto part = header.split(":", 1L).strip();
    if(!part[0] || part.size() != 2) continue;
    this->header.append(part[0], part[1]);

    if(part[0].iequals("Cookie")) {
      for(auto& block : part[1].split(";")) {
        auto p = block.split("=", 1L).strip();
        auto name = p(0);
        auto value = p(1).trim("\"", "\"", 1L);
        if(name) cookie.append(name, value);
      }
    }
  }

  if(requestHost) header.assign("Host", requestHost);  //request URI overrides host header
  return true;
}

auto Request::body(const function<bool (const uint8_t*, unsigned)>& callback) const -> bool {
  if(!callback) return false;

  if(_body) {
    return callback(_body.binary(), _body.size());
  }

  return true;
}

auto Request::setBody() -> bool {
  if(requestType() == RequestType::Post) {
    auto contentType = header["Content-Type"].value();
    if(contentType.iequals("application/x-www-form-urlencoded")) {
      for(auto& block : _body.split("&")) {
        auto p = block.rtrim("\r").split("=", 1L);
        auto name = Decode::URL(p(0));
        auto value = Decode::URL(p(1));
        if(name) post.append(name, value);
      }
    } else if(contentType.imatch("multipart/form-data; boundary=?*")) {
      auto boundary = contentType.iltrim("multipart/form-data; boundary=", 1L).trim("\"", "\"", 1L);
      auto blocks = _body.split({"--", boundary}, 1024L);  //limit blocks to prevent memory exhaustion
      for(auto& block : blocks) block.trim("\r\n", "\r\n", 1L);
      if(blocks.size() < 2 || (blocks.takeFirst(), !blocks.takeLast().beginsWith("--"))) return false;
      for(auto& block : blocks) {
        string name;
        string filename;
        string contentType;

        auto segments = block.split("\r\n\r\n", 1L);
        for(auto& segment : segments(0).split("\r\n")) {
          auto statement = segment.split(":", 1L);
          if(statement(0).ibeginsWith("Content-Disposition")) {
            for(auto& component : statement(1).split(";")) {
              auto part = component.split("=", 1L).strip();
              if(part(0).iequals("name")) {
                name = part(1).trim("\"", "\"", 1L);
              } else if(part(0).iequals("filename")) {
                filename = part(1).trim("\"", "\"", 1L);
              }
            }
          } else if(statement(0).ibeginsWith("Content-Type")) {
            contentType = statement(1).strip();
          }
        }

        if(name) {
          post.append(name, segments(1));
          post.append({name, ".filename"}, filename);
          post.append({name, ".content-type"}, contentType);
        }
      }
    }
  }

  return true;
}

}}