Module: Anorexic::HTTP

Defined in:
lib/anorexic/server/helpers/http.rb

Overview

includes general helper methods for HTTP protocol and related (url encoding etc')

Class Method Summary collapse

Class Method Details

.add_param_to_hash(param_name, param_value, target_hash) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/anorexic/server/helpers/http.rb', line 74

def add_param_to_hash param_name, param_value, target_hash
	begin
		a = target_hash
		p = param_name.gsub(']',' ').split(/\[/)
		p.each_index { |i| n = p[i].strip.to_sym; p[i+1] ? [ ( a[n] ||= ( p[i+1] == ' ' ? [] : {} ) ), ( a = a[n]) ] : (a.is_a?(Hash) ? [(a[n]? (a[n].is_a?(String)? (a[n] = [a[n]]) : true) : a[n]=''), (a=a[n])] : [(a << ''), (a = a.last)]) }
		a << param_value
	rescue Exception => e
		Anorexic.error e
		Anorexic.error "(Silent): paramaters parse error for #{param_name} ... maybe conflicts with a different set?"
		target_hash[param_name] = make_utf8! param_value
	end
end

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


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/anorexic/server/helpers/http.rb', line 87

def 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 "Anorexic Raising Hell (don't misuse us)!"
	end
end

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


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
# File 'lib/anorexic/server/helpers/http.rb', line 121

def 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.gsub!(/[^a-zA-Z0-9\*\.\_\-]/) {|m| m.ord <= 16 ? "%0#{m.ord.to_s(16)}" : "%#{m.ord.to_s(16)}"}
		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] }
		when :utf8
			object.gsub!(/[^\sa-zA-Z\d]/) {|m| "&#%04d;" % m.unpack('U')[0] }
		else

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

.escape(string) ⇒ Object

Based on the WEBRick source code, escapes &, “, > and < in a String object


68
69
70
71
72
73
# File 'lib/anorexic/server/helpers/http.rb', line 68

def escape(string)
	string.gsub(/&/n, '&amp;')
	.gsub(/\"/n, '&quot;')
	.gsub(/>/n, '&gt;')
	.gsub(/</n, '&lt;')
end

.extract_data(data, target_hash, decode = :form) ⇒ Object

extracts parameters from the query


152
153
154
155
156
157
158
# File 'lib/anorexic/server/helpers/http.rb', line 152

def extract_data data, target_hash, decode = :form
	data.each do |set|
		list = set.split('=')
		list.each {|s| HTTP.decode s, decode if s}
		add_param_to_hash list.shift, list.join('='), target_hash
	end
end

.make_utf8!(string, encoding = 'utf-8') ⇒ Object

re-encodes a string into UTF-8


161
162
163
164
165
# File 'lib/anorexic/server/helpers/http.rb', line 161

def make_utf8!(string, encoding= 'utf-8')
	return false unless string
	string.force_encoding("binary").encode!(encoding, "binary", invalid: :replace, undef: :replace, replace: '') unless string.force_encoding(encoding).valid_encoding?
	string
end