0247a3 | 1998-03-11 | David Hedbor | |
|
2a2a5b | 1996-12-01 | Per Hedbor | |
|
f78848 | 1998-03-18 | Henrik Grubbström (Grubba) | | constant cvs_version = "$Id: http.pike,v 1.63 1998/03/18 16:56:34 grubba Exp $";
|
2a2a5b | 1996-12-01 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | #include <config.h>
|
fc2fcf | 1997-04-13 | Per Hedbor | | private inherit "roxenlib";
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
8afc81 | 1998-02-04 | Per Hedbor | | #if efun(gethrtime)
# define HRTIME() gethrtime()
# define HRSEC(X) ((int)((X)*1000000))
# define SECHR(X) ((X)/(float)1000000)
#else
|
c79b26 | 1998-02-05 | Johan Schön | | # define HRTIME() (predef::time())
|
8afc81 | 1998-02-04 | Per Hedbor | | # define HRSEC(X) (X)
# define SECHR(X) ((float)(X))
#endif
|
ebb1c5 | 1998-02-24 | Per Hedbor | | #ifdef PROFILE
|
8afc81 | 1998-02-04 | Per Hedbor | | int req_time = HRTIME();
|
ebb1c5 | 1998-02-24 | Per Hedbor | | #endif
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
14179b | 1997-01-29 | Per Hedbor | | constant decode=roxen->decode;
constant find_supports=roxen->find_supports;
constant version=roxen->version;
constant handle=roxen->handle;
constant _query=roxen->query;
|
e5bad2 | 1998-02-10 | Per Hedbor | | constant thepipe = roxen->pipe;
|
ebb1c5 | 1998-02-24 | Per Hedbor | | constant _time=predef::time;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
7ee565 | 1996-12-10 | Per Hedbor | | private static array(string) cache;
private static int wanted_data, have_data;
|
b1fca0 | 1996-11-12 | Per Hedbor | | object conf;
#include <roxen.h>
#include <module.h>
#undef QUERY
|
294490 | 1998-01-20 | Henrik Grubbström (Grubba) | | #if constant(cpp)
#define QUERY(X) _query( #X )
#else /* !constant(cpp) */
#define QUERY(X) _query("X")
#endif /* constant(cpp) */
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
ec6315 | 1998-03-16 | Francesco Chemolli | | int time = predef::time(1);
|
b1fca0 | 1996-11-12 | Per Hedbor | | string raw_url;
|
d7b087 | 1997-08-31 | Per Hedbor | | int do_not_disconnect;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
mapping (string:string) variables = ([ ]);
mapping (string:mixed) misc = ([ ]);
|
ebb1c5 | 1998-02-24 | Per Hedbor | | mapping (string:string) cookies = ([ ]);
|
b1fca0 | 1996-11-12 | Per Hedbor | |
multiset (string) prestate = (< >);
multiset (string) config = (< >);
|
ebb1c5 | 1998-02-24 | Per Hedbor | | multiset (string) supports;
multiset (string) pragma = (< >);
|
b1fca0 | 1996-11-12 | Per Hedbor | |
string remoteaddr, host;
|
ebb1c5 | 1998-02-24 | Per Hedbor | | array (string) client;
array (string) referer;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
beaca0 | 1998-02-20 | Per Hedbor | | mapping file;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
object my_fd;
object pipe;
string prot;
|
1afe37 | 1997-06-12 | Henrik Grubbström (Grubba) | | string clientprot;
|
b1fca0 | 1996-11-12 | Per Hedbor | | string method;
string realfile, virtfile;
string rest_query="";
string raw;
string query;
string not_query;
string extra_extension = "";
|
ebb1c5 | 1998-02-24 | Per Hedbor | | string data, leftovers;
|
b1fca0 | 1996-11-12 | Per Hedbor | | array (int|string) auth;
string rawauth, realauth;
string since;
|
ebb1c5 | 1998-02-24 | Per Hedbor | | void end(string|void a,int|void b);
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
e5bad2 | 1998-02-10 | Per Hedbor | | private void setup_pipe()
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
e5bad2 | 1998-02-10 | Per Hedbor | | if(!my_fd)
{
|
184261 | 1997-05-30 | Henrik Grubbström (Grubba) | | end();
return;
}
|
e5bad2 | 1998-02-10 | Per Hedbor | | if(!pipe) pipe=thepipe();
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
e5bad2 | 1998-02-10 | Per Hedbor | | void send(string|object what, int|void len)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
if(!what) return;
|
e5bad2 | 1998-02-10 | Per Hedbor | | if(!pipe) setup_pipe();
if(!pipe) return;
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(stringp(what)) pipe->write(what);
|
e5bad2 | 1998-02-10 | Per Hedbor | | else pipe->input(what,len);
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
string scan_for_query( string f )
{
if(sscanf(f,"%s?%s", f, query) == 2)
{
string v, a, b;
foreach(query / "&", v)
if(sscanf(v, "%s=%s", a, b) == 2)
{
a = http_decode_string(replace(a, "+", " "));
b = http_decode_string(replace(b, "+", " "));
if(variables[ a ])
variables[ a ] += "\0" + b;
else
variables[ a ] = b;
} else
if(strlen( rest_query ))
rest_query += "&" + http_decode_string( v );
else
rest_query = http_decode_string( v );
|
ebb1c5 | 1998-02-24 | Per Hedbor | | rest_query=replace(rest_query, "+", "\000");
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
return f;
}
private int really_set_config(array mod_config)
{
string url, m;
string base;
|
14179b | 1997-01-29 | Per Hedbor | | base = conf->query("MyWorldLocation")||"/";
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(supports->cookies)
{
#ifdef REQUEST_DEBUG
perror("Setting cookie..\n");
#endif
if(mod_config)
foreach(mod_config, m)
if(m[-1]=='-')
config[m[1..]]=0;
else
config[m]=1;
if(sscanf(replace(raw_url,({"%3c","%3e","%3C","%3E" }),
({"<",">","<",">"})),"/<%*s>/%s",url)!=2)
url = "/";
|
537ec8 | 1997-11-12 | David Hedbor | | if ((base[-1] == '/') && (strlen(url) && url[0] == '/')) {
|
0ddff6 | 1997-07-20 | Henrik Grubbström (Grubba) | | url = base + url[1..];
} else {
url = base + url;
}
my_fd->write(prot + " 302 Config in cookie!\r\n"
|
b1fca0 | 1996-11-12 | Per Hedbor | | "Set-Cookie: "
|
0ddff6 | 1997-07-20 | Henrik Grubbström (Grubba) | | + http_roxen_config_cookie(indices(config) * ",") + "\r\n"
"Location: " + url + "\r\n"
|
b1fca0 | 1996-11-12 | Per Hedbor | | "Content-Type: text/html\r\n"
"Content-Length: 0\r\n\r\n");
} else {
#ifdef REQUEST_DEBUG
perror("Setting {config} for user without Cookie support..\n");
#endif
if(mod_config)
foreach(mod_config, m)
if(m[-1]=='-')
prestate[m[1..]]=0;
else
prestate[m]=1;
|
0ddff6 | 1997-07-20 | Henrik Grubbström (Grubba) | | if (sscanf(replace(raw_url, ({ "%3c", "%3e", "%3C", "%3E" }),
({ "<", ">", "<", ">" })), "/<%*s>/%s", url) == 2) {
url = "/" + url;
}
if (sscanf(replace(url, ({ "%28", "%29" }), ({ "(", ")" })),
"/(%*s)/%s", url) == 2) {
url = "/" + url;
}
url = add_pre_state(url, prestate);
if (base[-1] == '/') {
url = base + url[1..];
} else {
url = base + url;
}
my_fd->write(prot + " 302 Config In Prestate!\r\n"
"\r\nLocation: " + url + "\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 0\r\n\r\n");
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
return -2;
}
|
0a635b | 1998-03-08 | Henrik Grubbström (Grubba) | | private static mixed f, line;
|
b1fca0 | 1996-11-12 | Per Hedbor | | private int parse_got(string s)
{
multiset (string) sup;
array mod_config;
|
ebb1c5 | 1998-02-24 | Per Hedbor | | string a, b, linename, contents;
|
b1fca0 | 1996-11-12 | Per Hedbor | | int config_in_url;
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
ceb927 | 1997-05-15 | David Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | raw = s;
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
0a635b | 1998-03-08 | Henrik Grubbström (Grubba) | | if (!line) {
int start = search(s, "\r\n");
if ((< -1, 0 >)[start]) {
return ([ -1:0, 0:2 ])[start];
}
line = s[..start-1];
start = search(line, " ");
if (start != -1) {
method = upper_case(line[..start-1]);
int end = search(reverse(line[start+1..]), " ");
if (end != -1) {
f = line[start+1..sizeof(line)-(end+2)];
clientprot = line[sizeof(line)-end..];
if (clientprot != "HTTP/0.9") {
|
1afe37 | 1997-06-12 | Henrik Grubbström (Grubba) | | prot = "HTTP/1.0";
|
0a635b | 1998-03-08 | Henrik Grubbström (Grubba) | |
int end;
if ((end = search(s, "\r\n\r\n")) == -1) {
return 0;
}
data = s[end+4..];
s = s[sizeof(line)+2..end-1];
} else {
prot = clientprot;
data = s[sizeof(line)+2..];
s = "";
}
} else {
f = line[start+1..];
prot = clientprot = "HTTP/0.9";
data = s[sizeof(line)+2..];
s = "";
|
1afe37 | 1997-06-12 | Henrik Grubbström (Grubba) | | }
|
0a635b | 1998-03-08 | Henrik Grubbström (Grubba) | | } else {
method = upper_case(line);
f = "/";
prot = clientprot = "HTTP/0.9";
}
} else {
int end;
if ((end = search(s, "\r\n\r\n")) == -1) {
return 0;
|
1afe37 | 1997-06-12 | Henrik Grubbström (Grubba) | | }
|
0a635b | 1998-03-08 | Henrik Grubbström (Grubba) | | data = s[end+4..];
s = s[sizeof(line)+2..end-1];
|
1afe37 | 1997-06-12 | Henrik Grubbström (Grubba) | | }
|
0a635b | 1998-03-08 | Henrik Grubbström (Grubba) | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(method == "PING")
{
my_fd->write("PONG\n");
return -2;
}
raw_url = f;
time = _time(1);
|
b04f07 | 1997-01-29 | Per Hedbor | |
|
7ec660 | 1997-01-29 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(!remoteaddr)
{
|
2154ca | 1997-08-31 | Per Hedbor | | if(my_fd) catch(remoteaddr = ((my_fd->query_address()||"")/" ")[0]);
|
e5bad2 | 1998-02-10 | Per Hedbor | | if(!remoteaddr) {
end();
return 0;
}
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
7ec660 | 1997-01-29 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | f = scan_for_query( f );
|
06583f | 1997-09-03 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
e5bad2 | 1998-02-10 | Per Hedbor | | if (sscanf(f, "/<%s>/%s", a, f)==2)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
config_in_url = 1;
mod_config = (a/",");
|
e5bad2 | 1998-02-10 | Per Hedbor | | f = "/"+f;
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
e5bad2 | 1998-02-10 | Per Hedbor | | if ((sscanf(f, "/(%s)/%s", a, f)==2) && strlen(a))
{
|
b1fca0 | 1996-11-12 | Per Hedbor | | prestate = aggregate_multiset(@(a/","-({""})));
|
e5bad2 | 1998-02-10 | Per Hedbor | | f = "/"+f;
}
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
86729e | 1997-09-06 | Per Hedbor | | not_query = simplify_path(http_decode_string(f));
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
0a635b | 1998-03-08 | Henrik Grubbström (Grubba) | | if(sizeof(s)) {
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
ebb1c5 | 1998-02-24 | Per Hedbor | | foreach(s/"\r\n" - ({ "" }), line)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
linename=contents=0;
sscanf(line, "%s:%s", linename, contents);
if(linename&&contents)
{
linename=lower_case(linename);
sscanf(contents, "%*[\t ]%s", contents);
if(strlen(contents))
{
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | switch (linename) {
|
ebb1c5 | 1998-02-24 | Per Hedbor | | case "content-length":
|
b1fca0 | 1996-11-12 | Per Hedbor | | misc->len = (int)(contents-" ");
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(!misc->len) continue;
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(method == "POST")
{
|
dcc435 | 1997-05-20 | Per Hedbor | | if(!data) data="";
|
ebb1c5 | 1998-02-24 | Per Hedbor | | int l = misc->len-1;
|
7ee565 | 1996-12-10 | Per Hedbor | | wanted_data=l;
have_data=strlen(data);
|
ceb927 | 1997-05-15 | David Hedbor | |
if(strlen(data) <= l)
{
|
7ee565 | 1996-12-10 | Per Hedbor | | return 0;
|
ceb927 | 1997-05-15 | David Hedbor | | }
|
ebb1c5 | 1998-02-24 | Per Hedbor | | leftovers = data[l+1..];
|
7ee565 | 1996-12-10 | Per Hedbor | | data = data[..l];
switch(lower_case(((misc["content-type"]||"")/";")[0]-" "))
|
86c510 | 1997-10-05 | Henrik Grubbström (Grubba) | | {
default:
|
7ee565 | 1996-12-10 | Per Hedbor | | string v;
if(l < 200000)
{
foreach(replace(data-"\n", "+", " ")/"&", v)
if(sscanf(v, "%s=%s", a, b) == 2)
{
a = http_decode_string( a );
b = http_decode_string( b );
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
7ee565 | 1996-12-10 | Per Hedbor | | if(variables[ a ])
variables[ a ] += "\0" + b;
else
variables[ a ] = b;
}
}
break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "multipart/form-data":
|
7ee565 | 1996-12-10 | Per Hedbor | |
|
57d41d | 1997-08-14 | Marcus Comstedt | | object messg = MIME.Message(data, misc);
foreach(messg->body_parts, object part) {
if(part->disp_params->filename) {
variables[part->disp_params->name]=part->getdata();
variables[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 {
variables[part->disp_params->name]=part->getdata();
|
7ee565 | 1996-12-10 | Per Hedbor | | }
}
break;
}
}
break;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "authorization":
|
b1fca0 | 1996-11-12 | Per Hedbor | | string *y;
rawauth = contents;
y = contents /= " ";
if(sizeof(y) < 2)
break;
y[1] = decode(y[1]);
realauth=y[1];
if(conf && conf->auth_module)
y = conf->auth_module->auth( y, this_object() );
auth=y;
break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "proxy-authorization":
|
b1fca0 | 1996-11-12 | Per Hedbor | | string *y;
y = contents /= " ";
if(sizeof(y) < 2)
break;
y[1] = decode(y[1]);
if(conf && conf->auth_module)
y = conf->auth_module->auth( y, this_object() );
misc->proxyauth=y;
break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "pragma":
|
5e8921 | 1997-02-13 | Per Hedbor | | pragma|=aggregate_multiset(@replace(contents, " ", "")/ ",");
|
b1fca0 | 1996-11-12 | Per Hedbor | | break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "user-agent":
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(!client)
{
sscanf(contents, "%s via", contents);
client = contents/" " - ({ "" });
}
|
b1fca0 | 1996-11-12 | Per Hedbor | | break;
|
695bba | 1997-03-22 | Henrik Grubbström (Grubba) | |
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "ua-pixels":
case "ua-color":
case "ua-os":
case "ua-cpu":
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | |
|
695bba | 1997-03-22 | Henrik Grubbström (Grubba) | | break;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "referer":
|
f6d62d | 1997-03-26 | Per Hedbor | | referer = contents/" ";
break;
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
ebb1c5 | 1998-02-24 | Per Hedbor | | case "extension":
|
b1fca0 | 1996-11-12 | Per Hedbor | | #ifdef DEBUG
perror("Client extension: "+contents+"\n");
#endif
|
ebb1c5 | 1998-02-24 | Per Hedbor | | case "connection":
|
b1fca0 | 1996-11-12 | Per Hedbor | | contents = lower_case(contents);
|
ebb1c5 | 1998-02-24 | Per Hedbor | | case "content-type":
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | misc[linename] = lower_case(contents);
|
b1fca0 | 1996-11-12 | Per Hedbor | | break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "accept":
case "accept-encoding":
case "accept-charset":
case "accept-language":
case "session-id":
case "message-id":
case "from":
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(misc[linename])
|
5e8921 | 1997-02-13 | Per Hedbor | | misc[linename] += (contents-" ") / ",";
|
b1fca0 | 1996-11-12 | Per Hedbor | | else
|
5e8921 | 1997-02-13 | Per Hedbor | | misc[linename] = (contents-" ") / ",";
|
b1fca0 | 1996-11-12 | Per Hedbor | | break;
case "cookie":
string c;
misc->cookies = contents;
foreach(contents/";", c)
{
string name, value;
while(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))
{
array tmpconfig = value/"," + ({ });
string m;
if(mod_config && sizeof(mod_config))
foreach(mod_config, m)
if(!strlen(m))
{ continue; }
else if(m[0]=='-')
tmpconfig -= ({ m[1..] });
else
tmpconfig |= ({ m });
mod_config = 0;
config = aggregate_multiset(@tmpconfig);
}
}
}
break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "host":
case "proxy-connection":
case "security-scheme":
|
b1fca0 | 1996-11-12 | Per Hedbor | | misc[linename] = contents;
break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "proxy-by":
case "proxy-maintainer":
case "proxy-software":
case "mime-version":
|
b1fca0 | 1996-11-12 | Per Hedbor | | break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "if-modified-since":
|
ebb1c5 | 1998-02-24 | Per Hedbor | | since=contents;
|
b1fca0 | 1996-11-12 | Per Hedbor | | break;
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | |
case "via":
case "cache-control":
|
ebb1c5 | 1998-02-24 | Per Hedbor | | case "negotiate":
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | case "forwarded":
|
ebb1c5 | 1998-02-24 | Per Hedbor | | misc[linename]=contents;
|
b1fca0 | 1996-11-12 | Per Hedbor | | break;
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | #ifdef DEBUG
|
48633b | 1997-08-04 | Henrik Grubbström (Grubba) | | default:
|
b1fca0 | 1996-11-12 | Per Hedbor | |
if(linename[0] != 'x')
perror("Unknown header: `"+linename+"' -> `"+contents+"'\n");
#endif
}
}
}
}
}
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(!client) client = ({ "unknown" });
if(!supports)
supports = find_supports(lower_case(client*" "));
if(!referer) referer = ({ });
|
dde965 | 1996-12-08 | David Hedbor | | if(misc->proxyauth) {
mixed tmp1,tmp2;
foreach(tmp2 = (raw / "\n"), tmp1) {
if(!search(lower_case(tmp1), "proxy-authorization:"))
tmp2 -= ({tmp1});
}
raw = tmp2 * "\n";
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
ceb927 | 1997-05-15 | David Hedbor | | if(config_in_url) {
return really_set_config( mod_config );
}
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(!supports->cookies)
config = prestate;
else
if(conf
|
ebb1c5 | 1998-02-24 | Per Hedbor | | && QUERY(set_cookie)
|
27b0e1 | 1996-11-26 | Per Hedbor | | && !cookies->RoxenUserID && strlen(not_query)
|
ebb1c5 | 1998-02-24 | Per Hedbor | | && not_query[0]=='/' && method!="PUT")
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
a773c6 | 1997-07-06 | Henrik Grubbström (Grubba) | | if (!(QUERY(set_cookie_only_once) &&
cache_lookup("hosts_for_cookie",remoteaddr))) {
misc->moreheads = ([ "Set-Cookie":http_roxen_id_cookie(), ]);
}
if (QUERY(set_cookie_only_once))
cache_set("hosts_for_cookie",remoteaddr,1);
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
return 1;
}
void disconnect()
{
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(do_not_disconnect) return;
|
a37f78 | 1998-02-27 | Per Hedbor | | catch(file && file->close());
|
ebb1c5 | 1998-02-24 | Per Hedbor | | file = 0;
|
e8790b | 1998-02-19 | Per Hedbor | | my_fd = 0;
|
14179b | 1997-01-29 | Per Hedbor | | destruct();
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
ebb1c5 | 1998-02-24 | Per Hedbor | | void end(string|void s, int|void keepit)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
8afc81 | 1998-02-04 | Per Hedbor | | #ifdef PROFILE
if(conf)
{
float elapsed = SECHR(HRTIME()-req_time);
array p;
if(!(p=conf->profile_map[not_query]))
p = conf->profile_map[not_query] = ({0,0.0,0.0});
conf->profile_map[not_query][0]++;
p[1] += elapsed;
if(elapsed > p[2]) p[2]=elapsed;
}
|
ceb927 | 1997-05-15 | David Hedbor | | #endif
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
#ifdef KEEP_ALIVE
if(keepit &&
(!(file->raw || file->len<=0))
&& (misc->connection || (prot == "HTTP/1.1"))
&& my_fd)
{
object o = object_program(this_object())();
o->remoteaddr = remoteaddr;
o->supports = supports;
o->host = host;
o->client = client;
object fd = my_fd;
my_fd=0;
o->chain(fd,conf,leftovers);
disconnect();
return;
}
#endif
|
e8790b | 1998-02-19 | Per Hedbor | | if(objectp(my_fd))
{
|
ebb1c5 | 1998-02-24 | Per Hedbor | | catch {
my_fd->set_close_callback(0);
my_fd->set_read_callback(0);
|
a37f78 | 1998-02-27 | Per Hedbor | |
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(s) my_fd->write(s);
|
e8790b | 1998-02-19 | Per Hedbor | | my_fd->close();
destruct(my_fd);
|
ebb1c5 | 1998-02-24 | Per Hedbor | | };
|
e8790b | 1998-02-19 | Per Hedbor | | my_fd = 0;
}
disconnect();
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
ceb927 | 1997-05-15 | David Hedbor | | static void do_timeout(mapping foo)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
ebb1c5 | 1998-02-24 | Per Hedbor | | int elapsed = _time()-time;
|
ec6315 | 1998-03-16 | Francesco Chemolli | | if(elapsed >= 30)
|
8afc81 | 1998-02-04 | Per Hedbor | | {
end("HTTP/1.0 408 Timeout\r\n"
"Content-type: text/plain\r\n"
"Server: Roxen Challenger\r\n"
"\r\n"
"Your connection timed out.\n"
"Please try again.\n");
} else {
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
8afc81 | 1998-02-04 | Per Hedbor | | call_out(do_timeout, 10);
}
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
mapping internal_error(array err)
{
|
f78848 | 1998-03-18 | Henrik Grubbström (Grubba) | | if(QUERY(show_internals)) {
|
ec6315 | 1998-03-16 | Francesco Chemolli | | array(string) bt = (describe_backtrace(err)/"\n") - ({""});
file = http_low_answer(500,
sprintf("<h1>Error: Internal server error.</h1>"
"<pre><font size=+1>%s</font></pre>"
"<backtrace><ol><li>%s</ol>\n",
bt[..1]*"\n", bt[2..]*"<li>"));
|
f78848 | 1998-03-18 | Henrik Grubbström (Grubba) | | } else {
|
b1fca0 | 1996-11-12 | Per Hedbor | | file = http_low_answer(500, "<h1>Error: The server failed to "
"fulfill your query.</h1>");
|
f78848 | 1998-03-18 | Henrik Grubbström (Grubba) | | }
|
b1fca0 | 1996-11-12 | Per Hedbor | |
report_error("Internal server error: " +
describe_backtrace(err) + "\n");
}
|
23a789 | 1996-12-15 | Per Hedbor | | int wants_more()
{
return !!cache;
}
|
fc2fcf | 1997-04-13 | Per Hedbor | | constant errors =
([
200:"200 OK",
201:"201 URI follows",
202:"202 Accepted",
203:"203 Provisional Information",
204:"204 No Content",
300:"300 Moved",
301:"301 Permanent Relocation",
302:"302 Temporary Relocation",
303:"303 Temporary Relocation method and URI",
304:"304 Not Modified",
400:"400 Bad Request",
401:"401 Access denied",
402:"402 Payment Required",
403:"403 Forbidden",
404:"404 No such file or directory.",
405:"405 Method not allowed",
407:"407 Proxy authorization needed",
408:"408 Request timeout",
409:"409 Conflict",
410:"410 This document is no more. It has gone to meet it's creator. It is gone. It will not be coming back. Give up. I promise. There is no such file or directory.",
500:"500 Internal Server Error.",
501:"501 Not Implemented",
502:"502 Gateway Timeout",
503:"503 Service unavailable",
]);
|
d7b087 | 1997-08-31 | Per Hedbor | |
|
beaca0 | 1998-02-20 | Per Hedbor | | void do_log()
|
d7b087 | 1997-08-31 | Per Hedbor | | {
if(conf)
{
int len;
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(pipe) file->len = pipe->bytes_sent();
|
d7b087 | 1997-08-31 | Per Hedbor | | if(conf)
{
if(file->len > 0) conf->sent+=file->len;
|
f7d981 | 1997-09-12 | Per Hedbor | | conf->log(file, this_object());
|
d7b087 | 1997-08-31 | Per Hedbor | | }
}
|
ebb1c5 | 1998-02-24 | Per Hedbor | | end("",1);
|
e5bad2 | 1998-02-10 | Per Hedbor | | return;
|
d7b087 | 1997-08-31 | Per Hedbor | | }
|
9f46de | 1997-04-08 | Per Hedbor | | void handle_request( )
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
mixed *err;
|
5bc199 | 1997-01-29 | Per Hedbor | | int tmp;
|
b1fca0 | 1996-11-12 | Per Hedbor | | function funp;
mapping heads;
|
5bc199 | 1997-01-29 | Per Hedbor | | string head_string;
|
14179b | 1997-01-29 | Per Hedbor | | object thiso=this_object();
|
5bc199 | 1997-01-29 | Per Hedbor | |
|
ceb927 | 1997-05-15 | David Hedbor | | remove_call_out(do_timeout);
|
82f519 | 1997-03-02 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(conf)
{
|
14179b | 1997-01-29 | Per Hedbor | |
|
68e030 | 1997-08-19 | Per Hedbor | | object oc = conf;
|
ebb1c5 | 1998-02-24 | Per Hedbor | | foreach(conf->first_modules(), funp)
{
|
68e030 | 1997-08-19 | Per Hedbor | | if(file = funp( thiso)) break;
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(conf != oc) {
handle_request();
return;
}
|
68e030 | 1997-08-19 | Per Hedbor | | }
|
14179b | 1997-01-29 | Per Hedbor | | if(!file) err=catch(file = conf->get_file(thiso));
|
b1fca0 | 1996-11-12 | Per Hedbor | |
if(err) internal_error(err);
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(!mappingp(file))
|
14179b | 1997-01-29 | Per Hedbor | | foreach(conf->last_modules(), funp) if(file = funp(thiso)) break;
} else if(err=catch(file = roxen->configuration_parse( thiso ))) {
if(err==-1) return;
internal_error(err);
}
|
82f519 | 1997-03-02 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(!mappingp(file))
{
|
f6d62d | 1997-03-26 | Per Hedbor | | if(misc->error_code)
file = http_low_answer(misc->error_code, errors[misc->error]);
else if(method != "GET" && method != "HEAD" && method != "POST")
|
b1fca0 | 1996-11-12 | Per Hedbor | | file = http_low_answer(501, "Not implemented.");
else
|
4aaa82 | 1998-02-04 | Per Hedbor | | if(catch {
file=http_low_answer(404,
replace(parse_rxml(conf->query("ZNoSuchFile"),
thiso),
({"$File", "$Me"}),
({not_query,
conf->query("MyWorldLocation")})));})
internal_error(err);
|
14179b | 1997-01-29 | Per Hedbor | | } else {
if((file->file == -1) || file->leave_me)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
fc2fcf | 1997-04-13 | Per Hedbor | | if(do_not_disconnect) return;
|
d336db | 1998-03-11 | David Hedbor | | my_fd = 0; objectp(file) && file->close(); file = 0;
|
14179b | 1997-01-29 | Per Hedbor | | return;
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(file->type == "raw") file->raw = 1;
else if(!file->type) file->type="text/plain";
|
14179b | 1997-01-29 | Per Hedbor | | }
|
b1fca0 | 1996-11-12 | Per Hedbor | |
if(!file->raw && prot != "HTTP/0.9")
{
string h;
heads=
|
ebb1c5 | 1998-02-24 | Per Hedbor | | (["MIME-Version":(file["mime-version"] || "1.0"),
|
b1fca0 | 1996-11-12 | Per Hedbor | | "Content-type":file["type"],
|
ec6a43 | 1997-09-19 | Henrik Grubbström (Grubba) | | "Server":replace(version(), " ", "·"),
|
ebb1c5 | 1998-02-24 | Per Hedbor | | "Date":http_date(time) ]);
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(file->encoding)
heads["Content-Encoding"] = file->encoding;
if(!file->error)
file->error=200;
if(file->expires)
heads->Expires = http_date(file->expires);
if(!file->len)
{
if(objectp(file->file))
|
14179b | 1997-01-29 | Per Hedbor | | if(!file->stat && !(file->stat=misc->stat))
|
b1fca0 | 1996-11-12 | Per Hedbor | | file->stat = (int *)file->file->stat();
array fstat;
if(arrayp(fstat = file->stat))
{
if(file->file && !file->len)
file->len = fstat[1];
heads["Last-Modified"] = http_date(fstat[3]);
if(since)
{
if(is_modified(since, fstat[3], fstat[1]))
{
file->error = 304;
|
ebb1c5 | 1998-02-24 | Per Hedbor | | file->file = 0;
file->data="";
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
}
}
if(stringp(file->data))
file->len += strlen(file->data);
}
|
86c510 | 1997-10-05 | Henrik Grubbström (Grubba) | | if(mappingp(file->extra_heads)) {
|
b1fca0 | 1996-11-12 | Per Hedbor | | heads |= file->extra_heads;
|
86c510 | 1997-10-05 | Henrik Grubbström (Grubba) | | }
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
86c510 | 1997-10-05 | Henrik Grubbström (Grubba) | | if(mappingp(misc->moreheads)) {
|
b1fca0 | 1996-11-12 | Per Hedbor | | heads |= misc->moreheads;
|
86c510 | 1997-10-05 | Henrik Grubbström (Grubba) | | }
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
14179b | 1997-01-29 | Per Hedbor | | array myheads = ({prot+" "+(file->rettext||errors[file->error])});
|
b1fca0 | 1996-11-12 | Per Hedbor | | foreach(indices(heads), h)
if(arrayp(heads[h]))
foreach(heads[h], tmp)
|
5e8921 | 1997-02-13 | Per Hedbor | | myheads += ({ `+(h,": ", tmp)});
|
b1fca0 | 1996-11-12 | Per Hedbor | | else
|
5e8921 | 1997-02-13 | Per Hedbor | | myheads += ({ `+(h, ": ", heads[h])});
|
b1fca0 | 1996-11-12 | Per Hedbor | |
if(file->len > -1)
|
14179b | 1997-01-29 | Per Hedbor | | myheads += ({"Content-length: " + file->len });
|
a37f78 | 1998-02-27 | Per Hedbor | | #ifdef KEEP_ALIVE
myheads += ({ "Connection: Keep-Alive" });
#endif
|
14179b | 1997-01-29 | Per Hedbor | | head_string = (myheads+({"",""}))*"\r\n";
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
d7b087 | 1997-08-31 | Per Hedbor | | if(conf) conf->hsent+=strlen(head_string||"");
|
14179b | 1997-01-29 | Per Hedbor | | }
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
if(method == "HEAD")
{
file->file = 0;
file->data="";
}
if(!leftovers) leftovers = data||"";
if(file->len > 0 && file->len < 2000)
|
beaca0 | 1998-02-20 | Per Hedbor | | {
my_fd->write(head_string + (file->file?file->file->read():file->data));
do_log();
return;
}
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | if(head_string) send(head_string);
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
e5bad2 | 1998-02-10 | Per Hedbor | | if(method != "HEAD")
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
ebb1c5 | 1998-02-24 | Per Hedbor | | if(file->data && strlen(file->data))
send(file->data, file->len);
if(file->file)
send(file->file, file->len);
|
e5bad2 | 1998-02-10 | Per Hedbor | | }
|
0a635b | 1998-03-08 | Henrik Grubbström (Grubba) | | if (pipe) {
pipe->set_done_callback( do_log );
pipe->output(my_fd);
} else {
my_fd->close();
do_log();
}
|
14179b | 1997-01-29 | Per Hedbor | | }
|
ebb1c5 | 1998-02-24 | Per Hedbor | | int processed;
|
14179b | 1997-01-29 | Per Hedbor | | void got_data(mixed fooid, string s)
{
int tmp;
|
ceb927 | 1997-05-15 | David Hedbor | | remove_call_out(do_timeout);
|
2154ca | 1997-08-31 | Per Hedbor | | call_out(do_timeout, 30);
|
ceb927 | 1997-05-15 | David Hedbor | |
|
14179b | 1997-01-29 | Per Hedbor | | if(wanted_data)
{
if(strlen(s)+have_data < wanted_data)
{
cache += ({ s });
have_data += strlen(s);
return;
}
}
if(cache)
{
|
ebb1c5 | 1998-02-24 | Per Hedbor | | s = cache*""+s;
|
14179b | 1997-01-29 | Per Hedbor | | cache = 0;
}
|
ebb1c5 | 1998-02-24 | Per Hedbor | | sscanf(s, "%*[\n\r]%s", s);
if(strlen(s)) tmp = parse_got(s);
|
14179b | 1997-01-29 | Per Hedbor | | switch(-tmp)
{
case 0:
cache = ({ s });
return;
case 1:
end(prot+" 500 Stupid Client Error\r\nContent-Length: 0\r\n\r\n");
return;
case 2:
end();
return;
}
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
14179b | 1997-01-29 | Per Hedbor | | if(conf)
{
conf->received += strlen(s);
conf->requests++;
}
|
2154ca | 1997-08-31 | Per Hedbor | |
my_fd->set_close_callback(0);
my_fd->set_read_callback(0);
|
a37f78 | 1998-02-27 | Per Hedbor | |
|
ebb1c5 | 1998-02-24 | Per Hedbor | | processed=1;
|
14179b | 1997-01-29 | Per Hedbor | | #ifdef THREADS
|
e5bad2 | 1998-02-10 | Per Hedbor | | roxen->handle(this_object()->handle_request);
|
14179b | 1997-01-29 | Per Hedbor | | #else
|
e5bad2 | 1998-02-10 | Per Hedbor | | handle_request();
|
b1fca0 | 1996-11-12 | Per Hedbor | | #endif
}
object clone_me()
{
|
14179b | 1997-01-29 | Per Hedbor | | object c,t;
c=object_program(t=this_object())();
|
b1fca0 | 1996-11-12 | Per Hedbor | |
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | c->conf = conf;
c->time = time;
|
f6d62d | 1997-03-26 | Per Hedbor | | c->raw_url = raw_url;
c->variables = copy_value(variables);
|
48fa36 | 1997-04-05 | Per Hedbor | | c->misc = copy_value(misc);
c->misc->orig = t;
|
f6d62d | 1997-03-26 | Per Hedbor | |
|
b1fca0 | 1996-11-12 | Per Hedbor | | c->prestate = prestate;
c->supports = supports;
|
f6d62d | 1997-03-26 | Per Hedbor | | c->config = config;
|
b1fca0 | 1996-11-12 | Per Hedbor | | c->remoteaddr = remoteaddr;
|
f6d62d | 1997-03-26 | Per Hedbor | | c->host = host;
|
b1fca0 | 1996-11-12 | Per Hedbor | | c->client = client;
|
f6d62d | 1997-03-26 | Per Hedbor | | c->referer = referer;
c->pragma = pragma;
c->cookies = cookies;
c->my_fd = 0;
|
ebb1c5 | 1998-02-24 | Per Hedbor | |
|
f6d62d | 1997-03-26 | Per Hedbor | | c->prot = prot;
|
1afe37 | 1997-06-12 | Henrik Grubbström (Grubba) | | c->clientprot = clientprot;
|
f6d62d | 1997-03-26 | Per Hedbor | | c->method = method;
c->rest_query = rest_query;
c->raw = raw;
c->query = query;
c->not_query = not_query;
c->extra_extension = extra_extension;
c->data = data;
|
b1fca0 | 1996-11-12 | Per Hedbor | | c->auth = auth;
c->realauth = realauth;
|
f6d62d | 1997-03-26 | Per Hedbor | | c->rawauth = rawauth;
c->since = since;
|
b1fca0 | 1996-11-12 | Per Hedbor | | return c;
}
void clean()
{
if(!(my_fd && objectp(my_fd))) end();
else if((_time(1) - time) > 4800) end();
}
|
14179b | 1997-01-29 | Per Hedbor | | void create(object f, object c)
|
b1fca0 | 1996-11-12 | Per Hedbor | | {
|
14179b | 1997-01-29 | Per Hedbor | | if(f)
{
my_fd = f;
conf = c;
mark_fd(my_fd->query_fd(), "HTTP connection");
|
beaca0 | 1998-02-20 | Per Hedbor | | my_fd->set_close_callback(end);
my_fd->set_read_callback(got_data);
|
ceb927 | 1997-05-15 | David Hedbor | |
|
3158d2 | 1997-12-10 | David Hedbor | | call_out(do_timeout, 30);
|
14179b | 1997-01-29 | Per Hedbor | | }
|
b1fca0 | 1996-11-12 | Per Hedbor | | }
|
ceb927 | 1997-05-15 | David Hedbor | |
|
ebb1c5 | 1998-02-24 | Per Hedbor | | void chain(object f, object c, string le)
{
my_fd = f;
conf = c;
do_not_disconnect=-1;
if(strlen(le)) got_data(0,le);
if(!my_fd)
{
if(do_not_disconnect == -1)
{
do_not_disconnect=0;
disconnect();
}
} else if(!processed) {
f->set_close_callback(end);
f->set_read_callback(got_data);
}
}
|