<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><generator uri="http://jekyllrb.com" version="3.1.6">Jekyll</generator><link href="https://daurnimator.github.io/rss" rel="self" type="application/atom+xml" /><link href="https://daurnimator.github.io/" rel="alternate" type="text/html" /><updated>2016-06-19T16:04:52+00:00</updated><id>https://daurnimator.github.io/</id><title>Daurnimator&#39;s Hovel</title><subtitle>A place for thoughts, rants and progress reports</subtitle><entry><title>Instant postgres</title><link href="https://daurnimator.github.io/post/2016/05/07/instant-postgres/" rel="alternate" type="text/html" title="Instant postgres" /><published>2016-05-07T11:10:27+00:00</published><updated>2016-05-07T11:10:27+00:00</updated><id>https://daurnimator.github.io/post/2016/05/07/instant-postgres</id><content type="html" xml:base="https://daurnimator.github.io/post/2016/05/07/instant-postgres/">&lt;p&gt;Sometimes I need a temporary postgres to try something out.
I keep this script around as &lt;code&gt;instantpg.sh&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/bash
trap &quot;rm -rf -- $PGDATA&quot; EXIT
export PGDATA=&quot;$(mktemp -d)&quot; PGPORT=1234
initdb
echo &quot;Connect with: psql -p \&quot;$PGPORT\&quot; -h localhost -d postgres&quot;
postgres -F -k &quot;&quot;
&lt;/code&gt;&lt;/pre&gt;</content><category term="postgres" /><category term="postgresql" /><category term="command line" /><category term="sql" /><summary>Sometimes I need a temporary postgres to try something out.
I keep this script around as instantpg.sh:</summary></entry><entry><title>Migrating from luasocket to lua-http</title><link href="https://daurnimator.github.io/post/2016/04/06/migrating-from-luasocket-to-lua-http/" rel="alternate" type="text/html" title="Migrating from luasocket to lua-http" /><published>2016-04-06T01:17:18+00:00</published><updated>2016-04-06T01:17:18+00:00</updated><id>https://daurnimator.github.io/post/2016/04/06/migrating-from-luasocket-to-lua-http</id><content type="html" xml:base="https://daurnimator.github.io/post/2016/04/06/migrating-from-luasocket-to-lua-http/">&lt;p&gt;I saw &lt;a href=&quot;https://github.com/brunoos/luasec/issues/72#issuecomment-205328635&quot;&gt;https://github.com/brunoos/luasec/issues/72#issuecomment-205328635&lt;/a&gt; and couldn&amp;rsquo;t resist writing &lt;a href=&quot;https://gist.github.com/is73/d50fa12718812e0f0c76c991030b8583&quot;&gt;the linked code&lt;/a&gt; to use &lt;a href=&quot;https://github.com/daurnimator/lua-http&quot;&gt;lua-http&lt;/a&gt; instead.&lt;/p&gt;

&lt;p&gt;&lt;!-- more --&gt;&lt;/p&gt;

&lt;p&gt;As the code was originally using luasocket&amp;rsquo;s http interface, it was straightforward to convert it to lua-http&amp;rsquo;s &lt;a href=&quot;https://daurnimator.github.io/lua-http/#http.compat.socket&quot;&gt;http.compat.socket&lt;/a&gt; module. This compatability interface provides the same API as luasocket&amp;rsquo;s &lt;code&gt;socket.http&lt;/code&gt; and luasec&amp;rsquo;s &lt;code&gt;ssl.https&lt;/code&gt; modules.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
local http = require &quot;http.compat.socket&quot; -- require &quot;socket.http&quot;
local https = http -- require &quot;ssl.https&quot;
local ltn12 = require &quot;ltn12&quot;
local string_sub = string.sub
local table_concat = table.concat
local function make_request(url, timeout)
    http.TIMEOUT = timeout
    http.USERAGENT = &quot;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36&quot;
    local b, c, h, s
    local tbody = {}
    local https_opt = {
        url = url,
        protocol = &quot;tlsv1&quot;,
        verify = &quot;none&quot;,
        headers = {
            [&quot;Accept&quot;] = &quot;*/*&quot;,
            [&quot;Accept-Language&quot;] = &quot;sk;q=0.8,en-US,en;q=0.6,cs;q=0.4&quot;,
            [&quot;Accept-Charset&quot;] = &quot;UTF-8;q=0.8,*;q=0.7&quot;,
        },
        sink = ltn12.sink.table(tbody),
        redirect = false,
    }
    local http_opt = {
        url = url,
        headers = {
            [&quot;Accept&quot;] = &quot;*/*&quot;,
            [&quot;Accept-Language&quot;] = &quot;sk;q=0.8,en-US,en;q=0.6,cs;q=0.4&quot;,
            [&quot;Accept-Charset&quot;] = &quot;UTF-8;q=0.8,*;q=0.7&quot;,
        },
        sink = ltn12.sink.table(tbody),
        redirect = false,
    }
    if string_sub(url, 1, 5) ~= &quot;https&quot; then
        _, c, h, s = http.request(http_opt)
    else
        _, c, h, s = https.request(https_opt)
    end
    -- make headers keys lowercase
    if h ~= nil then
        local h_tmp = {}
        for k, v in pairs(h) do h_tmp[k:lower()] = v end
        h = h_tmp
    end
    -- concat body parts
    b = table_concat(tbody)
    return {body = b, code = c, headers = h, status = s}
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, this function can be rewritten much more nicely using the &lt;a href=&quot;https://daurnimator.github.io/lua-http/#http.request&quot;&gt;&lt;code&gt;http.request&lt;/code&gt;&lt;/a&gt; module.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
local http_req = require &quot;http.request&quot;
local h1_reason_phrases = require &quot;http.h1_reason_phrases&quot;
local function make_request(url, timeout)
    local r = http_req.new_from_uri(url)
    r.headers:upsert(&quot;useragent&quot;, &quot;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36&quot;)
    r.headers:upsert(&quot;accept&quot;, &quot;*/*&quot;)
    r.headers:upsert(&quot;accept-language&quot;, &quot;sk;q=0.8,en-US,en;q=0.6,cs;q=0.4&quot;)
    r.headers:upsert(&quot;accept-charset&quot;, &quot;UTF-8;q=0.8,*;q=0.7&quot;)
    r.follow_redirects = false
    local headers, stream = r:go(timeout)
    if headers == nil then
        return nil, stream
    end
    local b, err = stream:get_body_as_string(timeout) -- XXX: use a deadline instead of a timeout?
    stream:shutdown() -- shutdown ASAP to free resources
    if b == nil then
        return nil, err
    end
    local c = headers:get(&quot;:status&quot;)
    local s = h1_reason_phrases[c] -- look up reason phrase for code
    c = tonumber(c, 10) or c -- the code might not be numeric
    -- convert from headers object to unordered table of key/value pairs
    local h = {}
    for name in headers:each() do
        if name ~= &quot;:status&quot; and h[name] == nil then
            h[name] = headers:get_comma_separated(name)
        end
    end
    return {body = b, code = c, headers = h, status = s}
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If the user has the flexibility to change the API of their &lt;code&gt;make_request&lt;/code&gt; function, they may wish to use the lua-http &lt;code&gt;http.headers&lt;/code&gt; object directly instead of transforming it to &lt;code&gt;code&lt;/code&gt; + table of header name =&amp;gt; value pairs.&lt;/p&gt;</content><category term="lua" /><category term="programming" /><category term="lua-http" /><category term="http" /><summary>I saw https://github.com/brunoos/luasec/issues/72#issuecomment-205328635 and couldn&amp;rsquo;t resist writing the linked code to use lua-http instead.</summary></entry><entry><title>Lua lib to send SMS with Telstra’s SMS API</title><link href="https://daurnimator.github.io/post/2016/02/28/lua-lib-to-send-sms-with-telstras-sms-api/" rel="alternate" type="text/html" title="Lua lib to send SMS with Telstra&#39;s SMS API" /><published>2016-02-28T11:14:52+00:00</published><updated>2016-02-28T11:14:52+00:00</updated><id>https://daurnimator.github.io/post/2016/02/28/lua-lib-to-send-sms-with-telstras-sms-api</id><content type="html" xml:base="https://daurnimator.github.io/post/2016/02/28/lua-lib-to-send-sms-with-telstras-sms-api/">&lt;a href=&quot;https://gist.github.com/daurnimator/b7733e4b4999029970d4&quot;&gt;Lua lib to send SMS with Telstra&#39;s SMS API&lt;/a&gt;&lt;br/&gt;&lt;p&gt;Wrote a lua library wrapper around the Telstra SMS API.&lt;/p&gt;</content><category term="lua" /><category term="programming" /><category term="sms" /><category term="telstra" /><category term="lua-http" /><summary>Lua lib to send SMS with Telstra&#39;s SMS APIWrote a lua library wrapper around the Telstra SMS API.</summary></entry><entry><title>lpeg_patterns v0.2</title><link href="https://daurnimator.github.io/post/2015/12/13/lpegpatterns-v02/" rel="alternate" type="text/html" title="lpeg_patterns v0.2" /><published>2015-12-13T14:36:46+00:00</published><updated>2015-12-13T14:36:46+00:00</updated><id>https://daurnimator.github.io/post/2015/12/13/lpegpatterns-v02</id><content type="html" xml:base="https://daurnimator.github.io/post/2015/12/13/lpegpatterns-v02/">&lt;p&gt;I&amp;rsquo;m happy to announce the 0.2 release of lpeg_patterns.&lt;/p&gt;

&lt;p&gt;&amp;ldquo;lpeg_patterns&amp;rdquo; is a collection of patterns I&amp;rsquo;ve written for various widely used formats.
Current sub-modules are: IPv4, IPv6, email addresses, phone numbers, uri.&lt;/p&gt;

&lt;p&gt;This release includes:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Fixed parsing of IPv6 addresses (thanks spc)&lt;/li&gt;
&lt;li&gt;IPv6 zone support&lt;/li&gt;
&lt;li&gt;Stricter uri matching (scheme is now compulsory)&lt;/li&gt;
&lt;li&gt;&amp;ldquo;reference&amp;rdquo; (i.e. relative) URI matching&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Homepage: &lt;a href=&quot;https://github.com/daurnimator/lpeg_patterns&quot;&gt;https://github.com/daurnimator/lpeg_patterns&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s available via luarocks: &lt;a href=&quot;https://luarocks.org/modules/daurnimator/lpeg_patterns&quot;&gt;https://luarocks.org/modules/daurnimator/lpeg_patterns&lt;/a&gt;&lt;/p&gt;</content><category term="lua" /><category term="programming" /><category term="lpeg" /><summary>I&amp;rsquo;m happy to announce the 0.2 release of lpeg_patterns.</summary></entry><entry><title>Testing pre-commit with git</title><link href="https://daurnimator.github.io/post/2015/12/04/testing-pre-commit-with-git/" rel="alternate" type="text/html" title="Testing pre-commit with git" /><published>2015-12-04T10:24:27+00:00</published><updated>2015-12-04T10:24:27+00:00</updated><id>https://daurnimator.github.io/post/2015/12/04/testing-pre-commit-with-git</id><content type="html" xml:base="https://daurnimator.github.io/post/2015/12/04/testing-pre-commit-with-git/">&lt;p&gt;It&amp;rsquo;s great to run tests on your code &lt;em&gt;before&lt;/em&gt; you commit it. It&amp;rsquo;s even better to make that happen automatically!&lt;/p&gt;

&lt;p&gt;git lets you run a script before a commit succeeds by creating what is known as a &amp;ldquo;pre-commit&amp;rdquo; hook; it&amp;rsquo;s simply an executable located at &lt;code&gt;.git/hooks/pre-commit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, there are a few gotchas: by default, git will just run it against your current (possibly dirty) checkout. To make sure you&amp;rsquo;re actually testing the code you&amp;rsquo;re about to commit, we can stash your other changes while the tests run.&lt;/p&gt;

&lt;p&gt;This brings up another issue: that popping a git stash that includes an index will often result in conflicts; we can solve this by using &lt;code&gt;git reset --hard&lt;/code&gt; before we pop from the stash.&lt;/p&gt;

&lt;p&gt;One last thing: we want to recover to the original directory state no matter what happens (e.g. maybe our test suite itself fails); so we use a bash &lt;code&gt;trap&lt;/code&gt; to ensure that our stash popping happens no-matter the exit path.&lt;/p&gt;

&lt;p&gt;With that all said, here is what I use as a pre-commit hook for my lua projects:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/bash
set -eufo pipefail

if [ -n &quot;$(git status -z)&quot; ]; then
    git stash -q --keep-index --include-untracked
    trap &quot;git reset -q --hard &amp;amp;&amp;amp; git stash pop -q --index&quot; EXIT
fi

echo &quot;## Running luacheck&quot;
luacheck .
echo

echo &quot;## Running tests with busted&quot;
busted
echo
&lt;/code&gt;&lt;/pre&gt;</content><category term="git" /><category term="lua" /><category term="testing" /><summary>It&amp;rsquo;s great to run tests on your code before you commit it. It&amp;rsquo;s even better to make that happen automatically!</summary></entry><entry><title>The Codist: Software Estimation Is A Crock</title><link href="https://daurnimator.github.io/post/2015/08/20/the-codist-software-estimation-is-a-crock/" rel="alternate" type="text/html" title="The Codist: Software Estimation Is A Crock" /><published>2015-08-20T03:13:10+00:00</published><updated>2015-08-20T03:13:10+00:00</updated><id>https://daurnimator.github.io/post/2015/08/20/the-codist-software-estimation-is-a-crock</id><content type="html" xml:base="https://daurnimator.github.io/post/2015/08/20/the-codist-software-estimation-is-a-crock/">&lt;a href=&quot;http://thecodist.com/article/software-estimation-is-a-crock&quot;&gt;The Codist: Software Estimation Is A Crock&lt;/a&gt;&lt;br/&gt;</content><category term="management" /><category term="agile" /><category term="scrum" /><summary>The Codist: Software Estimation Is A Crock</summary></entry><entry><title>Fifo.lua 0.2 Released</title><link href="https://daurnimator.github.io/post/2015/08/04/fifolua-02-released/" rel="alternate" type="text/html" title="Fifo.lua 0.2 Released" /><published>2015-08-04T13:09:41+00:00</published><updated>2015-08-04T13:09:41+00:00</updated><id>https://daurnimator.github.io/post/2015/08/04/fifolua-02-released</id><content type="html" xml:base="https://daurnimator.github.io/post/2015/08/04/fifolua-02-released/">&lt;a href=&quot;https://github.com/daurnimator/fifo.lua&quot;&gt;Fifo.lua 0.2 Released&lt;/a&gt;&lt;br/&gt;&lt;p&gt;Today I&amp;rsquo;m releasing verion 0.2 of &amp;ldquo;fifo.lua&amp;rdquo;.
A lua library that provides a fifo of lua objects.&lt;/p&gt;

&lt;h2&gt;Changes since 0.1&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;&lt;code&gt;:setempty&lt;/code&gt; now returns the fifo so that initialisation is simplified&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:peek&lt;/code&gt; returns 2nd value indicating if there was a value at the given index&lt;/li&gt;
&lt;li&gt;Remove undocumented &lt;code&gt;:iter&lt;/code&gt; and &lt;code&gt;:foreach&lt;/code&gt; methods&lt;/li&gt;
&lt;li&gt;Added &lt;code&gt;__len&lt;/code&gt; metamethod as alias for &lt;code&gt;:length&lt;/code&gt; method (only available in 5.2+)&lt;/li&gt;
&lt;li&gt;Fixed bug in &lt;code&gt;:remove&lt;/code&gt; method where the last element could not be removed&lt;/li&gt;
&lt;li&gt;Stricter argument checking&lt;/li&gt;
&lt;li&gt;Move documentation out of README into a &lt;a href=&quot;http://pandoc.org/&quot;&gt;pandoc&lt;/a&gt; compatible markdown file&lt;/li&gt;
&lt;li&gt;Dropped Lua 5.0 support&lt;/li&gt;
&lt;li&gt;Improved performance (especially in LuaJIT)&lt;/li&gt;
&lt;/ul&gt;</content><category term="lua" /><category term="programming" /><category term="fifo.lua" /><summary>Fifo.lua 0.2 ReleasedToday I&amp;rsquo;m releasing verion 0.2 of &amp;ldquo;fifo.lua&amp;rdquo;.
A lua library that provides a fifo of lua objects.</summary></entry><entry><title>daurnimator/lua-http</title><link href="https://daurnimator.github.io/post/2015/08/04/daurnimatorlua-http/" rel="alternate" type="text/html" title="daurnimator/lua-http" /><published>2015-08-04T12:39:50+00:00</published><updated>2015-08-04T12:39:50+00:00</updated><id>https://daurnimator.github.io/post/2015/08/04/daurnimatorlua-http</id><content type="html" xml:base="https://daurnimator.github.io/post/2015/08/04/daurnimatorlua-http/">&lt;a href=&quot;https://github.com/daurnimator/lua-http&quot;&gt;daurnimator/lua-http&lt;/a&gt;&lt;br/&gt;&lt;p&gt;Over the last few weeks, I&amp;rsquo;ve been working on new lua library in my free time.&lt;/p&gt;

&lt;p&gt;Announcing: &lt;strong&gt;&amp;ldquo;lua-http&amp;rdquo;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s a fresh take on a http library for lua.
Designed it with HTTP2 in mind from the start, it caters for both clients and servers.&lt;/p&gt;

&lt;p&gt;All network operations are non-blocking, yet also work outside of a coroutine. This allows for lua-http to be used in any lua project, application or script.&lt;/p&gt;</content><category term="lua" /><category term="programming" /><category term="http" /><category term="lua-http" /><summary>daurnimator/lua-httpOver the last few weeks, I&amp;rsquo;ve been working on new lua library in my free time.</summary></entry><entry><title>daurnimator/multi-protocol.lua</title><link href="https://daurnimator.github.io/post/2015/07/17/daurnimatormulti-protocollua/" rel="alternate" type="text/html" title="daurnimator/multi-protocol.lua" /><published>2015-07-17T14:46:37+00:00</published><updated>2015-07-17T14:46:37+00:00</updated><id>https://daurnimator.github.io/post/2015/07/17/daurnimatormulti-protocollua</id><content type="html" xml:base="https://daurnimator.github.io/post/2015/07/17/daurnimatormulti-protocollua/">&lt;a href=&quot;https://gist.github.com/daurnimator/60fc881c374b56cebd5f&quot;&gt;daurnimator/multi-protocol.lua&lt;/a&gt;&lt;br/&gt;&lt;p&gt;This snippet demonstrates listening for multiple protocol signatures on the one TCP port.
I left off concurrency to make it easier to understand.&lt;/p&gt;

&lt;p&gt;The TLS path will only work with the newly commited &lt;a href=&quot;https://github.com/wahern/cqueues/issues/73&quot;&gt;cqueues &lt;code&gt;:starttls&lt;/code&gt; pushback&lt;/a&gt;, which runs userspace buffered data through the incoming &lt;code&gt;BIO&lt;/code&gt; before reading off the socket.&lt;/p&gt;

&lt;p&gt;A couple of the protocol patterns I stole from prosody (where a similar idea is done via &amp;lsquo;mod_net_multiplex&amp;rsquo;).&lt;/p&gt;</content><category term="programming" /><category term="lua" /><category term="cqueues" /><summary>daurnimator/multi-protocol.luaThis snippet demonstrates listening for multiple protocol signatures on the one TCP port.
I left off concurrency to make it easier to understand.</summary></entry><entry><title>C bug of the day</title><link href="https://daurnimator.github.io/post/2015/07/10/c-bug-of-the-day/" rel="alternate" type="text/html" title="C bug of the day" /><published>2015-07-10T06:35:11+00:00</published><updated>2015-07-10T06:35:11+00:00</updated><id>https://daurnimator.github.io/post/2015/07/10/c-bug-of-the-day</id><content type="html" xml:base="https://daurnimator.github.io/post/2015/07/10/c-bug-of-the-day/">&lt;a href=&quot;https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826&quot;&gt;C bug of the day&lt;/a&gt;&lt;br/&gt;</content><category term="programming" /><category term="gcc" /><category term="bug" /><summary>C bug of the day</summary></entry></feed>
