static int test_run_ejs(char *script) { EjsHandle handle = 0; MprVar result; char *emsg; TALLOC_CTX *mem_ctx = talloc_new(NULL); struct MprVar *return_var; mprSetCtx(mem_ctx); if (ejsOpen(NULL, NULL, NULL) != 0) { d_printf("ejsOpen(): unable to initialise EJS subsystem\n"); ejs_error = 127; goto failed; } smb_setup_ejs_functions(test_ejs_exception); if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) { d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n"); ejs_error = 127; goto failed; } mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL)); /* run the script */ if (ejsEvalScript(eid, script, &result, &emsg) == -1) { d_printf("smbscript: ejsEvalScript(): %s\n", emsg); if (ejs_error == 0) ejs_error = 127; goto failed; } return_var = ejsGetReturnValue(eid); ejs_error = mprVarToNumber(return_var); failed: ejsClose(); talloc_free(mem_ctx); return ejs_error; }
EspRequest *espCreateRequest(EspHandle webServerRequestHandle, char *uri, MprVar *variables) { EspRequest *ep; MprVar *global; #if BLD_FEATURE_LEGACY_API MprVar *np; char keyBuf[ESP_MAX_HEADER]; int i; #endif mprAssert(variables); ep = (EspRequest*)mprMalloc(sizeof(EspRequest)); if (ep == 0) { return 0; } memset(ep, 0, sizeof(EspRequest)); ep->requestHandle = webServerRequestHandle; ep->esp = esp; ep->uri = mprStrdup(uri); ep->docPath = 0; ep->variables = variables; /* * The handle passed to ejsOpenEngine is passed to every C function * called by JavaScript. */ ep->eid = ejsOpenEngine((EjsHandle) ep, (EjsHandle) webServerRequestHandle); if (ep->eid < 0) { mprFree(ep); return 0; } /* * All these copies and SetProperties will only copy references * They will increments the object ref counts. */ mprCopyVar(&variables[ESP_GLOBAL_OBJ], ejsGetGlobalObject(ep->eid), MPR_SHALLOW_COPY); mprCopyVar(&variables[ESP_LOCAL_OBJ], ejsGetLocalObject(ep->eid), MPR_SHALLOW_COPY); global = &variables[ESP_GLOBAL_OBJ]; mprCreateProperty(global, "application", &variables[ESP_APPLICATION_OBJ]); mprCreateProperty(global, "cookies", &variables[ESP_COOKIES_OBJ]); mprCreateProperty(global, "files", &variables[ESP_FILES_OBJ]); mprCreateProperty(global, "form", &variables[ESP_FORM_OBJ]); mprCreateProperty(global, "headers", &variables[ESP_HEADERS_OBJ]); mprCreateProperty(global, "request", &variables[ESP_REQUEST_OBJ]); // // FUTURE -- could server be shared across all requests for a given host // and be made read-only. // mprCreateProperty(global, "server", &variables[ESP_SERVER_OBJ]); #if BLD_FEATURE_SESSION mprCreateProperty(global, "session", &variables[ESP_SESSION_OBJ]); #endif #if BLD_FEATURE_LEGACY_API /* * DEPRECATED: 2.0 * Define variables as globals. headers[] are prefixed with "HTTP_". * NOTE: MaRequest::setVar does not copy into globals, whereas espSetVar * does if legacy_api is defined. So variables pre-defined by MaRequest * must be copied here into globals[]. * * NOTE: if a variable is in session[] and in form[], the form[] will * override being later in the variables[] list. Use mprSetProperty * instead of mprCreateProperty to cover for this case. */ for (i = 0; i < ESP_OBJ_MAX; i++) { if (i == ESP_GLOBAL_OBJ || i == ESP_LOCAL_OBJ) { continue; } if (variables[i].type != MPR_TYPE_OBJECT) { continue; } np = mprGetFirstProperty(&variables[i], MPR_ENUM_DATA); while (np) { if (i == ESP_HEADERS_OBJ) { mprSprintf(keyBuf, sizeof(keyBuf) - 1, "HTTP_%s", np->name); mprSetProperty(global, keyBuf, np); } else { mprSetProperty(global, np->name, np); } np = mprGetNextProperty(&variables[i], np, MPR_ENUM_DATA); } } #endif return ep; }