You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
igrigorik edited this page Apr 13, 2011
·
4 revisions
Redirects and Timeouts
Timeouts
em-http supports two types of timeouts: connection timeouts, and inactivity timeouts.
Connect timeouts (default: 5s) are invoked during the initial connection setup. If the server is slow to respond or overloaded, EventMachine will cancel the request and the errback function will be invoked.
Inactivity timeouts (default: 10s) are invoked after a successful connection has been made, but no data has been exchanged by either side. In cases where you have a long-lived connection (ex: stream of data from a firehose API), you may want to set this timeout to 0, which will effectively disable it.
em-http can automatically handle 3XX redirects on your behalf. By default, the library will return the 3XX response along with the headers. However, you can provide a :redirects => depth key when you make the request to tell em-http to follow redirects up to a certain depth.
If the redirects key is provided, em-http will transparently follow all 3XX redirect codes. To check what the final URL you arrived at is, check http.last_effective_url.
Following selective redirects
Sometimes you want to restrict which redirects you follow. For example, let’s say we want to follow bit.ly links to their destinations, but we want to avoid making any requests to yahoo.com. In this case, we can setup a headers callback and inspect the headers as em-http makes the requests – anytime em-http finishes parsing the requests, this function is called. Within the headers callback, we can inspect the Location header and selective abort the connection!
EM.rundoc=EM::HttpRequest.new('https://bit.ly/epqosH').get:redirects=>1c.headersdoifc.response_header['LOCATION'] !~ /gist/puts"Not pointing to a gist, aborting connection"c.closeendendc.callback{puts'done'}c.errback{puts'err'}end