• 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 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  */
52 class Request
53 {
54 public:
55     /**
56      * The constructor is to initialize Request instance.
57      *
58      */
59     Request(void);
60 
61     /**
62      * This method sets the Url field of a request.
63      *
64      * @param[in] aString  A pointer points to url string.
65      * @param[in] aLength  Length of the url string
66      *
67      */
68     void SetUrl(const char *aString, size_t aLength);
69 
70     /**
71      * This method sets the body field of a request.
72      *
73      * @param[in] aString  A pointer points to body string.
74      * @param[in] aLength  Length of the body string
75      *
76      */
77     void SetBody(const char *aString, size_t aLength);
78 
79     /**
80      * This method sets the content-length field of a request.
81      *
82      * @param[in] aContentLength  An unsigned integer representing content-length.
83      *
84      */
85     void SetContentLength(size_t aContentLength);
86 
87     /**
88      * This method sets the method of the parsed request.
89      *
90      * @param[in] aMethod  An integer representing request method.
91      *
92      */
93     void SetMethod(int32_t aMethod);
94 
95     /**
96      * This method sets the next header field of a request.
97      *
98      * @param[in] aString  A pointer points to body string.
99      * @param[in] aLength  Length of the body string
100      *
101      */
102     void SetNextHeaderField(const char *aString, size_t aLength);
103 
104     /**
105      * This method sets the header value of the previously set header of a request.
106      *
107      * @param[in] aString  A pointer points to body string.
108      * @param[in] aLength  Length of the body string
109      *
110      */
111     void SetHeaderValue(const char *aString, size_t aLength);
112 
113     /**
114      * This method labels the request as complete which means it no longer need to be parsed one more time .
115      *
116      */
117     void SetReadComplete(void);
118 
119     /**
120      * This method resets the request then it could be set by parser from start.
121      *
122      */
123     void ResetReadComplete(void);
124 
125     /**
126      * This method returns the HTTP method of this request.
127      *
128      * @returns A integer representing HTTP method.
129      */
130     HttpMethod GetMethod() const;
131 
132     /**
133      * This method returns the HTTP method of this request.
134      *
135      * @returns A HttpMethod enum representing HTTP method of this request.
136      */
137     std::string GetBody() const;
138 
139     /**
140      * This method returns the url for this request.
141      *
142      * @returns A string contains the url of this request.
143      */
144     std::string GetUrl(void) const;
145 
146     /**
147      * This method returns the specified header field for this request.
148      *
149      * @param[in] aHeaderField  A header field.
150      * @returns A string contains the header value of this request.
151      */
152     std::string GetHeaderValue(const std::string aHeaderField) const;
153 
154     /**
155      * This method indicates whether this request is parsed completely.
156      *
157      *
158      */
159     bool IsComplete(void) const;
160 
161 private:
162     int32_t                            mMethod;
163     size_t                             mContentLength;
164     std::string                        mUrl;
165     std::string                        mBody;
166     std::string                        mNextHeaderField;
167     std::map<std::string, std::string> mHeaders;
168     bool                               mComplete;
169 };
170 
171 } // namespace rest
172 } // namespace otbr
173 
174 #endif // OTBR_REST_REQUEST_HPP_
175