http.lsp

Module index



Module: Http

Author: Jeff Ober
Version: 1.1
Original location: http://static.artfulcode.net/newlisp/http.lsp
Source: http.lsp
Package definition: http.qwerty

A bare-bones HTTP 1.0 library (updated for newlisp 10).

Http is an extremely bare-bones HTTP 1.0 library. Not all functionality is implemented. In particular, the ability to parse an HTTP response is not yet finished, but the ability to parse requests and send both requests and responses is finished. This module has not been rigorously tested. Your mileage may vary. Requires newlisp 10.

Version history

1.1 • updated for newlisp 10 • code clean-up

1.0 • initial release

- § -

Http:parse-request

syntax: (Http:parse-request str-request)
parameter: str-request - an HTTP request received

Parses an HTTP request and returns an association list.

example:
 (parse-request
   (format-request "POST"
                   "/cgi-bin/post_comment.cgi"
                   '(("Host" "www.somesite.com"))
                   "name=Some+Person&comment=Hello+world!"))

 => (("method" "POST")
     ("path" "/cgi-bin/post_comment.cgi")
     ("http-version" "1.0")
     ("headers" (("host" "www.somesite.com")
                 ("content-length" "37") nil)) 
     ("content" ""))


- § -

Http:format-response

syntax: (Http:format-response str-response [int-code [str-content-type [assoc-headers]]])
parameter: str-response - the text of the HTTP response
parameter: int-code - the HTTP response code; default is 200 (success)
parameter: str-content-type - MIME type of response; default is "text/html"
parameter: assoc-headers - association list of headers to add to response

Formats an HTTP/1.0 response.

example:
 (format-response binary-file-content 200 "audio/mp3")
 => "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type: audio/mp3\r\nDate: Tue, 08 Jul 2008 10:30:09 EDT\r\nContent-Length: 17\r\n\r\n11000101010101..."


- § -

Http:format-request

syntax: (Http:format-request str-method [str-path [assoc-headers [str-content]]])
parameter: str-method - request method (GET, POST, HEAD, or PUT)
parameter: str-path - request path; default is "/"
parameter: assoc-headers - association list of headers to add to request
parameter: str-content - for POST and PUT methods, string containing request content

Formats an appropriate HTTP/1.0 request. Note that the "Host" header must be added explicitly if required.

@example (format-request "POST" "/cgi-bin/post_comment.cgi" '(("Host" "www.somesite.com")) "name=Some+Person&comment=Hello+world!")) => "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\nDate: Tue, 08 Jul 2008 10:28:03 EDT\r\nContent-Length: 46\r\n\r\nhtmlbody

Hello world

/body/html"

- ∂ -

Artful Code

generated with newLISP  and newLISPdoc