Skip to content

Commit f5bee23

Browse files
committed
network: HTTPClient+HTTPDownloadClientTest: some improvements(2)
1 parent af86c93 commit f5bee23

File tree

5 files changed

+18
-32
lines changed

5 files changed

+18
-32
lines changed

src/tdme/network/httpclient/HTTPClient.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ string HTTPClient::createHTTPRequestHeaders(const string& hostname, const string
117117
return request;
118118
}
119119

120-
void HTTPClient::parseHTTPResponseHeaders(stringstream& rawResponse, int16_t& statusCode, unordered_map<string, string>& responseHeaders) {
120+
void HTTPClient::parseHTTPResponseHeaders(stringstream& rawResponse) {
121121
int headerIdx = 0;
122122
string statusHeader;
123123
string line;
@@ -182,18 +182,13 @@ void HTTPClient::execute() {
182182
auto hostname = relativeUrl;
183183
if (slashIdx != -1) hostname = StringTools::substring(relativeUrl, 0, slashIdx);
184184
relativeUrl = StringTools::substring(relativeUrl, hostname.size());
185-
186-
Console::println("HTTPClient::execute(): hostname: " + hostname);
187-
Console::println("HTTPClient::execute(): relative url: " + relativeUrl);
188-
189-
Console::print("HTTPClient::execute(): resolving hostname to IP: " + hostname + ": ");
185+
//
190186
auto ip = Network::getIpByHostname(hostname);
191187
if (ip.empty() == true) {
192188
Console::println("HTTPClient::execute(): failed");
193189
throw HTTPClientException("Could not resolve host IP by hostname");
194190
}
195-
Console::println(ip);
196-
191+
//
197192
TCPSocket::create(socket, TCPSocket::determineIpVersion(ip));
198193
socket.connect(ip, 80);
199194
auto request = createHTTPRequestHeaders(hostname, relativeUrl, body);
@@ -211,7 +206,7 @@ void HTTPClient::execute() {
211206
}
212207

213208
//
214-
parseHTTPResponseHeaders(rawResponse, statusCode, responseHeaders);
209+
parseHTTPResponseHeaders(rawResponse);
215210

216211
//
217212
socket.shutdown();

src/tdme/network/httpclient/HTTPClient.h

+5-7
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ class tdme::network::httpclient::HTTPClient {
5656
/**
5757
* Parse HTTP response headers
5858
* @param rawResponse raw response
59-
* @param statusCode HTTP status code
60-
* @param responseHeaders HTTP response headers
6159
*/
62-
void parseHTTPResponseHeaders(stringstream& rawResponse, int16_t& statusCode, unordered_map<string, string>& responseHeaders);
60+
void parseHTTPResponseHeaders(stringstream& rawResponse);
6361

6462
public:
6563
static const constexpr int16_t HTTP_STATUSCODE_OK { 200 };
@@ -135,16 +133,16 @@ class tdme::network::httpclient::HTTPClient {
135133
}
136134

137135
/**
138-
* Get headers
139-
* @return headers
136+
* Get request headers
137+
* @return request headers
140138
*/
141139
inline const unordered_map<string, string>& getHeaders() {
142140
return headers;
143141
}
144142

145143
/**
146-
* Set headers
147-
* @param headers headers
144+
* Set request headers
145+
* @param headers request headers
148146
*/
149147
inline void setHeaders(const unordered_map<string, string>& headers) {
150148
this->headers = headers;

src/tdme/network/httpclient/HTTPDownloadClient.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ string HTTPDownloadClient::createHTTPRequestHeaders(const string& hostName, cons
113113
return request;
114114
}
115115

116-
uint64_t HTTPDownloadClient::parseHTTPResponseHeaders(ifstream& rawResponse, int16_t& statusCode, unordered_map<string, string>& responseHeaders) {
116+
uint64_t HTTPDownloadClient::parseHTTPResponseHeaders(ifstream& rawResponse) {
117117
responseHeaders.clear();
118118
auto headerSize = 0ll;
119119
auto returnHeaderSize = 0ll;
@@ -194,17 +194,12 @@ void HTTPDownloadClient::start() {
194194
auto hostname = relativeUrl;
195195
if (slashIdx != -1) hostname = StringTools::substring(relativeUrl, 0, slashIdx);
196196
relativeUrl = StringTools::substring(relativeUrl, hostname.size());
197-
198-
Console::println("HTTPDownloadClient::execute(): hostname: " + hostname);
199-
Console::println("HTTPDownloadClient::execute(): relative url: " + relativeUrl);
200-
Console::print("HTTPDownloadClient::execute(): resolving hostname to IP: " + hostname + ": ");
197+
//
201198
auto ip = Network::getIpByHostname(hostname);
202199
if (ip.empty() == true) {
203200
Console::println("HTTPDownloadClient::execute(): failed");
204201
throw HTTPClientException("Could not resolve host IP by hostname");
205202
}
206-
Console::println(ip);
207-
208203
// socket
209204
TCPSocket::create(socket, TCPSocket::determineIpVersion(ip));
210205
socket.connect(ip, 80);
@@ -236,7 +231,7 @@ void HTTPDownloadClient::start() {
236231
}
237232
// try to read headers
238233
downloadClient->responseHeaders.clear();
239-
if ((downloadClient->headerSize = downloadClient->parseHTTPResponseHeaders(ifs, downloadClient->statusCode, downloadClient->responseHeaders)) > 0) {
234+
if ((downloadClient->headerSize = downloadClient->parseHTTPResponseHeaders(ifs)) > 0) {
240235
downloadClient->haveHeaders = true;
241236
auto contentLengthHeaderIt = downloadClient->responseHeaders.find("Content-Length");
242237
if (contentLengthHeaderIt != downloadClient->responseHeaders.end()) {

src/tdme/network/httpclient/HTTPDownloadClient.h

+5-7
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ class tdme::network::httpclient::HTTPDownloadClient {
6767
/**
6868
* Parse HTTP response headers
6969
* @param rawResponse raw response
70-
* @param httpStatusCode HTTP status code
71-
* @param httpHeader HTTP header
7270
* @return http header size or 0 if not yet completely submitted
7371
*/
74-
uint64_t parseHTTPResponseHeaders(ifstream& rawResponse, int16_t& statusCode, unordered_map<string, string>& responseHeaders);
72+
uint64_t parseHTTPResponseHeaders(ifstream& rawResponse);
7573

7674
public:
7775

@@ -115,16 +113,16 @@ class tdme::network::httpclient::HTTPDownloadClient {
115113
}
116114

117115
/**
118-
* Get headers
119-
* @return headers
116+
* Get request headers
117+
* @return request headers
120118
*/
121119
inline const unordered_map<string, string>& getHeaders() {
122120
return headers;
123121
}
124122

125123
/**
126-
* Set headers
127-
* @param headers headers
124+
* Set request headers
125+
* @param headers request headers
128126
*/
129127
inline void setHeaders(const unordered_map<string, string>& headers) {
130128
this->headers = headers;

src/tdme/tests/HTTPClientTest-main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main(int argc, char *argv[]) {
2525
httpClient.execute();
2626
Console::println("HTTP status code: " + to_string(httpClient.getStatusCode()));
2727
for (const auto& [headerName, headerValue]: httpClient.getResponseHeaders()) {
28-
Console::println("Header: " + headerName + ": " + headerValue);
28+
Console::println("Response Header: " + headerName + ": " + headerValue);
2929
}
3030
Console::println("Response: ");
3131
char c;

0 commit comments

Comments
 (0)