-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.h
74 lines (58 loc) · 1.83 KB
/
http.h
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
#ifndef HTTP_H
#define HTTP_H
#include <WiFi.h>
#include <WiFiClient.h>
#include <string>
#include <map>
#include <sstream>
namespace HTTP {
const int TIMEOUT = 2000;
namespace Type {
typedef std::map<std::string, std::string> post_data;
typedef std::map<std::string, std::string> headers_t;
}
namespace Common {
bool parse_status_line(WiFiClient &client, uint16_t *status_code);
bool parse_request_line(WiFiClient &client, uint8_t *method, std::string &path);
bool parse_headers(WiFiClient &client, HTTP::Type::headers_t &headers);
bool read_body(WiFiClient &client, long length, std::string &dest);
long determine_body_length(HTTP::Type::headers_t &headers);
}
#define REQUEST_METHOD_GET 1
#define REQUEST_METHOD_POST 2
class Request {
public:
uint8_t method = REQUEST_METHOD_GET;
std::string path;
HTTP::Type::headers_t headers;
std::string body;
bool success = false;
Request(): path(), headers(), body() {};
Request(WiFiClient client){
success = parse(client);
}
bool parse(WiFiClient &client);
std::string render();
};
class Response {
public:
uint16_t status_code;
HTTP::Type::headers_t headers;
std::string body;
bool success = false;
Response(): headers(), body() {};
Response(WiFiClient client){
success = parse(client);
}
bool parse(WiFiClient &client);
std::string render();
};
namespace Shortcut {
HTTP::Response POST(WiFiClient &client,
uint8_t method,
std::string &host,
const char url[],
HTTP::Type::post_data &data);
}
}
#endif