• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CrowCpp / Crow / 687

16 Jun 2025 05:17PM UTC coverage: 87.332% (-0.3%) from 87.617%
687

push

gh-actions

gittiver
Merge branch 'unix_socket' into master_merge_unix_socket

* unix_socket:
  add unix_socket test
  fix unittesterror
  add example for unix socket
  set reuse addr false
  add handle_upgrade for unix socket
  add unix domain socket

90 of 114 new or added lines in 7 files covered. (78.95%)

2 existing lines in 2 files now uncovered.

4088 of 4681 relevant lines covered (87.33%)

400.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.83
/include/crow/http_connection.h
1
#pragma once
2

3
#ifdef CROW_USE_BOOST
4
#include <boost/asio.hpp>
5
#else
6
#ifndef ASIO_STANDALONE
7
#define ASIO_STANDALONE
8
#endif
9
#include <asio.hpp>
10
#endif
11

12
#include <algorithm>
13
#include <atomic>
14
#include <chrono>
15
#include <memory>
16
#include <vector>
17

18
#include "crow/http_parser_merged.h"
19
#include "crow/common.h"
20
#include "crow/compression.h"
21
#include "crow/http_response.h"
22
#include "crow/logging.h"
23
#include "crow/middleware.h"
24
#include "crow/middleware_context.h"
25
#include "crow/parser.h"
26
#include "crow/settings.h"
27
#include "crow/socket_adaptors.h"
28
#include "crow/task_timer.h"
29
#include "crow/utility.h"
30

31
namespace crow
32
{
33
#ifdef CROW_USE_BOOST
34
    namespace asio = boost::asio;
35
    using error_code = boost::system::error_code;
36
#else
37
    using error_code = asio::error_code;
38
#endif
39
    using tcp = asio::ip::tcp;
40

41
#ifdef CROW_ENABLE_DEBUG
42
    static std::atomic<int> connectionCount;
43
#endif
44

45
    /// An HTTP connection.
46
    template<typename Adaptor, typename Handler, typename... Middlewares>
47
    class Connection : public std::enable_shared_from_this<Connection<Adaptor, Handler, Middlewares...>>
48
    {
49
        friend struct crow::response;
50

51
    public:
52
        Connection(
303✔
53
          asio::io_context& io_context,
54
          Handler* handler,
55
          const std::string& server_name,
56
          std::tuple<Middlewares...>* middlewares,
57
          std::function<std::string()>& get_cached_date_str_f,
58
          detail::task_timer& task_timer,
59
          typename Adaptor::context* adaptor_ctx_,
60
          std::atomic<unsigned int>& queue_length):
61
          adaptor_(io_context, adaptor_ctx_),
303✔
62
          handler_(handler),
303✔
63
          parser_(this),
303✔
64
          req_(parser_.req),
303✔
65
          server_name_(server_name),
303✔
66
          middlewares_(middlewares),
303✔
67
          get_cached_date_str(get_cached_date_str_f),
303✔
68
          task_timer_(task_timer),
303✔
69
          res_stream_threshold_(handler->stream_threshold()),
555✔
70
          queue_length_(queue_length)
606✔
71
        {
72
            queue_length_++;
303✔
73
#ifdef CROW_ENABLE_DEBUG
74
            connectionCount++;
297✔
75
            CROW_LOG_DEBUG << "Connection (" << this << ") allocated, total: " << connectionCount;
297✔
76
#endif
77
        }
303✔
78

79
        ~Connection()
303✔
80
        {
81
            queue_length_--;
303✔
82
#ifdef CROW_ENABLE_DEBUG
83
            connectionCount--;
297✔
84
            CROW_LOG_DEBUG << "Connection (" << this << ") freed, total: " << connectionCount;
297✔
85
#endif
86
        }
303✔
87

88
        /// The TCP socket on top of which the connection is established.
89
        decltype(std::declval<Adaptor>().raw_socket())& socket()
303✔
90
        {
91
            return adaptor_.raw_socket();
303✔
92
        }
93

94
        void start()
204✔
95
        {
96
            auto self = this->shared_from_this();
204✔
97
            adaptor_.start([self](const error_code& ec) {
408✔
98
                if (!ec)
204✔
99
                {
100
                    self->start_deadline();
204✔
101
                    self->parser_.clear();
204✔
102

103
                    self->do_read();
204✔
104
                }
105
                else
106
                {
107
                    CROW_LOG_ERROR << "Could not start adaptor: " << ec.message();
×
108
                }
109
            });
110
        }
204✔
111

112
        void handle_url()
249✔
113
        {
114
            routing_handle_result_ = handler_->handle_initial(req_, res);
249✔
115
            // if no route is found for the request method, return the response without parsing or processing anything further.
116
            if (!routing_handle_result_->rule_index)
249✔
117
            {
118
                parser_.done();
9✔
119
                need_to_call_after_handlers_ = true;
9✔
120
                complete_request();
9✔
121
            }
122
        }
249✔
123

124
        void handle_header()
240✔
125
        {
126
            // HTTP 1.1 Expect: 100-continue
127
            if (req_.http_ver_major == 1 && req_.http_ver_minor == 1 && get_header_value(req_.headers, "expect") == "100-continue")
456✔
128
            {
129
                continue_requested = true;
×
130
                buffers_.clear();
×
131
                static std::string expect_100_continue = "HTTP/1.1 100 Continue\r\n\r\n";
132
                buffers_.emplace_back(expect_100_continue.data(), expect_100_continue.size());
×
133
                do_write_sync(buffers_);
×
134
            }
135
        }
240✔
136

137
        void handle()
240✔
138
        {
139
            // TODO(EDev): cancel_deadline_timer should be looked into, it might be a good idea to add it to handle_url() and then restart the timer once everything passes
140
            cancel_deadline_timer();
240✔
141
            bool is_invalid_request = false;
240✔
142
            add_keep_alive_ = false;
240✔
143

144
            // Create context
145
            ctx_ = detail::context<Middlewares...>();
240✔
146
            req_.middleware_context = static_cast<void*>(&ctx_);
240✔
147
            req_.middleware_container = static_cast<void*>(middlewares_);
240✔
148
            req_.io_context = &adaptor_.get_io_context();
240✔
149
            req_.remote_ip_address = adaptor_.address();
240✔
150
            add_keep_alive_ = req_.keep_alive;
240✔
151
            close_connection_ = req_.close_connection;
240✔
152

153
            if (req_.check_version(1, 1)) // HTTP/1.1
240✔
154
            {
155
                if (!req_.headers.count("host"))
324✔
156
                {
157
                    is_invalid_request = true;
3✔
158
                    res = response(400);
3✔
159
                }
160
                else if (req_.upgrade)
105✔
161
                {
162
                    // h2 or h2c headers
163
                    if (req_.get_header_value("upgrade").find("h2")==0)
81✔
164
                    {
165
                        // TODO(ipkn): HTTP/2
166
                        // currently, ignore upgrade header
167
                    }
168
                    else
169
                    {
170

171
                        detail::middleware_call_helper<detail::middleware_call_criteria_only_global,
172
                                                       0, decltype(ctx_), decltype(*middlewares_)>({}, *middlewares_, req_, res, ctx_);
27✔
173
                        close_connection_ = true;
27✔
174
                        handler_->handle_upgrade(req_, res, std::move(adaptor_));
27✔
175
                        return;
27✔
176
                    }
177
                }
178
            }
179

180
            CROW_LOG_INFO << "Request: " << utility::lexical_cast<std::string>(adaptor_.remote_endpoint()) << " " << this << " HTTP/" << (char)(req_.http_ver_major + '0') << "." << (char)(req_.http_ver_minor + '0') << ' ' << method_name(req_.method) << " " << req_.url;
213✔
181

182

183
            need_to_call_after_handlers_ = false;
213✔
184
            if (!is_invalid_request)
213✔
185
            {
186
                res.complete_request_handler_ = nullptr;
210✔
187
                auto self = this->shared_from_this();
210✔
188
                res.is_alive_helper_ = [self]() -> bool {
210✔
189
                    return self->adaptor_.is_open();
×
190
                };
191

192
                detail::middleware_call_helper<detail::middleware_call_criteria_only_global,
193
                                               0, decltype(ctx_), decltype(*middlewares_)>({}, *middlewares_, req_, res, ctx_);
210✔
194

195
                if (!res.completed_)
210✔
196
                {
197
                    res.complete_request_handler_ = [self] {
621✔
198
                        self->complete_request();
207✔
199
                    };
200
                    need_to_call_after_handlers_ = true;
207✔
201
                    handler_->handle(req_, res, routing_handle_result_);
207✔
202
                    if (add_keep_alive_)
207✔
203
                        res.set_header("connection", "Keep-Alive");
390✔
204
                }
205
                else
206
                {
207
                    complete_request();
3✔
208
                }
209
            }
210✔
210
            else
211
            {
212
                complete_request();
3✔
213
            }
214
        }
215

216
        /// Call the after handle middleware and send the write the response to the connection.
217
        void complete_request()
222✔
218
        {
219
            CROW_LOG_INFO << "Response: " << this << ' ' << req_.raw_url << ' ' << res.code << ' ' << close_connection_;
222✔
220
            res.is_alive_helper_ = nullptr;
222✔
221

222
            if (need_to_call_after_handlers_)
222✔
223
            {
224
                need_to_call_after_handlers_ = false;
216✔
225

226
                // call all after_handler of middlewares
227
                detail::after_handlers_call_helper<
228
                  detail::middleware_call_criteria_only_global,
229
                  (static_cast<int>(sizeof...(Middlewares)) - 1),
230
                  decltype(ctx_),
231
                  decltype(*middlewares_)>({}, *middlewares_, ctx_, req_, res);
216✔
232
            }
233
#ifdef CROW_ENABLE_COMPRESSION
234
            if (!res.body.empty() && handler_->compression_used())
222✔
235
            {
236
                std::string accept_encoding = req_.get_header_value("Accept-Encoding");
36✔
237
                if (!accept_encoding.empty() && res.compressed)
18✔
238
                {
239
                    switch (handler_->compression_algorithm())
6✔
240
                    {
241
                        case compression::DEFLATE:
3✔
242
                            if (accept_encoding.find("deflate") != std::string::npos)
3✔
243
                            {
244
                                res.body = compression::compress_string(res.body, compression::algorithm::DEFLATE);
3✔
245
                                res.set_header("Content-Encoding", "deflate");
15✔
246
                            }
247
                            break;
3✔
248
                        case compression::GZIP:
3✔
249
                            if (accept_encoding.find("gzip") != std::string::npos)
3✔
250
                            {
251
                                res.body = compression::compress_string(res.body, compression::algorithm::GZIP);
3✔
252
                                res.set_header("Content-Encoding", "gzip");
15✔
253
                            }
254
                            break;
3✔
255
                        default:
×
256
                            break;
×
257
                    }
258
                }
259
            }
18✔
260
#endif
261

262
            prepare_buffers();
222✔
263

264
            if (res.is_static_type())
222✔
265
            {
266
                do_write_static();
×
267
            }
268
            else
269
            {
270
                do_write_general();
222✔
271
            }
272
        }
222✔
273

274
    private:
275
        void prepare_buffers()
222✔
276
        {
277
            res.complete_request_handler_ = nullptr;
222✔
278
            res.is_alive_helper_ = nullptr;
222✔
279

280
            if (!adaptor_.is_open())
222✔
281
            {
282
                //CROW_LOG_DEBUG << this << " delete (socket is closed) " << is_reading << ' ' << is_writing;
283
                //delete this;
284
                return;
×
285
            }
286
            // TODO(EDev): HTTP version in status codes should be dynamic
287
            // Keep in sync with common.h/status
288
            static std::unordered_map<int, std::string> statusCodes = {
1,302✔
289
              {status::CONTINUE, "HTTP/1.1 100 Continue\r\n"},
×
290
              {status::SWITCHING_PROTOCOLS, "HTTP/1.1 101 Switching Protocols\r\n"},
×
291

292
              {status::OK, "HTTP/1.1 200 OK\r\n"},
×
293
              {status::CREATED, "HTTP/1.1 201 Created\r\n"},
×
294
              {status::ACCEPTED, "HTTP/1.1 202 Accepted\r\n"},
×
295
              {status::NON_AUTHORITATIVE_INFORMATION, "HTTP/1.1 203 Non-Authoritative Information\r\n"},
×
296
              {status::NO_CONTENT, "HTTP/1.1 204 No Content\r\n"},
×
297
              {status::RESET_CONTENT, "HTTP/1.1 205 Reset Content\r\n"},
×
298
              {status::PARTIAL_CONTENT, "HTTP/1.1 206 Partial Content\r\n"},
×
299

300
              {status::MULTIPLE_CHOICES, "HTTP/1.1 300 Multiple Choices\r\n"},
×
301
              {status::MOVED_PERMANENTLY, "HTTP/1.1 301 Moved Permanently\r\n"},
×
302
              {status::FOUND, "HTTP/1.1 302 Found\r\n"},
×
303
              {status::SEE_OTHER, "HTTP/1.1 303 See Other\r\n"},
×
304
              {status::NOT_MODIFIED, "HTTP/1.1 304 Not Modified\r\n"},
×
305
              {status::TEMPORARY_REDIRECT, "HTTP/1.1 307 Temporary Redirect\r\n"},
×
306
              {status::PERMANENT_REDIRECT, "HTTP/1.1 308 Permanent Redirect\r\n"},
×
307

308
              {status::BAD_REQUEST, "HTTP/1.1 400 Bad Request\r\n"},
×
309
              {status::UNAUTHORIZED, "HTTP/1.1 401 Unauthorized\r\n"},
×
310
              {status::FORBIDDEN, "HTTP/1.1 403 Forbidden\r\n"},
×
311
              {status::NOT_FOUND, "HTTP/1.1 404 Not Found\r\n"},
×
312
              {status::METHOD_NOT_ALLOWED, "HTTP/1.1 405 Method Not Allowed\r\n"},
×
313
              {status::NOT_ACCEPTABLE, "HTTP/1.1 406 Not Acceptable\r\n"},
×
314
              {status::PROXY_AUTHENTICATION_REQUIRED, "HTTP/1.1 407 Proxy Authentication Required\r\n"},
×
315
              {status::CONFLICT, "HTTP/1.1 409 Conflict\r\n"},
×
316
              {status::GONE, "HTTP/1.1 410 Gone\r\n"},
×
317
              {status::PAYLOAD_TOO_LARGE, "HTTP/1.1 413 Payload Too Large\r\n"},
×
318
              {status::UNSUPPORTED_MEDIA_TYPE, "HTTP/1.1 415 Unsupported Media Type\r\n"},
×
319
              {status::RANGE_NOT_SATISFIABLE, "HTTP/1.1 416 Range Not Satisfiable\r\n"},
×
320
              {status::EXPECTATION_FAILED, "HTTP/1.1 417 Expectation Failed\r\n"},
×
321
              {status::PRECONDITION_REQUIRED, "HTTP/1.1 428 Precondition Required\r\n"},
×
322
              {status::TOO_MANY_REQUESTS, "HTTP/1.1 429 Too Many Requests\r\n"},
×
323
              {status::UNAVAILABLE_FOR_LEGAL_REASONS, "HTTP/1.1 451 Unavailable For Legal Reasons\r\n"},
×
324

325
              {status::INTERNAL_SERVER_ERROR, "HTTP/1.1 500 Internal Server Error\r\n"},
×
326
              {status::NOT_IMPLEMENTED, "HTTP/1.1 501 Not Implemented\r\n"},
×
327
              {status::BAD_GATEWAY, "HTTP/1.1 502 Bad Gateway\r\n"},
×
328
              {status::SERVICE_UNAVAILABLE, "HTTP/1.1 503 Service Unavailable\r\n"},
×
329
              {status::GATEWAY_TIMEOUT, "HTTP/1.1 504 Gateway Timeout\r\n"},
×
330
              {status::VARIANT_ALSO_NEGOTIATES, "HTTP/1.1 506 Variant Also Negotiates\r\n"},
27✔
331
            };
332

333
            static const std::string seperator = ": ";
276✔
334

335
            buffers_.clear();
222✔
336
            buffers_.reserve(4 * (res.headers.size() + 5) + 3);
219✔
337

338
            if (!statusCodes.count(res.code))
222✔
339
            {
340
                CROW_LOG_WARNING << this << " status code "
6✔
341
                                 << "(" << res.code << ")"
3✔
342
                                 << " not defined, returning 500 instead";
3✔
343
                res.code = 500;
3✔
344
            }
345

346
            auto& status = statusCodes.find(res.code)->second;
222✔
347
            buffers_.emplace_back(status.data(), status.size());
222✔
348

349
            if (res.code >= 400 && res.body.empty())
222✔
350
                res.body = statusCodes[res.code].substr(9);
6✔
351

352
            for (auto& kv : res.headers)
312✔
353
            {
354
                buffers_.emplace_back(kv.first.data(), kv.first.size());
90✔
355
                buffers_.emplace_back(seperator.data(), seperator.size());
90✔
356
                buffers_.emplace_back(kv.second.data(), kv.second.size());
90✔
357
                buffers_.emplace_back(crlf.data(), crlf.size());
90✔
358
            }
359

360
            if (!res.manual_length_header && !res.headers.count("content-length"))
666✔
361
            {
362
                content_length_ = std::to_string(res.body.size());
222✔
363
                static std::string content_length_tag = "Content-Length: ";
273✔
364
                buffers_.emplace_back(content_length_tag.data(), content_length_tag.size());
219✔
365
                buffers_.emplace_back(content_length_.data(), content_length_.size());
222✔
366
                buffers_.emplace_back(crlf.data(), crlf.size());
222✔
367
            }
368
            if (!res.headers.count("server") && !server_name_.empty())
666✔
369
            {
370
                static std::string server_tag = "Server: ";
276✔
371
                buffers_.emplace_back(server_tag.data(), server_tag.size());
222✔
372
                buffers_.emplace_back(server_name_.data(), server_name_.size());
222✔
373
                buffers_.emplace_back(crlf.data(), crlf.size());
222✔
374
            }
375
            if (!res.headers.count("date"))
666✔
376
            {
377
                static std::string date_tag = "Date: ";
276✔
378
                date_str_ = get_cached_date_str();
222✔
379
                buffers_.emplace_back(date_tag.data(), date_tag.size());
222✔
380
                buffers_.emplace_back(date_str_.data(), date_str_.size());
222✔
381
                buffers_.emplace_back(crlf.data(), crlf.size());
222✔
382
            }
383
            if (add_keep_alive_)
219✔
384
            {
385
                static std::string keep_alive_tag = "Connection: Keep-Alive";
87✔
386
                buffers_.emplace_back(keep_alive_tag.data(), keep_alive_tag.size());
81✔
387
                buffers_.emplace_back(crlf.data(), crlf.size());
81✔
388
            }
389

390
            buffers_.emplace_back(crlf.data(), crlf.size());
219✔
391
        }
27✔
392

393
        void do_write_static()
×
394
        {
395
            asio::write(adaptor_.socket(), buffers_);
×
396

397
            if (res.file_info.statResult == 0)
×
398
            {
399
                std::ifstream is(res.file_info.path.c_str(), std::ios::in | std::ios::binary);
×
400
                std::vector<asio::const_buffer> buffers{1};
×
401
                char buf[16384];
402
                is.read(buf, sizeof(buf));
×
403
                while (is.gcount() > 0)
×
404
                {
405
                    buffers[0] = asio::buffer(buf, is.gcount());
×
406
                    do_write_sync(buffers);
×
407
                    is.read(buf, sizeof(buf));
×
408
                }
409
            }
410
            if (close_connection_)
×
411
            {
412
                adaptor_.shutdown_readwrite();
×
413
                adaptor_.close();
×
414
                CROW_LOG_DEBUG << this << " from write (static)";
×
415
            }
416

417
            res.end();
×
418
            res.clear();
×
419
            buffers_.clear();
×
420
            parser_.clear();
×
421
        }
422

423
        void do_write_general()
219✔
424
        {
425
            if (res.body.length() < res_stream_threshold_)
219✔
426
            {
427
                res_body_copy_.swap(res.body);
219✔
428
                buffers_.emplace_back(res_body_copy_.data(), res_body_copy_.size());
219✔
429

430
                do_write_sync(buffers_);
219✔
431

432
                if (need_to_start_read_after_complete_)
219✔
433
                {
434
                    need_to_start_read_after_complete_ = false;
×
435
                    start_deadline();
×
436
                    do_read();
×
437
                }
438
            }
439
            else
440
            {
UNCOV
441
                asio::write(adaptor_.socket(), buffers_); // Write the response start / headers
×
442
                cancel_deadline_timer();
3✔
443
                if (res.body.length() > 0)
3✔
444
                {
445
                    std::vector<asio::const_buffer> buffers{1};
3✔
446
                    const uint8_t* data = reinterpret_cast<const uint8_t*>(res.body.data());
3✔
447
                    size_t length = res.body.length();
3✔
448
                    for (size_t transferred = 0; transferred < length;)
234✔
449
                    {
450
                        size_t to_transfer = CROW_MIN(16384UL, length - transferred);
231✔
451
                        buffers[0] = asio::const_buffer(data + transferred, to_transfer);
231✔
452
                        do_write_sync(buffers);
231✔
453
                        transferred += to_transfer;
231✔
454
                    }
455
                }
3✔
456
                if (close_connection_)
3✔
457
                {
458
                    adaptor_.shutdown_readwrite();
3✔
459
                    adaptor_.close();
3✔
460
                    CROW_LOG_DEBUG << this << " from write (res_stream)";
3✔
461
                }
462

463
                res.end();
3✔
464
                res.clear();
3✔
465
                buffers_.clear();
3✔
466
                parser_.clear();
3✔
467
            }
468
        }
222✔
469

470
        void do_read()
408✔
471
        {
472
            auto self = this->shared_from_this();
408✔
473
            adaptor_.socket().async_read_some(
816✔
474
              asio::buffer(buffer_),
408✔
475
              [self](const error_code& ec, std::size_t bytes_transferred) {
1,203✔
476
                  bool error_while_reading = true;
387✔
477
                  if (!ec)
387✔
478
                  {
479
                      bool ret = self->parser_.feed(self->buffer_.data(), bytes_transferred);
528✔
480
                      if (ret && self->adaptor_.is_open())
264✔
481
                      {
482
                          error_while_reading = false;
216✔
483
                      }
484
                  }
485

486
                  if (error_while_reading)
384✔
487
                  {
488
                      self->cancel_deadline_timer();
168✔
489
                      self->parser_.done();
168✔
490
                      self->adaptor_.shutdown_read();
168✔
491
                      self->adaptor_.close();
168✔
492
                      CROW_LOG_DEBUG << self << " from read(1) with description: \"" << http_errno_description(static_cast<http_errno>(self->parser_.http_errno)) << '\"';
168✔
493
                  }
494
                  else if (self->close_connection_)
216✔
495
                  {
496
                      self->cancel_deadline_timer();
15✔
497
                      self->parser_.done();
15✔
498
                      // adaptor will close after write
499
                  }
500
                  else if (!self->need_to_call_after_handlers_)
201✔
501
                  {
502
                      self->start_deadline();
204✔
503
                      self->do_read();
204✔
504
                  }
505
                  else
506
                  {
507
                      // res will be completed later by user
508
                      self->need_to_start_read_after_complete_ = true;
×
509
                  }
510
              });
511
        }
408✔
512

513
        void do_write()
514
        {
515
            auto self = this->shared_from_this();
516
            asio::async_write(
517
              adaptor_.socket(), buffers_,
518
              [self](const error_code& ec, std::size_t /*bytes_transferred*/) {
519
                  self->res.clear();
520
                  self->res_body_copy_.clear();
521
                  if (!self->continue_requested)
522
                  {
523
                      self->parser_.clear();
524
                  }
525
                  else
526
                  {
527
                      self->continue_requested = false;
528
                  }
529

530
                  if (!ec)
531
                  {
532
                      if (self->close_connection_)
533
                      {
534
                          self->adaptor_.shutdown_write();
535
                          self->adaptor_.close();
536
                          CROW_LOG_DEBUG << self << " from write(1)";
537
                      }
538
                  }
539
                  else
540
                  {
541
                      CROW_LOG_DEBUG << self << " from write(2)";
542
                  }
543
              });
544
        }
545

546
        inline void do_write_sync(std::vector<asio::const_buffer>& buffers)
450✔
547
        {
548
            error_code ec;
450✔
549
            asio::write(adaptor_.socket(), buffers, ec);
447✔
550

551
            this->res.clear();
450✔
552
            this->res_body_copy_.clear();
450✔
553
            if (this->continue_requested)
450✔
554
            {
555
                this->continue_requested = false;
×
556
            }
557
            else
558
            {
559
                this->parser_.clear();
450✔
560
            }
561

562
            if (ec)
450✔
563
            {
564
                CROW_LOG_ERROR << ec << " - happened while sending buffers";
×
565
                CROW_LOG_DEBUG << this << " from write (sync)(2)";
×
566
            }
567
        }
447✔
568

569
        void cancel_deadline_timer()
834✔
570
        {
571
            CROW_LOG_DEBUG << this << " timer cancelled: " << &task_timer_ << ' ' << task_id_;
834✔
572
            task_timer_.cancel(task_id_);
834✔
573
        }
834✔
574

575
        void start_deadline(/*int timeout = 5*/)
408✔
576
        {
577
            cancel_deadline_timer();
408✔
578

579
            auto self = this->shared_from_this();
408✔
580
            task_id_ = task_timer_.schedule([self] {
417✔
581
                if (!self->adaptor_.is_open())
9✔
582
                {
583
                    return;
×
584
                }
585
                self->adaptor_.shutdown_readwrite();
9✔
586
                self->adaptor_.close();
9✔
587
            });
588
            CROW_LOG_DEBUG << this << " timer added: " << &task_timer_ << ' ' << task_id_;
408✔
589
        }
408✔
590

591
    private:
592
        Adaptor adaptor_;
593
        Handler* handler_;
594

595
        std::array<char, 4096> buffer_;
596

597
        HTTPParser<Connection> parser_;
598
        std::unique_ptr<routing_handle_result> routing_handle_result_;
599
        request& req_;
600
        response res;
601

602
        bool close_connection_ = false;
603

604
        const std::string& server_name_;
605
        std::vector<asio::const_buffer> buffers_;
606

607
        std::string content_length_;
608
        std::string date_str_;
609
        std::string res_body_copy_;
610

611
        detail::task_timer::identifier_type task_id_{};
612

613
        bool continue_requested{};
614
        bool need_to_call_after_handlers_{};
615
        bool need_to_start_read_after_complete_{};
616
        bool add_keep_alive_{};
617

618
        std::tuple<Middlewares...>* middlewares_;
619
        detail::context<Middlewares...> ctx_;
620

621
        std::function<std::string()>& get_cached_date_str;
622
        detail::task_timer& task_timer_;
623

624
        size_t res_stream_threshold_;
625

626
        std::atomic<unsigned int>& queue_length_;
627
    };
628

629
} // namespace crow
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc