diff options
author | Diego Nehab <diego@impa.br> | 2013-06-05 18:36:51 +0800 |
---|---|---|
committer | Diego Nehab <diego@impa.br> | 2013-06-05 18:36:51 +0800 |
commit | bc709ac7b71a1a2e8542c30a9c0dd07e6f70c0a0 (patch) | |
tree | c02a9323ca446a453b7f8fd67852b8ac0dea9c26 | |
parent | b1d1e721d1b325cacfc6342c696191730dea357d (diff) | |
download | luasocket-bc709ac7b71a1a2e8542c30a9c0dd07e6f70c0a0.tar.gz luasocket-bc709ac7b71a1a2e8542c30a9c0dd07e6f70c0a0.tar.bz2 luasocket-bc709ac7b71a1a2e8542c30a9c0dd07e6f70c0a0.zip |
Export global table only if "module()" is defined.
-rw-r--r-- | src/ltn12.lua | 9 | ||||
-rw-r--r-- | src/mbox.lua | 36 |
2 files changed, 24 insertions, 21 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua index c5f7c4c..8742b0e 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
@@ -10,9 +10,10 @@ | |||
10 | local string = require("string") | 10 | local string = require("string") |
11 | local table = require("table") | 11 | local table = require("table") |
12 | local base = _G | 12 | local base = _G |
13 | ltn12 = {} | 13 | local _M = {} |
14 | local _M = ltn12 | 14 | if module then -- heuristic for exporting a global package table |
15 | 15 | ltn12 = _M | |
16 | end | ||
16 | local filter,source,sink,pump = {},{},{},{} | 17 | local filter,source,sink,pump = {},{},{},{} |
17 | 18 | ||
18 | _M.filter = filter | 19 | _M.filter = filter |
@@ -294,4 +295,4 @@ function pump.all(src, snk, step) | |||
294 | end | 295 | end |
295 | end | 296 | end |
296 | 297 | ||
297 | return _M \ No newline at end of file | 298 | return _M |
diff --git a/src/mbox.lua b/src/mbox.lua index 2ce24fa..7724ae2 100644 --- a/src/mbox.lua +++ b/src/mbox.lua | |||
@@ -1,8 +1,10 @@ | |||
1 | local Public = {} | 1 | local _M = {} |
2 | 2 | ||
3 | mbox = Public | 3 | if module then |
4 | mbox = _M | ||
5 | end | ||
4 | 6 | ||
5 | function Public.split_message(message_s) | 7 | function _M.split_message(message_s) |
6 | local message = {} | 8 | local message = {} |
7 | message_s = string.gsub(message_s, "\r\n", "\n") | 9 | message_s = string.gsub(message_s, "\r\n", "\n") |
8 | string.gsub(message_s, "^(.-\n)\n", function (h) message.headers = h end) | 10 | string.gsub(message_s, "^(.-\n)\n", function (h) message.headers = h end) |
@@ -16,7 +18,7 @@ function Public.split_message(message_s) | |||
16 | return message.headers or "", message.body or "" | 18 | return message.headers or "", message.body or "" |
17 | end | 19 | end |
18 | 20 | ||
19 | function Public.split_headers(headers_s) | 21 | function _M.split_headers(headers_s) |
20 | local headers = {} | 22 | local headers = {} |
21 | headers_s = string.gsub(headers_s, "\r\n", "\n") | 23 | headers_s = string.gsub(headers_s, "\r\n", "\n") |
22 | headers_s = string.gsub(headers_s, "\n[ ]+", " ") | 24 | headers_s = string.gsub(headers_s, "\n[ ]+", " ") |
@@ -24,18 +26,18 @@ function Public.split_headers(headers_s) | |||
24 | return headers | 26 | return headers |
25 | end | 27 | end |
26 | 28 | ||
27 | function Public.parse_header(header_s) | 29 | function _M.parse_header(header_s) |
28 | header_s = string.gsub(header_s, "\n[ ]+", " ") | 30 | header_s = string.gsub(header_s, "\n[ ]+", " ") |
29 | header_s = string.gsub(header_s, "\n+", "") | 31 | header_s = string.gsub(header_s, "\n+", "") |
30 | local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") | 32 | local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") |
31 | return name, value | 33 | return name, value |
32 | end | 34 | end |
33 | 35 | ||
34 | function Public.parse_headers(headers_s) | 36 | function _M.parse_headers(headers_s) |
35 | local headers_t = Public.split_headers(headers_s) | 37 | local headers_t = _M.split_headers(headers_s) |
36 | local headers = {} | 38 | local headers = {} |
37 | for i = 1, #headers_t do | 39 | for i = 1, #headers_t do |
38 | local name, value = Public.parse_header(headers_t[i]) | 40 | local name, value = _M.parse_header(headers_t[i]) |
39 | if name then | 41 | if name then |
40 | name = string.lower(name) | 42 | name = string.lower(name) |
41 | if headers[name] then | 43 | if headers[name] then |
@@ -46,7 +48,7 @@ function Public.parse_headers(headers_s) | |||
46 | return headers | 48 | return headers |
47 | end | 49 | end |
48 | 50 | ||
49 | function Public.parse_from(from) | 51 | function _M.parse_from(from) |
50 | local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") | 52 | local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") |
51 | if not address then | 53 | if not address then |
52 | _, __, address = string.find(from, "%s*(.+)%s*") | 54 | _, __, address = string.find(from, "%s*(.+)%s*") |
@@ -58,7 +60,7 @@ function Public.parse_from(from) | |||
58 | return name, address | 60 | return name, address |
59 | end | 61 | end |
60 | 62 | ||
61 | function Public.split_mbox(mbox_s) | 63 | function _M.split_mbox(mbox_s) |
62 | mbox = {} | 64 | mbox = {} |
63 | mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" | 65 | mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" |
64 | local nj, i, j = 1, 1, 1 | 66 | local nj, i, j = 1, 1, 1 |
@@ -72,19 +74,19 @@ function Public.split_mbox(mbox_s) | |||
72 | return mbox | 74 | return mbox |
73 | end | 75 | end |
74 | 76 | ||
75 | function Public.parse(mbox_s) | 77 | function _M.parse(mbox_s) |
76 | local mbox = Public.split_mbox(mbox_s) | 78 | local mbox = _M.split_mbox(mbox_s) |
77 | for i = 1, #mbox do | 79 | for i = 1, #mbox do |
78 | mbox[i] = Public.parse_message(mbox[i]) | 80 | mbox[i] = _M.parse_message(mbox[i]) |
79 | end | 81 | end |
80 | return mbox | 82 | return mbox |
81 | end | 83 | end |
82 | 84 | ||
83 | function Public.parse_message(message_s) | 85 | function _M.parse_message(message_s) |
84 | local message = {} | 86 | local message = {} |
85 | message.headers, message.body = Public.split_message(message_s) | 87 | message.headers, message.body = _M.split_message(message_s) |
86 | message.headers = Public.parse_headers(message.headers) | 88 | message.headers = _M.parse_headers(message.headers) |
87 | return message | 89 | return message |
88 | end | 90 | end |
89 | 91 | ||
90 | return mbox \ No newline at end of file | 92 | return _M |