title: How to read the header of a bad request in beast::websocket?
body: <p>I have a websocket server(listening the loopback interface) that should respond to a GET request. Here's how I do that</p>
  
  <pre><code>ws_.async_accept_ex(
      [self = shared_from_this()](websocket::response_type&amp; res) //
      {
          if (res.result_int() == 400) { // bad request, assume HTTP GET
              osstream response;
              /* invoke request handlers */
              for (auto&amp; do_handle : self-&gt;handlers) {
                  do_handle({ "GET" }, response);
                  /* if responded, assing response to the body*/
                  if (response.tellp()) {
                      res.content_length(response.str().size());
                      res.body() = std::move(response.str());
                      break;
                  }
              }
          }
      },
      net::bind_executor(strand_,
          [self = shared_from_this()](beast::error_code ec) {
              self-&gt;on_accept(ec);
  }));
  </code></pre>
  
  <p>But I also need to be able to handle locations(like <code>localhost:2019/some_location</code>).</p>
  
  <p>Is there a way?</p>
  
  <p><strong>Solution:</strong> read the header manually, check if it's an upgrade</p>
  
  <pre><code>beast::flat_buffer buffer_;
  websocket::request_type req_;
  
  /*
    ...
    ...
  */
          http::async_read(ws_.next_layer(), buffer_, req_,
          [self=shared_from_this()](beast::error_code ec, size_t)
          {
              if(ec){
                  fail(ec, "[http] read");
                  return;
              }
              if(websocket::is_upgrade(self-&gt;req_)){
                  self-&gt;ws_.accept(self-&gt;req_,ec);
                  self-&gt;on_accept(ec);
              }
              else{
                  osstream response;
                  websocket::response_type res;
                  vector&lt;string_view&gt; req_args
                      { "GET", self-&gt;req_.base().target().begin() };
                  /* invoke request handlers */ 
                  for(auto &amp;do_handle : self-&gt;handlers){
                      do_handle(req_args, response);
                      /* if responded, assing response to the body*/
                      if(response.tellp()){
                          res.content_length(response.str().size());
                          res.body() = std::move(response.str());
                          break;
                  }}
                  http::write(self-&gt;ws_.next_layer(), res);
              }
          });
  </code></pre>