From 1e5e8b5ce5573fddc7060dae3c1bf2c02788b35a Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Mon, 11 Oct 2004 06:18:57 +0000 Subject: [PATCH] Fine tunned modules scheme. Adjusted client modules. Fixed proxy bug in http. --- FIX | 2 ++ TODO | 5 +++-- etc/check-links.lua | 4 ++-- etc/check-memory.lua | 10 +++++----- etc/dict.lua | 6 ++++-- etc/get.lua | 6 +++--- etc/lp.lua | 1 - etc/tftp.lua | 4 +++- samples/cddb.lua | 2 +- samples/lpr.lua | 2 +- src/http.lua | 17 +++++++++++++++-- src/luasocket.c | 2 +- src/mime.c | 2 +- test/dicttest.lua | 3 +++ test/tftptest.lua | 2 +- test/urltest.lua | 2 +- 16 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 test/dicttest.lua diff --git a/FIX b/FIX index 15eb4a9..5c31d3c 100644 --- a/FIX +++ b/FIX @@ -1,2 +1,4 @@ gettime returns time since Unix Epoch 1/1/1970 (UTC) sleep is robust to interrupts +select had a stupid bug +http.PROXY etc wasn't working diff --git a/TODO b/TODO index 9023555..1062177 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ +ftp.send/recv return bytes transfered? new scheme to choose family/protocol of object to create -use new distribution scheme -fix PROXY in http.lua change ltn13 to make sure drawbacks are obvious - check discussion make sure errors not thrown by try() are not caught by protect() @@ -24,6 +23,8 @@ testar os options! - proteger ou atomizar o conjunto (timedout, receive), (timedout, send) - inet_ntoa também é uma merda. +*fix PROXY in http.lua +*use new distribution scheme *create the getstats method. *fix local domain socket kludge of name size *use TLS diff --git a/etc/check-links.lua b/etc/check-links.lua index 4487593..79e6a54 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua @@ -4,8 +4,8 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -local http = require("http") -local url = require("url") +local http = require("socket.http") +local url = require("socket.url") http.TIMEOUT = 10 function readfile(path) diff --git a/etc/check-memory.lua b/etc/check-memory.lua index fdc6b9b..7892ba0 100644 --- a/etc/check-memory.lua +++ b/etc/check-memory.lua @@ -8,10 +8,10 @@ function load(s) end load("socket") -load("url") +load("socket.url") load("ltn12") load("mime") -load("tp") -load("smtp") -load("http") -load("ftp") +load("socket.tp") +load("socket.smtp") +load("socket.http") +load("socket.ftp") diff --git a/etc/dict.lua b/etc/dict.lua index dce1658..8c197f5 100644 --- a/etc/dict.lua +++ b/etc/dict.lua @@ -9,8 +9,10 @@ -- Load required modules ----------------------------------------------------------------------------- local socket = require("socket") -local url = require("url") -local tp = require("tp") +local url = require("socket.url") +local tp = require("socket.tp") + +module("socket.dict") ----------------------------------------------------------------------------- -- Globals diff --git a/etc/get.lua b/etc/get.lua index 712d807..0c95d54 100644 --- a/etc/get.lua +++ b/etc/get.lua @@ -5,9 +5,9 @@ -- RCS ID: $Id$ ----------------------------------------------------------------------------- local socket = require("socket") -local http = require("http") -local ftp = require("ftp") -local url = require("url") +local http = require("socket.http") +local ftp = require("socket.ftp") +local url = require("socket.url") local ltn12 = require("ltn12") -- formats a number of seconds into human readable form diff --git a/etc/lp.lua b/etc/lp.lua index 3c36b11..b69cc02 100644 --- a/etc/lp.lua +++ b/etc/lp.lua @@ -39,7 +39,6 @@ local function connect(localhost, option) until localport > 731 test(skt, err) else skt = test(socket.tcp()) end -print("'" .. host .. "'") try(skt:connect(host, port)) return { skt = skt, try = try } end diff --git a/etc/tftp.lua b/etc/tftp.lua index 70db050..f4af8bc 100644 --- a/etc/tftp.lua +++ b/etc/tftp.lua @@ -10,7 +10,9 @@ ----------------------------------------------------------------------------- local socket = require("socket") local ltn12 = require("ltn12") -local url = require("url") +local url = require("socket.url") + +module("socket.tftp") ----------------------------------------------------------------------------- -- Program constants diff --git a/samples/cddb.lua b/samples/cddb.lua index daa5df1..3e48cf6 100644 --- a/samples/cddb.lua +++ b/samples/cddb.lua @@ -1,5 +1,5 @@ local socket = require("socket") -local http = require("http") +local http = require("socket.http") if not arg or not arg[1] or not arg[2] then print("luasocket cddb.lua []") diff --git a/samples/lpr.lua b/samples/lpr.lua index d743026..caab387 100644 --- a/samples/lpr.lua +++ b/samples/lpr.lua @@ -1,4 +1,4 @@ -local lp = require("lp") +local lp = require("socket.lp") local function usage() print('\nUsage: lua lptest.lua [filename] [keyword=val...]\n') diff --git a/src/http.lua b/src/http.lua index 8ea4c47..b265650 100644 --- a/src/http.lua +++ b/src/http.lua @@ -113,8 +113,9 @@ end ----------------------------------------------------------------------------- -- High level HTTP API ----------------------------------------------------------------------------- -local function uri(reqt) +local function adjusturi(reqt) local u = reqt + -- if there is a proxy, we need the full url. otherwise, just a part. if not reqt.proxy and not PROXY then u = { path = socket.try(reqt.path, "invalid path 'nil'"), @@ -126,6 +127,16 @@ local function uri(reqt) return url.build(u) end +local function adjustproxy(reqt) + local proxy = reqt.proxy or PROXY + if proxy then + proxy = url.parse(proxy) + return proxy.host, proxy.port or 3128 + else + return reqt.host, reqt.port + end +end + local function adjustheaders(headers, host) local lower = {} -- override with user values @@ -152,7 +163,9 @@ local function adjustrequest(reqt) for i,v in reqt do nreqt[i] = reqt[i] end socket.try(nreqt.host, "invalid host '" .. tostring(nreqt.host) .. "'") -- compute uri if user hasn't overriden - nreqt.uri = nreqt.uri or uri(nreqt) + nreqt.uri = reqt.uri or adjusturi(nreqt) + -- ajust host and port if there is a proxy + nreqt.host, nreqt.port = adjustproxy(nreqt) -- adjust headers in request nreqt.headers = adjustheaders(nreqt.headers, nreqt.host) return nreqt diff --git a/src/luasocket.c b/src/luasocket.c index ebe9f5f..d07b90f 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -19,7 +19,7 @@ \*=========================================================================*/ #include #include -#include +#include "compat-5.1.h" /*=========================================================================*\ * LuaSocket includes diff --git a/src/mime.c b/src/mime.c index 594135f..8bc20d3 100644 --- a/src/mime.c +++ b/src/mime.c @@ -8,7 +8,7 @@ #include #include -#include +#include "compat-5.1.h" #include "mime.h" diff --git a/test/dicttest.lua b/test/dicttest.lua new file mode 100644 index 0000000..fcdd61f --- /dev/null +++ b/test/dicttest.lua @@ -0,0 +1,3 @@ +local dict = require"socket.dict" + +for i,v in dict.get("dict://localhost/d:banana") do print(v) end diff --git a/test/tftptest.lua b/test/tftptest.lua index f0dbd82..edb6484 100644 --- a/test/tftptest.lua +++ b/test/tftptest.lua @@ -1,5 +1,5 @@ -- load tftpclnt.lua -local tftp = require("tftp") +local tftp = require("socket.tftp") -- needs tftp server running on localhost, with root pointing to -- a directory with index.html in it diff --git a/test/urltest.lua b/test/urltest.lua index eb15bcf..570b52e 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -1,5 +1,5 @@ local socket = require("socket") -socket.url = require("url") +socket.url = require("socket.url") dofile("testsupport.lua") local check_build_url = function(parsed)