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->Hdr->urlstrings == NULL)
28 WCC->Hdr->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->Hdr->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->Hdr->urlstrings);
84 * Diagnostic function to display the contents of all variables
96 Cursor = GetNewHashPos (WCC->Hdr->urlstrings, 0);
97 while (GetNextHashPos(WCC->Hdr->urlstrings, Cursor, &HKLen, &HKey, &U)) {
99 wc_printf("%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->Hdr->urlstrings != NULL) &&
112 GetHash(WC->Hdr->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->Hdr->urlstrings != NULL) &&
127 GetHash(WC->Hdr->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->Hdr->urlstrings != NULL) &&
143 GetHash(WC->Hdr->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->Hdr->urlstrings != NULL) &&
154 GetHash(WC->Hdr->urlstrings, key, keylen, &U))
155 return ChrPtr(((urlcontent *)U)->url_data);
160 const StrBuf *SBSTR(const char *key)
164 if ((WC->Hdr->urlstrings != NULL) &&
165 GetHash(WC->Hdr->urlstrings, key, strlen (key), &U))
166 return ((urlcontent *)U)->url_data;
171 const StrBuf *SBstr(const char *key, size_t keylen)
175 if ((WC->Hdr->urlstrings != NULL) &&
176 GetHash(WC->Hdr->urlstrings, key, keylen, &U))
177 return ((urlcontent *)U)->url_data;
182 long LBstr(const char *key, size_t keylen)
186 if ((WC->Hdr->urlstrings != NULL) &&
187 GetHash(WC->Hdr->urlstrings, key, keylen, &U))
188 return StrTol(((urlcontent *)U)->url_data);
193 long LBSTR(const char *key)
197 if ((WC->Hdr->urlstrings != NULL) &&
198 GetHash(WC->Hdr->urlstrings, key, strlen(key), &U))
199 return StrTol(((urlcontent *)U)->url_data);
204 int IBstr(const char *key, size_t keylen)
208 if ((WC->Hdr->urlstrings != NULL) &&
209 GetHash(WC->Hdr->urlstrings, key, keylen, &U))
210 return StrTol(((urlcontent *)U)->url_data);
215 int IBSTR(const char *key)
219 if ((WC->Hdr->urlstrings != NULL) &&
220 GetHash(WC->Hdr->urlstrings, key, strlen(key), &U))
221 return StrToi(((urlcontent *)U)->url_data);
226 int HaveBstr(const char *key, size_t keylen)
230 if ((WC->Hdr->urlstrings != NULL) &&
231 GetHash(WC->Hdr->urlstrings, key, keylen, &U))
232 return (StrLength(((urlcontent *)U)->url_data) != 0);
237 int HAVEBSTR(const char *key)
241 if ((WC->Hdr->urlstrings != NULL) &&
242 GetHash(WC->Hdr->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->Hdr->urlstrings != NULL) &&
254 GetHash(WC->Hdr->urlstrings, key, keylen, &U))
255 return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0;
260 int YESBSTR(const char *key)
264 if ((WC->Hdr->urlstrings != NULL) &&
265 GetHash(WC->Hdr->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)
297 #ifdef DEBUG_URLSTRINGS
298 lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n", name, cbtype, length);
300 if (WCC->Hdr->urlstrings == NULL)
301 WCC->Hdr->urlstrings = NewHash(1, NULL);
304 if ( (length > 0) && (IsEmptyStr(cbtype)) ) {
305 u = (urlcontent *) malloc(sizeof(urlcontent));
307 safestrncpy(u->url_key, name, sizeof(u->url_key));
308 u->url_data = NewStrBufPlain(content, length);
310 Put(WCC->Hdr->urlstrings, u->url_key, strlen(u->url_key), u, free_url);
311 #ifdef DEBUG_URLSTRINGS
312 lprintf(9, "Key: <%s> len: [%ld] Data: <%s>\n",
314 StrLength(u->url_data),
315 ChrPtr(u->url_data));
319 /** Uploaded files */
320 if ( (length > 0) && (!IsEmptyStr(cbtype)) ) {
321 WCC->upload = NewStrBufPlain(content, length);
322 WCC->upload_length = length;
323 safestrncpy(WCC->upload_filename, filename,
324 sizeof(WC->upload_filename));
325 safestrncpy(WCC->upload_content_type, cbtype,
326 sizeof(WC->upload_content_type));
334 void PutBstr(const char *key, long keylen, StrBuf *Value)
338 if(keylen > sizeof(u->url_key)) {
339 lprintf(1, "URLkey to long! [%s]", key);
343 u = (urlcontent*)malloc(sizeof(urlcontent));
344 memcpy(u->url_key, key, keylen + 1);
346 Put(WC->Hdr->urlstrings, u->url_key, keylen, u, free_url);
351 int ConditionalBstr(StrBuf *Target, WCTemplputParams *TP)
353 if(TP->Tokens->nParameters == 3)
354 return HaveBstr(TKEY(2));
356 if (TP->Tokens->Params[3]->Type == TYPE_LONG)
357 return LBstr(TKEY(2)) == TP->Tokens->Params[3]->lvalue;
359 return strcmp(Bstr(TKEY(2)),
360 TP->Tokens->Params[3]->Start) == 0;
364 void tmplput_bstr(StrBuf *Target, WCTemplputParams *TP)
366 const StrBuf *Buf = SBstr(TKEY(0));
368 StrBufAppendTemplate(Target, TP, Buf, 1);
371 void diagnostics(void)
373 output_headers(1, 1, 1, 0, 0, 0);
374 wc_printf("Session: %d<hr />\n", WC->wc_session);
375 wc_printf("Command: <br /><PRE>\n");
377 StrEscPuts(WC->UrlFragment1);
378 wc_printf("<br />\n");
379 StrEscPuts(WC->UrlFragment2);
380 wc_printf("<br />\n");
381 StrEscPuts(WC->UrlFragment3);
383 wc_printf("</PRE><hr />\n");
384 wc_printf("Variables: <br /><PRE>\n");
386 wc_printf("</PRE><hr />\n");
391 void tmplput_url_part(StrBuf *Target, WCTemplputParams *TP)
394 StrBuf *UrlBuf = NULL;
398 if (TP->Tokens->Params[0]->lvalue == 0) {
399 if (WCC->Hdr->HR.Handler != NULL)
400 UrlBuf = Name = WCC->Hdr->HR.Handler->Name;
402 else if (TP->Tokens->Params[0]->lvalue == 1) {
403 UrlBuf = NewStrBuf();
404 StrBufExtract_token(UrlBuf, WCC->Hdr->HR.ReqLine, 0, '/');
407 UrlBuf = NewStrBuf();
408 StrBufExtract_token(UrlBuf, WCC->Hdr->HR.ReqLine, 1, '/');
411 if (UrlBuf == NULL) {
412 LogTemplateError(Target, "urlbuf", ERR_PARM1, TP, "not set.");
414 StrBufAppendTemplate(Target, TP, UrlBuf, 2);
415 if (Name == NULL) FreeStrBuf(&UrlBuf);
421 InitModule_PARAMHANDLING
424 WebcitAddUrlHandler(HKEY("diagnostics"), "", 0, diagnostics, NEED_URL);
426 RegisterConditional(HKEY("COND:BSTR"), 1, ConditionalBstr, CTX_NONE);
427 RegisterNamespace("BSTR", 1, 2, tmplput_bstr, NULL, CTX_NONE);
428 RegisterNamespace("URLPART", 1, 2, tmplput_url_part, NULL, CTX_NONE);
433 SessionAttachModule_PARAMHANDLING
436 sess->Hdr->urlstrings = NewHash(1,NULL);
440 SessionDetachModule_PARAMHANDLING
443 DeleteHash(&sess->Hdr->urlstrings);