diff options
Diffstat (limited to '')
-rw-r--r-- | src/libnetdata/buffer/buffer.h (renamed from libnetdata/buffer/buffer.h) | 91 |
1 files changed, 48 insertions, 43 deletions
diff --git a/libnetdata/buffer/buffer.h b/src/libnetdata/buffer/buffer.h index 88d3f0282..900907d49 100644 --- a/libnetdata/buffer/buffer.h +++ b/src/libnetdata/buffer/buffer.h @@ -6,12 +6,6 @@ #include "../string/utf8.h" #include "../libnetdata.h" -#ifdef ENABLE_H2O -#include "h2o/memory.h" -#endif - -#define WEB_DATA_LENGTH_INCREASE_STEP 1024 - #define BUFFER_JSON_MAX_DEPTH 32 // max is 255 extern const char hex_digits[16]; @@ -38,37 +32,6 @@ typedef enum __attribute__ ((__packed__)) { } BUFFER_OPTIONS; typedef enum __attribute__ ((__packed__)) { - CT_NONE = 0, - CT_APPLICATION_JSON, - CT_TEXT_PLAIN, - CT_TEXT_HTML, - CT_APPLICATION_X_JAVASCRIPT, - CT_TEXT_CSS, - CT_TEXT_XML, - CT_APPLICATION_XML, - CT_TEXT_XSL, - CT_APPLICATION_OCTET_STREAM, - CT_APPLICATION_X_FONT_TRUETYPE, - CT_APPLICATION_X_FONT_OPENTYPE, - CT_APPLICATION_FONT_WOFF, - CT_APPLICATION_FONT_WOFF2, - CT_APPLICATION_VND_MS_FONTOBJ, - CT_IMAGE_SVG_XML, - CT_IMAGE_PNG, - CT_IMAGE_JPG, - CT_IMAGE_GIF, - CT_IMAGE_XICON, - CT_IMAGE_ICNS, - CT_IMAGE_BMP, - CT_PROMETHEUS, - CT_AUDIO_MPEG, - CT_AUDIO_OGG, - CT_VIDEO_MP4, - CT_APPLICATION_PDF, - CT_APPLICATION_ZIP, -} HTTP_CONTENT_TYPE; - -typedef enum __attribute__ ((__packed__)) { BUFFER_JSON_OPTIONS_DEFAULT = 0, BUFFER_JSON_OPTIONS_MINIFY = (1 << 0), BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS = (1 << 1), @@ -109,7 +72,7 @@ typedef struct web_buffer { #define buffer_overflow_check(b) #endif -static inline void _buffer_overflow_check(BUFFER *b) { +static inline void _buffer_overflow_check(BUFFER *b __maybe_unused) { assert(b->len <= b->size && "BUFFER: length is above buffer size."); @@ -150,10 +113,6 @@ void buffer_char_replace(BUFFER *wb, char from, char to); void buffer_print_sn_flags(BUFFER *wb, SN_FLAGS flags, bool send_anomaly_bit); -#ifdef ENABLE_H2O -h2o_iovec_t buffer_to_h2o_iovec(BUFFER *wb); -#endif - static inline void buffer_need_bytes(BUFFER *buffer, size_t needed_free_size) { if(unlikely(buffer->len + needed_free_size >= buffer->size)) buffer_increase(buffer, needed_free_size + 1); @@ -166,6 +125,9 @@ void buffer_json_finalize(BUFFER *wb); static const char *buffer_tostring(BUFFER *wb) { + if(unlikely(!wb)) + return NULL; + buffer_need_bytes(wb, 1); wb->buffer[wb->len] = '\0'; @@ -309,7 +271,8 @@ static inline void buffer_memcat(BUFFER *wb, const void *mem, size_t bytes) { buffer_overflow_check(wb); } -static inline void buffer_json_strcat(BUFFER *wb, const char *txt) { +static inline void buffer_json_strcat(BUFFER *wb, const char *txt) +{ if(unlikely(!txt || !*txt)) return; const unsigned char *t = (const unsigned char *)txt; @@ -871,6 +834,26 @@ static inline void buffer_json_add_array_item_string(BUFFER *wb, const char *val wb->json.stack[wb->json.depth].count++; } +static inline void buffer_json_add_array_item_uuid(BUFFER *wb, uuid_t *value) { + if(value && !uuid_is_null(*value)) { + char uuid[GUID_LEN + 1]; + uuid_unparse_lower(*value, uuid); + buffer_json_add_array_item_string(wb, uuid); + } + else + buffer_json_add_array_item_string(wb, NULL); +} + +static inline void buffer_json_add_array_item_uuid_compact(BUFFER *wb, uuid_t *value) { + if(value && !uuid_is_null(*value)) { + char uuid[GUID_LEN + 1]; + uuid_unparse_lower_compact(*value, uuid); + buffer_json_add_array_item_string(wb, uuid); + } + else + buffer_json_add_array_item_string(wb, NULL); +} + static inline void buffer_json_add_array_item_double(BUFFER *wb, NETDATA_DOUBLE value) { buffer_print_json_comma_newline_spacing(wb); @@ -1225,4 +1208,26 @@ buffer_rrdf_table_add_field(BUFFER *wb, size_t field_id, const char *key, const buffer_json_object_close(wb); } +static inline void buffer_copy(BUFFER *dst, BUFFER *src) { + if(!src || !dst) + return; + + buffer_contents_replace(dst, buffer_tostring(src), buffer_strlen(src)); + + dst->content_type = src->content_type; + dst->options = src->options; + dst->date = src->date; + dst->expires = src->expires; + dst->json = src->json; +} + +static inline BUFFER *buffer_dup(BUFFER *src) { + if(!src) + return NULL; + + BUFFER *dst = buffer_create(buffer_strlen(src) + 1, src->statistics); + buffer_copy(dst, src); + return dst; +} + #endif /* NETDATA_WEB_BUFFER_H */ |