Sign in Join
Wiki copytemporary

CGI Web Programming/zh CN: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1-我刚刚开始从英文页面翻译这篇文章。我会尽力快速完成它。
1+本文涉及简单的 CGI 程序。请注意,选用一种 Web 框架开发效率可能会更高。
22
3-This is the start of a tutorial about cgi web programming. Everyone is invited to contribute.
3+## 关于 CGI
44
5-这是CGI教程的小小开始,希望所有人做出贡献。
5+[**CGI** (公共网关接口)](<http://en.wikipedia.org/wiki/Common_Gateway_Interface>)的工作原理很简单:
66
7-## 关于 CGI
7+假设有两台计算机:
88
9-[**CGI**(通用通道界面)](http://en.wikipedia.org/wiki/Common_Gateway_Interface) 并不复杂:
9+ * 服务器 - 存储了文件(网页、图象和 CGI),通过 Web 服务程序提供服务。
10+ * 客户机 - 通常具备浏览器,请求访问服务器上的文件。
1011
11-有两台计算机——客户端和服务器:
12+只有主机上的 Web 服务程序支持 CGI 功能,您才能使用 CGI。把所有文件放入主机后,CGI 程序通常可以如下启动: **http://www.yourserver.com[.xx]/yourlogin/cgi-bin/cginame.exe**
1213
13-- 服务器存储了要访问的文件(网页、图象和CGI)。
14-- 客户端通常使用网络浏览器请求服务器上的文档。
14+**注意:**
1515
16-只有你的服务器支持,你才能使用CGI。
16+ * CGI 程序文件扩展名不一定要是 **.exe** 。
17+ * CGI 显然应该根据服务器的平台进行编译(Linux、Windows……)。
1718
18-After hosted, CGI can now be used normally as: **http://www.yourserver.com\[.xx\]/yourlogin/cgi-bin/cginame.exe**
19+## Free Pascal CGI 程序及测试方式
1920
20-**Notes:**
21+和其他程序一样,用 Free Pascal 编写的 CGI 程序就是普通的可执行程序。与 Perl 或 Python 编写的 CGI 程序不同,用 Free Pascal 编写的 CGI 程序不是解释执行的,而是独立运行的可执行程序。
2122
22-- The CGI file don't need has the **.exe** extension.
23-- The CGI should be, obviously, compiled for the platform of the computer server (Linux, Windows...)
23+若要测试 CGI 程序,电脑中必须装有支持 CGI 功能的 **HTTP 服务** 程序。 推荐一款优秀的 [HTTP服务器](<http://en.wikipedia.org/wiki/HTTP_Server>):[Apache](<http://en.wikipedia.org/wiki/Apache_HTTP_Server>),Windows 和 Linux 均可支持。
2424
25-## 关于 FreePascal CGI 以及如何测试你的CGI
25+## 十分简单的示例
2626
27-Free Pascal 的CGI程序是普通的可执行程序。
27+以下是一个迷你 CGI 程序的示例,仅供演示。
2828
29-请注意,Free Pascal CGI不是解释执行的,而是编译执行的。
29+ 1. 设置 Cookie
30+ 2. 输出 Content-Type(即声明可输出合法的 HTTP 文本)
31+ 3. 读取 Cookie
32+ 4. 以 GET 方式读取表单数据
33+ 5. 以 POST 方式读取表单数据
3034
31-要测试你的CGI你必须在你的计算机上架设服务器,并且支持CGI.
35+```
36+program mini;
37+
38+ uses dos;
39+
40+ var
41+ a:string;
42+ c:char;
43+ begin
44+ //set a cookie (must come before content-type line below)
45+ //don't forget to change the expires date
46+ writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005 18:37:00 GMT');
47+
48+ //output legal http page
49+ writeln('Content-Type:text/html',#10#13);
50+
51+ //demonstrate get cookies
52+ a:= GetEnv('HTTP_COOKIE');
53+ writeln('cookies:',a);
54+
55+ //demonstrate GET result
56+ a:='';
57+ a:= GetEnv('QUERY_STRING');
58+ writeln('GET: ',a);
59+
60+ //demonstrate POST result
61+ a:='';
62+ while not eof(input) do
63+ begin
64+ read(c);
65+ a:= a+c;
66+ end;
67+ writeln('POST: ',a);
68+ end.
69+```
3270
33-我推荐一种优良的[HTTP服务器](http://en.wikipedia.org/wiki/HTTP_Server) ——[Apache](http://en.wikipedia.org/wiki/Apache_HTTP_Server) 。 支持多种操作系统平台。 Another **HTTP Server** is [Xitami](http://en.wikipedia.org/wiki/Xitami) that supports CGIs in perl CGI and 16bits (but **Xitami** is very archaic).
71+## 简单的点击计数程序
3472
35-## 一个很简单的样例
73+**hitcounter** 程序可为网站提供简单的点击计数功能。可以考虑在配置文件中加入阻止列表,以便不把机器人程序和自己的计算机纳入统计。当然还可以有更多改进,比如设置 Cookie 让每个会话只能获得一次点击计数,但这已足以介绍 CGI 编程了。
3674
37-Here is an example of a minimal CGI program to demonstrate...
75+代码在此:[[1]](<http://sourceforge.net/p/modelbuilder/code/HEAD/tree/html/cgi/>)
3876
39-1. Setting a cookie
40-2. Outputting the content-type (ie make it put out legal text for HTTP)
41-3. Reading Cookies
42-4. Reading form data via GET
43-5. Reading form data via POST
77+## Debugging CGI
4478
45-``` pascal
46-program mini;
79+因为 CGI 程序不会往屏幕上显式信息,而且是由其他进程(Web 服务程序)启动的,调试起来要比桌面应用困难得多。
4780
48-uses dos;
81+有一种解决方案是让 CGI 应用程序启动一个调试器: 程序引用单元代码时,作为初始化过程之一,将新开一个进程,该进程会打开终端(windows中的cmd、linux中的xterm)并启动附着于调用过程的 gdb 调试器。
4982
50-var
51- a:string;
52- c:char;
53-begin
54- //set a cookie (must come before content-type line below)
55- //don't forget to change the expires date
56-writeln('Set-cookie:widget=value; path=/; expires= Mon, 21-Mar-2005 18:37:00 GMT');
83+这里主程序会暂时“Sleep”一会儿,以确保 gdb 能完成初始化。
5784
58- //output legal http page
59- writeln('Content-Type:text/html',#10#13);
85+这种方案已在以下平台通过测试:
6086
61- //demonstrate get cookies
62- a:= GetEnv('HTTP_COOKIE');
63- writeln('cookies:',a);
64-
65- //demonstrate GET result
66- a:='';
67- a:= GetEnv('QUERY_STRING');
68- writeln('GET: ',a);
87+ * 带有 Apache 2.2 的 Windows XP 32位系统,运行为普通程序或系统服务(更高版本的 Windows 不适用,参见 <http://forum.lazarus.freepascal.org/index.php/topic,21931.msg128849.html#msg128849)>
88+ * 带有 Apache 2.2 的 ubuntu 10.04 x64 系统
89+ * 带有 Apache 2 的 Debian unstable x64 系统(2013年2月)
90+
91+更多说明详见单元代码。
92+```
93+unit SelfDebug;
6994
70- //demonstrate POST result
71- a:='';
72- while not eof(input) do
73- begin
74- read(c);
75- a:= a+c;
76- end;
77- writeln('POST: ',a);
78-end.
95+ {
96+ Instructions:
97+ -Include unit anywhere in the program.
98+ -Change DEBUGGER constant to match your debugger (gdb, ddd,gdbtui...)
99+ -For Windows, if the program to debug is running as a service (e.g. CGI application
100+ from Apache running as a service), make sure the service is configured with
101+ "Interact with desktop" checked. Failing to do so, the debugger will be started
102+ without a console, break the program and hang waiting for input. Killing the debugger
103+ is your only option.
104+ -For Linux, if the program to debug is running as a different user (e.g. CGI application),
105+ run "xhost +" in a terminal to enable all users to connect to xserver.
106+ If needed, change DISPLAY to match your DISPLAY environment variable
107+ }
108+
109+ {$mode objfpc}{$H+}
110+
111+ interface
112+
113+ uses
114+ Classes, SysUtils,Process;
115+
116+ implementation
117+
118+ const
119+ {$ifdef windows}
120+ DEBUGGER='C:\lazarus\mingw\bin\7.3\gdb.exe';
121+ {$endif}
122+ {$ifdef linux}
123+ DEBUGGER='gdb';
124+ DISPLAY=':0.0';
125+ {$endif}
126+ MSWAIT=2000;
127+
128+ var
129+ AProcess: TProcess;
130+
131+ initialization
132+ AProcess := TProcess.Create(nil);
133+ {$ifdef windows}
134+ AProcess.CommandLine := format('cmd /C START "Debugging %s" /WAIT "%s" "%s" %d"',[paramstr(0),debugger,paramstr(0),GetProcessID]);
135+ {$endif}
136+ {$ifdef linux}
137+ AProcess.CommandLine := format('xterm -display %s -T "Debugging %s" -e "%s" "%s" %d',[DISPLAY,paramstr(0),DEBUGGER,paramstr(0),GetProcessID]);
138+ {$endif}
139+ AProcess.Execute;
140+ sleep(MSWAIT);
141+ finalization
142+ AProcess.Free;
143+ end.
79144```
80145
81-## A simple hitcounter
82-
83-The application **hitcounter** presents a simple hitcounter for your website. It is possible to enter blocklists in a configfile so robots and perhaps your own computer are excluded from the statistics. There are still a lot of improvements possible ofcourse like setting a cookie so you get only one hit per session, but it gives a good introduction into CGI programming. If you use the code and modify it, please send me a patch so I can update it.
146+源代码:<http://www.hu.freepascal.org/lists/fpc-pascal/2011-October/030752.html>
84147
85-You can find the code here: [1](http://modelbuilder.svn.sourceforge.net/viewvc/modelbuilder/html/cgi/)
148+有关调试的其他解决方案,是将调试信息写入文件或事件日志 log/syslog。
86149
87150## Existing CGI Frameworks
88151
89-There are existing CGI units and frameworks that make working with CGI trivial (and allow one to set cookies, sessions, retrieve POST and GET). See [Powtils](http://z505.com/powtils/idx.shtml)
90-
91-There is also a CGIModule for lazarus, and a few CGI and HTML units in the freepascal FCL.
152+现成的 CGI 单元代码和框架已有很多了,而且能设置 Cookie、会话、读取 POST 和 GET 信息,因此直接使用 CGI 显得微末了。
92153
93-Another CGI and FastCGI framework for FreePascal/Delphi is [ExtPascal](http://extpascal.googlecode.com) an Ext JS wrapper.
154+ * 请参阅 [[Brook for Free Pascal]],完美的 Free Pascal Web 应用程序框架。
155+ * 请参阅 [FastPlaz](<http://www.fastplaz.com/>),MVC 风格的 Free Pascal Web 应用程序框架。
156+ * 请参阅 [[fcl-web]],FPC 和 Lazarus (weblaz)框架
157+ * 请参阅 [Powtils](<http://z505.com/powtils/idx.shtml>)
158+ * [ExtPascal](<http://extpascal.googlecode.com>),Ext JS 封装。
159+ * 请参阅 [Fano 框架](<https://fanoframework.github.io/>),现代 Pascal 编程语言的 Web 应用程序框架,用Free Pascal 编写。
94160
95161---
96-*From the Free Pascal wiki, page "CGI Web Programming/zh CN", as it stood on 2020-02-10 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
162+*From the Free Pascal wiki, page "CGI Web Programming/zh CN", as the Internet Archive kept it on 2025-02-11, last changed on 2024-09-22. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history