Line data Source code
1 : /* 2 : * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include <rapidjson/document.h> 6 : #include <rapidjson/stringbuffer.h> 7 : #include <rapidjson/writer.h> 8 : 9 : #include <http/http_session.h> 10 : #include <base/string_util.h> 11 : #include "rest_common.h" 12 : 13 : namespace REST { 14 : 15 0 : void SendResponse(HttpSession *session, 16 : const std::string &msg, int status_code, std::string context) { 17 : const std::string response = 18 0 : "HTTP/1.1 " + integerToString(status_code) + " OK\r\n" 19 : "Content-Type: application/json; charset=UTF-8\r\n" 20 0 : "Content-Length: " + 21 0 : integerToString(msg.length()) + "\r\n" "\r\n" + msg; 22 0 : if (context.empty()) { 23 0 : session->Send((const uint8_t *)(response.c_str()), 24 : response.length(), NULL); 25 : } else { 26 0 : session->SendSession(context, (const uint8_t *)(response.c_str()), 27 : response.length(), NULL); 28 : } 29 0 : } 30 : 31 0 : void SendErrorResponse(HttpSession *session, 32 : const std::string &error_msg, int status_code, std::string context) { 33 0 : contrail_rapidjson::Document document; 34 0 : document.SetObject(); 35 0 : contrail_rapidjson::Value v; 36 0 : document.AddMember("error", 37 : v.SetString(error_msg.c_str(), document.GetAllocator()), 38 : document.GetAllocator()); 39 0 : contrail_rapidjson::StringBuffer strbuf; 40 0 : contrail_rapidjson::Writer<contrail_rapidjson::StringBuffer> writer(strbuf); 41 0 : document.Accept(writer); 42 0 : SendResponse(session, strbuf.GetString(), status_code, context); 43 0 : } 44 : 45 : } // namespace REST