annotate gcc/ada/libgnat/g-cgi.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . C G I --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2000-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This is a package to interface a GNAT program with a Web server via the
kono
parents:
diff changeset
33 -- Common Gateway Interface (CGI).
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 -- Other related packages are:
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 -- GNAT.CGI.Cookie which deal with Web HTTP Cookies.
kono
parents:
diff changeset
38 -- GNAT.CGI.Debug which output complete CGI runtime environment
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 -- Basically this package parse the CGI parameter which are a set of key/value
kono
parents:
diff changeset
41 -- pairs. It builds a table whose index is the key and provides some services
kono
parents:
diff changeset
42 -- to deal with this table.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 -- Example:
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 -- Consider the following simple HTML form to capture a client name:
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 -- <!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML 3.2//EN">
kono
parents:
diff changeset
49 -- <html>
kono
parents:
diff changeset
50 -- <head>
kono
parents:
diff changeset
51 -- <title>My Web Page</title>
kono
parents:
diff changeset
52 -- </head>
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 -- <body>
kono
parents:
diff changeset
55 -- <form action="/cgi-bin/new_client" method="POST">
kono
parents:
diff changeset
56 -- <input type=text name=client_name>
kono
parents:
diff changeset
57 -- <input type=submit name="Enter">
kono
parents:
diff changeset
58 -- </form>
kono
parents:
diff changeset
59 -- </body>
kono
parents:
diff changeset
60 -- </html>
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 -- The following program will retrieve the client's name:
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 -- with GNAT.CGI;
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 -- procedure New_Client is
kono
parents:
diff changeset
67 -- use GNAT;
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 -- procedure Add_Client_To_Database (Name : String) is
kono
parents:
diff changeset
70 -- begin
kono
parents:
diff changeset
71 -- ...
kono
parents:
diff changeset
72 -- end Add_Client_To_Database;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 -- begin
kono
parents:
diff changeset
75 -- -- Check that we have 2 arguments (there is two inputs tag in
kono
parents:
diff changeset
76 -- -- the HTML form) and that one of them is called "client_name".
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 -- if CGI.Argument_Count = 2
kono
parents:
diff changeset
79 -- and then CGI.Key_Exists ("client_name")
kono
parents:
diff changeset
80 -- then
kono
parents:
diff changeset
81 -- Add_Client_To_Database (CGI.Value ("client_name"));
kono
parents:
diff changeset
82 -- end if;
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 -- ...
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 -- CGI.Put_Header;
kono
parents:
diff changeset
87 -- Text_IO.Put_Line ("<html><body>< ... Ok ... >");
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 -- exception
kono
parents:
diff changeset
90 -- when CGI.Data_Error =>
kono
parents:
diff changeset
91 -- CGI.Put_Header ("Location: /htdocs/error.html");
kono
parents:
diff changeset
92 -- -- This returns the address of a Web page to be displayed
kono
parents:
diff changeset
93 -- -- using a "Location:" header style.
kono
parents:
diff changeset
94 -- end New_Client;
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 -- Note that the names in this package interface have been designed so that
kono
parents:
diff changeset
97 -- they read nicely with the CGI prefix. The recommended style is to avoid
kono
parents:
diff changeset
98 -- a use clause for GNAT.CGI, but to include a use clause for GNAT.
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 -- This package builds up a table of CGI parameters whose memory is not
kono
parents:
diff changeset
101 -- released. A CGI program is expected to be a short lived program and
kono
parents:
diff changeset
102 -- so it is adequate to have the underlying OS free the program on exit.
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 package GNAT.CGI is
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 Data_Error : exception;
kono
parents:
diff changeset
107 -- This is raised when there is a problem with the CGI protocol. Either
kono
parents:
diff changeset
108 -- the data could not be retrieved or the CGI environment is invalid.
kono
parents:
diff changeset
109 --
kono
parents:
diff changeset
110 -- The package will initialize itself by parsing the runtime CGI
kono
parents:
diff changeset
111 -- environment during elaboration but we do not want to raise an
kono
parents:
diff changeset
112 -- exception at this time, so the exception Data_Error is deferred
kono
parents:
diff changeset
113 -- and will be raised when calling any services below (except for Ok).
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 Parameter_Not_Found : exception;
kono
parents:
diff changeset
116 -- This exception is raised when a specific parameter is not found
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 Default_Header : constant String := "Content-type: text/html";
kono
parents:
diff changeset
119 -- This is the default header returned by Put_Header. If the CGI program
kono
parents:
diff changeset
120 -- returned data is not an HTML page, this header must be change to a
kono
parents:
diff changeset
121 -- valid MIME type.
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 type Method_Type is (Get, Post);
kono
parents:
diff changeset
124 -- The method used to pass parameter from the Web client to the
kono
parents:
diff changeset
125 -- server. With the GET method parameters are passed via the command
kono
parents:
diff changeset
126 -- line, with the POST method parameters are passed via environment
kono
parents:
diff changeset
127 -- variables. Others methods are not supported by this implementation.
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 type Metavariable_Name is
kono
parents:
diff changeset
130 (Auth_Type,
kono
parents:
diff changeset
131 Content_Length,
kono
parents:
diff changeset
132 Content_Type,
kono
parents:
diff changeset
133 Document_Root, -- Web server dependent
kono
parents:
diff changeset
134 Gateway_Interface,
kono
parents:
diff changeset
135 HTTP_Accept,
kono
parents:
diff changeset
136 HTTP_Accept_Encoding,
kono
parents:
diff changeset
137 HTTP_Accept_Language,
kono
parents:
diff changeset
138 HTTP_Connection,
kono
parents:
diff changeset
139 HTTP_Cookie,
kono
parents:
diff changeset
140 HTTP_Extension,
kono
parents:
diff changeset
141 HTTP_From,
kono
parents:
diff changeset
142 HTTP_Host,
kono
parents:
diff changeset
143 HTTP_Referer,
kono
parents:
diff changeset
144 HTTP_User_Agent,
kono
parents:
diff changeset
145 Path,
kono
parents:
diff changeset
146 Path_Info,
kono
parents:
diff changeset
147 Path_Translated,
kono
parents:
diff changeset
148 Query_String,
kono
parents:
diff changeset
149 Remote_Addr,
kono
parents:
diff changeset
150 Remote_Host,
kono
parents:
diff changeset
151 Remote_Port, -- Web server dependent
kono
parents:
diff changeset
152 Remote_Ident,
kono
parents:
diff changeset
153 Remote_User,
kono
parents:
diff changeset
154 Request_Method,
kono
parents:
diff changeset
155 Request_URI, -- Web server dependent
kono
parents:
diff changeset
156 Script_Filename, -- Web server dependent
kono
parents:
diff changeset
157 Script_Name,
kono
parents:
diff changeset
158 Server_Addr, -- Web server dependent
kono
parents:
diff changeset
159 Server_Admin, -- Web server dependent
kono
parents:
diff changeset
160 Server_Name,
kono
parents:
diff changeset
161 Server_Port,
kono
parents:
diff changeset
162 Server_Protocol,
kono
parents:
diff changeset
163 Server_Signature, -- Web server dependent
kono
parents:
diff changeset
164 Server_Software);
kono
parents:
diff changeset
165 -- CGI metavariables that are set by the Web server during program
kono
parents:
diff changeset
166 -- execution. All these variables are part of the restricted CGI runtime
kono
parents:
diff changeset
167 -- environment and can be read using Metavariable service. The detailed
kono
parents:
diff changeset
168 -- meanings of these metavariables are out of the scope of this
kono
parents:
diff changeset
169 -- description. Please refer to http://www.w3.org/CGI/ for a description
kono
parents:
diff changeset
170 -- of the CGI specification. Some metavariables are Web server dependent
kono
parents:
diff changeset
171 -- and are not described in the cited document.
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 procedure Put_Header
kono
parents:
diff changeset
174 (Header : String := Default_Header;
kono
parents:
diff changeset
175 Force : Boolean := False);
kono
parents:
diff changeset
176 -- Output standard CGI header by default. The header string is followed by
kono
parents:
diff changeset
177 -- an empty line. This header must be the first answer sent back to the
kono
parents:
diff changeset
178 -- server. Do nothing if this function has already been called and Force
kono
parents:
diff changeset
179 -- is False.
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 function Ok return Boolean;
kono
parents:
diff changeset
182 -- Returns True if the CGI environment is valid and False otherwise.
kono
parents:
diff changeset
183 -- Every service used when the CGI environment is not valid will raise
kono
parents:
diff changeset
184 -- the exception Data_Error.
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 function Method return Method_Type;
kono
parents:
diff changeset
187 -- Returns the method used to call the CGI
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 function Metavariable
kono
parents:
diff changeset
190 (Name : Metavariable_Name;
kono
parents:
diff changeset
191 Required : Boolean := False) return String;
kono
parents:
diff changeset
192 -- Returns parameter Name value. Returns the null string if Name
kono
parents:
diff changeset
193 -- environment variable is not defined or raises Data_Error if
kono
parents:
diff changeset
194 -- Required is set to True.
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 function Metavariable_Exists (Name : Metavariable_Name) return Boolean;
kono
parents:
diff changeset
197 -- Returns True if the environment variable Name is defined in
kono
parents:
diff changeset
198 -- the CGI runtime environment and False otherwise.
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 function URL return String;
kono
parents:
diff changeset
201 -- Returns the URL used to call this script without the parameters.
kono
parents:
diff changeset
202 -- The URL form is: http://<server_name>[:<server_port>]<script_name>
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 function Argument_Count return Natural;
kono
parents:
diff changeset
205 -- Returns the number of parameters passed to the client. This is the
kono
parents:
diff changeset
206 -- number of input tags in a form or the number of parameters passed to
kono
parents:
diff changeset
207 -- the CGI via the command line.
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 ---------------------------------------------------
kono
parents:
diff changeset
210 -- Services to retrieve key/value CGI parameters --
kono
parents:
diff changeset
211 ---------------------------------------------------
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 function Value
kono
parents:
diff changeset
214 (Key : String;
kono
parents:
diff changeset
215 Required : Boolean := False) return String;
kono
parents:
diff changeset
216 -- Returns the parameter value associated to the parameter named Key.
kono
parents:
diff changeset
217 -- If parameter does not exist, returns an empty string if Required
kono
parents:
diff changeset
218 -- is False and raises the exception Parameter_Not_Found otherwise.
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 function Value (Position : Positive) return String;
kono
parents:
diff changeset
221 -- Returns the parameter value associated with the CGI parameter number
kono
parents:
diff changeset
222 -- Position. Raises Parameter_Not_Found if there is no such parameter
kono
parents:
diff changeset
223 -- (i.e. Position > Argument_Count)
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 function Key_Exists (Key : String) return Boolean;
kono
parents:
diff changeset
226 -- Returns True if the parameter named Key exists and False otherwise
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 function Key (Position : Positive) return String;
kono
parents:
diff changeset
229 -- Returns the parameter key associated with the CGI parameter number
kono
parents:
diff changeset
230 -- Position. Raises the exception Parameter_Not_Found if there is no
kono
parents:
diff changeset
231 -- such parameter (i.e. Position > Argument_Count)
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 generic
kono
parents:
diff changeset
234 with procedure
kono
parents:
diff changeset
235 Action
kono
parents:
diff changeset
236 (Key : String;
kono
parents:
diff changeset
237 Value : String;
kono
parents:
diff changeset
238 Position : Positive;
kono
parents:
diff changeset
239 Quit : in out Boolean);
kono
parents:
diff changeset
240 procedure For_Every_Parameter;
kono
parents:
diff changeset
241 -- Iterate through all existing key/value pairs and call the Action
kono
parents:
diff changeset
242 -- supplied procedure. The Key and Value are set appropriately, Position
kono
parents:
diff changeset
243 -- is the parameter order in the list, Quit is set to True by default.
kono
parents:
diff changeset
244 -- Quit can be set to False to control the iterator termination.
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 private
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 function Decode (S : String) return String;
kono
parents:
diff changeset
249 -- Decode Web string S. A string when passed to a CGI is encoded,
kono
parents:
diff changeset
250 -- this function will decode the string to return the original
kono
parents:
diff changeset
251 -- string's content. Every triplet of the form %HH (where H is an
kono
parents:
diff changeset
252 -- hexadecimal number) is translated into the character such that:
kono
parents:
diff changeset
253 -- Hex (Character'Pos (C)) = HH.
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 end GNAT.CGI;