]> arthur.barton.de Git - netdata.git/blobdiff - src/web_buffer.c
dns_query_time plugin: replace "." with "_" in dimensions
[netdata.git] / src / web_buffer.c
index 5b4f24623ba86ef230fc1c90e95a2bf283d34e94..9f9ceda63da26a5f3d9720c863494dabe46755b8 100644 (file)
@@ -35,6 +35,7 @@ void buffer_reset(BUFFER *wb)
     wb->contenttype = CT_TEXT_PLAIN;
     wb->options = 0;
     wb->date = 0;
+    wb->expires = 0;
 
     buffer_overflow_check(wb);
 }
@@ -112,12 +113,14 @@ void buffer_print_llu(BUFFER *wb, unsigned long long uvalue)
 
 void buffer_strcat(BUFFER *wb, const char *txt)
 {
+    // buffer_sprintf(wb, "%s", txt);
+
     if(unlikely(!txt || !*txt)) return;
 
     buffer_need_bytes(wb, 1);
 
     char *s = &wb->buffer[wb->len], *start, *end = &wb->buffer[wb->size];
-    long len = wb->len;
+    size_t len = wb->len;
 
     start = s;
     while(*txt && s != end)
@@ -142,6 +145,26 @@ void buffer_strcat(BUFFER *wb, const char *txt)
     }
 }
 
+void buffer_strcat_htmlescape(BUFFER *wb, const char *txt)
+{
+    char b[2] = { [0] = '\0', [1] = '\0' };
+
+    while(*txt) {
+        switch(*txt) {
+            case '&': buffer_strcat(wb, "&"); break;
+            case '<': buffer_strcat(wb, "&lt;"); break;
+            case '>': buffer_strcat(wb, "&gt;"); break;
+            case '"': buffer_strcat(wb, "&quot;"); break;
+            case '/': buffer_strcat(wb, "&#x2F;"); break;
+            case '\'': buffer_strcat(wb, "&#x27;"); break;
+            default: {
+                b[0] = *txt;
+                buffer_strcat(wb, b);
+            }
+        }
+        txt++;
+    }
+}
 
 void buffer_snprintf(BUFFER *wb, size_t len, const char *fmt, ...)
 {
@@ -189,11 +212,7 @@ void buffer_sprintf(BUFFER *wb, const char *fmt, ...)
     va_end(args);
 
     if(unlikely(wrote >= len)) {
-        // there is bug in vsnprintf() and it returns
-        // a number higher to len, but it does not
-        // overflow the buffer.
-        // our buffer overflow detector will log it
-        // if it does.
+        // truncated
         buffer_overflow_check(wb);
 
         debug(D_WEB_BUFFER, "web_buffer_sprintf(): increasing web_buffer at position %zu, size = %zu\n", wb->len, wb->size);
@@ -213,7 +232,13 @@ void buffer_sprintf(BUFFER *wb, const char *fmt, ...)
 void buffer_rrd_value(BUFFER *wb, calculated_number value)
 {
     buffer_need_bytes(wb, 50);
-    wb->len += print_calculated_number(&wb->buffer[wb->len], value);
+
+    if(isnan(value) || isinf(value)) {
+        buffer_strcat(wb, "null");
+        return;
+    }
+    else
+        wb->len += print_calculated_number(&wb->buffer[wb->len], value);
 
     // terminate it
     buffer_need_bytes(wb, 1);
@@ -334,8 +359,9 @@ BUFFER *buffer_create(size_t size)
     return(b);
 }
 
-void buffer_free(BUFFER *b)
-{
+void buffer_free(BUFFER *b) {
+    if(unlikely(!b)) return;
+
     buffer_overflow_check(b);
 
     debug(D_WEB_BUFFER, "Freeing web buffer of size %zu.", b->size);