aboutsummaryrefslogtreecommitdiff
path: root/src/http.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-04 15:15:45 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-04 15:15:45 +0000
commit9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (patch)
tree8c3521366ef84f534bbec278437be7ea24e2ac1c /src/http.lua
parent63d60223da9de60f873ca08a25dbd9512c998929 (diff)
downloadluasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.gz
luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.bz2
luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.zip
Só pra não perder se der merda.
Diffstat (limited to 'src/http.lua')
-rw-r--r--src/http.lua57
1 files changed, 28 insertions, 29 deletions
diff --git a/src/http.lua b/src/http.lua
index 66a236d..ebe6b54 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -5,23 +5,22 @@
5-- Conforming to RFC 2616 5-- Conforming to RFC 2616
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded
9require("socket")
10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME]
12 8
13-- require other modules 9-----------------------------------------------------------------------------
14require("ltn12") 10-- Load other required modules
15require("mime") 11-------------------------------------------------------------------------------
16-- get MIME namespace 12local socket = require("socket")
17local mime = _G[MIME_LIBNAME] 13local ltn12 = require("ltn12")
18require("url") 14local mime = require("mime")
15local url = require("url")
19 16
20-- create namespace inside LuaSocket namespace 17-----------------------------------------------------------------------------
21socket.http = socket.http or {} 18-- Setup namespace
19-------------------------------------------------------------------------------
20http = {}
22-- make all module globals fall into namespace 21-- make all module globals fall into namespace
23setmetatable(socket.http, { __index = _G }) 22setmetatable(http, { __index = _G })
24setfenv(1, socket.http) 23setfenv(1, http)
25 24
26----------------------------------------------------------------------------- 25-----------------------------------------------------------------------------
27-- Program constants 26-- Program constants
@@ -116,17 +115,17 @@ local function receive_status(reqt, respt, tmp)
116end 115end
117 116
118local function request_uri(reqt, respt, tmp) 117local function request_uri(reqt, respt, tmp)
119 local url = tmp.parsed 118 local u = tmp.parsed
120 if not reqt.proxy then 119 if not reqt.proxy then
121 local parsed = tmp.parsed 120 local parsed = tmp.parsed
122 url = { 121 u = {
123 path = parsed.path, 122 path = parsed.path,
124 params = parsed.params, 123 params = parsed.params,
125 query = parsed.query, 124 query = parsed.query,
126 fragment = parsed.fragment 125 fragment = parsed.fragment
127 } 126 }
128 end 127 end
129 return socket.url.build(url) 128 return url.build(u)
130end 129end
131 130
132local function send_request(reqt, respt, tmp) 131local function send_request(reqt, respt, tmp)
@@ -155,7 +154,7 @@ local function open(reqt, respt, tmp)
155 local proxy = reqt.proxy or PROXY 154 local proxy = reqt.proxy or PROXY
156 local host, port 155 local host, port
157 if proxy then 156 if proxy then
158 local pproxy = socket.url.parse(proxy) 157 local pproxy = url.parse(proxy)
159 socket.try(pproxy.port and pproxy.host, "invalid proxy") 158 socket.try(pproxy.port and pproxy.host, "invalid proxy")
160 host, port = pproxy.host, pproxy.port 159 host, port = pproxy.host, pproxy.port
161 else 160 else
@@ -169,15 +168,13 @@ end
169 168
170local function adjust_headers(reqt, respt, tmp) 169local function adjust_headers(reqt, respt, tmp)
171 local lower = {} 170 local lower = {}
172 local headers = reqt.headers or {}
173 -- set default headers
174 lower["user-agent"] = USERAGENT
175 -- override with user values 171 -- override with user values
176 for i,v in headers do 172 for i,v in (reqt.headers or lower) do
177 lower[string.lower(i)] = v 173 lower[string.lower(i)] = v
178 end 174 end
175 lower["user-agent"] = lower["user-agent"] or USERAGENT
176 -- these cannot be overriden
179 lower["host"] = tmp.parsed.host 177 lower["host"] = tmp.parsed.host
180 -- this cannot be overriden
181 lower["connection"] = "close" 178 lower["connection"] = "close"
182 -- store results 179 -- store results
183 tmp.headers = lower 180 tmp.headers = lower
@@ -185,7 +182,7 @@ end
185 182
186local function parse_url(reqt, respt, tmp) 183local function parse_url(reqt, respt, tmp)
187 -- parse url with default fields 184 -- parse url with default fields
188 local parsed = socket.url.parse(reqt.url, { 185 local parsed = url.parse(reqt.url, {
189 host = "", 186 host = "",
190 port = PORT, 187 port = PORT,
191 path ="/", 188 path ="/",
@@ -250,7 +247,7 @@ local function redirect(reqt, respt, tmp)
250 method = reqt.method, 247 method = reqt.method,
251 -- the RFC says the redirect URL has to be absolute, but some 248 -- the RFC says the redirect URL has to be absolute, but some
252 -- servers do not respect that 249 -- servers do not respect that
253 url = socket.url.absolute(reqt.url, respt.headers["location"]), 250 url = url.absolute(reqt.url, respt.headers["location"]),
254 source = reqt.source, 251 source = reqt.source,
255 sink = reqt.sink, 252 sink = reqt.sink,
256 headers = reqt.headers, 253 headers = reqt.headers,
@@ -296,20 +293,20 @@ function request(reqt)
296 return respt 293 return respt
297end 294end
298 295
299function get(url) 296function get(u)
300 local t = {} 297 local t = {}
301 respt = request { 298 respt = request {
302 url = url, 299 url = u,
303 sink = ltn12.sink.table(t) 300 sink = ltn12.sink.table(t)
304 } 301 }
305 return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers, 302 return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers,
306 respt.code, respt.error 303 respt.code, respt.error
307end 304end
308 305
309function post(url, body) 306function post(u, body)
310 local t = {} 307 local t = {}
311 respt = request { 308 respt = request {
312 url = url, 309 url = u,
313 method = "POST", 310 method = "POST",
314 source = ltn12.source.string(body), 311 source = ltn12.source.string(body),
315 sink = ltn12.sink.table(t), 312 sink = ltn12.sink.table(t),
@@ -318,3 +315,5 @@ function post(url, body)
318 return (table.getn(t) > 0 or nil) and table.concat(t), 315 return (table.getn(t) > 0 or nil) and table.concat(t),
319 respt.headers, respt.code, respt.error 316 respt.headers, respt.code, respt.error
320end 317end
318
319return http