User Tools

Site Tools


Sidebar

Network and communication

k24:k24.6:k24.6.5:start

24.6.5 Class Response

You can use the Response class (gb.web) to generate an HTTP response. This class mainly generates the response headers. The response contents must be sent to the standard output of the CGI script with a PRINT command. Examples for practical realisation can be found in chapter 24.6.8 Projects.

24.6.5.1 Properties

The Response class has these properties:

PropertyData typeDescription
BufferedBooleanReturns True if the property is set or sets the property. The default is the non-buffered response. If the HTTP response is buffered, then the headers and data are sent when the Response.End method is called. You must set this property before generating an HTTP response when deviating from the default.
ContentTypeStringReturns the MIME type of the response or sets the MIME type.
StatusStringReturns the status header of a HTTP response or sets the status header.

Table 24.6.5.1.1 : Essential properties of the Response class

The following procedure provides an overview of usable MIME types:

Private Sub GetContentTypeFrom(sPath As String) As String

  Select Case Lower(File.Ext(sPath))
    Case "css"
      Return "text/css"
    Case "jpg", "jpeg", "jpe", "thumb"
      Return "image/jpeg"
    Case "png"
      Return "image/png"
    Case "gif"
      Return "image/gif"
    Case "tiff", "tif"
      Return "image/tiff"
    Case "odt"
      Return "application/vnd.oasis.opendocument.text"
    Case "doc"
      Return "application/msword"
    Case "ods"
      Return "application/vnd.oasis.opendocument.spreadsheet"
    Case "xls"
      Return "application/msexcel"
    Case "pdf"
      Return "application/pdf"
    Case "zip"
      Return "application/zip"
    Case "html", "htm"
      Return "text/html"
    Case "txt"
      Return "text/plain"
    Case "avi"
      Return "video/x-msvideo"
    Case "mpg", "mpeg"
      Return "video/mpeg"
    Case "ps"
      Return "application/postscript"
    Case "dwg"
      Return "application/acad"
    Case "wav"
      Return "audio/x-wav"
    Case "ogg"
      Return "application/ogg"
    Case "jar"
      Return "application/x-jar"
    Case "xml", "kml"
      Return "text/plain"
    Case Else
      Return "application/octet-stream"
  End Select
End

24.6.5.2 Methods

The Response class has the following methods:

MethodReturnTypeDescription
AddHeader ( Name As String, Value As String )StringInserts HTTP header into the response.
Begin()-Sends the response header and allows you to output the response content.
End()-Ends the response. You must call this method when the response is buffered so that the buffer is emptied and sent to the client.
Redirect ( URL As String )-Redirects the HTTP client to another URL. If the specified URL is not a host, then the host of the request is used. You should exit the CGI script immediately after using this method (with Return)!
SetCookie ( Cookie As String, Value As String [ , Domain As String, Path As String, Expires As Date, HttpOnly As Boolean ] )-Generates a new cookie with the two mandatory and the other optional parameters.
RemoveCookie ( Cookie As String, Value As String [ , Domain As String, Path As String ] )-Removes a cookie. In fact, a cookie with an expiry date in the past is sent, so this destroys the cookie.
SendFile ( Path As String [ , ContentType As String ] )-Sends a file to the client. Optionally, you can specify the type of the file content. If you use this method, you should terminate the script immediately. Do not use any other method! Note that all headers defined with the method 'AddHeader(..)' are also sent.

Table 24.6.5.2.1 : Methods of the Response class

24.6.5.3 Examples

(1) Error message interacting with the CATCH instruction:

Dim sMessage As String
...
 
CATCH
sMessage = Error.Where & ": " & Error.Text
Response.Begin()
  Response.ContentType = "text/plain;charset=utf-8"
  Print "<pre>"; sMessage; "</pre>"
Response.End()

(2) Display of the request and response headers in a browser.

Displaying the request and response headers in a browser like Firefox is a bit awkward:

  • Open a (local) web page like http://localhost/~hans/cgi-bin/wp_report_sqlite3.gambas in the web folder of the local web server.
  • First call up the 'Inspector' entry in the 'Tools/Web Developer' menu (SHIFT+F9).
  • Then switch to the 'Network Analysis' tab in the Inspector.
  • Then reload the current page (Refresh button or F5).
  • Finally, click on a host entry in the table and look at the Request and Response headers on the right under 'Headers'; either formatted or without format, whichever is preferable.

Example of request and response headers:

Request headers (401 B):

GET /~hans/cgi-bin/wp_report_sqlite3.gambas HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0

Response headers (176 B):

HTTP/1.1 200 OK
Content-type: text/html;charset=utf-8
Content-Length: 12070
Date: Fri, 13 Nov 2020 17:47:21 GMT
Server: lighttpd/1.4.55

With the method Response.AddHeader(…) you are able to insert your own headers into the response.

B1
Figure 24.6.5.3.1: Network analysis for www.gambas-buch.de

If you switch to the 'Cookies' tab next to it instead of the 'Headers' tab, then you will see the cookies set for the current web page.

(3) Set or delete cookies.

With the methods Response.SetCookie(…) and Response.RemoveCookie(…) you can set or delete a cookie.

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.6/k24.6.5/start.txt · Last modified: 16.08.2022 (external edit)

Page Tools