Module: QNA::RequestInfoClassMethods
- Included in:
- Request
- Defined in:
- lib/qna/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
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/qna/request_info.rb', line 67 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
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/qna/request_info.rb', line 79 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
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/qna/request_info.rb', line 90 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/qna/request_info.rb', line 104 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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/qna/request_info.rb', line 127 def parse_urlencoded_form_data(body) body.force_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 |