public
Fork of igrigorik/em-http-request
Description: Asynchronous HTTP Client (EventMachine + Ruby)
Homepage:
Clone URL: git://github.com/joshbuddy/em-http-request.git
name age message
file .gitignore Sun Feb 01 10:47:43 -0800 2009 Ignore build products and extconf artifacts. [careo]
file LICENSE Fri Aug 22 10:56:57 -0700 2008 initial import [igrigorik]
file README.rdoc Thu Nov 26 08:42:46 -0800 2009 fixed proxy optional [joshbuddy]
file Rakefile Mon Nov 16 13:27:23 -0800 2009 added mock tests [joshbuddy]
file VERSION Wed Nov 25 06:34:17 -0800 2009 Version bump to 0.2.3 [igrigorik]
directory examples/ Sun Oct 25 12:33:07 -0700 2009 use local em-http in fetch [igrigorik]
directory ext/ Sun Oct 25 12:15:27 -0700 2009 rebuild http11_parser.c from http11_parser.rl ... [Astro]
directory lib/ Thu Nov 26 21:09:22 -0800 2009 correct http tunneling [joshbuddy]
directory test/ Wed Dec 02 22:38:06 -0800 2009 added proxy test [joshbuddy]
README.rdoc

EM-HTTP-Client

EventMachine based HTTP Request interface. Supports streaming response processing, uses Ragel HTTP parser.

  • Simple interface for single & parallel requests via deferred callbacks
  • Automatic gzip & deflate decoding
  • Basic-Auth support
  • Custom timeouts

Screencast / Demo of using EM-HTTP-Request:

Getting started

  # install & configure gemcutter repos
  gem update --system
  gem install gemcutter
  gem tumble

  gem install em-http-request

  irb:0> require 'em-http'

Simple client example

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://127.0.0.1/').get :query => {'keyname' => 'value'}, :timeout => 10

    http.callback {
      p http.response_header.status
      p http.response_header
      p http.response

      EventMachine.stop
    }
  }

Multi request example

  EventMachine.run {
    multi = EventMachine::MultiRequest.new

    # add multiple requests to the multi-handler
    multi.add(EventMachine::HttpRequest.new('http://www.google.com/').get)
    multi.add(EventMachine::HttpRequest.new('http://www.yahoo.com/').get)

    multi.callback  {
      p multi.responses[:succeeded]
      p multi.responses[:failed]

      EventMachine.stop
    }
  }

Basic-Auth example

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://www.website.com/').get :head => {'authorization' => ['user', 'pass']}

    http.errback { failed }
    http.callback {
      p http.response_header

      EventMachine.stop
    }
  }

POST example

  EventMachine.run {
    http1 = EventMachine::HttpRequest.new('http://www.website.com/').post :body => {"key1" => 1, "key2" => [2,3]}
    http2 = EventMachine::HttpRequest.new('http://www.website.com/').post :body => "some data"

    # ...
  }

Proxy example

  EventMachine.run {
    http1 = EventMachine::HttpRequest.new('http://www.website.com/').get :proxy => {
      :host => 'www.myproxy.com',
      :port => 8080,
      :authorization => ['username', 'password'] # authorization is optional
    }

Streaming body processing

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://www.website.com/').get
    http.stream { |chunk| print chunk }

    # ...
  }