Module: Qeweney::RequestInfoClassMethods
- Included in:
- Request
- Defined in:
- lib/qeweney/request_info.rb
Constant Summary collapse
- PARAMETER_RE =
/^(.+)=(.*)$/.freeze
- MAX_PARAMETER_NAME_SIZE =
256
- MAX_PARAMETER_VALUE_SIZE =
1MB
2**20
Instance Method Summary collapse
- #parse_form_data(body, headers) ⇒ Object
- #parse_multipart_form_data(body, boundary) ⇒ Object
- #parse_multipart_form_data_part(part, hash) ⇒ Object
- #parse_multipart_form_data_part_headers(part) ⇒ Object
- #parse_urlencoded_form_data(body) ⇒ Object
Instance Method Details
#parse_form_data(body, headers) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/qeweney/request_info.rb', line 121 def parse_form_data(body, headers) case (content_type = headers['content-type']) when /^multipart\/form\-data; boundary=([^\s]+)/ boundary = "--#{Regexp.last_match(1)}" parse_multipart_form_data(body, boundary) when /^application\/x-www-form-urlencoded/ parse_urlencoded_form_data(body) else raise "Unsupported form data content type: #{content_type}" end end |
#parse_multipart_form_data(body, boundary) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/qeweney/request_info.rb', line 133 def parse_multipart_form_data(body, boundary) parts = body.split(boundary) parts.each_with_object({}) do |p, h| next if p.empty? || p == "--\r\n" # remove post-boundary \r\n p.slice!(0, 2) parse_multipart_form_data_part(p, h) end end |
#parse_multipart_form_data_part(part, hash) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/qeweney/request_info.rb', line 144 def parse_multipart_form_data_part(part, hash) body, headers = parse_multipart_form_data_part_headers(part) disposition = headers['content-disposition'] || '' name = (disposition =~ /name="([^"]+)"/) ? Regexp.last_match(1) : nil filename = (disposition =~ /filename="([^"]+)"/) ? Regexp.last_match(1) : nil if filename hash[name] = { filename: filename, content_type: headers['content-type'], data: body } else hash[name] = body end end |
#parse_multipart_form_data_part_headers(part) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/qeweney/request_info.rb', line 158 def parse_multipart_form_data_part_headers(part) headers = {} while true idx = part.index("\r\n") break unless idx header = part[0, idx] part.slice!(0, idx + 2) break if header.empty? next unless header =~ /^([^\:]+)\:\s?(.+)$/ headers[Regexp.last_match(1).downcase] = Regexp.last_match(2) end # remove trailing \r\n part.slice!(part.size - 2, 2) [part, headers] end |
#parse_urlencoded_form_data(body) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/qeweney/request_info.rb', line 181 def parse_urlencoded_form_data(body) return {} unless body body.force_encoding(Encoding::UTF_8) unless body.encoding == Encoding::UTF_8 body.split('&').each_with_object({}) do |i, m| raise 'Invalid parameter format' unless i =~ PARAMETER_RE k = Regexp.last_match(1) raise 'Invalid parameter size' if k.size > MAX_PARAMETER_NAME_SIZE v = Regexp.last_match(2) raise 'Invalid parameter size' if v.size > MAX_PARAMETER_VALUE_SIZE m[EscapeUtils.unescape_uri(k)] = EscapeUtils.unescape_uri(v) end end |