-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHttpRequest.h
100 lines (86 loc) · 2.28 KB
/
HttpRequest.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
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
// HttpRequest.h
#ifndef HTTP_REQUEST_H_
#define HTTP_REQUEST_H_
#include <string>
#include <vector>
#include <map>
using std::string;
using std::vector;
using std::map;
typedef std::map<std::string, std::string> KeyValue;
// F(id, str)
#define FOREACH_CONTENT_TYPE(F) \
F(TEXT_PLAIN, "text/plain") \
F(TEXT_HTML, "text/html") \
F(TEXT_XML, "text/xml") \
F(APPLICATION_JSON, "application/json") \
F(APPLICATION_XML, "application/xml") \
F(APPLICATION_JAVASCRIPT, "application/javascript") \
\
F(FORM_DATA, "multipart/form-data") \
\
F(X_WWW_FORM_URLENCODED, "application/x-www-form-urlencoded") \
F(QUERY_STRING, "text/plain")
#define ENUM_CONTENT_TYPE(id, _) id,
enum ContentType {
FOREACH_CONTENT_TYPE(ENUM_CONTENT_TYPE)
};
struct FormData {
enum FormDataType {
CONTENT,
FILENAME
} type;
string data;
FormData() {
type = CONTENT;
}
FormData(const char* data, FormDataType type = CONTENT) {
this->type = type;
this->data = data;
}
FormData(const string& str, FormDataType type = CONTENT) {
this->type = type;
this->data = str;
}
FormData(int n) {
this->type = CONTENT;
this->data = std::to_string(n);
}
FormData(long long n) {
this->type = CONTENT;
this->data = std::to_string(n);
}
FormData(float f) {
this->type = CONTENT;
this->data = std::to_string(f);
}
FormData(double lf) {
this->type = CONTENT;
this->data = std::to_string(lf);
}
};
typedef std::multimap<std::string, FormData> Form;
struct HttpRequest {
// request line
string method;
string url;
string version;
// headers
KeyValue headers;
// body
ContentType content_type;
string text;
KeyValue kvs; // QUERY_STRING,X_WWW_FORM_URLENCODED
Form form; // FORM_DATA
};
struct HttpResponse {
// status line
string version;
int status_code;
string status_message;
// headers
KeyValue headers;
// body
string body;
};
#endif // HTTP_REQUEST_H_