http_server_response_info.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SERVER_HTTP_SERVER_RESPONSE_INFO_H_
  5. #define NET_SERVER_HTTP_SERVER_RESPONSE_INFO_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <utility>
  9. #include "base/strings/string_split.h"
  10. #include "net/http/http_status_code.h"
  11. namespace net {
  12. class HttpServerResponseInfo {
  13. public:
  14. // Creates a 200 OK HttpServerResponseInfo.
  15. HttpServerResponseInfo();
  16. explicit HttpServerResponseInfo(HttpStatusCode status_code);
  17. HttpServerResponseInfo(const HttpServerResponseInfo& other);
  18. ~HttpServerResponseInfo();
  19. static HttpServerResponseInfo CreateFor404();
  20. static HttpServerResponseInfo CreateFor500(const std::string& body);
  21. void AddHeader(const std::string& name, const std::string& value);
  22. // This also adds an appropriate Content-Length header.
  23. void SetBody(const std::string& body, const std::string& content_type);
  24. // Sets content-length and content-type. Body should be sent separately.
  25. void SetContentHeaders(size_t content_length,
  26. const std::string& content_type);
  27. std::string Serialize() const;
  28. HttpStatusCode status_code() const;
  29. const std::string& body() const;
  30. private:
  31. using Headers = base::StringPairs;
  32. HttpStatusCode status_code_;
  33. Headers headers_;
  34. std::string body_;
  35. };
  36. } // namespace net
  37. #endif // NET_SERVER_HTTP_SERVER_RESPONSE_INFO_H_