ESL_DECLARE(char *) esl_event_get_header_idx(esl_event_t *event, const char *header_name, int idx) { esl_event_header_t *hp; if ((hp = esl_event_get_header_ptr(event, header_name))) { if (idx > -1) { if (idx < hp->idx) { return hp->array[idx]; } else { return NULL; } } return hp->value; } return NULL; }
static esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, char *data) { esl_event_header_t *header = NULL; esl_ssize_t hlen = -1; int exists = 0, fly = 0; char *index_ptr; int index = 0; char *real_header_name = NULL; if ((index_ptr = strchr(header_name, '['))) { index_ptr++; index = atoi(index_ptr); real_header_name = DUP(header_name); if ((index_ptr = strchr(real_header_name, '['))) { *index_ptr++ = '\0'; } header_name = real_header_name; } if (index_ptr || (stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { if (!(header = esl_event_get_header_ptr(event, header_name)) && index_ptr) { header = new_header(header_name); if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)) { esl_event_del_header(event, header_name); } fly++; } if ((header = esl_event_get_header_ptr(event, header_name))) { if (index_ptr) { if (index > -1 && index <= 4000) { if (index < header->idx) { FREE(header->array[index]); header->array[index] = DUP(data); } else { int i; char **m; m = realloc(header->array, sizeof(char *) * (index + 1)); esl_assert(m); header->array = m; for (i = header->idx; i < index; i++) { m[i] = DUP(""); } m[index] = DUP(data); header->idx = index + 1; if (!fly) { exists = 1; } goto redraw; } } goto end; } else { if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { exists++; stack &= ~(ESL_STACK_TOP | ESL_STACK_BOTTOM); } else { header = NULL; } } } } if (!header) { if (esl_strlen_zero(data)) { esl_event_del_header(event, header_name); FREE(data); goto end; } if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)) { esl_event_del_header(event, header_name); } if (strstr(data, "ARRAY::")) { esl_event_add_array(event, header_name, data); FREE(data); goto end; } header = new_header(header_name); } if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { char **m = NULL; esl_size_t len = 0; char *hv; int i = 0, j = 0; if (header->value && !header->idx) { m = malloc(sizeof(char *)); esl_assert(m); m[0] = header->value; header->value = NULL; header->array = m; header->idx++; m = NULL; } i = header->idx + 1; m = realloc(header->array, sizeof(char *) * i); esl_assert(m); if ((stack & ESL_STACK_PUSH)) { m[header->idx] = data; } else if ((stack & ESL_STACK_UNSHIFT)) { for (j = header->idx; j > 0; j--) { m[j] = m[j-1]; } m[0] = data; } header->idx++; header->array = m; redraw: len = 0; for(j = 0; j < header->idx; j++) { len += strlen(header->array[j]) + 2; } if (len) { len += 8; hv = realloc(header->value, len); esl_assert(hv); header->value = hv; esl_snprintf(header->value, len, "ARRAY::"); for(j = 0; j < header->idx; j++) { esl_snprintf(header->value + strlen(header->value), len - strlen(header->value), "%s%s", j == 0 ? "" : "|:", header->array[j]); } } } else { header->value = data; } if (!exists) { header->hash = esl_ci_hashfunc_default(header->name, &hlen); if ((stack & ESL_STACK_TOP)) { header->next = event->headers; event->headers = header; if (!event->last_header) { event->last_header = header; } } else { if (event->last_header) { event->last_header->next = header; } else { event->headers = header; header->next = NULL; } event->last_header = header; } } end: esl_safe_free(real_header_name); return ESL_SUCCESS; }
static esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, char *data) { esl_event_header_t *header = NULL; esl_ssize_t hlen = -1; int exists = 0; if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT) || esl_test_flag(event, ESL_EF_CONTAINS_ARRAYS)) { if ((header = esl_event_get_header_ptr(event, header_name))) { if (!(stack & ESL_STACK_PUSH) && !(stack & ESL_STACK_UNSHIFT) && header->idx) { stack |= ESL_STACK_PUSH; } if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { exists++; stack &= ~(ESL_STACK_TOP | ESL_STACK_BOTTOM); } else { header = NULL; } } } if (!header) { #ifdef ESL_EVENT_RECYCLE void *pop; if (esl_queue_trypop(EVENT_HEADER_RECYCLE_QUEUE, &pop) == ESL_SUCCESS) { header = (esl_event_header_t *) pop; } else { #endif header = ALLOC(sizeof(*header)); esl_assert(header); #ifdef ESL_EVENT_RECYCLE } #endif if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)) { esl_event_del_header(event, header_name); } memset(header, 0, sizeof(*header)); header->name = DUP(header_name); } if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { char **m = NULL; esl_size_t len = 0; char *hv; int i = 0, j = 0; esl_set_flag(event, ESL_EF_CONTAINS_ARRAYS); if (header->value && !header->idx) { m = malloc(sizeof(char *)); esl_assert(m); m[0] = header->value; header->value = NULL; header->array = m; header->idx++; m = NULL; } i = header->idx + 1; m = realloc(header->array, sizeof(char *) * i); esl_assert(m); if ((stack & ESL_STACK_PUSH)) { m[header->idx] = data; } else if ((stack & ESL_STACK_UNSHIFT)) { for (j = header->idx; j > 0; j--) { m[j] = m[j-1]; } m[0] = data; } header->idx++; header->array = m; for(j = 0; j < header->idx; j++) { len += strlen(header->array[j]) + 2; } if (len) { len += 8; hv = realloc(header->value, len); esl_assert(hv); header->value = hv; esl_snprintf(header->value, len, "ARRAY::"); for(j = 0; j < header->idx; j++) { esl_snprintf(header->value + strlen(header->value), len - strlen(header->value), "%s%s", j == 0 ? "" : "::", header->array[j]); } } } else { header->value = data; } if (!exists) { header->hash = esl_ci_hashfunc_default(header->name, &hlen); if ((stack & ESL_STACK_TOP)) { header->next = event->headers; event->headers = header; if (!event->last_header) { event->last_header = header; } } else { if (event->last_header) { event->last_header->next = header; } else { event->headers = header; header->next = NULL; } event->last_header = header; } } return ESL_SUCCESS; }