# HTTP protocol object

This chapter covers the http objects of the standard network working group service module.

## HTTP transaction objects

The concept of http transactions is defined in RFC 2616. In the client/server approach, a client issues a request which is answered with a response. A special case arise when the server is asked to perform some extra works, such like executing a script. In this case, the answer is called a reply which is formatted into a response when the server does its job correctly.

The nature of the http objects determines how the associated stream behaves. With a http request as well as the http reply, the client view is adopted which means that the object is designed to operate with an output stream. On the hand, the http response object is designed to operate as an input strem since its represents the represents the server response that can be read by a client. In other word, the object described here are designed for a client perspective and not a server, where the respective object should be reversed.

### HTTP reply

The `HttpReply` class is a class designed to handle a HTTP reply transaction. Most of the time, this object is not needed since it is used by high level object. However, there is some situation where it might become handy. For example, a CGI script needs to reply with a text file that will be use for download. In this case, the `HttpReply` object provides a simple mechanism to perform this operation.

const reply (afnix:nwg:HttpReply)

The `HttpReply` class operates with a buffer which is used to accumulate characters. When the buffer is ready, this one is sent along with a header. The HTTP 1.1 protocol requires at least to inform the client of the content type of the data being transferred. The `HttpReply` has by default a text/plain content-type property set by default. This property can be changed at the object construction.

# create a reply object const reply (afnix:nwg:HttpReply) # add some data reply:add-buffer "Hello world" # write the reply to the output reply:write

## Cookie object

The `Cookie` object is a special object that can be used during a http session, to post data to the http client. The idea behind cookies is to be able to maintain some state, during the user session for some time. A cookie is a name/value pair and eventually an expiration time. By default, the cookie object are defined for one http client session, but this behavior can be changed.

### Managing cookies

A cookie is created with a name/value pair and eventually an expiration time. Such expiration time is called the maximum-age and is automatically formatted by the object. With two arguments a session cookie is created. With a third argument as an integer, the constructor set the maximum age in seconds.

# create a cookie with name/value const cookie (afnix:nwg:Cookie "cartid" "123456789")

The cookie implementation follows the recommendation of the RFC-2965 for http state management. The most important point to remember is the interpretation of the maximum age that differs from one cookie version to another. With version 1, which is the default, the maximum age is defined relatively in seconds, while it is absolute with version 0.The maximum age is set either at construction or with the `set-max-age` method. The `set-max-age` method sets the cookie life time in seconds, in reference to the current time. A negative value is always reset to -1 and defined a session cookie. A 0 value tells the http client to remove the cookie. The `set-path` method defines the path for which this cookie apply.

### Adding a cookie

Once the cookie is defined, the `set-cookie` method of the `HttpReply` object can be used to install the cookie. Combined with the `write` method, the cookie can be send to the http client.