r15356: Remove unused 'flags' argument from socket_send() and friends.
[bbaumbach/samba-autobuild/.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
97                 /*
98                  *      Allow nested inclusion of ESP requests
99                  */
100                 if (extension && mprStrCmpAnyCase(extension, ".esp") == 0) {
101                         if (espProcessRequest(ep, path, buf, &emsg) != 0) {
102                                 espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
103                                 mprFree(buf);
104                                 return -1;
105                         }
106                 } else {
107                         if (ejsEvalScript(espGetScriptHandle(ep), buf, 0, &emsg) < 0) {
108                                 espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
109                                 mprFree(buf);
110                                 return -1;
111                         }
112                 }
113                 mprFree(buf);
114         }
115         return 0;
116 }
117
118 /******************************************************************************/
119 /*
120  *      redirect
121  *
122  *      This implemements <% redirect(url, code); %> command. The redirection 
123  *      code is optional.
124  */ 
125
126 static int redirectProc(EspRequest *ep, int argc, char **argv)
127 {
128         char    *url;
129         int             code;
130
131         if (argc < 1) {
132                 espError(ep, "Bad args");
133                 return MPR_ERR_BAD_ARGS;
134         }
135         url = argv[0];
136         if (argc == 2) {
137                 code = atoi(argv[1]);
138         } else {
139                 code = 302;
140         }
141         espRedirect(ep, code, url);
142         return 0;
143 }
144
145 /******************************************************************************/
146 #if BLD_FEATURE_SESSION
147 /*
148  *      useSession
149  */
150
151 static int useSessionProc(EspRequest *ep, int argc, char **argv)
152 {
153         int                     timeout;
154
155         if (argc > 1) {
156                 espError(ep, "Bad args");
157                 return MPR_ERR_BAD_ARGS;
158
159         } else if (argc == 1) {
160                 timeout = atoi(argv[0]);
161         } else {
162                 timeout = 0;
163         }
164         
165         ep->esp->createSession(ep->requestHandle, timeout);
166         espSetReturnString(ep, ep->esp->getSessionId(ep->requestHandle));
167         return 0;
168 }
169
170 #endif /* BLD_FEATURE_SESSION */
171 /******************************************************************************/
172 /*
173  *      setHeader
174  *
175  *      This implemements <% setHeader("key: value", allowMultiple); %> command.
176  */ 
177
178 static int setHeaderProc(EspRequest *ep, int argc, char **argv)
179 {
180         mprAssert(argv);
181         if (argc != 2) {
182                 espError(ep, "Bad args");
183                 return MPR_ERR_BAD_ARGS;
184         }
185         ep->esp->setHeader(ep->requestHandle, argv[0], atoi(argv[1]));
186         return 0;
187 }
188
189 /******************************************************************************/
190 /*
191  *      write
192  *
193  *      This implemements <% write("text"); %> command.
194  */ 
195
196 static int writeProc(EspRequest *ep, int argc, char **argv)
197 {
198         char    *s;
199         int             i, len;
200
201         mprAssert(argv);
202         for (i = 0; i < argc; i++) {
203                 s = argv[i];
204                 len = strlen(s);
205                 if (len > 0) {
206                         if (espWrite(ep, s, len) != len) {
207                                 espError(ep, "Can't write to client");
208                                 return -1;
209                         }
210                 }
211         }
212         return 0;
213 }
214
215 /******************************************************************************/
216
217 void espRegisterProcs()
218 {
219         espDefineStringCFunction(0, "write", writeProc, 0);
220         espDefineStringCFunction(0, "setHeader", setHeaderProc, 0);
221         espDefineStringCFunction(0, "redirect", redirectProc, 0);
222         espDefineStringCFunction(0, "include", includeProc, 0);
223
224 #if BLD_FEATURE_SESSION
225         /*
226          *      Create and use are synonomous
227          */
228         espDefineStringCFunction(0, "useSession", useSessionProc, 0);
229         espDefineStringCFunction(0, "createSession", useSessionProc, 0);
230         espDefineStringCFunction(0, "destroySession", destroySessionProc, 0);
231 #endif
232 }
233
234 /******************************************************************************/
235
236 #else
237 void mprEspControlsDummy() {}
238
239 #endif /* BLD_FEATURE_ESP_MODULE */
240
241 /*
242  * Local variables:
243  * tab-width: 4
244  * c-basic-offset: 4
245  * End:
246  * vim:tw=78
247  * vim600: sw=4 ts=4 fdm=marker
248  * vim<600: sw=4 ts=4
249  */