uncgi.pp 11 KB

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