azure-storage-common2 2.0.8 → 2.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/azure/core/auth/authorizer.rb +36 -0
- data/lib/azure/core/auth/shared_key.rb +125 -0
- data/lib/azure/core/auth/shared_key_lite.rb +48 -0
- data/lib/azure/core/auth/signer.rb +51 -0
- data/lib/azure/core/default.rb +23 -0
- data/lib/azure/core/error.rb +21 -0
- data/lib/azure/core/filtered_service.rb +45 -0
- data/lib/azure/core/http/debug_filter.rb +36 -0
- data/lib/azure/core/http/http_error.rb +135 -0
- data/lib/azure/core/http/http_filter.rb +53 -0
- data/lib/azure/core/http/http_request.rb +195 -0
- data/lib/azure/core/http/http_response.rb +102 -0
- data/lib/azure/core/http/retry_policy.rb +84 -0
- data/lib/azure/core/http/signer_filter.rb +33 -0
- data/lib/azure/core/service.rb +46 -0
- data/lib/azure/core/signed_service.rb +45 -0
- data/lib/azure/core/utility.rb +244 -0
- data/lib/azure/core/version.rb +33 -0
- data/lib/azure/storage/common/autoload.rb +62 -0
- data/lib/azure/storage/common/client.rb +162 -0
- data/lib/azure/storage/common/client_options.rb +363 -0
- data/lib/azure/storage/common/client_options_error.rb +41 -0
- data/lib/azure/storage/common/configurable.rb +212 -0
- data/lib/azure/storage/common/core/auth/anonymous_signer.rb +43 -0
- data/lib/azure/storage/common/core/auth/shared_access_signature.rb +30 -0
- data/lib/azure/storage/common/core/auth/shared_access_signature_generator.rb +399 -0
- data/lib/azure/storage/common/core/auth/shared_access_signature_signer.rb +57 -0
- data/lib/azure/storage/common/core/auth/shared_key.rb +60 -0
- data/lib/azure/storage/common/core/auth/token_signer.rb +43 -0
- data/lib/azure/storage/common/core/autoload.rb +53 -0
- data/lib/azure/storage/common/core/error.rb +43 -0
- data/lib/azure/storage/common/core/filter/exponential_retry_filter.rb +64 -0
- data/lib/azure/storage/common/core/filter/linear_retry_filter.rb +55 -0
- data/lib/azure/storage/common/core/filter/retry_filter.rb +300 -0
- data/lib/azure/storage/common/core/http_client.rb +82 -0
- data/lib/azure/storage/common/core/sr.rb +85 -0
- data/lib/azure/storage/common/core/token_credential.rb +64 -0
- data/lib/azure/storage/common/core/utility.rb +261 -0
- data/lib/azure/storage/common/core.rb +35 -0
- data/lib/azure/storage/common/default.rb +868 -0
- data/lib/azure/storage/common/service/access_policy.rb +37 -0
- data/lib/azure/storage/common/service/cors.rb +38 -0
- data/lib/azure/storage/common/service/cors_rule.rb +48 -0
- data/lib/azure/storage/common/service/enumeration_results.rb +32 -0
- data/lib/azure/storage/common/service/geo_replication.rb +40 -0
- data/lib/azure/storage/common/service/logging.rb +47 -0
- data/lib/azure/storage/common/service/metrics.rb +45 -0
- data/lib/azure/storage/common/service/retention_policy.rb +37 -0
- data/lib/azure/storage/common/service/serialization.rb +335 -0
- data/lib/azure/storage/common/service/signed_identifier.rb +40 -0
- data/lib/azure/storage/common/service/storage_service.rb +322 -0
- data/lib/azure/storage/common/service/storage_service_properties.rb +48 -0
- data/lib/azure/storage/common/service/storage_service_stats.rb +39 -0
- data/lib/azure/storage/common/service/user_delegation_key.rb +50 -0
- data/lib/azure/storage/common/version.rb +49 -0
- metadata +57 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfd8cb6ee4fcad3e2354050762c85bc138c8963e9d4e53f2398c98132ff64e8c
|
4
|
+
data.tar.gz: 9e4a633ebc65ff91386edd548a400ce240d34f86b20191f43b020d9aea27007d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4c28756c8623d31ff6c9d50c05ee43ce5ed689310e970fda1b0945513fbef08a59edd317a7a722e0db30272f8c742f39bdfa2944b208b475b2f715e72ad112b
|
7
|
+
data.tar.gz: 6fe00a0bbc206db2047923e076dfcc0c70e83da661411368ad6a2220b5e68ba564c23b6fff0f1a2539914b4fae47d1bbd664bc172bc53c0fd1f1232fb35e6d52
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
require "azure/core/configuration"
|
16
|
+
|
17
|
+
module Azure
|
18
|
+
module Core
|
19
|
+
module Auth
|
20
|
+
class Authorizer
|
21
|
+
# Public: Signs an HTTP request before it's made, by adding the
|
22
|
+
# Authorization header
|
23
|
+
#
|
24
|
+
# request - An Azure::Core::HttpRequest that hasn't been signed
|
25
|
+
# signer - A signing strategy, such as Azure::Table::Auth::SharedKey
|
26
|
+
#
|
27
|
+
# Returns the modified request
|
28
|
+
def sign(request, signer)
|
29
|
+
signature = signer.sign(request.method, request.uri, request.headers)
|
30
|
+
request.headers['Authorization'] = "#{signer.name} #{signature}"
|
31
|
+
request
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
require 'cgi'
|
16
|
+
require 'azure/core/auth/signer'
|
17
|
+
|
18
|
+
module Azure
|
19
|
+
module Core
|
20
|
+
module Auth
|
21
|
+
class SharedKey < Signer
|
22
|
+
# The Azure account's name.
|
23
|
+
attr :account_name
|
24
|
+
|
25
|
+
# Initialize the Signer.
|
26
|
+
#
|
27
|
+
# @param account_name [String] The account name. Defaults to the one in the
|
28
|
+
# global configuration.
|
29
|
+
# @param access_key [String] The access_key encoded in Base64. Defaults to the
|
30
|
+
# one in the global configuration.
|
31
|
+
def initialize(account_name=Azure.storage_account_name, access_key=Azure.storage_access_key)
|
32
|
+
@account_name = account_name
|
33
|
+
super(access_key)
|
34
|
+
end
|
35
|
+
|
36
|
+
# The name of the strategy.
|
37
|
+
#
|
38
|
+
# @return [String]
|
39
|
+
def name
|
40
|
+
'SharedKey'
|
41
|
+
end
|
42
|
+
|
43
|
+
# Create the signature for the request parameters
|
44
|
+
#
|
45
|
+
# @param method [Symbol] HTTP request method.
|
46
|
+
# @param uri [URI] URI of the request we're signing.
|
47
|
+
# @param headers [Hash] HTTP request headers.
|
48
|
+
#
|
49
|
+
# @return [String] base64 encoded signature
|
50
|
+
def sign(method, uri, headers)
|
51
|
+
"#{account_name}:#{super(signable_string(method, uri, headers))}"
|
52
|
+
end
|
53
|
+
|
54
|
+
# Sign the request
|
55
|
+
#
|
56
|
+
# @param req [Azure::Core::Http::HttpRequest] HTTP request to sign
|
57
|
+
#
|
58
|
+
# @return [Azure::Core::Http::HttpRequest]
|
59
|
+
def sign_request(req)
|
60
|
+
# Need to make sure Content-Length is correctly set.
|
61
|
+
if ((!req.body.nil?)) then
|
62
|
+
if (req.body.respond_to? :bytesize) then
|
63
|
+
req.headers['Content-Length'] = req.body.bytesize.to_s
|
64
|
+
elsif (req.body.respond_to? :size)
|
65
|
+
req.headers['Content-Length'] = req.body.size.to_s
|
66
|
+
end
|
67
|
+
end
|
68
|
+
req.headers['Authorization'] = "#{name} #{sign(req.method, req.uri, req.headers)}"
|
69
|
+
req
|
70
|
+
end
|
71
|
+
|
72
|
+
# Generate the string to sign.
|
73
|
+
#
|
74
|
+
# @param method [Symbol] HTTP request method.
|
75
|
+
# @param uri [URI] URI of the request we're signing.
|
76
|
+
# @param headers [Hash] HTTP request headers.
|
77
|
+
#
|
78
|
+
# @return [String]
|
79
|
+
def signable_string(method, uri, headers)
|
80
|
+
[
|
81
|
+
method.to_s.upcase,
|
82
|
+
headers.fetch('Content-Encoding', ''),
|
83
|
+
headers.fetch('Content-Language', ''),
|
84
|
+
headers.fetch('Content-Length', ''),
|
85
|
+
headers.fetch('Content-MD5', ''),
|
86
|
+
headers.fetch('Content-Type', ''),
|
87
|
+
headers.fetch('Date', ''),
|
88
|
+
headers.fetch('If-Modified-Since', ''),
|
89
|
+
headers.fetch('If-Match', ''),
|
90
|
+
headers.fetch('If-None-Match', ''),
|
91
|
+
headers.fetch('If-Unmodified-Since', ''),
|
92
|
+
headers.fetch('Range', ''),
|
93
|
+
canonicalized_headers(headers),
|
94
|
+
canonicalized_resource(uri)
|
95
|
+
].join("\n")
|
96
|
+
end
|
97
|
+
|
98
|
+
# Calculate the Canonicalized Headers string for a request.
|
99
|
+
#
|
100
|
+
# @param headers [Hash] HTTP request headers.
|
101
|
+
#
|
102
|
+
# @return [String] a string with the canonicalized headers.
|
103
|
+
def canonicalized_headers(headers)
|
104
|
+
headers = headers.map { |k,v| [k.to_s.downcase, v] }
|
105
|
+
headers.select! { |k,_| k =~ /^x-ms-/ }
|
106
|
+
headers.sort_by! { |(k,_)| k }
|
107
|
+
headers.map! { |k,v| '%s:%s' % [k, v] }.join("\n")
|
108
|
+
end
|
109
|
+
|
110
|
+
# Calculate the Canonicalized Resource string for a request.
|
111
|
+
#
|
112
|
+
# @param uri [URI] URI of the request we're signing.
|
113
|
+
#
|
114
|
+
# @return [String] a string with the canonicalized resource.
|
115
|
+
def canonicalized_resource(uri)
|
116
|
+
resource = '/' + account_name + (uri.path.empty? ? '/' : uri.path)
|
117
|
+
params = CGI.parse(uri.query.to_s).map { |k,v| [k.downcase, v] }
|
118
|
+
params.sort_by! { |k,_| k }
|
119
|
+
params.map! { |k,v| '%s:%s' % [k, v.map(&:strip).sort.join(',')] }
|
120
|
+
[resource, *params].join("\n")
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
require "azure/core/auth/shared_key"
|
16
|
+
|
17
|
+
module Azure
|
18
|
+
module Core
|
19
|
+
module Auth
|
20
|
+
class SharedKeyLite < SharedKey
|
21
|
+
# The name of the strategy.
|
22
|
+
#
|
23
|
+
# @return [String]
|
24
|
+
def name
|
25
|
+
'SharedKeyLite'
|
26
|
+
end
|
27
|
+
|
28
|
+
# Generate the string to sign.
|
29
|
+
#
|
30
|
+
# @param method [Symbol] The HTTP request method.
|
31
|
+
# @param uri [URI] The URI of the request we're signing.
|
32
|
+
# @param headers [Hash] A Hash of HTTP request headers.
|
33
|
+
#
|
34
|
+
# Returns a plain text string.
|
35
|
+
def signable_string(method, uri, headers)
|
36
|
+
[
|
37
|
+
method.to_s.upcase,
|
38
|
+
headers.fetch('Content-MD5', ''),
|
39
|
+
headers.fetch('Content-Type', ''),
|
40
|
+
headers.fetch('Date') { raise IndexError, 'Headers must include Date' },
|
41
|
+
canonicalized_headers(headers),
|
42
|
+
canonicalized_resource(uri)
|
43
|
+
].join("\n")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
require 'openssl'
|
16
|
+
require 'base64'
|
17
|
+
|
18
|
+
module Azure
|
19
|
+
module Core
|
20
|
+
module Auth
|
21
|
+
# Utility class to sign strings with HMAC-256 and then encode the
|
22
|
+
# signed string using Base64.
|
23
|
+
class Signer
|
24
|
+
# The access key for the account
|
25
|
+
attr :access_key
|
26
|
+
|
27
|
+
# Initialize the Signer.
|
28
|
+
#
|
29
|
+
# @param access_key [String] The access_key encoded in Base64.
|
30
|
+
def initialize(access_key)
|
31
|
+
if access_key.nil?
|
32
|
+
raise ArgumentError, 'Signing key must be provided'
|
33
|
+
end
|
34
|
+
|
35
|
+
@access_key = Base64.decode64(access_key)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Generate an HMAC signature.
|
39
|
+
#
|
40
|
+
# @param body [String] The string to sign.
|
41
|
+
#
|
42
|
+
# @return [String] a Base64 String signed with HMAC.
|
43
|
+
def sign(body)
|
44
|
+
signed = OpenSSL::HMAC.digest('sha256', access_key, body)
|
45
|
+
Base64.strict_encode64(signed)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
|
16
|
+
module Azure
|
17
|
+
module Core
|
18
|
+
module Default
|
19
|
+
# Default User Agent header string
|
20
|
+
USER_AGENT = "Azure-Core/#{Azure::Core::Version}".freeze
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
module Azure
|
16
|
+
module Core
|
17
|
+
# Superclass for errors generated from this library, so people can
|
18
|
+
# just rescue this for generic error handling
|
19
|
+
class Error < StandardError;end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
require 'azure/core/service'
|
16
|
+
|
17
|
+
module Azure
|
18
|
+
module Core
|
19
|
+
# A base class for Service implementations
|
20
|
+
class FilteredService < Service
|
21
|
+
|
22
|
+
# Create a new instance of the FilteredService
|
23
|
+
#
|
24
|
+
# @param host [String] The hostname. (optional, Default empty)
|
25
|
+
# @param options [Hash] options including {:client} (optional, Default {})
|
26
|
+
def initialize(host='', options={})
|
27
|
+
super
|
28
|
+
@filters = []
|
29
|
+
end
|
30
|
+
|
31
|
+
attr_accessor :filters
|
32
|
+
|
33
|
+
def call(method, uri, body=nil, headers=nil, options={})
|
34
|
+
super(method, uri, body, headers) do |request|
|
35
|
+
filters.reverse.each { |filter| request.with_filter filter, options } if filters
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def with_filter(filter=nil, &block)
|
40
|
+
filter = filter || block
|
41
|
+
filters.push filter if filter
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
require "azure/core/http/http_filter"
|
16
|
+
|
17
|
+
module Azure
|
18
|
+
module Core
|
19
|
+
module Http
|
20
|
+
# A HttpFilter implementation that displays information about the request and response for debugging
|
21
|
+
class DebugFilter < HttpFilter
|
22
|
+
def call(req, _next)
|
23
|
+
puts "--REQUEST-BEGIN---------------------------"
|
24
|
+
puts "method:", req.method, "uri:", req.uri, "headers:", req.headers, "body:", req.body
|
25
|
+
puts "--REQUEST-END---------------------------"
|
26
|
+
|
27
|
+
r = _next.call
|
28
|
+
puts "--RESPONSE-BEGIN---------------------------"
|
29
|
+
puts "status_code:", r.status_code, "headers:", r.headers, "body:", r.body
|
30
|
+
puts "--RESPONSE-END---------------------------"
|
31
|
+
r
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
require 'azure/core/error'
|
16
|
+
require 'nokogiri'
|
17
|
+
require 'json'
|
18
|
+
|
19
|
+
module Azure
|
20
|
+
module Core
|
21
|
+
module Http
|
22
|
+
# Public: Class for handling all HTTP response errors
|
23
|
+
class HTTPError < Azure::Core::Error
|
24
|
+
|
25
|
+
# Public: Detail of the response
|
26
|
+
#
|
27
|
+
# Returns an Azure::Core::Http::HttpResponse object
|
28
|
+
attr :http_response
|
29
|
+
|
30
|
+
# Public: The request URI
|
31
|
+
#
|
32
|
+
# Returns a String
|
33
|
+
attr :uri
|
34
|
+
|
35
|
+
# Public: The HTTP status code of this error
|
36
|
+
#
|
37
|
+
# Returns a Fixnum
|
38
|
+
attr :status_code
|
39
|
+
|
40
|
+
# Public: The type of error
|
41
|
+
#
|
42
|
+
# http://msdn.microsoft.com/en-us/library/azure/dd179357
|
43
|
+
#
|
44
|
+
# Returns a String
|
45
|
+
attr :type
|
46
|
+
|
47
|
+
# Public: Description of the error
|
48
|
+
#
|
49
|
+
# Returns a String
|
50
|
+
attr :description
|
51
|
+
|
52
|
+
# Public: Detail of the error
|
53
|
+
#
|
54
|
+
# Returns a String
|
55
|
+
attr :detail
|
56
|
+
|
57
|
+
# Public: The header name whose value is invalid
|
58
|
+
#
|
59
|
+
# Returns a String
|
60
|
+
attr :header
|
61
|
+
|
62
|
+
# Public: The invalid header value
|
63
|
+
#
|
64
|
+
# Returns a String
|
65
|
+
attr :header_value
|
66
|
+
|
67
|
+
# Public: Initialize an error
|
68
|
+
#
|
69
|
+
# http_response - An Azure::Core::HttpResponse
|
70
|
+
def initialize(http_response)
|
71
|
+
@http_response = http_response
|
72
|
+
@uri = http_response.uri
|
73
|
+
@status_code = http_response.status_code
|
74
|
+
parse_response
|
75
|
+
# Use reason phrase as the description if description is empty
|
76
|
+
@description = http_response.reason_phrase if (@description.nil? || @description.empty?) && http_response.reason_phrase
|
77
|
+
super("#{type} (#{status_code}): #{description}")
|
78
|
+
end
|
79
|
+
|
80
|
+
# Extract the relevant information from the response's body. If the response
|
81
|
+
# body is not an XML, we return an 'Unknown' error with the entire body as
|
82
|
+
# the description
|
83
|
+
#
|
84
|
+
# Returns nothing
|
85
|
+
def parse_response
|
86
|
+
if @http_response.body && @http_response.respond_to?(:headers) && @http_response.headers['Content-Type']
|
87
|
+
if @http_response.headers['Content-Type'].include?('xml')
|
88
|
+
parse_xml_response
|
89
|
+
elsif @http_response.headers['Content-Type'].include?('json')
|
90
|
+
parse_json_response
|
91
|
+
end
|
92
|
+
else
|
93
|
+
parse_unknown_response
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def parse_xml_response
|
98
|
+
document = Nokogiri.Slop(@http_response.body)
|
99
|
+
|
100
|
+
@type = document.css('code').first.text if document.css('code').any?
|
101
|
+
@type = document.css('Code').first.text if document.css('Code').any?
|
102
|
+
@description = document.css('message').first.text if document.css('message').any?
|
103
|
+
@description = document.css('Message').first.text if document.css('Message').any?
|
104
|
+
@header = document.css('headername').first.text if document.css('headername').any?
|
105
|
+
@header = document.css('HeaderName').first.text if document.css('HeaderName').any?
|
106
|
+
@header_value = document.css('headervalue').first.text if document.css('headervalue').any?
|
107
|
+
@header_value = document.css('HeaderValue').first.text if document.css('HeaderValue').any?
|
108
|
+
|
109
|
+
# service bus uses detail instead of message
|
110
|
+
@detail = document.css('detail').first.text if document.css('detail').any?
|
111
|
+
@detail = document.css('Detail').first.text if document.css('Detail').any?
|
112
|
+
end
|
113
|
+
|
114
|
+
def parse_json_response
|
115
|
+
odata_error = JSON.parse(@http_response.body)['odata.error']
|
116
|
+
@type = odata_error['code']
|
117
|
+
@description = odata_error['message']['value']
|
118
|
+
end
|
119
|
+
|
120
|
+
def parse_unknown_response
|
121
|
+
@type = 'Unknown'
|
122
|
+
if @http_response.body
|
123
|
+
@description = "#{@http_response.body.strip}"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def inspect
|
128
|
+
string = "#<#{self.class.name}:#{self.object_id} "
|
129
|
+
fields = self.instance_variables.map{|field| "#{field}: #{self.send(field.to_s.delete("@")).inspect}"}
|
130
|
+
string << fields.join(", ") << ">"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#-------------------------------------------------------------------------
|
2
|
+
# # Copyright (c) Microsoft and contributors. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#--------------------------------------------------------------------------
|
15
|
+
module Azure
|
16
|
+
module Core
|
17
|
+
module Http
|
18
|
+
# A filter which can modify the HTTP pipeline both before and
|
19
|
+
# after requests/responses. Multiple filters can be nested in a
|
20
|
+
# "Russian Doll" model to create a compound HTTP pipeline
|
21
|
+
class HttpFilter
|
22
|
+
|
23
|
+
# Initialize a HttpFilter
|
24
|
+
#
|
25
|
+
# &block - An inline block which implements the filter.
|
26
|
+
#
|
27
|
+
# The inline block should take parameters |request, _next| where
|
28
|
+
# request is a HttpRequest and _next is an object that implements
|
29
|
+
# a method .call which returns an HttpResponse. The block passed
|
30
|
+
# to the constructor should also return HttpResponse, either as
|
31
|
+
# the result of calling _next.call or by customized logic.
|
32
|
+
#
|
33
|
+
def initialize(&block)
|
34
|
+
@block = block
|
35
|
+
end
|
36
|
+
|
37
|
+
# Executes the filter
|
38
|
+
#
|
39
|
+
# request - HttpRequest. The request
|
40
|
+
# _next - An object that implements .call (no params)
|
41
|
+
#
|
42
|
+
# NOTE: _next is a either a subsequent HttpFilter wrapped in a
|
43
|
+
# closure, or the HttpRequest object's call method. Either way,
|
44
|
+
# it must have it's .call method executed within each filter to
|
45
|
+
# complete the pipeline. _next.call should return an HttpResponse
|
46
|
+
# and so should this Filter.
|
47
|
+
def call(request, _next)
|
48
|
+
@block ? @block.call(request, _next) : _next.call
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|