Skip to content

Commit

Permalink
Merge pull request #1651 from kripton/libmicrohttpd_compatibility
Browse files Browse the repository at this point in the history
Libmicrohttpd compatibility
  • Loading branch information
peternewman authored Jul 17, 2020
2 parents 4e5395a + cd6f69d commit 5d88293
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 44 deletions.
17 changes: 13 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ matrix:
- moreutils
- os: linux
dist: xenial
env: TASK='codespell'
env:
- TASK='codespell'
- PATH=/opt/python/3.7.1/bin:$PATH
addons:
apt:
packages:
Expand Down Expand Up @@ -312,16 +314,23 @@ install:
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then pip install --user numpy; fi
- if [ "$TASK" = "coverage" ]; then pip install --user cpp-coveralls; fi
- if [ "$TASK" = "flake8" ]; then pip install --user flake8; fi
- if [ "$TASK" = "codespell" ]; then pip install --user git+https://github.com/codespell-project/codespell.git; fi
- if [ "$TASK" = "codespell" ]; then pip3 install --user git+https://github.com/codespell-project/codespell.git; fi
- if [ "$TASK" = "jshint" ]; then npm install -g grunt-cli; fi

before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
#Fix permissions for unbound (and possibly others)
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then if [ ! -d /usr/local/sbin ]; then sudo mkdir -p /usr/local/sbin && sudo chown -R $(whoami) /usr/local/sbin; fi; fi
#Add a missing gnupg folder
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then if [ ! -d /usr/local/Cellar/gnupg2/2.2.5 ]; then sudo mkdir -p /usr/local/Cellar/gnupg2/2.2.5 && sudo chown -R $(whoami) /usr/local/Cellar/gnupg2/2.2.5; fi; fi
#Fix a broken homebrew libtool install
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew reinstall libtool; fi
#Fix a broken homebrew gnupg install
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew uninstall --ignore-dependencies gnupg; fi
#Fix a broken homebrew python upgrade - see https://github.com/Homebrew/homebrew-core/issues/26358
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew upgrade python || true; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then if [ ! -d /usr/local/sbin ]; then sudo mkdir -p /usr/local/sbin && sudo chown -R $(whoami) /usr/local/sbin; fi; fi
#Having fixed everything up, update to the bleeding edge
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
#Install build dependencies
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install ccache bison flex liblo libmicrohttpd; fi # ossp-uuid, homebrew/python/numpy and libusb already present
- if [ "$TRAVIS_OS_NAME" == "osx" -a "$LIBFTDI" != "1" ]; then brew install libftdi0; fi # install libftdi0
- if [ "$TRAVIS_OS_NAME" == "osx" -a "$LIBFTDI" == "1" ]; then brew install libftdi; fi # install the latest libftdi
Expand Down
2 changes: 1 addition & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ x/y/2019 ola-0.10.8
* Renamed RESONSE_INVALID_DESTINATION(sic) to RESPONSE_INVALID_DESTINATION in
the ArduinoWidget code
* Fix compatibility with ncurses 6
* Fix compatibility with Protobuf 3.7 and newer (tested with up to 3.11.4)
* Fix compatibility with Protobuf 3.7 and newer (tested with up to 3.12.2)
* Rename CircularDepdendancyException(sic) to CircularDependencyException in
the Python RDM Test code, also the relevant comments

Expand Down
97 changes: 59 additions & 38 deletions common/http/HTTPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ const char HTTPServer::CONTENT_TYPE_OCT[] = "application/octet-stream";
* @param key the header name
* @param value the header value
*/
static int AddHeaders(void *cls, OLA_UNUSED enum MHD_ValueKind kind,
const char *key, const char *value) {
static MHD_RESULT AddHeaders(void *cls, OLA_UNUSED enum MHD_ValueKind kind,
const char *key, const char *value) {
HTTPRequest *request = static_cast<HTTPRequest*>(cls);
string key_string = key;
string value_string = value;
Expand All @@ -110,11 +110,12 @@ static int AddHeaders(void *cls, OLA_UNUSED enum MHD_ValueKind kind,
* @param off the offset of the data
* @param size the number of bytes available
*/
int IteratePost(void *request_cls, OLA_UNUSED enum MHD_ValueKind kind,
const char *key, OLA_UNUSED const char *filename,
OLA_UNUSED const char *content_type,
OLA_UNUSED const char *transfer_encoding, const char *data,
OLA_UNUSED uint64_t off, OLA_UNUSED size_t size) {
MHD_RESULT IteratePost(void *request_cls, OLA_UNUSED enum MHD_ValueKind kind,
const char *key, OLA_UNUSED const char *filename,
OLA_UNUSED const char *content_type,
OLA_UNUSED const char *transfer_encoding,
const char *data,
OLA_UNUSED uint64_t off, OLA_UNUSED size_t size) {
// libmicrohttpd has a bug where the size isn't set correctly.
HTTPRequest *request = static_cast<HTTPRequest*>(request_cls);
string value(data);
Expand All @@ -129,22 +130,23 @@ int IteratePost(void *request_cls, OLA_UNUSED enum MHD_ValueKind kind,
* This sets up HTTPRequest & HTTPResponse objects and then calls
* DispatchRequest.
*/
static int HandleRequest(void *http_server_ptr,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **ptr) {
static MHD_RESULT HandleRequest(void *http_server_ptr,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **ptr) {
HTTPServer *http_server = static_cast<HTTPServer*>(http_server_ptr);
HTTPRequest *request;

// on the first call ptr is null
if (*ptr == NULL) {
request = new HTTPRequest(url, method, version, connection);
if (!request)
if (!request) {
return MHD_NO;
}

if (!request->Init()) {
delete request;
Expand All @@ -156,14 +158,16 @@ static int HandleRequest(void *http_server_ptr,

request = static_cast<HTTPRequest*>(*ptr);

if (request->InFlight())
if (request->InFlight()) {
// don't dispatch more than once
return MHD_YES;
}

if (request->Method() == MHD_HTTP_METHOD_GET) {
HTTPResponse *response = new HTTPResponse(connection);
request->SetInFlight();
return http_server->DispatchRequest(request, response);
return static_cast<MHD_RESULT>(
http_server->DispatchRequest(request, response));

} else if (request->Method() == MHD_HTTP_METHOD_POST) {
if (*upload_data_size != 0) {
Expand All @@ -173,7 +177,8 @@ static int HandleRequest(void *http_server_ptr,
}
request->SetInFlight();
HTTPResponse *response = new HTTPResponse(connection);
return http_server->DispatchRequest(request, response);
return static_cast<MHD_RESULT>(
http_server->DispatchRequest(request, response));
}
return MHD_NO;
}
Expand All @@ -188,8 +193,9 @@ void RequestCompleted(void*,
struct MHD_Connection*,
void **request_cls,
enum MHD_RequestTerminationCode) {
if (!request_cls)
if (!request_cls) {
return;
}

delete static_cast<HTTPRequest*>(*request_cls);
*request_cls = NULL;
Expand Down Expand Up @@ -235,8 +241,9 @@ bool HTTPRequest::Init() {
* @brief Cleanup this request object
*/
HTTPRequest::~HTTPRequest() {
if (m_processor)
if (m_processor) {
MHD_destroy_post_processor(m_processor);
}
}


Expand Down Expand Up @@ -286,10 +293,11 @@ void HTTPRequest::ProcessPostData(const char *data, size_t *data_size) {
const string HTTPRequest::GetHeader(const string &key) const {
map<string, string>::const_iterator iter = m_headers.find(key);

if (iter == m_headers.end())
if (iter == m_headers.end()) {
return "";
else
} else {
return iter->second;
}
}


Expand All @@ -302,10 +310,11 @@ const string HTTPRequest::GetParameter(const string &key) const {
const char *value = MHD_lookup_connection_value(m_connection,
MHD_GET_ARGUMENT_KIND,
key.c_str());
if (value)
if (value) {
return string(value);
else
} else {
return string();
}
}

/**
Expand Down Expand Up @@ -339,10 +348,11 @@ bool HTTPRequest::CheckParameterExists(const string &key) const {
const string HTTPRequest::GetPostParameter(const string &key) const {
map<string, string>::const_iterator iter = m_post_params.find(key);

if (iter == m_post_params.end())
if (iter == m_post_params.end()) {
return "";
else
} else {
return iter->second;
}
}


Expand Down Expand Up @@ -386,10 +396,11 @@ int HTTPResponse::SendJson(const JsonValue &json) {
static_cast<void*>(const_cast<char*>(output.data())),
output.length());
HeadersMultiMap::const_iterator iter;
for (iter = m_headers.begin(); iter != m_headers.end(); ++iter)
for (iter = m_headers.begin(); iter != m_headers.end(); ++iter) {
MHD_add_response_header(response,
iter->first.c_str(),
iter->second.c_str());
}
int ret = MHD_queue_response(m_connection, m_status_code, response);
MHD_destroy_response(response);
return ret;
Expand All @@ -405,10 +416,11 @@ int HTTPResponse::Send() {
struct MHD_Response *response = HTTPServer::BuildResponse(
static_cast<void*>(const_cast<char*>(m_data.data())),
m_data.length());
for (iter = m_headers.begin(); iter != m_headers.end(); ++iter)
for (iter = m_headers.begin(); iter != m_headers.end(); ++iter) {
MHD_add_response_header(response,
iter->first.c_str(),
iter->second.c_str());
}
int ret = MHD_queue_response(m_connection, m_status_code, response);
MHD_destroy_response(response);
return ret;
Expand Down Expand Up @@ -443,8 +455,9 @@ HTTPServer::~HTTPServer() {
MHD_stop_daemon(m_httpd);

map<string, BaseHTTPCallback*>::const_iterator iter;
for (iter = m_handlers.begin(); iter != m_handlers.end(); ++iter)
for (iter = m_handlers.begin(); iter != m_handlers.end(); ++iter) {
delete iter->second;
}

if (m_default_handler) {
delete m_default_handler;
Expand Down Expand Up @@ -617,17 +630,20 @@ int HTTPServer::DispatchRequest(const HTTPRequest *request,
map<string, BaseHTTPCallback*>::iterator iter =
m_handlers.find(request->Url());

if (iter != m_handlers.end())
if (iter != m_handlers.end()) {
return iter->second->Run(request, response);
}

map<string, static_file_info>::iterator file_iter =
m_static_content.find(request->Url());

if (file_iter != m_static_content.end())
if (file_iter != m_static_content.end()) {
return ServeStaticContent(&(file_iter->second), response);
}

if (m_default_handler)
if (m_default_handler) {
return m_default_handler->Run(request, response);
}

return ServeNotFound(response);
}
Expand All @@ -642,8 +658,9 @@ int HTTPServer::DispatchRequest(const HTTPRequest *request,
bool HTTPServer::RegisterHandler(const string &path,
BaseHTTPCallback *handler) {
map<string, BaseHTTPCallback*>::const_iterator iter = m_handlers.find(path);
if (iter != m_handlers.end())
if (iter != m_handlers.end()) {
return false;
}
pair<string, BaseHTTPCallback*> pair(path, handler);
m_handlers.insert(pair);
return true;
Expand Down Expand Up @@ -678,8 +695,9 @@ bool HTTPServer::RegisterFile(const std::string &path,
map<string, static_file_info>::const_iterator file_iter = (
m_static_content.find(path));

if (file_iter != m_static_content.end())
if (file_iter != m_static_content.end()) {
return false;
}

static_file_info file_info;
file_info.file_path = file;
Expand All @@ -706,13 +724,15 @@ void HTTPServer::RegisterDefaultHandler(BaseHTTPCallback *handler) {
*/
void HTTPServer::Handlers(vector<string> *handlers) const {
map<string, BaseHTTPCallback*>::const_iterator iter;
for (iter = m_handlers.begin(); iter != m_handlers.end(); ++iter)
for (iter = m_handlers.begin(); iter != m_handlers.end(); ++iter) {
handlers->push_back(iter->first);
}

map<string, static_file_info>::const_iterator file_iter;
for (file_iter = m_static_content.begin();
file_iter != m_static_content.end(); ++file_iter)
file_iter != m_static_content.end(); ++file_iter) {
handlers->push_back(file_iter->first);
}
}

/**
Expand Down Expand Up @@ -806,10 +826,11 @@ int HTTPServer::ServeStaticContent(static_file_info *file_info,
struct MHD_Response *mhd_response = BuildResponse(static_cast<void*>(data),
length);

if (!file_info->content_type.empty())
if (!file_info->content_type.empty()) {
MHD_add_response_header(mhd_response,
MHD_HTTP_HEADER_CONTENT_TYPE,
file_info->content_type.c_str());
}

int ret = MHD_queue_response(response->Connection(),
MHD_HTTP_OK,
Expand Down
2 changes: 1 addition & 1 deletion include/ola/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
#define OLA_WARN OLA_LOG(ola::OLA_LOG_WARN)

/**
* Provide a stream to log an infomational message.
* Provide a stream to log an informational message.
* @code
* OLA_INFO << "Reading configs from " << config_dir;
* @endcode
Expand Down
11 changes: 11 additions & 0 deletions include/ola/http/HTTPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
#include <string>
#include <vector>

// Beginning with v0.9.71, libmicrohttpd changed the return type of most
// functions from int to enum MHD_Result
// https://git.gnunet.org/gnunet.git/tree/src/include/gnunet_mhd_compat.h
// proposes to define a constant for the return type so it works well
// with all versions of libmicrohttpd
#if MHD_VERSION >= 0x00097002
#define MHD_RESULT enum MHD_Result
#else
#define MHD_RESULT int
#endif

namespace ola {
namespace http {

Expand Down

0 comments on commit 5d88293

Please sign in to comment.