xref: /aosp_15_r20/external/ot-br-posix/src/rest/request.hpp (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 /**
30  * @file
31  *   This file includes request definition for RESTful HTTP server.
32  */
33 
34 #ifndef OTBR_REST_REQUEST_HPP_
35 #define OTBR_REST_REQUEST_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #include <map>
40 #include <string>
41 
42 #include "common/code_utils.hpp"
43 #include "rest/types.hpp"
44 
45 namespace otbr {
46 namespace rest {
47 
48 /**
49  * This class implements an instance to host services used by border router.
50  */
51 class Request
52 {
53 public:
54     /**
55      * The constructor is to initialize Request instance.
56      */
57     Request(void);
58 
59     /**
60      * This method sets the Url field of a request.
61      *
62      * @param[in] aString  A pointer points to url string.
63      * @param[in] aLength  Length of the url string
64      */
65     void SetUrl(const char *aString, size_t aLength);
66 
67     /**
68      * This method sets the body field of a request.
69      *
70      * @param[in] aString  A pointer points to body string.
71      * @param[in] aLength  Length of the body string
72      */
73     void SetBody(const char *aString, size_t aLength);
74 
75     /**
76      * This method sets the content-length field of a request.
77      *
78      * @param[in] aContentLength  An unsigned integer representing content-length.
79      */
80     void SetContentLength(size_t aContentLength);
81 
82     /**
83      * This method sets the method of the parsed request.
84      *
85      * @param[in] aMethod  An integer representing request method.
86      */
87     void SetMethod(int32_t aMethod);
88 
89     /**
90      * This method sets the next header field of a request.
91      *
92      * @param[in] aString  A pointer points to body string.
93      * @param[in] aLength  Length of the body string
94      */
95     void SetNextHeaderField(const char *aString, size_t aLength);
96 
97     /**
98      * This method sets the header value of the previously set header of a request.
99      *
100      * @param[in] aString  A pointer points to body string.
101      * @param[in] aLength  Length of the body string
102      */
103     void SetHeaderValue(const char *aString, size_t aLength);
104 
105     /**
106      * This method labels the request as complete which means it no longer need to be parsed one more time .
107      */
108     void SetReadComplete(void);
109 
110     /**
111      * This method resets the request then it could be set by parser from start.
112      */
113     void ResetReadComplete(void);
114 
115     /**
116      * This method returns the HTTP method of this request.
117      *
118      * @returns A integer representing HTTP method.
119      */
120     HttpMethod GetMethod() const;
121 
122     /**
123      * This method returns the HTTP method of this request.
124      *
125      * @returns A HttpMethod enum representing HTTP method of this request.
126      */
127     std::string GetBody() const;
128 
129     /**
130      * This method returns the url for this request.
131      *
132      * @returns A string contains the url of this request.
133      */
134     std::string GetUrl(void) const;
135 
136     /**
137      * This method returns the specified header field for this request.
138      *
139      * @param[in] aHeaderField  A header field.
140      * @returns A string contains the header value of this request.
141      */
142     std::string GetHeaderValue(const std::string aHeaderField) const;
143 
144     /**
145      * This method indicates whether this request is parsed completely.
146      */
147     bool IsComplete(void) const;
148 
149 private:
150     int32_t                            mMethod;
151     size_t                             mContentLength;
152     std::string                        mUrl;
153     std::string                        mBody;
154     std::string                        mNextHeaderField;
155     std::map<std::string, std::string> mHeaders;
156     bool                               mComplete;
157 };
158 
159 } // namespace rest
160 } // namespace otbr
161 
162 #endif // OTBR_REST_REQUEST_HPP_
163