diff options
Diffstat (limited to 'source4/lib')
-rw-r--r-- | source4/lib/appweb/config.mk | 24 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/config.h (renamed from source4/lib/ejs/config.h) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/config.mk (renamed from source4/lib/ejs/config.mk) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/ejs.h (renamed from source4/lib/ejs/ejs.h) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/ejsInternal.h (renamed from source4/lib/ejs/ejsInternal.h) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/ejsLex.c (renamed from source4/lib/ejs/ejsLex.c) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/ejsLib.c (renamed from source4/lib/ejs/ejsLib.c) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/ejsParser.c (renamed from source4/lib/ejs/ejsParser.c) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/ejsProcs.c (renamed from source4/lib/ejs/ejsProcs.c) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/miniMpr.c (renamed from source4/lib/ejs/miniMpr.c) | 2 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/miniMpr.h (renamed from source4/lib/ejs/miniMpr.h) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/var.c (renamed from source4/lib/ejs/var.c) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/ejs/var.h (renamed from source4/lib/ejs/var.h) | 0 | ||||
-rw-r--r-- | source4/lib/appweb/esp/esp.c | 1042 | ||||
-rw-r--r-- | source4/lib/appweb/esp/esp.h | 279 | ||||
-rw-r--r-- | source4/lib/appweb/esp/espEnv.h | 128 | ||||
-rw-r--r-- | source4/lib/appweb/esp/espProcs.c | 246 | ||||
-rw-r--r-- | source4/lib/ejs/mprOs.h | 627 |
18 files changed, 1720 insertions, 628 deletions
diff --git a/source4/lib/appweb/config.mk b/source4/lib/appweb/config.mk new file mode 100644 index 0000000000..7d1eca3437 --- /dev/null +++ b/source4/lib/appweb/config.mk @@ -0,0 +1,24 @@ +####################### +# Start SUBSYSTEM EJS +[SUBSYSTEM::EJS] +ADD_OBJ_FILES = \ + lib/appweb/ejs/ejsLib.o \ + lib/appweb/ejs/ejsLex.o \ + lib/appweb/ejs/ejsParser.o \ + lib/appweb/ejs/ejsProcs.o \ + lib/appweb/ejs/miniMpr.o \ + lib/appweb/ejs/var.o +NOPROTO=YES +# End SUBSYSTEM EJS +####################### + +####################### +# Start SUBSYSTEM ESP +[SUBSYSTEM::ESP] +ADD_OBJ_FILES = \ + lib/appweb/esp/esp.o \ + lib/appweb/esp/espProcs.o +REQUIRED_SUBSYSTEMS = EJS +NOPROTO=YES +# End SUBSYSTEM ESP +####################### diff --git a/source4/lib/ejs/config.h b/source4/lib/appweb/ejs/config.h index 320318a0b2..320318a0b2 100644 --- a/source4/lib/ejs/config.h +++ b/source4/lib/appweb/ejs/config.h diff --git a/source4/lib/ejs/config.mk b/source4/lib/appweb/ejs/config.mk index f2c0e62f1e..f2c0e62f1e 100644 --- a/source4/lib/ejs/config.mk +++ b/source4/lib/appweb/ejs/config.mk diff --git a/source4/lib/ejs/ejs.h b/source4/lib/appweb/ejs/ejs.h index f1d2bb4c6e..f1d2bb4c6e 100644 --- a/source4/lib/ejs/ejs.h +++ b/source4/lib/appweb/ejs/ejs.h diff --git a/source4/lib/ejs/ejsInternal.h b/source4/lib/appweb/ejs/ejsInternal.h index 3bf99d88b9..3bf99d88b9 100644 --- a/source4/lib/ejs/ejsInternal.h +++ b/source4/lib/appweb/ejs/ejsInternal.h diff --git a/source4/lib/ejs/ejsLex.c b/source4/lib/appweb/ejs/ejsLex.c index a5f15c2979..a5f15c2979 100644 --- a/source4/lib/ejs/ejsLex.c +++ b/source4/lib/appweb/ejs/ejsLex.c diff --git a/source4/lib/ejs/ejsLib.c b/source4/lib/appweb/ejs/ejsLib.c index caae5b6495..caae5b6495 100644 --- a/source4/lib/ejs/ejsLib.c +++ b/source4/lib/appweb/ejs/ejsLib.c diff --git a/source4/lib/ejs/ejsParser.c b/source4/lib/appweb/ejs/ejsParser.c index 772ed574c5..772ed574c5 100644 --- a/source4/lib/ejs/ejsParser.c +++ b/source4/lib/appweb/ejs/ejsParser.c diff --git a/source4/lib/ejs/ejsProcs.c b/source4/lib/appweb/ejs/ejsProcs.c index c01f411161..c01f411161 100644 --- a/source4/lib/ejs/ejsProcs.c +++ b/source4/lib/appweb/ejs/ejsProcs.c diff --git a/source4/lib/ejs/miniMpr.c b/source4/lib/appweb/ejs/miniMpr.c index 8b969289b0..7dda4e7bd7 100644 --- a/source4/lib/ejs/miniMpr.c +++ b/source4/lib/appweb/ejs/miniMpr.c @@ -29,7 +29,7 @@ * @end */ -#include "lib/ejs/miniMpr.h" +#include "lib/appweb/ejs/miniMpr.h" /************************************ Code ************************************/ #if !BLD_APPWEB diff --git a/source4/lib/ejs/miniMpr.h b/source4/lib/appweb/ejs/miniMpr.h index d431ebdc1b..d431ebdc1b 100644 --- a/source4/lib/ejs/miniMpr.h +++ b/source4/lib/appweb/ejs/miniMpr.h diff --git a/source4/lib/ejs/var.c b/source4/lib/appweb/ejs/var.c index 9d2afe5306..9d2afe5306 100644 --- a/source4/lib/ejs/var.c +++ b/source4/lib/appweb/ejs/var.c diff --git a/source4/lib/ejs/var.h b/source4/lib/appweb/ejs/var.h index 8ed13a4995..8ed13a4995 100644 --- a/source4/lib/ejs/var.h +++ b/source4/lib/appweb/ejs/var.h diff --git a/source4/lib/appweb/esp/esp.c b/source4/lib/appweb/esp/esp.c new file mode 100644 index 0000000000..ef20557f93 --- /dev/null +++ b/source4/lib/appweb/esp/esp.c @@ -0,0 +1,1042 @@ +/* + * @file esp.c + * @brief Embedded Server Pages (ESP) core processing. + * @overview The ESP handler provides an efficient way to generate + * dynamic pages using server-side Javascript. This code provides + * core processing, and should be called by an associated web + * server URL handler. + */ +/********************************* Copyright **********************************/ +/* + * @copy default + * + * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved. + * + * This software is distributed under commercial and open source licenses. + * You may use the GPL open source license described below or you may acquire + * a commercial license from Mbedthis Software. You agree to be fully bound + * by the terms of either license. Consult the LICENSE.TXT distributed with + * this software for full details. + * + * This software is open source; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See the GNU General Public License for more + * details at: http://www.mbedthis.com/downloads/gplLicense.html + * + * This program is distributed WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * This GPL license does NOT permit incorporating this software into + * proprietary programs. If you are unable to comply with the GPL, you must + * acquire a commercial license to use this software. Commercial licenses + * for this software and support services are available from Mbedthis + * Software at http://www.mbedthis.com + * + * @end + */ +/********************************** Includes **********************************/ + +#include "esp.h" + +#if BLD_FEATURE_ESP_MODULE + +/*********************************** Locals ***********************************/ +/* + * Master ESP control interface with the web server + */ + +static const Esp *esp; + +/***************************** Forward Declarations ***************************/ + +static int buildScript(EspRequest *ep, char **jsBuf, char *input, char + **errMsg); + +/************************************ Code ************************************/ +/* + * Called at server initialization + */ + +int espOpen(const Esp *control) +{ + mprAssert(control); + +#if BLD_FEATURE_MULTITHREAD + ejsOpen(control->lock, control->unlock, control->lockData); +#else + ejsOpen(0, 0, 0); +#endif + + /* + * Register the standard procedures + */ + espRegisterProcs(); + + /* + * Just for brain dead systems that don't zero global memory + */ + esp = control; + return 0; +} + +/******************************************************************************/ +/* + * Called at server termination + */ + +void espClose() +{ + ejsClose(); +} + +/******************************************************************************/ +/* + * Create for new ESP request. Assumed that this is called after all the + * HTTP headers have been read but before POST data has been read. It is + * expected that any session cookies have been read and that "variables" + * contains references to all the environment objects including "session". + * requestHandle is the web server request handle. + */ + +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 = 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; +} + +/******************************************************************************/ + +void espDestroyRequest(EspRequest *ep) +{ + mprAssert(ep); + mprAssert(ep->eid >= 0); + + mprFree(ep->uri); + mprFree(ep->docPath); + ejsCloseEngine(ep->eid); + mprFree(ep); +} + +/******************************************************************************/ +/* + * The callback function will be called: + * + * (fn)(EjsId eid, EspRequest *ep, argc, argv); + * + * Callers can get their web server handle by calling: + * + * rq = (requiredCast) espGetHandle(ep); + */ + +void espDefineCFunction(EspRequest *ep, const char *functionName, EspCFunction fn, + void *thisPtr) +{ + mprAssert(functionName && *functionName); + mprAssert(fn); + + if (ep) { + ejsDefineCFunction(ep->eid, functionName, (MprCFunction) fn, + thisPtr, 0); + } else { + ejsDefineCFunction(-1, functionName, (MprCFunction) fn, thisPtr, 0); + } +} + +/******************************************************************************/ + +void espDefineStringCFunction(EspRequest *ep, const char *functionName, + EspStringCFunction fn, void *thisPtr) +{ + mprAssert(functionName && *functionName); + mprAssert(fn); + + if (ep) { + ejsDefineStringCFunction(ep->eid, functionName, (MprStringCFunction) fn, + thisPtr, 0); + } else { + ejsDefineStringCFunction(-1, functionName, (MprStringCFunction) fn, + thisPtr, 0); + } +} + +/******************************************************************************/ + +void *espGetRequestHandle(EspRequest *ep) +{ + return ep->requestHandle; +} + +/******************************************************************************/ + +EjsId espGetScriptHandle(EspRequest *ep) +{ + return ep->eid; +} + +/******************************************************************************/ + +char *espGetStringVar(EspRequest *ep, EspEnvType oType, char *var, + char *defaultValue) +{ + MprVar value; + + if (espGetVar(ep, oType, var, &value) < 0 || + value.type != MPR_TYPE_STRING) { + return defaultValue; + } + return value.string; +} + +/******************************************************************************/ + +int espGetVar(EspRequest *ep, EspEnvType oType, char *var, MprVar *value) +{ + MprVar *vp; + + mprAssert(ep); + mprAssert(var); + + vp = mprGetProperty(&ep->variables[oType], var, 0); + if (vp == 0) { + return -1; + } + *value = *vp; + return 0; +} + +/******************************************************************************/ +/* + * Process the ESP page. docBuf holds the page already. We expect that + * ep->variables holds all the pertinent environment variables. + */ + +int espProcessRequest(EspRequest *ep, const char *docPath, char *docBuf, + char **errMsg) +{ + char *jsBuf; + + mprAssert(ep); + + ep->docPath = mprStrdup(docPath); + + jsBuf = 0; + if (buildScript(ep, &jsBuf, docBuf, errMsg) < 0) { + return MPR_ERR_CANT_COMPLETE; + } + + if (jsBuf) { + mprLog(7, "esp: script is:\n%s\n", jsBuf); + + /* + * Now evaluate the entire escript + * MOB could cache the script + */ + if (ejsEvalScript(ep->eid, jsBuf, 0, errMsg) < 0) { + return MPR_ERR_ABORTED; + } + + mprFree(jsBuf); + } + return 0; +} + +/******************************************************************************/ + +void espRedirect(EspRequest *ep, int code, char *url) +{ + mprAssert(ep); + mprAssert(url); + + ep->esp->redirect(ep->requestHandle, code, url); +} + +/******************************************************************************/ + +void espError(EspRequest *ep, const char *fmt, ...) +{ + va_list args; + char *buf; + + mprAssert(ep); + mprAssert(fmt); + + va_start(args, fmt); + mprAllocVsprintf(&buf, MPR_MAX_HEAP_SIZE, fmt, args); + ejsSetErrorMsg(ep->eid, buf); + mprFree(buf); + va_end(args); +} + +/******************************************************************************/ + +void espSetHeader(EspRequest *ep, char *header, bool allowMultiple) +{ + mprAssert(ep); + + ep->esp->setHeader(ep->requestHandle, header, allowMultiple); +} + +/******************************************************************************/ +/* + * Caller does not need to destroy the var + */ + +MprVar *espGetResult(EspRequest *ep) +{ + mprAssert(ep); + + return ejsGetReturnValue(ep->eid); +} + +/******************************************************************************/ + +void espSetReturn(EspRequest *ep, MprVar value) +{ + mprAssert(ep); + + ejsSetReturnValue(ep->eid, value); +} + +/******************************************************************************/ + +void espSetReturnString(EspRequest *ep, const char *str) +{ + mprAssert(ep); + + ejsSetReturnValue(ep->eid, mprCreateStringVar(str, 0)); +} + +/******************************************************************************/ + +void espSetResponseCode(EspRequest *ep, int code) +{ + mprAssert(ep); + + ep->esp->setResponseCode(ep->requestHandle, code); +} + +/******************************************************************************/ + +void espSetVar(EspRequest *ep, EspEnvType oType, char *var, MprVar value) +{ + mprCreatePropertyValue(&ep->variables[oType], var, value); +} + +/******************************************************************************/ + +void espSetStringVar(EspRequest *ep, EspEnvType oType, + const char *var, const char *value) +{ + /* + * Will create or update if already existing + */ + mprCreatePropertyValue(&ep->variables[oType], var, + mprCreateStringVar(value, 0)); +} + +/******************************************************************************/ + +int espUnsetVar(EspRequest *ep, EspEnvType oType, char *var) +{ + return mprDeleteProperty(&ep->variables[oType], var); +} + +/******************************************************************************/ + +int espWrite(EspRequest *ep, char *buf, int size) +{ + mprAssert(ep); + mprAssert(buf); + mprAssert(size >= 0); + + return ep->esp->writeBlock(ep->requestHandle, buf, size); +} + +/******************************************************************************/ + +int espWriteString(EspRequest *ep, char *buf) +{ + mprAssert(ep); + mprAssert(buf); + + return ep->esp->writeBlock(ep->requestHandle, buf, strlen(buf)); +} + +/******************************************************************************/ + +int espWriteFmt(EspRequest *ep, char *fmt, ...) +{ + va_list args; + char *buf; + int rc, len; + + mprAssert(ep); + mprAssert(fmt); + + va_start(args, fmt); + len = mprAllocVsprintf(&buf, MPR_MAX_HEAP_SIZE, fmt, args); + rc = ep->esp->writeBlock(ep->requestHandle, buf, len); + mprFree(buf); + va_end(args); + return rc; +} + +/******************************************************************************/ +/******************************************************************************/ +/******************************************************************************/ +/* + * Get a javascript identifier. Must allow x.y['abc'] or x.y["abc"]. + * Must be careful about quoting and only allow quotes inside []. + */ + +static int getIdentifier(EspParse *parse) +{ + int atQuote, prevC, c; + + mprAssert(parse); + + atQuote = 0; + prevC = 0; + c = *parse->inp++; + + while (isalnum(c) || c == '_' || c == '.' || c == '[' || + c == ']' || c == '\'' || c == '\"') { + if (c == '\'' || c == '\"') { + if (c == atQuote) { + atQuote = 0; + } else if (prevC == '[') { + atQuote = c; + } else { + break; + } + } + if (parse->tokp >= parse->endp) { + parse->token = (char*) mprRealloc(parse->token, + parse->tokLen + ESP_TOK_INCR); + if (parse->token == 0) { + return MPR_ERR_CANT_ALLOCATE; + } + parse->token[parse->tokLen] = '\0'; + parse->tokLen += ESP_TOK_INCR; + parse->endp = &parse->token[parse->tokLen - 1]; + } + *parse->tokp++ = c; + prevC = c; + c = *parse->inp++; + } + + parse->inp--; + *parse->tokp = '\0'; + + return 0; +} + +/******************************************************************************/ +/* + * Get the next ESP input token. input points to the next input token. + * parse->token will hold the parsed token. The function returns the token id. + */ + +static int getEspToken(int state, EspParse *parse) +{ + char *cp; + int tid, done, c, quoted; + + tid = ESP_TOK_LITERAL; + parse->tokp = parse->token; + parse->tokp[0] = '\0'; + quoted = 0; + + c = *parse->inp++; + for (done = 0; !done; c = *parse->inp++) { + + /* + * Get room for more characters in the token buffer + */ + if (parse->tokp >= parse->endp) { + parse->token = (char*) mprRealloc(parse->token, + parse->tokLen + ESP_TOK_INCR); + if (parse->token == 0) { + return ESP_TOK_ERR; + } + parse->token[parse->tokLen] = '\0';
+ parse->tokp = &parse->token[parse->tokLen - 1]; + parse->tokLen += ESP_TOK_INCR; + parse->endp = &parse->token[parse->tokLen - 1]; + } + + switch (c) { + case 0: + if (*parse->token) { + done++; + parse->inp--; + break; + } + return ESP_TOK_EOF; + + default: + if (c == '\"' && state != ESP_STATE_IN_ESP_TAG) { + *parse->tokp++ = '\\'; + } + *parse->tokp++ = c; + quoted = 0; + break; + + case '\\': + quoted = 1; + *parse->tokp++ = c; + break; + + case '@': + if (*parse->inp == '@' && state != ESP_STATE_IN_ESP_TAG) { + if (quoted) { + parse->tokp--; + quoted = 0; + } else { + if (*parse->token) { + parse->inp--; + } else { + parse->inp++; + tid = ESP_TOK_ATAT; + if (getIdentifier(parse) < 0) { + return ESP_TOK_ERR; + } + } + done++; + break; + } + } + *parse->tokp++ = c; + break; + + case '<': + if (*parse->inp == '%' && state != ESP_STATE_IN_ESP_TAG) { + if (quoted) { + parse->tokp--; + quoted = 0; + *parse->tokp++ = c; + break; + } + if (*parse->token) { + parse->inp--; + done++; + break; + } + parse->inp++; + while (isspace((int) *parse->inp)) { + parse->inp++; + } + if (*parse->inp == '=') { + parse->inp++; + while (isspace((int) *parse->inp)) { + parse->inp++; + } + tid = ESP_TOK_EQUALS; + if (getIdentifier(parse) < 0) { + return ESP_TOK_ERR; + } + done++; + break; + } + if (*parse->inp == 'i' && + strncmp(parse->inp, "include", 7) == 0 && + isspace((int) parse->inp[7])) { + tid = ESP_TOK_INCLUDE; + parse->inp += 7; + while (isspace((int) *parse->inp)) { + parse->inp++; + } + while (*parse->inp && !isspace((int) *parse->inp) && + *parse->inp != '%' && parse->tokp < parse->endp) { + *parse->tokp++ = *parse->inp++; + } + *parse->tokp = '\0'; + if (parse->token[0] == '"') { + parse->tokp = parse->token; + for (cp = &parse->token[1]; *cp; ) { + *parse->tokp++ = *cp++; + } + if (cp[-1] == '"') { + parse->tokp--; + } + *parse->tokp = '\0'; + } + + } else { + tid = ESP_TOK_START_ESP; + } + done++; + break; + } + *parse->tokp++ = c; + break; + + case '%': + if (*parse->inp == '>' && state == ESP_STATE_IN_ESP_TAG) { + if (quoted) { + parse->tokp--; + quoted = 0; + } else { + if (*parse->token) { + parse->inp--; + } else { + tid = ESP_TOK_END_ESP; + parse->inp++; + } + done++; + break; + } + } + *parse->tokp++ = c; + break; + } + } + + *parse->tokp = '\0'; + parse->inp--; + return tid; +} + +/******************************************************************************/ +/* + * Convert an ESP page into a JavaScript. We also expand include files. + */ + +static int buildScript(EspRequest *ep, char **jsBuf, char *input, char **errMsg) +{ + EspParse parse; + char path[MPR_MAX_FNAME], dir[MPR_MAX_FNAME], incPath[MPR_MAX_FNAME]; + char *incBuf, *incText; + int state, tid, len, rc, maxScriptSize, incSize; + + mprAssert(ep); + mprAssert(jsBuf); + mprAssert(input); + + rc = 0; + len = 0; + state = ESP_STATE_BEGIN; + if (errMsg) { + *errMsg = 0; + } + + memset(&parse, 0, sizeof(parse)); + parse.token = (char*) mprMalloc(ESP_TOK_INCR); + if (parse.token == 0) { + return MPR_ERR_CANT_ALLOCATE; + } + parse.token[0] = '\0'; + parse.tokLen = ESP_TOK_INCR; + parse.endp = &parse.token[parse.tokLen - 1]; + parse.tokp = parse.token; + parse.inBuf = input; + parse.inp = parse.inBuf; + + maxScriptSize = esp->maxScriptSize; + + tid = getEspToken(state, &parse); + while (tid != ESP_TOK_EOF && len >= 0) { + + switch (tid) { + default: + case ESP_TOK_ERR: + mprFree(parse.token); + return MPR_ERR_BAD_SYNTAX; + + case ESP_TOK_LITERAL: + len = mprReallocStrcat(jsBuf, maxScriptSize, len, 0, + "write(\"", parse.token, "\");\n", 0); + break; + + case ESP_TOK_ATAT: + /* + * Trick to get undefined variables to evaluate to "". + * Catenate with "" to cause toString to run. + */ + len = mprReallocStrcat(jsBuf, maxScriptSize, len, 0, + "write(\"\" + ", parse.token, ");\n", 0); + break; + + case ESP_TOK_EQUALS: + len = mprReallocStrcat(jsBuf, maxScriptSize, len, 0, + "write(\"\" + ", parse.token, ");\n", 0); + state = ESP_STATE_IN_ESP_TAG; + break; + + case ESP_TOK_START_ESP: + state = ESP_STATE_IN_ESP_TAG; + tid = getEspToken(state, &parse); + while (tid != ESP_TOK_EOF && tid != ESP_TOK_EOF && + tid != ESP_TOK_END_ESP && len >= 0) { + len = mprReallocStrcat(jsBuf, maxScriptSize, len, 0, + parse.token, 0); + tid = getEspToken(state, &parse); + } + state = ESP_STATE_BEGIN; + break; + + case ESP_TOK_END_ESP: + state = ESP_STATE_BEGIN; + break; + + case ESP_TOK_INCLUDE: + if (parse.token[0] == '/') { + mprStrcpy(incPath, sizeof(incPath), parse.token); + } else { + mprGetDirName(dir, sizeof(dir), ep->uri); + mprSprintf(incPath, sizeof(incPath), "%s/%s", + dir, parse.token); + } + if (esp->mapToStorage(ep->requestHandle, path, sizeof(path), + incPath, 0) < 0) { + mprAllocSprintf(errMsg, MPR_MAX_STRING, + "Can't find include file: %s", path); + rc = MPR_ERR_CANT_OPEN; + break; + } + if (esp->readFile(ep->requestHandle, &incText, &incSize, path) < 0){ + mprAllocSprintf(errMsg, MPR_MAX_STRING, + "Can't read include file: %s", path); + rc = MPR_ERR_CANT_READ; + break; + } + incText[incSize] = '\0'; + + /* + * Recurse and process the include script + */ + incBuf = 0; + if ((rc = buildScript(ep, &incBuf, incText, errMsg)) < 0) { + mprFree(incText); + mprFree(parse.token); + return rc; + } + + len = mprReallocStrcat(jsBuf, maxScriptSize, len, 0, incBuf, 0); + mprFree(incText); + mprFree(incBuf); + state = ESP_STATE_IN_ESP_TAG; + break; + } + tid = getEspToken(state, &parse); + } + mprFree(parse.token); + if (len < 0) { + mprAllocSprintf(errMsg, MPR_MAX_STRING, + "Script token is too big in %s.\nConfigured maximum is %d.", + path, maxScriptSize); + return MPR_ERR_WONT_FIT; + } + return rc; +} + +/******************************************************************************/ +/******************************* Wrapped Routines *****************************/ +/******************************************************************************/ + +int espCopyVar(EspRequest *ep, char *var, MprVar *value, int copyDepth) +{ + return ejsCopyVar(ep->eid, var, value, copyDepth); +} + +/******************************************************************************/ + +MprVar espCreateObjVar(char *name, int hashSize) +{ + return ejsCreateObj(name, hashSize); +} + +/******************************************************************************/ + +MprVar espCreateArrayVar(char *name, int size) +{ + return ejsCreateArray(name, size); +} + +/******************************************************************************/ + +bool espDestroyVar(MprVar *obj) +{ + return ejsDestroyVar(obj); +} + +/******************************************************************************/ + +MprVar *espCreateProperty(MprVar *obj, char *property, MprVar *newValue) +{ + return mprCreateProperty(obj, property, newValue); +} + +/******************************************************************************/ + +MprVar *espCreatePropertyValue(MprVar *obj, char *property, MprVar newValue) +{ + return mprCreatePropertyValue(obj, property, newValue); +} + +/******************************************************************************/ + +void espDefineFunction(EspRequest *ep, const char *functionName, char *args, char *body) +{ + ejsDefineFunction(ep->eid, functionName, args, body); +} + +/******************************************************************************/ + +int espDeleteProperty(MprVar *obj, char *property) +{ + return mprDeleteProperty(obj, property); +} + +/******************************************************************************/ + +int espDeleteVar(EspRequest *ep, char *var) +{ + return ejsDeleteVar(ep->eid, var); +} + +/******************************************************************************/ +int espEvalFile(EspRequest *ep, char *path, MprVar *result, char **emsg) +{ + return ejsEvalFile(ep->eid, path, result, emsg); +} + +/******************************************************************************/ + +int espEvalScript(EspRequest *ep, char *script, MprVar *result, char **emsg) +{ + return ejsEvalScript(ep->eid, script, result, emsg); +} + +/******************************************************************************/ + +int espGetPropertyCount(MprVar *obj, int includeFlags) +{ + if (obj->type != MPR_TYPE_OBJECT) { + return MPR_ERR_BAD_STATE; + } + return mprGetPropertyCount(obj, includeFlags); +} + +/******************************************************************************/ + +MprVar *espGetFirstProperty(MprVar *obj, int includeFlags) +{ + return mprGetFirstProperty(obj, includeFlags); +} + +/******************************************************************************/ + +MprVar *espGetGlobalObject(EspRequest *ep) +{ + return ejsGetGlobalObject(ep->eid); +} + +/******************************************************************************/ + +MprVar *espGetLocalObject(EspRequest *ep) +{ + return ejsGetLocalObject(ep->eid); +} + +/******************************************************************************/ + +MprVar *espGetNextProperty(MprVar *obj, MprVar *currentProperty, + int includeFlags) +{ + return mprGetNextProperty(obj, currentProperty, includeFlags); +} + +/******************************************************************************/ + +MprVar *espGetProperty(MprVar *obj, char *property, MprVar *value) +{ + return mprGetProperty(obj, property, value); +} + +/******************************************************************************/ + +void *espGetThisPtr(EspRequest *ep) +{ + return ejsGetThisPtr(ep->eid); +} + +/******************************************************************************/ +#if XX_UNUSED_XX + +int espReadProperty(MprVar *dest, MprVar *prop) +{ + mprAssert(prop); + mprAssert(dest); + + *dest = *prop; + return 0; +} + +#endif +/******************************************************************************/ + +int espReadVar(EspRequest *ep, char *var, MprVar *value) +{ + return ejsReadVar(ep->eid, var, value); +} + +/******************************************************************************/ + +int espRunFunction(EspRequest *ep, MprVar *obj, char *functionName, + MprArray *args) +{ + return ejsRunFunction(ep->eid, obj, functionName, args); +} + +/******************************************************************************/ + +MprVar *espSetProperty(MprVar *obj, char *property, MprVar *newValue) +{ + return mprSetProperty(obj, property, newValue); +} + +/******************************************************************************/ + +MprVar *espSetPropertyValue(MprVar *obj, char *property, MprVar newValue) +{ + return mprSetPropertyValue(obj, property, newValue); +} + +/******************************************************************************/ + +int espWriteVar(EspRequest *ep, char *var, MprVar *value) +{ + return ejsWriteVar(ep->eid, var, value); +} + +/******************************************************************************/ + +int espWriteVarValue(EspRequest *ep, char *var, MprVar value) +{ + return ejsWriteVarValue(ep->eid, var, value); +} + +/******************************************************************************/ +#if XX_UNUSED_XX + +int espWriteProperty(MprVar *prop, MprVar *newValue) +{ + return mprWriteProperty(prop, newValue); +} + +/******************************************************************************/ + +int espWritePropertyValue(MprVar *prop, MprVar newValue) +{ + return mprWritePropertyValue(prop, newValue); +} + +#endif +/******************************************************************************/ + +#else /* !BLD_FEATURE_ESP_MODULE */ +void espDummy() {} + +/******************************************************************************/ +#endif /* BLD_FEATURE_ESP_MODULE */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/esp/esp.h b/source4/lib/appweb/esp/esp.h new file mode 100644 index 0000000000..ee86e8d345 --- /dev/null +++ b/source4/lib/appweb/esp/esp.h @@ -0,0 +1,279 @@ +/** + * @file esp.h + * @brief Header for Embedded Server Pages (ESP) + */ +/********************************* Copyright **********************************/ +/* + * @copy default + * + * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved. + * + * This software is distributed under commercial and open source licenses. + * You may use the GPL open source license described below or you may acquire + * a commercial license from Mbedthis Software. You agree to be fully bound + * by the terms of either license. Consult the LICENSE.TXT distributed with + * this software for full details. + * + * This software is open source; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See the GNU General Public License for more + * details at: http://www.mbedthis.com/downloads/gplLicense.html + * + * This program is distributed WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * This GPL license does NOT permit incorporating this software into + * proprietary programs. If you are unable to comply with the GPL, you must + * acquire a commercial license to use this software. Commercial licenses + * for this software and support services are available from Mbedthis + * Software at http://www.mbedthis.com + * + * @end + */ +/********************************** Includes **********************************/ + +#ifndef _h_ESP_h +#define _h_ESP_h 1 + +#include "lib/appweb/ejs/ejs.h" +#include "lib/appweb/esp/espEnv.h" +#include "lib/appweb/ejs/var.h" +#include "lib/appweb/ejs/miniMpr.h" + +/*********************************** Defines **********************************/ + +#define ESP_STRING_ARGS MPR_TYPE_STRING_ARGS + +#if BLD_FEATURE_SQUEEZE +#define ESP_TOK_INCR 1024 +#define ESP_MAX_HEADER 1024 +#else +#define ESP_TOK_INCR 4096 +#define ESP_MAX_HEADER 4096 +#endif + +/* + * ESP lexical analyser tokens + */ +#define ESP_TOK_ERR -1 /* Any input error */ +#define ESP_TOK_EOF 0 /* End of file */ +#define ESP_TOK_START_ESP 1 /* <% */ +#define ESP_TOK_END_ESP 2 /* %> */ +#define ESP_TOK_ATAT 3 /* @@var */ +#define ESP_TOK_LITERAL 4 /* literal HTML */ +#define ESP_TOK_INCLUDE 5 /* include file.esp */ +#define ESP_TOK_EQUALS 6 /* = var */ + +/* + * ESP parser states + */ +#define ESP_STATE_BEGIN 1 /* Starting state */ +#define ESP_STATE_IN_ESP_TAG 2 /* Inside a <% %> group */ + +/*********************************** Types ************************************/ + +typedef void* EspHandle; /* Opaque Web server handle type */ + +/* + * Per request control block + */ +typedef struct EspRequest { + MprStr docPath; /* Physical path for ESP page */ + EjsId eid; /* EJS instance handle */ + const struct Esp *esp; /* Pointer to ESP control block */ + EspHandle requestHandle; /* Per request web server handle */ + MprStr uri; /* Request URI */ + MprVar *variables; /* Pointer to variables */ +} EspRequest; + +/* + * Master ESP control block. This defines the function callbacks for a + * web server handler to implement. ESP will call these functions as + * required. + */ +typedef struct Esp { + int maxScriptSize; + void (*createSession)(EspHandle handle, int timeout); + void (*destroySession)(EspHandle handle); + const char *(*getSessionId)(EspHandle handle); + int (*mapToStorage)(EspHandle handle, char *path, int len, const char *uri, + int flags); + int (*readFile)(EspHandle handle, char **buf, int *len, const char *path); + void (*redirect)(EspHandle handle, int code, char *url); + void (*setCookie)(EspHandle handle, const char *name, const char *value, + int lifetime, const char *path, bool secure); + void (*setHeader)(EspHandle handle, const char *value, bool allowMultiple); + void (*setResponseCode)(EspHandle handle, int code); + int (*writeBlock)(EspHandle handle, char *buf, int size); + int (*writeFmt)(EspHandle handle, char *fmt, ...); +#if BLD_FEATURE_MULTITHREAD + void (*lock)(void *lockData); + void (*unlock)(void *lockData); + void *lockData; +#endif +} Esp; + + +/* + * ESP parse context + */ +typedef struct { + char *inBuf; /* Input data to parse */ + char *inp; /* Next character for input */ + char *endp; /* End of storage (allow for null) */ + char *tokp; /* Pointer to current parsed token */ + char *token; /* Storage buffer for token */ + int tokLen; /* Length of buffer */ +} EspParse; + + +/******************************** Private APIs ********************************/ + +extern void espRegisterProcs(void); + +/******************************** Published API *******************************/ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Function callback signatures + */ +typedef int (*EspCFunction)(EspRequest *ep, int argc, + struct MprVar **argv); +typedef int (*EspStringCFunction)(EspRequest *ep, int argc, + char **argv); + +/* + * APIs for those hosting the ESP module + */ +extern int espOpen(const Esp *control); +extern void espClose(void); +extern EspRequest *espCreateRequest(EspHandle webServerRequestHandle, + char *uri, MprVar *envObj); +extern void espDestroyRequest(EspRequest *ep); +extern int espProcessRequest(EspRequest *ep, const char *docPath, + char *docBuf, char **errMsg); + +/* + * Method invocation + */ +extern void espDefineCFunction(EspRequest *ep, const char *functionName, + EspCFunction fn, void *thisPtr); +extern void espDefineFunction(EspRequest *ep, const char *functionName, + char *args, char *body); +extern void espDefineStringCFunction(EspRequest *ep, + const char *functionName, EspStringCFunction fn, + void *thisPtr); +extern int espRunFunction(EspRequest *ep, MprVar *obj, + char *functionName, MprArray *args); +extern void espSetResponseCode(EspRequest *ep, int code); +extern void espSetReturn(EspRequest *ep, MprVar value); +extern void *espGetThisPtr(EspRequest *ep); + +/* + * Utility routines to use in C methods + */ +extern void espError(EspRequest *ep, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); +extern int espEvalFile(EspRequest *ep, char *path, MprVar *result, + char **emsg); +extern int espEvalScript(EspRequest *ep, char *script, MprVar *result, + char **emsg); +extern MprVar *espGetLocalObject(EspRequest *ep); +extern MprVar *espGetGlobalObject(EspRequest *ep); +extern EspHandle espGetRequestHandle(EspRequest *ep); +extern MprVar *espGetResult(EspRequest *ep); +extern EjsId espGetScriptHandle(EspRequest *ep); +extern void espRedirect(EspRequest *ep, int code, char *url); +extern void espSetHeader(EspRequest *ep, char *header, + bool allowMultiple); +extern void espSetReturnString(EspRequest *ep, const char *str); +extern int espWrite(EspRequest *ep, char *buf, int size); +extern int espWriteString(EspRequest *ep, char *buf); +extern int espWriteFmt(EspRequest *ep, char *fmt, ...) PRINTF_ATTRIBUTE(2,3); + +/* + * ESP array[] variable access (set will update/create) + */ +extern int espGetVar(EspRequest *ep, EspEnvType oType, char *var, + MprVar *value); +extern char *espGetStringVar(EspRequest *ep, EspEnvType oType, + char *var, char *defaultValue); +extern void espSetVar(EspRequest *ep, EspEnvType oType, char *var, + MprVar value); +extern void espSetStringVar(EspRequest *ep, EspEnvType oType, + const char *var, const char *value); +extern int espUnsetVar(EspRequest *ep, EspEnvType oType, char *var); + +/* + * Object creation and management + */ +extern MprVar espCreateObjVar(char *name, int hashSize); +extern MprVar espCreateArrayVar(char *name, int size); +extern bool espDestroyVar(MprVar *var); +extern MprVar *espCreateProperty(MprVar *obj, char *property, + MprVar *newValue); +extern MprVar *espCreatePropertyValue(MprVar *obj, char *property, + MprVar newValue); +extern int espDeleteProperty(MprVar *obj, char *property); + +/* + * JavaScript variable management. Set will create/update a property. + * All return a property reference. GetProperty will optionally return the + * property in value. + */ +extern MprVar *espGetProperty(MprVar *obj, char *property, + MprVar *value); +extern MprVar *espSetProperty(MprVar *obj, char *property, + MprVar *newValue); +extern MprVar *espSetPropertyValue(MprVar *obj, char *property, + MprVar newValue); + +#if 0 +/* + * Low-level direct read and write of properties. + * FUTURE: -- Read is not (dest, src). MUST WARN IN DOC ABOUT COPY/READ + * Will still cause triggers to run. + */ +extern int espReadProperty(MprVar *dest, MprVar *prop); +extern int espWriteProperty(MprVar *prop, MprVar *newValue); +extern int espWritePropertyValue(MprVar *prop, MprVar newValue); +#endif + + +/* + * Access JavaScript variables by their full name. Can use "." or "[]". For + * example: "global.request['REQUEST_URI']" + * For Read/write, the variables must exist. + */ +extern int espCopyVar(EspRequest *ep, char *var, MprVar *value, + int copyDepth); +extern int espDeleteVar(EspRequest *ep, char *var); +extern int espReadVar(EspRequest *ep, char *var, MprVar *value); +extern int espWriteVar(EspRequest *ep, char *var, MprVar *value); +extern int espWriteVarValue(EspRequest *ep, char *var, MprVar value); + +/* + * Object property enumeration + */ +extern MprVar *espGetFirstProperty(MprVar *obj, int includeFlags); +extern MprVar *espGetNextProperty(MprVar *obj, MprVar *currentProperty, + int includeFlags); +extern int espGetPropertyCount(MprVar *obj, int includeFlags); + +#ifdef __cplusplus +} +#endif +/******************************************************************************/ +#endif /* _h_ESP_h */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/esp/espEnv.h b/source4/lib/appweb/esp/espEnv.h new file mode 100644 index 0000000000..a3c9d9f5c7 --- /dev/null +++ b/source4/lib/appweb/esp/espEnv.h @@ -0,0 +1,128 @@ +/* + * @file espEnv.h + * @brief ESP Environment Variables + */ +/********************************* Copyright **********************************/ +/* + * @copy default + * + * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved. + * + * This software is distributed under commercial and open source licenses. + * You may use the GPL open source license described below or you may acquire + * a commercial license from Mbedthis Software. You agree to be fully bound + * by the terms of either license. Consult the LICENSE.TXT distributed with + * this software for full details. + * + * This software is open source; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See the GNU General Public License for more + * details at: http://www.mbedthis.com/downloads/gplLicense.html + * + * This program is distributed WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * This GPL license does NOT permit incorporating this software into + * proprietary programs. If you are unable to comply with the GPL, you must + * acquire a commercial license to use this software. Commercial licenses + * for this software and support services are available from Mbedthis + * Software at http://www.mbedthis.com + * + * @end + */ + +/******************************************************************************/ + +#ifndef _h_ESP_ENV_h +#define _h_ESP_ENV_h 1 + +/* + * @brief Scripting environment variable array types + */ +typedef enum EspEnvType { + ESP_UNDEFINED_OBJ = -1, + + /** + * Elements for server[]: + * DOCUMENT_ROOT GATEWAY_INTERFACE SERVER_ADDR SERVER_PORT SERVER_NAME + * SERVER_PROTOCOL SERVER_SOFTWARE SERVER_URL UPLOAD_DIR + * FUTURE: SERVER_ADMIN + * FUTURE: this could be shared across all hosts and be made read-only. + */ + ESP_SERVER_OBJ = 0, /*! server[] data */ + + /** + * Elements for session[]: are user defined + */ + ESP_SESSION_OBJ = 1, /*! session[] data */ + + /** + * Elements for request[]: + * AUTH_TYPE CONTENT_LENGTH CONTENT_TYPE QUERY_STRING PATH_INFO + * PATH_TRANSLATED REMOTE_ADDR REMOTE_HOST REMOTE_USER REQUEST_METHOD + * REQUEST_URI SCRIPT_FILENAME SCRIPT_NAME + * FUTURE: FILEPATH_INFO REDIRECT_URL SELF REMOTE_PORT AUTH_USER + * AUTH_GROUP AUTH_ACL + */ + ESP_REQUEST_OBJ = 2, /*! request[] data */ + + /** + * Elements for headers[]: + * HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_CONNECTION HTTP_HOST + * HTTP_REFERER HTTP_USER_AGENT and any other custom headers + */ + ESP_HEADERS_OBJ = 3, /*! header [] data */ + + /** + * Elements for cookies[]: are defined by the HTTP request + */ + ESP_COOKIES_OBJ = 4, /*! cookies[] data */ + + /** + * Elements for files[]: are defined by the HTTP request + * CLIENT_FILENAME CONTENT_TYPE FILENAME SIZE + */ + ESP_FILES_OBJ = 5, /*! files[] data */ + + /** + * Elements for form[]: are defined by the HTTP request + */ + ESP_FORM_OBJ = 6, /*! form[] data */ + + /** + * Elements for application[]: are user defined + */ + ESP_APPLICATION_OBJ = 7, /*! application[] data */ + + /** + * Elements for global[]: are defined by ESP/EJS + */ + ESP_GLOBAL_OBJ = 8, /*! global [] data */ + + /* + * Elements for local[]: are defined by ESP/EJS + */ + ESP_LOCAL_OBJ = 9, /*! local [] data */ +} EspEnvType; + +#define ESP_OBJ_MAX 10 /* Total objects */ + +#if BLD_SQUEEZE +#define ESP_HASH_SIZE 19 /* Size of hash tables */ +#else +#define ESP_HASH_SIZE 37 +#endif + +/******************************************************************************/ +#endif /* _h_ESP_ENV_h */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/esp/espProcs.c b/source4/lib/appweb/esp/espProcs.c new file mode 100644 index 0000000000..28b69a8a6f --- /dev/null +++ b/source4/lib/appweb/esp/espProcs.c @@ -0,0 +1,246 @@ +/* + * @file espProcs.c + * @brief Embedded Server Pages (ESP) Procedures. + * @overview These ESP procedures can be used in ESP pages for common tasks. + */ +/********************************* Copyright **********************************/ +/* + * @copy default + * + * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved. + * + * This software is distributed under commercial and open source licenses. + * You may use the GPL open source license described below or you may acquire + * a commercial license from Mbedthis Software. You agree to be fully bound + * by the terms of either license. Consult the LICENSE.TXT distributed with + * this software for full details. + * + * This software is open source; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See the GNU General Public License for more + * details at: http://www.mbedthis.com/downloads/gplLicense.html + * + * This program is distributed WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * This GPL license does NOT permit incorporating this software into + * proprietary programs. If you are unable to comply with the GPL, you must + * acquire a commercial license to use this software. Commercial licenses + * for this software and support services are available from Mbedthis + * Software at http://www.mbedthis.com + * + * @end + */ +/********************************** Includes **********************************/ + +#include "esp.h" + +/************************************ Code ************************************/ +#if BLD_FEATURE_ESP_MODULE +#if BLD_FEATURE_SESSION +/* + * destroySession + */ + +static int destroySessionProc(EspRequest *ep, int argc, char **argv) +{ + ep->esp->destroySession(ep->requestHandle); + return 0; +} + +#endif /* BLD_FEATURE_SESSION */ + +/******************************************************************************/ +/* + * include + * + * This includes javascript libraries. For example: + * + * <% include("file", ...); %> + * + * Don't confuse with ESP includes: + * + * <% include file.esp %> + * + * Filenames are relative to the base document including the file. + * FUTURE -- move back to EJS. Only here now because we need ep->readFile. + */ + +static int includeProc(EspRequest *ep, int argc, char **argv) +{ + const Esp *esp; + char path[MPR_MAX_FNAME], dir[MPR_MAX_FNAME]; + char *emsg=NULL, *buf; + int size, i; + + esp = ep->esp; + mprAssert(argv); + for (i = 0; i < argc; i++) { + const char *extension; + + if (argv[i][0] != '/') { + mprGetDirName(dir, sizeof(dir), ep->docPath); + mprSprintf(path, sizeof(path), "%s/%s", dir, argv[i]); + } else { + mprSprintf(path, sizeof(path), "%s", argv[i]); + } + + if (esp->readFile(ep->requestHandle, &buf, &size, path) < 0) { + espError(ep, "Can't read include file: %s", path); + return MPR_ERR_CANT_ACCESS; + } + buf[size] = '\0'; + + extension = strrchr(argv[i], '.'); + /* this makes handling include files in esp scripts much more convenient */ + if (extension && strcasecmp(extension, ".esp") == 0) { + if (espProcessRequest(ep, path, buf, &emsg) != 0) { + espError(ep, "Cant evaluate script - %s", emsg?emsg:""); + mprFree(buf); + return -1; + } + } else { + if (ejsEvalScript(espGetScriptHandle(ep), buf, 0, &emsg) < 0) { + espError(ep, "Cant evaluate script - %s", emsg?emsg:""); + mprFree(buf); + return -1; + } + } + mprFree(buf); + } + return 0; +} + +/******************************************************************************/ +/* + * redirect + * + * This implemements <% redirect(url, code); %> command. The redirection + * code is optional. + */ + +static int redirectProc(EspRequest *ep, int argc, char **argv) +{ + char *url; + int code; + + if (argc < 1) { + espError(ep, "Bad args"); + return MPR_ERR_BAD_ARGS; + } + url = argv[0]; + if (argc == 2) { + code = atoi(argv[1]); + } else { + code = 302; + } + espRedirect(ep, code, url); + return 0; +} + +/******************************************************************************/ +#if BLD_FEATURE_SESSION +/* + * useSession + */ + +static int useSessionProc(EspRequest *ep, int argc, char **argv) +{ + int timeout; + + if (argc > 1) { + espError(ep, "Bad args"); + return MPR_ERR_BAD_ARGS; + + } else if (argc == 1) { + timeout = atoi(argv[0]); + } else { + timeout = 0; + } + + ep->esp->createSession(ep->requestHandle, timeout); + espSetReturnString(ep, ep->esp->getSessionId(ep->requestHandle)); + return 0; +} + +#endif /* BLD_FEATURE_SESSION */ +/******************************************************************************/ +/* + * setHeader + * + * This implemements <% setHeader("key: value", allowMultiple); %> command. + */ + +static int setHeaderProc(EspRequest *ep, int argc, char **argv) +{ + mprAssert(argv); + if (argc != 2) { + espError(ep, "Bad args"); + return MPR_ERR_BAD_ARGS; + } + ep->esp->setHeader(ep->requestHandle, argv[0], atoi(argv[1])); + return 0; +} + +/******************************************************************************/ +/* + * write + * + * This implemements <% write("text"); %> command. + */ + +static int writeProc(EspRequest *ep, int argc, char **argv) +{ + char *s; + int i, len; + + mprAssert(argv); + for (i = 0; i < argc; i++) { + s = argv[i]; + len = strlen(s); + if (len > 0) { + if (espWrite(ep, s, len) != len) { + espError(ep, "Can't write to client"); + return -1; + } + } + } + return 0; +} + +/******************************************************************************/ + +void espRegisterProcs() +{ + espDefineStringCFunction(0, "write", writeProc, 0); + espDefineStringCFunction(0, "setHeader", setHeaderProc, 0); + espDefineStringCFunction(0, "redirect", redirectProc, 0); + espDefineStringCFunction(0, "include", includeProc, 0); + +#if BLD_FEATURE_SESSION + /* + * Create and use are synonomous + */ + espDefineStringCFunction(0, "useSession", useSessionProc, 0); + espDefineStringCFunction(0, "createSession", useSessionProc, 0); + espDefineStringCFunction(0, "destroySession", destroySessionProc, 0); +#endif +} + +/******************************************************************************/ + +#else +void mprEspControlsDummy() {} + +#endif /* BLD_FEATURE_ESP_MODULE */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/ejs/mprOs.h b/source4/lib/ejs/mprOs.h deleted file mode 100644 index 6a6d0b4205..0000000000 --- a/source4/lib/ejs/mprOs.h +++ /dev/null @@ -1,627 +0,0 @@ -/* - * @file mprOs.h - * @brief Include O/S headers and smooth out per-O/S differences - * @copy default - * - * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved. - * - * This software is distributed under commercial and open source licenses. - * You may use the GPL open source license described below or you may acquire - * a commercial license from Mbedthis Software. You agree to be fully bound - * by the terms of either license. Consult the LICENSE.TXT distributed with - * this software for full details. - * - * This software is open source; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. See the GNU General Public License for more - * details at: http://www.mbedthis.com/downloads/gplLicense.html - * - * This program is distributed WITHOUT ANY WARRANTY; without even the - * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * This GPL license does NOT permit incorporating this software into - * proprietary programs. If you are unable to comply with the GPL, you must - * acquire a commercial license to use this software. Commercial licenses - * for this software and support services are available from Mbedthis - * Software at http://www.mbedthis.com - * - * @end - ******************************* Documentation ********************************* - * - * This header is part of the Mbedthis Portable Runtime and aims to include - * all necessary O/S headers and to unify the constants and declarations - * required by Mbedthis products. It can be included by C or C++ programs. - * - ********************************************************************************/ - -#error foo - -blah blah; - -#ifndef _h_MPR_OS_HDRS -#define _h_MPR_OS_HDRS 1 - -#include "lib/ejs/config.h" - -/********************************* CPU Families *********************************/ -/* - * Porters, add your CPU families here and update configure code. - */ -#define MPR_CPU_UNKNOWN 0 -#define MPR_CPU_IX86 1 -#define MPR_CPU_PPC 2 -#define MPR_CPU_SPARC 3 -#define MPR_CPU_XSCALE 4 -#define MPR_CPU_ARM 5 -#define MPR_CPU_MIPS 6 -#define MPR_CPU_68K 7 -#define MPR_CPU_SIMNT 8 /* VxWorks NT simulator */ -#define MPR_CPU_SIMSPARC 9 /* VxWorks sparc simulator */ - -/********************************* O/S Includes *********************************/ - -#if LINUX || SOLARIS - #include <sys/types.h> - #include <time.h> - #include <arpa/inet.h> - #include <ctype.h> - #include <dlfcn.h> - #include <fcntl.h> - #include <grp.h> - #include <errno.h> - #include <libgen.h> - #include <limits.h> - #include <netdb.h> - #include <net/if.h> - #include <netinet/in.h> - #include <netinet/tcp.h> - #include <netinet/ip.h> - #include <pthread.h> - #include <pwd.h> - #include <resolv.h> - #include <signal.h> - #include <stdarg.h> - #include <stdio.h> - #include <stdlib.h> - #include <string.h> - #include <syslog.h> - #include <sys/ioctl.h> - #include <sys/stat.h> - #include <sys/param.h> - #include <sys/resource.h> - #include <sys/sem.h> - #include <sys/shm.h> - #include <sys/socket.h> - #include <sys/select.h> - #include <sys/time.h> - #include <sys/times.h> - #include <sys/utsname.h> - #include <sys/wait.h> - #include <unistd.h> - -#if LINUX - #include <stdint.h> -#endif - -#if SOLARIS - #include <netinet/in_systm.h> -#endif - -#if BLD_FEATURE_FLOATING_POINT - #define __USE_ISOC99 1 - #include <math.h> - #include <values.h> -#endif - -#endif /* LINUX || SOLARIS */ - -#if VXWORKS - #include <vxWorks.h> - #include <envLib.h> - #include <sys/types.h> - #include <time.h> - #include <arpa/inet.h> - #include <ctype.h> - #include <fcntl.h> - #include <errno.h> - #include <limits.h> - #include <loadLib.h> - #include <netdb.h> - #include <net/if.h> - #include <netinet/tcp.h> - #include <netinet/in.h> - #include <netinet/ip.h> - #include <signal.h> - #include <stdarg.h> - #include <stdio.h> - #include <stdlib.h> - #include <string.h> - #include <sysSymTbl.h> - #include <sys/fcntlcom.h> - #include <sys/ioctl.h> - #include <sys/stat.h> - #include <sys/socket.h> - #include <sys/times.h> - #include <sys/wait.h> - #include <unistd.h> - #include <unldLib.h> - - #if BLD_FEATURE_FLOATING_POINT - #include <float.h> - #define __USE_ISOC99 1 - #include <math.h> - #endif - - #include <sockLib.h> - #include <inetLib.h> - #include <ioLib.h> - #include <pipeDrv.h> - #include <hostLib.h> - #include <netdb.h> - #include <tickLib.h> - #include <taskHookLib.h> - -#endif /* VXWORKS */ - -#if MACOSX - #include <time.h> - #include <arpa/inet.h> - #include <ctype.h> - #include <fcntl.h> - #include <grp.h> - #include <errno.h> - #include <libgen.h> - #include <limits.h> - #include <mach-o/dyld.h> - #include <netdb.h> - #include <net/if.h> - #include <netinet/in_systm.h> - #include <netinet/in.h> - #include <netinet/tcp.h> - #include <netinet/ip.h> - #include <pthread.h> - #include <pwd.h> - #include <resolv.h> - #include <signal.h> - #include <stdarg.h> - #include <stdio.h> - #include <stdlib.h> - #include <stdint.h> - #include <string.h> - #include <syslog.h> - #include <sys/ioctl.h> - #include <sys/types.h> - #include <sys/stat.h> - #include <sys/param.h> - #include <sys/resource.h> - #include <sys/sem.h> - #include <sys/shm.h> - #include <sys/socket.h> - #include <sys/select.h> - #include <sys/time.h> - #include <sys/times.h> - #include <sys/types.h> - #include <sys/utsname.h> - #include <sys/wait.h> - #include <unistd.h> -#endif /* MACOSX */ - -#if WIN - #include <ctype.h> - #include <conio.h> - #include <direct.h> - #include <errno.h> - #include <fcntl.h> - #include <io.h> - #include <limits.h> - #include <malloc.h> - #include <process.h> - #include <sys/stat.h> - #include <sys/types.h> - #include <stddef.h> - #include <stdio.h> - #include <stdlib.h> - #include <string.h> - #include <stdarg.h> - #include <time.h> - #define WIN32_LEAN_AND_MEAN - #include <winsock2.h> - #include <windows.h> - #include <winbase.h> - #if BLD_FEATURE_FLOATING_POINT - #include <float.h> - #endif - #include <shlobj.h> - #include <shellapi.h> - #include <wincrypt.h> -#endif /* WIN */ - -#ifdef __cplusplus -extern "C" { -#endif - -/********************************************************************************/ -/******************************* General Defines ********************************/ -/********************************************************************************/ - -#define MAXINT INT_MAX -#define BITS(type) (BITSPERBYTE * (int) sizeof(type)) - -#ifndef max -#define max(a,b) (((a) > (b)) ? (a) : (b)) -#endif - -#ifndef min -#define min(a,b) (((a) < (b)) ? (a) : (b)) -#endif - -/* - * Set FD_SETSIZE to the maximum number of files (sockets) that you want to - * support. It is used in select.cpp. - * - * #ifdef FD_SETSIZE - * #undef FD_SETSIZE - * #endif - * #define FD_SETSIZE 128 - */ - -typedef char *MprStr; /* Used for dynamic strings */ - -/********************************************************************************/ -/*******************************/ Linux Defines *********************************/ -/********************************************************************************/ - -#if LINUX - typedef unsigned char uchar; - -#if BLD_FEATURE_INT64 - __extension__ typedef long long int int64; - __extension__ typedef unsigned long long int uint64; - #define INT64(x) (x##LL) -#endif - - #define closesocket(x) close(x) - #define MPR_BINARY "" - #define MPR_TEXT "" - #define O_BINARY 0 - #define O_TEXT 0 - #define SOCKET_ERROR -1 - #define MPR_DLL_EXT ".so" - -#if BLD_FEATURE_FLOATING_POINT - #define MAX_FLOAT MAXFLOAT -#endif - - #if BLD_FEATURE_MALLOC - /* - * PORTERS: You will need add assembler code for your architecture here - * only if you want to use the fast malloc (BLD_FEATURE_MALLOC) - */ - #if UNUSED - #define MPR_GET_RETURN(ip) __builtin_return_address(0) - #else - #if BLD_HOST_CPU_ARCH == MPR_CPU_IX86 - #define MPR_GET_RETURN(ip) \ - asm("movl 4(%%ebp),%%eax ; movl %%eax,%0" : \ - "=g" (ip) : \ - : "eax") - #endif -#endif /* UNUSED */ -#endif /* BLD_FEATURE_MALLOC */ - -#if FUTURE -/* #define mprGetHiResTime(x) __asm__ __volatile__ ("rdtsc" : "=A" (x)) */ -/* extern char *inet_ntoa_r(const struct in_addr in, char *buffer, int buflen); */ - - /* */ - /* Atomic functions */ - /* */ - typedef struct { volatile int counter; } mprAtomic_t; - - #if BLD_FEATURE_MULTITHREAD - #define LOCK "lock ; " - #else - #define LOCK "" - #endif - - static __inline__ void mprAtomicInc(mprAtomic_t* v) { - __asm__ __volatile__( - LOCK "incl %0" - :"=m" (v->counter) - :"m" (v->counter)); - } - - static __inline__ void mprAtomicDec(mprAtomic_t* v) { - __asm__ __volatile__( - LOCK "decl %0" - :"=m" (v->counter) - :"m" (v->counter)); - } -#endif /* FUTURE */ - -#endif /* LINUX */ - -/********************************************************************************/ -/******************************* VxWorks Defines ********************************/ -/********************************************************************************/ - -#if VXWORKS - - typedef unsigned char uchar; - typedef unsigned int uint; - typedef unsigned long ulong; - - #define HAVE_SOCKLEN_T - typedef int socklen_t; - -#if BLD_FEATURE_INT64 - typedef long long int int64; - typedef unsigned long long int uint64; - #define INT64(x) (x##LL) -#endif - - #define closesocket(x) close(x) - #define getpid() taskIdSelf() - #define MPR_BINARY "" - #define MPR_TEXT "" - #define O_BINARY 0 - #define O_TEXT 0 - #define SOCKET_ERROR -1 - #define MPR_DLL_EXT ".so" - -#if BLD_FEATURE_FLOATING_POINT - #define MAX_FLOAT FLT_MAX -#endif - - #undef R_OK - #define R_OK 4 - #undef W_OK - #define W_OK 2 - #undef X_OK - #define X_OK 1 - #undef F_OK - #define F_OK 0 - - #define MSG_NOSIGNAL 0 - - extern int access(char *path, int mode); - extern int sysClkRateGet(); - - #if BLD_FEATURE_MALLOC - /* - * PORTERS: You will need add assembler code for your architecture here - * only if you want to use the fast malloc (BLD_FEATURE_MALLOC) - */ - #if UNUSED - #define MPR_GET_RETURN(ip) __builtin_return_address(0) - #else - #if BLD_HOST_CPU_ARCH == MPR_CPU_IX86 - #define MPR_GET_RETURN(ip) \ - asm("movl 4(%%ebp),%%eax ; movl %%eax,%0" : \ - "=g" (ip) : \ - : "eax") - #endif -#endif /* UNUSED */ -#endif /* BLD_FEATURE_MALLOC */ -#endif /* VXWORKS */ - -/********************************************************************************/ -/******************************** MacOsx Defines ********************************/ -/********************************************************************************/ -#if MACOSX - typedef unsigned long ulong; - typedef unsigned char uchar; - -#if BLD_FEATURE_INT64 - __extension__ typedef long long int int64; - __extension__ typedef unsigned long long int uint64; - #define INT64(x) (x##LL) -#endif - #define closesocket(x) close(x) - #define MPR_BINARY "" - #define MPR_TEXT "" - #define O_BINARY 0 - #define O_TEXT 0 - #define SOCKET_ERROR -1 - #define MPR_DLL_EXT ".dylib" - #define MSG_NOSIGNAL 0 - #define __WALL 0x40000000 - #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE - -#if BLD_FEATURE_FLOATING_POINT - #define MAX_FLOAT MAXFLOAT -#endif - - #if MPR_FEATURE_MALLOC - /* - * PORTERS: You will need add assembler code for your architecture here - * only if you want to use the fast malloc (MPR_FEATURE_MALLOC) - */ - #define MPR_GET_RETURN(ip) __builtin_return_address - #endif - -#if FUTURE -/* #define mprGetHiResTime(x) __asm__ __volatile__ ("rdtsc" : "=A" (x)) */ -/* extern char *inet_ntoa_r(const struct in_addr in, char *buffer, int buflen); */ - - /* */ - /* Atomic functions */ - /* */ - typedef struct { volatile int counter; } mprAtomic_t; - - #if MPR_FEATURE_MULTITHREAD - #define LOCK "lock ; " - #else - #define LOCK "" - #endif - - static __inline__ void mprAtomicInc(mprAtomic_t* v) { - __asm__ __volatile__( - LOCK "incl %0" - :"=m" (v->counter) - :"m" (v->counter)); - } - - static __inline__ void mprAtomicDec(mprAtomic_t* v) { - __asm__ __volatile__( - LOCK "decl %0" - :"=m" (v->counter) - :"m" (v->counter)); - } -#endif -#endif /* MACOSX */ - -/********************************************************************************/ -/******************************* Windows Defines ********************************/ -/********************************************************************************/ - -#if WIN - typedef unsigned char uchar; - typedef unsigned int uint; - typedef unsigned long ulong; - typedef unsigned short ushort; - -#if BLD_FEATURE_INT64 - typedef __int64 int64; - typedef unsigned __int64 uint64; - #define INT64(x) (x##i64) -#endif - - typedef int uid_t; - typedef void *handle; - typedef char *caddr_t; - typedef long pid_t; - typedef int gid_t; - typedef ushort mode_t; - typedef void *siginfo_t; - - #define HAVE_SOCKLEN_T - typedef int socklen_t; - - #undef R_OK - #define R_OK 4 - #undef W_OK - #define W_OK 2 - #undef X_OK - #define X_OK 1 - #undef F_OK - #define F_OK 0 - - #ifndef EADDRINUSE - #define EADDRINUSE 46 - #endif - #ifndef EWOULDBLOCK - #define EWOULDBLOCK EAGAIN - #endif - #ifndef ENETDOWN - #define ENETDOWN 43 - #endif - #ifndef ECONNRESET - #define ECONNRESET 44 - #endif - #ifndef ECONNREFUSED - #define ECONNREFUSED 45 - #endif - - #define MSG_NOSIGNAL 0 - #define MPR_BINARY "b" - #define MPR_TEXT "t" - -#if BLD_FEATURE_FLOATING_POINT - #define MAX_FLOAT DBL_MAX -#endif - -#ifndef FILE_FLAG_FIRST_PIPE_INSTANCE -#define FILE_FLAG_FIRST_PIPE_INSTANCE 0x00080000 -#endif - - #define access _access - #define close _close - #define fileno _fileno - #define fstat _fstat - #define getpid _getpid - #define open _open - #define putenv _putenv - #define read _read - #define stat _stat - #define umask _umask - #define unlink _unlink - #define write _write - #define strdup _strdup - #define lseek _lseek - - #define mkdir(a,b) _mkdir(a) - #define rmdir(a) _rmdir(a) - - #if BLD_FEATURE_MALLOC - /* - * PORTERS: You will need add assembler code for your architecture here - * only if you want to use the fast malloc (BLD_FEATURE_MALLOC) - */ - #if MPR_CPU_IX86 - #define MPR_GET_RETURN(ip) \ - __asm { mov eax, 4[ebp] } \ - __asm { mov ip, eax } - #endif - #endif - - #define MPR_DLL_EXT ".dll" - - extern void srand48(long); - extern long lrand48(void); - extern long ulimit(int, ...); - extern long nap(long); - extern uint sleep(unsigned int secs); - extern uid_t getuid(void); - extern uid_t geteuid(void); - -#endif /* WIN */ - -/********************************************************************************/ -/*****************************/ Solaris Defines *********************************/ -/********************************************************************************/ - -#if SOLARIS - typedef unsigned char uchar; - -#if BLD_FEATURE_INT64 - typedef long long int int64; - typedef unsigned long long int uint64; - #define INT64(x) (x##LL) -#endif - - #define closesocket(x) close(x) - #define MPR_BINARY "" - #define MPR_TEXT "" - #define O_BINARY 0 - #define O_TEXT 0 - #define SOCKET_ERROR -1 - #define MPR_DLL_EXT ".so" - #define MSG_NOSIGNAL 0 - #define INADDR_NONE ((in_addr_t) 0xffffffff) - #define __WALL 0 - #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE - -#if BLD_FEATURE_FLOATING_POINT - #define MAX_FLOAT MAXFLOAT -#endif - -#endif /* SOLARIS */ - -/********************************************************************************/ -#ifdef __cplusplus -} -#endif - -#endif /* _h_MPR_OS_HDRS */ - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim:tw=78 - * vim600: sw=4 ts=4 fdm=marker - * vim<600: sw=4 ts=4 - */ |