summaryrefslogtreecommitdiff
path: root/TAO/examples/Content_Server/AMI_Iterator/Iterator_Factory_i.cpp
blob: 214b818cfd3132d184bb8d344eebc84e55f83569 (plain)
1
2
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
// -*- C++ -*-
// $Id$

// Ossama Othman <ossama@uci.edu>

#include "Content_Iterator_i.h"
#include "Iterator_Factory_i.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_strings.h"
#include "ace/OS_NS_string.h"


ACE_RCSID (AMI_Iterator,
           Iterator_Factory_i,
           "$Id$")


void
Iterator_Factory_i::get_iterator (const char *pathname,
                                  Web_Server::Content_Iterator_out contents,
                                  Web_Server::Metadata_Type_out metadata
                                  ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException, Web_Server::Error_Result))
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Received request for file: <%s>\n"),
              pathname));

  ACE_stat file_status;
  if (ACE_OS::stat (pathname, &file_status) == -1)
    // HTTP 1.1 "Internal Server Error".
    ACE_THROW (Web_Server::Error_Result (500));

  Content_Iterator_i *iterator_servant = 0;
  ACE_NEW_THROW_EX (iterator_servant,
                    Content_Iterator_i (pathname,
                                        file_status.st_size),
                    CORBA::NO_MEMORY ());
  ACE_CHECK;

  if (iterator_servant->init () != 0)
    {
      if (errno == EACCES)
        // HTTP 1.1 "Forbidden".
        ACE_THROW (Web_Server::Error_Result (403));
      else
        // HTTP 1.1 "Internal Server Error".
        ACE_THROW (Web_Server::Error_Result (500));
    }


  // Activate the Content_Iterator object.
  Web_Server::Content_Iterator_var iterator =
    iterator_servant->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  Web_Server::Metadata_Type *tmp = 0;
  ACE_NEW_THROW_EX (tmp,
                    Web_Server::Metadata_Type,
                    CORBA::NO_MEMORY ());
  ACE_CHECK;

  metadata = tmp;

  if (this->modification_date (&file_status,
                               metadata) != 0)
    // HTTP 1.1 "Internal Server Error.
    ACE_THROW (Web_Server::Error_Result (500));

  if (this->content_type (pathname,
                          metadata) != 0)
    // HTTP 1.1 "Internal Server Error.
    ACE_THROW (Web_Server::Error_Result (500));

  contents = iterator._retn (); // Make a copy
}

int
Iterator_Factory_i::modification_date (ACE_stat * file_status,
                                       Web_Server::Metadata_Type_out metadata)
{
  // Get the modification time from the file status structure/
  struct tm *t_gmt = gmtime (&(file_status->st_mtime));

  // A time string is probably never going to exceed 256 bytes.
  const size_t buflen = 256;
  char buf[buflen];

  // Format the time to conform to RFC 1123.
  if (ACE_OS::strftime (buf,
                        buflen,
                        "%a, %d %b %Y %H:%M:%S GMT",
                        t_gmt) == 0)
    return -1;
  else
    metadata->modification_date = CORBA::string_dup (buf);

  return 0;
}

int
Iterator_Factory_i::content_type (const char *filename,
                                  Web_Server::Metadata_Type_out metadata)
{
  if (filename == 0)
    return -1;

  // @@ There are a bunch of const_casts in this method.  It's ugly.
  //    I know.
  //        -Ossama

  size_t len = ACE_OS::strlen (filename);

  // Search for extension
  // Handle the case where multiple periods exists in the filename,
  // e.g.:  foo.bar.ps
  char *extension = 0;
  for (char * tmp = const_cast<char *> (filename);
       tmp != 0 && tmp != tmp + len;
       )
    {
      tmp = const_cast<char *> (ACE_OS::strchr (tmp, '.'));
      if (tmp != 0)
        extension = ++tmp;  // Skip over the '.'
    }

  if (extension == 0)
    extension = const_cast<char *> (filename);  // No extension!

  if (ACE_OS::strcasecmp (extension, "htm") == 0
      || ACE_OS::strcasecmp (extension, "html") == 0)
    metadata->content_type = CORBA::string_dup ("text/html");
  else if (ACE_OS::strcasecmp (extension,
                               "txt") == 0)
    metadata->content_type = CORBA::string_dup ("text/plain");
  else if (ACE_OS::strcasecmp (extension,
                               "ps") == 0)
    metadata->content_type =
      CORBA::string_dup ("application/postscript");
  else if (ACE_OS::strcasecmp (extension,
                               "pdf") == 0)
    metadata->content_type = CORBA::string_dup ("application/pdf");
  else if (ACE_OS::strcasecmp (extension,
                               "jpeg") == 0
           || ACE_OS::strcasecmp (extension,
                                  "jpg") == 0)
    metadata->content_type = CORBA::string_dup ("image/jpeg");
  else if (ACE_OS::strcasecmp (extension,
                               "tiff") == 0)
    metadata->content_type = CORBA::string_dup ("image/tiff");
  else if (ACE_OS::strcasecmp (extension,
                               "gif") == 0)
    metadata->content_type = CORBA::string_dup ("image/gif");
  else if (ACE_OS::strcasecmp (extension,
                               "png") == 0)
    metadata->content_type = CORBA::string_dup ("image/png");
  else
    {
      metadata->content_type = CORBA::string_dup ("text/html");
      ACE_ERROR ((LM_WARNING,
                  ACE_TEXT ("\n  ")
                  ACE_TEXT ("Unknown file type.  ")
                  ACE_TEXT ("Using \"text/html\" content type.\n")));
    }

  return 0;
}