manage content-encoding
[webserver.git] / http.c
diff --git a/http.c b/http.c
index 044ab362ed8fb3e365635dc2567aeaa7d71caecc..2f37611e656f5f4aecebf7553faa164aa5a65d69 100644 (file)
--- a/http.c
+++ b/http.c
@@ -310,6 +310,15 @@ char *trim (char *str)
     return str;
 }
 
+/* creqte command */
+
+char *createcommand (char *format, char *name)
+{
+    char *command = (char *) calloc (strlen (format) + strlen (name) + 1, 1);
+    sprintf (command, format, name);
+    return command;
+}
+
 /* main HTTP processing */
 
 int processing (char *data, int len, conf_t *conf, char **pdata)
@@ -362,7 +371,7 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
         if (*line == 0) {
             break;
         }
-        
+
         VERBOSE (DEBUG, PRINT ("Analyse field\n"));
         if (strcmp (field, "Allow") == 0) { header.allow = value; }
         else if (strcmp (field, "Authorization") == 0) { header.authorization = value; }
@@ -393,15 +402,62 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
 
     /* body */
     char *body = data;
+    char *newbody = NULL;
     len -= saved_data - data;
     if (len != (header.content_length ? atoi (header.content_length) : 0)) {
         VERBOSE (WARNING, PRINT ("Incoherent size (%d <> %s)\n", len, header.content_length));
     }
     if (len > 0) {
+        int i;
+        char *fcomp= NULL;
+        char *fdecomp = NULL;
+        char *command = NULL;
+
         switch (header.content_encoding) {
-        case encoding_plain_e: break;
-        case encoding_gzip_e: VERBOSE (WARNING, PRINT ("Gzip encoding not supported yet\n")); break;
-        case encoding_compress_e: VERBOSE (WARNING, PRINT ("Compress encoding not supported yet\n")); break;
+        case encoding_plain_e:
+            break;
+
+        case encoding_gzip_e:
+            fcomp = tempname (conf->temp, ".gz");
+            writefile (fcomp, body, len);
+            command = createcommand ("gunzip %s", fcomp);
+            system (command);
+            fdecomp = strdup (fcomp);
+            for (i = strlen (fdecomp) - 1; i > 0; i--) {
+                if (fdecomp[i] == '.') {
+                    fdecomp[i] = 0;
+                    break;
+                }
+            }
+            len = readfile (&newbody, fdecomp);
+            break;
+
+        case encoding_compress_e:
+            fcomp = tempname (conf->temp, ".Z");
+            writefile (fcomp, body, len);
+            command = createcommand ("compress %s", fcomp);
+            system (command);
+            fdecomp = strdup (fcomp);
+            for (i = strlen (fdecomp) - 1; i > 0; i--) {
+                if (fdecomp[i] == '.') {
+                    fdecomp[i] = 0;
+                    break;
+                }
+            }
+            len = readfile (&newbody, fdecomp);
+            break;
+        }
+
+        if (fcomp) {
+            unlink (fcomp);
+            free (fcomp);
+        }
+        if (fdecomp) {
+            unlink (fdecomp);
+            free (fdecomp);
+        }
+        if (command) {
+            free (command);
         }
     }
 
@@ -441,9 +497,11 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
     /* cleaning */
     VERBOSE (DEBUG, PRINT ("Cleaning\n"));
     if (path) {
-        VERBOSE (DEBUG, PRINT ("Cleaning path\n"));
         free (path);
     }
+    if (newbody) {
+        free (newbody);
+    }
 
     return len;
 }