File size: 5,600 Bytes
7791ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/crt/Exports.h>
#include <aws/crt/Types.h>
#include <aws/crt/io/Stream.h>

struct aws_http_header;
struct aws_http_message;

namespace Aws
{
    namespace Crt
    {
        namespace Mqtt
        {
            class MqttConnection;
            class MqttConnectionCore;
        } // namespace Mqtt
        namespace Mqtt5
        {
            class Mqtt5ClientCore;
        }
        namespace Http
        {
            using HttpHeader = aws_http_header;

            /**
             * Base class representing a mutable http request or response.
             */
            class AWS_CRT_CPP_API HttpMessage
            {
              public:
                virtual ~HttpMessage();

                HttpMessage(const HttpMessage &) = delete;
                HttpMessage(HttpMessage &&) = delete;
                HttpMessage &operator=(const HttpMessage &) = delete;
                HttpMessage &operator=(HttpMessage &&) = delete;

                /**
                 * Gets the input stream representing the message body
                 */
                std::shared_ptr<Aws::Crt::Io::InputStream> GetBody() const noexcept;

                /**
                 * Sets the input stream representing the message body
                 * @param body the input stream representing the message body
                 * @return success/failure
                 */
                bool SetBody(const std::shared_ptr<Aws::Crt::Io::IStream> &body) noexcept;

                /**
                 * Sets the input stream representing the message body
                 * @param body the input stream representing the message body
                 * @return success/failure
                 */
                bool SetBody(const std::shared_ptr<Aws::Crt::Io::InputStream> &body) noexcept;

                /**
                 * Gets the number of headers contained in this request
                 * @return the number of headers contained in this request
                 */
                size_t GetHeaderCount() const noexcept;

                /**
                 * Gets a particular header in the request
                 * @param index index of the header to fetch
                 * @return an option containing the requested header if the index is in bounds
                 */
                Optional<HttpHeader> GetHeader(size_t index) const noexcept;

                /**
                 * Adds a header to the request
                 * @param header header to add
                 * @return success/failure
                 */
                bool AddHeader(const HttpHeader &header) noexcept;

                /**
                 * Removes a header from the request
                 * @param index index of the header to remove
                 * @return success/failure
                 */
                bool EraseHeader(size_t index) noexcept;

                /**
                 * @return true/false if the underlying object is valid
                 */
                operator bool() const noexcept { return m_message != nullptr; }

                /// @private
                struct aws_http_message *GetUnderlyingMessage() const noexcept { return m_message; }

              protected:
                HttpMessage(Allocator *allocator, struct aws_http_message *message) noexcept;

                Allocator *m_allocator;
                struct aws_http_message *m_message;
                std::shared_ptr<Aws::Crt::Io::InputStream> m_bodyStream;
            };

            /**
             * Class representing a mutable http request.
             */
            class AWS_CRT_CPP_API HttpRequest : public HttpMessage
            {
                friend class Mqtt::MqttConnectionCore;
                friend class Mqtt5::Mqtt5ClientCore;

              public:
                HttpRequest(Allocator *allocator = ApiAllocator());

                /**
                 * @return the value of the Http method associated with this request
                 */
                Optional<ByteCursor> GetMethod() const noexcept;

                /**
                 * Sets the value of the Http method associated with this request
                 */
                bool SetMethod(ByteCursor method) noexcept;

                /**
                 * @return the value of the URI-path associated with this request
                 */
                Optional<ByteCursor> GetPath() const noexcept;

                /**
                 * Sets the value of the URI-path associated with this request
                 */
                bool SetPath(ByteCursor path) noexcept;

              protected:
                HttpRequest(Allocator *allocator, struct aws_http_message *message);
            };

            /**
             * Class representing a mutable http response.
             */
            class AWS_CRT_CPP_API HttpResponse : public HttpMessage
            {
              public:
                HttpResponse(Allocator *allocator = ApiAllocator());

                /**
                 * @return the integral Http response code associated with this response
                 */
                Optional<int> GetResponseCode() const noexcept;

                /**
                 * Sets the integral Http response code associated with this response
                 */
                bool SetResponseCode(int response) noexcept;
            };
        } // namespace Http
    } // namespace Crt
} // namespace Aws