2 * Copyright (c) 2020, The OpenThread Authors.
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.
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.
31 * This file includes response definition for RESTful HTTP server.
34 #ifndef OTBR_REST_RESPONSE_HPP_
35 #define OTBR_REST_RESPONSE_HPP_
41 #include "rest/types.hpp"
43 using std::chrono::duration_cast;
44 using std::chrono::microseconds;
45 using std::chrono::seconds;
46 using std::chrono::steady_clock;
52 * This class implements a response class for OTBR_REST, it could be manipulated by connection instance and resource
60 * The constructor to initialize a response instance.
67 * This method set the response body.
69 * @param[in] aBody A string to be set as response body.
72 void SetBody(std::string &aBody);
75 * This method return a string contains the body field of this response.
77 * @returns A string containing the body field.
79 std::string GetBody(void) const;
82 * This method set the response code.
84 * @param[in] aCode A string representing response code such as "404 not found".
87 void SetResponsCode(std::string &aCode);
90 * This method labels the response as need callback.
94 void SetCallback(void);
97 * This method checks whether this response need to be processed by callback handler later.
99 * @returns A bool value indicates whether this response need to be processed by callback handler later.
101 bool NeedCallback(void);
104 * This method labels the response as complete which means all fields has been successfully set.
110 * This method checks whether this response is ready to be written to buffer.
112 * @returns A bool value indicates whether this response is ready to be written to buffer..
117 * This method is used to set a timestamp. when a callback is needed and this field tells callback handler when to
118 * collect all the data and form the response.
120 * @param[in] aStartTime A timestamp indicates when the response start to wait for callback.
122 void SetStartTime(steady_clock::time_point aStartTime);
125 * This method returns a timestamp of start time.
127 * @returns A timepoint object indicates start time.
129 steady_clock::time_point GetStartTime() const;
132 * This method serialize a response to a string that could be sent by socket later.
134 * @returns A string contains status line, headers and body of a response.
136 std::string Serialize(void) const;
140 std::vector<std::string> mHeaderField;
141 std::vector<std::string> mHeaderValue;
143 std::string mProtocol;
146 steady_clock::time_point mStartTime;
152 #endif // OTBR_REST_RESPONSE_HPP_