void Server::Handle(ConnectionPtr connection) {
  auto request = connection->request();

  const Url& url = request->url();
  LOG_INFO("Request URL path: %s", url.path().c_str());

  UrlArgs args;
  auto view = FindView(request->method(), url.path(), &args);

  if (!view) {
    LOG_WARN("No view matches the request: %s %s", request->method().c_str(),
             url.path().c_str());

    if (request->method() == methods::kGet) {
      // Try to serve static files for GET request.
      auto response = ServeStatic(request);
      if (!response) {
        // Static file not found.
        connection->SendResponse(Status::kNotFound);
      } else {
        connection->SendResponse(response);
      }
    } else {
      connection->SendResponse(Status::kBadRequest);
    }

    return;
  }

  // Save the (regex matched) URL args to request object.
  request->set_args(std::move(args));

  // Ask the matched view to process the request.
  ResponsePtr response = view->Handle(request);

  // Send the response back.
  if (response) {
    connection->SendResponse(response);
  } else {
    connection->SendResponse(Status::kBadRequest);
  }
}
void Server::Handle(ConnectionPtr connection) {
  auto request = connection->request();

  const Url& url = request->url();
  LOG_INFO("Request URL path: %s", url.path().c_str());

  UrlArgs args;
  auto view = FindView(request->method(), url.path(), &args);

  if (!view) {
    LOG_WARN("No view matches the request: %s %s", request->method().c_str(),
             url.path().c_str());

    if (request->method() == methods::kGet) {
       
      auto response = ServeStatic(request);
      if (!response) {
         
        connection->SendResponse(Status::kNotFound);
      } else {
        connection->SendResponse(response);
      }
    } else {
      connection->SendResponse(Status::kBadRequest);
    }

    return;
  }

   
  request->set_args(std::move(args));

   
  ResponsePtr response = view->Handle(request);

   
  if (response) {
    connection->SendResponse(response);
  } else {
    connection->SendResponse(Status::kBadRequest);
  }
}