/* Server-sent events framer, following the WHATWG event-stream parsing rules: * CRLF, LF or CR end a line; a blank line dispatches; ':' lines are comments; * one leading space after a field's colon is dropped; data lines join with LF. */ #include #include #include "sse.h" int pic_sse_init(pic_sse *p, size_t line_cap, size_t data_cap, pic_sse_event_fn event_fn, void *ctx) { memset(p, 0, sizeof *p); p->line = malloc(line_cap + 1); p->data = malloc(data_cap + 1); if (!p->line || !p->data || line_cap == 0 || data_cap == 0) { pic_sse_free(p); return -1; } p->line_cap = line_cap; p->data_cap = data_cap; p->on_event = event_fn; p->ctx = ctx; return 0; } void pic_sse_free(pic_sse *p) { free(p->line); free(p->data); p->line = p->data = NULL; } static int dispatch(pic_sse *p) { int rc = 0; if (p->has_data) { size_t len = p->data_len > 0 ? p->data_len - 1 : 0; /* drop the final LF */ p->data[len] = '\0'; rc = p->on_event(p->ctx, p->event[0] ? p->event : "message", p->data, len); } p->data_len = 0; p->has_data = 0; p->event[0] = '\0'; return rc; } static int append_data(pic_sse *p, const char *value, size_t len) { if (len + 1 > p->data_cap - p->data_len) { return -1; } memcpy(p->data + p->data_len, value, len); p->data_len += len; p->data[p->data_len++] = '\n'; p->has_data = 1; return 0; } /* Processes one complete line (without its terminator). */ static int process_line(pic_sse *p) { const char *line = p->line, *colon, *value; size_t len = p->line_len, name_len, value_len; if (len == 0) { return dispatch(p); } if (line[0] == ':') { return 0; /* comment, e.g. a keep-alive */ } colon = memchr(line, ':', len); name_len = colon ? (size_t)(colon - line) : len; value = colon ? colon + 1 : line + len; value_len = len - (size_t)(value - line); if (value_len > 0 && value[0] == ' ') { value++; value_len--; } if (name_len == 4 && memcmp(line, "data", 4) == 0) { return append_data(p, value, value_len); } if (name_len == 5 && memcmp(line, "event", 5) == 0) { if (value_len >= sizeof p->event) { return -1; } memcpy(p->event, value, value_len); p->event[value_len] = '\0'; } if (name_len == 2 && memcmp(line, "id", 2) == 0 && value_len < sizeof p->last_id && !memchr(value, '\0', value_len)) { memcpy(p->last_id, value, value_len); /* the last event id, kept across events */ p->last_id[value_len] = '\0'; } return 0; /* retry and unknown fields do not affect framing */ } int pic_sse_feed(pic_sse *p, const char *bytes, size_t n) { static const char bom[3] = {'\xEF', '\xBB', '\xBF'}; if (p->failed) { return -1; } for (size_t i = 0; i < n; i++) { char c = bytes[i]; int rc; /* A byte-order mark may open the stream, split across any number of * reads. Bytes that only looked like its start are ordinary content. */ if (!p->started) { if (c == bom[p->bom_matched]) { p->started = ++p->bom_matched == 3; continue; } p->started = 1; for (int b = 0; b < p->bom_matched; b++) { if (p->line_len >= p->line_cap) { p->failed = 1; return -1; } p->line[p->line_len++] = bom[b]; } } if (p->after_cr) { p->after_cr = 0; if (c == '\n') { continue; /* the LF of a CRLF pair */ } } if (c == '\r' || c == '\n') { p->after_cr = c == '\r'; p->line[p->line_len] = '\0'; rc = process_line(p); p->line_len = 0; if (rc != 0) { if (rc < 0) { p->failed = 1; } return rc; } continue; } if (p->line_len >= p->line_cap) { p->failed = 1; return -1; } p->line[p->line_len++] = c; } return 0; } int pic_sse_pending(const pic_sse *p) { return p->line_len > 0 || p->has_data || p->event[0] != '\0'; }