10 #include <boost/exception/diagnostic_information.hpp> 11 #include <pion/error.hpp> 12 #include <pion/http/plugin_server.hpp> 13 #include <pion/http/request.hpp> 14 #include <pion/http/request_reader.hpp> 15 #include <pion/http/response_writer.hpp> 16 #include <pion/http/basic_auth.hpp> 17 #include <pion/http/cookie_auth.hpp> 32 m_services.
add(clean_resource, service_ptr);
34 PION_LOG_INFO(
m_logger,
"Loaded static web service for resource (" << clean_resource <<
")");
41 service_ptr = m_services.
load(clean_resource, service_name);
44 PION_LOG_INFO(
m_logger,
"Loaded web service plug-in for resource (" << clean_resource <<
"): " << service_name);
48 const std::string& name,
const std::string& value)
52 PION_LOG_INFO(
m_logger,
"Set web service option for resource (" 53 << resource <<
"): " << name <<
'=' << value);
58 std::string config_file;
63 std::ifstream config_stream;
64 config_stream.open(config_file.c_str(), std::ios::in);
65 if (! config_stream.is_open())
66 BOOST_THROW_EXCEPTION(
error::open_file() << error::errinfo_file_name(config_name) );
69 http::auth_ptr my_auth_ptr;
71 PARSE_NEWLINE, PARSE_COMMAND, PARSE_RESOURCE, PARSE_VALUE, PARSE_COMMENT, PARSE_USERNAME
72 } parse_state = PARSE_NEWLINE;
73 std::string command_string;
74 std::string resource_string;
75 std::string username_string;
76 std::string value_string;
77 std::string option_name_string;
78 std::string option_value_string;
79 int c = config_stream.get();
81 while (config_stream) {
87 parse_state = PARSE_COMMENT;
88 }
else if (isalpha(c)) {
90 parse_state = PARSE_COMMAND;
92 command_string += tolower(c);
93 }
else if (c !=
'\r' && c !=
'\n') {
94 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
100 if (c ==
' ' || c ==
'\t') {
102 if (command_string==
"path" || command_string==
"auth" || command_string==
"restrict") {
103 value_string.clear();
104 parse_state = PARSE_VALUE;
105 }
else if (command_string==
"service" || command_string==
"option") {
106 resource_string.clear();
107 parse_state = PARSE_RESOURCE;
108 }
else if (command_string==
"user") {
109 username_string.clear();
110 parse_state = PARSE_USERNAME;
112 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
114 }
else if (! isalpha(c)) {
116 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
119 command_string += tolower(c);
125 if (c ==
' ' || c ==
'\t') {
127 if (! resource_string.empty()) {
129 value_string.clear();
130 parse_state = PARSE_VALUE;
132 }
else if (c ==
'\r' || c ==
'\n') {
134 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
137 resource_string += c;
143 if (c ==
' ' || c ==
'\t') {
145 if (! username_string.empty()) {
147 value_string.clear();
148 parse_state = PARSE_VALUE;
150 }
else if (c ==
'\r' || c ==
'\n') {
152 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
155 username_string += c;
161 if (c ==
'\r' || c ==
'\n') {
163 if (value_string.empty()) {
165 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
166 }
else if (command_string ==
"path") {
169 catch (std::exception& e) {
170 PION_LOG_WARN(
m_logger, boost::diagnostic_information(e));
172 }
else if (command_string ==
"auth") {
175 if (value_string ==
"basic"){
178 else if (value_string ==
"cookie"){
183 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
185 }
else if (command_string ==
"restrict") {
189 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
190 else if (value_string.empty())
192 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
193 my_auth_ptr->add_restrict(value_string);
194 }
else if (command_string ==
"user") {
198 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
199 else if (value_string.empty())
201 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
202 my_auth_ptr->add_user(username_string, value_string);
203 }
else if (command_string ==
"service") {
206 }
else if (command_string ==
"option") {
208 std::string::size_type pos = value_string.find(
'=');
209 if (pos == std::string::npos)
210 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
211 option_name_string = value_string.substr(0, pos);
212 option_value_string = value_string.substr(pos + 1);
214 option_value_string);
216 command_string.clear();
217 parse_state = PARSE_NEWLINE;
218 }
else if (c ==
' ' || c ==
'\t') {
220 if (! value_string.empty())
230 if (c ==
'\r' || c ==
'\n')
231 parse_state = PARSE_NEWLINE;
236 c = config_stream.get();
static bool find_config_file(std::string &path_to_file, const std::string &name)
void run(PluginRunFunction run_func)
void add_service(const std::string &resource, http::plugin_service *service_ptr)
static void add_plugin_directory(const std::string &dir)
appends a directory to the plug-in search path
void set_service_option(const std::string &resource, const std::string &name, const std::string &value)
void set_resource(const std::string &str)
sets the URI stem or resource that is bound to the web service
void add(const std::string &plugin_id, PluginType *plugin_object_ptr)
PluginType * load(const std::string &plugin_id, const std::string &plugin_type)
virtual void set_option(const std::string &name, const std::string &value)
void add_resource(const std::string &resource, request_handler_t request_handler)
static std::string strip_trailing_slash(const std::string &str)
exception thrown if a file is not found
exception thrown if we failed to open a file
logger m_logger
primary logging interface used by this class
exception thrown if there is an error parsing a configuration file
void load_service(const std::string &resource, const std::string &service_name)
void load_service_config(const std::string &config_name)
void set_authentication(http::auth_ptr auth)