Hi Witold,

Great to see that you fixed the issue with MIME types for directories.
But I think that your patch does more than it has to.

What about the following patch?

---

Simplify commit 8d4f44f2f16d51e147df1dde7355d5da85a41115, in particular
detecting MIME types for files. It is more consistent to do it the way
it was already done by the session/download code.

Instead, write a NUL byte to stderr when getting FSP files and only set
cache->content_type when the header string is non-empty.

Additionally it also moves close(stderr) after the fsp_error() in the
file handling part of do_fsp() so the error message is shown with the
correct type.

---

 src/mime/mime.c        |    6 ------
 src/mime/mime.h        |    3 ---
 src/protocol/fsp/fsp.c |   50 ++++++++++++++++--------------------------------
 3 files changed, 17 insertions(+), 42 deletions(-)

diff --git a/src/mime/mime.c b/src/mime/mime.c
index bbeca78..3737291 100644
--- a/src/mime/mime.c
+++ b/src/mime/mime.c
@@ -225,12 +225,6 @@ get_cache_header_content_type(struct cac
 }
 
 unsigned char *
-get_default_content_type(void)
-{
-       return get_default_mime_type();
-}
-
-unsigned char *
 get_content_type(struct cache_entry *cached)
 {
        unsigned char *extension, *ctype;
diff --git a/src/mime/mime.h b/src/mime/mime.h
index 8584ee4..779ed3b 100644
--- a/src/mime/mime.h
+++ b/src/mime/mime.h
@@ -20,9 +20,6 @@ extern struct module mime_module;
  * scanning the uri for extensions. */
 unsigned char *get_content_type(struct cache_entry *cached);
 
-/* Default mime type */
-unsigned char *get_default_content_type(void);
-
 /* Guess content type by looking at configurations of the given @extension */
 unsigned char *get_extension_content_type(unsigned char *extension);
 
diff --git a/src/protocol/fsp/fsp.c b/src/protocol/fsp/fsp.c
index d343d8e..8b8e6a5 100644
--- a/src/protocol/fsp/fsp.c
+++ b/src/protocol/fsp/fsp.c
@@ -27,7 +27,6 @@
 #include "intl/gettext/libintl.h"
 #include "main/module.h"
 #include "main/select.h"
-#include "mime/mime.h"
 #include "network/connection.h"
 #include "network/socket.h"
 #include "osdep/osdep.h"
@@ -180,31 +179,6 @@ end:
        exit(0);
 }
 
-static unsigned char *
-get_content_type_uri(struct uri *uri)
-{
-       unsigned char *extension = get_extension_from_uri(uri);
-
-       if (extension) {
-               unsigned char *ctype;
-               /* XXX: A little hack for making extension handling case
-                * insensitive. We could probably do it better by making
-                * guess_encoding() case independent the real problem however
-                * is with default (via option system) and mimetypes resolving
-                * doing that option and hash lookup will not be easy to
-                * convert. --jonas */
-               convert_to_lowercase(extension, strlen(extension));
-
-               ctype = get_extension_content_type(extension);
-               if (ctype && *ctype) {
-                       return ctype;
-               }
-       }
-
-       return get_default_content_type();
-
-}
-
 #define READ_SIZE      4096
 
 static void
@@ -229,13 +203,18 @@ do_fsp(struct connection *conn)
                FSP_FILE *file = fsp_fopen(ses, data, "r");
                int r;
 
-               fprintf(stderr, "%s", get_content_type_uri(uri));
-               fclose(stderr);
                if (!file)
                        fsp_error("fsp_fopen error.");
 
+               /* Use the default way to find the MIME type, so write an
+                * 'empty' name, since something needs to be written in order
+                * to avoid socket errors. */
+               fprintf(stderr, "%c", '\0');
+               fclose(stderr);
+
                while ((r = fsp_fread(buf, 1, READ_SIZE, file)) > 0)
                        fwrite(buf, 1, r, stdout);
+
                fsp_fclose(file);
                fsp_close_session(ses);
                exit(0);
@@ -273,7 +252,6 @@ fsp_got_data(struct socket *socket, stru
 static void
 fsp_got_header(struct socket *socket, struct read_buffer *rb)
 {
-       int len = rb->length;
        struct connection *conn = socket->conn;
        struct read_buffer *buf;
 
@@ -285,10 +263,16 @@ fsp_got_header(struct socket *socket, st
                return;
        }
        socket->state = SOCKET_END_ONCLOSE;
-       if (len <= 0) goto end;
-       rb->data[len] = '\0';
-       mem_free_set(&conn->cached->content_type, stracpy(rb->data));
-end:
+
+       if (rb->length > 0) {
+               unsigned char *ctype = memacpy(rb->data, rb->length);
+
+               if (ctype && *ctype)
+                       mem_free_set(&conn->cached->content_type, ctype);
+               else
+                       mem_free_if(ctype);
+       }
+
        buf = alloc_read_buffer(conn->data_socket);
        if (!buf) {
                close(socket->fd);
-- 
Jonas Fonseca
_______________________________________________
elinks-dev mailing list
[email protected]
http://linuxfromscratch.org/mailman/listinfo/elinks-dev

Reply via email to