http

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2024 License: BSD-3-Clause Imports: 35 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accept

func Accept(L *lua.LState) int

Accept lua http_server_ud:accept() returns request_table, http_server_response_writer_ud

Example

http:server()

package main

import (
	"log"

	http "github.com/vadv/gopher-lua-libs/http"
	plugin "github.com/vadv/gopher-lua-libs/plugin"

	lua "github.com/yuin/gopher-lua"
)

func main() {
	state := lua.NewState()
	http.Preload(state)
	plugin.Preload(state)
	source := `
    local http = require("http")
    local plugin = require("plugin")

    local server, err = http.server("127.0.0.1:1999")
    if err then error(err) end

    local client_plugin = [[
        local time = require("time")
        local http = require("http")
        time.sleep(1)
        local client = http.client({timeout=1})
        local request, err = http.request("GET", "http://127.0.0.1:1999/get/url?param1=value1")
        if err then error(err) end
        client:do_request(request)
    ]]

    local client_plugin = plugin.do_string(client_plugin)
    client_plugin:run()

    local request, response = server:accept()
    print("host: "..request.host)
    print("method: "..request.method)
    -- print("referer: "..request.referer)
    print("proto: "..request.proto)
    print("request_uri: "..request.request_uri)
    print("user_agent: "..request.user_agent)
    -- print(request.remote_addr)
    print("header: Accept-Encoding="..request.headers["Accept-Encoding"])
    for k, v in pairs(request.query) do
      print("query: "..k.."="..v)
    end

    local body, err = request.body()
    if err then error(err) end
    print("body:", body)

    response:code(200) -- write code
    response:header("Content-Type", "application/json") -- write header
    response:write("ok")
    response:done()

    client_plugin:stop()
`
	if err := state.DoString(source); err != nil {
		log.Fatal(err.Error())
	}
}
Output:

host: 127.0.0.1:1999
method: GET
proto: HTTP/1.1
request_uri: /get/url?param1=value1
user_agent: gopher-lua
header: Accept-Encoding=gzip
query: param1=value1
body:

func Done

func Done(L *lua.LState) int

Done lua http_server_response_writer_ud:done()

func HandleFile

func HandleFile(L *lua.LState) int

HandleFile lua http_server_ud:handler_file(filename)

func HandleString

func HandleString(L *lua.LState) int

HandleString lua http_server_ud:handler_string(body)

func Header(L *lua.LState) int

Header lua http_server_response_writer_ud:header(key, value)

func HeaderCode

func HeaderCode(L *lua.LState) int

HeaderCode lua http_server_response_writer_ud:code(number)

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func New

func New(L *lua.LState) int

http.server(bind, handler) returns (user data, error)

func NewRequest

func NewRequest(L *lua.LState, req *http.Request) *lua.LTable

NewRequest return lua table with http.Request representation

func NewWriter

func NewWriter(L *lua.LState, w http.ResponseWriter, req *http.Request, done chan bool) *lua.LUserData

NewWriter return lua userdata with luaWriter

func Preload

func Preload(L *lua.LState)

Preload adds http_server to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local http_server = require("http_server")

func Redirect

func Redirect(L *lua.LState) int

Redirect lua http_server_response_writer_ud:redirect(url, code) return err

func ServeStaticFiles

func ServeStaticFiles(L *lua.LState) int

ServeStaticFiles lua http:serve_static("directory", ":port") return err

func Write

func Write(L *lua.LState) int

Write lua http_server_response_writer_ud:write(string) return (number, err)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL