-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretriever.cpp
More file actions
229 lines (194 loc) · 7.31 KB
/
Copy pathretriever.cpp
File metadata and controls
229 lines (194 loc) · 7.31 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
#include <bits/types/struct_iovec.h>
#include <bits/types/struct_timeval.h>
#include <netdb.h> //gethostbyname
#include <sys/socket.h> //socket, bind, listen, inet_ntoa
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/uio.h> //writev
#include <unistd.h> //read, write, close
#include <array>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
const int BUFFER_SIZE = 1024;
//Generate unique filename
std::string CreateFilename(const std::string& file_name) {
//Fix for unclear file names
std::string unique_file_name = file_name;
if (unique_file_name == "") {
unique_file_name = "index";
}
std::string base_file;
std::size_t dot_pos = unique_file_name.find('.');
std::string extension;
extension = (dot_pos != std::string::npos)
? unique_file_name.substr(dot_pos)
: ".html"; //Default extension if no dot is found
if (dot_pos == std::string::npos) {
unique_file_name += extension;
}
int version = 0;
struct stat buf {};
//Check for existing filename, and generate new filename with (version) appended.
//Checks for existing (version)s and edits the string so we get (2) instead of (1)(2)
while (stat(unique_file_name.c_str(), &buf) != -1) {
std::string base_file =
unique_file_name.substr(0, unique_file_name.find('.'));
if (base_file.find_last_of(')') != std::string::npos) {
if (isdigit(base_file.at(base_file.find_last_of(')') - 1)) != 0) {
int current_version =
std::stoi(base_file.substr(base_file.find_last_of(')') - 1,
base_file.find_last_of(')')));
version = current_version + 1;
unique_file_name =
base_file.substr(0, unique_file_name.find_last_of(')') - 1);
unique_file_name += std::to_string(version) + ")" + extension;
}
} else {
unique_file_name =
(base_file += '(' + std::to_string(1) + ')' + extension);
}
}
return unique_file_name;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cout << "RETRIEVER: Please specify URL for GET request\n";
return -1;
}
std::string special_request = (argv[2] != nullptr) ? argv[2] : "";
bool unallowed = false;
bool malformed = false;
if (special_request == "unallowed") {
std::cout << "RETRIEVER: Unallowed test\n";
unallowed = true;
} else if (special_request == "malformed") {
std::cout << "RETRIEVER: Malformed test\n";
malformed = true;
}
std::string url = argv[1];
//Strip HTTP/HTTPS prefix from URL
size_t prefix_location = url.find("://");
url = (prefix_location != std::string::npos)
? url = url.substr(prefix_location + 3)
: url;
std::size_t slash_pos = url.find('/');
//Get server ip without file path
std::string server_ip =
(slash_pos != std::string::npos) ? url.substr(0, slash_pos) : url;
//Get filepath
std::string file_path =
(slash_pos != std::string::npos) ? url.substr(slash_pos) : "/";
const std::string port = "80";
//Struct for getaddrinfo
struct addrinfo hints {};
struct addrinfo* servinfo{};
struct addrinfo* p{};
//Zero structure
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
//Uses getaddrinfo instead of depreciated gethostbyname
const int status =
getaddrinfo(server_ip.c_str(), port.c_str(), &hints, &servinfo);
if (status != 0) {
perror("getaddrinfo");
std::cerr << "getaddrinfo error\n";
return -1;
}
int client_sd = -1;
for (p = servinfo; p != nullptr; p = p->ai_next) {
client_sd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (client_sd == -1) {
continue; //Try next if socket creation fails
}
if (connect(client_sd, p->ai_addr, p->ai_addrlen) == -1) {
close(client_sd);
continue; //Try next if connection fails
}
break; //Successfully connected
}
//Exit if failed to connect
if (p == nullptr) {
std::cerr << "Failed to connect to the server\n";
//Free addrinfo structure as client_sd is connected
freeaddrinfo(servinfo);
return -1;
}
//Free addrinfo structure as client_sd is connected
freeaddrinfo(servinfo);
std::string request;
if (unallowed) {
//Create unallowed HTTP POST request
request = "POST " + file_path + " HTTP/1.1\r\n";
request += "Host: " + std::string(server_ip) + "\r\n";
request += "Connection: close\r\n";
request += "\r\n"; //End of request header
} else if (malformed) {
//Create malformed HTTP GET request
request = "GeT" + file_path + "HTtP/1.1";
request += "Host:" + std::string(server_ip) + "";
request += "Connection:close\r";
request += "\n"; //End of request header
} else {
//Create HTTP GET request
request = "GET " + file_path + " HTTP/1.1\r\n";
request += "Host: " + std::string(server_ip) + "\r\n";
request += "Connection: close\r\n";
request += "\r\n"; //End of request header
}
//Send HTTP GET request to server
ssize_t bytes_sent = send(client_sd, request.c_str(), request.size(), 0);
if (bytes_sent == -1) {
std::cerr << "Error sending request\n";
close(client_sd);
return -1;
}
//Read and save response from server
std::array<char, BUFFER_SIZE> buffer = std::array<char, BUFFER_SIZE>();
ssize_t bytes_received = 0;
bool is_header = true;
std::string response_data;
// Read response from server
while ((bytes_received =
read(client_sd, buffer.data(), buffer.size() - 1)) > 0) {
buffer[bytes_received] = '\0'; // Null-terminate response
response_data.append(buffer.data()); // Add response data
if (is_header) {
// Check if header has been read
std::size_t header_end = response_data.find("\r\n\r\n");
if (header_end != std::string::npos) {
is_header = false;
// Parse headers
std::string headers = response_data.substr(0, header_end);
response_data =
response_data.substr(header_end + 4); // Move past headers
}
}
}
if (bytes_received == 0) {
// std::cerr << "No response from server\n";
} else if (bytes_received == -1) {
std::cerr << "Error reading from server\n";
}
//Get filename
std::string file_name =
CreateFilename(file_path.substr(file_path.find_last_of('/') + 1));
//Open file to save response
std::ofstream outfile(file_name);
if (!outfile.is_open()) {
std::cerr << "Error opening file for writing\n";
close(client_sd);
return -1;
}
// Write the body to the file
outfile << response_data; // Write the accumulated response data
//Close the file and the client socket
outfile.close();
close(client_sd);
std::cout << "RETRIEVER: Response saved to: " << file_name << "\n";
return 0;
}