Commit 8af92c96 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

refactoring function ngx_http_push_stream_free_message_memory_locked and...

refactoring function ngx_http_push_stream_free_message_memory_locked and adding function ngx_http_push_stream_create_str to be easy create an ngx_str_t
parent c9511c49
......@@ -227,6 +227,7 @@ static void ngx_http_push_stream_timer_reset(ngx_msec_t timer_in
static void ngx_http_push_stream_worker_subscriber_cleanup_locked(ngx_http_push_stream_worker_subscriber_t *worker_subscriber);
u_char * ngx_http_push_stream_append_crlf(const ngx_str_t *str, ngx_pool_t *pool);
static ngx_str_t * ngx_http_push_stream_create_str(ngx_pool_t *pool, uint len);
static void ngx_http_push_stream_mark_message_to_delete_locked(ngx_http_push_stream_msg_t *msg);
static void ngx_http_push_stream_delete_channel(ngx_str_t *id, ngx_pool_t *temp_pool);
......
......@@ -47,14 +47,11 @@ ngx_http_push_stream_get_channel_id(ngx_http_request_t *r, ngx_http_push_stream_
return NGX_HTTP_PUSH_STREAM_TOO_LARGE_CHANNEL_ID;
}
if ((id = ngx_pcalloc(r->pool, sizeof(ngx_str_t) + vv->len + 1)) == NULL) {
if ((id = ngx_http_push_stream_create_str(r->pool, vv->len)) == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory for $push_stream_channel_id string");
return NULL;
}
id->data = (u_char *) (id + 1);
id->len = vv->len;
ngx_memset(id->data, '\0', vv->len + 1);
ngx_memcpy(id->data, vv->data, vv->len);
return id;
......
......@@ -312,8 +312,7 @@ ngx_http_push_stream_parse_channels_ids_from_path(ngx_http_request_t *r, ngx_poo
return NULL;
}
// make channels_path one unit larger than vv_channels_path to have allways a \0 in the end
if ((channels_path = ngx_pcalloc(pool, sizeof(ngx_str_t) + vv_channels_path->len + 1)) == NULL) {
if ((channels_path = ngx_http_push_stream_create_str(pool, vv_channels_path->len)) == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory for channels_path string");
return NULL;
}
......@@ -323,9 +322,6 @@ ngx_http_push_stream_parse_channels_ids_from_path(ngx_http_request_t *r, ngx_poo
return NULL;
}
channels_path->data = (u_char *) (channels_path + 1);
channels_path->len = vv_channels_path->len;
ngx_memset(channels_path->data, '\0', vv_channels_path->len + 1);
ngx_memcpy(channels_path->data, vv_channels_path->data, vv_channels_path->len);
ngx_queue_init(&channels_ids->queue);
......@@ -369,13 +365,10 @@ ngx_http_push_stream_parse_channels_ids_from_path(ngx_http_request_t *r, ngx_poo
return NULL;
}
if ((cur->id = ngx_pcalloc(pool, sizeof(ngx_str_t) + len + 1)) == NULL) {
if ((cur->id = ngx_http_push_stream_create_str(pool, len)) == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory for channel_id string");
return NULL;
}
cur->id->data = (u_char *) (cur->id + 1);
cur->id->len = len;
ngx_memset(cur->id->data, '\0', len + 1);
ngx_memcpy(cur->id->data, channel_pos, len);
cur->backtrack_messages = (backtrack_messages > 0) ? backtrack_messages : 0;
......
......@@ -147,16 +147,17 @@ ngx_http_push_stream_convert_char_to_msg_on_shared_locked(u_char *data, size_t l
return NULL;
}
msg->formatted_messages = NULL;
msg->buf = ngx_slab_alloc_locked(shpool, sizeof(ngx_buf_t));
if (msg->buf == NULL) {
ngx_slab_free_locked(shpool, msg);
ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL;
}
msg->buf->start = ngx_slab_alloc_locked(shpool, len + 1);
if (msg->buf->start == NULL) {
ngx_slab_free_locked(shpool, msg->buf);
ngx_slab_free_locked(shpool, msg);
ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL;
}
......@@ -501,20 +502,20 @@ static void
ngx_http_push_stream_free_message_memory_locked(ngx_slab_pool_t *shpool, ngx_http_push_stream_msg_t *msg) {
u_int i;
for(i = 0; i < ngx_http_push_stream_module_main_conf->qtd_templates; i++) {
ngx_str_t *formmated = (msg->formatted_messages + i);
if ((formmated != NULL) && (formmated->data != NULL)) {
ngx_slab_free_locked(shpool, formmated->data);
if (msg->formatted_messages != NULL) {
for(i = 0; i < ngx_http_push_stream_module_main_conf->qtd_templates; i++) {
ngx_str_t *formmated = (msg->formatted_messages + i);
if ((formmated != NULL) && (formmated->data != NULL)) {
ngx_slab_free_locked(shpool, formmated->data);
}
}
}
if (msg->formatted_messages != NULL) {
ngx_slab_free_locked(shpool, msg->formatted_messages);
}
ngx_slab_free_locked(shpool, msg->buf->start);
ngx_slab_free_locked(shpool, msg->buf);
ngx_slab_free_locked(shpool, msg);
if ((msg->buf != NULL) && (msg->buf->start != NULL)) ngx_slab_free_locked(shpool, msg->buf->start);
if (msg->buf != NULL) ngx_slab_free_locked(shpool, msg->buf);
if (msg != NULL) ngx_slab_free_locked(shpool, msg);
}
......@@ -809,13 +810,10 @@ ngx_http_push_stream_get_formatted_current_time(ngx_pool_t *pool)
ngx_tm_t tm;
ngx_str_t *currenttime;
currenttime = (ngx_str_t *) ngx_pcalloc(pool, sizeof(ngx_str_t) + 20); //ISO 8601 pattern plus 1
currenttime = ngx_http_push_stream_create_str(pool, 19); //ISO 8601 pattern
if (currenttime != NULL) {
currenttime->data = (u_char *) (currenttime + 1);
ngx_memset(currenttime->data, '\0', 20);
ngx_gmtime(ngx_time(), &tm);
ngx_sprintf(currenttime->data, (char *) NGX_HTTP_PUSH_STREAM_DATE_FORMAT_ISO_8601.data, tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
currenttime->len = ngx_strlen(currenttime->data);
} else {
currenttime = &NGX_HTTP_PUSH_STREAM_EMPTY;
}
......@@ -828,12 +826,9 @@ ngx_http_push_stream_get_formatted_hostname(ngx_pool_t *pool)
{
ngx_str_t *hostname;
hostname = (ngx_str_t *) ngx_pcalloc(pool, sizeof(ngx_str_t) + ngx_cycle->hostname.len + 1); //hostname length plus 1
hostname = ngx_http_push_stream_create_str(pool, sizeof(ngx_str_t) + ngx_cycle->hostname.len);
if (hostname != NULL) {
hostname->data = (u_char *) (hostname + 1);
ngx_memset(hostname->data, '\0', ngx_cycle->hostname.len + 1);
ngx_memcpy(hostname->data, ngx_cycle->hostname.data, ngx_cycle->hostname.len);
hostname->len = ngx_strlen(hostname->data);
} else {
hostname = &NGX_HTTP_PUSH_STREAM_EMPTY;
}
......@@ -846,16 +841,25 @@ static ngx_str_t *
ngx_http_push_stream_get_formatted_chunk(const u_char *text, off_t len, ngx_pool_t *temp_pool)
{
ngx_str_t *chunk;
u_int max_len;
/* the "0000000000000000" is 64-bit hexadimal string */
max_len = sizeof("0000000000000000" CRLF CRLF CRLF) + len;
chunk = (ngx_str_t *) ngx_pcalloc(temp_pool, sizeof(ngx_str_t) + max_len);
chunk = ngx_http_push_stream_create_str(temp_pool, sizeof("0000000000000000" CRLF CRLF CRLF) + len);
if (chunk != NULL) {
chunk->data = (u_char *) (chunk + 1);
ngx_memset(chunk->data, '\0', max_len);
ngx_sprintf(chunk->data, "%xO" CRLF "%s" CRLF CRLF, len + sizeof(CRLF) - 1, text);
chunk->len = ngx_strlen(chunk->data);
}
return chunk;
}
static ngx_str_t *
ngx_http_push_stream_create_str(ngx_pool_t *pool, uint len)
{
ngx_str_t *aux = (ngx_str_t *) ngx_pcalloc(pool, sizeof(ngx_str_t) + len + 1);
if (aux != NULL) {
aux->data = (u_char *) (aux + 1);
aux->len = len;
ngx_memset(aux->data, '\0', len + 1);
}
return aux;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment