1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
3 |
|
---|
4 | /** @file httpapi.h
|
---|
5 | * @brief This module implements the standard HTTP API used by the C IoT client
|
---|
6 | * library.
|
---|
7 | *
|
---|
8 | * @details For example, on the Windows platform the HTTP API code uses
|
---|
9 | * WinHTTP and for Linux it uses curl and so forth. HTTPAPI must support
|
---|
10 | * HTTPs (HTTP+SSL).
|
---|
11 | */
|
---|
12 |
|
---|
13 | #ifndef HTTPAPI_H
|
---|
14 | #define HTTPAPI_H
|
---|
15 |
|
---|
16 | #ifdef __cplusplus
|
---|
17 | #include <cstddef>
|
---|
18 | #else
|
---|
19 | #include <stddef.h>
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include "azure_macro_utils/macro_utils.h"
|
---|
23 | #include "azure_c_shared_utility/httpheaders.h"
|
---|
24 | #include "azure_c_shared_utility/buffer_.h"
|
---|
25 | #include "umock_c/umock_c_prod.h"
|
---|
26 |
|
---|
27 | #ifdef __cplusplus
|
---|
28 | extern "C" {
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | typedef struct HTTP_HANDLE_DATA_TAG* HTTP_HANDLE;
|
---|
32 |
|
---|
33 | #define AMBIGUOUS_STATUS_CODE (300)
|
---|
34 |
|
---|
35 | #define HTTPAPI_RESULT_VALUES \
|
---|
36 | HTTPAPI_OK, \
|
---|
37 | HTTPAPI_INVALID_ARG, \
|
---|
38 | HTTPAPI_ERROR, \
|
---|
39 | HTTPAPI_OPEN_REQUEST_FAILED, \
|
---|
40 | HTTPAPI_SET_OPTION_FAILED, \
|
---|
41 | HTTPAPI_SEND_REQUEST_FAILED, \
|
---|
42 | HTTPAPI_RECEIVE_RESPONSE_FAILED, \
|
---|
43 | HTTPAPI_QUERY_HEADERS_FAILED, \
|
---|
44 | HTTPAPI_QUERY_DATA_AVAILABLE_FAILED, \
|
---|
45 | HTTPAPI_READ_DATA_FAILED, \
|
---|
46 | HTTPAPI_ALREADY_INIT, \
|
---|
47 | HTTPAPI_NOT_INIT, \
|
---|
48 | HTTPAPI_HTTP_HEADERS_FAILED, \
|
---|
49 | HTTPAPI_STRING_PROCESSING_ERROR, \
|
---|
50 | HTTPAPI_ALLOC_FAILED, \
|
---|
51 | HTTPAPI_INIT_FAILED, \
|
---|
52 | HTTPAPI_INSUFFICIENT_RESPONSE_BUFFER, \
|
---|
53 | HTTPAPI_SET_X509_FAILURE, \
|
---|
54 | HTTPAPI_SET_TIMEOUTS_FAILED \
|
---|
55 |
|
---|
56 | /** @brief Enumeration specifying the possible return values for the APIs in
|
---|
57 | * this module.
|
---|
58 | */
|
---|
59 | MU_DEFINE_ENUM(HTTPAPI_RESULT, HTTPAPI_RESULT_VALUES);
|
---|
60 |
|
---|
61 | #define HTTPAPI_REQUEST_TYPE_VALUES\
|
---|
62 | HTTPAPI_REQUEST_GET, \
|
---|
63 | HTTPAPI_REQUEST_POST, \
|
---|
64 | HTTPAPI_REQUEST_PUT, \
|
---|
65 | HTTPAPI_REQUEST_DELETE, \
|
---|
66 | HTTPAPI_REQUEST_PATCH, \
|
---|
67 | HTTPAPI_REQUEST_HEAD \
|
---|
68 |
|
---|
69 |
|
---|
70 | /** @brief Enumeration specifying the HTTP request verbs accepted by
|
---|
71 | * the HTTPAPI module.
|
---|
72 | */
|
---|
73 | MU_DEFINE_ENUM(HTTPAPI_REQUEST_TYPE, HTTPAPI_REQUEST_TYPE_VALUES);
|
---|
74 |
|
---|
75 | #define MAX_HOSTNAME_LEN 65
|
---|
76 | #define MAX_USERNAME_LEN 65
|
---|
77 | #define MAX_PASSWORD_LEN 65
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * @brief Global initialization for the HTTP API component.
|
---|
81 | *
|
---|
82 | * Platform specific implementations are expected to initialize
|
---|
83 | * the underlying HTTP API stacks.
|
---|
84 | *
|
---|
85 | * @return @c HTTPAPI_OK if initialization is successful or an error
|
---|
86 | * code in case it fails.
|
---|
87 | */
|
---|
88 | MOCKABLE_FUNCTION(, HTTPAPI_RESULT, HTTPAPI_Init);
|
---|
89 |
|
---|
90 | /** @brief Free resources allocated in ::HTTPAPI_Init. */
|
---|
91 | MOCKABLE_FUNCTION(, void, HTTPAPI_Deinit);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * @brief Creates an HTTPS connection to the host specified by the @p
|
---|
95 | * hostName parameter.
|
---|
96 | *
|
---|
97 | * @param hostName Name of the host.
|
---|
98 | *
|
---|
99 | * This function returns a handle to the newly created connection.
|
---|
100 | * You can use the handle in subsequent calls to execute specific
|
---|
101 | * HTTP calls using ::HTTPAPI_ExecuteRequest.
|
---|
102 | *
|
---|
103 | * @return A @c HTTP_HANDLE to the newly created connection or @c NULL in
|
---|
104 | * case an error occurs.
|
---|
105 | */
|
---|
106 | MOCKABLE_FUNCTION(, HTTP_HANDLE, HTTPAPI_CreateConnection, const char*, hostName);
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * @brief Closes a connection created with ::HTTPAPI_CreateConnection.
|
---|
110 | *
|
---|
111 | * @param handle The handle to the HTTP connection created via ::HTTPAPI_CreateConnection.
|
---|
112 | *
|
---|
113 | * All resources allocated by ::HTTPAPI_CreateConnection should be
|
---|
114 | * freed in ::HTTPAPI_CloseConnection.
|
---|
115 | */
|
---|
116 | MOCKABLE_FUNCTION(, void, HTTPAPI_CloseConnection, HTTP_HANDLE, handle);
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * @brief Sends the HTTP request to the host and handles the response for
|
---|
120 | * the HTTP call.
|
---|
121 | *
|
---|
122 | * @param handle The handle to the HTTP connection created
|
---|
123 | * via ::HTTPAPI_CreateConnection.
|
---|
124 | * @param requestType Specifies which HTTP method is used (GET,
|
---|
125 | * POST, DELETE, PUT, PATCH).
|
---|
126 | * @param relativePath Specifies the relative path of the URL
|
---|
127 | * excluding the host name.
|
---|
128 | * @param httpHeadersHandle Specifies a set of HTTP headers (name-value
|
---|
129 | * pairs) to be added to the
|
---|
130 | * HTTP request. The @p httpHeadersHandle
|
---|
131 | * handle can be created and setup with
|
---|
132 | * the proper name-value pairs by using the
|
---|
133 | * HTTPHeaders APIs available in @c
|
---|
134 | * HTTPHeaders.h.
|
---|
135 | * @param content Specifies a pointer to the request body.
|
---|
136 | * This value is optional and can be @c NULL.
|
---|
137 | * @param contentLength Specifies the request body size (this is
|
---|
138 | * typically added into the HTTP headers as
|
---|
139 | * the Content-Length header). This value is
|
---|
140 | * optional and can be 0.
|
---|
141 | * @param statusCode This is an out parameter, where
|
---|
142 | * ::HTTPAPI_ExecuteRequest returns the status
|
---|
143 | * code from the HTTP response (200, 201, 400,
|
---|
144 | * 401, etc.)
|
---|
145 | * @param responseHeadersHandle This is an HTTP headers handle to which
|
---|
146 | * ::HTTPAPI_ExecuteRequest must add all the
|
---|
147 | * HTTP response headers so that the caller of
|
---|
148 | * ::HTTPAPI_ExecuteRequest can inspect them.
|
---|
149 | * You can manipulate @p responseHeadersHandle
|
---|
150 | * by using the HTTPHeaders APIs available in
|
---|
151 | * @c HTTPHeaders.h
|
---|
152 | * @param responseContent This is a buffer that must be filled by
|
---|
153 | * ::HTTPAPI_ExecuteRequest with the contents
|
---|
154 | * of the HTTP response body. The buffer size
|
---|
155 | * must be increased by the
|
---|
156 | * ::HTTPAPI_ExecuteRequest implementation in
|
---|
157 | * order to fit the response body.
|
---|
158 | * ::HTTPAPI_ExecuteRequest must also handle
|
---|
159 | * chunked transfer encoding for HTTP responses.
|
---|
160 | * To manipulate the @p responseContent buffer,
|
---|
161 | * use the APIs available in @c Strings.h.
|
---|
162 | *
|
---|
163 | * @return @c HTTPAPI_OK if the API call is successful or an error
|
---|
164 | * code in case it fails.
|
---|
165 | */
|
---|
166 | MOCKABLE_FUNCTION(, HTTPAPI_RESULT, HTTPAPI_ExecuteRequest, HTTP_HANDLE, handle, HTTPAPI_REQUEST_TYPE, requestType, const char*, relativePath,
|
---|
167 | HTTP_HEADERS_HANDLE, httpHeadersHandle, const unsigned char*, content,
|
---|
168 | size_t, contentLength, unsigned int*, statusCode,
|
---|
169 | HTTP_HEADERS_HANDLE, responseHeadersHandle, BUFFER_HANDLE, responseContent);
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * @brief Sets the option named @p optionName bearing the value
|
---|
173 | * @p value for the HTTP_HANDLE @p handle.
|
---|
174 | *
|
---|
175 | * @param handle The handle to the HTTP connection created via
|
---|
176 | * ::HTTPAPI_CreateConnection.
|
---|
177 | * @param optionName A @c NULL terminated string representing the name
|
---|
178 | * of the option.
|
---|
179 | * @param value A pointer to the value for the option.
|
---|
180 | *
|
---|
181 | * @return @c HTTPAPI_OK if initialization is successful or an error
|
---|
182 | * code in case it fails.
|
---|
183 | */
|
---|
184 | MOCKABLE_FUNCTION(, HTTPAPI_RESULT, HTTPAPI_SetOption, HTTP_HANDLE, handle, const char*, optionName, const void*, value);
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * @brief Clones the option named @p optionName bearing the value @p value
|
---|
188 | * into the pointer @p savedValue.
|
---|
189 | *
|
---|
190 | * @param optionName A @c NULL terminated string representing the name of
|
---|
191 | * the option
|
---|
192 | * @param value A pointer to the value of the option.
|
---|
193 | * @param savedValue This pointer receives the copy of the value of the
|
---|
194 | * option. The copy needs to be free-able.
|
---|
195 | *
|
---|
196 | * @return @c HTTPAPI_OK if initialization is successful or an error
|
---|
197 | * code in case it fails.
|
---|
198 | */
|
---|
199 | MOCKABLE_FUNCTION(, HTTPAPI_RESULT, HTTPAPI_CloneOption, const char*, optionName, const void*, value, const void**, savedValue);
|
---|
200 |
|
---|
201 | #ifdef __cplusplus
|
---|
202 | }
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | #endif /* HTTPAPI_H */
|
---|