Permalink
Browse files

http, feat: check option items, throw unknown option.

  • Loading branch information...
xicilion committed Nov 1, 2017
1 parent b6f1aef commit d8113504db54a2d26fea1b7f956e526c51248534
Showing with 32 additions and 0 deletions.
  1. +2 −0 fibjs/include/utils.h
  2. +22 −0 fibjs/src/base/utils.cpp
  3. +8 −0 fibjs/src/http/HttpClient.cpp
View
@@ -919,6 +919,8 @@ result_t GetConfigValue(v8::Isolate* isolate, v8::Local<v8::Object> o,
return GetArgumentValue(isolate, v, n, bStrict);
}
result_t CheckConfig(v8::Local<v8::Object> opts, const char** keys);
inline v8::Local<v8::Value> GetReturnValue(v8::Isolate* isolate, int32_t v)
{
return v8::Int32::New(isolate, v);
View
@@ -220,4 +220,26 @@ void ReportException(TryCatch& try_catch, result_t hr)
if (try_catch.HasCaught() || hr < 0)
errorLog(GetException(try_catch, hr));
}
result_t CheckConfig(v8::Local<v8::Object> opts, const char** keys)
{
v8::Local<v8::Array> ks = opts->GetPropertyNames();
int32_t len = ks->Length();
int32_t i;
for (i = 0; i < len; i++) {
v8::String::Utf8Value k(ks->Get(i));
const char** p = keys;
while (p[0]) {
if (!qstrcmp(*k, p[0]))
break;
p++;
}
if (!p[0])
return CHECK_ERROR(Runtime::setError(exlib::string("unknown option \'") + *k + "\'."));
}
return 0;
}
}
@@ -505,6 +505,10 @@ result_t HttpClient::request(exlib::string method, exlib::string url, SeekableSt
result_t HttpClient::request(exlib::string method, exlib::string url,
v8::Local<v8::Object> opts, obj_ptr<HttpResponse_base>& retVal, AsyncEvent* ac)
{
static const char* s_keys[] = {
"query", "headers", "body", "json", NULL
};
if (ac->isSync()) {
Isolate* isolate = holder();
obj_ptr<NObject> map = new NObject();
@@ -513,6 +517,10 @@ result_t HttpClient::request(exlib::string method, exlib::string url,
v8::Local<v8::Value> v;
result_t hr;
hr = CheckConfig(opts, s_keys);
if (hr < 0)
return hr;
ac->m_ctx.resize(3);
hr = GetArgumentValue(opts->Get(isolate->NewString("query", 5)), o);

0 comments on commit d811350

Please sign in to comment.