|
4 | 4 |
|
5 | 5 | # Extensions for em-http so that we can use consumer.sign! with an EventMachine::HttpClient
|
6 | 6 | # instance. This is purely syntactic sugar.
|
7 |
| -class EventMachine::HttpClient |
| 7 | +module EventMachine |
| 8 | + class HttpClient |
| 9 | + attr_reader :oauth_helper |
8 | 10 |
|
9 |
| - attr_reader :oauth_helper |
| 11 | + # Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting |
| 12 | + # this may add a header, additional query string parameters, or additional POST body parameters. |
| 13 | + # The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+ |
| 14 | + # header. |
| 15 | + # |
| 16 | + # * http - Configured Net::HTTP instance, ignored in this scenario except for getting host. |
| 17 | + # * consumer - OAuth::Consumer instance |
| 18 | + # * token - OAuth::Token instance |
| 19 | + # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+, |
| 20 | + # +signature_method+, +nonce+, +timestamp+) |
| 21 | + # |
| 22 | + # This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version. |
| 23 | + # |
| 24 | + # See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1] |
| 25 | + def oauth!(http, consumer = nil, token = nil, options = {}) |
| 26 | + options = { :request_uri => normalized_oauth_uri(http), |
| 27 | + :consumer => consumer, |
| 28 | + :token => token, |
| 29 | + :scheme => 'header', |
| 30 | + :signature_method => nil, |
| 31 | + :nonce => nil, |
| 32 | + :timestamp => nil }.merge(options) |
10 | 33 |
|
11 |
| - # Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting |
12 |
| - # this may add a header, additional query string parameters, or additional POST body parameters. |
13 |
| - # The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+ |
14 |
| - # header. |
15 |
| - # |
16 |
| - # * http - Configured Net::HTTP instance, ignored in this scenario except for getting host. |
17 |
| - # * consumer - OAuth::Consumer instance |
18 |
| - # * token - OAuth::Token instance |
19 |
| - # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+, |
20 |
| - # +signature_method+, +nonce+, +timestamp+) |
21 |
| - # |
22 |
| - # This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version. |
23 |
| - # |
24 |
| - # See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1] |
25 |
| - def oauth!(http, consumer = nil, token = nil, options = {}) |
26 |
| - options = { :request_uri => normalized_oauth_uri(http), |
27 |
| - :consumer => consumer, |
28 |
| - :token => token, |
29 |
| - :scheme => 'header', |
30 |
| - :signature_method => nil, |
31 |
| - :nonce => nil, |
32 |
| - :timestamp => nil }.merge(options) |
33 |
| - |
34 |
| - @oauth_helper = OAuth::Client::Helper.new(self, options) |
35 |
| - self.__send__(:"set_oauth_#{options[:scheme]}") |
36 |
| - end |
| 34 | + @oauth_helper = OAuth::Client::Helper.new(self, options) |
| 35 | + self.__send__(:"set_oauth_#{options[:scheme]}") |
| 36 | + end |
37 | 37 |
|
38 |
| - # Create a string suitable for signing for an HTTP request. This process involves parameter |
39 |
| - # normalization as specified in the OAuth specification. The exact normalization also depends |
40 |
| - # on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request |
41 |
| - # itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+ |
42 |
| - # header. |
43 |
| - # |
44 |
| - # * http - Configured Net::HTTP instance |
45 |
| - # * consumer - OAuth::Consumer instance |
46 |
| - # * token - OAuth::Token instance |
47 |
| - # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+, |
48 |
| - # +signature_method+, +nonce+, +timestamp+) |
49 |
| - # |
50 |
| - # See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1] |
51 |
| - def signature_base_string(http, consumer = nil, token = nil, options = {}) |
52 |
| - options = { :request_uri => normalized_oauth_uri(http), |
53 |
| - :consumer => consumer, |
54 |
| - :token => token, |
55 |
| - :scheme => 'header', |
56 |
| - :signature_method => nil, |
57 |
| - :nonce => nil, |
58 |
| - :timestamp => nil }.merge(options) |
| 38 | + # Create a string suitable for signing for an HTTP request. This process involves parameter |
| 39 | + # normalization as specified in the OAuth specification. The exact normalization also depends |
| 40 | + # on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request |
| 41 | + # itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+ |
| 42 | + # header. |
| 43 | + # |
| 44 | + # * http - Configured Net::HTTP instance |
| 45 | + # * consumer - OAuth::Consumer instance |
| 46 | + # * token - OAuth::Token instance |
| 47 | + # * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+, |
| 48 | + # +signature_method+, +nonce+, +timestamp+) |
| 49 | + # |
| 50 | + # See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1] |
| 51 | + def signature_base_string(http, consumer = nil, token = nil, options = {}) |
| 52 | + options = { :request_uri => normalized_oauth_uri(http), |
| 53 | + :consumer => consumer, |
| 54 | + :token => token, |
| 55 | + :scheme => 'header', |
| 56 | + :signature_method => nil, |
| 57 | + :nonce => nil, |
| 58 | + :timestamp => nil }.merge(options) |
59 | 59 |
|
60 |
| - OAuth::Client::Helper.new(self, options).signature_base_string |
61 |
| - end |
| 60 | + OAuth::Client::Helper.new(self, options).signature_base_string |
| 61 | + end |
62 | 62 |
|
63 |
| - # This code was lifted from the em-http-request because it was removed from |
64 |
| - # the gem June 19, 2010 |
65 |
| - # see: http://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b |
66 |
| - def normalize_uri |
67 |
| - @normalized_uri ||= begin |
68 |
| - uri = @uri.dup |
69 |
| - encoded_query = encode_query(@uri, @options[:query]) |
70 |
| - path, query = encoded_query.split("?", 2) |
71 |
| - uri.query = query unless encoded_query.empty? |
72 |
| - uri.path = path |
73 |
| - uri |
| 63 | + # This code was lifted from the em-http-request because it was removed from |
| 64 | + # the gem June 19, 2010 |
| 65 | + # see: http://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b |
| 66 | + def normalize_uri |
| 67 | + @normalized_uri ||= begin |
| 68 | + uri = @conn.dup |
| 69 | + encoded_query = encode_query(@conn, @req[:query]) |
| 70 | + path, query = encoded_query.split("?", 2) |
| 71 | + uri.query = query unless encoded_query.empty? |
| 72 | + uri.path = path |
| 73 | + uri |
| 74 | + end |
74 | 75 | end
|
75 |
| - end |
76 | 76 |
|
77 |
| - protected |
| 77 | + protected |
78 | 78 |
|
79 |
| - def combine_query(path, query, uri_query) |
80 |
| - combined_query = if query.kind_of?(Hash) |
81 |
| - query.map { |k, v| encode_param(k, v) }.join('&') |
82 |
| - else |
83 |
| - query.to_s |
| 79 | + def combine_query(path, query, uri_query) |
| 80 | + combined_query = if query.kind_of?(Hash) |
| 81 | + query.map { |k, v| encode_param(k, v) }.join('&') |
| 82 | + else |
| 83 | + query.to_s |
| 84 | + end |
| 85 | + if !uri_query.to_s.empty? |
| 86 | + combined_query = [combined_query, uri_query].reject {|part| part.empty?}.join("&") |
| 87 | + end |
| 88 | + combined_query.to_s.empty? ? path : "#{path}?#{combined_query}" |
84 | 89 | end
|
85 |
| - if !uri_query.to_s.empty? |
86 |
| - combined_query = [combined_query, uri_query].reject {|part| part.empty?}.join("&") |
87 |
| - end |
88 |
| - combined_query.to_s.empty? ? path : "#{path}?#{combined_query}" |
89 |
| - end |
90 | 90 |
|
91 |
| - # Since we expect to get the host etc details from the http instance (...), |
92 |
| - # we create a fake url here. Surely this is a horrible, horrible idea? |
93 |
| - def normalized_oauth_uri(http) |
94 |
| - uri = URI.parse(normalize_uri.path) |
95 |
| - uri.host = http.address |
96 |
| - uri.port = http.port |
| 91 | + # Since we expect to get the host etc details from the http instance (...), |
| 92 | + # we create a fake url here. Surely this is a horrible, horrible idea? |
| 93 | + def normalized_oauth_uri(http) |
| 94 | + uri = URI.parse(normalize_uri.path) |
| 95 | + uri.host = http.address |
| 96 | + uri.port = http.port |
97 | 97 |
|
98 |
| - if http.respond_to?(:use_ssl?) && http.use_ssl? |
99 |
| - uri.scheme = "https" |
100 |
| - else |
101 |
| - uri.scheme = "http" |
| 98 | + if http.respond_to?(:use_ssl?) && http.use_ssl? |
| 99 | + uri.scheme = "https" |
| 100 | + else |
| 101 | + uri.scheme = "http" |
| 102 | + end |
| 103 | + uri.to_s |
102 | 104 | end
|
103 |
| - uri.to_s |
104 |
| - end |
105 | 105 |
|
106 |
| - def set_oauth_header |
107 |
| - headers = (self.options[:head] ||= {}) |
108 |
| - headers['Authorization'] = @oauth_helper.header |
109 |
| - end |
| 106 | + def set_oauth_header |
| 107 | + self.req[:head] ||= {} |
| 108 | + self.req[:head].merge!('Authorization' => @oauth_helper.header) |
| 109 | + end |
110 | 110 |
|
111 |
| - def set_oauth_body |
112 |
| - raise NotImplementedError, 'please use the set_oauth_header method instead' |
113 |
| - end |
| 111 | + def set_oauth_body |
| 112 | + raise NotImplementedError, 'please use the set_oauth_header method instead' |
| 113 | + end |
114 | 114 |
|
115 |
| - def set_oauth_query_string |
116 |
| - raise NotImplementedError, 'please use the set_oauth_header method instead' |
| 115 | + def set_oauth_query_string |
| 116 | + raise NotImplementedError, 'please use the set_oauth_header method instead' |
| 117 | + end |
117 | 118 | end
|
118 |
| - |
119 | 119 | end
|
0 commit comments