uncgi.pp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. unit uncgi;
  2. {
  3. $Id$
  4. UNCGI UNIT 2.0.11
  5. ----------------
  6. }
  7. interface
  8. uses
  9. strings
  10. {$ifdef ver1_0}
  11. {$ifdef FreeBSD} {In 1.0 FreeBSD still defines Linux!}
  12. ,Linux
  13. {$else}
  14. {$ifdef linux}
  15. ,Linux
  16. {$endif}
  17. {$endif}
  18. {$else}
  19. {$ifdef Unix}
  20. ,Unix
  21. {$endif}
  22. {$endif}
  23. {$IFDEF OS2}
  24. ,DosCalls
  25. {$ENDIF OS2}
  26. ;
  27. {***********************************************************************}
  28. const
  29. maxquery = 100;
  30. hextable : array[0..15] of char=('0','1','2','3','4','5','6','7',
  31. '8','9','A','B','C','D','E','F');
  32. uncgi_version = 'UNCGI 2.0.11';
  33. uncgi_year = '1999';
  34. maintainer_name = 'Your Name Here';
  35. maintainer_email= '[email protected]';
  36. Type cgi_error_proc = procedure (Const Proc,Err : String);
  37. var
  38. get_nodata : boolean;
  39. query_read : word;
  40. query_array : array[1..2,1..maxquery] of pchar;
  41. uncgi_error : cgi_error_proc;
  42. {***********************************************************************}
  43. { FUNCTION
  44. This function returns the REQUEST METHOD of the CGI-BIN script
  45. Input - Nothing
  46. Output - [GET|POST]
  47. }
  48. function http_request_method: pchar;
  49. { FUNCTION
  50. This function returns the "referring" page. i.e. the page you followed
  51. the link to this CGI-BIN from
  52. Input - Nothing
  53. Output - [http://somewhere.a.tld]
  54. }
  55. function http_referer: pchar;
  56. { FUNCTION
  57. This function returns the users's USER AGENT, the browser name etc.
  58. Input - Nothing
  59. Output - user agent string
  60. }
  61. function http_useragent: pchar;
  62. { FUNCTION
  63. This function returns a value from an id=value pair
  64. Input - The identifier you want the value from
  65. Output - If the identifier was found, the resulting value is
  66. the output, otherwise the output is NIL
  67. }
  68. function get_value(id: pchar): pchar;
  69. { PROCEDURE
  70. This procedure writes the content-type to the screen
  71. Input - The content type in MIME format
  72. Output - Nothing
  73. Example - set_content('text/plain');
  74. set_content('text/html');
  75. }
  76. procedure set_content(ctype: string);
  77. procedure cgi_init;
  78. procedure cgi_deinit;
  79. implementation
  80. {$IFDEF OS2}
  81. function GetEnv (EnvVar: string): PChar;
  82. var P: PChar;
  83. begin
  84. EnvVar := EnvVar + #0;
  85. if DosScanEnv (@EnvVar [1], P) = 0 then GetEnv := P else GetEnv := nil;
  86. end;
  87. {$ENDIF OS2}
  88. {$ifdef win32}
  89. Var EnvP : PChar;
  90. EnvLen : Longint;
  91. OldExitProc : Pointer;
  92. function GetEnvironmentStrings : pchar; external 'kernel32' name 'GetEnvironmentStringsA';
  93. function FreeEnvironmentStrings(p : pchar) : longbool; external 'kernel32' name 'FreeEnvironmentStringsA';
  94. Procedure FInitWin32CGI;
  95. begin
  96. { Free memory }
  97. FreeMem (EnvP,EnvLen);
  98. ExitProc:=OldExitProc;
  99. end;
  100. Procedure InitWin32CGI;
  101. var s : String;
  102. i,len : longint;
  103. hp,p : pchar;
  104. begin
  105. { Make a local copy of environment}
  106. p:=GetEnvironmentStrings;
  107. hp:=p;
  108. envp:=Nil;
  109. envlen:=0;
  110. while hp[0]<>#0 do
  111. begin
  112. len:=strlen(hp);
  113. hp:=hp+len+1;
  114. EnvLen:=Envlen+len+1;
  115. end;
  116. GetMem(EnvP,Envlen);
  117. Move(P^,EnvP^,EnvLen);
  118. FreeEnvironmentStrings(p);
  119. OldExitProc:=ExitProc;
  120. ExitProc:=@FinitWin32CGI;
  121. end;
  122. Function GetEnv(envvar: string): pchar;
  123. { Getenv that can return environment vars of length>255 }
  124. var s : String;
  125. i,len : longint;
  126. hp : pchar;
  127. begin
  128. s:=Envvar+#0;
  129. getenv:=Nil;
  130. hp:=envp;
  131. while hp[0]<>#0 do
  132. begin
  133. len:=strlen(hp);
  134. i:=Longint(strscan(hp,'='))-longint(hp);
  135. if StrLIComp(@s[1],HP,i-1)=0 then
  136. begin
  137. Len:=Len-i;
  138. getmem (getenv,len);
  139. Move(HP[I+1],getenv^,len+1);
  140. break;
  141. end;
  142. { next string entry}
  143. hp:=hp+len+1;
  144. end;
  145. end;
  146. {$endif}
  147. {$ifdef GO32V2}
  148. Function GetEnv(envvar: string): pchar;
  149. var
  150. hp : ppchar;
  151. p : pchar;
  152. hs : string;
  153. eqpos : longint;
  154. begin
  155. envvar:=upcase(envvar);
  156. hp:=envp;
  157. getenv:=nil;
  158. while assigned(hp^) do
  159. begin
  160. hs:=strpas(hp^);
  161. eqpos:=pos('=',hs);
  162. if copy(hs,1,eqpos-1)=envvar then
  163. begin
  164. getenv:=hp^+eqpos;
  165. exit;
  166. end;
  167. inc(hp);
  168. end;
  169. end;
  170. {$endif}
  171. var
  172. done_init : boolean;
  173. procedure set_content(ctype: string);
  174. begin
  175. writeln('Content-Type: ',ctype);
  176. writeln;
  177. end;
  178. function http_request_method: pchar;
  179. begin
  180. http_request_method :=getenv('REQUEST_METHOD');
  181. end;
  182. function http_referer: pchar;
  183. begin
  184. http_referer :=getenv('HTTP_REFERER');
  185. end;
  186. function http_useragent: pchar;
  187. begin
  188. http_useragent :=getenv('HTTP_USER_AGENT');
  189. end;
  190. function hexconv(h1,h2: char): char;
  191. var
  192. cnt : byte;
  193. thex : byte;
  194. begin
  195. for cnt :=0 to 15 do if upcase(h1)=hextable[cnt] then thex := cnt * 16;
  196. for cnt :=0 to 15 do if upcase(h2)=hextable[cnt] then thex := thex + cnt;
  197. hexconv := chr(thex);
  198. end;
  199. procedure def_uncgi_error(const pname,perr: string);
  200. begin
  201. set_content('text/html');
  202. writeln('<html><head><title>UNCGI ERROR</title></head>');
  203. writeln('<body>');
  204. writeln('<center><hr><h1>UNCGI ERROR</h1><hr></center><br><br>');
  205. writeln('UnCgi encountered the following error: <br>');
  206. writeln('<ul><br>');
  207. writeln('<li> procedure: ',pname,'<br>');
  208. writeln('<li> error: ',perr,'<br><hr>');
  209. writeln(
  210. '<h5><p><i>uncgi (c) ',uncgi_year,' ',maintainer_name,
  211. { skelet fix }
  212. '<a href="mailto:',maintainer_email,'">',
  213. maintainer_email,'</a></i></p></h5>');
  214. writeln('</body></html>');
  215. halt;
  216. end;
  217. function get_value(id: pchar): pchar;
  218. var
  219. cnt : word;
  220. begin
  221. get_value:=Nil;
  222. if done_init then
  223. for cnt :=1 to query_read do
  224. if strcomp(strupper(id),strupper(query_array[1,cnt]))=0 then
  225. begin
  226. get_value := query_array[2,cnt];
  227. exit;
  228. end;
  229. end;
  230. Function UnEscape(QueryString: PChar): PChar;
  231. var
  232. qunescaped : pchar;
  233. sptr : longint;
  234. cnt : word;
  235. qslen : longint;
  236. begin
  237. qslen:=strlen(QueryString);
  238. if qslen=0 then
  239. begin
  240. Unescape:=#0;
  241. get_nodata:=true;
  242. exit;
  243. end
  244. else
  245. get_nodata :=false;
  246. { skelet fix }
  247. getmem(qunescaped,qslen+1);
  248. if qunescaped=nil then
  249. begin
  250. writeln ('Oh-oh');
  251. halt;
  252. end;
  253. sptr :=0;
  254. for cnt := 0 to qslen do
  255. begin
  256. case querystring[cnt] of
  257. '+': qunescaped[sptr] := ' ';
  258. '%': begin
  259. qunescaped[sptr] :=
  260. hexconv(querystring[cnt+1], querystring[cnt+2]);
  261. inc(cnt,2);
  262. end;
  263. else
  264. qunescaped[sptr] := querystring[cnt];
  265. end;
  266. inc(sptr);
  267. { skelet fix }
  268. qunescaped[sptr]:=#0;
  269. end;
  270. UnEscape:=qunescaped;
  271. end;
  272. Function Chop(QunEscaped : PChar) : Longint;
  273. var
  274. qptr : word;
  275. cnt : word;
  276. qslen : longint;
  277. begin
  278. qptr := 1;
  279. qslen:=strlen(QUnescaped);
  280. query_array[1,qptr] := qunescaped;
  281. for cnt := 0 to qslen-1 do
  282. case qunescaped[cnt] of
  283. '=': begin
  284. qunescaped[cnt] := #0;
  285. { save address }
  286. query_array[2,qptr] := @qunescaped[cnt+1];
  287. end;
  288. '&': begin
  289. qunescaped[cnt] := #0;
  290. { Unescape previous one. }
  291. query_array[2,qptr]:=unescape(query_array[2,qptr]);
  292. inc(qptr);
  293. query_array[1,qptr] := @qunescaped[cnt+1];
  294. end;
  295. end; { Case }
  296. { Unescape last one. }
  297. query_array[2,qptr]:=unescape(query_array[2,qptr]);
  298. Chop :=qptr;
  299. end;
  300. procedure cgi_read_get_query;
  301. var
  302. querystring : pchar;
  303. qslen : longint;
  304. begin
  305. querystring :=strnew(getenv('QUERY_STRING'));
  306. if querystring<>NIL then
  307. begin
  308. qslen :=strlen(querystring);
  309. if qslen=0 then
  310. begin
  311. get_nodata :=true;
  312. exit;
  313. end
  314. else
  315. get_nodata :=false;
  316. query_read:=Chop(QueryString);
  317. end;
  318. done_init :=true;
  319. end;
  320. procedure cgi_read_post_query;
  321. var
  322. querystring : pchar;
  323. qslen : longint;
  324. sptr : longint;
  325. clen : string;
  326. ch : char;
  327. begin
  328. if getenv('CONTENT_LENGTH')<>Nil then
  329. begin
  330. clen:=strpas (getenv('CONTENT_LENGTH'));
  331. val(clen,qslen);
  332. if upcase(strpas(getenv('CONTENT_TYPE')))='APPLICATION/X-WWW-FORM-URLENCODED'
  333. then
  334. begin
  335. getmem(querystring,qslen+1);
  336. sptr :=0;
  337. while sptr<>qslen do
  338. begin
  339. read(ch);
  340. pchar(longint(querystring)+sptr)^ :=ch;
  341. inc(sptr);
  342. end;
  343. { !!! force null-termination }
  344. pchar(longint(querystring)+sptr)^ :=#0;
  345. query_read:=Chop(QueryString);
  346. end;
  347. end;
  348. done_init :=true;
  349. end;
  350. procedure cgi_init;
  351. var
  352. rmeth : pchar;
  353. begin
  354. query_read:=0;
  355. rmeth :=http_request_method;
  356. if rmeth=nil then
  357. begin
  358. uncgi_error('cgi_init()','No REQUEST_METHOD passed from server!');
  359. exit;
  360. end;
  361. if strcomp('POST',rmeth)=0 then cgi_read_post_query else
  362. if strcomp('GET',rmeth)=0 then cgi_read_get_query else
  363. uncgi_error('cgi_init()','No REQUEST_METHOD passed from server!');
  364. end;
  365. procedure cgi_deinit;
  366. begin
  367. done_init :=false;
  368. query_read :=0;
  369. fillchar(query_array,sizeof(query_array),0);
  370. end;
  371. begin
  372. {$ifdef win32}
  373. InitWin32CGI;
  374. {$endif}
  375. uncgi_error:=@def_uncgi_error;
  376. done_init :=false;
  377. fillchar(query_array,sizeof(query_array),0);
  378. end.
  379. {
  380. HISTORY
  381. $Log$
  382. Revision 1.7 2001-07-08 13:21:12 marco
  383. * Unit linux was imported twice under FreeBSD (1.0 still defines Linux!)
  384. Revision 1.6 2001/04/08 12:27:55 peter
  385. * made it compilable with both 1.0.x and 1.1
  386. Revision 1.5 2001/01/23 20:54:18 hajny
  387. * OS/2 GetEnv correction
  388. Revision 1.4 2001/01/21 21:38:52 marco
  389. * renamefest in packages
  390. Revision 1.3 2000/12/19 00:47:11 hajny
  391. + OS/2 support added
  392. Revision 1.2 2000/07/13 11:33:32 michael
  393. + removed logs
  394. }