Skip to content

Commit

Permalink
Fix validating multipart\/form-data with boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Jun 17, 2021
1 parent 38335ec commit f9c2013
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Revision history for perl distribution JSON-Validator

4.18 Not Released
- Fix content_type validation for OpenAPIv2 and OpenAPIv3
- Fix validating multipart\/form-data with boundary

4.17 2021-04-28T11:30:56+0900
- Add add_default_response() to OpenAPIv2 and OpenAPIv3
Expand Down
9 changes: 6 additions & 3 deletions lib/JSON/Validator/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,16 @@ sub negotiate_content_type {
my ($accepts, $header) = @_;
return '' unless $header;

my %header_map;
/^\s*([^,; ]+)(?:\s*\;\s*q\s*=\s*(\d+(?:\.\d+)?))?\s*$/i and $header_map{lc $1} = $2 // 1 for split /,/, $header;
my %header_map = map {
/^\s*([^,; ]+)(?:\s*\;\s*q\s*=\s*(\d+(?:\.\d+)?))?\s*$/i ? (lc $1, $2)
: /^\s*([^,; ]+)(?:\s*\;\s*\w+\s*=\S+)?\s*$/i ? (lc $1, -1)
: (lc $_, -2);
} split /,/, $header;
my @headers = sort { $header_map{$b} <=> $header_map{$a} } sort keys %header_map;

# Check for exact match
for my $ct (@$accepts) {
return $ct if $header_map{$ct};
return $ct if exists $header_map{$ct};
}

# Check for closest match
Expand Down
4 changes: 3 additions & 1 deletion t/util.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ is_deeply(
);

is negotiate_content_type([]), '', 'accepts nothing';
is negotiate_content_type(['multipart/form-data'], 'multipart/form-data; boundary=mgkBX'), 'multipart/form-data',
'form-data boundary';
is negotiate_content_type(['application/json']), '', 'header missing';
is negotiate_content_type(['application/json', 'text/plain'], 'application/json'), 'application/json', 'exact match';
is negotiate_content_type(['application/json', 'text/*'], 'text/plain'), 'text/*', 'closest accept';
is negotiate_content_type(['application/json', 'text/*'], 'text/plain'), 'text/*', 'closest accept';
is negotiate_content_type(
['text/plain', 'application/xml'],
'text/html;text/plain;q=0.2,application/xml;q=0.9,*/*;q=0.8'
Expand Down

0 comments on commit f9c2013

Please sign in to comment.