diff options
author | goodale <goodale@61ea717e-8e0c-4c3c-b38e-e9c67f54f1f1> | 2000-09-17 17:47:24 +0000 |
---|---|---|
committer | goodale <goodale@61ea717e-8e0c-4c3c-b38e-e9c67f54f1f1> | 2000-09-17 17:47:24 +0000 |
commit | 9ea2444dfdd284c3678c43825c30e9784ffd59ca (patch) | |
tree | 9bfe656b6ec948ea5cfb86156b1b861fdb3c53fa | |
parent | 34fb933cc4f64bf1ea040844c33b7271a944ce22 (diff) |
Added basic page for making advertised files available.
Tom
git-svn-id: http://svn.cactuscode.org/arrangements/CactusConnect/HTTPDExtra/trunk@6 61ea717e-8e0c-4c3c-b38e-e9c67f54f1f1
-rw-r--r-- | src/IO.c | 339 | ||||
-rw-r--r-- | src/Startup.c | 4 | ||||
-rw-r--r-- | src/make.code.defn | 2 |
3 files changed, 342 insertions, 3 deletions
diff --git a/src/IO.c b/src/IO.c new file mode 100644 index 0000000..ba79c63 --- /dev/null +++ b/src/IO.c @@ -0,0 +1,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; +} + + diff --git a/src/Startup.c b/src/Startup.c index 79d37c3..c07145b 100644 --- a/src/Startup.c +++ b/src/Startup.c @@ -16,8 +16,6 @@ #include "cctk_Arguments.h" #include "cctk_Parameters.h" -#include "DevThorns/httpd/src/httpd.h" - static char *rcsid = "$Header$"; CCTK_FILEVERSION(DevThorn_http_utils_Startup_c) @@ -42,6 +40,8 @@ int HTTPUTILS_Startup(void) HTTPUTILS_RegisterPages(); + HTTP_util_RegisterIOPages(); + return 0; } diff --git a/src/make.code.defn b/src/make.code.defn index 3869d45..60cd782 100644 --- a/src/make.code.defn +++ b/src/make.code.defn @@ -2,7 +2,7 @@ # $Header$ # Source files in this directory -SRCS = Groups.c Startup.c +SRCS = Groups.c Startup.c IO.c # Subdirectories containing source files SUBDIRS = |