aboutsummaryrefslogtreecommitdiff
path: root/src/IO.c
blob: 784a32789717c41c0ee8353f295751ee4d2e335e (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
 /*@@
   @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   $Id$
 @@*/

#include "cctk.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "util_String.h"

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

#include "CactusConnect/HTTPD/src/http_Request.h"
#include "CactusConnect/HTTPD/src/http_Content.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(CactusConnect_HTTPDExtra_IO_c)


/********************************************************************
 ********************    Macro Definitions   ************************
 ********************************************************************/
#ifndef O_BINARY
#define O_BINARY 0
#endif


/********************************************************************
 *********************     Internal Typedefs   **********************
 ********************************************************************/
struct httpuFileList
{
  struct httpuFileList *next;
  char *filename;
  char *linkname;
  ioAdvertisedFileDesc data;
};

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


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
static int IOFileListener(const cGH *GH, const char *filename,
                          const ioAdvertisedFileDesc *description);
static int AdvertisedFilePage(const cGH *GH, httpRequest *request, void *data);
static int ViewportFilePage(const cGH *GH, httpRequest *request, void *data);
static int SendFilePage(const cGH *GH, httpRequest *request, void *data);


/********************************************************************
 ********************    External Routines   ************************
 ********************************************************************/
int HTTP_util_RegisterIOPages(void);


/********************************************************************
 *********************     Local Data   *****************************
 ********************************************************************/
static struct httpuFileList *filelist   = NULL;
static struct httpuMimeType *mimetypes  = NULL;


 /*@@
   @routine    HTTP_util_RegisterIOPages
   @date       Wed Sep 14 11:29:43 2000
   @author     Gabrielle Allen
   @desc
               Registers HTTPDExtra's Output and ViewPort page.
   @enddesc
   @calls      IOUtil_RegisterAdvertisedFileListener
               HTTP_RegisterPage
               HTTP_ContentLink

   @returntype int
   @returndesc
               0 for success
   @endreturndesc
@@*/
int HTTP_util_RegisterIOPages(void)
{
  ioAdvertisedFileListenerCallbacks listener;

  listener.advertise = IOFileListener;

  IOUtil_RegisterAdvertisedFileListener (NULL, CCTK_THORNSTRING,
                                         &listener);

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

  HTTP_ContentLink("/Output/index.html", "Files",
                   "Downloadable files",
                   HTTP_QUICKLINK);
  HTTP_ContentLink("/Output/viewport.html", "Viewport",
                   "Viewport for certain output files",
                   HTTP_QUICKLINK);

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

  return 0;
}


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
 /*@@
   @routine    IOFileListener
   @date       Sun Sep 17 17:56:22 2000
   @author     Tom Goodale
   @desc
               Listener for advertised files.
   @enddesc
@@*/
static int IOFileListener(const cGH *GH, const char *filename,
                          const ioAdvertisedFileDesc *description)
{
  struct httpuFileList *entry;
  struct httpuMimeType *type;
  char *position;


  /* avoid compiler warning about unused parameter */
  (void) (GH + 0);

  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;
}


 /*@@
   @routine    AdvertisedFilePage
   @date       Sun Sep 17 18:26:01 2000
   @author     Tom Goodale
   @desc
               Page to deal with advertised files.
   @enddesc
@@*/
static int AdvertisedFilePage(const cGH *GH, httpRequest *request, void *data)
{
  int retval;
  char message[4098];
  struct httpuFileList *list;


  /* avoid compiler warning about unused parameter */
  data = data;

  /* 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");

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

  /* HTTP_Write out the header part. */

  HTTP_ContentHeader(GH, 0, 4098, message,NULL);

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

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

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

  strcpy(message,
         "<p>From this page you can download various output files \n"
         "from the simulation. Depending on the software available on your"
	 " local machine, you can change the browser properties to launch"
	 " files directly to visualization clients.</p> \n "
	 "<p>Some of these output files can be directly viewed on "
	 "a browser on the simulation "
	 "<a href=\"/Output/viewport.html\">viewport</a></p>"
	 "<p>Many IO methods have <i>steerable</i> parameters which "
	 "allow you to e.g. add fields and customise behaviour."
	 "Depending on your authorisation, you can access the"
	 " <a href=\"/Parameters/index.html\">parameter steering page</a></p>"
	 "<center>"
         "<table cellspacing=5 cellpadding=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>\n",
            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. */

  /* HTTP_Write out the footer part. */

  HTTP_ContentFooter(GH, 0, 4098, message);

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

  return retval;
}


 /*@@
   @routine    SendFilePage
   @date       Sun Sep 17 18:43:40 2000
   @author     Tom Goodale
   @desc
               Sends an advertised file.
   @enddesc
@@*/
static int SendFilePage(const cGH *GH, httpRequest *request, void *data)
{
  char message[4098];
  struct httpuFileList *list;
  int filedes;


  /* avoid compiler warning about unused parameters */
  (void) (GH + 0);
  data = data;

  for (list = filelist; list; list = list->next)
  {
    if(!strcmp(list->linkname, request->residual))
    {
      if((filedes = open(list->filename, O_RDONLY | O_BINARY)) >= 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;
}


 /*@@
   @routine    AdvertisedFilePage
   @date       Sun Sep 17 18:26:01 2000
   @author     Tom Goodale
   @desc
               Page to deal with advertised files.
   @enddesc
@@*/
static int ViewportFilePage(const cGH *GH, httpRequest *request, void *data)
{
  int retval;
  int foundone;
  char message[4098];
  struct httpuFileList *list;


  /* avoid compiler warning about unused parameters */
  (void) (GH + 0);
  data = data;

  /* 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");

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

  /* HTTP_Write out the header part. */

  HTTP_ContentHeader(GH, 0, 4098, message,NULL);

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

  strcpy(message, "<center><h1>Viewport</h1></center>");

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

  strcpy(message,
	 "<p>This page displays certain types of the output files "
	 "from the <a href=\"/Output/index.html\">download</a> page"
	 " as images (currently only jpegs [mime type image/jpeg]).</p>"
	 "<p>Many IO methods have <i>steerable</i> parameters which "
	 "allow you to e.g. add fields and customise behaviour."
	 "Depending on your authorisation, you can access the"
	 " <a href=\"/Parameters/index.html\">parameter steering page</a></p>");

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

  foundone = 0;
  for (list = filelist; list; list = list->next)
  {
    if (CCTK_Equals(list->data.mimetype,"image/jpeg"))
    {
      if (!foundone)
      {
	strcpy(message,
	       "<center>"
	       "<table cellspacing=5 cellpadding=5 border=0\n>"
	       "<tr><th>Variable<br>File Name</th>\n"
	       "<th>Description</th><th>Image</th></tr>\n");
	HTTP_Write(request, message, strlen(message));
	foundone = 1;
      }
      sprintf(message,
	      "<tr>\n"
	      "<td valign=center><small>%s<br>\n"
	      "<A HREF=\"/Output/%s\">%s</A>\n"
	      "</small></td>\n"
	      "<td valign=center>%s</td>\n"
	      "<td valign=center><A HREF=\"/Output/%s\"><img border=0 width=100 height=100 src=\"%s\"></A></td>\n"
	      "</tr>\n",
	      list->data.varname,list->linkname, list->filename,
	      list->data.description,list->linkname,list->linkname);
      HTTP_Write(request, message, strlen(message));
    }
  }

  if (!foundone)
  {
    strcpy(message,"<center><b><p>No viewable images registered!</p>"
	   "<b><center>\n");
    HTTP_Write(request, message, strlen(message));
  }


  strcpy(message,"</table>\n</center>\n");
  HTTP_Write(request, message, strlen(message));

  /* Write out the footer part. */

  HTTP_ContentFooter(GH, 0, 4098, message);

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

  return retval;
}