-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcc.cpp
More file actions
174 lines (149 loc) · 5.7 KB
/
cc.cpp
File metadata and controls
174 lines (149 loc) · 5.7 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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <algorithm>
#pragma comment(lib, "ws2_32.lib")
class HTTPSimpleClient {
public:
HTTPSimpleClient() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
throw std::runtime_error("WSAStartup failed");
}
}
~HTTPSimpleClient() {
WSACleanup();
}
bool request(const std::string& protocol, const std::string& address, int port, const std::string& path) {
// Create socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
std::cerr << "Socket creation failed: " << WSAGetLastError() << std::endl;
return false;
}
// Resolve address (IP or domain)
struct hostent* he = gethostbyname(address.c_str());
if (he == nullptr) {
std::cerr << "Address resolution failed" << std::endl;
closesocket(sock);
return false;
}
// Set up server address
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr*)he->h_addr);
memset(&(server_addr.sin_zero), 0, 8);
// Connect to server
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
std::cerr << "Connection failed: " << WSAGetLastError() << std::endl;
closesocket(sock);
return false;
}
// Build HTTP request
std::string host_header = address;
if (port != 80 && port != 443) {
host_header += ":" + std::to_string(port);
}
std::string request = "GET " + path + " HTTP/1.1\r\n"
"Host: " + host_header + "\r\n"
"Connection: close\r\n\r\n";
// Send request
if (send(sock, request.c_str(), (int)request.size(), 0) == SOCKET_ERROR) {
std::cerr << "Request send failed: " << WSAGetLastError() << std::endl;
closesocket(sock);
return false;
}
// Receive response
std::vector<char> buffer(4096);
std::string response;
int bytes_received;
while ((bytes_received = recv(sock, buffer.data(), (int)buffer.size(), 0)) > 0) {
response.append(buffer.data(), bytes_received);
}
if (bytes_received == SOCKET_ERROR) {
std::cerr << "Response receive failed: " << WSAGetLastError() << std::endl;
closesocket(sock);
return false;
}
// Print response status line
size_t end_of_status = response.find("\r\n");
if (end_of_status != std::string::npos) {
std::cout << "Response status: " << response.substr(0, end_of_status) << std::endl;
} else {
std::cout << "Received response but couldn't parse status" << std::endl;
}
// Close socket
closesocket(sock);
return true;
}
};
std::string toLower(const std::string& str) {
std::string lowerStr = str;
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
return lowerStr;
}
int main() {
try {
HTTPSimpleClient client;
// Get protocol (HTTP/HTTPS)
std::string protocol;
while (true) {
std::cout << "HTTP? [http or https]: ";
std::cin >> protocol;
protocol = toLower(protocol);
if (protocol == "http" || protocol == "https") {
break;
}
std::cout << "Invalid protocol. Please enter 'http' or 'https'." << std::endl;
}
// Get address (domain or IP)
std::string address;
std::cout << "Domains OR IP?: ";
std::cin >> address;
// Get port
int port = (protocol == "https") ? 443 : 80;
std::string portInput;
std::cout << "Port? [enter port number or NULL for default]: ";
std::cin >> portInput;
if (toLower(portInput) != "null") {
try {
port = std::stoi(portInput);
} catch (...) {
std::cerr << "Invalid port number, using default (" << port << ")" << std::endl;
}
}
// Get number of requests
int attempts = 1;
std::cout << "Num? [number of requests]: ";
std::cin >> attempts;
attempts = std::max(1, attempts);
std::string path = "/";
std::cout << "\nStarting " << attempts << " " << protocol << " requests to " << address;
if (port != ((protocol == "https") ? 443 : 80)) {
std::cout << ":" << port;
}
std::cout << path << std::endl;
int successCount = 0;
for (int i = 0; i < attempts; ++i) {
std::cout << "\nRequest #" << i + 1 << ":" << std::endl;
if (client.request(protocol, address, port, path)) {
std::cout << "SUCCESS" << std::endl;
successCount++;
} else {
std::cerr << "FAILED" << std::endl;
}
}
std::cout << "\n--------------------------------" << std::endl;
std::cout << "Completed " << attempts << " requests (" << successCount << " successful)" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}