httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dovid Zirkind" <do...@tig.com.au>
Subject Re: [STATUS] (apache-1.3) Wed Jul 7 23:45:11 EDT 1999
Date Thu, 08 Jul 1999 22:43:52 GMT

B"H

[snip]
>  Help:
>
>     * should trap ^C when running not-as-service and do proper shutdown
>
here's a module that handles the ^C in win9x and also allows for "live"
output of incoming requests (and possible color coding schemes - which can
be enabled/disabled with a #define statment)



#ifdef _WIN32
#define __WANT_DISP_LIVE_OUTPUT__
/*
#undef __WANT_DISP_LIVE_OUTPUT__
*/
/*
MODULE-DEFINITION-START
Name: display
ConfigStart
 echo " + This is only meant for WIN32 build"
ConfigEnd
MODULE-DEFINITION-END
*/
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "util_script.h"
#include "http_request.h"
#include <stdio.h>

module display_module;

#ifdef __WANT_DISP_LIVE_OUTPUT__
#define DEFAULT_COLOR (FOREGROUND_INTENSITY | FOREGROUND_GREEN |
FOREGROUND_RED)
typedef struct
 {
 int methods, bg, fg;
 }display_method_st;
typedef struct
 {
 int disp;
 int dm_count;
 int dm_in_mem;
 int prev_method_no;
 display_method_st *dm;
 }display_server_st;
#endif
#define get_s_config(server) (display_server_st
*)ap_get_module_config(server->module_config, &display_module)

static int display_count=0;
static BOOL WINAPI display_HandlerRoutine(DWORD dwCtrlType)
 {
 static int volatile done=0;
 if(!done++)
  {
#ifdef _DEBUG_WHY
 #define case_reason(x) case x:  msg=#x;  break
  char *msg;
  switch(dwCtrlType)
   {
   case_reason(CTRL_C_EVENT);
   case_reason(CTRL_BREAK_EVENT);
   case_reason(CTRL_CLOSE_EVENT);
   case_reason(CTRL_LOGOFF_EVENT);
   case_reason(CTRL_SHUTDOWN_EVENT);
   default:  msg="unknown";
   }
  printf("closing down due to: %s (%d - %x)\n",msg, done, getpid());
  MessageBox(0, msg, ap_get_server_version(), MB_OK);
#endif
  ap_start_shutdown();
  if(display_count>1)  /* I'm not sure *WHY* this works!!! */
   return TRUE;  /* say we did whatever was required */
  }
 return FALSE; /* now let the default handler take over */
 }

static void display_init(server_rec *server, pool *p)
 {
#ifdef __WANT_DISP_LIVE_OUTPUT__
 display_server_st *si=get_s_config(server);
 HANDLE con=GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleTitle(ap_get_server_version());
 if(si->dm)
  SetConsoleTextAttribute(con, (WORD)(si->dm->fg | (si->dm->bg << 4)));
 else
  SetConsoleTextAttribute(con, DEFAULT_COLOR);
#endif
 SetConsoleCtrlHandler(display_HandlerRoutine, 1);
 display_count++;
 }

#ifdef __WANT_DISP_LIVE_OUTPUT__
static int display_output(request_rec *r)
 {
 display_server_st *si=get_s_config(r->server);
 if(si->disp && ap_is_initial_req(r))
  {
  int method_number=(r->method_number + 1);
  if(method_number==(M_GET + 1) && !r->header_only)  method_number--;
  method_number=(1 << method_number);
  if(si->prev_method_no != method_number)
   {
   display_method_st *dm;
   HANDLE con=GetStdHandle(STD_OUTPUT_HANDLE);
   int x, color=DEFAULT_COLOR;
   for(x=0, dm=si->dm; x<si->dm_count; dm++, x++)
    if(method_number & dm->methods)
     {
     color=dm->fg | (dm->bg << 4);
     break;
     }
   SetConsoleTextAttribute(con, (WORD)color);
   si->prev_method_no=method_number;
   }
  printf("%s\n", r->the_request);
  }
 return DECLINED;
 }

static void *display_create_server_config(pool *p, server_rec *s)
 {
 display_server_st *ret=(display_server_st *)ap_pcalloc(p,
sizeof(display_server_st));
 ret->disp=1;
 return ret;
 }

static const char *display_set_flag_slot(cmd_parms *cmd, void *mconfig, int
flag)
 {
 display_server_st *si=get_s_config(cmd->server);
 int offset = (int) (long) cmd->info;
 *(int *) (((char *)(si)) + offset) = flag ? 1 : 0;
 return NULL;
 }

static char *ap_str_toupper(char *in)
 {
 char *str=in;
 while(*str)
  {
  *str = ap_toupper(*str);
  ++str;
  }
 return in;
 }

#define case_cmp(a, l, c, b, ret) case c: if(!strncmp(a, b, l)) return ret;
break;
static int lookup_color(char *str)
 {
 int len=strlen(str);
 ap_str_toupper(str);

 switch(*str)
  {
  case_cmp(str, len, 'B', "BLUE",   FOREGROUND_BLUE);
  case_cmp(str, len, 'G', "GREEN",  FOREGROUND_GREEN);
  case_cmp(str, len, 'R', "RED",   FOREGROUND_RED);
  case_cmp(str, len, 'I', "INTENSITY", FOREGROUND_INTENSITY);
  }
 return -1;
 }

static display_server_st *realloc_dm(pool *p, display_server_st *si)
 {
 int x=si->dm_count;
 display_method_st *new;
 if(((x+1) > si->dm_in_mem))
  {
  int new_num=si->dm_in_mem+6;
  new=(display_method_st *)ap_pcalloc(p, new_num *
sizeof(display_method_st));
  si->dm_in_mem=new_num;
  if(si->dm && (x>0))
   memcpy(new, si->dm, sizeof(display_method_st) * x);
  si->dm=new;
  }
 memset(&si->dm[x].bg, 0, sizeof(display_method_st));
 si->dm_count++;
 return(si);
 }

static const char *display_set_color_slot(cmd_parms *cmd, void *mconfig,
char *method, char *color)
 {
 display_server_st *si=get_s_config(cmd->server);
 display_method_st *dm;
 int offset = (int) (long) cmd->info;
 int found=0, x;
 /* make sure M_GET is 0, and "M_HEAD" is 1 */
 int method_number=ap_method_number_of(ap_str_toupper(method))+1;
 int method_mask, color_no=lookup_color(color);
 if(method_number==(M_GET + 1) && *method=='G')  method_number--;
 if(method_number==(M_INVALID + 1))
  {
  if(!strcmp(method,"*") || !strcmp(method, "ALL"))
   method_mask=0xFFFFFFFF; /* that's ALL */
  else
   return ap_pstrcat(cmd->pool, "invalid method \"", method, "\"", 0);
  }
 else
  method_mask=(1 << method_number); /* do this once */
 if(color_no < 0)
  return ap_pstrcat(cmd->pool, "invalid color \"", color, "\"", 0);
 for(x=0, dm=si->dm; x<si->dm_count; dm++, x++)
  if(method_mask == dm->methods)
   {
   found=1;
   *(int *) (((char *)(dm)) + offset) |= color_no;
   }
 if(!found)
  {
  realloc_dm(cmd->pool, si);
  dm=si->dm+si->dm_count-1;
  dm->methods=method_mask;
  *(int *) (((char *)(dm)) + offset) |= color_no;
  }
 return NULL;
 }

static command_rec display_cmds[]=
 {
  {
   "ShowIncomingRequests",
   display_set_flag_slot,
   (void *)XtOffsetOf(display_server_st, disp),
   RSRC_CONF,
   FLAG,
   "should we show requests as they come in ? ([On] or Off)"
  },
  {
   "ForegroundColor",
   display_set_color_slot,
   (void *)XtOffsetOf(display_method_st, fg),
   RSRC_CONF,
   ITERATE2,
   "colors to use when displaying specific incoming request\n { GET | HEAD |
POST | TRACE } { RED | GREEN | BLUE | INTENSITY }\n example: \"GET red blue
intensity\" will show bright purple"
  },
  {
   "BackgroundColor",
   display_set_color_slot,
   (void *)XtOffsetOf(display_method_st, bg),
   RSRC_CONF,
   ITERATE2,
   "colors to use when displaying specific incoming request\n { GET | HEAD |
POST | TRACE } { RED | GREEN | BLUE | INTENSITY }\n example: \"GET red blue
intensity\" will show bright purple"
  },
  {NULL},
 };
#else
 #define display_cmds     0
 #define display_create_server_config 0
 #define display_output     0
#endif

module display_module =
 {
 STANDARD_MODULE_STUFF,
 display_init,    /* module initializer */
 0,       /* per-directory config creator */
 0,       /* dir config merger */
 display_create_server_config, /* server config creator */
 0,       /* server config merger */
 display_cmds,    /* command table */
 0,       /* [9] list of handlers */
 display_output,    /* [2] filename-to-URI translation */
 0,       /* [5] check/validate user_id */
 0,       /* [6] check user_id is valid *here* */
 0,       /* [4] check access by host address */
 0,       /* [7] MIME type checker/setter */
 0,       /* [8] fixups */
 0,       /* [10] logger */
#if MODULE_MAGIC_NUMBER >= 19970103
 0,       /* [3] header parser */
#endif
#if MODULE_MAGIC_NUMBER >= 19970719
 0,       /* process initializer */
#endif
#if MODULE_MAGIC_NUMBER >= 19970728
 0,       /* process exit/cleanup */
#endif
#if MODULE_MAGIC_NUMBER >= 19970902
 0,       /* [1] post read_request handling */
#endif
 };
#endif



Mime
View raw message