]> code.citadel.org Git - citadel.git/blobdiff - webcit/webcit.c
* add transitional beginboxx template and move some places to the new syntax
[citadel.git] / webcit / webcit.c
index 2660b3d2f0c883ae68a6b348cc162f337309e083..c2866279e847c260c6e282df4e8ab688fa51f868 100644 (file)
@@ -5,7 +5,8 @@
  * persistent session to the Citadel server, handling HTTP WebCit requests as
  * they arrive and presenting a user interface.
  */
-
+#include <stdarg.h>
+#define SHOW_ME_VAPPEND_PRINTF
 #include "webcit.h"
 #include "groupdav.h"
 #include "webserver.h"
@@ -87,64 +88,66 @@ long unescape_input(char *buf)
 void free_url(void *U)
 {
        urlcontent *u = (urlcontent*) U;
-       free(u->url_data);
+       FreeStrBuf(&u->url_data);
        free(u);
 }
 
 /*
  * Extract variables from the URL.
  */
-void addurls(char *url, long ulen)
+void ParseURLParams(StrBuf *url)
 {
-       char *aptr, *bptr, *eptr;
-       char *up;
-       char *buf;
+       const char *aptr, *bptr, *eptr, *up;
        int len, keylen;
        urlcontent *u;
        struct wcsession *WCC = WC;
 
        if (WCC->urlstrings == NULL)
                WCC->urlstrings = NewHash(1, NULL);
-       buf = (char*) malloc (ulen + 1);
-       memcpy(buf, url, ulen);
-       buf[ulen] = '\0';
-       eptr = buf + ulen;
-       up = buf;
+       eptr = ChrPtr(url) + StrLength(url);
+       up = ChrPtr(url);
        while ((up < eptr) && (!IsEmptyStr(up))) {
                aptr = up;
                while ((aptr < eptr) && (*aptr != '\0') && (*aptr != '='))
                        aptr++;
-               if (*aptr != '=')
+               if (*aptr != '=') {
                        return;
-               *aptr = '\0';
+               }
                aptr++;
                bptr = aptr;
                while ((bptr < eptr) && (*bptr != '\0')
                      && (*bptr != '&') && (*bptr != '?') && (*bptr != ' ')) {
                        bptr++;
                }
-               *bptr = '\0';
-               u = (urlcontent *) malloc(sizeof(urlcontent));
+               keylen = aptr - up - 1; /* -1 -> '=' */
+               if(keylen > sizeof(u->url_key)) {
+                       lprintf(1, "URLkey to long! [%s]", up);
+                       continue;
+               }
 
-               keylen = safestrncpy(u->url_key, up, sizeof u->url_key);
-               if (keylen < 0){
+               u = (urlcontent *) malloc(sizeof(urlcontent));
+               memcpy(u->url_key, up, keylen);
+               u->url_key[keylen] = '\0';
+               if (keylen < 0) {
                        lprintf(1, "URLkey to long! [%s]", up);
+                       free(u);
                        continue;
                }
 
                Put(WCC->urlstrings, u->url_key, keylen, u, free_url);
                len = bptr - aptr;
-               u->url_data = malloc(len + 2);
-               safestrncpy(u->url_data, aptr, len + 2);
-               u->url_data_size = unescape_input(u->url_data);
-               u->url_data[u->url_data_size] = '\0';
+               u->url_data = NewStrBufPlain(aptr, len);
+               StrBufUnescape(u->url_data, 1);
+            
                up = bptr;
                ++up;
 #ifdef DEBUG_URLSTRINGS
-               lprintf(9, "%s = [%ld]  %s\n", u->url_key, u->url_data_size, u->url_data); 
+               lprintf(9, "%s = [%ld]  %s\n", 
+                       u->url_key, 
+                       StrLength(u->url_data), 
+                       ChrPtr(u->url_data)); 
 #endif
        }
-       free(buf);
 }
 
 /*
@@ -165,13 +168,13 @@ void dump_vars(void)
        urlcontent *u;
        void *U;
        long HKLen;
-       char *HKey;
+       const char *HKey;
        HashPos *Cursor;
        
        Cursor = GetNewHashPos ();
        while (GetNextHashPos(WCC->urlstrings, Cursor, &HKLen, &HKey, &U)) {
                u = (urlcontent*) U;
-               wprintf("%38s = %s\n", u->url_key, u->url_data);
+               wprintf("%38s = %s\n", u->url_key, ChrPtr(u->url_data));
        }
 }
 
@@ -179,14 +182,14 @@ void dump_vars(void)
  * Return the value of a variable supplied to the current web page (from the url or a form)
  */
 
-const char *XBstr(char *key, size_t keylen, size_t *len)
+const char *XBstr(const char *key, size_t keylen, size_t *len)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, keylen, &U)) {
-               *len = ((urlcontent *)U)->url_data_size;
-               return ((urlcontent *)U)->url_data;
+               *len = StrLength(((urlcontent *)U)->url_data);
+               return ChrPtr(((urlcontent *)U)->url_data);
        }
        else {
                *len = 0;
@@ -194,14 +197,14 @@ const char *XBstr(char *key, size_t keylen, size_t *len)
        }
 }
 
-const char *XBSTR(char *key, size_t *len)
+const char *XBSTR(const char *key, size_t *len)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) &&
            GetHash(WC->urlstrings, key, strlen (key), &U)){
-               *len = ((urlcontent *)U)->url_data_size;
-               return ((urlcontent *)U)->url_data;
+               *len = StrLength(((urlcontent *)U)->url_data);
+               return ChrPtr(((urlcontent *)U)->url_data);
        }
        else {
                *len = 0;
@@ -210,113 +213,135 @@ const char *XBSTR(char *key, size_t *len)
 }
 
 
-const char *BSTR(char *key)
+const char *BSTR(const char *key)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) &&
            GetHash(WC->urlstrings, key, strlen (key), &U))
-               return ((urlcontent *)U)->url_data;
+               return ChrPtr(((urlcontent *)U)->url_data);
        else    
                return ("");
 }
 
-const char *Bstr(char *key, size_t keylen)
+const char *Bstr(const char *key, size_t keylen)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, keylen, &U))
-               return ((urlcontent *)U)->url_data;
+               return ChrPtr(((urlcontent *)U)->url_data);
        else    
                return ("");
 }
 
-long LBstr(char *key, size_t keylen)
+const StrBuf *SBSTR(const char *key)
+{
+       void *U;
+
+       if ((WC->urlstrings != NULL) &&
+           GetHash(WC->urlstrings, key, strlen (key), &U))
+               return ((urlcontent *)U)->url_data;
+       else    
+               return NULL;
+}
+
+const StrBuf *SBstr(const char *key, size_t keylen)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, keylen, &U))
-               return atol(((urlcontent *)U)->url_data);
+               return ((urlcontent *)U)->url_data;
+       else    
+               return NULL;
+}
+
+long LBstr(const char *key, size_t keylen)
+{
+       void *U;
+
+       if ((WC->urlstrings != NULL) && 
+           GetHash(WC->urlstrings, key, keylen, &U))
+               return StrTol(((urlcontent *)U)->url_data);
        else    
                return (0);
 }
 
-long LBSTR(char *key)
+long LBSTR(const char *key)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, strlen(key), &U))
-               return atol(((urlcontent *)U)->url_data);
+               return StrTol(((urlcontent *)U)->url_data);
        else    
                return (0);
 }
 
-int IBstr(char *key, size_t keylen)
+int IBstr(const char *key, size_t keylen)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, keylen, &U))
-               return atoi(((urlcontent *)U)->url_data);
+               return StrTol(((urlcontent *)U)->url_data);
        else    
                return (0);
 }
 
-int IBSTR(char *key)
+int IBSTR(const char *key)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, strlen(key), &U))
-               return atoi(((urlcontent *)U)->url_data);
+               return StrToi(((urlcontent *)U)->url_data);
        else    
                return (0);
 }
 
-int HaveBstr(char *key, size_t keylen)
+int HaveBstr(const char *key, size_t keylen)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, keylen, &U))
-               return ((urlcontent *)U)->url_data_size != 0;
+               return (StrLength(((urlcontent *)U)->url_data) != 0);
        else    
                return (0);
 }
 
-int HAVEBSTR(char *key)
+int HAVEBSTR(const char *key)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, strlen(key), &U))
-               return ((urlcontent *)U)->url_data_size != 0;
+               return (StrLength(((urlcontent *)U)->url_data) != 0);
        else    
                return (0);
 }
 
 
-int YesBstr(char *key, size_t keylen)
+int YesBstr(const char *key, size_t keylen)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, keylen, &U))
-               return strcmp( ((urlcontent *)U)->url_data, "yes") == 0;
+               return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0;
        else    
                return (0);
 }
 
-int YESBSTR(char *key)
+int YESBSTR(const char *key)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, strlen(key), &U))
-               return strcmp( ((urlcontent *)U)->url_data, "yes") == 0;
+               return strcmp( ChrPtr(((urlcontent *)U)->url_data), "yes") == 0;
        else    
                return (0);
 }
@@ -326,30 +351,58 @@ int YESBSTR(char *key)
  */
 void wprintf(const char *format,...)
 {
+       struct wcsession *WCC = WC;
        va_list arg_ptr;
-       char wbuf[4096];
+
+       if (WCC->WBuf == NULL)
+               WCC->WBuf = NewStrBuf();
 
        va_start(arg_ptr, format);
-       vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
+       StrBufVAppendPrintf(WCC->WBuf, format, arg_ptr);
        va_end(arg_ptr);
 
-       client_write(wbuf, strlen(wbuf));
+///    if (StrLength(WCC-WBuf) > 2048)
+               ///client_write(wbuf, strlen(wbuf));
 }
 
+/*
+ * http-header-printing funcion. uses our vsnprintf wrapper
+ */
+void hprintf(const char *format,...)
+{
+       struct wcsession *WCC = WC;
+       va_list arg_ptr;
+
+       va_start(arg_ptr, format);
+       StrBufVAppendPrintf(WCC->HBuf, format, arg_ptr);
+       va_end(arg_ptr);
+
+///    if (StrLength(WCC-WBuf) > 2048)
+               ///client_write(wbuf, strlen(wbuf));
+}
+
+
+void tmplput_trailing_javascript(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
+{
+       struct wcsession *WCC = WC;
+
+       if (WCC != NULL)
+               StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType,
+                                    WCC->trailing_javascript, 0);
+}
 
 /*
  * wrap up an HTTP session, closes tags, etc.
  *
  * print_standard_html_footer should be set to:
- * 0 to transmit only,
- * 1 to append the main menu and closing tags,
- * 2 to append the closing tags only.
+ * 0           - to transmit only,
+ * nonzero     - to append the closing tags
  */
 void wDumpContent(int print_standard_html_footer)
 {
        if (print_standard_html_footer) {
-               wprintf("</div>\n");    /* end of "text" div */
-               do_template("trailing");
+               wprintf("</div> <!-- end of 'content' div -->\n");
+               do_template("trailing", NULL);
        }
 
        /* If we've been saving it all up for one big output burst,
@@ -359,89 +412,89 @@ void wDumpContent(int print_standard_html_footer)
 }
 
 
 /*
  * Copy a string, escaping characters which have meaning in HTML.  
  *
- * target              target buffer
- * strbuf              source buffer
- * nbsp                        If nonzero, spaces are converted to non-breaking spaces.
- * nolinebreaks                if set, linebreaks are removed from the string.
+ * target              target buffer
+ * strbuf              source buffer
+ * nbsp                        If nonzero, spaces are converted to non-breaking spaces.
+ * nolinebreaks                if set, linebreaks are removed from the string.
  */
 long stresc(char *target, long tSize, char *strbuf, int nbsp, int nolinebreaks)
 {
-       char *aptr, *bptr, *eptr;
-
-       *target = '\0';
-       aptr = strbuf;
-       bptr = target;
-       eptr = target + tSize - 6; // our biggest unit to put in... 
-
-       while ((bptr < eptr) && !IsEmptyStr(aptr) ){
-               if (*aptr == '<') {
-                       memcpy(bptr, "&lt;", 4);
-                       bptr += 4;
-               }
-               else if (*aptr == '>') {
-                       memcpy(bptr, "&gt;", 4);
-                       bptr += 4;
-               }
-               else if (*aptr == '&') {
-                       memcpy(bptr, "&amp;", 5);
-                       bptr += 5;
-               }
-               else if (*aptr == '\"') {
-                       memcpy(bptr, "&quot;", 6);
-                       bptr += 6;
-               }
-               else if (*aptr == '\'') {
-                       memcpy(bptr, "&#39;", 5);
-                       bptr += 5;
-               }
-               else if (*aptr == LB) {
-                       *bptr = '<';
-                       bptr ++;
-               }
-               else if (*aptr == RB) {
-                       *bptr = '>';
-                       bptr ++;
-               }
-               else if (*aptr == QU) {
-                       *bptr ='"';
-                       bptr ++;
-               }
-               else if ((*aptr == 32) && (nbsp == 1)) {
-                       memcpy(bptr, "&nbsp;", 6);
-                       bptr += 6;
-               }
-               else if ((*aptr == '\n') && (nolinebreaks)) {
-                       *bptr='\0';     /* nothing */
-               }
-               else if ((*aptr == '\r') && (nolinebreaks)) {
-                       *bptr='\0';     /* nothing */
-               }
-               else{
-                       *bptr = *aptr;
-                       bptr++;
-               }
-               aptr ++;
-       }
-       *bptr = '\0';
-       if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
-               return -1;
-       return (bptr - target);
+        char *aptr, *bptr, *eptr;
+        *target = '\0';
+        aptr = strbuf;
+        bptr = target;
+        eptr = target + tSize - 6; // our biggest unit to put in... 
+        while ((bptr < eptr) && !IsEmptyStr(aptr) ){
+                if (*aptr == '<') {
+                        memcpy(bptr, "&lt;", 4);
+                        bptr += 4;
+                }
+                else if (*aptr == '>') {
+                        memcpy(bptr, "&gt;", 4);
+                        bptr += 4;
+                }
+                else if (*aptr == '&') {
+                        memcpy(bptr, "&amp;", 5);
+                        bptr += 5;
+                }
+                else if (*aptr == '\"') {
+                        memcpy(bptr, "&quot;", 6);
+                        bptr += 6;
+                }
+                else if (*aptr == '\'') {
+                        memcpy(bptr, "&#39;", 5);
+                        bptr += 5;
+                }
+                else if (*aptr == LB) {
+                        *bptr = '<';
+                        bptr ++;
+                }
+                else if (*aptr == RB) {
+                        *bptr = '>';
+                        bptr ++;
+                }
+                else if (*aptr == QU) {
+                        *bptr ='"';
+                        bptr ++;
+                }
+                else if ((*aptr == 32) && (nbsp == 1)) {
+                        memcpy(bptr, "&nbsp;", 6);
+                        bptr += 6;
+                }
+                else if ((*aptr == '\n') && (nolinebreaks)) {
+                        *bptr='\0';     /* nothing */
+                }
+                else if ((*aptr == '\r') && (nolinebreaks)) {
+                        *bptr='\0';     /* nothing */
+                }
+                else{
+                        *bptr = *aptr;
+                        bptr++;
+                }
+                aptr ++;
+        }
+        *bptr = '\0';
+        if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
+                return -1;
+        return (bptr - target);
 }
 
+
 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
 {
-       char *buf;
-       long Siz;
+       StrEscAppend(WC->WBuf, NULL, strbuf, nbsp, nolinebreaks);
+}
 
-       if (strbuf == NULL) return;
-       Siz = (3 * strlen(strbuf)) + SIZ ;
-       buf = malloc(Siz);
-       stresc(buf, Siz, strbuf, nbsp, nolinebreaks);
-       wprintf("%s", buf);
-       free(buf);
+void StrEscputs1(const StrBuf *strbuf, int nbsp, int nolinebreaks)
+{
+       StrEscAppend(WC->WBuf, strbuf, NULL, nbsp, nolinebreaks);
 }
 
 /* 
@@ -453,15 +506,37 @@ void escputs(char *strbuf)
 }
 
 
+/* 
+ * static wrapper for ecsputs1
+ */
+void StrEscPuts(const StrBuf *strbuf)
+{
+       StrEscputs1(strbuf, 0, 0);
+}
+
+
 /*
  * urlescape buffer and print it to the client
  */
-void urlescputs(char *strbuf)
+void urlescputs(const char *strbuf)
 {
-       char outbuf[SIZ];
-       
-       urlesc(outbuf, SIZ, strbuf);
-       wprintf("%s", outbuf);
+       StrBufUrlescAppend(WC->WBuf, NULL, strbuf);
+}
+
+/*
+ * urlescape buffer and print it to the client
+ */
+void UrlescPutStrBuf(const StrBuf *strbuf)
+{
+       StrBufUrlescAppend(WC->WBuf, strbuf, NULL);
+}
+
+/**
+ * urlescape buffer and print it as header 
+ */
+void hurlescputs(const char *strbuf) 
+{
+       StrBufUrlescAppend(WC->HBuf, NULL, strbuf);
 }
 
 
@@ -534,81 +609,27 @@ void jsescputs(char *strbuf)
        wprintf("%s", outbuf);
 }
 
-/*
- * Copy a string, escaping characters for message text hold
- */
-void msgesc(char *target, size_t tlen, char *strbuf)
-{
-       int len;
-       char *tend;
-       char *send;
-       char *tptr;
-       char *sptr;
-
-       target[0]='\0';
-       len = strlen (strbuf);
-       send = strbuf + len;
-       tend = target + tlen;
-       sptr = strbuf;
-       tptr = target;
-
-       while (!IsEmptyStr(sptr) && 
-              (sptr < send) &&
-              (tptr < tend)) {
-              
-               if (*sptr == '\n')
-                       *tptr = ' ';
-               else if (*sptr == '\r')
-                       *tptr = ' ';
-               else if (*sptr == '\'') {
-                       if (tend - tptr < 8)
-                               return;
-                       *(tptr++) = '&';
-                       *(tptr++) = '#';
-                       *(tptr++) = '3';
-                       *(tptr++) = '9';
-                       *tptr = ';';
-               } else {
-                       *tptr = *sptr;
-               }
-               tptr++; sptr++;
-       }
-       *tptr = '\0';
-}
-
 /*
  * print a string to the client after cleaning it with msgesc() and stresc()
  */
 void msgescputs1( char *strbuf)
 {
-       char *outbuf;
-       char *outbuf2;
-       int buflen;
+       StrBuf *OutBuf;
 
-       if (strbuf == NULL) return;
-       buflen = 3 * strlen(strbuf) + SIZ;
-       outbuf = malloc( buflen);
-       outbuf2 = malloc( buflen);
-       msgesc(outbuf, buflen, strbuf);
-       stresc(outbuf2, buflen, outbuf, 0, 0);
-       wprintf("%s", outbuf2);
-       free(outbuf);
-       free(outbuf2);
+       if ((strbuf == NULL) || IsEmptyStr(strbuf))
+               return;
+       OutBuf = NewStrBuf();
+       StrMsgEscAppend(OutBuf, NULL, strbuf);
+       StrEscAppend(WC->WBuf, OutBuf, NULL, 0, 0);
+       FreeStrBuf(&OutBuf);
 }
 
 /*
  * print a string to the client after cleaning it with msgesc()
  */
 void msgescputs(char *strbuf) {
-       char *outbuf;
-       size_t len;
-
-       if (strbuf == NULL) return;
-       len =  (3 * strlen(strbuf)) + SIZ;
-       outbuf = malloc(len);
-       msgesc(outbuf, len, strbuf);
-       wprintf("%s", outbuf);
-       free(outbuf);
+       if ((strbuf != NULL) && !IsEmptyStr(strbuf))
+               StrMsgEscAppend(WC->WBuf, NULL, strbuf);
 }
 
 
@@ -632,11 +653,11 @@ void output_headers(      int do_httpheaders,     /* 1 = output HTTP headers
        char cookie[1024];
        char httpnow[128];
 
-       wprintf("HTTP/1.1 200 OK\n");
+       hprintf("HTTP/1.1 200 OK\n");
        http_datestring(httpnow, sizeof httpnow, time(NULL));
 
        if (do_httpheaders) {
-               wprintf("Content-type: text/html; charset=utf-8\r\n"
+               hprintf("Content-type: text/html; charset=utf-8\r\n"
                        "Server: %s / %s\n"
                        "Connection: close\r\n",
                        PACKAGE_STRING, serv_info.serv_software
@@ -649,7 +670,7 @@ void output_headers(        int do_httpheaders,     /* 1 = output HTTP headers
                http_datestring(httpTomorow, sizeof httpTomorow, 
                                time(NULL) + 60 * 60 * 24 * 2);
 
-               wprintf("Pragma: public\r\n"
+               hprintf("Pragma: public\r\n"
                        "Cache-Control: max-age=3600, must-revalidate\r\n"
                        "Last-modified: %s\r\n"
                        "Expires: %s\r\n",
@@ -658,7 +679,7 @@ void output_headers(        int do_httpheaders,     /* 1 = output HTTP headers
                );
        }
        else {
-               wprintf("Pragma: no-cache\r\n"
+               hprintf("Pragma: no-cache\r\n"
                        "Cache-Control: no-store\r\n"
                        "Expires: -1\r\n"
                );
@@ -668,27 +689,22 @@ void output_headers(      int do_httpheaders,     /* 1 = output HTTP headers
                        WC->wc_password, WC->wc_roomname);
 
        if (unset_cookies) {
-               wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
+               hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
        } else {
-               wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
+               hprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
                if (server_cookie != NULL) {
-                       wprintf("%s\n", server_cookie);
+                       hprintf("%s\n", server_cookie);
                }
        }
 
        if (do_htmlhead) {
                begin_burst();
-               if (!access("static.local/webcit.css", R_OK)) {
-                       svprintf(HKEY("CSSLOCAL"), WCS_STRING,
-                          "<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"
-                       );
-               }
-               do_template("head");
+               do_template("head", NULL);
        }
 
        /* ICONBAR */
        if (do_htmlhead) {
-
+               begin_burst();
 
                /* check for ImportantMessages (these display in a div overlaying the main screen) */
                if (!IsEmptyStr(WC->ImportantMessage)) {
@@ -728,13 +744,14 @@ void output_headers(      int do_httpheaders,     /* 1 = output HTTP headers
  * Generic function to do an HTTP redirect.  Easy and fun.
  */
 void http_redirect(const char *whichpage) {
-       wprintf("HTTP/1.1 302 Moved Temporarily\n");
-       wprintf("Location: %s\r\n", whichpage);
-       wprintf("URI: %s\r\n", whichpage);
-       wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
+       hprintf("HTTP/1.1 302 Moved Temporarily\n");
+       hprintf("Location: %s\r\n", whichpage);
+       hprintf("URI: %s\r\n", whichpage);
+       hprintf("Content-type: text/html; charset=utf-8\r\n");
        wprintf("<html><body>");
        wprintf("Go <a href=\"%s\">here</A>.", whichpage);
        wprintf("</body></html>\n");
+       end_burst();
 }
 
 
@@ -742,48 +759,24 @@ void http_redirect(const char *whichpage) {
 /*
  * Output a piece of content to the web browser using conformant HTTP and MIME semantics
  */
-void http_transmit_thing(char *thing, size_t length, const char *content_type,
+void http_transmit_thing(const char *content_type,
                         int is_static) {
 
+#ifndef TECH_PREVIEW
+       lprintf(9, "http_transmit_thing(%s)%s\n",
+               content_type,
+               (is_static ? " (static)" : "")
+       );
+#endif
        output_headers(0, 0, 0, 0, 0, is_static);
 
-       wprintf("Content-type: %s\r\n"
+       hprintf("Content-type: %s\r\n"
                "Server: %s\r\n"
                "Connection: close\r\n",
                content_type,
                PACKAGE_STRING);
 
-#ifdef HAVE_ZLIB
-       /* If we can send the data out compressed, please do so. */
-       if (WC->gzip_ok) {
-               char *compressed_data = NULL;
-               size_t compressed_len;
-
-               compressed_len =  ((length * 101) / 100) + 100;
-               compressed_data = malloc(compressed_len);
-
-               if (compress_gzip((Bytef *) compressed_data,
-                                 &compressed_len,
-                                 (Bytef *) thing,
-                                 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
-                       wprintf("Content-encoding: gzip\r\n"
-                               "Content-length: %ld\r\n"
-                               "\r\n",
-                               (long) compressed_len
-                       );
-                       client_write(compressed_data, (size_t)compressed_len);
-                       free(compressed_data);
-                       return;
-               }
-       }
-#endif
-
-       /* No compression ... just send it out as-is */
-       wprintf("Content-length: %ld\r\n"
-               "\r\n",
-               (long) length
-       );
-       client_write(thing, (size_t)length);
+       end_burst();
 }
 
 /*
@@ -800,7 +793,7 @@ void print_menu_box(char* Title, char *Class, int nLines, ...)
        long i;
        
        svput("BOXTITLE", WCS_STRING, Title);
-       do_template("beginbox");
+       do_template("beginboxx", NULL);
        
        wprintf("<ul class=\"%s\">", Class);
        
@@ -817,7 +810,7 @@ void print_menu_box(char* Title, char *Class, int nLines, ...)
        
        wprintf("</ul>");
        
-       do_template("endbox");
+       do_template("endbox", NULL);
 }
 
 
@@ -826,60 +819,53 @@ void print_menu_box(char* Title, char *Class, int nLines, ...)
  */
 void output_static(char *what)
 {
-       FILE *fp;
+       int fd;
        struct stat statbuf;
        off_t bytes;
        off_t count = 0;
-       size_t res;
-       char *bigbuffer;
        const char *content_type;
        int len;
+       const char *Err;
 
-       fp = fopen(what, "rb");
-       if (fp == NULL) {
+       fd = open(what, O_RDONLY);
+       if (fd <= 0) {
                lprintf(9, "output_static('%s')  -- NOT FOUND --\n", what);
-               wprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
-               wprintf("Content-Type: text/plain\r\n");
-               wprintf("\r\n");
+               hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
+               hprintf("Content-Type: text/plain\r\n");
                wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
+               end_burst();
        } else {
                len = strlen (what);
                content_type = GuessMimeByFilename(what, len);
 
-               if (fstat(fileno(fp), &statbuf) == -1) {
+               if (fstat(fd, &statbuf) == -1) {
                        lprintf(9, "output_static('%s')  -- FSTAT FAILED --\n", what);
-                       wprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
-                       wprintf("Content-Type: text/plain\r\n");
-                       wprintf("\r\n");
+                       hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
+                       hprintf("Content-Type: text/plain\r\n");
                        wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
+                       end_burst();
                        return;
                }
 
                count = 0;
                bytes = statbuf.st_size;
-               if ((bigbuffer = malloc(bytes + 2)) == NULL) {
-                       lprintf(9, "output_static('%s')  -- MALLOC FAILED (%s) --\n", what, strerror(errno));
-                       wprintf("HTTP/1.1 500 internal server error\r\n");
-                       wprintf("Content-Type: text/plain\r\n");
-                       wprintf("\r\n");
-                       return;
-               }
-               while (count < bytes) {
-                       if ((res = fread(bigbuffer + count, 1, bytes - count, fp)) == 0) {
-                               lprintf(9, "output_static('%s')  -- FREAD FAILED (%s) %zu bytes of %zu --\n", what, strerror(errno), bytes - count, bytes);
-                               wprintf("HTTP/1.1 500 internal server error \r\n");
-                               wprintf("Content-Type: text/plain\r\n");
-                               wprintf("\r\n");
+
+               if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
+               {
+                       if (fd > 0) close(fd);
+                       lprintf(9, "output_static('%s')  -- FREAD FAILED (%s) --\n", what, strerror(errno));
+                               hprintf("HTTP/1.1 500 internal server error \r\n");
+                               hprintf("Content-Type: text/plain\r\n");
+                               end_burst();
                                return;
-                       }
-                       count += res;
                }
 
-               fclose(fp);
 
+               close(fd);
+#ifndef TECH_PREVIEW
                lprintf(9, "output_static('%s')  %s\n", what, content_type);
-               http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
-               free(bigbuffer);
+#endif
+               http_transmit_thing(content_type, 1);
        }
        if (yesbstr("force_close_session")) {
                end_webcit_session();
@@ -892,35 +878,31 @@ void output_static(char *what)
  */
 void output_image()
 {
+       struct wcsession *WCC = WC;
        char buf[SIZ];
-       char *xferbuf = NULL;
        off_t bytes;
        const char *MimeType;
-
+       
        serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
        serv_getln(buf, sizeof buf);
        if (buf[0] == '2') {
                bytes = extract_long(&buf[4], 0);
-               xferbuf = malloc(bytes + 2);
 
                /** Read it from the server */
-               read_server_binary(xferbuf, bytes);
-               serv_puts("CLOS");
-               serv_getln(buf, sizeof buf);
-
-               MimeType = GuessMimeType (xferbuf, bytes);
-               /** Write it to the browser */
-               if (!IsEmptyStr(MimeType))
-               {
-                       http_transmit_thing(xferbuf, 
-                                           (size_t)bytes, 
-                                           MimeType, 
-                                           0);
-                       free(xferbuf);
-                       return;
+               
+               if (read_server_binary(WCC->WBuf, bytes) > 0) {
+                       serv_puts("CLOS");
+                       serv_getln(buf, sizeof buf);
+               
+                       MimeType = GuessMimeType (ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
+                       /** Write it to the browser */
+                       if (!IsEmptyStr(MimeType))
+                       {
+                               http_transmit_thing(MimeType, 0);
+                               return;
+                       }
                }
                /* hm... unknown mimetype? fallback to blank gif */
-               free(xferbuf);
        } 
 
        
@@ -941,34 +923,71 @@ void display_vcard_photo_img(void)
        long msgnum = 0L;
        char *vcard;
        struct vCard *v;
-       char *xferbuf;
-    char *photosrc;
-       int decoded;
+       char *photosrc;
        const char *contentType;
+       struct wcsession *WCC = WC;
 
-       msgnum = StrTol(WC->UrlFragment1);
+       msgnum = StrTol(WCC->UrlFragment1);
        
        vcard = load_mimepart(msgnum,"1");
        v = vcard_load(vcard);
        
        photosrc = vcard_get_prop(v, "PHOTO", 1,0,0);
-       xferbuf = malloc(strlen(photosrc));
-       if (xferbuf == NULL) {
-               lprintf(5, "xferbuf malloc failed\n");
+       FlushStrBuf(WCC->WBuf);
+       StrBufAppendBufPlain(WCC->WBuf, photosrc, -1, 0);
+       if (StrBufDecodeBase64(WCC->WBuf) <= 0) {
+               FlushStrBuf(WCC->WBuf);
+               
+               hprintf("HTTP/1.1 500 %s\n","Unable to get photo");
+               output_headers(0, 0, 0, 0, 0, 0);
+               hprintf("Content-Type: text/plain\r\n");
+               wprintf(_("Could Not decode vcard photo\n"));
+               end_burst();
                return;
        }
-       memset(xferbuf, 1, SIZ);
-       decoded = CtdlDecodeBase64(
-               xferbuf,
-               photosrc,
-               strlen(photosrc));
-       contentType = GuessMimeType(xferbuf, decoded);
-       http_transmit_thing(xferbuf, decoded, contentType, 0);
+       contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
+       http_transmit_thing(contentType, 0);
        free(v);
        free(photosrc);
-       free(xferbuf);
 }
 
+/*
+ * Generic function to output an arbitrary MIME attachment from
+ * message being composed
+ *
+ * partnum             The MIME part to be output
+ * filename            Fake filename to give
+ * force_download      Nonzero to force set the Content-Type: header to "application/octet-stream"
+ */
+void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
+{
+       void *vPart;
+       char content_type[256];
+       wc_attachment *part;
+       
+       if (GetHash(WC->attachments, SKEY(partnum), &vPart) &&
+           (vPart != NULL)) {
+               part = (wc_attachment*) vPart;
+               if (force_download) {
+                       strcpy(content_type, "application/octet-stream");
+               }
+               else {
+                       strncpy(content_type, ChrPtr(part->content_type), sizeof content_type);
+               }
+               output_headers(0, 0, 0, 0, 0, 0);
+               StrBufAppendBufPlain(WC->WBuf, part->data, part->length, 0);
+               http_transmit_thing(content_type, 0);
+       } else {
+               hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
+               output_headers(0, 0, 0, 0, 0, 0);
+               hprintf("Content-Type: text/plain\r\n");
+               wprintf(_("An error occurred while retrieving this part: %s/%s\n"), 
+                       ChrPtr(partnum), ChrPtr(filename));
+               end_burst();
+       }
+}
+
+
 /*
  * Generic function to output an arbitrary MIME part from an arbitrary
  * message number on the server.
@@ -982,13 +1001,11 @@ void mimepart(const char *msgnum, const char *partnum, int force_download)
        char buf[256];
        off_t bytes;
        char content_type[256];
-       char *content = NULL;
        
        serv_printf("OPNA %s|%s", msgnum, partnum);
        serv_getln(buf, sizeof buf);
        if (buf[0] == '2') {
                bytes = extract_long(&buf[4], 0);
-               content = malloc(bytes + 2);
                if (force_download) {
                        strcpy(content_type, "application/octet-stream");
                }
@@ -996,19 +1013,18 @@ void mimepart(const char *msgnum, const char *partnum, int force_download)
                        extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
                }
                output_headers(0, 0, 0, 0, 0, 0);
-               read_server_binary(content, bytes);
+
+               read_server_binary(WC->WBuf, bytes);
                serv_puts("CLOS");
                serv_getln(buf, sizeof buf);
-               http_transmit_thing(content, bytes, content_type, 0);
-               free(content);
+               http_transmit_thing(content_type, 0);
        } else {
-               wprintf("HTTP/1.1 404 %s\n", &buf[4]);
+               hprintf("HTTP/1.1 404 %s\n", &buf[4]);
                output_headers(0, 0, 0, 0, 0, 0);
-               wprintf("Content-Type: text/plain\r\n");
-               wprintf("\r\n");
+               hprintf("Content-Type: text/plain\r\n");
                wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
+               end_burst();
        }
-
 }
 
 
@@ -1037,7 +1053,30 @@ char *load_mimepart(long msgnum, char *partnum)
        else {
                return(NULL);
        }
+}
+
+/*
+ * Read any MIME part of a message, from the server, into memory.
+ */
+void MimeLoadData(wc_mime_attachment *Mime)
+{
+       char buf[SIZ];
+       off_t bytes;
+//// TODO: is there a chance the contenttype is different  to the one we know? 
+       serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '6') {
+               bytes = extract_long(&buf[4], 0);
 
+               if (Mime->Data == NULL)
+                       Mime->Data = NewStrBufPlain(NULL, bytes);
+               StrBuf_ServGetBLOB(Mime->Data, bytes);
+
+       }
+       else {
+               FlushStrBuf(Mime->Data);
+               /// TODO XImportant message
+       }
 }
 
 
@@ -1050,7 +1089,7 @@ char *load_mimepart(long msgnum, char *partnum)
  */
 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
 {
-       wprintf("HTTP/1.1 200 OK\n");
+       hprintf("HTTP/1.1 200 OK\n");
        output_headers(1, 1, 2, 0, 0, 0);
        wprintf("<div id=\"banner\">\n");
        wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
@@ -1077,7 +1116,11 @@ void blank_page(void) {
  * A template has been requested
  */
 void url_do_template(void) {
-       do_template(bstr("template"));
+       const StrBuf *Tmpl = sbstr("template");
+       begin_burst();
+       output_headers(1, 0, 0, 0, 1, 0);
+       DoTemplate(ChrPtr(Tmpl), StrLength(Tmpl), NULL, NULL, 0);
+       end_burst();
 }
 
 
@@ -1085,7 +1128,7 @@ void url_do_template(void) {
 /*
  * Offer to make any page the user's "start page."
  */
-void offer_start_page(void) {
+void offer_start_page(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType) {
        wprintf("<a href=\"change_start_page?startpage=");
        urlescputs(WC->this_page);
        wprintf("\">");
@@ -1117,7 +1160,7 @@ void change_start_page(void) {
        set_preference("startpage", NewStrBufPlain(bstr("startpage"), -1), 1);
 
        output_headers(1, 1, 0, 0, 0, 0);
-       do_template("newstartpage");
+       do_template("newstartpage", NULL);
        wDumpContent(1);
 }
 
@@ -1138,15 +1181,16 @@ void display_success(char *successmessage)
  */
 void authorization_required(const char *message)
 {
-       wprintf("HTTP/1.1 401 Authorization Required\r\n");
-       wprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n", serv_info.serv_humannode);
-       wprintf("Content-Type: text/html\r\n\r\n");
+       hprintf("HTTP/1.1 401 Authorization Required\r\n");
+       hprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n", serv_info.serv_humannode);
+       hprintf("Content-Type: text/html\r\n");
        wprintf("<h1>");
        wprintf(_("Authorization Required"));
        wprintf("</h1>\r\n");
        wprintf(_("The resource you requested requires a valid username and password. "
                "You could not be logged in: %s\n"), message);
        wDumpContent(0);
+       
 }
 
 /*
@@ -1163,11 +1207,12 @@ void authorization_required(const char *message)
  * cbcharset   Character set
  * length      Content length
  * encoding    MIME encoding type (not needed)
+ * cbid                Content ID (not needed)
  * userdata    Not used here
  */
 void upload_handler(char *name, char *filename, char *partnum, char *disp,
                        void *content, char *cbtype, char *cbcharset,
-                       size_t length, char *encoding, void *userdata)
+                       size_t length, char *encoding, char *cbid, void *userdata)
 {
        urlcontent *u;
 #ifdef DEBUG_URLSTRINGS
@@ -1181,13 +1226,14 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
                u = (urlcontent *) malloc(sizeof(urlcontent));
                
                safestrncpy(u->url_key, name, sizeof(u->url_key));
-               u->url_data = malloc(length + 1);
-               u->url_data_size = length;
-               memcpy(u->url_data, content, length);
-               u->url_data[length] = 0;
+               u->url_data = NewStrBufPlain(content, length);
+               
                Put(WC->urlstrings, u->url_key, strlen(u->url_key), u, free_url);
 #ifdef DEBUG_URLSTRINGS
-               lprintf(9, "Key: <%s> len: [%ld] Data: <%s>\n", u->url_key, u->url_data_size, u->url_data);
+               lprintf(9, "Key: <%s> len: [%ld] Data: <%s>\n", 
+                       u->url_key, 
+                       StrLength(u->url_data), 
+                       ChrPtr(u->url_data));
 #endif
        }
 
@@ -1213,14 +1259,14 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
  * Convenience functions to wrap around asynchronous ajax responses
  */
 void begin_ajax_response(void) {
+       struct wcsession *WCC = WC;
+
+       FlushStrBuf(WCC->HBuf);
         output_headers(0, 0, 0, 0, 0, 0);
 
-        wprintf("Content-type: text/html; charset=UTF-8\r\n"
+        hprintf("Content-type: text/html; charset=UTF-8\r\n"
                 "Server: %s\r\n"
                 "Connection: close\r\n"
-                "Pragma: no-cache\r\n"
-                "Cache-Control: no-cache\r\n"
-               "Expires: -1\r\n"
                ,
                 PACKAGE_STRING);
         begin_burst();
@@ -1230,7 +1276,6 @@ void begin_ajax_response(void) {
  * print ajax response footer 
  */
 void end_ajax_response(void) {
-        wprintf("\r\n");
         wDumpContent(0);
 }
 
@@ -1299,11 +1344,11 @@ void seconds_since_last_gexp(void)
 {
        char buf[256];
 
-       begin_ajax_response();
        if ( (time(NULL) - WC->last_pager_check) < 30) {
                wprintf("NO\n");
        }
        else {
+               memset(buf, 5, 0);
                serv_puts("NOOP");
                serv_getln(buf, sizeof buf);
                if (buf[3] == '*') {
@@ -1313,43 +1358,46 @@ void seconds_since_last_gexp(void)
                        wprintf("NO");
                }
        }
-       end_ajax_response();
 }
 
 /**
  * \brief Detects a 'mobile' user agent 
  */
 int is_mobile_ua(char *user_agent) {
-       if (strstr(user_agent,"iPhone OS") != NULL) {
-               return 1;
-       } else if (strstr(user_agent,"Windows CE") != NULL) {
-               return 1;
-       } else if (strstr(user_agent,"SymbianOS") != NULL) {
-               return 1;
-       }
-       return 0;
+      if (strstr(user_agent,"iPhone OS") != NULL) {
+       return 1;
+      } else if (strstr(user_agent,"Windows CE") != NULL) {
+       return 1;
+      } else if (strstr(user_agent,"SymbianOS") != NULL) {
+       return 1;
+      } else if (strstr(user_agent, "Opera Mobi") != NULL) {
+       return 1;
+      } else if (strstr(user_agent, "Firefox/2.0.0 Opera 9.51 Beta") != NULL) {
+                // For some reason a new install of Opera 9.51beta decided to spoof.
+         return 1;
+         }
+      return 0;
 }
 
 
 /*
  * Entry point for WebCit transaction
  */
-void session_loop(struct httprequest *req)
+void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method, StrBuf *ReadBuf)
 {
-       char cmd[1024];
+       const char *pch, *pchs, *pche;
+       void *vLine;
        char action[1024];
        char arg[8][128];
        size_t sizes[10];
        char *index[10];
        char buf[SIZ];
-       char request_method[128];
-       char pathname[1024];
-       int a, b, nBackDots, nEmpty;
+       int a, nBackDots, nEmpty;
        int ContentLength = 0;
-       char ContentType[512];
-       char *content = NULL;
-       char *content_end = NULL;
-       struct httprequest *hptr;
+       StrBuf *ContentType = NULL;
+       StrBuf *UrlLine = NULL;
+       StrBuf *content = NULL;
+       const char *content_end = NULL;
        char browser_host[256];
        char user_agent[256];
        int body_start = 0;
@@ -1366,9 +1414,8 @@ void session_loop(struct httprequest *req)
        char c_httpauth_string[SIZ];
        char c_httpauth_user[SIZ];
        char c_httpauth_pass[SIZ];
-       char cookie[SIZ];
-       struct wcsession *WCC = WC;
-
+       struct wcsession *WCC;
+       
        safestrncpy(c_username, "", sizeof c_username);
        safestrncpy(c_password, "", sizeof c_password);
        safestrncpy(c_roomname, "", sizeof c_roomname);
@@ -1377,17 +1424,19 @@ void session_loop(struct httprequest *req)
        safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
        strcpy(browser_host, "");
 
+       WCC= WC;
+       if (WCC->WBuf == NULL)
+               WC->WBuf = NewStrBufPlain(NULL, 32768);
+       FlushStrBuf(WCC->WBuf);
+
+       if (WCC->HBuf == NULL)
+               WCC->HBuf = NewStrBuf();
+       FlushStrBuf(WCC->HBuf);
+
        WCC->upload_length = 0;
        WCC->upload = NULL;
        WCC->is_mobile = 0;
-
-       hptr = req;
-       if (hptr == NULL) return;
-
-       safestrncpy(cmd, hptr->line, sizeof cmd);
-       hptr = hptr->next;
-       extract_token(request_method, cmd, 0, ' ', sizeof request_method);
-       extract_token(pathname, cmd, 1, ' ', sizeof pathname);
+       WCC->trailing_javascript = NewStrBuf();
 
        /** Figure out the action */
        index[0] = action;
@@ -1402,7 +1451,7 @@ void session_loop(struct httprequest *req)
        nEmpty = 0;
        for ( a = 0; a < 9; ++a)
        {
-               extract_token(index[a], pathname, a + 1, '/', sizes[a]);
+               extract_token(index[a], ChrPtr(ReqLine), a + 1, '/', sizes[a]);
                if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
                if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
                if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
@@ -1412,111 +1461,124 @@ void session_loop(struct httprequest *req)
                        nEmpty++;
        }
 
-       while (hptr != NULL) {
-               safestrncpy(buf, hptr->line, sizeof buf);
-               /* lprintf(9, "HTTP HEADER: %s\n", buf); */
-               hptr = hptr->next;
 
-               if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
-                       safestrncpy(cookie, &buf[15], sizeof cookie);
-                       cookie_to_stuff(cookie, NULL,
-                                       c_username, sizeof c_username,
-                                       c_password, sizeof c_password,
-                                       c_roomname, sizeof c_roomname);
-               }
-               else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
-                       CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
-                       extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
-                       extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
-               }
-               else if (!strncasecmp(buf, "Content-length: ", 16)) {
-                       ContentLength = atoi(&buf[16]);
-               }
-               else if (!strncasecmp(buf, "Content-type: ", 14)) {
-                       safestrncpy(ContentType, &buf[14], sizeof ContentType);
-               }
-               else if (!strncasecmp(buf, "User-agent: ", 12)) {
-                       safestrncpy(user_agent, &buf[12], sizeof user_agent);
-                       if (is_mobile_ua(&buf[12])) {
-                               WCC->is_mobile = 1;
-                       }
-               }
-               else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
-                       if (follow_xff) {
-                               safestrncpy(WCC->http_host, &buf[18], sizeof WCC->http_host);
-                       }
+       if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) && 
+           (vLine != NULL)){
+               cookie_to_stuff((StrBuf *)vLine, NULL,
+                               c_username, sizeof c_username,
+                               c_password, sizeof c_password,
+                               c_roomname, sizeof c_roomname);
+       }
+       if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) &&
+           (vLine!=NULL)) {
+               CtdlDecodeBase64(c_httpauth_string, ChrPtr((StrBuf*)vLine), StrLength((StrBuf*)vLine));
+               extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
+               extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
+       }
+       if (GetHash(HTTPHeaders, HKEY("CONTENT-LENGTH"), &vLine) &&
+           (vLine!=NULL)) {
+               ContentLength = StrToi((StrBuf*)vLine);
+       }
+       if (GetHash(HTTPHeaders, HKEY("CONTENT-TYPE"), &vLine) &&
+           (vLine!=NULL)) {
+               ContentType = (StrBuf*)vLine;
+       }
+       if (GetHash(HTTPHeaders, HKEY("USER-AGENT"), &vLine) &&
+           (vLine!=NULL)) {
+               safestrncpy(user_agent, ChrPtr((StrBuf*)vLine), sizeof user_agent);
+#ifdef TECH_PREVIEW
+               if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {                   
+                       WCC->is_mobile = 1;
                }
-               else if (!strncasecmp(buf, "Host: ", 6)) {
-                       if (IsEmptyStr(WCC->http_host)) {
-                               safestrncpy(WCC->http_host, &buf[6], sizeof WCC->http_host);
-                       }
+               else {
+                       WCC->is_mobile = 0;
                }
-               else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
-                       safestrncpy(browser_host, &buf[17], sizeof browser_host);
-                       while (num_tokens(browser_host, ',') > 1) {
-                               remove_token(browser_host, 0, ',');
-                       }
-                       striplt(browser_host);
+#endif
+       }
+       if ((follow_xff) &&
+           GetHash(HTTPHeaders, HKEY("X-FORWARDED-HOST"), &vLine) &&
+           (vLine != NULL)) {
+               safestrncpy(WCC->http_host, 
+                           ChrPtr((StrBuf*)vLine), 
+                           sizeof WCC->http_host);
+       }
+       if (IsEmptyStr(WCC->http_host) && 
+           GetHash(HTTPHeaders, HKEY("HOST"), &vLine) &&
+           (vLine!=NULL)) {
+               safestrncpy(WCC->http_host, 
+                           ChrPtr((StrBuf*)vLine), 
+                           sizeof WCC->http_host);
+               
+       }
+       if (GetHash(HTTPHeaders, HKEY("X-FORWARDED-FOR"), &vLine) &&
+           (vLine!=NULL)) {
+               safestrncpy(browser_host, 
+                           ChrPtr((StrBuf*) vLine), 
+                           sizeof browser_host);
+               while (num_tokens(browser_host, ',') > 1) {
+                       remove_token(browser_host, 0, ',');
                }
+               striplt(browser_host);
        }
 
        if (ContentLength > 0) {
-               int BuffSize;
-
-               BuffSize = ContentLength + SIZ;
-               content = malloc(BuffSize);
-               memset(content, 0, BuffSize);
-               snprintf(content,  BuffSize, "Content-type: %s\n"
-                               "Content-length: %d\n\n",
-                               ContentType, ContentLength);
-               body_start = strlen(content);
+               content = NewStrBuf();
+               StrBufPrintf(content, "Content-type: %s\n"
+                        "Content-length: %d\n\n",
+                        ChrPtr(ContentType), ContentLength);
+/*
+               hprintf("Content-type: %s\n"
+                       "Content-length: %d\n\n",
+                       ContentType, ContentLength);
+*/
+               body_start = StrLength(content);
 
                /** Read the entire input data at once. */
-               client_read(WCC->http_sock, &content[body_start], ContentLength);
-
-               if (!strncasecmp(ContentType, "application/x-www-form-urlencoded", 33)) {
-                       addurls(&content[body_start], ContentLength);
-               } else if (!strncasecmp(ContentType, "multipart", 9)) {
-                       content_end = content + ContentLength + body_start;
-                       mime_parser(content, content_end, *upload_handler, NULL, NULL, NULL, 0);
+               client_read(&WCC->http_sock, content, ReadBuf, ContentLength + body_start);
+
+               if (!strncasecmp(ChrPtr(ContentType), "application/x-www-form-urlencoded", 33)) {
+                       StrBufCutLeft(content, body_start);
+                       ParseURLParams(content);
+               } else if (!strncasecmp(ChrPtr(ContentType), "multipart", 9)) {
+                       content_end = ChrPtr(content) + ContentLength + body_start;
+                       mime_parser(ChrPtr(content), content_end, *upload_handler, NULL, NULL, NULL, 0);
                }
        } else {
                content = NULL;
        }
 
        /* make a note of where we are in case the user wants to save it */
-       safestrncpy(WCC->this_page, cmd, sizeof(WCC->this_page));
+       safestrncpy(WCC->this_page, ChrPtr(ReqLine), sizeof(WCC->this_page));
        remove_token(WCC->this_page, 2, ' ');
        remove_token(WCC->this_page, 0, ' ');
 
        /* If there are variables in the URL, we must grab them now */
-       len = strlen(cmd);
-       for (a = 0; a < len; ++a) {
-               if ((cmd[a] == '?') || (cmd[a] == '&')) {
-                       for (b = a; b < len; ++b) {
-                               if (isspace(cmd[b])){
-                                       cmd[b] = 0;
-                                       len = b - 1;
-                               }
-                       }
-                       addurls(&cmd[a + 1], len - a);
-                       cmd[a] = 0;
-                       len = a - 1;
+       UrlLine = NewStrBufDup(ReqLine);
+       len = StrLength(UrlLine);
+       pch = pchs = ChrPtr(UrlLine);
+       pche = pchs + len;
+       while (pch < pche) {
+               if ((*pch == '?') || (*pch == '&')) {
+                       StrBufCutLeft(UrlLine, pch - pchs + 1);
+                       ParseURLParams(UrlLine);
+                       break;
                }
+               pch ++;
        }
+       FreeStrBuf(&UrlLine);
 
        /* If it's a "force 404" situation then display the error and bail. */
        if (!strcmp(action, "404")) {
-               wprintf("HTTP/1.1 404 Not found\r\n");
-               wprintf("Content-Type: text/plain\r\n");
-               wprintf("\r\n");
+               hprintf("HTTP/1.1 404 Not found\r\n");
+               hprintf("Content-Type: text/plain\r\n");
                wprintf("Not found\r\n");
+               end_burst();
                goto SKIP_ALL_THIS_CRAP;
        }
 
        /* Static content can be sent without connecting to Citadel. */
        is_static = 0;
-       for (a=0; a<ndirs; ++a) {
+       for (a=0; a<ndirs && ! is_static; ++a) {
                if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
                        is_static = 1;
                        n_static = a;
@@ -1543,10 +1605,10 @@ void session_loop(struct httprequest *req)
                else 
                {
                        lprintf(9, "Suspicious request. Ignoring.");
-                       wprintf("HTTP/1.1 404 Security check failed\r\n");
-                       wprintf("Content-Type: text/plain\r\n");
-                       wprintf("\r\n");
+                       hprintf("HTTP/1.1 404 Security check failed\r\n");
+                       hprintf("Content-Type: text/plain\r\n");
                        wprintf("You have sent a malformed or invalid request.\r\n");
+                       end_burst();
                }
                goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
        }
@@ -1557,10 +1619,10 @@ void session_loop(struct httprequest *req)
                        bstr("nonce"), WCC->nonce);
                if (ibstr("nonce") != WCC->nonce) {
                        lprintf(9, "Ignoring request with mismatched nonce.\n");
-                       wprintf("HTTP/1.1 404 Security check failed\r\n");
-                       wprintf("Content-Type: text/plain\r\n");
-                       wprintf("\r\n");
+                       hprintf("HTTP/1.1 404 Security check failed\r\n");
+                       hprintf("Content-Type: text/plain\r\n");
                        wprintf("Security check failed.\r\n");
+                       end_burst();
                        goto SKIP_ALL_THIS_CRAP;
                }
        }
@@ -1600,6 +1662,7 @@ void session_loop(struct httprequest *req)
 
                        get_serv_info(browser_host, user_agent);
                        if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
+                               begin_burst();
                                wprintf(_("You are connected to a Citadel "
                                        "server running Citadel %d.%02d. \n"
                                        "In order to run this version of WebCit "
@@ -1610,12 +1673,13 @@ void session_loop(struct httprequest *req)
                                                MINIMUM_CIT_VERSION / 100,
                                                MINIMUM_CIT_VERSION % 100
                                        );
+                               end_burst();
                                end_webcit_session();
                                goto SKIP_ALL_THIS_CRAP;
                        }
                }
        }
-
+////////todo: restore language in this case
        /*
         * Functions which can be performed without logging in
         */
@@ -1624,7 +1688,7 @@ void session_loop(struct httprequest *req)
                goto SKIP_ALL_THIS_CRAP;
        }
        if (!strcasecmp(action, "freebusy")) {
-               do_freebusy(cmd);
+               do_freebusy(ChrPtr(ReqLine));
                goto SKIP_ALL_THIS_CRAP;
        }
 
@@ -1666,8 +1730,10 @@ void session_loop(struct httprequest *req)
         * our session's authentication.
         */
        if (!strncasecmp(action, "groupdav", 8)) {
-               groupdav_main(req, ContentType, /* do GroupDAV methods */
-                       ContentLength, content+body_start);
+               groupdav_main(HTTPHeaders, 
+                             ReqLine, request_method,
+                             ContentType, /* do GroupDAV methods */
+                             ContentLength, content, body_start);
                if (!WCC->logged_in) {
                        WCC->killthis = 1;      /* If not logged in, don't */
                }                               /* keep the session active */
@@ -1679,9 +1745,10 @@ void session_loop(struct httprequest *req)
         * Automatically send requests with any method other than GET or
         * POST to the GroupDAV code as well.
         */
-       if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
-               groupdav_main(req, ContentType, /** do GroupDAV methods */
-                       ContentLength, content+body_start);
+       if ((strcasecmp(ChrPtr(request_method), "GET")) && (strcasecmp(ChrPtr(request_method), "POST"))) {
+               groupdav_main(HTTPHeaders, ReqLine, 
+                             request_method, ContentType, /** do GroupDAV methods */
+                             ContentLength, content, body_start);
                if (!WCC->logged_in) {
                        WCC->killthis = 1;      /** If not logged in, don't */
                }                               /** keep the session active */
@@ -1701,7 +1768,13 @@ void session_loop(struct httprequest *req)
                        serv_printf("PASS %s", c_password);
                        serv_getln(buf, sizeof buf);
                        if (buf[0] == '2') {
+                               StrBuf *Lang;
                                become_logged_in(c_username, c_password, buf);
+                               if (get_preference("language", &Lang)) {
+                                       set_selected_language(ChrPtr(Lang));
+                                       go_selected_language();         /* set locale */
+                               }
+                               get_preference("default_header_charset", &WCC->DefaultCharset);
                        }
                }
        }
@@ -1759,7 +1832,7 @@ void session_loop(struct httprequest *req)
 SKIP_ALL_THIS_CRAP:
        fflush(stdout);
        if (content != NULL) {
-               free(content);
+               FreeStrBuf(&content);
                content = NULL;
        }
        free_urls();
@@ -1767,6 +1840,7 @@ SKIP_ALL_THIS_CRAP:
                free(WCC->upload);
                WCC->upload_length = 0;
        }
+       FreeStrBuf(&WCC->trailing_javascript);
 }
 
 
@@ -1787,9 +1861,9 @@ void diagnostics(void)
        output_headers(1, 1, 1, 0, 0, 0);
        wprintf("Session: %d<hr />\n", WC->wc_session);
        wprintf("Command: <br /><PRE>\n");
-       escputs(WC->UrlFragment1);
+       StrEscPuts(WC->UrlFragment1);
        wprintf("<br />\n");
-       escputs(WC->UrlFragment2);
+       StrEscPuts(WC->UrlFragment2);
        wprintf("</PRE><hr />\n");
        wprintf("Variables: <br /><PRE>\n");
        dump_vars();
@@ -1809,6 +1883,77 @@ void download_mimepart(void) {
                 1);
 }
 
+void view_postpart(void) {
+       postpart(WC->UrlFragment1,
+                WC->UrlFragment2,
+                0);
+}
+
+void download_postpart(void) {
+       postpart(WC->UrlFragment1,
+                WC->UrlFragment2,
+                1);
+}
+
+
+int ConditionalImportantMesage(WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       struct wcsession *WCC = WC;
+       if (WCC != NULL)
+               return (!IsEmptyStr(WCC->ImportantMessage));
+       else
+               return 0;
+}
+
+void tmplput_importantmessage(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       struct wcsession *WCC = WC;
+       
+       if (WCC != NULL) {
+/*
+               StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType,
+                                    WCC->ImportantMessage, 0);
+*/
+               WCC->ImportantMessage[0] = '\0';
+               StrEscAppend(Target, NULL, WCC->ImportantMessage, 0, 0);
+                       WCC->ImportantMessage[0] = '\0';
+       }
+}
+
+int ConditionalBstr(WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       if(Tokens->nParameters == 3)
+               return HaveBstr(Tokens->Params[2]->Start, 
+                               Tokens->Params[2]->len);
+       else
+               return strcmp(Bstr(Tokens->Params[2]->Start, 
+                                  Tokens->Params[2]->len),
+                             Tokens->Params[3]->Start) == 0;
+}
+
+void tmplput_bstr(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       const StrBuf *Buf = SBstr(Tokens->Params[0]->Start, 
+                           Tokens->Params[0]->len);
+       if (Buf != NULL)
+               StrBufAppendTemplate(Target, nArgs, Tokens, 
+                                    Context, ContextType,
+                                    Buf, 1);
+}
+
+
+void tmplput_csslocal(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       extern StrBuf *csslocal;
+       StrBufAppendBuf(Target, 
+                       csslocal, 0);
+}
+
+
+
+
+
+
 void 
 InitModule_WEBCIT
 (void)
@@ -1822,5 +1967,15 @@ InitModule_WEBCIT
        WebcitAddUrlHandler(HKEY("vcardphoto"), display_vcard_photo_img, NEED_URL);
        WebcitAddUrlHandler(HKEY("mimepart"), view_mimepart, NEED_URL);
        WebcitAddUrlHandler(HKEY("mimepart_download"), download_mimepart, NEED_URL);
+       WebcitAddUrlHandler(HKEY("postpart"), view_postpart, NEED_URL);
+       WebcitAddUrlHandler(HKEY("postpart_download"), download_postpart, NEED_URL);
        WebcitAddUrlHandler(HKEY("diagnostics"), diagnostics, NEED_URL);
+
+       RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage, CTX_NONE);
+       RegisterConditional(HKEY("COND:BSTR"), 1, ConditionalBstr, CTX_NONE);
+       RegisterNamespace("BSTR", 1, 2, tmplput_bstr, CTX_NONE);
+       RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, CTX_NONE);
+       RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage, CTX_NONE);
+       RegisterNamespace("OFFERSTARTPAGE", 0, 0, offer_start_page, CTX_NONE);
+       RegisterNamespace("TRAILING_JAVASCRIPT", 0, 0, tmplput_trailing_javascript, CTX_NONE);
 }