unit uncgi; { $Id$ UNCGI UNIT 2.0.11 ---------------- } {$ASSERTIONS ON} interface uses strings {$IFDEF OS2} ,DosCalls {$ENDIF OS2} ; {***********************************************************************} const maxquery = 100; uncgi_version = 'UNCGI 2.1.1'; uncgi_year = '1999'; maintainer_name = 'Your Name Here'; maintainer_email= 'your@email.address.here'; Type cgi_error_proc = procedure (Const Proc,Err : String); PCgiVar=^TCgiVar; TCgiVar=Record Name:PChar; NbrValues:LongInt; Value:PPChar end; var cgiEnvC:LongInt; cgiEnvP:PCgiVar; get_nodata : boolean; query_read : Cardinal; uncgi_error : cgi_error_proc; {***********************************************************************} { FUNCTION This function returns the REQUEST METHOD of the CGI-BIN script Input - Nothing Output - [GET|POST] } function http_request_method: pchar; { FUNCTION This function returns the "referring" page. i.e. the page you followed the link to this CGI-BIN from Input - Nothing Output - [http://somewhere.a.tld] } function http_referer: pchar; { FUNCTION This function returns the IP address of the client. Input - Nothing Output - an internet IP address. } function http_remote: pchar; { FUNCTION This function returns the users's USER AGENT, the browser name etc. Input - Nothing Output - user agent string } function http_useragent: pchar; { FUNCTION This function returns a value from an id=value pair Input - The identifier you want the value from Output - If the identifier was found, the resulting value is the output, otherwise the output is NIL } function get_value(id: pchar): pchar; { FUNCTION This function extracts array of values for the same variable (used in some checkbox forms) Use like in this example: v:=get_value('some_id'); while v<>'' do begin Do_something_with(v); v:=get_next_value; end; } function get_next_value:PChar; { PROCEDURE This procedure writes the content-type to the screen Input - The content type in MIME format Output - Nothing Example - set_content('text/plain'); set_content('text/html'); } procedure set_content(const ctype: string); { Function to get the requested URL } function http_url: pchar; procedure cgi_init; procedure cgi_deinit; implementation {$IFDEF OS2} function GetEnv (EnvVar: string): PChar; var P: PChar; begin EnvVar := EnvVar + #0; if DosScanEnv (@EnvVar [1], P) = 0 then GetEnv := P else GetEnv := nil; end; {$ENDIF OS2} {$ifdef win32} Var EnvP : PChar; EnvLen : Longint; OldExitProc : Pointer; function GetEnvironmentStrings : pchar; external 'kernel32' name 'GetEnvironmentStringsA'; function FreeEnvironmentStrings(p : pchar) : longbool; external 'kernel32' name 'FreeEnvironmentStringsA'; Procedure FInitWin32CGI; begin { Free memory } FreeMem (EnvP,EnvLen); ExitProc:=OldExitProc; end; Procedure InitWin32CGI; var s : String; i,len : longint; hp,p : pchar; begin { Make a local copy of environment} p:=GetEnvironmentStrings; hp:=p; envp:=Nil; envlen:=0; while hp[0]<>#0 do begin len:=strlen(hp); hp:=hp+len+1; EnvLen:=Envlen+len+1; end; GetMem(EnvP,Envlen); Move(P^,EnvP^,EnvLen); FreeEnvironmentStrings(p); OldExitProc:=ExitProc; ExitProc:=@FinitWin32CGI; end; Function GetEnv(envvar: string): pchar; { Getenv that can return environment vars of length>255 } var s : String; i,len : longint; hp : pchar; begin s:=Envvar+#0; getenv:=Nil; hp:=envp; while hp[0]<>#0 do begin len:=strlen(hp); i:=Longint(strscan(hp,'='))-longint(hp); if StrLIComp(@s[1],HP,i-1)=0 then begin Len:=Len-i; getmem (getenv,len); Move(HP[I+1],getenv^,len+1); break; end; { next string entry} hp:=hp+len+1; end; end; {$endif} {$ifdef GO32V2} Function GetEnv(envvar: string): pchar; var hp : ppchar; hs : string; eqpos : longint; begin envvar:=upcase(envvar); hp:=envp; getenv:=nil; while assigned(hp^) do begin hs:=strpas(hp^); eqpos:=pos('=',hs); if copy(hs,1,eqpos-1)=envvar then begin getenv:=hp^+eqpos; exit; end; inc(hp); end; end; {$endif} {$ifdef unix} Function GetEnv(P:string):Pchar; { Searches the environment for a string with name p and returns a pchar to it's value. A pchar is used to accomodate for strings of length > 255 } var ep : ppchar; found : boolean; Begin p:=p+'='; {Else HOST will also find HOSTNAME, etc} ep:=envp; found:=false; if ep<>nil then begin while (not found) and (ep^<>nil) do begin if strlcomp(@p[1],(ep^),length(p))=0 then found:=true else inc(ep); end; end; if found then getenv:=ep^+length(p) else getenv:=nil; end; {$endif unix} var done_init : boolean; procedure set_content(const ctype: string); begin writeln('Content-Type: ',ctype); writeln; end; function http_request_method: pchar; begin http_request_method :=getenv('REQUEST_METHOD'); end; function http_referer: pchar; begin http_referer :=getenv('HTTP_REFERER'); end; function http_useragent: pchar; begin http_useragent :=getenv('HTTP_USER_AGENT'); end; function hexconv(h1,h2: char): char; function h2c(c:char):byte; begin case c of '0'..'9':h2c := ord(c) - ord('0'); 'A'..'F':h2c := 10 + (ord(UpCase(c)) - ord('A')); end; end; begin HexConv:=Chr(h2c(h1)*16+h2c(h2)); end; procedure def_uncgi_error(const pname,perr: string); begin set_content('text/html'); writeln('UNCGI ERROR'); writeln(''); writeln('

UNCGI ERROR




'); writeln('UnCgi encountered the following error:
'); writeln('