Sending HTTP requests from translation functions

How to configure the Director to send HTTP requests from translation functions in Lua.

It is possible to configure all translation functions to send HTTP requests. If an outgoing request is sent in a translation function, the Director will delay the response to the incoming request until the outgoing request has been completed. Note that the response to the outgoing request is not handled by the Director, it only waits for the outgoing request to complete.

Requests can be sent from any translation function by defining the table OutgoingRequest in the translation function return value:

{
    OutgoingRequest = {
        Method = "HEAD",
        Protocol = "HTTP",
        Host = "example.com",
        Port = 8080,
        Path = "/example/path",
        QueryParameters = {{"param1", "value1"}, {"param2", "value2"}},
        Headers = {{"x-header", "header-value"}, {"Authorization", "Basic dXNlcjpwYXNz"}}
    }
}

The following fields for OutgoingRequest are supported:

  • Method: The HTTP method to use. Defaults to HEAD.
  • Protocol: The protocol to use. Defaults to the protocol of the incoming request.
  • Host: The host to send the request to.
  • Port: The port to send the request to. Defaults to 80 if Protocol is HTTP and 443 if Protocol is HTTPS.
  • Path: The path to send the request to. Defaults to /.
  • QueryParameters: A list of query parameters to include in the request. Note that the query parameters are defined as two-element lists in Lua.
  • Headers: A Lua table of headers to include in the request. Note that if the header name contains a dash -, it must be defined as a two-element list as seen in the example above.
  • Body: A string containing the body of the request. If this field is not defined, no body will be included in the request. If it is defined, the Content-Length header, with the length of the body, will be added to the request.

All fields except Host are optional.

Using the example above, the following response translation function will make the Director can send a GET request to http://example.com:8080/example/path?param1=value1&param2=value2 with the headers x-header: x-value and Authorization: Basic dXNlcjpwYXNz:

return HTTPResponse({
    OutgoingRequest = {
        Method = "HEAD",
        Protocol = "HTTP",
        Host = "example.com",
        Port = 8080,
        Path = "/example/path",
        QueryParameters = {{"param1", "value1"}, {"param2", "value2"}},
        Headers = {{"x-header", "x-value"}, {"Authorization", "Basic dXNlcjpwYXNz"}}
    }
})

Using log level 4, the outgoing request can be seen in the Director logs:

DEBUG orc-re-work-0 AsyncRequestSender: Sending request: url=http://example.com/example/path?param1=value1&param2=value2
DEBUG orc-re-work-0 CDNManager: OutboundContentConn: example.com:8080: Connecting to target CDN example.com:8080
DEBUG orc-re-work-0 ClientConn: 192.168.103.16/28:60201/https: Sent a Lua request: outstanding-requests=1
DEBUG orc-re-work-0 CDNManager: OutboundContentConn: example.com:8080: Target CDN connection established.
DEBUG orc-re-work-0 CDNManager: OutboundContentConn: example.com:8080: Sending request to target CDN:
GET /example/path?param1=value1&param2=value2 HTTP/1.0
Authorization: Basic dXNlcjpwYXNz
Host: example.com:8080
x-header: x-value