Created
July 15, 2010 08:47
-
-
Save morygonzalez/476679 to your computer and use it in GitHub Desktop.
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 characters
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'cgi' | |
require 'rubygems' | |
require 'hpricot' | |
class Proxy | |
def initialize | |
@url = nil | |
@path = nil | |
get_path | |
end | |
# URL中の"url"引数からurlを取得 | |
def get_url | |
cgi = CGI.new | |
url = cgi.params["url"] | |
return url[0] | |
end | |
# URL中のドメインとパスを正規表現で分離 | |
def get_path | |
if /(.+?)(\/.*)/ =~ get_url | |
@url = $1 | |
@path = $2 | |
else | |
@url = get_url | |
@path = "/" | |
end | |
end | |
# HTTPリクエストヘッダを書き換え | |
def http_get | |
http = Net::HTTP.new("#{@url}") | |
response = http.request_get("#{@path}", "User-Agent" => "DoCoMo/2.0 N901iS(c100;TB;W24H12)") | |
print "Content-Type: ", response['content-type'], "\n\n" | |
response.body | |
end | |
end | |
proxy = "http://localhost/proxy/proxy.rb?url=" | |
output = Proxy.new | |
# puts output.http_get | |
doc = Hpricot(output.http_get) | |
(doc/:a).each do |link| | |
link[:href] = link[:href].sub(/\?.+/, "") | |
unless /http:\/\// =~ link[:href] | |
link[:href] = link[:href].sub(/(\.\/)?/, proxy + output.get_url + "/") | |
end | |
end | |
(doc/:img).each do |img| | |
img[:src] = img[:src].sub(/\?.+/, "") | |
unless /http:\/\// =~ img[:src] | |
img[:src] = img[:src].sub(/(\.\/)?/, proxy + output.get_url + "/") | |
end | |
end | |
puts doc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment