-rw-r--r-- | components/exception_dev | 7 | ||||
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | include/sitecing/sitecing_util.h | 87 | ||||
-rw-r--r-- | lib/component_factory.cc | 31 | ||||
-rw-r--r-- | lib/configuration.cc | 9 | ||||
-rw-r--r-- | lib/sitecing_parser.ll | 13 | ||||
-rw-r--r-- | lib/sitecing_util.cc | 117 | ||||
-rw-r--r-- | lib/sitespace.cc | 3 | ||||
-rw-r--r-- | src/sitecing-build.cc | 5 | ||||
-rw-r--r-- | src/sitecing-fastcgi.cc | 4 | ||||
-rw-r--r-- | src/sitecing-plaincgi.cc | 4 |
11 files changed, 48 insertions, 236 deletions
diff --git a/components/exception_dev b/components/exception_dev index d8c84e1..d62f462 100644 --- a/components/exception_dev +++ b/components/exception_dev @@ -10,6 +10,7 @@ #include <sitecing/sitecing_util.h> #include <sitecing/util.h> #include <sitecing/magic.h> + #include <konforka/util.h> #include <konforka/exception.h> </%impl> %%var string message; @@ -254,7 +255,7 @@ } } if((oel>0 && !oef.empty()) && (oel!=error_line || oef!=error_file)) { - string ef = "/"+sitecing::combine_path(root_source+component,oef); + string ef = "/"+konforka::combine_path(root_source+component,oef); report_error(ef,oel,remove_roots(cumulative)); cumulative.clear(); } @@ -264,7 +265,7 @@ cumulative += line; } if(!(cumulative.empty() || error_file.empty() || error_line<0)) { - error_file = "/"+sitecing::combine_path(root_source+component,error_file); + error_file = "/"+konforka::combine_path(root_source+component,error_file); report_error(error_file,error_line,remove_roots(cumulative)); } } @@ -318,7 +319,7 @@ </div> </%method> <%codemethod string strip_roots(const string& filename) %> - string np = sitecing::normalize_path(filename); + string np = konforka::normalize_path(filename); try{ return sitecing::strip_prefix(np,root_source); }catch(sitecing::utility_no_prefix& e){ } diff --git a/configure.ac b/configure.ac index a8aa142..fde326b 100644 --- a/configure.ac +++ b/configure.ac @@ -20,6 +20,10 @@ AC_FUNC_REALLOC AC_WITH_PKGCONFIG +PKG_CHECK_MODULES([KONFORKA],[konforka >= 0.0.1],,[ + AC_MSG_ERROR([no proper version of konforka library found, get it at http://kin.klever.net/konforka/]) +]) + PKG_CHECK_MODULES([KINGATE],[kingate >= 0.0.1],,[ AC_MSG_ERROR([no kingate library found, get it at http://kin.klever.net/kingate/]) ]) diff --git a/include/sitecing/sitecing_util.h b/include/sitecing/sitecing_util.h index d1a6c4a..f642c74 100644 --- a/include/sitecing/sitecing_util.h +++ b/include/sitecing/sitecing_util.h @@ -4,6 +4,7 @@ #include <sys/types.h> #include <string> #include <konforka/exception.h> +#include <konforka/util.h> /** * @file @@ -21,14 +22,7 @@ namespace sitecing { utility_error(const string& fi,const string& fu,int l,const string& w) : konforka::exception(fi,fu,l,w) { } }; - /** - * Restricted sequence encountered. - */ - class utility_restricted_sequence : public utility_error { - public: - utility_restricted_sequence(const string& fi,const string& fu,int l,const string& w) - : utility_error(fi,fu,l,w) { } - }; + /** * No prefix or suffix found to strip out. */ @@ -55,16 +49,6 @@ namespace sitecing { }; /** - * Went up beyond root. - * @todo TODO: wish I could remember the details -- document me. - */ - class utility_beyond_root : public utility_error { - public: - utility_beyond_root(const string& fi,const string& fu,int l,const string& w) - : utility_error(fi,fu,l,w) { } - }; - - /** * The file lock object. Released at the object destruction. */ class file_lock { @@ -215,48 +199,6 @@ namespace sitecing { }; /** - * normalize_path options enumeration. - * @see normalize_path() - */ - enum normalize_path_options { - /** - * Restrict the /../ sequence. - */ - restrict_dotdot = 1, - /** - * Strip out the leading slash. - */ - strip_leading_slash = 2, - /** - * Strip out the trailing slash. - */ - strip_trailing_slash = 4 - }; - /** - * combine_path options enumeration. - * @see combine_path() - */ - enum combine_path_options { - /** - * The origin is file. Otherwise it is directory. - */ - origin_is_file = 1, - /** - * Fail if we've gone up beyond root. - */ - fail_beyond_root = 2 - }; - - /** - * Normalize pathname by stripping duplicate slashes, etc. - * @param path the path name. - * @param opts options. - * @return the normalized path. - * @see normalize_path_options - * @todo TODO: document exceptions. - */ - string normalize_path(const string& path,int opts=(restrict_dotdot|strip_trailing_slash)); - /** * Strip prefix from the string. * @param str the string. * @param prefix prefix to strip. @@ -272,30 +214,7 @@ namespace sitecing { * @todo TODO: document exceptions. */ string strip_suffix(const string& str,const string& suffix); - /** - * Get the directory part of the filename. - * @param filename the full file name. - * @return the directory part. - */ - string dir_name(const string& filename); - /** - * Combine path with the relative path. - * @param origin the origin. - * @param relative relative path to combine origin with. - * @param opts options. - * @return the pathc combined. - * @see combine_path_options - * @todo TODO: document exceptions. - */ - string combine_path(const string& origin,const string& relative,int opts=origin_is_file); - - /** - * Create directory and parent directories if needed. - * @param path the pathname. - * @param mode the mode for newly created directories. - */ - void make_path(const string& path,mode_t mode); - + /** * Change to the directory and pop back at object's destruction (e.g. when * the object goes out of scope). diff --git a/lib/component_factory.cc b/lib/component_factory.cc index d9692de..af3d911 100644 --- a/lib/component_factory.cc +++ b/lib/component_factory.cc @@ -11,6 +11,7 @@ #include <stdexcept> #include <vector> using namespace std; + #include <konforka/util.h> #include "sitecing/component_factory.h" #include "sitecing/sitecing_util.h" #include "sitecing/sitecing_parser.h" @@ -24,14 +25,14 @@ namespace sitecing { component_factory::component_factory(configuration& c) : config(c), - root_source(normalize_path(c.root_source,strip_trailing_slash)+'/'), - root_intermediate(normalize_path(c.root_intermediate,strip_trailing_slash)+'/'), - root_so(normalize_path(c.root_so,strip_trailing_slash)+'/') { + root_source(konforka::normalize_path(c.root_source,konforka::strip_trailing_slash)+'/'), + root_intermediate(konforka::normalize_path(c.root_intermediate,konforka::strip_trailing_slash)+'/'), + root_so(konforka::normalize_path(c.root_so,konforka::strip_trailing_slash)+'/') { } void component_factory::get_dependencies(const string& dst,file_list_t& deps) { deps.clear(); - string dp = normalize_path(dst,strip_trailing_slash); + string dp = konforka::normalize_path(dst,konforka::strip_trailing_slash); // source documents try { // XXX: or just compare it off? string noro = strip_prefix(dp,root_source); @@ -98,7 +99,7 @@ namespace sitecing { while(!df.eof()) { df >> str; if(str.find_first_of("\\:")==string::npos) - deps.push_back(combine_path(config.root_source+nos,str)); + deps.push_back(konforka::combine_path(config.root_source+nos,str)); } } } @@ -114,7 +115,7 @@ namespace sitecing { } bool component_factory::is_uptodate(const string& dst,file_list_t *deps) { - string dp = normalize_path(dst,strip_trailing_slash); + string dp = konforka::normalize_path(dst,konforka::strip_trailing_slash); try { string noro = strip_prefix(dp,root_intermediate); for(int ppt=0;(ppt+1)<sizeof(pp_targets)/sizeof(*pp_targets);ppt++) { @@ -135,7 +136,7 @@ namespace sitecing { } void component_factory::build(const string& dst) { - string dp = normalize_path(dst,strip_trailing_slash); + string dp = konforka::normalize_path(dst,konforka::strip_trailing_slash); // sources try { string noro = strip_prefix(dp,root_source); @@ -150,7 +151,7 @@ namespace sitecing { cerr << "Linking " << noro << endl; if(access(o.c_str(),R_OK)) throw konforka::exception(CODEPOINT,string("can't access compiled component code (")+o+")"); - make_path(dir_name(root_so+noro),0755); + konforka::make_path(konforka::dir_name(root_so+noro),0755); file_lock lock_cc(root_intermediate+noro+".o.lock"); file_lock lock_so(root_so+noro+".so.lock"); int stdO = open((root_intermediate+noro+".ld.stdout").c_str(),O_CREAT|O_TRUNC|O_WRONLY,0664); @@ -195,8 +196,8 @@ namespace sitecing { cerr << "Compiling " << nos << endl; if(access(cc.c_str(),R_OK)) throw konforka::exception(CODEPOINT,string("can't access preprocessed component code (")+cc+")"); - make_path(dir_name(cc),0755); - string pwd = dir_name(root_source+nos); + konforka::make_path(konforka::dir_name(cc),0755); + string pwd = konforka::dir_name(root_source+nos); auto_chdir dir_changer(pwd); file_lock lock_source(root_intermediate+nos+".lock"); file_lock lock_cc(root_intermediate+nos+".o.lock"); @@ -235,14 +236,14 @@ namespace sitecing { cerr << "Preprocessing " << nos << endl; if(access(src.c_str(),R_OK)) throw konforka::exception(CODEPOINT,string("can't access component source (")+src+")"); - make_path(dir_name(root_intermediate+nos),0755); + konforka::make_path(konforka::dir_name(root_intermediate+nos),0755); file_lock lock(root_intermediate+nos+".lock"); sitecing_parser parser(*this); config_options *co_skeleton = config.lookup_config(nos,config_options::flag_skeleton); if(co_skeleton) parser.skeleton = co_skeleton->skeleton; static const char *id_chars = "abcdefghijklmnopqrstuvwxyz0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - parser.class_name = normalize_path(nos,strip_leading_slash|strip_trailing_slash); + parser.class_name = konforka::normalize_path(nos,konforka::strip_leading_slash|konforka::strip_trailing_slash); for(string::size_type illc = parser.class_name.find_first_not_of(id_chars);illc!=string::npos;illc=parser.class_name.find_first_not_of(id_chars,illc)) { string::size_type lc = parser.class_name.find_first_of(id_chars,illc); int n = ((lc==string::npos)?parser.class_name.length():lc)-illc; @@ -268,7 +269,7 @@ namespace sitecing { } void component_factory::make(const string& dst) { - string dp = normalize_path(dst,strip_trailing_slash); + string dp = konforka::normalize_path(dst,konforka::strip_trailing_slash); try { string noso = strip_suffix(dp,".so"); string noro = strip_prefix(noso,root_so); @@ -317,7 +318,7 @@ namespace sitecing { } string component_factory::get_classname(const string& component) { - string cn = root_intermediate+normalize_path(component,strip_trailing_slash|strip_leading_slash)+".classname"; + string cn = root_intermediate+konforka::normalize_path(component,konforka::strip_trailing_slash|konforka::strip_leading_slash)+".classname"; make(cn); ifstream ifs(cn.c_str()); if(!ifs.good()) @@ -327,7 +328,7 @@ namespace sitecing { } void component_factory::get_ancestors(const string& component,file_list_t& rv) { - string cn = root_intermediate+normalize_path(component,strip_trailing_slash|strip_leading_slash)+".ancestors"; + string cn = root_intermediate+konforka::normalize_path(component,konforka::strip_trailing_slash|konforka::strip_leading_slash)+".ancestors"; make(cn); ifstream ifs(cn.c_str()); if(!ifs.good()) diff --git a/lib/configuration.cc b/lib/configuration.cc index 4ee1526..6b21690 100644 --- a/lib/configuration.cc +++ b/lib/configuration.cc @@ -6,6 +6,7 @@ #include <cassert> #include <stdexcept> using namespace std; + #include <konforka/util.h> #include <dotconf.h> #include "sitecing/configuration.h" #include "sitecing/sitecing_util.h" @@ -304,7 +305,7 @@ namespace sitecing { string configuration::lookup_http_status_handler(const string& target,const string& status) { string t = "/"; - t += normalize_path(target,strip_leading_slash); + t += konforka::normalize_path(target,konforka::strip_leading_slash); string rv; for(;;) { if(t[t.length()-1]=='/') { @@ -337,7 +338,7 @@ namespace sitecing { config_options::action_handler_t *configuration::lookup_action_handler(const string& target) { string t = "/"; - t += normalize_path(target,strip_leading_slash); + t += konforka::normalize_path(target,konforka::strip_leading_slash); for(;;) { if(t[t.length()-1]=='/') { loaded_options* lo = lookup_loaded_options(t); @@ -369,7 +370,7 @@ namespace sitecing { config_options* configuration::lookup_config(const string& target,int flag) { string t = "/"; // always assume leading slash - t += normalize_path(target,strip_leading_slash); + t += konforka::normalize_path(target,konforka::strip_leading_slash); // XXX: reconsider precedence for(;;) { if(t[t.length()-1]=='/') { @@ -414,7 +415,7 @@ namespace sitecing { bool configuration::match_autobuild_files(const string& target,const char *fn) { string t = "/"; - t += normalize_path(target,strip_leading_slash|strip_trailing_slash); + t += konforka::normalize_path(target,konforka::strip_leading_slash|konforka::strip_trailing_slash); t += "/"; bool rv = false; for(;;) { diff --git a/lib/sitecing_parser.ll b/lib/sitecing_parser.ll index 4fd6709..8dd8d5f 100644 --- a/lib/sitecing_parser.ll +++ b/lib/sitecing_parser.ll @@ -7,6 +7,7 @@ #include <cassert> #include <stdexcept> using namespace std; +#include <konforka/util.h> #include "sitecing/sitecing_util.h" #include "sitecing/sitecing_exception.h" using namespace sitecing; @@ -297,8 +298,8 @@ NOIDCHAR [^A-Za-z0-9_] m.output.erase(0,1); m.output.erase(m.output.length()-1); } - string c = combine_path(component_basename,m.output); - member_variables.push_back(member_variable(m._type,m._name,normalize_path(c,strip_leading_slash),true)); + string c = konforka::combine_path(component_basename,m.output); + member_variables.push_back(member_variable(m._type,m._name,konforka::normalize_path(c,konforka::strip_leading_slash),true)); modi.pop_front(); BEGIN(INITIAL); } @@ -331,8 +332,8 @@ NOIDCHAR [^A-Za-z0-9_] m.output.erase(0,1); m.output.erase(m.output.length()-1); } - string c = combine_path(component_basename,m.output); - member_variables.push_back(member_variable(m._type,m._name,normalize_path(c,strip_leading_slash),true,true)); + string c = konforka::combine_path(component_basename,m.output); + member_variables.push_back(member_variable(m._type,m._name,konforka::normalize_path(c,konforka::strip_leading_slash),true,true)); modi.pop_front(); BEGIN(INITIAL); } @@ -365,8 +366,8 @@ NOIDCHAR [^A-Za-z0-9_] m.output.erase(0,1); m.output.erase(m.output.length()-1); } - string c = combine_path(component_basename,m.output); - ancestor_classes.push_back(ancestor_class(m._name,normalize_path(c,strip_leading_slash))); + string c = konforka::combine_path(component_basename,m.output); + ancestor_classes.push_back(ancestor_class(m._name,konforka::normalize_path(c,konforka::strip_leading_slash))); modi.pop_front(); BEGIN(INITIAL); } diff --git a/lib/sitecing_util.cc b/lib/sitecing_util.cc index f892a60..f1432df 100644 --- a/lib/sitecing_util.cc +++ b/lib/sitecing_util.cc @@ -20,41 +20,6 @@ namespace sitecing { * XXX: all of these utilities could be sheerly optimized. */ - string normalize_path(const string& path,int opts) { - const char *s = path.c_str(); - string rv; - string::size_type notslash = 0; - if( (*s)=='.' && s[1]=='/' ) - s+=2; - if(opts&strip_leading_slash) - for(;(*s) && (*s)=='/';s++); - for(;*s;s++) { - if( (*s)=='/' ) { - if(s[1]=='/') - continue; - if(s[1]=='.' && s[2]=='/') { - s+=2; - continue; - } - } - if(opts&restrict_dotdot) { - if( - ( rv.empty() && s[0]=='.' && s[1]=='.' && s[2]=='/' ) // "^../" - || ( s[0]=='/' && s[1]=='.' && s[2]=='.' && (s[3]==0 || s[3]=='/') ) // "/..(/|$)" - ) - throw utility_restricted_sequence(CODEPOINT,"restricted updir sequence encountered"); - } - rv += *s; - if( (*s) != '/' ) - notslash=rv.length(); - } - if(!(opts&strip_trailing_slash)) - notslash++; - if(notslash<rv.length()) - rv.erase(notslash); // XXX: check the logic of stripping/not strippling trailing slash - return rv; - } - string strip_prefix(const string& str,const string& prefix) { if( (str.length()<prefix.length()) || str.compare(0,prefix.length(),prefix)) throw utility_no_prefix(CODEPOINT,"no such prefix"); @@ -67,33 +32,6 @@ namespace sitecing { return str.substr(0,str.length()-suffix.length()); } - string dir_name(const string& filename) { - string::size_type sl = filename.find_last_of('/'); - if(sl==string::npos) - return ""; // no slashes -- no dir. - string::size_type nosl = filename.find_last_not_of('/',sl); - if(nosl==string::npos) - return ""; // only slashes -- no dir. XXX: only slashes after the last slash... does it mean no dir? - return filename.substr(0,nosl+1); - } - - void make_path(const string& path,mode_t mode) { - struct stat st; - for(string::size_type sl=0;sl!=string::npos;sl=path.find('/',sl+1)) { - if(!sl) - continue; - string p = path.substr(0,sl); - if(stat(p.c_str(),&st) || !S_ISDIR(st.st_mode)) { - if(mkdir(p.c_str(),mode)) - throw konforka::exception(CODEPOINT,"failed to mkdir()"); - } - } - if(stat(path.c_str(),&st) || !S_ISDIR(st.st_mode)) { - if(mkdir(path.c_str(),mode)) - throw konforka::exception(CODEPOINT,"failed to mkdir()"); - } - } - void file_lock::lock(const string& f) { unlock(); fd = open(f.c_str(),O_CREAT|O_RDWR,S_IRUSR|S_IWUSR); @@ -204,61 +142,6 @@ namespace sitecing { locked=false; } - string combine_path(const string& origin,const string& relative,int opts) { - string r = normalize_path(relative,0); - string rv; - // XXX: what to do if relative is empty is a question, really. - if(r.empty()) { - return normalize_path( (opts&origin_is_file)?dir_name(origin):origin ,strip_leading_slash|restrict_dotdot|strip_trailing_slash); - }else{ - if(r[0]=='/') { - r.erase(0,1); - }else{ - rv = normalize_path((opts&origin_is_file)?dir_name(origin):origin,restrict_dotdot|strip_trailing_slash); - } - } - string::size_type lsl = rv.rfind('/'); - for(string::size_type sl=r.find('/');sl!=string::npos;sl=r.find('/')) { - assert(sl!=0); - if(sl==1 && r[0]=='.') { - // it's a "./" - r.erase(0,2); - }else if(sl==2 && r[0]=='.' && r[1]=='.') { - // we have a "../" - if(lsl==string::npos) { - if(rv.empty() && (opts&fail_beyond_root)) - throw utility_beyond_root(CODEPOINT,"went beyond root while combining path"); - rv.clear(); - }else{ - rv.erase(lsl); - lsl = rv.rfind('/'); - } - r.erase(0,3); - }else{ - // we have a "something/" - lsl = rv.length(); - rv += '/'; - rv += r.substr(0,sl); - r.erase(0,sl+1); - } - } - if(r.empty()) - return rv+'/'; - if(r.length()==2 && r[0]=='.' && r[0]=='.') { - if(lsl==string::npos) { - if(rv.empty() & (opts&fail_beyond_root)) - throw utility_beyond_root(CODEPOINT,"went beyond root while combining path"); - return "/"; - }else{ - rv.erase(lsl+1); - return rv; - } - } - rv += '/'; - rv += r; - return rv; - } - void auto_chdir::pushdir(const string& td,bool ap) { /* TODO: make use of fchdir(2) instead */ char *tmp = getcwd(0,0); diff --git a/lib/sitespace.cc b/lib/sitespace.cc index 0406d11..d5592bf 100644 --- a/lib/sitespace.cc +++ b/lib/sitespace.cc @@ -2,6 +2,7 @@ #include "pch.h" #else #include <cassert> + #include <konforka/util.h> #include "sitecing/sitespace.h" #include "sitecing/sitecing_util.h" #endif @@ -20,7 +21,7 @@ namespace sitecing { so_component sitespace::fetch(const string& c,sitecing_interface* scif) { execute_sentenced(); - string sobase = normalize_path(c); + string sobase = konforka::normalize_path(c); string sopath = factory.root_so+sobase+".so"; config_options *co_build = config.lookup_config(sobase,config_options::flag_build); if( (!co_build) || co_build->build ) diff --git a/src/sitecing-build.cc b/src/sitecing-build.cc index 4cad0a3..887ef83 100644 --- a/src/sitecing-build.cc +++ b/src/sitecing-build.cc @@ -7,6 +7,7 @@ #include <cassert> #include <set> using namespace std; +#include <konforka/util.h> #include "sitecing/sitecing_util.h" #include "sitecing/util.h" #include "sitecing/sitespace.h" @@ -78,7 +79,7 @@ void build_http_status_handlers(const string& target) { assert(site_space); set<string> stop_list; string t = "/"; - t += normalize_path(target,strip_leading_slash); + t += konforka::normalize_path(target,konforka::strip_leading_slash); for(;;) { if(t[t.length()-1]=='/') { loaded_options* lo = site_space->config.lookup_loaded_options(t); @@ -147,7 +148,7 @@ void build(const string& target) { continue; if(!strcmp(de->d_name,"..")) continue; - string subtarget = normalize_path(target+"/"+de->d_name); + string subtarget = konforka::normalize_path(target+"/"+de->d_name); struct stat sts; if(stat((site_space->config.root_source+subtarget).c_str(),&sts)) throw konforka::exception(CODEPOINT,"failed to stat() subtarget"); diff --git a/src/sitecing-fastcgi.cc b/src/sitecing-fastcgi.cc index 756dcee..03587aa 100644 --- a/src/sitecing-fastcgi.cc +++ b/src/sitecing-fastcgi.cc @@ -165,7 +165,7 @@ void sitecing_fastcgi_pm::process(int slot) { cgi_gateway gw(fi); scif.prepare(&gw); try { - component_path = normalize_path(gw.path_info(),strip_leading_slash|strip_trailing_slash); + component_path = konforka::normalize_path(gw.path_info(),konforka::strip_leading_slash|konforka::strip_trailing_slash); string full_component_path; string sitecing_path_info; while(true) { @@ -184,7 +184,7 @@ void sitecing_fastcgi_pm::process(int slot) { if(action_handler) { action = action_handler->action; } - string pwd = dir_name(full_component_path); + string pwd = konforka::dir_name(full_component_path); if(chdir(pwd.c_str())) throw konforka::exception(CODEPOINT,"failed to chdir() into document's directory"); so_component soc = ss.fetch(action,&scif); diff --git a/src/sitecing-plaincgi.cc b/src/sitecing-plaincgi.cc index 3bd291a..2f93cc5 100644 --- a/src/sitecing-plaincgi.cc +++ b/src/sitecing-plaincgi.cc @@ -50,7 +50,7 @@ void process_request(configuration& config) { cgi_gateway gw(ci); scif.prepare(&gw); try { - component_path = normalize_path(gw.path_info(),strip_leading_slash|strip_trailing_slash); + component_path = konforka::normalize_path(gw.path_info(),konforka::strip_leading_slash|konforka::strip_trailing_slash); string full_component_path; string sitecing_path_info; while(true) { @@ -69,7 +69,7 @@ void process_request(configuration& config) { if(action_handler) { action = action_handler->action; } - string pwd = dir_name(full_component_path); + string pwd = konforka::dir_name(full_component_path); if(chdir(pwd.c_str())) throw konforka::exception(CODEPOINT,"failed to chdir() into document's directory"); so_component soc = ss.fetch(action,&scif); |