-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.cpp
More file actions
executable file
·212 lines (194 loc) · 7.07 KB
/
http_client.cpp
File metadata and controls
executable file
·212 lines (194 loc) · 7.07 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
#include <arpa/inet.h>
#include <cstdlib> // exit()
#include <cstring> // std::memset()
#include <iostream> // std::cout, std::cerr, std::endl
#include <map> // std::map
#include <netdb.h> // getaddinfo(), freeaddrinfo(), gai_strerror(), struct addrinfo, gethostbyname()
#include <regex> // std::regex, std::smatch, std::regex_search()
#include <string> // std::string, std::to_string(), std::string::find(), std::string::substr()
// #include <sys/socket.h> // socket(), connetct()
#include "http_client.hpp"
#include <linux/if_ether.h>
#include <tuple>
#include <unistd.h> // write(), read(), close()
HttpConnection::HttpConnection(const std::string hostAddress,
const uint16_t hostPort) {
addrinfo hints;
std::memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC;
if (int infoStatus =
getaddrinfo(hostAddress.data(), std::to_string(hostPort).data(),
&hints, &hostInfo) != 0) {
std::cerr << gai_strerror(infoStatus) << std::endl;
}
if (hostInfo->ai_family == AF_INET) {
socketAddress = (sockaddr_in *)(hostInfo->ai_addr);
} else if (hostInfo->ai_family == AF_INET6) {
socketAddress = (sockaddr_in6 *)(hostInfo->ai_addr);
}
// std::string buf;
// buf.resize(hostInfo->ai_addrlen);
// if (inet_ntop(hostInfo->ai_family, &(((sockaddr_in
// *)socketAddress)->sin_addr), &buf[0], hostInfo->ai_addrlen) <= 0)
// {
// std::cerr << "ERROR could not convert\n";
// }
// else
// {
// std::cout << buf << std::endl;
// }
socketFileDescriptor =
socket(hostInfo->ai_family, hostInfo->ai_socktype, hostInfo->ai_protocol);
if (socketFileDescriptor < 0) {
std::cerr << "ERROR could not create socket\n";
std::exit(EXIT_FAILURE);
}
}
void HttpConnection::openConnection() {
if (connected) {
std::cerr << "WARNING already connected\n";
} else {
if (connect(socketFileDescriptor, (sockaddr *)socketAddress,
hostInfo->ai_addrlen) < 0) {
std::cerr << "ERROR could not connect\n";
std::exit(EXIT_FAILURE);
} else {
connected = true;
}
}
}
void HttpConnection::closeConnection() {
if (!connected) {
std::cerr << "WARNING already closed\n";
} else {
close(socketFileDescriptor);
connected = false;
}
}
HttpResponse HttpConnection::sendRequest(HttpRequest request) {
// Send request
std::string requestString = request.string();
uint64_t sentBytesTotal = 0;
int64_t sentBytes;
if (!connected) {
openConnection();
}
do {
sentBytes =
send(socketFileDescriptor, requestString.data() + sentBytesTotal,
requestString.length() - sentBytesTotal, 0);
std::cout << "sent: " << sentBytes << std::endl;
if (sentBytes > 0) {
sentBytesTotal += sentBytes;
} else if (sentBytes < 0) {
std::cerr << "ERROR writing to socket\n";
std::exit(EXIT_FAILURE);
} else {
break;
}
} while (sentBytesTotal <= requestString.length());
// Receive response
uint64_t receivedBytesTotal = 0;
int64_t receivedBytes;
std::string buffer;
buffer.resize(1024);
receivedBytes = recv(socketFileDescriptor, &buffer[0] + receivedBytesTotal,
buffer.length() - receivedBytesTotal, 0);
std::cout << "received: " << receivedBytes << std::endl << std::endl;
if (receivedBytes > 0) {
receivedBytesTotal += receivedBytes;
} else if (receivedBytes < 0) {
std::cerr << "ERROR reading from socket\n";
std::exit(EXIT_FAILURE);
}
std::tuple<StatusLine, std::size_t> statusLineAndHeadersPosition =
getStatusLine(buffer);
StatusLine statusLine = std::get<0>(statusLineAndHeadersPosition);
std::size_t headersBeginPosition = std::get<1>(statusLineAndHeadersPosition);
std::tuple<Headers, std::size_t> headersAndBodyPosition =
getHeaders(buffer, headersBeginPosition);
Headers headers = std::get<0>(headersAndBodyPosition);
std::size_t bodyBeginPosition = std::get<1>(headersAndBodyPosition);
std::string body =
buffer.substr(bodyBeginPosition, receivedBytesTotal - bodyBeginPosition);
// trim right
std::size_t newLinePosition;
if ((newLinePosition = body.rfind("\r\n", receivedBytesTotal)) !=
std::string::npos) {
body = body.erase(newLinePosition);
}
// std::cout << "version: " << statusLine.version << std::endl;
// std::cout << "code: " << statusLine.code << std::endl;
// std::cout << "message: " << statusLine.message << std::endl;
// std::cout << std::endl;
// for (Headers::iterator it = headers.begin(); it != headers.end(); ++it)
// {
// std::cout << it->first << " : " << it->second << std::endl;
// }
// std::cout << buffer.substr(0) << std::endl;
// std::cout << std::endl;
// std::cout << buffer << std::endl;
return HttpResponse(statusLine, headers, body);
}
HttpConnection::~HttpConnection() { freeaddrinfo(hostInfo); }
std::tuple<StatusLine, std::size_t>
HttpConnection::getStatusLine(std::string &buffer) {
std::string firstLine;
std::size_t position = buffer.find("\r\n");
if (position == std::string::npos) {
std::cerr << "ERROR could not find \\r\\n\n";
}
firstLine = buffer.substr(0, position);
std::regex pattern(R"(([ -~]+)\s([0-9]+)\s(.*))");
std::smatch match;
if (std::regex_search(firstLine, match, pattern)) {
return std::tuple<StatusLine, std::size_t>(
StatusLine(match.str(1), std::stoul(match.str(2), nullptr, 10),
match.str(3)),
position + 2);
} else {
std::cerr << "ERROR invalid status line\n";
return std::tuple<StatusLine, std::size_t>(StatusLine("", 0, ""),
position + 2);
}
}
std::tuple<Headers, std::size_t>
HttpConnection::getHeaders(std::string &buffer, std::size_t startPosition) {
if (!startPosition) {
std::size_t position = buffer.find("\r\n");
if (position == std::string::npos) {
std::cerr << "ERROR could not find \\r\\n\n";
return std::tuple<Headers, std::size_t>(Headers(), position);
}
startPosition = position + 2;
}
Headers headers;
std::size_t delimeterPosition = startPosition;
std::size_t lastDelimeterPosition = startPosition;
std::string substring;
do {
lastDelimeterPosition = delimeterPosition;
delimeterPosition = buffer.find("\r\n", lastDelimeterPosition + 2);
// std::cout << "lastDelimeterPosition: " << lastDelimeterPosition <<
// std::endl; std::cout << "delimeterPosition: " << delimeterPosition <<
// std::endl;
if (delimeterPosition == std::string::npos) {
// std::cout << "NOT FOUND\n";
break;
}
substring = buffer.substr(lastDelimeterPosition,
delimeterPosition - lastDelimeterPosition);
if (substring.length() == 2) {
// std::cout << "END OF HEADERS\n";
break;
}
std::regex pattern(R"(([ -~]+):\s+(.*))");
std::smatch match;
if (std::regex_search(substring, match, pattern)) {
headers.insert(Header(match.str(1), match.str(2)));
// headers[match.str(1)] = match.str(2);
}
} while (1);
return std::tuple<Headers, std::size_t>(headers, delimeterPosition + 2);
}