Skip to content

Instantly share code, notes, and snippets.

@mtgto
Last active December 20, 2015 05:49
Show Gist options
  • Save mtgto/6081685 to your computer and use it in GitHub Desktop.
Save mtgto/6081685 to your computer and use it in GitHub Desktop.
ゴミHTTP Proxy (HEAD, GETのみ対応)
require 'socket'
require 'net/http'
require 'uri'
class Proxy
PORT = 8888
def initialize
@server_socket = TCPServer.open(nil, PORT)
end
def start
while true
Thread.start(@server_socket.accept) do |s|
method, url, version = get_base_header(s)
uri = URI.parse(url)
Net::HTTP.start(uri.host, uri.port) {|h|
case method
when "HEAD"
res = h.head(uri.request_uri)
when "GET"
res = h.get(uri.request_uri)
else
raise RuntimeError.new("サポートしてないプロトコル: #{method}")
end
s.puts "HTTP/#{res.http_version} #{res.code} #{res.message}"
res.each_capitalized {|k, v| s.puts "#{k}: #{v}"}
s.puts
s.write res.body
}
s.close
end
end
end
def get_base_header(s)
# TODO: 何時まで経っても改行コードを書かない悪い子がいたらどうしよう
l = s.gets
l.split
end
end
proxy = Proxy.new
proxy.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment