-
-
Save guange2015/3930574 to your computer and use it in GitHub Desktop.
Revisions
-
torsten revised this gist
Mar 6, 2009 . 1 changed file with 10 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -17,19 +17,19 @@ def run port # Handle every request in another thread loop do s = @socket.accept Thread.new s, &method(:handle_request) end # CTRL-C rescue Interrupt puts 'Got Interrupt..' # Ensure that we release the socket on errors ensure if @socket @socket.close puts 'Socked closed..' end puts 'Quitting.' end end @@ -49,20 +49,16 @@ def handle_request to_client content_len = 0 loop do line = to_client.readline if line =~ /^Content-Length:\s+(\d+)\s*$/ content_len = $1.to_i end # Strip proxy headers if line =~ /^proxy/i next elsif line.strip.empty? to_server.write("Connection: close\r\n\r\n") @@ -76,9 +72,12 @@ def handle_request to_client end end buff = "" loop do to_server.read(4048, buff) to_client.write(buff) break if buff.size < 4048 end # Close the sockets to_client.close -
torsten revised this gist
Mar 5, 2009 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,4 @@ #!/usr/bin/env ruby # A quick and dirty implementation of an HTTP proxy server in Ruby # because I did not want to install anything. # @@ -54,7 +53,7 @@ def handle_request to_client loop do line = to_client.readline # puts line if line =~ /content|len|agent|proxy|x-/i if line =~ /^Content-Length:\s+(\d+)\s*$/ content_len = $1.to_i -
torsten revised this gist
Mar 5, 2009 . 1 changed file with 25 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -23,45 +23,61 @@ def run port # CTRL-C rescue Interrupt puts 'got interrupt..' # Ensure that we release the socket on errors ensure if @socket @socket.close puts 'socked closed..' end puts 'quiting.' end end def handle_request to_client request_line = to_client.readline verb = request_line[/^\w+/] url = request_line[/^\w+\s+(\S+)/, 1] version = request_line[/HTTP\/(1\.\d)\s*$/, 1] uri = URI::parse url # Show what got requested puts((" %4s "%verb) + url) to_server = TCPSocket.new(uri.host, (uri.port.nil? ? 80 : uri.port)) to_server.write("#{verb} #{uri.path}?#{uri.query} HTTP/#{version}\r\n") content_len = 0 # TODO: keep connection to client alive loop do line = to_client.readline puts line if line =~ /content|len|agent|proxy|auth|x-/i if line =~ /^Content-Length:\s+(\d+)\s*$/ content_len = $1.to_i end # Strip proxy headers if line =~ /^proxy/i next # Inject connection: close 'cuz I am lazy elsif line.strip.empty? to_server.write("Connection: close\r\n\r\n") if content_len >= 0 to_server.write(to_client.read(content_len)) end break else to_server.write(line) end end # TODO: make it read in 4K blocks or so answer = to_server.read to_client.write(answer) -
torsten revised this gist
Mar 5, 2009 . 1 changed file with 31 additions and 40 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ #!/usr/bin/env ruby # A quick and dirty implementation of an HTTP proxy server in Ruby # because I did not want to install anything. # # Copyright (C) 2009 Torsten Becker <[email protected]> @@ -12,84 +12,75 @@ class Proxy def run port begin # Start our server to handle connections (will raise things on errors) @socket = TCPServer.new port # Handle every request in another thread loop do s = @socket.accept Thread.new(s, &method(:handle_request)) end # CTRL-C rescue Interrupt puts 'Got Interrupt..' # Ensure that we release the socket on errors ensure if @socket @socket.close puts 'Socked closed..' end puts 'Quitting.' end end def handle_request to_client request_line = to_client.readline verb = request_line[/^\w+/] url = request_line[/^\w+\s+(\S+)/, 1] uri = URI::parse url # Show what got requested puts(("%6s "%verb) + url) to_server = TCPSocket.new(uri.host, (uri.port.nil? ? 80 : uri.port)) to_server.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.1\r\n") loop do line = to_client.readline # Strip proxy headers if line =~ /^proxy/i next # Inject connection: close 'cuz I am lazy elsif line.strip.empty? to_server.write("Connection: close\r\n\r\n") break else to_server.write(line) end end answer = to_server.read to_client.write(answer) # Close the sockets to_client.close to_server.close end end # Get parameters and start the server if ARGV.empty? port = 8008 elsif ARGV.size == 1 port = ARGV[0].to_i else puts 'Usage: proxy.rb [port]' exit 1 end Proxy.new.run port -
torsten revised this gist
Mar 5, 2009 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,9 +9,7 @@ require 'uri' class Proxy def run port begin # start our server to handle connections (will raise things on errors) @@ -52,10 +50,11 @@ def handle_request socket url = request_line[/^\w+\s+(\S+)/, 1] uri = URI::parse url # Show what gets requested puts(("%-4s "%verb) + url) client = TCPSocket.new(uri.host, 80) client.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.1\r\n") loop do line = socket.readline @@ -93,4 +92,5 @@ def handle_request socket end Proxy.new.run port -
torsten revised this gist
Mar 5, 2009 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,7 @@ # A quick and dirty implementation of a proxy server in Ruby # because I did not want to install anything. # # Copyright (C) 2009 Torsten Becker <[email protected]> require 'socket' require 'uri' @@ -95,4 +94,3 @@ def handle_request socket end Proxy.new().run port -
torsten created this gist
Mar 5, 2009 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,98 @@ #!/usr/bin/env ruby # A quick and dirty implementation of a proxy server in Ruby # because I did not want to install anything. # # Copyright (C) 2009 Torsten Becker <[email protected]>. require 'socket' require 'uri' # Its Proxy class Proxy def run port begin # start our server to handle connections (will raise things on errors) @socket = TCPServer.new port # handle every request with another method while true s = @socket.accept Thread.new(s) do |r| handle_request r end end # ctrl-c rescue Interrupt puts 'got interrupt..' # get all possibly errors rescue => e puts(e.class.to_s + ': ' + e.message) # ensure that we release the socket on errors ensure if @socket @socket.close puts 'socked closed..' end puts 'quiting.' end end def handle_request socket request_line = socket.readline verb = request_line[/^\w+/] url = request_line[/^\w+\s+(\S+)/, 1] uri = URI::parse url puts url client = TCPSocket.new(uri.host, 80) client.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.0\r\n") loop do line = socket.readline # Inject connection: close 'cuz I am lazy if line.strip.empty? client.write("Connection: close\r\n\r\n") break else client.write(line) end end answer = client.read socket.write(answer) # close the sockets client.close socket.close end end # get parameters and start the server if ARGV.empty? port = 8008 elsif ARGV.size == 1 port = ARGV[0].to_i else puts 'Call me like this: proxy.rb [port]' exit -1 end Proxy.new().run port