-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.cpp
More file actions
104 lines (80 loc) · 2.67 KB
/
request.cpp
File metadata and controls
104 lines (80 loc) · 2.67 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
//
// Created by Lynx on 2025-05-13.
//
#include "request.h"
#include <chrono>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <utility>
#include "url.h"
#include "WebResponse.h"
#pragma comment(lib, "ws2_32.lib")
WebResponse request(std::string url) {
WebResponse res = {};
auto timeStart = std::chrono::high_resolution_clock::now();
std::string domainShort, domainFull, path;
parseUrl(std::move(url), domainShort, domainFull, path);
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
res._error = "WSAStartup failed.";
return res;
}
addrinfo hints{};
addrinfo* serverInfo;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
const char* hostname = domainShort.c_str();
const char* port = "80";
if (getaddrinfo(hostname, port, &hints, &serverInfo) != 0) {
res._error = "getaddrinfo failed.";
WSACleanup();
return res;
}
SOCKET sock = socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);
if (sock == INVALID_SOCKET) {
res._error = "socket creation failed.";
freeaddrinfo(serverInfo);
WSACleanup();
return res;
}
if (connect(sock, serverInfo->ai_addr, (int)serverInfo->ai_addrlen) == SOCKET_ERROR) {
res._error = "connect failed.";
closesocket(sock);
freeaddrinfo(serverInfo);
WSACleanup();
return res;
}
freeaddrinfo(serverInfo);
std::string request = "GET " + path + " HTTP/1.1\r\n";
request += "Host: " + domainFull + "\r\n";
request += "Connection: close\r\n";
request += "\r\n";
int bytesSent = send(sock, request.c_str(), request.length(), 0);
if (bytesSent == SOCKET_ERROR) {
res._error = "send failed.";
closesocket(sock);
WSACleanup();
return res;
}
char buffer[4096];
int bytesReceived;
std::string rawResponse;
auto timeResponse = std::chrono::high_resolution_clock::now();
while ((bytesReceived = recv(sock, buffer, sizeof(buffer) - 1, 0)) > 0) {
buffer[bytesReceived] = '\0';
rawResponse += buffer;
}
if (bytesReceived == SOCKET_ERROR) {
res._error = "socket error.";
}
closesocket(sock);
WSACleanup();
res.raw = rawResponse;
res.latencyFullMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - timeStart).count();
res.latencyResponseMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - timeResponse).count();
res._process();
return res;
}