1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 2000 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- This is a package to interface a GNAT program with a Web server via the
36 -- Common Gateway Interface (CGI).
38 -- Other related packages are:
40 -- GNAT.CGI.Cookie which deal with Web HTTP Cookies.
41 -- GNAT.CGI.Debug which output complete CGI runtime environment
43 -- Basically this package parse the CGI parameter which are a set of key/value
44 -- pairs. It builds a table whose index is the key and provides some services
45 -- to deal with this table.
49 -- Consider the following simple HTML form to capture a client name:
51 -- <!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML 3.2//EN">
54 -- <title>My Web Page</title>
58 -- <form action="/cgi-bin/new_client" method="POST">
59 -- <input type=text name=client_name>
60 -- <input type=submit name="Enter">
65 -- The following program will retrieve the client's name:
69 -- procedure New_Client is
72 -- procedure Add_Client_To_Database (Name : in String) is
75 -- end Add_Client_To_Database;
78 -- -- Check that we have 2 arguments (there is two inputs tag in
79 -- -- the HTML form) and that one of them is called "client_name".
81 -- if CGI.Argument_Count = 2
82 -- and the CGI.Key_Exists ("client_name")
84 -- Add_Client_To_Database (CGI.Value ("client_name"));
90 -- Text_IO.Put_Line ("<html><body>< ... Ok ... >");
93 -- when CGI.Data_Error =>
94 -- CGI.Put_Header ("Location: /htdocs/error.html");
95 -- -- This returns the address of a Web page to be displayed
96 -- -- using a "Location:" header style.
99 -- Note that the names in this package interface have been designed so that
100 -- they read nicely with the CGI prefix. The recommended style is to avoid
101 -- a use clause for GNAT.CGI, but to include a use clause for GNAT.
103 -- This package builds up a table of CGI parameters whose memory is not
104 -- released. A CGI program is expected to be a short lived program and
105 -- so it is adequate to have the underlying OS free the program on exit.
109 Data_Error : exception;
110 -- This is raised when there is a problem with the CGI protocol. Either
111 -- the data could not be retrieved or the CGI environment is invalid.
113 -- The package will initialize itself by parsing the runtime CGI
114 -- environment during elaboration but we do not want to raise an
115 -- exception at this time, so the exception Data_Error is deferred
116 -- and will be raised when calling any services below (except for Ok).
118 Parameter_Not_Found : exception;
119 -- This exception is raised when a specific parameter is not found.
121 Default_Header : constant String := "Content-type: text/html";
122 -- This is the default header returned by Put_Header. If the CGI program
123 -- returned data is not an HTML page, this header must be change to a
126 type Method_Type is (Get, Post);
127 -- The method used to pass parameter from the Web client to the
128 -- server. With the GET method parameters are passed via the command
129 -- line, with the POST method parameters are passed via environment
130 -- variables. Others methods are not supported by this implementation.
132 type Metavariable_Name is
136 Document_Root, -- Web server dependant
139 HTTP_Accept_Encoding,
140 HTTP_Accept_Language,
154 Remote_Port, -- Web server dependant
158 Request_URI, -- Web server dependant
159 Script_Filename, -- Web server dependant
161 Server_Addr, -- Web server dependant
162 Server_Admin, -- Web server dependant
166 Server_Signature, -- Web server dependant
168 -- CGI metavariables that are set by the Web server during program
169 -- execution. All these variables are part of the restricted CGI runtime
170 -- environment and can be read using Metavariable service. The detailed
171 -- meanings of these metavariables are out of the scope of this
172 -- description. Please refer to http://www.w3.org/CGI/ for a description
173 -- of the CGI specification. Some metavariables are Web server dependant
174 -- and are not described in the cited document.
177 (Header : String := Default_Header;
178 Force : Boolean := False);
179 -- Output standard CGI header by default. The header string is followed by
180 -- an empty line. This header must be the first answer sent back to the
181 -- server. Do nothing if this function has already been called and Force
184 function Ok return Boolean;
185 -- Returns True if the CGI environment is valid and False otherwise.
186 -- Every service used when the CGI environment is not valid will raise
187 -- the exception Data_Error.
189 function Method return Method_Type;
190 -- Returns the method used to call the CGI.
192 function Metavariable
193 (Name : Metavariable_Name;
194 Required : Boolean := False)
196 -- Returns parameter Name value. Returns the null string if Name
197 -- environment variable is not defined or raises Data_Error if
198 -- Required is set to True.
200 function Metavariable_Exists (Name : Metavariable_Name) return Boolean;
201 -- Returns True if the environment variable Name is defined in
202 -- the CGI runtime environment and False otherwise.
204 function URL return String;
205 -- Returns the URL used to call this script without the parameters.
206 -- The URL form is: http://<server_name>[:<server_port>]<script_name>
208 function Argument_Count return Natural;
209 -- Returns the number of parameters passed to the client. This is the
210 -- number of input tags in a form or the number of parameters passed to
211 -- the CGI via the command line.
213 ---------------------------------------------------
214 -- Services to retrieve key/value CGI parameters --
215 ---------------------------------------------------
219 Required : Boolean := False)
221 -- Returns the parameter value associated to the parameter named Key.
222 -- If parameter does not exist, returns an empty string if Required
223 -- is False and raises the exception Parameter_Not_Found otherwise.
225 function Value (Position : Positive) return String;
226 -- Returns the parameter value associated with the CGI parameter number
227 -- Position. Raises Parameter_Not_Found if there is no such parameter
228 -- (i.e. Position > Argument_Count)
230 function Key_Exists (Key : String) return Boolean;
231 -- Returns True if the parameter named Key existx and False otherwise.
233 function Key (Position : Positive) return String;
234 -- Returns the parameter key associated with the CGI parameter number
235 -- Position. Raises the exception Parameter_Not_Found if there is no
236 -- such parameter (i.e. Position > Argument_Count)
244 Quit : in out Boolean);
245 procedure For_Every_Parameter;
246 -- Iterate through all existing key/value pairs and call the Action
247 -- supplied procedure. The Key and Value are set appropriately, Position
248 -- is the parameter order in the list, Quit is set to True by default.
249 -- Quit can be set to False to control the iterator termination.
253 function Decode (S : String) return String;
254 -- Decode Web string S. A string when passed to a CGI is encoded,
255 -- this function will decode the string to return the original
256 -- string's content. Every triplet of the form %HH (where H is an
257 -- hexadecimal number) is translated into the character such that:
258 -- Hex (Character'Pos (C)) = HH.