buffer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_WEB_BUFFER_H
  3. #define NETDATA_WEB_BUFFER_H 1
  4. #include "../libnetdata.h"
  5. #define WEB_DATA_LENGTH_INCREASE_STEP 1024
  6. typedef struct web_buffer {
  7. size_t size; // allocation size of buffer, in bytes
  8. size_t len; // current data length in buffer, in bytes
  9. char *buffer; // the buffer itself
  10. uint8_t contenttype; // the content type of the data in the buffer
  11. uint8_t options; // options related to the content
  12. time_t date; // the timestamp this content has been generated
  13. time_t expires; // the timestamp this content expires
  14. size_t *statistics;
  15. } BUFFER;
  16. // options
  17. #define WB_CONTENT_CACHEABLE 1
  18. #define WB_CONTENT_NO_CACHEABLE 2
  19. // content-types
  20. #define CT_APPLICATION_JSON 1
  21. #define CT_TEXT_PLAIN 2
  22. #define CT_TEXT_HTML 3
  23. #define CT_APPLICATION_X_JAVASCRIPT 4
  24. #define CT_TEXT_CSS 5
  25. #define CT_TEXT_XML 6
  26. #define CT_APPLICATION_XML 7
  27. #define CT_TEXT_XSL 8
  28. #define CT_APPLICATION_OCTET_STREAM 9
  29. #define CT_APPLICATION_X_FONT_TRUETYPE 10
  30. #define CT_APPLICATION_X_FONT_OPENTYPE 11
  31. #define CT_APPLICATION_FONT_WOFF 12
  32. #define CT_APPLICATION_FONT_WOFF2 13
  33. #define CT_APPLICATION_VND_MS_FONTOBJ 14
  34. #define CT_IMAGE_SVG_XML 15
  35. #define CT_IMAGE_PNG 16
  36. #define CT_IMAGE_JPG 17
  37. #define CT_IMAGE_GIF 18
  38. #define CT_IMAGE_XICON 19
  39. #define CT_IMAGE_ICNS 20
  40. #define CT_IMAGE_BMP 21
  41. #define CT_PROMETHEUS 22
  42. #define buffer_cacheable(wb) do { (wb)->options |= WB_CONTENT_CACHEABLE; if((wb)->options & WB_CONTENT_NO_CACHEABLE) (wb)->options &= ~WB_CONTENT_NO_CACHEABLE; } while(0)
  43. #define buffer_no_cacheable(wb) do { (wb)->options |= WB_CONTENT_NO_CACHEABLE; if((wb)->options & WB_CONTENT_CACHEABLE) (wb)->options &= ~WB_CONTENT_CACHEABLE; (wb)->expires = 0; } while(0)
  44. #define buffer_strlen(wb) ((wb)->len)
  45. const char *buffer_tostring(BUFFER *wb);
  46. #define buffer_flush(wb) wb->buffer[(wb)->len = 0] = '\0'
  47. void buffer_reset(BUFFER *wb);
  48. void buffer_strcat(BUFFER *wb, const char *txt);
  49. void buffer_fast_strcat(BUFFER *wb, const char *txt, size_t len);
  50. void buffer_rrd_value(BUFFER *wb, NETDATA_DOUBLE value);
  51. void buffer_date(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds);
  52. void buffer_jsdate(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds);
  53. BUFFER *buffer_create(size_t size, size_t *statistics);
  54. void buffer_free(BUFFER *b);
  55. void buffer_increase(BUFFER *b, size_t free_size_required);
  56. void buffer_snprintf(BUFFER *wb, size_t len, const char *fmt, ...) PRINTFLIKE(3, 4);
  57. void buffer_vsprintf(BUFFER *wb, const char *fmt, va_list args);
  58. void buffer_sprintf(BUFFER *wb, const char *fmt, ...) PRINTFLIKE(2,3);
  59. void buffer_strcat_jsonescape(BUFFER *wb, const char *txt);
  60. void buffer_strcat_htmlescape(BUFFER *wb, const char *txt);
  61. void buffer_char_replace(BUFFER *wb, char from, char to);
  62. char *print_number_lu_r(char *str, unsigned long uvalue);
  63. char *print_number_llu_r(char *str, unsigned long long uvalue);
  64. char *print_number_llu_r_smart(char *str, unsigned long long uvalue);
  65. void buffer_print_llu(BUFFER *wb, unsigned long long uvalue);
  66. void buffer_print_ll(BUFFER *wb, long long value);
  67. void buffer_print_llu_hex(BUFFER *wb, unsigned long long value);
  68. static inline void buffer_need_bytes(BUFFER *buffer, size_t needed_free_size) {
  69. if(unlikely(buffer->size - buffer->len < needed_free_size))
  70. buffer_increase(buffer, needed_free_size);
  71. }
  72. #endif /* NETDATA_WEB_BUFFER_H */