/* * File: file.c :) * * Copyright (C) 2000-2007 Jorge Arellano Cid * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. */ /* * Directory scanning is no longer streamed, but it gets sorted instead! * Directory entries on top, files next. * With new HTML layout. */ #include #include /* for tolower */ #include /* for errno */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../dpip/dpip.h" #include "dpiutil.h" #include "d_size.h" /* * Debugging macros */ #define _MSG(...) #define MSG(...) printf("[file dpi]: " __VA_ARGS__) #define MAXNAMESIZE 30 #define HIDE_DOTFILES TRUE typedef struct { char *full_path; const char *filename; off_t size; mode_t mode; time_t mtime; } FileInfo; typedef struct { char *dirname; Dlist *flist; /* List of files and subdirectories (for sorting) */ } DilloDir; typedef struct { SockHandler *sh; int status; int old_style; pthread_t thrID; int done; } ClientInfo; /* * Forward references */ static const char *File_content_type(const char *filename); static int File_get_file(ClientInfo *Client, const char *filename, struct stat *sb, const char *orig_url); static int File_get_dir(ClientInfo *Client, const char *DirName, const char *orig_url); /* * Global variables */ static volatile int DPIBYE = 0; static volatile int ThreadRunning = 0; static int OLD_STYLE = 0; /* A list for the clients we are serving */ static Dlist *Clients; /* a mutex for operations on clients */ static pthread_mutex_t ClMut; /* * Close a file descriptor, but handling EINTR */ static void File_close(int fd) { while (close(fd) < 0 && errno == EINTR) ; } /* * Detects 'Content-Type' when the server does not supply one. * It uses the magic(5) logic from file(1). Currently, it * only checks the few mime types that Dillo supports. * * 'Data' is a pointer to the first bytes of the raw data. * (this is based on a_Misc_get_content_type_from_data()) */ static const char *File_get_content_type_from_data(void *Data, size_t Size) { static const char *Types[] = { "application/octet-stream", "text/html", "text/plain", "image/gif", "image/png", "image/jpeg", }; int Type = 0; char *p = Data; size_t i, non_ascci; _MSG("File_get_content_type_from_data:: Size = %d\n", Size); /* HTML try */ for (i = 0; i < Size && isspace(p[i]); ++i); if ((Size - i >= 5 && !dStrncasecmp(p+i, "= 5 && !dStrncasecmp(p+i, "= 6 && !dStrncasecmp(p+i, "= 14 && !dStrncasecmp(p+i, "= 17 && !dStrncasecmp(p+i, "