uncgi.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. Function GetEnv(envvar: string): pchar;
  74. { Getenv that can return environment vars of length>255 }
  75. var s : String;
  76. i,len : longint;
  77. hp : pchar;
  78. begin
  79. s:=Envvar+#0;
  80. getenv:=Nil;
  81. hp:=envp;
  82. while hp[0]<>#0 do
  83. begin
  84. len:=strlen(hp);
  85. i:=Longint(strscan(hp,'='))-longint(hp);
  86. if StrLIComp(@s[1],HP,i-1)=0 then
  87. begin
  88. Len:=Len-i;
  89. getmem (getenv,len);
  90. Move(HP[I+1],getenv^,len+1);
  91. break;
  92. end;
  93. { next string entry}
  94. hp:=hp+len+1;
  95. end;
  96. end;
  97. Procedure FInitWin32CGI;
  98. begin
  99. { Free memory }
  100. FreeMem (EnvP,EnvLen);
  101. ExitProc:=OldExitProc;
  102. end;
  103. Procedure InitWin32CGI;
  104. var s : String;
  105. i,len : longint;
  106. hp,p : pchar;
  107. begin
  108. { Make a local copy of environment}
  109. p:=GetEnvironmentStrings;
  110. hp:=p;
  111. envp:=Nil;
  112. envlen:=0;
  113. while hp[0]<>#0 do
  114. begin
  115. len:=strlen(hp);
  116. hp:=hp+len+1;
  117. EnvLen:=Envlen+len+1;
  118. end;
  119. GetMem(EnvP,Envlen);
  120. Move(P^,EnvP^,EnvLen);
  121. FreeEnvironmentStrings(p);
  122. OldExitProc:=ExitProc;
  123. ExitProc:=@FinitWin32CGI;
  124. end;
  125. {$endif}
  126. var
  127. done_init : boolean;
  128. procedure set_content(ctype: string);
  129. begin
  130. writeln('Content-Type: ',ctype);
  131. writeln;
  132. end;
  133. function http_request_method: pchar;
  134. begin
  135. http_request_method :=getenv('REQUEST_METHOD');
  136. end;
  137. function http_referer: pchar;
  138. begin
  139. http_referer :=getenv('HTTP_REFERER');
  140. end;
  141. function http_useragent: pchar;
  142. begin
  143. http_useragent :=getenv('HTTP_USER_AGENT');
  144. end;
  145. function hexconv(h1,h2: char): char;
  146. var
  147. cnt : byte;
  148. thex : byte;
  149. begin
  150. for cnt :=0 to 15 do if upcase(h1)=hextable[cnt] then thex := cnt * 16;
  151. for cnt :=0 to 15 do if upcase(h2)=hextable[cnt] then thex := thex + cnt;
  152. hexconv := chr(thex);
  153. end;
  154. procedure def_uncgi_error(const pname,perr: string);
  155. begin
  156. set_content('text/html');
  157. writeln('<html><head><title>UNCGI ERROR</title></head>');
  158. writeln('<body>');
  159. writeln('<center><hr><h1>UNCGI ERROR</h1><hr></center><br><br>');
  160. writeln('UnCgi encountered the following error: <br>');
  161. writeln('<ul><br>');
  162. writeln('<li> procedure: ',pname,'<br>');
  163. writeln('<li> error: ',perr,'<br><hr>');
  164. writeln(
  165. '<h5><p><i>uncgi (c) ',uncgi_year,' ',maintainer_name,
  166. { skelet fix }
  167. '<a href="mailto:',maintainer_email,'">',
  168. maintainer_email,'</a></i></p></h5>');
  169. writeln('</body></html>');
  170. halt;
  171. end;
  172. function get_value(id: pchar): pchar;
  173. var
  174. cnt : word;
  175. begin
  176. get_value:=Nil;
  177. if done_init then
  178. for cnt :=1 to query_read do
  179. if strcomp(strupper(id),strupper(query_array[1,cnt]))=0 then
  180. begin
  181. get_value := query_array[2,cnt];
  182. exit;
  183. end;
  184. end;
  185. Function UnEscape(QueryString: PChar): PChar;
  186. var
  187. qunescaped : pchar;
  188. sptr : longint;
  189. cnt : word;
  190. qslen : longint;
  191. begin
  192. qslen:=strlen(QueryString);
  193. if qslen=0 then
  194. begin
  195. Unescape:=#0;
  196. get_nodata:=true;
  197. exit;
  198. end
  199. else
  200. get_nodata :=false;
  201. { skelet fix }
  202. getmem(qunescaped,qslen+1);
  203. if qunescaped=nil then
  204. begin
  205. writeln ('Oh-oh');
  206. halt;
  207. end;
  208. sptr :=0;
  209. for cnt := 0 to qslen do
  210. begin
  211. case querystring[cnt] of
  212. '+': qunescaped[sptr] := ' ';
  213. '%': begin
  214. qunescaped[sptr] :=
  215. hexconv(querystring[cnt+1], querystring[cnt+2]);
  216. inc(cnt,2);
  217. end;
  218. else
  219. qunescaped[sptr] := querystring[cnt];
  220. end;
  221. inc(sptr);
  222. { skelet fix }
  223. qunescaped[sptr]:=#0;
  224. end;
  225. UnEscape:=qunescaped;
  226. end;
  227. Function Chop(QunEscaped : PChar) : Longint;
  228. var
  229. qptr : word;
  230. cnt : word;
  231. qslen : longint;
  232. begin
  233. qptr := 1;
  234. qslen:=strlen(QUnescaped);
  235. query_array[1,qptr] := qunescaped;
  236. for cnt := 0 to qslen-1 do
  237. case qunescaped[cnt] of
  238. '=': begin
  239. qunescaped[cnt] := #0;
  240. { save address }
  241. query_array[2,qptr] := @qunescaped[cnt+1];
  242. end;
  243. '&': begin
  244. qunescaped[cnt] := #0;
  245. { Unescape previous one. }
  246. query_array[2,qptr]:=unescape(query_array[2,qptr]);
  247. inc(qptr);
  248. query_array[1,qptr] := @qunescaped[cnt+1];
  249. end;
  250. end; { Case }
  251. { Unescape last one. }
  252. query_array[2,qptr]:=unescape(query_array[2,qptr]);
  253. Chop :=qptr;
  254. end;
  255. procedure cgi_read_get_query;
  256. var
  257. querystring : pchar;
  258. qslen : longint;
  259. begin
  260. querystring :=strnew(getenv('QUERY_STRING'));
  261. if querystring<>NIL then
  262. begin
  263. qslen :=strlen(querystring);
  264. if qslen=0 then
  265. begin
  266. get_nodata :=true;
  267. exit;
  268. end
  269. else
  270. get_nodata :=false;
  271. query_read:=Chop(QueryString);
  272. end;
  273. done_init :=true;
  274. end;
  275. procedure cgi_read_post_query;
  276. var
  277. querystring : pchar;
  278. qslen : longint;
  279. sptr : longint;
  280. clen : string;
  281. ch : char;
  282. begin
  283. if getenv('CONTENT_LENGTH')<>Nil then
  284. begin
  285. clen:=strpas (getenv('CONTENT_LENGTH'));
  286. val(clen,qslen);
  287. if upcase(strpas(getenv('CONTENT_TYPE')))='APPLICATION/X-WWW-FORM-URLENCODED'
  288. then
  289. begin
  290. getmem(querystring,qslen+1);
  291. sptr :=0;
  292. while sptr<>qslen do
  293. begin
  294. read(ch);
  295. pchar(longint(querystring)+sptr)^ :=ch;
  296. inc(sptr);
  297. end;
  298. { !!! force null-termination }
  299. pchar(longint(querystring)+sptr)^ :=#0;
  300. query_read:=Chop(QueryString);
  301. end;
  302. end;
  303. done_init :=true;
  304. end;
  305. procedure cgi_init;
  306. var
  307. rmeth : pchar;
  308. begin
  309. query_read:=0;
  310. rmeth :=http_request_method;
  311. if rmeth=nil then
  312. begin
  313. uncgi_error('cgi_init()','No REQUEST_METHOD passed from server!');
  314. exit;
  315. end;
  316. if strcomp('POST',rmeth)=0 then cgi_read_post_query else
  317. if strcomp('GET',rmeth)=0 then cgi_read_get_query else
  318. uncgi_error('cgi_init()','No REQUEST_METHOD passed from server!');
  319. end;
  320. procedure cgi_deinit;
  321. begin
  322. done_init :=false;
  323. query_read :=0;
  324. fillchar(query_array,sizeof(query_array),0);
  325. end;
  326. begin
  327. {$ifdef win32}
  328. InitWin32CGI;
  329. {$endif}
  330. uncgi_error:=@def_uncgi_error;
  331. done_init :=false;
  332. fillchar(query_array,sizeof(query_array),0);
  333. end.
  334. {
  335. HISTORY
  336. $Log$
  337. Revision 1.5 1999-11-14 15:59:06 peter
  338. * fpcmake'd
  339. Revision 1.4 1999/07/26 20:07:44 michael
  340. + Fix for empty values by Andre Steinert
  341. - 1.0.0 03/07/97
  342. ----------------
  343. Only GET method implemented
  344. - 1.0.1 05/07/97
  345. ----------------
  346. + Extra procedures for getting extra information:
  347. * referrer
  348. * user agent
  349. * request method
  350. + Crude POST reading
  351. - 1.1.0 14/07/97
  352. ----------------
  353. + Bugfix in POST reading, still doesn't work right.
  354. - 2.0.0 02/08/97
  355. ----------------
  356. Started from scratch, POST reading still limited to
  357. 255 characters max. for value
  358. - 2.0.1 04/08/97
  359. ---------------
  360. Conversion from strings to pchar done by
  361. Michael van Canneyt. (Routines "UnEscape" and "Chop")
  362. Small changes made to convert everything to pchar usage.
  363. - 2.0.2 22/04/98
  364. ---------------
  365. tested with Apache HTTP Server and bugfix in pchar conversion,
  366. PChar(Longint(PChar)+x)) syntax modified to PChar[x]
  367. by Laszlo Nemeth.
  368. - 2.0.3 05/06/98
  369. ---------------
  370. Added maintainer_name,maintainer_email,uncgi_year
  371. diff from Bernhard van Staveren.
  372. - 2.0.4 09/07/98
  373. ---------------
  374. Some more error checking.
  375. cgi_error is now a procedural variable with the old
  376. cgi_erro procedure as a default value, allowing recovery
  377. incase of an error, and not immediate shutdown.
  378. - 2.0.6 02/28/99
  379. --------------
  380. The unit can be used under Win32 also.
  381. - 2.0.7 04/04/99
  382. --------------
  383. Inversed order of Chop and unescape, thus fixing bug of possible
  384. '=' and '&' characters in values.
  385. - 2.0.8 04/08/99
  386. --------------
  387. Fixed bug in unescape, qslen wasn't initialized.
  388. - 2.0.9 05/16/99
  389. --------------
  390. Some fixes by Georgi Georgiev
  391. }