OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bfd_rest_common.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Codilime.
3  */
4 
6 
7 #include <boost/lexical_cast.hpp>
8 #include <rapidjson/document.h>
9 #include <rapidjson/stringbuffer.h>
10 #include <rapidjson/writer.h>
11 
12 #include "http/http_session.h"
13 
14 using contrail_rapidjson::Document;
15 using contrail_rapidjson::StringBuffer;
16 using contrail_rapidjson::Value;
17 using contrail_rapidjson::Writer;
18 
19 
20 namespace BFD {
21 namespace REST {
22 
23 void SendResponse(HttpSession *session,
24  const std::string &msg, int status_code) {
25  const std::string response =
26  "HTTP/1.1 " + boost::lexical_cast<std::string>(status_code) + " OK\r\n"
27  "Content-Type: application/json; charset=UTF-8\r\n"
28  "Content-Length: " +
29  boost::lexical_cast<std::string>(msg.length()) + "\r\n" "\r\n" + msg;
30  session->Send((const u_int8_t *)(response.c_str()),
31  response.length(), NULL);
32 }
33 
34 void SendErrorResponse(HttpSession *session, const std::string &error_msg,
35  int status_code) {
36  Document document;
37  document.SetObject();
38  Document::AllocatorType &a = document.GetAllocator();
39  Value v;
40  document.AddMember("error", v.SetString(error_msg.c_str(), a), a);
41  StringBuffer strbuf;
42  Writer<StringBuffer> writer(strbuf);
43  document.Accept(writer);
44  SendResponse(session, strbuf.GetString(), status_code);
45 }
46 
47 } // namespace REST
48 } // namespace BFD
void SendResponse(HttpSession *session, const std::string &msg, int status_code)
virtual bool Send(const uint8_t *data, size_t size, size_t *sent)
Definition: tcp_session.cc:428
void SendErrorResponse(HttpSession *session, const std::string &error_msg, int status_code)