Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does sse_events correctly detect the end of an SSE event? #114

Closed
kennethlakin opened this issue Nov 10, 2015 · 1 comment
Closed

Does sse_events correctly detect the end of an SSE event? #114

kennethlakin opened this issue Nov 10, 2015 · 1 comment

Comments

@kennethlakin
Copy link
Contributor

https://html.spec.whatwg.org/#parsing-an-event-stream specifies an SSE event as

stream        = [ bom ] *event
event         = *( comment / field ) end-of-line
comment       = colon *any-char end-of-line
field         = 1*name-char [ colon [ space ] *any-char ] end-of-line
end-of-line   = ( cr lf / cr / lf )

; characters
lf            = %x000A ; U+000A LINE FEED (LF)
cr            = %x000D ; U+000D CARRIAGE RETURN (CR)
  ...

So, it looks like we can detect the end of an event by scanning for two EOL characters. https://github.com/inaka/shotgun/blob/master/src/shotgun.erl#L624 does do this, but it only looks for pairs of LF, when it should be looking for pairs of CR, LF, or CR/LF. Mightn't the following code at that line be more complete?

    DataList = binary:split(NewBuffer, [<<"\n\n">>, <<"\r\r">>, <<"\r\n\r\n">>], [global]),

Tiny test from an Erlang shell is below. Note that the removal of any of the patterns in the call to binary:split causes at least one of the test SSEs to fail to have its trailing EOLs removed:

1> A= <<"test server side event\r\n\r\n">>.
<<"test server side event\r\n\r\n">>
2> B= <<"test server side event\r\r">>.    
<<"test server side event\r\r">>
3> C= <<"test server side event\n\n">>. 
<<"test server side event\n\n">>
4> lists:foldl(fun(F, A) -> lists:append(A, [binary:split(F, [<<"\r\r">>, <<"\n\n">>, <<"\r\n\r\n">>], [global])]) end, [], [A, B, C]).
[[<<"test server side event">>,<<>>],
 [<<"test server side event">>,<<>>],
 [<<"test server side event">>,<<>>]]
@jfacorro
Copy link
Contributor

@kennethlakin Your observation is spot on. There is an existing issue (#16) that reports this problem not only for the end of an event but for the different lines that are part of an event. I will close this issue and mark it as duplicated, we can address both problems in issue #16.

elbrujohalcon pushed a commit that referenced this issue Jan 21, 2016
[Fix #114] update cowboy and cowlib version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants