Windows CGI the interface

The review


Plenty World Wide Web of applications is based on use of the external programs controlled Web by the server. Use of the given programs allows to build Web applications with dynamically updated information, khnanjahhejsja in databases or generated depending on business - rules rehhaemykh problems{tasks}. For communication{connection} between Web the server and caused programs it is widely used Common Gateway Interface (CGI), having realizations both for Windows-guided programs, and for the applications functioning in Unix environment. The given document describes the Windows-updating of interface CG differently named Windows CGI the interface.



Analysis of the given HTML-forms


Windows CGI demands, that Web the server decoded the data from HTML forms if they are transferred{handed} with help POST of a method of search. He does not demand from the server of decoding of parameters, if they are transferred{handed} as a line of search (" query string "), being part URL.


There are two ways with which the data from forms can be transferred{handed} to the server by a browser:


URL-Encoded

It is the most used data format, transmitted of forms. Contents of fields of the form are allocated from the form and are passed according to specification HTML 1.0, and then are going to in one line where are separated from each other by a symbol ampersanda. The type of the maintenance{contents} of the message is established by a browser in application/x-www-form-urlencoded.

Multipart Form Data

The given format is developed for effective loading files on the server with use of forms. Contents of fields of the form are passed as multipage MIME the message. Each field contains in one page. The type of the maintenance{contents}, is established by a browser in multipart/form-data.


"Competent" servers should be able to process both types of the data from forms.



Call CGI of programs


The server uses function CreateProcess () for call CGI of programs. The server is synchronized with CGI the program as he should define{determine} the moment of end CGI of the program. It is reached{achieved} by use of function Win32 WaitForSingleObject (), expecting reception of a signal of end CGI of the program.



The command line


The server should cause CGI the program carrying out function CreateProcess () with the command line of the following format:


WinCGI-exe cgi-data-file


WinCGI-exe


Full way to executed CGI to the program. The server does not depend on " the current catalogue " or a variable of environment PATH. Take into account, that "executed" not necessarily means .EXE a file. It can be the document, associirujuhhijsja with really executed program described in WIN.INI or System Registry.


cgi-data-file


Full way to CGI to a file of the data.



Method of a call


The server uses CreateProcess () for start of the process which is not having the main window. The called process will not be displayed what or image on the monitor of the server.


The some people of the server support a mode of debugging CGI of programs and scripts, that pozovoljaet to the server to start CGI the program as usual process with creation of the main window and display of the information on the monitor of the server. The given way is rather convenient at a stage of debugging CGI of programs.



CGI a file of the data


The server passes given CGI to programs through Windows " private profile " afqk, in a format "parameter - value" (windows INI a file). CGI the program can read the given file and will obtain all data transmitted to her from the form, and also data automatically generated by a browser.


CGI the file are given will consist of the following sekcij:


* [CGI]

* [Accept]

* [System]

* [Extra Headers]

* [Form Literal]

* [Form External]

* [Form Huge]

* [Form File]


Section [CGI]


The given section contains the majority specific CGI parameters (type dotupa, type of search, the additional headings determined in others sekcijakh, etc.). Each value is submitted vvide a symbolical line. If value is empty line, the given parameter means was lowered{omitted}. Tspisok parameters of the given section it is submitted below:


Request Protocol


The name and updating of the information report used for transfer of the given search. A format: report / updating. An example: "HTTP/1.0".


Request Method


Method which was used for the given search. For HTTP it "GET", "HEAD", "POST", etc.


Executable Path


Logic way to executed CGI to the program, necessary for link CGI to the program on itself.


Logical Path


The search also can specify to the resources necessary for performance of the given search. The given parameter contains a way in that kind which has been received by the server without mehpirovanija it{him} on a physical way on a disk.


Physical Path


If the search contains the information on a logic way, the server will transform it{him} to a physical way (for example, to a way to a file on a disk) dosupa sogasno to syntactic rules of operational system.


Query String


The information, placed the ambassador? In URL caused CGI programs. The server leaves this information without changes in that kind in which she has been placed in URL.


Request Range


Byte-range The specification received together with search (if is). Look current Internet Draft (or RFC), describing expansion HTTP for reception of more full information. The server should support job CGI of programs in byte-ranging.


Referer


URL the document containing the link on given CGI the program. It is necessary to notice, that some browsers close the given opportunity and do not give her{it} to use.


From


E-mail The address of the user of a browser. It is necessary to notice, that the given parameter is present from specification HTTP, but is not used by the majority a browser from reasons of privacy.


User Agent


The line describing the software of a browser. It is not generated by the majority of browsers.


Content Type


The given parameter contains MIME-type of the data sent by the client together with fields from the form if these data have been sent. A format: type/subtype.


Content Length


For searches with which the additional data are sent to this field will be worn out length of the sent data in bajtakh.


Content File


For the searches containing the additional data, sent by the user, this parameter contains a name of a file in which the WEB-server writes down these data. Further, the user program can count these data. The parameter contains polnuj a way to a file of the data.


Server Software


The name and the version of the server software, obrabotavshego search and called the CGI-program. A format: name/version.


Server Name


The share name of the server or the pseudonym necessary for referring on URL This parameter (in kombanacii with parameter ServerPort) can be used for calculation full URL to the server.


Server Port


Number{Room} of port on which the server works.


Server Admin


E-mail The address of the manager of the server. The given parameter is necessary dljageneracii error messages and sending of the given messages for the manager of the server or for generation of forms with URL " mailto: ".


CGI Version


The version of specification CGI. A format: the CGI/version. For the given version, " CGI/1.2 (Win) ".


Remote Host


The share name of a host of the client if it is accessible. The given parameter can be used for the identification of the client.


Remote Address


Network (IP) the address of the client. The given parameter can be used for check of the user if there is no share name.


Authentication Method


If protected call CGI of the program is used, it is the report - dependent a method autentifikacii, used for autentifikacii the user.


Authentication Realm


If protected call CGI of the program is used, it is the report - dependent the service used for autentifikacii of the user. The list of users for the received kind of service iproverjaetsja for autentifikacii the user.


Authenticated Username


If protected call CGI of the program, this login name which the client uses for autentifikacii at access to the CGI-program is used.


Section [Accept]


The given section contains types of the data sent by the client, found in heading of search as


Accept: type/subtype {parameters}


If the given parameters are present (for example, "q=0.100"), they are passed as option value Accept. For each type of the transmitted data the parameter Accept is got{started}.


Section [System]


The given section contains the parameters specific for Windows of realization CGI:


GMT Offset


Quantity{Amount} of seconds which is necessary for adding by time across Greenwich for calculation of local time of the client.


Debug Mode


The given parameter matters "Yes" if the mode " CGI/script tracing " on the server is switched on.


Output File


Full way to a file in which it is necessary to place the data sent by the server to the client after end of job of the program.


Content File


The full way to a file at in which contains the additional information acting together with search.


Section [Extra Headers]


The given section contains "additional" headings which are switched on in search as "parametr=znachenie". The server should decode both parameter, and his{its} value before they will be placed in a file of data CGI.


Section [Form Literal]


If the search from the client has come as HTTP POST from HTML forms (with a content type application/x-www-form-urlencoded or multipart/form-data) the server will decode the data from the form and will place them in section [Form Literal].


For the URL-coded data of the form, the line of transmitted parameters looks as " »???????=????????*»???????=???????? and... ", where values are in the url-coded format. The server divides{shares} "parametr=znachenie" on a symbol 'and', then divides{shares} actually "parameter" and "value", decodes "value" and places result as "parametr=raskodirovannoe_znachenie" in section [Form Literal].


For the multipage data the line of the data is represented in multipage MIME a format where each field is submitted as a separate part (file). The server decodes names and value of each part and places them in a format "parametr=znachenie" in section [Form Literal].


If the form contains any elements SELECT MULTIPLE some lines from a kind "parametr=znachenie" with an identical name of "parameter" will be created. In this case generates a normal line "parametr=znachenie" for the first meeting element, and each following represents as "parametr_X=znachenie", where "X" - the increasing counter.


Section [Form External]


If the size of the decoded line exceeds 254 symbols or the decoded line contains managing symbols, such as translation of a line, return of the carriage, double inverted commas, etc. the server places the given value in a separate temporary file, and in section [Form External] places a line as:


parametr=put` Length


Where the way is a full way and a name of the time file containing the decoded option value, and length - length in bajtakh this file.


Section [Form Huge]


If the general length of a line with the coded parameters exceeds 65,535 bytes the server does not carry out decoding, and leaves given in Content File, and in section [Form Huge] places lines as:


parametr=smehhenie Length


Where displacement is a displacement from beginning Content File on which there is a required parameter, and length - length in bajtakh values of the chosen parameter. You mozhte to use displacement for performance of search of the beginning of value of the parameter chosen you and to use length for reading value of the chosen parameter. Do not overlook, that if the parameter is coded, it is necessary for you to decode it{him} before use.


Section [Form File]


If the search has come as multipart/form-data he can contain one or several files loaded from the client. In this case each loaded file is placed in a special time file, and in section [Form File] lines have the same format, as section [Form External]. Each line of parameter in this case looks so:


Parameter = [polnyj_put`_k_fajlu] length type the link [imja_fajla]


Where polnyj_put`_k_fajlu is a way to the time file containing the loaded file, length - length in bajtakh the loaded file, type - type MIME of the loaded file, the link - a way of the coding of the loaded file and imja_fajla - the initial name of the loaded file. Use of square brackets necessarily as the name of a file and a way can contain symbols of a blank.


Example of the decoded values of the form


The form contains a small field in the given example, SELECT MULTIPLE with 2-mja small sekcijami, a field in length of 300 symbols, a field containing special symbols and a field in length 230KB.


[Form Literal]

smallfield=123 Main St. *122

multiple=first selection

multiple_1=second selection


[Form External]

field300chars=C:TEMPHS19AF6C.000 300

fieldwithlinebreaks=C:TEMPHS19AF6C.001 43


[Form Huge]

field230K=C:TEMPHS19AF6C.002 276920


Processing of result


CGI the program returns the result of job responding (obviously or implicitly) the purposes of search. The server codes result of job according to standard HTTP and uses HTTP for sending result to the client. It means, that the server adds necessary HTTP headings in the message formed CGI by the program.


The result of job CGI of the program will consist of two parts: heading and a body of the message. The heading will consist of one or more lines of the text separated from a body in the empty line. The body of the message contains the data submitted in MIME a format, specified in heading.


The server does not change a body of the document that means, that the server passes generated CGI the program the answer " as he is " .T

Special lines of heading


The server distinguishes the next lines of heading in a target stream:


Content-Type:


Specifies on MIME type of a body of the message. Value of this parameter should be in a format type/subtype.


URI: <value> (value enclosed in angle brackets)


The given value specifies on full URL or the link to a local file, the message from which will be returned to the client in a body of the message. If value is a local file, the server sends it{him} as result of search, as though the client vospol`khzovalsja method GET at generation of search. If value is full URL the server returns the message " 401 redirect " for maintenance of direct loading the specified object.


Location:


The same, as URI, but the given form now is not used. The parameter value should not be taken in angular brackets.


Other headings


Other headings are passed the client in that kind in which they are submitted.



Direct return


The server allows the final application to carry out direct return of result of search to the client. It is carried out by means of inclusion in heading of the returned message of his{its} information report. It allows CGI to programs to form the direct answer to the client with instruction{indication} HTTP of heading without preliminary processing by his{its} server..


The server analyzes result of search, pomehhaemiyj CGI the program in a target file (Output File), and if the first line "HTTP/1.0", he assumes, that the message contains full HTTP the answer and sends his{its} client unpacked.


Creation of files with help JavaScript and VBScript

1. Creation of an empty file


At first sight this function can seem useless, however, as you will bring the data in a file, not having the file, we shall not spoil faly the visitor of ours stranici.


To create an empty file with help VBSctipt it is necessary to write the following code in a HTML-file:



<script language = "VBScript">

Dim fso, f1

Set fso = CreateObject (" Scripting. FileSystemObject ")

Set f1 = fso.CreateTextFile("c:testfile.txt ", True)

</script>


The same function carries out and the following code written already on JavaScirpt:



<script language = "JavaScript">

var fso, f1;

fso = new ActiveXObject (" Scripting. FileSystemObject ");

f1 = fso.CreateTextFile("c:testfile.txt ", true);

</script>


Now, having created a pure{clean} file, it is possible to start his{its} filling...



2. Data recording in a file


Opening of a file. To open a file it is necessary to apply the following script:



Dim fso, ts

Const ForWriting = 2

Set fso = CreateObject (" Scripting. FileSystemObject ")

Set ts = fso.OpenTextFile("c:test.txt ", ForWriting, True)


Given skipt it is written on VBScript. Here in my opinion all is understandable. ForWriting - a flag showing what for to open a file. The same script on JavaScript:



var fso, ts;

var ForWriting = 2;

fso = new ActiveXObject (" Scripting. FileSystemObject ");

ts = fso.OpenTextFile("c:test.txt ", ForWriting, true);


It is necessary to note, that thus files will be automatically created.


Examples of scripts. On VBScript:



Sub CreateFile ()

Dim fso, tf

Set fso = CreateObject (" Scripting. FileSystemObject ")

Set tf = fso.CreateTextFile("c:testfile.txt ", True)

' The insert of a line with carry on new.

tf. WriteLine (" Testing 1, 2, 3. ")

' The insert 3 putykh lines.

tf. WriteBlankLines (3)

' The insert of a new line, without carry.

tf. Write (" This is a test. ")

' Closing of a file.

tf. Close

End Sub


On JavaScript:



function CreateFile ()

{

var fso, tf;

fso = new ActiveXObject (" Scripting. FileSystemObject ");

tf = fso.CreateTextFile("c:testfile.txt ", true);

// An insert of a line with carry on new.

tf. WriteLine (" Testing 1, 2, 3. ");

// An insert of 3 empty lines.

tf. WriteBlankLines (3);

// An insert of a new line, without carry.

tf. Write (" This is a test. ");

  Closing of a file.

tf. Close ();

}


That's all. That with this business to do{make}, you probably already know. In the peace purposes this opportunity to create files by client machines is very useful. It is possible to suggest for the visitor to download yours firm a badge and to establish it{him} in kchestve a badge dika C, D, E, F..., Z. It is possible to create autorun for disk X in which the line which will allow to load your firm site at start of this disk will be registered. Preservation of adjustments without use cookies, etc., etc. On this optimistic note I stop.


Thanks for attention.