Skip to content

Instantly share code, notes, and snippets.

@pirj
Created April 19, 2010 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirj/371399 to your computer and use it in GitHub Desktop.
Save pirj/371399 to your computer and use it in GitHub Desktop.
http proxy
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
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()
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