Skip to content

SSE not working if content_type header has charset indication #7238

@YanickSchraner

Description

@YanickSchraner

Branch/Environment/Version

Describe the bug

Tyk.io is not able to detect SSE responses if the Content-Type response header contains charset indications:

Content-Type: text/event-stream; charset=utf-8. E.g. FastAPI use this charset indication per default for SSE responses.

The code responsible to detect SSE responses in tyk is:

func IsSseStreamingResponse(r *http.Response) bool {

// IsSseStreamingResponse returns true if the response designates SSE streaming.
func IsSseStreamingResponse(r *http.Response) bool {
	return r.Header.Get(headerContentType) == "text/event-stream"
}

Replacing the exact string match with something like

// IsSseStreamingResponse returns true if the response designates SSE streaming.
func IsSseStreamingResponse(r *http.Response) bool {
	return strings.HasPrefix(r.Header.Get(headerContentType), "text/event-stream")
}

shuld fix the issue.

Reproduction steps
Steps to reproduce the behavior:

I created this GitHub repo with an example server to reproduce the behaviour: FastAPI SSE

  • docker run yanickbs/fastapi-sse:latest
  • Publish an API with the following configuration:
    1. Set an appropriate listen path, e.g. "listen_path": "/sse"
    2. Strip the listen path from the request URL, e.g. "strip_listen_path": true
    3. Set the target URL to the server, e.g. "target_url": "http://host.docker.internal:8000/sse"
    4. Configure the API endpoints GET /sse and GET /sse-fixed-header
    5. Click Save, and wait for the API to be published.
  • Test the protected SSE service via curl -vvv http://localhost:8000/sse/sse and curl -vvv http://localhost:8000/sse/sse-fixed-header and check the response headers.

Actual behavior
The response arrives at the CURL client in one message after the SSE is finished.
You get this behavior using this CURL: curl -vvv http://localhost:8000/sse/sse

Expected behavior
The response arrives event by event to the CURL client.
You get this response with this CURL:
curl -vvv http://localhost:8000/sse/sse-fixed-header

Configuration (tyk config file):

Follow the official documentation to configure the API Gateway to use the server.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions