Example #1
0
char *
octstr_snprintf(octstr_t *octstr, char *fmt, ...)
{
	assert(octstr != NULL);

	size_t res, new_res;
	va_list ap, ap_copy;

	va_start(ap, fmt);
	va_copy(ap_copy, ap);
	buffer_reset(octstr->buf);
	buffer_advance(octstr->buf, OCTSTR_SNPRINTF_ADVANCE);
	res = vsnprintf(buffer_ptr(octstr->buf), OCTSTR_SNPRINTF_ADVANCE, fmt, ap);
	if (res < 0)
		error(1, "octstr_snprintf: vsnprintf res=%d", (int)res);
	if (res >= OCTSTR_SNPRINTF_ADVANCE) {
		buffer_reset(octstr->buf);
		buffer_advance(octstr->buf, res+1); /* plus one zero char */
		new_res = vsnprintf(buffer_ptr(octstr->buf), res+1, fmt, ap_copy);
		if (new_res < 0 || new_res != res)
			error(1, "octstr_snprintf: should not reach");
	} else
		buffer_trim(octstr->buf, OCTSTR_SNPRINTF_ADVANCE - res);
	/* now reduce buffer size down one byte because we have '\0' there */
	buffer_trim(octstr->buf, 1);
	va_end(ap);
	return buffer_ptr(octstr->buf);
}
Example #2
0
char *
octstr_trim(octstr_t *octstr, size_t n)
{
	assert(octstr != NULL);

	buffer_trim(octstr->buf, n);
	return buffer_ptr(octstr->buf);
}
Example #3
0
void stat_header_received(HURLPath *path,
                          int response_code,
                          HURLHeader *headers,
                          size_t header_len)
{
    HURLHeader *h;
    ElementStat *stat = (ElementStat *)path->tag;
    struct tm date;
    Buffer *json = NULL;
    char *tmp;
    char *escaped;
    size_t tmp_len;

    /* Initialize HTTP statistics */
    if (!stat->http)
    {
        stat->http = calloc(1, sizeof(HTTPStat));
    }

    if (!buffer_init(&json, 1024, 256))
    {
        return;
    }

    buffer_insert_strlen(json, "{");

    stat->http->response_code = response_code;
    stat->http->header_size = (int)header_len;

    /* Check if header data should be ignored. */

    h = headers;
    while (h != NULL)
    {
        if (strcasecmp("date", h->key) == 0)
        {
            strptime(h->value, "%a, %d %b %Y %T %z", &date);
            stat->http->date = mktime(&date);
        }
        else if (strcasecmp("expires", h->key) == 0)
        {
            strptime(h->value, "%a, %d %b %Y %T %z", &date);
            stat->http->expiry_date = mktime(&date);
        }
        else if (test->stats.http.all_headers
            || hurl_header_exists(test->stat_headers, h->key))
        {
            escaped = json_escape(h->value);
            tmp_len = strlen(escaped) + strlen(h->key) + strlen("\"\":\"\",")
                + 1;
            tmp = malloc(sizeof(char) * tmp_len);
            snprintf(tmp, tmp_len, "\"%s\":\"%s\",", h->key, escaped);
            buffer_insert_strlen(json, tmp);
            free(escaped);
            free(tmp);
        }
        /* Specifically extract content type header */
        if (strcasecmp("content-type", h->key) == 0)
        {
            stat->http->content_type = allocstrcpy(h->value,
                                                   strlen(h->value),
                                                   1);
        }
        h = h->next;

    }
    /* Remove last comma */
    if (json->data_len > 1)
    {
        buffer_rewind(json, 1);
    }
    buffer_insert_strlen(json, "}");
    buffer_trim(json);
    stat->http->headers = json->head;
    free(json);
}