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

CrowCpp / Crow / 265

18 May 2024 06:18PM UTC coverage: 87.784% (-0.07%) from 87.853%
265

Pull #818

gh-actions

gittiver
set version to 1.2 and build to build on branch only
Pull Request #818: Branch for version 1.2

3823 of 4355 relevant lines covered (87.78%)

109.27 hits per line

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

93.68
/include/crow/parser.h
1
#pragma once
2

3
#include <string>
4
#include <unordered_map>
5
#include <algorithm>
6

7
#include "crow/http_request.h"
8
#include "crow/http_parser_merged.h"
9

10
namespace crow
11
{
12
    /// A wrapper for `nodejs/http-parser`.
13

14
    ///
15
    /// Used to generate a \ref crow.request from the TCP socket buffer.
16
    template<typename Handler>
17
    struct HTTPParser : public http_parser
18
    {
19
        static int on_message_begin(http_parser*)
73✔
20
        {
21
            return 0;
73✔
22
        }
23
        static int on_method(http_parser* self_)
225✔
24
        {
25
            HTTPParser* self = static_cast<HTTPParser*>(self_);
225✔
26
            self->req.method = static_cast<HTTPMethod>(self->method);
225✔
27

28
            return 0;
225✔
29
        }
30
        static int on_url(http_parser* self_, const char* at, size_t length)
71✔
31
        {
32
            HTTPParser* self = static_cast<HTTPParser*>(self_);
71✔
33
            self->req.raw_url.insert(self->req.raw_url.end(), at, at + length);
71✔
34
            self->req.url_params = query_string(self->req.raw_url);
71✔
35
            self->req.url = self->req.raw_url.substr(0, self->qs_point != 0 ? self->qs_point : std::string::npos);
71✔
36

37
            self->process_url();
71✔
38

39
            return 0;
71✔
40
        }
41
        static int on_header_field(http_parser* self_, const char* at, size_t length)
65✔
42
        {
43
            HTTPParser* self = static_cast<HTTPParser*>(self_);
65✔
44
            switch (self->header_building_state)
65✔
45
            {
46
                case 0:
65✔
47
                    if (!self->header_value.empty())
65✔
48
                    {
49
                        self->req.headers.emplace(std::move(self->header_field), std::move(self->header_value));
21✔
50
                    }
51
                    self->header_field.assign(at, at + length);
65✔
52
                    self->header_building_state = 1;
65✔
53
                    break;
65✔
54
                case 1:
×
55
                    self->header_field.insert(self->header_field.end(), at, at + length);
×
56
                    break;
×
57
            }
58
            return 0;
65✔
59
        }
60
        static int on_header_value(http_parser* self_, const char* at, size_t length)
65✔
61
        {
62
            HTTPParser* self = static_cast<HTTPParser*>(self_);
65✔
63
            switch (self->header_building_state)
65✔
64
            {
65
                case 0:
×
66
                    self->header_value.insert(self->header_value.end(), at, at + length);
×
67
                    break;
×
68
                case 1:
65✔
69
                    self->header_building_state = 0;
65✔
70
                    self->header_value.assign(at, at + length);
65✔
71
                    break;
65✔
72
            }
73
            return 0;
65✔
74
        }
75
        static int on_headers_complete(http_parser* self_)
70✔
76
        {
77
            HTTPParser* self = static_cast<HTTPParser*>(self_);
70✔
78
            if (!self->header_field.empty())
70✔
79
            {
80
                self->req.headers.emplace(std::move(self->header_field), std::move(self->header_value));
44✔
81
            }
82

83
            self->set_connection_parameters();
70✔
84

85
            self->process_header();
70✔
86
            return 0;
70✔
87
        }
88
        static int on_body(http_parser* self_, const char* at, size_t length)
3✔
89
        {
90
            HTTPParser* self = static_cast<HTTPParser*>(self_);
3✔
91
            self->req.body.insert(self->req.body.end(), at, at + length);
3✔
92
            return 0;
3✔
93
        }
94
        static int on_message_complete(http_parser* self_)
76✔
95
        {
96
            HTTPParser* self = static_cast<HTTPParser*>(self_);
76✔
97

98
            self->message_complete = true;
76✔
99
            self->process_message();
76✔
100
            return 0;
73✔
101
        }
102
        HTTPParser(Handler* handler):
79✔
103
          handler_(handler)
79✔
104
        {
105
            http_parser_init(this);
79✔
106
        }
79✔
107

108
        // return false on error
109
        /// Parse a buffer into the different sections of an HTTP request.
110
        bool feed(const char* buffer, int length)
122✔
111
        {
112
            if (message_complete)
122✔
113
                return true;
11✔
114

115
            const static http_parser_settings settings_{
116
              on_message_begin,
117
              on_method,
118
              on_url,
119
              on_header_field,
120
              on_header_value,
121
              on_headers_complete,
122
              on_body,
123
              on_message_complete,
124
            };
125

126
            int nparsed = http_parser_execute(this, &settings_, buffer, length);
111✔
127
            if (http_errno != CHPE_OK)
108✔
128
            {
129
                return false;
14✔
130
            }
131
            return nparsed == length;
94✔
132
        }
133

134
        bool done()
47✔
135
        {
136
            return feed(nullptr, 0);
47✔
137
        }
138

139
        void clear()
114✔
140
        {
141
            req = crow::request();
114✔
142
            header_field.clear();
114✔
143
            header_value.clear();
114✔
144
            header_building_state = 0;
114✔
145
            qs_point = 0;
114✔
146
            message_complete = false;
114✔
147
            state = CROW_NEW_MESSAGE();
114✔
148
        }
114✔
149

150
        inline void process_url()
71✔
151
        {
152
            handler_->handle_url();
71✔
153
        }
71✔
154

155
        inline void process_header()
70✔
156
        {
157
            handler_->handle_header();
70✔
158
        }
70✔
159

160
        inline void process_message()
76✔
161
        {
162
            handler_->handle();
76✔
163
        }
73✔
164

165
        inline void set_connection_parameters()
70✔
166
        {
167
            req.http_ver_major = http_major;
70✔
168
            req.http_ver_minor = http_minor;
70✔
169

170
            //NOTE(EDev): it seems that the problem is with crow's policy on closing the connection for HTTP_VERSION < 1.0, the behaviour for that in crow is "don't close the connection, but don't send a keep-alive either"
171

172
            // HTTP1.1 = always send keep_alive, HTTP1.0 = only send if header exists, HTTP?.? = never send
173
            req.keep_alive = (http_major == 1 && http_minor == 0) ?
135✔
174
                               ((flags & F_CONNECTION_KEEP_ALIVE) ? true : false) :
5✔
175
                               ((http_major == 1 && http_minor == 1) ? true : false);
65✔
176

177
            // HTTP1.1 = only close if close header exists, HTTP1.0 = always close unless keep_alive header exists, HTTP?.?= never close
178
            req.close_connection = (http_major == 1 && http_minor == 0) ?
135✔
179
                                     ((flags & F_CONNECTION_KEEP_ALIVE) ? false : true) :
5✔
180
                                     ((http_major == 1 && http_minor == 1) ? ((flags & F_CONNECTION_CLOSE) ? true : false) : false);
65✔
181
            req.upgrade = static_cast<bool>(upgrade);
70✔
182
        }
70✔
183

184
        /// The final request that this parser outputs.
185
        ///
186
        /// Data parsed is put directly into this object as soon as the related callback returns. (e.g. the request will have the cooorect method as soon as on_method() returns)
187
        request req;
188

189
    private:
190
        int header_building_state = 0;
191
        bool message_complete = false;
192
        std::string header_field;
193
        std::string header_value;
194

195
        Handler* handler_; ///< This is currently an HTTP connection object (\ref crow.Connection).
196
    };
197
} // namespace crow
198

199
#undef CROW_NEW_MESSAGE
200
#undef CROW_start_state
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