ESL_DECLARE(int) esl_config_open_file(esl_config_t *cfg, const char *file_path) { FILE *f; const char *path = NULL; char path_buf[1024]; if (file_path[0] == '/') { path = file_path; } else { esl_snprintf(path_buf, sizeof(path_buf), "%s%s%s", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR, file_path); path = path_buf; } if (!path) { return 0; } memset(cfg, 0, sizeof(*cfg)); cfg->lockto = -1; esl_log(ESL_LOG_DEBUG, "Configuration file is %s.\n", path); f = fopen(path, "r"); if (!f) { if (file_path[0] != '/') { int last = -1; char *var, *val; esl_snprintf(path_buf, sizeof(path_buf), "%s%sopenesl.conf", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR); path = path_buf; if ((f = fopen(path, "r")) == 0) { return 0; } cfg->file = f; esl_set_string(cfg->path, path); while (esl_config_next_pair(cfg, &var, &val)) { if ((cfg->sectno != last) && !strcmp(cfg->section, file_path)) { cfg->lockto = cfg->sectno; return 1; } } esl_config_close_file(cfg); memset(cfg, 0, sizeof(*cfg)); return 0; } return 0; } else { cfg->file = f; esl_set_string(cfg->path, path); return 1; } }
ESL_DECLARE(esl_status_t) esl_event_serialize_json(esl_event_t *event, char **str) { esl_event_header_t *hp; cJSON *cj; *str = NULL; cj = cJSON_CreateObject(); for (hp = event->headers; hp; hp = hp->next) { cJSON_AddItemToObject(cj, hp->name, cJSON_CreateString(hp->value)); } if (event->body) { int blen = (int) strlen(event->body); char tmp[25]; esl_snprintf(tmp, sizeof(tmp), "%d", blen); cJSON_AddItemToObject(cj, "Content-Length", cJSON_CreateString(tmp)); cJSON_AddItemToObject(cj, "_body", cJSON_CreateString(event->body)); } *str = cJSON_Print(cj); cJSON_Delete(cj); return ESL_SUCCESS; }
ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode) { size_t len = 0; esl_event_header_t *hp; size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1536, new_len = 0; char *buf; char *encode_buf = NULL; /* used for url encoding of variables to make sure unsafe things stay out of the serialized copy */ int clen = 0; if (!event || !event->headers) return ESL_FAIL; *str = NULL; dlen = blocksize * 2; if (!(buf = malloc(dlen))) { return ESL_FAIL; } /* go ahead and give ourselves some space to work with, should save a few reallocs */ if (!(encode_buf = malloc(encode_len))) { esl_safe_free(buf); return ESL_FAIL; } /* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "hit serialized!.\n"); */ for (hp = event->headers; hp; hp = hp->next) { /* * grab enough memory to store 3x the string (url encode takes one char and turns it into %XX) * so we could end up with a string that is 3 times the originals length, unlikely but rather * be safe than destroy the string, also add one for the null. And try to be smart about using * the memory, allocate and only reallocate if we need more. This avoids an alloc, free CPU * destroying loop. */ if (!strcasecmp(hp->name, "content-length")) { clen++; } new_len = (strlen(hp->value) * 3) + 1; if (encode_len < new_len) { char *tmp; /* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len); */ /* we can use realloc for initial alloc as well, if encode_buf is zero it treats it as a malloc */ /* keep track of the size of our allocation */ encode_len = new_len; if (!(tmp = realloc(encode_buf, encode_len))) { /* oh boy, ram's gone, give back what little we grabbed and bail */ esl_safe_free(buf); esl_safe_free(encode_buf); return ESL_FAIL; } encode_buf = tmp; } /* handle any bad things in the string like newlines : etc that screw up the serialized format */ if (encode) { esl_url_encode(hp->value, encode_buf, encode_len); } else { esl_snprintf(encode_buf, encode_len, "%s", hp->value); } llen = strlen(hp->name) + strlen(encode_buf) + 8; if ((len + llen) > dlen) { char *m; dlen += (blocksize + (len + llen)); if ((m = realloc(buf, dlen))) { buf = m; } else { /* we seem to be out of memory trying to resize the serialize string, give back what we already have and give up */ esl_safe_free(buf); esl_safe_free(encode_buf); return ESL_FAIL; } } snprintf(buf + len, dlen - len, "%s: %s\n", hp->name, *encode_buf == '\0' ? "_undef_" : encode_buf); len = strlen(buf); } /* we are done with the memory we used for encoding, give it back */ esl_safe_free(encode_buf); if (event->body) { int blen = (int) strlen(event->body); llen = blen; if (blen) { llen += 25; } else { llen += 5; } if ((len + llen) > dlen) { char *m; dlen += (blocksize + (len + llen)); if ((m = realloc(buf, dlen))) { buf = m; } else { esl_safe_free(buf); return ESL_FAIL; } } if (blen) { if (clen) { snprintf(buf + len, dlen - len, "\n%s", event->body); } else { snprintf(buf + len, dlen - len, "Content-Length: %d\n\n%s", (int)strlen(event->body), event->body); } } } else { snprintf(buf + len, dlen - len, "\n"); } *str = buf; return ESL_SUCCESS; }
ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode) { esl_size_t len = 0; esl_event_header_t *hp; esl_size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1536, new_len = 0; char *buf; char *encode_buf = NULL; /* used for url encoding of variables to make sure unsafe things stay out of the serialized copy */ *str = NULL; dlen = blocksize * 2; if (!(buf = malloc(dlen))) { abort(); } /* go ahead and give ourselves some space to work with, should save a few reallocs */ if (!(encode_buf = malloc(encode_len))) { abort(); } /* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "hit serialized!.\n"); */ for (hp = event->headers; hp; hp = hp->next) { /* * grab enough memory to store 3x the string (url encode takes one char and turns it into %XX) * so we could end up with a string that is 3 times the originals length, unlikely but rather * be safe than destroy the string, also add one for the null. And try to be smart about using * the memory, allocate and only reallocate if we need more. This avoids an alloc, free CPU * destroying loop. */ if (hp->idx) { int i; new_len = 0; for(i = 0; i < hp->idx; i++) { new_len += (strlen(hp->array[i]) * 3) + 1; } } else { new_len = (strlen(hp->value) * 3) + 1; } if (encode_len < new_len) { char *tmp; /* keep track of the size of our allocation */ encode_len = new_len; if (!(tmp = realloc(encode_buf, encode_len))) { abort(); } encode_buf = tmp; } /* handle any bad things in the string like newlines : etc that screw up the serialized format */ if (encode) { esl_url_encode(hp->value, encode_buf, encode_len); } else { esl_snprintf(encode_buf, encode_len, "[%s]", hp->value); } llen = strlen(hp->name) + strlen(encode_buf) + 8; if ((len + llen) > dlen) { char *m; char *old = buf; dlen += (blocksize + (len + llen)); if ((m = realloc(buf, dlen))) { buf = m; } else { buf = old; abort(); } } esl_snprintf(buf + len, dlen - len, "%s: %s\n", hp->name, *encode_buf == '\0' ? "_undef_" : encode_buf); len = strlen(buf); } /* we are done with the memory we used for encoding, give it back */ esl_safe_free(encode_buf); if (event->body) { int blen = (int) strlen(event->body); llen = blen; if (blen) { llen += 25; } else { llen += 5; } if ((len + llen) > dlen) { char *m; char *old = buf; dlen += (blocksize + (len + llen)); if ((m = realloc(buf, dlen))) { buf = m; } else { buf = old; abort(); } } if (blen) { esl_snprintf(buf + len, dlen - len, "Content-Length: %d\n\n%s", blen, event->body); } else { esl_snprintf(buf + len, dlen - len, "\n"); } } else { esl_snprintf(buf + len, dlen - len, "\n"); } *str = buf; 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, 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; }