http2 0.0.28 → 0.0.33
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +150 -28
- data/Rakefile +20 -31
- data/lib/http2.rb +105 -70
- data/lib/http2/base_request.rb +22 -0
- data/{include → lib/http2}/connection.rb +56 -28
- data/lib/http2/cookie.rb +22 -0
- data/lib/http2/errors.rb +18 -0
- data/lib/http2/get_request.rb +33 -0
- data/{include → lib/http2}/post_data_generator.rb +7 -6
- data/{include → lib/http2}/post_multipart_request.rb +20 -20
- data/{include → lib/http2}/post_request.rb +17 -22
- data/lib/http2/response.rb +109 -0
- data/{include → lib/http2}/response_reader.rb +75 -42
- data/{include → lib/http2}/url_builder.rb +6 -6
- data/lib/http2/utils.rb +52 -0
- data/spec/helpers.rb +27 -0
- data/spec/http2/cookies_spec.rb +18 -0
- data/spec/http2/get_request_spec.rb +11 -0
- data/spec/http2/post_data_generator_spec.rb +2 -1
- data/spec/http2/post_multipart_request_spec.rb +11 -0
- data/spec/http2/post_request_spec.rb +11 -0
- data/spec/http2/response_reader_spec.rb +12 -0
- data/spec/http2/response_spec.rb +73 -0
- data/spec/http2/url_builder_spec.rb +1 -1
- data/spec/http2_spec.rb +107 -89
- data/spec/spec_helper.rb +8 -8
- data/spec/spec_root/content_type_test.rhtml +4 -0
- data/spec/spec_root/cookie_test.rhtml +8 -0
- data/spec/spec_root/json_test.rhtml +9 -0
- data/spec/spec_root/multipart_test.rhtml +28 -0
- data/spec/spec_root/redirect_test.rhtml +3 -0
- data/spec/spec_root/unauthorized.rhtml +3 -0
- data/spec/spec_root/unsupported_media_type.rhtml +3 -0
- metadata +118 -36
- data/.document +0 -5
- data/.rspec +0 -1
- data/Gemfile +0 -14
- data/Gemfile.lock +0 -77
- data/VERSION +0 -1
- data/http2.gemspec +0 -77
- data/include/errors.rb +0 -11
- data/include/get_request.rb +0 -34
- data/include/response.rb +0 -73
- data/include/utils.rb +0 -40
- data/shippable.yml +0 -7
data/http2.gemspec
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: http2 0.0.28 ruby lib
|
6
|
-
|
7
|
-
Gem::Specification.new do |s|
|
8
|
-
s.name = "http2"
|
9
|
-
s.version = "0.0.28"
|
10
|
-
|
11
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
-
s.require_paths = ["lib"]
|
13
|
-
s.authors = ["Kasper Johansen"]
|
14
|
-
s.date = "2014-12-03"
|
15
|
-
s.description = "A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more."
|
16
|
-
s.email = "k@spernj.org"
|
17
|
-
s.extra_rdoc_files = [
|
18
|
-
"LICENSE.txt",
|
19
|
-
"README.md"
|
20
|
-
]
|
21
|
-
s.files = [
|
22
|
-
".document",
|
23
|
-
".rspec",
|
24
|
-
"Gemfile",
|
25
|
-
"Gemfile.lock",
|
26
|
-
"LICENSE.txt",
|
27
|
-
"README.md",
|
28
|
-
"Rakefile",
|
29
|
-
"VERSION",
|
30
|
-
"http2.gemspec",
|
31
|
-
"include/connection.rb",
|
32
|
-
"include/errors.rb",
|
33
|
-
"include/get_request.rb",
|
34
|
-
"include/post_data_generator.rb",
|
35
|
-
"include/post_multipart_request.rb",
|
36
|
-
"include/post_request.rb",
|
37
|
-
"include/response.rb",
|
38
|
-
"include/response_reader.rb",
|
39
|
-
"include/url_builder.rb",
|
40
|
-
"include/utils.rb",
|
41
|
-
"lib/http2.rb",
|
42
|
-
"shippable.yml",
|
43
|
-
"spec/http2/post_data_generator_spec.rb",
|
44
|
-
"spec/http2/url_builder_spec.rb",
|
45
|
-
"spec/http2_spec.rb",
|
46
|
-
"spec/spec_helper.rb"
|
47
|
-
]
|
48
|
-
s.homepage = "http://github.com/kaspernj/http2"
|
49
|
-
s.licenses = ["MIT"]
|
50
|
-
s.rubygems_version = "2.4.0"
|
51
|
-
s.summary = "A lightweight framework for doing http-connections in Ruby. Supports cookies, keep-alive, compressing and much more."
|
52
|
-
|
53
|
-
if s.respond_to? :specification_version then
|
54
|
-
s.specification_version = 4
|
55
|
-
|
56
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
-
s.add_runtime_dependency(%q<string-cases>, [">= 0"])
|
58
|
-
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
59
|
-
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
60
|
-
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
61
|
-
s.add_development_dependency(%q<jeweler>, [">= 1.8.4"])
|
62
|
-
else
|
63
|
-
s.add_dependency(%q<string-cases>, [">= 0"])
|
64
|
-
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
65
|
-
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
66
|
-
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
67
|
-
s.add_dependency(%q<jeweler>, [">= 1.8.4"])
|
68
|
-
end
|
69
|
-
else
|
70
|
-
s.add_dependency(%q<string-cases>, [">= 0"])
|
71
|
-
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
72
|
-
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
73
|
-
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
74
|
-
s.add_dependency(%q<jeweler>, [">= 1.8.4"])
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
data/include/errors.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
#This class holds various classes for error-handeling.
|
2
|
-
class Http2::Errors
|
3
|
-
class Http2error < RuntimeError
|
4
|
-
attr_accessor :response
|
5
|
-
end
|
6
|
-
|
7
|
-
class Noaccess < Http2error; end
|
8
|
-
class Internalserver < Http2error; end
|
9
|
-
class Notfound < Http2error; end
|
10
|
-
class Badrequest < Http2error; end
|
11
|
-
end
|
data/include/get_request.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
class Http2::GetRequest
|
2
|
-
def initialize(http2, args)
|
3
|
-
@http2, @args, @debug, @nl = http2, http2.parse_args(args), http2.debug, http2.nl
|
4
|
-
end
|
5
|
-
|
6
|
-
def execute
|
7
|
-
@http2.mutex.synchronize do
|
8
|
-
puts "Http2: Writing headers: #{header_string}" if @debug
|
9
|
-
@http2.connection.write(header_string)
|
10
|
-
|
11
|
-
puts "Http2: Reading response." if @debug
|
12
|
-
resp = @http2.read_response(@args)
|
13
|
-
|
14
|
-
puts "Http2: Done with get request." if @debug
|
15
|
-
return resp
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def method
|
22
|
-
if @args[:method]
|
23
|
-
@args[:method].to_s.upcase
|
24
|
-
else
|
25
|
-
"GET"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def header_string
|
30
|
-
header_str = "#{method} /#{@args[:url]} HTTP/1.1#{@nl}"
|
31
|
-
header_str << @http2.header_str(@http2.default_headers(@args), @args)
|
32
|
-
header_str << @nl
|
33
|
-
end
|
34
|
-
end
|
data/include/response.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
#This object will be returned as the response for each request.
|
2
|
-
class Http2::Response
|
3
|
-
#All the data the response contains. Headers, body, cookies, requested URL and more.
|
4
|
-
attr_reader :args
|
5
|
-
attr_accessor :body, :charset, :code, :content_type, :http_version
|
6
|
-
|
7
|
-
#This method should not be called manually.
|
8
|
-
def initialize(args = {})
|
9
|
-
@args = args
|
10
|
-
@args[:headers] = {} if !@args.key?(:headers)
|
11
|
-
@body = args[:body] || ""
|
12
|
-
@debug = @args[:debug]
|
13
|
-
end
|
14
|
-
|
15
|
-
#Returns headers given from the host for the result.
|
16
|
-
#===Examples
|
17
|
-
# headers_hash = res.headers
|
18
|
-
def headers
|
19
|
-
return @args[:headers]
|
20
|
-
end
|
21
|
-
|
22
|
-
#Returns a certain header by name or false if not found.
|
23
|
-
#===Examples
|
24
|
-
# val = res.header("content-type")
|
25
|
-
def header(key)
|
26
|
-
return false if !@args[:headers].key?(key)
|
27
|
-
return @args[:headers][key].first.to_s
|
28
|
-
end
|
29
|
-
|
30
|
-
#Returns true if a header of the given string exists.
|
31
|
-
#===Examples
|
32
|
-
# print "No content-type was given." if !http.header?("content-type")
|
33
|
-
def header?(key)
|
34
|
-
return true if @args[:headers].key?(key) and @args[:headers][key].first.to_s.length > 0
|
35
|
-
return false
|
36
|
-
end
|
37
|
-
|
38
|
-
def content_length
|
39
|
-
header("content-length").to_i if header?("content-length")
|
40
|
-
end
|
41
|
-
|
42
|
-
#Returns the requested URL as a string.
|
43
|
-
#===Examples
|
44
|
-
# res.requested_url #=> "?show=status&action=getstatus"
|
45
|
-
def requested_url
|
46
|
-
raise "URL could not be detected." if !@args[:request_args][:url]
|
47
|
-
return @args[:request_args][:url]
|
48
|
-
end
|
49
|
-
|
50
|
-
# Checks the data that has been sat on the object and raises various exceptions, if it does not validate somehow.
|
51
|
-
def validate!
|
52
|
-
puts "Http2: Validating response length." if @debug
|
53
|
-
validate_body_versus_content_length!
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
# Checks that the length of the body is the same as the given content-length if given.
|
59
|
-
def validate_body_versus_content_length!
|
60
|
-
unless self.header?("content-length")
|
61
|
-
puts "Http2: No content length given - skipping length validation." if @debug
|
62
|
-
return nil
|
63
|
-
end
|
64
|
-
|
65
|
-
content_length = self.header("content-length").to_i
|
66
|
-
body_length = @body.bytesize
|
67
|
-
|
68
|
-
puts "Http2: Body length: #{body_length}" if @debug
|
69
|
-
puts "Http2: Content length: #{content_length}" if @debug
|
70
|
-
|
71
|
-
raise "Body does not match the given content-length: '#{body_length}', '#{content_length}'." if body_length != content_length
|
72
|
-
end
|
73
|
-
end
|
data/include/utils.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
#This class holds various methods for encoding, decoding and parsing of HTTP-related stuff.
|
2
|
-
class Http2::Utils
|
3
|
-
#URL-encodes a string.
|
4
|
-
def self.urlenc(string)
|
5
|
-
#Thanks to CGI framework
|
6
|
-
string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/) do
|
7
|
-
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
|
8
|
-
end.tr(' ', '+')
|
9
|
-
end
|
10
|
-
|
11
|
-
#URL-decodes a string.
|
12
|
-
def self.urldec(string)
|
13
|
-
#Thanks to CGI framework
|
14
|
-
str = string.to_s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) do
|
15
|
-
[$1.delete('%')].pack('H*')
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
#Parses a cookies-string and returns them in an array.
|
20
|
-
def self.parse_set_cookies(str)
|
21
|
-
str = String.new(str.to_s)
|
22
|
-
return [] if str.length <= 0
|
23
|
-
args = {}
|
24
|
-
cookie_start_regex = /^(.+?)=(.*?)(;\s*|$)/
|
25
|
-
|
26
|
-
match = str.match(cookie_start_regex)
|
27
|
-
raise "Could not match cookie: '#{str}'." if !match
|
28
|
-
str.gsub!(cookie_start_regex, "")
|
29
|
-
|
30
|
-
args["name"] = self.urldec(match[1].to_s)
|
31
|
-
args["value"] = self.urldec(match[2].to_s)
|
32
|
-
|
33
|
-
while match = str.match(/(.+?)=(.*?)(;\s*|$)/)
|
34
|
-
str = str.gsub(match[0], "")
|
35
|
-
args[match[1].to_s.downcase] = match[2].to_s
|
36
|
-
end
|
37
|
-
|
38
|
-
return [args]
|
39
|
-
end
|
40
|
-
end
|