GETHandler.cpp 799 B

12345678910111213141516171819202122232425262728293031323334
  1. //
  2. // Created by David Mike on 2020/2/26.
  3. //
  4. #include "GETHandler.hpp"
  5. namespace greatbridf
  6. {
  7. namespace Handler
  8. {
  9. void GET(HTTPRequest& request, std::iostream& stream, HTTPResponse& response)
  10. {
  11. auto path = request.getQueryPath();
  12. if (path == "/")
  13. {
  14. path = "/index.html";
  15. }
  16. File file("." + path);
  17. if (!file.good())
  18. {
  19. response.setResponseCode(404);
  20. stream << response << std::flush;
  21. return;
  22. }
  23. response.setResponseCode(200);
  24. response.setHeader("Content-Length", file.fileSize());
  25. stream << response;
  26. redirectStream(stream, file, file.fileSize());
  27. }
  28. }
  29. }