]> code.citadel.org Git - citadel.git/blobdiff - webcit/webcit.c
* add new 'Context' Parameter to the template call
[citadel.git] / webcit / webcit.c
index 27edb1a3d3aeef1d12fdfb511c7182cb5930f50d..723bc0db9113acb70e403c13f887a5d24d656a7a 100644 (file)
@@ -88,64 +88,71 @@ 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 addurls(StrBuf *url)
 {
-       char *aptr, *bptr, *eptr;
-       char *up;
-       char *buf;
+       const char *aptr, *bptr, *eptr, *up;
+///    char *buf;
        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;
+//     buf = (char*) malloc (ulen + 1);
+//     memcpy(buf, url, ulen);
+///    buf[ulen] = '\0';
+       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 != '=') {
+                       ///free(buf);
                        return;
-               *aptr = '\0';
+               }
+               ///*aptr = '\0';
                aptr++;
                bptr = aptr;
                while ((bptr < eptr) && (*bptr != '\0')
                      && (*bptr != '&') && (*bptr != '?') && (*bptr != ' ')) {
                        bptr++;
                }
-               *bptr = '\0';
-               u = (urlcontent *) malloc(sizeof(urlcontent));
+               //*bptr = '\0';
+               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, u->url_data_size, ChrPtr(u->url_data)); 
 #endif
        }
-       free(buf);
+       //free(buf);
 }
 
 /*
@@ -172,7 +179,7 @@ void dump_vars(void)
        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));
        }
 }
 
@@ -186,8 +193,8 @@ const char *XBstr(char *key, size_t keylen, size_t *len)
 
        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;
@@ -201,8 +208,8 @@ const char *XBSTR(char *key, size_t *len)
 
        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;
@@ -217,7 +224,7 @@ const char *BSTR(char *key)
 
        if ((WC->urlstrings != NULL) &&
            GetHash(WC->urlstrings, key, strlen (key), &U))
-               return ((urlcontent *)U)->url_data;
+               return ChrPtr(((urlcontent *)U)->url_data);
        else    
                return ("");
 }
@@ -228,18 +235,40 @@ const char *Bstr(char *key, size_t keylen)
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, keylen, &U))
-               return ((urlcontent *)U)->url_data;
+               return ChrPtr(((urlcontent *)U)->url_data);
        else    
                return ("");
 }
 
+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 ((urlcontent *)U)->url_data;
+       else    
+               return NULL;
+}
+
 long LBstr(char *key, size_t keylen)
 {
        void *U;
 
        if ((WC->urlstrings != NULL) && 
            GetHash(WC->urlstrings, key, keylen, &U))
-               return atol(((urlcontent *)U)->url_data);
+               return StrTol(((urlcontent *)U)->url_data);
        else    
                return (0);
 }
@@ -250,7 +279,7 @@ long LBSTR(char *key)
 
        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);
 }
@@ -261,7 +290,7 @@ int IBstr(char *key, size_t keylen)
 
        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);
 }
@@ -272,7 +301,7 @@ int IBSTR(char *key)
 
        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);
 }
@@ -283,7 +312,7 @@ int HaveBstr(char *key, size_t keylen)
 
        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);
 }
@@ -294,7 +323,7 @@ int HAVEBSTR(char *key)
 
        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);
 }
@@ -306,7 +335,7 @@ int YesBstr(char *key, size_t keylen)
 
        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);
 }
@@ -317,7 +346,7 @@ int YESBSTR(char *key)
 
        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);
 }
@@ -370,7 +399,7 @@ void wDumpContent(int print_standard_html_footer)
 {
        if (print_standard_html_footer) {
                wprintf("</div>\n");    /* end of "text" div */
-               do_template("trailing");
+               do_template("trailing", NULL);
        }
 
        /* If we've been saving it all up for one big output burst,
@@ -484,6 +513,15 @@ void urlescputs(char *strbuf)
        urlesc(outbuf, SIZ, strbuf);
        wprintf("%s", outbuf);
 }
+/**
+ * urlescape buffer and print it as header 
+ */
+void hurlescputs(char *strbuf) {
+       char outbuf[SIZ];
+       
+       urlesc(outbuf, SIZ, strbuf);
+       hprintf("%s", outbuf);
+}
 
 
 /*
@@ -704,7 +742,7 @@ void output_headers(        int do_httpheaders,     /* 1 = output HTTP headers
                           "<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"
                        );
                }
-               do_template("head");
+               do_template("head", NULL);
        }
 
        /* ICONBAR */
@@ -764,7 +802,7 @@ 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(StrBuf *thing, const char *content_type,
+void http_transmit_thing(const char *content_type,
                         int is_static) {
 
        output_headers(0, 0, 0, 0, 0, is_static);
@@ -775,9 +813,7 @@ void http_transmit_thing(StrBuf *thing, const char *content_type,
                content_type,
                PACKAGE_STRING);
 
-       WC->WBuf = thing;
        end_burst();
-       WC->WBuf = NULL;
 }
 
 /*
@@ -794,7 +830,7 @@ void print_menu_box(char* Title, char *Class, int nLines, ...)
        long i;
        
        svput("BOXTITLE", WCS_STRING, Title);
-       do_template("beginbox");
+       do_template("beginbox", NULL);
        
        wprintf("<ul class=\"%s\">", Class);
        
@@ -811,7 +847,7 @@ void print_menu_box(char* Title, char *Class, int nLines, ...)
        
        wprintf("</ul>");
        
-       do_template("endbox");
+       do_template("endbox", NULL);
 }
 
 
@@ -824,7 +860,6 @@ void output_static(char *what)
        struct stat statbuf;
        off_t bytes;
        off_t count = 0;
-       StrBuf *Buf;
        const char *content_type;
        int len;
        const char *Err;
@@ -851,17 +886,10 @@ void output_static(char *what)
 
                count = 0;
                bytes = statbuf.st_size;
-               Buf = NewStrBufPlain(NULL, bytes + 2);
-               if (Buf == NULL) {
-                       lprintf(9, "output_static('%s')  -- MALLOC 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;
-               }
-///            StrBufAppendBuf(Buf, WC->WBuf, 0);
-               if (StrBufReadBLOB(Buf, &fd, 1, bytes, &Err) < 0)
+
+               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");
@@ -872,8 +900,7 @@ void output_static(char *what)
 
                close(fd);
                lprintf(9, "output_static('%s')  %s\n", what, content_type);
-               http_transmit_thing(Buf, content_type, 1);
-               FreeStrBuf(&Buf);
+               http_transmit_thing(content_type, 1);
        }
        if (yesbstr("force_close_session")) {
                end_webcit_session();
@@ -886,33 +913,29 @@ void output_static(char *what)
  */
 void output_image()
 {
+       struct wcsession *WCC = WC;
        char buf[SIZ];
-       char *xferbuf = NULL;
        off_t bytes;
        const char *MimeType;
-       StrBuf *Buf;
-
+       
        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 */
                
-               Buf = read_server_binary(bytes);
-               serv_puts("CLOS");
-               serv_getln(buf, sizeof buf);
-
-               MimeType = GuessMimeType (ChrPtr(Buf), StrLength(Buf));
-               /** Write it to the browser */
-               if (!IsEmptyStr(MimeType))
-               {
-                       http_transmit_thing(Buf, 
-                                           MimeType, 
-                                           0);
-                       FreeStrBuf(&Buf);
-                       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 */
        } 
@@ -935,35 +958,32 @@ void display_vcard_photo_img(void)
        long msgnum = 0L;
        char *vcard;
        struct vCard *v;
-       char *xferbuf;
        char *photosrc;
-       int decoded;
        const char *contentType;
-       StrBuf *Buf;
+       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);
-       Buf = _NewConstStrBuf(xferbuf, decoded);
-       http_transmit_thing(Buf, contentType, 0);
+       contentType = GuessMimeType(ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
+       http_transmit_thing(contentType, 0);
        free(v);
        free(photosrc);
-       free(xferbuf);
-       FreeStrBuf(&Buf);
 }
 
 /*
@@ -984,7 +1004,6 @@ void mimepart(const char *msgnum, const char *partnum, int force_download)
        serv_printf("OPNA %s|%s", msgnum, partnum);
        serv_getln(buf, sizeof buf);
        if (buf[0] == '2') {
-               StrBuf *Buf;
                bytes = extract_long(&buf[4], 0);
                content = malloc(bytes + 2);
                if (force_download) {
@@ -995,11 +1014,10 @@ void mimepart(const char *msgnum, const char *partnum, int force_download)
                }
                output_headers(0, 0, 0, 0, 0, 0);
 
-               Buf = read_server_binary(bytes);
+               read_server_binary(WC->WBuf, bytes);
                serv_puts("CLOS");
                serv_getln(buf, sizeof buf);
-               http_transmit_thing(Buf, content_type, 0);
-               FreeStrBuf(&Buf);
+               http_transmit_thing(content_type, 0);
        } else {
                hprintf("HTTP/1.1 404 %s\n", &buf[4]);
                output_headers(0, 0, 0, 0, 0, 0);
@@ -1076,7 +1094,7 @@ void blank_page(void) {
  * A template has been requested
  */
 void url_do_template(void) {
-       do_template(bstr("template"));
+       do_template(bstr("template"), NULL);
 }
 
 
@@ -1116,7 +1134,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);
 }
 
@@ -1181,10 +1199,8 @@ 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);
@@ -1320,14 +1336,19 @@ void seconds_since_last_gexp(void)
  * \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;
 }
 
 
@@ -1378,9 +1399,14 @@ void session_loop(struct httprequest *req)
        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;
@@ -1441,9 +1467,11 @@ void session_loop(struct httprequest *req)
                }
                else if (!strncasecmp(buf, "User-agent: ", 12)) {
                        safestrncpy(user_agent, &buf[12], sizeof user_agent);
+                       #ifdef TECH_PREVIEW
                        if (is_mobile_ua(&buf[12])) {
                                WCC->is_mobile = 1;
                        }
+                       #endif
                }
                else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
                        if (follow_xff) {
@@ -1479,7 +1507,11 @@ void session_loop(struct httprequest *req)
                client_read(WCC->http_sock, &content[body_start], ContentLength);
 
                if (!strncasecmp(ContentType, "application/x-www-form-urlencoded", 33)) {
-                       addurls(&content[body_start], ContentLength);
+                       StrBuf *Content;
+
+                       Content = _NewConstStrBuf(&content[body_start],ContentLength);
+                       addurls(Content);
+                       FreeStrBuf(&Content);
                } else if (!strncasecmp(ContentType, "multipart", 9)) {
                        content_end = content + ContentLength + body_start;
                        mime_parser(content, content_end, *upload_handler, NULL, NULL, NULL, 0);
@@ -1497,13 +1529,16 @@ void session_loop(struct httprequest *req)
        len = strlen(cmd);
        for (a = 0; a < len; ++a) {
                if ((cmd[a] == '?') || (cmd[a] == '&')) {
+                       StrBuf *Params;
                        for (b = a; b < len; ++b) {
                                if (isspace(cmd[b])){
                                        cmd[b] = 0;
                                        len = b - 1;
                                }
                        }
-                       addurls(&cmd[a + 1], len - a);
+                       //cmd[len - a] = '\0';
+                       Params = _NewConstStrBuf(&cmd[a + 1], len - a);
+                       addurls(Params);
                        cmd[a] = 0;
                        len = a - 1;
                }
@@ -1619,7 +1654,7 @@ void session_loop(struct httprequest *req)
                        }
                }
        }
-
+////////todo: restorte language in this case
        /*
         * Functions which can be performed without logging in
         */
@@ -1705,7 +1740,12 @@ 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 */
+                               }
                        }
                }
        }