diff options
-rw-r--r-- | scrape.c | 48 | ||||
-rw-r--r-- | util.c | 32 | ||||
-rw-r--r-- | util.h | 20 |
3 files changed, 50 insertions, 50 deletions
diff --git a/scrape.c b/scrape.c index f9c711b..c86e853 100644 --- a/scrape.c +++ b/scrape.c @@ -38,7 +38,7 @@ struct scrape_req { int socket; - cbuf *buf; + bbuf *buf; }; struct scrape_server { @@ -112,7 +112,7 @@ scrape_server *scrape_listen(const char *port) { void scrape_serve(scrape_server *srv, scrape_handler *handler, void *handler_ctx) { struct scrape_req req; - req.buf = cbuf_alloc(BUF_INITIAL, BUF_MAX); + req.buf = bbuf_alloc(BUF_INITIAL, BUF_MAX); int ret; @@ -151,24 +151,24 @@ void scrape_close(scrape_server *srv) { } void scrape_write(scrape_req *req, const char *metric, const struct label *labels, double value) { - cbuf_reset(req->buf); + bbuf_reset(req->buf); - cbuf_puts(req->buf, metric); + bbuf_puts(req->buf, metric); if (labels && labels->key) { - cbuf_putc(req->buf, '{'); + bbuf_putc(req->buf, '{'); for (const struct label *l = labels; l->key; l++) { if (l != labels) - cbuf_putc(req->buf, ','); - cbuf_putf(req->buf, "%s=\"%s\"", l->key, l->value); + bbuf_putc(req->buf, ','); + bbuf_putf(req->buf, "%s=\"%s\"", l->key, l->value); } - cbuf_putc(req->buf, '}'); + bbuf_putc(req->buf, '}'); } - cbuf_putf(req->buf, " %.16g\n", value); + bbuf_putf(req->buf, " %.16g\n", value); size_t buf_len; - const char *buf = cbuf_get(req->buf, &buf_len); + const char *buf = bbuf_get(req->buf, &buf_len); write_all(req->socket, buf, buf_len); } @@ -194,7 +194,7 @@ static const char http_error[] = static bool handle_http(struct scrape_req *req) { unsigned char http_buf[1024]; - cbuf *tmp_buf = req->buf; + bbuf *tmp_buf = req->buf; enum { state_read_method, @@ -203,7 +203,7 @@ static bool handle_http(struct scrape_req *req) { state_skip_headers_1, state_skip_headers_2 } state = state_read_method; - cbuf_reset(tmp_buf); + bbuf_reset(tmp_buf); while (true) { ssize_t got = read(req->socket, &http_buf, sizeof http_buf); @@ -217,41 +217,41 @@ static bool handle_http(struct scrape_req *req) { switch (state) { case state_read_method: if (c == ' ') { - if (cbuf_cmp(tmp_buf, "GET") != 0) + if (bbuf_cmp(tmp_buf, "GET") != 0) goto fail; state = state_read_path; - cbuf_reset(tmp_buf); + bbuf_reset(tmp_buf); break; } - if (!isalnum(c) || cbuf_len(tmp_buf) >= 16) + if (!isalnum(c) || bbuf_len(tmp_buf) >= 16) goto fail; - cbuf_putc(tmp_buf, c); + bbuf_putc(tmp_buf, c); break; case state_read_path: if (c == ' ') { - if (cbuf_cmp(tmp_buf, "/metrics") != 0) + if (bbuf_cmp(tmp_buf, "/metrics") != 0) goto fail; state = state_read_version; - cbuf_reset(tmp_buf); + bbuf_reset(tmp_buf); break; } - if (!isprint(c) || c == '\n' || cbuf_len(tmp_buf) >= 128) + if (!isprint(c) || c == '\n' || bbuf_len(tmp_buf) >= 128) goto fail; - cbuf_putc(tmp_buf, c); + bbuf_putc(tmp_buf, c); break; case state_read_version: if (c == '\n') { - if (cbuf_cmp(tmp_buf, "HTTP/1.1") != 0) + if (bbuf_cmp(tmp_buf, "HTTP/1.1") != 0) goto fail; state = state_skip_headers_1; - cbuf_reset(tmp_buf); + bbuf_reset(tmp_buf); break; } - if (!isgraph(c) || cbuf_len(tmp_buf) >= 16) + if (!isgraph(c) || bbuf_len(tmp_buf) >= 16) goto fail; - cbuf_putc(tmp_buf, c); + bbuf_putc(tmp_buf, c); break; case state_skip_headers_1: diff --git a/util.c b/util.c index 1d85d5c..c302fc7 100644 --- a/util.c +++ b/util.c @@ -28,14 +28,14 @@ // character buffers -struct cbuf { +struct bbuf { char *data; size_t len; size_t size; size_t max_size; }; -static bool cbuf_reserve(cbuf *buf, size_t len) { +static bool bbuf_reserve(bbuf *buf, size_t len) { if (buf->len + len <= buf->size) return true; @@ -51,8 +51,8 @@ static bool cbuf_reserve(cbuf *buf, size_t len) { return true; } -cbuf *cbuf_alloc(size_t initial_size, size_t max_size) { - cbuf *buf = must_malloc(sizeof *buf); +bbuf *bbuf_alloc(size_t initial_size, size_t max_size) { + bbuf *buf = must_malloc(sizeof *buf); buf->data = must_malloc(initial_size); buf->len = 0; buf->size = initial_size; @@ -60,32 +60,32 @@ cbuf *cbuf_alloc(size_t initial_size, size_t max_size) { return buf; } -void cbuf_reset(cbuf *buf) { +void bbuf_reset(bbuf *buf) { buf->len = 0; } -size_t cbuf_len(cbuf *buf) { +size_t bbuf_len(bbuf *buf) { return buf->len; } -void cbuf_put(cbuf *buf, const void *src, size_t len) { - if (!cbuf_reserve(buf, len)) +void bbuf_put(bbuf *buf, const void *src, size_t len) { + if (!bbuf_reserve(buf, len)) return; memcpy(buf->data + buf->len, src, len); buf->len += len; } -void cbuf_puts(cbuf *buf, const char *src) { - cbuf_put(buf, src, strlen(src)); +void bbuf_puts(bbuf *buf, const char *src) { + bbuf_put(buf, src, strlen(src)); } -void cbuf_putc(cbuf *buf, int c) { - if (!cbuf_reserve(buf, 1)) +void bbuf_putc(bbuf *buf, int c) { + if (!bbuf_reserve(buf, 1)) return; buf->data[buf->len++] = c; } -void cbuf_putf(cbuf *buf, const char *fmt, ...) { +void bbuf_putf(bbuf *buf, const char *fmt, ...) { va_list ap; int len = 0; @@ -94,7 +94,7 @@ void cbuf_putf(cbuf *buf, const char *fmt, ...) { va_end(ap); if (len > 0) { - if (!cbuf_reserve(buf, len + 1)) + if (!bbuf_reserve(buf, len + 1)) return; va_start(ap, fmt); vsnprintf(buf->data + buf->len, len + 1, fmt, ap); @@ -103,12 +103,12 @@ void cbuf_putf(cbuf *buf, const char *fmt, ...) { } } -const char *cbuf_get(struct cbuf *buf, size_t *len) { +const char *bbuf_get(struct bbuf *buf, size_t *len) { *len = buf->len; return buf->data; } -int cbuf_cmp(cbuf *buf, const char *other) { +int bbuf_cmp(bbuf *buf, const char *other) { size_t other_len = strlen(other); if (buf->len < other_len) return -1; diff --git a/util.h b/util.h index 5049a9f..8e76262 100644 --- a/util.h +++ b/util.h @@ -24,31 +24,31 @@ // character buffers /** Opaque type for an internally maintained character (byte) buffer. */ -typedef struct cbuf cbuf; +typedef struct bbuf bbuf; /** Allocates a new buffer with \p initial_size, growing up to \p max_size. */ -cbuf *cbuf_alloc(size_t initial_size, size_t max_size); +bbuf *bbuf_alloc(size_t initial_size, size_t max_size); /** Clears the contents of the buffer, resetting it to the empty string. */ -void cbuf_reset(cbuf *buf); +void bbuf_reset(bbuf *buf); /** Returns the current length of the buffer contents. */ -size_t cbuf_len(cbuf *buf); +size_t bbuf_len(bbuf *buf); /** Appends \p len bytes from address \p src to the buffer \p buf. */ -void cbuf_put(cbuf *buf, const void *src, size_t len); +void bbuf_put(bbuf *buf, const void *src, size_t len); /** Appends the null-terminated string at \p src to the buffer \p buf. */ -void cbuf_puts(cbuf *buf, const char *src); +void bbuf_puts(bbuf *buf, const char *src); /** Appends a single byte \p c to \p buf. */ -void cbuf_putc(cbuf *buf, int c); +void bbuf_putc(bbuf *buf, int c); /** Appends a formatted string to \p buf. */ -void cbuf_putf(cbuf *buf, const char *fmt, ...); +void bbuf_putf(bbuf *buf, const char *fmt, ...); /** * Returns the contents of \p buf, writing the length to \p len. * * There may not be a terminating '\0' byte after the contents. And even if there is, the returned * length will not include it. */ -const char *cbuf_get(struct cbuf *buf, size_t *len); +const char *bbuf_get(struct bbuf *buf, size_t *len); /** Compares the contents of \p buf to the string in \p other, in shortlex order. */ -int cbuf_cmp(cbuf *buf, const char *other); +int bbuf_cmp(bbuf *buf, const char *other); // string lists |