vibe.d icon indicating copy to clipboard operation
vibe.d copied to clipboard

[Bug] Unable to call WebSocket.request._query.get() due to const

Open vnayar opened this issue 1 year ago • 1 comments

When attaching a handler to a WebSocket like so:

  @Autowire
  URLRouter router;

  @PostConstruct
  void postConstruct() {
    logInfo("Registering RtcNotifier as HTTP GET " ~ PATH);
    router.get(PATH, handleWebSockets((WebSocket sock) => handleConnection(sock)));
  }

The handler itself is unable to access the query-string parameters using the get method.

  void handleConnection(scope WebSocket socket) {
    string peerId = socket.request()._query.get("peerId", "");

    string peerId = socket.request().query.get("peerId", "");

This code results in the error:

Error: none of the overloads of `get` are callable using a `const` object
Error: mutable method `vibe.http.server.HTTPServerRequest.query` is not callable using a `const` object

The reason for this is that the WebSocket.request() method returns a const value, which unfortunately does not work for DictionaryList.get. A const version of DictionaryList.get should be added, which is needed due to returning a const HTTPServerRequest.

vnayar avatar Apr 20 '23 17:04 vnayar

The problem here is actually the query property itself that is not const. It used to be a normal field in the early days, but now parses the query string lazily for performance reasons and thus cannot be const. It seems like we need to change it from const(HTTPServerRequest) request() const to inout(HTTPServerRequest) request() inout and just document that the returned request may not be modified.

s-ludwig avatar Aug 29 '23 19:08 s-ludwig