### 4.8 HTTP and CGI ¶

`(require 'http)` or `(require 'cgi)`

Function: http:header alist ¶

  Returns a string containing lines for each element of alist; the `car` of which is followed by ‘`:` ’, then the `cdr`.

Function: http:content alist body … ¶

  Returns the concatenation of strings body with the `(http:header alist)` and the ‘`Content-Length`’ prepended.

Variable: *http:byline* ¶

  String appearing at the bottom of error pages.

Function: http:error-page status-code reason-phrase html-string … ¶

  status-code and reason-phrase should be an integer and string as specified in RFC 2068. The returned page (string) will show the status-code and reason-phrase and any additional html-strings …; with *http:byline* or SLIB’s default at the bottom.

Function: http:forwarding-page title dly uri html-string … ¶

  The string or symbol title is the page title. dly is a non-negative integer. The html-strings … are typically used to explain to the user why this page is being forwarded.

  `http:forwarding-page` returns an HTML string for a page which automatically forwards to uri after dly seconds. The returned page (string) contains any html-strings … followed by a manual link to uri, in case the browser does not forward automatically.

Function: http:serve-query serve-proc input-port output-port ¶

  reads the URI and query-string from input-port. If the query is a valid ‘`"POST"`’ or ‘`"GET"`’ query, then `http:serve-query` calls serve-proc with three arguments, the request-line, query-string, and header-alist. Otherwise, `http:serve-query` calls serve-proc with the request-line, #f, and header-alist.

  If serve-proc returns a string, it is sent to output-port. If serve-proc returns a list whose first element is an integer, then an error page with the status integer which is the first element of the list and strings from the list. If serve-proc returns a list whose first element isn’t an number, then an error page with the status code 500 and strings from the list. If serve-proc returns #f, then a ‘`Bad Request`’ (400) page is sent to output-port.

  Otherwise, `http:serve-query` replies (to output-port) with appropriate HTML describing the problem.

This example services HTTP queries from port-number:

    (define socket (make-stream-socket AF_INET 0))
    (and (socket:bind socket port-number) ; AF_INET INADDR_ANY
         (socket:listen socket 10)        ; Queue up to 10 requests.
         (dynamic-wind
             (lambda () #f)
             (lambda ()
               (do ((port (socket:accept socket) (socket:accept socket)))
                   (#f)
                 (let ((iport (duplicate-port port "r"))
                       (oport (duplicate-port port "w")))
                   (http:serve-query build:serve iport oport)
                   (close-port iport)
                   (close-port oport))
                 (close-port port)))
             (lambda () (close-port socket))))

Function: cgi:serve-query serve-proc ¶

  reads the URI and query-string from `(current-input-port)`. If the query is a valid ‘`"POST"`’ or ‘`"GET"`’ query, then `cgi:serve-query` calls serve-proc with three arguments, the request-line, query-string, and header-alist. Otherwise, `cgi:serve-query` calls serve-proc with the request-line, #f, and header-alist.

  If serve-proc returns a string, it is sent to `(current-ouput-port)`. If serve-proc returns a list whose first element is an integer, then an error page with the status integer which is the first element of the list and strings from the list. If serve-proc returns a list whose first element isn’t an number, then an error page with the status code 500 and strings from the list. If serve-proc returns #f, then a ‘`Bad Request`’ (400) page is sent to `(current-ouput-port)`.

  Otherwise, `cgi:serve-query` replies (to `(current-output-port)`) with appropriate HTML describing the problem.

Function: make-query-alist-command-server rdb command-table ¶
Function: make-query-alist-command-server rdb command-table #t ¶

  Returns a procedure of one argument. When that procedure is called with a query-alist (as returned by `uri:decode-query`, the value of the ‘`*command*`’ association will be the command invoked in command-table. If ‘`*command*`’ is not in the query-alist then the value of ‘`*suggest*`’ is tried. If neither name is in the query-alist, then the literal value ‘`*default*`’ is tried in command-table.

  If optional third argument is non-false, then the command is called with just the parameter-list; otherwise, command is called with the arguments described in its table.