/* set a mpr component, allowing for sub objects, using the '.' convention destroys 'val' after setting */ NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val) { const char *p = strchr(name, '.'); char *objname; struct MprVar *v2; NTSTATUS status; if (p == NULL) { v2 = mprSetProperty(v, name, &val); if (v2 == NULL) { DEBUG(1,("mprSetVar unable to set '%s'\n", name)); return NT_STATUS_INVALID_PARAMETER_MIX; } mprDestroyVar(&val); return NT_STATUS_OK; } objname = talloc_strndup(mprMemCtx(), name, p-name); if (objname == NULL) { return NT_STATUS_NO_MEMORY; } v2 = mprGetProperty(v, objname, NULL); if (v2 == NULL) { mprSetVar(v, objname, mprObject(objname)); v2 = mprGetProperty(v, objname, NULL); } status = mprSetVar(v2, p+1, val); talloc_free(objname); return status; }
MprVar *espSetProperty(MprVar *obj, char *property, MprVar *newValue) { return mprSetProperty(obj, property, newValue); }
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; }