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], '.');
96 /* this makes handling include files in esp scripts much more convenient */
97 if (extension && strcasecmp(extension, ".esp") == 0) {
98 if (espProcessRequest(ep, path, buf, &emsg) != 0) {
99 espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
104 if (ejsEvalScript(espGetScriptHandle(ep), buf, 0, &emsg) < 0) {
105 espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
115 /******************************************************************************/
119 * This implemements <% redirect(url, code); %> command. The redirection
123 static int redirectProc(EspRequest *ep, int argc, char **argv)
129 espError(ep, "Bad args");
130 return MPR_ERR_BAD_ARGS;
134 code = atoi(argv[1]);
138 espRedirect(ep, code, url);
142 /******************************************************************************/
143 #if BLD_FEATURE_SESSION
148 static int useSessionProc(EspRequest *ep, int argc, char **argv)
153 espError(ep, "Bad args");
154 return MPR_ERR_BAD_ARGS;
156 } else if (argc == 1) {
157 timeout = atoi(argv[0]);
162 ep->esp->createSession(ep->requestHandle, timeout);
163 espSetReturnString(ep, ep->esp->getSessionId(ep->requestHandle));
167 #endif /* BLD_FEATURE_SESSION */
168 /******************************************************************************/
172 * This implemements <% setHeader("key: value", allowMultiple); %> command.
175 static int setHeaderProc(EspRequest *ep, int argc, char **argv)
179 espError(ep, "Bad args");
180 return MPR_ERR_BAD_ARGS;
182 ep->esp->setHeader(ep->requestHandle, argv[0], atoi(argv[1]));
186 /******************************************************************************/
190 * This implemements <% write("text"); %> command.
193 static int writeProc(EspRequest *ep, int argc, char **argv)
199 for (i = 0; i < argc; i++) {
203 if (espWrite(ep, s, len) != len) {
204 espError(ep, "Can't write to client");
212 /******************************************************************************/
214 void espRegisterProcs()
216 espDefineStringCFunction(0, "write", writeProc, 0);
217 espDefineStringCFunction(0, "setHeader", setHeaderProc, 0);
218 espDefineStringCFunction(0, "redirect", redirectProc, 0);
219 espDefineStringCFunction(0, "include", includeProc, 0);
221 #if BLD_FEATURE_SESSION
223 * Create and use are synonomous
225 espDefineStringCFunction(0, "useSession", useSessionProc, 0);
226 espDefineStringCFunction(0, "createSession", useSessionProc, 0);
227 espDefineStringCFunction(0, "destroySession", destroySessionProc, 0);
231 /******************************************************************************/
234 void mprEspControlsDummy() {}
236 #endif /* BLD_FEATURE_ESP_MODULE */
244 * vim600: sw=4 ts=4 fdm=marker