/*@@ @file Redirect.c @date Fri May 18 10:03:15 2001 @author Tom Goodale @desc File to contain John Shalf's HTTP redirect stuff. @enddesc @version $Id$ @@*/ #include #include #include #include "cctk.h" #ifdef CCTK_MPI #include #endif /* CCTK_MPI */ #include "util_Network.h" /* Util_GetHostName() */ #include "httpd.h" #include "http_Request.h" #include "http_Content.h" #include "http_Redirect.h" static const char *rcsid = "$Header$"; CCTK_FILEVERSION(CactusConnect_HTTPD_Redirect_c) /******************************************************************** ********************* Local Data Types *********************** ********************************************************************/ /******************************************************************** ********************* Local Routine Prototypes ********************* ********************************************************************/ static void RegisterRedirect(void); static int RedirectPage(const cGH *cctkGH, httpRequest *request, void *data); /******************************************************************** ********************* Other Routine Prototypes ********************* ********************************************************************/ /******************************************************************** ********************* Local Data ***************************** ********************************************************************/ static char httpredirect=0; static char httpmaster[1024]; /******************************************************************** ********************* External Routines ********************** ********************************************************************/ /*@@ @routine HTTP_SetupRedirect @date Wed April 20 20:39:15 2001 @author John Shalf @desc Creates a socket to listen on purely for server redirect to the root node. @enddesc @calls @calledby @history @endhistory @@*/ int HTTP_SetupRedirect(int port, int queue_size, int hunt) { int retval; /* should not hunt */ int i; char hostnm[1024]; char *alladdr; int addrlen; int nprocs; int proc; addrlen = 1024; nprocs = CCTK_nProcs(NULL); proc = CCTK_MyProc(NULL); retval = 0; #ifdef HTTPD_DEBUG CCTK_INFO("enter setup redirect------------"); #endif memset(hostnm,0,addrlen); Util_GetHostName(hostnm, addrlen); alladdr = (char *) calloc (addrlen, nprocs); #ifdef CCTK_MPI { /* push stack */ #ifdef HTTPD_DEBUG CCTK_INFO("all gather"); #endif MPI_Allgather(hostnm,addrlen,MPI_CHAR, alladdr,addrlen,MPI_CHAR, MPI_COMM_WORLD); #ifdef HTTPD_DEBUG CCTK_INFO("collected"); #endif } #endif /* set our master */ #ifdef HTTPD_DEBUG printf("alladdr is %s\n", alladdr); #endif strcpy(httpmaster,alladdr); /* the 0th element is the master hostname */ /* so compare my addr to the list sequentially */ for(i=0;ih_length; if(addrlen>sizeof(CCTK_INT8)) addrlen=CCTK_INT8; addr=0; memcpy(&addr,he->h_addr,addrlen); return addr; } #endif /******************************************************************** ********************* Local Routines ************************* ********************************************************************/ /*@@ @routine RegisterRedirect @date Fri April 20 23:47:43 2001 @author John Shalf @desc Redirection Page Registration. @enddesc @calls @calledby @history @endhistory @@*/ static void RegisterRedirect(void) { #ifdef HTTP_DEBUG printf("Registering Redirection Page"); #endif HTTP_RegisterPage("/index.html",RedirectPage,NULL); } /*@@ @routine RedirectPage @date Fri April 20 23:47:43 2001 @author John Shalf @desc Redirects the webbrowser to the main server page @enddesc @calls @calledby @history @hdate Thu Sep 14 10:54:22 2000 @hauthor John Shalf @hdesc For clusters where its difficult to know which node is going to have the webserver process, this opens a port on *all* distinct nodes and redirects all web connection attempts to the root node for processing. @endhistory @@*/ static int RedirectPage(const cGH *cctkGH, httpRequest *request, void *data) { int retval; char message[1024]; /* 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,"Server Redirect\n"); /* Write out the main header part */ HTTP_Write(request, message, strlen(message)); sprintf(message,"\n", HTTP_Master(), (unsigned int) HTTP_Port()); HTTP_Write(request,message,strlen(message)); /* ********** Server Redirect To Master ************* */ sprintf(message,"

Redirect to master host=%s:%u

\n", HTTP_Master(), (unsigned int) HTTP_Port()); HTTP_Write(request,message,strlen(message)); HTTP_ContentFooter(cctkGH, 0, sizeof (message), message); retval = HTTP_Write(request, message, strlen(message)); return retval; }