root/tags/5.4.pre05/wxis_src/cgilist.c

Revision 1, 8.8 kB (checked in by heitor.barbieri, 4 years ago)

Criação do svn para Cisis.

Line 
1/* --------------------------------------------------------------- CGILIST.C */
2
3/* ---------------------------------------------------------  C HEADER FILES */
4#include <stdio.h>
5#include <string.h>
6#include <ctype.h>
7/* ------------------------------------------------------------ HEADER FILES */
8#include "../cisis.h"
9#include "easyfc.h"
10#include "cgilist.h"
11/* ----------------------------------------------------------------- globals */
12char *cgi_empty = "";   /* empty string */
13
14/* ============================================================== cgi_getenv */
15char *cgi_getenv(char *env_name)                /* environment name */
16{
17/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
18        1. Get environment content, return string pointer
19
20   1.0 - 28.Oct.1998
21>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
22   char *envp;                  /* environment content pointer */
23
24   /* 1 */
25   envp = getenv(env_name);
26   return envp ? envp : cgi_empty;
27}
28/* ======================================================== cgi_0xHH_to_char */
29static char cgi_0xHH_to_char(char *what)
30{
31/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
32        1. Copied from a magazine (original name: x2h)
33
34   1.0 - 28.Oct.1998
35>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
36   register char digit;
37
38   /* 1 */
39   digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
40   digit *= 16;
41   digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
42   return(digit);
43}
44/* ======================================================== cgi_unescape_url */
45static void cgi_unescape_url(char *url)
46{
47/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
48        1. Copied from a magazine (original name: unescape_url)
49
50   1.0 - 28.Oct.1998
51>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
52   register int x,y;
53
54   /* 1 */
55   for (x=0,y=0; url[y]; ++x,++y) {
56      if ((url[x] = url[y]) == '%') {
57         url[x] = cgi_0xHH_to_char(&url[y+1]);
58         y+=2;
59      }
60   }
61   url[x] = '\0';
62}
63/* ======================================================= cgi_plus_to_space */
64static void cgi_plus_to_space(char *str)
65{
66/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
67        1. Copied from a magazine (original name: plustospace)
68
69   1.0 - 28.Oct.1998
70>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
71   register int x;
72
73   /* 1 */
74   for (x=0; str[x]; x++) if (str[x] == '+') str[x] = ' ';
75}
76/* ========================================================== cgi_method_get */
77char *cgi_method_get(void)
78{
79/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
80        1. Get CGI query string of method GET
81   2. Return the content copy, NULL indicates no query string
82
83   1.0 - 28.Oct.1998
84>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
85   char *buff;          /* auxiliary string buffer pointer */
86
87   /* 1 */
88   buff = getenv("QUERY_STRING");
89   return buff ? strdup(buff) : NULL;
90}
91/* ========================================================= cgi_method_post */
92char *cgi_method_post(EFC_ERROR *err)   /* error structure */
93{
94/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
95        1. Check content type
96   2. Get content lenght
97   3, Allocate buffer for the CGI data
98   4. Read CGI data from stdin
99   5. Return allocated buffer with CGI data
100
101   1.0 - 28.Oct.1998
102>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
103   char *buff;                                  /* auxiliary string buffer pointer */
104   long content_lenght;         /* content lenght */
105   long bytes_read;                     /* bytes read from stdin */
106
107   /* 1 */
108   if (strcmp(cgi_getenv("CONTENT_TYPE"),
109                          "application/x-www-form-urlencoded") != 0) return NULL;
110
111   /* 2 */
112   content_lenght = atol(cgi_getenv("CONTENT_LENGTH"));
113   if (content_lenght < 1L) return NULL;
114
115   /* 3 */
116   buff = efc_new(content_lenght+1);
117   if (!buff) {
118        efc_error(err,CGI_ERROR_POST,"buff");
119      return NULL;
120   }
121
122   /* 4 */
123   bytes_read = (long)fread(buff,1,content_lenght,stdin);
124   if (bytes_read != content_lenght) {
125        efc_error(err,CGI_ERROR_POST,"bytes_read");
126      return NULL;
127   }
128   *(buff+content_lenght) = '\0';
129
130   /* 5 */
131   return buff;
132}
133/* ============================================================ cgi_newparam */
134CGI_PARAM *cgi_newparam(EFC_ERROR *err,         /* error structure */
135                                                           CGI_PARAM *cgi_pos,  /* last cgiparam item */
136                        char *cgicouple)     /* cgi couple name=value */
137{
138/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
139        1.      Allocate cgi parameter structure, reset it
140   2. Set cgi parameter name
141   3. Set cgi parameter value
142   4. Set list pointer
143   5. Return allocated pointer
144
145   1.0 - 16.Nov.1998
146>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
147   CGI_PARAM *cgi_aux;     /* auxiliary cgiparam structure */
148   char *sepp;                                  /* separator pointer */
149
150   /* 1 */
151   cgi_aux = efc_new(sizeof(CGI_PARAM));
152   if (!cgi_aux) {
153        efc_error(err,CGI_ERROR_ALLOC,"newcgiparam");
154        return NULL;
155   }
156   memset(cgi_aux,0x00,sizeof(CGI_PARAM));
157
158   /* 2 */
159   cgi_aux->name = cgicouple;
160
161        /* 3 */
162   sepp = strchr(cgicouple,'=');
163   if (sepp) {
164      *sepp = '\0';
165      cgi_aux->value = ++sepp;
166   } else cgi_aux->value = cgi_empty;
167
168        /* 4 */
169   if (cgi_pos) cgi_pos->next = cgi_aux;
170
171   /* 5 */
172   return cgi_aux;
173}
174/* =============================================================== cgi_split */
175CGI_PARAM *cgi_split(EFC_ERROR *err,            /* error structure */
176                                                        char *query_string)     /* query string buffer */
177{
178/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
179        1. Loop all query string
180   2. Check string end
181   3. Find end of item, set end of string for that item
182   4. Copy item, apply convertion
183   5. Allocate item, append on the list
184   6. Return first item
185
186   1.0 - 16.Nov.1998
187>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
188   CGI_PARAM *cgi_head = NULL;  /* first cgi parameter */
189   CGI_PARAM *cgi_pos = NULL;           /* current cgi parameter */
190   char *p;                                                             /* auxiliary string pointer */
191   char *endp;                                                  /* end of item pointer */
192   char *cgicouple;
193
194   /* 1 */
195   for (p = query_string; p; p = endp) {
196
197        /* 2 */
198      if (!*p) break;
199
200        /* 3 */
201      endp = strchr(p,'&');
202      if (endp) { *endp = '\0'; endp++; }
203
204        /* 4 */
205      cgicouple = strdup(p);
206      if (!cgicouple) {
207        efc_error(err,CGI_ERROR_ALLOC,p);
208        return NULL;
209      }
210      cgi_plus_to_space(cgicouple);
211      cgi_unescape_url(cgicouple);
212
213        /* 5 */
214      cgi_pos = cgi_newparam(err,cgi_pos,cgicouple);
215      if (!cgi_pos) return NULL;
216      if (!cgi_head) cgi_head = cgi_pos;
217
218   } /* for */
219
220   /* 6 */
221   return cgi_head;
222}
223/* ================================================================= cgi_env */
224char *cgi_env(EFC_ERROR *err)           /* error structure */
225{
226/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
227        1. Get method, get query string
228   2. Return query string
229
230   1.0 - 28.Oct.1998
231>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
232   char *req_method;                            /* CGI request method */
233   char *query_string = NULL;   /* CGI query string */
234
235   /* 1 */
236   req_method = cgi_getenv("REQUEST_METHOD");
237   if (strcmp(req_method,"GET") == 0) query_string = cgi_method_get();
238   else
239      if (strcmp(req_method,"POST") == 0) query_string = cgi_method_post(err);
240
241   /* 2 */
242   return query_string;
243}
244/* ================================================================ cgi_read */
245CGI_PARAM *cgi_read(EFC_ERROR *err)             /* error structure */
246{
247/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
248        1. Get CGI query string
249   2. Allocate cgiparam list
250   3. Garbage collector
251   4. Return cgiparam first item
252
253   1.0 - 28.Oct.1998
254>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
255   CGI_PARAM *cgi_head;         /* first cgi parameter */
256   char *cgibuff;                               /* cgi query string buffer pointer */
257
258   /* 1 */
259   cgibuff = cgi_env(err);
260   if (!cgibuff) return NULL;
261
262   /* 2 */
263   cgi_head = cgi_split(err,cgibuff);
264   if (!cgi_head) return NULL;
265
266   /* 3 */
267   efc_free(cgibuff);
268
269   /* 4 */
270   return cgi_head;
271}
272/* ================================================================ cgi_free */
273void cgi_free(CGI_PARAM *cgiparam)      /* first cgi parameter */
274{
275/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
276        1. Garbage collector
277
278   1.0 - 28.Oct.1998
279>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
280        CGI_PARAM *next;   /* next cgi paramater pointer */
281
282   /* 1 */
283   for ( ; cgiparam; cgiparam = next) {
284        next = cgiparam->next;
285      cgiparam->name = efc_free(cgiparam->name);
286      /* cgiparam->value = efc_strfree(cgiparam->value); */
287      efc_free(cgiparam);
288   }
289}
Note: See TracBrowser for help on using the browser.