Skip to content

Commit 6efc0e1

Browse files
committed
Optimize code
1 parent 21217ab commit 6efc0e1

File tree

2 files changed

+77
-103
lines changed

2 files changed

+77
-103
lines changed

src/ConnectionPoolImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class ConnectionPoolImpl
240240
idle_.erase(current);
241241
} else {
242242
RESTC_CPP_LOG_TRACE_("Keeping << " << *current->second->GetConnection()
243-
<< " expieres in "
243+
<< " expires in "
244244
<< std::chrono::duration_cast<std::chrono::seconds>(expires - now).count()
245245
<< " seconds ");
246246
}

src/RequestImpl.cpp

Lines changed: 76 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,39 @@ void DoSocks5Handshake(const Connection::ptr_t& connection,
287287
}
288288

289289

290+
void _throwHttpExceptionWhenNot200(const Reply::HttpResponse& response) {
291+
// Silence the cursed clang tidy!
292+
constexpr auto magic_2 = 2;
293+
constexpr auto magic_100 = 100;
294+
constexpr auto http_401 = 401;
295+
constexpr auto http_403 = 403;
296+
constexpr auto http_404 = 404;
297+
constexpr auto http_405 = 405;
298+
constexpr auto http_406 = 406;
299+
constexpr auto http_407 = 407;
300+
constexpr auto http_408 = 408;
301+
302+
if ((response.status_code / magic_100) > magic_2) switch(response.status_code) {
303+
case http_401:
304+
throw HttpAuthenticationException(response);
305+
case http_403:
306+
throw HttpForbiddenException(response);
307+
case http_404:
308+
throw HttpNotFoundException(response);
309+
case http_405:
310+
throw HttpMethodNotAllowedException(response);
311+
case http_406:
312+
throw HttpNotAcceptableException(response);
313+
case http_407:
314+
throw HttpProxyAuthenticationRequiredException(response);
315+
case http_408:
316+
throw HttpRequestTimeOutException(response);
317+
default:
318+
throw RequestFailedWithErrorException(response);
319+
}
320+
}
321+
322+
290323
void DoProxyConnect(const Connection::ptr_t& connection,
291324
const Url& url,
292325
const Request::Properties::ptr_t& properties,
@@ -296,81 +329,51 @@ void DoProxyConnect(const Connection::ptr_t& connection,
296329
static const string crlf{"\r\n"};
297330
auto& sck = connection->GetSocket();
298331

299-
{
300-
const string host(url.GetHost().to_string() + ":" + to_string(url.GetPort()));
301-
ostringstream request_buffer;
302-
request_buffer << "CONNECT ";
303-
request_buffer << host;
304-
request_buffer << " HTTP/1.1" << crlf;
305-
request_buffer << "Host: " << host << crlf << crlf;
306-
RESTC_CPP_LOG_DEBUG_("DoProxyConnect - send CONNECT " << host << " HTTP/1.1");
307-
sck.AsyncWriteT(request_buffer.str(), ctx.GetYield());
332+
const string host(url.GetHost().to_string() + ":" + to_string(url.GetPort()));
333+
ostringstream request_buffer;
334+
request_buffer << "CONNECT ";
335+
request_buffer << host;
336+
request_buffer << " HTTP/1.1" << crlf;
337+
request_buffer << "Host: " << host << crlf << crlf;
338+
RESTC_CPP_LOG_DEBUG_("DoProxyConnect - send CONNECT " << host << " HTTP/1.1");
339+
sck.AsyncWriteT(request_buffer.str(), ctx.GetYield());
340+
341+
RESTC_CPP_LOG_TRACE_("DoProxyConnect: starting receiving from proxy");
342+
//struct { http_version=HTTP_1_1; int status_code=0; string reason_phrase; }
343+
Reply::HttpResponse proxy_response;
344+
345+
try {
346+
DataReader::ReadConfig cfg; //one element struct
347+
cfg.msReadTimeout = properties->recvTimeout;//1000*21
348+
349+
//make_unique<IoReaderImpl>(connection, ctx, cfg);
350+
unique_ptr<DataReader> io_reader = DataReader::CreateIoReader(connection, ctx, cfg);
351+
352+
//get from reply->StartReceiveFromServer(io_reader);
353+
auto timer = IoTimer::Create("ReceivingFromProxy"s,
354+
properties->replyTimeoutMs,//1000*21
355+
connection);
356+
357+
auto stream = make_unique<DataReaderStream>(move(io_reader));
358+
359+
//sets status_code, reason_phrase in response
360+
stream->ReadServerResponse(proxy_response);
361+
stream->ReadHeaderLines(
362+
[](std::string&& name, std::string&& value) {
363+
RESTC_CPP_LOG_TRACE_("Read proxy header: " << name);
364+
});
365+
366+
} catch (const exception& ex) {
367+
RESTC_CPP_LOG_DEBUG_("DoProxyConnect: exception from ReceivingFromProxy: " << ex.what());
368+
throw;
308369
}
309-
{
310-
RESTC_CPP_LOG_TRACE_("DoProxyConnect: starting receiving from proxy");
311-
//struct { http_version=HTTP_1_1; int status_code=0; string reason_phrase; }
312-
Reply::HttpResponse proxy_response;
313-
314-
try {
315-
DataReader::ReadConfig cfg; //one element struct
316-
cfg.msReadTimeout = properties->recvTimeout;//1000*21
317370

318-
//make_unique<IoReaderImpl>(connection, ctx, cfg);
319-
unique_ptr<DataReader> io_reader = DataReader::CreateIoReader(connection, ctx, cfg);
371+
RESTC_CPP_LOG_DEBUG_("DoProxyConnect: Returned from ReceivingFromProxy. code=" << proxy_response.status_code);
320372

321-
//get from reply->StartReceiveFromServer(io_reader);
322-
auto timer = IoTimer::Create("ReceivingFromProxy"s,
323-
properties->replyTimeoutMs,//1000*21
324-
connection);
373+
//check for response code 200
374+
RESTC_CPP_LOG_TRACE_("DoProxyConnect: validating proxy reply");
375+
_throwHttpExceptionWhenNot200(proxy_response);
325376

326-
auto stream = make_unique<DataReaderStream>(move(io_reader));
327-
328-
//sets status_code, reason_phrase in response
329-
stream->ReadServerResponse(proxy_response);
330-
stream->ReadHeaderLines(
331-
[](std::string&& name, std::string&& value) {
332-
RESTC_CPP_LOG_TRACE_("Read proxy header: " << name);
333-
});
334-
335-
} catch (const exception& ex) {
336-
RESTC_CPP_LOG_DEBUG_("DoProxyConnect: exception from ReceivingFromProxy: " << ex.what());
337-
throw;
338-
}
339-
340-
int status_code = proxy_response.status_code;
341-
RESTC_CPP_LOG_DEBUG_("DoProxyConnect: Returned from ReceivingFromProxy. code=" << status_code);
342-
343-
//check for response code 200
344-
RESTC_CPP_LOG_TRACE_("DoProxyConnect: validating proxy reply");
345-
constexpr auto magic_2 = 2;
346-
constexpr auto magic_100 = 100;
347-
constexpr auto http_401 = 401;
348-
constexpr auto http_403 = 403;
349-
constexpr auto http_404 = 404;
350-
constexpr auto http_405 = 405;
351-
constexpr auto http_406 = 406;
352-
constexpr auto http_407 = 407;
353-
constexpr auto http_408 = 408;
354-
355-
if ((status_code / magic_100) > magic_2) switch(status_code) {
356-
case http_401:
357-
throw HttpAuthenticationException(proxy_response);
358-
case http_403:
359-
throw HttpForbiddenException(proxy_response);
360-
case http_404:
361-
throw HttpNotFoundException(proxy_response);
362-
case http_405:
363-
throw HttpMethodNotAllowedException(proxy_response);
364-
case http_406:
365-
throw HttpNotAcceptableException(proxy_response);
366-
case http_407:
367-
throw HttpProxyAuthenticationRequiredException(proxy_response);
368-
case http_408:
369-
throw HttpRequestTimeOutException(proxy_response);
370-
default:
371-
throw RequestFailedWithErrorException(proxy_response);
372-
}
373-
}
374377
RESTC_CPP_LOG_TRACE_("DoProxyConnect - done");
375378
}
376379

@@ -526,36 +529,7 @@ class RequestImpl : public Request {
526529

527530
private:
528531
void ValidateReply(const Reply& reply) {
529-
// Silence the cursed clang tidy!
530-
constexpr auto magic_2 = 2;
531-
constexpr auto magic_100 = 100;
532-
constexpr auto http_401 = 401;
533-
constexpr auto http_403 = 403;
534-
constexpr auto http_404 = 404;
535-
constexpr auto http_405 = 405;
536-
constexpr auto http_406 = 406;
537-
constexpr auto http_407 = 407;
538-
constexpr auto http_408 = 408;
539-
540-
const auto& response = reply.GetHttpResponse();
541-
if ((response.status_code / magic_100) > magic_2) switch(response.status_code) {
542-
case http_401:
543-
throw HttpAuthenticationException(response);
544-
case http_403:
545-
throw HttpForbiddenException(response);
546-
case http_404:
547-
throw HttpNotFoundException(response);
548-
case http_405:
549-
throw HttpMethodNotAllowedException(response);
550-
case http_406:
551-
throw HttpNotAcceptableException(response);
552-
case http_407:
553-
throw HttpProxyAuthenticationRequiredException(response);
554-
case http_408:
555-
throw HttpRequestTimeOutException(response);
556-
default:
557-
throw RequestFailedWithErrorException(response);
558-
}
532+
_throwHttpExceptionWhenNot200(reply.GetHttpResponse());
559533
}
560534

561535
std::string BuildOutgoingRequest() {
@@ -1086,7 +1060,7 @@ class RequestImpl : public Request {
10861060
std::uint64_t bytes_sent_ = 0;
10871061
bool dirty_ = false;
10881062
bool add_url_args_ = true;
1089-
};
1063+
}; //class RequestImpl
10901064

10911065

10921066
std::unique_ptr<Request>

0 commit comments

Comments
 (0)