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