xref: /aosp_15_r20/external/ot-br-posix/src/rest/response.cpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
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_ACCESS_CONTROL_ALLOW_ORIGIN "*"
34 #define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_HEADERS                                                              \
35     "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, " \
36     "Access-Control-Request-Headers"
37 #define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_METHOD "DELETE, GET, OPTIONS, PUT"
38 #define OT_REST_RESPONSE_CONNECTION "close"
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     mHeaders[OT_REST_CONTENT_TYPE_HEADER]    = OT_REST_CONTENT_TYPE_JSON;
52     mHeaders["Access-Control-Allow-Origin"]  = OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_ORIGIN;
53     mHeaders["Access-Control-Allow-Methods"] = OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_METHOD;
54     mHeaders["Access-Control-Allow-Headers"] = OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_HEADERS;
55     mHeaders["Connection"]                   = OT_REST_RESPONSE_CONNECTION;
56 }
57 
SetComplete()58 void Response::SetComplete()
59 {
60     mComplete = true;
61 }
62 
SetStartTime(steady_clock::time_point aStartTime)63 void Response::SetStartTime(steady_clock::time_point aStartTime)
64 {
65     mStartTime = aStartTime;
66 }
67 
GetStartTime() const68 steady_clock::time_point Response::GetStartTime() const
69 {
70     return mStartTime;
71 }
72 
IsComplete()73 bool Response::IsComplete()
74 {
75     return mComplete == true;
76 }
77 
SetResponsCode(std::string & aCode)78 void Response::SetResponsCode(std::string &aCode)
79 {
80     mCode = aCode;
81 }
82 
SetContentType(const std::string & aContentType)83 void Response::SetContentType(const std::string &aContentType)
84 {
85     mHeaders[OT_REST_CONTENT_TYPE_HEADER] = aContentType;
86 }
87 
SetCallback(void)88 void Response::SetCallback(void)
89 {
90     mCallback = true;
91 }
92 
SetBody(std::string & aBody)93 void Response::SetBody(std::string &aBody)
94 {
95     mBody = aBody;
96 }
97 
GetBody(void) const98 std::string Response::GetBody(void) const
99 {
100     return mBody;
101 }
102 
NeedCallback(void)103 bool Response::NeedCallback(void)
104 {
105     return mCallback;
106 }
107 
Serialize(void) const108 std::string Response::Serialize(void) const
109 {
110     std::string spacer = "\r\n";
111     std::string ret(mProtocol + " " + mCode);
112 
113     for (const auto &header : mHeaders)
114     {
115         ret += (spacer + header.first + ": " + header.second);
116     }
117     ret += spacer + "Content-Length: " + std::to_string(mBody.size());
118     ret += (spacer + spacer + mBody);
119 
120     return ret;
121 }
122 
123 } // namespace rest
124 } // namespace otbr
125