diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/socket.lua | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/socket.lua b/src/socket.lua index b3a4269..c3bbba9 100644 --- a/src/socket.lua +++ b/src/socket.lua | |||
@@ -7,8 +7,11 @@ | |||
7 | ----------------------------------------------------------------------------- | 7 | ----------------------------------------------------------------------------- |
8 | -- Declare module and import dependencies | 8 | -- Declare module and import dependencies |
9 | ----------------------------------------------------------------------------- | 9 | ----------------------------------------------------------------------------- |
10 | module("socket") | 10 | local base = require("base") |
11 | local string = require("string") | ||
12 | local math = require("math") | ||
11 | local socket = require("lsocket") | 13 | local socket = require("lsocket") |
14 | module("socket") | ||
12 | 15 | ||
13 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
14 | -- Auxiliar functions | 17 | -- Auxiliar functions |
@@ -40,11 +43,11 @@ socket.try = socket.newtry() | |||
40 | 43 | ||
41 | function socket.choose(table) | 44 | function socket.choose(table) |
42 | return function(name, opt1, opt2) | 45 | return function(name, opt1, opt2) |
43 | if type(name) ~= "string" then | 46 | if base.type(name) ~= "string" then |
44 | name, opt1, opt2 = "default", name, opt1 | 47 | name, opt1, opt2 = "default", name, opt1 |
45 | end | 48 | end |
46 | local f = table[name or "nil"] | 49 | local f = table[name or "nil"] |
47 | if not f then error("unknown key (" .. tostring(name) .. ")", 3) | 50 | if not f then base.error("unknown key (".. base.tostring(name) ..")", 3) |
48 | else return f(opt1, opt2) end | 51 | else return f(opt1, opt2) end |
49 | end | 52 | end |
50 | end | 53 | end |
@@ -59,7 +62,7 @@ socket.sinkt = {} | |||
59 | socket.BLOCKSIZE = 2048 | 62 | socket.BLOCKSIZE = 2048 |
60 | 63 | ||
61 | socket.sinkt["http-chunked"] = function(sock) | 64 | socket.sinkt["http-chunked"] = function(sock) |
62 | return setmetatable({ | 65 | return base.setmetatable({ |
63 | getfd = function() return sock:getfd() end, | 66 | getfd = function() return sock:getfd() end, |
64 | dirty = function() return sock:dirty() end | 67 | dirty = function() return sock:dirty() end |
65 | }, { | 68 | }, { |
@@ -72,7 +75,7 @@ socket.sinkt["http-chunked"] = function(sock) | |||
72 | end | 75 | end |
73 | 76 | ||
74 | socket.sinkt["close-when-done"] = function(sock) | 77 | socket.sinkt["close-when-done"] = function(sock) |
75 | return setmetatable({ | 78 | return base.setmetatable({ |
76 | getfd = function() return sock:getfd() end, | 79 | getfd = function() return sock:getfd() end, |
77 | dirty = function() return sock:dirty() end | 80 | dirty = function() return sock:dirty() end |
78 | }, { | 81 | }, { |
@@ -86,7 +89,7 @@ socket.sinkt["close-when-done"] = function(sock) | |||
86 | end | 89 | end |
87 | 90 | ||
88 | socket.sinkt["keep-open"] = function(sock) | 91 | socket.sinkt["keep-open"] = function(sock) |
89 | return setmetatable({ | 92 | return base.setmetatable({ |
90 | getfd = function() return sock:getfd() end, | 93 | getfd = function() return sock:getfd() end, |
91 | dirty = function() return sock:dirty() end | 94 | dirty = function() return sock:dirty() end |
92 | }, { | 95 | }, { |
@@ -102,7 +105,7 @@ socket.sinkt["default"] = socket.sinkt["keep-open"] | |||
102 | socket.sink = socket.choose(socket.sinkt) | 105 | socket.sink = socket.choose(socket.sinkt) |
103 | 106 | ||
104 | socket.sourcet["by-length"] = function(sock, length) | 107 | socket.sourcet["by-length"] = function(sock, length) |
105 | return setmetatable({ | 108 | return base.setmetatable({ |
106 | getfd = function() return sock:getfd() end, | 109 | getfd = function() return sock:getfd() end, |
107 | dirty = function() return sock:dirty() end | 110 | dirty = function() return sock:dirty() end |
108 | }, { | 111 | }, { |
@@ -119,7 +122,7 @@ end | |||
119 | 122 | ||
120 | socket.sourcet["until-closed"] = function(sock) | 123 | socket.sourcet["until-closed"] = function(sock) |
121 | local done | 124 | local done |
122 | return setmetatable({ | 125 | return base.setmetatable({ |
123 | getfd = function() return sock:getfd() end, | 126 | getfd = function() return sock:getfd() end, |
124 | dirty = function() return sock:dirty() end | 127 | dirty = function() return sock:dirty() end |
125 | }, { | 128 | }, { |
@@ -137,7 +140,7 @@ socket.sourcet["until-closed"] = function(sock) | |||
137 | end | 140 | end |
138 | 141 | ||
139 | socket.sourcet["http-chunked"] = function(sock) | 142 | socket.sourcet["http-chunked"] = function(sock) |
140 | return setmetatable({ | 143 | return base.setmetatable({ |
141 | getfd = function() return sock:getfd() end, | 144 | getfd = function() return sock:getfd() end, |
142 | dirty = function() return sock:dirty() end | 145 | dirty = function() return sock:dirty() end |
143 | }, { | 146 | }, { |
@@ -145,7 +148,7 @@ socket.sourcet["http-chunked"] = function(sock) | |||
145 | -- get chunk size, skip extention | 148 | -- get chunk size, skip extention |
146 | local line, err = sock:receive() | 149 | local line, err = sock:receive() |
147 | if err then return nil, err end | 150 | if err then return nil, err end |
148 | local size = tonumber(string.gsub(line, ";.*", ""), 16) | 151 | local size = base.tonumber(string.gsub(line, ";.*", ""), 16) |
149 | if not size then return nil, "invalid chunk size" end | 152 | if not size then return nil, "invalid chunk size" end |
150 | -- was it the last chunk? | 153 | -- was it the last chunk? |
151 | if size <= 0 then | 154 | if size <= 0 then |
@@ -168,3 +171,5 @@ end | |||
168 | socket.sourcet["default"] = socket.sourcet["until-closed"] | 171 | socket.sourcet["default"] = socket.sourcet["until-closed"] |
169 | 172 | ||
170 | socket.source = socket.choose(socket.sourcet) | 173 | socket.source = socket.choose(socket.sourcet) |
174 | |||
175 | base.setmetatable(socket, nil) | ||