1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2000-2009, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a package to interface a GNAT program with a Web server via the
35 -- Common Gateway Interface (CGI).
37 -- Other related packages are:
39 -- GNAT.CGI.Cookie which deal with Web HTTP Cookies.
40 -- GNAT.CGI.Debug which output complete CGI runtime environment
42 -- Basically this package parse the CGI parameter which are a set of key/value
43 -- pairs. It builds a table whose index is the key and provides some services
44 -- to deal with this table.
48 -- Consider the following simple HTML form to capture a client name:
50 -- <!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML 3.2//EN">
53 -- <title>My Web Page</title>
57 -- <form action="/cgi-bin/new_client" method="POST">
58 -- <input type=text name=client_name>
59 -- <input type=submit name="Enter">
64 -- The following program will retrieve the client's name:
68 -- procedure New_Client is
71 -- procedure Add_Client_To_Database (Name : String) is
74 -- end Add_Client_To_Database;
77 -- -- Check that we have 2 arguments (there is two inputs tag in
78 -- -- the HTML form) and that one of them is called "client_name".
80 -- if CGI.Argument_Count = 2
81 -- and then CGI.Key_Exists ("client_name")
83 -- Add_Client_To_Database (CGI.Value ("client_name"));
89 -- Text_IO.Put_Line ("<html><body>< ... Ok ... >");
92 -- when CGI.Data_Error =>
93 -- CGI.Put_Header ("Location: /htdocs/error.html");
94 -- -- This returns the address of a Web page to be displayed
95 -- -- using a "Location:" header style.
98 -- Note that the names in this package interface have been designed so that
99 -- they read nicely with the CGI prefix. The recommended style is to avoid
100 -- a use clause for GNAT.CGI, but to include a use clause for GNAT.
102 -- This package builds up a table of CGI parameters whose memory is not
103 -- released. A CGI program is expected to be a short lived program and
104 -- so it is adequate to have the underlying OS free the program on exit.
108 Data_Error
: exception;
109 -- This is raised when there is a problem with the CGI protocol. Either
110 -- the data could not be retrieved or the CGI environment is invalid.
112 -- The package will initialize itself by parsing the runtime CGI
113 -- environment during elaboration but we do not want to raise an
114 -- exception at this time, so the exception Data_Error is deferred
115 -- and will be raised when calling any services below (except for Ok).
117 Parameter_Not_Found
: exception;
118 -- This exception is raised when a specific parameter is not found
120 Default_Header
: constant String := "Content-type: text/html";
121 -- This is the default header returned by Put_Header. If the CGI program
122 -- returned data is not an HTML page, this header must be change to a
125 type Method_Type
is (Get
, Post
);
126 -- The method used to pass parameter from the Web client to the
127 -- server. With the GET method parameters are passed via the command
128 -- line, with the POST method parameters are passed via environment
129 -- variables. Others methods are not supported by this implementation.
131 type Metavariable_Name
is
135 Document_Root
, -- Web server dependent
138 HTTP_Accept_Encoding
,
139 HTTP_Accept_Language
,
153 Remote_Port
, -- Web server dependent
157 Request_URI
, -- Web server dependent
158 Script_Filename
, -- Web server dependent
160 Server_Addr
, -- Web server dependent
161 Server_Admin
, -- Web server dependent
165 Server_Signature
, -- Web server dependent
167 -- CGI metavariables that are set by the Web server during program
168 -- execution. All these variables are part of the restricted CGI runtime
169 -- environment and can be read using Metavariable service. The detailed
170 -- meanings of these metavariables are out of the scope of this
171 -- description. Please refer to http://www.w3.org/CGI/ for a description
172 -- of the CGI specification. Some metavariables are Web server dependent
173 -- and are not described in the cited document.
176 (Header
: String := Default_Header
;
177 Force
: Boolean := False);
178 -- Output standard CGI header by default. The header string is followed by
179 -- an empty line. This header must be the first answer sent back to the
180 -- server. Do nothing if this function has already been called and Force
183 function Ok
return Boolean;
184 -- Returns True if the CGI environment is valid and False otherwise.
185 -- Every service used when the CGI environment is not valid will raise
186 -- the exception Data_Error.
188 function Method
return Method_Type
;
189 -- Returns the method used to call the CGI
191 function Metavariable
192 (Name
: Metavariable_Name
;
193 Required
: Boolean := False) return String;
194 -- Returns parameter Name value. Returns the null string if Name
195 -- environment variable is not defined or raises Data_Error if
196 -- Required is set to True.
198 function Metavariable_Exists
(Name
: Metavariable_Name
) return Boolean;
199 -- Returns True if the environment variable Name is defined in
200 -- the CGI runtime environment and False otherwise.
202 function URL
return String;
203 -- Returns the URL used to call this script without the parameters.
204 -- The URL form is: http://<server_name>[:<server_port>]<script_name>
206 function Argument_Count
return Natural;
207 -- Returns the number of parameters passed to the client. This is the
208 -- number of input tags in a form or the number of parameters passed to
209 -- the CGI via the command line.
211 ---------------------------------------------------
212 -- Services to retrieve key/value CGI parameters --
213 ---------------------------------------------------
217 Required
: Boolean := False) return String;
218 -- Returns the parameter value associated to the parameter named Key.
219 -- If parameter does not exist, returns an empty string if Required
220 -- is False and raises the exception Parameter_Not_Found otherwise.
222 function Value
(Position
: Positive) return String;
223 -- Returns the parameter value associated with the CGI parameter number
224 -- Position. Raises Parameter_Not_Found if there is no such parameter
225 -- (i.e. Position > Argument_Count)
227 function Key_Exists
(Key
: String) return Boolean;
228 -- Returns True if the parameter named Key exists and False otherwise
230 function Key
(Position
: Positive) return String;
231 -- Returns the parameter key associated with the CGI parameter number
232 -- Position. Raises the exception Parameter_Not_Found if there is no
233 -- such parameter (i.e. Position > Argument_Count)
241 Quit
: in out Boolean);
242 procedure For_Every_Parameter
;
243 -- Iterate through all existing key/value pairs and call the Action
244 -- supplied procedure. The Key and Value are set appropriately, Position
245 -- is the parameter order in the list, Quit is set to True by default.
246 -- Quit can be set to False to control the iterator termination.
250 function Decode
(S
: String) return String;
251 -- Decode Web string S. A string when passed to a CGI is encoded,
252 -- this function will decode the string to return the original
253 -- string's content. Every triplet of the form %HH (where H is an
254 -- hexadecimal number) is translated into the character such that:
255 -- Hex (Character'Pos (C)) = HH.