-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.cpp
123 lines (107 loc) · 4.05 KB
/
requests.cpp
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
#include <unistd.h> /* read, write, close */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
#include <arpa/inet.h>
#include "requests.hpp"
using namespace std;
string compute_get_request(std::string host, std::string url, std::string query_params, string authToken,
std::vector<std::string> cookies, int cookies_count)
{
string message;
string line;
string cookiesString;
// Step 1: write the method name, URL, request params (if any) and protocol type
if (!query_params.empty()) {
line = "GET " + url + "?" + query_params + " HTTP/1.1";
} else {
line = "GET " + url + " HTTP/1.1";
}
message += line + "\r\n";
if(!authToken.empty()){
message += "Authorization: Bearer " + authToken + "\r\n";
}
// Step 2: add the host
message += "Host: " + host + "\r\n";
// Step 3 (optional): add headers and/or cookies, according to the protocol format
if (!cookies.empty()) {
cookiesString = "Cookie:";
while(!cookies.empty()){
cookiesString += " ";
cookiesString += cookies.front();
cookies.pop_back();
cookiesString += ";";
}
message += cookiesString + "\r\n";
}
// Step 4: add final new line
message += "\r\n";
return message;
}
string compute_post_request(std::string host, std::string url, string authToken, std::string content_type, std::string body_data,
int body_data_fields_count, std::vector<std::string> cookies, int cookies_count)
{
string message;
string cookiesString;
// Step 1: write the method name, URL and protocol type
message += "POST " + url + " HTTP/1.1\r\n";
// Step 2: add the host
message += "Host: " + host + "\r\n";
if(!authToken.empty()){
message += "Authorization: Bearer " + authToken + "\r\n";
}
/* Step 3: add necessary headers (Content-Type and Content-Length are mandatory)
in order to write Content-Length you must first compute the message size
*/
message += "Content-Type: " + content_type + "\r\n";
message += "Content-Length: " + to_string(body_data_fields_count) + "\r\n";
// Step 4 (optional): add cookies
if (!cookies.empty()) {
cookiesString = "Cookie:";
while(!cookies.empty()){
cookiesString += " ";
cookiesString += cookies.front();
cookies.pop_back();
cookiesString += ";";
}
message += cookiesString + "\r\n";
}
// Step 5: add new line at end of header
message += "\r\n";
// Step 6: add the actual payload data
message += body_data + "\r\n";
return message;
}
std::string compute_delete_request(std::string host, std::string url, string authToken, std::string content_type, std::string body_data,
int body_data_fields_count, std::vector<std::string> cookies, int cookies_count)
{
string message;
string cookiesString;
// Step 1: write the method name, URL and protocol type
message += "DELETE " + url + " HTTP/1.1\r\n";
// Step 2: add the host
message += "Host: " + host + "\r\n";
if(!authToken.empty()){
message += "Authorization: Bearer " + authToken + "\r\n";
}
/* Step 3: add necessary headers (Content-Type and Content-Length are mandatory)
in order to write Content-Length you must first compute the message size
*/
message += "Content-Type: " + content_type + "\r\n";
message += "Content-Length: " + to_string(body_data_fields_count) + "\r\n";
if (!cookies.empty()) {
cookiesString = "Cookie:";
while(!cookies.empty()){
cookiesString += " ";
cookiesString += cookies.front();
cookies.pop_back();
cookiesString += ";";
}
message += cookiesString + "\r\n";
}
// Step 5: add new line at end of header
message += "\r\n";
// Step 6: add the actual payload data
message += body_data + "\r\n";
return message;
}