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 class Response 58 { 59 public: 60 /** 61 * The constructor to initialize a response instance. 62 */ 63 Response(void); 64 65 /** 66 * This method set the response body. 67 * 68 * @param[in] aBody A string to be set as response body. 69 */ 70 void SetBody(std::string &aBody); 71 72 /** 73 * This method return a string contains the body field of this response. 74 * 75 * @returns A string containing the body field. 76 */ 77 std::string GetBody(void) const; 78 79 /** 80 * This method set the response code. 81 * 82 * @param[in] aCode A string representing response code such as "404 not found". 83 */ 84 void SetResponsCode(std::string &aCode); 85 86 /** 87 * This method sets the content type. 88 * 89 * @param[in] aCode A string representing response content type such as text/plain. 90 */ 91 void SetContentType(const std::string &aContentType); 92 93 /** 94 * This method labels the response as need callback. 95 */ 96 void SetCallback(void); 97 98 /** 99 * This method checks whether this response need to be processed by callback handler later. 100 * 101 * @returns A bool value indicates whether this response need to be processed by callback handler later. 102 */ 103 bool NeedCallback(void); 104 105 /** 106 * This method labels the response as complete which means all fields has been successfully set. 107 */ 108 void SetComplete(); 109 110 /** 111 * This method checks whether this response is ready to be written to buffer. 112 * 113 * @returns A bool value indicates whether this response is ready to be written to buffer.. 114 */ 115 bool IsComplete(); 116 117 /** 118 * This method is used to set a timestamp. when a callback is needed and this field tells callback handler when to 119 * collect all the data and form the response. 120 * 121 * @param[in] aStartTime A timestamp indicates when the response start to wait for callback. 122 */ 123 void SetStartTime(steady_clock::time_point aStartTime); 124 125 /** 126 * This method returns a timestamp of start time. 127 * 128 * @returns A timepoint object indicates start time. 129 */ 130 steady_clock::time_point GetStartTime() const; 131 132 /** 133 * This method serialize a response to a string that could be sent by socket later. 134 * 135 * @returns A string contains status line, headers and body of a response. 136 */ 137 std::string Serialize(void) const; 138 139 private: 140 bool mCallback; 141 std::map<std::string, std::string> mHeaders; 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