Added HTTP header to connect options

This commit is contained in:
fpagliughi 2020-10-26 22:35:11 -04:00
parent c8a8dba470
commit e46317ac15
4 changed files with 72 additions and 15 deletions

View File

@ -38,6 +38,7 @@ To keep up with the latest announcements for this project, or to ask questions:
- Can choose to clear the persistence store on startup
- Select whether to persist QoS 0 messages
- Started classes to create options using the Builder Pattern, with the `create_options_builder`, `connect_options_builder`, `message_ptr_builder`, etc.
- User-defined websocket HTTP headers.
- Converted the unit tests to use Catch2
- [#231] Added `on_disconnected` callback to handle receipt of disconnect packet from server.
- [#211, 223, #235] Removed use of Log() function from the Paho C library.

View File

@ -73,6 +73,9 @@ class connect_options
/** The connect properties */
properties props_;
/** HTTP Headers */
name_value_collection httpHeaders_;
/** The client has special access */
friend class async_client;
friend class connect_options_test;
@ -436,9 +439,32 @@ public:
* @param props The properties to move into the connect object.
*/
void set_properties(properties&& props) {
props_ = props;
props_ = std::move(props);
opts_.connectProperties = const_cast<MQTTProperties*>(&props_.c_struct());
}
/**
* Gets the HTTP headers
* @return A const reference to the HTTP headers name/value collection.
*/
const name_value_collection& get_http_headers() const {
return httpHeaders_;
}
/**
* Sets the HTTP headers for the connection.
* @param httpHeaders The header nam/value collection.
*/
void set_http_headers(const name_value_collection& httpHeaders) {
httpHeaders_ = httpHeaders;
opts_.httpHeaders = httpHeaders_.empty() ? nullptr : httpHeaders_.c_arr();
}
/**
* Sets the HTTP headers for the connection.
* @param httpHeaders The header nam/value collection.
*/
void set_http_headers(name_value_collection&& httpHeaders) {
httpHeaders_ = std::move(httpHeaders);
opts_.httpHeaders = httpHeaders_.empty() ? nullptr : httpHeaders_.c_arr();
}
/**
* Gets a string representation of the object.
* @return A string representation of the object.
@ -647,6 +673,22 @@ public:
opts_.set_properties(std::move(props));
return *this;
}
/**
* Sets the HTTP headers for the connection.
* @param httpHeaders The header nam/value collection.
*/
auto http_headers(const name_value_collection& headers) -> self& {
opts_.set_http_headers(headers);
return *this;
}
/**
* Sets the HTTP headers for the connection.
* @param httpHeaders The header nam/value collection.
*/
auto set_http_headers(name_value_collection&& headers) -> self& {
opts_.set_http_headers(std::move(headers));
return *this;
}
/**
* Finish building the options and return them.
* @return The option struct as built.

View File

@ -265,7 +265,7 @@ public:
using ptr_t = std::shared_ptr<name_value_collection>;
/** Smart/shared pointer to a const object of this type */
using const_ptr_t = std::shared_ptr<const name_value_collection>;
/** The type of the string/string pair of values */
using value_type = collection_type::value_type;
/**
* Default construtor for an empty collection.
@ -311,6 +311,20 @@ public:
: map_{ init } {
update_c_arr();
}
/**
* Copy assignment.
* @param other Another collection of name/value pairs.
*/
name_value_collection& operator=(const name_value_collection& other) {
map_ = other.map_;
update_c_arr();
return *this;
}
/**
* Move constructor.
* @param other Another collection of name/value pairs
*/
name_value_collection& operator=(name_value_collection&& other) = default;
/**
* Determines if the collection is empty.
* @return @em true if the container is empty, @em false if it contains
@ -351,7 +365,7 @@ public:
* @return pointer to an array of NUL-terminated C string pointer pairs
* for name/values. The array is terminated by a NULL/NULL pair.
*/
char* const* c_arr() const { return (char* const *) cArr_.data(); }
const MQTTAsync_nameValue* c_arr() const { return cArr_.data(); }
};

View File

@ -350,15 +350,15 @@ TEST_CASE("name_value_collection initializer ctor", "[collections]")
auto cArr = nvc.c_arr();
REQUIRE(0 == strcmp("name0", cArr[0]));
REQUIRE(0 == strcmp("value0", cArr[1]));
REQUIRE(0 == strcmp("name1", cArr[2]));
REQUIRE(0 == strcmp("value1", cArr[3]));
REQUIRE(0 == strcmp("name2", cArr[4]));
REQUIRE(0 == strcmp("value2", cArr[5]));
REQUIRE(0 == strcmp("name0", cArr[0].name));
REQUIRE(0 == strcmp("value0", cArr[0].value));
REQUIRE(0 == strcmp("name1", cArr[1].name));
REQUIRE(0 == strcmp("value1", cArr[1].value));
REQUIRE(0 == strcmp("name2", cArr[2].name));
REQUIRE(0 == strcmp("value2", cArr[2].value));
REQUIRE(nullptr == cArr[6]);
REQUIRE(nullptr == cArr[7]);
REQUIRE(nullptr == cArr[3].name);
REQUIRE(nullptr == cArr[3].value);
}
TEST_CASE("name_value_collection coll ctor", "[collections]")
@ -374,8 +374,8 @@ TEST_CASE("name_value_collection coll ctor", "[collections]")
auto cArr = nvc.c_arr();
for (size_t i=0; i<SZ; ++i) {
auto key = string(cArr[i*2+0]);
auto val = string(cArr[i*2+1]);
auto key = string(cArr[i].name);
auto val = string(cArr[i].value);
auto it = nvPairs.find(key);
if (it == nvPairs.end()) {
@ -407,8 +407,8 @@ TEST_CASE("name_value_collection insert", "[collections]")
auto cArr = nvc.c_arr();
for (size_t i=0; i<SZ; ++i) {
auto key = string(cArr[i*2+0]);
auto val = string(cArr[i*2+1]);
auto key = string(cArr[i].name);
auto val = string(cArr[i].value);
auto it = nvPairs.find(key);
if (it == nvPairs.end()) {