lib/em-net-http.rb in em-net-http-0.1.0 vs lib/em-net-http.rb in em-net-http-0.1.1

- old
+ new

@@ -4,23 +4,25 @@ require 'net/http' require 'fiber' module EventMachine class NetHTTPResponse - include Enumerable - def each(&blk) - @header.each(&blk) - end + attr_reader :code, :body, :header, :message, :http_version + alias_method :msg, :message - attr_reader :code, :body, :header - def initialize(res) - @code = res.response_header.status.to_s + @code = res.response_header.http_status + @message = res.response_header.http_reason + @http_version = res.response_header.http_version @header = res.response_header @body = res.response end + def content_type + self['content-type'] + end + def [](k) @header[key(k)] end def key?(k) @@ -29,25 +31,50 @@ def read_body(dest=nil,&block) @body end + def to_hash + h={} + @header.each do |k, v| + h[fromkey(k)] = v + end + h + end + private def key(k) k.upcase.tr('-','_') end + def fromkey(k) + k.tr('_', '-').split('-').map{|i|i.capitalize}.join('-') + end + + include Enumerable + def each(&blk) + @header.each(&blk) + end + end end module Net class HTTP def request(req, body = nil, &block) - uri = Addressable::URI.parse("#{use_ssl? ? 'https://' : 'http://'}#{addr_port}#{req.path}") - req = EM::HttpRequest.new(uri).send(req.class::METHOD.downcase.to_sym) f=Fiber.current - req.callback {|res|f.resume(EM::NetHTTPResponse.new(res))} + uri = Addressable::URI.parse("#{use_ssl? ? 'https://' : 'http://'}#{addr_port}#{req.path}") + opts = body.nil? ? {} : {:body => body} + if use_ssl? + sslopts = opts[:ssl] = {} + sslopts[:verify_peer] = verify_mode == OpenSSL::SSL::VERIFY_PEER + sslopts[:private_key_file] = key if key + sslopts[:cert_chain_file] = ca_file if ca_file + end + httpreq = EM::HttpRequest.new(uri).send(req.class::METHOD.downcase.to_sym, opts) + httpreq.callback {|res|f.resume(EM::NetHTTPResponse.new(res))} + httpreq.errback {|res|f.resume(EM::NetHTTPResponse.new(res))} res = Fiber.yield yield res if block_given? res end