Skip to content

Instantly share code, notes, and snippets.

@indutny

indutny/1.diff Secret

Created June 30, 2019 22:30
Show Gist options
  • Save indutny/15352991e85a51786436646a31c790db to your computer and use it in GitHub Desktop.
Save indutny/15352991e85a51786436646a31c790db to your computer and use it in GitHub Desktop.
diff --git a/src/node_http_parser_impl.h b/src/node_http_parser_impl.h
index 1afedb0509..43b147fe8a 100644
--- a/src/node_http_parser_impl.h
+++ b/src/node_http_parser_impl.h
@@ -719,36 +719,51 @@ class Parser : public AsyncWrap, public StreamListener {
// If there was a parse error in one of the callbacks
// TODO(bnoordhuis) What if there is an error on EOF?
if (!parser_.upgrade && err != HPE_OK) {
- Local<Value> e = Exception::Error(env()->parse_error_string());
- Local<Object> obj = e->ToObject(env()->isolate()->GetCurrentContext())
- .ToLocalChecked();
- obj->Set(env()->context(),
- env()->bytes_parsed_string(),
- nread_obj).Check();
+ const char* code;
+ size_t code_len;
+ const char* reason;
+ size_t reason_len;
+
#ifdef NODE_EXPERIMENTAL_HTTP
- const char* errno_reason = llhttp_get_error_reason(&parser_);
+ reason = llhttp_get_error_reason(&parser_);
- Local<String> code;
- Local<String> reason;
if (err == HPE_USER) {
- const char* colon = strchr(errno_reason, ':');
+ const char* colon = strchr(reason, ':');
CHECK_NOT_NULL(colon);
- code = OneByteString(env()->isolate(), errno_reason,
- colon - errno_reason);
- reason = OneByteString(env()->isolate(), colon + 1);
+ code = reason;
+ code_len = colon - reason;
+ reason = colon + 1;
} else {
- code = OneByteString(env()->isolate(), llhttp_errno_name(err));
- reason = OneByteString(env()->isolate(), errno_reason);
+ code = llhttp_errno_name(err);
+ code_len = strlen(code);
}
-
- obj->Set(env()->context(), env()->code_string(), code).Check();
- obj->Set(env()->context(), env()->reason_string(), reason).Check();
#else /* !NODE_EXPERIMENTAL_HTTP */
+ reason = "(unknown)";
+ code = http_errno_name(err);
+ code_len = strlen(code);
+#endif /* NODE_EXPERIMENTAL_HTTP */
+ reason_len = strlen(reason);
+
+ char error_msg[1024];
+ snprintf(error_msg, sizeof(error_msg), "Parse Error: %.*s, %.*s",
+ static_cast<int>(code_len), code,
+ static_cast<int>(reason_len), reason);
+
+ Local<Value> e = Exception::Error(OneByteString(env()->isolate(),
+ error_msg));
+ Local<Object> obj = e->ToObject(env()->isolate()->GetCurrentContext())
+ .ToLocalChecked();
+
+ obj->Set(env()->context(),
+ env()->bytes_parsed_string(),
+ nread_obj).Check();
obj->Set(env()->context(),
env()->code_string(),
- OneByteString(env()->isolate(),
- http_errno_name(err))).Check();
-#endif /* NODE_EXPERIMENTAL_HTTP */
+ OneByteString(env()->isolate(), code, code_len)).Check();
+ obj->Set(env()->context(),
+ env()->reason_string(),
+ OneByteString(env()->isolate(), reason, reason_len)).Check();
+
return scope.Escape(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment