2014-07-25 10:29:08 -06:00
|
|
|
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
2014-03-03 15:07:58 -07:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
2014-07-25 10:29:08 -06:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
|
|
// documentation and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Andrey N. Sabelnikov nor the
|
|
|
|
// names of its contributors may be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
2014-07-23 07:03:52 -06:00
|
|
|
//
|
2014-07-25 10:29:08 -06:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
|
|
|
|
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2014-07-23 07:03:52 -06:00
|
|
|
//
|
2014-07-25 10:29:08 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
|
|
|
|
#ifndef _HTTP_SERVER_H_
|
|
|
|
#define _HTTP_SERVER_H_
|
|
|
|
|
2016-12-08 18:28:28 -07:00
|
|
|
#include <boost/optional/optional.hpp>
|
2014-03-03 15:07:58 -07:00
|
|
|
#include <string>
|
|
|
|
#include "net_utils_base.h"
|
|
|
|
#include "to_nonconst_iterator.h"
|
2016-12-08 18:28:28 -07:00
|
|
|
#include "http_auth.h"
|
2014-03-03 15:07:58 -07:00
|
|
|
#include "http_base.h"
|
|
|
|
|
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
|
|
|
#undef MONERO_DEFAULT_LOG_CATEGORY
|
|
|
|
#define MONERO_DEFAULT_LOG_CATEGORY "net.http"
|
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
namespace epee
|
|
|
|
{
|
|
|
|
namespace net_utils
|
|
|
|
{
|
|
|
|
namespace http
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
|
|
|
struct http_server_config
|
|
|
|
{
|
|
|
|
std::string m_folder;
|
2017-10-24 07:26:05 -06:00
|
|
|
std::vector<std::string> m_access_control_origins;
|
2016-12-29 23:47:57 -07:00
|
|
|
boost::optional<login> m_user;
|
2014-03-03 15:07:58 -07:00
|
|
|
critical_section m_lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
2014-05-25 11:06:40 -06:00
|
|
|
template<class t_connection_context = net_utils::connection_context_base>
|
2014-03-03 15:07:58 -07:00
|
|
|
class simple_http_connection_handler
|
|
|
|
{
|
|
|
|
public:
|
2014-05-25 11:06:40 -06:00
|
|
|
typedef t_connection_context connection_context;//t_connection_context net_utils::connection_context_base connection_context;
|
2014-03-03 15:07:58 -07:00
|
|
|
typedef http_server_config config_type;
|
|
|
|
|
2018-04-17 03:30:18 -06:00
|
|
|
simple_http_connection_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context);
|
2014-03-03 15:07:58 -07:00
|
|
|
virtual ~simple_http_connection_handler(){}
|
|
|
|
|
2014-05-25 11:06:40 -06:00
|
|
|
bool release_protocol()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
|
|
|
virtual bool thread_init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool thread_deinit()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool after_init_connection()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
virtual bool handle_recv(const void* ptr, size_t cb);
|
|
|
|
virtual bool handle_request(const http::http_request_info& query_info, http_response_info& response);
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum machine_state{
|
|
|
|
http_state_retriving_comand_line,
|
|
|
|
http_state_retriving_header,
|
|
|
|
http_state_retriving_body,
|
|
|
|
http_state_connection_close,
|
|
|
|
http_state_error
|
|
|
|
};
|
|
|
|
|
|
|
|
enum body_transfer_type{
|
|
|
|
http_body_transfer_chunked,
|
|
|
|
http_body_transfer_measure,//mean "Content-Length" valid
|
|
|
|
http_body_transfer_chunked_instead_measure,
|
|
|
|
http_body_transfer_connection_close,
|
|
|
|
http_body_transfer_multipart,
|
|
|
|
http_body_transfer_undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
bool handle_buff_in(std::string& buf);
|
|
|
|
|
|
|
|
bool analize_cached_request_header_and_invoke_state(size_t pos);
|
|
|
|
|
|
|
|
bool handle_invoke_query_line();
|
|
|
|
bool parse_cached_header(http_header_info& body_info, const std::string& m_cache_to_process, size_t pos);
|
|
|
|
std::string::size_type match_end_of_header(const std::string& buf);
|
|
|
|
bool get_len_from_content_lenght(const std::string& str, size_t& len);
|
|
|
|
bool handle_retriving_query_body();
|
|
|
|
bool handle_query_measure();
|
|
|
|
bool set_ready_state();
|
|
|
|
bool slash_to_back_slash(std::string& str);
|
|
|
|
std::string get_file_mime_tipe(const std::string& path);
|
|
|
|
std::string get_response_header(const http_response_info& response);
|
|
|
|
|
|
|
|
//major function
|
|
|
|
inline bool handle_request_and_send_response(const http::http_request_info& query_info);
|
|
|
|
|
|
|
|
|
|
|
|
std::string get_not_found_response_body(const std::string& URI);
|
|
|
|
|
|
|
|
std::string m_root_path;
|
|
|
|
std::string m_cache;
|
|
|
|
machine_state m_state;
|
|
|
|
body_transfer_type m_body_transfer_type;
|
|
|
|
bool m_is_stop_handling;
|
|
|
|
http::http_request_info m_query_info;
|
|
|
|
size_t m_len_summary, m_len_remain;
|
|
|
|
config_type& m_config;
|
|
|
|
bool m_want_close;
|
2018-05-26 13:17:15 -06:00
|
|
|
size_t m_newlines;
|
2014-03-03 15:07:58 -07:00
|
|
|
protected:
|
|
|
|
i_service_endpoint* m_psnd_hndlr;
|
2018-04-17 03:30:18 -06:00
|
|
|
t_connection_context& m_conn_context;
|
2014-03-03 15:07:58 -07:00
|
|
|
};
|
|
|
|
|
2014-05-25 11:06:40 -06:00
|
|
|
template<class t_connection_context>
|
2014-03-03 15:07:58 -07:00
|
|
|
struct i_http_server_handler
|
|
|
|
{
|
|
|
|
virtual ~i_http_server_handler(){}
|
2014-05-25 11:06:40 -06:00
|
|
|
virtual bool handle_http_request(const http_request_info& query_info,
|
|
|
|
http_response_info& response,
|
|
|
|
t_connection_context& m_conn_context) = 0;
|
|
|
|
virtual bool init_server_thread(){return true;}
|
2014-03-03 15:07:58 -07:00
|
|
|
virtual bool deinit_server_thread(){return true;}
|
|
|
|
};
|
|
|
|
|
2014-05-25 11:06:40 -06:00
|
|
|
template<class t_connection_context>
|
2014-03-03 15:07:58 -07:00
|
|
|
struct custum_handler_config: public http_server_config
|
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
i_http_server_handler<t_connection_context>* m_phandler;
|
2017-12-21 04:45:01 -07:00
|
|
|
std::function<void(size_t, uint8_t*)> rng;
|
2014-03-03 15:07:58 -07:00
|
|
|
};
|
|
|
|
|
2014-05-25 11:06:40 -06:00
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
2014-03-20 05:46:11 -06:00
|
|
|
|
2014-05-25 11:06:40 -06:00
|
|
|
template<class t_connection_context = net_utils::connection_context_base>
|
2014-03-20 05:46:11 -06:00
|
|
|
class http_custom_handler: public simple_http_connection_handler<t_connection_context>
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-03-20 05:46:11 -06:00
|
|
|
typedef custum_handler_config<t_connection_context> config_type;
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2014-05-25 11:06:40 -06:00
|
|
|
http_custom_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context)
|
2018-04-17 03:30:18 -06:00
|
|
|
: simple_http_connection_handler<t_connection_context>(psnd_hndlr, config, conn_context),
|
2014-05-25 11:06:40 -06:00
|
|
|
m_config(config),
|
2017-12-21 04:45:01 -07:00
|
|
|
m_auth(m_config.m_user ? http_server_auth{*m_config.m_user, config.rng} : http_server_auth{})
|
2014-03-03 15:07:58 -07:00
|
|
|
{}
|
|
|
|
inline bool handle_request(const http_request_info& query_info, http_response_info& response)
|
|
|
|
{
|
|
|
|
CHECK_AND_ASSERT_MES(m_config.m_phandler, false, "m_config.m_phandler is NULL!!!!");
|
2016-12-08 18:28:28 -07:00
|
|
|
|
|
|
|
const auto auth_response = m_auth.get_response(query_info);
|
|
|
|
if (auth_response)
|
|
|
|
{
|
|
|
|
response = std::move(*auth_response);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
//fill with default values
|
|
|
|
response.m_mime_tipe = "text/plain";
|
|
|
|
response.m_response_code = 200;
|
|
|
|
response.m_response_comment = "OK";
|
|
|
|
response.m_body.clear();
|
2017-10-24 07:26:05 -06:00
|
|
|
|
2018-04-17 03:30:18 -06:00
|
|
|
return m_config.m_phandler->handle_http_request(query_info, response, this->m_conn_context);
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool thread_init()
|
|
|
|
{
|
2020-02-14 16:35:53 -07:00
|
|
|
return m_config.m_phandler->init_server_thread();
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool thread_deinit()
|
|
|
|
{
|
|
|
|
return m_config.m_phandler->deinit_server_thread();
|
|
|
|
}
|
2014-05-25 11:06:40 -06:00
|
|
|
void handle_qued_callback()
|
|
|
|
{}
|
2014-03-03 15:07:58 -07:00
|
|
|
bool after_init_connection()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
//simple_http_connection_handler::config_type m_stub_config;
|
|
|
|
config_type& m_config;
|
2016-12-29 23:47:57 -07:00
|
|
|
http_server_auth m_auth;
|
2014-03-03 15:07:58 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "http_protocol_handler.inl"
|
|
|
|
|
|
|
|
#endif //_HTTP_SERVER_H_
|