1  
//
1  
//
2  
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3  
// Copyright (c) 2025 Mohammad Nejati
3  
// Copyright (c) 2025 Mohammad Nejati
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/cppalliance/http
8  
// Official repository: https://github.com/cppalliance/http
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_HTTP_REQUEST_BASE_HPP
11  
#ifndef BOOST_HTTP_REQUEST_BASE_HPP
12  
#define BOOST_HTTP_REQUEST_BASE_HPP
12  
#define BOOST_HTTP_REQUEST_BASE_HPP
13  

13  

14  
#include <boost/http/detail/config.hpp>
14  
#include <boost/http/detail/config.hpp>
15  
#include <boost/http/message_base.hpp>
15  
#include <boost/http/message_base.hpp>
16  

16  

17  
namespace boost {
17  
namespace boost {
18  
namespace http {
18  
namespace http {
19  

19  

20  
/** Mixin for modifing HTTP requests.
20  
/** Mixin for modifing HTTP requests.
21  

21  

22  
    @see
22  
    @see
23  
        @ref message_base,
23  
        @ref message_base,
24  
        @ref request,
24  
        @ref request,
25  
        @ref static_request.
25  
        @ref static_request.
26  
*/
26  
*/
27  
class request_base
27  
class request_base
28  
    : public message_base
28  
    : public message_base
29  
{
29  
{
30  
    friend class request;
30  
    friend class request;
31  
    friend class static_request;
31  
    friend class static_request;
32  

32  

33  
    request_base() noexcept
33  
    request_base() noexcept
34  
        : message_base(detail::kind::request)
34  
        : message_base(detail::kind::request)
35  
    {
35  
    {
36  
    }
36  
    }
37  

37  

38  
    explicit
38  
    explicit
39  
    request_base(core::string_view s)
39  
    request_base(core::string_view s)
40  
        : message_base(detail::kind::request, s)
40  
        : message_base(detail::kind::request, s)
41  
    {
41  
    {
42  
    }
42  
    }
43  

43  

44  
    request_base(
44  
    request_base(
45  
        void* storage,
45  
        void* storage,
46  
        std::size_t cap) noexcept
46  
        std::size_t cap) noexcept
47  
        : message_base(
47  
        : message_base(
48  
            detail::kind::request, storage, cap)
48  
            detail::kind::request, storage, cap)
49  
    {
49  
    {
50  
    }
50  
    }
51  

51  

52  
public:
52  
public:
53  
    //--------------------------------------------
53  
    //--------------------------------------------
54  
    //
54  
    //
55  
    // Observers
55  
    // Observers
56  
    //
56  
    //
57  
    //--------------------------------------------
57  
    //--------------------------------------------
58  

58  

59  
    /** Return the method as a name constant.
59  
    /** Return the method as a name constant.
60  

60  

61  
        If the method returned is equal to
61  
        If the method returned is equal to
62  
        @ref method::unknown, the method may
62  
        @ref method::unknown, the method may
63  
        be obtained as a string instead, by
63  
        be obtained as a string instead, by
64  
        calling @ref method_text.
64  
        calling @ref method_text.
65  
    */
65  
    */
66  
    http::method
66  
    http::method
67  
    method() const noexcept
67  
    method() const noexcept
68  
    {
68  
    {
69  
        return h_.req.method;
69  
        return h_.req.method;
70  
    }
70  
    }
71  

71  

72  
    /** Return the method as a string.
72  
    /** Return the method as a string.
73  
    */
73  
    */
74  
    core::string_view
74  
    core::string_view
75  
    method_text() const noexcept
75  
    method_text() const noexcept
76  
    {
76  
    {
77  
        return core::string_view(
77  
        return core::string_view(
78  
            h_.cbuf,
78  
            h_.cbuf,
79  
            h_.req.method_len);
79  
            h_.req.method_len);
80  
    }
80  
    }
81  

81  

82  
    /** Return the request-target string.
82  
    /** Return the request-target string.
83  
    */
83  
    */
84  
    core::string_view
84  
    core::string_view
85  
    target() const noexcept
85  
    target() const noexcept
86  
    {
86  
    {
87  
        return core::string_view(
87  
        return core::string_view(
88  
            h_.cbuf +
88  
            h_.cbuf +
89  
                h_.req.method_len + 1,
89  
                h_.req.method_len + 1,
90  
            h_.req.target_len);
90  
            h_.req.target_len);
91  
    }
91  
    }
92  

92  

93  
    //--------------------------------------------
93  
    //--------------------------------------------
94  
    //
94  
    //
95  
    // Modifiers
95  
    // Modifiers
96  
    //
96  
    //
97  
    //--------------------------------------------
97  
    //--------------------------------------------
98  

98  

99  
    /** Set the method of the request to the enum.
99  
    /** Set the method of the request to the enum.
100  

100  

101  
        @par Exception Safety
101  
        @par Exception Safety
102  
        Strong guarantee.
102  
        Strong guarantee.
103  
        Calls to allocate may throw.
103  
        Calls to allocate may throw.
104  
        Exception thrown if max capacity exceeded.
104  
        Exception thrown if max capacity exceeded.
105  

105  

106  
        @throw std::length_error
106  
        @throw std::length_error
107  
        Max capacity would be exceeded.
107  
        Max capacity would be exceeded.
108  

108  

109  
        @param m The method to set.
109  
        @param m The method to set.
110  
    */
110  
    */
111  
    void
111  
    void
112  
    set_method(
112  
    set_method(
113  
        http::method m)
113  
        http::method m)
114  
    {
114  
    {
115  
        set_start_line_impl(
115  
        set_start_line_impl(
116  
            m,
116  
            m,
117  
            to_string(m),
117  
            to_string(m),
118  
            target(),
118  
            target(),
119  
            version());
119  
            version());
120  
    }
120  
    }
121  

121  

122  
    /** Set the method of the request to the string.
122  
    /** Set the method of the request to the string.
123  

123  

124  
        @par Exception Safety
124  
        @par Exception Safety
125  
        Strong guarantee.
125  
        Strong guarantee.
126  
        Calls to allocate may throw.
126  
        Calls to allocate may throw.
127  
        Exception thrown on invalid input.
127  
        Exception thrown on invalid input.
128  
        Exception thrown if max capacity exceeded.
128  
        Exception thrown if max capacity exceeded.
129  

129  

130  
        @throw system_error
130  
        @throw system_error
131  
        Input is invalid.
131  
        Input is invalid.
132  

132  

133  
        @throw std::length_error
133  
        @throw std::length_error
134  
        Max capacity would be exceeded.
134  
        Max capacity would be exceeded.
135  

135  

136  
        @param s A string view representing the
136  
        @param s A string view representing the
137  
        method to set.
137  
        method to set.
138  
    */
138  
    */
139  
    void
139  
    void
140  
    set_method(
140  
    set_method(
141  
        core::string_view s)
141  
        core::string_view s)
142  
    {
142  
    {
143  
        set_start_line_impl(
143  
        set_start_line_impl(
144  
            string_to_method(s),
144  
            string_to_method(s),
145  
            s,
145  
            s,
146  
            target(),
146  
            target(),
147  
            version());
147  
            version());
148  
    }
148  
    }
149  

149  

150  
    /** Set the target string of the request.
150  
    /** Set the target string of the request.
151  

151  

152  
        This function sets the request-target.
152  
        This function sets the request-target.
153  
        The caller is responsible for ensuring
153  
        The caller is responsible for ensuring
154  
        that the string passed is syntactically
154  
        that the string passed is syntactically
155  
        valid.
155  
        valid.
156  

156  

157  
        @par Exception Safety
157  
        @par Exception Safety
158  
        Strong guarantee.
158  
        Strong guarantee.
159  
        Calls to allocate may throw.
159  
        Calls to allocate may throw.
160  
        Exception thrown on invalid input.
160  
        Exception thrown on invalid input.
161  
        Exception thrown if max capacity exceeded.
161  
        Exception thrown if max capacity exceeded.
162  

162  

163  
        @throw system_error
163  
        @throw system_error
164  
        Input is invalid.
164  
        Input is invalid.
165  

165  

166  
        @throw std::length_error
166  
        @throw std::length_error
167  
        Max capacity would be exceeded.
167  
        Max capacity would be exceeded.
168  

168  

169  
        @param s A string view representing the
169  
        @param s A string view representing the
170  
        target to set.
170  
        target to set.
171  
    */
171  
    */
172  
    void
172  
    void
173  
    set_target(
173  
    set_target(
174  
        core::string_view s)
174  
        core::string_view s)
175  
    {
175  
    {
176  
        set_start_line_impl(
176  
        set_start_line_impl(
177  
            h_.req.method,
177  
            h_.req.method,
178  
            method_text(),
178  
            method_text(),
179  
            s,
179  
            s,
180  
            version());
180  
            version());
181  
    }
181  
    }
182  

182  

183  
    /** Set the HTTP version of the request.
183  
    /** Set the HTTP version of the request.
184  

184  

185  
        @par Exception Safety
185  
        @par Exception Safety
186  
        Strong guarantee.
186  
        Strong guarantee.
187  
        Calls to allocate may throw.
187  
        Calls to allocate may throw.
188  
        Exception thrown if max capacity exceeded.
188  
        Exception thrown if max capacity exceeded.
189  

189  

190  
        @throw std::length_error
190  
        @throw std::length_error
191  
        Max capacity would be exceeded.
191  
        Max capacity would be exceeded.
192  

192  

193  
        @param v The version to set.
193  
        @param v The version to set.
194  
    */
194  
    */
195  
    void
195  
    void
196  
    set_version(
196  
    set_version(
197  
        http::version v)
197  
        http::version v)
198  
    {
198  
    {
199  
        set_start_line_impl(
199  
        set_start_line_impl(
200  
            h_.req.method,
200  
            h_.req.method,
201  
            method_text(),
201  
            method_text(),
202  
            target(),
202  
            target(),
203  
            v);
203  
            v);
204  
    }
204  
    }
205  

205  

206  
    /** Set the method, target, and version of the request.
206  
    /** Set the method, target, and version of the request.
207  

207  

208  
        This is more efficient than setting the
208  
        This is more efficient than setting the
209  
        properties individually.
209  
        properties individually.
210  

210  

211  
        @par Exception Safety
211  
        @par Exception Safety
212  
        Strong guarantee.
212  
        Strong guarantee.
213  
        Calls to allocate may throw.
213  
        Calls to allocate may throw.
214  
        Exception thrown on invalid input.
214  
        Exception thrown on invalid input.
215  
        Exception thrown if max capacity exceeded.
215  
        Exception thrown if max capacity exceeded.
216  

216  

217  
        @throw system_error
217  
        @throw system_error
218  
        Input is invalid.
218  
        Input is invalid.
219  

219  

220  
        @throw std::length_error
220  
        @throw std::length_error
221  
        Max capacity would be exceeded.
221  
        Max capacity would be exceeded.
222  

222  

223  
        @param m The method to set.
223  
        @param m The method to set.
224  

224  

225  
        @param t A string view representing the
225  
        @param t A string view representing the
226  
        target to set.
226  
        target to set.
227  

227  

228  
        @param v The version to set.
228  
        @param v The version to set.
229  
    */
229  
    */
230  
    void
230  
    void
231  
    set_start_line(
231  
    set_start_line(
232  
        http::method m,
232  
        http::method m,
233  
        core::string_view t,
233  
        core::string_view t,
234  
        http::version v =
234  
        http::version v =
235  
            http::version::http_1_1)
235  
            http::version::http_1_1)
236  
    {
236  
    {
237  
        set_start_line_impl(m, to_string(m), t, v);
237  
        set_start_line_impl(m, to_string(m), t, v);
238  
    }
238  
    }
239  

239  

240  
    /** Set the method, target, and version of the request.
240  
    /** Set the method, target, and version of the request.
241  

241  

242  
        This is more efficient than setting the
242  
        This is more efficient than setting the
243  
        properties individually.
243  
        properties individually.
244  

244  

245  
        @par Exception Safety
245  
        @par Exception Safety
246  
        Strong guarantee.
246  
        Strong guarantee.
247  
        Calls to allocate may throw.
247  
        Calls to allocate may throw.
248  
        Exception thrown on invalid input.
248  
        Exception thrown on invalid input.
249  
        Exception thrown if max capacity exceeded.
249  
        Exception thrown if max capacity exceeded.
250  

250  

251  
        @throw system_error
251  
        @throw system_error
252  
        Input is invalid.
252  
        Input is invalid.
253  

253  

254  
        @throw std::length_error
254  
        @throw std::length_error
255  
        Max capacity would be exceeded.
255  
        Max capacity would be exceeded.
256  

256  

257  
        @param m A string view representing the
257  
        @param m A string view representing the
258  
        method to set.
258  
        method to set.
259  

259  

260  
        @param t A string view representing the
260  
        @param t A string view representing the
261  
        target to set.
261  
        target to set.
262  

262  

263  
        @param v The version to set.
263  
        @param v The version to set.
264  
    */
264  
    */
265  
    void
265  
    void
266  
    set_start_line(
266  
    set_start_line(
267  
        core::string_view m,
267  
        core::string_view m,
268  
        core::string_view t,
268  
        core::string_view t,
269  
        http::version v =
269  
        http::version v =
270  
            http::version::http_1_1)
270  
            http::version::http_1_1)
271  
    {
271  
    {
272  
        set_start_line_impl(string_to_method(m), m, t, v);
272  
        set_start_line_impl(string_to_method(m), m, t, v);
273  
    }
273  
    }
274  

274  

275  
    /** Set the `Expect: 100-continue` header.
275  
    /** Set the `Expect: 100-continue` header.
276  

276  

277  
        @par Exception Safety
277  
        @par Exception Safety
278  
        Strong guarantee.
278  
        Strong guarantee.
279  
        Calls to allocate may throw.
279  
        Calls to allocate may throw.
280  
        Exception thrown if max capacity exceeded.
280  
        Exception thrown if max capacity exceeded.
281  

281  

282  
        @throw std::length_error
282  
        @throw std::length_error
283  
        Max capacity would be exceeded.
283  
        Max capacity would be exceeded.
284  

284  

285  
        @param b If `true` sets `Expect: 100-continue`
285  
        @param b If `true` sets `Expect: 100-continue`
286  
        header otherwise erase it.
286  
        header otherwise erase it.
287  
    */
287  
    */
288  
    BOOST_HTTP_DECL
288  
    BOOST_HTTP_DECL
289  
    void
289  
    void
290  
    set_expect_100_continue(bool b);
290  
    set_expect_100_continue(bool b);
291  

291  

292  
private:
292  
private:
293  
    BOOST_HTTP_DECL
293  
    BOOST_HTTP_DECL
294  
    void
294  
    void
295  
    set_start_line_impl(
295  
    set_start_line_impl(
296  
        http::method m,
296  
        http::method m,
297  
        core::string_view ms,
297  
        core::string_view ms,
298  
        core::string_view t,
298  
        core::string_view t,
299  
        http::version v);
299  
        http::version v);
300  
};
300  
};
301  

301  

302  
} // http
302  
} // http
303  
} // boost
303  
} // boost
304  

304  

305  
#endif
305  
#endif