gluaurl

October 21, 2016 ยท View on GitHub

gluahttp provides an easy way to parse and build URLs from within GopherLua.

Installation

go get github.com/cjoudrey/gluaurl

Usage

import "github.com/yuin/gopher-lua"
import "github.com/cjoudrey/gluaurl"

func main() {
    L := lua.NewState()
    defer L.Close()

    L.PreloadModule("url", gluaurl.Loader)

    if err := L.DoString(`

        local url = require("url")

        parsed_url = url.parse("http://example.com/")

        print(parsed_url.host)

    `); err != nil {
        panic(err)
    }
}

API

url.parse(url)

Parse URL into a table of key/value components.

Attributes

NameTypeDescription
urlStringURL to parsed

Returns

Table with parsed URL or (nil, error message)

NameTypeDescription
schemeStringScheme of the URL
usernameStringUsername
passwordStringPassword
hostStringHost and port of the URL
pathStringPath
queryStringQuery string
fragmentStringFragment

url.build(options)

Assemble a URL string from a table of URL components.

Attributes

NameTypeDescription
optionsTableTable with URL components, see url.parse for list of valid components

Returns

String

url.build_query_string(query_params)

Assemble table of query string parameters into a string.

Attributes

NameTypeDescription
query_paramsTableTable with query parameters

Returns

String

url.resolve(from, to)

Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.

NameTypeDescription
fromStringbase URL
toStringhref URL

Returns

String or (nil, error message)