Parse a query like original `parse_qs` from `urlparse`, `urllib.parse`, but query given as a bytes argument. Arguments: qs: percent-encoded query bytes to be parsed keep_blank_values: flag indicating whether blank values in percent-encoded queries should be treated as blank by
(qs, keep_blank_values=False, strict_parsing=False, max_num_fields=None, separator=b'&')
| 221 | |
| 222 | |
| 223 | def parse_qsl_binary(qs, keep_blank_values=False, strict_parsing=False, max_num_fields=None, separator=b'&'): |
| 224 | """Parse a query like original `parse_qs` from `urlparse`, `urllib.parse`, but query given as a bytes argument. |
| 225 | |
| 226 | Arguments: |
| 227 | |
| 228 | qs: percent-encoded query bytes to be parsed |
| 229 | |
| 230 | keep_blank_values: flag indicating whether blank values in |
| 231 | percent-encoded queries should be treated as blank byte strings. |
| 232 | A true value indicates that blanks should be retained as blank |
| 233 | byte strings. The default false value indicates that blank values |
| 234 | are to be ignored and treated as if they were not included. |
| 235 | |
| 236 | strict_parsing: flag indicating what to do with parsing errors. If |
| 237 | false (the default), errors are silently ignored. If true, |
| 238 | errors raise a ValueError exception. |
| 239 | |
| 240 | max_num_fields: int. If set, then throws a ValueError |
| 241 | if there are more than n fields read by parse_qsl_binary(). |
| 242 | |
| 243 | separator: bytes. The symbol to use for separating the query arguments. |
| 244 | Defaults to &. |
| 245 | |
| 246 | Returns a list. |
| 247 | """ |
| 248 | |
| 249 | if max_num_fields is not None: |
| 250 | num_fields = 1 + qs.count(separator) if qs else 0 |
| 251 | if max_num_fields < num_fields: |
| 252 | raise ValueError('Max number of fields exceeded') |
| 253 | |
| 254 | r = [] |
| 255 | query_args = qs.split(separator) if qs else [] |
| 256 | for name_value in query_args: |
| 257 | if not name_value and not strict_parsing: |
| 258 | continue |
| 259 | nv = name_value.split(b'=', 1) |
| 260 | |
| 261 | if len(nv) != 2: |
| 262 | if strict_parsing: |
| 263 | raise ValueError("bad query field: %r" % (name_value,)) |
| 264 | # Handle case of a control-name with no equal sign |
| 265 | if keep_blank_values: |
| 266 | nv.append(b'') |
| 267 | else: |
| 268 | continue |
| 269 | if len(nv[1]) or keep_blank_values: |
| 270 | name = nv[0].replace(b'+', b' ') |
| 271 | name = unquote_binary(name) |
| 272 | value = nv[1].replace(b'+', b' ') |
| 273 | value = unquote_binary(value) |
| 274 | r.append((name, value)) |
| 275 | return r |
| 276 | |
| 277 | |
| 278 | def unquote_binary(string): |
no test coverage detected