aboutsummaryrefslogtreecommitdiff
path: root/src/IO.c
blob: ba79c63f3e993a28c05836d14a558a040c870a7c (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
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
 /*@@
   @file      IO.c
   @date      Sun Sep 17 16:19:17 2000
   @author    Tom Goodale
   @desc 
   Stuff for displaying IO callbacks on the web page.
   @enddesc 
   @version $Header$
 @@*/

#include "cctk.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "util_String.h"

#include "CactusBase/IOUtil/src/ioGH.h"

#include "DevThorns/httpd/src/http_Request.h"
#include "DevThorns/httpd/src/http_Content.h"

static char *rcsid = "$Header$";

CCTK_FILEVERSION(DevThorns_httpd_utils_IO_c)

/********************************************************************
 *********************     Local Data Types   ***********************
 ********************************************************************/

struct httpuFileList
{
  struct httpuFileList *next;
  char *filename;
  char *linkname;
  IOUtil_AdvertisedFileDesc_t data;  
};

struct httpuMimeType
{
  struct httpuMimeType *next;
  const char *name;
};

/********************************************************************
 ********************* Local Routine Prototypes *********************
 ********************************************************************/

static int IOFileListener(cGH *GH, const char *filename,
                          const IOUtil_AdvertisedFileDesc_t *description);

static int AdvertisedFilePage(cGH *cctkGH, httpRequest *request, void *data);

static int SendFilePage(cGH *cctkGH, httpRequest *request, void *data);

/********************************************************************
 ********************* Other Routine Prototypes *********************
 ********************************************************************/

/********************************************************************
 *********************     Local Data   *****************************
 ********************************************************************/

static struct httpuFileList *filelist   = NULL;
static struct httpuMimeType *mimetypes  = NULL;

/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/

int HTTP_util_RegisterIOPages(void)
{
  IOUtil_AdvertisedFileListenerCallbacks_t listener;

  listener.advertise = IOFileListener;

  IOUtil_RegisterAdvertisedFileListener (NULL, CCTK_THORNSTRING,
                                         &listener);

  HTTP_RegisterPage("/Output/index.html", AdvertisedFilePage, NULL);

  HTTP_ContentLink("/Output/index.html", "Files",
                   "Downloadable files",
                   HTTP_QUICKLINK);

  HTTP_RegisterPage("/Output", SendFilePage, NULL);

  return 0;
}
/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/

 /*@@
   @routine    IOFileListener
   @date       Sun Sep 17 17:56:22 2000
   @author     Tom Goodale
   @desc 
   Listener for advertised files.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static int IOFileListener(cGH *GH, const char *filename,
                          const IOUtil_AdvertisedFileDesc_t *description)
{
  struct httpuFileList *entry;
  struct httpuMimeType *type;
  char *position;

  entry = (struct httpuFileList *)malloc(sizeof(struct httpuFileList));

  if(entry)
  {
    entry->next             = NULL;
    entry->filename         = Util_Strdup(filename);
    entry->linkname         = Util_Strdup(filename);
    entry->data.thorn       = Util_Strdup(description->thorn);
    entry->data.varname     = Util_Strdup(description->varname);
    entry->data.mimetype    = Util_Strdup(description->mimetype);
    entry->data.slice       = Util_Strdup(description->slice);
    entry->data.description = Util_Strdup(description->description);

    entry->next = filelist;
    filelist = entry;

    /* Need to mangle the filename to get a decent linkname */
    for(position=entry->linkname; *position;position++)
    {
      if(*position == '/')
      {
        *position = '@';
      }
    }

    for(type=mimetypes; type; type=type->next)
    {
      if(!strcmp(type->name,entry->data.mimetype))
      {
        break;
      }
    }

    /* Keep a list of mimetypes too. */
    if(!type)
    {
      type = (struct httpuMimeType *)malloc(sizeof(struct httpuMimeType));
      
      if(type)
      {
        type->name = Util_Strdup(description->mimetype);

        type->next = mimetypes;
        mimetypes  = type;
      }
    }                                            
  }

  return 0;
}

static const char *cactus_header = 
"</HEAD>\n"
"<BODY BGCOLOR=\"#FFFFFF\""
"        link=\"#1B831D\" vlink=\"#768000\" alink=\"#00FF00\">\n"
"<center>"
"</A>"
"<table width=70% border=0><tr><td>"
"\n";

static const char *cactus_footer =
"</table>"
"\n"
"<HR size=1>\n"
"<ADDRESS><DIV ALIGN=left>\n"
"<SMALL>\n"
"<A HREF=\"http://www.cactuscode.org/\">Cactus Home Page</A><BR>\n"
"Cactus Web Interface by <A HREF=\"mailto:cactusmaint@cactuscode.org\">The Cactus Team</A><BR>\n"
"</SMALL></DIV></ADDRESS></BODY></HTML>\n";

 /*@@
   @routine    AdvertisedFilePage
   @date       Sun Sep 17 18:26:01 2000
   @author     Tom Goodale
   @desc 
   Page to deal with advertised files.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static int AdvertisedFilePage(cGH *cctkGH, httpRequest *request, void *data)
{
  int retval;
  char message[4098];
  struct httpuFileList *list;

  /* Status message */
  strcpy(message,"HTTP/1.0 200 OK\r\n");
  
  HTTP_Write(request, message, strlen(message));

  /* Content-Type */
  strcpy(message,"Content-Type: text/html\r\n\r\n");

  HTTP_Write(request, message, strlen(message));

  /* Start the page */
  strcpy(message, "<HTML><HEAD><TITLE>Cactus Downloadable Files</TITLE>\n");
  strcat(message, cactus_header);

  strcat(message, "<center><h1>Downloadable Files</h1></center>");

  HTTP_Write(request, message, strlen(message));

  strcpy(message, "<center>");
  HTTP_Write(request, message, strlen(message));

  strcpy(message,
         "<center>[<A HREF=\"/\">This Simulation Homepage</A>]</center>\n"
         "</center><p>From this page you can download various output files \n"
         "from the simulation. \n"
         "<table cellspacing=5 border=0\n>"
         "<tr><th>File Name</th><th>Variable</th><th>Description</th></tr>\n");

  HTTP_Write(request, message, strlen(message));

  for (list = filelist; list; list = list->next)
  {
    sprintf(message, 
            "<tr><td valign=top><A HREF=\"/Output/%s\">%s</A></td>"
            "<td valign=top>%s</td><td valign=top>%s</td></tr>",
            list->linkname, list->filename, 
            list->data.varname, list->data.description);
      HTTP_Write(request, message, strlen(message));
  }

  strcpy(message,"</table></center>");
  HTTP_Write(request, message, strlen(message));
  
  /* Write out the footer part. */

  retval = HTTP_Write(request, cactus_footer, strlen(cactus_footer));

  return retval;
}


 /*@@
   @routine    SendFilePage
   @date       Sun Sep 17 18:43:40 2000
   @author     Tom Goodale
   @desc 
   Sends an advertised file.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static int SendFilePage(cGH *cctkGH, httpRequest *request, void *data)
{
  int retval;
  char message[4098];
  struct httpuFileList *list;
  int filedes;

  for (list = filelist; list; list = list->next)
  {
    if(!strcmp(list->linkname, request->residual))
    {
      if((filedes = open(list->filename, O_RDONLY)) >= 0)
      {    
        strcpy(message,"HTTP/1.0 200 OK\r\n");
  
        HTTP_Write(request, message, strlen(message));
        
        /* Content-Type */
        sprintf(message,"Content-Type: %s\r\n\r\n", list->data.mimetype);
        
        HTTP_Write(request, message, strlen(message));

        HTTP_ContentSendFromFile(request, filedes);

        close(filedes);
      }
      else
      {
        strcpy(message,"HTTP/1.0 500 Server Internal Error\r\n");
        HTTP_Write(request, message, strlen(message));
        
        /* Content-Type */
        strcpy(message,"Content-Type: text/html\r\n\r\n");
        
        HTTP_Write(request, message, strlen(message));

        sprintf(message,
                "<HTML>\n<HEAD><TITLE>Error 500: Internal Error</TITLE></HEAD>\n"
                "<BODY><CENTER><p>Unable to open %s</P></BODY>\n</HTML>\n",
                list->filename);
        HTTP_Write(request, message, strlen(message));
      }
      break;
    }
  }

  if(!list)
  {
    strcpy(message,"HTTP/1.0 404 Not Found\r\n");
    HTTP_Write(request, message, strlen(message));
    
    /* Content-Type */
    strcpy(message,"Content-Type: text/html\r\n\r\n");
    
    HTTP_Write(request, message, strlen(message));
    
    sprintf(message,
            "<HTML>\n<HEAD><TITLE>Error 404: Not Found</TITLE></HEAD>\n"
            "<BODY><CENTER><p>%s does not exist</P></BODY>\n</HTML>\n",
            request->uri);
    HTTP_Write(request, message, strlen(message));
  } 

  return 0;
}