Created
April 19, 2010 18:25
-
-
Save pirj/371399 to your computer and use it in GitHub Desktop.
http proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function handler(sock_in) | |
local line = sock_in:receive('*l') | |
-- where to? | |
local url = string.match(line, 'http://([%a%d\.-]+):*%d*/') | |
local port = string.match(line, 'http://[%a%d\.-]+:(%d+)/') | |
local sock_out = socket.connect(url, port or 80) | |
repeat | |
if line then sock_out:send(line..'\r\n') end | |
line = sock_in:receive('*l') | |
until line == '' | |
sock_out:send('Connection: close\r\n') | |
sock_out:send('\r\n') | |
local response = sock_out:receive('*a') | |
if line then sock_in:send(response) end | |
end | |
server = socket.bind('localhost', PORT) | |
while true do | |
local sock_in = server:accept() | |
handler(sock_in) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function handler(sock_in) | |
sock_in = copas.wrap(sock_in) | |
local line = sock_in:receive('*l') | |
-- where to? | |
local url = string.match(line, 'http://([%a%d\.-]+):*%d*/') | |
local port = string.match(line, 'http://[%a%d\.-]+:(%d+)/') | |
local sock_out = socket.connect(url, port or 80) | |
repeat | |
if line then sock_out:send(line..'\r\n') end | |
line = sock_in:receive('*l') | |
until line == '' | |
sock_out:send('Connection: close\r\n') | |
sock_out:send('\r\n') | |
local response = sock_out:receive('*a') | |
if line then sock_in:send(response) end | |
end | |
server = socket.bind('localhost', PORT) | |
while true do | |
local sock_in = server:accept() | |
handler(sock_in) | |
end | |
copas.addserver(socket.bind('localhost', PORT), handler) | |
copas.loop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function handler(sock_in) | |
sock_in = copas.wrap(sock_in) | |
local line = sock_in:receive('*l') | |
-- where to? | |
local url = string.match(line, 'http://([%a%d\.-]+):*%d*/') | |
local port = string.match(line, 'http://[%a%d\.-]+:(%d+)/') | |
local sock_out = socket.connect(url, port or 80) | |
sock_out = copas.wrap(sock_out) | |
repeat | |
if line then sock_out:send(line..'\r\n') end | |
line = sock_in:receive('*l') | |
until line == '' | |
sock_out:send('Connection: close\r\n') | |
sock_out:send('\r\n') | |
local response = sock_out:receive('*a') | |
if line then sock_in:send(response) end | |
end | |
server = socket.bind('localhost', PORT) | |
while true do | |
local sock_in = server:accept() | |
handler(sock_in) | |
end | |
copas.addserver(socket.bind('localhost', PORT), handler) | |
copas.loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment