Quoted-printable encode HTML messages on the WebCit Server side.
[citadel.git] / webcit-ng / server / messages.c
index f176156a51d4ccdda7728acf21464348adb80098..f3cba7c6636bbee51b64f8b080d0b7323f5702bb 100644 (file)
@@ -163,6 +163,7 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum) {
        char buf[1024];
        char *content_type = NULL;
+       char *content_transfer_encoding = NULL;
        int n;
        long new_msgnum;
        char new_euid[1024];
@@ -213,9 +214,52 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
 
        // This section
        content_type = header_val(h, "Content-type");
-       ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
-       ctdl_write(c, HKEY("\r\n"));
-       ctdl_write(c, h->request_body, h->request_body_length);
+       content_transfer_encoding = header_val(h, "Content-transfer-encoding");
+
+       // If the content is already encoded, pass it along as-is
+       if (!IsEmptyStr(content_transfer_encoding)) {
+               ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
+               ctdl_write(c, HKEY("\r\n"));
+               ctdl_write(c, h->request_body, h->request_body_length);
+       }
+
+       // But if it's raw, we ought to encode it so it's MIME-friendly.
+       else {
+               ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
+               ctdl_printf(c, "Content-transfer-encoding: quoted-printable\r");
+               ctdl_write(c, HKEY("\r\n"));
+               h->request_body[h->request_body_length] = 0;            // make doubly sure it's null terminated.
+
+               // Adapted from https://www.w3.org/Tools/Mail-Transcode/mail-transcode.c
+               char *s = h->request_body;
+               char strconv[8];
+               int strconv_len;
+               int n;
+               for (n = 0; *s; s++) {
+                       if (n >= 73 && *s != 10 && *s != 13) {
+                               ctdl_write(c, HKEY("=\r\n"));
+                               n = 0;
+                       }
+                       if (*s == 10 || *s == 13) {
+                               ctdl_write(c, s, 1);
+                               n = 0;
+                       }
+                       else if (*s<32 || *s==61 || *s>126) {
+                               strconv_len = sprintf(strconv, "=%02X", (unsigned char)*s);
+                               ctdl_write(c, strconv, strconv_len);
+                               n += strconv_len;
+                       }
+                       else if (*s != 32 || (*(s+1) != 10 && *(s+1) != 13)) {
+                               ctdl_write(c, s, 1);
+                               n++;
+                       }
+                       else {
+                               ctdl_write(c, "=20", 3);
+                               n += 3;
+                       }
+               }
+       }
+
        if (h->request_body[h->request_body_length] != '\n') {
                ctdl_write(c, HKEY("\r\n"));
        }