Write CGI programs with Delphi (4)

zhaozj2021-02-17  61

Write CGI programs with Delphi (4)

From this lecture, we will enter the CGI programming process. Through the a few of the previous studies, you have passed the basic knowledge of the CGI program. Now you can sit down and write the CGI program! Third, the CGI program design 1, the server side attachment (SSI) and gateways 2, the gateway: Other protocols in the Web Connection In the process of writing CGI programs, it is best to follow the key points of the following applications: (1) Propose Problem - - You have to solve the problem (2) Design phase - conceive the basic framework and function of the CGI program (3) Coding phase - implement ideology (4) Program Transplantation - Writing Portable Code (5) Leveling - Make the program more upstairs 1st floor 1, server-side accessory (SSI) and gateways will introduce server-side accessories SSI (Server Side Include) and gateways. Strictly said that SSI is not a CGI program design, but SSI can make a work that can be completed in a simple CGI program, and sometimes SSI is even the best choice. Therefore, it is a simple introduction here. SSI defines a set of commands embedded in HTML text. Before the HTML text is sent to the HTTP client, the web server will make the SSI command to pre-processes, and the processed HTML text is output to the HTTP client's browser. The SSI command format is: ssi commands are different from Java or JavaScript, which is handled on the server, not on the client, this is the CGI program is similar. Of course, in the functional range of SSI, its advantages are the same as the CGI program, and can increase the resources utilization rate of the server, and the HTML text containing SSI is available in any web browser in the client. The following is a commonly used SSI command: (1) include command (2) echo command (3) exec command (4) config command (5) fsize command (6) FlastMod Command The only supported parameter is file, this is Insert the file of the file specified by the file parameter in the current HTML text. If you understand the C language, you can see that the "#include" command in it and the C language can be the same.

For example, there are two html text: main.html and header.html, use include command in main.html: main.html: test include ssi command The Above header comes from header.html! Header.html:

this is a title in header.html! ( However, it seems that omnihttpd does not support the include command,: this document was last updated on When you use your browser to open main.shtml to see the last modified time. (To note, you must store main.shtml in the HTDOCS directory of Omnihttpd, and use the address http://localhost/main.shtml in the browser.) EXEC commands Two parameters are CGI and CMD. The former calls a license file, such as cgi = "/ cgi-bin / finger.cgi"; the latter calls a system command, such as cmd = "ls". Regret, OmniHTTPD does not support this SSI command (also the latest version of the current). Config Command Settings This command sets the server processing file and the method of displaying the date. It has two parameters: (1) Timefmt determines the format of the date. Use the Man StrfTime to query the available value in UNIX. (2) Sizefmt determines the format of the length of the document. The value is BYTES or AddRev. This command is not supported in Omnihttpd. Fsize Command This command shows the size of a given document. The parameter is file, specifying the path and document name of the document. FLASTMOD Command This command expresses the most close-up date of the specified document.

The parameter is file, specifying the path and document name of the document. 2, the gateway: through the Web Connection Other HTTP protocols, the HTTP protocol is not allowed to visit all resources of the Internet, and to visit the resources outside the HTTP protocol (such as POP3 and SMTP Removal Electronics), you need to go to the gate. The CGI program is a good method for real network. In many UNIX's HTTP server, some common network is provided, such as Finger, Wais, Archie, and more. But in Omnihttpd, this network is not available. But we can add network level to OmniHTTPD through the presence of the CGI program. The form and its processing HTML form is a part of the web document, which is used to fill the information filled with the user to the server. Tongmong, these information is delivered to the CGI program, and the CGI program is used to enter a series of operations or data to generate the HTML document that represents the handling results. From this, it can be seen that the key part of the CGI program is to obtain the input data and the HTML document, while the operation and data is divided into large numbers. Here, I will introduce how to get data and output HTML documents in Perl and Delphi.

First, we create a HTML document called Greeting.html: Greeting.html file (stored in the htdocs directory stored in Omnihttpd) this is a greeting page! </ Title> <h1> Greeting </ h1> <body> <hr> <form action = "/ cgi-bin / greeting.pl" method = post> <p> Your first name: <input type = text name = "firstname" size = 60 maxlength = 80> </ p> <p> Your last name: <input type = text name = "lastname" size = 60 maxLENGTH = 80> <input type = submit value = "all ok ! "> <input type = reset value =" clear all "> </ p> </ form> </ body> </ html> The following is Perl's CGI program Greeting.pl: Greeting.pl file (stored in Omnihttpd CGI-BIN directory) # Greeting you! Require "cgi-lib.pl"; # =============================== ===== # GET INPUT VALUES & READPARSE (* INPUT); $ mfirstname = $ INPUT {'firstname'}; $ mlastname = $}; # ============= ====================== # do some Operations here $ mfullname = "$ mfirstname $ mlastname"; # ============ ======================== # Create HTML Document To Ou TPUTPRINT &</p> <p>PrintHeader; Print "<html> <head> <title> greeting you! </ Title> </ head> / n"; print "<body> hello, <i> $ mfullname </ i>! / N"; print "<hr> by greeting.pl </ body> </ html>"; # =============================== ==== # all Done! Test the CGI program by browsing http: //localhost/greeting.html. In the Upper Perl program, as a CGI program, it is necessary to use the Require "CGI-LIB.PL" to reference the CGI-LIB.PL file, which has many functions and processes for the CGI programming. Require is in the #include in C, but it is important to note that the require statement must have a number. The readparse process reads the data of the HTML form, the parameter is a array pointer. What should be noted is that in Perl, the call to the process is to add the & symbol in front. MfirstName, MlastName and MFullName are variables. In Perl, there must be $ symbol before the variable name. The PRINTHEADER function is actually the return value is "content-type: text / html / n / n", which tells the data on the browser to the HTML document. It is very simple to generate a html document part, which is to use the print statement to output the content of the HTML document. How is it, use Perl to write the CGI program? Below, let's write a Delphi program to complete the same function: Turn off all items in Delphi, select the menu file / new, select the Web Server Application type in the dialog, use the CGI Stand-Alone Excutable option, there is a new Project, its main window is called WebModule1. Double-click the mouse on the Actions attribute of WebModule1, and the Actions property editing window is now.</p> <p>In a new window Action, called WebActionItem1, its Default property to True; and double-click OnAction event in its Events, the insertion of the following code: procedure TWebModule1.WebModule1WebActionItem1Action (Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); var mFirstName, mLastName, mFullName: string; HtmlDoc: string; begin // Get Input Values: mFirstName: = Request.ContentFields.Values ​​[ 'firstname']; mLastName: = Request.ContentFields.Values ['lastname']; // do some operations here mfullname: = mfirstname '' mlastname; // create HTML Document to output htmldoc: = '<html> <head> <title> Greeting you! </ title> < / head> '; htmldoc: = htmldoc ' <body> Hello, <i> ' mFullName ' </ i>! '; htmldoc: = HTMLDoc ' <hr> by Greeting.cgi </ body> </ HTML> '; response.content: = HTMLDoc; END; save the unit of this project as cgimain.pas, save the project as GRETING.DPR. After compiling (with CTRL F9), copy Greeting.exe to the OmniHttpd CGI-BIN directory and renamed Greeting.cgi. At the same time, do the following we write greeting.html as follows: <form action = "/ cgi-bin / greeting.pl" method = post "is changed to <form action =" / cgi-bin / greeting.cgi "Method = POST> This, you can test the CGI program written with Delphi by browsing http: //localhost/greeting.html. From this program, it can be seen that in Delphi, the CGI program is given to a request to get the onaction event of WebActionItem. In this event, the data input and HTML documents are generated: through the request.contentfields.values ​​[HTML table single element name] to get the value of the formula.</p> <p>By assigning the Response.content assignment to the HTML document.</p> <p>Here is the three file contents of the Delphi program: -------------------------------------------------------------------------------------------------------------------------------- ------------------- Greeting.dpr: Program Greeting; {$ Apptype Console} Uses httpapp, cgiapp, cgimain in 'cgimain.pas' {webmodule1: twebmodule}; $ E CGI} {$ r * .res} Begin Application.initialize; Application.createform (TWEBModule1, WebModule1); Application.Run; End. ------------------- ---------------------------------------- cgimain.pas: Unit cgimain; Interfaces Windows, Messages, SysUtils, Classes, HTTPApp; type TWebModule1 = class (TWebModule) procedure WebModule1WebActionItem1Action (Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); private {Private declarations} public {public declarations} end; var WebModule1 : TWebModule1; implementation {$ R * .DFM} procedure TWebModule1.WebModule1WebActionItem1Action (Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); var mFirstName, mLastName, mFullName: string; HtmlDoc: string; begin // Get Input VALUES: MFirstname : = Request.ContentFields.Values ​​[ 'firstname']; mLastName: = Request.ContentFields.Values ​​[ 'lastname']; // Do some operations here mFullName: = mFirstName '' mLastName; // Create HTML document to output HTMLDoc: = '<html> <head> <title> Greeting you! </ Title> </ head>'; htmldoc: = HTMLDoc '<body> Hello, <i> mfullname ' </ i>! '; Htmldoc ' <hr> by greeting.cgi </ body> </ html> '; response.content: = HTMLDoc; End; End. ---------------- -------------------------------------------- cgimain.dfm:</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-30778.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="30778" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.053</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'sWMkWLtiYPktpcM2mKdJs_2FdaplJCGC_2F_2FExDSEUIgMQsgVIAh_2B1Ij7_2FeaKZtI_2FM8T6rztmi0dmx_2FJrv_2Fe7kvsKA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>