• 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 /**
30  * @file
31  *   This file includes response definition for RESTful HTTP server.
32  */
33 
34 #ifndef OTBR_REST_RESPONSE_HPP_
35 #define OTBR_REST_RESPONSE_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #include <chrono>
40 #include <map>
41 #include <string>
42 
43 #include "rest/types.hpp"
44 
45 using std::chrono::duration_cast;
46 using std::chrono::microseconds;
47 using std::chrono::seconds;
48 using std::chrono::steady_clock;
49 
50 namespace otbr {
51 namespace rest {
52 
53 /**
54  * This class implements a response class for OTBR_REST, it could be manipulated by connection instance and resource
55  * handler.
56  *
57  */
58 class Response
59 {
60 public:
61     /**
62      * The constructor to initialize a response instance.
63      *
64      *
65      */
66     Response(void);
67 
68     /**
69      * This method set the response body.
70      *
71      * @param[in] aBody  A string to be set as response body.
72      *
73      */
74     void SetBody(std::string &aBody);
75 
76     /**
77      * This method return a string contains the body field of this response.
78      *
79      * @returns A string containing the body field.
80      */
81     std::string GetBody(void) const;
82 
83     /**
84      * This method set the response code.
85      *
86      * @param[in] aCode  A string representing response code such as "404 not found".
87      *
88      */
89     void SetResponsCode(std::string &aCode);
90 
91     /**
92      * This method sets the content type.
93      *
94      * @param[in] aCode  A string representing response content type such as text/plain.
95      *
96      */
97     void SetContentType(const std::string &aContentType);
98 
99     /**
100      * This method labels the response as need callback.
101      *
102      *
103      */
104     void SetCallback(void);
105 
106     /**
107      * This method checks whether this response need to be processed by callback handler later.
108      *
109      * @returns A bool value indicates whether this response need to be processed by callback handler later.
110      */
111     bool NeedCallback(void);
112 
113     /**
114      * This method labels the response as complete which means all fields has been successfully set.
115      *
116      */
117     void SetComplete();
118 
119     /**
120      * This method checks whether this response is ready to be written to buffer.
121      *
122      * @returns A bool value indicates whether this response is ready to be written to buffer..
123      */
124     bool IsComplete();
125 
126     /**
127      * This method is used to set a timestamp. when a callback is needed and this field tells callback handler when to
128      * collect all the data and form the response.
129      *
130      * @param[in] aStartTime  A timestamp indicates when the response start to wait for callback.
131      */
132     void SetStartTime(steady_clock::time_point aStartTime);
133 
134     /**
135      * This method returns a timestamp of start time.
136      *
137      * @returns A timepoint object indicates start time.
138      */
139     steady_clock::time_point GetStartTime() const;
140 
141     /**
142      * This method serialize a response to a string that could be sent by socket later.
143      *
144      * @returns A string contains status line, headers and body of a response.
145      */
146     std::string Serialize(void) const;
147 
148 private:
149     bool                               mCallback;
150     std::map<std::string, std::string> mHeaders;
151     std::string                        mCode;
152     std::string                        mProtocol;
153     std::string                        mBody;
154     bool                               mComplete;
155     steady_clock::time_point           mStartTime;
156 };
157 
158 } // namespace rest
159 } // namespace otbr
160 
161 #endif // OTBR_REST_RESPONSE_HPP_
162