From 6c5fcdfbe3594b7311ab0bea417fc68f2eb9337d Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 14 Oct 2018 22:49:10 +0200 Subject: Revert "Bug 1495741 - memory issues: Avoid copying stuff in the webservice layer so much" Fix an "Unexpected Error" message when viewing a bug. This reverts commit 5688d0e712b85bc892ce405a1b79e3571f6d6d0f. Signed-off-by: Florian Pritz --- Bugzilla/WebService/JSON.pm | 64 ----------------------------------- Bugzilla/WebService/JSON/Box.pm | 43 ----------------------- Bugzilla/WebService/Server/JSONRPC.pm | 16 ++------- Bugzilla/WebService/Server/REST.pm | 5 ++- 4 files changed, 5 insertions(+), 123 deletions(-) delete mode 100644 Bugzilla/WebService/JSON.pm delete mode 100644 Bugzilla/WebService/JSON/Box.pm (limited to 'Bugzilla/WebService') diff --git a/Bugzilla/WebService/JSON.pm b/Bugzilla/WebService/JSON.pm deleted file mode 100644 index f670d1fd9..000000000 --- a/Bugzilla/WebService/JSON.pm +++ /dev/null @@ -1,64 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# This Source Code Form is "Incompatible With Secondary Licenses", as -# defined by the Mozilla Public License, v. 2.0. - -package Bugzilla::WebService::JSON; -use 5.10.1; -use Moo; - -use Bugzilla::Logging; -use Bugzilla::WebService::JSON::Box; -use JSON::MaybeXS; -use Scalar::Util qw(refaddr blessed); -use Package::Stash; - -use constant Box => 'Bugzilla::WebService::JSON::Box'; - -has 'json' => ( - init_arg => undef, - is => 'lazy', - handles => {_encode => 'encode', _decode => 'decode'}, -); - -sub encode { - my ($self, $value) = @_; - return Box->new(json => $self, value => $value); -} - -sub decode { - my ($self, $box) = @_; - - if (blessed($box) && $box->isa(Box)) { - return $box->value; - } - else { - return $self->_decode($box); - } -} - -sub _build_json { JSON::MaybeXS->new } - -# delegation all the json options to the real json encoder. -{ - my @json_methods = qw( - utf8 ascii pretty canonical - allow_nonref allow_blessed convert_blessed - ); - my $stash = Package::Stash->new(__PACKAGE__); - foreach my $method (@json_methods) { - my $symbol = '&' . $method; - $stash->add_symbol( - $symbol => sub { - my $self = shift; - $self->json->$method(@_); - return $self; - } - ); - } -} - - -1; diff --git a/Bugzilla/WebService/JSON/Box.pm b/Bugzilla/WebService/JSON/Box.pm deleted file mode 100644 index fc39aeee8..000000000 --- a/Bugzilla/WebService/JSON/Box.pm +++ /dev/null @@ -1,43 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# This Source Code Form is "Incompatible With Secondary Licenses", as -# defined by the Mozilla Public License, v. 2.0. - -package Bugzilla::WebService::JSON::Box; -use 5.10.1; -use Moo; - -use overload '${}' => 'value', '""' => 'to_string', fallback => 1; - -has 'value' => (is => 'ro', required => 1); -has 'json' => (is => 'ro', required => 1); -has 'label' => (is => 'lazy'); -has 'encode' => (init_arg => undef, is => 'lazy', predicate => 'is_encoded'); - -sub TO_JSON { - my ($self) = @_; - - return $self->to_string; -} - -sub to_string { - my ($self) = @_; - - return $self->is_encoded ? $self->encode : $self->label; -} - -sub _build_encode { - my ($self) = @_; - - return $self->json->_encode($self->value); -} - -sub _build_label { - my ($self) = @_; - - return "" . $self->value; -} - -1; diff --git a/Bugzilla/WebService/Server/JSONRPC.pm b/Bugzilla/WebService/Server/JSONRPC.pm index ef00737ad..c9c11e0d4 100644 --- a/Bugzilla/WebService/Server/JSONRPC.pm +++ b/Bugzilla/WebService/Server/JSONRPC.pm @@ -32,9 +32,7 @@ use Bugzilla::Util; use HTTP::Message; use MIME::Base64 qw(decode_base64 encode_base64); -use Scalar::Util qw(blessed); use List::MoreUtils qw(none); -use Bugzilla::WebService::JSON; ##################################### # Public JSON::RPC Method Overrides # @@ -51,7 +49,7 @@ sub new { sub create_json_coder { my $self = shift; - my $json = Bugzilla::WebService::JSON->new; + my $json = $self->SUPER::create_json_coder(@_); $json->allow_blessed(1); $json->convert_blessed(1); $json->allow_nonref(1); @@ -89,9 +87,6 @@ sub response { # Implement JSONP. if (my $callback = $self->_bz_callback) { my $content = $response->content; - if (blessed $content) { - $content = $content->encode; - } # Prepend the JSONP response with /**/ in order to protect # against possible encoding attacks (e.g., affecting Flash). @@ -120,12 +115,7 @@ sub response { else { push(@header_args, "-ETag", $etag) if $etag; print $cgi->header(-status => $response->code, @header_args); - my $content = $response->content; - if (blessed $content) { - $content = $content->encode; - utf8::encode($content); - } - print $content; + print $response->content; } } @@ -375,7 +365,7 @@ sub _argument_type_check { # Now, convert dateTime fields on input. $self->_bz_method_name =~ /^(\S+)\.(\S+)$/; my ($class, $method) = ($1, $2); - my $pkg = $self->{dispatch_path}->{$class}; + my $pkg = $self->{dispatch_path}->{$class}; my @date_fields = @{$pkg->DATE_FIELDS->{$method} || []}; foreach my $field (@date_fields) { if (defined $params->{$field}) { diff --git a/Bugzilla/WebService/Server/REST.pm b/Bugzilla/WebService/Server/REST.pm index 781960c68..5f0839734 100644 --- a/Bugzilla/WebService/Server/REST.pm +++ b/Bugzilla/WebService/Server/REST.pm @@ -54,7 +54,7 @@ sub handle { # unless we were looking for OPTIONS if (!$self->_find_resource($self->cgi->path_info)) { if ($self->request->method eq 'OPTIONS' && $self->bz_rest_options) { - my $response = $self->response_header(STATUS_OK, ""); + my $response = $self->response_header(STATUS_OK, ""); my $options_string = join(', ', @{$self->bz_rest_options}); $response->header( 'Allow' => $options_string, @@ -165,7 +165,6 @@ sub response { my $template = Bugzilla->template; $content = ""; - $result->encode if blessed $result; $template->process("rest.html.tmpl", {result => $result}, \$content) || ThrowTemplateError($template->error()); @@ -508,7 +507,7 @@ sub _get_content_prefs { # Parse the Accept header, and save type name, score, and position. my @accept_types = split /,/, $self->cgi->http('accept') || ''; - my $order = 0; + my $order = 0; for my $accept_type (@accept_types) { my ($weight) = ($accept_type =~ /q=(\d\.\d+|\d+)/); my ($name) = ($accept_type =~ m#(\S+/[^;]+)#); -- cgit v1.2.3-24-g4f1b