http 0.5.1 → 0.6.0.pre
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of http might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.gitignore +3 -3
- data/.rspec +3 -2
- data/.rubocop.yml +101 -0
- data/.travis.yml +19 -8
- data/Gemfile +24 -6
- data/LICENSE.txt +1 -1
- data/README.md +144 -29
- data/Rakefile +23 -1
- data/examples/parallel_requests_with_celluloid.rb +2 -2
- data/http.gemspec +14 -14
- data/lib/http.rb +5 -4
- data/lib/http/authorization_header.rb +37 -0
- data/lib/http/authorization_header/basic_auth.rb +24 -0
- data/lib/http/authorization_header/bearer_token.rb +29 -0
- data/lib/http/backports.rb +2 -0
- data/lib/http/backports/base64.rb +6 -0
- data/lib/http/{uri_backport.rb → backports/uri.rb} +10 -10
- data/lib/http/chainable.rb +24 -25
- data/lib/http/client.rb +97 -67
- data/lib/http/content_type.rb +27 -0
- data/lib/http/errors.rb +13 -0
- data/lib/http/headers.rb +154 -0
- data/lib/http/headers/mixin.rb +11 -0
- data/lib/http/mime_type.rb +61 -36
- data/lib/http/mime_type/adapter.rb +24 -0
- data/lib/http/mime_type/json.rb +23 -0
- data/lib/http/options.rb +21 -48
- data/lib/http/redirector.rb +12 -7
- data/lib/http/request.rb +82 -33
- data/lib/http/request/writer.rb +79 -0
- data/lib/http/response.rb +39 -68
- data/lib/http/response/body.rb +62 -0
- data/lib/http/{response_parser.rb → response/parser.rb} +3 -1
- data/lib/http/version.rb +1 -1
- data/logo.png +0 -0
- data/spec/http/authorization_header/basic_auth_spec.rb +29 -0
- data/spec/http/authorization_header/bearer_token_spec.rb +36 -0
- data/spec/http/authorization_header_spec.rb +41 -0
- data/spec/http/backports/base64_spec.rb +13 -0
- data/spec/http/client_spec.rb +181 -0
- data/spec/http/content_type_spec.rb +47 -0
- data/spec/http/headers/mixin_spec.rb +36 -0
- data/spec/http/headers_spec.rb +417 -0
- data/spec/http/options/body_spec.rb +6 -7
- data/spec/http/options/form_spec.rb +4 -5
- data/spec/http/options/headers_spec.rb +9 -17
- data/spec/http/options/json_spec.rb +17 -0
- data/spec/http/options/merge_spec.rb +18 -19
- data/spec/http/options/new_spec.rb +5 -19
- data/spec/http/options/proxy_spec.rb +6 -6
- data/spec/http/options_spec.rb +3 -9
- data/spec/http/redirector_spec.rb +100 -0
- data/spec/http/request/writer_spec.rb +25 -0
- data/spec/http/request_spec.rb +54 -14
- data/spec/http/response/body_spec.rb +24 -0
- data/spec/http/response_spec.rb +61 -32
- data/spec/http_spec.rb +77 -86
- data/spec/spec_helper.rb +25 -2
- data/spec/support/example_server.rb +58 -49
- data/spec/support/proxy_server.rb +27 -11
- metadata +60 -55
- data/lib/http/header.rb +0 -11
- data/lib/http/mime_types/json.rb +0 -19
- data/lib/http/request_stream.rb +0 -77
- data/spec/http/options/callbacks_spec.rb +0 -62
- data/spec/http/options/response_spec.rb +0 -24
- data/spec/http/request_stream_spec.rb +0 -25
@@ -1,17 +1,33 @@
|
|
1
1
|
require 'webrick/httpproxy'
|
2
2
|
|
3
|
-
|
3
|
+
handler = proc do | req, res |
|
4
|
+
res['X-PROXIED'] = true
|
5
|
+
end
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
+
ProxyServer = WEBrick::HTTPProxyServer.new(
|
8
|
+
:Port => 8080,
|
9
|
+
:AccessLog => [],
|
10
|
+
:RequestCallback => handler
|
11
|
+
)
|
7
12
|
|
8
|
-
AuthenticatedProxyServer = WEBrick::HTTPProxyServer.new(
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
AuthenticatedProxyServer = WEBrick::HTTPProxyServer.new(
|
14
|
+
:Port => 8081,
|
15
|
+
:ProxyAuthProc => proc do | req, res |
|
16
|
+
WEBrick::HTTPAuth.proxy_basic_auth(req, res, 'proxy') do | user, pass |
|
17
|
+
user == 'username' && pass == 'password'
|
18
|
+
end
|
19
|
+
end,
|
20
|
+
:RequestCallback => handler
|
21
|
+
)
|
14
22
|
|
23
|
+
Thread.new { ProxyServer.start }
|
24
|
+
trap('INT') do
|
25
|
+
ProxyServer.shutdown
|
26
|
+
exit
|
27
|
+
end
|
15
28
|
|
16
|
-
|
17
|
-
trap(
|
29
|
+
Thread.new { AuthenticatedProxyServer.start }
|
30
|
+
trap('INT') do
|
31
|
+
AuthenticatedProxyServer.shutdown
|
32
|
+
exit
|
33
|
+
end
|
metadata
CHANGED
@@ -1,71 +1,44 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Tony
|
7
|
+
- Tony
|
8
|
+
- Arcieri
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2014-
|
12
|
+
date: 2014-03-29 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: http_parser.rb
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
|
-
- - "
|
18
|
+
- - "~>"
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
+
version: 0.6.0
|
20
21
|
type: :runtime
|
21
22
|
prerelease: false
|
22
23
|
version_requirements: !ruby/object:Gem::Requirement
|
23
24
|
requirements:
|
24
|
-
- - "
|
25
|
+
- - "~>"
|
25
26
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
27
|
+
version: 0.6.0
|
27
28
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
29
|
+
name: bundler
|
29
30
|
requirement: !ruby/object:Gem::Requirement
|
30
31
|
requirements:
|
31
|
-
- - "
|
32
|
+
- - "~>"
|
32
33
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
+
version: '1.0'
|
34
35
|
type: :development
|
35
36
|
prerelease: false
|
36
37
|
version_requirements: !ruby/object:Gem::Requirement
|
37
38
|
requirements:
|
38
|
-
- - "
|
39
|
+
- - "~>"
|
39
40
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rspec
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '2.11'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '2.11'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: json
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
41
|
+
version: '1.0'
|
69
42
|
description: HTTP so awesome it will lure Catherine Zeta Jones into your unicorn petting
|
70
43
|
zoo
|
71
44
|
email:
|
@@ -77,6 +50,7 @@ files:
|
|
77
50
|
- ".coveralls.yml"
|
78
51
|
- ".gitignore"
|
79
52
|
- ".rspec"
|
53
|
+
- ".rubocop.yml"
|
80
54
|
- ".travis.yml"
|
81
55
|
- CHANGES.md
|
82
56
|
- Gemfile
|
@@ -87,37 +61,58 @@ files:
|
|
87
61
|
- examples/parallel_requests_with_celluloid.rb
|
88
62
|
- http.gemspec
|
89
63
|
- lib/http.rb
|
64
|
+
- lib/http/authorization_header.rb
|
65
|
+
- lib/http/authorization_header/basic_auth.rb
|
66
|
+
- lib/http/authorization_header/bearer_token.rb
|
67
|
+
- lib/http/backports.rb
|
68
|
+
- lib/http/backports/base64.rb
|
69
|
+
- lib/http/backports/uri.rb
|
90
70
|
- lib/http/chainable.rb
|
91
71
|
- lib/http/client.rb
|
92
|
-
- lib/http/
|
72
|
+
- lib/http/content_type.rb
|
73
|
+
- lib/http/errors.rb
|
74
|
+
- lib/http/headers.rb
|
75
|
+
- lib/http/headers/mixin.rb
|
93
76
|
- lib/http/mime_type.rb
|
94
|
-
- lib/http/
|
77
|
+
- lib/http/mime_type/adapter.rb
|
78
|
+
- lib/http/mime_type/json.rb
|
95
79
|
- lib/http/options.rb
|
96
80
|
- lib/http/redirector.rb
|
97
81
|
- lib/http/request.rb
|
98
|
-
- lib/http/
|
82
|
+
- lib/http/request/writer.rb
|
99
83
|
- lib/http/response.rb
|
100
|
-
- lib/http/
|
101
|
-
- lib/http/
|
84
|
+
- lib/http/response/body.rb
|
85
|
+
- lib/http/response/parser.rb
|
102
86
|
- lib/http/version.rb
|
87
|
+
- logo.png
|
88
|
+
- spec/http/authorization_header/basic_auth_spec.rb
|
89
|
+
- spec/http/authorization_header/bearer_token_spec.rb
|
90
|
+
- spec/http/authorization_header_spec.rb
|
91
|
+
- spec/http/backports/base64_spec.rb
|
92
|
+
- spec/http/client_spec.rb
|
93
|
+
- spec/http/content_type_spec.rb
|
94
|
+
- spec/http/headers/mixin_spec.rb
|
95
|
+
- spec/http/headers_spec.rb
|
103
96
|
- spec/http/options/body_spec.rb
|
104
|
-
- spec/http/options/callbacks_spec.rb
|
105
97
|
- spec/http/options/form_spec.rb
|
106
98
|
- spec/http/options/headers_spec.rb
|
99
|
+
- spec/http/options/json_spec.rb
|
107
100
|
- spec/http/options/merge_spec.rb
|
108
101
|
- spec/http/options/new_spec.rb
|
109
102
|
- spec/http/options/proxy_spec.rb
|
110
|
-
- spec/http/options/response_spec.rb
|
111
103
|
- spec/http/options_spec.rb
|
104
|
+
- spec/http/redirector_spec.rb
|
105
|
+
- spec/http/request/writer_spec.rb
|
112
106
|
- spec/http/request_spec.rb
|
113
|
-
- spec/http/
|
107
|
+
- spec/http/response/body_spec.rb
|
114
108
|
- spec/http/response_spec.rb
|
115
109
|
- spec/http_spec.rb
|
116
110
|
- spec/spec_helper.rb
|
117
111
|
- spec/support/example_server.rb
|
118
112
|
- spec/support/proxy_server.rb
|
119
113
|
homepage: https://github.com/tarcieri/http
|
120
|
-
licenses:
|
114
|
+
licenses:
|
115
|
+
- MIT
|
121
116
|
metadata: {}
|
122
117
|
post_install_message:
|
123
118
|
rdoc_options: []
|
@@ -130,29 +125,39 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
130
125
|
version: '0'
|
131
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
127
|
requirements:
|
133
|
-
- - "
|
128
|
+
- - ">"
|
134
129
|
- !ruby/object:Gem::Version
|
135
|
-
version:
|
130
|
+
version: 1.3.1
|
136
131
|
requirements: []
|
137
132
|
rubyforge_project:
|
138
|
-
rubygems_version: 2.2.
|
133
|
+
rubygems_version: 2.2.0
|
139
134
|
signing_key:
|
140
135
|
specification_version: 4
|
141
136
|
summary: HTTP should be easy
|
142
137
|
test_files:
|
138
|
+
- spec/http/authorization_header/basic_auth_spec.rb
|
139
|
+
- spec/http/authorization_header/bearer_token_spec.rb
|
140
|
+
- spec/http/authorization_header_spec.rb
|
141
|
+
- spec/http/backports/base64_spec.rb
|
142
|
+
- spec/http/client_spec.rb
|
143
|
+
- spec/http/content_type_spec.rb
|
144
|
+
- spec/http/headers/mixin_spec.rb
|
145
|
+
- spec/http/headers_spec.rb
|
143
146
|
- spec/http/options/body_spec.rb
|
144
|
-
- spec/http/options/callbacks_spec.rb
|
145
147
|
- spec/http/options/form_spec.rb
|
146
148
|
- spec/http/options/headers_spec.rb
|
149
|
+
- spec/http/options/json_spec.rb
|
147
150
|
- spec/http/options/merge_spec.rb
|
148
151
|
- spec/http/options/new_spec.rb
|
149
152
|
- spec/http/options/proxy_spec.rb
|
150
|
-
- spec/http/options/response_spec.rb
|
151
153
|
- spec/http/options_spec.rb
|
154
|
+
- spec/http/redirector_spec.rb
|
155
|
+
- spec/http/request/writer_spec.rb
|
152
156
|
- spec/http/request_spec.rb
|
153
|
-
- spec/http/
|
157
|
+
- spec/http/response/body_spec.rb
|
154
158
|
- spec/http/response_spec.rb
|
155
159
|
- spec/http_spec.rb
|
156
160
|
- spec/spec_helper.rb
|
157
161
|
- spec/support/example_server.rb
|
158
162
|
- spec/support/proxy_server.rb
|
163
|
+
has_rdoc:
|
data/lib/http/header.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
module HTTP
|
2
|
-
module Header
|
3
|
-
# Matches HTTP header names when in "Canonical-Http-Format"
|
4
|
-
CANONICAL_HEADER = /^[A-Z][a-z]*(-[A-Z][a-z]*)*$/
|
5
|
-
|
6
|
-
# Transform to canonical HTTP header capitalization
|
7
|
-
def canonicalize_header(header)
|
8
|
-
header.to_s.split(/[\-_]/).map(&:capitalize).join('-')
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
data/lib/http/mime_types/json.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
json = HTTP::MimeType.new 'application/json', :json
|
2
|
-
|
3
|
-
json.parse_with do |obj|
|
4
|
-
if defined?(JSON) and JSON.respond_to? :parse
|
5
|
-
JSON.parse(obj)
|
6
|
-
else
|
7
|
-
obj
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
json.emit_with do |obj|
|
12
|
-
if obj.is_a? String
|
13
|
-
obj
|
14
|
-
elsif obj.respond_to? :to_json
|
15
|
-
obj.to_json
|
16
|
-
else
|
17
|
-
obj
|
18
|
-
end
|
19
|
-
end
|
data/lib/http/request_stream.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
module HTTP
|
2
|
-
class RequestStream
|
3
|
-
|
4
|
-
# CRLF is the universal HTTP delimiter
|
5
|
-
CRLF = "\r\n"
|
6
|
-
|
7
|
-
def initialize(socket, body, headers, headerstart)
|
8
|
-
@body = body
|
9
|
-
raise ArgumentError, "body of wrong type" unless valid_body_type
|
10
|
-
@socket = socket
|
11
|
-
@headers = headers
|
12
|
-
@request_header = [headerstart]
|
13
|
-
end
|
14
|
-
|
15
|
-
def valid_body_type
|
16
|
-
valid_types= [String, NilClass, Enumerable]
|
17
|
-
checks = valid_types.map {|type| @body.is_a? type}
|
18
|
-
checks.any?
|
19
|
-
end
|
20
|
-
|
21
|
-
#Adds headers to the request header from the headers array
|
22
|
-
def add_headers
|
23
|
-
@headers.each do |field, value|
|
24
|
-
@request_header << "#{field}: #{value}"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# Stream the request to a socket
|
29
|
-
def stream
|
30
|
-
self.send_request_header
|
31
|
-
self.send_request_body
|
32
|
-
end
|
33
|
-
|
34
|
-
# Adds the headers to the header array for the given request body we are working
|
35
|
-
# with
|
36
|
-
def add_body_type_headers
|
37
|
-
if @body.is_a? String and not @headers['Content-Length']
|
38
|
-
@request_header << "Content-Length: #{@body.length}"
|
39
|
-
elsif @body.is_a? Enumerable
|
40
|
-
if encoding = @headers['Transfer-Encoding'] and not encoding == "chunked"
|
41
|
-
raise ArgumentError, "invalid transfer encoding"
|
42
|
-
else
|
43
|
-
@request_header << "Transfer-Encoding: chunked"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
# Joins the headers specified in the request into a correctly formatted
|
49
|
-
# http request header string
|
50
|
-
def join_headers
|
51
|
-
# join the headers array with crlfs, stick two on the end because
|
52
|
-
# that ends the request header
|
53
|
-
@request_header.join(CRLF) + (CRLF)*2
|
54
|
-
end
|
55
|
-
|
56
|
-
def send_request_header
|
57
|
-
self.add_headers
|
58
|
-
self.add_body_type_headers
|
59
|
-
header = self.join_headers
|
60
|
-
|
61
|
-
@socket << header
|
62
|
-
end
|
63
|
-
|
64
|
-
def send_request_body
|
65
|
-
if @body.is_a? String
|
66
|
-
@socket << @body
|
67
|
-
elsif @body.is_a? Enumerable
|
68
|
-
@body.each do |chunk|
|
69
|
-
@socket << chunk.bytesize.to_s(16) << CRLF
|
70
|
-
@socket << chunk
|
71
|
-
end
|
72
|
-
|
73
|
-
@socket << "0" << CRLF * 2
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe HTTP::Options, "callbacks" do
|
4
|
-
|
5
|
-
let(:opts){ HTTP::Options.new }
|
6
|
-
let(:callback){ Proc.new{|r| nil } }
|
7
|
-
|
8
|
-
it 'recognizes invalid events' do
|
9
|
-
expect {
|
10
|
-
opts.with_callback(:notacallback, callback)
|
11
|
-
}.to raise_error(ArgumentError, /notacallback/)
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'recognizes invalid callbacks' do
|
15
|
-
expect {
|
16
|
-
opts.with_callback(:request, Object.new)
|
17
|
-
}.to raise_error(ArgumentError, /invalid callback/)
|
18
|
-
expect {
|
19
|
-
opts.with_callback(:request, Proc.new{|a,b| nil})
|
20
|
-
}.to raise_error(ArgumentError, /only one argument/)
|
21
|
-
end
|
22
|
-
|
23
|
-
describe "request" do
|
24
|
-
|
25
|
-
it 'defaults to []' do
|
26
|
-
expect(opts.callbacks[:request]).to eq([])
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'may be specified with with_callback(:request, ...)' do
|
30
|
-
|
31
|
-
opts2 = opts.with_callback(:request, callback)
|
32
|
-
expect(opts.callbacks[:request]).to eq([])
|
33
|
-
expect(opts2.callbacks[:request]).to eq([callback])
|
34
|
-
|
35
|
-
opts3 = opts2.with_callback(:request, callback)
|
36
|
-
expect(opts2.callbacks[:request]).to eq([callback])
|
37
|
-
expect(opts3.callbacks[:request]).to eq([callback, callback])
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "response" do
|
43
|
-
|
44
|
-
it 'defaults to []' do
|
45
|
-
expect(opts.callbacks[:response]).to eq([])
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'may be specified with with_callback(:response, ...)' do
|
49
|
-
|
50
|
-
opts2 = opts.with_callback(:response, callback)
|
51
|
-
expect(opts.callbacks[:response]).to eq([])
|
52
|
-
expect(opts2.callbacks[:response]).to eq([callback])
|
53
|
-
|
54
|
-
opts3 = opts2.with_callback(:response, callback)
|
55
|
-
expect(opts2.callbacks[:response]).to eq([callback])
|
56
|
-
expect(opts3.callbacks[:response]).to eq([callback, callback])
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe HTTP::Options, "response" do
|
4
|
-
|
5
|
-
let(:opts){ HTTP::Options.new }
|
6
|
-
|
7
|
-
it 'defaults to :auto' do
|
8
|
-
expect(opts.response).to eq(:auto)
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'may be specified with with_response' do
|
12
|
-
opts2 = opts.with_response(:body)
|
13
|
-
expect(opts.response).to eq(:auto)
|
14
|
-
expect(opts2.response).to eq(:body)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'recognizes invalid responses' do
|
18
|
-
expect {
|
19
|
-
opts.with_response(:not_a_valid_response)
|
20
|
-
}.to raise_error(ArgumentError, /not_a_valid_response/)
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|