Skip to content

Commit

Permalink
Content encoding fixes.
Browse files Browse the repository at this point in the history
* `em-http-client` has issues with large gzip responses - stripping gzip
  headers for now as a workaround.

* Fix issue with UTF8/BINARY encodings in responses.

* Add Tumblr API example.
  • Loading branch information
oesmith committed Oct 12, 2012
1 parent 67c3f33 commit 1149024
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
puffing-billy (0.0.1)
puffing-billy (0.1.0)
em-http-request
eventmachine
eventmachine_httpserver
Expand Down
3 changes: 3 additions & 0 deletions bin/proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
require 'billy'
Billy::Proxy.new.start(false)
22 changes: 22 additions & 0 deletions examples/tumblr_api.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<body>
<h1>Latest news</h1>
<div id="news"></div>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.min.js'></script>
<script type='text/javascript'>
$(function () {
var url = 'http://blog.howmanyleft.co.uk/api/read/json?callback=?&type=text&num=3&filter=text';
$.getJSON(url, function (data) {
$.each(data.posts, function (idx, post) {
var title = post['regular-title'];
var href = post['url-with-slug'];
var body = post['regular-body'];
$('#news').append(
'<h3><a href="' + href + '">' + title + '</a></h3>' +
'<p>' + body + '</p>');
});
});
})
</script>
</body>
34 changes: 21 additions & 13 deletions lib/billy/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@ def initialize
reset
end

def start
Thread.new do
EM.run do
EM.error_handler do |e|
puts e.class.name, e
puts e.backtrace.join("\n")
end

@signature = EM.start_server('127.0.0.1', 0, ProxyConnection) do |p|
p.handler = self
end
end
def start(threaded = true)
if threaded
Thread.new { main_loop }
sleep(0.01) while @signature.nil?
else
main_loop
end
sleep(0.01) while @signature.nil?
end

def url
Expand Down Expand Up @@ -63,5 +56,20 @@ def find_stub(method, url)
end
nil
end

def main_loop
EM.run do
EM.error_handler do |e|
puts e.class.name, e
puts e.backtrace.join("\n")
end

@signature = EM.start_server('127.0.0.1', 0, ProxyConnection) do |p|
p.handler = self
end

Billy.log(:info, "Proxy listening on #{url}")
end
end
end
end
5 changes: 3 additions & 2 deletions lib/billy/proxy_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def handle_request

def proxy_request
headers = Hash[@headers.map { |k,v| [k.downcase, v] }]
headers.delete('accept-encoding')

req = EventMachine::HttpRequest.new(@url)
req_opts = {
Expand All @@ -98,15 +99,15 @@ def proxy_request
req = req.send(@parser.http_method.downcase, req_opts)

req.errback do
puts "Request failed: #{url}"
puts "Request failed: #{@url}"
close_connection
end

req.callback do
res = EM::DelegatedHttpResponse.new(self)
res.status = req.response_header.status
res.headers = req.response_header.merge('Connection' => 'close')
res.content = req.response
res.content = req.response.force_encoding('BINARY')
res.send_response
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/requests/examples/tumblr_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

describe 'Tumblr API example', :type => :request, :js => true do
before do
proxy.stub('http://blog.howmanyleft.co.uk/api/read/json').and_return(
:jsonp => {
:posts => [
{
'regular-title' => 'News Item 1',
'url-with-slug' => 'http://example.com/news/1',
'regular-body' => 'News item 1 content here'
},
{
'regular-title' => 'News Item 2',
'url-with-slug' => 'http://example.com/news/2',
'regular-body' => 'News item 2 content here'
}
]
})
end

it 'should show news stories', :js => true do
visit '/tumblr_api.html'
page.should have_link('News Item 1', :href => 'http://example.com/news/1')
page.should have_content('News item 1 content here')
page.should have_link('News Item 2', :href => 'http://example.com/news/2')
page.should have_content('News item 2 content here')
end
end

0 comments on commit 1149024

Please sign in to comment.