• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "rest/response.hpp"
30 
31 #include <stdio.h>
32 
33 #define OT_REST_RESPONSE_CONTENT_TYPE_JSON "application/json"
34 #define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_ORIGIN "*"
35 #define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_HEADERS                                                              \
36     "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, " \
37     "Access-Control-Request-Headers"
38 #define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_METHOD "GET"
39 
40 namespace otbr {
41 namespace rest {
42 
Response(void)43 Response::Response(void)
44     : mCallback(false)
45     , mComplete(false)
46 {
47     // HTTP protocol
48     mProtocol = "HTTP/1.1 ";
49 
50     // Pre-defined headers
51     mHeaderField.push_back("Content-Type");
52     mHeaderValue.push_back(OT_REST_RESPONSE_CONTENT_TYPE_JSON);
53 
54     mHeaderField.push_back("Access-Control-Allow-Origin");
55     mHeaderValue.push_back(OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_ORIGIN);
56 
57     mHeaderField.push_back("Access-Control-Allow-Methods");
58     mHeaderValue.push_back(OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_METHOD);
59 
60     mHeaderField.push_back("Access-Control-Allow-Headers");
61     mHeaderValue.push_back(OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_HEADERS);
62 }
63 
SetComplete()64 void Response::SetComplete()
65 {
66     mComplete = true;
67 }
68 
SetStartTime(steady_clock::time_point aStartTime)69 void Response::SetStartTime(steady_clock::time_point aStartTime)
70 {
71     mStartTime = aStartTime;
72 }
73 
GetStartTime() const74 steady_clock::time_point Response::GetStartTime() const
75 {
76     return mStartTime;
77 }
78 
IsComplete()79 bool Response::IsComplete()
80 {
81     return mComplete == true;
82 }
83 
SetResponsCode(std::string & aCode)84 void Response::SetResponsCode(std::string &aCode)
85 {
86     mCode = aCode;
87 }
88 
SetCallback(void)89 void Response::SetCallback(void)
90 {
91     mCallback = true;
92 }
93 
SetBody(std::string & aBody)94 void Response::SetBody(std::string &aBody)
95 {
96     mBody = aBody;
97 }
98 
GetBody(void) const99 std::string Response::GetBody(void) const
100 {
101     return mBody;
102 }
103 
NeedCallback(void)104 bool Response::NeedCallback(void)
105 {
106     return mCallback;
107 }
108 
Serialize(void) const109 std::string Response::Serialize(void) const
110 {
111     size_t      index;
112     std::string spacer = "\r\n";
113     std::string ret(mProtocol + " " + mCode);
114 
115     for (index = 0; index < mHeaderField.size(); index++)
116     {
117         ret += (spacer + mHeaderField[index] + ": " + mHeaderValue[index]);
118     }
119     ret += spacer + "Content-Length: " + std::to_string(mBody.size());
120     ret += (spacer + spacer + mBody);
121 
122     return ret;
123 }
124 
125 } // namespace rest
126 } // namespace otbr
127