martian: support server sent events without chunked encoding#812
Merged
martian: support server sent events without chunked encoding#812
Conversation
mmatczuk
reviewed
May 24, 2024
internal/martian/flush.go
Outdated
| } | ||
|
|
||
| // flushAfterSSEWriter forces a flush after each '\n\n' is written. | ||
| type flushAfterSSEWriter struct { |
Contributor
There was a problem hiding this comment.
We need a flushWriter that can be used in proxy conn and handler for sse and chunked encoding.
type interface flusher Flush() error
type afterFlusher struct {
w io.Writer
f flusher
eom [2]byte
}
This needs to come with a set of extensive tests.
This needs to be applied everywhere.
From http.Server:
HTTP/1.1 or greater: Transfer-Encoding has been set to identity, and no
content-length has been provided. The connection must be closed after the
reply is written, and no chunking is to be done. This is the setup
recommended in the Server-Sent Events candidate recommendation 11,
section 8.
if hasTE && te == "identity" {
cw.chunking = false
w.closeAfterReply = true
delHeader("Transfer-Encoding")
} else {
// HTTP/1.1 or greater: use chunked transfer encoding
// to avoid closing the connection at EOF.
cw.chunking = true
setHeader.transferEncoding = "chunked"
if hasTE && te == "chunked" {
// We will send the chunked Transfer-Encoding header later.
delHeader("Transfer-Encoding")
}
}
mmatczuk
reviewed
May 27, 2024
internal/martian/flush.go
Outdated
| } | ||
|
|
||
| p = p[:n] | ||
| l := len(p) |
mmatczuk
reviewed
May 27, 2024
internal/martian/proxy_conn.go
Outdated
| } else { | ||
| switch { | ||
| case isTextEventStream(res): | ||
| err = res.Write(&patternFlushWriter{w: p.brw.Writer, f: p.brw.Writer, pattern: [2]byte{'\n', '\n'}}) |
Contributor
There was a problem hiding this comment.
Maybe it makes sense to specify the special byte sequences as global variables.
mmatczuk
reviewed
May 27, 2024
internal/martian/proxy_conn.go
Outdated
| } else { | ||
| switch { | ||
| case isTextEventStream(res): | ||
| err = res.Write(&patternFlushWriter{w: p.brw.Writer, f: p.brw.Writer, pattern: [2]byte{'\n', '\n'}}) |
Contributor
There was a problem hiding this comment.
I'd prefer to have constructor.
mmatczuk
reviewed
May 27, 2024
internal/martian/flush.go
Outdated
| w io.Writer | ||
| f flusher | ||
| pattern [2]byte | ||
| last byte |
Contributor
There was a problem hiding this comment.
Can we add space before last?
patternFlushWriter will flush when specified pattern occurs.
This patch applies patternFlushWriter to support - non-chunked SSE i.e. flush after '\n\n' - chunked-encoding i.e. flush after '\r\n'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This patch applies patternFlushWriter to support