GithubHelp home page GithubHelp logo

isabella232 / haproxy-lua-http Goto Github PK

View Code? Open in Web Editor NEW

This project forked from haproxytech/haproxy-lua-http

0.0 0.0 0.0 73 KB

Simple Lua HTTP helper && client for use with HAProxy.

License: Apache License 2.0

Lua 100.00%

haproxy-lua-http's Introduction

Lua HTTP client library for HAProxy

This is a pure Lua HTTP 1.1 library for HAProxy. It should work on any modern HAProxy version (1.7+)

The library is loosely modeled after Python's Requests Library using the same attribute names and similar calling conventions for "HTTP verb" methods (where we use Lua specific named parameter support)

In addition to client side, the library also supports server side request parsing, where we utilize HAProxy Lua API for all heavy lifting (i.e. HAProxy handles client side connections, parses the headers and gives us access to request body).

Usage

After downloading this library, you will need to move it into your Lua package package path, to be able to use it from your script. In later HAProxy versions, there is a lua-prepend-path directive which can make your life easier.

Basic usage for parsing client requests, constructing responses, or sending custom requests to external servers is demonstrated bellow. You can use this in your own Lua actions or services:

local http = require('http')

local function main(applet)

    -- 1) Parse client side request and print received headers

    local req = http.request.parse(applet)
    for k, v in req:get_headers() do
        core.Debug(k .. ": " .. v)
    end

    -- You can also parse submitted form data
    local form, err = req:parse_multipart()

    -- 2) Send request to external server (please note there is no DNS
    --    support for Lua on HAProxy

    local res, err = http.get{url="http://1.2.3.4",
        headers={host="example.net", ["x-test"]={"a", "b"}}
    }

    if res then
        for k, v in res:get_headers() do
            core.Debug(k .. ": " .. v)
        end
        -- We can access the response body in content attribute
        core.Debug(res.content)
    else
      core.Debug(err)
    end


    -- 3) Send response to client side
    http.response.create{status_code=200, content="Hello World"}:send(applet)

end

core.register_service("test", "http", main)

Naturally, you need to add your script to main haproxy configuration:

global
  ...
  lua-load test.lua

frontend test
  ...
  http-request use-service lua.test

haproxy-lua-http's People

Contributors

anezirovic avatar epicfilemcnulty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.