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];
79 for (i = 0; i < argc; i++) {
80 if (argv[i][0] != '/') {
81 mprGetDirName(dir, sizeof(dir), ep->docPath);
82 mprSprintf(path, sizeof(path), "%s/%s", dir, argv[i]);
84 mprSprintf(path, sizeof(path), "%s", argv[i]);
87 if (esp->readFile(ep->requestHandle, &buf, &size, path) < 0) {
88 espError(ep, "Can't read include file: %s", path);
89 return MPR_ERR_CANT_ACCESS;
93 if (ejsEvalScript(espGetScriptHandle(ep), buf, 0, &emsg) < 0) {
94 espError(ep, "Cant evaluate script");
103 /******************************************************************************/
107 * This implemements <% redirect(url, code); %> command. The redirection
111 static int redirectProc(EspRequest *ep, int argc, char **argv)
117 espError(ep, "Bad args");
118 return MPR_ERR_BAD_ARGS;
122 code = atoi(argv[1]);
126 espRedirect(ep, code, url);
130 /******************************************************************************/
131 #if BLD_FEATURE_SESSION
136 static int useSessionProc(EspRequest *ep, int argc, char **argv)
141 espError(ep, "Bad args");
142 return MPR_ERR_BAD_ARGS;
144 } else if (argc == 1) {
145 timeout = atoi(argv[0]);
150 ep->esp->createSession(ep->requestHandle, timeout);
151 espSetReturnString(ep, ep->esp->getSessionId(ep->requestHandle));
155 #endif /* BLD_FEATURE_SESSION */
156 /******************************************************************************/
160 * This implemements <% setHeader("key: value", allowMultiple); %> command.
163 static int setHeaderProc(EspRequest *ep, int argc, char **argv)
167 espError(ep, "Bad args");
168 return MPR_ERR_BAD_ARGS;
170 ep->esp->setHeader(ep->requestHandle, argv[0], atoi(argv[1]));
174 /******************************************************************************/
178 * This implemements <% write("text"); %> command.
181 static int writeProc(EspRequest *ep, int argc, char **argv)
187 for (i = 0; i < argc; i++) {
191 if (espWrite(ep, s, len) != len) {
192 espError(ep, "Can't write to client");
200 /******************************************************************************/
202 void espRegisterProcs()
204 espDefineStringCFunction(0, "write", writeProc, 0);
205 espDefineStringCFunction(0, "setHeader", setHeaderProc, 0);
206 espDefineStringCFunction(0, "redirect", redirectProc, 0);
207 espDefineStringCFunction(0, "include", includeProc, 0);
209 #if BLD_FEATURE_SESSION
211 * Create and use are synonomous
213 espDefineStringCFunction(0, "useSession", useSessionProc, 0);
214 espDefineStringCFunction(0, "createSession", useSessionProc, 0);
215 espDefineStringCFunction(0, "destroySession", destroySessionProc, 0);
219 /******************************************************************************/
222 void mprEspControlsDummy() {}
224 #endif /* BLD_FEATURE_ESP_MODULE */
232 * vim600: sw=4 ts=4 fdm=marker