Created
March 31, 2011 13:39
-
-
Save stenno/896358 to your computer and use it in GitHub Desktop.
Proxy in Ruby, very dirty
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
#Proxy in Ruby, very dirty, supports chunking (kinda), don't expect it to work | |
#OOP version will be available soon | |
#See it as an example ... ;) | |
require 'net/http' | |
require 'uri' | |
TCPServer.open("127.0.0.1", 55555) do |server| | |
s = server.accept | |
req = s.recvfrom(4096) | |
splitter1 = req[0].split("\r\n") | |
splitter1.delete_if {|elem| elem =~ /.*Content-Encoding: compressed.*|.*Accept-Encoding.*/} | |
splitter2 = splitter1[0].split(" ") | |
uri = URI.parse(splitter2[1]) | |
splitter2[1] = "#{uri.path}" | |
splitter1[0] = splitter2.join(" ") | |
splitter1 = splitter1.join("\r\n") | |
req[0] = splitter1 | |
$received = [] | |
TCPSocket.open(uri.host, 80) do |conn| | |
conn.print(req[0] + "\r\n\r\n") | |
bla = false | |
counter = 0 | |
while !bla | |
puts "Got chunk #{counter}" | |
$received.push conn.recvfrom(1408)[0] | |
counter += 1 | |
puts $received.last.inspect | |
bla = ($received.last[-5..-1] == "0\r\n\r\n" or ($received.first.index(/.*chunked.*/) == nil)) | |
end | |
conn.close | |
end | |
puts "done receiving" | |
s.print $received.join | |
s.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment