aboutsummaryrefslogtreecommitdiff
path: root/src/http.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/http.lua')
-rw-r--r--src/http.lua27
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")
15local headers = require("socket.headers") 15local headers = require("socket.headers")
16local base = _G 16local base = _G
17local table = require("table") 17local table = require("table")
18module("socket.http") 18socket.http = {}
19local _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
24TIMEOUT = 60 25TIMEOUT = 60
25-- default port for document retrieval 26-- default port for document retrieval
26PORT = 80 27_M.PORT = 80
27-- user agent field sent in request 28-- user agent field sent in request
28USERAGENT = 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-----------------------------------------------------------------------------
106local metat = { __index = {} } 107local metat = { __index = {} }
107 108
108function open(host, port, create) 109function _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
119end 120end
@@ -209,7 +210,7 @@ end
209local function adjustheaders(reqt) 210local 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
230local default = { 231local 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
271local trequest, tredirect 272local trequest, tredirect
272 273
273function 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
289end 290end
290 291
291function 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
346end 347end
347 348
348request = 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
351end) 352end)
353
354return _M \ No newline at end of file