Did you know ... Search Documentation:
http_wrapper.pl -- Server processing of an HTTP request
PublicShow source

This library provides the core of the implementation of the HTTP protocol at the server side and is mainly intended for internal use. It is used by library(thread_httpd) and library(inet_httpd) (deprecated).

Still, it provides a few predicates that are occasinally useful for applications:

  • http_current_request/1 finds the current request for occasional usage in places where it is not avaialable otherwise.
  • http_peer/2 finds the (IP4) peer address, getting the original address if we are behind a proxy (X-Forwarded-For)
  • http_relative_path/2 can be used to find a relative path from the current request.
Source http_wrapper(:Goal, +In, +Out, -Close, +Options) is det
Simple wrapper to read and decode an HTTP header from `In', call :Goal while watching for exceptions and send the result to the stream `Out'.

The goal is assumed to write the reply to current_output preceeded by an HTTP header, closed by a blank line. The header must contain a Content-type: <type> line. It may optionally contain a line Transfer-encoding: chunked to request chunked encoding.

Options:

request(-Request)
Return the full request to the caller
peer(+Peer)
IP address of client
Arguments:
Close- Unified to one of close, Keep-Alive or spawned(ThreadId).
Source http_wrap_spawned(:Goal, -Request, -Close) is det
Internal use only. Helper for wrapping the handler for http_spawn/2.
See also
- http_spawned/1, http_spawn/2.
Source http_spawned(+ThreadId)
Internal use only. Indicate that the request is handed to thread ThreadId.
Source cgi_close(+CGI, +Request, +State0, +Error, -Close) is det[private]
The wrapper has completed. Finish the CGI output. We have three cases:
  • The wrapper delegated the request to a new thread
  • The wrapper succeeded
  • The wrapper threw an error, non-200 status reply (e.g., not_modified, moved) or a request to reply with the content of a file.
Errors
- socket I/O errors.
Source send_error(+Out, +Request, +State0, +Error, -Close)[private]
Send status replies and reply files. The current_output no longer points to the CGI stream, but simply to the socket that connects us to the client.
Arguments:
State0- is start-status as returned by status/1. Used to find CPU usage, etc.
Source http_done(+Code, +Status, +BytesSent, +State0) is det[private]
Provide feedback for logging and debugging on how the request has been completed.
Source handler_with_output_to(:Goal, +Id, +Request, +Output, -Status) is det[private]
Run Goal with output redirected to Output. Unifies Status with ok, the error from catch/3 or a term error(goal_failed(Goal), _).
Arguments:
Request- The HTTP request read or '-' for a continuation using http_spawn/2.
Source thread_cputime(-CPU) is det[private]
CPU is the CPU time used by the calling thread.
Source cgi_hook(+Event, +CGI) is det[private]
Hook called from the CGI processing stream. See http_stream.pl for details.
Source redirect(+Header, -Action, -RestHeader) is semidet[private]
Detect the CGI Location and optional Status headers for formulating a HTTP redirect. Redirection is only established if no Status is provided, or Status is 3XX.
Source http_send_header(+Header)
This API provides an alternative for writing the header field as a CGI header. Header has the format Name(Value), as produced by http_read_header/2.
deprecated
- Use CGI lines instead
Source expand_request(+Request0, -Request)[private]
Allow for general rewrites of a request by calling request_expansion/2.
Source extend_request(+Options, +RequestIn, -Request)[private]
Merge options in the request.
Source http_current_request(-Request) is semidet
Returns the HTTP request currently being processed. Fails silently if there is no current request. This typically happens if a goal is run outside the HTTP server context.
Source http_peer(+Request, -PeerIP:atom) is semidet
True when PeerIP is the IP address of the connection peer. If the connection is established via a proxy or CDN we try to find the initiating peer. Currently supports:
  • Fastly-client-ip
  • X-real-ip
  • X-forwarded-for
  • Direct connections
bug
- The X-forwarded-for header is problematic. According to Wikipedia, the original client is the first, while according to AWS it is the last.
Source http_relative_path(+AbsPath, -RelPath) is det
Convert an absolute path (without host, fragment or search) into a path relative to the current page. This call is intended to create reusable components returning relative paths for easier support of reverse proxies.
Source debug_request(+Code, +Status, +Id, +CPU0, Bytes)[private]
Emit debugging info after a request completed with Status.