|
|
|
#include <stat.h> |
#include <config.h> |
#include <module.h> |
#include <variables.h> |
#include <module_constants.h> |
constant cvs_version="$Id: prototypes.pike,v 1.92 2004/04/30 09:19:30 grubba Exp $"; |
|
#ifdef DAV_DEBUG |
#define DAV_WERROR(X...) werror(X) |
#else /* !DAV_DEBUG */ |
#define DAV_WERROR(X...) |
#endif /* DAV_DEBUG */ |
|
|
|
object Roxen; |
|
class Variable |
{ |
constant is_variable = 1; |
constant type = "Basic"; |
|
string get_warnings(); |
int get_flags(); |
void set_flags( int flags ); |
int check_visibility( RequestID id, |
int more_mode, |
int expert_mode, |
int devel_mode, |
int initial, |
int|void variable_in_cfif ); |
void set_invisibility_check_callback( function(RequestID,Variable:int) cb ); |
function(Variable:void) get_changed_callback( ); |
void set_changed_callback( function(Variable:void) cb ); |
void add_changed_callback( function(Variable:void) cb ); |
function(RequestID,Variable:int) get_invisibility_check_callback() ; |
string doc( ); |
string name( ); |
string type_hint( ); |
mixed default_value(); |
void set_warning( string to ); |
int set( mixed to ); |
int low_set( mixed to ); |
mixed query(); |
int is_defaulted(); |
array(string|mixed) verify_set( mixed new_value ); |
mapping(string:string) get_form_vars( RequestID id ); |
mixed transform_from_form( string what ); |
void set_from_form( RequestID id ); |
string path(); |
void set_path( string to ); |
string render_form( RequestID id, void|mapping additional_args ); |
string render_view( RequestID id ); |
} |
|
class BasicDefvar |
{ |
mapping(string:Variable) variables=([]); |
Variable getvar( string name ); |
int deflocaledoc( string locale, string variable, |
string name, string doc, mapping|void translate ); |
void set(string var, mixed value); |
int killvar(string var); |
void setvars( mapping (string:mixed) vars ); |
Variable defvar(string var, mixed value, |
mapping|string|void|object name, |
int|void type, |
mapping|string|void|object doc_str, |
mixed|void misc, |
int|function|void not_in_config, |
mapping|void option_translations); |
mixed query(string|void var, int|void ok); |
void definvisvar(string name, mixed value, int type, array|void misc); |
} |
|
|
|
class StringFile( string data, mixed|void _st ) |
{ |
int offset; |
|
string _sprintf() |
{ |
return "StringFile("+strlen(data)+","+offset+")"; |
} |
|
string read(int nbytes) |
{ |
if(!nbytes) |
{ |
offset = strlen(data); |
return data; |
} |
string d = data[offset..offset+nbytes-1]; |
offset += strlen(d); |
return d; |
} |
|
array stat() |
{ |
if( _st ) return (array)_st; |
return ({ 0, strlen(data), time(), time(), time(), 0, 0, 0 }); |
} |
|
void write(mixed ... args) |
{ |
throw( ({ "File not open for write\n", backtrace() }) ); |
} |
|
void seek(int to) |
{ |
offset = to; |
} |
} |
|
class ModuleInfo |
{ |
string sname; |
string filename; |
|
int last_checked; |
int type, multiple_copies; |
int|string locked; |
mapping(Configuration:int) config_locked; |
|
string get_name(); |
string get_description(); |
RoxenModule instance( object conf, void|int silent ); |
void save(); |
void update_with( RoxenModule mod, string what ); |
int init_module( string what ); |
int rec_find_module( string what, string dir ); |
int find_module( string sn ); |
int check (void|int force); |
} |
|
class ModuleCopies |
{ |
mapping copies = ([]); |
mixed `[](mixed q ) |
{ |
return copies[q]; |
} |
mixed `[]=(mixed q,mixed w ) |
{ |
return copies[q]=w; |
} |
array _indices() |
{ |
return indices(copies); |
} |
array _values() |
{ |
return values(copies); |
} |
string _sprintf( ) { return "ModuleCopies("+sizeof(copies)+")"; } |
} |
|
|
static constant Node = Parser.XML.Tree.Node; |
static constant RootNode = Parser.XML.Tree.RootNode; |
static constant HeaderNode = Parser.XML.Tree.HeaderNode; |
static constant TextNode = Parser.XML.Tree.TextNode; |
static constant ElementNode = Parser.XML.Tree.ElementNode; |
|
class DAVLock(string locktoken, |
string path, |
int(0..1) recursive, |
string|Node lockscope, |
string|Node locktype, |
void|string owner, |
) |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Node get_xml() |
{ |
ElementNode res = ElementNode("DAV:activelock", ([])); |
ElementNode tmp; |
res->add_child(tmp = ElementNode("DAV:locktype", ([]))); |
tmp->add_child(stringp(locktype)?ElementNode(locktype, ([])):locktype); |
res->add_child(tmp = ElementNode("DAV:lockscope", ([]))); |
tmp->add_child(stringp(lockscope)?ElementNode(lockscope, ([])):lockscope); |
res->add_child(tmp = ElementNode("DAV:depth", ([]))); |
tmp->add_child(recursive?TextNode("Infinity"):TextNode("0")); |
|
#if 0 |
if (owner) { |
res->add_child(tmp = ElementNode("DAV:owner", ([]))); |
tmp->add_child(owner); |
} |
#endif /* 0 */ |
|
|
|
res->add_child(tmp = ElementNode("DAV:locktoken", ([]))); |
tmp->add_child(tmp = ElementNode("DAV:href", ([]))); |
tmp->add_child(TextNode(locktoken)); |
|
return res; |
} |
} |
|
class Configuration |
{ |
inherit BasicDefvar; |
constant is_configuration = 1; |
mapping enabled_modules = ([]); |
mapping(string:array(int)) error_log=([]); |
|
#ifdef PROFILE |
mapping profile_map = ([]); |
#endif |
|
class Priority |
{ |
string _sprintf() |
{ |
return "Priority()"; |
} |
|
array (RoxenModule) url_modules = ({ }); |
array (RoxenModule) logger_modules = ({ }); |
array (RoxenModule) location_modules = ({ }); |
array (RoxenModule) filter_modules = ({ }); |
array (RoxenModule) last_modules = ({ }); |
array (RoxenModule) first_modules = ({ }); |
mapping (string:array(RoxenModule)) file_extension_modules = ([ ]); |
mapping (RoxenModule:multiset(string)) provider_modules = ([ ]); |
} |
|
class DataCache |
{ |
int current_size, max_size, max_file_size; |
int hits, misses; |
void flush(); |
void expire_entry( string url ); |
void set( string url, string data, mapping meta, int expire ); |
array(string|mapping(string:mixed)) get( string url ); |
void init_from_variables( ); |
}; |
|
array(Priority) allocate_pris(); |
|
object throttler; |
RoxenModule types_module; |
RoxenModule dir_module; |
function types_fun; |
|
string name; |
int inited; |
|
|
int requests, sent, hsent, received; |
|
function(string:int) log_function; |
DataCache datacache; |
|
int get_config_id(); |
string get_doc_for( string region, string variable ); |
string query_internal_location(RoxenModule|void mod); |
string query_name(); |
string comment(); |
void unregister_urls(); |
void stop(void|int asynch); |
string|array(string) type_from_filename( string file, int|void to, |
string|void myext ); |
|
string get_url(); |
|
array (RoxenModule) get_providers(string provides); |
RoxenModule get_provider(string provides); |
array(mixed) map_providers(string provides, string fun, mixed ... args); |
mixed call_provider(string provides, string fun, mixed ... args); |
array(function) file_extension_modules(string ext); |
array(function) url_modules(); |
mapping api_functions(void|RequestID id); |
array(function) logger_modules(); |
array(function) last_modules(); |
array(function) first_modules(); |
array location_modules(); |
array(function) filter_modules(); |
void init_log_file(); |
int|mapping check_security(function|object a, RequestID id, void|int slevel); |
void invalidate_cache(); |
void clear_memory_caches(); |
string examine_return_mapping(mapping m); |
mapping|int(-1..0) low_get_file(RequestID id, int|void no_magic); |
mapping get_file(RequestID id, int|void no_magic, int|void internal_get); |
array(string) find_dir(string file, RequestID id, void|int(0..1) verbose); |
array(int)|object(Stdio.Stat) stat_file(string file, RequestID id); |
array open_file(string fname, string mode, RequestID id, void|int ig, void|int rc); |
mapping(string:array(mixed)) find_dir_stat(string file, RequestID id); |
array access(string file, RequestID id); |
string real_file(string file, RequestID id); |
int|string try_get_file(string s, RequestID id, |
int|void status, int|void nocache, |
int|void not_internal, mapping|void result_mapping); |
int(0..1) is_file(string virt_path, RequestID id, int(0..1)|void internal); |
void start(int num); |
void save_me(); |
int save_one( RoxenModule o ); |
RoxenModule reload_module( string modname ); |
RoxenModule enable_module( string modname, RoxenModule|void me, |
ModuleInfo|void moduleinfo, |
int|void nostart, |
int|void nosave ); |
void call_start_callbacks( RoxenModule me, |
ModuleInfo moduleinfo, |
ModuleCopies module ); |
void call_low_start_callbacks( RoxenModule me, |
ModuleInfo moduleinfo, |
ModuleCopies module ); |
int disable_module( string modname, int|void nodest ); |
int add_modules( array(string) mods, int|void now ); |
RoxenModule find_module(string name); |
#if ROXEN_COMPAT < 2.2 |
Sql.Sql sql_cache_get(string what); |
Sql.Sql sql_connect(string db); |
#endif |
void enable_all_modules(); |
void low_init(void|int modules_already_enabled); |
|
|
string parse_rxml(string what, RequestID id, |
void|Stdio.File file, |
void|mapping defines ); |
void add_parse_module (RoxenModule mod); |
void remove_parse_module (RoxenModule mod); |
|
string real_file(string a, RequestID b); |
|
|
mapping authenticate_throw( RequestID id, string realm, |
UserDB|void database, |
AuthModule|void method); |
User authenticate( RequestID id, |
UserDB|void database, |
AuthModule|void method ); |
|
array(AuthModule) auth_modules(); |
array(UserDB) user_databases(); |
|
AuthModule find_auth_module( string name ); |
UserDB find_user_database( string name ); |
|
static string _sprintf( ) |
{ |
return "Configuration("+name+")"; |
} |
} |
|
|
class Protocol |
{ |
inherit BasicDefvar; |
|
constant name = "unknown"; |
|
|
constant prot_name = "unknown"; |
|
|
constant supports_ipless = 0; |
constant requesthandlerfile = ""; |
constant default_port = 4711; |
|
int bound; |
int refs; |
|
program requesthandler; |
|
string path; |
int port; |
string ip; |
array(string) sorted_urls = ({}); |
mapping(string:mapping) urls = ([]); |
mapping(Configuration:mapping) conf_data = ([]); |
|
void ref(string url, mapping data); |
void unref(string url); |
Configuration find_configuration_for_url( string url, RequestID id, |
int|void no_default ); |
string get_key(); |
void save(); |
void restore(); |
}; |
|
|
class FakedVariables( mapping real_variables ) |
{ |
static array _indices() |
{ |
return indices( real_variables ); |
} |
|
static array _values() |
{ |
return map( _indices(), `[] ); |
} |
|
static mixed fix_value( mixed what ) |
{ |
if( !what ) return what; |
if( !arrayp(what) ) return what; |
|
if( sizeof( what ) == 1 ) |
return what[0]; |
return what*"\0"; |
} |
|
static mixed `[]( string ind ) { |
return fix_value( real_variables[ ind ] ); |
} |
|
static mixed `->(string ind ) { |
return `[]( ind ); |
} |
|
static mixed `[]=( string ind, mixed what ) { |
real_variables[ ind ] = ({ what }); |
return what; |
} |
|
static mixed `->=(string ind, mixed what ) { |
return `[]=( ind,what ); |
} |
|
static mixed _m_delete( mixed what ) { |
|
return fix_value( m_delete( real_variables, what ) ); |
} |
|
static int _equal( mixed what ) { |
return `==(what); |
} |
|
static int `==( mixed what ) { |
if( mappingp( what ) && (real_variables == what) ) |
return 1; |
} |
|
static string _sprintf( int f ) |
{ |
switch( f ) |
{ |
case 'O': |
return sprintf( "FakedVariables(%O)", real_variables ); |
default: |
return sprintf( sprintf("%%%c", f ), real_variables ); |
} |
} |
|
static this_program `|( mapping what ) |
{ |
foreach( indices(what), string q )`[]=( q,what[q] ); |
return this_object(); |
} |
|
static this_program `+=( mapping what ) |
{ |
foreach( indices(what), string q )`[]=( q,what[q] ); |
return this_object(); |
} |
|
static this_program `+( mapping what ) |
{ |
foreach( indices(what), string q )`[]=( q,what[q] ); |
return this_object(); |
} |
|
static mapping cast(string to) |
{ |
if (to[..6]=="mapping") |
{ |
array v=indices(real_variables); |
return mkmapping(v,map(v,`[])); |
} |
|
error("can't cast to %O\n",to); |
} |
} |
|
class PrefLanguages |
|
|
|
{ |
int decoded=0; |
int sorted=0; |
array(string) subtags=({}); |
array(string) languages=({}); |
array(float) qualities=({}); |
|
array(string) get_languages() { |
sort_lang(); |
return languages; |
} |
|
string get_language() { |
if(!languages || !sizeof(languages)) return 0; |
sort_lang(); |
return languages[0]; |
} |
|
array(float) get_qualities() { |
sort_lang(); |
return qualities; |
} |
|
float get_quality() { |
if(!qualities || !sizeof(qualities)) return 0.0; |
sort_lang(); |
return qualities[0]; |
} |
|
void set_sorted(array(string) lang, void|array(float) q) { |
languages=lang; |
if(q && sizeof(q)==sizeof(lang)) |
qualities=q; |
else |
qualities=({1.0})*sizeof(lang); |
sorted=1; |
decoded=1; |
} |
|
void sort_lang() { |
if(sorted && decoded) return; |
array(float) q; |
array(string) s=reverse(languages)-({""}), u=({}); |
|
if(!decoded) { |
q=({}); |
s=Array.map(s, lambda(string x) { |
float n=1.0; |
string sub=""; |
sscanf(lower_case(x), "%s;q=%f", x, n); |
if(n==0.0) return ""; |
sscanf(x, "%s-%s", x, sub); |
q+=({n}); |
u+=({sub}); |
return x; |
}); |
s-=({""}); |
decoded=1; |
} |
else |
q=reverse(qualities); |
|
sort(q,s,u); |
languages=reverse(s); |
qualities=reverse(q); |
subtags=reverse(u); |
sorted=1; |
} |
} |
|
|
|
static function _charset_decoder_func; |
|
class RequestID |
|
|
|
|
|
|
|
|
{ |
Configuration conf; |
|
Protocol port_obj; |
|
|
int time; |
|
|
string raw_url; |
|
|
|
|
|
|
|
int do_not_disconnect; |
|
|
|
mapping (string:array) real_variables; |
|
|
|
|
|
|
|
|
|
|
|
|
|
mapping(string:mixed)|FakedVariables variables; |
|
|
|
|
|
|
|
|
|
mapping (string:mixed) misc; |
|
|
|
|
|
mapping (string:mixed) connection_misc; |
|
|
|
|
|
mapping (string:string) cookies; |
|
|
|
|
mapping (string:array(string)|string) request_headers; |
|
|
|
|
|
|
mapping (string:mixed) throttle; |
|
|
mapping (string:mixed) client_var; |
|
|
|
multiset(string) prestate; |
|
|
|
|
|
|
|
|
|
|
multiset(string) config; |
|
|
|
|
|
multiset(string) supports; |
|
|
multiset(string) pragma; |
|
|
|
|
|
array(string) client; |
array(string) referer; |
|
Stdio.File my_fd; |
|
|
string prot; |
|
|
|
string clientprot; |
|
|
|
|
string method; |
|
|
string realfile; |
|
|
|
string virtfile; |
|
|
|
|
|
string rest_query; |
|
|
|
string raw; |
|
|
string query; |
|
|
|
string not_query; |
|
|
|
|
|
|
string extra_extension; |
|
string data; |
|
|
string leftovers; |
string rawauth, realauth; |
string since; |
|
string remoteaddr; |
|
|
string host; |
|
|
multiset(string) cache_status = (<>); |
|
|
|
|
|
|
object root_id; |
|
|
|
|
|
static void create(Stdio.File fd, Protocol port, Configuration conf){} |
void send(string|object what, int|void len){} |
|
#if constant(Parser.XML.Tree.XMLNSParser) |
static Parser.XML.Tree.Node xml_data; |
|
Parser.XML.Tree.Node get_xml_data() |
{ |
if (!sizeof(data)) return 0; |
if (xml_data) return xml_data; |
|
|
DAV_WERROR("Parsing XML data: %O\n", data); |
return xml_data = Parser.XML.Tree.parse_input(data, 0, 0, 0, 1); |
} |
#endif /* Parser.XML.Tree.XMLNSParser */ |
|
static string cached_url_base; |
|
string url_base() |
|
|
|
|
|
|
|
{ |
|
|
if (!cached_url_base) { |
string tmp; |
|
|
if (port_obj) { |
string host = port_obj->conf_data[conf]->hostname; |
if (host == "*" && conf && sizeof (host = conf->get_url())) |
if (sscanf (host, "%*s://%[^:/]", host) < 2) |
host = port_obj->ip; |
cached_url_base = port_obj->prot_name + "://" + host; |
if (port_obj->port != port_obj->default_port) |
cached_url_base += ":" + port_obj->port; |
} |
|
|
else if (conf && sizeof (tmp = conf->get_url())) |
cached_url_base = tmp[..sizeof (tmp) - 2]; |
|
|
|
else return cached_url_base = ""; |
|
if (string p = misc->site_prefix_path) cached_url_base += p; |
cached_url_base += "/"; |
} |
return cached_url_base; |
} |
|
void add_response_header (string name, string value) |
|
|
|
|
|
|
|
|
|
|
|
{ |
mapping hdrs = misc->defines && misc->defines[" _extra_heads"] || misc->moreheads; |
if (!hdrs) hdrs = misc->moreheads = ([]); |
|
|
|
array|string cur_val = hdrs[name]; |
if(cur_val) { |
if(arrayp(cur_val)) { |
if (!has_value(cur_val, value)) |
cur_val += ({ value }); |
} else { |
if (cur_val != value) |
cur_val = ({ cur_val, value }); |
} |
} |
else |
cur_val = value; |
|
if (hdrs == misc->moreheads) |
hdrs[name] = cur_val; |
else if (object ctx = RXML_CONTEXT) |
ctx->set_var (name, cur_val, "header"); |
else |
hdrs[name] = cur_val; |
} |
|
void set_response_header (string name, string value) |
|
|
|
|
|
|
|
|
|
|
|
|
{ |
if (misc->defines && misc->defines[" _extra_heads"]) { |
misc->defines[" _extra_heads"][name] = value; |
if (object ctx = RXML_CONTEXT) |
ctx->signal_var_change (name, "header"); |
} |
else { |
if (!misc->moreheads) misc->moreheads = ([]); |
misc->moreheads[name] = value; |
} |
} |
|
|
|
|
array(string) output_charset = ({}); |
string input_charset; |
|
void set_output_charset( string|function to, int|void mode ) |
{ |
if (object ctx = RXML_CONTEXT) |
ctx->add_p_code_callback ("set_output_charset", to, mode); |
|
if( search( output_charset, to ) != -1 ) |
return; |
|
switch( mode ) |
{ |
case 0: |
output_charset = ({ to }); |
break; |
|
case 1: |
if( !sizeof( output_charset ) ) |
output_charset = ({ to }); |
break; |
|
case 2: |
output_charset |= ({ to }); |
break; |
} |
} |
|
static string charset_name(function|string what) |
{ |
switch (what) { |
case string_to_unicode: return "ISO10646-1"; |
case string_to_utf8: return "UTF-8"; |
default: return upper_case((string) what); |
} |
} |
|
static function charset_function(function|string what, int allow_entities) |
{ |
switch (what) { |
case "ISO-10646-1": |
case "ISO10646-1": |
case string_to_unicode: |
return string_to_unicode; |
|
case "UTF-8": |
case string_to_utf8: |
return string_to_utf8; |
|
default: |
catch { |
|
function fallback_func = |
allow_entities && |
lambda(string char) { |
return sprintf("&#x%x;", char[0]); |
}; |
|
_charset_decoder_func = |
_charset_decoder_func || Roxen->_charset_decoder; |
return |
_charset_decoder_func(Locale.Charset.encoder((string) what, "", |
fallback_func)) |
->decode; |
}; |
} |
return lambda(string what) { return what; }; |
} |
|
static array(string) join_charset(string old, |
function|string add, |
function oldcodec, |
int allow_entities) |
{ |
switch (old && upper_case(old)) { |
case 0: |
return ({ charset_name(add), charset_function(add, allow_entities) }); |
case "ISO10646-1": |
case "UTF-8": |
return ({ old, oldcodec }); |
case "ISO-2022": |
return ({ old, oldcodec }); |
default: |
|
return ({ charset_name(add), charset_function(add, allow_entities) }); |
} |
} |
|
array(string) output_encode(string what, int|void allow_entities, |
string|void force_charset) |
{ |
|
|
if (String.width(what) == 8) { |
if (force_charset) { |
if (upper_case(force_charset) == "ISO-8859-1") |
return ({ "ISO-8859-1", what }); |
} else { |
if (sizeof(output_charset) == 1 && |
upper_case(output_charset[0]) == "ISO-8859-1") |
return ({ "ISO-8859-1", what }); |
} |
} |
|
if (!force_charset) { |
string charset; |
function encoder; |
|
foreach( output_charset, string|function f ) |
[charset,encoder] = join_charset(charset, f, encoder, allow_entities); |
if (!encoder) |
if (String.width(what) > 8) { |
charset = "UTF-8"; |
encoder = string_to_utf8; |
} |
if (encoder) |
what = encoder(what); |
return ({ charset, what }); |
} else |
return ({ |
0, |
Locale.Charset.encoder((force_charset / "=")[-1])->feed(what)->drain() |
}); |
} |
|
|
string scan_for_query( string f ) |
{ |
if(sscanf(f,"%s?%s", f, query) == 2) |
{ |
string v, a, b; |
|
foreach(query / "&", v) |
if(sscanf(v, "%s=%s", a, b) == 2) |
{ |
a = _Roxen.http_decode_string(replace(a, "+", " ")); |
b = _Roxen.http_decode_string(replace(b, "+", " ")); |
real_variables[ a ] += ({ b }); |
} else |
if(strlen( rest_query )) |
rest_query += "&" + _Roxen.http_decode_string( v ); |
else |
rest_query = _Roxen.http_decode_string( v ); |
rest_query=replace(rest_query, "+", "\000"); |
} |
return f; |
} |
|
mapping(string:string) make_response_headers (mapping(string:mixed) file) |
|
|
|
|
|
|
|
|
{ |
if (!file->stat) file->stat = misc->stat; |
if(objectp(file->file)) { |
if(!file->stat) |
file->stat = file->file->stat(); |
if (zero_type(misc->cacheable) && file->file->is_file) { |
|
misc->cacheable = (predef::time(1) - file->stat[ST_MTIME])/4; |
} |
} |
|
if( Stat fstat = file->stat ) |
{ |
if( !file->len && fstat[1] >= 0 ) |
file->len = fstat[1]; |
if ( fstat[ST_MTIME] > misc->last_modified ) |
misc->last_modified = fstat[ST_MTIME]; |
} |
|
if (!file->error) |
file->error = Protocols.HTTP.HTTP_OK; |
|
if(!file->type) file->type="text/plain"; |
|
mapping(string:string) heads = ([]); |
|
if( !zero_type(misc->cacheable) && |
(misc->cacheable != INITIAL_CACHEABLE) ) { |
if (!misc->cacheable) { |
|
heads["Expires"] = Roxen->http_date( predef::time(1)-31557600 ); |
} else |
heads["Expires"] = Roxen->http_date( predef::time(1)+misc->cacheable ); |
if (misc->cacheable < INITIAL_CACHEABLE) { |
|
|
misc->last_modified = predef::time(1); |
} |
} |
|
if (misc->last_modified) |
heads["Last-Modified"] = Roxen->http_date(misc->last_modified); |
|
{ |
string charset=""; |
if( stringp(file->data) ) |
{ |
if (sizeof (output_charset) || |
has_prefix (file->type, "text/") || |
(String.width(file->data) > 8)) |
{ |
int allow_entities = |
has_prefix(file->type, "text/xml") || |
has_prefix(file->type, "text/html"); |
[charset,file->data] = output_encode( file->data, allow_entities ); |
if( charset && (search(file["type"], "; charset=") == -1)) |
charset = "; charset="+charset; |
else |
charset = ""; |
} |
file->len = strlen(file->data); |
} |
heads["Content-Type"] = file->type + charset; |
} |
|
heads["Accept-Ranges"] = "bytes"; |
heads["Server"] = replace(roxenp()->version(), " ", "·"); |
if( misc->connection ) |
heads["Connection"] = misc->connection; |
|
if(file->encoding) heads["Content-Encoding"] = file->encoding; |
|
heads->Date = Roxen->http_date(predef::time(1)); |
if(file->expires) |
heads->Expires = Roxen->http_date(file->expires); |
|
|
heads["Content-Length"] = (string)file->len; |
|
if (misc->etag) |
heads->ETag = misc->etag; |
|
#ifdef RAM_CACHE |
if (!misc->etag && file->len && |
(file->data || file->file) && |
file->error == 200 && (<"HEAD", "GET">)[method] && |
(file->len < conf->datacache->max_file_size)) { |
string data = ""; |
if (file->file) { |
data = file->file->read(file->len); |
if (file->data && (sizeof(data) < file->len)) { |
data += file->data[..file->len - (sizeof(data)+1)]; |
} |
m_delete(file, "file"); |
} else if (file->data) { |
data = file->data[..file->len - 1]; |
} |
file->data = data; |
heads->ETag = misc->etag = |
Crypto.string_to_hex(Crypto.md5()->update(data)->digest()); |
heads->Vary = "ETag"; |
} |
#endif /* RAM_CACHE */ |
|
if(mappingp(file->extra_heads)) |
heads |= file->extra_heads; |
|
if(mappingp(misc->moreheads)) |
heads |= misc->moreheads; |
|
return heads; |
} |
|
void adjust_for_config_path( string p ) |
{ |
if( not_query ) not_query = not_query[ strlen(p).. ]; |
raw_url = raw_url[ strlen(p).. ]; |
misc->site_prefix_path = p; |
} |
|
void end(string|void s, int|void keepit){} |
void ready_to_receive(){} |
void send_result(mapping|void result){} |
RequestID clone_me() |
{ |
object c,t; |
c=object_program(t=this_object())(0, port_obj, conf); |
|
c->port_obj = port_obj; |
c->conf = conf; |
c->root_id = root_id; |
c->time = time; |
c->raw_url = raw_url; |
|
c->real_variables = copy_value( real_variables ); |
c->variables = FakedVariables( c->real_variables ); |
c->misc = copy_value( misc ); |
c->misc->orig = t; |
|
c->connection_misc = connection_misc; |
|
c->prestate = prestate; |
c->supports = supports; |
c->config = config; |
c->client_var = client_var; |
|
c->remoteaddr = remoteaddr; |
c->host = host; |
|
c->client = client; |
c->referer = referer; |
c->pragma = pragma; |
|
c->cookies = cookies; |
c->my_fd = 0; |
c->prot = prot; |
c->clientprot = clientprot; |
c->method = method; |
|
c->rest_query = rest_query; |
c->raw = raw; |
c->query = query; |
c->not_query = not_query; |
c->data = data; |
c->extra_extension = extra_extension; |
|
c->realauth = realauth; |
c->rawauth = rawauth; |
c->since = since; |
return c; |
} |
|
Stdio.File connection( ) |
|
{ |
return my_fd; |
} |
|
Configuration configuration() |
|
|
{ |
return conf; |
} |
} |
|
class XMLStatusNode |
{ |
inherit ElementNode; |
static void create(int|string code, void|string message) |
{ |
::create("DAV:status", ([])); |
if (intp(code)) { |
code = sprintf("HTTP/1.1 %d %s", code, |
message||errors[code]||"Unknown status"); |
} |
add_child(TextNode(code)); |
} |
} |
|
class XMLPropStatNode |
{ |
inherit Parser.XML.Tree.ElementNode; |
|
static mapping(string:Node) properties = ([]); |
static multiset(string) descriptions = (<>); |
|
static Node prop_node; |
static Node description_node; |
|
void add_property(string prop_name, void|string|array(Node)|Node value) |
{ |
Node n; |
if (!(n = properties[prop_name])) { |
string type; |
|
|
|
switch (prop_name) { |
case "DAV:creationdate": type = "dateTime.tz"; break; |
case "DAV:getlastmodified": type = "dateTime.rfc1123"; break; |
|
|
} |
n = ElementNode(prop_name, |
type ? |
(["urn:schemas-microsoft-com:datatypesdt": type]) : |
([])); |
properties[prop_name] = n; |
prop_node->add_child(n); |
} |
if (value) { |
if (stringp(value)) { |
value = TextNode(value); |
} |
if (arrayp(value)) { |
n->replace_children(value); |
} else { |
n->replace_children(({ value })); |
} |
} else { |
n->replace_children(({})); |
} |
} |
|
void add_description(string description) |
{ |
if (descriptions[description]) return; |
descriptions[description] = 1; |
if (!description_node) { |
description_node = ElementNode("DAV:responsedescription", ([])); |
add_child(description_node); |
} |
description_node->add_child(TextNode(description + "\n")); |
} |
|
int http_code; |
|
static void create(int|void code, string|void message) |
{ |
http_code = code || 200; |
|
::create("DAV:propstat", ([])); |
add_child(XMLStatusNode(code, message)); |
add_child(prop_node = ElementNode("DAV:prop", ([]))); |
} |
} |
|
class MultiStatus |
{ |
static mapping(string:array(Node)) status_set = ([]); |
|
static mapping(string:string) args = ([ |
"xmlns:DAV": "DAV:", |
|
|
|
|
"xmlns:MS": "urn:schemas-microsoft-com:datatypes", |
]); |
|
int(0..1) is_empty() |
{ |
return !sizeof(status_set); |
} |
|
void add_response(string href, Node response_node) |
{ |
if (!status_set[href]) { |
status_set[href] = ({ response_node }); |
} else { |
status_set[href] += ({ response_node }); |
} |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void add_property(string href, string prop_name, |
void|int(0..0)|string|array(Node)|Node| |
mapping(string:mixed) prop_value) |
{ |
array(XMLStatusNode) stat_nodes; |
XMLStatusNode stat_node; |
int code = 200; |
string message; |
|
if (mappingp(prop_value)) { |
code = prop_value->error; |
message = prop_value->rettext; |
} |
|
DAV_WERROR("Adding property %O code:%O val:%O\n", |
prop_name, code, prop_value); |
if (!(stat_nodes = status_set[href])) { |
status_set[href] = ({ stat_node = XMLPropStatNode(code, message) }); |
} else { |
int index; |
for (; index < sizeof (stat_nodes); index++) { |
XMLStatusNode n = stat_nodes[index]; |
if (n->http_code == code) { |
stat_node = n; |
break; |
} |
if (n->http_code > code) break; |
} |
if (!stat_node) { |
status_set[href] = |
status_set[href][..index-1] + |
({ stat_node = XMLPropStatNode(code, message) }) + |
status_set[href][index..]; |
} |
} |
if (message) { |
|
stat_node->add_description(message); |
prop_value = 0; |
} |
stat_node->add_property(prop_name, prop_value); |
} |
|
void add_namespace (string namespace) |
|
|
{ |
int ns_count = 0; |
string ns_name; |
while (args[ns_name = "xmlns:NS" + ns_count]) { |
if (args[ns_name] == namespace) return; |
ns_count++; |
} |
args[ns_name] = namespace; |
} |
|
Node get_xml_node() |
{ |
RootNode root = RootNode(); |
root->add_child (HeaderNode ((["version": "1.0", "encoding": "utf-8"]))); |
ElementNode node = ElementNode ("DAV:multistatus", args); |
root->add_child (node); |
|
array(Node) response_xml = allocate(sizeof(status_set)); |
int i; |
|
DAV_WERROR("Generating XML Nodes for status_set:%O\n", |
status_set); |
|
foreach(sort(indices(status_set)), string href) { |
array(Node) responses = status_set[href]; |
Node href_node = ElementNode("DAV:href", ([])); |
href_node->add_child(TextNode(href)); |
(response_xml[i++] = |
ElementNode("DAV:response", ([])))-> |
replace_children(({href_node})+responses); |
} |
node->replace_children(response_xml); |
|
return root; |
} |
|
mapping(string:mixed) http_answer() |
{ |
string xml = get_xml_node()->render_xml(); |
return ([ |
"error": 207, |
"data": xml, |
"len": sizeof(xml), |
"type": "text/xml; charset=\"utf-8\"", |
]); |
} |
|
class prefix(static string href_prefix) { |
int(0..1) is_empty() |
{ |
return MultiStatus::is_empty(); |
} |
void add_response(string href, Node response_node) { |
MultiStatus::add_response(href_prefix + href, response_node); |
} |
void add_property(string path, string prop_name, |
void|int(0..0)|string|Node|mapping(string:mixed) |
prop_value) |
{ |
MultiStatus::add_property(href_prefix + path, prop_name, prop_value); |
} |
void add_namespace (string namespace) |
{ |
MultiStatus::add_namespace (namespace); |
} |
this_program prefix(string href_prefix) { |
return MultiStatus::prefix(this_program::href_prefix + href_prefix); |
} |
Node get_xml_node() |
{ |
return MultiStatus::get_xml_node(); |
} |
mapping(string:mixed) http_answer() |
{ |
return MultiStatus::http_answer(); |
} |
} |
} |
|
|
static class PropertySet |
{ |
RequestID id; |
string path; |
Stat st; |
multiset(string) query_all_properties(); |
string|array(Parser.XML.Tree.Node)|mapping(string:mixed) |
query_property(string prop_name); |
mapping(string:mixed) start(); |
void unroll(); |
void commit(); |
mapping(string:mixed) set_property(string prop_name, |
string|array(Parser.XML.Tree.Node) value); |
mapping(string:mixed) set_dead_property(string prop_name, |
array(Parser.XML.Tree.Node) value); |
mapping(string:mixed) remove_property(string prop_name); |
mapping(string:mixed) find_properties(string mode, |
MultiStatus result, |
multiset(string)|void filt); |
} |
|
class RoxenModule |
{ |
inherit BasicDefvar; |
constant is_module = 1; |
constant module_type = 0; |
constant module_unique = 1; |
LocaleString module_name; |
LocaleString module_doc; |
|
string module_identifier(); |
string module_local_id(); |
|
array(int|string|mapping) register_module(); |
string file_name_and_stuff(); |
|
void start(void|int num, void|object conf); |
|
string query_internal_location(); |
string query_location(); |
string query_provides(); |
function(RequestID:int|mapping) query_seclevels(); |
array(int)|object(Stdio.Stat) stat_file(string f, RequestID id); |
array(string) find_dir(string f, RequestID id); |
mapping(string:array(mixed)) find_dir_stat(string f, RequestID id); |
string real_file(string f, RequestID id); |
void save(); |
mapping api_functions(); |
mapping query_tag_callers(); |
mapping query_container_callers(); |
|
string info(object conf); |
string comment(); |
|
PropertySet|mapping(string:mixed) query_properties(string path, RequestID id); |
string|array(Parser.XML.Tree.Node)|mapping(string:mixed) |
query_property(string path, string prop_name, RequestID id); |
void recurse_find_properties(string path, string mode, int depth, |
MultiStatus result, RequestID id, |
multiset(string)|void filt); |
mapping(string:mixed) patch_properties(string path, |
array(PatchPropertyCommand) instructions, |
MultiStatus result, RequestID id); |
mapping(string:mixed) set_property (string path, string prop_name, |
string|array(Parser.XML.Tree.Node) value, |
RequestID id); |
mapping(string:mixed) remove_property (string path, string prop_name, |
RequestID id); |
multiset(DAVLock) find_all_locks(string path, |
int(0..1) recursive, |
RequestID id); |
DAVLock|int(-2..1) check_locks(string path, |
int(0..1) recursive, |
RequestID id); |
mapping(string:mixed)|int(0..1) lock_file(string path, |
DAVLock lock, |
RequestID id); |
mapping(string:mixed) unlock_file (string path, |
DAVLock lock, |
RequestID id); |
mapping(string:mixed)|int(-1..0)|Stdio.File find_file(string path, |
RequestID id); |
mapping(string:mixed) delete_file(string path, RequestID id); |
int(0..1) recurse_delete_files(string path, MultiStatus result, RequestID id); |
} |
|
class PatchPropertyCommand |
{ |
constant command = ""; |
string property_name; |
mapping(string:mixed) execute(PropertySet context); |
} |
|
class _roxen |
{ |
mapping(string:object) variables; |
constant real_version = ""; |
object locale; |
int start_time; |
array(Configuration) configurations; |
|
mixed query(string a); |
void store(string a, mapping b, int c, object d); |
mapping(string:mixed) retrieve(string a, object b); |
void remove(string a, object b); |
string version(); |
void dump(string a); |
void nwrite(string a, int|void b, int|void c, void|mixed ... d); |
int main(int a, array(string) b); |
} |
|
class AuthModule |
|
|
{ |
inherit RoxenModule; |
constant module_type = MODULE_AUTH; |
constant thread_safe=1; |
|
constant name = "method name"; |
|
User authenticate( RequestID id, UserDB db ); |
|
|
|
|
|
mapping authenticate_throw( RequestID id, string realm, UserDB db ); |
|
|
|
|
} |
|
static mapping(string:function(void:void)) user_sql_inited = ([]); |
static Sql.Sql user_mysql; |
static void init_user_sql(string table) |
{ |
string db = all_constants()->REPLICATE?"replicate":"local"; |
if( !user_mysql ) |
user_mysql = master()->resolv("DBManager.get")( db ); |
if(catch(user_mysql->query( "SELECT module FROM "+ |
table+" WHERE module=''"))) |
{ |
user_mysql->query( "CREATE TABLE "+table+" " |
" (module varchar(30) NOT NULL, " |
" name varchar(30) NOT NULL, " |
" user varchar(30) NOT NULL, " |
" value blob, " |
" raw int not null, " |
" INDEX foo (module,name,user))" ); |
master()->resolv("DBManager.is_module_table")( 0, db, table, |
"Contains metadata about users. " |
"Userdatabases can store information here " |
"at the request of other modules, or they " |
"can keep their own state in this table" ); |
} |
user_sql_inited[ table ]= |
lambda(){user_mysql = master()->resolv("DBManager.get")( db );}; |
} |
|
|
class Group( UserDB database ) |
{ |
string name(); |
|
|
array(string) members() |
|
|
|
|
{ |
array res = ({}); |
User uid; |
int id = gid(); |
foreach( database->list_users(), string u ) |
if( (uid = database->find_user( u )) && |
((uid->gid() == id) || has_value(uid->groups(), name()))) |
res += ({ u }); |
return res; |
} |
|
int gid(); |
|
|
|
int set_name( string new_name ) { return 0; } |
int set_gid( int new_gid ) { return 0; } |
int set_members( array(string) members ) { return 0; } |
|
|
} |
|
#ifdef THREADS |
static Thread.Mutex mutex = Thread.Mutex(); |
#endif |
|
|
class User( UserDB database ) |
{ |
static string table; |
|
string name(); |
|
|
string real_name(); |
|
|
int password_authenticate(string password) |
|
|
{ |
string c = crypted_password(); |
return !sizeof(c) || crypt(password, c); |
} |
|
int uid(); |
|
|
int gid(); |
|
|
string shell(); |
|
|
string gecos() |
|
{ |
return real_name(); |
} |
|
string homedir(); |
string crypted_password() { return "x"; } |
|
|
array(string) groups() |
|
|
{ |
return ({}); |
} |
|
int set_name(string name) {} |
int set_real_name(string rname) {} |
int set_uid(int uid) {} |
int set_gid(int gid) {} |
int set_shell(string shell) {} |
int set_gecos(string gecos) {} |
int set_homedir(string hodir) {} |
int set_crypted_password(string passwd) {} |
int set_password(string passwd) {} |
|
|
array compat_userinfo( ) |
|
|
|
|
|
|
|
|
{ |
return ({name(),crypted_password(),uid(),gid(),gecos(),homedir(),shell()}); |
} |
|
|
#define INIT_SQL() do{ \ |
if(!table) table = replace(database->my_configuration()->name," ","_")+"_user_variables"; \ |
if(!user_sql_inited[ table ] )init_user_sql( table );else user_sql_inited[ table ](); \ |
} while( 0 ) |
|
|
#ifdef THREADS |
#define LOCK() mixed ___key = mutex->lock() |
#else |
#define LOCK() |
#endif |
|
static string module_name( RoxenModule module ) |
{ |
if( !module ) |
|
|
return "'0'"; |
else |
return replace("'"+user_mysql->quote(module->sname())+"'","%","%%"); |
} |
|
mixed set_var( RoxenModule module, string index, mixed value ) |
|
|
|
|
|
|
|
|
|
|
|
{ |
delete_var( module, index ); |
mixed oval = value; |
LOCK(); |
INIT_SQL(); |
int encoded; |
|
if( stringp( value ) ) |
value = string_to_utf8( value ); |
else |
{ |
value = encode_value( value ); |
encoded = 1; |
} |
|
user_mysql->query( |
"INSERT INTO "+table+" (module,name,user,value,raw) " |
"VALUES ("+module_name( module )+", %s, %s, %s, %d)", |
index, name(), value, encoded |
); |
return oval; |
} |
|
mixed get_var( RoxenModule module, string index ) |
|
{ |
array rows; |
LOCK(); |
INIT_SQL(); |
rows = user_mysql->query( "SELECT * FROM "+table+ |
" WHERE module="+module_name( module ) |
+" AND name=%s AND user=%s", |
index, name() ); |
if( !sizeof( rows ) ) |
return 0; |
mapping m = rows[0]; |
|
if( (int)m->raw ) |
return decode_value( m->value ); |
return utf8_to_string( m->value ); |
} |
|
void delete_var( RoxenModule module, string index ) |
|
{ |
LOCK(); |
INIT_SQL(); |
user_mysql->query( "DELETE FROM "+table+" WHERE (module="+ |
module_name( module )+ |
" AND name=%s AND user=%s)", index, name() ); |
} |
#undef INIT_SQL |
#undef LOCK |
} |
|
class UserDB |
|
|
{ |
inherit RoxenModule; |
constant module_type = MODULE_USERDB; |
constant thread_safe=1; |
|
constant name = "db name"; |
|
User find_user( string s, RequestID|void id ); |
|
|
User find_user_from_uid( int uid, RequestID|void id ) |
|
|
{ |
User user; |
foreach( list_users(), string u ) |
if( (user = find_user( u )) && (user->uid() == uid) ) |
return user; |
} |
|
Group find_group( string group, RequestID|void id ) |
|
|
{ |
} |
|
Group find_group_from_gid( int gid, RequestID|void id ) |
|
|
{ |
Group group; |
foreach( list_groups(), string u ) |
if( (group = find_group( u )) && (group->gid() == gid) ) |
return group; |
} |
|
array(string) list_groups( RequestID|void id ) |
|
|
{ |
return ({}); |
} |
|
array(string) list_users( RequestID|void id ); |
|
|
User create_user( string s ) |
|
|
|
{ |
return 0; |
} |
|
Group create_group( string s ) |
|
|
|
{ |
return 0; |
} |
} |
|
|
|