troelskn-handsoap 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION.yml +1 -1
  2. data/lib/handsoap/http.rb +34 -1
  3. metadata +2 -2
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 5
2
+ :patch: 6
3
3
  :major: 0
4
4
  :minor: 5
data/lib/handsoap/http.rb CHANGED
@@ -6,13 +6,19 @@ module Handsoap
6
6
 
7
7
  # Represents a HTTP Request.
8
8
  class Request
9
- attr_reader :url, :http_method, :headers, :body
9
+ attr_reader :url, :http_method, :headers, :body, :username, :password
10
10
  attr_writer :body, :http_method
11
11
  def initialize(url, http_method = :get)
12
12
  @url = url
13
13
  @http_method = http_method
14
14
  @headers = {}
15
15
  @body = nil
16
+ @username = nil
17
+ @password = nil
18
+ end
19
+ def set_auth(username, password)
20
+ @username = username
21
+ @password = password
16
22
  end
17
23
  def add_header(key, value)
18
24
  if @headers[key].nil?
@@ -31,6 +37,13 @@ module Handsoap
31
37
  "===============\n" +
32
38
  "--- Request ---\n" +
33
39
  "#{http_method.to_s.upcase} #{url}\n" +
40
+ (
41
+ if username && password
42
+ "Auth credentials: #{username}:#{password}\n"
43
+ else
44
+ ""
45
+ end
46
+ ) +
34
47
  (
35
48
  if headers.any?
36
49
  "---\n" + headers.map { |key,values| values.map {|value| key + ": " + value + "\n" }.join("") }.join("")
@@ -137,6 +150,11 @@ module Handsoap
137
150
  def self.send_http_request(request)
138
151
  self.load!
139
152
  http_client = HTTPClient.new
153
+ # Set credentials. The driver will negotiate the actual scheme
154
+ if request.username && request.password
155
+ domain = request.url.match(/^(http(s?):\/\/[^\/]+\/)/)[1]
156
+ http_client.set_auth(domain, request.username, request.password)
157
+ end
140
158
  # pack headers
141
159
  headers = request.headers.inject([]) do |arr, (k,v)|
142
160
  arr + v.map {|x| [k,x] }
@@ -163,6 +181,10 @@ module Handsoap
163
181
  def self.send_http_request(request)
164
182
  self.load!
165
183
  http_client = Curl::Easy.new(request.url)
184
+ # Set credentials. The driver will negotiate the actual scheme
185
+ if request.username && request.password
186
+ http_client.userpwd = [request.username, ":", request.password].join
187
+ end
166
188
  # pack headers
167
189
  headers = request.headers.inject([]) do |arr, (k,v)|
168
190
  arr + v.map {|x| "#{k}: #{x}" }
@@ -213,6 +235,10 @@ module Handsoap
213
235
  end
214
236
  http_client = Net::HTTP.new(url.host, url.port)
215
237
  http_client.read_timeout = 120
238
+ if request.username && request.password
239
+ # TODO: http://codesnippets.joyent.com/posts/show/1075
240
+ http_request.basic_auth request.username, request.password
241
+ end
216
242
  request.headers.each do |k, values|
217
243
  values.each do |v|
218
244
  http_request.add_field(k, v)
@@ -233,6 +259,13 @@ module Handsoap
233
259
  h
234
260
  end
235
261
  end
262
+ # net/http only supports basic auth. We raise a warning if the server requires something else.
263
+ if http_response.code == 401 && http_response.get_headers['www-authenticate']
264
+ auth_type = http_response.get_headers['www-authenticate'].chomp.match(/\w+/)[0].downcase
265
+ if auth_type != "basic"
266
+ raise "Authentication type #{auth_type} is unsupported by net/http"
267
+ end
268
+ end
236
269
  Handsoap::Http.parse_http_part(http_response.get_headers, http_response.body, http_response.code)
237
270
  end
238
271
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: troelskn-handsoap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Troels Knak-Nielsen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-21 00:00:00 -07:00
12
+ date: 2009-09-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15