* tiny 'file'-extract, now we can detect the type of an image. and set the mimetype...
[citadel.git] / webcit / webcit.c
index 71b780a76a4c3d6b7d78f9bb5c43c597846c1967..67ed35c212eb393005d22acbbead1c5f376c4bfe 100644 (file)
@@ -589,7 +589,7 @@ void http_redirect(char *whichpage) {
 /**
  * \brief Output a piece of content to the web browser
  */
-void http_transmit_thing(char *thing, size_t length, char *content_type,
+void http_transmit_thing(char *thing, size_t length, const char *content_type,
                         int is_static) {
 
        output_headers(0, 0, 0, 0, 0, is_static);
@@ -764,6 +764,43 @@ void output_static(char *what)
 }
 
 
+
+typedef struct _MimeGuess {
+       const char *Pattern;
+       size_t PatternLen;
+       long PatternOffset;
+       const char *MimeString;
+} MimeGuess;
+
+MimeGuess MyMimes [] = {
+       {
+               "GIF",
+               3,
+               0,
+               "image/gif"
+       },
+       {
+               "\xff\xd8",
+               2,
+               0,
+               "image/jpeg"
+       },
+       {
+               "\x89PNG",
+               4,
+               0,
+               "image/png"
+       },
+       { // last...
+               "",
+               0,
+               0,
+               ""
+       }
+};
+
+
+
 /**
  * \brief When the browser requests an image file from the Citadel server,
  * this function is called to transmit it.
@@ -773,6 +810,7 @@ void output_image()
        char buf[SIZ];
        char *xferbuf = NULL;
        off_t bytes;
+       int MimeIndex = 0;
 
        serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
        serv_getln(buf, sizeof buf);
@@ -785,22 +823,37 @@ void output_image()
                serv_puts("CLOS");
                serv_getln(buf, sizeof buf);
 
+               while (MyMimes[MimeIndex].PatternLen != 0)
+               {
+                       if (strncmp(MyMimes[MimeIndex].Pattern, 
+                                   &xferbuf[MyMimes[MimeIndex].PatternOffset], 
+                                   MyMimes[MimeIndex].PatternLen) == 0)
+                               break;
+                       MimeIndex ++;
+               }
+
                /** Write it to the browser */
-               http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
+               if (MyMimes[MimeIndex].PatternLen != 0)
+               {
+                       http_transmit_thing(xferbuf, 
+                                           (size_t)bytes, 
+                                           MyMimes[MimeIndex].MimeString, 
+                                           0);
+                       free(xferbuf);
+                       return;
+               }
+               /* hm... unknown mimetype? fallback to blank gif */
                free(xferbuf);
+       } 
 
-       } else {
-               /**
-                * Instead of an ugly 404, send a 1x1 transparent GIF
-                * when there's no such image on the server.
-                */
-               char blank_gif[SIZ];
-               snprintf (blank_gif, SIZ, "%s%s", static_dirs[0], "/blank.gif");
-               output_static(blank_gif);
-       }
-
-
-
+       
+       /**
+        * Instead of an ugly 404, send a 1x1 transparent GIF
+        * when there's no such image on the server.
+        */
+       char blank_gif[SIZ];
+       snprintf (blank_gif, SIZ, "%s%s", static_dirs[0], "/blank.gif");
+       output_static(blank_gif);
 }
 
 /**