Code Monkey home page Code Monkey logo

neturl's Introduction

A Robust URL Parser and Builder for Lua

This small Lua library provides a few functions to parse URL with querystring and build new URL easily.

> url = require "net.url"

Querystring parser

The library supports brackets in querystrings, like PHP. It means you can use brackets to build multi-dimensional tables. The parsed querystring has a tostring() helper. As usual with Lua, if no index is specified, it starts from index 1.

> query = url.parseQuery("first=abc&a[]=123&a[]=false&b[]=str&c[]=3.5&a[]=last")
> = query
a[1]=123&a[2]=false&a[3]=last&b[1]=str&c[1]=3.5&first=abc
> = query.a[1]
123

URL parser

The library converts an URL to a table of the elements as described in RFC : scheme, host, path, etc.

> u = url.parse("http://www.example.com/test/?start=10")
> = u.scheme
http
> = u.host
www.example.com
> = u.path
/test/

URL normalization

> = url.parse("http://www.FOO.com:80///foo/../foo/./bar"):normalize()
http://www.foo.com/foo/bar

URL resolver

URL resolution follows the examples provided in the RFC 2396.

> = url.parse("http://a/b/c/d;p?q"):resolve("../../g")
http://a/g

Querystring builder

> u = url.parse("http://www.example.com")
> u.query.foo = "bar"
> = u
http://www.example.com/?foo=bar

Differences with luasocket/url.lua

  • Luasocket/url.lua can't parse http://www.example.com?url=net correctly because there are no path.
  • Luasocket/url.lua can't clean and normalize url, for example by removing default port, extra zero in port, empty authority, uppercase scheme, domain name.
  • Luasocket/url.lua doesn't parse the query string parameters.
  • Luasocket/url.lua is less compliant with RFC 2396 and will resolve http://a/b/c/d;p?q and : ../../../g to http://ag instead of http://a/g ../../../../g to http://a../g instead of http://a/g g;x=1/../y to http://a/b/c/g;x=1/../y instead of http://a/b/c/y /./g to http://a/./g instead of http://a/g g;x=1/./y to http://a/b/c/g;x=1/./y instead of http://a/b/c/g;x=1/y

neturl's People

Contributors

golgote avatar

Watchers

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