Commit 3e0ac4b3 authored by Nitesh Jain's avatar Nitesh Jain

Merge tag 'upstream/0.6.1'

Upstream version 0.6.1
parents b8c5269c ed65b4a4
*.gem
*.rbc
.bundle
.config
.rvmrc
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
doc
lib/bundler/man
measurement
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
.rvmrc
--color
--format documentation
--backtrace
--color
--format=documentation
--order random
--warnings
AllCops:
Includes:
- 'Gemfile'
- 'Rakefile'
- 'http.gemspec'
# Avoid long parameter lists
ParameterLists:
Max: 3
CountKeywordArgs: true
MethodLength:
CountComments: false
Max: 31 # TODO: lower to 15
ClassLength:
CountComments: false
Max: 132 # TODO: lower to 100
CyclomaticComplexity:
Max: 13 # TODO: lower to 6
# Avoid more than `Max` levels of nesting.
BlockNesting:
Max: 3
# Do not force public/protected/private keyword to be indented at the same
# level as the def keyword. My personal preference is to outdent these keywords
# because I think when scanning code it makes it easier to identify the
# sections of code and visually separate them. When the keyword is at the same
# level I think it sort of blends in with the def keywords and makes it harder
# to scan the code and see where the sections are.
AccessModifierIndentation:
Enabled: false
# Limit line length
LineLength:
Enabled: false
# Disable documentation checking until a class needs to be documented once
Documentation:
Enabled: false
# Not all trivial readers/writers can be defined with attr_* methods
TrivialAccessors:
Enabled: false
# Enforce Ruby 1.8-compatible hash syntax
HashSyntax:
EnforcedStyle: hash_rockets
# No spaces inside hash literals
SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space
# Allow dots at the end of lines
DotPosition:
Enabled: false
# Don't require magic comment at the top of every file
Encoding:
Enabled: false
# Enforce outdenting of access modifiers (i.e. public, private, protected)
AccessModifierIndentation:
EnforcedStyle: outdent
EmptyLinesAroundAccessModifier:
Enabled: true
# Align ends correctly
EndAlignment:
AlignWith: variable
# Indentation of when/else
CaseIndentation:
IndentWhenRelativeTo: end
IndentOneStep: false
# Use the old lambda literal syntax
Lambda:
Enabled: false
DoubleNegation:
Enabled: false
PercentLiteralDelimiters:
PreferredDelimiters:
'%': ()
'%i': ()
'%q': ()
'%Q': ()
'%r': '{}'
'%s': ()
'%w': '[]'
'%W': '[]'
'%x': ()
Semicolon:
Exclude:
- 'spec/support/'
before_install:
- gem update bundler
- bundle --version
- gem update --system 2.1.11
- gem --version
bundler_args: --without development
language: ruby
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0
- ree
- 2.1.0
- rbx-2
- ruby-head
- jruby-18mode
- jruby-19mode
- jruby-head
- rbx-18mode
- rbx-19mode
matrix:
include:
- rvm: jruby-18mode
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
- rvm: jruby-19mode
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
- rvm: jruby-head
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
- rvm: ruby-head
fast_finish: true
0.6.1 (2014-05-07)
------------------
* Fix request `Content-Length` calculation for Unicode (@challengeechallengee)
* Add `Response#flush` (@ixti)
* Fix `Response::Body#readpartial` default size (@hannesg, @ixti)
* Add missing `CRLF` for chunked bodies (@hannesg)
* Fix forgotten CGI require (@ixti)
* Improve README (@tarcieri)
0.6.0 (2014-04-04)
------------------
* Rename `HTTP::Request#method` to `HTTP::Request#verb` (@krainboltgreene)
* Add `HTTP::ResponseBody` class (@tarcieri)
* Change API of response on `HTTP::Client.request` and "friends" (`#get`, `#post`, etc) (@tarcieri)
* Add `HTTP::Response#readpartial` (@tarcieri)
* Add `HTTP::Headers` class (@ixti)
* Fix and improve following redirects (@ixti)
* Add `HTTP::Request#redirect` (@ixti)
* Add `HTTP::Response#content_type` (@ixti)
* Add `HTTP::Response#mime_type` (@ixti)
* Add `HTTP::Response#charset` (@ixti)
* Improve error message upon invalid URI scheme (@ixti)
* Consolidate errors under common `HTTP::Error` namespace (@ixti)
* Add easy way of adding Authorization header (@ixti)
* Fix proxy support (@hundredwatt)
* Fix and improve query params handing (@jwinter)
* Change API of custom MIME type parsers (@ixti)
* Remove `HTTP::Chainable#with_response` (@ixti)
* Remove `HTTP::Response::BodyDelegator` (@ixti)
* Remove `HTTP::Response#parsed_body` (@ixti)
* Bump up input buffer from 4K to 16K (@tarcieri)
``` ruby
# Main API change you will mention is that `request` method and it's
# syntax sugar helpers like `get`, `post`, etc. now returns Response
# object instead of BodyDelegator:
response = HTTP.get "http://example.com"
raw_body = HTTP.get("http://example.com").to_s
parsed_body = HTTP.get("http://example.com/users.json").parse
# Second major change in API is work with request/response headers
# It is now delegated to `HTTP::Headers` class, so you can check it's
# documentation for details, here we will only outline main difference.
# Duckface (`[]=`) does not appends headers anymore
request[:content_type] = "text/plain"
request[:content_type] = "text/html"
request[:content_type] # => "text/html"
# In order to add multiple header values, you should pass array:
request[:cookie] = ["foo=bar", "woo=hoo"]
request[:cookie] # => ["foo=bar", "woo=hoo"]
# or call `#add` on headers:
request.headers.add :accept, "text/plain"
request.headers.add :accept, "text/html"
request[:accept] # => ["text/plain", "text/html"]
# Also, you can now read body in chunks (stream):
res = HTTP.get "http://example.com"
File.open "/tmp/dummy.bin", "wb" do |io|
while (chunk = res.readpartial)
io << chunk
end
end
```
[Changes discussion](https://github.com/tarcieri/http/issues/116)
0.5.0
-----
* Add query string support
......
source 'http://rubygems.org'
gem 'rake', '~> 10.1.1'
gem 'jruby-openssl' if defined? JRUBY_VERSION
gem 'coveralls', :require => false
# Specify your gem's dependencies in http.gemspec
gemspec
group :development do
gem 'guard-rspec'
gem 'celluloid-io' if RUBY_VERSION >= "1.9.3"
gem 'pry'
platforms :ruby_19, :ruby_20 do
gem 'pry-debugger'
gem 'pry-stack_explorer'
end
platforms :ruby_19, :ruby_20, :ruby_21 do
gem 'celluloid-io'
gem 'guard-rspec'
end
end
group :test do
gem 'backports'
gem 'coveralls', :require => false
gem 'json', '>= 1.8.1', :platforms => [:jruby, :rbx, :ruby_18, :ruby_19]
gem 'mime-types', '~> 1.25', :platforms => [:jruby, :ruby_18]
gem 'rspec', '>= 2.14'
gem 'rubocop', '~> 0.19.0', :platforms => [:ruby_19, :ruby_20, :ruby_21]
gem 'simplecov', :require => false
gem 'yardstick'
end
# Specify your gem's dependencies in http.gemspec
gemspec
Copyright (c) 2011 Tony Arcieri, Carl Lerche
Copyright (c) 2014 Tony Arcieri, Erik Michaels-Ober
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
......
The HTTP Gem*
![The HTTP Gem](https://raw.github.com/tarcieri/http/master/logo.png)
==============
[![Gem Version](https://badge.fury.io/rb/http.png)](http://rubygems.org/gems/http)
[![Build Status](https://secure.travis-ci.org/tarcieri/http.png?branch=master)](http://travis-ci.org/tarcieri/http)
[![Code Climate](https://codeclimate.com/github/tarcieri/http.png)](https://codeclimate.com/github/tarcieri/http)
[![Coverage Status](https://coveralls.io/repos/tarcieri/http/badge.png?branch=master)](https://coveralls.io/r/tarcieri/http)
*NOTE: this gem has the worst name in the history of SEO. But perhaps we can fix
that if we all refer to it as "The HTTP Gem". Entering that phrase into Google
actually pulls it up as #4 for me!
SEO Note
--------
This Gem has the worst name in the history of SEO. But perhaps we can fix that if we
all refer to it as "The HTTP Gem", or even better, the "Ruby HTTP Gem".
About
-----
The HTTP Gem is an easy-to-use client library for making requests from Ruby. It uses
a simple method chaining system for building requests, similar to libraries
like JQuery or Python's [Requests](http://docs.python-requests.org/en/latest/).
a simple method chaining system for building requests, similar to Python's [Requests]
Under the hood, The HTTP Gem uses [http_parser.rb], a fast HTTP parsing native
extension based on the Node.js parser and a Java port thereof.
[requests]: http://docs.python-requests.org/en/latest/
[http_parser.rb]: https://github.com/tmm1/http_parser.rb
Help and Discussion
-------------------
If you need help or just want to talk about the Ruby HTTP Gem, [visit our Google
Group][googlegroup], or join by email by sending a message to:
[ruby-http-gem+subscribe@googlegroups.com][subscribe].
[googlegroup]: https://groups.google.com/forum/#!forum/ruby-http-gem
[subscribe]: mailto:ruby-http-gem+subscribe@googlegroups.com
If you believe you've found a bug, please report it at:
https://github.com/tarcieri/http/issues
Installation
------------
......@@ -34,24 +58,52 @@ Inside of your Ruby program do:
...to pull it in as a dependency.
Making Requests
---------------
Documentation
-------------
[Please see the HTTP Gem Wiki](https://github.com/tarcieri/http/wiki)
for more detailed documentation and usage notes.
Let's start with getting things:
Basic Usage
-----------
Here's some simple examples to get you started:
### GET requests
```ruby
>> HTTP.get("http://www.google.com")
>> HTTP.get("http://www.google.com").to_s
=> "<html><head><meta http-equiv=\"content-type\" content=..."
```
That's it! The result is the response body as a string. To obtain an HTTP::Response object
instead of the response body, chain `.response` on the end of the request:
That's all it takes! To obtain an `HTTP::Response` object instead of the response
body, all we have to do is omit the #to_s on the end:
```ruby
>> HTTP.get("http://www.google.com").response
>> HTTP.get("http://www.google.com")
=> #<HTTP/1.0 200 OK @headers={"Content-Type"=>"text/html; charset=UTF-8", "Date"=>"Fri, ...>
=> #<HTTP::Response/1.1 200 OK @headers={"Content-Type"=>"text/html; ...>
```
We can also obtain an `HTTP::ResponseBody` object for this response:
```ruby
>> HTTP.get("http://www.google.com").body
=> #<HTTP::ResponseBody:814d7aac @streaming=false>
```
The response body can be streamed with `HTTP::ResponseBody#readpartial`:
```ruby
>> HTTP.get("http://www.google.com").body.readpartial
=> "<!doctype html><html "
```
In practice you'll want to bind the HTTP::ResponseBody to a local variable (e.g.
"body") and call readpartial on it repeatedly until it returns nil.
### POST requests
Making POST requests is simple too. Want to POST a form?
```ruby
......@@ -66,21 +118,33 @@ HTTP.get "http://example.com/resource", :params => {:foo => "bar"}
Want to POST with a specific body, JSON for instance?
```ruby
HTTP.post "http://example.com/resource", :body => JSON.dump(:foo => '42')
HTTP.post "http://example.com/resource", :json => { :foo => '42' })
```
It's easy!
### Proxy Support
Making request behind proxy is as simple as making them directly. Just specify
hostname (or IP address) of your proxy server and it's port, and here you go:
```ruby
HTTP.via("proxy-hostname.local", 8080)
.get "http://example.com/resource"
```
Or have it serialize JSON for you:
Proxy needs authentication? No problem:
```ruby
HTTP.post "http://example.com/resource", :json => {:foo => '42'}
HTTP.via("proxy-hostname.local", 8080, "username", "password")
.get "http://example.com/resource"
```
It's easy!
Adding Headers
--------------
### Adding Headers
The HTTP library uses the concept of chaining to simplify requests. Let's say
The HTTP gem uses the concept of chaining to simplify requests. Let's say
you want to get the latest commit of this library from Github in JSON format.
One way we could do this is by tacking a filename on the end of the URL:
......@@ -102,7 +166,7 @@ HTTP.with_headers(:accept => 'application/json').
This requests JSON from Github. Github is smart enough to understand our
request and returns a response with Content-Type: application/json. If you
happen to have a library loaded which defines the JSON constant and implements
JSON.parse, the HTTP library will attempt to parse the JSON response.
JSON.parse, the HTTP gem will attempt to parse the JSON response.
Shorter aliases exists for HTTP.with_headers:
......@@ -114,10 +178,9 @@ HTTP[:accept => 'application/json'].
get("https://github.com/tarcieri/http/commit/HEAD")
```
Content Negotiation
-------------------
### Content Negotiation
As important a concept as content negotiation is HTTP, it sure should be easy,
As important a concept as content negotiation is to HTTP, it sure should be easy,
right? But usually it's not, and so we end up adding ".json" onto the ends of
our URLs because the existing mechanisms make it too hard. It should be easy:
......@@ -128,15 +191,69 @@ HTTP.accept(:json).get("https://github.com/tarcieri/http/commit/HEAD")
This adds the appropriate Accept header for retrieving a JSON response for the
given resource.
Contributing to HTTP
--------------------
* Fork HTTP on github
### Celluloid::IO Support
The HTTP Gem makes it simple to make multiple concurrent HTTP requests from a
Celluloid::IO actor. Here's a parallel HTTP fetcher with the HTTP Gem and
Celluloid::IO:
```ruby
require 'celluloid/io'
require 'http'
class HttpFetcher
include Celluloid::IO
def fetch(url)
HTTP.get(url, socket_class: Celluloid::IO::TCPSocket).response
end
end
```
There's a little more to it, but that's the core idea!
* [Full parallel HTTP fetcher example](https://github.com/tarcieri/http/wiki/Parallel-requests-with-Celluloid%3A%3AIO)
* See also: [Celluloid::IO](https://github.com/celluloid/celluloid-io)
Supported Ruby Versions
-----------------------
This library aims to support and is [tested against][travis] the following Ruby
versions:
* Ruby 1.8.7
* Ruby 1.9.2
* Ruby 1.9.3
* Ruby 2.0.0
* Ruby 2.1.0
If something doesn't work on one of these versions, it's a bug.
This library may inadvertently work (or seem to work) on other Ruby versions,
however support will only be provided for the versions listed above.
If you would like this library to support another Ruby version or
implementation, you may volunteer to be a maintainer. Being a maintainer
entails making sure all tests run and pass on that implementation. When
something breaks on your implementation, you will be responsible for providing
patches in a timely fashion. If critical issues for a particular implementation
exist at the time of a major release, support for that Ruby version may be
dropped.
[travis]: http://travis-ci.org/tarcieri/http
Contributing to The HTTP Gem
----------------------------
* Fork the HTTP gem on github
* Make your changes and send me a pull request
* If I like them I'll merge them
* If I've accepted a patch, feel free to ask for a commit bit!
* If we like them we'll merge them
* If we've accepted a patch, feel free to ask for commit access!
Copyright
---------
Copyright (c) 2013 Tony Arcieri. See LICENSE.txt for further details.
Copyright (c) 2014 Tony Arcieri, Erik Michaels-Ober. See LICENSE.txt for further details.
......@@ -4,4 +4,26 @@ require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :default => :spec
task :test => :spec
begin
require 'rubocop/rake_task'
Rubocop::RakeTask.new
rescue LoadError
task :rubocop do
$stderr.puts 'Rubocop is disabled'
end
end
require 'yardstick/rake/measurement'
Yardstick::Rake::Measurement.new do |measurement|
measurement.output = 'measurement/report.txt'
end
require 'yardstick/rake/verify'
Yardstick::Rake::Verify.new do |verify|
verify.require_exact_threshold = false
verify.threshold = 58
end
task :default => [:spec, :rubocop, :verify_measurements]
No preview for this file type
#!/usr/bin/env ruby
#
# Example of using the HTTP Gem with Celluloid::IO
# Make sure to 'gem install celluloid-io' before running
#
# Run as: bundle exec examples/celluloid.rb
#
require 'celluloid/io'
require 'http'
puts HTTP.get("https://www.google.com/", :socket_class => Celluloid::IO::TCPSocket, :ssl_socket_class => Celluloid::IO::SSLSocket)
#!/usr/bin/env ruby
#
# Example of using the HTTP Gem with Celluloid::IO
# Make sure to 'gem install celluloid-io' before running
#
# Run as: bundle exec examples/parallel_requests_with_celluloid.rb
#
require 'celluloid/io'
require 'http'
class HttpFetcher
include Celluloid::IO
def fetch(url)
# Note: For SSL support specify:
# ssl_socket_class: Celluloid::IO::SSLSocket
HTTP.get(url, :socket_class => Celluloid::IO::TCPSocket).response
end
end
fetcher = HttpFetcher.new
urls = %w[http://www.ruby-lang.org/ http://www.rubygems.org/ http://celluloid.io/]
# Kick off a bunch of future calls to HttpFetcher to grab the URLs in parallel
futures = urls.map { |u| [u, fetcher.future.fetch(u)] }
# Consume the results as they come in
futures.each do |url, future|
# Wait for HttpFetcher#fetch to complete for this request
response = future.value
puts "Got #{url}: #{response.inspect}"
end
# Suppress Celluloid's shutdown messages
# Otherwise the example is a bit noisy :|
exit!
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/http/version', __FILE__)
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'http/version'
Gem::Specification.new do |gem|
gem.authors = ["Tony Arcieri"]
gem.email = ["tony.arcieri@gmail.com"]
gem.description = "HTTP so awesome it will lure Catherine Zeta Jones into your unicorn petting zoo"
gem.summary = "HTTP should be easy"
gem.homepage = "https://github.com/tarcieri/http"
gem.authors = %w[Tony Arcieri]
gem.email = %w[tony.arcieri@gmail.com]
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.description = <<-DESCRIPTION.strip.gsub(/\s+/, ' ')
An easy-to-use client library for making requests from Ruby.
It uses a simple method chaining system for building requests,
similar to Python's Requests.
DESCRIPTION
gem.summary = 'HTTP should be easy'
gem.homepage = 'https://github.com/tarcieri/http'
gem.licenses = %w[MIT]
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "http"
gem.require_paths = ["lib"]
gem.name = 'http'
gem.require_paths = %w[lib]
gem.version = HTTP::VERSION
gem.add_runtime_dependency 'http_parser.rb'
gem.add_runtime_dependency 'http_parser.rb', '~> 0.6.0'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec', '>= 2.11'
gem.add_development_dependency 'json'
gem.add_development_dependency 'bundler', '~> 1.0'
end
require 'http/parser'
require 'http/errors'
require 'http/chainable'
require 'http/client'
require 'http/mime_type'
require 'http/options'
require 'http/request'
require 'http/request_stream'
require 'http/request/writer'
require 'http/response'
require 'http/response_parser'
require 'http/uri_backport' if RUBY_VERSION < "1.9.0"
require 'http/response/body'
require 'http/response/parser'
require 'http/backports'
# HTTP should be easy
module HTTP
......
module HTTP
# Authorization header value builders
module AuthorizationHeader
class << self
# Associate type with given builder.
# @param [#to_sym] type
# @param [Class] klass
# @return [void]
def register(type, klass)
builders[type.to_sym] = klass
end
# Builds Authorization header value with associated builder.
# @param [#to_sym] type
# @param [Object] opts
# @return [String]
def build(type, opts)
klass = builders[type.to_sym]
fail Error, "Unknown authorization type #{type}" unless klass
klass.new opts
end
private
# :nodoc:
def builders
@builders ||= {}
end
end
end
end
# built-in builders
require 'http/authorization_header/basic_auth'
require 'http/authorization_header/bearer_token'
require 'base64'
module HTTP
module AuthorizationHeader
# Basic authorization header builder
# @see http://tools.ietf.org/html/rfc2617
class BasicAuth
# @param [#fetch] opts
# @option opts [#to_s] :user
# @option opts [#to_s] :pass
def initialize(opts)
@user = opts.fetch :user
@pass = opts.fetch :pass
end
# :nodoc:
def to_s
'Basic ' << Base64.strict_encode64("#{@user}:#{@pass}")
end
end