diff options
Diffstat (limited to '')
| -rw-r--r-- | samples/README | 90 | ||||
| -rw-r--r-- | samples/b64.lua (renamed from etc/b64.lua) | 0 | ||||
| -rw-r--r-- | samples/cddb.lua | 2 | ||||
| -rw-r--r-- | samples/check-links.lua (renamed from etc/check-links.lua) | 0 | ||||
| -rw-r--r-- | samples/check-memory.lua (renamed from etc/check-memory.lua) | 0 | ||||
| -rw-r--r-- | samples/cookie.lua (renamed from etc/cookie.lua) | 22 | ||||
| -rw-r--r-- | samples/dict.lua (renamed from etc/dict.lua) | 0 | ||||
| -rw-r--r-- | samples/dispatch.lua (renamed from etc/dispatch.lua) | 0 | ||||
| -rw-r--r-- | samples/eol.lua (renamed from etc/eol.lua) | 0 | ||||
| -rw-r--r-- | samples/forward.lua (renamed from etc/forward.lua) | 0 | ||||
| -rw-r--r-- | samples/get.lua (renamed from etc/get.lua) | 2 | ||||
| -rw-r--r-- | samples/links (renamed from etc/links) | 0 | ||||
| -rw-r--r-- | samples/lp.lua (renamed from etc/lp.lua) | 0 | ||||
| -rw-r--r-- | samples/qp.lua (renamed from etc/qp.lua) | 0 | ||||
| -rw-r--r-- | samples/tftp.lua (renamed from etc/tftp.lua) | 0 |
15 files changed, 100 insertions, 16 deletions
diff --git a/samples/README b/samples/README index e63a6f5..4ee06b6 100644 --- a/samples/README +++ b/samples/README | |||
| @@ -1,11 +1,95 @@ | |||
| 1 | This directory contains some sample programs using | 1 | This directory contains some sample programs using |
| 2 | LuaSocket. This code is not supported. | 2 | LuaSocket. This code is not supported. |
| 3 | 3 | ||
| 4 | tftp.lua -- Trivial FTP client | ||
| 5 | |||
| 6 | This module implements file retrieval by the TFTP protocol. | ||
| 7 | Its main use was to test the UDP code, but since someone | ||
| 8 | found it usefull, I turned it into a module that is almost | ||
| 9 | official (no uploads, yet). | ||
| 10 | |||
| 11 | dict.lua -- Dict client | ||
| 12 | |||
| 13 | The dict.lua module started with a cool simple client | ||
| 14 | for the DICT protocol, written by Luiz Henrique Figueiredo. | ||
| 15 | This new version has been converted into a library, similar | ||
| 16 | to the HTTP and FTP libraries, that can be used from within | ||
| 17 | any luasocket application. Take a look on the source code | ||
| 18 | and you will be able to figure out how to use it. | ||
| 19 | |||
| 20 | lp.lua -- LPD client library | ||
| 21 | |||
| 22 | The lp.lua module implements the client part of the Line | ||
| 23 | Printer Daemon protocol, used to print files on Unix | ||
| 24 | machines. It is courtesy of David Burgess! See the source | ||
| 25 | code and the lpr.lua in the examples directory. | ||
| 26 | |||
| 27 | b64.lua | ||
| 28 | qp.lua | ||
| 29 | eol.lua | ||
| 30 | |||
| 31 | These are tiny programs that perform Base64, | ||
| 32 | Quoted-Printable and end-of-line marker conversions. | ||
| 33 | |||
| 34 | get.lua -- file retriever | ||
| 35 | |||
| 36 | This little program is a client that uses the FTP and | ||
| 37 | HTTP code to implement a command line file graber. Just | ||
| 38 | run | ||
| 39 | |||
| 40 | lua get.lua <remote-file> [<local-file>] | ||
| 41 | |||
| 42 | to download a remote file (either ftp:// or http://) to | ||
| 43 | the specified local file. The program also prints the | ||
| 44 | download throughput, elapsed time, bytes already downloaded | ||
| 45 | etc during download. | ||
| 46 | |||
| 47 | check-memory.lua -- checks memory consumption | ||
| 48 | |||
| 49 | This is just to see how much memory each module uses. | ||
| 50 | |||
| 51 | dispatch.lua -- coroutine based dispatcher | ||
| 52 | |||
| 53 | This is a first try at a coroutine based non-blocking | ||
| 54 | dispatcher for LuaSocket. Take a look at 'check-links.lua' | ||
| 55 | and at 'forward.lua' to see how to use it. | ||
| 56 | |||
| 57 | check-links.lua -- HTML link checker program | ||
| 58 | |||
| 59 | This little program scans a HTML file and checks for broken | ||
| 60 | links. It is similar to check-links.pl by Jamie Zawinski, | ||
| 61 | but uses all facilities of the LuaSocket library and the Lua | ||
| 62 | language. It has not been thoroughly tested, but it should | ||
| 63 | work. Just run | ||
| 64 | |||
| 65 | lua check-links.lua [-n] {<url>} > output | ||
| 66 | |||
| 67 | and open the result to see a list of broken links. Make sure | ||
| 68 | you check the '-n' switch. It runs in non-blocking mode, | ||
| 69 | using coroutines, and is MUCH faster! | ||
| 70 | |||
| 71 | forward.lua -- coroutine based forward server | ||
| 72 | |||
| 73 | This is a forward server that can accept several connections | ||
| 74 | and transfers simultaneously using non-blocking I/O and the | ||
| 75 | coroutine-based dispatcher. You can run, for example | ||
| 76 | |||
| 77 | lua forward.lua 8080:proxy.com:3128 | ||
| 78 | |||
| 79 | to redirect all local conections to port 8080 to the host | ||
| 80 | 'proxy.com' at port 3128. | ||
| 81 | |||
| 82 | unix.c and unix.h | ||
| 83 | |||
| 84 | This is an implementation of Unix local domain sockets and | ||
| 85 | demonstrates how to extend LuaSocket with a new type of | ||
| 86 | transport. It has been tested on Linux and on Mac OS X. | ||
| 87 | |||
| 4 | listener.lua -- socket to stdout | 88 | listener.lua -- socket to stdout |
| 5 | talker.lua -- stdin to socket | 89 | talker.lua -- stdin to socket |
| 6 | 90 | ||
| 7 | listener.lua and talker.lua are about the simplest | 91 | listener.lua and talker.lua are about the simplest |
| 8 | applications you can write using LuaSocket. Run | 92 | applications you can write using LuaSocket. Run |
| 9 | 93 | ||
| 10 | 'lua listener.lua' and 'lua talker.lua' | 94 | 'lua listener.lua' and 'lua talker.lua' |
| 11 | 95 | ||
| @@ -17,13 +101,13 @@ be printed by listen.lua. | |||
| 17 | This is a cool program written by David Burgess to print | 101 | This is a cool program written by David Burgess to print |
| 18 | files using the Line Printer Daemon protocol, widely used in | 102 | files using the Line Printer Daemon protocol, widely used in |
| 19 | Unix machines. It uses the lp.lua implementation, in the | 103 | Unix machines. It uses the lp.lua implementation, in the |
| 20 | etc directory. Just run 'lua lpr.lua <filename> | 104 | samples directory. Just run 'lua lpr.lua <filename> |
| 21 | queue=<printername>' and the file will print! | 105 | queue=<printername>' and the file will print! |
| 22 | 106 | ||
| 23 | cddb.lua -- CDDB client | 107 | cddb.lua -- CDDB client |
| 24 | 108 | ||
| 25 | This is the first try on a simple CDDB client. Not really | 109 | This is the first try on a simple CDDB client. Not really |
| 26 | useful, but one day it might become a module. | 110 | useful, but one day it might become a module. |
| 27 | 111 | ||
| 28 | daytimeclnt.lua -- day time client | 112 | daytimeclnt.lua -- day time client |
| 29 | 113 | ||
diff --git a/etc/b64.lua b/samples/b64.lua index 11eeb2d..11eeb2d 100644 --- a/etc/b64.lua +++ b/samples/b64.lua | |||
diff --git a/samples/cddb.lua b/samples/cddb.lua index 49a1871..59d5a44 100644 --- a/samples/cddb.lua +++ b/samples/cddb.lua | |||
| @@ -26,7 +26,7 @@ function parse(body) | |||
| 26 | data[key] = value | 26 | data[key] = value |
| 27 | end | 27 | end |
| 28 | end | 28 | end |
| 29 | return data, code, message | 29 | return data, code, message |
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | local host = socket.dns.gethostname() | 32 | local host = socket.dns.gethostname() |
diff --git a/etc/check-links.lua b/samples/check-links.lua index 283f3ac..283f3ac 100644 --- a/etc/check-links.lua +++ b/samples/check-links.lua | |||
diff --git a/etc/check-memory.lua b/samples/check-memory.lua index 7bd984d..7bd984d 100644 --- a/etc/check-memory.lua +++ b/samples/check-memory.lua | |||
diff --git a/etc/cookie.lua b/samples/cookie.lua index 4adb403..fec10a1 100644 --- a/etc/cookie.lua +++ b/samples/cookie.lua | |||
| @@ -5,7 +5,7 @@ local ltn12 = require"ltn12" | |||
| 5 | 5 | ||
| 6 | local token_class = '[^%c%s%(%)%<%>%@%,%;%:%\\%"%/%[%]%?%=%{%}]' | 6 | local token_class = '[^%c%s%(%)%<%>%@%,%;%:%\\%"%/%[%]%?%=%{%}]' |
| 7 | 7 | ||
| 8 | local function unquote(t, quoted) | 8 | local function unquote(t, quoted) |
| 9 | local n = string.match(t, "%$(%d+)$") | 9 | local n = string.match(t, "%$(%d+)$") |
| 10 | if n then n = tonumber(n) end | 10 | if n then n = tonumber(n) end |
| 11 | if quoted[n] then return quoted[n] | 11 | if quoted[n] then return quoted[n] |
| @@ -14,19 +14,19 @@ end | |||
| 14 | 14 | ||
| 15 | local function parse_set_cookie(c, quoted, cookie_table) | 15 | local function parse_set_cookie(c, quoted, cookie_table) |
| 16 | c = c .. ";$last=last;" | 16 | c = c .. ";$last=last;" |
| 17 | local _, __, n, v, i = string.find(c, "(" .. token_class .. | 17 | local _, _, n, v, i = string.find(c, "(" .. token_class .. |
| 18 | "+)%s*=%s*(.-)%s*;%s*()") | 18 | "+)%s*=%s*(.-)%s*;%s*()") |
| 19 | local cookie = { | 19 | local cookie = { |
| 20 | name = n, | 20 | name = n, |
| 21 | value = unquote(v, quoted), | 21 | value = unquote(v, quoted), |
| 22 | attributes = {} | 22 | attributes = {} |
| 23 | } | 23 | } |
| 24 | while 1 do | 24 | while 1 do |
| 25 | _, __, n, v, i = string.find(c, "(" .. token_class .. | 25 | _, _, n, v, i = string.find(c, "(" .. token_class .. |
| 26 | "+)%s*=?%s*(.-)%s*;%s*()", i) | 26 | "+)%s*=?%s*(.-)%s*;%s*()", i) |
| 27 | if not n or n == "$last" then break end | 27 | if not n or n == "$last" then break end |
| 28 | cookie.attributes[#cookie.attributes+1] = { | 28 | cookie.attributes[#cookie.attributes+1] = { |
| 29 | name = n, | 29 | name = n, |
| 30 | value = unquote(v, quoted) | 30 | value = unquote(v, quoted) |
| 31 | } | 31 | } |
| 32 | end | 32 | end |
| @@ -46,8 +46,8 @@ local function split_set_cookie(s, cookie_table) | |||
| 46 | -- split into individual cookies | 46 | -- split into individual cookies |
| 47 | i = 1 | 47 | i = 1 |
| 48 | while 1 do | 48 | while 1 do |
| 49 | local _, __, cookie, next_token | 49 | local _, _, cookie, next_token |
| 50 | _, __, cookie, i, next_token = string.find(s, "(.-)%s*%,%s*()(" .. | 50 | _, _, cookie, i, next_token = string.find(s, "(.-)%s*%,%s*()(" .. |
| 51 | token_class .. "+)%s*=", i) | 51 | token_class .. "+)%s*=", i) |
| 52 | if not next_token then break end | 52 | if not next_token then break end |
| 53 | parse_set_cookie(cookie, quoted, cookie_table) | 53 | parse_set_cookie(cookie, quoted, cookie_table) |
| @@ -62,12 +62,12 @@ local function quote(s) | |||
| 62 | end | 62 | end |
| 63 | 63 | ||
| 64 | local _empty = {} | 64 | local _empty = {} |
| 65 | local function build_cookies(cookies) | 65 | local function build_cookies(cookies) |
| 66 | s = "" | 66 | s = "" |
| 67 | for i,v in ipairs(cookies or _empty) do | 67 | for i,v in ipairs(cookies or _empty) do |
| 68 | if v.name then | 68 | if v.name then |
| 69 | s = s .. v.name | 69 | s = s .. v.name |
| 70 | if v.value and v.value ~= "" then | 70 | if v.value and v.value ~= "" then |
| 71 | s = s .. '=' .. quote(v.value) | 71 | s = s .. '=' .. quote(v.value) |
| 72 | end | 72 | end |
| 73 | end | 73 | end |
| @@ -83,6 +83,6 @@ local function build_cookies(cookies) | |||
| 83 | end | 83 | end |
| 84 | if i < #cookies then s = s .. ", " end | 84 | if i < #cookies then s = s .. ", " end |
| 85 | end | 85 | end |
| 86 | return s | 86 | return s |
| 87 | end | 87 | end |
| 88 | 88 | ||
diff --git a/etc/dict.lua b/samples/dict.lua index 8c5b711..8c5b711 100644 --- a/etc/dict.lua +++ b/samples/dict.lua | |||
diff --git a/etc/dispatch.lua b/samples/dispatch.lua index 2485415..2485415 100644 --- a/etc/dispatch.lua +++ b/samples/dispatch.lua | |||
diff --git a/etc/eol.lua b/samples/eol.lua index eeaf0ce..eeaf0ce 100644 --- a/etc/eol.lua +++ b/samples/eol.lua | |||
diff --git a/etc/forward.lua b/samples/forward.lua index 05ced1a..05ced1a 100644 --- a/etc/forward.lua +++ b/samples/forward.lua | |||
diff --git a/etc/get.lua b/samples/get.lua index 9edc235..d53c465 100644 --- a/etc/get.lua +++ b/samples/get.lua | |||
| @@ -71,7 +71,7 @@ function stats(size) | |||
| 71 | local current = socket.gettime() | 71 | local current = socket.gettime() |
| 72 | if chunk then | 72 | if chunk then |
| 73 | -- total bytes received | 73 | -- total bytes received |
| 74 | got = got + string.len(chunk) | 74 | got = got + string.len(chunk) |
| 75 | -- not enough time for estimate | 75 | -- not enough time for estimate |
| 76 | if current - last > 1 then | 76 | if current - last > 1 then |
| 77 | io.stderr:write("\r", gauge(got, current - start, size)) | 77 | io.stderr:write("\r", gauge(got, current - start, size)) |
diff --git a/etc/links b/samples/links index 087f1c0..087f1c0 100644 --- a/etc/links +++ b/samples/links | |||
diff --git a/etc/lp.lua b/samples/lp.lua index 25f0b95..25f0b95 100644 --- a/etc/lp.lua +++ b/samples/lp.lua | |||
diff --git a/etc/qp.lua b/samples/qp.lua index 523238b..523238b 100644 --- a/etc/qp.lua +++ b/samples/qp.lua | |||
diff --git a/etc/tftp.lua b/samples/tftp.lua index ed99cd1..ed99cd1 100644 --- a/etc/tftp.lua +++ b/samples/tftp.lua | |||
