2 * $Id: paramhandling.c 6808 2008-12-11 00:00:36Z dothebart $
4 * parse urlparts and post data
10 void free_url(void *U
)
12 urlcontent
*u
= (urlcontent
*) U
;
13 FreeStrBuf(&u
->url_data
);
18 * Extract variables from the URL.
20 void ParseURLParams(StrBuf
*url
)
22 const char *aptr
, *bptr
, *eptr
, *up
;
27 if (WCC
->urlstrings
== NULL
)
28 WCC
->urlstrings
= NewHash(1, NULL
);
29 eptr
= ChrPtr(url
) + StrLength(url
);
31 while ((up
< eptr
) && (!IsEmptyStr(up
))) {
33 while ((aptr
< eptr
) && (*aptr
!= '\0') && (*aptr
!= '='))
40 while ((bptr
< eptr
) && (*bptr
!= '\0')
41 && (*bptr
!= '&') && (*bptr
!= '?') && (*bptr
!= ' ')) {
44 keylen
= aptr
- up
- 1; /* -1 -> '=' */
45 if(keylen
> sizeof(u
->url_key
)) {
46 lprintf(1, "URLkey to long! [%s]", up
);
50 u
= (urlcontent
*) malloc(sizeof(urlcontent
));
51 memcpy(u
->url_key
, up
, keylen
);
52 u
->url_key
[keylen
] = '\0';
54 lprintf(1, "URLkey to long! [%s]", up
);
59 Put(WCC
->urlstrings
, u
->url_key
, keylen
, u
, free_url
);
61 u
->url_data
= NewStrBufPlain(aptr
, len
);
62 StrBufUnescape(u
->url_data
, 1);
66 #ifdef DEBUG_URLSTRINGS
67 lprintf(9, "%s = [%ld] %s\n",
69 StrLength(u
->url_data
),
76 * free urlstring memory
80 DeleteHash(&WC
->urlstrings
);
84 * Diagnostic function to display the contents of all variables
96 Cursor
= GetNewHashPos (WCC
->urlstrings
, 0);
97 while (GetNextHashPos(WCC
->urlstrings
, Cursor
, &HKLen
, &HKey
, &U
)) {
99 wprintf("%38s = %s\n", u
->url_key
, ChrPtr(u
->url_data
));
104 * Return the value of a variable supplied to the current web page (from the url or a form)
107 const char *XBstr(const char *key
, size_t keylen
, size_t *len
)
111 if ((WC
->urlstrings
!= NULL
) &&
112 GetHash(WC
->urlstrings
, key
, keylen
, &U
)) {
113 *len
= StrLength(((urlcontent
*)U
)->url_data
);
114 return ChrPtr(((urlcontent
*)U
)->url_data
);
122 const char *XBSTR(const char *key
, size_t *len
)
126 if ((WC
->urlstrings
!= NULL
) &&
127 GetHash(WC
->urlstrings
, key
, strlen (key
), &U
)){
128 *len
= StrLength(((urlcontent
*)U
)->url_data
);
129 return ChrPtr(((urlcontent
*)U
)->url_data
);
138 const char *BSTR(const char *key
)
142 if ((WC
->urlstrings
!= NULL
) &&
143 GetHash(WC
->urlstrings
, key
, strlen (key
), &U
))
144 return ChrPtr(((urlcontent
*)U
)->url_data
);
149 const char *Bstr(const char *key
, size_t keylen
)
153 if ((WC
->urlstrings
!= NULL
) &&
154 GetHash(WC
->urlstrings
, key
, keylen
, &U
))
155 return ChrPtr(((urlcontent
*)U
)->url_data
);
160 const StrBuf
*SBSTR(const char *key
)
164 if ((WC
->urlstrings
!= NULL
) &&
165 GetHash(WC
->urlstrings
, key
, strlen (key
), &U
))
166 return ((urlcontent
*)U
)->url_data
;
171 const StrBuf
*SBstr(const char *key
, size_t keylen
)
175 if ((WC
->urlstrings
!= NULL
) &&
176 GetHash(WC
->urlstrings
, key
, keylen
, &U
))
177 return ((urlcontent
*)U
)->url_data
;
182 long LBstr(const char *key
, size_t keylen
)
186 if ((WC
->urlstrings
!= NULL
) &&
187 GetHash(WC
->urlstrings
, key
, keylen
, &U
))
188 return StrTol(((urlcontent
*)U
)->url_data
);
193 long LBSTR(const char *key
)
197 if ((WC
->urlstrings
!= NULL
) &&
198 GetHash(WC
->urlstrings
, key
, strlen(key
), &U
))
199 return StrTol(((urlcontent
*)U
)->url_data
);
204 int IBstr(const char *key
, size_t keylen
)
208 if ((WC
->urlstrings
!= NULL
) &&
209 GetHash(WC
->urlstrings
, key
, keylen
, &U
))
210 return StrTol(((urlcontent
*)U
)->url_data
);
215 int IBSTR(const char *key
)
219 if ((WC
->urlstrings
!= NULL
) &&
220 GetHash(WC
->urlstrings
, key
, strlen(key
), &U
))
221 return StrToi(((urlcontent
*)U
)->url_data
);
226 int HaveBstr(const char *key
, size_t keylen
)
230 if ((WC
->urlstrings
!= NULL
) &&
231 GetHash(WC
->urlstrings
, key
, keylen
, &U
))
232 return (StrLength(((urlcontent
*)U
)->url_data
) != 0);
237 int HAVEBSTR(const char *key
)
241 if ((WC
->urlstrings
!= NULL
) &&
242 GetHash(WC
->urlstrings
, key
, strlen(key
), &U
))
243 return (StrLength(((urlcontent
*)U
)->url_data
) != 0);
249 int YesBstr(const char *key
, size_t keylen
)
253 if ((WC
->urlstrings
!= NULL
) &&
254 GetHash(WC
->urlstrings
, key
, keylen
, &U
))
255 return strcmp( ChrPtr(((urlcontent
*)U
)->url_data
), "yes") == 0;
260 int YESBSTR(const char *key
)
264 if ((WC
->urlstrings
!= NULL
) &&
265 GetHash(WC
->urlstrings
, key
, strlen(key
), &U
))
266 return strcmp( ChrPtr(((urlcontent
*)U
)->url_data
), "yes") == 0;
275 * This function is called by the MIME parser to handle data uploaded by
276 * the browser. Form data, uploaded files, and the data from HTTP PUT
277 * operations (such as those found in GroupDAV) all arrive this way.
279 * name Name of the item being uploaded
280 * filename Filename of the item being uploaded
281 * partnum MIME part identifier (not needed)
282 * disp MIME content disposition (not needed)
283 * content The actual data
284 * cbtype MIME content-type
285 * cbcharset Character set
286 * length Content length
287 * encoding MIME encoding type (not needed)
288 * cbid Content ID (not needed)
289 * userdata Not used here
291 void upload_handler(char *name
, char *filename
, char *partnum
, char *disp
,
292 void *content
, char *cbtype
, char *cbcharset
,
293 size_t length
, char *encoding
, char *cbid
, void *userdata
)
296 #ifdef DEBUG_URLSTRINGS
297 lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n", name
, cbtype
, length
);
299 if (WC
->urlstrings
== NULL
)
300 WC
->urlstrings
= NewHash(1, NULL
);
303 if ( (length
> 0) && (IsEmptyStr(cbtype
)) ) {
304 u
= (urlcontent
*) malloc(sizeof(urlcontent
));
306 safestrncpy(u
->url_key
, name
, sizeof(u
->url_key
));
307 u
->url_data
= NewStrBufPlain(content
, length
);
309 Put(WC
->urlstrings
, u
->url_key
, strlen(u
->url_key
), u
, free_url
);
310 #ifdef DEBUG_URLSTRINGS
311 lprintf(9, "Key: <%s> len: [%ld] Data: <%s>\n",
313 StrLength(u
->url_data
),
314 ChrPtr(u
->url_data
));
318 /** Uploaded files */
319 if ( (length
> 0) && (!IsEmptyStr(cbtype
)) ) {
320 WC
->upload
= malloc(length
);
321 if (WC
->upload
!= NULL
) {
322 WC
->upload_length
= length
;
323 safestrncpy(WC
->upload_filename
, filename
,
324 sizeof(WC
->upload_filename
));
325 safestrncpy(WC
->upload_content_type
, cbtype
,
326 sizeof(WC
->upload_content_type
));
327 memcpy(WC
->upload
, content
, length
);
330 lprintf(3, "malloc() failed: %s\n", strerror(errno
));
338 void PutBstr(const char *key
, long keylen
, StrBuf
*Value
)
342 if(keylen
> sizeof(u
->url_key
)) {
343 lprintf(1, "URLkey to long! [%s]", key
);
347 u
= (urlcontent
*)malloc(sizeof(urlcontent
));
348 memcpy(u
->url_key
, key
, keylen
+ 1);
350 Put(WC
->urlstrings
, u
->url_key
, keylen
, u
, free_url
);
355 int ConditionalBstr(StrBuf
*Target
, WCTemplputParams
*TP
)
357 if(TP
->Tokens
->nParameters
== 3)
358 return HaveBstr(TKEY(2));
360 if (TP
->Tokens
->Params
[3]->Type
== TYPE_LONG
)
361 return LBstr(TKEY(2)) == TP
->Tokens
->Params
[3]->lvalue
;
363 return strcmp(Bstr(TKEY(2)),
364 TP
->Tokens
->Params
[3]->Start
) == 0;
368 void tmplput_bstr(StrBuf
*Target
, WCTemplputParams
*TP
)
370 const StrBuf
*Buf
= SBstr(TKEY(0));
372 StrBufAppendTemplate(Target
, TP
, Buf
, 1);
375 void diagnostics(void)
377 output_headers(1, 1, 1, 0, 0, 0);
378 wprintf("Session: %d<hr />\n", WC
->wc_session
);
379 wprintf("Command: <br /><PRE>\n");
380 StrEscPuts(WC
->UrlFragment1
);
382 StrEscPuts(WC
->UrlFragment2
);
384 StrEscPuts(WC
->UrlFragment3
);
385 wprintf("</PRE><hr />\n");
386 wprintf("Variables: <br /><PRE>\n");
388 wprintf("</PRE><hr />\n");
393 void tmplput_url_part(StrBuf
*Target
, WCTemplputParams
*TP
)
399 if (TP
->Tokens
->Params
[0]->lvalue
== 0)
400 UrlBuf
= WCC
->UrlFragment1
;
401 else if (TP
->Tokens
->Params
[0]->lvalue
== 1)
402 UrlBuf
= WCC
->UrlFragment2
;
404 UrlBuf
= WCC
->UrlFragment3
;
405 if (UrlBuf
== NULL
) {
406 LogTemplateError(Target
, "urlbuf", ERR_PARM1
, TP
, "not set.");
408 StrBufAppendTemplate(Target
, TP
, UrlBuf
, 2);
414 InitModule_PARAMHANDLING
417 WebcitAddUrlHandler(HKEY("diagnostics"), diagnostics
, NEED_URL
);
419 RegisterConditional(HKEY("COND:BSTR"), 1, ConditionalBstr
, CTX_NONE
);
420 RegisterNamespace("BSTR", 1, 2, tmplput_bstr
, CTX_NONE
);
421 RegisterNamespace("URLPART", 1, 2, tmplput_url_part
, CTX_NONE
);