1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
//
// 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/.

#include <config.h>

#include <http/response_json.h>
#include <map><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::data;

namespace isc {
namespace http {

HttpResponseJson::HttpResponseJson()
    : HttpResponse() {
    context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json"));
}


HttpResponseJson::HttpResponseJson(const HttpVersion& version,
                                   const HttpStatusCode& status_code,
                                   const CallSetGenericBody& generic_body)
    : HttpResponse(version, status_code, CallSetGenericBody::no()) {
    context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json"));
    // This class provides its own implementation of the setGenericBody.
    // We call it here unless the derived class calls this constructor
    // from its own constructor and indicates that we shouldn't set the
    // generic content in the body.
    if (generic_body.set_) {
        setGenericBody(status_code);
    }
}

void
HttpResponseJson::setGenericBody(const HttpStatusCode& status_code) {
    // Only generate the content for the client or server errors. For
    // other status codes (status OK in particular) the content should
    // be created using setBodyAsJson or setBody.
    if (isClientError(status_code) || isServerError(status_code)) {
        std::map<std::string, ConstElementPtr> map_elements;
        map_elements["result"] =
            ConstElementPtr(new IntElement(statusCodeToNumber(status_code)));
        map_elements["text"] =
            ConstElementPtr(new StringElement(statusCodeToString(status_code)));
        auto body = Element::createMap();
        body->setValue(map_elements);
        setBodyAsJson(body);
    }
}

void
HttpResponseJson::finalize() {
    if (!created_) {
        create();
    }

    // Parse JSON body and store.
    parseBodyAsJson();
    finalized_ = true;
}

void
HttpResponseJson::reset() {
    HttpResponse::reset();
    json_.reset();
}

ConstElementPtr
HttpResponseJson::getBodyAsJson() const {
    checkFinalized();
    return (json_);
}

void
HttpResponseJson::setBodyAsJson(const ConstElementPtr& json_body) {
    if (json_body) {
        context()->body_ = json_body->str();

    } else {
        context()->body_.clear();
    }

    json_ = json_body;
}

ConstElementPtr
HttpResponseJson::getJsonElement(const std::string& element_name) const {<--- Derived function 'HttpResponseJson::getJsonElement'
    try {
        ConstElementPtr body = getBodyAsJson();
        if (body) {
            const std::map<std::string, ConstElementPtr>& map_value = body->mapValue();
            auto map_element = map_value.find(element_name);
            if (map_element != map_value.end()) {
                return (map_element->second);
            }
        }

    } catch (const std::exception& ex) {
        isc_throw(HttpResponseJsonError, "unable to get JSON element "
                  << element_name << ": " << ex.what());
    }
    return (ConstElementPtr());
}

void
HttpResponseJson::parseBodyAsJson() {
   try {
       // Only parse the body if it hasn't been parsed yet.
       if (!json_ && !context_->body_.empty()) {
           json_ = Element::fromJSON(context_->body_);
       }
    } catch (const std::exception& ex) {
        isc_throw(HttpResponseJsonError, "unable to parse the body of the HTTP"
                  " response: " << ex.what());
    }
}

} // namespace http
} // namespace isc