Skip to content

dnsdist: add support for a Lua HTTP API handler #9120

Closed
@wojas

Description

@wojas
  • Program: dnsdist
  • Issue type: Feature request

Short description

It would be nice if dnsdist supported adding an API HTTP handler in Lua.

Usecase

This would allow HTTP clients to update the Lua configuration in arbitrary - but controlled - ways without having to implement custom API endpoints for every possible feature. Currently this can only be achieved by opening a socket to the dnsdist console and writing Lua function calls to it.

Description

For example, dnsdist could define a /api/v1/servers/localhost/custom[/....] endpoint that is handled in Lua. When an HTTP request is received, dnsdist calls a user-defined customhttphandler Lua function to handle the request.

The Lua callback function could look as follows:

function customhttphandler(req)
    print(req.method, req.path, req.body)
    return 200, {["Content-Type"] = "text/plain"}, "hello world!"
end 

The request object would be a table for easy future extensibility without an explosion of positional arguments. Suggested attributes to include in the request object:

  • method: the HTTP method
  • path: the full request path
  • body: the raw request body as string
  • params: parsed GET params
  • form: parsed POST values if this is a POST request and the content type is application/x-www-form-urlencoded
  • subpath: the subpath under /api/v1/servers/localhost/custom, allowing for custom routing in Lua.

The response args are:

  • The HTTP status code
  • An optional table of extra HTTP headers to set on the response (nil allowed)
  • A string with the response contents

In order to limit the time spent in the Lua code (which will block dnsdist), dnsdist buffers the whole request with body before calling the Lua code. This processing is all done in a separate thread. For the same reason, the Lua handler returns the whole response at once, instead of streaming the response as is common in modern frameworks.

If the customhttphandler callback is not defined, dnsdist returns a 404 and an appropriate message.

The HTTP request is subject to API key validation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions