parsed-attributes 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/parsed/http/basicauth.rb +19 -0
- data/lib/parsers/http/basicauth.rb +31 -0
- data/lib/parsers/http/cookie.rb +9 -15
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb567d0b511aa92402e92e102bead9f810efc2c4
|
4
|
+
data.tar.gz: 0dfd9fea52e36b6a923aa89ab016e3f5202323ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95615c9b181463833d99f1a5d6424c438282a24bd8ccad5bfb29e29045425d856bf05e7a314377f7fa7887d13d89cdeb376428bb1cddf19f5c2f6182e472f67f
|
7
|
+
data.tar.gz: 67e707b700895528463868ea7067c850fb6061531b61502b7fa574f4d6bb503b8ce5d66f1cdaaef77971455ec7e09ec1afd43ea91ad68a217ab0703a1e3664d3
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative '../base'
|
2
|
+
require_relative '../../parsers/http/basicauth'
|
3
|
+
module Parsed
|
4
|
+
module HTTP
|
5
|
+
module BasicAuth
|
6
|
+
include Parsed::Base
|
7
|
+
|
8
|
+
##
|
9
|
+
# Defines an attribute pair to handle de/encoding HTTP Basic auth strings
|
10
|
+
# ---
|
11
|
+
# Accepts default options, see Parsed:Base
|
12
|
+
def http_basic_auth_attribute(name, options = {})
|
13
|
+
__define_parsed_attributes_all_methods name, options do |raw_value|
|
14
|
+
::BasicAuth.parse raw_value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class BasicAuth
|
2
|
+
class ParserError < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
attr_accessor :username, :password
|
6
|
+
def initialize(user, pass)
|
7
|
+
@username = user
|
8
|
+
@password = pass
|
9
|
+
end
|
10
|
+
|
11
|
+
def userpwd
|
12
|
+
"#{@username}:#{@password}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
[self.userpwd].pack('m').strip
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.parse(raw_value)
|
20
|
+
decoded = raw_value.unpack("m").first
|
21
|
+
si = decoded.index ':'
|
22
|
+
raise ParserError.new 'Malformed string, expecting ":"' if si.nil?
|
23
|
+
|
24
|
+
username = decoded[0...si]
|
25
|
+
password = decoded[si + 1..-1]
|
26
|
+
|
27
|
+
raise ParserError.new 'Username cannot be blank' if username.empty?
|
28
|
+
self.new username, password
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/lib/parsers/http/cookie.rb
CHANGED
@@ -21,7 +21,7 @@ class Cookie
|
|
21
21
|
|
22
22
|
def to_s
|
23
23
|
cookie = ["#{@name}=#{@value}"]
|
24
|
-
cookie << "Expires=#{@expires}" if defined? @
|
24
|
+
cookie << "Expires=#{@expires}" if defined?(@expires) and !defined?(@max_age)
|
25
25
|
cookie << "Max-Age=#{@max_age == Time.at(0) ? 0 : self.age_left }" if defined? @max_age
|
26
26
|
cookie << "Domain=#{@domain}" if defined? @domain
|
27
27
|
cookie << "Path=#{@path}" if defined? @path
|
@@ -32,7 +32,7 @@ class Cookie
|
|
32
32
|
|
33
33
|
def to_h
|
34
34
|
h = { name: @name, value: @value }
|
35
|
-
h[:expires] = @expires if defined? @
|
35
|
+
h[:expires] = @expires if defined?(@expires) and !defined?(@max_age)
|
36
36
|
h[:max_age] = @max_age if defined? @max_age
|
37
37
|
h[:domain] = @domain if defined? @domain
|
38
38
|
h[:path] = @path if defined? @path
|
@@ -48,12 +48,6 @@ class Cookie
|
|
48
48
|
class Parser
|
49
49
|
attr_reader :name, :value, :expires, :max_age, :domain, :path, :secure, :http_only
|
50
50
|
|
51
|
-
def self.empty?(val)
|
52
|
-
return true if val.nil?
|
53
|
-
return true if val.respond_to?(:size) && val.size == 0
|
54
|
-
return false
|
55
|
-
end
|
56
|
-
|
57
51
|
def initialize(options = {})
|
58
52
|
@time = options[:time]
|
59
53
|
@default_path = options[:default_path] || ''
|
@@ -61,7 +55,7 @@ class Cookie
|
|
61
55
|
|
62
56
|
def to_h
|
63
57
|
h = { name: @name, value: @value }
|
64
|
-
h[:expires] = @expires if defined? @
|
58
|
+
h[:expires] = @expires if defined?(@expires) and !defined?(@max_age)
|
65
59
|
h[:max_age] = @max_age if defined? @max_age
|
66
60
|
h[:domain] = @domain if defined? @domain
|
67
61
|
h[:path] = @path if defined? @path
|
@@ -93,12 +87,12 @@ class Cookie
|
|
93
87
|
end
|
94
88
|
|
95
89
|
def name_and_value!(chunk)
|
96
|
-
raise ParserError.new "Name cannot be blank" if
|
90
|
+
raise ParserError.new "Name cannot be blank" if chunk.nil? or chunk.empty?
|
97
91
|
raise ParserError.new "Name/value pair must include '='" if chunk.size == 1
|
98
92
|
@name = chunk[0]
|
99
93
|
@value = chunk[1]
|
100
94
|
|
101
|
-
raise ParserError.new "Name cannot be blank" if
|
95
|
+
raise ParserError.new "Name cannot be blank" if @name.empty?
|
102
96
|
end
|
103
97
|
|
104
98
|
def attribute!(chunk)
|
@@ -115,12 +109,12 @@ class Cookie
|
|
115
109
|
end
|
116
110
|
|
117
111
|
def expires!(val)
|
118
|
-
raise ParserError.new 'Expires cannot have a blank value' if
|
112
|
+
raise ParserError.new 'Expires cannot have a blank value' if val.empty?
|
119
113
|
@expires = val
|
120
114
|
end
|
121
115
|
|
122
116
|
def max_age!(val)
|
123
|
-
raise ParserError.new 'Max-Age cannot have a blank value' if
|
117
|
+
raise ParserError.new 'Max-Age cannot have a blank value' if val.empty?
|
124
118
|
|
125
119
|
unless val =~ /^-?\d+$/
|
126
120
|
raise ParserError.new "Expected integer for Max-Age instead of #{val}"
|
@@ -135,12 +129,12 @@ class Cookie
|
|
135
129
|
end
|
136
130
|
|
137
131
|
def domain!(val)
|
138
|
-
raise ParserError.new 'Domain cannot have a blank value' if
|
132
|
+
raise ParserError.new 'Domain cannot have a blank value' if val.empty?
|
139
133
|
@domain = (val[0] == '.' ? val[1..-1] : val).downcase
|
140
134
|
end
|
141
135
|
|
142
136
|
def path!(val)
|
143
|
-
if
|
137
|
+
if val.empty? || val[0] != '/'
|
144
138
|
@path = @default_path
|
145
139
|
else
|
146
140
|
@path = val
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parsed-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morgen Peschke
|
@@ -21,8 +21,10 @@ extensions: []
|
|
21
21
|
extra_rdoc_files: []
|
22
22
|
files:
|
23
23
|
- lib/parsed/base.rb
|
24
|
+
- lib/parsed/http/basicauth.rb
|
24
25
|
- lib/parsed/http/cookie.rb
|
25
26
|
- lib/parsed/json.rb
|
27
|
+
- lib/parsers/http/basicauth.rb
|
26
28
|
- lib/parsers/http/cookie.rb
|
27
29
|
homepage: https://github.com/morgen-peschke/parsed-attributes
|
28
30
|
licenses:
|