cgi_cgi.c File Reference

#include "cgi.h"

Include dependency graph for cgi_cgi.c:

Go to the source code of this file.

Defines

#define NEWALLOCSIZE   128
#define NEXTCHAR(s)   fgetc(s->f)
#define TESTEOF(s)   feof(s->f)
#define MAXSIZE   65535
#define MAX_ENTRIES   1000

Functions

void Translate (char *s, unsigned char *list)
void LogConnection (char *program, char *host)
int main (int argc, char *argv[])
int SendCGIHeader (EZSSTREAM *htmlout, char *header)
char * fmakeword (EZSSTREAM *in, char stop, int *cl)
char * GetEnvironment (EZSSTREAM *htmlout, char *key)
void SetupCGIData (EZSSTREAM *htmlin, CGINameValue *data)
CGINameValueReadCGIGetData (EZSSTREAM *htmlin, int extra)
CGINameValueReadCGIPostData (EZSSTREAM *htmlin, int reserve)
int CheckCGISendMethod (EZSSTREAM *htmlin)

Variables

int CGILogLevel


Define Documentation

#define MAX_ENTRIES   1000
 

Definition at line 465 of file cgi_cgi.c.

Referenced by ReadCGIPostData().

#define MAXSIZE   65535
 

Definition at line 283 of file cgi_cgi.c.

Referenced by fmakeword().

#define NEWALLOCSIZE   128
 

Definition at line 273 of file cgi_cgi.c.

Referenced by fmakeword().

#define NEXTCHAR  )     fgetc(s->f)
 

Definition at line 279 of file cgi_cgi.c.

Referenced by fmakeword().

#define TESTEOF  )     feof(s->f)
 

Definition at line 280 of file cgi_cgi.c.


Function Documentation

int CheckCGISendMethod EZSSTREAM htmlin  ) 
 

0 = POST, 1 = GET, 2 = POST_ERROR, 3=CONSOLE

Definition at line 571 of file cgi_cgi.c.

References GetEnvironment(), NULLSTR, stricmp(), and strnicmp().

Referenced by main().

00572 {
00573 #ifdef WINCGI
00574  char c[256],host[256];
00575  if (!*WinCGIFile) return 3;
00576  GetPrivateProfileString("CGI","Request Method",NULLSTR,c,256,WinCGIFile);
00577 #else
00578  char * c;
00579 
00580  c = GetEnvironment(htmlin,"REQUEST_METHOD");
00581 #endif
00582 
00583  if (!c) return 3; /* not cgi */
00584 
00585 if (c && (stricmp(c,"GET") == 0))
00586    {
00587     return 1;
00588    }
00589 
00590 #ifndef WINCGI
00591 if (c && (stricmp(c,"POST") == 0))
00592    {
00593      c =  getenv("CONTENT_TYPE");
00594 
00595     if(c && strnicmp(c,"application/x-www-form-urlencoded",33))
00596      {
00597       return 2;
00598      }
00599     return 0;
00600    }
00601 return 0;
00602 #else
00603 return 0;
00604 #endif
00605 }

char* fmakeword EZSSTREAM in,
char  stop,
int *  cl
 

Definition at line 286 of file cgi_cgi.c.

References CGIMALLOC, MAXSIZE, NEWALLOCSIZE, and NEXTCHAR.

Referenced by ReadCGIPostData().

00286                                                {
00287     int wsize;
00288     char *word;
00289     int ll;
00290 
00291     wsize = NEWALLOCSIZE;
00292     ll=0;
00293     word = (char *) CGIMALLOC(sizeof(char) * (wsize + 1));
00294 
00295     while(1) {
00296         word[ll] = (char)NEXTCHAR(in);
00297         if(ll==wsize) {
00298             word[ll+1] = 0;
00299             wsize+=NEWALLOCSIZE;
00300             if (wsize > MAXSIZE)
00301              break;
00302             else
00303             {
00304              char* W2 = (char *)realloc(word,sizeof(char)*(wsize+1));
00305              if (!W2) break;
00306              word = W2;
00307             }
00308         }
00309         --(*cl);
00310         if((word[ll] == stop) || (TESTEOF(in)) || (!(*cl))) {
00311             if(word[ll] != stop) ll++;
00312             word[ll] = '\0';
00313             return word; /* success */
00314         }
00315         ++ll;
00316     }
00317 /* error recovery */
00318     free(word);
00319     return 0;
00320 }

char* GetEnvironment EZSSTREAM htmlout,
char *  key
 

Definition at line 334 of file cgi_cgi.c.

References NULLSTR.

00335 {
00336 #ifdef WINCGI
00337  GetPrivateProfileString("System",key,NULLSTR,WINCGIenv,256,WinCGIFile);
00338  return WINCGIenv;
00339 #elif defined(FASTCGI)
00340  return FCGX_GetParam(key,htmlout->envp);
00341 #else
00342  return getenv(key);
00343 #endif
00344 }

void LogConnection char *  program,
char *  host
 

Definition at line 64 of file cgi_cgi.c.

References GetTime(), and LogMessage().

Referenced by main().

00065 {
00066  char Date[14],Time[14];
00067 
00068  GetTime(Date,Time,0);
00069  LogMessage("Run\tPROGRAM=");
00070  LogMessage(program);
00071  LogMessage(",HOST=");
00072  LogMessage(host);
00073  LogMessage(",DATE=");
00074  LogMessage(Date);
00075  LogMessage(",TIME=");
00076  LogMessage(Time);
00077  LogMessage("\n");
00078 }

int main int  argc,
char *  argv[]
 

Definition at line 147 of file cgi_cgi.c.

References CGImain(), CheckCGISendMethod(), DeleteNVP(), ExpandLocalPath(), GetEnvironment(), GetFieldValue, HTMLPrintf(), HTMLWrite(), HTMLWriteFile(), LogConnection(), LogError(), LogMessage(), LogStartup(), LogTrx(), MAXPATH, NewNVP(), NULL, NULLSTR, RAOSOFT_CGI_VERSION, ReadCGIGetData(), ReadCGIPostData(), ReadPairedValues(), STREAM, and swapchars().

00148 {
00149 #ifdef FASTCGI
00150  int fcgiCount = 0;
00151  FCGISTREAM chtmlin,chtmlerr,chtmlout;
00152  STREAM htmlerr;
00153 #else
00154  EZSSTREAM ehtmlin, ehtmlout;
00155 #endif /* FASTCGI */
00156 #endif /* WINCGI */
00157  CGINameValue * Params;
00158  int i;
00159  STREAM htmlin;
00160  STREAM htmlout;
00161  char * host;
00162 
00163 #ifdef FASTCGI
00164  htmlin=&chtmlin;
00165  htmlout=&chtmlout;
00166  htmlerr=&chtmlerr;
00167  htmlin->envp = htmlout->envp = htmlerr->envp = NULL;
00168  while (FCGX_Accept(&htmlin->strp, &htmout->strp, &htmlerr->strp, &htmlout.envp) >= 0)
00169  {
00170    htmlin->envp = htmlerr->envp = htmlout->envp;
00171    fcgiCount++;
00172  }
00173 #else
00174  htmlin = &ehtmlin; htmlout = &ehtmlout;
00175 #ifdef WINCGI
00176   argc = SplitArgs(lpszCmdLine, argv,128);
00177   if (!InitWindowsCGI(argv[1],htmlin,htmlout)) return 1;
00178 #else /* assume UNIX, or at least win32 console. ISAPI never got this far. */
00179 
00180 #ifdef XP_WIN
00181 swapchars(argv[0],'/','\\'); /* Apache 1.3.6 needs this */
00182 setmode(fileno(stdout),O_BINARY); /* Apache hack to be able to send images */
00183 #endif
00184 
00185  htmlin->t = NULL;
00186  htmlin->f = stdin;
00187 
00188  htmlout->t = NULL;
00189  htmlout->f = stdout;
00190 #endif
00191 #endif
00192 
00193   switch ( CheckCGISendMethod(htmlin) )
00194   {
00195    case 0: Params = ReadCGIPostData(htmlin,64);  break;
00196    case 1: Params = ReadCGIGetData(htmlin,64); break;
00197    case 2: Params = 0; break;
00198    default:Params = argc ? ReadPairedValues(argc-1,argv+1,64) : 0;
00199   };
00200 
00201  host = Params != NULL ? GetFieldValue(Params,"HOST") : "unknown";
00202 
00203 #ifndef CGI_FAST
00204  LogStartup(htmlout,argv[0]);
00205  LogConnection(argv[0],host);
00206  if (Params) LogTrx(Params);
00207 #endif
00208 
00209  if (!Params) Params = NewNVP(1);
00210 
00211  i = CGImain(argv[0],Params,htmlout);
00212 
00213  if (i)
00214  {
00215         char file[MAXPATH];
00216         ExpandLocalPath(argv[0],file,"error.html",NULLSTR);
00217         HTMLWriteFile(htmlout,file);
00218  }
00219 
00220  if (i == 101) /* security violation */
00221   {
00222    HTMLWrite(htmlout,"<P>Error: Access denied.\n");
00223    HTMLWrite(htmlout,"<!-- Raosoft CGI " RAOSOFT_CGI_VERSION " -->");
00224    LogMessage(",ERROR=Access denied");
00225   }
00226  else if (i != 0)
00227   {
00228    char err[128];
00229    HTMLPrintf(htmlout,
00230    "<P><A href=http://www.raosoft.com/help/cgi/ezs/error.html#s%d>Error %d</a>\n",i,i);
00231    HTMLWrite(htmlout,"<!-- Raosoft CGI " RAOSOFT_CGI_VERSION " -->");
00232    sprintf(err,"\nCGImain returned %d",i);
00233    LogError(err);
00234 #ifndef CGI_FAST
00235    LogError("\nCGI parameters");
00236    for (i=0;Params[i].name; i++)
00237    {
00238       if (!Params[i].name[0]) continue;
00239        LogError("\n");LogError(Params[i].name);LogError("=");LogError(Params[i].value);
00240    }
00241    LogError("\nEnvironment\nContent_type=");
00242    LogError(GetEnvironment(htmlout,"CONTENT_TYPE"));
00243    LogError("\nQUERY_STRING=");
00244    LogError(GetEnvironment(htmlout,"QUERY_STRING"));
00245    LogError("\nREQUEST_METHOD=");
00246    LogError(GetEnvironment(htmlout,"REQUEST_METHOD"));
00247    LogError("\nCONTENT_LENGTH=");
00248    LogError(GetEnvironment(htmlout,"CONTENT_LENGTH"));
00249 #endif
00250   }
00251 
00252 #ifdef WINCGI
00253  DoneWindowsCGI(htmlin,htmlout);
00254 #endif
00255  if (Params) DeleteNVP(Params);
00256 
00257  return 0;
00258 }

CGINameValue* ReadCGIGetData EZSSTREAM htmlin,
int  extra
 

Definition at line 376 of file cgi_cgi.c.

References ANSItoUTF8(), CGIFREE, CGIMALLOC, ContainsANSIChars(), ExpandUrl(), GetEnvironment(), NULLSTR, ReadPairedString(), SetupCGIData(), swapchars(), Translate(), UTF7toUTF8(), and CGINameValue::value.

Referenced by main().

00377 {
00378  char * c;
00379  CGINameValue* data;
00380  int x;
00381 #ifdef WINCGI
00382  c = CGIMALLOC(65535);
00383  GetPrivateProfileString("CGI","Query String",NULLSTR,c,65535,WinCGIFile);
00384 #else
00385  c = GetEnvironment(htmlin,"QUERY_STRING");
00386 #endif
00387 
00388  if (c && *c)
00389   {
00390 #ifdef EBCDIC
00391     Translate(c,ascii2ebcdic);
00392 #endif
00393    /*2005 this is really old code, before web browsers were standardized.
00394    if (!strchr(c,'&')) data = ReadPairedString(c,'/',3+extra);
00395    else */
00396    data = ReadPairedString(c,'&',3+extra);
00397   }
00398  else
00399  {
00400 #ifdef WINCGI
00401   GetPrivateProfileString("CGI","Logical Path",NULLSTR,c,65535,WinCGIFile);
00402 #else
00403   c = GetEnvironment(htmlin,"PATH_INFO");
00404 #endif
00405   if (c &&*c)
00406    {
00407 #ifdef EBCDIC
00408     Translate(c,ascii2ebcdic);
00409 #endif
00410     if (*c=='/') c++;
00411     if (!strchr(c,'&')) data = ReadPairedString(c,'/',3+extra);
00412     else data = ReadPairedString(c,'&',3+extra);
00413    }
00414   else return 0;
00415  }
00416 
00417  if (!data) return 0;
00418  SetupCGIData(htmlin,data);
00419 
00420 #ifndef WINCGI
00421  for (x = 3+extra; data[x].name; x++)
00422   {
00423     swapchars(data[x].name,'+',' '); /*plus to space*/
00424     swapchars(data[x].value,'+',' '); /*plus to space*/
00425     ExpandUrl(data[x].name);
00426     ExpandUrl(data[x].value);
00427 
00428     if (strstr(data[x].name,"+AAA"))
00429     {
00430       UTF7toUTF8(data[x].name);
00431       UTF7toUTF8(data[x].value);
00432     }
00433     else if (ContainsANSIChars(data[x].value))
00434     {
00435      char* c = data[x].value;
00436      data[x].value = ANSItoUTF8(c);
00437      free(c);
00438     }
00439      swapchars(data[x].name,'\t',' '); /*remove tabs*/
00440      swapchars(data[x].name,'\r',' '); /*remove carriage-returns*/
00441      swapchars(data[x].name,'\n',' '); /*remove linefeeds*/
00442      swapchars(data[x].value,'\t',' '); /*remove tabs*/
00443      swapchars(data[x].value,'\r',' '); /*remove carriage-returns*/
00444      swapchars(data[x].value,'\n',' '); /*remove linefeeds*/
00445   }
00446 #endif
00447 
00448 #ifdef WINCGI
00449  CGIFREE(c);
00450 #endif
00451  return data;
00452 }

CGINameValue* ReadCGIPostData EZSSTREAM htmlin,
int  reserve
 

Definition at line 466 of file cgi_cgi.c.

References ANSItoUTF8(), CGIFREE, ContainsANSIChars(), DeleteNVP(), ExpandUrl(), fmakeword(), MAX_ENTRIES, CGINameValue::name, NewNVP(), NULL, NULLSTR, SetupCGIData(), strdup(), swapchars(), Translate(), UTF7toUTF8(), and CGINameValue::value.

Referenced by main().

00467 {int cl;
00468  register int x;
00469  char * c;
00470  int maxsize = MAX_ENTRIES + reserve;
00471  int growcount = 0; /* that should do the trick! */
00472  #ifdef EBCDIC
00473  char stopstr[2];
00474  #endif
00475 
00476  CGINameValue* data;
00477 
00478  #ifdef EBCDIC
00479  stopstr[0]='&';
00480  stopstr[1]=0;
00481  Translate(stopstr,ebcdic2ascii);
00482  #endif
00483 
00484  c = getenv("CONTENT_LENGTH");
00485  cl = c ? atoi(c) : 0;
00486 
00487  if (cl == 0) return NULL;
00488  if (!htmlin->f) return NULL;
00489  /* Read the host information */
00490 
00491  data = NewNVP(maxsize);
00492 
00493  for (x=0; x<reserve; x++)
00494   {
00495    data[x].name=strdup(NULLSTR);
00496    data[x].value=strdup(NULLSTR);
00497   }
00498 
00499  SetupCGIData(htmlin,data);
00500  x=reserve;
00501 
00502  while((cl > 0) && (!feof(htmlin->f)) && (growcount  < 64 ))
00503  {
00504  /* Convert the CGI data back to ASCII, read from stdin */
00505      if (x == maxsize) /* running out of space? grow the list */
00506      {
00507       CGINameValue* newdata = NewNVP(maxsize+ MAX_ENTRIES);
00508       if (newdata == NULL) return data;
00509       memcpy(newdata,data,maxsize * sizeof(CGINameValue));
00510       CGIFREE(data);
00511       data = newdata;
00512       maxsize += MAX_ENTRIES;
00513       growcount++;
00514      }
00515 
00516 #ifdef EBCDIC
00517 /* Translate binary data to EBCDIC text before doing anything. */
00518      c = fmakeword(htmlin,stopstr[0],&cl);
00519 #else
00520      c = fmakeword(htmlin,'&',&cl);
00521 #endif
00522 
00523 #ifdef EBCDIC
00524      Translate(c,ascii2ebcdic);
00525 #endif
00526 
00527      if (!c) {DeleteNVP(data); return NULL;}
00528      swapchars(c,'+',' '); /*plus to space*/
00529      ExpandUrl(c);      /*converts %xx to characters*/
00530      /* swapchars(c,'\t',' '); remove tabs*/
00531      data[x].name = c;
00532 
00533      c = strchr(c,'=');
00534      if ((c) && (*c))
00535       {
00536        *c++ = 0;
00537        data[x].value = strdup(c);
00538       }
00539      else
00540       {
00541        data[x].value = strdup(NULLSTR);
00542       }
00543 
00544      if (strstr(data[x].name,"+AAA"))
00545      {
00546       UTF7toUTF8(data[x].name);
00547       UTF7toUTF8(data[x].value);
00548      } /*application/x-www-form-urlencoded*/
00549      else if (ContainsANSIChars(data[x].value))
00550      {
00551       char* c = data[x].value;
00552       data[x].value = ANSItoUTF8(c);
00553       free(c);
00554      }
00555 
00556      swapchars(data[x].name,'\t',' '); /*remove tabs*/
00557      swapchars(data[x].name,'\r',' '); /*remove carriage-returns*/
00558      swapchars(data[x].name,'\n',' '); /*remove linefeeds*/
00559      swapchars(data[x].value,'\t',' '); /*remove tabs*/
00560      swapchars(data[x].value,'\r',' '); /*remove carriage-returns*/
00561      swapchars(data[x].value,'\n',' '); /*remove linefeeds*/
00562      /*not all compilers reliably increment x after for loops*/
00563      x++;
00564  }
00565  return data;
00566 } /* end of ReadCGIData */

int SendCGIHeader EZSSTREAM htmlout,
char *  header
 

send NULL for the default header "Content-type: text/html"

Definition at line 262 of file cgi_cgi.c.

References CGI_DEFAULT_HEADER, HTMLWrite(), and NULL.

00263 {
00264  if (header != NULL)
00265   if (strlen(header))
00266   { HTMLWrite(htmlout,header); return 1;}
00267  HTMLWrite(htmlout,CGI_DEFAULT_HEADER);
00268  return 1;
00269 }

void SetupCGIData EZSSTREAM htmlin,
CGINameValue data
 

Definition at line 346 of file cgi_cgi.c.

References GetEnvironment(), GetTime(), NULL, NULLSTR, and SetFieldValue().

Referenced by ReadCGIGetData(), and ReadCGIPostData().

00347 {
00348 /* Date/Time format: 19960214 124253 */
00349  char Date[14],Time[14];
00350  char * host;
00351  char temp[256];
00352  temp[0]=0;
00353 
00354  /*RenameField(data,"","HOST");   */
00355 
00356 #ifdef WINCGI
00357  GetPrivateProfileString("System","Remote Address",NULLSTR,temp,256,WinCGIFile);
00358  SetFieldValue(data,"HOST",temp);
00359 #else
00360  host = GetEnvironment(htmlin,"REMOTE_ADDR");
00361  if (host == NULL || host[0] == 0) host = GetEnvironment(htmlin,"HTTP_ADDR");
00362  if (host == NULL || host[0] == 0) host = GetEnvironment(htmlin,"REMOTE_HOST");
00363  if (host == NULL || host[0] == 0) host = GetEnvironment(htmlin,"HTTP_HOST");
00364  if (host == NULL || host[0] == 0) host = "unknown";
00365  SetFieldValue(data,"HOST",host);
00366 #endif
00367 
00368  GetTime(Date,Time,0);
00369 /* RenameField(data,"","DATE");  */
00370  SetFieldValue(data,"DATE",Date);
00371 
00372 /* RenameField(data,"","TIME");   */
00373  SetFieldValue(data,"TIME",Time);
00374 }

void Translate char *  s,
unsigned char *  list
 

Definition at line 58 of file cgi_cgi.c.

Referenced by ReadCGIGetData(), and ReadCGIPostData().

00059 {
00060   unsigned char * x = (unsigned char*)s;
00061   while (*x) {*x = list[*x]; x++;}
00062 }


Variable Documentation

int CGILogLevel
 

Definition at line 50 of file cgi_log.c.



Raosoft, Inc.
Raosoft EZReport, EZSurvey, InterForm, RapidReport, Raosoft, and SurveyWin are registered trademarks of Raosoft, Inc. Page contents © 1996-2007 by Raosoft, Inc. You may use and modify this file for your own use, but may not distribute it or derivative works without the prior written consent of Raosoft, Inc. This software is provided "as is," and Raosoft makes no warranty, express or implied, of fitness for a particular application. Every measure has been taken to anticipate risks inherent to computer networks, but we cannot guarantee safety or reliability of this program in every situation.
Tel: 206-525-4025 (US) Email: raosoft@raosoft.com
http://www.raosoft.com/