Browse Source

Remove SHARED specifier; fix various compiler warnings when building as static lib; fix a likely crash in HttpServer

task/remove_strings
Jessica James 3 years ago
parent
commit
4ff6e99638
  1. 11
      src/common/CMakeLists.txt
  2. 97
      src/common/DataBuffer.cpp
  3. 18
      src/common/HTTP_Server.cpp
  4. 28
      src/common/SecureSocket.cpp
  5. 11
      src/common/Socket.cpp
  6. 8
      src/include/Jupiter/Jupiter.h
  7. 1
      src/include/Jupiter/Rehash.h
  8. 23
      src/include/Jupiter/String.hpp
  9. 4
      src/include/Jupiter/String_Type.h

11
src/common/CMakeLists.txt

@ -24,18 +24,25 @@ set(SOURCE_FILES
UDPSocket.cpp)
# Setup library build target
add_library(jupiter SHARED ${SOURCE_FILES})
add_library(jupiter ${SOURCE_FILES})
# Find and link OpenSSL
find_package(OpenSSL REQUIRED)
target_link_libraries(jupiter
OpenSSL::SSL
OpenSSL::Crypto)
OpenSSL::Crypto
${CMAKE_DL_LIBS})
# Setup include directories
target_include_directories(jupiter PUBLIC ../include)
target_include_directories(jupiter PRIVATE ../include/Jupiter)
target_include_directories(jupiter PRIVATE ${OPENSSL_INCLUDE_DIR})
# Define JUPITER_SHARED_LIB when compiling shared lib
get_target_property(jupiter_target_type jupiter TYPE)
if (jupiter_target_type STREQUAL "SHARED_LIBRARY")
target_compile_definitions(jupiter PUBLIC JUPITER_SHARED_LIB)
endif()
# Setup platform-specific definitions
target_compile_definitions(jupiter PRIVATE ${JUPITER_PRIVATE_DEFS})

97
src/common/DataBuffer.cpp

@ -21,34 +21,35 @@
#include "DataBuffer.h"
#include "Reference_String.h"
Jupiter::DataBuffer::DataBuffer()
{
Jupiter::DataBuffer::DataBuffer() {
Jupiter::DataBuffer::base = nullptr;
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head;
Jupiter::DataBuffer::bufferSize = 0U;
}
Jupiter::DataBuffer::DataBuffer(size_t size_)
{
Jupiter::DataBuffer::base = reinterpret_cast<uint8_t *>(malloc(size_));
Jupiter::DataBuffer::DataBuffer(size_t size_) {
Jupiter::DataBuffer::base = new uint8_t[size_];
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head;
Jupiter::DataBuffer::bufferSize = size_;
}
Jupiter::DataBuffer::DataBuffer(FILE *file)
{
fread(std::addressof(Jupiter::DataBuffer::bufferSize), sizeof(size_t), 1, file);
Jupiter::DataBuffer::base = reinterpret_cast<uint8_t *>(malloc(Jupiter::DataBuffer::bufferSize * sizeof(uint8_t)));
Jupiter::DataBuffer::DataBuffer(FILE *file) {
if (fread(std::addressof(Jupiter::DataBuffer::bufferSize), sizeof(size_t), 1, file) != 1) {
Jupiter::DataBuffer::base = nullptr;
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head;
Jupiter::DataBuffer::bufferSize = 0U;
return;
}
Jupiter::DataBuffer::base = new uint8_t[Jupiter::DataBuffer::bufferSize * sizeof(uint8_t)];
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head + Jupiter::DataBuffer::bufferSize;
int chr;
while (Jupiter::DataBuffer::head != Jupiter::DataBuffer::end)
{
while (Jupiter::DataBuffer::head != Jupiter::DataBuffer::end) {
chr = fgetc(file);
if (chr == EOF)
{
if (chr == EOF) {
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head + 1;
break;
}
@ -57,18 +58,15 @@ Jupiter::DataBuffer::DataBuffer(FILE *file)
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
}
Jupiter::DataBuffer::DataBuffer(FILE *file, size_t size_)
{
Jupiter::DataBuffer::DataBuffer(FILE *file, size_t size_) {
Jupiter::DataBuffer::bufferSize = size_;
Jupiter::DataBuffer::base = reinterpret_cast<uint8_t *>(malloc(Jupiter::DataBuffer::bufferSize * sizeof(uint8_t)));
Jupiter::DataBuffer::base = new uint8_t[Jupiter::DataBuffer::bufferSize * sizeof(uint8_t)];
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head + Jupiter::DataBuffer::bufferSize;
int chr;
while (Jupiter::DataBuffer::head != Jupiter::DataBuffer::end)
{
while (Jupiter::DataBuffer::head != Jupiter::DataBuffer::end) {
chr = fgetc(file);
if (chr == EOF)
{
if (chr == EOF) {
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head + 1;
break;
}
@ -77,43 +75,37 @@ Jupiter::DataBuffer::DataBuffer(FILE *file, size_t size_)
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
}
Jupiter::DataBuffer::~DataBuffer()
{
free(Jupiter::DataBuffer::base);
Jupiter::DataBuffer::~DataBuffer() {
delete[] Jupiter::DataBuffer::base; // TODO: replace with std::unique_ptr
}
void Jupiter::DataBuffer::push(const uint8_t *data, size_t size_)
{
void Jupiter::DataBuffer::push(const uint8_t *data, size_t size_) {
Jupiter::DataBuffer::secure(size_);
while (size_-- != 0)
*Jupiter::DataBuffer::end++ = *data++;
}
void Jupiter::DataBuffer::peek_from(FILE *file)
{
void Jupiter::DataBuffer::peek_from(FILE *file) {
fpos_t pos;
fgetpos(file, &pos);
Jupiter::DataBuffer::pop_from(file);
fsetpos(file, &pos);
}
void Jupiter::DataBuffer::peek_from(FILE *file, size_t size_)
{
void Jupiter::DataBuffer::peek_from(FILE *file, size_t size_) {
fpos_t pos;
fgetpos(file, &pos);
Jupiter::DataBuffer::pop_from(file, size_);
fsetpos(file, &pos);
}
void Jupiter::DataBuffer::pop_from(FILE *file)
{
void Jupiter::DataBuffer::pop_from(FILE *file) {
size_t size_;
fread(&size_, sizeof(size_t), 1, file);
Jupiter::DataBuffer::pop_from(file, size_);
}
void Jupiter::DataBuffer::pop_from(FILE *file, size_t size_)
{
void Jupiter::DataBuffer::pop_from(FILE *file, size_t size_) {
Jupiter::DataBuffer::secure(size_);
int chr;
while (size_-- != 0)
@ -125,38 +117,32 @@ void Jupiter::DataBuffer::pop_from(FILE *file, size_t size_)
}
}
void Jupiter::DataBuffer::copy_to(FILE *file)
{
void Jupiter::DataBuffer::copy_to(FILE *file) {
size_t data_size = Jupiter::DataBuffer::size();
fwrite(&data_size, sizeof(size_t), 1, file);
Jupiter::DataBuffer::copy_to(file, Jupiter::DataBuffer::size());
}
void Jupiter::DataBuffer::copy_to(FILE *file, size_t size_)
{
void Jupiter::DataBuffer::copy_to(FILE *file, size_t size_) {
fwrite(Jupiter::DataBuffer::head, sizeof(uint8_t), size_, file);
}
void Jupiter::DataBuffer::copy_to(FILE *file, size_t index, size_t size_)
{
void Jupiter::DataBuffer::copy_to(FILE *file, size_t index, size_t size_) {
fwrite(Jupiter::DataBuffer::head + index, sizeof(uint8_t), size_, file);
}
void Jupiter::DataBuffer::push_to(FILE *file)
{
void Jupiter::DataBuffer::push_to(FILE *file) {
Jupiter::DataBuffer::copy_to(file);
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head;
}
void Jupiter::DataBuffer::push_to(FILE *file, size_t size_)
{
void Jupiter::DataBuffer::push_to(FILE *file, size_t size_) {
Jupiter::DataBuffer::copy_to(file, size_);
Jupiter::DataBuffer::head += size_;
}
size_t Jupiter::DataBuffer::shrink()
{
size_t Jupiter::DataBuffer::shrink() {
Jupiter::DataBuffer::bufferSize = Jupiter::DataBuffer::size();
if (Jupiter::DataBuffer::base != Jupiter::DataBuffer::head)
@ -168,8 +154,7 @@ size_t Jupiter::DataBuffer::shrink()
return Jupiter::DataBuffer::bufferSize;
}
size_t Jupiter::DataBuffer::expandBuffer(size_t size_)
{
size_t Jupiter::DataBuffer::expandBuffer(size_t size_) {
Jupiter::DataBuffer::bufferSize += size_;
size_ = Jupiter::DataBuffer::end - Jupiter::DataBuffer::head;
@ -182,8 +167,7 @@ size_t Jupiter::DataBuffer::expandBuffer(size_t size_)
return Jupiter::DataBuffer::bufferSize;
}
void Jupiter::DataBuffer::secure(size_t size_)
{
void Jupiter::DataBuffer::secure(size_t size_) {
size_t data_size = Jupiter::DataBuffer::end - Jupiter::DataBuffer::head;
size_ += data_size;
if (Jupiter::DataBuffer::bufferSize < size_)
@ -198,8 +182,7 @@ void Jupiter::DataBuffer::secure(size_t size_)
}
}
void Jupiter::DataBuffer::setBufferSize(size_t size_)
{
void Jupiter::DataBuffer::setBufferSize(size_t size_) {
Jupiter::DataBuffer::bufferSize = size_;
size_ = Jupiter::DataBuffer::end - Jupiter::DataBuffer::head;
@ -211,23 +194,19 @@ void Jupiter::DataBuffer::setBufferSize(size_t size_)
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head + size_;
}
size_t Jupiter::DataBuffer::size() const
{
size_t Jupiter::DataBuffer::size() const {
return Jupiter::DataBuffer::end - Jupiter::DataBuffer::head;
}
size_t Jupiter::DataBuffer::getBufferSize() const
{
size_t Jupiter::DataBuffer::getBufferSize() const {
return Jupiter::DataBuffer::bufferSize;
}
uint8_t *Jupiter::DataBuffer::getHead() const
{
uint8_t *Jupiter::DataBuffer::getHead() const {
return Jupiter::DataBuffer::head;
}
void Jupiter::DataBuffer::empty()
{
void Jupiter::DataBuffer::empty() {
Jupiter::DataBuffer::head = Jupiter::DataBuffer::base;
Jupiter::DataBuffer::end = Jupiter::DataBuffer::head;
}

18
src/common/HTTP_Server.cpp

@ -229,9 +229,9 @@ HTTPSession::~HTTPSession() {
struct Jupiter::HTTP::Server::Data {
/** Data */
std::vector<std::unique_ptr<Jupiter::HTTP::Server::Host>> m_hosts;
std::vector<std::unique_ptr<Socket>> m_ports;
std::vector<std::unique_ptr<HTTPSession>> m_sessions;
std::vector<std::unique_ptr<Jupiter::HTTP::Server::Host>> m_hosts; // TODO: remove heap allocation, requires move semantics
std::vector<std::unique_ptr<Socket>> m_ports; // TODO: remove heap allocation, sockets are already pimpl
std::vector<std::unique_ptr<HTTPSession>> m_sessions; // TODO: consider removing heap allocation
std::chrono::milliseconds session_timeout = std::chrono::milliseconds(2000); // TODO: Config variable
std::chrono::milliseconds keep_alive_session_timeout = std::chrono::milliseconds(5000); // TODO: Config variable
size_t max_request_size = 8192; // TODO: Config variable
@ -706,13 +706,13 @@ int Jupiter::HTTP::Server::think() {
auto& session = *itr;
if (session->sock.isShutdown()) {
if (session->sock.recv() == 0) {
itr = m_data->m_sessions.erase(itr + 1);
itr = m_data->m_sessions.erase(itr);
continue;
}
}
else if ((std::chrono::steady_clock::now() > session->last_active + m_data->keep_alive_session_timeout)
|| (session->keep_alive == false && std::chrono::steady_clock::now() > session->last_active + m_data->session_timeout)) {
itr = m_data->m_sessions.erase(itr + 1);
itr = m_data->m_sessions.erase(itr);
continue;
}
else if (session->sock.recv() > 0) {
@ -723,25 +723,25 @@ int Jupiter::HTTP::Server::think() {
session->last_active = std::chrono::steady_clock::now();
m_data->process_request(*session);
if (session->keep_alive == false) { // remove completed session
itr = m_data->m_sessions.erase(itr + 1);
itr = m_data->m_sessions.erase(itr);
//session->sock.shutdown();
continue;
}
// else // keep_alive: session not deleted
}
else if (session->request.size() == m_data->max_request_size) { // reject (full buffer)
itr = m_data->m_sessions.erase(itr + 1);
itr = m_data->m_sessions.erase(itr);
continue;
}
// else // request not over: session not deleted
}
else { // reject
itr = m_data->m_sessions.erase(itr + 1);
itr = m_data->m_sessions.erase(itr);
continue;
}
}
else if (session->sock.getLastError() != JUPITER_SOCK_EWOULDBLOCK) {
itr = m_data->m_sessions.erase(itr + 1);
itr = m_data->m_sessions.erase(itr);
continue;
}
// else // EWOULDBLOCK: session not deleted

28
src/common/SecureSocket.cpp

@ -117,20 +117,23 @@ void Jupiter::SecureSocket::close()
}
}
const SSL_METHOD *translateEncryptionMethod(Jupiter::SecureSocket::EncryptionMethod method)
{
switch (method)
{
const SSL_METHOD *translateEncryptionMethod(Jupiter::SecureSocket::EncryptionMethod method) {
// These are disabled due to deprecations, but also there's not that much reason to actually maintain this method
// since we realistically never use anything except EncryptionMethod::ANY. It was an interesting idea, but there
// just isn't a real use case for these methods. Regardless, better way to implement setMethod/getMethod() would be
// to not provide any abstract implementation at all, and to instead include such option setting as part of some
// OpenSSL provider plugin
switch (method) {
//case Jupiter::SecureSocket::EncryptionMethod::SSL3:
// return SSLv3_method();
case Jupiter::SecureSocket::EncryptionMethod::TLS1:
return TLSv1_method();
//return TLSv1_method();
case Jupiter::SecureSocket::EncryptionMethod::TLS1_1:
return TLSv1_1_method();
//return TLSv1_1_method();
case Jupiter::SecureSocket::EncryptionMethod::TLS1_2:
return TLSv1_2_method();
//return TLSv1_2_method();
case Jupiter::SecureSocket::EncryptionMethod::DTLS1:
return DTLSv1_method();
//return DTLSv1_method();
case Jupiter::SecureSocket::EncryptionMethod::ANY:
return SSLv23_method();
default:
@ -197,7 +200,7 @@ int Jupiter::SecureSocket::peek()
return -1;
Jupiter::Socket::Buffer &buffer = this->getInternalBuffer();
buffer.erase();
int r = SSL_peek(Jupiter::SecureSocket::SSLdata_->handle, buffer.get_str(), this->getBufferSize());
int r = SSL_peek(Jupiter::SecureSocket::SSLdata_->handle, buffer.get_str(), static_cast<int>(this->getBufferSize()));
if (r > 0)
buffer.set_length(r);
return r;
@ -209,7 +212,7 @@ int Jupiter::SecureSocket::recv()
return -1;
Jupiter::Socket::Buffer &buffer = this->getInternalBuffer();
buffer.erase();
int r = SSL_read(Jupiter::SecureSocket::SSLdata_->handle, buffer.get_str(), this->getBufferSize());
int r = SSL_read(Jupiter::SecureSocket::SSLdata_->handle, buffer.get_str(), static_cast<int>(this->getBufferSize()));
if (r > 0)
buffer.set_length(r);
return r;
@ -217,7 +220,7 @@ int Jupiter::SecureSocket::recv()
int Jupiter::SecureSocket::send(const char *data, size_t datalen)
{
return SSL_write(Jupiter::SecureSocket::SSLdata_->handle, data, datalen);
return SSL_write(Jupiter::SecureSocket::SSLdata_->handle, data, static_cast<int>(datalen));
}
bool Jupiter::SecureSocket::initSSL()
@ -250,6 +253,9 @@ bool Jupiter::SecureSocket::initSSL()
ERR_print_errors_fp(stderr);
return false;
}
// Not suppressing this warning; descriptor ideally shouldn't be getting shortened here, even if it's "safe"
// TODO: Can't resolve this without removing OpenSSL usage on Windows; we should ideally use Windows APIs
if (SSL_set_fd(Jupiter::SecureSocket::SSLdata_->handle, this->getDescriptor()) == 0)
{
ERR_print_errors_fp(stderr);

11
src/common/Socket.cpp

@ -39,6 +39,12 @@ bool socketInit = false;
#include "Socket.h"
#include "Functions.h"
/** Narrowing conversions; I just don't want to explicitly static_cast parameters to methods that might vary by platform */
#if defined _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4267)
#endif
void Jupiter::Socket::Buffer::set_length(size_t in_length)
{
this->length = in_length;
@ -698,3 +704,8 @@ Jupiter::Socket::Buffer &Jupiter::Socket::getInternalBuffer() const
{
return Jupiter::Socket::data_->buffer;
}
/** Re-enable warnings */
#if defined _MSC_VER
#pragma warning(pop)
#endif

8
src/include/Jupiter/Jupiter.h

@ -24,7 +24,7 @@
* On anything other than Windows, JUPITER_API is defined as nothing, to prevent compiler errors.
*/
#if defined _WIN32
#if defined(_WIN32) && defined(JUPITER_SHARED_LIB)
#if defined JUPITER_EXPORTS
#define JUPITER_API __declspec(dllexport)
@ -55,11 +55,7 @@
#endif // JUPITER_PLATFORM
#if !defined JUPITER_VERSION_SHORT
#if !defined JUPITER_REVISION
#define JUPITER_REVISION
#endif // JUPITER_REVISION
#define JUPITER_VERSION_SHORT "1.1.0" JUPITER_REVISION
#define JUPITER_VERSION_SHORT "[unversioned]"
#endif // JUPITER_VERSION_SHORT
#define JUPITER_VERSION "Jupiter " JUPITER_VERSION_SHORT " (" JUPITER_PLATFORM " " __DATE__ ")" /** Version of this program at compile time. */

1
src/include/Jupiter/Rehash.h

@ -24,6 +24,7 @@
* @brief Provides a C++ rehash system, which is also accessable through a C interface.
*/
#include <cstddef>
#include "Jupiter.h"
namespace Jupiter

23
src/include/Jupiter/String.hpp

@ -273,7 +273,7 @@ namespace Jupiter
};
#if defined JUPITER_STRING_STRICT_OPERATOR_PLUS
/** String_Loose<T> Addition Operators */
/** String_Strict<T> Addition Operators */
template<typename T> static inline Jupiter::String_Strict<T> operator+(const Jupiter::Readable_String<T> &lhs, const Jupiter::Readable_String<T> &rhs);
template<typename T> static inline Jupiter::String_Strict<T> operator+(const Jupiter::Readable_String<T> &lhs, const T &rhs);
template<typename T> static inline Jupiter::String_Strict<T> operator+(const Jupiter::Readable_String<T> &lhs, const Jupiter::Readable_String<T> &rhs);
@ -561,34 +561,15 @@ namespace Jupiter
/** Definition of a Strict String. */
typedef String_Strict<char> StringS;
/** Definition of a Strict Wide String */
typedef String_Strict<wchar_t> WStringS;
/** Definition of a Loose String. */
typedef String_Loose<char> StringL;
/** Definition of a Loose Wide String */
typedef String_Loose<wchar_t> WStringL;
/** Definition of a String. */
typedef StringL String;
/** Definition of a Wide String */
typedef WStringL WString;
namespace literals
{
namespace literals {
/** String_Strict literals */
inline Jupiter::StringS operator""_jss(const char *str, size_t len) { return Jupiter::StringS(str, len); }
inline Jupiter::WStringS operator""_jwss(const wchar_t *str, size_t len) { return Jupiter::WStringS(str, len); }
/** String_Loose literals */
inline Jupiter::StringL operator""_jsl(const char *str, size_t len) { return Jupiter::StringL(str, len); }
inline Jupiter::WStringL operator""_jwsl(const wchar_t *str, size_t len) { return Jupiter::WStringL(str, len); }
/** String literals */
inline Jupiter::StringS operator""_js(const char *str, size_t len) { return Jupiter::String(str, len); }
inline Jupiter::WStringS operator""_jws(const wchar_t *str, size_t len) { return Jupiter::WString(str, len); }
}
// Carried over from Hash_Table.h for compatibility

4
src/include/Jupiter/String_Type.h

@ -313,10 +313,6 @@ namespace Jupiter
/** Generic String Type */
typedef String_Type<char> StringType;
/** Generic Wide String Type */
typedef String_Type<wchar_t> WStringType;
}
/** Re-enable warning */

Loading…
Cancel
Save