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