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/basic_auth.hpp>
15 #include <pion/http/cookie_auth.hpp>
30 m_services.
add(clean_resource, service_ptr);
32 PION_LOG_INFO(
m_logger,
"Loaded static web service for resource (" << clean_resource <<
")");
39 service_ptr = m_services.
load(clean_resource, service_name);
42 PION_LOG_INFO(
m_logger,
"Loaded web service plug-in for resource (" << clean_resource <<
"): " << service_name);
46 const std::string& name,
const std::string& value)
50 PION_LOG_INFO(
m_logger,
"Set web service option for resource ("
51 << resource <<
"): " << name <<
'=' << value);
56 std::string config_file;
61 std::ifstream config_stream;
62 config_stream.open(config_file.c_str(), std::ios::in);
63 if (! config_stream.is_open())
64 BOOST_THROW_EXCEPTION(
error::open_file() << error::errinfo_file_name(config_name) );
67 http::auth_ptr my_auth_ptr;
69 PARSE_NEWLINE, PARSE_COMMAND, PARSE_RESOURCE, PARSE_VALUE, PARSE_COMMENT, PARSE_USERNAME
70 } parse_state = PARSE_NEWLINE;
71 std::string command_string;
72 std::string resource_string;
73 std::string username_string;
74 std::string value_string;
75 std::string option_name_string;
76 std::string option_value_string;
77 int c = config_stream.get();
79 while (config_stream) {
85 parse_state = PARSE_COMMENT;
86 }
else if (isalpha(c)) {
88 parse_state = PARSE_COMMAND;
90 command_string += tolower(c);
91 }
else if (c !=
'\r' && c !=
'\n') {
92 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
98 if (c ==
' ' || c ==
'\t') {
100 if (command_string==
"path" || command_string==
"auth" || command_string==
"restrict") {
101 value_string.clear();
102 parse_state = PARSE_VALUE;
103 }
else if (command_string==
"service" || command_string==
"option") {
104 resource_string.clear();
105 parse_state = PARSE_RESOURCE;
106 }
else if (command_string==
"user") {
107 username_string.clear();
108 parse_state = PARSE_USERNAME;
110 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
112 }
else if (! isalpha(c)) {
114 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
117 command_string += tolower(c);
123 if (c ==
' ' || c ==
'\t') {
125 if (! resource_string.empty()) {
127 value_string.clear();
128 parse_state = PARSE_VALUE;
130 }
else if (c ==
'\r' || c ==
'\n') {
132 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
135 resource_string += c;
141 if (c ==
' ' || c ==
'\t') {
143 if (! username_string.empty()) {
145 value_string.clear();
146 parse_state = PARSE_VALUE;
148 }
else if (c ==
'\r' || c ==
'\n') {
150 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
153 username_string += c;
159 if (c ==
'\r' || c ==
'\n') {
161 if (value_string.empty()) {
163 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
164 }
else if (command_string ==
"path") {
167 catch (std::exception& e) {
168 PION_LOG_WARN(
m_logger, boost::diagnostic_information(e));
170 }
else if (command_string ==
"auth") {
173 if (value_string ==
"basic"){
176 else if (value_string ==
"cookie"){
181 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
183 }
else if (command_string ==
"restrict") {
187 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
188 else if (value_string.empty())
190 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
191 my_auth_ptr->add_restrict(value_string);
192 }
else if (command_string ==
"user") {
196 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
197 else if (value_string.empty())
199 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
200 my_auth_ptr->add_user(username_string, value_string);
201 }
else if (command_string ==
"service") {
204 }
else if (command_string ==
"option") {
206 std::string::size_type pos = value_string.find(
'=');
207 if (pos == std::string::npos)
208 BOOST_THROW_EXCEPTION(
error::bad_config() << error::errinfo_file_name(config_name) );
209 option_name_string = value_string.substr(0, pos);
210 option_value_string = value_string.substr(pos + 1);
212 option_value_string);
214 command_string.clear();
215 parse_state = PARSE_NEWLINE;
216 }
else if (c ==
' ' || c ==
'\t') {
218 if (! value_string.empty())
228 if (c ==
'\r' || c ==
'\n')
229 parse_state = PARSE_NEWLINE;
234 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)