diff options
Diffstat (limited to 'src/http.lua')
-rw-r--r-- | src/http.lua | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/http.lua b/src/http.lua index 4c7e7d8..ac4b2d6 100644 --- a/src/http.lua +++ b/src/http.lua | |||
@@ -15,7 +15,8 @@ local string = require("string") | |||
15 | local headers = require("socket.headers") | 15 | local headers = require("socket.headers") |
16 | local base = _G | 16 | local base = _G |
17 | local table = require("table") | 17 | local table = require("table") |
18 | module("socket.http") | 18 | socket.http = {} |
19 | local _M = socket.http | ||
19 | 20 | ||
20 | ----------------------------------------------------------------------------- | 21 | ----------------------------------------------------------------------------- |
21 | -- Program constants | 22 | -- Program constants |
@@ -23,9 +24,9 @@ module("socket.http") | |||
23 | -- connection timeout in seconds | 24 | -- connection timeout in seconds |
24 | TIMEOUT = 60 | 25 | TIMEOUT = 60 |
25 | -- default port for document retrieval | 26 | -- default port for document retrieval |
26 | PORT = 80 | 27 | _M.PORT = 80 |
27 | -- user agent field sent in request | 28 | -- user agent field sent in request |
28 | USERAGENT = socket._VERSION | 29 | _M.USERAGENT = socket._VERSION |
29 | 30 | ||
30 | ----------------------------------------------------------------------------- | 31 | ----------------------------------------------------------------------------- |
31 | -- Reads MIME headers from a connection, unfolding where needed | 32 | -- Reads MIME headers from a connection, unfolding where needed |
@@ -105,15 +106,15 @@ end | |||
105 | ----------------------------------------------------------------------------- | 106 | ----------------------------------------------------------------------------- |
106 | local metat = { __index = {} } | 107 | local metat = { __index = {} } |
107 | 108 | ||
108 | function open(host, port, create) | 109 | function _M.open(host, port, create) |
109 | -- create socket with user connect function, or with default | 110 | -- create socket with user connect function, or with default |
110 | local c = socket.try((create or socket.tcp)()) | 111 | local c = socket.try((create or socket.tcp)()) |
111 | local h = base.setmetatable({ c = c }, metat) | 112 | local h = base.setmetatable({ c = c }, metat) |
112 | -- create finalized try | 113 | -- create finalized try |
113 | h.try = socket.newtry(function() h:close() end) | 114 | h.try = socket.newtry(function() h:close() end) |
114 | -- set timeout before connecting | 115 | -- set timeout before connecting |
115 | h.try(c:settimeout(TIMEOUT)) | 116 | h.try(c:settimeout(_M.TIMEOUT)) |
116 | h.try(c:connect(host, port or PORT)) | 117 | h.try(c:connect(host, port or _M.PORT)) |
117 | -- here everything worked | 118 | -- here everything worked |
118 | return h | 119 | return h |
119 | end | 120 | end |
@@ -209,7 +210,7 @@ end | |||
209 | local function adjustheaders(reqt) | 210 | local function adjustheaders(reqt) |
210 | -- default headers | 211 | -- default headers |
211 | local lower = { | 212 | local lower = { |
212 | ["user-agent"] = USERAGENT, | 213 | ["user-agent"] = _M.USERAGENT, |
213 | ["host"] = reqt.host, | 214 | ["host"] = reqt.host, |
214 | ["connection"] = "close, TE", | 215 | ["connection"] = "close, TE", |
215 | ["te"] = "trailers" | 216 | ["te"] = "trailers" |
@@ -229,7 +230,7 @@ end | |||
229 | -- default url parts | 230 | -- default url parts |
230 | local default = { | 231 | local default = { |
231 | host = "", | 232 | host = "", |
232 | port = PORT, | 233 | port = _M.PORT, |
233 | path ="/", | 234 | path ="/", |
234 | scheme = "http" | 235 | scheme = "http" |
235 | } | 236 | } |
@@ -270,7 +271,7 @@ end | |||
270 | -- forward declarations | 271 | -- forward declarations |
271 | local trequest, tredirect | 272 | local trequest, tredirect |
272 | 273 | ||
273 | function tredirect(reqt, location) | 274 | --[[local]] function tredirect(reqt, location) |
274 | local result, code, headers, status = trequest { | 275 | local result, code, headers, status = trequest { |
275 | -- the RFC says the redirect URL has to be absolute, but some | 276 | -- the RFC says the redirect URL has to be absolute, but some |
276 | -- servers do not respect that | 277 | -- servers do not respect that |
@@ -288,11 +289,11 @@ function tredirect(reqt, location) | |||
288 | return result, code, headers, status | 289 | return result, code, headers, status |
289 | end | 290 | end |
290 | 291 | ||
291 | function trequest(reqt) | 292 | --[[local]] function trequest(reqt) |
292 | -- we loop until we get what we want, or | 293 | -- we loop until we get what we want, or |
293 | -- until we are sure there is no way to get it | 294 | -- until we are sure there is no way to get it |
294 | local nreqt = adjustrequest(reqt) | 295 | local nreqt = adjustrequest(reqt) |
295 | local h = open(nreqt.host, nreqt.port, nreqt.create) | 296 | local h = _M.open(nreqt.host, nreqt.port, nreqt.create) |
296 | -- send request line and headers | 297 | -- send request line and headers |
297 | h:sendrequestline(nreqt.method, nreqt.uri) | 298 | h:sendrequestline(nreqt.method, nreqt.uri) |
298 | h:sendheaders(nreqt.headers) | 299 | h:sendheaders(nreqt.headers) |
@@ -345,7 +346,9 @@ local function srequest(u, b) | |||
345 | return table.concat(t), code, headers, status | 346 | return table.concat(t), code, headers, status |
346 | end | 347 | end |
347 | 348 | ||
348 | request = socket.protect(function(reqt, body) | 349 | _M.request = socket.protect(function(reqt, body) |
349 | if base.type(reqt) == "string" then return srequest(reqt, body) | 350 | if base.type(reqt) == "string" then return srequest(reqt, body) |
350 | else return trequest(reqt) end | 351 | else return trequest(reqt) end |
351 | end) | 352 | end) |
353 | |||
354 | return _M \ No newline at end of file | ||