| 5442 | } |
| 5443 | |
| 5444 | inline bool Server::write_response_core(Stream &strm, bool close_connection, |
| 5445 | const Request &req, Response &res, |
| 5446 | bool need_apply_ranges) { |
| 5447 | assert(res.status != -1); |
| 5448 | |
| 5449 | if (400 <= res.status && error_handler_ && |
| 5450 | error_handler_(req, res) == HandlerResponse::Handled) { |
| 5451 | need_apply_ranges = true; |
| 5452 | } |
| 5453 | |
| 5454 | std::string content_type; |
| 5455 | std::string boundary; |
| 5456 | if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } |
| 5457 | |
| 5458 | // Prepare additional headers |
| 5459 | if (close_connection || req.get_header_value("Connection") == "close") { |
| 5460 | res.set_header("Connection", "close"); |
| 5461 | } else { |
| 5462 | std::stringstream ss; |
| 5463 | ss << "timeout=" << keep_alive_timeout_sec_ |
| 5464 | << ", max=" << keep_alive_max_count_; |
| 5465 | res.set_header("Keep-Alive", ss.str()); |
| 5466 | } |
| 5467 | |
| 5468 | if (!res.has_header("Content-Type") && |
| 5469 | (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) { |
| 5470 | res.set_header("Content-Type", "text/plain"); |
| 5471 | } |
| 5472 | |
| 5473 | if (!res.has_header("Content-Length") && res.body.empty() && |
| 5474 | !res.content_length_ && !res.content_provider_) { |
| 5475 | res.set_header("Content-Length", "0"); |
| 5476 | } |
| 5477 | |
| 5478 | if (!res.has_header("Accept-Ranges") && req.method == "HEAD") { |
| 5479 | res.set_header("Accept-Ranges", "bytes"); |
| 5480 | } |
| 5481 | |
| 5482 | if (post_routing_handler_) { post_routing_handler_(req, res); } |
| 5483 | |
| 5484 | // Response line and headers |
| 5485 | { |
| 5486 | detail::BufferStream bstrm; |
| 5487 | |
| 5488 | if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, |
| 5489 | detail::status_message(res.status))) { |
| 5490 | return false; |
| 5491 | } |
| 5492 | |
| 5493 | if (!detail::write_headers(bstrm, res.headers)) { return false; } |
| 5494 | |
| 5495 | // Flush buffer |
| 5496 | auto &data = bstrm.get_buffer(); |
| 5497 | detail::write_data(strm, data.data(), data.size()); |
| 5498 | } |
| 5499 | |
| 5500 | // Body |
| 5501 | auto ret = true; |
nothing calls this directly
no test coverage detected