24c6c1 | 2000-02-20 | Martin Nilsson | |
|
edc9af | 1998-07-11 | David Hedbor | |
|
77c7e9 | 2000-11-02 | Peter J. Holzer | | constant cvs_version="$Id: roxen.pike,v 1.569 2000/11/02 09:10:56 per Exp $";
|
34d3fa | 1999-04-22 | Per Hedbor | |
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
34d3fa | 1999-04-22 | Per Hedbor | | object backend_thread;
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
855c39 | 1999-11-29 | Per Hedbor | | ArgCache argcache;
|
172054 | 1998-10-04 | Henrik Grubbström (Grubba) | |
|
4dd97c | 1996-12-04 | Per Hedbor | | #define IN_ROXEN
|
8540e1 | 1997-06-14 | Francesco Chemolli | | #include <roxen.h>
#include <config.h>
|
b1fca0 | 1996-11-12 | Per Hedbor | | #include <module.h>
#include <variables.h>
|
753a83 | 1999-08-30 | Per Hedbor | | #include <stat.h>
|
14179b | 1997-01-29 | Per Hedbor | |
|
172054 | 1998-10-04 | Henrik Grubbström (Grubba) | |
|
753a83 | 1999-08-30 | Per Hedbor | | inherit "global_variables";
|
b1fca0 | 1996-11-12 | Per Hedbor | | inherit "hosts";
inherit "disk_cache";
|
970e24 | 2000-09-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | inherit "supports";
|
08eba6 | 2000-07-09 | Per Hedbor | | inherit "module_support";
|
970e24 | 2000-09-12 | Per Hedbor | | inherit "config_userdb";
|
7c92b9 | 1998-11-19 | Per Hedbor | |
|
23414a | 2000-07-21 | Andreas Lange | |
#define LOC_S(X,Y) _STR_LOCALE("roxen_start",X,Y)
#define LOC_M(X,Y) _STR_LOCALE("roxen_message",X,Y)
#define CALL_M(X,Y) _LOCALE_FUN("roxen_message",X,Y)
|
be788e | 2000-03-27 | Martin Nilsson | |
|
81f8af | 1999-12-20 | Martin Nilsson | |
#ifdef SSL3_DEBUG
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | # define SSL3_WERR(X) werror("SSL3: "+X+"\n")
|
81f8af | 1999-12-20 | Martin Nilsson | | #else
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | # define SSL3_WERR(X)
|
81f8af | 1999-12-20 | Martin Nilsson | | #endif
#ifdef THREAD_DEBUG
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | # define THREAD_WERR(X) werror("Thread: "+X+"\n")
|
81f8af | 1999-12-20 | Martin Nilsson | | #else
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | # define THREAD_WERR(X)
|
81f8af | 1999-12-20 | Martin Nilsson | | #endif
|
60a912 | 1999-10-10 | Henrik Grubbström (Grubba) | |
|
970e24 | 2000-09-12 | Per Hedbor | | string query_configuration_dir()
{
return configuration_dir;
}
|
855c39 | 1999-11-29 | Per Hedbor | |
|
596425 | 1999-11-19 | Per Hedbor | | string filename( program|object o )
|
95b69e | 1999-09-05 | Per Hedbor | | {
|
596425 | 1999-11-19 | Per Hedbor | | if( objectp( o ) )
o = object_program( o );
|
2537c3 | 2000-02-14 | Per Hedbor | | string fname = master()->program_name( o );
|
596425 | 1999-11-19 | Per Hedbor | | if( !fname )
fname = "Unknown Program";
return fname-(getcwd()+"/");
|
95b69e | 1999-09-05 | Per Hedbor | | }
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | #ifdef THREADS
|
855c39 | 1999-11-29 | Per Hedbor | | Thread.Mutex euid_egid_lock = Thread.Mutex();
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | #endif /* THREADS */
|
c5e096 | 1999-10-04 | Per Hedbor | | |
8fb517 | 2000-05-27 | Per Hedbor | | * The privilege changer. Works like a mutex lock, but changes the UID/GID
* while held. Blocks all threads.
*
|
c5e096 | 1999-10-04 | Per Hedbor | | * Based on privs.pike,v 1.36.
*/
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | int privs_level;
|
81f8af | 1999-12-20 | Martin Nilsson | | static class Privs
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | #if efun(seteuid)
int saved_uid;
int saved_gid;
int new_uid;
int new_gid;
|
7b798d | 2000-07-04 | Per Hedbor | | #define LOGP (variables && variables->audit && variables->audit->query())
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | |
#if constant(geteuid) && constant(getegid) && constant(seteuid) && constant(setegid)
#define HAVE_EFFECTIVE_USER
#endif
static private string _getcwd()
{
if (catch{return(getcwd());}) {
return("Unknown directory (no x-bit on current directory?)");
}
}
static private string dbt(array t)
{
if(!arrayp(t) || (sizeof(t)<2)) return "";
return (((t[0]||"Unknown program")-(_getcwd()+"/"))-"base_server/")+":"+t[1]+"\n";
}
#ifdef THREADS
|
b84a16 | 1999-06-07 | Martin Stjernholm | | static mixed mutex_key;
|
d3a71c | 1999-04-20 | Martin Stjernholm | | static object threads_disabled;
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | #endif /* THREADS */
int p_level;
void create(string reason, int|string|void uid, int|string|void gid)
{
#ifdef PRIVS_DEBUG
werror(sprintf("Privs(%O, %O, %O)\n"
"privs_level: %O\n",
reason, uid, gid, privs_level));
#endif /* PRIVS_DEBUG */
#ifdef HAVE_EFFECTIVE_USER
array u;
#ifdef THREADS
if (euid_egid_lock) {
catch { mutex_key = euid_egid_lock->lock(); };
}
|
d3a71c | 1999-04-20 | Martin Stjernholm | | threads_disabled = _disable_threads();
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | #endif /* THREADS */
p_level = privs_level++;
if(getuid()) return;
saved_uid = geteuid();
saved_gid = getegid();
seteuid(0);
|
c5e096 | 1999-10-04 | Per Hedbor | | if(stringp(uid) && (replace(uid,"0123456789"/"",({""})*10)==""))
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | uid = (int)uid;
|
c5e096 | 1999-10-04 | Per Hedbor | |
|
3a4b9a | 1999-12-27 | Martin Nilsson | | if(stringp(gid) && (replace(gid, "0123456789"/"", ({"" })*10) == ""))
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | gid = (int)gid;
|
c5e096 | 1999-10-04 | Per Hedbor | | if(!stringp(uid))
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | u = getpwuid(uid);
|
81f8af | 1999-12-20 | Martin Nilsson | | else
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | u = getpwnam(uid);
|
81f8af | 1999-12-20 | Martin Nilsson | | if(u)
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | uid = u[2];
}
|
81f8af | 1999-12-20 | Martin Nilsson | | if(u && !gid)
|
c5e096 | 1999-10-04 | Per Hedbor | | gid = u[3];
|
81f8af | 1999-12-20 | Martin Nilsson | |
if(!u)
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
81f8af | 1999-12-20 | Martin Nilsson | | if (uid && (uid != "root"))
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
81f8af | 1999-12-20 | Martin Nilsson | | if (intp(uid) && (uid >= 60000))
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
b84a16 | 1999-06-07 | Martin Stjernholm | | report_warning(sprintf("Privs: User %d is not in the password database.\n"
"Assuming nobody.\n", uid));
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | |
gid = gid || uid;
u = ({ "fake-nobody", "x", uid, gid, "A real nobody", "/", "/sbin/sh" });
} else {
error("Unknown user: "+uid+"\n");
}
} else {
u = ({ "root", "x", 0, gid, "The super-user", "/", "/sbin/sh" });
}
}
if(LOGP)
|
49cd28 | 2000-08-11 | Andreas Lange | | report_notice(LOC_M(1, "Change to %s(%d):%d privs wanted (%s), from %s"),
|
23414a | 2000-07-21 | Andreas Lange | | (string)u[0], (int)uid, (int)gid,
(string)reason,
(string)dbt(backtrace()[-2]));
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | |
#if efun(cleargroups)
catch { cleargroups(); };
#endif /* cleargroups */
#if efun(initgroups)
catch { initgroups(u[0], u[3]); };
#endif
gid = gid || getgid();
int err = (int)setegid(new_gid = gid);
if (err < 0) {
|
49cd28 | 2000-08-11 | Andreas Lange | | report_warning(LOC_M(2, "Privs: WARNING: Failed to set the "
|
23414a | 2000-07-21 | Andreas Lange | | "effective group id to %d!\n"
"Check that your password database is correct "
"for user %s(%d),\n and that your group "
"database is correct.\n"),
gid, (string)u[0], (int)uid);
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | int gid2 = gid;
#ifdef HPUX_KLUDGE
if (gid >= 60000) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Privs: WARNING: Assuming nobody-group.\n"
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | "Trying some alternatives...\n");
foreach(({ 60001, 65534, -2 }), gid2) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("%d... ", gid2);
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | if (initgroups(u[0], gid2) >= 0) {
if ((err = setegid(new_gid = gid2)) >= 0) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Success!\n");
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | break;
}
}
}
}
#endif /* HPUX_KLUDGE */
if (err < 0) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Privs: Failed\n");
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | throw(({ sprintf("Failed to set EGID to %d\n", gid), backtrace() }));
}
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Privs: WARNING: Set egid to %d instead of %d.\n",
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | gid2, gid);
gid = gid2;
}
if(getgid()!=gid) setgid(gid||getgid());
seteuid(new_uid = uid);
#endif /* HAVE_EFFECTIVE_USER */
}
void destroy()
{
#ifdef PRIVS_DEBUG
werror(sprintf("Privs->destroy()\n"
"privs_level: %O\n",
privs_level));
#endif /* PRIVS_DEBUG */
#ifdef HAVE_EFFECTIVE_USER
if (p_level >= privs_level) {
report_error(sprintf("Change back to uid#%d gid#%d from uid#%d gid#%d\n"
"in wrong order! Saved level:%d Current level:%d\n"
"Occurs in:\n%s\n",
saved_uid, saved_gid, new_uid, new_gid,
p_level, privs_level,
describe_backtrace(backtrace())));
return(0);
}
if (p_level != privs_level-1) {
report_error(sprintf("Change back to uid#%d gid#%d from uid#%d gid#%d\n"
"Skips privs level. Saved level:%d Current level:%d\n"
"Occurs in:\n%s\n",
saved_uid, saved_gid, new_uid, new_gid,
p_level, privs_level,
describe_backtrace(backtrace())));
}
privs_level = p_level;
if(LOGP) {
catch {
array bt = backtrace();
if (sizeof(bt) >= 2) {
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_notice(LOC_M(3,"Change back to uid#%d gid#%d, from %s")+"\n",
|
23414a | 2000-07-21 | Andreas Lange | | saved_uid, saved_gid, dbt(bt[-2]));
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | } else {
|
49cd28 | 2000-08-11 | Andreas Lange | | report_notice(LOC_M(4,"Change back to uid#%d gid#%d, "
|
c8ee71 | 2000-09-09 | Andreas Lange | | "from backend")+"\n", saved_uid, saved_gid);
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | }
};
}
if(getuid()) return;
|
81f8af | 1999-12-20 | Martin Nilsson | | #ifdef PRIVS_DEBUG
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | int uid = geteuid();
if (uid != new_uid) {
|
81f8af | 1999-12-20 | Martin Nilsson | | werror("Privs: UID #%d differs from expected #%d\n"
"%s\n",
uid, new_uid, describe_backtrace(backtrace()));
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | }
int gid = getegid();
if (gid != new_gid) {
|
81f8af | 1999-12-20 | Martin Nilsson | | werror("Privs: GID #%d differs from expected #%d\n"
"%s\n",
gid, new_gid, describe_backtrace(backtrace()));
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | }
|
81f8af | 1999-12-20 | Martin Nilsson | | #endif /* PRIVS_DEBUG */
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | |
seteuid(0);
array u = getpwuid(saved_uid);
#if efun(cleargroups)
catch { cleargroups(); };
#endif /* cleargroups */
if(u && (sizeof(u) > 3)) {
catch { initgroups(u[0], u[3]); };
}
setegid(saved_gid);
seteuid(saved_uid);
#endif /* HAVE_EFFECTIVE_USER */
}
|
dbfe9d | 2000-04-13 | Per Hedbor | | #else /* efun(seteuid) */
void create(string reason, int|string|void uid, int|string|void gid){}
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | | #endif /* efun(seteuid) */
|
c5e096 | 1999-10-04 | Per Hedbor | | }
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | |
static object PRIVS(string r, int|string|void u, int|string|void g)
{
return Privs(r, u, g);
}
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
fb7ef9 | 1998-11-02 | Per Hedbor | | object fonts;
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
d09399 | 2000-09-25 | Per Hedbor | |
|
9a8a15 | 2000-09-25 | Per Hedbor | | program _configuration;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
d09399 | 2000-09-25 | Per Hedbor | | array(Configuration) configurations = ({});
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
8fb517 | 2000-05-27 | Per Hedbor | |
local static int die_die_die;
|
506ede | 1997-11-11 | Henrik Grubbström (Grubba) | |
|
172054 | 1998-10-04 | Henrik Grubbström (Grubba) | |
|
a9d811 | 1998-09-01 | Henrik Grubbström (Grubba) | | private static void really_low_shutdown(int exit_code)
{
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
310710 | 1998-09-01 | Marcus Comstedt | | #ifdef THREADS
|
c974c9 | 1999-06-27 | Per Hedbor | | catch( stop_handler_threads() );
|
310710 | 1998-09-01 | Marcus Comstedt | | #endif /* THREADS */
|
8fb517 | 2000-05-27 | Per Hedbor | | exit( exit_code );
|
a9d811 | 1998-09-01 | Henrik Grubbström (Grubba) | | }
|
3a4b9a | 1999-12-27 | Martin Nilsson | |
|
a9d811 | 1998-09-01 | Henrik Grubbström (Grubba) | |
private static void low_shutdown(int exit_code)
{
|
81f8af | 1999-12-20 | Martin Nilsson | | catch
|
c5e096 | 1999-10-04 | Per Hedbor | | {
configurations->stop();
int pid;
if (exit_code) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Restarting Roxen.\n");
|
c5e096 | 1999-10-04 | Per Hedbor | | } else {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Shutting down Roxen.\n");
|
c5e096 | 1999-10-04 | Per Hedbor | |
}
};
|
8fb517 | 2000-05-27 | Per Hedbor | | call_out(really_low_shutdown, 0.1, exit_code);
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
a9d811 | 1998-09-01 | Henrik Grubbström (Grubba) | |
|
c974c9 | 1999-06-27 | Per Hedbor | |
|
81f8af | 1999-12-20 | Martin Nilsson | | void restart(float|void i)
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
81f8af | 1999-12-20 | Martin Nilsson | | {
|
23264c | 2000-10-17 | Per Hedbor | | werror(describe_backtrace(backtrace()));
|
81f8af | 1999-12-20 | Martin Nilsson | | call_out(low_shutdown, i, -1);
}
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
81f8af | 1999-12-20 | Martin Nilsson | | void shutdown(float|void i)
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
81f8af | 1999-12-20 | Martin Nilsson | | {
call_out(low_shutdown, i, 0);
|
f4e1b7 | 1999-10-08 | Per Hedbor | | }
|
14179b | 1997-01-29 | Per Hedbor | |
|
4820e0 | 1999-07-27 | Henrik Grubbström (Grubba) | |
|
2ff846 | 1999-09-02 | Per Hedbor | | #ifndef THREADS
|
172054 | 1998-10-04 | Henrik Grubbström (Grubba) | |
|
8fb517 | 2000-05-27 | Per Hedbor | | local static void unthreaded_handle(function f, mixed ... args)
|
3aaaa7 | 1997-06-12 | Wilhelm Köhler | | {
f(@args);
}
|
34fbbc | 1998-02-05 | Henrik Grubbström (Grubba) | | function handle = unthreaded_handle;
|
2ff846 | 1999-09-02 | Per Hedbor | | #else
|
7277a1 | 1999-09-02 | Per Hedbor | | function handle = threaded_handle;
|
2ff846 | 1999-09-02 | Per Hedbor | | #endif
|
34fbbc | 1998-02-05 | Henrik Grubbström (Grubba) | |
|
172054 | 1998-10-04 | Henrik Grubbström (Grubba) | |
|
34fbbc | 1998-02-05 | Henrik Grubbström (Grubba) | | #ifdef THREADS
|
4f4bc1 | 1998-02-04 | Per Hedbor | | object do_thread_create(string id, function f, mixed ... args)
{
object t = thread_create(f, @args);
catch(t->set_name( id ));
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | THREAD_WERR(id+" started");
|
4f4bc1 | 1998-02-04 | Per Hedbor | | return t;
}
|
676f58 | 2000-03-24 | Per Hedbor | |
class Queue
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
676f58 | 2000-03-24 | Per Hedbor | | {
inherit Thread.Condition : r_cond;
array buffer=allocate(8);
int r_ptr, w_ptr;
int size()
{
return w_ptr - r_ptr;
}
mixed read()
{
while(!(w_ptr - r_ptr)) r_cond::wait();
mixed tmp = buffer[r_ptr];
buffer[r_ptr++] = 0;
return tmp;
}
void write(mixed v)
{
if(w_ptr >= sizeof(buffer))
{
buffer=buffer[r_ptr..]+allocate(8);
w_ptr-=r_ptr;
r_ptr=0;
}
buffer[w_ptr++]=v;
r_cond::signal();
}
}
|
8fb517 | 2000-05-27 | Per Hedbor | | local static Queue handle_queue = Queue();
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
172054 | 1998-10-04 | Henrik Grubbström (Grubba) | |
|
8fb517 | 2000-05-27 | Per Hedbor | | local static int thread_reap_cnt;
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
14179b | 1997-01-29 | Per Hedbor | |
|
8fb517 | 2000-05-27 | Per Hedbor | | local static void handler_thread(int id)
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
14179b | 1997-01-29 | Per Hedbor | | {
|
4f4bc1 | 1998-02-04 | Per Hedbor | | array (mixed) h, q;
|
34d3fa | 1999-04-22 | Per Hedbor | | while(!die_die_die)
|
4f4bc1 | 1998-02-04 | Per Hedbor | | {
if(q=catch {
|
45cae3 | 1998-03-06 | Henrik Grubbström (Grubba) | | do {
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | THREAD_WERR("Handle thread ["+id+"] waiting for next event");
|
310710 | 1998-09-01 | Marcus Comstedt | | if((h=handle_queue->read()) && h[0]) {
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | THREAD_WERR(sprintf("Handle thread [%O] calling %O(@%O)...",
|
81f8af | 1999-12-20 | Martin Nilsson | | id, h[0], h[1..]));
|
67f60e | 2000-07-09 | Martin Nilsson | | set_locale();
|
45cae3 | 1998-03-06 | Henrik Grubbström (Grubba) | | h[0](@h[1]);
h=0;
|
310710 | 1998-09-01 | Marcus Comstedt | | } else if(!h) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Handle thread ["+id+"] stopped\n");
|
310710 | 1998-09-01 | Marcus Comstedt | | thread_reap_cnt--;
|
23007b | 2000-05-28 | Martin Nilsson | | #ifdef NSERIOUS
|
10089c | 2000-05-17 | Martin Nilsson | | if(!thread_reap_cnt) report_debug("+++ATH\n");
|
23007b | 2000-05-28 | Martin Nilsson | | #endif
|
310710 | 1998-09-01 | Marcus Comstedt | | return;
|
45cae3 | 1998-03-06 | Henrik Grubbström (Grubba) | | }
} while(1);
}) {
|
774c9e | 2000-01-13 | Henrik Grubbström (Grubba) | | if (h = catch {
|
434bac | 2000-07-14 | Andreas Lange | | report_error(
describe_backtrace(q));
|
774c9e | 2000-01-13 | Henrik Grubbström (Grubba) | | if (q = catch {h = 0;}) {
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(5, "Uncaught error in handler thread: %s Client"
"will not get any response from Roxen.")+"\n",
|
67f60e | 2000-07-09 | Martin Nilsson | | describe_backtrace(q));
|
774c9e | 2000-01-13 | Henrik Grubbström (Grubba) | | }
}) {
catch {
report_error("Error reporting error:\n");
report_error(sprintf("Raw error: %O\n", h[0]));
report_error(sprintf("Original raw error: %O\n", q[0]));
};
|
45cae3 | 1998-03-06 | Henrik Grubbström (Grubba) | | }
}
|
4f4bc1 | 1998-02-04 | Per Hedbor | | }
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
8fb517 | 2000-05-27 | Per Hedbor | | local static void threaded_handle(function f, mixed ... args)
|
3aaaa7 | 1997-06-12 | Wilhelm Köhler | | {
handle_queue->write(({f, args }));
}
|
14179b | 1997-01-29 | Per Hedbor | | int number_of_threads;
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | static array(object) handler_threads = ({});
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
14179b | 1997-01-29 | Per Hedbor | | void start_handler_threads()
{
|
a60c4c | 1997-07-03 | Henrik Grubbström (Grubba) | | if (QUERY(numthreads) <= 1) {
|
7b798d | 2000-07-04 | Per Hedbor | | set( "numthreads", 1 );
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_notice (LOC_S(1, "Starting one thread to handle requests.")+"\n");
|
23414a | 2000-07-21 | Andreas Lange | | } else {
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_notice (LOC_S(2, "Starting %d threads to handle requests.")+"\n",
|
23414a | 2000-07-21 | Andreas Lange | | QUERY(numthreads) );
|
a60c4c | 1997-07-03 | Henrik Grubbström (Grubba) | | }
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | array(object) new_threads = ({});
|
14179b | 1997-01-29 | Per Hedbor | | for(; number_of_threads < QUERY(numthreads); number_of_threads++)
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | new_threads += ({ do_thread_create( "Handle thread [" +
number_of_threads + "]",
handler_thread, number_of_threads ) });
handler_threads += new_threads;
|
14179b | 1997-01-29 | Per Hedbor | | }
|
06583f | 1997-09-03 | Per Hedbor | |
|
310710 | 1998-09-01 | Marcus Comstedt | | void stop_handler_threads()
|
434bac | 2000-07-14 | Andreas Lange | |
|
310710 | 1998-09-01 | Marcus Comstedt | | {
|
95b69e | 1999-09-05 | Per Hedbor | | int timeout=10;
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | #if constant(_reset_dmalloc)
timeout *= 10;
#endif /* constant(_reset_dmalloc) */
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Stopping all request handler threads.\n");
|
310710 | 1998-09-01 | Marcus Comstedt | | while(number_of_threads>0) {
number_of_threads--;
handle_queue->write(0);
thread_reap_cnt++;
}
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | handler_threads = ({});
|
310710 | 1998-09-01 | Marcus Comstedt | | while(thread_reap_cnt) {
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | sleep(0.1);
|
310710 | 1998-09-01 | Marcus Comstedt | | if(--timeout<=0) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Giving up waiting on threads!\n");
|
310710 | 1998-09-01 | Marcus Comstedt | | return;
}
}
}
|
49284b | 2000-02-17 | Per Hedbor | | #endif /* THREADS */
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
934b3f | 2000-02-04 | Per Hedbor | | mapping get_port_options( string key )
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
934b3f | 2000-02-04 | Per Hedbor | | {
return (query( "port_options" )[ key ] || ([]));
}
void set_port_options( string key, mapping value )
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
934b3f | 2000-02-04 | Per Hedbor | | {
mapping q = query("port_options");
q[ key ] = value;
set( "port_options" , q );
save( );
}
|
4c04e5 | 2000-07-25 | Marcus Comstedt | | class InternalRequestID
{
inherit RequestID;
|
b00cae | 2000-09-30 | Per Hedbor | | static void create()
|
4c04e5 | 2000-07-25 | Marcus Comstedt | | {
client = ({ "Roxen" });
prot = "INTERNAL";
method = "GET";
variables = ([]);
misc = ([]);
cookies = ([]);
throttle = ([]);
|
db4ca9 | 2000-09-18 | Henrik Grubbström (Grubba) | | client_var = ([]);
|
4c04e5 | 2000-07-25 | Marcus Comstedt | | request_headers = ([]);
prestate = (<>);
config = (<>);
supports = (<>);
pragma = (<>);
rest_query = "";
extra_extension = "";
}
}
|
49284b | 2000-02-17 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | class Protocol
|
c09144 | 2000-08-15 | Johan Sundström | |
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
761baa | 1999-12-08 | Per Hedbor | | inherit Stdio.Port: port;
|
934b3f | 2000-02-04 | Per Hedbor | | inherit "basic_defvar";
|
abc59a | 2000-08-23 | Per Hedbor | | int bound;
|
c5e096 | 1999-10-04 | Per Hedbor | |
|
5773f6 | 2000-08-27 | Martin Stjernholm | | string path;
|
c5e096 | 1999-10-04 | Per Hedbor | | constant name = "unknown";
constant supports_ipless = 0;
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | constant requesthandlerfile = "";
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c09144 | 2000-08-15 | Johan Sundström | |
|
c5e096 | 1999-10-04 | Per Hedbor | | constant default_port = 4711;
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
934b3f | 2000-02-04 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | int port;
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | string ip;
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
8fb517 | 2000-05-27 | Per Hedbor | | int refs;
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | program requesthandler;
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | array(string) sorted_urls = ({});
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | mapping(string:mapping) urls = ([]);
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | |
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | void ref(string name, mapping data)
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | if(urls[name])
|
8fb517 | 2000-05-27 | Per Hedbor | | {
urls[name] = data;
return;
}
|
5773f6 | 2000-08-27 | Martin Stjernholm | | if (!refs) path = data->path;
else if (path != (data->path || "")) path = 0;
|
c5e096 | 1999-10-04 | Per Hedbor | | refs++;
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | urls[name] = data;
|
24625e | 2000-08-28 | Per Hedbor | | sorted_urls = Array.sort_array(indices(urls),
lambda(string a, string b) {
return sizeof(a)<sizeof(b);
});
|
c5e096 | 1999-10-04 | Per Hedbor | | }
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | void unref(string name)
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
bc5c2a | 2000-08-23 | Per Hedbor | |
|
5773f6 | 2000-08-27 | Martin Stjernholm | |
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | m_delete(urls, name);
|
5773f6 | 2000-08-27 | Martin Stjernholm | | if (!path && sizeof (Array.uniq (values (urls)->path)) == 1)
path = values (urls)[0]->path;
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | sorted_urls -= ({name});
|
c5e096 | 1999-10-04 | Per Hedbor | | if( !--refs )
destruct( );
}
|
1550aa | 2000-08-12 | Per Hedbor | | mapping mu;
|
8fb517 | 2000-05-27 | Per Hedbor | | static void got_connection()
|
c5e096 | 1999-10-04 | Per Hedbor | | {
object q = accept( );
|
8fb517 | 2000-05-27 | Per Hedbor | | if( q )
|
1550aa | 2000-08-12 | Per Hedbor | | {
object c;
|
24625e | 2000-08-28 | Per Hedbor | | if( refs < 2 )
|
1550aa | 2000-08-12 | Per Hedbor | | {
if(!mu)
{
mu = urls[sorted_urls[0]];
|
1f4a6c | 2000-08-28 | Per Hedbor | | if(!(c=mu->conf)->inited )
c->enable_all_modules();
} else
c = mu->conf;
|
1550aa | 2000-08-12 | Per Hedbor | | }
requesthandler( q, this_object(), c );
}
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | }
|
8fb517 | 2000-05-27 | Per Hedbor | | local function sp_fcfu;
|
4c8777 | 2000-08-11 | Per Hedbor | |
|
347c72 | 2000-08-31 | Per Hedbor | | #define INIT(X) do{mapping _=(X);string __=_->path;c=_->conf;if(__&&id->adjust_for_config_path) id->adjust_for_config_path(__);if(!c->inited)c->enable_all_modules(); } while(0)
|
9a8a15 | 2000-09-25 | Per Hedbor | | Configuration find_configuration_for_url( string url, RequestID id,
int|void no_default )
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | {
|
9a8a15 | 2000-09-25 | Per Hedbor | | Configuration c;
|
4c8777 | 2000-08-11 | Per Hedbor | | if( sizeof( urls ) == 1 )
{
|
347c72 | 2000-08-31 | Per Hedbor | | if(!mu) mu=urls[sorted_urls[0]];
INIT( mu );
|
4c8777 | 2000-08-11 | Per Hedbor | | return c;
}
url = lower_case( url );
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | foreach( sorted_urls, string in )
{
if( glob( in+"*", url ) )
{
|
347c72 | 2000-08-31 | Per Hedbor | | INIT( urls[in] );
|
1d7d6d | 2000-02-16 | Per Hedbor | | return c;
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | }
}
|
8fb517 | 2000-05-27 | Per Hedbor | |
if( no_default )
return 0;
|
347c72 | 2000-08-31 | Per Hedbor | |
|
2ad7d0 | 2000-03-27 | Per Hedbor | | mixed i;
|
347c72 | 2000-08-31 | Per Hedbor | | if( !functionp(sp_fcfu) && ( i=open_ports[ name ][ 0 ][ port ] ) )
|
8fb517 | 2000-05-27 | Per Hedbor | | sp_fcfu = i->find_configuration_for_url;
|
347c72 | 2000-08-31 | Per Hedbor | | if( sp_fcfu && (sp_fcfu != find_configuration_for_url)
|
8fb517 | 2000-05-27 | Per Hedbor | | && (i = sp_fcfu( url, id, 1 )))
|
7120b8 | 2000-03-27 | Per Hedbor | | return i;
|
8fb517 | 2000-05-27 | Per Hedbor | |
multiset choices = (< >);
|
d09399 | 2000-09-25 | Per Hedbor | | foreach( configurations, Configuration c )
|
8fb517 | 2000-05-27 | Per Hedbor | | if( c->query( "default_server" ) )
choices |= (< c >);
if( sizeof( choices ) )
|
7120b8 | 2000-03-27 | Per Hedbor | | {
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
347c72 | 2000-08-31 | Per Hedbor | | foreach( values(urls), mapping cc )
if( choices[ cc->conf ] )
|
8fb517 | 2000-05-27 | Per Hedbor | | {
|
347c72 | 2000-08-31 | Per Hedbor | | INIT( cc );
return c;
|
8fb517 | 2000-05-27 | Per Hedbor | | }
|
347c72 | 2000-08-31 | Per Hedbor | |
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
347c72 | 2000-08-31 | Per Hedbor | |
|
1a231e | 2000-06-04 | Martin Nilsson | | c = ((array)choices)[0];
if(!c->inited) c->enable_all_modules();
return c;
|
7120b8 | 2000-03-27 | Per Hedbor | | }
|
8fb517 | 2000-05-27 | Per Hedbor | |
|
347c72 | 2000-08-31 | Per Hedbor | | INIT( urls[sorted_urls[0]] );
|
8fb517 | 2000-05-27 | Per Hedbor | | id->misc->defaulted=1;
return c;
|
c5e096 | 1999-10-04 | Per Hedbor | | }
|
934b3f | 2000-02-04 | Per Hedbor | | mixed query_option( string x )
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
10bdc5 | 1999-10-08 | Henrik Grubbström (Grubba) | | {
|
934b3f | 2000-02-04 | Per Hedbor | | return query( x );
|
10bdc5 | 1999-10-08 | Henrik Grubbström (Grubba) | | }
|
934b3f | 2000-02-04 | Per Hedbor | | string get_key()
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | {
|
934b3f | 2000-02-04 | Per Hedbor | | return name+":"+ip+":"+port;
}
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
934b3f | 2000-02-04 | Per Hedbor | | void save()
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
934b3f | 2000-02-04 | Per Hedbor | | {
set_port_options( get_key(),
mkmapping( indices(variables),
map(indices(variables),query)));
}
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
934b3f | 2000-02-04 | Per Hedbor | | void restore()
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
934b3f | 2000-02-04 | Per Hedbor | | {
foreach( (array)get_port_options( get_key() ), array kv )
set( kv[0], kv[1] );
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | }
|
8fb517 | 2000-05-27 | Per Hedbor | | static void create( int pn, string i )
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
934b3f | 2000-02-04 | Per Hedbor | | port = pn;
ip = i;
restore();
|
c5e096 | 1999-10-04 | Per Hedbor | | if( !requesthandler )
requesthandler = (program)requesthandlerfile;
::create();
|
81f8af | 1999-12-20 | Martin Nilsson | | if(!bind( port, got_connection, ip ))
|
b6fb05 | 1999-11-02 | Per Hedbor | | {
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(6, "Failed to bind %s://%s:%d/ (%s)")+"\n",
|
23414a | 2000-07-21 | Andreas Lange | | (string)name, (ip||"*"), (int)port, strerror( errno() ));
|
abc59a | 2000-08-23 | Per Hedbor | | bound = 0;
} else
bound = 1;
|
c5e096 | 1999-10-04 | Per Hedbor | | }
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
8fb517 | 2000-05-27 | Per Hedbor | | static string _sprintf( )
|
b6fb05 | 1999-11-02 | Per Hedbor | | {
|
c8ee71 | 2000-09-09 | Andreas Lange | | return "Protocol("+name+"://"+ip+":"+port+")";
|
b6fb05 | 1999-11-02 | Per Hedbor | | }
|
c5e096 | 1999-10-04 | Per Hedbor | | }
|
4820e0 | 1999-07-27 | Henrik Grubbström (Grubba) | |
|
b8fd5c | 2000-09-28 | Per Hedbor | | #if constant(SSL.sslfile)
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | class SSLProtocol
|
a6e4a1 | 2000-07-09 | Per Hedbor | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | {
inherit Protocol;
object ctx;
class destruct_protected_sslfile
{
object sslfile;
mixed `[](string s)
{
return sslfile[s];
}
|
60a912 | 1999-10-10 | Henrik Grubbström (Grubba) | | mixed `[]=(string s, mixed val)
{
return sslfile[s] = val;
}
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | mixed `->(string s)
{
return sslfile[s];
}
|
60a912 | 1999-10-10 | Henrik Grubbström (Grubba) | | mixed `->=(string s, mixed val)
{
return sslfile[s] = val;
}
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | void destroy()
{
|
2dd46b | 2000-03-24 | Per Hedbor | | if (sslfile)
|
60a912 | 1999-10-10 | Henrik Grubbström (Grubba) | | sslfile->close();
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | }
void create(object q, object ctx)
{
sslfile = SSL.sslfile(q, ctx);
}
}
object accept()
{
object q = ::accept();
|
2dd46b | 2000-03-24 | Per Hedbor | | if (q) return destruct_protected_sslfile(q, ctx);
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | return 0;
}
void create(int pn, string i)
{
ctx = SSL.context();
|
6f72d4 | 2000-02-08 | Per Hedbor | | set_up_ssl_variables( this_object() );
|
2dd46b | 2000-03-24 | Per Hedbor | | port = pn;
ip = i;
|
934b3f | 2000-02-04 | Per Hedbor | |
|
2dd46b | 2000-03-24 | Per Hedbor | | restore();
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | object privs = Privs("Reading cert file");
|
6f72d4 | 2000-02-08 | Per Hedbor | |
|
2dd46b | 2000-03-24 | Per Hedbor | | string f, f2;
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
2dd46b | 2000-03-24 | Per Hedbor | | if( catch{ f = lopen(query_option("ssl_cert_file"), "r")->read(); } )
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(8,"SSL3: Reading cert-file failed!")+"\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
|
2dd46b | 2000-03-24 | Per Hedbor | | if( strlen(query_option("ssl_key_file")) &&
catch{ f2 = lopen(query_option("ssl_key_file"),"r")->read(); } )
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(9, "SSL3: Reading key-file failed!")+"\n");
|
2dd46b | 2000-03-24 | Per Hedbor | | destruct();
return;
}
if (privs)
destruct(privs);
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
2dd46b | 2000-03-24 | Per Hedbor | | object msg = Tools.PEM.pem_msg()->init( f );
object part = msg->parts["CERTIFICATE"] || msg->parts["X509 CERTIFICATE"];
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | string cert;
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
2dd46b | 2000-03-24 | Per Hedbor | | if (!part || !(cert = part->decoded_body()))
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(10, "SSL3: No certificate found.")+"\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
2dd46b | 2000-03-24 | Per Hedbor | | if( f2 )
msg = Tools.PEM.pem_msg()->init( f2 );
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
function r = Crypto.randomness.reasonably_random()->read;
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | SSL3_WERR(sprintf("key file contains: %O", indices(msg->parts)));
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
if (part = msg->parts["RSA PRIVATE KEY"])
{
string key;
|
2dd46b | 2000-03-24 | Per Hedbor | | if (!(key = part->decoded_body()))
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(11,"SSL3: Private rsa key not valid")+" (PEM).\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | object rsa = Standards.PKCS.RSA.parse_private_key(key);
|
2dd46b | 2000-03-24 | Per Hedbor | | if (!rsa)
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(11, "SSL3: Private rsa key not valid")+" (DER).\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
ctx->rsa = rsa;
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | SSL3_WERR(sprintf("RSA key size: %d bits", rsa->rsa_size()));
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | if (rsa->rsa_size() > 512)
{
ctx->short_rsa = Crypto.rsa()->generate_key(512, r);
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
}
ctx->rsa_mode();
object tbs = Tools.X509.decode_certificate (cert);
|
2dd46b | 2000-03-24 | Per Hedbor | | if (!tbs)
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(13,"SSL3: Certificate not valid (DER).")+"\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
|
2dd46b | 2000-03-24 | Per Hedbor | | if (!tbs->public_key->rsa->public_key_equal (rsa))
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(14, "SSL3: Certificate and private key do not "
"match.")+"\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
}
else if (part = msg->parts["DSA PRIVATE KEY"])
{
string key;
|
2dd46b | 2000-03-24 | Per Hedbor | | if (!(key = part->decoded_body()))
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(15,"SSL3: Private dsa key not valid")+" (PEM).\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | object dsa = Standards.PKCS.DSA.parse_private_key(key);
|
2dd46b | 2000-03-24 | Per Hedbor | | if (!dsa)
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(15,"SSL3: Private dsa key not valid")+" (DER).\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | SSL3_WERR(sprintf("Using DSA key."));
|
2f74c3 | 2000-01-21 | Per Hedbor | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | dsa->use_random(r);
ctx->dsa = dsa;
ctx->dh_params = SSL.cipher.dh_parameters();
ctx->dhe_dss_mode();
}
|
2dd46b | 2000-03-24 | Per Hedbor | | else
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(17,"SSL3: No private key found.")+"\n");
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | destruct();
return;
}
ctx->certificates = ({ cert });
ctx->random = r;
#if EXPORT
ctx->export_mode();
#endif
::create(pn, i);
}
|
b8fd5c | 2000-09-28 | Per Hedbor | |
|
b6fb05 | 1999-11-02 | Per Hedbor | | string _sprintf( )
{
return "SSLProtocol("+name+"://"+ip+":"+port+")";
}
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | }
|
b8fd5c | 2000-09-28 | Per Hedbor | | #endif
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
761baa | 1999-12-08 | Per Hedbor | | #if constant(HTTPLoop.prog)
class FHTTP
{
inherit Protocol;
constant supports_ipless=1;
constant name = "fhttp";
constant default_port = 80;
int dolog;
int requests, received, sent;
HTTPLoop.Loop l;
Stdio.Port portobj;
mapping flatten_headers( mapping from )
{
mapping res = ([]);
foreach(indices(from), string f)
res[f] = from[f]*", ";
return res;
}
void setup_fake(object o)
{
mapping vars = ([]);
o->extra_extension = "";
o->misc = flatten_headers(o->headers);
o->cmf = 100*1024;
o->cmp = 100*1024;
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
761baa | 1999-12-08 | Per Hedbor | | if(o->method == "POST" && strlen(o->data))
{
mapping variabels = ([]);
switch((o->misc["content-type"]/";")[0])
{
default:
break;
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
761baa | 1999-12-08 | Per Hedbor | | case "multipart/form-data":
object messg = MIME.Message(o->data, o->misc);
mapping misc = o->misc;
|
81f8af | 1999-12-20 | Martin Nilsson | | foreach(messg->body_parts, object part)
|
761baa | 1999-12-08 | Per Hedbor | | {
|
81f8af | 1999-12-20 | Martin Nilsson | | if(part->disp_params->filename)
|
761baa | 1999-12-08 | Per Hedbor | | {
vars[part->disp_params->name]=part->getdata();
vars[part->disp_params->name+".filename"]=
part->disp_params->filename;
if(!misc->files)
misc->files = ({ part->disp_params->name });
else
misc->files += ({ part->disp_params->name });
} else {
vars[part->disp_params->name]=part->getdata();
}
}
break;
}
o->variables = vars|o->variables;
}
string contents;
if(contents = o->misc["cookie"])
{
string c;
mapping cookies = ([]);
multiset config = (<>);
o->misc->cookies = contents;
foreach(((contents/";") - ({""})), c)
{
string name, value;
while(sizeof(c) && c[0]==' ') c=c[1..];
if(sscanf(c, "%s=%s", name, value) == 2)
{
value=http_decode_string(value);
name=http_decode_string(name);
cookies[ name ]=value;
if(name == "RoxenConfig" && strlen(value))
config = aggregate_multiset(@(value/"," + ({ })));
}
}
o->cookies = cookies;
o->config = config;
} else {
o->cookies = ([]);
o->config = (<>);
}
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
761baa | 1999-12-08 | Per Hedbor | | if(contents = o->misc->accept)
o->misc->accept = contents/",";
if(contents = o->misc["accept-charset"])
o->misc["accept-charset"] = ({ contents/"," });
if(contents = o->misc["accept-language"])
o->misc["accept-language"] = ({ contents/"," });
if(contents = o->misc["session-id"])
o->misc["session-id"] = ({ contents/"," });
}
void handle_request(object o)
{
setup_fake( o );
handle( o->handle_request, this_object() );
}
int cdel=10;
void do_log()
{
if(l->logp())
{
switch(query("log"))
{
case "None":
l->log_as_array();
break;
|
934b3f | 2000-02-04 | Per Hedbor | | case "CommonLog":
|
761baa | 1999-12-08 | Per Hedbor | | object f = Stdio.File( query("log_file"), "wca" );
l->log_as_commonlog_to_file( f );
destruct(f);
break;
|
934b3f | 2000-02-04 | Per Hedbor | | default:
report_notice( "It is not yet possible to log using the "+
|
23414a | 2000-07-21 | Andreas Lange | | query("log")+" method. Sorry. Out of time...");
|
934b3f | 2000-02-04 | Per Hedbor | | break;
|
761baa | 1999-12-08 | Per Hedbor | | }
cdel--;
if(cdel < 1) cdel=1;
} else {
cdel++;
}
call_out(do_log, cdel);
}
|
6796aa | 1999-12-11 | Per Hedbor | | string status( )
|
761baa | 1999-12-08 | Per Hedbor | | {
|
6796aa | 1999-12-11 | Per Hedbor | | mapping m = l->cache_status();
string res;
low_adjust_stats( m );
|
725645 | 1999-12-08 | Per Hedbor | | #define PCT(X) ((int)(((X)/(float)(m->total+0.1))*100))
|
e3bbe1 | 1999-12-15 | Per Hedbor | | res = ("\nCache statistics\n<pre>\n");
|
725645 | 1999-12-08 | Per Hedbor | | m->total = m->hits + m->misses + m->stale;
|
e3bbe1 | 1999-12-15 | Per Hedbor | | res += sprintf(" %d elements in cache, size is %1.1fMb max is %1.1fMb\n"
|
6796aa | 1999-12-11 | Per Hedbor | | " %d cache lookups, %d%% hits, %d%% misses and %d%% stale.\n",
m->entries, m->size/(1024.0*1024.0), m->max_size/(1024*1024.0),
m->total, PCT(m->hits), PCT(m->misses), PCT(m->stale));
|
e3bbe1 | 1999-12-15 | Per Hedbor | | return res+"\n</pre>\n";
|
6796aa | 1999-12-11 | Per Hedbor | | }
|
725645 | 1999-12-08 | Per Hedbor | |
|
6796aa | 1999-12-11 | Per Hedbor | | void low_adjust_stats(mapping m)
{
|
761baa | 1999-12-08 | Per Hedbor | | array q = values( urls )->conf;
if( sizeof( q ) )
{
q[0]->requests += m->num_request;
q[0]->received += m->received_bytes;
q[0]->sent += m->sent_bytes;
}
requests += m->num_requests;
received += m->received_bytes;
sent += m->sent_bytes;
}
void adjust_stats()
{
call_out(adjust_stats, 2);
|
ebdf94 | 2000-02-04 | Per Hedbor | |
low_adjust_stats( l->cache_status() );
|
761baa | 1999-12-08 | Per Hedbor | | }
void create( int pn, string i )
{
requesthandler = (program)"protocols/fhttp.pike";
port = pn;
ip = i;
|
6f72d4 | 2000-02-08 | Per Hedbor | | set_up_fhttp_variables( this_object() );
|
934b3f | 2000-02-04 | Per Hedbor | | restore();
|
6f72d4 | 2000-02-08 | Per Hedbor | |
|
934b3f | 2000-02-04 | Per Hedbor | | dolog = (query_option( "log" ) && (query_option( "log" )!="None"));
|
761baa | 1999-12-08 | Per Hedbor | | portobj = Stdio.Port();
if( !portobj->bind( port, 0, ip ) )
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(6,"Failed to bind %s://%s:%d/ (%s)")+"\n",
|
761baa | 1999-12-08 | Per Hedbor | | name,ip||"*",(int)port, strerror(errno()));
destruct(portobj);
return;
}
|
81f8af | 1999-12-20 | Martin Nilsson | | l = HTTPLoop.Loop( portobj, requesthandler,
|
761baa | 1999-12-08 | Per Hedbor | | handle_request, 0,
|
0c989e | 2000-02-04 | Per Hedbor | | ((int)query_option("ram_cache")||20)*1024*1024,
|
761baa | 1999-12-08 | Per Hedbor | | dolog, (query_option("read_timeout")||120) );
call_out(adjust_stats, 10);
if(dolog)
call_out(do_log, 5);
}
}
#endif
|
c5e096 | 1999-10-04 | Per Hedbor | | class HTTP
{
inherit Protocol;
constant supports_ipless = 1;
constant name = "http";
constant requesthandlerfile = "protocols/http.pike";
constant default_port = 80;
|
934b3f | 2000-02-04 | Per Hedbor | |
|
1f4a6c | 2000-08-28 | Per Hedbor | | int set_cookie, set_cookie_only_once;
void fix_cvars( Variable.Variable a )
{
set_cookie = query( "set_cookie" );
set_cookie_only_once = query( "set_cookie_only_once" );
}
|
934b3f | 2000-02-04 | Per Hedbor | | void create( mixed ... args )
{
set_up_http_variables( this_object() );
|
1f4a6c | 2000-08-28 | Per Hedbor | | if( variables[ "set_cookie" ] )
variables[ "set_cookie" ]->set_changed_callback( fix_cvars );
if( variables[ "set_cookie_only_once" ] )
variables[ "set_cookie_only_once" ]->set_changed_callback( fix_cvars );
fix_cvars(0);
|
934b3f | 2000-02-04 | Per Hedbor | | ::create( @args );
}
|
1f4a6c | 2000-08-28 | Per Hedbor | |
|
c5e096 | 1999-10-04 | Per Hedbor | | }
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
b8fd5c | 2000-09-28 | Per Hedbor | | #if constant(SSL.sslfile)
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | class HTTPS
{
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | inherit SSLProtocol;
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | constant supports_ipless = 0;
constant name = "https";
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | constant requesthandlerfile = "protocols/http.pike";
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | constant default_port = 443;
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
1d7d6d | 2000-02-16 | Per Hedbor | |
class fallback_redirect_request
{
string in = "";
string out;
string default_prefix;
int port;
Stdio.File f;
void die()
{
SSL3_WERR(sprintf("fallback_redirect_request::die()"));
f->close();
destruct(f);
destruct(this_object());
}
void write_callback(object id)
{
SSL3_WERR(sprintf("fallback_redirect_request::write_callback()"));
int written = id->write(out);
if (written <= 0)
die();
out = out[written..];
if (!strlen(out))
die();
}
void read_callback(object id, string s)
{
SSL3_WERR(sprintf("fallback_redirect_request::read_callback(X, \"%s\")\n", s));
in += s;
string name;
string prefix;
if (search(in, "\r\n\r\n") >= 0)
{
array(string) lines = in / "\r\n";
array(string) req = replace(lines[0], "\t", " ") / " ";
if (sizeof(req) < 2)
{
out = "HTTP/1.0 400 Bad Request\r\n\r\n";
}
else
{
if (sizeof(req) == 2)
{
name = req[1];
}
else
{
name = req[1..sizeof(req)-2] * " ";
foreach(map(lines[1..], `/, ":"), array header)
{
if ( (sizeof(header) >= 2) &&
(lower_case(header[0]) == "host") )
prefix = "https://" + header[1] - " ";
}
}
if (prefix) {
if (prefix[-1] == '/')
prefix = prefix[..strlen(prefix)-2];
prefix = prefix + ":" + port;
} else {
if (!(prefix = default_prefix)) {
string ip = (f->query_address(1)/" ")[0];
prefix = "https://" + ip + ":" + port;
} else if (prefix[..4] == "http:") {
prefix = "https:" + prefix[5..];
}
}
out = sprintf("HTTP/1.0 301 Redirect to secure server\r\n"
"Location: %s%s\r\n\r\n", prefix, name);
}
f->set_read_callback(0);
f->set_write_callback(write_callback);
}
}
void create(object socket, string s, string l, int p)
{
SSL3_WERR(sprintf("fallback_redirect_request(X, \"%s\", \"%s\", %d)", s, l||"CONFIG PORT", p));
f = socket;
default_prefix = l;
port = p;
f->set_nonblocking(read_callback, 0, die);
f->set_id(f);
read_callback(f, s);
}
}
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | class http_fallback {
object my_fd;
void ssl_alert_callback(object alert, object|int n, string data)
{
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | SSL3_WERR(sprintf("http_fallback(X, %O, \"%s\")", n, data));
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
if ( (my_fd->current_write_state->seq_num == 0)
&& search(lower_case(data), "http"))
{
object raw_fd = my_fd->socket;
|
60a912 | 1999-10-10 | Henrik Grubbström (Grubba) | | my_fd->socket = 0;
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
fallback_redirect_request(raw_fd, data,
|
81f8af | 1999-12-20 | Martin Nilsson | | my_fd->config &&
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | my_fd->config->query("MyWorldLocation"),
port);
destruct(my_fd);
destruct(this_object());
}
}
void ssl_accept_callback(object id)
{
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | SSL3_WERR(sprintf("ssl_accept_callback(X)"));
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | id->set_alert_callback(0);
my_fd = 0;
}
void create(object fd)
{
my_fd = fd;
fd->set_alert_callback(ssl_alert_callback);
fd->set_accept_callback(ssl_accept_callback);
}
}
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | object accept()
{
object q = ::accept();
if (q) {
http_fallback(q);
}
return q;
}
|
934b3f | 2000-02-04 | Per Hedbor | |
|
1f4a6c | 2000-08-28 | Per Hedbor | | int set_cookie, set_cookie_only_once;
void fix_cvars( Variable.Variable a )
{
set_cookie = query( "set_cookie" );
set_cookie_only_once = query( "set_cookie_only_once" );
}
|
934b3f | 2000-02-04 | Per Hedbor | | void create( mixed ... args )
{
set_up_http_variables( this_object() );
|
1f4a6c | 2000-08-28 | Per Hedbor | | if( variables[ "set_cookie" ] )
variables[ "set_cookie" ]->set_changed_callback( fix_cvars );
if( variables[ "set_cookie_only_once" ] )
variables[ "set_cookie_only_once" ]->set_changed_callback( fix_cvars );
fix_cvars(0);
|
934b3f | 2000-02-04 | Per Hedbor | | ::create( @args );
}
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | }
|
b8fd5c | 2000-09-28 | Per Hedbor | | #endif
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
b00cae | 2000-09-30 | Per Hedbor | | class HILFE
{
inherit Protocol;
constant supports_ipless = 0;
constant name = "hilfe";
constant requesthandlerfile = "protocols/hilfe.pike";
constant default_port = 2345;
|
e056e9 | 2000-10-19 | Per Hedbor | |
void create( mixed ... args )
{
add_permission( "Hilfe", LOC_M( 0, "Hilfe" ) );
set_up_hilfe_variables( this_object() );
::create( @args );
}
|
b00cae | 2000-09-30 | Per Hedbor | | }
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | class FTP
{
inherit Protocol;
constant supports_ipless = 0;
constant name = "ftp";
constant requesthandlerfile = "protocols/ftp.pike";
constant default_port = 21;
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
int sessions;
int ftp_users;
int ftp_users_now;
|
934b3f | 2000-02-04 | Per Hedbor | |
|
c8a3ca | 2000-09-18 | Henrik Grubbström (Grubba) | | mapping(string:int) ftp_sessions = ([]);
|
934b3f | 2000-02-04 | Per Hedbor | | void create( mixed ... args )
{
set_up_ftp_variables( this_object() );
::create( @args );
}
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | }
|
b8fd5c | 2000-09-28 | Per Hedbor | | #if constant(SSL.sslfile)
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | class FTPS
{
inherit SSLProtocol;
constant supports_ipless = 0;
constant name = "ftps";
constant requesthandlerfile = "protocols/ftp.pike";
constant default_port = 21;
int sessions;
int ftp_users;
int ftp_users_now;
|
934b3f | 2000-02-04 | Per Hedbor | |
|
89a273 | 2000-09-18 | Henrik Grubbström (Grubba) | | mapping(string:int) ftp_sessions = ([]);
|
934b3f | 2000-02-04 | Per Hedbor | | void create( mixed ... args )
{
set_up_ftp_variables( this_object() );
::create( @args );
}
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | }
|
b8fd5c | 2000-09-28 | Per Hedbor | | #endif
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | class GOPHER
{
inherit Protocol;
constant supports_ipless = 0;
constant name = "gopher";
constant requesthandlerfile = "protocols/gopher.pike";
constant default_port = 70;
}
class TETRIS
{
inherit Protocol;
constant supports_ipless = 0;
constant name = "tetris";
constant requesthandlerfile = "protocols/tetris.pike";
constant default_port = 2050;
}
|
479d8a | 1999-11-25 | Henrik Grubbström (Grubba) | | class SMTP
{
inherit Protocol;
constant supports_ipless = 1;
constant name = "smtp";
constant requesthandlerfile = "protocols/smtp.pike";
constant default_port = Protocols.Ports.tcp.smtp;
}
class POP3
{
inherit Protocol;
constant supports_ipless = 0;
constant name = "pop3";
constant requesthandlerfile = "protocols/pop3.pike";
constant default_port = Protocols.Ports.tcp.pop3;
}
class IMAP
{
inherit Protocol;
constant supports_ipless = 0;
constant name = "imap";
constant requesthandlerfile = "protocols/imap.pike";
constant default_port = Protocols.Ports.tcp.imap2;
}
|
c5e096 | 1999-10-04 | Per Hedbor | | mapping protocols = ([
|
761baa | 1999-12-08 | Per Hedbor | | #if constant(HTTPLoop.prog)
"fhttp":FHTTP,
#else
"fhttp":HTTP,
#endif
|
c5e096 | 1999-10-04 | Per Hedbor | | "http":HTTP,
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | "ftp":FTP,
|
b8fd5c | 2000-09-28 | Per Hedbor | |
|
b00cae | 2000-09-30 | Per Hedbor | | #if constant(SSL.sslfile)
|
3eebb9 | 2000-02-12 | Martin Stjernholm | | "https":HTTPS,
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | | "ftps":FTPS,
|
57298f | 2000-09-19 | Per Hedbor | | #endif
|
df36c6 | 1999-10-08 | Henrik Grubbström (Grubba) | |
|
b00cae | 2000-09-30 | Per Hedbor | | "hilfe":HILFE,
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | "gopher":GOPHER,
"tetris":TETRIS,
|
479d8a | 1999-11-25 | Henrik Grubbström (Grubba) | |
"smtp":SMTP,
"pop3":POP3,
"imap":IMAP,
|
c5e096 | 1999-10-04 | Per Hedbor | | ]);
mapping(string:mapping) open_ports = ([ ]);
mapping(string:object) urls = ([]);
array sorted_urls = ({});
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | array(string) find_ips_for( string what )
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
c5e096 | 1999-10-04 | Per Hedbor | | if( what == "*" || lower_case(what) == "any" )
|
b1fca0 | 1996-11-12 | Per Hedbor | | return 0;
|
c5e096 | 1999-10-04 | Per Hedbor | |
|
15635b | 1999-10-10 | Per Hedbor | | if( is_ip( what ) )
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | return ({ what });
|
c5e096 | 1999-10-04 | Per Hedbor | |
array res = gethostbyname( what );
if( !res || !sizeof( res[1] ) )
|
c18318 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(46, "Cannot possibly bind to %O, that host is "
|
c8ee71 | 2000-09-09 | Andreas Lange | | "unknown. Substituting with ANY")+"\n", what);
|
c5e096 | 1999-10-04 | Per Hedbor | | else
|
92898c | 1999-10-10 | Marcus Comstedt | | return Array.uniq(res[1]);
|
c5e096 | 1999-10-04 | Per Hedbor | | }
|
81f8af | 1999-12-20 | Martin Nilsson | | void unregister_url( string url )
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
03aa49 | 2000-08-23 | Per Hedbor | | string ourl = url;
|
8fb517 | 2000-05-27 | Per Hedbor | | url = lower_case( url );
|
bc5c2a | 2000-08-23 | Per Hedbor | | string host, path, protocol;
int port;
if (!sizeof (url - " " - "\t")) return;
url = replace( url, "/ANY", "/*" );
url = replace( url, "/any", "/*" );
sscanf( url, "%[^:]://%[^/]%s", protocol, host, path );
if (!host || !stringp(host))
return;
sscanf(host, "%[^:]:%d", host, port);
if( !port )
{
port = protocols[ protocol ]->default_port;
url = protocol+"://"+host+":"+port+path;
}
|
e7e603 | 1999-11-05 | Per Hedbor | | report_debug("Unregister "+url+"\n");
|
0aee22 | 2000-08-15 | Martin Stjernholm | |
|
c5e096 | 1999-10-04 | Per Hedbor | | if( urls[ url ] && urls[ url ]->port )
{
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | urls[ url ]->port->unref(url);
|
c5e096 | 1999-10-04 | Per Hedbor | | m_delete( urls, url );
|
03aa49 | 2000-08-23 | Per Hedbor | | m_delete( urls, ourl );
|
c5e096 | 1999-10-04 | Per Hedbor | | sort_urls();
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
}
|
934b3f | 2000-02-04 | Per Hedbor | | array all_ports( )
{
|
f04752 | 2000-09-25 | Per Hedbor | | return Array.uniq( values( urls )->port )-({0});
|
934b3f | 2000-02-04 | Per Hedbor | | }
Protocol find_port( string name )
{
foreach( all_ports(), Protocol p )
if( p->get_key() == name )
return p;
}
|
c5e096 | 1999-10-04 | Per Hedbor | | void sort_urls()
|
1668b2 | 1998-04-23 | Henrik Grubbström (Grubba) | | {
|
c5e096 | 1999-10-04 | Per Hedbor | | sorted_urls = indices( urls );
sort( map( map( sorted_urls, strlen ), `-), sorted_urls );
|
1668b2 | 1998-04-23 | Henrik Grubbström (Grubba) | | }
|
d09399 | 2000-09-25 | Per Hedbor | | int register_url( string url, Configuration conf )
|
c5e096 | 1999-10-04 | Per Hedbor | | {
|
03aa49 | 2000-08-23 | Per Hedbor | | string ourl = url;
|
8fb517 | 2000-05-27 | Per Hedbor | | url = lower_case( url );
|
1e3cd5 | 2000-02-02 | Martin Stjernholm | | if (!sizeof (url - " " - "\t")) return 1;
|
c5e096 | 1999-10-04 | Per Hedbor | | string protocol;
string host;
int port;
string path;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
ac76fc | 1999-10-07 | Henrik Grubbström (Grubba) | | url = replace( url, "/ANY", "/*" );
url = replace( url, "/any", "/*" );
|
c5e096 | 1999-10-04 | Per Hedbor | |
sscanf( url, "%[^:]://%[^/]%s", protocol, host, path );
|
228fea | 2000-02-15 | Leif Stensson | | if (!host || !stringp(host))
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(19,"Bad URL '%s' for server `%s'")+"\n",
|
23414a | 2000-07-21 | Andreas Lange | | url, conf->query_name());
|
abc59a | 2000-08-23 | Per Hedbor | | return 0;
|
228fea | 2000-02-15 | Leif Stensson | | }
|
b8fd5c | 2000-09-28 | Per Hedbor | |
if( !protocols[ protocol ] )
{
report_error(LOC_M(0,"The protocol '%s' is not available")+"\n", protocol);
return 0;
}
|
228fea | 2000-02-15 | Leif Stensson | | sscanf(host, "%[^:]:%d", host, port);
|
c5e096 | 1999-10-04 | Per Hedbor | |
|
38c507 | 2000-02-28 | Per Hedbor | | if( !port )
{
port = protocols[ protocol ]->default_port;
url = protocol+"://"+host+":"+port+path;
}
|
c5e096 | 1999-10-04 | Per Hedbor | | if( strlen( path ) && ( path[-1] == '/' ) )
path = path[..strlen(path)-2];
if( !strlen( path ) )
path = 0;
|
bc5c2a | 2000-08-23 | Per Hedbor | | if( urls[ url ] && urls[ url ]->conf )
|
c5e096 | 1999-10-04 | Per Hedbor | | {
if( urls[ url ]->conf != conf )
{
|
49cd28 | 2000-08-11 | Andreas Lange | | report_error(LOC_M(20, "Cannot register URL %s, "
|
c8ee71 | 2000-09-09 | Andreas Lange | | "already registered by %s!")+"\n",
|
23414a | 2000-07-21 | Andreas Lange | | url, urls[ url ]->conf->name);
|
c5e096 | 1999-10-04 | Per Hedbor | | return 0;
}
|
ddefa6 | 1999-10-04 | Marcus Comstedt | | urls[ url ]->port->ref(url, urls[url]);
|
c5e096 | 1999-10-04 | Per Hedbor | | return 1;
|
bc5c2a | 2000-08-23 | Per Hedbor | | }
else if( urls[ url ] )
{
urls[ url ]->port->unref( url );
|
c5e096 | 1999-10-04 | Per Hedbor | | }
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | Protocol prot;
|
c5e096 | 1999-10-04 | Per Hedbor | | if( !( prot = protocols[ protocol ] ) )
{
|
49cd28 | 2000-08-11 | Andreas Lange | | report_error(LOC_M(21, "Cannot register URL %s, "
|
c8ee71 | 2000-09-09 | Andreas Lange | | "cannot find the protocol %s!")+"\n",
|
23414a | 2000-07-21 | Andreas Lange | | url, protocol);
|
c5e096 | 1999-10-04 | Per Hedbor | | return 0;
}
if( !port )
port = prot->default_port;
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | array(string) required_hosts;
|
8fb517 | 2000-05-27 | Per Hedbor | | if (is_ip(host))
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | required_hosts = ({ host });
|
8fb517 | 2000-05-27 | Per Hedbor | | else
required_hosts = find_ips_for( host );
|
b6fb05 | 1999-11-02 | Per Hedbor | |
|
8fb517 | 2000-05-27 | Per Hedbor | | if (!required_hosts)
required_hosts = ({ 0 });
|
c5e096 | 1999-10-04 | Per Hedbor | |
mapping m;
if( !( m = open_ports[ protocol ] ) )
|
8fb517 | 2000-05-27 | Per Hedbor | |
m = open_ports[ protocol ] = ([ 0:([]) ]);
if( sizeof( required_hosts - ({ 0 }) )
&& m[ 0 ][ port ]
&& prot->supports_ipless )
required_hosts = ({ 0 });
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
c5e096 | 1999-10-04 | Per Hedbor | | urls[ url ] = ([ "conf":conf, "path":path ]);
|
03aa49 | 2000-08-23 | Per Hedbor | | urls[ ourl ] = ([ "conf":conf, "path":path ]);
|
c5e096 | 1999-10-04 | Per Hedbor | | sorted_urls += ({ url });
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | int failures;
|
c5e096 | 1999-10-04 | Per Hedbor | |
|
38c507 | 2000-02-28 | Per Hedbor | | foreach(required_hosts, string required_host)
{
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | if( m[ required_host ] && m[ required_host ][ port ] )
{
|
7c708f | 2000-02-05 | Henrik Grubbström (Grubba) | | m[required_host][port]->ref(url, urls[url]);
|
38c507 | 2000-02-28 | Per Hedbor | |
|
7c708f | 2000-02-05 | Henrik Grubbström (Grubba) | | urls[url]->port = m[required_host][port];
|
65d200 | 2000-09-25 | Per Hedbor | | urls[ourl]->port = m[required_host][port];
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | continue;
}
if( !m[ required_host ] )
m[ required_host ] = ([ ]);
|
7c708f | 2000-02-05 | Henrik Grubbström (Grubba) | | mixed err;
if (err = catch {
m[ required_host ][ port ] = prot( port, required_host );
}) {
failures++;
report_error(sprintf("Initializing the port handler for URL " +
url + " failed!\n"
"%s\n",
describe_backtrace(err)));
continue;
}
|
abc59a | 2000-08-23 | Per Hedbor | |
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | if( !( m[ required_host ][ port ] ) )
{
m_delete( m[ required_host ], port );
failures++;
if (required_host) {
|
49cd28 | 2000-08-11 | Andreas Lange | | report_warning(LOC_M(22, "Binding the port on IP %s "
|
23414a | 2000-07-21 | Andreas Lange | | "failed\n for URL %s!\n"),
|
434bac | 2000-07-14 | Andreas Lange | | url, required_host);
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | }
continue;
}
|
abc59a | 2000-08-23 | Per Hedbor | |
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | urls[ url ]->port = m[ required_host ][ port ];
|
03aa49 | 2000-08-23 | Per Hedbor | | urls[ ourl ]->port = m[ required_host ][ port ];
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | m[ required_host ][ port ]->ref(url, urls[url]);
|
03aa49 | 2000-08-23 | Per Hedbor | |
if( !m[ required_host ][ port ]->bound )
failures++;
|
dd7e66 | 1999-10-09 | Henrik Grubbström (Grubba) | | }
|
abc59a | 2000-08-23 | Per Hedbor | | if (failures == sizeof(required_hosts))
{
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(23, "Cannot register URL %s!")+"\n", url);
|
c5e096 | 1999-10-04 | Per Hedbor | | return 0;
}
sort_urls();
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_notice(LOC_S(3, "Registered %s for %s")+"\n",
|
434bac | 2000-07-14 | Andreas Lange | | url, conf->query_name() );
|
c5e096 | 1999-10-04 | Per Hedbor | | return 1;
}
|
d09399 | 2000-09-25 | Per Hedbor | | Configuration find_configuration( string name )
|
45dc02 | 2000-08-16 | Martin Stjernholm | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
9699bf | 1999-10-11 | Per Hedbor | | name = replace( lower_case( replace(name,"-"," ") )-" ", "/", "-" );
|
d09399 | 2000-09-25 | Per Hedbor | | foreach( configurations, Configuration o )
|
9699bf | 1999-10-11 | Per Hedbor | | {
|
066674 | 1999-12-07 | Henrik Grubbström (Grubba) | | if( (lower_case( replace( replace(o->name, "-"," ") - " " ,
"/", "-" ) ) == name) ||
(lower_case( replace( replace(o->query_name(), "-", " ") - " " ,
"/", "-" ) ) == name) )
|
c5e096 | 1999-10-04 | Per Hedbor | | return o;
|
9699bf | 1999-10-11 | Per Hedbor | | }
|
066674 | 1999-12-07 | Henrik Grubbström (Grubba) | | return 0;
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
c5e096 | 1999-10-04 | Per Hedbor | | mapping(string:array(int)) error_log=([]);
|
ec2fe1 | 1997-06-09 | Henrik Grubbström (Grubba) | |
|
a6ef1f | 2000-03-28 | Johan Sundström | |
|
c60cae | 2000-02-02 | Johan Sundström | | void nwrite(string s, int|void perr, int|void errtype,
|
c821c3 | 1999-12-27 | Martin Stjernholm | | object|void mod, object|void conf)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
add34e | 2000-08-17 | Per Hedbor | | int log_time = time(1);
|
0fe69d | 2000-03-19 | Martin Nilsson | | string reference = (mod ? Roxen.get_modname(mod) : conf && conf->name) || "";
|
c60cae | 2000-02-02 | Johan Sundström | | string log_index = sprintf("%d,%s,%s", errtype, reference, s);
if(!error_log[log_index])
error_log[log_index] = ({ log_time });
|
c5e096 | 1999-10-04 | Per Hedbor | | else
|
c60cae | 2000-02-02 | Johan Sundström | | error_log[log_index] += ({ log_time });
|
e99fd4 | 1999-11-17 | Per Hedbor | |
if( mod )
{
if( !mod->error_log )
mod->error_log = ([]);
|
c60cae | 2000-02-02 | Johan Sundström | | mod->error_log[log_index] += ({ log_time });
|
e99fd4 | 1999-11-17 | Per Hedbor | | }
if( conf )
{
if( !conf->error_log )
conf->error_log = ([]);
|
c60cae | 2000-02-02 | Johan Sundström | | conf->error_log[log_index] += ({ log_time });
|
e99fd4 | 1999-11-17 | Per Hedbor | | }
|
c60cae | 2000-02-02 | Johan Sundström | | if(errtype >= 1)
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug( s );
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | int boot_time =time();
int start_time =time();
|
b1fca0 | 1996-11-12 | Per Hedbor | |
string version()
{
|
10089c | 2000-05-17 | Martin Nilsson | | #ifndef NSERIOUS
|
71a11e | 1997-08-13 | Henrik Grubbström (Grubba) | | return QUERY(default_ident)?real_version:QUERY(ident);
|
daff90 | 2000-02-23 | Martin Nilsson | | #else
multiset choices=(<>);
string version=QUERY(default_ident)?real_version:QUERY(ident);
|
089364 | 2000-02-25 | Martin Nilsson | | return version+", "+ ({
"Applier of Templates",
"Beautifier of Layouts",
"Conqueror of Comdex",
"Deliverer of Documents",
"Enhancer of Abilities",
"Freer of Webmasters",
"Generator of Logs",
"Helper of Users",
"Interpreter of Scripts",
"Juggler of Java-code",
"Keeper of Databases",
"Locator of Keywords",
"Manipulator of Data",
"Negatiator of Protocols",
"Operator of Sites",
"Provider of Contents",
"Quintessence of Quality",
"Responder to Connections",
"Server of Webs",
"Translator of Texts",
"Unifier of Interfaces",
"Valet of Visitors",
"Watcher for Requests",
"Xylem of Services",
"Yielder of Information",
"Zenith of Extensibility"
})[random(26)];
|
daff90 | 2000-02-23 | Martin Nilsson | | #endif
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
81f8af | 1999-12-20 | Martin Nilsson | | public void log(mapping file, RequestID request_id)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
81f8af | 1999-12-20 | Martin Nilsson | | if(!request_id->conf) return;
|
14179b | 1997-01-29 | Per Hedbor | | request_id->conf->log(file, request_id);
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | private object current_user_id_file;
private int current_user_id_number, current_user_id_file_last_mod;
private void restore_current_user_id_number()
{
if(!current_user_id_file)
current_user_id_file = open(configuration_dir + "LASTUSER~", "rwc");
if(!current_user_id_file)
{
call_out(restore_current_user_id_number, 2);
return;
|
81f8af | 1999-12-20 | Martin Nilsson | | }
|
b1fca0 | 1996-11-12 | Per Hedbor | | current_user_id_number = (int)current_user_id_file->read(100);
current_user_id_file_last_mod = current_user_id_file->stat()[2];
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Restoring unique user ID information. (" + current_user_id_number
+ ")\n");
|
323569 | 1998-03-26 | Per Hedbor | | #ifdef FD_DEBUG
|
434bac | 2000-07-14 | Andreas Lange | | mark_fd(current_user_id_file->query_fd(), "Unique user ID logfile.\n");
|
323569 | 1998-03-26 | Per Hedbor | | #endif
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
f6d62d | 1997-03-26 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | int increase_id()
{
if(!current_user_id_file)
{
restore_current_user_id_number();
|
add34e | 2000-08-17 | Per Hedbor | | return current_user_id_number+time(1);
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
if(current_user_id_file->stat()[2] != current_user_id_file_last_mod)
restore_current_user_id_number();
current_user_id_number++;
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | current_user_id_file->seek(0);
current_user_id_file->write((string)current_user_id_number);
current_user_id_file_last_mod = current_user_id_file->stat()[2];
return current_user_id_number;
}
|
c8ee71 | 2000-09-09 | Andreas Lange | |
|
dbfe9d | 2000-04-13 | Per Hedbor | | #ifndef __NT__
|
ee8b20 | 1998-07-13 | David Hedbor | | static int abs_started;
|
6ca8f6 | 1998-10-13 | Per Hedbor | |
|
81f8af | 1999-12-20 | Martin Nilsson | | void restart_if_stuck (int force)
|
6ca8f6 | 1998-10-13 | Per Hedbor | | {
|
ee8b20 | 1998-07-13 | David Hedbor | | remove_call_out(restart_if_stuck);
if (!(QUERY(abs_engage) || force))
|
edc9af | 1998-07-11 | David Hedbor | | return;
|
81f8af | 1999-12-20 | Martin Nilsson | | if(!abs_started)
|
6ca8f6 | 1998-10-13 | Per Hedbor | | {
|
ee8b20 | 1998-07-13 | David Hedbor | | abs_started = 1;
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Anti-Block System Enabled.\n");
|
ee8b20 | 1998-07-13 | David Hedbor | | }
call_out (restart_if_stuck,10);
|
6ca8f6 | 1998-10-13 | Per Hedbor | | signal(signum("SIGALRM"),
lambda( int n ) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("**** %s: ABS engaged!\n"
"Trying to dump backlog: \n",
ctime(time()) - "\n");
|
b0659e | 1999-07-20 | Henrik Grubbström (Grubba) | | catch {
describe_all_threads();
};
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("**** %s: ABS exiting roxen!\n\n",
ctime(time()));
|
6ca8f6 | 1998-10-13 | Per Hedbor | | _exit(1);
});
|
ee8b20 | 1998-07-13 | David Hedbor | | alarm (60*QUERY(abs_timeout)+10);
|
edc9af | 1998-07-11 | David Hedbor | | }
|
dbfe9d | 2000-04-13 | Per Hedbor | | #endif
|
edc9af | 1998-07-11 | David Hedbor | |
|
753a83 | 1999-08-30 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | class ImageCache
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
2ff846 | 1999-09-02 | Per Hedbor | | {
string name;
string dir;
function draw_function;
mapping data_cache = ([]);
mapping meta_cache = ([]);
|
0f8b2f | 1999-03-27 | Henrik Grubbström (Grubba) | |
|
6f76f1 | 2000-06-01 | Martin Nilsson | | string documentation(void|string tag_n_args) {
Stdio.File doc_file;
if(!(doc_file=Stdio.File("base_server/image_cache.xml"))) return "";
string doc=doc_file->read();
if(!tag_n_args)
return Parser.HTML()->add_container("ex", "")->feed(doc)->read();
return replace(doc, "###", tag_n_args);
}
|
34d3fa | 1999-04-22 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | static mapping meta_cache_insert( string i, mapping what )
{
return meta_cache[i] = what;
}
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
2ff846 | 1999-09-02 | Per Hedbor | | static string data_cache_insert( string i, string what )
{
return data_cache[i] = what;
}
|
753a83 | 1999-08-30 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | static mixed frommapp( mapping what )
|
753a83 | 1999-08-30 | Per Hedbor | | {
|
ede135 | 2000-08-12 | Martin Stjernholm | | if( !zero_type(what[""]) ) return what[""];
|
2ff846 | 1999-09-02 | Per Hedbor | | return what;
|
753a83 | 1999-08-30 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | static void draw( string name, RequestID id )
{
mixed args = Array.map( Array.map( name/"$", argcache->lookup, id->client ), frommapp);
mapping meta;
string data;
|
37ecea | 2000-02-21 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | mixed reply = draw_function( @copy_value(args), id );
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | if( arrayp( args ) )
args = args[0];
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
37ecea | 2000-02-21 | Per Hedbor | | if( arrayp( reply ) )
reply = Image.lay( reply );
if( objectp( reply ) && reply->image )
{
reply = ([
"img":reply->image(),
"alpha":reply->alpha(),
]);
}
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | if( objectp( reply ) || (mappingp(reply) && reply->img) )
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
2ff846 | 1999-09-02 | Per Hedbor | | int quant = (int)args->quant;
string format = lower_case(args->format || "gif");
string dither = args->dither;
Image.Colortable ct;
|
e9d7c5 | 1999-11-02 | Per Hedbor | | Image.Color.Color bgcolor;
|
2ff846 | 1999-09-02 | Per Hedbor | | object alpha;
|
81f8af | 1999-12-20 | Martin Nilsson | | int true_alpha;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | if( args->fs || dither == "fs" )
dither = "floyd_steinberg";
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | if( dither == "random" )
dither = "random_dither";
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
81f8af | 1999-12-20 | Martin Nilsson | | if( format == "jpg" )
|
2ff846 | 1999-09-02 | Per Hedbor | | format = "jpeg";
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
112b1c | 2000-02-02 | Per Hedbor | | if( dither )
dither = replace( dither, "-", "_" );
|
2ff846 | 1999-09-02 | Per Hedbor | | if(mappingp(reply))
{
alpha = reply->alpha;
reply = reply->img;
|
93ebdd | 1999-03-11 | Martin Stjernholm | | }
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
c3a53d | 1999-06-25 | Per Hedbor | | if( args["true-alpha"] )
true_alpha = 1;
|
e7e603 | 1999-11-05 | Per Hedbor | | if( args["background"] || args["background-color"])
bgcolor = Image.Color( (args["background"]||args["background-color"]) );
|
e9d7c5 | 1999-11-02 | Per Hedbor | |
|
b79be2 | 1999-06-11 | Per Hedbor | | if( args["opaque-value"] )
{
|
91a156 | 2000-04-11 | Per Hedbor | | if( !bgcolor ) true_alpha = 1;
|
b79be2 | 1999-06-11 | Per Hedbor | | int ov = (int)(((float)args["opaque-value"])*2.55);
if( ov < 0 )
ov = 0;
else if( ov > 255 )
ov = 255;
if( alpha )
|
356484 | 2000-09-15 | Per Hedbor | | alpha *= ov;
|
b79be2 | 1999-06-11 | Per Hedbor | | else
|
2537c3 | 2000-02-14 | Per Hedbor | | alpha = Image.Image( reply->xsize(), reply->ysize(), ov,ov,ov );
|
b79be2 | 1999-06-11 | Per Hedbor | | }
|
c52692 | 1999-05-18 | Per Hedbor | |
|
91a156 | 2000-04-11 | Per Hedbor | | if( args->gamma )
reply = reply->gamma( (float)args->gamma );
if( bgcolor && alpha && !true_alpha )
{
reply = Image.Image( reply->xsize(),
reply->ysize(), bgcolor )
->paste_mask( reply, alpha );
alpha = alpha->threshold( 4 );
}
|
3cbd0d | 2000-01-31 | Per Hedbor | | int x0, y0, x1, y1;
if( args["x-offset"] || args["xoffset"] )
x0 = (int)(args["x-offset"]||args["xoffset"]);
if( args["y-offset"] || args["yoffset"] )
y0 = (int)(args["y-offset"]||args["yoffset"]);
if( args["width"] || args["x-size"] );
x1 = (int)(args["x-size"]||args["width"]);
if( args["height"] || args["y-size"] );
y1 = (int)(args["y-size"]||args["height"]);
if( args->crop )
{
|
c08937 | 2000-09-13 | Per Hedbor | | if( sscanf( args->crop, "%d,%d-%d,%d", x0, y0, x1, y1 ) )
{
x1 -= x0;
y1 -= y0;
} else {
[ x0, y0, x1, y1 ] = reply->find_autocrop();
x1 -= x0;
y1 -= y0;
}
|
3cbd0d | 2000-01-31 | Per Hedbor | | }
if( x0 || x1 || y0 || y1 )
{
if( !x1 ) x1 = reply->xsize()-x0;
if( !y1 ) y1 = reply->ysize()-y0;
|
91a156 | 2000-04-11 | Per Hedbor | | reply = reply->copy( x0,y0,x0+x1-1,y0+y1-1 );
|
3cbd0d | 2000-01-31 | Per Hedbor | | if( alpha )
|
91a156 | 2000-04-11 | Per Hedbor | | alpha = alpha->copy( x0,y0,x0+x1-1,y0+y1-1 );
|
3cbd0d | 2000-01-31 | Per Hedbor | | }
|
c52692 | 1999-05-18 | Per Hedbor | | if( args->scale )
{
int x, y;
if( sscanf( args->scale, "%d,%d", x, y ) == 2)
{
reply = reply->scale( x, y );
if( alpha )
alpha = alpha->scale( x, y );
}
|
3255b1 | 1999-05-19 | Per Hedbor | | else if( (float)args->scale < 3.0)
|
c52692 | 1999-05-18 | Per Hedbor | | {
|
3255b1 | 1999-05-19 | Per Hedbor | | reply = reply->scale( ((float)args->scale) );
|
c52692 | 1999-05-18 | Per Hedbor | | if( alpha )
|
3255b1 | 1999-05-19 | Per Hedbor | | alpha = alpha->scale( ((float)args->scale) );
|
c52692 | 1999-05-18 | Per Hedbor | | }
}
|
81f8af | 1999-12-20 | Martin Nilsson | | if( args->maxwidth || args->maxheight ||
|
e7e603 | 1999-11-05 | Per Hedbor | | args["max-width"] || args["max-height"])
|
c52692 | 1999-05-18 | Per Hedbor | | {
|
e7e603 | 1999-11-05 | Per Hedbor | | int x = (int)args->maxwidth||(int)args["max-width"];
int y = (int)args->maxheight||(int)args["max-height"];
|
c52692 | 1999-05-18 | Per Hedbor | | if( x && reply->xsize() > x )
{
reply = reply->scale( x, 0 );
if( alpha )
alpha = alpha->scale( x, 0 );
}
if( y && reply->ysize() > y )
{
reply = reply->scale( 0, y );
if( alpha )
alpha = alpha->scale( 0, y );
}
}
|
112b1c | 2000-02-02 | Per Hedbor | | if( args["rotate-cw"] || args["rotate-ccw"])
{
float degree = (float)(args["rotate-cw"] || args["rotate-ccw"]);
|
66868a | 2000-04-15 | Per Hedbor | | switch( args["rotate-unit"] && args["rotate-unit"][0..0] )
|
112b1c | 2000-02-02 | Per Hedbor | | {
|
9792c7 | 2000-05-16 | Henrik Grubbström (Grubba) | | case "r": degree = (degree / (2*3.1415)) * 360; break;
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | case "d": break;
|
66868a | 2000-04-15 | Per Hedbor | | case "n": degree = (degree / 400) * 360; break;
case "p": degree = (degree / 1.0) * 360; break;
|
112b1c | 2000-02-02 | Per Hedbor | | }
|
66868a | 2000-04-15 | Per Hedbor | | if( args["rotate-cw"] )
|
112b1c | 2000-02-02 | Per Hedbor | | degree = -degree;
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | if(!alpha)
|
66868a | 2000-04-15 | Per Hedbor | | alpha = reply->copy()->clear(255,255,255);
reply = reply->rotate_expand( degree );
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | alpha = alpha->rotate( degree, 0,0,0 );
|
112b1c | 2000-02-02 | Per Hedbor | | }
if( args["mirror-x"] )
{
if( alpha )
alpha = alpha->mirrorx();
reply = reply->mirrorx();
}
if( args["mirror-y"] )
{
if( alpha )
alpha = alpha->mirrory();
reply = reply->mirrory();
}
|
91a156 | 2000-04-11 | Per Hedbor | | if( bgcolor && alpha && !true_alpha )
|
e7e603 | 1999-11-05 | Per Hedbor | | {
reply = Image.Image( reply->xsize(),
reply->ysize(), bgcolor )
->paste_mask( reply, alpha );
}
|
91a156 | 2000-04-11 | Per Hedbor | | if( args["cs-rgb-hsv"] )reply = reply->rgb_to_hsv();
if( args["cs-grey"] ) reply = reply->grey();
if( args["cs-invert"] ) reply = reply->invert();
if( args["cs-hsv-rgb"] )reply = reply->hsv_to_rgb();
if( !true_alpha && alpha )
alpha = alpha->threshold( 4 );
|
69a869 | 1999-05-18 | Per Hedbor | | if( quant || (format=="gif") )
{
|
641a88 | 2000-06-01 | Martin Nilsson | | int ncols = quant;
if( format=="gif" ) {
ncols = ncols||id->misc->defquant||32;
if( ncols > 254 )
ncols = 254;
}
|
000c78 | 1999-05-25 | Per Hedbor | | ct = Image.Colortable( reply, ncols );
|
69a869 | 1999-05-18 | Per Hedbor | | if( dither )
|
23264c | 2000-10-17 | Per Hedbor | | {
if( dither == "random" )
dither = "random_grey";
|
000c78 | 1999-05-25 | Per Hedbor | | if( ct[ dither ] )
ct[ dither ]();
|
69a869 | 1999-05-18 | Per Hedbor | | else
ct->ordered();
|
23264c | 2000-10-17 | Per Hedbor | |
}
|
69a869 | 1999-05-18 | Per Hedbor | | }
mapping enc_args = ([]);
if( ct )
enc_args->colortable = ct;
|
a1c869 | 2000-09-12 | Per Hedbor | |
|
69a869 | 1999-05-18 | Per Hedbor | | if( alpha )
enc_args->alpha = alpha;
|
c52692 | 1999-05-18 | Per Hedbor | | foreach( glob( "*-*", indices(args)), string n )
if(sscanf(n, "%*[^-]-%s", string opt ) == 2)
|
a1c869 | 2000-09-12 | Per Hedbor | | if( opt != "alpha" )
enc_args[opt] = (int)args[n];
|
c52692 | 1999-05-18 | Per Hedbor | |
|
69a869 | 1999-05-18 | Per Hedbor | | switch(format)
{
case "gif":
|
f04b92 | 2000-09-13 | Jonas Wallden | | #if constant(Image.GIF) && constant(Image.GIF.encode)
|
c3a53d | 1999-06-25 | Per Hedbor | | if( alpha && true_alpha )
|
b79be2 | 1999-06-11 | Per Hedbor | | {
|
91a156 | 2000-04-11 | Per Hedbor | | Image.Colortable bw=Image.Colortable( ({ ({ 0,0,0 }),
({ 255,255,255 }) }) );
|
e9d7c5 | 1999-11-02 | Per Hedbor | | bw->floyd_steinberg();
alpha = bw->map( alpha );
|
b79be2 | 1999-06-11 | Per Hedbor | | }
|
000c78 | 1999-05-25 | Per Hedbor | | if( catch {
if( alpha )
data = Image.GIF.encode_trans( reply, ct, alpha );
else
data = Image.GIF.encode( reply, ct );
})
data = Image.GIF.encode( reply );
|
23264c | 2000-10-17 | Per Hedbor | | break;
|
f04b92 | 2000-09-13 | Jonas Wallden | | #else
|
23264c | 2000-10-17 | Per Hedbor | |
format = "png";
|
f04b92 | 2000-09-13 | Jonas Wallden | | #endif
|
e9d7c5 | 1999-11-02 | Per Hedbor | |
|
69a869 | 1999-05-18 | Per Hedbor | | case "png":
|
e9d7c5 | 1999-11-02 | Per Hedbor | | if( ct ) enc_args->palette = ct;
|
69a869 | 1999-05-18 | Per Hedbor | | m_delete( enc_args, "colortable" );
|
36a6ad | 2000-09-15 | Per Hedbor | | if( !(args["png-use-alpha"] || args["true-alpha"]) )
m_delete( enc_args, "alpha" );
else if( enc_args->alpha )
m_delete( enc_args, "palette");
else
|
f04b92 | 2000-09-13 | Jonas Wallden | | m_delete( enc_args, "alpha" );
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
69a869 | 1999-05-18 | Per Hedbor | | default:
|
23264c | 2000-10-17 | Per Hedbor | | if(!Image[upper_case( format )]
|| !Image[upper_case( format )]->encode )
error("Image format "+format+" unknown\n");
|
f04b92 | 2000-09-13 | Jonas Wallden | | data = Image[upper_case( format )]->encode( reply, enc_args );
|
69a869 | 1999-05-18 | Per Hedbor | | }
|
81f8af | 1999-12-20 | Martin Nilsson | | meta =
([
|
69a869 | 1999-05-18 | Per Hedbor | | "xsize":reply->xsize(),
"ysize":reply->ysize(),
"type":"image/"+format,
]);
|
c52692 | 1999-05-18 | Per Hedbor | | }
|
81f8af | 1999-12-20 | Martin Nilsson | | else if( mappingp(reply) )
|
69a869 | 1999-05-18 | Per Hedbor | | {
meta = reply->meta;
data = reply->data;
if( !meta || !data )
error("Invalid reply mapping.\n"
"Should be ([ \"meta\": ([metadata]), \"data\":\"data\" ])\n");
}
store_meta( name, meta );
store_data( name, data );
}
static void store_meta( string id, mapping meta )
{
meta_cache_insert( id, meta );
string data = encode_value( meta );
|
4f2d5f | 2000-03-13 | Per Hedbor | | Stdio.File f;
if(!(f=open(dir+id+".i", "wct" )))
|
69a869 | 1999-05-18 | Per Hedbor | | {
report_error( "Failed to open image cache persistant cache file "+
dir+id+".i: "+strerror( errno() )+ "\n" );
return;
}
f->write( data );
}
static void store_data( string id, string data )
{
|
1a231e | 2000-06-04 | Martin Nilsson | | if(!data) return;
|
4f2d5f | 2000-03-13 | Per Hedbor | | Stdio.File f;
if(!(f = open(dir+id+".d", "wct" )))
|
69a869 | 1999-05-18 | Per Hedbor | | {
data_cache_insert( id, data );
report_error( "Failed to open image cache persistant cache file "+
|
376017 | 1999-06-27 | Per Hedbor | | dir+id+".d: "+strerror( errno() )+ "\n" );
|
69a869 | 1999-05-18 | Per Hedbor | | return;
}
f->write( data );
}
static mapping restore_meta( string id )
{
Stdio.File f;
if( meta_cache[ id ] )
return meta_cache[ id ];
|
4f2d5f | 2000-03-13 | Per Hedbor | | if( !(f=open(dir+id+".i", "r" ) ) )
|
69a869 | 1999-05-18 | Per Hedbor | | return 0;
|
18e19c | 2000-06-12 | Martin Stjernholm | | string s = f->read();
mapping m;
if (catch (m = decode_value (s))) {
rm (dir + id + ".i");
report_error( "Corrupt data in persistent cache file "+
dir+id+".i; removed it.\n" );
return 0;
}
return meta_cache_insert( id, m );
|
69a869 | 1999-05-18 | Per Hedbor | | }
|
93dd0e | 2000-07-27 | Johan Sundström | | void flush(int|void age)
|
4f2d5f | 2000-03-13 | Per Hedbor | | {
|
1f58d0 | 2000-01-02 | Martin Nilsson | | report_debug("Flushing "+name+" image cache.\n");
|
4f2d5f | 2000-03-13 | Per Hedbor | | foreach(r_get_dir(dir), string f)
if(f[-2]=='.' && (f[-1]=='i' || f[-1]=='d') &&
(!age || age>r_file_stat(dir+f)[2]))
r_rm(dir+f);
|
1f58d0 | 2000-01-02 | Martin Nilsson | | }
|
93dd0e | 2000-07-27 | Johan Sundström | | array(int) status(int|void age)
{
|
1f58d0 | 2000-01-02 | Martin Nilsson | | int files=0, size=0, aged=0;
|
1f4a6c | 2000-08-28 | Per Hedbor | | Stat stat;
|
4f2d5f | 2000-03-13 | Per Hedbor | | foreach(r_get_dir(dir), string f)
|
1f58d0 | 2000-01-02 | Martin Nilsson | | if(f[-2]=='.' && (f[-1]=='i' || f[-1]=='d')) {
files++;
|
4f2d5f | 2000-03-13 | Per Hedbor | | stat=r_file_stat(dir+f,1);
|
1f58d0 | 2000-01-02 | Martin Nilsson | | if(stat[1]>0) size+=stat[1];
if(age<stat[2]) aged++;
}
return ({files, size, aged});
}
|
69a869 | 1999-05-18 | Per Hedbor | | static mapping restore( string id )
{
|
37ecea | 2000-02-21 | Per Hedbor | | mixed f;
|
69a869 | 1999-05-18 | Per Hedbor | | mapping m;
|
37ecea | 2000-02-21 | Per Hedbor | |
|
69a869 | 1999-05-18 | Per Hedbor | | if( data_cache[ id ] )
f = data_cache[ id ];
|
81f8af | 1999-12-20 | Martin Nilsson | | else
|
4f2d5f | 2000-03-13 | Per Hedbor | | if(!(f = open( dir+id+".d", "r" )))
|
37ecea | 2000-02-21 | Per Hedbor | | return 0;
|
69a869 | 1999-05-18 | Per Hedbor | |
m = restore_meta( id );
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
69a869 | 1999-05-18 | Per Hedbor | | if(!m)
return 0;
if( stringp( f ) )
|
0fe69d | 2000-03-19 | Martin Nilsson | | return Roxen.http_string_answer( f, m->type||("image/gif") );
return Roxen.http_file_answer( f, m->type||("image/gif") );
|
69a869 | 1999-05-18 | Per Hedbor | | }
|
93dd0e | 2000-07-27 | Johan Sundström | | string data( array|string|mapping args, RequestID id, int|void nodraw )
|
69a869 | 1999-05-18 | Per Hedbor | | {
string na = store( args, id );
mixed res;
if(!( res = restore( na )) )
{
if(nodraw)
return 0;
draw( na, id );
res = restore( na );
}
if( res->file )
return res->file->read();
return res->data;
}
|
93dd0e | 2000-07-27 | Johan Sundström | | mapping http_file_answer( array|string|mapping data,
|
81f8af | 1999-12-20 | Martin Nilsson | | RequestID id,
|
c3a53d | 1999-06-25 | Per Hedbor | | int|void nodraw )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
69a869 | 1999-05-18 | Per Hedbor | | {
string na = store( data,id );
mixed res;
if(!( res = restore( na )) )
{
if(nodraw)
return 0;
draw( na, id );
res = restore( na );
}
return res;
}
|
93dd0e | 2000-07-27 | Johan Sundström | | mapping metadata( array|string|mapping data,
RequestID id,
int|void nodraw )
|
2ff846 | 1999-09-02 | Per Hedbor | | {
string na = store( data,id );
if(!restore_meta( na ))
{
if(nodraw)
return 0;
draw( na, id );
return restore_meta( na );
}
return restore_meta( na );
}
mapping tomapp( mixed what )
{
if( mappingp( what ))
return what;
return ([ "":what ]);
}
string store( array|string|mapping data, RequestID id )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
2ff846 | 1999-09-02 | Per Hedbor | | {
string ci;
|
f04b92 | 2000-09-13 | Jonas Wallden | | if( mappingp( data ) ) {
if (!data->format) {
#if constant(Image.GIF) && constant(Image.GIF.encode)
data->format = "gif";
#else
data->format = "png";
#endif
}
|
2ff846 | 1999-09-02 | Per Hedbor | | ci = argcache->store( data );
|
f04b92 | 2000-09-13 | Jonas Wallden | | } else if( arrayp( data ) ) {
if (!data[0]->format) {
#if constant(Image.GIF) && constant(Image.GIF.encode)
data[0]->format = "gif";
#else
data[0]->format = "png";
#endif
}
|
93dd0e | 2000-07-27 | Johan Sundström | | ci = map( map( data, tomapp ), argcache->store )*"$";
|
f04b92 | 2000-09-13 | Jonas Wallden | | } else
|
2ff846 | 1999-09-02 | Per Hedbor | | ci = data;
return ci;
}
void set_draw_function( function to )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
2ff846 | 1999-09-02 | Per Hedbor | | {
draw_function = to;
}
void create( string id, function draw_func, string|void d )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
2ff846 | 1999-09-02 | Per Hedbor | | {
|
359074 | 2000-03-08 | Martin Nilsson | | if(!d) d = roxenp()->query("argument_cache_dir");
|
2ff846 | 1999-09-02 | Per Hedbor | | if( d[-1] != '/' )
d+="/";
d += id+"/";
mkdirhier( d+"foo");
dir = d;
name = id;
draw_function = draw_func;
}
}
class ArgCache
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
2ff846 | 1999-09-02 | Per Hedbor | | {
static string name;
static string path;
static int is_db;
|
43665d | 1999-12-19 | Martin Nilsson | | static Sql.sql db;
|
2ff846 | 1999-09-02 | Per Hedbor | |
#define CACHE_VALUE 0
#define CACHE_SKEY 1
#define CACHE_SIZE 600
#define CLEAN_SIZE 100
#ifdef THREADS
static Thread.Mutex mutex = Thread.Mutex();
# define LOCK() object __key = mutex->lock()
#else
|
81f8af | 1999-12-20 | Martin Nilsson | | # define LOCK()
|
2ff846 | 1999-09-02 | Per Hedbor | | #endif
static mapping (string:mixed) cache = ([ ]);
|
b870ac | 1999-09-03 | Henrik Grubbström (Grubba) | | static void setup_table()
|
2ff846 | 1999-09-02 | Per Hedbor | | {
if(catch(db->query("select id from "+name+" where id=-1")))
if(catch(db->query("create table "+name+" ("
"id int auto_increment primary key, "
"lkey varchar(80) not null default '', "
"contents blob not null default '', "
"atime bigint not null default 0)")))
throw("Failed to create table in database\n");
}
|
81f8af | 1999-12-20 | Martin Nilsson | | void create( string _name,
string _path,
|
2ff846 | 1999-09-02 | Per Hedbor | | int _is_db )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
2ff846 | 1999-09-02 | Per Hedbor | | {
name = _name;
path = _path;
is_db = _is_db;
if(is_db)
{
db = Sql.sql( path );
if(!db)
error("Failed to connect to database for argument cache\n");
setup_table( );
} else {
if(path[-1] != '/' && path[-1] != '\\')
path += "/";
path += replace(name, "/", "_")+"/";
mkdirhier( path + "/tmp" );
|
4f2d5f | 2000-03-13 | Per Hedbor | | Stdio.File test;
if (!(test = open (path + "/.testfile", "wc")))
error ("Can't create files in the argument cache directory " +
path +
#if constant(strerror)
" ("+strerror(errno())+
#endif
"\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | }
}
static string read_args( string id )
{
if( is_db )
{
|
43665d | 1999-12-19 | Martin Nilsson | | array res = db->query("select contents from "+name+" where id='"+id+"'");
|
2ff846 | 1999-09-02 | Per Hedbor | | if( sizeof(res) )
{
|
add34e | 2000-08-17 | Per Hedbor | | db->query("update "+name+" set atime='"+time(1)+"' where id='"+id+"'");
|
2ff846 | 1999-09-02 | Per Hedbor | | return res[0]->contents;
}
return 0;
} else {
|
4f2d5f | 2000-03-13 | Per Hedbor | | Stdio.File f;
if( search( id, "/" )<0 && (f = open(path+"/"+id, "r")))
return f->read();
|
2ff846 | 1999-09-02 | Per Hedbor | | }
return 0;
}
|
a7def0 | 2000-06-26 | Per Hedbor | | string tohex( string what )
{
|
ae46e9 | 2000-09-23 | Per Hedbor | | #if constant(Gmp.mpz)
|
a7def0 | 2000-06-26 | Per Hedbor | | return sprintf( "%x", Gmp.mpz( what, 256 ) );
|
ae46e9 | 2000-09-23 | Per Hedbor | | #else
int i = 0;
for( int q = 0; q<strlen(what); q++ )
{
i<<=8;
i |= what[strlen(what)-1-q];
}
return sprintf( "%x", i );
#endif
|
a7def0 | 2000-06-26 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | static string create_key( string long_key )
{
if( is_db )
{
|
43665d | 1999-12-19 | Martin Nilsson | | array data = db->query(sprintf("select id,contents from %s where lkey='%s'",
|
2ff846 | 1999-09-02 | Per Hedbor | | name,long_key[..79]));
foreach( data, mapping m )
if( m->contents == long_key )
return m->id;
db->query( sprintf("insert into %s (contents,lkey,atime) values "
|
81f8af | 1999-12-20 | Martin Nilsson | | "('%s','%s','%d')",
|
2ff846 | 1999-09-02 | Per Hedbor | | name, long_key, long_key[..79], time() ));
return create_key( long_key );
} else {
|
a7def0 | 2000-06-26 | Per Hedbor | | string _key=tohex(Crypto.md5()->update(long_key)->digest());
|
2ff846 | 1999-09-02 | Per Hedbor | | _key = replace(_key-"=","/","=");
string short_key = _key[0..1];
|
4f2d5f | 2000-03-13 | Per Hedbor | | Stdio.File f;
while( f = open( path+short_key, "r" ) )
|
2ff846 | 1999-09-02 | Per Hedbor | | {
|
4f2d5f | 2000-03-13 | Per Hedbor | | if( f->read() == long_key )
|
2ff846 | 1999-09-02 | Per Hedbor | | return short_key;
short_key = _key[..strlen(short_key)];
if( strlen(short_key) >= strlen(_key) )
short_key += ".";
}
|
4f2d5f | 2000-03-13 | Per Hedbor | | f = open( path+short_key, "wct" );
|
2ff846 | 1999-09-02 | Per Hedbor | | f->write( long_key );
return short_key;
}
}
int key_exists( string key )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
2ff846 | 1999-09-02 | Per Hedbor | | {
LOCK();
|
4f2d5f | 2000-03-13 | Per Hedbor | | if( !is_db )
return !!open( path+key, "r" );
|
2ff846 | 1999-09-02 | Per Hedbor | | return !!read_args( key );
}
string store( mapping args )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
69a869 | 1999-05-18 | Per Hedbor | | {
|
2ff846 | 1999-09-02 | Per Hedbor | | LOCK();
array b = values(args), a = sort(indices(args),b);
string data = MIME.encode_base64(encode_value(({a,b})),1);
if( cache[ data ] )
return cache[ data ][ CACHE_SKEY ];
|
8693b2 | 2000-01-08 | Martin Stjernholm | | if( sizeof( cache ) >= CACHE_SIZE )
{
array i = indices(cache);
while( sizeof(cache) > CACHE_SIZE-CLEAN_SIZE ) {
string idx=i[random(sizeof(i))];
if(arrayp(cache[idx])) {
m_delete( cache, cache[idx][CACHE_SKEY] );
m_delete( cache, idx );
}
else {
m_delete( cache, cache[idx] );
m_delete( cache, idx );
}
}
}
|
2ff846 | 1999-09-02 | Per Hedbor | | string id = create_key( data );
cache[ data ] = ({ 0, 0 });
cache[ data ][ CACHE_VALUE ] = copy_value( args );
cache[ data ][ CACHE_SKEY ] = id;
cache[ id ] = data;
return id;
|
69a869 | 1999-05-18 | Per Hedbor | | }
|
56b7fc | 1999-12-21 | Per Hedbor | | mapping lookup( string id, array|void client )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
c3a53d | 1999-06-25 | Per Hedbor | | {
|
2ff846 | 1999-09-02 | Per Hedbor | | LOCK();
|
c5e096 | 1999-10-04 | Per Hedbor | | if(cache[id] && cache[ cache[id] ] )
|
2ff846 | 1999-09-02 | Per Hedbor | | return cache[cache[id]][CACHE_VALUE];
string q = read_args( id );
|
bfb4d4 | 1999-12-28 | Martin Nilsson | | if(!q)
|
56b7fc | 1999-12-21 | Per Hedbor | | if( client )
error("Key does not exist! (Thinks "+ (client*"") +")\n");
else
error("Requesting unknown key\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | mixed data = decode_value(MIME.decode_base64( q ));
data = mkmapping( data[0],data[1] );
cache[ q ] = ({0,0});
cache[ q ][ CACHE_VALUE ] = data;
cache[ q ][ CACHE_SKEY ] = id;
cache[ id ] = q;
return data;
|
c3a53d | 1999-06-25 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | void delete( string id )
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
69a869 | 1999-05-18 | Per Hedbor | | {
|
2ff846 | 1999-09-02 | Per Hedbor | | LOCK();
if(cache[id])
{
m_delete( cache, cache[id] );
m_delete( cache, id );
}
if( is_db )
db->query( "delete from "+name+" where id='"+id+"'" );
|
69a869 | 1999-05-18 | Per Hedbor | | else
|
4f2d5f | 2000-03-13 | Per Hedbor | | r_rm( path+id );
|
69a869 | 1999-05-18 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | }
|
69a869 | 1999-05-18 | Per Hedbor | |
|
95b69e | 1999-09-05 | Per Hedbor | | mapping cached_decoders = ([]);
string decode_charset( string charset, string data )
{
|
808e17 | 1999-09-05 | Henrik Grubbström (Grubba) | |
|
95b69e | 1999-09-05 | Per Hedbor | | if( charset == "iso-8859-1" ) return data;
if( !cached_decoders[ charset ] )
cached_decoders[ charset ] = Locale.Charset.decoder( charset );
data = cached_decoders[ charset ]->feed( data )->drain();
|
dc3a47 | 1999-12-15 | Marcus Comstedt | | cached_decoders[ charset ]->clear();
|
95b69e | 1999-09-05 | Per Hedbor | | return data;
}
|
2ff846 | 1999-09-02 | Per Hedbor | | void create()
{
|
23414a | 2000-07-21 | Andreas Lange | |
#define __REG_PROJ Locale.register_project
|
4ee494 | 2000-07-21 | Andreas Lange | | __REG_PROJ("roxen_start", "translations/%L/roxen_start.xml");
__REG_PROJ("roxen_config", "translations/%L/roxen_config.xml");
|
23414a | 2000-07-21 | Andreas Lange | | __REG_PROJ("roxen_message", "translations/%L/roxen_message.xml");
|
c8ee71 | 2000-09-09 | Andreas Lange | | __REG_PROJ("admin_tasks", "translations/%L/admin_tasks.xml");
|
23414a | 2000-07-21 | Andreas Lange | | #undef __REG_PROJ
|
5629e6 | 2000-07-11 | Andreas Lange | | define_global_variables();
|
cde8d6 | 2000-02-16 | Per Hedbor | |
|
970e24 | 2000-09-12 | Per Hedbor | |
|
dbfe9d | 2000-04-13 | Per Hedbor | | master()->resolv ("RXML.refs");
master()->resolv ("RXML.PXml");
master()->resolv ("RXML.PEnt");
|
970e24 | 2000-09-12 | Per Hedbor | |
|
a40c15 | 2000-02-16 | Per Hedbor | | dump( "etc/roxen_master.pike" );
|
cde8d6 | 2000-02-16 | Per Hedbor | | dump( "etc/modules/Dims.pmod" );
|
970e24 | 2000-09-12 | Per Hedbor | |
|
dbfe9d | 2000-04-13 | Per Hedbor | | foreach(({ "module.pmod","PEnt.pike", "PExpr.pike","PXml.pike",
"refs.pmod","utils.pmod" }), string q )
|
27008b | 2000-03-20 | Martin Stjernholm | | dump( "etc/modules/RXML.pmod/"+ q );
dump( "etc/modules/Roxen.pmod" );
|
970e24 | 2000-09-12 | Per Hedbor | | dump( "base_server/config_userdb.pike" );
|
cde8d6 | 2000-02-16 | Per Hedbor | | dump( "base_server/disk_cache.pike" );
|
2537c3 | 2000-02-14 | Per Hedbor | | dump( "base_server/roxen.pike" );
|
7e596b | 2000-02-13 | Per Hedbor | | dump( "base_server/roxenlib.pike" );
|
6f72d4 | 2000-02-08 | Per Hedbor | | dump( "base_server/basic_defvar.pike" );
|
2ff846 | 1999-09-02 | Per Hedbor | | dump( "base_server/newdecode.pike" );
dump( "base_server/read_config.pike" );
dump( "base_server/global_variables.pike" );
dump( "base_server/module_support.pike" );
dump( "base_server/http.pike" );
dump( "base_server/socket.pike" );
dump( "base_server/cache.pike" );
dump( "base_server/supports.pike" );
dump( "base_server/hosts.pike");
dump( "base_server/language.pike");
|
69a869 | 1999-05-18 | Per Hedbor | |
|
95b69e | 1999-09-05 | Per Hedbor | | #ifndef __NT__
|
640cc4 | 1999-11-29 | Per Hedbor | | if(!getuid())
|
95b69e | 1999-09-05 | Per Hedbor | | add_constant("Privs", Privs);
|
640cc4 | 1999-11-29 | Per Hedbor | | else
|
95b69e | 1999-09-05 | Per Hedbor | | #endif /* !__NT__ */
|
eb6ce9 | 2000-01-03 | Martin Stjernholm | | add_constant("Privs", class {
void create(string reason, int|string|void uid, int|string|void gid) {}
});
|
69a869 | 1999-05-18 | Per Hedbor | |
|
855c39 | 1999-11-29 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | |
|
d3b98e | 1999-11-28 | Per Hedbor | |
|
95b69e | 1999-09-05 | Per Hedbor | | add_constant( "ArgCache", ArgCache );
|
27008b | 2000-03-20 | Martin Stjernholm | |
|
2ff846 | 1999-09-02 | Per Hedbor | |
|
95b69e | 1999-09-05 | Per Hedbor | | add_constant( "roxen", this_object());
|
27008b | 2000-03-20 | Martin Stjernholm | |
|
855c39 | 1999-11-29 | Per Hedbor | |
add_constant( "RoxenModule", RoxenModule);
|
67c749 | 1999-12-27 | Martin Stjernholm | | add_constant( "ModuleInfo", ModuleInfo );
|
855c39 | 1999-11-29 | Per Hedbor | |
|
95b69e | 1999-09-05 | Per Hedbor | | add_constant( "load", load);
add_constant( "Roxen.set_locale", set_locale );
|
2537c3 | 2000-02-14 | Per Hedbor | | add_constant( "roxen.locale", locale );
|
27008b | 2000-03-20 | Martin Stjernholm | |
|
2537c3 | 2000-02-14 | Per Hedbor | |
|
a40c15 | 2000-02-16 | Per Hedbor | |
|
9a8a15 | 2000-09-25 | Per Hedbor | | _configuration = (program)"configuration";
|
7e596b | 2000-02-13 | Per Hedbor | | dump( "base_server/configuration.pike" );
dump( "base_server/rxmlhelp.pike" );
|
9a8a15 | 2000-09-25 | Per Hedbor | | add_constant( "Configuration", _configuration );
|
7e596b | 2000-02-13 | Per Hedbor | |
|
a40c15 | 2000-02-16 | Per Hedbor | |
|
69a869 | 1999-05-18 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | int set_u_and_gid()
|
93dd0e | 2000-07-27 | Johan Sundström | |
|
2ff846 | 1999-09-02 | Per Hedbor | | {
#ifndef __NT__
string u, g;
int uid, gid;
array pw;
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
2ff846 | 1999-09-02 | Per Hedbor | | u=QUERY(User);
sscanf(u, "%s:%s", u, g);
if(strlen(u))
{
if(getuid())
{
|
49cd28 | 2000-08-11 | Andreas Lange | | report_error(LOC_M(24, "It is only possible to change uid and gid "
|
c8ee71 | 2000-09-09 | Andreas Lange | | "if the server is running as root.")+"\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | } else {
if (g) {
#if constant(getgrnam)
pw = getgrnam (g);
if (!pw)
if (sscanf (g, "%d", gid)) pw = getgrgid (gid), g = (string) gid;
else report_error ("Couldn't resolve group " + g + ".\n"), g = 0;
if (pw) g = pw[0], gid = pw[2];
|
a22f6f | 1999-05-12 | Per Hedbor | | #else
|
2ff846 | 1999-09-02 | Per Hedbor | | if (!sscanf (g, "%d", gid))
report_warning ("Can't resolve " + g + " to gid on this system; "
"numeric gid required.\n");
|
a22f6f | 1999-05-12 | Per Hedbor | | #endif
|
2ff846 | 1999-09-02 | Per Hedbor | | }
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | pw = getpwnam (u);
if (!pw)
if (sscanf (u, "%d", uid)) pw = getpwuid (uid), u = (string) uid;
else {
report_error ("Couldn't resolve user " + u + ".\n");
return 0;
}
if (pw) {
u = pw[0], uid = pw[2];
if (!g) gid = pw[3];
}
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | #ifdef THREADS
object mutex_key;
catch { mutex_key = euid_egid_lock->lock(); };
object threads_disabled = _disable_threads();
#endif
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | #if constant(seteuid)
if (geteuid() != getuid()) seteuid (getuid());
#endif
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | #if constant(initgroups)
catch {
initgroups(pw[0], gid);
};
#endif
if (QUERY(permanent_uid)) {
#if constant(setuid)
if (g) {
# if constant(setgid)
setgid(gid);
|
23414a | 2000-07-21 | Andreas Lange | | if (getgid() != gid) {
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(25, "Failed to set gid.")+"\n");
|
23414a | 2000-07-21 | Andreas Lange | | g = 0;
}
|
2ff846 | 1999-09-02 | Per Hedbor | | # else
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_warning(LOC_M(26, "Setting gid not supported on this system.")
+"\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | g = 0;
# endif
}
setuid(uid);
|
23414a | 2000-07-21 | Andreas Lange | | if (getuid() != uid) {
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(27, "Failed to set uid.")+"\n");
|
23414a | 2000-07-21 | Andreas Lange | | u = 0;
}
if (u) report_notice(CALL_M("setting_uid_gid_permanently", "eng")
|
1c783c | 2000-07-15 | Andreas Lange | | (uid, gid, u, g));
|
2ff846 | 1999-09-02 | Per Hedbor | | #else
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_warning(LOC_M(28, "Setting uid not supported on this system.")
+"\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | u = g = 0;
#endif
}
|
b84a16 | 1999-06-07 | Martin Stjernholm | | else {
|
2ff846 | 1999-09-02 | Per Hedbor | | #if constant(seteuid)
if (g) {
# if constant(setegid)
setegid(gid);
|
23414a | 2000-07-21 | Andreas Lange | | if (getegid() != gid) {
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(29, "Failed to set effective gid.")+"\n");
|
23414a | 2000-07-21 | Andreas Lange | | g = 0;
}
|
2ff846 | 1999-09-02 | Per Hedbor | | # else
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_warning(LOC_M(30, "Setting effective gid not supported on "
"this system.")+"\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | g = 0;
# endif
}
seteuid(uid);
|
23414a | 2000-07-21 | Andreas Lange | | if (geteuid() != uid) {
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error(LOC_M(31, "Failed to set effective uid.")+"\n");
|
23414a | 2000-07-21 | Andreas Lange | | u = 0;
}
if (u) report_notice(CALL_M("setting_uid_gid", "eng")(uid, gid, u, g));
|
2ff846 | 1999-09-02 | Per Hedbor | | #else
|
49cd28 | 2000-08-11 | Andreas Lange | | report_warning(LOC_M(32, "Setting effective uid not supported on "
|
c8ee71 | 2000-09-09 | Andreas Lange | | "this system.")+"\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | u = g = 0;
#endif
|
b84a16 | 1999-06-07 | Martin Stjernholm | | }
|
2ff846 | 1999-09-02 | Per Hedbor | |
return !!u;
|
a22f6f | 1999-05-12 | Per Hedbor | | }
}
|
2ff846 | 1999-09-02 | Per Hedbor | | #endif
return 0;
}
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | void reload_all_configurations()
{
object conf;
array (object) new_confs = ({});
mapping config_cache = ([]);
int modified;
setvars(retrieve("Variables", 0));
foreach(list_all_configurations(), string config)
|
a22f6f | 1999-05-12 | Per Hedbor | | {
|
1f4a6c | 2000-08-28 | Per Hedbor | | mixed err;
Stat st;
|
4cf783 | 2000-02-16 | Per Hedbor | | conf = find_configuration( config );
|
2ff846 | 1999-09-02 | Per Hedbor | | if(!(st = config_is_modified(config))) {
if(conf) {
config_cache[config] = config_stat_cache[config];
new_confs += ({ conf });
|
a22f6f | 1999-05-12 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | continue;
|
a22f6f | 1999-05-12 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | modified = 1;
config_cache[config] = st;
|
4cf783 | 2000-02-16 | Per Hedbor | | if(conf)
{
|
2ff846 | 1999-09-02 | Per Hedbor | | conf->stop();
conf->invalidate_cache();
conf->create(conf->name);
|
a22f6f | 1999-05-12 | Per Hedbor | | } else {
|
2ff846 | 1999-09-02 | Per Hedbor | | if(err = catch
|
a22f6f | 1999-05-12 | Per Hedbor | | {
|
2ff846 | 1999-09-02 | Per Hedbor | | conf = enable_configuration(config);
}) {
|
67f60e | 2000-07-09 | Martin Nilsson | | string bt=describe_backtrace(err);
|
49cd28 | 2000-08-11 | Andreas Lange | | report_error(LOC_M(33, "Error while enabling configuration %s%s"),
|
23414a | 2000-07-21 | Andreas Lange | | config, (bt ? ":\n"+bt : "\n"));
|
2ff846 | 1999-09-02 | Per Hedbor | | continue;
|
a22f6f | 1999-05-12 | Per Hedbor | | }
}
|
2ff846 | 1999-09-02 | Per Hedbor | | if(err = catch
{
conf->start();
conf->enable_all_modules();
}) {
|
67f60e | 2000-07-09 | Martin Nilsson | | string bt=describe_backtrace(err);
|
49cd28 | 2000-08-11 | Andreas Lange | | report_error(LOC_M(33, "Error while enabling configuration %s%s"),
|
23414a | 2000-07-21 | Andreas Lange | | config, (bt ? ":\n"+bt : "\n" ));
|
2ff846 | 1999-09-02 | Per Hedbor | | continue;
}
new_confs += ({ conf });
|
a22f6f | 1999-05-12 | Per Hedbor | | }
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
2ff846 | 1999-09-02 | Per Hedbor | | foreach(configurations - new_confs, conf)
|
a22f6f | 1999-05-12 | Per Hedbor | | {
|
2ff846 | 1999-09-02 | Per Hedbor | | modified = 1;
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_notice(LOC_M(34,"Disabling old configuration %s")+"\n", conf->name);
|
acf0fa | 2000-02-29 | David Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | conf->stop();
destruct(conf);
}
if(modified) {
configurations = new_confs;
|
f02f5c | 2000-08-03 | Martin Stjernholm | | fix_config_lookup();
|
2ff846 | 1999-09-02 | Per Hedbor | | config_stat_cache = config_cache;
|
a22f6f | 1999-05-12 | Per Hedbor | | }
|
2ff846 | 1999-09-02 | Per Hedbor | | }
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
d09399 | 2000-09-25 | Per Hedbor | | private mapping(string:Configuration) config_lookup = ([]);
|
f02f5c | 2000-08-03 | Martin Stjernholm | |
void fix_config_lookup()
{
config_lookup = mkmapping (configurations->name, configurations);
#ifdef DEBUG
if (sizeof (configurations) != sizeof (config_lookup))
error ("Duplicate configuration names in configurations array: %O",
configurations->name);
#endif
}
|
9a8a15 | 2000-09-25 | Per Hedbor | | Configuration get_configuration (string name)
|
45dc02 | 2000-08-16 | Martin Stjernholm | |
|
f02f5c | 2000-08-03 | Martin Stjernholm | | {
#ifdef DEBUG
if (sizeof (configurations) != sizeof (config_lookup))
error ("config_lookup out of synch with configurations.\n");
#endif
return config_lookup[name];
}
|
9a8a15 | 2000-09-25 | Per Hedbor | | Configuration enable_configuration(string name)
|
2ff846 | 1999-09-02 | Per Hedbor | | {
|
f02f5c | 2000-08-03 | Martin Stjernholm | | #ifdef DEBUG
if (get_configuration (name))
error ("A configuration called %O already exists.\n", name);
#endif
|
9a8a15 | 2000-09-25 | Per Hedbor | | Configuration cf = _configuration( name );
|
2ff846 | 1999-09-02 | Per Hedbor | | configurations += ({ cf });
|
bc5c2a | 2000-08-23 | Per Hedbor | | fix_config_lookup();
|
2ff846 | 1999-09-02 | Per Hedbor | | return cf;
}
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
45dc02 | 2000-08-16 | Martin Stjernholm | | void disable_configuration (string name)
{
|
bc5c2a | 2000-08-23 | Per Hedbor | | if (object conf = config_lookup[ name ]) {
|
45dc02 | 2000-08-16 | Martin Stjernholm | | configurations -= ({conf});
|
bc5c2a | 2000-08-23 | Per Hedbor | | fix_config_lookup();
|
45dc02 | 2000-08-16 | Martin Stjernholm | | }
}
void remove_configuration (string name)
{
disable_configuration (name);
::remove_configuration (name);
}
|
2ff846 | 1999-09-02 | Per Hedbor | |
void enable_configurations()
{
array err;
configurations = ({});
|
f02f5c | 2000-08-03 | Martin Stjernholm | | config_lookup = ([]);
|
15635b | 1999-10-10 | Per Hedbor | |
|
2ff846 | 1999-09-02 | Per Hedbor | | foreach(list_all_configurations(), string config)
|
1d7d6d | 2000-02-16 | Per Hedbor | | {
int t = gethrtime();
|
2dd46b | 2000-03-24 | Per Hedbor | | report_debug("\nEnabling the configuration %s ...\n", config);
|
9a8a15 | 2000-09-25 | Per Hedbor | | if(err=catch( enable_configuration(config)->start(0) ))
|
c8ee71 | 2000-09-09 | Andreas Lange | | report_error("\n"+LOC_M(35, "Error while loading configuration %s%s"),
config+":\n", describe_backtrace(err)+"\n");
|
2dd46b | 2000-03-24 | Per Hedbor | | report_debug("Enabled %s in %.1fms\n", config, (gethrtime()-t)/1000.0 );
|
1d7d6d | 2000-02-16 | Per Hedbor | | }
|
d09399 | 2000-09-25 | Per Hedbor | | foreach( configurations, Configuration c )
|
abc59a | 2000-08-23 | Per Hedbor | | {
if(sizeof( c->registered_urls ) )
return;
}
report_fatal("No configurations could open any ports. Will shutdown.\n");
shutdown();
|
2ff846 | 1999-09-02 | Per Hedbor | | }
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
1d7d6d | 2000-02-16 | Per Hedbor | | int all_modules_loaded;
|
2ff846 | 1999-09-02 | Per Hedbor | | void enable_configurations_modules()
{
|
4cf783 | 2000-02-16 | Per Hedbor | | if( all_modules_loaded++ ) return;
|
d09399 | 2000-09-25 | Per Hedbor | | foreach(configurations, Configuration config)
|
4cf783 | 2000-02-16 | Per Hedbor | | if(mixed err=catch( config->enable_all_modules() ))
|
49cd28 | 2000-08-11 | Andreas Lange | | report_error(LOC_M(36, "Error while loading modules in "
|
c8ee71 | 2000-09-09 | Andreas Lange | | "configuration %s%s"),
config->name+":\n", describe_backtrace(err)+"\n");
|
a22f6f | 1999-05-12 | Per Hedbor | | }
|
d68184 | 2000-02-08 | Per Hedbor | | mapping low_decode_image(string data, void|mixed tocolor)
|
a22f6f | 1999-05-12 | Per Hedbor | | {
|
91a156 | 2000-04-11 | Per Hedbor | | mapping w = Image._decode( data, tocolor );
if( w->image ) return w;
return 0;
|
a22f6f | 1999-05-12 | Per Hedbor | | }
|
d68184 | 2000-02-08 | Per Hedbor | | array(Image.Layer) decode_layers(string data, void|mixed tocolor)
{
return Image.decode_layers( data, tocolor );
}
|
81f8af | 1999-12-20 | Martin Nilsson | | mapping low_load_image(string f, RequestID id)
|
a22f6f | 1999-05-12 | Per Hedbor | | {
string data;
|
81f8af | 1999-12-20 | Martin Nilsson | | Stdio.File file;
if(id->misc->_load_image_called < 5)
|
69a869 | 1999-05-18 | Per Hedbor | | {
|
3ffe06 | 1999-05-16 | David Hedbor | |
id->misc->_load_image_called++;
|
c52692 | 1999-05-18 | Per Hedbor | | if(!(data=id->conf->try_get_file(f, id)))
{
|
3ffe06 | 1999-05-16 | David Hedbor | | file=Stdio.File();
|
c52692 | 1999-05-18 | Per Hedbor | | if(!file->open(f,"r") || !(data=file->read()))
|
76ad0a | 2000-09-14 | Martin Nilsson | | #ifdef THREADS
|
56b7fc | 1999-12-21 | Per Hedbor | | catch
{
|
91a156 | 2000-04-11 | Per Hedbor | | string host = "";
sscanf( f, "http://%[^/]", host );
if( sscanf( host, "%*s:%*d" ) != 2)
host += ":80";
mapping hd =
([
"User-Agent":version(),
"Host":host,
]);
data = Protocols.HTTP.get_url_data( f, 0, hd );
|
56b7fc | 1999-12-21 | Per Hedbor | | };
|
76ad0a | 2000-09-14 | Martin Nilsson | | #endif
|
56b7fc | 1999-12-21 | Per Hedbor | | if( !data )
|
3ffe06 | 1999-05-16 | David Hedbor | | return 0;
}
}
id->misc->_load_image_called = 0;
|
56b7fc | 1999-12-21 | Per Hedbor | | if(!data) return 0;
|
a22f6f | 1999-05-12 | Per Hedbor | | return low_decode_image( data );
}
|
d68184 | 2000-02-08 | Per Hedbor | | array(Image.Layer) load_layers(string f, RequestID id)
{
string data;
Stdio.File file;
if(id->misc->_load_image_called < 5)
{
id->misc->_load_image_called++;
if(!(data=id->conf->try_get_file(f, id)))
{
file=Stdio.File();
if(!file->open(f,"r") || !(data=file->read()))
|
8ace00 | 2000-09-19 | Martin Nilsson | | #ifdef THREADS
|
d68184 | 2000-02-08 | Per Hedbor | | catch
{
data = Protocols.HTTP.get_url_nice( f )[1];
};
|
8ace00 | 2000-09-19 | Martin Nilsson | | #endif
|
d68184 | 2000-02-08 | Per Hedbor | | if( !data )
return 0;
}
}
id->misc->_load_image_called = 0;
if(!data) return 0;
return decode_layers( data );
}
|
a22f6f | 1999-05-12 | Per Hedbor | |
|
2537c3 | 2000-02-14 | Per Hedbor | | Image.Image load_image(string f, RequestID id)
|
a22f6f | 1999-05-12 | Per Hedbor | | {
mapping q = low_load_image( f, id );
if( q ) return q->img;
return 0;
}
|
b1fca0 | 1996-11-12 | Per Hedbor | |
private void fix_root(string to)
{
|
c79b26 | 1998-02-05 | Johan Schön | | #ifndef __NT__
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(getuid())
{
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("It is impossible to chroot() if the server is not run as root.\n");
|
b1fca0 | 1996-11-12 | Per Hedbor | | return;
}
if(!chroot(to))
{
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Roxen: Cannot chroot to "+to+": ");
|
b1fca0 | 1996-11-12 | Per Hedbor | | #if efun(real_perror)
real_perror();
#endif
return;
}
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Root is now "+to+".\n");
|
c79b26 | 1998-02-05 | Johan Schön | | #endif
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
void create_pid_file(string where)
{
|
c79b26 | 1998-02-05 | Johan Schön | | #ifndef __NT__
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(!where) return;
|
81f8af | 1999-12-20 | Martin Nilsson | | where = replace(where, ({ "$pid", "$uid" }),
|
b1fca0 | 1996-11-12 | Per Hedbor | | ({ (string)getpid(), (string)getuid() }));
|
4f2d5f | 2000-03-13 | Per Hedbor | | r_rm(where);
|
5e8921 | 1997-02-13 | Per Hedbor | | if(catch(Stdio.write_file(where, sprintf("%d\n%d", getpid(), getppid()))))
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("I cannot create the pid file ("+where+").\n");
|
c79b26 | 1998-02-05 | Johan Schön | | #endif
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
95b69e | 1999-09-05 | Per Hedbor | | program pipe;
|
ccffe7 | 1999-03-05 | Wilhelm Köhler | | object shuffle(object from, object to,
object|void to2, function(:void)|void callback)
|
14179b | 1997-01-29 | Per Hedbor | | {
|
beaca0 | 1998-02-20 | Per Hedbor | | #if efun(spider.shuffle)
if(!to2)
|
5bc199 | 1997-01-29 | Per Hedbor | | {
|
95b69e | 1999-09-05 | Per Hedbor | | if(!pipe)
pipe = ((program)"smartpipe");
object p = pipe( );
|
beaca0 | 1998-02-20 | Per Hedbor | | p->input(from);
|
95e2b4 | 1997-05-25 | Wilhelm Köhler | | p->set_done_callback(callback);
|
beaca0 | 1998-02-20 | Per Hedbor | | p->output(to);
|
ccffe7 | 1999-03-05 | Wilhelm Köhler | | return p;
|
a60c4c | 1997-07-03 | Henrik Grubbström (Grubba) | | } else {
|
beaca0 | 1998-02-20 | Per Hedbor | | #endif
object p = Pipe.pipe();
if (callback) p->set_done_callback(callback);
p->output(to);
|
33b770 | 1998-03-20 | Johan Schön | | if(to2) p->output(to2);
|
beaca0 | 1998-02-20 | Per Hedbor | | p->input(from);
|
ccffe7 | 1999-03-05 | Wilhelm Köhler | | return p;
|
beaca0 | 1998-02-20 | Per Hedbor | | #if efun(spider.shuffle)
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
beaca0 | 1998-02-20 | Per Hedbor | | #endif
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
3aaaa7 | 1997-06-12 | Wilhelm Köhler | |
|
4f4bc1 | 1998-02-04 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | static private int _recurse;
|
a9d811 | 1998-09-01 | Henrik Grubbström (Grubba) | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | void exit_when_done()
{
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Interrupt request received. Exiting,\n");
|
4f4bc1 | 1998-02-04 | Per Hedbor | | die_die_die=1;
|
c5e096 | 1999-10-04 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(++_recurse > 4)
|
38dca8 | 1996-12-10 | Per Hedbor | | {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Exiting roxen (spurious signals received).\n");
|
c5e096 | 1999-10-04 | Per Hedbor | | configurations->stop();
|
1f79ba | 1998-09-01 | Marcus Comstedt | | #ifdef THREADS
stop_handler_threads();
#endif /* THREADS */
|
3835ca | 1998-01-16 | Henrik Grubbström (Grubba) | | exit(-1);
|
38dca8 | 1996-12-10 | Per Hedbor | | }
|
81f8af | 1999-12-20 | Martin Nilsson | |
report_debug("Exiting roxen.\n");
|
c5e096 | 1999-10-04 | Per Hedbor | | configurations->stop();
|
1f79ba | 1998-09-01 | Marcus Comstedt | | #ifdef THREADS
|
c5e096 | 1999-10-04 | Per Hedbor | | stop_handler_threads();
|
1f79ba | 1998-09-01 | Marcus Comstedt | | #endif /* THREADS */
|
c5e096 | 1999-10-04 | Per Hedbor | | exit(-1);
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
void exit_it()
{
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Recursive signals.\n");
|
3835ca | 1998-01-16 | Henrik Grubbström (Grubba) | | exit(-1);
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
1c9d08 | 1999-07-19 | Henrik Grubbström (Grubba) | |
void describe_all_threads()
{
array(mixed) all_backtraces;
#if constant(all_threads)
all_backtraces = all_threads()->backtrace();
#else /* !constant(all_threads) */
all_backtraces = ({ backtrace() });
#endif /* constant(all_threads) */
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Describing all threads:\n");
|
1c9d08 | 1999-07-19 | Henrik Grubbström (Grubba) | | int i;
for(i=0; i < sizeof(all_backtraces); i++) {
|
81f8af | 1999-12-20 | Martin Nilsson | | report_debug("Thread %d:\n"
"%s\n",
i+1,
describe_backtrace(all_backtraces[i]));
|
1c9d08 | 1999-07-19 | Henrik Grubbström (Grubba) | | }
}
|
753a83 | 1999-08-30 | Per Hedbor | |
void dump( string file )
{
|
dde833 | 1999-11-15 | Per Hedbor | | if( file[0] != '/' )
file = getcwd() +"/"+ file;
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | #ifdef __NT__
|
dbfe9d | 2000-04-13 | Per Hedbor | | file = normalize_path( file );
|
f52669 | 2000-05-16 | Henrik Grubbström (Grubba) | | #endif
|
dde833 | 1999-11-15 | Per Hedbor | | program p = master()->programs[ replace(file, "//", "/" ) ];
|
dbfe9d | 2000-04-13 | Per Hedbor | | #ifdef __NT__
if( !p )
{
if( sscanf( file, "%*s:/%s", file ) )
{
file = "/"+file;
p = master()->programs[ replace(file, "//", "/" ) ];
}
}
#endif
|
753a83 | 1999-08-30 | Per Hedbor | | array q;
|
2bb924 | 1999-11-24 | Per Hedbor | |
|
753a83 | 1999-08-30 | Per Hedbor | | if(!p)
|
2ff846 | 1999-09-02 | Per Hedbor | | {
#ifdef DUMP_DEBUG
|
81f8af | 1999-12-20 | Martin Nilsson | | werror(file+" not loaded, and thus cannot be dumped.\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | #endif
|
753a83 | 1999-08-30 | Per Hedbor | | return;
|
2ff846 | 1999-09-02 | Per Hedbor | | }
|
753a83 | 1999-08-30 | Per Hedbor | |
|
2bb924 | 1999-11-24 | Per Hedbor | | string ofile = master()->make_ofilename( replace(file, "//", "/") );
if(!file_stat( ofile ) ||
(file_stat( ofile )[ ST_MTIME ] < file_stat(file)[ ST_MTIME ]))
|
2ff846 | 1999-09-02 | Per Hedbor | | {
|
2bb924 | 1999-11-24 | Per Hedbor | | if(q=catch( master()->dump_program( replace(file, "//", "/"), p ) ) )
|
1b9902 | 2000-04-06 | Leif Stensson | | {
|
2ff846 | 1999-09-02 | Per Hedbor | | #ifdef DUMP_DEBUG
|
1b9902 | 2000-04-06 | Leif Stensson | | report_debug("** Cannot encode "+file+": "+describe_backtrace(q)+"\n");
#else
|
c6fd2e | 2000-09-03 | Per Hedbor | |
|
646ba3 | 2000-04-06 | Martin Stjernholm | | #endif
|
1b9902 | 2000-04-06 | Leif Stensson | | }
|
646ba3 | 2000-04-06 | Martin Stjernholm | | #ifdef DUMP_DEBUG
else
werror( file+" dumped successfully to "+ofile+"\n" );
|
2ff846 | 1999-09-02 | Per Hedbor | | #endif
}
#ifdef DUMP_DEBUG
else
|
646ba3 | 2000-04-06 | Martin Stjernholm | | werror(file+" already dumped (and up to date)\n");
|
2ff846 | 1999-09-02 | Per Hedbor | | #endif
|
753a83 | 1999-08-30 | Per Hedbor | | }
|
a57779 | 1999-11-24 | Per Hedbor | | program slowpipe, fastpipe;
|
1d7d6d | 2000-02-16 | Per Hedbor | | void initiate_argcache()
{
int t = gethrtime();
report_debug( "Initiating argument cache ... ");
int id;
string cp = QUERY(argument_cache_dir), na = "args";
if( QUERY(argument_cache_in_db) )
{
id = 1;
cp = QUERY(argument_cache_db_path);
na = "argumentcache";
}
mixed e;
e = catch( argcache = ArgCache(na,cp,id) );
if( e )
{
|
d77f9a | 2000-03-28 | Per Hedbor | | report_fatal( "Failed to initialize the global argument cache:\n"
|
1d7d6d | 2000-02-16 | Per Hedbor | | + (describe_backtrace( e )/"\n")[0]+"\n");
|
d77f9a | 2000-03-28 | Per Hedbor | | sleep(10);
exit(1);
|
1d7d6d | 2000-02-16 | Per Hedbor | | }
add_constant( "roxen.argcache", argcache );
report_debug("Done [%.2fms]\n", (gethrtime()-t)/1000.0);
}
|
7339a0 | 2000-02-10 | Per Hedbor | | int main(int argc, array tmp)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
7339a0 | 2000-02-10 | Per Hedbor | | array argv = tmp;
tmp = 0;
|
a57779 | 1999-11-24 | Per Hedbor | | slowpipe = ((program)"slowpipe");
fastpipe = ((program)"fastpipe");
|
753a83 | 1999-08-30 | Per Hedbor | | call_out( lambda() {
|
4cf783 | 2000-02-16 | Per Hedbor | | (program)"module";
|
95b69e | 1999-09-05 | Per Hedbor | | dump( "protocols/http.pike");
dump( "protocols/ftp.pike");
|
753a83 | 1999-08-30 | Per Hedbor | | dump( "base_server/state.pike" );
|
2ff846 | 1999-09-02 | Per Hedbor | | dump( "base_server/highlight_pike.pike");
|
753a83 | 1999-08-30 | Per Hedbor | | dump( "base_server/wizard.pike" );
dump( "base_server/proxyauth.pike" );
dump( "base_server/html.pike" );
dump( "base_server/module.pike" );
|
95b69e | 1999-09-05 | Per Hedbor | | dump( "base_server/throttler.pike" );
dump( "base_server/smartpipe.pike" );
dump( "base_server/slowpipe.pike" );
dump( "base_server/fastpipe.pike" );
|
48884e | 2000-07-31 | Martin Nilsson | | dump( "languages/abstract.pike" );
|
753a83 | 1999-08-30 | Per Hedbor | | }, 9);
|
b1fca0 | 1996-11-12 | Per Hedbor | | mark_fd(0, "Stdin");
mark_fd(1, "Stdout");
mark_fd(2, "Stderr");
|
51643e | 1997-08-21 | Per Hedbor | | configuration_dir =
|
6ca8f6 | 1998-10-13 | Per Hedbor | | Getopt.find_option(argv, "d",({"config-dir","configuration-directory" }),
|
51643e | 1997-08-21 | Per Hedbor | | ({ "ROXEN_CONFIGDIR", "CONFIGURATIONS" }), "../configurations");
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
a92b95 | 1997-08-05 | Martin Stjernholm | | if(configuration_dir[-1] != '/')
|
b1fca0 | 1996-11-12 | Per Hedbor | | configuration_dir += "/";
|
08eba6 | 2000-07-09 | Per Hedbor | | restore_global_variables();
|
14179b | 1997-01-29 | Per Hedbor | |
|
48884e | 2000-07-31 | Martin Nilsson | | mixed tmp;
|
6ca8f6 | 1998-10-13 | Per Hedbor | | if(tmp = Getopt.find_option(argv, "r", "root")) fix_root(tmp);
|
b1fca0 | 1996-11-12 | Per Hedbor | |
argv -= ({ 0 });
|
51643e | 1997-08-21 | Per Hedbor | | argc = sizeof(argv);
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
c6fd2e | 2000-09-03 | Per Hedbor | | add_constant( "roxen.fonts",
(fonts = ((program)"base_server/fonts.pike")()) );
dump( "base_server/fonts.pike" );
|
48884e | 2000-07-31 | Martin Nilsson | | initiate_languages(query("locale"));
set_locale();
|
67f60e | 2000-07-09 | Martin Nilsson | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | #if efun(syslog)
init_logger();
#endif
init_garber();
initiate_supports();
|
1d7d6d | 2000-02-16 | Per Hedbor | | initiate_argcache();
|
970e24 | 2000-09-12 | Per Hedbor | | init_configuserdb();
|
1d7d6d | 2000-02-16 | Per Hedbor | |
|
b84a16 | 1999-06-07 | Martin Stjernholm | | enable_configurations();
|
95b69e | 1999-09-05 | Per Hedbor | | set_u_and_gid();
|
b84a16 | 1999-06-07 | Martin Stjernholm | |
create_pid_file(Getopt.find_option(argv, "p", "pid-file", "ROXEN_PID_FILE")
|| QUERY(pidfile));
|
1766be | 2000-02-16 | Per Hedbor | | if( Getopt.find_option( argv, 0, "no-delayed-load" ) )
enable_configurations_modules();
|
11f192 | 2000-02-16 | Per Hedbor | | else
|
d09399 | 2000-09-25 | Per Hedbor | | foreach( configurations, Configuration c )
|
11f192 | 2000-02-16 | Per Hedbor | | if( c->query( "no_delayed_load" ) )
c->enable_all_modules();
|
81f8af | 1999-12-20 | Martin Nilsson | |
|
48fa36 | 1997-04-05 | Per Hedbor | | #ifdef THREADS
start_handler_threads();
|
4f4bc1 | 1998-02-04 | Per Hedbor | | catch( this_thread()->set_name("Backend") );
|
34d3fa | 1999-04-22 | Per Hedbor | | backend_thread = this_thread();
|
34fbbc | 1998-02-05 | Henrik Grubbström (Grubba) | | #endif /* THREADS */
|
990cbb | 1997-08-12 | David Hedbor | |
|
3835ca | 1998-01-16 | Henrik Grubbström (Grubba) | |
|
15635b | 1999-10-10 | Per Hedbor | | foreach( ({ "SIGINT", "SIGTERM" }), string sig)
catch( signal(signum(sig), exit_when_done) );
catch( signal(signum("SIGHUP"), reload_all_configurations) );
|
1c9d08 | 1999-07-19 | Henrik Grubbström (Grubba) | |
|
15635b | 1999-10-10 | Per Hedbor | | foreach( ({ "SIGUSR1", "SIGUSR2", "SIGTRAP" }), string sig)
catch( signal(signum(sig), describe_all_threads) );
|
0f28da | 1997-08-13 | Per Hedbor | |
|
4f4bc1 | 1998-02-04 | Per Hedbor | | #ifdef __RUN_TRACE
trace(1);
#endif
|
08152b | 1998-04-24 | Per Hedbor | | start_time=time();
|
4cf783 | 2000-02-16 | Per Hedbor | |
|
dbfe9d | 2000-04-13 | Per Hedbor | |
|
4cf783 | 2000-02-16 | Per Hedbor | | if (QUERY(suicide_engage))
|
23264c | 2000-10-17 | Per Hedbor | | call_out (restart,60*60*24*max(1,QUERY(suicide_timeout)));
|
dbfe9d | 2000-04-13 | Per Hedbor | | #ifndef __NT__
|
4cf783 | 2000-02-16 | Per Hedbor | | restart_if_stuck( 0 );
|
dbfe9d | 2000-04-13 | Per Hedbor | | #endif
|
b1fca0 | 1996-11-12 | Per Hedbor | | return -1;
}
|
7a61de | 1998-03-26 | Per Hedbor | |
|
a6ef1f | 2000-03-28 | Johan Sundström | |
|
ee8b20 | 1998-07-13 | David Hedbor | | string check_variable(string name, mixed value)
|
edc9af | 1998-07-11 | David Hedbor | | {
switch(name)
{
|
dbfe9d | 2000-04-13 | Per Hedbor | | #ifndef __NT__
|
ee8b20 | 1998-07-13 | David Hedbor | | case "abs_engage":
if (value)
restart_if_stuck(1);
|
81f8af | 1999-12-20 | Martin Nilsson | | else
|
ee8b20 | 1998-07-13 | David Hedbor | | remove_call_out(restart_if_stuck);
break;
|
dbfe9d | 2000-04-13 | Per Hedbor | | #endif
|
ee8b20 | 1998-07-13 | David Hedbor | |
case "suicide_engage":
|
81f8af | 1999-12-20 | Martin Nilsson | | if (value)
|
23264c | 2000-10-17 | Per Hedbor | | call_out(restart,60*60*24*max(1,QUERY(suicide_timeout)));
|
ee8b20 | 1998-07-13 | David Hedbor | | else
remove_call_out(restart);
break;
|
edc9af | 1998-07-11 | David Hedbor | | }
}
|
9fd61c | 1999-02-16 | Per Hedbor | |
int is_ip(string s)
{
|
1f58d0 | 2000-01-02 | Martin Nilsson | | return (sscanf(s,"%*d.%*d.%*d.%*d")==4 && s[-1]>47 && s[-1]<58);
|
9fd61c | 1999-02-16 | Per Hedbor | | }
|
8b64e8 | 2000-01-03 | Martin Nilsson | |
|
a6e4a1 | 2000-07-09 | Per Hedbor | | static string _sprintf( )
{
return "roxen";
}
|
f3ca76 | 2000-08-19 | Per Hedbor | |
class LogFormat
{
static string host_ip_to_int(string s)
{
int a, b, c, d;
sscanf(s, "%d.%d.%d.%d", a, b, c, d);
return sprintf("%c%c%c%c",a, b, c, d);
}
static string extract_user(string from)
{
array tmp;
if (!from || sizeof(tmp = from/":")<2)
return "-";
return tmp[0];
}
void log( function do_write, RequestID id, mapping file );
static void do_async_write( string host, string data, string ip, function c )
{
|
3e0274 | 2000-08-23 | Per Hedbor | | if( c )
c( replace( data, "\4711", (host||ip) ) );
|
f3ca76 | 2000-08-19 | Per Hedbor | | }
}
|
f68365 | 2000-08-19 | Per Hedbor | | static mapping(string:function) compiled_formats = ([ ]);
|
f3ca76 | 2000-08-19 | Per Hedbor | |
constant formats =
({
|
ad092a | 2000-08-21 | Per Hedbor | | ({ "ip_number", "%s", "(string)request_id->remoteaddr",0 }),
|
f3ca76 | 2000-08-19 | Per Hedbor | | ({ "bin-ip_number","%s", "host_ip_to_int(request_id->remoteaddr)",0 }),
({ "cern_date", "%s", "Roxen.cern_http_date( time( 1 ) )",0 }),
({ "bin-date", "%4c", "time(1)",0 }),
|
ad092a | 2000-08-21 | Per Hedbor | | ({ "method", "%s", "(string)request_id->method",0 }),
({ "resource", "%s", "(string)(request_id->raw_url||request_id->not_query)", 0 }),
({ "full_resource","%s", "(string)(request_id->raw_url||request_id->not_query)",0 }),
({ "protocol", "%s", "(string)request_id->prot",0 }),
({ "response", "%d", "(int)(file->error || 200)",0 }),
({ "bin-response","%2c", "(int)(file->error || 200)",0 }),
({ "length", "%d", "(int)file->len",0 }),
({ "bin-length", "%4c", "(int)file->len",0 }),
|
f3ca76 | 2000-08-19 | Per Hedbor | | ({ "referer", "%s",
"sizeof(request_id->referer||({}))?request_id->referer[0]:\"\"", 0 }),
({ "user_agent", "%s",
"request_id->client?request_id->client*\" \":\"-\"", 0 }),
({ "user", "%s", "extract_user( request_id->realauth )",0 }),
({ "user_id", "%s", "request_id->cookies->RoxenUserID||\"0\"",0 }),
|
77c7e9 | 2000-11-02 | Peter J. Holzer | | ({ "request-time","%1.2f", "time(request_id->time )",0 }),
|
f3ca76 | 2000-08-19 | Per Hedbor | | ({ "host", "\4711", 0, 1 }),
});
|
f68365 | 2000-08-19 | Per Hedbor | | void run_log_format( string fmt, function c, RequestID id, mapping file )
{
(compiled_formats[ fmt ] || compile_log_format( fmt ))(c,id,file);
}
function compile_log_format( string fmt )
|
f3ca76 | 2000-08-19 | Per Hedbor | | {
|
e46184 | 2000-08-19 | Per Hedbor | | if( compiled_formats[ fmt ] )
return compiled_formats[ fmt ];
|
f3ca76 | 2000-08-19 | Per Hedbor | | array parts = fmt/"$";
string format = parts[0];
array args = ({});
int do_it_async = 0;
int add_nl = 1;
string sr( string s ) { return s[1..strlen(s)-2]; };
#define DO_ES(X) replace(X, ({"\\n", "\\r", "\\t", }), ({ "\n", "\r", "\t" }) )
foreach( parts[1..], string part )
{
int c, processed;
foreach( formats, array q )
if( part[..strlen(q[0])-1] == q[0])
{
format += q[1] + DO_ES(part[ strlen(q[0]) .. ]);
if( q[2] ) args += ({ q[2] });
if( q[3] ) do_it_async = 1;
processed=1;
break;
}
if( processed )
continue;
if( sscanf( part, "char(%d)%s", c, part ) )
format += sprintf( "%"+(c<0?"-":"")+"c", abs( c ) )+DO_ES(part);
else if( sscanf( part, "wchar(%d)%s", c, part ) )
format += sprintf( "%"+(c<0?"-":"")+"2c", abs( c ) )+DO_ES(part);
else if( sscanf( part, "int(%d)%s", c, part ) )
format += sprintf( "%"+(c<0?"-":"")+"4c", abs( c ) )+DO_ES(part);
else if( part[0] == '^' )
{
format += DO_ES(part[1..]);
add_nl = 0;
} else
format += "$"+part;
}
if( add_nl ) format += "\n";
add_constant( "___LogFormat", LogFormat );
string code = sprintf(
#"
inherit ___LogFormat;
void log( function callback, object request_id, mapping file )
{
|
3e0274 | 2000-08-23 | Per Hedbor | | if(!callback) return;
|
f3ca76 | 2000-08-19 | Per Hedbor | | string data = sprintf( %O %{, %s%} );
", format, args );
if( do_it_async )
{
code +=
#"
roxen.ip_to_host(request_id->remoteaddr,do_async_write,
data, request_id->remoteaddr, callback );
}
";
} else
code +=
#"
callback( data );
}
";
|
f68365 | 2000-08-19 | Per Hedbor | | return compiled_formats[ fmt ] = compile_string( code )()->log;
|
f3ca76 | 2000-08-19 | Per Hedbor | | }
static string cached_hostname = gethostname();
|
87dee2 | 2000-09-24 | Henrik Grubbström (Grubba) | | class LogFile(string fname)
|
f3ca76 | 2000-08-19 | Per Hedbor | | {
Stdio.File fd;
int opened;
|
87dee2 | 2000-09-24 | Henrik Grubbström (Grubba) | |
|
f3ca76 | 2000-08-19 | Per Hedbor | | void do_open()
{
mixed parent;
if (catch { parent = function_object(object_program(this_object())); } ||
!parent) {
remove_call_out(do_open);
remove_call_out(do_close);
destruct();
return;
}
string ff = fname;
mapping m = localtime(time(1));
m->year += 1900;
m->mon++;
if(m->mon < 10) m->mon = "0"+m->mon;
if(m->mday < 10) m->mday = "0"+m->mday;
if(m->hour < 10) m->hour = "0"+m->hour;
ff = replace(fname,({"%d","%m","%y","%h", "%H" }),
({ (string)m->mday, (string)(m->mon),
(string)(m->year),(string)m->hour,
cached_hostname,
}));
mkdirhier( ff );
fd = open( ff, "wac" );
if(!fd)
{
remove_call_out( do_open );
call_out( do_open, 120 );
report_error(LOC_M(37, "Failed to open logfile")+" "+fname+" "
#if constant(strerror)
"(" + strerror(errno()) + ")"
#endif
"\n");
return;
}
opened = 1;
remove_call_out( do_open );
|
e46184 | 2000-08-19 | Per Hedbor | | call_out( do_open, 900 );
|
f3ca76 | 2000-08-19 | Per Hedbor | | }
void do_close()
{
destruct( fd );
opened = 0;
}
|
e46184 | 2000-08-19 | Per Hedbor | | array(string) write_buf = ({});
static void do_the_write( )
|
f3ca76 | 2000-08-19 | Per Hedbor | | {
if( !opened ) do_open();
if( !opened ) return 0;
|
e46184 | 2000-08-19 | Per Hedbor | | fd->write( write_buf );
write_buf = ({});
|
f3ca76 | 2000-08-19 | Per Hedbor | | remove_call_out( do_close );
call_out( do_close, 10.0 );
|
e46184 | 2000-08-19 | Per Hedbor | | }
int write( string what )
{
if( !sizeof( write_buf ) )
call_out( do_the_write, 1 );
write_buf += ({what});
return strlen(what);
|
f3ca76 | 2000-08-19 | Per Hedbor | | }
}
|