void auth_pam(x0::HttpRequest* r, const x0::FlowParams& args, x0::FlowValue& result) { if (!r->customData<AuthBasic>(this)) r->setCustomData<AuthBasic>(this); r->customData<AuthBasic>(this)->setupPAM(args.size() != 0 ? args[0].toString() : "x0"); }
void install_filter(x0::HttpRequest *r, const x0::FlowParams& args, x0::FlowValue& /*result*/) { if (args.size() != 1) { log(x0::Severity::error, "No argument passed."); return; } if (!args[0].isString()) { log(x0::Severity::error, "Invalid argument type passed."); return; } if (strcmp(args[0].toString(), "identity") == 0) r->outputFilters.push_back(std::make_shared<ExampleFilter>(ExampleFilter::IDENTITY)); else if (strcmp(args[0].toString(), "upper") == 0) r->outputFilters.push_back(std::make_shared<ExampleFilter>(ExampleFilter::UPPER)); else if (strcmp(args[0].toString(), "lower") == 0) r->outputFilters.push_back(std::make_shared<ExampleFilter>(ExampleFilter::LOWER)); else { log(x0::Severity::error, "Invalid argument value passed."); return; } r->responseHeaders.push_back("Content-Encoding", "filter_example"); // response might change according to Accept-Encoding if (!r->responseHeaders.contains("Vary")) r->responseHeaders.push_back("Vary", "Accept-Encoding"); else r->responseHeaders["Vary"] += ",Accept-Encoding"; // removing content-length implicitely enables chunked encoding r->responseHeaders.remove("Content-Length"); }
void setup_types(const x0::FlowParams& args, x0::FlowValue& result) { contentTypes_.clear(); for (int i = 0, e = args.size(); i != e; ++i) populateContentTypes(args[i]); }
void auth_userfile(x0::HttpRequest* r, const x0::FlowParams& args, x0::FlowValue& result) { if (!r->customData<AuthBasic>(this)) r->setCustomData<AuthBasic>(this); r->customData<AuthBasic>(this)->setupUserfile(args.size() != 0 ? args[0].toString() : "/etc/htpasswd"); }
void Flower::flow_mkbuf(void *, x0::FlowParams& args, void *) { if (args.size() == 2 && args[1].isString()) args[0].set(args[1].toString(), strlen(args[1].toString())); else args[0].set("", 0); // empty buffer }
void set_loglevel(const x0::FlowParams& args, x0::FlowValue& result) { if (args.size() == 1) { if (args[0].isNumber()) setLogLevel(args[0].toNumber()); } }
void Flower::flow_error(void *, x0::FlowParams& args, void *) { if (args.size() == 2) printf("error. %s\n", args[1].toString()); else printf("error\n"); args[0].set(true); }
// ssl.add( // 'keyfile' => PATH, // 'certfile' => PATH, // 'trustfile' => PATH, // 'priorities' => CIPHERS); // void add_context(const x0::FlowParams& args, x0::FlowValue& result) { std::auto_ptr<SslContext> cx(new SslContext()); cx->setLogger(server().logger()); std::string keyname; for (std::size_t i = 0, e = args.size(); i != e; ++i) { if (!args[i].isArray()) continue; const x0::FlowArray& arg = args[i].toArray(); // key => value if (arg.size() != 2) continue; const x0::FlowValue* key = &arg[0]; const x0::FlowValue* value = &arg[1]; if (!key->isString()) continue; keyname = key->toString(); if (!value->isString() && !value->isNumber() && !value->isBool()) continue; std::string sval; if (keyname == "certfile") { if (!value->load(sval)) return; cx->certFile = sval; } else if (keyname == "keyfile") { if (!value->load(sval)) return; cx->keyFile = sval; } else if (keyname == "trustfile") { if (!value->load(sval)) return; cx->trustFile = sval; } else if (keyname == "priorities") { if (!value->load(sval)) return; cx->priorities = sval; } else { server().log(x0::Severity::error, "ssl: Unknown ssl.context key: '%s'\n", keyname.c_str()); return; } } // context setup successful -> put into our ssl context set. contexts_.push_back(cx.release()); }
void Flower::flow_assert(void *u, x0::FlowParams& args, void *) { Flower* self = (Flower*) u; const FlowValue& sourceValue = args[args.size() - 1]; std::string source; if (sourceValue.isString()) source = sourceValue.toString(); if (!args[1].toBool()) { printf("[ FAILED ] %s\n", source.c_str()); args[0].set(true); } else { printf("[ OK ] %s\n", source.c_str()); ++self->totalSuccess_; args[0].set(false); } }
void Flower::flow_assertFail(void *, x0::FlowParams& args, void *) { if (args[1].toBool()) { if (args.size() == 3 && args[2].isString()) fprintf(stderr, "Assertion failed. %s\n", args[2].toString()); else fprintf(stderr, "Assertion failed.\n"); fflush(stderr); args[0].set(true); } else { //printf("assert ok (%d, %f)\n", args[1].type_, args[1].toNumber()); args[0].set(false); } }
// void expire(datetime / timespan) void expire(x0::HttpRequest *r, const x0::FlowParams& args, x0::FlowValue& result) { if (args.size() < 1) return; time_t now = r->connection.worker().now().unixtime(); time_t mtime = r->fileinfo ? r->fileinfo->mtime() : now; time_t value = args[0].toNumber(); // passed a timespan if (value < mtime) value = value + now; // (mtime+span) points to past? if (value < now) value = now; r->responseHeaders.overwrite("Expires", x0::DateTime(value).http_str().str()); x0::Buffer cc(20); cc << "max-age=" << (value - now); r->responseHeaders.overwrite("Cache-Control", cc.str()); }