Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

further separation of HTTP, HTTPBody and HTTPRequest

  • Loading branch information...
commit a2a6450249d25be34f24badadab48a063335af7b 1 parent fd91c63
@hukl authored
Showing with 36 additions and 46 deletions.
  1. +1 −43 lib/rig/http.rb
  2. +18 −1 lib/rig/http_body.rb
  3. +17 −2 lib/rig/http_header.rb
View
44 lib/rig/http.rb
@@ -16,7 +16,7 @@ class HTTP
def initialize *options
@options = normalize_options( options )
@body = HTTPBody.new( @options )
- @header = HTTPHeader.new( prepare_header )
+ @header = HTTPHeader.new( @options, @body )
end
def with_body?
@@ -27,33 +27,6 @@ def http_method
@options[:http_method] || "GET"
end
- def prepare_header
-
- header = {
- "" => "#{@options[:http_method]} #{path} HTTP/1.1",
- "Host" => @options[:host],
- "Origin" => "localhost",
- "Content-Length" => @body.join.bytes.to_a.length,
- "Content-Type" => determine_content_type
- }.merge(
- (@options[:custom_header] || {})
- ).merge(
- "Connection" => "close"
- )
- end
-
- def determine_content_type
- if @body.multipart?
- "multipart/form-data; boundary=#{@body.boundary}"
- else
- if %w(POST PUT).include?( http_method )
- "application/x-www-form-urlencoded; charset=UTF-8"
- else
- "text/plain"
- end
- end
- end
-
def normalize_options options
options.flatten!
@@ -94,21 +67,6 @@ def normalize_options options
options
end
- def generate_uri options
- if options.first.is_a?( String )
- URI.parse( options.first )
- elsif options.first.is_a?( Hash )
- host = options[:host]
-
- raise NoHostProvided unless host
-
- port = (options[:port] || 80)
- path = (options[:path] || "").gsub(/^\//, "")
-
- URI.parse( "#{host}:#{port}/#{path}" )
- end
- end
-
def method_missing name, *args, &block
if options[name]
return options[name]
View
19 lib/rig/http_body.rb
@@ -2,7 +2,8 @@ module Rig
class HTTPBody < Array
def initialize options
- @options = options[:body] || {}
+ @options = options[:body] || {}
+ @http_method = options[:http_method]
if multipart?
create_multipart_body
@@ -11,6 +12,22 @@ def initialize options
end
end
+ def content_length
+ join.bytes.to_a.length
+ end
+
+ def content_type
+ if multipart?
+ "multipart/form-data; boundary=#{boundary}"
+ else
+ if %w(POST PUT).include?( @http_method )
+ "application/x-www-form-urlencoded; charset=UTF-8"
+ else
+ "text/plain"
+ end
+ end
+ end
+
def boundary
@boundary ||= "----rigHTTPmultipart#{rand(2**32)}XZWCFOOBAR"
end
View
19 lib/rig/http_header.rb
@@ -1,8 +1,23 @@
module Rig
class HTTPHeader < Hash
- def initialize options
- super.merge! options
+ def initialize options, body
+ http_method = options[:http_method]
+ path = options[:path]
+
+ header = {
+ "" => "#{http_method} #{path} HTTP/1.1",
+ "Host" => options[:host],
+ "Origin" => "localhost",
+ "Content-Length" => body.content_length,
+ "Content-Type" => body.content_type
+ }.merge(
+ (options[:custom_header] || {})
+ ).merge(
+ "Connection" => "close"
+ )
+
+ merge!( header )
end
def to_s
Please sign in to comment.
Something went wrong with that request. Please try again.