Skip to content

Commit

Permalink
Add custom message type support (#199)
Browse files Browse the repository at this point in the history
feat(message-type): add custom message type support

Add custom message type support for the following APIs: publish, signal, share file, subscribe
and history.

feat(ipv6): add connectivity setter

Add `pubnub_set_ipv4_connectivity` and `pubnub_set_ipv6_connectivity` to `pubnub_coreapi` to switch
preferred connectivity protocol.

fix(proxy): add proxy context reset on connection close

Make sure that in case of connection close (including because of error) proxy context object will
be reset.

fix(subscribe): fix subscribe v2 decrypt issue

Fix issue because of which library with enabled but not configured crypto didn't return message
from `pubnub_get_v2`.
  • Loading branch information
parfeon authored Nov 25, 2024
1 parent 538879c commit eb84d56
Show file tree
Hide file tree
Showing 38 changed files with 448 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e

INFO_BG="\033[0m\033[48;2;5;49;70m"
INFO_FG="\033[38;2;19;181;255m"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/bin/bash
set -e

if [ -d "/usr/lib/x86_64-linux-gnu/" ]; then
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
fi


echo "::group::Run unit tests ('$1' $CC / $CXX)"
cd "$GITHUB_WORKSPACE/core"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/composite/unit-test-framework/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ runs:
steps:
- name: Build and Cache Unit Test framework ('${{ inputs.os }}' ${{ inputs.compilers }})
id: unit-test-framework
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: |
cgreen
Expand All @@ -27,7 +27,7 @@ runs:
${{ inputs.os }}-cgreen-${{ inputs.version }}-
- name: Checkout Unit Test framework
if: steps.unit-test-framework.outputs.cache-hit != 'true'
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: cgreen-devs/cgreen
ref: ${{ matrix.cgreen }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ jobs:
# group: organization/macos-gh
steps:
- name: Checkout project
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
token: ${{ secrets.GH_TOKEN }}
- name: Checkout actions
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: pubnub/client-engineering-deployment-tools
ref: v1
Expand Down
25 changes: 17 additions & 8 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
name: c-core
schema: 1
version: "4.14.1"
version: "4.15.0"
scm: github.com/pubnub/c-core
changelog:
- date: 2024-11-25
version: v4.15.0
changes:
- type: feature
text: "Add custom message type support for the following APIs: publish, signal, share file, subscribe and history."
- type: feature
text: "Add `pubnub_set_ipv4_connectivity` and `pubnub_set_ipv6_connectivity` to `pubnub_coreapi` to switch preferred connectivity protocol."
- type: bug
text: "Make sure that in case of connection close (including because of error) proxy context object will be reset."
- date: 2024-10-24
version: v4.14.1
changes:
Expand Down Expand Up @@ -875,7 +884,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.14.1
location: https://github.com/pubnub/c-core/releases/tag/v4.15.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -941,7 +950,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.14.1
location: https://github.com/pubnub/c-core/releases/tag/v4.15.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1007,7 +1016,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.14.1
location: https://github.com/pubnub/c-core/releases/tag/v4.15.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1069,7 +1078,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.14.1
location: https://github.com/pubnub/c-core/releases/tag/v4.15.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1130,7 +1139,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.14.1
location: https://github.com/pubnub/c-core/releases/tag/v4.15.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1186,7 +1195,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.14.1
location: https://github.com/pubnub/c-core/releases/tag/v4.15.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1239,7 +1248,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.14.1
location: https://github.com/pubnub/c-core/releases/tag/v4.15.0
requires:
-
name: "miniz"
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## v4.15.0
November 25 2024

#### Added
- Add custom message type support for the following APIs: publish, signal, share file, subscribe and history.
- Add `pubnub_set_ipv4_connectivity` and `pubnub_set_ipv6_connectivity` to `pubnub_coreapi` to switch preferred connectivity protocol.

#### Fixed
- Make sure that in case of connection close (including because of error) proxy context object will be reset.

## v4.14.1
October 24 2024

Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ RUN apt-get install -y cmake g++ ruby ruby-dev git ninja-build libboost-all-dev
RUN git clone https://github.com/cgreen-devs/cgreen.git
RUN cd cgreen && git checkout 1.4.1 && make

FROM ubuntu:20.04
FROM ubuntu:20.04
ARG DEBIAN_FRONTEND=noninteractive
WORKDIR /home

ENV GMOCK_VER=1.7.0
ENV GMOCK_VER=1.7.0
ENV CMAKE_CXX_COMPILER=/usr/bin/g++

RUN apt-get update
Expand Down Expand Up @@ -83,7 +83,7 @@ RUN if [ -z "$MOCK_SERVER_DOCKER" ]; then \
-Icucumber-cpp/include -Icucumber-cpp/build/src/ -Iposix -Icore -I. -Icpp \
-D PUBNUB_CRYPTO_API=1 -D PUBNUB_USE_SSL=0 -D MOCK_SERVER_DOCKER; \
fi


RUN g++ -o steps BoostSteps.o cpp/pubnub_sync.a cucumber-cpp/build/src/libcucumber-cpp.a \
-Lboost -lboost_unit_test_framework -lpthread -lboost_regex \
Expand Down
36 changes: 33 additions & 3 deletions core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pubnub_timer_list_unittest: pubnub_timer_list.c pubnub_timer_list_unit_test.c
#$(GCOVR) -r . --html --html-details -o coverage.html


PROXY_PROJECT_SOURCEFILES = pubnub_proxy_core.c pubnub_proxy.c pbhttp_digest.c pbntlm_core.c pbntlm_packer_std.c pubnub_generate_uuid_v4_random_std.c ../lib/pubnub_parse_ipv4_addr.c ../lib/pubnub_parse_ipv6_addr.c ../lib/md5/md5.c
PROXY_PROJECT_SOURCEFILES = pubnub_proxy_core.c pubnub_proxy.c pbhttp_digest.c pbntlm_core.c pbntlm_packer_std.c pubnub_generate_uuid_v4_random_std.c pubnub_dns_servers.c ../lib/pubnub_parse_ipv4_addr.c ../lib/pubnub_parse_ipv6_addr.c ../lib/md5/md5.c

pubnub_proxy_unittest: $(PROJECT_SOURCEFILES) $(PROXY_PROJECT_SOURCEFILES) pubnub_proxy_unit_test.c
gcc -o pubnub_proxy_unit_test.so -shared $(CFLAGS) $(LDFLAGS) -D PUBNUB_CALLBACK_API -D PUBNUB_PROXY_API=1 -Wall $(COVERAGE_FLAGS) -fPIC $(PROJECT_SOURCEFILES) $(PROXY_PROJECT_SOURCEFILES) pubnub_proxy_unit_test.c -lcgreen -lm
Expand All @@ -87,17 +87,47 @@ CRYPTO_INCLUDES += -I ../openssl/.
CRYPTO_LIBS += -lssl -lcrypto
CRYPTO_SOURCEFILES += pubnub_crypto.c pbcc_crypto.c pbcc_crypto_aes_cbc.c pbcc_crypto_legacy.c ../openssl/pbaes256.c


OS := $(shell uname)
ifeq ($(OS),Darwin)
# Ensure OpenSSL paths are set.
OPENSSL_CFLAGS := $(shell pkg-config --cflags openssl 2>/dev/null)
OPENSSL_LIBS := $(shell pkg-config --libs openssl 2>/dev/null)
ifneq ($(OPENSSL_CFLAGS),)
CRYPTO_INCLUDES += $(OPENSSL_CFLAGS)
endif
ifneq ($(OPENSSL_LIBS),)
CRYPTO_LIBS += $(OPENSSL_LIBS)
endif
else
ifeq ($(shell test -d "/usr/local/opt/openssl" && echo yes || echo no),yes)
CRYPTO_INCLUDES += -I/usr/local/opt/openssl/include
else
# Path on GitHub Action Runner (ubuntu-latest image)
ifeq ($(shell test -d "/usr/include/openssl" && echo yes || echo no),yes)
CRYPTO_INCLUDES += -I/usr/include
endif
endif
# Path on GitHub Action Runner (ubuntu-latest image)
ifeq ($(shell test -d "/usr/lib/x86_64-linux-gnu" && echo yes || echo no),yes)
CRYPTO_LIBS += -L/usr/lib/x86_64-linux-gnu -Wl,-Bdynamic
endif
endif


pbcc_crypto_unittest: $(PROJECT_SOURCEFILES) $(CRYPTO_SOURCEFILES) pbcc_crypto_unit_tests.c
gcc -o pbcc_crypto_unit_test.so -shared $(CFLAGS) $(LDFLAGS) $(CRYPTO_INCLUDES) $(CRYPTO_LIBS) -D PUBNUB_CRYPTO_API=1 -Wall $(COVERAGE_FLAGS) -fPIC $(PROJECT_SOURCEFILES) $(CRYPTO_SOURCEFILES) test/pubnub_test_mocks.c pbcc_crypto_unit_tests.c -lcgreen -lm
gcc -o pbcc_crypto_unit_test.so -shared $(CFLAGS) $(LDFLAGS) $(CRYPTO_INCLUDES) -D PUBNUB_CRYPTO_API=1 -Wall $(COVERAGE_FLAGS) -fPIC $(PROJECT_SOURCEFILES) $(CRYPTO_SOURCEFILES) test/pubnub_test_mocks.c pbcc_crypto_unit_tests.c $(CRYPTO_LIBS) -lcgreen -lm
# gcc -o pubnub_core_unit_testo $(CFLAGS) -Wall $(COVERAGE_FLAGS) $(PROJECT_SOURCEFILES) pbcc_crypto_unit_tests.c -lcgreen -lm
#ldd pbcc_crypto_unit_test.so
$(CGREEN_RUNNER) ./pbcc_crypto_unit_test.so
#$(GCOVR) -r . --html --html-details -o coverage.html

SUBSCRIBE_V2_SOURCEFILES += pubnub_subscribe_v2.c pbcc_subscribe_v2.c

pubnub_crypto_unittest: $(PROJECT_SOURCEFILES) $(CRYPTO_SOURCEFILES) $(SUBSCRIBE_V2_SOURCEFILES) pubnub_crypto_unit_tests.c
gcc -o pubnub_crypto_unit_test.so -shared $(CFLAGS) $(LDFLAGS) $(CRYPTO_INCLUDES) $(CRYPTO_LIBS) -D PUBNUB_CRYPTO_API=1 -D PUBNUB_USE_SUBSCRIBE_V2=1 -Wall $(COVERAGE_FLAGS) -fPIC $(PROJECT_SOURCEFILES) $(CRYPTO_SOURCEFILES) $(SUBSCRIBE_V2_SOURCEFILES) test/pubnub_test_mocks.c pubnub_crypto_unit_tests.c -lcgreen -lm
gcc -o pubnub_crypto_unit_test.so -shared $(CFLAGS) $(LDFLAGS) $(CRYPTO_INCLUDES) -D PUBNUB_CRYPTO_API=1 -D PUBNUB_USE_SUBSCRIBE_V2=1 -Wall $(COVERAGE_FLAGS) -fPIC $(PROJECT_SOURCEFILES) $(CRYPTO_SOURCEFILES) $(SUBSCRIBE_V2_SOURCEFILES) test/pubnub_test_mocks.c pubnub_crypto_unit_tests.c $(CRYPTO_LIBS) -lcgreen -lm
# gcc -o pubnub_core_unit_testo $(CFLAGS) -Wall $(COVERAGE_FLAGS) $(PROJECT_SOURCEFILES) pubnub_crypto_unit_tests.c -lcgreen -lm
#ldd pubnub_crypto_unit_test.so
$(CGREEN_RUNNER) ./pubnub_crypto_unit_test.so


Expand Down
4 changes: 3 additions & 1 deletion core/pbcc_fetch_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum pubnub_res pbcc_fetch_history_prep(struct pbcc_context* pb,
const char* channel,
unsigned int max_per_channel,
enum pubnub_tribool include_meta,
enum pubnub_tribool include_custom_message_type,
enum pubnub_tribool include_message_type,
enum pubnub_tribool include_user_id,
enum pubnub_tribool include_message_actions,
Expand Down Expand Up @@ -59,7 +60,8 @@ enum pubnub_res pbcc_fetch_history_prep(struct pbcc_context* pb,
if (max_per_channel) { ADD_URL_PARAM(qparam, max, max_per_ch_cnt_buf); }

if (include_meta != pbccNotSet) { ADD_URL_PARAM(qparam, include_meta, include_meta == pbccTrue ? "true" : "false"); }
if (include_message_type != pbccNotSet) { ADD_URL_PARAM(qparam, include_message_type, include_meta == pbccTrue ? "true" : "false"); }
if (include_custom_message_type != pbccNotSet) { ADD_URL_PARAM(qparam, include_custom_message_type, include_custom_message_type == pbccTrue ? "true" : "false"); }
if (include_message_type != pbccNotSet) { ADD_URL_PARAM(qparam, include_message_type, include_message_type == pbccTrue ? "true" : "false"); }
if (include_user_id != pbccNotSet) { ADD_URL_PARAM(qparam, include_uuid, include_user_id == pbccTrue ? "true" : "false"); }
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
Expand Down
1 change: 1 addition & 0 deletions core/pbcc_fetch_history.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum pubnub_res pbcc_fetch_history_prep(struct pbcc_context* pb,
const char* channel,
unsigned int max_per_channel,
enum pubnub_tribool include_meta,
enum pubnub_tribool include_custom_message_type,
enum pubnub_tribool include_message_type,
enum pubnub_tribool include_user_id,
enum pubnub_tribool include_message_actions,
Expand Down
10 changes: 9 additions & 1 deletion core/pbcc_subscribe_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
enum pubnub_res pbcc_subscribe_v2_prep(struct pbcc_context* p,
char const* channel,
char const* channel_group,
unsigned* heartbeat,
const unsigned* heartbeat,
char const* filter_expr)
{
char region_str[20];
Expand Down Expand Up @@ -254,6 +254,9 @@ struct pubnub_v2_message pbcc_get_msg_v2(struct pbcc_context* p)
found.end - found.start,
&rslt.payload.size
);
} else {
rslt.payload.ptr = (char*)found.start;
rslt.payload.size = found.end - found.start;
}
#else
rslt.payload.ptr = (char*)found.start;
Expand Down Expand Up @@ -303,6 +306,11 @@ struct pubnub_v2_message pbcc_get_msg_v2(struct pbcc_context* p)
rslt.message_type = pbsbPublished;
}

if (jonmpOK == pbjson_get_object_value(&el, "cmt", &found)) {
rslt.custom_message_type.ptr = (char*)found.start + 1;
rslt.custom_message_type.size = found.end - found.start - 2;
}

jpresult = pbjson_get_object_value(&el, "p", &found);
if (jonmpOK == jpresult) {
struct pbjson_elem titel;
Expand Down
2 changes: 1 addition & 1 deletion core/pbcc_subscribe_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct pbcc_context;
enum pubnub_res pbcc_subscribe_v2_prep(struct pbcc_context* p,
char const* channel,
char const* channel_group,
unsigned* heartbeat,
const unsigned* heartbeat,
char const* filter_expr);


Expand Down
14 changes: 11 additions & 3 deletions core/pubnub_ccore_pubsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ enum pubnub_res pbcc_append_url_param_encoded(struct pbcc_context* pb,
enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
const char* channel,
const char* message,
const char* custom_message_type,
bool store_in_history,
bool norep,
char const* meta,
Expand Down Expand Up @@ -596,7 +597,7 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
ADD_URL_PARAM(qparam, uuid, user_id);
if (ttl != SIZE_MAX) { ADD_URL_PARAM_SIZET(qparam, ttl, ttl); }
if (ttl != 0 && ttl != SIZE_MAX) { ADD_URL_PARAM_SIZET(qparam, ttl, ttl); }
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
ADD_TS_TO_URL_PARAM();
Expand All @@ -606,6 +607,9 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
if (!store_in_history) { ADD_URL_PARAM(qparam, store, "0"); }
if (norep) { ADD_URL_PARAM(qparam, norep, "true"); }
if (meta) { ADD_URL_PARAM(qparam, meta, meta); }
if (custom_message_type) {
ADD_URL_PARAM(qparam, custom_message_type, custom_message_type);
}

#if PUBNUB_CRYPTO_API
SORT_URL_PARAMETERS(qparam);
Expand Down Expand Up @@ -667,7 +671,8 @@ enum pubnub_res pbcc_sign_url(struct pbcc_context* pc, const char* msg, enum pub

enum pubnub_res pbcc_signal_prep(struct pbcc_context* pb,
const char* channel,
const char* message)
const char* message,
const char* custom_message_type)
{
enum pubnub_res rslt = PNR_OK;
char const* const uname = pubnub_uname();
Expand All @@ -689,6 +694,9 @@ enum pubnub_res pbcc_signal_prep(struct pbcc_context* pb,
URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
if (user_id) { ADD_URL_PARAM(qparam, uuid, user_id); }
if (custom_message_type) {
ADD_URL_PARAM(qparam, custom_message_type, custom_message_type);
}
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
ADD_TS_TO_URL_PARAM();
Expand Down Expand Up @@ -716,7 +724,7 @@ enum pubnub_res pbcc_signal_prep(struct pbcc_context* pb,
enum pubnub_res pbcc_subscribe_prep(struct pbcc_context* p,
char const* channel,
char const* channel_group,
unsigned* heartbeat)
const unsigned* heartbeat)
{
char const* user_id = pbcc_user_id_get(p);
char const* const uname = pubnub_uname();
Expand Down
6 changes: 4 additions & 2 deletions core/pubnub_ccore_pubsub.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ void pbcc_via_post_headers(struct pbcc_context* p, char* header, size_t max_leng
enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
const char* channel,
const char* message,
const char* custom_message_type,
bool store_in_history,
bool norep,
char const* meta,
Expand All @@ -488,15 +489,16 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
*/
enum pubnub_res pbcc_signal_prep(struct pbcc_context* pb,
const char* channel,
const char* message);
const char* message,
const char* custom_message_type);

/** Prepares the Subscribe operation (transaction), mostly by
formatting the URI of the HTTP request.
*/
enum pubnub_res pbcc_subscribe_prep(struct pbcc_context* p,
char const* channel,
char const* channel_group,
unsigned* heartbeat);
const unsigned* heartbeat);


/** Split @p buf string containing a JSON array (with arbitrary
Expand Down
17 changes: 17 additions & 0 deletions core/pubnub_coreapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,20 @@ int pubnub_last_http_response_body(pubnub_t* pb, pubnub_chamebl_t* o_msg)
pubnub_mutex_unlock(pb->monitor);
return 0;
}

#if PUBNUB_USE_IPV6
void pubnub_set_ipv4_connectivity(pubnub_t *p)
{
pubnub_mutex_lock(p->monitor);
p->options.ipv6_connectivity = false;
pubnub_mutex_unlock(p->monitor);
}

void pubnub_set_ipv6_connectivity(pubnub_t *p)
{
pubnub_mutex_lock(p->monitor);
p->options.ipv6_connectivity = true;
pubnub_mutex_unlock(p->monitor);
}
#endif /* PUBNUB_USE_IPV6 */

Loading

0 comments on commit eb84d56

Please sign in to comment.