aboutsummaryrefslogtreecommitdiff
path: root/src/Redirect.c
blob: 4b3fbc4ac17916cea3e6e69acbd801c6c78e56a3 (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
 /*@@
   @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 <stdlib.h>
#include <string.h>

#include "cctk.h"

#ifdef CCTK_MPI
#include <mpi.h>
#endif /* CCTK_MPI */

#include "util_Network.h"    /* Util_GetHostName() */

#include "httpd.h"

#include "httpRequest.h"
#include "Content.h"

#include "Redirect.h"
#include "SString_Namespace.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

@@*/

#define ADDRLEN 1024

int HTTP_SetupRedirect(int port,
                       int queue_size,
                       int hunt)
{
  int retval = 0;
  /* should not hunt */
  int i;
  char hostnm[ADDRLEN] = EMPTYSTRING;
  char *alladdr;

  int nprocs   = CCTK_nProcs(NULL);
  int proc     = CCTK_MyProc(NULL);

#ifdef HTTPD_DEBUG
  CCTK_INFO("enter setup redirect------------");
#endif
  memset(hostnm,0,sizeof(hostnm));
  Util_GetHostName(hostnm, sizeof(hostnm));
  alladdr = (char *) calloc (sizeof(hostnm), nprocs);

#ifdef CCTK_MPI
  { /* push stack */
#ifdef HTTPD_DEBUG
    CCTK_INFO("all gather");
#endif

    MPI_Allgather(hostnm,sizeof(hostnm),MPI_CHAR,
                  alladdr,sizeof(hostnm),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
  strncpy(httpmaster,alladdr, sizeof(httpmaster)); /* the 0th element is the master hostname */
  httpmaster[sizeof(httpmaster) - 1] = '\0';

  /* so compare my addr to the list sequentially */
  for(i=0;i<nprocs;i++)
  {
#ifdef HTTPD_DEBUG
    printf("Cycle through %u\n",i);
#endif
    if(!strcmp(alladdr+i*sizeof(hostnm),hostnm))
    {
      /* we matched addresses */
      if(i<proc || i==0)
      {
        break; /* I'm not the lowest order */
      }
      else
      {
      /* otherwise, I am the lowest ranked processor with this
         address that isn't the primary webserver.  So
         I should do a server redirect */
        httpredirect=1; /* this has a redirect socket */
        HTTP_SetupServer(port,queue_size,hunt);

        CCTK_VInfo(CCTK_THORNSTRING, "HTTP requests will be redirected to "
                   "primary webserver node on\n                http://%s:%d/",
                   HTTP_Master(), (unsigned int) HTTP_Port());

        RegisterRedirect();
        retval = 1;
        break; /* one redirect is enough */
      }
    }
  }
  if(! retval)
  {
#ifdef HTTPD_DEBUG
    CCTK_INFO("No Redirect**************\n");
#endif
    httpredirect=0;
  }

  free (alladdr);

  return retval;
}

 /*@@
   @routine    HTTP_Master
   @date       Friday April 20 11:24:40 2001
   @author     John Shalf
   @desc
   Reports the hostname of the node the HTTP server is listening on.
   The returns the hostname of the master as a string.
   @enddesc
   @calls
   @calledby
   @history

   @endhistory
@@*/
const char *HTTP_Master(void)
{
  return httpmaster;
}

 /*@@
   @routine    HTTP_IsServer
   @date       Friday April 20 11:24:40 2001
   @author     John Shalf
   @desc
           True if this node has a server socket open.
   @enddesc
   @calls
   @calledby
   @history

   @endhistory
@@*/
int HTTP_IsServer(void)
{
  return (CCTK_MyProc (NULL) == 0 || httpredirect);
}

 /*@@
   @routine    HTTP_IsRedirect
   @date       Friday April 20 11:24:40 2001
   @author     John Shalf
   @desc
           True only if this node has a server open and that server
        is exclusively used to redirect web traffic back to the root
    node.
   @enddesc
   @calls
   @calledby
   @history

   @endhistory
@@*/
int HTTP_IsRedirect(void)
{
  return httpredirect;
}


#if 0 /* this would be more efficient, but PUGH doesn't define
         a datatype for 8-byte integers.  So instead,
         setupRedirect will use the actual hostname. */
CCTK_INT8 Util_GetHostAddr(char *hostname)
{
  hostent *he;
  char hostname[1025];
  int addrlen;
  CCTK_INT8 addr;

  Util_GetHostName(hostname, 1024);
  he=gethostbyname(hostname);
  addrlen=he->h_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 = -1;
  String *message = String_New();

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

  HTTP_SendOKHeader(request);

  /* Start the page */
  SetToCString(message,"<html><head><title>Server Redirect</title>\n");
  /* Write out the main header part */
  HTTP_SendString(request, message);
  SetToCString(message,"<meta http-equiv=\"refresh\" content=\"1; URL=http://");
  ConcatCString(message,HTTP_Master());
  ConcatCString(message, ":");
  ConcatDecimal(message,(unsigned int) HTTP_Port());
  ConcatCString(message, "\" />\n</head>\n<body>\n");
  HTTP_SendString(request,message);
  /* ********** Server Redirect To Master ************* */
  SetToCString(message,"<h1>Redirect to master host=");
  ConcatCString(message,HTTP_Master());
  ConcatCString(message, ":");
  ConcatDecimal(message,(unsigned int) HTTP_Port());
  ConcatCString(message, "</h1>\n");
  HTTP_SendString(request,message);

  HTTP_SetContentFooterString(cctkGH, 0, message);
  retval = HTTP_SendString(request, message);

  String_Delete( message );
  return retval;
}