Basic VSP Programming
VS-B-5 Controlling the Response Header
How to control the HTTP response from VSP
Preliminaries
- The behavior of the user-agents (Eg. browser) depends on the server's HTTP response.
- The HTTP responses are: positive (2xx), informative (1xx), redirection(3xx), client error(4xx), server error(5xx).
- The Virtuoso Web server automatically generates positive, client error and server error codes.
- The http_request_status() function overwrites the default response. Example:
http_request_status ('HTTP/1.1 404 Not found'); - Note that the response line MUST begin with "HTTP/1.1 ", and numeric code MUST be one described in RFC2616.
- Some responses require additional information in the HTTP response header.
- The functions for setting the additional lines in a response header are:
- http_header() - accept a response header line(s) as string. If more than one line is supplied, they must be separated with <CR><LF> characters. Note that the string must always finish with <CR><LF> chars. Subsequent calls of this function replace the old response.
- http_header_get() - returns the HTTP response header currently defined. It is useful when you need to append more lines.
Redirection example
The first example demonstrates a redirection to another page. It uses http_request_status() and http_header() functions to set redirection code 'HTTP/1.1 302 Found' and 'Location:' header line in the response.
If a document has moved to another url, it's possible to redirect using a meta tag within the <head> of a page. However using the method in this example, the redirection can be dynamic and conditional.
Load random Images
The second example demonstrates loading random images onto a page. Three images are loaded into the page by calling the vsp to generate each image. Each image URL is made unique by including a pic parameter. This does not imply the images will be different, however it stops the browser from reusing one image three times.
The image content is made by setting the 'Content-Type' header field of the response to the 'image/gif', then returning a randomly chosen block of Gif data.
A real world use for a random image, is to show an advertising banner graphic. Rather than change the url for the banner, the actual image content is controlled. This technique might also be useful for changing an appearance of a web page based on some criteria such as if the user has membership access or the time of day is either day or night to show a sun or moon image.
| View the source | Action |
|---|---|
| 1. vs_b_5_sample_1.html | Run |
| 2. vs_b_5_sample_1.vsp | Run |
| 3. vs_b_5_sample_2.vsp | Run |
OpenLink Home
Technical Support