| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 
 | //
// HtFile.cc
//
// HtFile: Interface classes for retriving local documents
//
// Including:
// 	 -  Generic class
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 1995-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtFile.cc,v 1.13 2004/05/28 13:15:23 lha Exp $ 
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include "lib.h"
#include "Transport.h"
#include "HtFile.h"
#include "Dictionary.h"
#include "StringList.h"
#include "defaults.h" // for config
#include <signal.h>
#include <sys/types.h>
#include <ctype.h>
#ifdef HAVE_STD
#include <iostream>
#include <fstream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <iostream.h>
#include <fstream.h>
#endif /* HAVE_STD */
#include <stdio.h> // for sscanf
#include <sys/stat.h>
#ifndef _MSC_VER /* _WIN32 */
#include <unistd.h>
#endif
#ifdef _MSC_VER /* _WIN32 */
#include "dirent_local.h"
#else
#include <dirent.h> // for scandir
#endif
#ifdef _MSC_VER /* _WIN32 */
#define popen _popen
#define pclose _pclose
#define lstat stat
#define readlink(x,y,z) {-1}
#endif
///////
   //    HtFile_Response class
///////
// Construction
HtFile_Response::HtFile_Response()
{
}
// Destruction
HtFile_Response::~HtFile_Response()
{
}
///////
   //    HtFile generic class
   //
   //    
///////
// Construction
HtFile::HtFile()
{
}
// Destruction
HtFile::~HtFile()
{
   // It's empty
}
// Return mime type indicated by extension  ext  (which is assumed not
// to contain the '.'), or  NULL  if  ext  is not a know mime type.
const String *HtFile::Ext2Mime (const char *ext)
{
   static Dictionary *mime_map = 0;
   if (!mime_map)
     {
       HtConfiguration* config= HtConfiguration::config();
       mime_map = new Dictionary();
       if (!mime_map)
	 return NULL;
       if (debug > 2)
 	    cout << "MIME types: " << config->Find("mime_types").get() << endl;
       ifstream in(config->Find("mime_types").get());
       if (in)
         {
           String line;
           while (in >> line)
             {
               line.chop("\n\r \t");
               int cmt;
               if ((cmt = line.indexOf('#')) >= 0)
                 line = line.sub(0, cmt);
               StringList split_line(line, "\t ");
               // Let's cache mime type to lesser the number of 
               // operator [] callings
               String mime_type = split_line[0];
               // Fill map with values.
               for (int i = 1; i < split_line.Count(); i++)
	       {
	         if (debug > 3)
		   cout << "MIME: " << split_line[i]
		        << "\t-> " << mime_type << endl;
                 mime_map->Add(split_line[i], new String(mime_type));
	       }
             }
         }
       else
	 {
	   if (debug > 2)
		cout << "MIME types file not found.  Using default types.\n";
	   mime_map->Add(String("html"), new String("text/html"));
	   mime_map->Add(String("htm"),  new String("text/html"));
	   mime_map->Add(String("txt"),  new String("text/plain"));
	   mime_map->Add(String("asc"),  new String("text/plain"));
	   mime_map->Add(String("pdf"),  new String("application/pdf"));
	   mime_map->Add(String("ps"),   new String("application/postscript"));
	   mime_map->Add(String("eps"),  new String("application/postscript"));
	 }
     }
   // return MIME type, or NULL if not found
   return (String *)mime_map->Find(ext);
}
// Return mime type of the file named 'fname'.
// If the type can't be determined, "application/x-unknown" is returned.
String HtFile::File2Mime (const char *fname)
{
     HtConfiguration* config= HtConfiguration::config();
    // default to "can't identify"
    char content_type [100] = "application/x-unknown\n";
    String cmd = config->Find ("content_classifier");
    if (cmd.get() && *cmd)
    {
	cmd << " \"" << fname << '\"';	// allow file names to have spaces
	FILE *fileptr;
	if ( (fileptr = popen (cmd.get(), "r")) != NULL )
	{
	    fgets (content_type, sizeof (content_type), fileptr);
	    pclose (fileptr);
	}
    }
    // Remove trailing newline, charset or language information
    int delim = strcspn (content_type, ",; \n\t");
    content_type [delim] = '\0';
    if (debug > 1)
	cout << "Mime type: " << fname << ' ' << content_type << endl;
    return (String (content_type));
}
///////
   //    Manages the requesting process
///////
HtFile::DocStatus HtFile::Request()
{
   // Reset the response
   _response.Reset();
   
   struct stat stat_buf;
   String path (_url.path());
   decodeURL (path);		// Convert '%20' to ' ' etc
   // Check that it exists, and is a regular file or directory
   // Don't allow symbolic links to directories; they mess up '../'.
   // Should we allow FIFO's?
   if ( stat(path.get(), &stat_buf) != 0 || 
	!(S_ISREG(stat_buf.st_mode) || S_ISDIR(stat_buf.st_mode)) )
   {
     return Transport::Document_not_found;
   }
   // Now handle directories with a pseudo-HTML document (and appropriate noindex)
   if ( S_ISDIR(stat_buf.st_mode) )
     {
       _response._content_type = "text/html";
       _response._contents = "<html><head><meta name=\"robots\" content=\"noindex\">\n";
       struct dirent *namelist;
       DIR *dirList;
       String filename;
       String encodedName;
       if (( dirList = opendir(path.get()) ))
	 {
	   while (( namelist = readdir(dirList) ))
	    {
	     filename = path;
	     filename << namelist->d_name;
	     
	     if ( namelist->d_name[0] != '.' 
		  && lstat(filename.get(), &stat_buf) == 0 )
	       {
		 // Recursively resolve symbolic links.
		 // Could leave "absolute" links, or even all not
		 // containing '../'.  That would allow "aliasing" of
		 // directories without causing loops.
		 int i;		// avoid infinite loops
		 for (i=0; (stat_buf.st_mode & S_IFMT) == S_IFLNK && i<10; i++)
		 {
		     char link [100];
		     int count = readlink(filename.get(), link, sizeof(link)-1);
		     if (count < 0)
			 break;
		     link [count] = '\0';
		     encodedName = link;
		     encodeURL (encodedName);
		     URL newURL (encodedName, _url);	// resolve relative paths
		     filename = newURL.path();
		     decodeURL (filename);
		     if (debug > 2)
			 cout << "Link to " << link << " gives "
			      << filename.get() << endl;
		     lstat(filename.get(), &stat_buf);
		 }
		 // filename now only sym-link if nested too deeply or I/O err.
		 encodeURL (filename, UNRESERVED "/");	// convert ' ' to '%20' etc., but leave "/" intact
		 if (S_ISDIR(stat_buf.st_mode))
		   _response._contents << "<link href=\"file://"
				       << filename.get() << "/\">\n";
		 else if (S_ISREG(stat_buf.st_mode))
		   _response._contents << "<link href=\"file://"
				       << filename.get() << "\">\n";  
	       }
	    }
	   closedir(dirList);
	 }
       _response._contents << "</head><body></body></html>\n";
       if (debug > 4)
	 cout << " Directory listing: " << endl << _response._contents << endl;
       _response._content_length = stat_buf.st_size;
       _response._document_length = _response._contents.length();
       _response._modification_time = new HtDateTime(stat_buf.st_mtime);
       _response._status_code = 0;
       return Transport::Document_ok;
     }
   if (_modification_time && *_modification_time >= HtDateTime(stat_buf.st_mtime))
     return Transport::Document_not_changed;
   bool unknown_ext = false;
   char *ext = strrchr(path.get(), '.');
   if (ext == NULL)
      unknown_ext = true;
   else
   {
      const String *mime_type = Ext2Mime(ext + 1);
      if (mime_type)
        _response._content_type = *mime_type;
      else
        unknown_ext = true;
   }
   if (unknown_ext)
   {
       _response._content_type = File2Mime (path.get());
       if (!strncmp (_response._content_type.get(), "application/x-", 14))
           return Transport::Document_not_local;
   }
   _response._modification_time = new HtDateTime(stat_buf.st_mtime);
   FILE *f = fopen((const char *)path.get(), "r");
   if (f == NULL)
     return Document_not_found;
   char	docBuffer[8192];
   int		bytesRead;
   while ((bytesRead = fread(docBuffer, 1, sizeof(docBuffer), f)) > 0)
     {
	if (_response._contents.length() + bytesRead > _max_document_size)
	    bytesRead = _max_document_size - _response._contents.length();
	_response._contents.append(docBuffer, bytesRead);
	if (_response._contents.length() >= _max_document_size)
	    break;
     }
   fclose(f);
   _response._content_length = stat_buf.st_size;
   _response._document_length = _response._contents.length();
   _response._status_code = 0;
   if (debug > 2)
     cout << "Read a total of " << _response._document_length << " bytes\n";
   return Transport::Document_ok;
}
HtFile::DocStatus HtFile::GetDocumentStatus()
{ 
   // Let's give a look at the return status code
   if (_response._status_code == -1)
      return Transport::Document_not_found;
   return Transport::Document_ok;
}
 |