| 5840 | } |
| 5841 | |
| 5842 | inline bool Server::write_response_core(Stream &strm, bool close_connection, |
| 5843 | const Request &req, Response &res, |
| 5844 | bool need_apply_ranges) { |
| 5845 | assert(res.status != -1); |
| 5846 | |
| 5847 | if (400 <= res.status && error_handler_ && |
| 5848 | error_handler_(req, res) == HandlerResponse::Handled) { |
| 5849 | need_apply_ranges = true; |
| 5850 | } |
| 5851 | |
| 5852 | std::string content_type; |
| 5853 | std::string boundary; |
| 5854 | if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } |
| 5855 | |
| 5856 | // Prepare additional headers |
| 5857 | if (close_connection || req.get_header_value("Connection") == "close") { |
| 5858 | res.set_header("Connection", "close"); |
| 5859 | } else { |
| 5860 | std::stringstream ss; |
| 5861 | ss << "timeout=" << keep_alive_timeout_sec_ |
| 5862 | << ", max=" << keep_alive_max_count_; |
| 5863 | res.set_header("Keep-Alive", ss.str()); |
| 5864 | } |
| 5865 | |
| 5866 | if (!res.has_header("Content-Type") && |
| 5867 | (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) { |
| 5868 | res.set_header("Content-Type", "text/plain"); |
| 5869 | } |
| 5870 | |
| 5871 | if (!res.has_header("Content-Length") && res.body.empty() && |
| 5872 | !res.content_length_ && !res.content_provider_) { |
| 5873 | res.set_header("Content-Length", "0"); |
| 5874 | } |
| 5875 | |
| 5876 | if (!res.has_header("Accept-Ranges") && req.method == "HEAD") { |
| 5877 | res.set_header("Accept-Ranges", "bytes"); |
| 5878 | } |
| 5879 | |
| 5880 | if (post_routing_handler_) { post_routing_handler_(req, res); } |
| 5881 | |
| 5882 | // Response line and headers |
| 5883 | { |
| 5884 | detail::BufferStream bstrm; |
| 5885 | |
| 5886 | if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, |
| 5887 | status_message(res.status))) { |
| 5888 | return false; |
| 5889 | } |
| 5890 | |
| 5891 | if (!header_writer_(bstrm, res.headers)) { return false; } |
| 5892 | |
| 5893 | // Flush buffer |
| 5894 | auto &data = bstrm.get_buffer(); |
| 5895 | detail::write_data(strm, data.data(), data.size()); |
| 5896 | } |
| 5897 | |
| 5898 | // Body |
| 5899 | auto ret = true; |
nothing calls this directly
no test coverage detected