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