9front and more

tcp80 had the beginning of support for handling the Content-Type header. This patch completes it.

Published by be0ba on

tcp80 Content-Type headers overhaul

be0ba
diff --git a/tcp80.c b/tcp80.c
index ab3e56b..05af9f4 100644
--- a/tcp80.c
+++ b/tcp80.c
@@ -36,6 +36,7 @@ Ctype ctypemap[] = {
    ".html", "text/html;charset=utf-8",
    ".txt", "text/plain;charset=utf-8",
    ".md", "text/markdown;charset=utf-8",
+   ".css", "text/css;charset=utf-8",
 };
 
 Pair*
@@ -228,8 +229,20 @@ headers(char *path, Dir *d)
                f[0], tm->mday, f[1], f[5], f[3], f[4]);
    }
    isdir = d && (d->qid.type & QTDIR);
-   if(isdir || cistrstr(path, ".htm"))
+   if(isdir){
        print("Content-Type: text/html; charset=utf-8\r\n");
+   }else{
+        int i;
+        for(i = 0; i < nelem(ctypemap); i++){
+            int offset = strlen(path) - strlen(ctypemap[i].suffix);
+            if (offset <= 0)
+                break;
+            if(cistrcmp(path+offset, ctypemap[i].suffix) == 0){
+               print("Content-Type: %s\r\n", ctypemap[i].type);
+               break;
+           }
+       }
+   }
    if(*path == '/')
        print("Content-Location: %s%s\r\n",
            urlenc(buf, path, sizeof(buf)), isdir ? "/" : "");

Tags: 9fronttcp80.