Charles.Forsyth20060303a828770d19 years agoHistory
861 lines · 17.1 KB · Brainfuck
RawDownload
implement Parser; include "sys.m";	sys: Sys;include "draw.m";	draw: Draw;include "bufio.m";include "string.m";	str: String;include "daytime.m";	daytime: Daytime;include "contents.m";	contents : Contents;	Content: import contents;include "cache.m";include "httpd.m";	Entity, Private_info: import Httpd;	Internal, TempFail, Unimp, UnkVers, BadCont, BadReq, Syntax, 	BadSearch, NotFound, NoSearch , OnlySearch, Unauth, OK : import Httpd;	include "parser.m";include "date.m";	date : Date;include "alarms.m";	alarms: Alarms;	Alarm: import alarms;include "lock.m";	locks: Lock;	Semaphore: import locks; Error: adt {	num : string;	concise: string;	verbose: string;}; errormsg := array[] of {	Internal => Error("500 Internal Error", "Internal Error",		"This server could not process your request due to an interal error."),	TempFail =>	Error("500 Internal Error", "Temporary Failure",		"The object %s is currently inaccessible.<p>Please try again later."),	Unimp =>	Error("501 Not implemented", "Command not implemented",		"This server does not implement the %s command."),	UnkVers =>	Error("501 Not Implemented", "Unknown http version",		"This server does not know how to respond to http version %s."),	BadCont =>	Error("501 Not Implemented", "Impossible format",		"This server cannot produce %s in any of the formats your client accepts."),	BadReq =>	Error("400 Bad Request", "Strange Request",		"Your client sent a query that this server could not understand."),	Syntax =>	Error("400 Bad Request", "Garbled Syntax",		"Your client sent a query with incoherent syntax."),	BadSearch =>Error("400 Bad Request", "Inapplicable Search",		"Your client sent a search that cannot be applied to %s."),	NotFound =>Error("404 Not Found", "Object not found",		"The object %s does not exist on this server."),	NoSearch =>	Error("403 Forbidden", "Search not supported",		"The object %s does not support the search command."),	OnlySearch =>Error("403 Forbidden", "Searching Only",		"The object %s only supports the searching methods."),	Unauth =>	Error("401 Unauthorized", "Unauthorized",		"You are not authorized to see the object %s."),	OK =>	Error("200 OK", "everything is fine","Groovy man"),};	 latin1 := array[] of {	'¡',	'¢',	'£',	'¤',	'¥',	'¦',	'§',	'¨',	'©',	'ª',	'«',	'¬',	'­',	'®',	'¯',	'°',	'±',	'²',	'³',	'´',	'µ',	'¶',	'·',	'¸',	'¹',	'º',	'»',	'¼',	'½',	'¾',	'¿',	'À',	'Á',	'Â',	'Ã',	'Ä',	'Å',	'Æ',	'Ç',	'È',	'É',	'Ê',	'Ë',	'Ì',	'Í',	'Î',	'Ï',	'Ð',	'Ñ',	'Ò',	'Ó',	'Ô',	'Õ',	'Ö',	'×',	'Ø',	'Ù',	'Ú',	'Û',	'Ü',	'Ý',	'Þ',	'ß',	'à',	'á',	'â',	'ã',	'ä',	'å',	'æ',	'ç',	'è',	'é',	'ê',	'ë',	'ì',	'í',	'î',	'ï',	'ð',	'ñ',	'ò',	'ó',	'ô',	'õ',	'ö',	'÷',	'ø',	'ù',	'ú',	'û',	'ü',	'ý',	'þ',	'ÿ',	0,}; entities :=array[] of {		Entity( "&#161;",	'¡' ),		Entity( "&#162;",	'¢' ),		Entity( "&#163;",	'£' ),		Entity( "&#164;",	'¤' ),		Entity( "&#165;",	'¥' ),		Entity( "&#166;",	'¦' ),		Entity( "&#167;",	'§' ),		Entity( "&#168;",	'¨' ),		Entity( "&#169;",	'©' ),		Entity( "&#170;",	'ª' ),		Entity( "&#171;",	'«' ),		Entity( "&#172;",	'¬' ),		Entity( "&#173;",	'­' ),		Entity( "&#174;",	'®' ),		Entity( "&#175;",	'¯' ),		Entity( "&#176;",	'°' ),		Entity( "&#177;",	'±' ),		Entity( "&#178;",	'²' ),		Entity( "&#179;",	'³' ),		Entity( "&#180;",	'´' ),		Entity( "&#181;",	'µ' ),		Entity( "&#182;",	'¶' ),		Entity( "&#183;",	'·' ),		Entity( "&#184;",	'¸' ),		Entity( "&#185;",	'¹' ),		Entity( "&#186;",	'º' ),		Entity( "&#187;",	'»' ),		Entity( "&#188;",	'¼' ),		Entity( "&#189;",	'½' ),		Entity( "&#190;",	'¾' ),		Entity( "&#191;",	'¿' ),		Entity( "&Agrave;",	'À' ),		Entity( "&Aacute;",	'Á' ),		Entity( "&Acirc;",	'Â' ),		Entity( "&Atilde;",	'Ã' ),		Entity( "&Auml;",	'Ä' ),		Entity( "&Aring;",	'Å' ),		Entity( "&AElig;",	'Æ' ),		Entity( "&Ccedil;",	'Ç' ),		Entity( "&Egrave;",	'È' ),		Entity( "&Eacute;",	'É' ),		Entity( "&Ecirc;",	'Ê' ),		Entity( "&Euml;",	'Ë' ),		Entity( "&Igrave;",	'Ì' ),		Entity( "&Iacute;",	'Í' ),		Entity( "&Icirc;",	'Î' ),		Entity( "&Iuml;",	'Ï' ),		Entity( "&ETH;",	'Ð' ),		Entity( "&Ntilde;",	'Ñ' ),		Entity( "&Ograve;",	'Ò' ),		Entity( "&Oacute;",	'Ó' ),		Entity( "&Ocirc;",	'Ô' ),		Entity( "&Otilde;",	'Õ' ),		Entity( "&Ouml;",	'Ö' ),		Entity( "&215;",	'×' ),		Entity( "&Oslash;",	'Ø' ),		Entity( "&Ugrave;",	'Ù' ),		Entity( "&Uacute;",	'Ú' ),		Entity( "&Ucirc;",	'Û' ),		Entity( "&Uuml;",	'Ü' ),		Entity( "&Yacute;",	'Ý' ),		Entity( "&THORN;",	'Þ' ),		Entity( "&szlig;",	'ß' ),		Entity( "&agrave;",	'à' ),		Entity( "&aacute;",	'á' ),		Entity( "&acirc;",	'â' ),		Entity( "&atilde;",	'ã' ),		Entity( "&auml;",	'ä' ),		Entity( "&aring;",	'å' ),		Entity( "&aelig;",	'æ' ),		Entity( "&ccedil;",	'ç' ),		Entity( "&egrave;",	'è' ),		Entity( "&eacute;",	'é' ),		Entity( "&ecirc;",	'ê' ),		Entity( "&euml;",	'ë' ),		Entity( "&igrave;",	'ì' ),		Entity( "&iacute;",	'í' ),		Entity( "&icirc;",	'î' ),		Entity( "&iuml;",	'ï' ),		Entity( "&eth;",	'ð' ),		Entity( "&ntilde;",	'ñ' ),		Entity( "&ograve;",	'ò' ),		Entity( "&oacute;",	'ó' ),		Entity( "&ocirc;",	'ô' ),		Entity( "&otilde;",	'õ' ),		Entity( "&ouml;",	'ö' ),		Entity( "&247;",	'÷' ),		Entity( "&oslash;",	'ø' ),		Entity( "&ugrave;",	'ù' ),		Entity( "&uacute;",	'ú' ),		Entity( "&ucirc;",	'û' ),		Entity( "&uuml;",	'ü' ),		Entity( "&yacute;",	'ý' ),		Entity( "&thorn;",	'þ' ),		Entity( "&yuml;",	'ÿ' ), 		Entity( "&#SPACE;",	' ' ),		Entity( "&#RS;",	'\n' ),		Entity( "&#RE;",	'\r' ),		Entity( "&quot;",	'"' ),		Entity( "&amp;",	'&' ),		Entity( "&lt;",	'<' ),		Entity( "&gt;",	'>' ), 		Entity( "CAP-DELTA",	'Δ' ),		Entity( "ALPHA",	'α' ),		Entity( "BETA",	'β' ),		Entity( "DELTA",	'δ' ),		Entity( "EPSILON",	'ε' ),		Entity( "THETA",	'θ' ),		Entity( "MU",		'μ' ),		Entity( "PI",		'π' ),		Entity( "TAU",	'τ' ),		Entity( "CHI",	'χ' ), 		Entity( "<-",		'←' ),		Entity( "^",		'↑' ),		Entity( "->",		'→' ),		Entity( "v",		'↓' ),		Entity( "!=",		'≠' ),		Entity( "<=",		'≤' ),		Entity( nil, 0 ),	};  initarray() : array of Entity{	return entities;} badmodule(p: string){	sys->fprint(sys->fildes(2), "parse: cannot load %s: %r", p);	raise "fail:bad module";} lock: ref Semaphore; init(){	sys = load Sys Sys->PATH; 	date = load Date Date->PATH;	if (date==nil) badmodule(Date->PATH); 	daytime = load Daytime Daytime->PATH;	if(daytime == nil) badmodule(Daytime->PATH); 	contents = load Contents Contents->PATH;	if(contents == nil) badmodule(Contents->PATH); 	str = load String String->PATH;	if(str == nil) badmodule(String->PATH); 	alarms = load Alarms Alarms->PATH;	if(alarms == nil) badmodule(Alarms->PATH); 	locks = load Lock Lock->PATH;	if(locks == nil) badmodule(Lock->PATH);	locks->init();	lock = Semaphore.new();	date->init();} atexit(g: ref Private_info){	if (g.dbg_log!=nil){		sys->fprint(g.dbg_log,"At exit from parse, closing fds. \n");	}	if (g.bin!=nil)		g.bufio->g.bin.close();	if (g.bout!=nil)		g.bufio->g.bout.close();	g.bin=nil;	g.bout=nil;	exit;}  httpheaders(g: ref Private_info,vers : string){	if(vers == "")		return;	g.tok = '\n';	# 15 minutes to get request line	a := Alarm.alarm(15*1000*60); 	while(lex(g) != '\n'){		if(g.tok == Word && lex(g) == ':'){			if (g.dbg_log!=nil)				sys->fprint(g.dbg_log,"hitting parsejump. wordval is %s\n",										g.wordval);			parsejump(g,g.wordval);		}		while(g.tok != '\n')			lex(g);	}	a.stop();}  mimeok(g: ref Private_info,name : string,multipart : int,head : list of ref Content): list of ref Content{ 	generic, specific, s : string;	v : real; 	while(lex(g) != Word)		if(g.tok != ',')			return head; 	generic = g.wordval;	lex(g);	if(g.tok == '/' || multipart){		if(g.tok != '/')			return head;		if(lex(g) != Word)			return head;		specific = g.wordval;		lex(g);	}else		specific = "*";	tmp := contents->mkcontent(generic, specific);	head = tmp::head;	for(;;){		case g.tok {		';' =>			if(lex(g) == Word){				s = g.wordval;				if(lex(g) != '=' || lex(g) != Word)					return head;				v = 3.14; # should be strtof(g.wordval, nil);				if(s=="q")					tmp.q = v;				else					logit(g,sys->sprint(						"unknown %s param: %s %s",						name, s, g.wordval));			}			break;		',' =>			return  mimeok(g,name, multipart,head);		* =>			return head;		}		lex(g);	}	return head;} mimeaccept(g: ref Private_info,name : string){	g.oktype = mimeok(g,name, 1, g.oktype);} mimeacceptenc(g: ref Private_info,name : string){	g.okencode = mimeok(g,name, 0, g.okencode);} mimeacceptlang(g: ref Private_info,name : string){	g.oklang = mimeok(g,name, 0, g.oklang);} mimemodified(g: ref Private_info,name : string){	lexhead(g);	g.modtime = date->date2sec(g.wordval);	if (g.dbg_log!=nil){		sys->fprint(g.dbg_log,"modtime %d\n",g.modtime);	}	if(g.modtime == 0)		logit(g,sys->sprint("%s: %s", name, g.wordval));}  mimeagent(g: ref Private_info,nil : string){	lexhead(g);	g.client = g.wordval;} mimefrom(g: ref Private_info,nil : string){	lexhead(g);}  mimehost(g: ref Private_info,nil : string){	h : string;	lexhead(g);	(nil,h)=str->splitr(g.wordval," \t");	g.host = h;} mimereferer(g: ref Private_info,nil : string){	h : string;	lexhead(g);	(nil,h)=str->splitr(g.wordval," \t");	g.referer = h;} mimeclength(g: ref Private_info,nil : string){	h : string;	lexhead(g);	(nil,h)=str->splitr(g.wordval," \t");	g.clength = int h;} mimectype(g: ref Private_info,nil : string){	h : string;	lexhead(g);	(nil,h)=str->splitr(g.wordval," \t");	g.ctype = h;}  mimeignore(g: ref Private_info,nil : string){	lexhead(g);}  mimeunknown(g: ref Private_info,name : string){	lexhead(g);	if(g.client!="")		logit(g,sys->sprint("agent %s: ignoring header %s: %s ", 			g.client, name, g.wordval));	else		logit(g,sys->sprint("ignoring header %s: %s", name, g.wordval));} 

Lines 1–500 of 861.