Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wojas opened this issue May 19, 2020 · 2 comments · Fixed by #9676
Closed

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

wojas opened this issue May 19, 2020 · 2 comments · Fixed by #9676

Comments

@wojas
Copy link
Member

wojas commented May 19, 2020

  • 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.

@Habbie
Copy link
Member

Habbie commented May 19, 2020

#6937 (unmerged, closed)

@neilcook
Copy link
Contributor

FYI I implemented something very similar to this in weakforced, and it works pretty well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants