| 1 |
<?xml version="1.0" encoding="UTF-8"?> |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
<chapter module="nwg" number="1"> |
| 20 |
<title>Standard Network Working Group Module</title> |
| 21 |
|
| 22 |
<p> |
| 23 |
The <em>Standard Network Working Group</em> module is an original |
| 24 |
implemtation of the recommendations proposed by the NWG and currently |
| 25 |
found in the form of <em>Request for Comments</em> (RFC). Most of the |
| 26 |
objects are used with networking application, with the most common one |
| 27 |
beeing the <em>Universal Resource Identifier</em> (URI) object. |
| 28 |
</p> |
| 29 |
|
| 30 |
|
| 31 |
<section> |
| 32 |
<title>The uri class</title> |
| 33 |
|
| 34 |
<p> |
| 35 |
The <code>Uri</code> class is a base class that parses a |
| 36 |
<em>Uniform Resource Identifier</em> or uri string and provides |
| 37 |
methods to access individual component of that uri. The |
| 38 |
implementation conforms to RFC 3986. The URI components are the |
| 39 |
scheme, the authority, the path, the query and the fragment. The |
| 40 |
class also takes care of the character escaping. |
| 41 |
</p> |
| 42 |
|
| 43 |
<example> |
| 44 |
const uri (afnix:www:Uri "http://www.afnix.org") |
| 45 |
</example> |
| 46 |
|
| 47 |
<p> |
| 48 |
An uri can be broken into several components called the |
| 49 |
<em>scheme</em>, the <em>authority</em>, the <em>path</em>, |
| 50 |
optionally the <em>query</em> and the <em>fragment</em>. The |
| 51 |
<code>Uri</code> class provide a method to retrieve each component |
| 52 |
of the parsed uri. |
| 53 |
</p> |
| 54 |
|
| 55 |
<example> |
| 56 |
const uri (afnix:www:Uri "http://www.afnix.org/") |
| 57 |
println (uri:get-scheme) # http |
| 58 |
println (uri:get-authority) # www.afnix.org |
| 59 |
println (uri:get-path) # / |
| 60 |
</example> |
| 61 |
|
| 62 |
|
| 63 |
<subsect> |
| 64 |
<title>Character conversion</title> |
| 65 |
|
| 66 |
<p> |
| 67 |
The <code>Uri</code> class performs automatically the character |
| 68 |
conversion in the input uri. For example, the <tt>+</tt> |
| 69 |
character is replaced by a blank. The <tt>%</tt> character |
| 70 |
followed by two hexadecimal values is replaced by the |
| 71 |
corresponding ASCII character. Note that this conversion does |
| 72 |
now apply to the query string. |
| 73 |
</p> |
| 74 |
</subsect> |
| 75 |
|
| 76 |
|
| 77 |
<subsect> |
| 78 |
<title>Query string</title> |
| 79 |
|
| 80 |
<p> |
| 81 |
The <code>get-query</code> method returns the query string of |
| 82 |
the uri. The query string starts after the <tt>?</tt> |
| 83 |
character. The query string is a series of key-pair values |
| 84 |
separated by the <tt>&</tt> character. |
| 85 |
</p> |
| 86 |
|
| 87 |
<example> |
| 88 |
const uri (afnix:www:Uri |
| 89 |
"http://www.afnix.org?name=hello&value=world") |
| 90 |
println (uri:get-query) # name=hello&value=world |
| 91 |
</example> |
| 92 |
|
| 93 |
<p> |
| 94 |
The module also provides the <code>UriQuery</code> class that |
| 95 |
parses the query string and store the result in the form of a |
| 96 |
property list. The query string parse is particularly useful |
| 97 |
when writing automated scripts. |
| 98 |
</p> |
| 99 |
</subsect> |
| 100 |
|
| 101 |
<example> |
| 102 |
# create a query string object |
| 103 |
const qs (afnix:nwg:UriQuery (uri:get-query)) |
| 104 |
# get the name value |
| 105 |
qs:get-value "name" |
| 106 |
</example> |
| 107 |
</section> |
| 108 |
|
| 109 |
|
| 110 |
<section> |
| 111 |
<title>Managing a cgi request</title> |
| 112 |
|
| 113 |
<p> |
| 114 |
Managing a cgi request involves primarily the parsing of the |
| 115 |
requesting uri. The uri generally contains the http referrer as |
| 116 |
well as parameter which are stored in the form of a query |
| 117 |
string. However, depending on the cgi method which can be of type |
| 118 |
<em>GET</em> or <em>POST</em>, the treatment is somewhat |
| 119 |
different. |
| 120 |
</p> |
| 121 |
|
| 122 |
|
| 123 |
<subsect> |
| 124 |
<title>Checking the protocol version</title> |
| 125 |
|
| 126 |
<p> |
| 127 |
In the presence of a cgi protocol, it is always a good idea to |
| 128 |
check the protocol version, or at least to put an assertion. The |
| 129 |
protocol version is normally <em>CGI/1.1</em> and is stored in |
| 130 |
the <em>GATEWAY_INTERFACE</em> environment variable. |
| 131 |
</p> |
| 132 |
|
| 133 |
<example> |
| 134 |
# check the cgi protocol |
| 135 |
assert "CGI/1.1" ( |
| 136 |
afnix:sys:get-env "GATEWAY_INTERFACE") |
| 137 |
</example> |
| 138 |
</subsect> |
| 139 |
|
| 140 |
|
| 141 |
<subsect> |
| 142 |
<title>Getting the query string</title> |
| 143 |
|
| 144 |
<p> |
| 145 |
If the request method is <tt>GET</tt>, then the query string is |
| 146 |
available in the environment variable <tt>QUERY_STRING</tt>. If |
| 147 |
the request method is <tt>POST</tt>, the query string is |
| 148 |
available in the input stream. The length of the query string is |
| 149 |
given by the <tt>CONTENT_LENGTH</tt> environment variable. The |
| 150 |
following example illustrates the extraction of the query |
| 151 |
string. |
| 152 |
</p> |
| 153 |
|
| 154 |
<example> |
| 155 |
# check the cgi protocol |
| 156 |
assert "CGI/.1" ( |
| 157 |
afnix:sys:get-env "GATEWAY_INTERFACE") |
| 158 |
# initialize the query string |
| 159 |
const query (afnix:sys:get-env "QUERY_STRING") |
| 160 |
# get the request method |
| 161 |
const rqm (afnix:sys:get-env "REQUEST_METHOD") |
| 162 |
# check for a post request and update the query string |
| 163 |
if (== rqm "POST") { |
| 164 |
# create a buffer from the content length |
| 165 |
const len ( |
| 166 |
Integer (afnix:sys:get-env "CONTENT_LENGTH")) |
| 167 |
# get the standard input stream and read content |
| 168 |
const is (interp:get-input-stream) |
| 169 |
const buf (is:read len) |
| 170 |
# set the query string |
| 171 |
query:= (buf:to-string) |
| 172 |
} |
| 173 |
</example> |
| 174 |
</subsect> |
| 175 |
|
| 176 |
|
| 177 |
<subsect> |
| 178 |
<title>Parsing the query string</title> |
| 179 |
|
| 180 |
<p> |
| 181 |
The <code>UriQuery</code> class is designed to parse a cgi query |
| 182 |
string. Once the string has been parsed, it is possible to |
| 183 |
perform a query by key since the class operates with a property |
| 184 |
list. |
| 185 |
</p> |
| 186 |
|
| 187 |
<example> |
| 188 |
const query ( |
| 189 |
afnix:www:UriQuery "name=hello&value=world") |
| 190 |
query:length # 2 |
| 191 |
query:get-value "name" # hello |
| 192 |
query:get-value "value" # world |
| 193 |
</example> |
| 194 |
|
| 195 |
<p> |
| 196 |
The <code>UriQuery</code> class is the foundation to build |
| 197 |
cgi script. When the library is combined with the <em>web |
| 198 |
application management (wam)</em> service, powerful |
| 199 |
applications can be built easily. |
| 200 |
</p> |
| 201 |
</subsect> |
| 202 |
</section> |
| 203 |
|
| 204 |
|
| 205 |
<section> |
| 206 |
<title>Special functions</title> |
| 207 |
|
| 208 |
<p> |
| 209 |
Several dedicated functions are available in the library as a way |
| 210 |
to ease the object manipulations. Theses functions operate mostly |
| 211 |
on uri and files as described below. |
| 212 |
</p> |
| 213 |
|
| 214 |
|
| 215 |
<subsect> |
| 216 |
<title>Uri functions</title> |
| 217 |
|
| 218 |
<p> |
| 219 |
Several functions are designed to ease the uri |
| 220 |
manipulation. Most of them operate on the uri name or their |
| 221 |
associated system name. The <code>normalize-uri-name</code> |
| 222 |
function normalizes a string argument by adding a uri scheme if |
| 223 |
missing in the original string. If the function detects that the |
| 224 |
name starts with a host name, the <em>http</em> scheme is |
| 225 |
added. If the function detects that the string starts with a |
| 226 |
path, the <em>file</em> scheme is added. otherwise, the name |
| 227 |
argument is left untouched. The <code>system-uri-name</code> |
| 228 |
function normalizes the string argument by prioritizing the |
| 229 |
system name. The function attempts to find a file that match the |
| 230 |
sring argument and eventually build a uri file scheme. If the |
| 231 |
file is not fond, the normalization process occurs with |
| 232 |
the <code>normalize-uri-name</code> function. |
| 233 |
</p> |
| 234 |
|
| 235 |
<example> |
| 236 |
# normalize a uri name |
| 237 |
trans unm "http://www.afnix.org" |
| 238 |
assert unm ( |
| 239 |
afnix:nwg:normalize-uri-name unm) |
| 240 |
assert unm ( |
| 241 |
afnix:nwg:normalize-uri-name "www.afnix.org") |
| 242 |
assert unm ( |
| 243 |
afnix:nwg:normalize-uri-name "//www.afnix.org") |
| 244 |
</example> |
| 245 |
|
| 246 |
</subsect> |
| 247 |
|
| 248 |
|
| 249 |
<subsect> |
| 250 |
<title>Mime functions</title> |
| 251 |
|
| 252 |
<p> |
| 253 |
Mime functions are dedicated to easee the mainpulation of media |
| 254 |
types or mime. A media type is defined by a string in the form |
| 255 |
of a type and content value such |
| 256 |
as <em>text/plain</em>. The <code>mime-value-p</code> predicate |
| 257 |
returns true if a string mime value is a valid media type. From |
| 258 |
a file perspective, the <code>mime-extension-p</code> predicate |
| 259 |
returns true if the string extension has a valid media type |
| 260 |
associated to it. Finally, the <code>extension-to-mime</code> |
| 261 |
function can be used to get the string mime value associated |
| 262 |
with a file extension. |
| 263 |
</p> |
| 264 |
|
| 265 |
<example> |
| 266 |
# check a media type |
| 267 |
assert true (afnix:nwg:mime-value-p "text/plain") |
| 268 |
# check the mime extension predicate |
| 269 |
assert true (afnix:nwg:mime-extension-p "txt") |
| 270 |
# check the extension to mime |
| 271 |
assert "text/plain" ( |
| 272 |
afnix:nwg:extension-to-mime "txt") |
| 273 |
</example> |
| 274 |
</subsect> |
| 275 |
</section> |
| 276 |
|
| 277 |
|
| 278 |
<section> |
| 279 |
<title>HTTP transaction objects</title> |
| 280 |
|
| 281 |
<p> |
| 282 |
The concept of HTTP transactions is defined in RFC 2616. In the |
| 283 |
client/server approach, a client issues a request which is |
| 284 |
answered with a response. A special case arise when the server is |
| 285 |
asked to perform some extra works, such like executing a |
| 286 |
script. In this case, the answer is called a reply which is |
| 287 |
formatted into a response when the server does its job correctly. |
| 288 |
</p> |
| 289 |
|
| 290 |
<p> |
| 291 |
The nature of the HTTP objects determines how the associated |
| 292 |
stream behaves. With a HTTP request, the object is filled by |
| 293 |
reading an input stream when operating on the server side. On the |
| 294 |
other hand, the request is filled by data when operating on the |
| 295 |
client side. With a HTTP response, the opposite situation |
| 296 |
occurs. The HTTP response is filled by reading an input stream |
| 297 |
when operating on the client side and filled by data when |
| 298 |
operating on the server side. |
| 299 |
</p> |
| 300 |
|
| 301 |
<subsect> |
| 302 |
<title>HTTP protocol</title> |
| 303 |
|
| 304 |
<p> |
| 305 |
The <code>HttpProto</code> class is a base class designed to |
| 306 |
handle a HTTP header that is found in both HTTP request and |
| 307 |
response. The class is built around a property list that is |
| 308 |
filled either by parsing an input stream or by processing |
| 309 |
specific methods. The <code>HttpProto</code> defines also |
| 310 |
some methods which are often used with a HTTP request or |
| 311 |
response. |
| 312 |
</p> |
| 313 |
</subsect> |
| 314 |
</section> |
| 315 |
|
| 316 |
|
| 317 |
<section> |
| 318 |
<title>HTTP response</title> |
| 319 |
|
| 320 |
<p> |
| 321 |
The <code>HttpResponse</code> class is a class designed to handle |
| 322 |
a HTTP response. When operating on the client side, the response |
| 323 |
object is built by reading an input stream. When operating on the |
| 324 |
server side, the response object is built by calling specific |
| 325 |
methods. |
| 326 |
</p> |
| 327 |
|
| 328 |
|
| 329 |
<subsect> |
| 330 |
<title>Creating a server response</title> |
| 331 |
|
| 332 |
<p> |
| 333 |
A server response is created by specifying the response status |
| 334 |
code. By default, a HTTP response is created with the default |
| 335 |
media type <em>text/html</em>. If the media type needs to be |
| 336 |
changed, it can be passed as the second argument to the response |
| 337 |
constructor. By default, the empty constructor creates an empty |
| 338 |
constructor with a valid status code. |
| 339 |
</p> |
| 340 |
|
| 341 |
<example> |
| 342 |
#create a valid response |
| 343 |
const hr (afnix:nwg:HttpResponse 200) |
| 344 |
</example> |
| 345 |
|
| 346 |
<p> |
| 347 |
Once the server response is created, it can be augmented with |
| 348 |
some headed values. Typically, a server will add some information |
| 349 |
about the response, such like the content length, the |
| 350 |
modification time or a tag. The <code>HttpResponse</code> |
| 351 |
provides several methods that ease the generation of these |
| 352 |
header values. |
| 353 |
</p> |
| 354 |
</subsect> |
| 355 |
|
| 356 |
|
| 357 |
<subsect> |
| 358 |
<title>Creating a client response</title> |
| 359 |
|
| 360 |
<p> |
| 361 |
A client response is created by binding an input stream to a |
| 362 |
response object. During the construction, the input stream is |
| 363 |
read and the HTTP protocol header is filled. It is also during |
| 364 |
this phase that the status code is processed. It is therefore |
| 365 |
important to ensure that a response object is built correctly |
| 366 |
before attempting to access it. |
| 367 |
</p> |
| 368 |
|
| 369 |
<example> |
| 370 |
# create a client response by stream |
| 371 |
const hr (afnix:nwg:HttpResponse is) |
| 372 |
</example> |
| 373 |
</subsect> |
| 374 |
|
| 375 |
|
| 376 |
<subsect> |
| 377 |
<title>Reading a client response</title> |
| 378 |
|
| 379 |
<p> |
| 380 |
When the response has been created, it is important to check its |
| 381 |
status code. Most of the time, the response is valid and its |
| 382 |
content can be read directly. The <code>status-ok-p</code> |
| 383 |
predicate returns true if the status code is valid. In such |
| 384 |
case, a HTTP stream can be built in order to read the |
| 385 |
response. |
| 386 |
</p> |
| 387 |
|
| 388 |
<example> |
| 389 |
# check that a response is valid |
| 390 |
if (hr:status-ok-p) { |
| 391 |
# create a http stream |
| 392 |
const rs (afnix:nwg:HttpStream ht is) |
| 393 |
# read the response stream |
| 394 |
while (rs:eos-p) (rs:read) |
| 395 |
} |
| 396 |
</example> |
| 397 |
|
| 398 |
<p> |
| 399 |
Before reading a http stream, it is important to detect and verify |
| 400 |
the nature of the response content. The <code>media-type-p</code> |
| 401 |
predicate returns true if the media type is defined and the |
| 402 |
<code>get-media-type</code> method returns the response type |
| 403 |
in the form of a mime code such like <em>text/html</em>. |
| 404 |
Eventually, the character set associated with the media type can |
| 405 |
also be detected. The <code>encoding-mode-p</code> |
| 406 |
predicate and the <code>get-encoding-mode</code> method |
| 407 |
can be used to detect the content encoding mode. However, it is |
| 408 |
worth to note that the <code>HttpStream</code> object is |
| 409 |
automatically sets with the proper encoding if it can be found |
| 410 |
in the response header. |
| 411 |
</p> |
| 412 |
</subsect> |
| 413 |
|
| 414 |
|
| 415 |
<subsect> |
| 416 |
<title>Special client response</title> |
| 417 |
|
| 418 |
<p> |
| 419 |
Certain response can sometime contains special status codes that |
| 420 |
require a specific treatment. This is the case when the response |
| 421 |
corresponds to a http redirection. In this case, the new uri |
| 422 |
must be fetched to get the desired response. |
| 423 |
The <code>location-p</code> predicate returns true if the |
| 424 |
response corresponds to a http redirect and |
| 425 |
the <code>get-location</code> method can be used to get the new |
| 426 |
location uri. If this situation arises, it is up to the |
| 427 |
implementation to decide what to do with the new uri. In most |
| 428 |
cases, a new request will be sent to the server. |
| 429 |
</p> |
| 430 |
</subsect> |
| 431 |
</section> |
| 432 |
|
| 433 |
|
| 434 |
<section> |
| 435 |
<title>Cookie object</title> |
| 436 |
|
| 437 |
<p> |
| 438 |
The <code>Cookie</code> object is a special object that can be |
| 439 |
used during a http session, to post data to the http client. The |
| 440 |
idea behind <em>cookies</em> is to be able to maintain some state, |
| 441 |
during the user session for some time. A cookie is a |
| 442 |
<em>name/value</em> pair and eventually an expiration time. By |
| 443 |
default, the cookie object are defined for one http client |
| 444 |
session, but this behavior can be changed. |
| 445 |
</p> |
| 446 |
|
| 447 |
|
| 448 |
<subsect> |
| 449 |
<title>Managing cookies</title> |
| 450 |
|
| 451 |
<p> |
| 452 |
A cookie is created with a <em>name/value</em> pair and |
| 453 |
eventually an expiration time. Such expiration time is called |
| 454 |
the <em>maximum-age</em> and is automatically formatted by the |
| 455 |
object. With two arguments a session cookie is created. With a |
| 456 |
third argument as an integer, the constructor set the maximum |
| 457 |
age in seconds. |
| 458 |
</p> |
| 459 |
|
| 460 |
<example> |
| 461 |
# create a cookie with name/value |
| 462 |
const cookie (afnix:nwg:Cookie "cartid" "123456789") |
| 463 |
</example> |
| 464 |
|
| 465 |
<p> |
| 466 |
The cookie implementation follows the recommendation of the |
| 467 |
RFC-2965 for http state management. The most important point to |
| 468 |
remember is the interpretation of the maximum age that differs |
| 469 |
from one cookie version to another. With version 1, which is the |
| 470 |
default, the maximum age is defined relatively in seconds, while |
| 471 |
it is absolute with version 0.The maximum age is set either at |
| 472 |
construction or with the <code>set-max-age</code> method. |
| 473 |
The <code>set-max-age</code> method sets the cookie life time in |
| 474 |
seconds, in reference to the current time. A negative value is |
| 475 |
always reset to -1 and defined a session cookie. A 0 value tells |
| 476 |
the http client to remove the cookie. The <code>set-path</code> |
| 477 |
method defines the path for which this cookie apply. |
| 478 |
</p> |
| 479 |
</subsect> |
| 480 |
|
| 481 |
|
| 482 |
<subsect> |
| 483 |
<title>Adding a cookie</title> |
| 484 |
|
| 485 |
<p> |
| 486 |
Once the cookie is defined, the <code>set-cookie</code> method |
| 487 |
of the <code>HttpResponse</code> object can be used to install the |
| 488 |
cookie. Combined with the <code>write</code> method, the |
| 489 |
cookie can be send to the http client. |
| 490 |
</p> |
| 491 |
</subsect> |
| 492 |
</section> |
| 493 |
</chapter> |