1 | /* |
---|
2 | * form.c -- Form processing (in-memory CGI) for the GoAhead Web server |
---|
3 | * |
---|
4 | * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved. |
---|
5 | * |
---|
6 | * See the file "license.txt" for usage and redistribution license requirements |
---|
7 | */ |
---|
8 | |
---|
9 | /********************************** Description *******************************/ |
---|
10 | |
---|
11 | /* |
---|
12 | * This module implements the /goform handler. It emulates CGI processing |
---|
13 | * but performs this in-process and not as an external process. This enables |
---|
14 | * a very high performance implementation with easy parsing and decoding |
---|
15 | * of query strings and posted data. |
---|
16 | */ |
---|
17 | |
---|
18 | /*********************************** Includes *********************************/ |
---|
19 | |
---|
20 | #include "wsIntrn.h" |
---|
21 | |
---|
22 | /************************************ Locals **********************************/ |
---|
23 | |
---|
24 | static sym_fd_t formSymtab = -1; /* Symbol table for form handlers */ |
---|
25 | |
---|
26 | /************************************* Code ***********************************/ |
---|
27 | /* |
---|
28 | * Process a form request. Returns 1 always to indicate it handled the URL |
---|
29 | */ |
---|
30 | |
---|
31 | int websFormHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg, |
---|
32 | char_t *url, char_t *path, char_t *query) |
---|
33 | { |
---|
34 | sym_t *sp; |
---|
35 | char_t formBuf[FNAMESIZE]; |
---|
36 | char_t *cp, *formName; |
---|
37 | int (*fn)(void *sock, char_t *path, char_t *args); |
---|
38 | |
---|
39 | a_assert(websValid(wp)); |
---|
40 | a_assert(url && *url); |
---|
41 | a_assert(path && *path == '/'); |
---|
42 | |
---|
43 | websStats.formHits++; |
---|
44 | |
---|
45 | /* |
---|
46 | * Extract the form name |
---|
47 | */ |
---|
48 | gstrncpy(formBuf, path, TSZ(formBuf)); |
---|
49 | if ((formName = gstrchr(&formBuf[1], '/')) == NULL) { |
---|
50 | websError(wp, 200, T("Missing form name")); |
---|
51 | return 1; |
---|
52 | } |
---|
53 | formName++; |
---|
54 | if ((cp = gstrchr(formName, '/')) != NULL) { |
---|
55 | *cp = '\0'; |
---|
56 | } |
---|
57 | |
---|
58 | /* |
---|
59 | * Lookup the C form function first and then try tcl (no javascript support |
---|
60 | * yet). |
---|
61 | */ |
---|
62 | sp = symLookup(formSymtab, formName); |
---|
63 | if (sp == NULL) { |
---|
64 | websError(wp, 200, T("Form %s is not defined"), formName); |
---|
65 | } else { |
---|
66 | fn = (int (*)(void *, char_t *, char_t *)) sp->content.value.integer; |
---|
67 | a_assert(fn); |
---|
68 | if (fn) { |
---|
69 | /* |
---|
70 | * For good practice, forms must call websDone() |
---|
71 | */ |
---|
72 | (*fn)((void*) wp, formName, query); |
---|
73 | |
---|
74 | /* |
---|
75 | * Remove the test to force websDone, since this prevents |
---|
76 | * the server "push" from a form> |
---|
77 | */ |
---|
78 | #if 0 /* push */ |
---|
79 | if (websValid(wp)) { |
---|
80 | websError(wp, 200, T("Form didn't call websDone")); |
---|
81 | } |
---|
82 | #endif /* push */ |
---|
83 | } |
---|
84 | } |
---|
85 | return 1; |
---|
86 | } |
---|
87 | |
---|
88 | /******************************************************************************/ |
---|
89 | /* |
---|
90 | * Define a form function in the "form" map space. |
---|
91 | */ |
---|
92 | |
---|
93 | int websFormDefine(char_t *name, void (*fn)(webs_t wp, char_t *path, |
---|
94 | char_t *query)) |
---|
95 | { |
---|
96 | a_assert(name && *name); |
---|
97 | a_assert(fn); |
---|
98 | |
---|
99 | if (fn == NULL) { |
---|
100 | return -1; |
---|
101 | } |
---|
102 | |
---|
103 | symEnter(formSymtab, name, valueInteger((int) fn), (int) NULL); |
---|
104 | return 0; |
---|
105 | } |
---|
106 | |
---|
107 | /******************************************************************************/ |
---|
108 | /* |
---|
109 | * Open the symbol table for forms. |
---|
110 | */ |
---|
111 | |
---|
112 | void websFormOpen() |
---|
113 | { |
---|
114 | formSymtab = symOpen(WEBS_SYM_INIT); |
---|
115 | } |
---|
116 | |
---|
117 | /******************************************************************************/ |
---|
118 | /* |
---|
119 | * Close the symbol table for forms. |
---|
120 | */ |
---|
121 | |
---|
122 | void websFormClose() |
---|
123 | { |
---|
124 | if (formSymtab != -1) { |
---|
125 | symClose(formSymtab); |
---|
126 | formSymtab = -1; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | /******************************************************************************/ |
---|
131 | /* |
---|
132 | * Write a webs header. This is a convenience routine to write a common |
---|
133 | * header for a form back to the browser. |
---|
134 | */ |
---|
135 | |
---|
136 | void websHeader(webs_t wp) |
---|
137 | { |
---|
138 | a_assert(websValid(wp)); |
---|
139 | |
---|
140 | websWrite(wp, T("HTTP/1.0 200 OK\n")); |
---|
141 | |
---|
142 | /* |
---|
143 | * By license terms the following line of code must not be modified |
---|
144 | */ |
---|
145 | websWrite(wp, T("Server: %s\r\n"), WEBS_NAME); |
---|
146 | |
---|
147 | websWrite(wp, T("Pragma: no-cache\n")); |
---|
148 | websWrite(wp, T("Cache-control: no-cache\n")); |
---|
149 | websWrite(wp, T("Content-Type: text/html\n")); |
---|
150 | websWrite(wp, T("\n")); |
---|
151 | websWrite(wp, T("<html>\n")); |
---|
152 | } |
---|
153 | |
---|
154 | /******************************************************************************/ |
---|
155 | /* |
---|
156 | * Write a webs footer |
---|
157 | */ |
---|
158 | |
---|
159 | void websFooter(webs_t wp) |
---|
160 | { |
---|
161 | a_assert(websValid(wp)); |
---|
162 | |
---|
163 | websWrite(wp, T("</html>\n")); |
---|
164 | } |
---|
165 | |
---|
166 | /******************************************************************************/ |
---|