Module: GRHttp::HTTP

Included in:
Base::HTTPHandler
Defined in:
lib/grhttp/http_helpers.rb

Overview

These are some HTTP helper methods supplied for your convinience and the Generic HTTP server's use.

Class Method Summary collapse

Class Method Details

.add_param_to_hash(name, value, target) ⇒ Object

Adds paramaters to a Hash object, according to the GRHttp's server conventions.


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/grhttp/http_helpers.rb', line 108

def self.add_param_to_hash name, value, target
	begin
		c = target
		val = rubyfy! value
		a = name.chomp('[]').split('[')

		a[0...-1].inject(target) do |h, n|
			n.chomp!(']');
			n.strip!;
			raise "malformed parameter name for #{name}" if n.empty?
			n = (n.to_i.to_s == n) ?  n.to_i : n.to_sym            
			c = (h[n] ||= {})
		end
		n = a.last
		n.chomp!(']'); n.strip!;
		n = n.empty? ? nil : ( (n.to_i.to_s == n) ?  n.to_i : n.to_sym )
		if n
			if c[n]
				c[n].is_a?(Array) ? (c[n] << val) : (c[n] = [c[n], val])
			else
				c[n] = val
			end
		else
			if c[n]
				c[n].is_a?(Array) ? (c[n] << val) : (c[n] = [c[n], val])
			else
				c[n] = [val]
			end
		end
		val
	rescue => e
		GReactor.error e
		GReactor.error "(Silent): parameters parse error for #{name} ... maybe conflicts with a different set?"
		target[name] = val
	end
	# begin
	# 	h = target
	# 	val = rubyfy! value
	# 	a = name.chomp('[]').split('[')
	# 	len = a.count - 1
	# 	p = nil
	# 	a.each.with_index do |n, i|
	# 		n.chomp!(']');
	# 		n.strip!;
	# 		n = n.empty? ? nil : (n.to_i.to_s == n) ?  n.to_i : n.to_sym
	# 		if i == len
	# 			if n
	# 				if h[n]
	# 					h[n].is_a?(Array) ? (h[n] << val) : (h[n] = [h[n], val])
	# 				else
	# 					h[n] = val
	# 				end
	# 			else
	# 				h = (h[n] ||= [])
	# 				h << val
	# 			end
	# 		else
	# 			h = ( h[n] ||= {} )
	# 		end
	# 	end
	# 	val
	# rescue => e
	# 	GReactor.error e
	# 	GReactor.error "(Silent): parameters parse error for #{name} ... maybe conflicts with a different set?"
	# 	target[name] = val
	# end
end

.decode(object, decode_method = :form) ⇒ Object

Decodes data from :form, :html and :url (default) escaped strings.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grhttp/http_helpers.rb', line 33

def self.decode object, decode_method = :form
	if object.is_a?(Hash)
		object.values.each {|v| decode v, decode_method}
	elsif object.is_a?(Array)
		object.each {|v| decode v, decode_method}
	elsif object.is_a?(String)
		case decode_method
		when :form
			object.gsub!('+', '%20')
			object.gsub!(/\%[0-9a-fA-F][0-9a-fA-F]/) {|m| m[1..2].to_i(16).chr}
		when :uri, :url
			object.gsub!(/\%[0-9a-fA-F][0-9a-fA-F]/) {|m| m[1..2].to_i(16).chr}
		when :html
			object.gsub!(/&amp;/i, '&')
			object.gsub!(/&quot;/i, '"')
			object.gsub!(/&gt;/i, '>')
			object.gsub!(/&lt;/i, '<')
		when :utf8

		else

		end
		object.gsub!(/&#([0-9a-fA-F]{2});/) {|m| m.match(/[0-9a-fA-F]{2}/)[0].hex.chr}
		object.gsub!(/&#([0-9]{4});/) {|m| [m.match(/[0-9]+/)[0].to_i].pack 'U'}
		make_utf8! object
		return object
	elsif object.is_a?(Symbol)
		str = object.to_str
		decode str, decode_method
		return str.to_sym
	else
		raise "GReactor Raising Hell (don't misuse us)!"
	end
end

.encode(object, decode_method = :form) ⇒ Object

Encodes data for :html, :url and :form (default) escaped strings.


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/grhttp/http_helpers.rb', line 68

def self.encode object, decode_method = :form
	if object.is_a?(Hash)
		object.values.each {|v| encode v, decode_method}
	elsif object.is_a?(Array)
		object.each {|v| encode v, decode_method}
	elsif object.is_a?(String)
		case decode_method
		when :uri, :url, :form
			object.force_encoding ::Encoding::ASCII_8BIT
			object.gsub!(/[^a-zA-Z0-9\*\.\_\-]/) {|m| '%%%02x' % m.ord }
		when :html
			object.gsub!('&', '&amp;')
			object.gsub!('"', '&quot;')
			object.gsub!('>', '&gt;')
			object.gsub!('<', '&lt;')
			object.gsub!(/[^\sa-zA-Z\d\&\;]/) {|m| '&#%04d;' % m.unpack('U')[0] }
			# object.gsub!(/[^\s]/) {|m| "&#%04d;" % m.unpack('U')[0] }
			object.force_encoding ::Encoding::ASCII_8BIT
		when :utf8
			object.gsub!(/[^\sa-zA-Z\d]/) {|m| '&#%04d;' % m.unpack('U')[0] }
			object.force_encoding ::Encoding::ASCII_8BIT
		else

		end
		return object
	elsif object.is_a?(Symbol)
		str = object.to_str
		encode str, decode_method
		return str.to_sym
	else
		raise "GReactor Raising Hell (don't misuse us)!"
	end
end

.encode_url(str) ⇒ Object


102
103
104
105
# File 'lib/grhttp/http_helpers.rb', line 102

def self.encode_url str
	str.to_s.dup.force_encoding(::Encoding::ASCII_8BIT).gsub(/[^a-z0-9\*\.\_\-]/i) {|m| '%%%02x' % m.ord }
	# str.to_s.b.gsub(/[^a-z0-9\*\.\_\-]/i) {|m| '%%%02x' % m.ord }
end

.escape(string) ⇒ Object

Escapes html. based on the WEBRick source code, escapes &, “, > and < in a String object


24
25
26
27
28
29
30
# File 'lib/grhttp/http_helpers.rb', line 24

def self.escape(string)
	# string.gsub('&', '&amp;')
	# .gsub('"', '&quot;')
	# .gsub('>', '&gt;')
	# .gsub('<', '&lt;')
	CGI.escapeHTML(string.to_s)
end

.make_utf8!(string, encoding = ::Encoding::UTF_8) ⇒ Object

re-encodes a string into UTF-8


10
11
12
13
14
# File 'lib/grhttp/http_helpers.rb', line 10

def self.make_utf8!(string, encoding= ::Encoding::UTF_8)
	return false unless string
	string.force_encoding(::Encoding::ASCII_8BIT).encode!(encoding, ::Encoding::ASCII_8BIT, invalid: :replace, undef: :replace, replace: '') unless string.force_encoding(encoding).valid_encoding?
	string
end

.try_utf8!(string, encoding = ::Encoding::UTF_8) ⇒ Object

re-encodes a string into UTF-8


17
18
19
20
21
# File 'lib/grhttp/http_helpers.rb', line 17

def self.try_utf8!(string, encoding= ::Encoding::UTF_8)
	return false unless string
	string.force_encoding(::Encoding::ASCII_8BIT) unless string.force_encoding(encoding).valid_encoding?
	string
end