xref: /aosp_15_r20/external/cronet/net/server/http_server_response_info.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/server/http_server_response_info.h"
6 
7 #include "base/check.h"
8 #include "base/format_macros.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/http/http_request_headers.h"
11 
12 namespace net {
13 
HttpServerResponseInfo()14 HttpServerResponseInfo::HttpServerResponseInfo() : status_code_(HTTP_OK) {}
15 
HttpServerResponseInfo(HttpStatusCode status_code)16 HttpServerResponseInfo::HttpServerResponseInfo(HttpStatusCode status_code)
17     : status_code_(status_code) {}
18 
19 HttpServerResponseInfo::HttpServerResponseInfo(
20     const HttpServerResponseInfo& other) = default;
21 
22 HttpServerResponseInfo::~HttpServerResponseInfo() = default;
23 
24 // static
CreateFor404()25 HttpServerResponseInfo HttpServerResponseInfo::CreateFor404() {
26   HttpServerResponseInfo response(HTTP_NOT_FOUND);
27   response.SetBody(std::string(), "text/html");
28   return response;
29 }
30 
31 // static
CreateFor500(const std::string & body)32 HttpServerResponseInfo HttpServerResponseInfo::CreateFor500(
33     const std::string& body) {
34   HttpServerResponseInfo response(HTTP_INTERNAL_SERVER_ERROR);
35   response.SetBody(body, "text/html");
36   return response;
37 }
38 
AddHeader(const std::string & name,const std::string & value)39 void HttpServerResponseInfo::AddHeader(const std::string& name,
40                                        const std::string& value) {
41   headers_.emplace_back(name, value);
42 }
43 
SetBody(const std::string & body,const std::string & content_type)44 void HttpServerResponseInfo::SetBody(const std::string& body,
45                                      const std::string& content_type) {
46   DCHECK(body_.empty());
47   body_ = body;
48   SetContentHeaders(body.length(), content_type);
49 }
50 
SetContentHeaders(size_t content_length,const std::string & content_type)51 void HttpServerResponseInfo::SetContentHeaders(
52     size_t content_length,
53     const std::string& content_type) {
54   AddHeader(HttpRequestHeaders::kContentLength,
55             base::StringPrintf("%" PRIuS, content_length));
56   AddHeader(HttpRequestHeaders::kContentType, content_type);
57 }
58 
Serialize() const59 std::string HttpServerResponseInfo::Serialize() const {
60   std::string response = base::StringPrintf(
61       "HTTP/1.1 %d %s\r\n", status_code_, GetHttpReasonPhrase(status_code_));
62   Headers::const_iterator header;
63   for (header = headers_.begin(); header != headers_.end(); ++header)
64     response += header->first + ":" + header->second + "\r\n";
65 
66   return response + "\r\n" + body_;
67 }
68 
status_code() const69 HttpStatusCode HttpServerResponseInfo::status_code() const {
70   return status_code_;
71 }
72 
body() const73 const std::string& HttpServerResponseInfo::body() const {
74   return body_;
75 }
76 
77 }  // namespace net
78