@@ -72,22 +72,6 @@ const uint32_t kOnMessageComplete = 3;
72
72
const uint32_t kOnExecute = 4 ;
73
73
74
74
75
- #define HTTP_CB (name ) \
76
- static int name (http_parser* p_) { \
77
- Parser* self = ContainerOf (&Parser::parser_, p_); \
78
- return self->name ##_ (); \
79
- } \
80
- int name##_()
81
-
82
-
83
- #define HTTP_DATA_CB (name ) \
84
- static int name (http_parser* p_, const char * at, size_t length) { \
85
- Parser* self = ContainerOf (&Parser::parser_, p_); \
86
- return self->name ##_ (at, length); \
87
- } \
88
- int name##_(const char * at, size_t length)
89
-
90
-
91
75
// helper class for the Parser
92
76
struct StringPtr {
93
77
StringPtr () {
@@ -182,27 +166,27 @@ class Parser : public AsyncWrap {
182
166
}
183
167
184
168
185
- HTTP_CB ( on_message_begin) {
169
+ int on_message_begin ( ) {
186
170
num_fields_ = num_values_ = 0 ;
187
171
url_.Reset ();
188
172
status_message_.Reset ();
189
173
return 0 ;
190
174
}
191
175
192
176
193
- HTTP_DATA_CB ( on_url) {
177
+ int on_url ( const char * at, size_t length ) {
194
178
url_.Update (at, length);
195
179
return 0 ;
196
180
}
197
181
198
182
199
- HTTP_DATA_CB ( on_status) {
183
+ int on_status ( const char * at, size_t length ) {
200
184
status_message_.Update (at, length);
201
185
return 0 ;
202
186
}
203
187
204
188
205
- HTTP_DATA_CB ( on_header_field) {
189
+ int on_header_field ( const char * at, size_t length ) {
206
190
if (num_fields_ == num_values_) {
207
191
// start of new field name
208
192
num_fields_++;
@@ -224,7 +208,7 @@ class Parser : public AsyncWrap {
224
208
}
225
209
226
210
227
- HTTP_DATA_CB ( on_header_value) {
211
+ int on_header_value ( const char * at, size_t length ) {
228
212
if (num_values_ != num_fields_) {
229
213
// start of new header value
230
214
num_values_++;
@@ -240,7 +224,7 @@ class Parser : public AsyncWrap {
240
224
}
241
225
242
226
243
- HTTP_CB ( on_headers_complete) {
227
+ int on_headers_complete ( ) {
244
228
// Arguments for the on-headers-complete javascript callback. This
245
229
// list needs to be kept in sync with the actual argument list for
246
230
// `parserOnHeadersComplete` in lib/_http_common.js.
@@ -317,7 +301,7 @@ class Parser : public AsyncWrap {
317
301
}
318
302
319
303
320
- HTTP_DATA_CB ( on_body) {
304
+ int on_body ( const char * at, size_t length ) {
321
305
EscapableHandleScope scope (env ()->isolate ());
322
306
323
307
Local<Object> obj = object ();
@@ -354,7 +338,7 @@ class Parser : public AsyncWrap {
354
338
}
355
339
356
340
357
- HTTP_CB ( on_message_complete) {
341
+ int on_message_complete ( ) {
358
342
HandleScope scope (env ()->isolate ());
359
343
360
344
if (num_fields_)
@@ -751,21 +735,35 @@ class Parser : public AsyncWrap {
751
735
StreamResource::Callback<StreamResource::AllocCb> prev_alloc_cb_;
752
736
StreamResource::Callback<StreamResource::ReadCb> prev_read_cb_;
753
737
int refcount_ = 1 ;
738
+
739
+ // These are helper functions for filling `http_parser_settings`, which turn
740
+ // a member function of Parser into a C-style HTTP parser callback.
741
+ template <typename Parser, Parser> struct Proxy ;
742
+ template <typename Parser, typename ...Args, int (Parser::*Member)(Args...)>
743
+ struct Proxy <int (Parser::*)(Args...), Member> {
744
+ static int Raw (http_parser* p, Args ... args) {
745
+ Parser* parser = ContainerOf (&Parser::parser_, p);
746
+ return (parser->*Member)(std::forward<Args>(args)...);
747
+ }
748
+ };
749
+
750
+ typedef int (Parser::*Call)();
751
+ typedef int (Parser::*DataCall)(const char * at, size_t length);
752
+
754
753
static const struct http_parser_settings settings;
755
754
756
755
friend class ScopedRetainParser ;
757
756
};
758
757
759
-
760
758
const struct http_parser_settings Parser::settings = {
761
- Parser::on_message_begin,
762
- Parser::on_url,
763
- Parser::on_status,
764
- Parser::on_header_field,
765
- Parser::on_header_value,
766
- Parser::on_headers_complete,
767
- Parser::on_body,
768
- Parser::on_message_complete,
759
+ Proxy<Call, & Parser::on_message_begin>::Raw ,
760
+ Proxy<DataCall, & Parser::on_url>::Raw ,
761
+ Proxy<DataCall, & Parser::on_status>::Raw ,
762
+ Proxy<DataCall, & Parser::on_header_field>::Raw ,
763
+ Proxy<DataCall, & Parser::on_header_value>::Raw ,
764
+ Proxy<Call, & Parser::on_headers_complete>::Raw ,
765
+ Proxy<DataCall, & Parser::on_body>::Raw ,
766
+ Proxy<Call, & Parser::on_message_complete>::Raw ,
769
767
nullptr , // on_chunk_header
770
768
nullptr // on_chunk_complete
771
769
};
0 commit comments