pax_global_header00006660000000000000000000000064133225616130014514gustar00rootroot0000000000000052 comment=22684839237b2db6b0ba3f49636a442da0f6af50 lua-psl-0.3/000077500000000000000000000000001332256161300127335ustar00rootroot00000000000000lua-psl-0.3/.gitignore000066400000000000000000000000421332256161300147170ustar00rootroot00000000000000*.so *.o *.rock doc/*.pdf doc/*.3 lua-psl-0.3/.luacheckrc000066400000000000000000000000621332256161300150360ustar00rootroot00000000000000std = "min" files["spec"] = { std = "+busted"; } lua-psl-0.3/LICENSE000066400000000000000000000020731332256161300137420ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016-2018 Daurnimator Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. lua-psl-0.3/NEWS000066400000000000000000000007261332256161300134370ustar00rootroot000000000000000.3 - 2018-07-15 - Updates for libpsl 0.20 (PSL_TYPE_NO_STAR_RULE) - Updates for libpsl 0.19 (`count` functions can return nil, use of `psl_free_string`) 0.2 - 2017-03-30 - Fix crash when trying to load a psl file under lua 5.1 - Set `__name` in metatable in lua 5.1 (fixes broken test) - Bind new functions in libpsl 0.16 (`.dist_filename` and `.latest`) 0.1 - 2016-12-04 - Bindings to all exposed libpsl <= 0.15 symbols - Documentation via pandoc lua-psl-0.3/README.md000066400000000000000000000031361332256161300142150ustar00rootroot00000000000000# lua-psl Bindings to [libpsl](https://github.com/rockdaboot/libpsl), a C library that handles the Public Suffix List (PSL). The PSL is a list of domains where there may be sub-domains outside of the administrator's control. e.g. the administrator of '.com' does not manage 'github.com'. This list has found use in many internet technologies including: - preventing cross-domain cookie leakage - allowance of issuing wildcard TLS certificates More information can be found at [publicsuffix.org](https://publicsuffix.org/) ## Documentation Can be found at [https://daurnimator.github.io/lua-psl/](https://daurnimator.github.io/lua-psl/) ## Features - Compatible with Lua 5.1, 5.2, 5.3 and [LuaJIT](http://luajit.org/) ## Status - This project was started in late 2016 - Binds all functions available from libpsl up to the 0.19 release # Installation It's recommended to install lua-psl by using [luarocks](https://luarocks.org/). $ luarocks install psl ## Dependencies - [libpsl](https://github.com/rockdaboot/libpsl) >= 0.4.0 ### For running tests - [busted](http://olivinelabs.com/busted/) # Development ## Getting started - Clone the repo: ``` $ git clone https://github.com/daurnimator/lua-psl.git $ cd lua-psl ``` - Install your local copy: ``` $ luarocks make psl-scm-0.rockspec ``` - Run tests ([install tools first](#for-running-tests)) ``` $ busted ``` ## Generating documentation Documentation is written in markdown and intended to be consumed by [pandoc](http://pandoc.org/). See the `doc/` directory for more information. lua-psl-0.3/doc/000077500000000000000000000000001332256161300135005ustar00rootroot00000000000000lua-psl-0.3/doc/Makefile000066400000000000000000000012221332256161300151350ustar00rootroot00000000000000FILES = \ index.md all: lua-psl.html lua-psl.pdf lua-psl.3 lua-psl.html: template.html site.css metadata.yaml $(FILES) pandoc -o $@ -t html5 -s --toc --template=template.html --section-divs --self-contained -c site.css metadata.yaml $(FILES) lua-psl.pdf: metadata.yaml $(FILES) pandoc -o $@ -t latex -s --toc --toc-depth=2 -V documentclass=article -V classoption=oneside -V links-as-notes -V geometry=a4paper,includeheadfoot,margin=2.54cm metadata.yaml $(FILES) lua-psl.3: metadata.yaml $(FILES) pandoc -o $@ -t man -s metadata.yaml $(FILES) man: lua-psl.3 man -l $^ clean: rm -f lua-psl.html lua-psl.pdf lua-psl.3 .PHONY: all man install clean lua-psl-0.3/doc/README.md000066400000000000000000000004711332256161300147610ustar00rootroot00000000000000Documentation in this directory is intended to be converted to other formats using [pandoc](http://pandoc.org/). An online HTML version can be found at [https://daurnimator.github.io/lua-psl/](https://daurnimator.github.io/lua-psl/) The *Makefile* in this directory should be used to compile the documentation. lua-psl-0.3/doc/index.md000066400000000000000000000170651332256161300151420ustar00rootroot00000000000000# Introduction lua-psl is a set of [lua](https://www.lua.org) bindings to [libpsl](https://github.com/rockdaboot/libpsl), a C library that handles the Public Suffix List (PSL). The PSL is a list of domains where there may be sub-domains outside of the administrator's control. e.g. the administrator of '.com' does not manage 'github.com'. This list has found use in many internet technologies including: - preventing cross-domain cookie leakage - allowance of issuing wild-card TLS certificates More information can be found at [publicsuffix.org](https://publicsuffix.org/) # API ## psl library Load with `require "psl"` ### `psl.load_file(path)` {#psl.load_file} Loads a psl from the given file path. On success, returns a [psl object](#psl-object). On failure, returns `nil`. ### `psl.load_fp(file)` {#psl.load_fp} Loads a psl from the given [lua file object](https://www.lua.org/manual/5.3/manual.html#6.8). On success, returns a [psl object](#psl-object). On failure, returns `nil`. ### `psl.builtin()` {#psl.builtin} Returns the psl built in to libpsl. If it exists, returns a [psl object](#psl-object). If libpsl was compiled without one, returns `nil`. ### `psl.builtin_file_time()` {#psl.builtin_file_time} Returns the time-stamp of the file used to build the built in psl. If it exists, returns the time-stamp as an integer. If libpsl was compiled without one, returns `nil`. ### `psl.builtin_sha1sum()` {#psl.builtin_sha1sum} Returns the SHA1 checksum of the file used to build the built in psl. If it exists, returns the checksum as a string of lower-case hex digits. If libpsl was compiled without one, returns `nil`. ### `psl.builtin_filename()` {#psl.builtin_filename} Returns the file name of the file used to build the built in psl. If it exists, returns a string. If libpsl was compiled without one, returns `nil`. ### `psl.builtin_outdated()` {#psl.builtin_outdated} Returns a boolean indicating if the built-in data is older than the file currently at [`psl.builtin_filename()`](#psl.builtin_filename). Note: This function will not exist if lua-psl is compiled against libpsl 0.10 or earlier. ### `psl.dist_filename()` {#psl.dist_filename} Returns a string containing the path to your computer's default psl database location. If libpsl was not configured with one or was configured with an empty path, returns `nil`. Note: This function will not exist if lua-psl is compiled against libpsl 0.15 or earlier. ### `psl.latest(filename)` {#psl.latest} This function loads the the latest available PSL data from the following locations: 1. `filename` (application specific filename, may be `nil`) 2. default psl database location (as returned from [`psl.dist_filename()`](#psl.dist_filename)) 3. built-in PSL data (as returned from [`psl.builtin()`](#psl.builtin)) 4. location of built-in data (as returned from [`psl.builtin_filename()`](#psl.builtin_filename)) If none of the locations contain a valid psl object then returns `nil`. Note: This function will not exist if lua-psl is compiled against libpsl 0.15 or earlier. ### `psl.get_version()` {#psl.get_version} Returns a string containing the version of libpsl. ### `psl.check_version_number(version)` {#psl.check_version_number} Check if the given version number is at least the current library version number. Returns the current library version number if the given version number is at least the version of the library, otherwise returns `false`. If the `version` argument is missing, `nil` or `0` then returns the current library version number without performing a check. Note: This function will not exist if lua-psl is compiled against libpsl 0.10 or earlier. ### `psl.str_to_utf8lower(str, encoding, locale)` {#psl.str_to_utf8lower} Converts a string to UTF-8 lower-case + NFKC representation. `encoding` (optional) is the charset encoding of `str`. `locale` (optional) is the locale of `str`. On success, returns a string. On failure, returns `nil`. ### `psl.VERSION` {#psl.VERSION} The libpsl version as a string. Note: This constant will not exist if lua-psl is compiled against libpsl 0.10 or earlier. ### `psl.VERSION_NUMBER` {#psl.VERSION_NUMBER} The libpsl version as an integer. Note: This constant will not exist if lua-psl is compiled against libpsl 0.10 or earlier. ### `psl.VERSION_MAJOR` {#psl.VERSION_MAJOR} The libpsl major version as an integer. Note: This constant will not exist if lua-psl is compiled against libpsl 0.10 or earlier. ### `psl.VERSION_MINOR` {#psl.VERSION_MINOR} The libpsl minor version as an integer. Note: This constant will not exist if lua-psl is compiled against libpsl 0.10 or earlier. ### `psl.VERSION_PATCH` {#psl.VERSION_PATCH} The libpsl patch version as an integer. Note: This constant will not exist if lua-psl is compiled against libpsl 0.10 or earlier. ### `psl.TYPE` {#psl.TYPE} A table containing constants for [`psl:is_public_suffix()`](#psl:is_public_suffix). - `ANY` - `ICANN` - `PRIVATE` - `NO_STAR_RULE` (if compiled against libpsl 0.20 or newer) Note: This table will not exist if lua-psl is compiled against libpsl 0.11 or earlier. ## psl object ### `psl:free()` {#psl:free} Manually free the resources used by the psl object. Will be called automatically if the psl object is collected. ### `psl:is_public_suffix(domain, type)` {#psl:is_public_suffix} Checks if `domain` is on the public suffix list. `domain` should be a UTF-8 string. `type` (optional) should be one of the [`psl.TYPE`](#psl.TYPE) constants; the default behaviour is `psl.TYPE.ANY`. This argument is only supported if compiled against libpsl 0.12.0 or above. Returns a boolean. ### `psl:unregistrable_domain(domain)` {#psl:unregistrable_domain} `domain` should be a UTF-8 string. Returns the longest public suffix part of `domain` as a string. If `domain` does not contain a public suffix, returns `nil`. ### `psl:registrable_domain(domain)` {#psl:registrable_domain} `domain` should be a UTF-8 string. Returns the shortest private suffix part of `domain` as a string. If `domain` does not contain a private suffix, returns `nil`. ### `psl:suffix_count()` {#psl:suffix_count} Returns the number of public suffixes as an integer. When the count is unknown, returns `nil`. ### `psl:suffix_exception_count()` {#psl:suffix_exception_count} Returns the number of public suffix exceptions as an integer. When the count is unknown, returns `nil`. ### `psl:suffix_wildcard_count()` {#psl:suffix_wildcard_count} Returns the number of public suffix wild-cards as an integer. When the count is unknown, returns `nil`. Note: This function will not exist if lua-psl is compiled against libpsl 0.10 or earlier. ### `psl:is_cookie_domain_acceptable(hostname, cookie_domain)` {#psl:is_cookie_domain_acceptable} Checks whether `cookie_domain` is an acceptable cookie domain value for the request `hostname`. `hostname` and `cookie_domain` should be UTF-8 strings. Returns a boolean. #### Example {#psl:is_cookie_domain_acceptable-example} ```lua local psl = assert(require "psl".builtin()) assert(psl:is_cookie_domain_acceptable("www.example.com", "example.com")) assert(not psl:is_cookie_domain_acceptable("example.com", "com")) ``` # Links - [Github](https://github.com/daurnimator/lua-psl) - [Issue tracker](https://github.com/daurnimator/lua-psl/issues) - [libpsl](https://github.com/rockdaboot/libpsl) - [publicsuffix.org](https://publicsuffix.org/) lua-psl-0.3/doc/metadata.yaml000066400000000000000000000001441332256161300161430ustar00rootroot00000000000000 --- title: lua-psl subtitle: Lua bindings to libpsl author: Daurnimator ... lua-psl-0.3/doc/site.css000066400000000000000000000037631332256161300151670ustar00rootroot00000000000000* { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box } html, body { height: 100% } article, aside, figure, footer, header, hgroup, menu, nav, section { display: block } body { margin: 0 } h1, h2, h3 { margin: 1rem 0 } h4, h5, h6, ul, ol, dl, blockquote, address, p, figure { margin: 0 0 1rem 0 } img { max-width: 100% } h1, h2, h3, h4, h5, h6 { font-weight: 700 } h1 { font-size: 2.5rem; line-height: 3rem } h2 { font-size: 1.5rem; line-height: 2rem } h3 { font-size: 1.25rem; line-height: 1.5rem } h4, h5, h6 { font-size: 1rem; line-height: 1.25rem } hr { border: 0; border-bottom: 1px solid; margin-top: -1px; margin-bottom: 1rem } a:hover { color: inherit } small { font-size: .875rem } ul, ol { padding-left: 1rem } ul ul, ul ol, ol ol, ol ul { margin: 0 } dt { font-weight: 700 } dd { margin: 0 } blockquote { border-left: 1px solid; padding-left: 1rem } address { font-style: normal } html { color: #333; font: 100%/1.5 Avenir, 'Helvetica Neue', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; background: #FFF; } a { color: #999; text-decoration: none; transition: color 0.3s; } a > h1, a > h2, a > h3 { color: #333; } body > * { padding: 0 1rem; } .subtitle { font-size: 1rem; line-height: 1.5rem } .author { display: none } @media screen and (min-width: 55rem) { .meta { position: fixed; width: 20rem; height: 100%; overflow: auto; background: #FFF; z-index: 1; } main { display: block; /* required for e.g. konqueror */ margin-left: 20rem; overflow: auto; } } @media print { section.level1 { page-break-inside: avoid } nav a::after { content: leader('.') target-counter(attr(href url), page, decimal) } } lua-psl-0.3/doc/template.html000066400000000000000000000030151332256161300162000ustar00rootroot00000000000000 $for(author-meta)$ $endfor$ $if(date-meta)$ $endif$ $if(keywords)$ $endif$ $if(title-prefix)$$title-prefix$ – $endif$$pagetitle$ $if(quotes)$ $endif$ $if(highlighting-css)$ $endif$ $for(css)$ $endfor$ $if(math)$ $math$ $endif$ $for(header-includes)$ $header-includes$ $endfor$ $for(include-before)$ $include-before$ $endfor$
$if(title)$

$title$

$if(subtitle)$

$subtitle$

$endif$ $for(author)$

$author$

$endfor$ $if(date)$

$date$

$endif$
$endif$ $if(toc)$ $endif$
$body$
$for(include-after)$ $include-after$ $endfor$ lua-psl-0.3/psl-0.3-0.rockspec000066400000000000000000000021401332256161300157140ustar00rootroot00000000000000package = "psl" version = "0.3-0" source = { url = "https://github.com/daurnimator/lua-psl/archive/v0.3.zip"; dir = "lua-psl-0.3"; } description = { summary = "Bindings to libpsl, a C library that handles the Public Suffix List (PSL)"; detailed = [[ Bindings to libpsl, a C library that handles the Public Suffix List (PSL). The PSL is a list of domains where there may be sub-domains outside of the administrator's control. e.g. the administrator of '.com' does not manage 'github.com'. This list has found use in many internet technologies including: - preventing cross-domain cookie leakage - allowance of issuing wildcard TLS certificates More information can be found at https://publicsuffix.org/]]; homepage = "https://github.com/daurnimator/lua-psl"; license = "MIT"; } dependencies = {} external_dependencies = { PSL = { header = "libpsl.h"; library = "psl"; }; } build = { type = "builtin"; modules = { psl = { sources = "psl/psl.c"; libraries = { "psl"; }; defines = { }; incdirs = { "$(PSL_INCDIR)"; }; libdirs = { "$(PSL_LIBDIR)"; }; }; }; } lua-psl-0.3/psl/000077500000000000000000000000001332256161300135315ustar00rootroot00000000000000lua-psl-0.3/psl/psl.c000066400000000000000000000264311332256161300145010ustar00rootroot00000000000000#include /* FILE* */ #include /* free() */ #include /* time_t */ #include #include #include #define LUAPSL_NAME "lua-psl" #define LUAPSL_DESCRIPTION "Bindings to libpsl" #define LUAPSL_VERSION "0.3" /* compatibility with lua 5.1 */ #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 #define luaL_newmetatable(L, tn) (luaL_newmetatable(L, tn) ? (lua_pushstring(L, tn), lua_setfield(L, -2, "__name"), 1) : 0) #define luaL_setmetatable luapslL_setmetatable static void luaL_setmetatable (lua_State *L, const char *tname) { luaL_checkstack(L, 1, "not enough stack slots"); luaL_getmetatable(L, tname); lua_setmetatable(L, -2); } #define luaL_newlibtable(L, l) (lua_createtable(L, 0, sizeof(l)/sizeof(*(l))-1)) #define luaL_newlib(L, l) (luaL_newlibtable(L, l), luaL_register(L, NULL, l)) #endif /* compatibility with lua 5.1 *and* 5.2 */ #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502 #define lua_getfield(L, i, k) (lua_getfield(L, i, k), lua_type(L, -1)) #define luaL_getmetafield(L, o, e) (luaL_getmetafield(L, o, e) ? lua_type(L, -1) : LUA_TNIL) #endif static FILE *luapsl_checkFILE(lua_State *L, int idx) { #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 return *(FILE**)luaL_checkudata(L, idx, "FILE*"); #else luaL_Stream *stream = luaL_checkudata(L, idx, LUA_FILEHANDLE); return stream->f; #endif } static const psl_ctx_t **luapsl_preppslctx(lua_State *L) { const psl_ctx_t **ud = lua_newuserdata(L, sizeof(psl_ctx_t*)); *ud = NULL; luaL_setmetatable(L, "psl_ctx_t*"); return ud; } static void luapsl_pushpslctx(lua_State *L, const psl_ctx_t *psl) { if (psl == NULL) { lua_pushnil(L); } else { const psl_ctx_t **ud = luapsl_preppslctx(L); *ud = psl; } } static const psl_ctx_t *luapsl_checkpslctx(lua_State *L, int idx) { const psl_ctx_t **ud = luaL_checkudata(L, idx, "psl_ctx_t*"); return *ud; } static const psl_ctx_t *luapsl_checkpslctxnotnull(lua_State *L, int idx) { const psl_ctx_t *psl = luapsl_checkpslctx(L, idx); luaL_argcheck(L, psl != NULL, idx, "psl_ctx_t* is freed"); return psl; } /* A __tostring like 5.3.4 */ static int luapsl__tostring(lua_State *L) { int tt = luaL_getmetafield(L, 1, "__name"); const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : luaL_typename(L, 1); lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, 1)); return 1; } static int luapsl_load_file(lua_State *L) { const char *filename = luaL_checkstring(L, 1); const psl_ctx_t **ud = luapsl_preppslctx(L); *ud = psl_load_file(filename); if (*ud == NULL) { lua_pushnil(L); } return 1; } static int luapsl_load_fp(lua_State *L) { /* psl_load_fp doesn't hold onto the FILE*, so no need to e.g. stow away a reference */ FILE *file = luapsl_checkFILE(L, 1); const psl_ctx_t **ud = luapsl_preppslctx(L); *ud = psl_load_fp(file); if (*ud == NULL) { lua_pushnil(L); } return 1; } /* builtin is cached in upvalue */ static int luapsl_builtin(lua_State *L) { lua_pushvalue(L, lua_upvalueindex(1)); return 1; } static int luapsl_free(lua_State *L) { psl_ctx_t **ud = luaL_checkudata(L, 1, "psl_ctx_t*"); if (*ud != NULL && *ud != psl_builtin()) { psl_free(*ud); *ud = NULL; } return 0; } static int luapsl_is_public_suffix(lua_State *L) { const psl_ctx_t *psl = luapsl_checkpslctxnotnull(L, 1); const char *domain = luaL_checkstring(L, 2); #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x000c00 int type = luaL_optinteger(L, 3, PSL_TYPE_ANY); lua_pushboolean(L, psl_is_public_suffix2(psl, domain, type)); #else luaL_argcheck(L, lua_isnoneornil(L, 3), 3, "'type' argument only supported in libpsl >= 0.12.0"); lua_pushboolean(L, psl_is_public_suffix(psl, domain)); #endif return 1; } static int luapsl_unregistrable_domain(lua_State *L) { const psl_ctx_t *psl = luapsl_checkpslctxnotnull(L, 1); const char *domain = luaL_checkstring(L, 2); const char *res = psl_unregistrable_domain(psl, domain); lua_pushstring(L, res); return 1; } static int luapsl_registrable_domain(lua_State *L) { const psl_ctx_t *psl = luapsl_checkpslctxnotnull(L, 1); const char *domain = luaL_checkstring(L, 2); const char *res = psl_registrable_domain(psl, domain); lua_pushstring(L, res); return 1; } static int luapsl_suffix_count(lua_State *L) { const psl_ctx_t *psl = luapsl_checkpslctxnotnull(L, 1); int count = psl_suffix_count(psl); if (count < 0) { lua_pushnil(L); } else { lua_pushinteger(L, count); } return 1; } static int luapsl_suffix_exception_count(lua_State *L) { const psl_ctx_t *psl = luapsl_checkpslctxnotnull(L, 1); int count = psl_suffix_exception_count(psl); if (count < 0) { lua_pushnil(L); } else { lua_pushinteger(L, count); } return 1; } #ifdef PSL_VERSION_NUMBER static int luapsl_suffix_wildcard_count(lua_State *L) { const psl_ctx_t *psl = luapsl_checkpslctxnotnull(L, 1); int count = psl_suffix_wildcard_count(psl); if (count < 0) { lua_pushnil(L); } else { lua_pushinteger(L, count); } return 1; } #endif static int luapsl_builtin_file_time(lua_State *L) { time_t t = psl_builtin_file_time(); if (t == 0) { lua_pushnil(L); } else { lua_pushinteger(L, t); } return 1; } static int luapsl_builtin_sha1sum(lua_State *L) { lua_pushstring(L, psl_builtin_sha1sum()); return 1; } static int luapsl_builtin_filename(lua_State *L) { lua_pushstring(L, psl_builtin_filename()); return 1; } #ifdef PSL_VERSION_NUMBER static int luapsl_builtin_outdated(lua_State *L) { lua_pushboolean(L, psl_builtin_outdated()); return 1; } #endif static int luapsl_is_cookie_domain_acceptable(lua_State *L) { const psl_ctx_t *psl = luapsl_checkpslctxnotnull(L, 1); const char *hostname = luaL_checkstring(L, 2); const char *cookie_domain = luaL_checkstring(L, 3); lua_pushboolean(L, psl_is_cookie_domain_acceptable(psl, hostname, cookie_domain)); return 1; } static int luapsl_dist_filename(lua_State *L) { const char *dist_filename = psl_dist_filename(); if (*dist_filename == '\0') { lua_pushnil(L); } else { lua_pushstring(L, dist_filename); } return 1; } static int luapsl_latest(lua_State *L) { const char *filename = luaL_optstring(L, 1, NULL); const psl_ctx_t **ud = luapsl_preppslctx(L); *ud = psl_latest(filename); if (*ud == NULL) { lua_pushnil(L); } else if (*ud == psl_builtin()) { *ud = NULL; lua_pushvalue(L, lua_upvalueindex(1)); } return 1; } static int luapsl_get_version(lua_State *L) { lua_pushstring(L, psl_get_version()); return 1; } #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x000b00 /* Returns the library version number if the given version number is at least the version of the library, else return 0; If the argument is 0, the function returns the library version number without performing a check. */ static int luapsl_check_version_number(lua_State *L) { int minversion = luaL_optinteger(L, 1, 0); int res = psl_check_version_number(minversion); if (res == 0) { lua_pushboolean(L, 0); } else { lua_pushinteger(L, res); } return 1; } #endif static int luapsl_str_to_utf8lower(lua_State *L) { const char *str = luaL_checkstring(L, 1); const char *encoding = luaL_optstring(L, 2, NULL); const char *locale = luaL_optstring(L, 3, NULL); char **lower = lua_newuserdata(L, sizeof(char*)); lua_pushvalue(L, lua_upvalueindex(1)); lua_setmetatable(L, -2); psl_error_t res = psl_str_to_utf8lower(str, encoding, locale, lower); if(res == PSL_SUCCESS) { lua_pushstring(L, *lower); } #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001300 psl_free_string(*lower); #else free(*lower); #endif *lower = NULL; switch(res) { case PSL_SUCCESS: return 1; case PSL_ERR_INVALID_ARG: return luaL_argerror(L, 1, "string invalid"); case PSL_ERR_CONVERTER: /* Failed to open the unicode converter with name encoding */ case PSL_ERR_TO_UTF16: /* Failed to convert str to unicode */ case PSL_ERR_TO_LOWER: /* Failed to convert unicode to lowercase */ case PSL_ERR_TO_UTF8: /* Failed to convert unicode to UTF-8 */ default: lua_pushnil(L); return 1; } } static int boxed_pointer__gc(lua_State *L) { void **ud = lua_touserdata(L, 1); #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001300 psl_free_string(*ud); #else free(*ud); #endif *ud = NULL; return 0; } int luaopen_psl(lua_State *L) { static const luaL_Reg lib[] = { {"load_file", luapsl_load_file}, {"load_fp", luapsl_load_fp}, {"builtin_file_time", luapsl_builtin_file_time}, {"builtin_sha1sum", luapsl_builtin_sha1sum}, {"builtin_filename", luapsl_builtin_filename}, #ifdef PSL_VERSION_NUMBER {"builtin_outdated", luapsl_builtin_outdated}, #endif #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000 {"dist_filename", luapsl_dist_filename}, #endif {"get_version", luapsl_get_version}, #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x000b00 {"check_version_number", luapsl_check_version_number}, #endif {NULL, NULL} }; static const luaL_Reg psl_methods[] = { {"free", luapsl_free}, {"is_public_suffix", luapsl_is_public_suffix}, {"unregistrable_domain", luapsl_unregistrable_domain}, {"registrable_domain", luapsl_registrable_domain}, {"suffix_count", luapsl_suffix_count}, {"suffix_exception_count", luapsl_suffix_exception_count}, #ifdef PSL_VERSION_NUMBER {"suffix_wildcard_count", luapsl_suffix_wildcard_count}, #endif {"is_cookie_domain_acceptable", luapsl_is_cookie_domain_acceptable}, {NULL, NULL} }; /* create psl metatable */ luaL_newmetatable(L, "psl_ctx_t*"); lua_pushcfunction(L, luapsl_free); lua_setfield(L, -2, "__gc"); lua_pushcfunction(L, luapsl__tostring); lua_setfield(L, -2, "__tostring"); luaL_newlib(L, psl_methods); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newlib(L, lib); lua_pushliteral(L, LUAPSL_NAME); lua_setfield(L, -2, "_NAME"); lua_pushliteral(L, LUAPSL_DESCRIPTION); lua_setfield(L, -2, "_DESCRIPTION"); #ifdef LUAPSL_VERSION lua_pushliteral(L, LUAPSL_VERSION); lua_setfield(L, -2, "_VERSION"); #endif #ifdef PSL_VERSION_NUMBER lua_pushliteral(L, PSL_VERSION); lua_setfield(L, -2, "VERSION"); lua_pushinteger(L, PSL_VERSION_NUMBER); lua_setfield(L, -2, "VERSION_NUMBER"); lua_pushinteger(L, PSL_VERSION_MAJOR); lua_setfield(L, -2, "VERSION_MAJOR"); lua_pushinteger(L, PSL_VERSION_MINOR); lua_setfield(L, -2, "VERSION_MINOR"); lua_pushinteger(L, PSL_VERSION_PATCH); lua_setfield(L, -2, "VERSION_PATCH"); #endif #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x000c00 /* constants for psl_is_public_suffix2 */ lua_createtable(L, 0, 3); lua_pushinteger(L, PSL_TYPE_ANY); lua_setfield(L, -2, "ANY"); lua_pushinteger(L, PSL_TYPE_ICANN); lua_setfield(L, -2, "ICANN"); lua_pushinteger(L, PSL_TYPE_PRIVATE); lua_setfield(L, -2, "PRIVATE"); #ifdef PSL_TYPE_NO_STAR_RULE lua_pushinteger(L, PSL_TYPE_NO_STAR_RULE); lua_setfield(L, -2, "NO_STAR_RULE"); #endif lua_setfield(L, -2, "TYPE"); #endif /* cache builtin as upvalue so same pointer is returned every time */ luapsl_pushpslctx(L, psl_builtin()); #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000 lua_pushvalue(L, -1); lua_pushcclosure(L, luapsl_latest, 1); lua_setfield(L, -3, "latest"); #endif lua_pushcclosure(L, luapsl_builtin, 1); lua_setfield(L, -2, "builtin"); /* special metatable for boxed pointers that need to be freed */ lua_createtable(L, 0, 1); lua_pushcfunction(L, boxed_pointer__gc); lua_setfield(L, -2, "__gc"); lua_pushcclosure(L, luapsl_str_to_utf8lower, 1); lua_setfield(L, -2, "str_to_utf8lower"); return 1; } lua-psl-0.3/spec/000077500000000000000000000000001332256161300136655ustar00rootroot00000000000000lua-psl-0.3/spec/psl_spec.lua000066400000000000000000000056511332256161300162070ustar00rootroot00000000000000describe("psl", function() local psl_lib = require "psl" local function newer_than(x) if psl_lib.VERSION_NUMBER and psl_lib.VERSION_NUMBER >= x then return it end return pending end it("builtin returns same object each time", function() assert.equal(psl_lib.builtin(), psl_lib.builtin()) end) it("__tostring works", function() local psl = psl_lib.builtin() local str = tostring(psl) assert.same("psl_ctx_t*: 0x", str:match("^.-0x")) end); newer_than(0x001000)("has working psl_lib.dist_filename()", function() local dist_filename = psl_lib.dist_filename() if dist_filename then assert.same("string", type(dist_filename)) else assert.same(nil, dist_filename) end end); it("has working psl_lib.get_version()", function() assert.same("string", type(psl_lib.get_version())) end); newer_than(0x000b00)("has working psl_lib.check_version_number()", function() local version = psl_lib.check_version_number() assert.same("number", type(version)) assert.same(false, psl_lib.check_version_number(version - 1)) end); newer_than(0x000c00)("has psl_lib.TYPE field", function() assert.same("table", type(psl_lib.TYPE)) assert.same("number", type(psl_lib.TYPE.ANY)) end) describe("psl_lib.str_to_utf8lower()", function() it("works", function() assert.same("foo.com", psl_lib.str_to_utf8lower("foo.com")) assert.same("foo.com", psl_lib.str_to_utf8lower("FOO.com")) end) it("throws error on invalid arguments", function() assert.has.errors(function() psl_lib.str_to_utf8lower() end) assert.has.errors(function() psl_lib.str_to_utf8lower(nil) end) assert.has.errors(function() psl_lib.str_to_utf8lower({}) end) assert.has.errors(function() psl_lib.str_to_utf8lower("foo", {}) end) assert.has.errors(function() psl_lib.str_to_utf8lower("foo", nil, {}) end) end) end) it("can read the most basic of files", function() local file = io.tmpfile() assert(file:write("foo.bar")) assert(file:seek("set", 0)) local psl = assert(psl_lib.load_fp(file)) assert.same(1, psl:suffix_count()) assert.same(0, psl:suffix_exception_count()) assert.same(true, psl:is_public_suffix("bar")) assert.same(true, psl:is_public_suffix("foo.bar")) assert.same(false, psl:is_public_suffix("other.bar")) assert.same(false, psl:is_public_suffix("sub.foo.bar")) if psl_lib.VERSION then assert.same(0, psl:suffix_wildcard_count()) end assert.same(true, psl:is_cookie_domain_acceptable("foo.bar", "foo.bar")) assert.same(false, psl:is_cookie_domain_acceptable("other.bar", "foo.bar")) assert.same(false, psl:is_cookie_domain_acceptable("foo.bar", "other.bar")) assert.same(false, psl:is_cookie_domain_acceptable("sub.foo.bar", "foo.bar")) assert.same(true, psl:is_cookie_domain_acceptable("sub.foo.bar", "sub.foo.bar")) assert.same(true, psl:is_cookie_domain_acceptable("qux.sub.foo.bar", "sub.foo.bar")) end); newer_than(0x001000)("has working psl_lib.latest()", function() psl_lib.latest() end); end)