webproxy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/webproxy +8 -0
  2. data/lib/webproxy.rb +62 -0
  3. metadata +86 -0
data/bin/webproxy ADDED
@@ -0,0 +1,8 @@
1
+ #! /usr/bin/env ruby
2
+ require 'webproxy'
3
+
4
+ if ARGV.length == 1
5
+ WebProxy.run!(ARGV.first)
6
+ else
7
+ puts "Usage: webproxy http://www.google.com"
8
+ end
data/lib/webproxy.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'rubygems'
2
+ require 'rack'
3
+ require 'httparty' # <--- I should remove this as a dependency and just use Net::HTTP ... if I ever actually use this code for anything
4
+
5
+ class WebProxy
6
+
7
+ def self.run!(host)
8
+ handler = Rack::Handler::WEBrick
9
+
10
+ begin
11
+ require 'thin'
12
+ handler = Rack::Handler::Thin
13
+ rescue LoadError
14
+ begin
15
+ require 'mongrel'
16
+ handler = Rack::Handler::Mongrel
17
+ rescue LoadError
18
+ end
19
+ end
20
+
21
+ handler.run WebProxy.new(host)
22
+ end
23
+
24
+ def initialize host
25
+ @host = host
26
+ end
27
+
28
+ def call env
29
+ url = File.join @host, env['PATH_INFO']
30
+ method = env['REQUEST_METHOD'].downcase
31
+
32
+ puts "#{env['REQUEST_METHOD']} #{url}"
33
+
34
+ # make request
35
+ response = HTTParty.send method, url, :body => _get_request_body(env), :headers => _get_request_headers(env), :format => 'text'
36
+
37
+ # return response
38
+ [ response.code, _get_response_headers(response), [response.body] ]
39
+ end
40
+
41
+ # env['rack.input'] returns an IO object that you must #each over to get the full body
42
+ def _get_request_body env
43
+ body = ''
44
+ env['rack.input'].each {|string| body << string }
45
+ body
46
+ end
47
+
48
+ # response.headers returns an object that wraps the actual Hash of headers ... this gets the actual Hash
49
+ def _get_response_headers response
50
+ response_headers = response.headers.instance_variable_get('@header')
51
+ response_headers.each {|k, v| response_headers[k] = response_headers[k][0] } # values are all in arrays
52
+ response_headers
53
+ end
54
+
55
+ # we should pass along all HTTP_* headers and we need to CHANGE the HTTP_HOST header to reflect the new host we're making the request to
56
+ def _get_request_headers env
57
+ headers = {}
58
+ env.each {|k,v| if k =~ /HTTP_(\w+)/ then headers[$1] = v end }
59
+ headers.delete 'HOST' # simply delete it and let HTTP do the rest
60
+ headers
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webproxy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - remi
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-17 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rack
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: httparty
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ description: proxy requests to remote hosts
45
+ email: remi@remitaylor.com
46
+ executables:
47
+ - webproxy
48
+ extensions: []
49
+
50
+ extra_rdoc_files: []
51
+
52
+ files:
53
+ - lib/webproxy.rb
54
+ - bin/webproxy
55
+ has_rdoc: true
56
+ homepage: http://github.com/remi/webproxy
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: proxy requests to remote hosts
85
+ test_files: []
86
+