-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.cpp
More file actions
111 lines (85 loc) · 3.12 KB
/
requests.cpp
File metadata and controls
111 lines (85 loc) · 3.12 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
#include <cstdlib>
#include <string>
#include "helpers.hpp"
#include "requests.hpp"
#include "nlohmann/json.hpp"
using namespace std;
using json_t = nlohmann::json;
char *compute_delete_request(string host, string url, string query_params,
const string& cookie, const string& JWT_token)
{
char *message = (char *)calloc(BUFLEN, sizeof(char));
char *line = (char *)calloc(LINELEN, sizeof(char));
if (query_params != "") {
sprintf(line, "DELETE %s?%s HTTP/1.1", url.c_str(), query_params.c_str());
} else {
sprintf(line, "DELETE %s HTTP/1.1", url.c_str());
}
compute_message(message, line);
sprintf(line, "Host: %s", host.c_str());
compute_message(message, line);
if (!cookie.empty()) {
sprintf(line, "Cookie: %s", cookie.c_str());
compute_message(message, line);
}
if (!JWT_token.empty()) {
sprintf(line, "Authorization: Bearer %s", JWT_token.c_str());
compute_message(message, line);
}
compute_message(message, "");
return message;
}
char * compute_get_request(const char * host, char * url, char * query_params, const string & cookies, int cookies_count, const string & token)
{
char *message = (char *) calloc(BUFLEN, sizeof(char));
char *line = (char *) calloc(LINELEN, sizeof(char));
if (query_params != nullptr) {
sprintf(line, "GET %s?%s HTTP/1.1", url, query_params);
} else {
sprintf(line, "GET %s HTTP/1.1", url);
}
compute_message(message, line);
memset(line, 0, sizeof(char) * LINELEN);
sprintf(line, "Host: %s", host);
compute_message(message, line);
if (!cookies.empty()) {
memset(line, 0, sizeof(char) * LINELEN);
sprintf(line, "Cookie: %s", cookies.c_str());
compute_message(message, line);
}
if (!token.empty()) {
memset(line, 0, sizeof(char) * LINELEN);
sprintf(line, "Authorization: Bearer %s", token.c_str());
compute_message(message, line);
}
compute_message(message, "");
free(line);
return message;
}
char *compute_post_request(const char *host, char *url, char* content_type, json_t *json, const string& token)
{
char *message = (char *) calloc(BUFLEN, sizeof(char));
char *line = (char *) calloc(LINELEN, sizeof(char));
sprintf(line, "POST %s HTTP/1.1", url);
compute_message(message, line);
memset(line, 0, sizeof(char) * LINELEN);
sprintf(line, "Host: %s", host);
compute_message(message, line);
auto payload = json->dump(4);
memset(line, 0, sizeof(char) * LINELEN);
sprintf(line, "Content-Type: %s", content_type);
compute_message(message, line);
memset(line, 0, sizeof(char) * LINELEN);
sprintf(line, "Content-Length: %ld", strlen(payload.c_str()));
compute_message(message, line);
if (!token.empty()) {
memset(line, 0, sizeof(char) * LINELEN);
sprintf(line, "Authorization: Bearer %s", token.c_str());
compute_message(message, line);
}
compute_message(message, "");
memset(line, 0, LINELEN);
strcat(message, payload.c_str());
free(line);
return message;
}