From 89b88d27b20641e04cd1c2714c299d8908822b54 Mon Sep 17 00:00:00 2001 From: swhite Date: Tue, 6 Apr 2004 17:45:14 +0000 Subject: Regarding Cactus bug report 1632 "HTTPD contains buffer overflows" 1) Got rid of most strcat/sprintf into automatic array, replaced with a String module that allocates dynamic memory on the heap. 2) Went a long way toward initializing all variables. 3) Tested: Ran two copies with same parfile except different port, one with my changes, one with original. Went through different kinds of pages by hand, checked by eye. 4) Tried to make HTML XHTML 1.0-compliant. Checked with Amaya. One problem: How to deal with raw less-than characters, etc. Made a function to convert them to HTML Character Entities, but isn't clear this will work properly in the forms. So I left these symbols in the forms. 5) Also checked with more primitive browsers, lynx and dillo. 6) Marked a few instances of questionable code with 'SW' To do ----- Document a few new functions, esp. in Content.c git-svn-id: http://svn.cactuscode.org/arrangements/CactusConnect/HTTPD/trunk@187 1faa4e14-9dd3-4be0-9f0e-ffe519881164 --- src/Content.c | 971 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 507 insertions(+), 464 deletions(-) (limited to 'src/Content.c') diff --git a/src/Content.c b/src/Content.c index 2cf34a7..425b9f2 100644 --- a/src/Content.c +++ b/src/Content.c @@ -40,10 +40,13 @@ #include "cctk_Arguments.h" #include "cctk_Parameters.h" +#include "http_SString.h" + static const char *rcsid = "$Header$"; CCTK_FILEVERSION(CactusConnect_HTTPD_Content_c) +#define EMPTYSTRING {'\0'} /******************************************************************** ********************* Local Data Types *********************** ********************************************************************/ @@ -103,11 +106,10 @@ int HTTPi_RegisterParameterPages(void); struct httpLink *ContentLinks = NULL; static const char *notauthorized_page = -"\nError 401: Not Authorized\ -You are not authorized to access this page\n\n"; +"\nError 401: Not Authorized\n\n" +"\nYou are not authorized to access this page\n\n\n"; -#define USER_LENGTH 255 /******************************************************************** ********************* External Routines ********************** ********************************************************************/ @@ -212,12 +214,10 @@ int HTTP_ContentLink(const char *URL, const char *description, int flags) { - int retval; - struct httpLink *hlink; + int retval = -1; struct httpLink *last; struct httpLink *current; - - hlink = (struct httpLink *)malloc(sizeof(struct httpLink)); + struct httpLink *hlink = (struct httpLink *)malloc(sizeof(struct httpLink)); if(hlink) { @@ -243,10 +243,6 @@ int HTTP_ContentLink(const char *URL, retval = 0; } - else - { - retval = -1; - } return retval; } @@ -297,146 +293,211 @@ static int CompareStrings(const void *string1, const void *string2) @endhistory @@*/ +int +Send_HTTP( httpRequest * request, const char * message ) +{ + return HTTP_Write(request, message, strlen(message)); +} + +int +Send_HTTP_String( httpRequest *request, const String * message ) +{ + return Send_HTTP(request, GetBuffer( message ) ); +} + +void +SendHTTP_OK_Header( httpRequest *request ) +{ + /* Status message */ + Send_HTTP(request, "HTTP/1.0 200 OK\r\n"); + + /* Content-Type */ + Send_HTTP(request, "Content-Type: text/html\r\n"); + Send_HTTP(request, "\r\n"); +} + +void +SSUtil_SplitFilename(const char **dir,const char **file, const String *message) +{ + Util_SplitFilename((char **)dir,(char **)file,GetBuffer(message)); +} + +static void +TimeListItem( String *message, int time, const char *units ) +{ + ConcatCString(message, "
  • " ); + ConcatDecimal(message, time ); + ConcatCString(message, " " ); + ConcatCString(message, units ); + ConcatCString(message, "
  • \n" ); +} + +void +CCTK_GetRunTitleString( String *s ) +{ + char buf[1024] = EMPTYSTRING; + CCTK_RunTitle(sizeof(buf)-1,buf); + SetToCString( s, buf ); +} + +void +CCTK_GetParameterFilenameString( String *name ) +{ + char buf[1024] = EMPTYSTRING; + CCTK_ParameterFilename(sizeof(buf)-1,buf); + SetToCString( name, buf ); +} + static int MainPage(const cGH *cctkGH, httpRequest *request, void *data) { DECLARE_CCTK_PARAMETERS - int retval; - char message[10098]; - char title[4098]; - char menu[4098]; - char *dir; - char *file; - char *user; - struct httpLink *hlink; + int retval = -1; + String *message = String_New(); + String *title = String_New(); + String *menu = String_New(); + const char *dir = NULL; + const char *file = NULL; + char *user = NULL; + struct httpLink *hlink = NULL; int seconds,minutes,hours,days,weeks,months,years,millenia; - char host[HOSTLENGTH+1]; + char host[HOSTLENGTH+1] = EMPTYSTRING; /* 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)); + SendHTTP_OK_Header( request ); + SetHTML_Doctype( message ); + Send_HTTP_String(request, message); /* Start the page */ - strcpy(message,"Running CACTUS Status Information\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "\n\n"); + Send_HTTP(request, "Running CACTUS Status Information\n"); + SetHTML_HeadHeader( message); + Send_HTTP_String(request, message ); + Send_HTTP(request, "\n\n"); /* Write out the main header part */ /* LIST COMPILED THORNS */ { int i; - int nthorns; - const char **thorns; - - nthorns = CCTK_NumCompiledThorns(); - thorns = (const char **)malloc(nthorns * sizeof(char *)); - for(i=0; i < nthorns; i++) + int nthorns = CCTK_NumCompiledThorns(); + if( nthorns > 0 ) { - thorns[i] = CCTK_CompiledThorn (i); - } - qsort(thorns, nthorns, sizeof(char *), CompareStrings); - - strcpy(menu, "
    Active Thorns:\n"); - for(i=0; i < nthorns; i++) - { - if (CCTK_IsThornActive(thorns[i])) + const char **thorns = (const char **)malloc(nthorns * sizeof(char *)); + if( thorns != NULL ) { - sprintf(menu,"%s
    %s\n", - menu, thorns[i], thorns[i]); + for(i=0; i < nthorns; i++) + thorns[i] = CCTK_CompiledThorn (i); + + qsort(thorns, nthorns, sizeof(char *), CompareStrings); + + SetToCString( menu,"

    Active Thorns:

    \n"); + for(i=0; i < nthorns; i++) + if (CCTK_IsThornActive(thorns[i])) + { + ConcatCString( menu,""); + ConcatCString( menu, thorns[i]); + ConcatCString( menu,"
    \n"); + } + free(thorns); } } - free(thorns); } - HTTP_ContentHeader(cctkGH, 0, 10096, message, menu); - HTTP_Write(request, message, strlen(message)); + SetHTML_ContentHeader(cctkGH, 0, message, menu); + Send_HTTP_String(request, message ); - strcpy(message,"
    \n" - "\"Cactus\"
    \n"); + Send_HTTP(request, + "
    \n" + "\"Cactus\"
    \n"); - HTTP_Write(request, message, strlen(message)); - - CCTK_RunTitle(4098,title); + CCTK_GetRunTitleString(title); /* Some blurb */ - sprintf(message, "
    " - "
    \n" - "\n" - "\n" - "\n" - "\n" - "
    \n" - "

    %s

    \n" - "

    This browser is connected to a Cactus simulation which " - "contains a web server thorn. This thorn provides information " - " and control for the simulation.

    \n" - "\n" - "\n" - "
    \n" - "

    Before controlling any features of the simulation, users " - "must authenticate.

    \n" - "
    \n" - "
    \n",title); - - HTTP_Write(request, message, strlen(message)); - - strcpy(message, - "
    \n" - "\n" - "\n" - "\n\n
    \n"); - HTTP_Write(request, message, strlen(message)); + SetToCString(message, + "
    \n" + "\n" + "\n" + "\n" + "\n" + "
    \n" + "

    "); + Concat(message, title ); + ConcatCString(message, + "

    \n" + "

    This browser is connected to a Cactus simulation which \n" + "contains a web server thorn. This thorn provides information \n" + " and control for the simulation.

    \n" + "\n" + "\n" + "
    \n" + "

    Before controlling any features of the simulation, users \n" + "must authenticate.

    \n" + "
    \n" + "
    \n" + "
    \n"); + + Send_HTTP_String(request, message ); + + Send_HTTP(request, + "\n" + "\n" + "\n" - "\n" + "\n" - "\n" - "
    \n"); /* AVAILABLE OPTIONS */ if(ContentLinks) { - strcpy(message, - "

    Available options:

    \n" - "
    \n"); + SetToCString(message, + "

    Available options:

    \n" + "
    \n"); for(hlink = ContentLinks; hlink; hlink=hlink->next) { - sprintf(message, - "%s
    %s
    %s \n", - message, hlink->URL, hlink->name, hlink->description); - + ConcatCString(message, "
    URL ); + ConcatCString(message, "\">" ); + ConcatCString(message, hlink->name ); + ConcatCString(message, "
    \n
    " ); + ConcatCString(message, hlink->description ); + ConcatCString(message, "
    \n" ); } - sprintf(message,"%s
    \n",message); - HTTP_Write(request, message, strlen(message)); + ConcatCString(message, "
    \n" ); + Send_HTTP_String(request, message ); } - strcpy(message, - "
    \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, + "\n"); /* CONFIGURATION DETAILS */ - sprintf(message, + SetToCString(message, "

    Simulation:

    \n" "
      \n" - "
    • Flesh version %s
    • \n" - "
    • Flesh compiled on %s" - " at %s
    • \n", - CCTK_FullVersion(),CCTK_CompileDate(),CCTK_CompileTime()); - - HTTP_Write(request, message, strlen(message)); + "
    • Flesh version "); + ConcatCString(message, CCTK_FullVersion() ); + ConcatCString(message, + "
    • \n" + "
    • Flesh compiled on "); + ConcatCString(message, CCTK_CompileDate() ); + ConcatCString(message, + "\n" + " at "); + ConcatCString(message, CCTK_CompileTime() ); + ConcatCString(message, + "
    • \n"); + + Send_HTTP_String(request, message ); seconds = CCTK_RunTime(); minutes = seconds/60; @@ -454,66 +515,59 @@ static int MainPage(const cGH *cctkGH, httpRequest *request, void *data) millenia=years/1000; years=years-millenia*1000; - strcpy(message,"
    • Time since start up\n
        "); + SetToCString(message, "
      • Time since start up\n
          "); if (millenia) { - sprintf(message, - "%s
        • %d millenia\n",message,millenia); + TimeListItem( message, millenia, "millenia" ); } if (years) { - sprintf(message, - "%s
        • %d years\n",message,years); + TimeListItem( message, years, "years" ); } if (months) { - sprintf(message, - "%s
        • %d months\n",message,months); + TimeListItem( message, months, "months" ); } if (weeks) { - sprintf(message, - "%s
        • %d weeks\n",message,weeks); + TimeListItem( message, weeks, "weeks" ); } if (days) { - sprintf(message, - "%s
        • %d days\n",message,days); + TimeListItem( message, days, "days" ); } if (hours) { - sprintf(message, - "%s
        • %d hours\n",message,hours); + TimeListItem( message, hours, "hours" ); } if (minutes) { - sprintf(message, - "%s
        • %d minutes\n",message,minutes); + TimeListItem( message, minutes, "minutes" ); } if (seconds) { - sprintf(message, - "%s
        • %d seconds\n",message,seconds); + TimeListItem( message, seconds, "seconds" ); } - strcat(message,"
      • Parameter filename "); - HTTP_Write(request, message, strlen(message)); + ConcatCString(message, "
    • \n
    • Parameter filename " ); + Send_HTTP_String(request, message ); - CCTK_ParameterFilename(4098,message); - Util_SplitFilename(&dir,&file,message); - HTTP_Write(request, file, strlen(file)); + CCTK_GetParameterFilenameString(message); + SSUtil_SplitFilename(&dir,&file,message); + Send_HTTP(request, file); - strcpy(message,"\n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "
    • \n"); if (cctkGH && cctkGH->cctk_iteration) { - strcpy(message,"
    • Estimated time per iteration:
        "); - sprintf(message,"%s
      • %f seconds
      ", - message,CCTK_RunTime()/(double)cctkGH->cctk_iteration); - HTTP_Write(request, message, strlen(message)); + SetToCString(message, "
    • Estimated time per iteration:
        \n"); + ConcatCString(message, "
      • "); + ConcatDouble(message, CCTK_RunTime()/(double)cctkGH->cctk_iteration ); + ConcatCString(message, " seconds
    • \n"); - strcpy(message,"
    • Estimated time to completion:
        "); + Send_HTTP_String(request, message ); + + SetToCString(message, "
      • Estimated time to completion:\n
          "); if (cctk_final_time%d millenia\n",message,millenia); + TimeListItem( message, millenia, "millenia" ); } if (years) { - sprintf(message, - "%s
        • %d years\n",message,years); + TimeListItem( message, years, "years" ); } if (months) { - sprintf(message, - "%s
        • %d months\n",message,months); + TimeListItem( message, months, "months" ); } if (weeks) { - sprintf(message, - "%s
        • %d weeks\n",message,weeks); + TimeListItem( message, months, "months" ); } if (days) { - sprintf(message, - "%s
        • %d days\n",message,days); + TimeListItem( message, days, "days" ); } if (hours) { - sprintf(message, - "%s
        • %d hours\n",message,hours); + TimeListItem( message, hours, "hours" ); } if (minutes) { - sprintf(message, - "%s
        • %d minutes\n",message,minutes); + TimeListItem( message, minutes, "minutes" ); } if (seconds) { - sprintf(message, - "%s
        • %d seconds\n",message,seconds); + TimeListItem( message, seconds, "seconds" ); } - strcat(message,"
        "); + ConcatCString(message, "
    • \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message ); } Util_GetHostName(host,HOSTLENGTH); @@ -593,41 +639,44 @@ static int MainPage(const cGH *cctkGH, httpRequest *request, void *data) if (CCTK_nProcs(cctkGH) == 1) { - sprintf(message, - "
    • Single processor run
    • \n" - "
    • Running on %s
    • \n",host); + SetToCString(message, "
    • Single processor run
    • \n" + "
    • Running on " ); + ConcatCString(message, host); + ConcatCString(message, "
    • \n"); } else { - sprintf(message, - "
    • Multiprocessor run on %d CPUs
    • \n" - "
    • Processor 0 running on %s
    • \n", - CCTK_nProcs(cctkGH),host); + SetToCString(message, "
    • Multiprocessor run on " ); + ConcatDecimal(message, CCTK_nProcs(cctkGH)); + ConcatCString(message, " CPUs
    • \n" + "
    • Processor 0 running on "); + ConcatCString(message, host); + ConcatCString(message, "
    • \n"); } - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message ); user = getenv ("USER"); if (user) { - sprintf(message, "
    • Started by %s
    • \n",user); + SetToCString(message, "
    • Started by "); + ConcatCString(message, user); + ConcatCString(message, "
    • \n"); + Send_HTTP_String(request, message ); } - HTTP_Write(request, message, strlen(message)); - strcpy(message,"
    "); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "\n"); /* Finish table started by blurb */ - strcpy(message, - "
    \n" - "\n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "
    \n\n"); /* Write out the footer part. */ - HTTP_ContentFooter(cctkGH, 0, 4096, message); - retval = HTTP_Write(request, message, strlen(message)); + SetHTML_ContentFooter(cctkGH, 0, message); + retval = Send_HTTP_String(request, message ); + + String_Delete( message ); + String_Delete( title ); + String_Delete( menu ); return retval; } @@ -657,10 +706,9 @@ static int MainPage(const cGH *cctkGH, httpRequest *request, void *data) @@*/ static int RegisterImages(void) { - struct httpStaticPage *image; - struct httpStaticPage *image2; - - image = (struct httpStaticPage *)malloc(sizeof(struct httpStaticPage)); + struct httpStaticPage *image2 = NULL; + struct httpStaticPage *image + = (struct httpStaticPage *)malloc(sizeof(struct httpStaticPage)); if(image) { @@ -709,37 +757,35 @@ static int RegisterImages(void) @@*/ static int ShowStaticPage(const cGH *cctkGH, httpRequest *request, void *data) { - int retval; - char message[1024]; - struct httpStaticPage *page; + int retval = -1; /* avoid compiler warning about unused parameter */ cctkGH = cctkGH; if(data) { - page = (struct httpStaticPage *)data; + struct httpStaticPage *page = (struct httpStaticPage *)data; + String *message = String_New(); - strcpy(message,"HTTP/1.0 200 OK\r\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "HTTP/1.0 200 OK\r\n"); - sprintf(message,"Content-Length: %d\r\nContent-Type: %s\r\n\r\n", page->length, page->mime_type); + SetToCString(message, "Content-Length: "); + ConcatDecimal(message, page->length ); + ConcatCString(message, "\r\nContent-Type: "); + ConcatCString(message, page->mime_type ); + ConcatCString(message, "\r\n\r\n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); retval = HTTP_Write(request, page->page, page->length); - } - else - { - retval = -1; - } + String_Delete( message ); + } return retval; } - +#define USER_LENGTH 255 /*@@ @routine ControlPage @date Sun Sep 17 14:37:59 2000 @@ -758,142 +804,155 @@ static int ControlPage(const cGH *cctkGH, httpRequest *request, void *data) { DECLARE_CCTK_PARAMETERS - char message[4098]; - - int notauthorised; + int notauthorised = 0; + char thisuser[USER_LENGTH+1] = EMPTYSTRING; - char thisuser[USER_LENGTH+1]; + data = data; /* avoid compiler warning about unused parameter */ - /* avoid compiler warning about unused parameter */ - data = data; - - notauthorised = HTTP_AuthenticateBasic(request, "user", thisuser, USER_LENGTH); + notauthorised = HTTP_AuthenticateBasic(request, "user", thisuser, + sizeof(thisuser)); if(!notauthorised) { /* Ok the person is authorised. */ if(request->n_arguments == 0) { + String *message = String_New(); /* No arguments, so just display the page */ - strcpy(message,"HTTP/1.0 200 Ok\r\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"HTTP/1.0 200 Ok\r\n"); - strcpy(message,"WWW-Authenticate: Basic realm=\"Cactus Control\"\r\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"WWW-Authenticate: Basic realm=\"Cactus Control\"\r\n"); HTTP_CookieSend(request,"user", thisuser, "/", NULL, NULL, 0); - strcpy(message,"Content-Type: text/html\r\n\r\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"Content-Type: text/html\r\n\r\n"); + SetHTML_Doctype( message ); + Send_HTTP_String(request, message); /* Start the page */ - strcpy(message, "Cactus Control and Status Page\n"); - - HTTP_Write(request, message, strlen(message)); - HTTP_ContentHeader(cctkGH, 0,4098,message,NULL); + Send_HTTP(request, "\n"); + Send_HTTP(request, "Cactus Control and Status Page\n"); - strcat(message, "

    Control and Status Page

    "); + SetHTML_HeadHeader( message); + Send_HTTP_String(request, message ); - HTTP_Write(request, message, strlen(message)); - - strcpy(message, - "

    This page is the control center for interacting with" - " the current simulation. It is possible to steer certain" - " parameters, as well as pause, restart, or terminate the" - " simulation.

    "); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "\n\n"); + + SetHTML_ContentHeader(cctkGH, 0,message,NULL); - strcpy(message, - "
    \n" - "
    \n"); + ConcatCString(message, "

    Control and Status Page

    \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); - strcpy(message, - "

    Run Control

    \n" - "

    Select if the run should be paused, running normally, or terminated." - " You may also single step to the next iteration.

    "); + Send_HTTP(request, + "

    This page is the control center for interacting with\n" + " the current simulation. It is possible to steer certain\n" + " parameters, as well as pause, restart, or terminate the" + " simulation.

    \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, + "
    \n" + "\n"); - strcpy(message, - "\n" - "\n"); + Send_HTTP(request, + "

    Run Control

    \n" + "

    Select if the run should be paused, running normally, " + "or terminated.\n" + " You may also single step to the next iteration.

    \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, + "
    \n" + "\n"); - sprintf(message, - "\n", - pause ? "checked" : ""); - HTTP_Write(request, message, strlen(message)); + SetToCString(message, + "\n"); + Send_HTTP_String(request, message); - sprintf(message, - "\n", - pause ? "" : "checked"); - HTTP_Write(request, message, strlen(message)); + SetToCString(message, + "\n"); + Send_HTTP_String(request, message); - strcpy(message, - "\n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, + "\n"); - strcpy(message, - "\n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, + "\n"); - strcpy(message, - "
    PAUSE PAUSE RUN RUN TERMINATE TERMINATE
    \n" - "\n" - "\n" - "\n" - "
    \n"); - - HTTP_Write(request, message, strlen(message)); - - strcpy(message, - "

    Run Until

    \n" - "

    The following parameters allow you to select an iteration number" - " or physical time at which the code will pause.\n" - " You may also choose to pause if a particular expression made up" - " of grid scalars, simulation time and iteration is true. \n" - " Note that even if 'run' is selected above, the settings here have" - " precedence.

    "); - - HTTP_Write(request, message, strlen(message)); - - strcpy(message, - "\n"); - - sprintf(message,"%s" - "" - "\n" - "\n", message, until_it, until_it_active ? "checked" : ""); - sprintf(message,"%s" - "" - "\n" - "\n", message, until_time, until_time_active ? "checked" : ""); - sprintf(message,"%s" - "" - "\n" - "\n", message, until_expression, until_expression_active ? "checked" : ""); - - strcat(message,"
    Iteration
    Time
    Expression
    \n"); + Send_HTTP(request, + "\n" + "\n" + "\n" + "\n" + "\n" + "
    \n"); + + Send_HTTP(request, + "

    Run Until

    \n" + "

    The following parameters allow you to select an iteration\n" + "number or physical time at which the code will pause.\n" + " You may also choose to pause if a particular expression made up\n" + " of grid scalars, simulation time and iteration is true. \n" + " Note that even if 'run' is selected above, the settings here have" + " precedence.

    \n"); + + SetToCString(message, + "\n"); + + ConcatCString(message,"" + "\n" + "\n" + "\n"); + ConcatCString(message, + "" + "\n" + "\n" + "\n"); + ConcatCString(message, + "\n" + "\n" + "\n" + "\n"); + + ConcatCString(message,"
    Iteration
    Time
    Expression
    \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); - strcpy(message, - "\n" - "
    \n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, + "\n" + "\n"); /* Write out the footer part. */ - HTTP_ContentFooter(cctkGH, 0, 4098, message); - HTTP_Write(request, message, strlen(message)); + SetHTML_ContentFooter(cctkGH, 0, message); + Send_HTTP_String(request, message); + + String_Delete( message ); } else { @@ -904,24 +963,17 @@ static int ControlPage(const cGH *cctkGH, httpRequest *request, void *data) else { /* Not authorised */ - strcpy(message,"HTTP/1.0 401 Unauthorized\r\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"HTTP/1.0 401 Unauthorized\r\n"); - strcpy(message,"WWW-Authenticate: Basic realm=\"Cactus Control\"\r\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"WWW-Authenticate: Basic realm=\"Cactus Control\"\r\n"); HTTP_CookieCancel(request,"user", "/"); - strcpy(message,"Content-Type: text/html\r\n\r\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"Content-Type: text/html\r\n\r\n"); - HTTP_Write(request, notauthorized_page, strlen(notauthorized_page)); + Send_HTTP(request, notauthorized_page); } - return 0; } @@ -942,8 +994,8 @@ static int ControlPage(const cGH *cctkGH, httpRequest *request, void *data) static int ControlSet(const cGH *cctkGH, httpRequest *request) { DECLARE_CCTK_PARAMETERS - char message[4098]; - const char *value; + String *message = String_New(); + const char *value = NULL; /* What is the runstate ? */ value = HTTP_ArgumentValue(request,"runstate"); @@ -1074,22 +1126,27 @@ static int ControlSet(const cGH *cctkGH, httpRequest *request) /**************************************************************************** **************** Now redirect the browser to the normal page *************** */ + /* SW: I don't think this is working right. On my browsers, it just + * displays the ControlTerminationPage, with the following lines + * of text rendered at the bottom. */ /* Status message */ if(request->http_major_version < 1 || (request->http_major_version == 1 && request->http_minor_version < 1)) { /* Older browsers don't understand 303 */ - strcpy(message,"HTTP/1.0 302 Found\r\n"); + SetToCString(message,"HTTP/1.0 302 Found\r\n"); } else { - strcpy(message,"HTTP/1.0 303 See Other\r\n"); + SetToCString(message,"HTTP/1.0 303 See Other\r\n"); } - sprintf(message, "%sLocation: /control.html\r\n\r\n", message); + ConcatCString(message, "Location: /control.html\r\n\r\n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); + + String_Delete( message ); return 0; } @@ -1110,121 +1167,109 @@ static int ControlSet(const cGH *cctkGH, httpRequest *request) @@*/ static int ControlTerminationPage(const cGH *cctkGH, httpRequest *request) { - int retval; - char message[4098]; - - /* 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)); + int retval = -1; + String *message = String_New(); + SendHTTP_OK_Header( request ); + SetHTML_Doctype( message ); + Send_HTTP_String(request, message); /* Start the page */ - strcpy(message,"Running CACTUS Status Information : Terminated\n"); + Send_HTTP(request, "\n"); + Send_HTTP(request, "Running CACTUS Status Information : Terminated\n"); - HTTP_Write(request, message, strlen(message)); + SetHTML_HeadHeader( message); + Send_HTTP_String(request, message ); - /* Write out the main header part */ - HTTP_ContentHeader(cctkGH,1,4098,message,NULL); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "\n\n"); - strcpy(message, "

    Simulation Home Page

    "); + /* Write out the main header part */ + SetHTML_ContentHeader(cctkGH,1,message,NULL); + Send_HTTP_String(request, message); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "

    Simulation Home Page

    \n"); /* Some blurb */ - strcpy(message, "
    " - "
    " - "

    Simulation web server:

    " - "

    This browser is connected to a Cactus simulation which contains " - "a web server thorn. This thorn allows you to monitor the simulation, " - "and view and change parameters

    " - "

    Depending on which other thorns are active, there may be additional " - "features available, such as the viewing and downloading of output files

    "); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, + "
    \n" + "
    \n" + "

    Simulation web server:

    \n" + "

    This browser is connected to a Cactus simulation which \n" + "contains a web server thorn. This thorn allows you to monitor \n" + "the simulation,\n" + "and view and change parameters

    \n" + "

    Depending on which other thorns are active, there may be \n" + "additional features available, such as the viewing and \n" + "downloading of output files

    \n"); /* CONFIGURATION DETAILS */ - sprintf(message, "

    Simulation:

    " - "
    • Flesh version %s" - "
    • \n" - "
    • Flesh compiled on " - __DATE__ " at "__TIME__ "
    • \n" - ,CCTK_FullVersion()); + SetToCString(message, + "

      Simulation:

      \n" + "
      • Flesh version "); + ConcatCString(message, CCTK_FullVersion() ); + ConcatCString(message, + "
      • \n" + "
      • Flesh compiled on __DATE__ \n" + "at __TIME__
      • \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); if (cctkGH) { if (CCTK_nProcs(cctkGH) == 1) { - strcpy(message,"
      • Single processor run
      • "); + SetToCString(message,"
      • Single processor run
      • \n"); } else { - sprintf(message,"
      • Multiprocessor run on %d CPUs
      • ", - CCTK_nProcs(cctkGH)); + SetToCString(message,"
      • Multiprocessor run on "); + ConcatDecimal( message, CCTK_nProcs(cctkGH) ); + ConcatCString( message, " CPUs
      • \n"); } - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); } - strcpy(message,"
      "); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"
    \n"); - /******************************************************************************/ + /************************************************************************/ /* NEW COLUMN */ - strcpy(message, "
    "); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, ""); - /*******************************************************************************/ + /************************************************************************/ /* CURRENT STATE OF SIMULATION */ if (cctkGH) { - sprintf(message, "

    Current state:

    " - "
    • Physical time %f" - "\n" - "
    • Iteration number %d" - "
    • \n", - cctkGH->cctk_time, - cctkGH->cctk_iteration); + SetToCString(message, + "

      Current state:

      \n" + "
      • Physical time "); + ConcatDouble(message, cctkGH->cctk_time ); + ConcatCString(message, + "
      • \n" + "
      • Iteration number "); + ConcatDecimal(message, cctkGH->cctk_iteration ); + ConcatCString(message, "
      • \n"); } else { - strcpy(message, "
      • Current cactus state is unknown
      • \n"); + SetToCString(message, "
      • Current cactus state is unknown
      • \n"); } - HTTP_Write(request, message, strlen(message)); - - strcpy(message, "
      • This Cactus run is over.
      • \n"); - - HTTP_Write(request, message, strlen(message)); - - strcpy(message, "
      "); + Send_HTTP_String(request, message); + Send_HTTP(request, "
    • This Cactus run is over.
    • \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "
    "); /* LIST COMPILED THORNS */ - { int i; - int nthorns; - const char **thorns; - - nthorns = CCTK_NumCompiledThorns(); - - thorns = (const char **)malloc(nthorns * sizeof(char *)); + int nthorns = CCTK_NumCompiledThorns(); + const char **thorns = (const char **)malloc(nthorns * sizeof(char *)); for(i=0; i < nthorns; i++) { @@ -1234,42 +1279,45 @@ static int ControlTerminationPage(const cGH *cctkGH, httpRequest *request) /* Sort the thorns */ qsort(thorns, nthorns, sizeof(char *), CompareStrings); - strcpy(message, "

    Compiled thorns:

    " - "

    This list shows all thorns compiled into the executable, " - "those thorns which have been activated in the parameter " - "file for this simulation are shown in " - "red

    " - "
      \n"); + SetToCString(message, + "

      Compiled thorns:

      \n" + "

      This list shows all thorns compiled into the executable; those \n" + "thorns which have been activated in the parameter file for this \n" + "simulation are shown in red

      \n" + "
        \n"); for(i=0; i < nthorns; i++) { - strcat(message, "
      • "); + ConcatCString(message, "
      • "); if (CCTK_IsThornActive(thorns[i])) { - sprintf(message,"%s%s\n", message, thorns[i]); + ConcatCString(message, " "); + ConcatCString(message, thorns[i]); + ConcatCString(message, " "); } else { - sprintf(message, "%s%s\n", message, thorns[i]); + ConcatCString(message, thorns[i]); } + ConcatCString(message, "
      • \n"); } - strcat(message, "
      \n"); + ConcatCString(message, "
    \n"); free(thorns); } - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); /* Finish table started by blurb */ - strcpy(message, "
    "); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "
    \n"); /* Write out the footer part. */ - HTTP_ContentFooter(cctkGH,0,4098,message); - retval = HTTP_Write(request, message, strlen(message)); + SetHTML_ContentFooter(cctkGH,0,message); + retval = Send_HTTP_String(request, message); + + String_Delete( message ); return retval; } @@ -1289,12 +1337,12 @@ static int ControlTerminationPage(const cGH *cctkGH, httpRequest *request) @@*/ int HTTP_ContentSendFromFile(httpRequest *request, int filedes) { - int bytes_sent; - int n_bytes; - char buffer[4098]; + int bytes_sent = 0; + int n_bytes = 0; + char buffer[4098] = EMPTYSTRING; bytes_sent = 0; - while((n_bytes = read(filedes, buffer,4098)) > 0) + while((n_bytes = read(filedes, buffer,sizeof(buffer))) > 0) { HTTP_Write(request, buffer, n_bytes); bytes_sent += n_bytes; @@ -1309,7 +1357,6 @@ int HTTP_ContentSendFromFile(httpRequest *request, int filedes) } - /*@@ @routine AboutPage @date Sun Sep 17 2000 @@ -1322,60 +1369,54 @@ int HTTP_ContentSendFromFile(httpRequest *request, int filedes) @@*/ static int AboutPage(const cGH *cctkGH, httpRequest *request, void *data) { - int retval; - char message[4098]; + int retval = -1; + String *message = String_New(); /* 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)); + SendHTTP_OK_Header( request ); + SetHTML_Doctype( message ); + Send_HTTP_String(request, message); /* Start the page */ - strcpy(message, "About Cactus Server\n"); - HTTP_ContentHeader(cctkGH,0,4098,message,NULL); - strcat(message,""); - HTTP_Write(request, message, strlen(message)); + SetToCString(message, "\nAbout Cactus Server\n"); + SetHTML_HeadHeader( message); + Send_HTTP_String(request, message ); - strcpy(message, "
    "); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "\n\n"); + SetHTML_ContentHeader(cctkGH,0,message,NULL); + Send_HTTP_String(request, message); - strcpy(message, "

    About this Web Server

    "); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "

    About this Web Server

    \n"); - strcpy(message, "

    These web pages are served by a simulation " - "which is using the Cactus Code and Computational ToolKit, " + SetToCString(message, "

    These web pages are served by a simulation \n" + "which is using the Cactus Code and Computational ToolKit, \n" "a freely available, parallel, collaborative," - " portable and " - "modular programming environment for HPC.

    " - "

    The HTTPD module, or thorn " - "which is serving these pages" - " can be added to any Cactus application to provide on-line " - "monitoring and control of simulations from any web browser.

    " - "

    This HTTPD server and thorn interface has been designed and " - "and implemented by Tom Goodale, based on the original idea and " - "implementation by Werner Benger.

    "); + " portable and \n" + "modular programming environment for HPC.

    \n" + "

    The HTTPD module, or thorn " + "which is serving these pages\n" + " can be added to any Cactus application to provide on-line \n" + "monitoring and control of simulations from any web browser.

    \n" + "

    This HTTPD server and thorn interface has been designed and \n" + "and implemented by Tom Goodale, based on the original idea and \n" + "implementation by Werner Benger.

    \n"); - retval = HTTP_Write(request, message, strlen(message)); + retval = Send_HTTP_String(request, message); - strcpy(message, "

    For more information about Cactus, visit our " - "permanent home page at " - "www.cactuscode.org

    "); + SetToCString(message, "

    For more information about Cactus, visit our " + "permanent home page at \n" + "www.cactuscode.org

    \n"); - retval = HTTP_Write(request, message, strlen(message)); + retval = Send_HTTP_String(request, message); /* Write out the footer part. */ - HTTP_ContentFooter(cctkGH,0,4098,message); - retval = HTTP_Write(request, message, strlen(message)); + SetHTML_ContentFooter(cctkGH,0,message); + retval = Send_HTTP_String(request, message); + String_Delete( message); return retval; } @@ -1397,18 +1438,16 @@ static int AboutPage(const cGH *cctkGH, httpRequest *request, void *data) @@*/ static int CookieTestPage(const cGH *cctkGH, httpRequest *request, void *data) { - int retval; - char message[4098]; - const char *value; - char *value2; + int retval = -1; + String *message = String_New(); + const char *value = NULL; + char *value2 = NULL; /* 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)); + Send_HTTP(request,"HTTP/1.0 200 OK\r\n"); /* Cookie */ HTTP_CookieSend(request, "user1", "foobar4", NULL,NULL,NULL,0); @@ -1419,45 +1458,49 @@ static int CookieTestPage(const cGH *cctkGH, httpRequest *request, void *data) HTTP_CookieSend(request, "user4", "foobar1", NULL,NULL,NULL,0); - strcpy(message,"Content-Type: text/html\r\n\r\n"); - - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"Content-Type: text/html\r\n\r\n"); + SetHTML_Doctype( message ); + Send_HTTP_String(request, message); /* Start the page */ - strcpy(message, "Cookie Test\n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "Cookie Test\n"); + SetHTML_HeadHeader( message); + Send_HTTP_String(request, message ); - HTTP_ContentHeader(cctkGH,0,4098,message,NULL); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "\n\n"); - strcat(message, "

    Cookie Test

    "); + SetHTML_ContentHeader(cctkGH,0,message,NULL); + Send_HTTP_String(request, message); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "

    Cookie Test

    \n"); - strcpy(message, "
    "); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request, "
    "); value = HTTP_HeaderValue(request, "Cookie"); - sprintf(message, "

    Cookie was '%s'

    \n", value); + SetToCString(message, "

    Cookie was '"); + ConcatCString(message, value); + ConcatCString(message, "'

    \n"); - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); value2 = HTTP_CookieGet(request,"user3"); - sprintf(message, "

    Cookie from decoder was '%s'

    \n", value2); + SetToCString(message, "

    Cookie from decoder was '"); + ConcatCString(message, value2); + ConcatCString(message, "'

    \n"); free(value2); - HTTP_Write(request, message, strlen(message)); + Send_HTTP_String(request, message); - strcpy(message,"
    "); - HTTP_Write(request, message, strlen(message)); + Send_HTTP(request,"\n"); /* Write out the footer part. */ - HTTP_ContentFooter(cctkGH,0,4098,message); - retval = HTTP_Write(request, message, strlen(message)); + SetHTML_ContentFooter(cctkGH,0,message); + retval = Send_HTTP_String(request, message); + String_Delete( message ); return retval; } -- cgit v1.2.3