Skip to content

Setting extra headers in HTTP response #783

@robertseliger

Description

@robertseliger

First, please accept our compliments on the websockets library. It has saved us a lot of work.

In our WebSocket server we have the need to receive extra header fields in the HTTP request, We also need to include extra header fields in the HTTP response. We created a class derived from WebSocketServerProtocol so that we could override the process_request method and in so doing, deal with the extra header fields. We chose this approach because it also allowed us to decorate the websocket object with some additional attributes that are unique to our server.

We ran into an issue with being able to set extra header fields in the response. Even though we set self.extra_headers to Headers that contained our extra field, we did not see these fields in the subsequent HTTP response header. We eventually determined that the value we set self.extra_headers to is ignored in the implementation of WebSocketServerProtocol.handshake.

For now our solution is to temporarily modify server.py by adding this statement before the current line 571:

extra_headers = self.extra_headers

The problem seems to be that extra_headers is an input to handshake. It is set when handshake is called on line 130, where extra_headers is set to the then value of self.extra_headers. Subsequent changes to self.extra_headers are not captured, and therefore do not appear in the HTTP response. By adding the line of code shown above, the extra fields appear as expected.

Perhaps there is a better way to solve this problem, or perhaps there is a different way to set extra headers in the response message? In either case, we wanted to bring this issue to your attention and offer a potential solution as well.

Respectfully,

Robert Seliger
WHIM Inc.
www.whimsmart.com

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions