Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing ttl parameter #192

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
name: c-core
schema: 1
version: "4.12.2"
version: "4.12.3"
scm: github.com/pubnub/c-core
changelog:
- date: 2024-08-05
version: v4.12.3
changes:
- type: bug
text: "Add the missing `ttl` parameter to the `pubnub_publish_options` for extended `publish` configuration."
- date: 2024-08-05
version: v4.12.2
changes:
Expand Down Expand Up @@ -838,7 +843,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.12.2
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
requires:
-
name: "miniz"
Expand Down Expand Up @@ -904,7 +909,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.12.2
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
requires:
-
name: "miniz"
Expand Down Expand Up @@ -970,7 +975,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.12.2
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1032,7 +1037,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.12.2
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1093,7 +1098,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.12.2
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1149,7 +1154,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.12.2
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1202,7 +1207,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.12.2
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
requires:
-
name: "miniz"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v4.12.3
August 05 2024

#### Fixed
- Add the missing `ttl` parameter to the `pubnub_publish_options` for extended `publish` configuration.

## v4.12.2
August 05 2024

Expand Down
38 changes: 21 additions & 17 deletions core/pubnub_ccore_pubsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,18 @@ enum pubnub_res pbcc_append_url_param_encoded(struct pbcc_context* pb,
return pbcc_url_encode(pb, param_val, pt);
}


enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
const char* channel,
const char* message,
bool store_in_history,
bool norep,
char const* meta,
const size_t ttl,
enum pubnub_method method)
{
char const* const uname = pubnub_uname();
char const* user_id = pbcc_user_id_get(pb);
enum pubnub_res rslt = PNR_OK;
char const* const uname = pubnub_uname();
char const* user_id = pbcc_user_id_get(pb);
enum pubnub_res rslt = PNR_OK;

PUBNUB_ASSERT_OPT(user_id != NULL);
PUBNUB_ASSERT_OPT(message != NULL);
Expand All @@ -526,10 +526,11 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
#if PUBNUB_CRYPTO_API
if (NULL != pb->crypto_module) {
pubnub_bymebl_t message_block;
message_block.ptr = (uint8_t*)message;
message_block.ptr = (uint8_t*)message;
message_block.size = strlen(message);

pubnub_bymebl_t encrypted = pb->crypto_module->encrypt(pb->crypto_module, message_block);
pubnub_bymebl_t encrypted =
pb->crypto_module->encrypt(pb->crypto_module, message_block);

if (NULL == encrypted.ptr) {
PUBNUB_LOG_ERROR("pbcc_publish_prep(pbcc=%p) - encryption failed\n", pb);
Expand All @@ -541,15 +542,19 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
message = pbcc_base64_encode(encrypted);

free(encrypted.ptr);

if (NULL == message) {
PUBNUB_LOG_ERROR("pbcc_publish_prep(pbcc=%p) - base64 encoding failed\n", pb);
PUBNUB_LOG_ERROR(
"pbcc_publish_prep(pbcc=%p) - base64 encoding failed\n", pb);
return PNR_INTERNAL_ERROR;
}

char* quoted_message = (char*)malloc(strlen(message) + 3); // quotes + null-terminator
char* quoted_message =
(char*)malloc(strlen(message) + 3); // quotes + null-terminator
if (NULL == quoted_message) {
PUBNUB_LOG_ERROR("pbcc_publish_prep(pbcc=%p) - failed to allocate memory for quoted message\n", pb);
PUBNUB_LOG_ERROR("pbcc_publish_prep(pbcc=%p) - failed to allocate "
"memory for quoted message\n",
pb);
free((void*)message);
return PNR_OUT_OF_MEMORY;
}
Expand All @@ -566,7 +571,8 @@ 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); }
if (user_id) { ADD_URL_PARAM(qparam, uuid, user_id); }
ADD_URL_PARAM(qparam, uuid, user_id);
if (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 @@ -578,14 +584,15 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
if (meta) { ADD_URL_PARAM(qparam, meta, meta); }

#if PUBNUB_CRYPTO_API
SORT_URL_PARAMETERS(qparam);
SORT_URL_PARAMETERS(qparam);
#endif
ENCODE_URL_PARAMETERS(pb, qparam);
#if PUBNUB_CRYPTO_API
if (pb->secret_key != NULL) {
rslt = pbcc_sign_url(pb, message, method, false);
if (rslt != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_sign_url failed. pb=%p, message=%s, method=%d", pb, message, method);
PUBNUB_LOG_ERROR(
"pbcc_sign_url failed. pb=%p, message=%s, method=%d", pb, message, method);
}
}
#endif
Expand All @@ -595,10 +602,7 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
}
PUBNUB_LOG_DEBUG("pbcc_publish_prep. REQUEST =%s\n", pb->http_buf);
#if PUBNUB_CRYPTO_API
if (NULL != pb->crypto_module)
{
free((void*) message);
}
if (NULL != pb->crypto_module) { free((void*)message); }
#endif

return (rslt != PNR_OK) ? rslt : PNR_STARTED;
Expand Down
5 changes: 3 additions & 2 deletions core/pubnub_ccore_pubsub.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ struct pbcc_context {
else if (pb->auth != NULL) { ADD_URL_PARAM(name, key, pb->auth); }

#define ADD_URL_PARAM_SIZET(name, key, value) \
int val_len = snprintf(NULL, 0, "%lu", (long unsigned int)value); \
int val_len = snprintf(NULL, 0, "%lu", (long unsigned int)value); \
if (val_len >= 21) { \
PUBNUB_LOG_ERROR("Error: in ADD_URL_PARAM_SIZET:\n"); \
return PNR_TX_BUFF_TOO_SMALL; \
} \
snprintf(name##_val_str, val_len + 1, "%lu", (long unsigned int)value); \
snprintf(name##_val_str, val_len + 1, "%lu", (long unsigned int)value); \
name[(int)(name##_index)].param_key = #key; \
name[(int)(name##_index)].key_size = sizeof(#key)-1; \
name[(int)(name##_index)].param_val = name##_val_str; \
Expand Down Expand Up @@ -457,6 +457,7 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb,
bool store_in_history,
bool norep,
char const* meta,
size_t ttl,
enum pubnub_method method);

/** Prepares the Signal operation (transaction), mostly by
Expand Down
26 changes: 19 additions & 7 deletions core/pubnub_coreapi_ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct pubnub_publish_options pubnub_publish_defopts(void)
result.replicate = true;
result.meta = NULL;
result.method = pubnubSendViaGET;
result.ttl = SIZE_MAX;
return result;
}

Expand All @@ -54,11 +55,15 @@ enum pubnub_res pubnub_publish_ex(pubnub_t* pb,
}
#if PUBNUB_CRYPTO_API
if (NULL != pb->core.crypto_module && NULL != opts.cipher_key) {
PUBNUB_LOG_WARNING("pubnub_publish_ex(pb=%p).cipher_key called while client crypto module is set! Using crypto module instead!\n", pb);
} else if (NULL != opts.cipher_key) {
PUBNUB_LOG_WARNING(
"pubnub_publish_ex(pb=%p).cipher_key called while client crypto "
"module is set! Using crypto module instead!\n",
pb);
}
else if (NULL != opts.cipher_key) {
pubnub_bymebl_t to_encrypt;
char* encrypted_msg = pb->core.encrypted_msg_buf;
size_t n = sizeof pb->core.encrypted_msg_buf - sizeof("\"\"");
char* encrypted_msg = pb->core.encrypted_msg_buf;
size_t n = sizeof pb->core.encrypted_msg_buf - sizeof("\"\"");

to_encrypt.ptr = (uint8_t*)message;
to_encrypt.size = strlen(message);
Expand All @@ -73,11 +78,18 @@ enum pubnub_res pubnub_publish_ex(pubnub_t* pb,
#endif
#if PUBNUB_USE_GZIP_COMPRESSION
if (pubnubSendViaPOSTwithGZIP == opts.method) {
message = (pbgzip_compress(pb, message) == PNR_OK) ? pb->core.gzip_msg_buf : message;
message = (pbgzip_compress(pb, message) == PNR_OK) ? pb->core.gzip_msg_buf
: message;
}
#endif
rslt = pbcc_publish_prep(
&pb->core, channel, message, opts.store, !opts.replicate, opts.meta, opts.method);
rslt = pbcc_publish_prep(&pb->core,
channel,
message,
opts.store,
!opts.replicate,
opts.meta,
opts.ttl,
opts.method);
if (PNR_STARTED == rslt) {
pb->trans = PBTT_PUBLISH;
pb->core.last_result = PNR_STARTED;
Expand Down
6 changes: 5 additions & 1 deletion core/pubnub_coreapi_ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ struct pubnub_publish_options {
*/
char const* meta;
/** Defines the method by which publish transaction will be performed */
enum pubnub_method method;
enum pubnub_method method;
/** For how many hours message should be kept and available with history
API.
*/
size_t ttl;
};

/** This returns the default options for publish V1 transactions.
Expand Down
2 changes: 1 addition & 1 deletion core/pubnub_objects_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ enum pubnub_res pubnub_set_uuidmetadata_ex(pubnub_t* pb,
offset > 1 ? "," : "",
opts.data.profile_url);
}
snprintf(obj_buffer + offset, obj_len - offset, "}\0");
snprintf(obj_buffer + offset, obj_len - offset, "}");

result = pubnub_set_uuidmetadata(pb, opts.uuid, opts.include, obj_buffer);
free(obj_buffer);
Expand Down
3 changes: 2 additions & 1 deletion core/pubnub_pubsubapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ enum pubnub_res pubnub_publish(pubnub_t* pb, const char* channel, const char* me
return PNR_IN_PROGRESS;
}

rslt = pbcc_publish_prep(&pb->core, channel, message, true, false, NULL, pubnubSendViaGET);
rslt = pbcc_publish_prep(
&pb->core, channel, message, true, false, NULL, SIZE_MAX, pubnubSendViaGET);
if (PNR_STARTED == rslt) {
pb->trans = PBTT_PUBLISH;
pb->core.last_result = PNR_STARTED;
Expand Down
2 changes: 1 addition & 1 deletion core/pubnub_version_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define INC_PUBNUB_VERSION_INTERNAL


#define PUBNUB_SDK_VERSION "4.12.2"
#define PUBNUB_SDK_VERSION "4.12.3"


#endif /* !defined INC_PUBNUB_VERSION_INTERNAL */
27 changes: 27 additions & 0 deletions core/samples/pubnub_publish_via_post_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,33 @@ int main()
pubnub_res_2_string(res));
}

puts("=========================================");
puts("Publishing(via post with ttl)...");
time(&t0);
struct pubnub_publish_options options = publish_opts_method(pubnubSendViaPOSTwithGZIP);
options.ttl = 16;
res = pubnub_publish_ex(pbp,
chan,
"\"Hello world!\"",
options);
if (PNR_STARTED == res) {
res = pubnub_await(pbp);
}
printf("Publish 'via post with ttl' lasted %lf seconds.\n", difftime(time(NULL), t0));
if (PNR_OK == res) {
printf("Published! Response from Pubnub: %s\n",
pubnub_last_publish_result(pbp));
}
else if (PNR_PUBLISH_FAILED == res) {
printf("Publishing failed on Pubnub, description: %s\n",
pubnub_last_publish_result(pbp));
}
else {
printf("Publishing failed with code: %d('%s')\n",
res,
pubnub_res_2_string(res));
}

puts("=========================================");
/* Publishing via post with gzip */
puts("Publishing(via post with gzip)...");
Expand Down
Loading