3 * @brief Embedded Server Pages (ESP) Procedures.
4 * @overview These ESP procedures can be used in ESP pages for common tasks.
6 /********************************* Copyright **********************************/
10 * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
12 * This software is distributed under commercial and open source licenses.
13 * You may use the GPL open source license described below or you may acquire
14 * a commercial license from Mbedthis Software. You agree to be fully bound
15 * by the terms of either license. Consult the LICENSE.TXT distributed with
16 * this software for full details.
18 * This software is open source; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2 of the License, or (at your
21 * option) any later version. See the GNU General Public License for more
22 * details at: http://www.mbedthis.com/downloads/gplLicense.html
24 * This program is distributed WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 * This GPL license does NOT permit incorporating this software into
28 * proprietary programs. If you are unable to comply with the GPL, you must
29 * acquire a commercial license to use this software. Commercial licenses
30 * for this software and support services are available from Mbedthis
31 * Software at http://www.mbedthis.com
35 /********************************** Includes **********************************/
39 /************************************ Code ************************************/
40 #if BLD_FEATURE_ESP_MODULE
41 #if BLD_FEATURE_SESSION
46 static int destroySessionProc(EspRequest *ep, int argc, char **argv)
48 ep->esp->destroySession(ep->requestHandle);
52 #endif /* BLD_FEATURE_SESSION */
54 /******************************************************************************/
58 * This includes javascript libraries. For example:
60 * <% include("file", ...); %>
62 * Don't confuse with ESP includes:
64 * <% include file.esp %>
66 * Filenames are relative to the base document including the file.
67 * FUTURE -- move back to EJS. Only here now because we need ep->readFile.
70 static int includeProc(EspRequest *ep, int argc, char **argv)
73 char path[MPR_MAX_FNAME], dir[MPR_MAX_FNAME];
74 char *emsg=NULL, *buf;
79 for (i = 0; i < argc; i++) {
80 const char *extension;
82 if (argv[i][0] != '/') {
83 mprGetDirName(dir, sizeof(dir), ep->docPath);
84 mprSprintf(path, sizeof(path), "%s/%s", dir, argv[i]);
86 mprSprintf(path, sizeof(path), "%s", argv[i]);
89 if (esp->readFile(ep->requestHandle, &buf, &size, path) < 0) {
90 espError(ep, "Can't read include file: %s", path);
91 return MPR_ERR_CANT_ACCESS;
95 extension = strrchr(argv[i], '.');
98 * Allow nested inclusion of ESP requests
100 if (extension && mprStrCmpAnyCase(extension, ".esp") == 0) {
101 if (espProcessRequest(ep, path, buf, &emsg) != 0) {
102 espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
107 if (ejsEvalScript(espGetScriptHandle(ep), buf, 0, &emsg) < 0) {
108 espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
118 /******************************************************************************/
122 * This implemements <% redirect(url, code); %> command. The redirection
126 static int redirectProc(EspRequest *ep, int argc, char **argv)
132 espError(ep, "Bad args");
133 return MPR_ERR_BAD_ARGS;
137 code = atoi(argv[1]);
141 espRedirect(ep, code, url);
145 /******************************************************************************/
146 #if BLD_FEATURE_SESSION
151 static int useSessionProc(EspRequest *ep, int argc, char **argv)
156 espError(ep, "Bad args");
157 return MPR_ERR_BAD_ARGS;
159 } else if (argc == 1) {
160 timeout = atoi(argv[0]);
165 ep->esp->createSession(ep->requestHandle, timeout);
166 espSetReturnString(ep, ep->esp->getSessionId(ep->requestHandle));
170 #endif /* BLD_FEATURE_SESSION */
171 /******************************************************************************/
175 * This implemements <% setHeader("key: value", allowMultiple); %> command.
178 static int setHeaderProc(EspRequest *ep, int argc, char **argv)
182 espError(ep, "Bad args");
183 return MPR_ERR_BAD_ARGS;
185 ep->esp->setHeader(ep->requestHandle, argv[0], atoi(argv[1]));
189 /******************************************************************************/
193 * This implemements <% write("text"); %> command.
196 static int writeProc(EspRequest *ep, int argc, char **argv)
202 for (i = 0; i < argc; i++) {
206 if (espWrite(ep, s, len) != len) {
207 espError(ep, "Can't write to client");
215 /******************************************************************************/
217 void espRegisterProcs()
219 espDefineStringCFunction(0, "write", writeProc, 0);
220 espDefineStringCFunction(0, "setHeader", setHeaderProc, 0);
221 espDefineStringCFunction(0, "redirect", redirectProc, 0);
222 espDefineStringCFunction(0, "include", includeProc, 0);
224 #if BLD_FEATURE_SESSION
226 * Create and use are synonomous
228 espDefineStringCFunction(0, "useSession", useSessionProc, 0);
229 espDefineStringCFunction(0, "createSession", useSessionProc, 0);
230 espDefineStringCFunction(0, "destroySession", destroySessionProc, 0);
234 /******************************************************************************/
237 void mprEspControlsDummy() {}
239 #endif /* BLD_FEATURE_ESP_MODULE */
247 * vim600: sw=4 ts=4 fdm=marker