aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Kropf <josh@slashdev.ca>2007-07-30 17:04:16 (GMT)
committerJosh Kropf <josh@slashdev.ca>2007-07-30 17:04:16 (GMT)
commit805292d633f3a035fb3a7701a65d52109cc9696a (patch)
tree5b8501383e4d0132d53c91d0270125dbe1c15dc8
parent6d82be4fca171d8591105747f3cce093f6c52bdc (diff)
downloadecho-server-805292d633f3a035fb3a7701a65d52109cc9696a.zip
echo-server-805292d633f3a035fb3a7701a65d52109cc9696a.tar.gz
echo-server-805292d633f3a035fb3a7701a65d52109cc9696a.tar.bz2
Bug fixes
Project structure changes Added modules directory with common makefile
-rw-r--r--Makefile7
-rw-r--r--action/action.cpp48
-rw-r--r--action/action.h (renamed from http/action.h)32
-rw-r--r--common/config.cpp4
-rw-r--r--common/config.h10
-rw-r--r--fcgi/dispatcher.cpp (renamed from http/dispatcher.cpp)7
-rw-r--r--fcgi/fcgi_io.cpp (renamed from http/fcgi_io.cpp)1
-rw-r--r--fcgi/fcgi_io.h (renamed from http/fcgi_io.h)0
-rw-r--r--http/action.cpp36
-rw-r--r--modules/Makefile11
10 files changed, 100 insertions, 56 deletions
diff --git a/Makefile b/Makefile
index cd55c0b..7420683 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,7 @@
-CXXFLAGS=-Wall -g
+CXXFLAGS=-Wall -g -I./common -I./action
LDFLAGS=-lstdc++ -lfcgi++ -lcgicc -lconfuse
-OBJS=common/config.o http/action.o http/fcgi_io.o http/dispatcher.o
-TARGETS=http/dispatcher.fcgi
+OBJS=common/config.o action/action.o fcgi/fcgi_io.o fcgi/dispatcher.o
all: dispatcher.fcgi
@@ -13,4 +12,4 @@ dispatcher.fcgi: $(OBJS)
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
- rm -f common/*.o http/*.o
+ rm -f common/*.o action/*.o fcgi/*.o dispatcher.fcgi
diff --git a/action/action.cpp b/action/action.cpp
new file mode 100644
index 0000000..adf0e7c
--- /dev/null
+++ b/action/action.cpp
@@ -0,0 +1,48 @@
+#include "action.h"
+#include "config.h"
+#include <dlfcn.h>
+
+using namespace std;
+using namespace action;
+
+ActionMapping* ActionMapping::instance()
+{
+ static ActionMapping instance;
+ return &instance;
+}
+
+ActionMapping::ActionMapping()
+{
+ void* handle;
+ void* func;
+
+ Config config;
+ vector<string> v = config.modules();
+ for (vector<string>::iterator i = v.begin(); i != v.end(); i++) {
+ // open modules dynamic library
+ handle = dlopen((*i).c_str(), RTLD_NOW);
+
+ if (!handle) throw action_error(dlerror());
+
+ // run the auto mapping function
+ void* func = dlsym(handle, "DL_MAPPING_SYMBOL");
+ if (!func) throw action_error(dlerror());
+
+ ((void (*)())func)();
+ }
+}
+
+void ActionMapping::reg(string key, create_ptr ptr)
+{
+ _mapping[key] = ptr;
+}
+
+bool ActionMapping::contains(string key) const
+{
+ return _mapping.count(key) != 0;
+}
+
+Action* ActionMapping::get(string key)
+{
+ return (*_mapping[key])();
+}
diff --git a/http/action.h b/action/action.h
index cb9ff92..ee6e5ea 100644
--- a/http/action.h
+++ b/action/action.h
@@ -1,12 +1,37 @@
#include <cgicc/Cgicc.h>
#include <iostream>
+#include <exception>
#include <string>
#include <map>
#ifndef _ACTION_H_
#define _ACTION_H_
-namespace http {
+/**
+ * These macros must be used by action modules to define the
+ * auto-mapping function. This function is called by the
+ * action mapping class for each dynamic library listed
+ * in the configuration file.
+ */
+#define DL_MAPPING_SYMBOL __action_mapping
+#define BEGIN_DL_MAPPING extern "C" void DL_MAPPING_SYMBOL() {
+#define END_DL_MAPPING }
+
+namespace action {
+
+/**
+ * Exception raised when an error occured loading an action module
+ */
+class action_error: public std::exception
+{
+private:
+ const char* _msg;
+
+public:
+ action_error(const char* msg) { _msg = msg; }
+
+ virtual const char* what() { return _msg; }
+};
/**
* Interface for all dispatchable actions. An action handles an http request
@@ -17,11 +42,6 @@ public:
virtual ~Action();
/**
- * Creates the action (usually just new's the class)
- */
- virtual Action* create() = 0;
-
- /**
* Executes the action. Actions are passed the cgicc object containing request
* parameters/information and must format a standard HTTP response using the given
* output stream.
diff --git a/common/config.cpp b/common/config.cpp
index 07eba5c..2566d89 100644
--- a/common/config.cpp
+++ b/common/config.cpp
@@ -1,4 +1,4 @@
-#include "Config.h"
+#include "config.h"
Config::Config()
{
@@ -32,7 +32,7 @@ vector<string> Config::modules() const
string root = cfg_getstr(actions, "root");
for (int i=cfg_size(actions, "modules")-1; i>=0; i--) {
- *modules.end() = root + cfg_getnstr(actions, "modules", i);
+ modules.push_back(root + cfg_getnstr(actions, "modules", i));
}
}
diff --git a/common/config.h b/common/config.h
index 6b27d20..9966a7e 100644
--- a/common/config.h
+++ b/common/config.h
@@ -3,14 +3,16 @@
#include <exception>
#include <confuse.h>
-#ifndef CONFIG_H_
-#define CONFIG_H_
+#ifndef _CONFIG_H_
+#define _CONFIG_H_
#define CONFIG_FILE "/etc/echo.conf"
using namespace std;
-
+/**
+ * Exception raised if a configuration parse error occured
+ */
class config_error: public exception
{
public:
@@ -47,4 +49,4 @@ public:
vector<string> modules() const;
};
-#endif /*CONFIG_H_*/
+#endif
diff --git a/http/dispatcher.cpp b/fcgi/dispatcher.cpp
index 8f72365..a2c1009 100644
--- a/http/dispatcher.cpp
+++ b/fcgi/dispatcher.cpp
@@ -7,12 +7,12 @@
using namespace std;
using namespace cgicc;
-using namespace http;
+using namespace action;
int main (void)
{
ActionMapping* mapping = ActionMapping::instance();
-
+
FCGX_Request request;
FCGX_Init();
@@ -22,7 +22,7 @@ int main (void)
fcgi_io io(request);
Cgicc cgi(&io);
- Action* action;
+ Action* action = NULL;
// script name is the full path of the request
string script = cgi.getEnvironment().getScriptName();
@@ -38,6 +38,7 @@ int main (void)
if (end == string::npos) end = script.size();
string key = script.substr(begin, end-begin);
+
if (mapping->contains(key)) {
action = mapping->get(key);
}
diff --git a/http/fcgi_io.cpp b/fcgi/fcgi_io.cpp
index 9b1ef7e..15a2d09 100644
--- a/http/fcgi_io.cpp
+++ b/fcgi/fcgi_io.cpp
@@ -18,4 +18,3 @@ fcgi_io::fcgi_io(FCGX_Request& request)
_env[s.substr(0, i)] = s.substr(i + 1);
}
}
-
diff --git a/http/fcgi_io.h b/fcgi/fcgi_io.h
index 1eba173..1eba173 100644
--- a/http/fcgi_io.h
+++ b/fcgi/fcgi_io.h
diff --git a/http/action.cpp b/http/action.cpp
deleted file mode 100644
index 7c854fd..0000000
--- a/http/action.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-#include "action.h"
-#include "Config.h"
-#include <fstream>
-
-using namespace http;
-using namespace std;
-
-ActionMapping* ActionMapping::instance()
-{
- static ActionMapping instance;
- return &instance;
-}
-
-ActionMapping::ActionMapping()
-{
- Config config;
- vector<string> v = config.modules();
- while (const_iterator i = v.begin(); i != v.end(); ++i) {
- // TODO load dynamic library
- }
-}
-
-void ActionMapping::reg(string key, create_ptr ptr)
-{
- _mapping[key] = ptr;
-}
-
-bool ActionMapping::contains(string key) const
-{
- return _mapping.count(key) != 0;
-}
-
-Action* ActionMapping::get(string key)
-{
- return (*_mapping[key])();
-}
diff --git a/modules/Makefile b/modules/Makefile
new file mode 100644
index 0000000..0838804
--- /dev/null
+++ b/modules/Makefile
@@ -0,0 +1,11 @@
+CXXFLAGS=-Wall -fpic -g -I../../action
+LDFLAGS=-shared
+
+$(TARGET): $(OBJS)
+ $(CXX) $(LDFLAGS) -o $@ $?
+
+%.o: %.cpp
+ $(CXX) $(CXXFLAGS) -c $< -o $@
+
+clean:
+ rm -f $(TARGET) *.o