Skip to content

Commit

Permalink
Merge pull request #700 from hplato/master
Browse files Browse the repository at this point in the history
add in option to compress json
  • Loading branch information
hplato authored Jun 29, 2017
2 parents 9131261 + c60c09c commit 6914098
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/json_server.pl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ sub json_put {

# Translate special characters
$json_raw = $json_raw->pretty->encode( \%json );
return &json_page($json_raw);
my $options = "";
$options = "compress" if ($Http{'Accept-Encoding'} =~ m/gzip/);
return &json_page($json_raw,$options);
}

# Handles Get (READ) Requests
Expand Down Expand Up @@ -810,7 +812,9 @@ sub json_get {
# Translate special characters
$json_raw->canonical(1); #Order the data so that objects show alphabetically
$json_raw = $json_raw->pretty->encode( \%json );
return &json_page($json_raw);
my $options = "";
$options = "compress" if ($Http{'Accept-Encoding'} =~ m/gzip/);
return &json_page($json_raw,$options);

}

Expand Down Expand Up @@ -1146,18 +1150,23 @@ sub filter_object {
}

sub json_page {
my ($json_raw) = @_;
my $json;
my ($json_raw,$options) = @_;

## utf8::encode( $json_raw ); #may need to wrap gzip in an eval and encode it if errors develop. It crashes if a < is in the text
gzip \$json_raw => \$json;
my $output = "HTTP/1.0 200 OK\r\n";
$output .= "Server: MisterHouse\r\n";
$output .= "Content-type: application/json\r\n";
$output .= "Content-Encoding: gzip\r\n";
$output .= "\r\n";
$output .= $json;
## $output .= $json_raw;
if ($options =~ m/compress/) {
print_log("json_server.pl: DEBUG: Compressing Data as requested by client") if $Debug{json};
my $json;
gzip \$json_raw => \$json;
$output .= "Content-Encoding: gzip\r\n";
$output .= "\r\n";
$output .= $json;
} else {
$output .= "\r\n";
$output .= $json_raw;
}

return $output;
}
Expand Down

0 comments on commit 6914098

Please sign in to comment.