r8397: merged an upstream fix for the expression bug tpot found yesterday
[ira/wip.git] / source4 / lib / appweb / esp / espProcs.c
1 /*
2  *      @file   espProcs.c
3  *      @brief  Embedded Server Pages (ESP) Procedures.
4  *      @overview These ESP procedures can be used in ESP pages for common tasks.
5  */
6 /********************************* Copyright **********************************/
7 /*
8  *      @copy   default
9  *      
10  *      Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
11  *      
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.
17  *      
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
23  *      
24  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
25  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
26  *      
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 
32  *      
33  *      @end
34  */
35 /********************************** Includes **********************************/
36
37 #include        "esp.h"
38
39 /************************************ Code ************************************/
40 #if BLD_FEATURE_ESP_MODULE
41 #if BLD_FEATURE_SESSION
42 /*
43  *      destroySession
44  */
45
46 static int destroySessionProc(EspRequest *ep, int argc, char **argv)
47 {
48         ep->esp->destroySession(ep->requestHandle);
49         return 0;
50 }
51
52 #endif  /* BLD_FEATURE_SESSION */
53
54 /******************************************************************************/
55 /*
56  *      include
57  *
58  *      This includes javascript libraries. For example:
59  *
60  *              <% include("file", ...); %> 
61  *
62  *      Don't confuse with ESP includes:
63  *
64  *              <% include file.esp %>
65  *
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.
68  */ 
69
70 static int includeProc(EspRequest *ep, int argc, char **argv)
71 {
72         const Esp               *esp;
73         char    path[MPR_MAX_FNAME], dir[MPR_MAX_FNAME];
74         char    *emsg=NULL, *buf;
75         int             size, i;
76
77         esp = ep->esp;
78         mprAssert(argv);
79         for (i = 0; i < argc; i++) {
80                 const char *extension;
81
82                 if (argv[i][0] != '/') {
83                         mprGetDirName(dir, sizeof(dir), ep->docPath);
84                         mprSprintf(path, sizeof(path), "%s/%s", dir, argv[i]);
85                 } else {
86                         mprSprintf(path, sizeof(path), "%s", argv[i]);
87                 }
88                 
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;
92                 }
93                 buf[size] = '\0';
94
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:"");
100                                 mprFree(buf);
101                                 return -1;
102                         }
103                 } else {
104                         if (ejsEvalScript(espGetScriptHandle(ep), buf, 0, &emsg) < 0) {
105                                 espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
106                                 mprFree(buf);
107                                 return -1;
108                         }
109                 }
110                 mprFree(buf);
111         }
112         return 0;
113 }
114
115 /******************************************************************************/
116 /*
117  *      redirect
118  *
119  *      This implemements <% redirect(url, code); %> command. The redirection 
120  *      code is optional.
121  */ 
122
123 static int redirectProc(EspRequest *ep, int argc, char **argv)
124 {
125         char    *url;
126         int             code;
127
128         if (argc < 1) {
129                 espError(ep, "Bad args");
130                 return MPR_ERR_BAD_ARGS;
131         }
132         url = argv[0];
133         if (argc == 2) {
134                 code = atoi(argv[1]);
135         } else {
136                 code = 302;
137         }
138         espRedirect(ep, code, url);
139         return 0;
140 }
141
142 /******************************************************************************/
143 #if BLD_FEATURE_SESSION
144 /*
145  *      useSession
146  */
147
148 static int useSessionProc(EspRequest *ep, int argc, char **argv)
149 {
150         int                     timeout;
151
152         if (argc > 1) {
153                 espError(ep, "Bad args");
154                 return MPR_ERR_BAD_ARGS;
155
156         } else if (argc == 1) {
157                 timeout = atoi(argv[0]);
158         } else {
159                 timeout = 0;
160         }
161         
162         ep->esp->createSession(ep->requestHandle, timeout);
163         espSetReturnString(ep, ep->esp->getSessionId(ep->requestHandle));
164         return 0;
165 }
166
167 #endif /* BLD_FEATURE_SESSION */
168 /******************************************************************************/
169 /*
170  *      setHeader
171  *
172  *      This implemements <% setHeader("key: value", allowMultiple); %> command.
173  */ 
174
175 static int setHeaderProc(EspRequest *ep, int argc, char **argv)
176 {
177         mprAssert(argv);
178         if (argc != 2) {
179                 espError(ep, "Bad args");
180                 return MPR_ERR_BAD_ARGS;
181         }
182         ep->esp->setHeader(ep->requestHandle, argv[0], atoi(argv[1]));
183         return 0;
184 }
185
186 /******************************************************************************/
187 /*
188  *      write
189  *
190  *      This implemements <% write("text"); %> command.
191  */ 
192
193 static int writeProc(EspRequest *ep, int argc, char **argv)
194 {
195         char    *s;
196         int             i, len;
197
198         mprAssert(argv);
199         for (i = 0; i < argc; i++) {
200                 s = argv[i];
201                 len = strlen(s);
202                 if (len > 0) {
203                         if (espWrite(ep, s, len) != len) {
204                                 espError(ep, "Can't write to client");
205                                 return -1;
206                         }
207                 }
208         }
209         return 0;
210 }
211
212 /******************************************************************************/
213
214 void espRegisterProcs()
215 {
216         espDefineStringCFunction(0, "write", writeProc, 0);
217         espDefineStringCFunction(0, "setHeader", setHeaderProc, 0);
218         espDefineStringCFunction(0, "redirect", redirectProc, 0);
219         espDefineStringCFunction(0, "include", includeProc, 0);
220
221 #if BLD_FEATURE_SESSION
222         /*
223          *      Create and use are synonomous
224          */
225         espDefineStringCFunction(0, "useSession", useSessionProc, 0);
226         espDefineStringCFunction(0, "createSession", useSessionProc, 0);
227         espDefineStringCFunction(0, "destroySession", destroySessionProc, 0);
228 #endif
229 }
230
231 /******************************************************************************/
232
233 #else
234 void mprEspControlsDummy() {}
235
236 #endif /* BLD_FEATURE_ESP_MODULE */
237
238 /*
239  * Local variables:
240  * tab-width: 4
241  * c-basic-offset: 4
242  * End:
243  * vim:tw=78
244  * vim600: sw=4 ts=4 fdm=marker
245  * vim<600: sw=4 ts=4
246  */