| 5040 | } |
| 5041 | |
| 5042 | inline bool Server::write_response_core(Stream &strm, bool close_connection, |
| 5043 | const Request &req, Response &res, |
| 5044 | bool need_apply_ranges) { |
| 5045 | assert(res.status != -1); |
| 5046 | |
| 5047 | if (400 <= res.status && error_handler_ && |
| 5048 | error_handler_(req, res) == HandlerResponse::Handled) { |
| 5049 | need_apply_ranges = true; |
| 5050 | } |
| 5051 | |
| 5052 | std::string content_type; |
| 5053 | std::string boundary; |
| 5054 | if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } |
| 5055 | |
| 5056 | // Prepare additional headers |
| 5057 | if (close_connection || req.get_header_value("Connection") == "close") { |
| 5058 | res.set_header("Connection", "close"); |
| 5059 | } else { |
| 5060 | std::stringstream ss; |
| 5061 | ss << "timeout=" << keep_alive_timeout_sec_ |
| 5062 | << ", max=" << keep_alive_max_count_; |
| 5063 | res.set_header("Keep-Alive", ss.str()); |
| 5064 | } |
| 5065 | |
| 5066 | if (!res.has_header("Content-Type") && |
| 5067 | (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) { |
| 5068 | res.set_header("Content-Type", "text/plain"); |
| 5069 | } |
| 5070 | |
| 5071 | if (!res.has_header("Content-Length") && res.body.empty() && |
| 5072 | !res.content_length_ && !res.content_provider_) { |
| 5073 | res.set_header("Content-Length", "0"); |
| 5074 | } |
| 5075 | |
| 5076 | if (!res.has_header("Accept-Ranges") && req.method == "HEAD") { |
| 5077 | res.set_header("Accept-Ranges", "bytes"); |
| 5078 | } |
| 5079 | |
| 5080 | if (post_routing_handler_) { post_routing_handler_(req, res); } |
| 5081 | |
| 5082 | // Response line and headers |
| 5083 | { |
| 5084 | detail::BufferStream bstrm; |
| 5085 | |
| 5086 | if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, |
| 5087 | detail::status_message(res.status))) { |
| 5088 | return false; |
| 5089 | } |
| 5090 | |
| 5091 | if (!detail::write_headers(bstrm, res.headers)) { return false; } |
| 5092 | |
| 5093 | // Flush buffer |
| 5094 | auto &data = bstrm.get_buffer(); |
| 5095 | strm.write(data.data(), data.size()); |
| 5096 | } |
| 5097 | |
| 5098 | // Body |
| 5099 | auto ret = true; |
nothing calls this directly
no test coverage detected