|
| 1 | +package io.vertx.ext.web.codec.sse; |
| 2 | + |
| 3 | +import io.vertx.core.Future; |
| 4 | +import io.vertx.core.Handler; |
| 5 | +import io.vertx.core.buffer.Buffer; |
| 6 | +import io.vertx.core.streams.WriteStream; |
| 7 | +import io.vertx.ext.web.codec.BodyCodec; |
| 8 | +import io.vertx.ext.web.codec.spi.BodyStream; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | + |
| 11 | +public class SseBodyCodec implements BodyCodec<Void> { |
| 12 | + |
| 13 | + private final WriteStream<SseEvent> eventHandler; |
| 14 | + |
| 15 | + private SseBodyCodec(WriteStream<SseEvent> eventHandler) { |
| 16 | + this.eventHandler = eventHandler; |
| 17 | + } |
| 18 | + |
| 19 | + private static final int MAX_BUFFER = 8000000; |
| 20 | + |
| 21 | + @Override |
| 22 | + public BodyStream<Void> stream() throws Exception { |
| 23 | + return new BodyStream<Void>() { |
| 24 | + private Buffer lineBuffer = Buffer.buffer(); |
| 25 | + private SseEvent.Builder eventBuilder = SseEvent.builder(); |
| 26 | + private Handler<Throwable> exceptionHandler; |
| 27 | + private Handler<Void> drainHandler; |
| 28 | + private int maxQueueSize = 8192; |
| 29 | + private boolean ended = false; |
| 30 | + |
| 31 | + @Override |
| 32 | + public Future<Void> result() { |
| 33 | + return Future.succeededFuture(); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) { |
| 38 | + this.exceptionHandler = handler; |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Future<Void> write(Buffer data) { |
| 44 | + if (ended) { |
| 45 | + return Future.failedFuture("Stream is ended"); |
| 46 | + } |
| 47 | + |
| 48 | + try { |
| 49 | + for (byte b : data.getBytes()) { |
| 50 | + if (b == '\n' || b == '\r') { |
| 51 | + // Process the current line (could be empty) |
| 52 | + String line = lineBuffer.toString(StandardCharsets.UTF_8); |
| 53 | + lineBuffer = Buffer.buffer(); |
| 54 | + |
| 55 | + if (line.isEmpty()) { |
| 56 | + // Empty line dispatches the event |
| 57 | + if (eventHandler != null) { |
| 58 | + eventHandler.write(eventBuilder.build()); |
| 59 | + } |
| 60 | + eventBuilder = SseEvent.builder(); |
| 61 | + } else { |
| 62 | + parseLine(line, eventBuilder); |
| 63 | + } |
| 64 | + } else { |
| 65 | + if (lineBuffer.length() > MAX_BUFFER) { |
| 66 | + return Future.failedFuture("Data is too big"); |
| 67 | + } |
| 68 | + lineBuffer.appendByte(b); |
| 69 | + } |
| 70 | + } |
| 71 | + return Future.succeededFuture(); |
| 72 | + } catch (Exception e) { |
| 73 | + if (exceptionHandler != null) { |
| 74 | + exceptionHandler.handle(e); |
| 75 | + } |
| 76 | + return Future.failedFuture(e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public WriteStream<Buffer> setWriteQueueMaxSize(int maxSize) { |
| 82 | + this.maxQueueSize = maxSize; |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean writeQueueFull() { |
| 88 | + return lineBuffer.length() >= maxQueueSize; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public WriteStream<Buffer> drainHandler(Handler<Void> handler) { |
| 93 | + this.drainHandler = handler; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void handle(Throwable event) { |
| 99 | + if (exceptionHandler != null) { |
| 100 | + exceptionHandler.handle(event); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Future<Void> end() { |
| 106 | + ended = true; |
| 107 | + // Process any remaining data in buffer |
| 108 | + if (lineBuffer.length() > 0) { |
| 109 | + String line = lineBuffer.toString(StandardCharsets.UTF_8); |
| 110 | + if (!line.isEmpty()) { |
| 111 | + parseLine(line, eventBuilder); |
| 112 | + } |
| 113 | + } |
| 114 | + // Dispatch final event if there's data |
| 115 | + if (eventHandler != null && (eventBuilder.toString().length() > 0)) { |
| 116 | + eventHandler.end(eventBuilder.build()); |
| 117 | + } |
| 118 | + return Future.succeededFuture(); |
| 119 | + } |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + public void parseLine(String line, SseEvent.Builder builder) { |
| 124 | + int colonIndex = line.indexOf(':'); |
| 125 | + if (colonIndex == 0) { |
| 126 | + return; |
| 127 | + } |
| 128 | + if (colonIndex == -1) { |
| 129 | + processField(builder, line, ""); |
| 130 | + return; |
| 131 | + } |
| 132 | + String field = line.substring(0, colonIndex); |
| 133 | + String value = line.substring(colonIndex + 1); |
| 134 | + // Remove leading space from value if present (SSE spec) |
| 135 | + if (value.startsWith(" ")) { |
| 136 | + value = value.substring(1); |
| 137 | + } |
| 138 | + processField(builder, field, value); |
| 139 | + } |
| 140 | + |
| 141 | + public void processField(SseEvent.Builder builder, String field, String value) { |
| 142 | + // Field names must be compared literally, with no case folding performed. |
| 143 | + switch (field) { |
| 144 | + case "event": |
| 145 | + builder.event(value); |
| 146 | + break; |
| 147 | + case "data": |
| 148 | + builder.data(value); |
| 149 | + break; |
| 150 | + case "id": |
| 151 | + builder.id(value); |
| 152 | + break; |
| 153 | + case "retry": |
| 154 | + // If the field value consists of only ASCII digits, then interpret the field value as an |
| 155 | + // integer in base ten, and set the event stream's reconnection time to that integer. |
| 156 | + // Otherwise, ignore the field. |
| 157 | + try { |
| 158 | + builder.retry(Integer.parseInt(value)); |
| 159 | + } catch (NumberFormatException ex) { |
| 160 | + throw new RuntimeException("Invalid \"retry\" value:" + value); |
| 161 | + } |
| 162 | + break; |
| 163 | + default: |
| 164 | + // Ignore unknown fields as per SSE spec |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public static BodyCodec<Void> sseStream(WriteStream<SseEvent> handler) { |
| 170 | + return new SseBodyCodec(handler); |
| 171 | + } |
| 172 | +} |
0 commit comments