best makefile
[webserver.git] / http.c
diff --git a/http.c b/http.c
index fbd18ef87e67ba23e0ceabc2728e4d1c6d298e34..22b5cbcbbf901c169ca439bbcd45b97f837d1c8c 100644 (file)
--- a/http.c
+++ b/http.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <unistd.h>
 
 #include "debug.h"
 #include "file.h"
@@ -41,10 +42,14 @@ typedef enum {
     not_supported_e = 0, get_e, head_e, post_e
 } method_t;
 
+typedef enum {
+    encoding_plain_e = 0, encoding_gzip_e, encoding_compress_e
+} encoding_t;
+
 typedef struct {
     char *allow;
     char *authorization;
-    char *content_encoding;
+    encoding_t content_encoding;
     char *content_length;
     char *content_type;
     char *date;
@@ -112,7 +117,14 @@ void print_header_values (header_t *header)
     printf ("Header values\n");
     if (header->allow) printf ("allow = '%s'\n", header->allow);
     if (header->authorization) printf ("authorization = '%s'\n", header->authorization);
-    if (header->content_encoding) printf ("content_encoding = '%s'\n", header->content_encoding);
+    if (header->content_encoding) {
+        printf ("content_encoding = ");
+        switch (header->content_encoding) {
+        case encoding_plain_e: printf ("plain\n"); break;
+        case encoding_gzip_e: printf ("gzip\n"); break;
+        case encoding_compress_e: printf ("compress\n"); break;
+        }
+    }
     if (header->content_length) printf ("content_length = '%s'\n", header->content_length);
     if (header->content_type) printf ("content_type = '%s'\n", header->content_type);
     if (header->date) printf ("date = '%s'\n", header->date);
@@ -299,6 +311,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)
@@ -351,12 +372,19 @@ 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; }
-        else if (strcmp (field, "Content-Encoding") == 0) { header.content_encoding = value; }
-        else if (strcmp (field, "Content-Length") == 0) { header.content_length = value; }
+        else if (strcmp (field, "Content-Encoding") == 0) {
+            if (strcmp (value, "x-gzip") == 0) {
+                header.content_encoding = encoding_gzip_e;
+            } else if (strcmp (value, "x-compress") == 0) {
+                header.content_encoding = encoding_compress_e;
+            } else {
+                VERBOSE (WARNING, PRINT ("Unknown content encoding: %s\n", value));
+            }
+        } else if (strcmp (field, "Content-Length") == 0) { header.content_length = value; }
         else if (strcmp (field, "Content-Type") == 0) { header.content_type = value; }
         else if (strcmp (field, "Date") == 0) { header.date = value; }
         else if (strcmp (field, "Expires") == 0) { header.expires = value; }
@@ -375,10 +403,64 @@ 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:
+            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);
+        }
+    }
 
     /* response */
     char *buffer = NULL;
@@ -416,9 +498,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;
 }