aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/socket.lua25
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-----------------------------------------------------------------------------
10module("socket") 10local base = require("base")
11local string = require("string")
12local math = require("math")
11local socket = require("lsocket") 13local socket = require("lsocket")
14module("socket")
12 15
13----------------------------------------------------------------------------- 16-----------------------------------------------------------------------------
14-- Auxiliar functions 17-- Auxiliar functions
@@ -40,11 +43,11 @@ socket.try = socket.newtry()
40 43
41function socket.choose(table) 44function 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
50end 53end
@@ -59,7 +62,7 @@ socket.sinkt = {}
59socket.BLOCKSIZE = 2048 62socket.BLOCKSIZE = 2048
60 63
61socket.sinkt["http-chunked"] = function(sock) 64socket.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)
72end 75end
73 76
74socket.sinkt["close-when-done"] = function(sock) 77socket.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)
86end 89end
87 90
88socket.sinkt["keep-open"] = function(sock) 91socket.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"]
102socket.sink = socket.choose(socket.sinkt) 105socket.sink = socket.choose(socket.sinkt)
103 106
104socket.sourcet["by-length"] = function(sock, length) 107socket.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
120socket.sourcet["until-closed"] = function(sock) 123socket.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)
137end 140end
138 141
139socket.sourcet["http-chunked"] = function(sock) 142socket.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
168socket.sourcet["default"] = socket.sourcet["until-closed"] 171socket.sourcet["default"] = socket.sourcet["until-closed"]
169 172
170socket.source = socket.choose(socket.sourcet) 173socket.source = socket.choose(socket.sourcet)
174
175base.setmetatable(socket, nil)