| 249 | ///@brief handle request |
| 250 | |
| 251 | void HttpSession::handle_request(bool cors) { |
| 252 | // --- BEGIN: Preflight (OPTIONS) request handling --- |
| 253 | if (req_.method() == http::verb::options && cors) { |
| 254 | header_print("✈️ ", "Handling Preflight (OPTIONS) request"); |
| 255 | |
| 256 | // reponse empty body for OPTIONS |
| 257 | auto options_res = std::make_shared<http::response<http::empty_body>>(); |
| 258 | options_res->version(req_.version()); |
| 259 | options_res->result(http::status::ok); |
| 260 | options_res->keep_alive(req_.keep_alive()); |
| 261 | |
| 262 | options_res->set("Access-Control-Allow-Origin", "*"); |
| 263 | options_res->set("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); |
| 264 | options_res->set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With"); |
| 265 | |
| 266 | options_res->prepare_payload(); |
| 267 | auto self = shared_from_this(); |
| 268 | http::async_write(socket_, *options_res, |
| 269 | [self, cors, options_res](beast::error_code ec, std::size_t) { |
| 270 | if (ec) { |
| 271 | return; |
| 272 | } |
| 273 | // waiting the true response |
| 274 | self->read_request(cors); |
| 275 | }); |
| 276 | return; |
| 277 | } |
| 278 | // --- END: Preflight (OPTIONS) request handling --- |
| 279 | |
| 280 | |
| 281 | // IF not OPTIONS |
| 282 | // Reset response |
| 283 | res_ = {}; |
| 284 | res_.version(req_.version()); |
| 285 | res_.keep_alive(req_.keep_alive()); |
| 286 | |
| 287 | // Handle the request through the server |
| 288 | bool deferred = server_.handle_request(req_, res_, socket_, shared_from_this()); |
| 289 | |
| 290 | // Clear the buffer after processing to prevent data accumulation |
| 291 | buffer_.consume(buffer_.size()); |
| 292 | |
| 293 | if (!is_streaming_ && !deferred) { |
| 294 | write_response(); |
| 295 | } else { |
| 296 | // For streaming responses, we need to handle connection cleanup differently |
| 297 | // The connection will be closed when streaming ends |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | ///@brief write response |
| 302 | void HttpSession::write_response() { |
no test coverage detected