microget 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/Gemfile +16 -0
- data/LICENSE.txt +20 -0
- data/README.md +80 -0
- data/Rakefile +52 -0
- data/lib/microget.rb +130 -0
- data/spec/helper.rb +6 -0
- data/spec/microget_spec.rb +42 -0
- data/spec/microget_with_real_server_spec.rb +58 -0
- data/spec/server_runner.rb +57 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/streaming_app.ru +66 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de1316815435cc4a99b3d2a7c6f13b0e43015d87
|
4
|
+
data.tar.gz: 13e92c78deb579b3fd98ff7ccc48bf6c659595b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 220d1cada56fdded21fc72b5970505b93bb92f582ffda2eeca4a2f99e583ed1e2abbf02074f73182632ad560f15a4f6ebf00998f8ee97749da2f160089c50d15
|
7
|
+
data.tar.gz: 8d09e070e936bf79bdb53ec97af71b31eaeba3f3b41dfa4cffa2267fb77a5efe9146412d35edd52cceb4db5b509b5c0194f1e1e8984c217de2556b225b027d59
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem 'puma'
|
10
|
+
gem 'yard'
|
11
|
+
gem "rspec", "~> 3.3.0"
|
12
|
+
gem "rdoc", "~> 3.12"
|
13
|
+
gem "bundler", "~> 1.0"
|
14
|
+
gem "jeweler", "~> 2.0.1"
|
15
|
+
gem "simplecov", ">= 0"
|
16
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 Julik Tarkhanov
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# microget
|
2
|
+
|
3
|
+
An no-nonsense, pedal-to-the-metal unbuffered HTTP streaming client for doing GETs of large or slow responses, _fast._
|
4
|
+
It is meant for situations when you want to get access to the raw data read from the upstream HTTP server,
|
5
|
+
and read it in your desired increments, with or without buffering. And for situations where you want to
|
6
|
+
perform such a request but for some reason want to bail out of it early.
|
7
|
+
|
8
|
+
It is a prefect vehicle for writing fast HTTP proxies or download managers. It has very few features, but
|
9
|
+
the features it does have warrant it's existence.
|
10
|
+
|
11
|
+
It currently supports:
|
12
|
+
|
13
|
+
* Unbuffered GET requests for a given URL
|
14
|
+
* Streaming body reads directly from-socket, including syscall-driven reads (splice())
|
15
|
+
* Early disconnect/close on block return
|
16
|
+
* Any GET headers
|
17
|
+
|
18
|
+
It explicitly __does not support__:
|
19
|
+
|
20
|
+
* HTTPS
|
21
|
+
* Keepalive
|
22
|
+
* Redirects (you can implement them on top)
|
23
|
+
* Chunked responses (you can implement them on top as well)
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To read in chunks of N bytes using non-blocking reads (via `IO.select()`):
|
28
|
+
|
29
|
+
# Read in 5 megabyte chunks (assumes the upstream server is fast and you are connected
|
30
|
+
# to it via an almost-local downlink):
|
31
|
+
Microget.perform_get('http://files.com/hugefile.bin', chunk_size: 5 * 1024 * 1024) do | status, headers, body_chunk |
|
32
|
+
if status != 200
|
33
|
+
raise "Whoopsie daisy"
|
34
|
+
end
|
35
|
+
output << body_chunk
|
36
|
+
true # Signal Microget that it should continue reading by yielding a truthy value from the block
|
37
|
+
end
|
38
|
+
|
39
|
+
When using `perform_get` the socket will be closed for you at method return, automatically.
|
40
|
+
|
41
|
+
If you want to do interesting things to the read socket of the HTTP client, use `get_status_headers_and_body_socket`.
|
42
|
+
For instance, apply it in a reactor-driven setup such as EventMachine, or pass it to a syscall like `splice()`:
|
43
|
+
|
44
|
+
s, h, read_socket_containing_body = Microget.get_status_headers_and_body_socket('http://files.com/hugefile.bin')
|
45
|
+
if s != 200
|
46
|
+
read_socket_containing_body.close
|
47
|
+
raise "Unexpected status #{s}"
|
48
|
+
end
|
49
|
+
# Use IO::Splice (will only work on linux) to copy the data via the kernel, bypassing the
|
50
|
+
# userspace entirely
|
51
|
+
IO::Splice.copy_stream(read_socket_containing_body, client_socket_of_the_webserver)
|
52
|
+
...
|
53
|
+
socket.close
|
54
|
+
|
55
|
+
## What is it's use?
|
56
|
+
|
57
|
+
* Testing streaming web servers, web socket servers and so on with total buffering control
|
58
|
+
* Building HTTP reverse proxies
|
59
|
+
* Testing HTTP throughput
|
60
|
+
* Performing downloads from trusted HTTP servers with tight IO control
|
61
|
+
|
62
|
+
## Dependencies
|
63
|
+
|
64
|
+
None at all. Not even Net::HTTP. Well, ok, I lied - it used Net::HTTP for tests, sparingly.
|
65
|
+
|
66
|
+
## Contributing to microget
|
67
|
+
|
68
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
69
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
70
|
+
* Fork the project.
|
71
|
+
* Start a feature/bugfix branch.
|
72
|
+
* Commit and push until you are happy with your contribution.
|
73
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
74
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
75
|
+
|
76
|
+
## Copyright
|
77
|
+
|
78
|
+
Copyright (c) 2015 Julik Tarkhanov. See LICENSE.txt for
|
79
|
+
further details.
|
80
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
require_relative 'lib/microget'
|
14
|
+
|
15
|
+
require 'jeweler'
|
16
|
+
Jeweler::Tasks.new do |gem|
|
17
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
18
|
+
gem.name = "microget"
|
19
|
+
gem.version = Microget::VERSION
|
20
|
+
gem.homepage = "http://github.com/julik/microget"
|
21
|
+
gem.license = "MIT"
|
22
|
+
gem.description = %Q{Pedal-to-the-metal HTTP client for GET requests}
|
23
|
+
gem.summary = %Q{Designed for slow and/or large response bodies}
|
24
|
+
gem.email = "me@julik.nl"
|
25
|
+
gem.authors = ["Julik Tarkhanov"]
|
26
|
+
# dependencies defined in Gemfile
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
require 'rspec/core'
|
31
|
+
require 'rspec/core/rake_task'
|
32
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
33
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Code coverage detail"
|
37
|
+
task :simplecov do
|
38
|
+
ENV['COVERAGE'] = "true"
|
39
|
+
Rake::Task['spec'].execute
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :spec
|
43
|
+
|
44
|
+
require 'rdoc/task'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "microget #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/lib/microget.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'socket'
|
3
|
+
|
4
|
+
# An no-nonsense, pedal-to-the-metal unbuffered HTTP streaming client for doing GETs of large bodies, fast.
|
5
|
+
module Microget
|
6
|
+
VERSION = '1.0.0'
|
7
|
+
|
8
|
+
extend self
|
9
|
+
|
10
|
+
HEADER_LIMIT = 1024 * 64
|
11
|
+
HEADER_SEPARATOR = "\r\n\r\n"
|
12
|
+
STATUS_PAT = /HTTP\/([\d\.]+) (\d+) (.+)$/ # "HTTP/1.1 200 OK"
|
13
|
+
SOCKET_TIMEOUT = 60 * 5 # After which time to assume that the connection to the server has died
|
14
|
+
|
15
|
+
# Executes a GET request to the given URI with the given headers.
|
16
|
+
#
|
17
|
+
# Reads the status code and the response headers and parses them into a Hash and the numeric
|
18
|
+
# status code. Once that is done, it returns the socket so that the caller can read the body.
|
19
|
+
# The caller is responsible for closing the socket when done.
|
20
|
+
#
|
21
|
+
# @param uri[String] the full URI of the request
|
22
|
+
# @param request_headers[Hash] all the request headers to send with the request
|
23
|
+
# @return [Array<Numeric, Hash, Socket>] the HTTP status code, the header hash and the socket the body can be read from
|
24
|
+
def get_status_headers_and_body_socket(uri, request_headers: {})
|
25
|
+
uri = URI(uri.to_s)
|
26
|
+
raise ('Only plain HTTP is supported (%s)' % uri) unless uri.scheme == 'http'
|
27
|
+
raise "Unknown host" unless uri.host
|
28
|
+
|
29
|
+
socket = TCPSocket.open(uri.host, uri.port || 80)
|
30
|
+
socket.write("GET #{uri.request_uri} HTTP/1.1\r\n")
|
31
|
+
|
32
|
+
# AWS signs the Host: header, so introducing port 80 into it "just because" is a bad idea
|
33
|
+
if uri.port && uri.port.to_i != 80
|
34
|
+
socket.write("Host: %s:%d\r\n" % [uri.host, uri.port])
|
35
|
+
else
|
36
|
+
socket.write("Host: %s\r\n" % uri.host)
|
37
|
+
end
|
38
|
+
socket.write("Connection: close\r\n") # Do not request keepalive
|
39
|
+
|
40
|
+
# Write all the request headers
|
41
|
+
request_headers.each { |k, v| socket.write("%s: %s\r\n" % [k,v]) }
|
42
|
+
|
43
|
+
# Terminate the request
|
44
|
+
socket.write("\r\n")
|
45
|
+
|
46
|
+
# First read anything that might be related to the headers, up to and including \r\n\r\n.
|
47
|
+
# Once that one is encountered - stash the remaining part we have read, and parse the headers
|
48
|
+
headers_buf = read_ahead_headers(socket)
|
49
|
+
status_code, header_hash = parse_status_and_headers(headers_buf)
|
50
|
+
[status_code, header_hash, socket]
|
51
|
+
end
|
52
|
+
|
53
|
+
# Executes a GET request to the given URI. Will yield the status, header hash and a chunk of the body
|
54
|
+
# to the given block.
|
55
|
+
#
|
56
|
+
# The socket will be read from as long as the block given to the method yields a truthy value.
|
57
|
+
# Once the block returns a truthy value (or the HTTP response is read completely) the method
|
58
|
+
# will return the number of bytes of the body it did read and terminate.
|
59
|
+
#
|
60
|
+
# @param uri[String] the full URI of the request
|
61
|
+
# @param request_headers[Hash] all the request headers to send with the request
|
62
|
+
# @param chunk_size[Numeric] what size to feed to read() when reading the response from the socket
|
63
|
+
# @yield [Array<Numeric, Hash, String>] the status code, the header hash and the chunk of the body data read.
|
64
|
+
# @return [Numeric] the total number of body bytes read from the socket
|
65
|
+
def perform_get(uri, request_headers: {}, chunk_size: 1024 * 1024 * 5)
|
66
|
+
status_code, header_hash, socket = get_status_headers_and_body_socket(uri, request_headers: request_headers)
|
67
|
+
body_bytes_received = 0
|
68
|
+
# ...and then just read the body, without any buffering, using a non-blocking read
|
69
|
+
while !socket.eof?
|
70
|
+
begin
|
71
|
+
data = socket.read_nonblock(chunk_size)
|
72
|
+
body_bytes_received += data.bytesize
|
73
|
+
return unless yield(status_code, header_hash, data)
|
74
|
+
rescue IO::WaitReadable
|
75
|
+
IO.select([socket], [], SOCKET_TIMEOUT)
|
76
|
+
retry
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Yield the status and headers once with an empty response
|
81
|
+
# so that the client gets at least something.
|
82
|
+
if body_bytes_received.zero?
|
83
|
+
yield(status_code, header_hash, '')
|
84
|
+
end
|
85
|
+
|
86
|
+
body_bytes_received
|
87
|
+
ensure
|
88
|
+
socket.close if socket && !socket.closed?
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
|
94
|
+
# Parses a large string with CRLF separators within it into a neat Hash of header key=>values.
|
95
|
+
def parse_status_and_headers(headers_str)
|
96
|
+
status_and_headers = headers_str.split("\r\n")
|
97
|
+
status_line = status_and_headers.shift
|
98
|
+
|
99
|
+
# TODO: there is no support for repeating headers (like Cookie:)
|
100
|
+
header_hash = status_and_headers.each_with_object({}) do | header, h|
|
101
|
+
split_at = header.index(':')
|
102
|
+
key, value = header[0...split_at], header[(split_at + 1)..-1]
|
103
|
+
h[key] = value.strip
|
104
|
+
end
|
105
|
+
raise "Invalid response status line #{status_line}" unless status_line =~ STATUS_PAT
|
106
|
+
|
107
|
+
http_version, status_code, status = $1, $2, $3
|
108
|
+
[status_code.to_i, header_hash]
|
109
|
+
end
|
110
|
+
|
111
|
+
# Buffer the data from the socket until we encounter the end of the headers (2x CRLF).
|
112
|
+
# Do it per byte so we can leave the socket exactly at byte offset 0 of the response body.
|
113
|
+
def read_ahead_headers(socket)
|
114
|
+
headers_str = ''
|
115
|
+
headers_and_start_of_body = ''
|
116
|
+
start_of_body = ''
|
117
|
+
|
118
|
+
while byte = socket.read(1) do
|
119
|
+
raise "Response header size limit reached" if headers_and_start_of_body.bytesize > HEADER_LIMIT
|
120
|
+
headers_and_start_of_body << byte
|
121
|
+
|
122
|
+
if headers_and_start_of_body[-4..-1] == HEADER_SEPARATOR
|
123
|
+
headers_str = headers_and_start_of_body[0...-4]
|
124
|
+
return headers_str
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
raise 'No header terminating \r\n\r\n found in the response'
|
129
|
+
end
|
130
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
describe 'Microget' do
|
4
|
+
describe '.get_status_headers_and_body_socket' do
|
5
|
+
it 'returns the status, headers and the socket to read the body from, writes the request to the socket' do
|
6
|
+
|
7
|
+
http_response = "HTTP/1.0 200 OK\r\n" +
|
8
|
+
"Content-Type: text/plain\r\n" +
|
9
|
+
"\r\n" +
|
10
|
+
"Yes!"
|
11
|
+
|
12
|
+
# Use a fake TCPSocket substituted with a StringIO
|
13
|
+
fake_socket = StringIO.new(http_response)
|
14
|
+
request_bytes = []
|
15
|
+
allow(fake_socket).to receive(:write) {|bytes| request_bytes << bytes }
|
16
|
+
|
17
|
+
expect(TCPSocket).to receive(:open).with('0.0.0.0', 9393) { fake_socket }
|
18
|
+
|
19
|
+
alive_check_url = "http://0.0.0.0:9393/alive"
|
20
|
+
|
21
|
+
status, headers, socket = Microget.get_status_headers_and_body_socket(alive_check_url)
|
22
|
+
|
23
|
+
expect(request_bytes).to eq(["GET /alive HTTP/1.1\r\n", "Host: 0.0.0.0:9393\r\n", "Connection: close\r\n", "\r\n"])
|
24
|
+
|
25
|
+
expect(status).to be_kind_of(Fixnum)
|
26
|
+
expect(status).to eq(200)
|
27
|
+
|
28
|
+
expect(headers).to be_kind_of(Hash)
|
29
|
+
expect(headers).not_to be_empty
|
30
|
+
expect(headers['Content-Type']).to eq('text/plain')
|
31
|
+
|
32
|
+
expect(socket).to eq(fake_socket)
|
33
|
+
expect(socket).not_to be_closed
|
34
|
+
expect(socket).not_to be_eof
|
35
|
+
|
36
|
+
yes = socket.read
|
37
|
+
expect(socket).to be_eof
|
38
|
+
|
39
|
+
expect(yes).to eq('Yes!')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
describe "Microget running against a real server" do
|
4
|
+
before :all do
|
5
|
+
rack_app = File.expand_path(File.join(__dir__, 'streaming_app.ru'))
|
6
|
+
@server = ServerRunner.new("bundle exec puma --port %d %s", 9393, rack_app)
|
7
|
+
@server.start!
|
8
|
+
sleep 0.5 until @server.running?
|
9
|
+
end
|
10
|
+
|
11
|
+
after :all do
|
12
|
+
@server.stop!
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.perform_get' do
|
16
|
+
|
17
|
+
it 'still yields status and headers with an empty response' do
|
18
|
+
uri = 'http://localhost:9393/empty-response'
|
19
|
+
expect { |b|
|
20
|
+
Microget.perform_get(uri, &b)
|
21
|
+
}.to yield_with_args(304, {"Location"=>"http://elsewhere.com", "Connection"=>"close"}, "")
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'streams 512 megabytes of random data from the server without too much trouble' do
|
25
|
+
uri = 'http://localhost:9393/huge-response'
|
26
|
+
|
27
|
+
bytes_received = 0
|
28
|
+
Microget.perform_get(uri, request_headers: {}, chunk_size: 256) do | status, headers, body_chunk|
|
29
|
+
bytes_received += body_chunk.bytesize
|
30
|
+
end
|
31
|
+
expect(bytes_received).to eq(512 * 1024 * 1024)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'fetches from the streaming app on Puma using proper chunking' do
|
35
|
+
uri = 'http://localhost:9393/with-content-length'
|
36
|
+
|
37
|
+
time_deltas_and_chunks = []
|
38
|
+
read_headers = {}
|
39
|
+
t = Time.now
|
40
|
+
|
41
|
+
# Read in larger chunks - nonblocking read will read what it can and then select() anyway, so the timings
|
42
|
+
# should be accurate on the outpue
|
43
|
+
Microget.perform_get(uri, request_headers: {}, chunk_size: 256) do | status, headers, body_chunk|
|
44
|
+
read_headers.merge!(headers)
|
45
|
+
expect(status).to eq(200)
|
46
|
+
expect(headers).to have_key('Content-Length')
|
47
|
+
expect(body_chunk).to be_kind_of(String)
|
48
|
+
time_deltas_and_chunks << [Time.now - t, body_chunk]
|
49
|
+
t = Time.now
|
50
|
+
end
|
51
|
+
|
52
|
+
time_deltas_and_chunks.each do |(delta, chunk_contents)|
|
53
|
+
expect(chunk_contents).to include('Message number ')
|
54
|
+
expect(delta).to be_within(0.2).of(1.0) # The server "drips" down one message every second, approximately
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
class ServerRunner < Struct.new(:command, :port, :rackup_file_path)
|
4
|
+
def command
|
5
|
+
super % [port, rackup_file_path]
|
6
|
+
end
|
7
|
+
|
8
|
+
def start!
|
9
|
+
# Boot Puma in a forked process
|
10
|
+
@pid = fork do
|
11
|
+
$stderr.puts "Spinning up with #{command.inspect}"
|
12
|
+
# Do not pollute the RSpec output with the Puma logs,
|
13
|
+
# save the stuff to logfiles instead
|
14
|
+
# $stdout.reopen(File.open('server_runner_stdout.log' % name, 'a'))
|
15
|
+
# $stderr.reopen(File.open('server_runner_stderr.log' % name, 'a'))
|
16
|
+
|
17
|
+
# Since we have to do with timing tolerances, having the output drip in ASAP is useful
|
18
|
+
$stdout.sync = true
|
19
|
+
$stderr.sync = true
|
20
|
+
exec(command)
|
21
|
+
end
|
22
|
+
|
23
|
+
Thread.abort_on_exception = true
|
24
|
+
|
25
|
+
Thread.new do
|
26
|
+
t = Time.now
|
27
|
+
# Wait for Puma to be online, poll the alive URL until it stops responding
|
28
|
+
loop do
|
29
|
+
sleep 0.5
|
30
|
+
begin
|
31
|
+
alive_check_url = "http://0.0.0.0:%d/alive" % port
|
32
|
+
response = Net::HTTP.get_response(URI(alive_check_url))
|
33
|
+
@running = true
|
34
|
+
break
|
35
|
+
rescue Errno::ECONNREFUSED
|
36
|
+
if Time.now - t > 2 # Timeout when starting
|
37
|
+
raise "Could not get the server started in 2 seconds, something might be misconfigured"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
trap("TERM") { stop! }
|
44
|
+
end
|
45
|
+
|
46
|
+
def running?
|
47
|
+
!!@running
|
48
|
+
end
|
49
|
+
|
50
|
+
def stop!
|
51
|
+
return unless @pid
|
52
|
+
|
53
|
+
# Tell the webserver to quit, twice (we do not care if there are running responses)
|
54
|
+
%W( TERM TERM KILL ).each {|sig| Process.kill(sig, @pid); sleep 0.5 }
|
55
|
+
@pid = nil
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
# The test app
|
4
|
+
class Streamer
|
5
|
+
class TestBody
|
6
|
+
def each
|
7
|
+
File.open("/tmp/streamer_messages.log", "w") do |f|
|
8
|
+
25.times do |i|
|
9
|
+
sleep 1
|
10
|
+
yield "Message number #{i}\n"
|
11
|
+
f.puts(i)
|
12
|
+
f.flush # Make sure it is on disk
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def close
|
18
|
+
FileUtils.touch('/tmp/streamer_close.mark')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.call(env)
|
23
|
+
# The absence of Content-Length will trigger Rack::Chunking into work automatically.
|
24
|
+
[200, {'Content-Type' => 'text/plain'}, TestBody.new]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class StreamerWithLength < Streamer
|
29
|
+
def self.call(env)
|
30
|
+
s, h, b = super
|
31
|
+
[s, h.merge('Content-Length' => '440'), b]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class HugeBody
|
36
|
+
def each
|
37
|
+
512.times do
|
38
|
+
yield SecureRandom.random_bytes(1024 * 1024)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
map '/huge-response' do
|
44
|
+
run ->(env) {
|
45
|
+
[200, {'Content-Length' => (512 * 1024 * 1024).to_s}, HugeBody.new]
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
map '/empty-response' do
|
50
|
+
run ->(env) {
|
51
|
+
[304, {'Location' => 'http://elsewhere.com'}, []]
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
map '/chunked' do
|
56
|
+
use Rack::Chunked
|
57
|
+
run Streamer
|
58
|
+
end
|
59
|
+
|
60
|
+
map '/with-content-length' do
|
61
|
+
run StreamerWithLength
|
62
|
+
end
|
63
|
+
|
64
|
+
map '/alive' do
|
65
|
+
run ->(env) { [200, {}, ['Yes']]}
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: microget
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julik Tarkhanov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: puma
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: yard
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.3.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.3.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rdoc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: jeweler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.0.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.0.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Pedal-to-the-metal HTTP client for GET requests
|
112
|
+
email: me@julik.nl
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files:
|
116
|
+
- LICENSE.txt
|
117
|
+
- README.md
|
118
|
+
files:
|
119
|
+
- ".document"
|
120
|
+
- ".rspec"
|
121
|
+
- ".yardopts"
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- lib/microget.rb
|
127
|
+
- spec/helper.rb
|
128
|
+
- spec/microget_spec.rb
|
129
|
+
- spec/microget_with_real_server_spec.rb
|
130
|
+
- spec/server_runner.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/streaming_app.ru
|
133
|
+
homepage: http://github.com/julik/microget
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.2.2
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: Designed for slow and/or large response bodies
|
157
|
+
test_files: []
|