| 1 | #ifndef _XMLRPCHTTPD_H_
|
|---|
| 2 | #define _XMLRPCHTTPD_H_
|
|---|
| 3 |
|
|---|
| 4 | #define XMLRPCHTTPD_VERSION 0x0100
|
|---|
| 5 |
|
|---|
| 6 | #pragma warning(disable:4018 4503 4530 4786)
|
|---|
| 7 | #include <sys/types.h>
|
|---|
| 8 | #ifdef _WIN32
|
|---|
| 9 | #include <winsock2.h>
|
|---|
| 10 | #include <process.h>
|
|---|
| 11 | #include <io.h>
|
|---|
| 12 | #pragma comment(lib, "ws2_32.lib")
|
|---|
| 13 | #else
|
|---|
| 14 | #include <sys/socket.h>
|
|---|
| 15 | #include <netinet/in.h>
|
|---|
| 16 | #include <arpa/inet.h>
|
|---|
| 17 | #include <netdb.h>
|
|---|
| 18 | #include <sys/uio.h>
|
|---|
| 19 | #include <unistd.h>
|
|---|
| 20 | #endif
|
|---|
| 21 | #include <sstream>
|
|---|
| 22 |
|
|---|
| 23 | #include "XmlRpcUtils.h"
|
|---|
| 24 |
|
|---|
| 25 | namespace XmlRpc {
|
|---|
| 26 |
|
|---|
| 27 | class XmlRpcHttpd {
|
|---|
| 28 | public:
|
|---|
| 29 | typedef struct {
|
|---|
| 30 | std::string name;
|
|---|
| 31 | unsigned long size;
|
|---|
| 32 | bool isdir;
|
|---|
| 33 | struct tm date;
|
|---|
| 34 | } ListInfo;
|
|---|
| 35 | typedef struct {
|
|---|
| 36 | int msgsock;
|
|---|
| 37 | XmlRpcHttpd *httpd;
|
|---|
| 38 | tstring address;
|
|---|
| 39 | } HttpdInfo;
|
|---|
| 40 | typedef std::map<tstring, tstring> BasicAuths;
|
|---|
| 41 | typedef struct {
|
|---|
| 42 | std::vector<tstring> accept_list;
|
|---|
| 43 | } AcceptAuth;
|
|---|
| 44 | typedef std::map<tstring, AcceptAuth> AcceptAuths;
|
|---|
| 45 | typedef std::vector<tstring> AcceptIPs;
|
|---|
| 46 |
|
|---|
| 47 | typedef XmlRpcValue (*XmlRpcFunc)(const std::vector<XmlRpcValue>& requests);
|
|---|
| 48 | typedef void (*XmlRpcLoggerFunc)(const HttpdInfo* httpd_info, const tstring& request);
|
|---|
| 49 | typedef std::map<tstring, tstring> MimeTypes;
|
|---|
| 50 | typedef std::vector<tstring> DefaultPages;
|
|---|
| 51 |
|
|---|
| 52 | private:
|
|---|
| 53 | std::map<tstring, XmlRpcFunc> callbacks;
|
|---|
| 54 | #ifdef _WIN32
|
|---|
| 55 | HANDLE thread;
|
|---|
| 56 | #else
|
|---|
| 57 | pthread_t thread;
|
|---|
| 58 | #endif
|
|---|
| 59 |
|
|---|
| 60 | public:
|
|---|
| 61 | int sock;
|
|---|
| 62 | std::string hostname;
|
|---|
| 63 | std::string hostaddr;
|
|---|
| 64 | std::string target;
|
|---|
| 65 | std::string root;
|
|---|
| 66 | std::string fs_charset;
|
|---|
| 67 | unsigned short port;
|
|---|
| 68 | BasicAuths basic_auths;
|
|---|
| 69 | AcceptAuths accept_auths;
|
|---|
| 70 | AcceptIPs accept_ips;
|
|---|
| 71 | MimeTypes mime_types;
|
|---|
| 72 | DefaultPages default_pages;
|
|---|
| 73 | XmlRpcLoggerFunc loggerfunc;
|
|---|
| 74 |
|
|---|
| 75 | void initialize() {
|
|---|
| 76 | sock = -1;
|
|---|
| 77 | port = 80;
|
|---|
| 78 | target = "/RPC2";
|
|---|
| 79 | fs_charset = "utf-8";
|
|---|
| 80 | thread = 0;
|
|---|
| 81 | loggerfunc = NULL;
|
|---|
| 82 | mime_types[_T("gif")] = _T("image/gif");
|
|---|
| 83 | mime_types[_T("jpg")] = _T("image/jpeg");
|
|---|
| 84 | mime_types[_T("png")] = _T("image/png");
|
|---|
| 85 | mime_types[_T("htm")] = _T("text/html");
|
|---|
| 86 | mime_types[_T("html")] = _T("text/html");
|
|---|
| 87 | mime_types[_T("txt")] = _T("text/plain");
|
|---|
| 88 | mime_types[_T("xml")] = _T("text/xml");
|
|---|
| 89 | mime_types[_T("js")] = _T("application/x-javascript");
|
|---|
| 90 | mime_types[_T("css")] = _T("text/css");
|
|---|
| 91 | default_pages.push_back(_T("index.html"));
|
|---|
| 92 | default_pages.push_back(_T("index.php"));
|
|---|
| 93 | default_pages.push_back(_T("index.cgi"));
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | XmlRpcHttpd() {
|
|---|
| 97 | initialize();
|
|---|
| 98 | }
|
|---|
| 99 | XmlRpcHttpd(unsigned short _port) {
|
|---|
| 100 | initialize();
|
|---|
| 101 | port = _port;
|
|---|
| 102 | }
|
|---|
| 103 | XmlRpcHttpd(unsigned short _port, tstring _target) {
|
|---|
| 104 | initialize();
|
|---|
| 105 | port = _port;
|
|---|
| 106 | target = tstring2string(_target);
|
|---|
| 107 | }
|
|---|
| 108 | ~XmlRpcHttpd() {
|
|---|
| 109 | stop();
|
|---|
| 110 | }
|
|---|
| 111 | bool start();
|
|---|
| 112 | bool stop();
|
|---|
| 113 | bool wait();
|
|---|
| 114 | void set_fs_charset(tstring _fs_charset) {
|
|---|
| 115 | fs_charset = tstring2string(_fs_charset);
|
|---|
| 116 | }
|
|---|
| 117 | tstring get_fs_charset() {
|
|---|
| 118 | return string2tstring(fs_charset);
|
|---|
| 119 | }
|
|---|
| 120 | void registerXmlRpcFunc(tstring name, XmlRpcFunc func) {
|
|---|
| 121 | callbacks[name] = func;
|
|---|
| 122 | accept_auths.erase(name);
|
|---|
| 123 | }
|
|---|
| 124 | void registerXmlRpcFunc(tstring name, XmlRpcFunc func, std::vector<tstring> _accept_auth) {
|
|---|
| 125 | callbacks[name] = func;
|
|---|
| 126 | accept_auths[name].accept_list = _accept_auth;
|
|---|
| 127 | }
|
|---|
| 128 | void unregisterXmlRpcFunc(tstring name) {
|
|---|
| 129 | callbacks.erase(name);
|
|---|
| 130 | }
|
|---|
| 131 | bool dispatchXmlRpcFunc(tstring name, std::vector<XmlRpcValue>& requests, XmlRpcValue& response);
|
|---|
| 132 | void setAuthentication(BasicAuths _basic_auths) {
|
|---|
| 133 | basic_auths = _basic_auths;
|
|---|
| 134 | }
|
|---|
| 135 | void bindRoot(tstring _root) {
|
|---|
| 136 | root = get_realpath(tstring2string(_root));
|
|---|
| 137 | }
|
|---|
| 138 | static std::string get_realpath(std::string path) {
|
|---|
| 139 | std::replace(path.begin(), path.end(), '\\', '/');
|
|---|
| 140 | int end_pos = path.find_last_of("?#");
|
|---|
| 141 | if (end_pos != -1) path.resize(end_pos);
|
|---|
| 142 |
|
|---|
| 143 | std::vector<std::string> path_sep = split_string(path, "/");
|
|---|
| 144 | std::vector<std::string>::iterator it;
|
|---|
| 145 | while(true) {
|
|---|
| 146 | it = std::find(path_sep.begin(), path_sep.end(), "..");
|
|---|
| 147 | if (it == path_sep.end()) break;
|
|---|
| 148 | if (it == path_sep.begin()) {
|
|---|
| 149 | continue;
|
|---|
| 150 | }
|
|---|
| 151 | path_sep.erase(it-1);
|
|---|
| 152 | path_sep.erase(it-1);
|
|---|
| 153 | }
|
|---|
| 154 | std::string path_real;
|
|---|
| 155 | for(it = path_sep.begin(); it != path_sep.end(); it++) {
|
|---|
| 156 | path_real += *it;
|
|---|
| 157 | if (it+1 != path_sep.end())
|
|---|
| 158 | path_real += "/";
|
|---|
| 159 | }
|
|---|
| 160 | if (path[path.size()-1] == '/')
|
|---|
| 161 | path_real += "/";
|
|---|
| 162 | return path_real;
|
|---|
| 163 | }
|
|---|
| 164 | };
|
|---|
| 165 |
|
|---|
| 166 | void registerXmlRpcFunc(tstring name, XmlRpcHttpd::XmlRpcFunc func);
|
|---|
| 167 | void unregisterXmlRpcFunc(tstring name);
|
|---|
| 168 | bool dispatchXmlRpcFunc(tstring name, std::vector<XmlRpcValue>& requests, XmlRpcValue& response);
|
|---|
| 169 |
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | #endif /* _XMLRPCHTTPD_H_ */
|
|---|