-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.cpp
More file actions
244 lines (203 loc) · 7.38 KB
/
Copy pathrequests.cpp
File metadata and controls
244 lines (203 loc) · 7.38 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdlib.h> /* exit, atoi, malloc, free */
#include <stdio.h>
#include <unistd.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#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 "helper.hpp"
#include "requests.hpp"
char *compute_delete_request(char *host, char *url, char *query_params,
char **cookies, int cookies_count, char *JWT_token)
{
char *message = (char *)calloc(BUFLEN, sizeof(char));
char *line = (char *)calloc(LINELEN, sizeof(char));
// Step 1: write the method name, URL, request params (if any) and protocol type
if (query_params != NULL) {
sprintf(line, "DELETE %s?%s HTTP/1.1", url, query_params);
} else {
sprintf(line, "DELETE %s HTTP/1.1", url);
}
compute_message(message, line);
// Step 2: add the host
memset(line, 0, LINELEN);
sprintf(line, "Host: %s", host);
compute_message(message, line);
// Step 3 (optional): add headers and/or cookies, according to the protocol format
if (cookies != NULL) {
memset(line, 0, LINELEN);
strcat(line, "Cookie: ");
for (int i = 0; i < cookies_count; i++) {
strcat(line, cookies[i]);
if (i < cookies_count - 1) {
strcat(line, "; ");
}
}
compute_message(message, line);
}
// Step 4: add authorization
if (JWT_token != NULL) {
memset(line, 0, LINELEN);
sprintf(line, "Authorization: Bearer %s", JWT_token);
compute_message(message, line);
}
// Step 5: add final new line
compute_message(message, "");
free(line);
return message;
}
char *compute_get_request(char *host, char *url, char *query_params,
char **cookies, int cookies_count, char *JWT_token)
{
char *message = (char *)calloc(BUFLEN, sizeof(char));
char *line = (char *)calloc(LINELEN, sizeof(char));
// Step 1: write the method name, URL, request params (if any) and protocol type
if (query_params != NULL) {
sprintf(line, "GET %s?%s HTTP/1.1", url, query_params);
} else {
sprintf(line, "GET %s HTTP/1.1", url);
}
compute_message(message, line);
// Step 2: add the host
memset(line, 0, LINELEN);
sprintf(line, "Host: %s", host);
compute_message(message, line);
// Step 3 (optional): add headers and/or cookies, according to the protocol format
if (cookies != NULL) {
memset(line, 0, LINELEN);
strcat(line, "Cookie: ");
for (int i = 0; i < cookies_count; i++) {
strcat(line, cookies[i]);
if (i < cookies_count - 1) {
strcat(line, "; ");
}
}
compute_message(message, line);
}
// Step 4: add authorization
if (JWT_token != NULL) {
memset(line, 0, LINELEN);
sprintf(line, "Authorization: Bearer %s", JWT_token);
compute_message(message, line);
}
// Step 5: add final new line
compute_message(message, "");
free(line);
return message;
}
char *compute_post_request(char *host, char *url, char* content_type, char **body_data,
int body_data_fields_count, char **cookies, int cookies_count,
char *JWT_token)
{
char *message = (char *)calloc(BUFLEN, sizeof(char));
char *line = (char *)calloc(LINELEN, sizeof(char));
char *body_data_buffer = (char *)calloc(LINELEN, sizeof(char));
// Step 1: write the method name, URL and protocol type
sprintf(line, "POST %s HTTP/1.1", url);
compute_message(message, line);
// Step 2: add the host
memset(line, 0, LINELEN);
sprintf(line, "Host: %s", host);
compute_message(message, line);
/* 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
*/
if (content_type != NULL) {
memset(line, 0, LINELEN);
sprintf(line, "Content-Type: %s", content_type);
compute_message(message, line);
}
memset(body_data_buffer, 0, LINELEN);
for (int i = 0; i < body_data_fields_count; i++) {
strcat(body_data_buffer, body_data[i]);
if (i < body_data_fields_count - 1) {
strcat(body_data_buffer, "&");
}
}
memset(line, 0, LINELEN);
sprintf(line, "Content-Length: %ld", strlen(body_data_buffer));
compute_message(message, line);
// Step 4 (optional): add cookies
if (cookies != NULL) {
memset(line, 0, LINELEN);
strcat(line, "Cookie: ");
for (int i = 0; i < cookies_count; i++) {
strcat(line, cookies[i]);
if (i < cookies_count - 1) {
strcat(line, "; ");
}
}
compute_message(message, line);
}
// Step 5: add authorization
if (JWT_token != NULL) {
memset(line, 0, LINELEN);
sprintf(line, "Authorization: Bearer %s", JWT_token);
compute_message(message, line);
}
// Step 7: add new line at end of header
compute_message(message, "");
// Step 8: add the actual payload data
memset(line, 0, LINELEN);
strcat(message, body_data_buffer);
free(line);
free(body_data_buffer);
return message;
}
char *compute_put_request(char *host, char *url, char* content_type, char **body_data,
int body_data_fields_count, char **cookies, int cookies_count,
char *JWT_token)
{
char *message = (char *)calloc(BUFLEN, sizeof(char));
char *line = (char *)calloc(LINELEN, sizeof(char));
char *body_data_buffer = (char *)calloc(LINELEN, sizeof(char));
// Step 1: write the method name, URL and protocol type (PUT instead of POST)
sprintf(line, "PUT %s HTTP/1.1", url);
compute_message(message, line);
// Step 2: add the host
memset(line, 0, LINELEN);
sprintf(line, "Host: %s", host);
compute_message(message, line);
// Step 3: Content-Type + Content-Length
if (content_type != NULL) {
memset(line, 0, LINELEN);
sprintf(line, "Content-Type: %s", content_type);
compute_message(message, line);
}
memset(body_data_buffer, 0, LINELEN);
for (int i = 0; i < body_data_fields_count; i++) {
strcat(body_data_buffer, body_data[i]);
if (i < body_data_fields_count - 1) {
strcat(body_data_buffer, "&");
}
}
memset(line, 0, LINELEN);
sprintf(line, "Content-Length: %ld", strlen(body_data_buffer));
compute_message(message, line);
// Step 4: add cookies
if (cookies != NULL) {
memset(line, 0, LINELEN);
strcat(line, "Cookie: ");
for (int i = 0; i < cookies_count; i++) {
strcat(line, cookies[i]);
if (i < cookies_count - 1) {
strcat(line, "; ");
}
}
compute_message(message, line);
}
// Step 5: Authorization
if (JWT_token != NULL) {
memset(line, 0, LINELEN);
sprintf(line, "Authorization: Bearer %s", JWT_token);
compute_message(message, line);
}
// Step 6: end of headers
compute_message(message, "");
// Step 7: body
strcat(message, body_data_buffer);
free(line);
free(body_data_buffer);
return message;
}