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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b647051e277907433baf4b118a8b63a3d04aa67b
|
4
|
+
data.tar.gz: 30e721ae9be7d37ad8e9359ad0d52dce0f6a8d16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2531298e15fc793c052a03e524875151c91d8eb111fdb9c6886546009cf827dab8d51eb8158052cdaaa1edcb968a63c4be89350fc357cc1972a20c04693429c3
|
7
|
+
data.tar.gz: ecf369955e78188f2d6eff9c159b55beb9d160a9ce36f6acb5472b5d1c9cd5b374b848582901861389da4ba0e0b8ead5d7f92b1d223ec1ed30212839f321aa3f
|
data/.gitignore
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
*.gem
|
2
|
-
*.rbc
|
3
2
|
.bundle
|
4
3
|
.config
|
4
|
+
.rvmrc
|
5
5
|
.yardoc
|
6
6
|
Gemfile.lock
|
7
7
|
InstalledFiles
|
8
8
|
_yardoc
|
9
9
|
coverage
|
10
|
-
doc
|
10
|
+
doc
|
11
11
|
lib/bundler/man
|
12
|
+
measurement
|
12
13
|
pkg
|
13
14
|
rdoc
|
14
15
|
spec/reports
|
15
16
|
test/tmp
|
16
17
|
test/version_tmp
|
17
18
|
tmp
|
18
|
-
.rvmrc
|
data/.rspec
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
AllCops:
|
2
|
+
Includes:
|
3
|
+
- 'Gemfile'
|
4
|
+
- 'Rakefile'
|
5
|
+
- 'http.gemspec'
|
6
|
+
|
7
|
+
# Avoid long parameter lists
|
8
|
+
ParameterLists:
|
9
|
+
Max: 3
|
10
|
+
CountKeywordArgs: true
|
11
|
+
|
12
|
+
MethodLength:
|
13
|
+
CountComments: false
|
14
|
+
Max: 31 # TODO: lower to 15
|
15
|
+
|
16
|
+
ClassLength:
|
17
|
+
CountComments: false
|
18
|
+
Max: 132 # TODO: lower to 100
|
19
|
+
|
20
|
+
CyclomaticComplexity:
|
21
|
+
Max: 13 # TODO: lower to 6
|
22
|
+
|
23
|
+
# Avoid more than `Max` levels of nesting.
|
24
|
+
BlockNesting:
|
25
|
+
Max: 3
|
26
|
+
|
27
|
+
# Do not force public/protected/private keyword to be indented at the same
|
28
|
+
# level as the def keyword. My personal preference is to outdent these keywords
|
29
|
+
# because I think when scanning code it makes it easier to identify the
|
30
|
+
# sections of code and visually separate them. When the keyword is at the same
|
31
|
+
# level I think it sort of blends in with the def keywords and makes it harder
|
32
|
+
# to scan the code and see where the sections are.
|
33
|
+
AccessModifierIndentation:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
# Limit line length
|
37
|
+
LineLength:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
# Disable documentation checking until a class needs to be documented once
|
41
|
+
Documentation:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
# Not all trivial readers/writers can be defined with attr_* methods
|
45
|
+
TrivialAccessors:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
# Enforce Ruby 1.8-compatible hash syntax
|
49
|
+
HashSyntax:
|
50
|
+
EnforcedStyle: hash_rockets
|
51
|
+
|
52
|
+
# No spaces inside hash literals
|
53
|
+
SpaceInsideHashLiteralBraces:
|
54
|
+
EnforcedStyle: no_space
|
55
|
+
|
56
|
+
# Allow dots at the end of lines
|
57
|
+
DotPosition:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
# Don't require magic comment at the top of every file
|
61
|
+
Encoding:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
# Enforce outdenting of access modifiers (i.e. public, private, protected)
|
65
|
+
AccessModifierIndentation:
|
66
|
+
EnforcedStyle: outdent
|
67
|
+
|
68
|
+
EmptyLinesAroundAccessModifier:
|
69
|
+
Enabled: true
|
70
|
+
|
71
|
+
# Align ends correctly
|
72
|
+
EndAlignment:
|
73
|
+
AlignWith: variable
|
74
|
+
|
75
|
+
# Indentation of when/else
|
76
|
+
CaseIndentation:
|
77
|
+
IndentWhenRelativeTo: end
|
78
|
+
IndentOneStep: false
|
79
|
+
|
80
|
+
# Use the old lambda literal syntax
|
81
|
+
Lambda:
|
82
|
+
Enabled: false
|
83
|
+
|
84
|
+
DoubleNegation:
|
85
|
+
Enabled: false
|
86
|
+
|
87
|
+
PercentLiteralDelimiters:
|
88
|
+
PreferredDelimiters:
|
89
|
+
'%': ()
|
90
|
+
'%i': ()
|
91
|
+
'%q': ()
|
92
|
+
'%Q': ()
|
93
|
+
'%r': '{}'
|
94
|
+
'%s': ()
|
95
|
+
'%w': '[]'
|
96
|
+
'%W': '[]'
|
97
|
+
'%x': ()
|
98
|
+
|
99
|
+
Semicolon:
|
100
|
+
Exclude:
|
101
|
+
- 'spec/support/'
|
data/.travis.yml
CHANGED
@@ -1,16 +1,27 @@
|
|
1
|
+
before_install:
|
2
|
+
- gem update bundler
|
3
|
+
- bundle --version
|
4
|
+
- gem update --system 2.1.11
|
5
|
+
- gem --version
|
6
|
+
bundler_args: --without development
|
7
|
+
language: ruby
|
1
8
|
rvm:
|
2
9
|
- 1.8.7
|
10
|
+
- 1.9.2
|
3
11
|
- 1.9.3
|
4
12
|
- 2.0.0
|
5
|
-
-
|
13
|
+
- 2.1.0
|
14
|
+
- rbx-2
|
6
15
|
- ruby-head
|
7
|
-
- jruby-18mode
|
8
|
-
- jruby-19mode
|
9
|
-
- jruby-head
|
10
|
-
- rbx-18mode
|
11
|
-
- rbx-19mode
|
12
|
-
|
13
16
|
matrix:
|
17
|
+
include:
|
18
|
+
- rvm: jruby-18mode
|
19
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
20
|
+
- rvm: jruby-19mode
|
21
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
22
|
+
- rvm: jruby-head
|
23
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
14
24
|
allow_failures:
|
15
|
-
- rvm: ruby-head
|
16
25
|
- rvm: jruby-head
|
26
|
+
- rvm: ruby-head
|
27
|
+
fast_finish: true
|
data/Gemfile
CHANGED
@@ -1,12 +1,30 @@
|
|
1
1
|
source 'http://rubygems.org'
|
2
2
|
|
3
|
+
gem 'rake', '~> 10.1.1'
|
3
4
|
gem 'jruby-openssl' if defined? JRUBY_VERSION
|
4
|
-
gem 'coveralls', :require => false
|
5
|
-
|
6
|
-
# Specify your gem's dependencies in http.gemspec
|
7
|
-
gemspec
|
8
5
|
|
9
6
|
group :development do
|
10
|
-
gem '
|
11
|
-
|
7
|
+
gem 'pry'
|
8
|
+
platforms :ruby_19, :ruby_20 do
|
9
|
+
gem 'pry-debugger'
|
10
|
+
gem 'pry-stack_explorer'
|
11
|
+
end
|
12
|
+
platforms :ruby_19, :ruby_20, :ruby_21 do
|
13
|
+
gem 'celluloid-io'
|
14
|
+
gem 'guard-rspec'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
group :test do
|
19
|
+
gem 'backports'
|
20
|
+
gem 'coveralls', :require => false
|
21
|
+
gem 'json', '>= 1.8.1', :platforms => [:jruby, :rbx, :ruby_18, :ruby_19]
|
22
|
+
gem 'mime-types', '~> 1.25', :platforms => [:jruby, :ruby_18]
|
23
|
+
gem 'rspec', '>= 2.14'
|
24
|
+
gem 'rubocop', '>= 0.19', :platforms => [:ruby_19, :ruby_20, :ruby_21]
|
25
|
+
gem 'simplecov', :require => false
|
26
|
+
gem 'yardstick'
|
12
27
|
end
|
28
|
+
|
29
|
+
# Specify your gem's dependencies in http.gemspec
|
30
|
+
gemspec
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,17 +1,27 @@
|
|
1
|
-
The HTTP Gem
|
1
|
+
![The HTTP Gem](https://raw.github.com/tarcieri/http/master/logo.png)
|
2
2
|
==============
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/http.png)](http://rubygems.org/gems/http)
|
4
4
|
[![Build Status](https://secure.travis-ci.org/tarcieri/http.png?branch=master)](http://travis-ci.org/tarcieri/http)
|
5
5
|
[![Code Climate](https://codeclimate.com/github/tarcieri/http.png)](https://codeclimate.com/github/tarcieri/http)
|
6
6
|
[![Coverage Status](https://coveralls.io/repos/tarcieri/http/badge.png?branch=master)](https://coveralls.io/r/tarcieri/http)
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
SEO Note
|
9
|
+
--------
|
10
|
+
|
11
|
+
This Gem has the worst name in the history of SEO. But perhaps we can fix that if we
|
12
|
+
all refer to it as "The HTTP Gem", or even better, the "Ruby HTTP Gem".
|
13
|
+
|
14
|
+
About
|
15
|
+
-----
|
11
16
|
|
12
17
|
The HTTP Gem is an easy-to-use client library for making requests from Ruby. It uses
|
13
|
-
a simple method chaining system for building requests, similar to
|
14
|
-
|
18
|
+
a simple method chaining system for building requests, similar to Python's [Requests]
|
19
|
+
|
20
|
+
Under the hood, The HTTP Gem uses [http_parser.rb], a fast HTTP parsing native
|
21
|
+
extension based on the Node.js parser and a Java port thereof.
|
22
|
+
|
23
|
+
[requests]: http://docs.python-requests.org/en/latest/
|
24
|
+
[http_parser.rb]: https://github.com/tmm1/http_parser.rb
|
15
25
|
|
16
26
|
Installation
|
17
27
|
------------
|
@@ -34,24 +44,64 @@ Inside of your Ruby program do:
|
|
34
44
|
|
35
45
|
...to pull it in as a dependency.
|
36
46
|
|
37
|
-
|
38
|
-
|
47
|
+
Documentation
|
48
|
+
-------------
|
49
|
+
|
50
|
+
[Please see the HTTP Gem Wiki](https://github.com/tarcieri/http/wiki)
|
51
|
+
for more detailed documentation and usage notes.
|
52
|
+
|
53
|
+
Support
|
54
|
+
-------
|
55
|
+
|
56
|
+
For help with The HTTP Gem, either open a [Github Issue] or discuss it
|
57
|
+
on the Celluloid mailing list:
|
39
58
|
|
40
|
-
|
59
|
+
http://groups.google.com/group/celluloid-ruby
|
60
|
+
|
61
|
+
You can also find us on IRC at #celluloid on freenode
|
62
|
+
|
63
|
+
[Github Issue]: https://github.com/tarcieri/http/issues
|
64
|
+
|
65
|
+
Basic Usage
|
66
|
+
-----------
|
67
|
+
|
68
|
+
Here's some simple examples to get you started:
|
69
|
+
|
70
|
+
### GET requests
|
41
71
|
|
42
72
|
```ruby
|
43
|
-
>> HTTP.get("http://www.google.com")
|
73
|
+
>> HTTP.get("http://www.google.com").to_s
|
44
74
|
=> "<html><head><meta http-equiv=\"content-type\" content=..."
|
45
75
|
```
|
46
76
|
|
47
|
-
That's it!
|
48
|
-
|
77
|
+
That's all it takes! To obtain an `HTTP::Response` object instead of the response
|
78
|
+
body, all we have to do is omit the #to_s on the end:
|
49
79
|
|
50
80
|
```ruby
|
51
|
-
>> HTTP.get("http://www.google.com")
|
81
|
+
>> HTTP.get("http://www.google.com")
|
52
82
|
=> #<HTTP/1.0 200 OK @headers={"Content-Type"=>"text/html; charset=UTF-8", "Date"=>"Fri, ...>
|
83
|
+
=> #<HTTP::Response/1.1 200 OK @headers={"Content-Type"=>"text/html; ...>
|
53
84
|
```
|
54
85
|
|
86
|
+
We can also obtain an `HTTP::ResponseBody` object for this response:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
>> HTTP.get("http://www.google.com").body
|
90
|
+
=> #<HTTP::ResponseBody:814d7aac @streaming=false>
|
91
|
+
```
|
92
|
+
|
93
|
+
The response body can be streamed with `HTTP::ResponseBody#readpartial`:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
>> HTTP.get("http://www.google.com").body.readpartial
|
97
|
+
=> "<!doctype html><html "
|
98
|
+
```
|
99
|
+
|
100
|
+
In practice you'll want to bind the HTTP::ResponseBody to a local variable (e.g.
|
101
|
+
"body") and call readpartial on it repeatedly until it returns nil.
|
102
|
+
|
103
|
+
### POST requests
|
104
|
+
|
55
105
|
Making POST requests is simple too. Want to POST a form?
|
56
106
|
|
57
107
|
```ruby
|
@@ -66,21 +116,33 @@ HTTP.get "http://example.com/resource", :params => {:foo => "bar"}
|
|
66
116
|
Want to POST with a specific body, JSON for instance?
|
67
117
|
|
68
118
|
```ruby
|
69
|
-
HTTP.post "http://example.com/resource", :
|
119
|
+
HTTP.post "http://example.com/resource", :json => { :foo => '42' })
|
120
|
+
```
|
121
|
+
|
122
|
+
It's easy!
|
123
|
+
|
124
|
+
|
125
|
+
### Proxy Support
|
126
|
+
|
127
|
+
Making request behind proxy is as simple as making them directly. Just specify
|
128
|
+
hostname (or IP address) of your proxy server and it's port, and here you go:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
HTTP.via("proxy-hostname.local", 8080)
|
132
|
+
.get "http://example.com/resource"
|
70
133
|
```
|
71
134
|
|
72
|
-
|
135
|
+
Proxy needs authentication? No problem:
|
73
136
|
|
74
137
|
```ruby
|
75
|
-
HTTP.
|
138
|
+
HTTP.via("proxy-hostname.local", 8080, "username", "password")
|
139
|
+
.get "http://example.com/resource"
|
76
140
|
```
|
77
141
|
|
78
|
-
It's easy!
|
79
142
|
|
80
|
-
Adding Headers
|
81
|
-
--------------
|
143
|
+
### Adding Headers
|
82
144
|
|
83
|
-
The HTTP
|
145
|
+
The HTTP gem uses the concept of chaining to simplify requests. Let's say
|
84
146
|
you want to get the latest commit of this library from Github in JSON format.
|
85
147
|
One way we could do this is by tacking a filename on the end of the URL:
|
86
148
|
|
@@ -102,7 +164,7 @@ HTTP.with_headers(:accept => 'application/json').
|
|
102
164
|
This requests JSON from Github. Github is smart enough to understand our
|
103
165
|
request and returns a response with Content-Type: application/json. If you
|
104
166
|
happen to have a library loaded which defines the JSON constant and implements
|
105
|
-
JSON.parse, the HTTP
|
167
|
+
JSON.parse, the HTTP gem will attempt to parse the JSON response.
|
106
168
|
|
107
169
|
Shorter aliases exists for HTTP.with_headers:
|
108
170
|
|
@@ -114,8 +176,7 @@ HTTP[:accept => 'application/json'].
|
|
114
176
|
get("https://github.com/tarcieri/http/commit/HEAD")
|
115
177
|
```
|
116
178
|
|
117
|
-
Content Negotiation
|
118
|
-
-------------------
|
179
|
+
### Content Negotiation
|
119
180
|
|
120
181
|
As important a concept as content negotiation is to HTTP, it sure should be easy,
|
121
182
|
right? But usually it's not, and so we end up adding ".json" onto the ends of
|
@@ -128,15 +189,69 @@ HTTP.accept(:json).get("https://github.com/tarcieri/http/commit/HEAD")
|
|
128
189
|
This adds the appropriate Accept header for retrieving a JSON response for the
|
129
190
|
given resource.
|
130
191
|
|
131
|
-
Contributing to HTTP
|
132
|
-
--------------------
|
133
192
|
|
134
|
-
|
193
|
+
### Celluloid::IO Support
|
194
|
+
|
195
|
+
The HTTP Gem makes it simple to make multiple concurrent HTTP requests from a
|
196
|
+
Celluloid::IO actor. Here's a parallel HTTP fetcher with the HTTP Gem and
|
197
|
+
Celluloid::IO:
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
require 'celluloid/io'
|
201
|
+
require 'http'
|
202
|
+
|
203
|
+
class HttpFetcher
|
204
|
+
include Celluloid::IO
|
205
|
+
|
206
|
+
def fetch(url)
|
207
|
+
HTTP.get(url, socket_class: Celluloid::IO::TCPSocket).response
|
208
|
+
end
|
209
|
+
end
|
210
|
+
```
|
211
|
+
|
212
|
+
There's a little more to it, but that's the core idea!
|
213
|
+
|
214
|
+
* [Full parallel HTTP fetcher example](https://github.com/tarcieri/http/wiki/Parallel-requests-with-Celluloid%3A%3AIO)
|
215
|
+
* See also: [Celluloid::IO](https://github.com/celluloid/celluloid-io)
|
216
|
+
|
217
|
+
|
218
|
+
Supported Ruby Versions
|
219
|
+
-----------------------
|
220
|
+
|
221
|
+
This library aims to support and is [tested against][travis] the following Ruby
|
222
|
+
versions:
|
223
|
+
|
224
|
+
* Ruby 1.8.7
|
225
|
+
* Ruby 1.9.2
|
226
|
+
* Ruby 1.9.3
|
227
|
+
* Ruby 2.0.0
|
228
|
+
* Ruby 2.1.0
|
229
|
+
|
230
|
+
If something doesn't work on one of these versions, it's a bug.
|
231
|
+
|
232
|
+
This library may inadvertently work (or seem to work) on other Ruby versions,
|
233
|
+
however support will only be provided for the versions listed above.
|
234
|
+
|
235
|
+
If you would like this library to support another Ruby version or
|
236
|
+
implementation, you may volunteer to be a maintainer. Being a maintainer
|
237
|
+
entails making sure all tests run and pass on that implementation. When
|
238
|
+
something breaks on your implementation, you will be responsible for providing
|
239
|
+
patches in a timely fashion. If critical issues for a particular implementation
|
240
|
+
exist at the time of a major release, support for that Ruby version may be
|
241
|
+
dropped.
|
242
|
+
|
243
|
+
[travis]: http://travis-ci.org/tarcieri/http
|
244
|
+
|
245
|
+
|
246
|
+
Contributing to The HTTP Gem
|
247
|
+
----------------------------
|
248
|
+
|
249
|
+
* Fork the HTTP gem on github
|
135
250
|
* Make your changes and send me a pull request
|
136
|
-
* If
|
137
|
-
* If
|
251
|
+
* If we like them we'll merge them
|
252
|
+
* If we've accepted a patch, feel free to ask for commit access!
|
138
253
|
|
139
254
|
Copyright
|
140
255
|
---------
|
141
256
|
|
142
|
-
Copyright (c)
|
257
|
+
Copyright (c) 2014 Tony Arcieri, Erik Michaels-Ober. See LICENSE.txt for further details.
|