Applied changes from review

Also moved builtin_expect to utility.h (for use in sanitizer function)
This commit is contained in:
The-EDev 2022-02-11 00:56:30 +03:00
parent ab50fb3134
commit 9a7677bf1a
No known key found for this signature in database
GPG Key ID: 51C45DC0C413DCD9
7 changed files with 35 additions and 34 deletions

View File

@ -6,15 +6,6 @@
#include <iostream>
#include "crow/utility.h"
// TODO(EDev): Adding C++20's [[likely]] and [[unlikely]] attributes might be useful
#if defined(__GNUG__) || defined(__clang__)
#define CROW_LIKELY(X) __builtin_expect(!!(X), 1)
#define CROW_UNLIKELY(X) __builtin_expect(!!(X), 0)
#else
#define CROW_LIKELY(X) (X)
#define CROW_UNLIKELY(X) (X)
#endif
namespace crow
{
const char cr = '\r';

View File

@ -302,7 +302,7 @@ namespace crow
}
}
CROW_LOG_INFO << "Request: " << boost::lexical_cast<std::string>(adaptor_.remote_endpoint()) << " " << this << " HTTP/" << (char)(req.http_ver_major + 48) << "." << (char)(req.http_ver_minor + 48) << ' ' << method_name(req.method) << " " << req.url;
CROW_LOG_INFO << "Request: " << boost::lexical_cast<std::string>(adaptor_.remote_endpoint()) << " " << this << " HTTP/" << (char)(req.http_ver_major + '0') << "." << (char)(req.http_ver_minor + '0') << ' ' << method_name(req.method) << " " << req.url;
need_to_call_after_handlers_ = false;
@ -539,11 +539,9 @@ namespace crow
is_writing = false;
if (close_connection_)
{
//boost::asio::socket_base::linger option(true, 30);
//adaptor_.raw_socket().set_option(option);
adaptor_.shutdown_readwrite();
adaptor_.close();
//CROW_LOG_DEBUG << this << " from write (sync)(1)";
CROW_LOG_DEBUG << this << " from write (static)";
check_destroy();
}
@ -598,11 +596,9 @@ namespace crow
is_writing = false;
if (close_connection_)
{
//boost::asio::socket_base::linger option(true, 30);
//adaptor_.raw_socket().set_option(option);
adaptor_.shutdown_readwrite();
adaptor_.close();
//CROW_LOG_DEBUG << this << " from write (sync)(1)";
CROW_LOG_DEBUG << this << " from write (res_stream)";
check_destroy();
}

View File

@ -125,7 +125,6 @@ enum http_connection_flags // This is basically 7 booleans placed into 1 integer
CROW_XX(INVALID_CONSTANT, "invalid constant string") \
CROW_XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state") \
CROW_XX(STRICT, "strict mode assertion failed") \
/*CROW_XX(PAUSED, "parser is paused")*/ /*There's no need to pause the parser */ \
CROW_XX(UNKNOWN, "an unknown error occurred") \
CROW_XX(INVALID_TRANSFER_ENCODING, "request has invalid transfer-encoding") \

View File

@ -18,7 +18,7 @@
#include <vector>
#include <math.h>
#include "crow/common.h"
#include "crow/utility.h"
#include "crow/settings.h"
#include "crow/returnable.h"
#include "crow/logging.h"

View File

@ -93,10 +93,11 @@ namespace crow
private:
std::string get_boundary(const std::string& header) const
{
size_t found = header.find("boundary=");
constexpr char boundary_text[] = "boundary=";
size_t found = header.find(boundary_text);
if (found)
{
std::string to_return(header.substr(found + 9));
std::string to_return(header.substr(found + strlen(boundary_text)));
if (to_return[0] == '\"')
{
to_return = to_return.substr(1, to_return.length() - 2);

View File

@ -70,17 +70,8 @@ namespace crow
{
self->headers.emplace(std::move(self->header_field), std::move(self->header_value));
}
//NOTE(EDev): it seems that the problem is with crow's policy on closing the connection for HTTP_VERSION < 1.0, the behaviour for that in crow is "don't close the connection, but don't send a keep-alive either"
// HTTP1.1 = always send keep_alive, HTTP1.0 = only send if header exists, HTTP?.? = never send
self->keep_alive = (self->http_major == 1 && self->http_minor == 0) ?
((self->flags & F_CONNECTION_KEEP_ALIVE) ? true : false) :
((self->http_major == 1 && self->http_minor == 1) ? true : false);
// HTTP1.1 = only close if close header exists, HTTP1.0 = always close unless keep_alive header exists, HTTP?.?= never close
self->close_connection = (self->http_major == 1 && self->http_minor == 0) ?
((self->flags & F_CONNECTION_KEEP_ALIVE) ? false : true) :
((self->http_major == 1 && self->http_minor == 1) ? ((self->flags & F_CONNECTION_CLOSE) ? true : false) : false);
self->set_connection_parameters();
self->process_header();
return 0;
@ -157,6 +148,21 @@ namespace crow
handler_->handle();
}
inline void set_connection_parameters()
{
//NOTE(EDev): it seems that the problem is with crow's policy on closing the connection for HTTP_VERSION < 1.0, the behaviour for that in crow is "don't close the connection, but don't send a keep-alive either"
// HTTP1.1 = always send keep_alive, HTTP1.0 = only send if header exists, HTTP?.? = never send
keep_alive = (http_major == 1 && http_minor == 0) ?
((flags & F_CONNECTION_KEEP_ALIVE) ? true : false) :
((http_major == 1 && http_minor == 1) ? true : false);
// HTTP1.1 = only close if close header exists, HTTP1.0 = always close unless keep_alive header exists, HTTP?.?= never close
close_connection = (http_major == 1 && http_minor == 0) ?
((flags & F_CONNECTION_KEEP_ALIVE) ? false : true) :
((http_major == 1 && http_minor == 1) ? ((flags & F_CONNECTION_CLOSE) ? true : false) : false);
}
/// Take the parsed HTTP request data and convert it to a \ref crow.request
request to_request() const
{

View File

@ -11,6 +11,15 @@
#include "crow/settings.h"
// TODO(EDev): Adding C++20's [[likely]] and [[unlikely]] attributes might be useful
#if defined(__GNUG__) || defined(__clang__)
#define CROW_LIKELY(X) __builtin_expect(!!(X), 1)
#define CROW_UNLIKELY(X) __builtin_expect(!!(X), 0)
#else
#define CROW_LIKELY(X) (X)
#define CROW_UNLIKELY(X) (X)
#endif
namespace crow
{
namespace black_magic
@ -693,9 +702,8 @@ namespace crow
data[i] = replacement;
}
else if ((c == '/') || (c == '\\'))
{
//TODO(EDev): uncomment below once #332 is merged
if (/*CROW_UNLIKELY(*/ i == 0 /*)*/) //Prevent Unix Absolute Paths (Windows Absolute Paths are prevented with `(c == ':')`)
{
if (CROW_UNLIKELY( i == 0 )) //Prevent Unix Absolute Paths (Windows Absolute Paths are prevented with `(c == ':')`)
{
data[i] = replacement;
}
@ -703,9 +711,9 @@ namespace crow
{
checkForSpecialEntries = true;
}
}
}
}
}
} // namespace utility
} // namespace crow