Back to index

courier  0.68.2
Classes | Defines | Functions | Variables
cgi.h File Reference
#include "../unicode/unicode.h"
#include <string.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cgi_arglist

Defines

#define cgi_set_cookie_info_init(i)   (memset((i), 0, sizeof(*(i))), (i)->age=-1)
#define cgi_set_cookie_info_free(i)
#define cgi_set_cookie_session(c, n, v)   ( ((c)->name=(n)), ((c)->value)=(v))
#define cgi_set_cookie_expired(c, n)
#define cgi_set_cookie_secure(c)   ((c)->secure=1)
#define SOCKENVIRONLEN   8192
#define VALIDCGIVAR(p)
#define CGI_PASSFD   0

Functions

void fake_exit (int)
void cgi_setup ()
void cgi_cleanup ()
const char * cgi (const char *)
char * cgi_multiple (const char *, const char *)
char * cgi_cookie (const char *)
void cgi_setcookie (const char *, const char *)
int cgi_useragent (const char *)
void cgiurldecode (char *)
void cgi_put (const char *, const char *)
char * cgiurlencode (const char *)
char * cgiurlencode_noamp (const char *)
char * cgiurlencode_noeq (const char *)
int cgi_getfiles (int(*)(const char *, const char *, void *), int(*)(const char *, size_t, void *), void(*)(void *), size_t, void *)
const char * cgihttpscriptptr ()
const char * cgihttpsscriptptr ()
const char * cgiextrapath ()
void cgihttpscriptptr_init ()
void cgihttpsscriptptr_init ()
const char * cgirelscriptptr ()
void cginocache ()
void cgiredirect (const char *)
void cgiversion (unsigned *, unsigned *)
int cgihasversion (unsigned, unsigned)
int cgi_set_cookie_url (struct cgi_set_cookie_info *i, const char *url)
void cgi_set_cookies (struct cgi_set_cookie_info *cookies, size_t n_cookies)
char * cgi_get_cookie (const char *cookie_name)
char * cgi_select (const char *name, const char *optvalues, const char *optlabels, const char *default_value, size_t list_size, const char *flags)
char * cgi_checkbox (const char *name, const char *value, const char *flags)
char * cgi_input (const char *name, const unicode_char *value, int size, int maxlength, const char *opts)
char * cgi_textarea (const char *name, int rows, int cols, const unicode_char *value, const char *wrap, const char *opts)
void cgiformdatatempdir (const char *)
void cgi_daemon (int nprocs, const char *lockfile, void(*postinit)(void *), void(*handler)(void *), void *dummy)
void cgi_connectdaemon (const char *sockfilename, int pass_fd)

Variables

struct cgi_arglistcgi_arglist

Class Documentation

struct cgi_arglist

Definition at line 36 of file cgi.h.

Collaboration diagram for cgi_arglist:
Class Members
const char * argname
const char * argvalue
struct cgi_arglist * next
struct cgi_arglist * prev
struct cgi_set_cookie_info

Definition at line 73 of file cgi.h.

Class Members
int age
char * domain
const char * name
char * path
int secure
const char * value

Define Documentation

#define CGI_PASSFD   0

Definition at line 152 of file cgi.h.

#define cgi_set_cookie_expired (   c,
  n 
)
Value:
( ((c)->name=(n)), ((c)->value)="",\
                                  (c)->age=0)

Definition at line 91 of file cgi.h.

#define cgi_set_cookie_info_free (   i)
Value:
do { if ((i)->path) \
                     free((i)->path);            \
              if ((i)->domain)                   \
                     free((i)->domain);          \
       } while(0)

Definition at line 84 of file cgi.h.

#define cgi_set_cookie_info_init (   i)    (memset((i), 0, sizeof(*(i))), (i)->age=-1)

Definition at line 83 of file cgi.h.

#define cgi_set_cookie_secure (   c)    ((c)->secure=1)

Definition at line 97 of file cgi.h.

#define cgi_set_cookie_session (   c,
  n,
 
)    ( ((c)->name=(n)), ((c)->value)=(v))

Definition at line 90 of file cgi.h.

#define SOCKENVIRONLEN   8192

Definition at line 135 of file cgi.h.

#define VALIDCGIVAR (   p)
Value:
(strncmp((p), "DOCUMENT_", 9) == 0 || \
                   strncmp((p), "GATEWAY_", 8) == 0 || \
                   strncmp((p), "HTTP_", 5) == 0 || \
                   strncmp((p), "HTTPS=", 6) == 0 || \
                   strncmp((p), "SSL_", 4) == 0 || \
                   strncmp((p), "QUERY_STRING=", 13) == 0 || \
                   strncmp((p), "SQWEBMAIL_", 10) == 0 || \
                   strncmp((p), "REMOTE_", 7) == 0 || \
                   strncmp((p), "REQUEST_", 8) == 0 || \
                   strncmp((p), "SCRIPT_", 7) == 0 || \
                   strncmp((p), "SERVER_", 7) == 0 || \
                   strncmp((p), "CONTENT_", 8) == 0 || \
                   strncmp((p), "PATH_INFO=", 10) == 0)

Definition at line 137 of file cgi.h.


Function Documentation

const char* cgi ( const char *  )

Definition at line 289 of file cgi.c.

{
struct cgi_arglist *argp;

       for (argp=cgi_arglist; argp; argp=argp->next)
              if (strcmp(argp->argname, arg) == 0)
                     return (argp->argvalue);
       return ("");
}
char* cgi_checkbox ( const char *  name,
const char *  value,
const char *  flags 
)

Definition at line 15 of file cgicheckbox.c.

{
       char *buf;

       if (!value)
              value="";

       buf=malloc(strlen(name)+strlen(flags)+200);

       if (!buf)
              return NULL;

       strcpy(buf, "<input type='checkbox' name='");
       strcat(buf, name);
       strcat(buf, "' value='");
       strcat(buf, value);
       strcat(buf, "'");

       if (strchr(flags, '*'))
              strcat(buf, " checked='checked'");
       if (strchr(flags, 'd'))
              strcat(buf, " disabled='disabled'");
       strcat(buf, " />");
       return buf;
}
void cgi_cleanup ( )

Definition at line 276 of file cgi.c.

{
#if    CGIFORMDATA

       if (hascgiformfd)
       {
              close(cgiformfd);
              hascgiformfd=0;
       }
#endif

}
void cgi_connectdaemon ( const char *  sockfilename,
int  pass_fd 
)

Definition at line 299 of file cgidaemon.c.

{
       int    s;
       struct  sockaddr_un ssun;
       int    triedagain=0;
       int    rc;

       /* Connect to sqwebmaild via a socket */

       signal(SIGPIPE, SIG_IGN);
       if ((s=socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
              sys_err("socket");

       if (fcntl(s, F_SETFL, O_NDELAY) < 0)
              sys_err("fcntl");

       ssun.sun_family=AF_UNIX;
       strcpy(ssun.sun_path, sockfilename);

       while ((rc=connect(s, (struct sockaddr *)&ssun, sizeof(ssun))) < 0
              && errno == EAGAIN)
       {
              if (++triedagain > 5)
                     break;
              sleep(1);
              ssun.sun_family=AF_UNIX;
              strcpy(ssun.sun_path, sockfilename);
       }

       if (rc < 0)
       {
              struct timeval       tv;
              fd_set fds;

              int    errcode;
              socklen_t errcode_l;

              if (errno != EINPROGRESS)
                     connect_err("connect");

              tv.tv_sec=30;
              tv.tv_usec=0;
              FD_ZERO(&fds);
              FD_SET(s, &fds);
              if (select(s+1, 0, &fds, 0, &tv) <= 0)
                     connect_err("select");


              errcode_l=sizeof(errcode);

              if (getsockopt(s, SOL_SOCKET, SO_ERROR, &errcode, &errcode_l)
                  < 0)
                     connect_err("setsockopt");


              if (errcode)
              {
                     errno=errcode;
                     connect_err("connect");
              }
       }

       if (triedagain)
       {
              fprintf(stderr,
                     "CRIT: Several attempts were necessary to connect to sqwebmaild\n");
              fprintf(stderr,
                     "CRIT: Consider increasing the number of pre-forked sqwebmaild processes\n");
       }

       if (fcntl(s, F_SETFL, 0) < 0)
              sys_err("fcntl");

       send_environ(s, pass_fd);
       passthrough(s, pass_fd);
       close(s);
       exit(0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* cgi_cookie ( const char *  )

Definition at line 22 of file cgicookie.c.

{
size_t pl=strlen(p);
const char *c=getenv("HTTP_COOKIE");
char   *buf;

       while (c && *c)
       {
       size_t i;

              for (i=0; c[i] && c[i] != '='; i++)
                     ;
              if (i == pl && strncmp(p, c, i) == 0)
              {
                     c += i;
                     if (*c)       ++c;   /* skip over = */
                     for (i=0; c[i] && c[i] != ';'; i++)
                            ;

                     buf=malloc(i+1);
                     if (!buf)     enomem();
                     memcpy(buf, c, i);
                     buf[i]=0;
                     cgiurldecode(buf);
                     return (buf);
              }
              c=strchr(c, ';');
              if (c)
                     do
                     {
                            ++c;
                     } while (isspace((int)(unsigned char)*c));
       }
       buf=malloc(1);
       if (!buf)     enomem();
       *buf=0;
       return (buf);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cgi_daemon ( int  nprocs,
const char *  lockfile,
void(*)(void *)  postinit,
void(*)(void *)  handler,
void *  dummy 
)

Definition at line 48 of file cgidaemond.c.

{
       int fd=start_daemon(lockfile);

       if (postinit)
              (*postinit)(dummy);

       if (nprocs > 0)
              run_prefork(fd, nprocs, handler, dummy);
       else
              run_daemon(fd, -1, -1, handler, dummy);
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* cgi_get_cookie ( const char *  cookie_name)

Definition at line 794 of file cgi.c.

{
       size_t cookie_name_len=strlen(cookie_name);
       const char *cookie=getenv("HTTP_COOKIE");

       if (!cookie)
              cookie="";

       while (*cookie)
       {
              if (isspace((int)(unsigned char)*cookie) ||
                  *cookie == ';' || *cookie == ',')
              {
                     ++cookie;
                     continue;
              }

              if (strncmp(cookie, cookie_name, cookie_name_len) == 0 &&
                  cookie[cookie_name_len] == '=')
              {
                     char *buf;

                     cookie += cookie_name_len;
                     ++cookie;

                     if ((buf=malloc(get_cookie_value(cookie, NULL, NULL)))
                         == NULL)
                     {
                            return NULL;
                     }

                     get_cookie_value(cookie, NULL, buf);

                     if (*buf == 0) /* Pretend not found */
                     {
                            free(buf);
                            errno=ENOENT;
                            return NULL;
                     }

                     return buf;
              }

              get_cookie_value(cookie, &cookie, NULL);
       }

       errno=ENOENT;
       return NULL;
}

Here is the call graph for this function:

int cgi_getfiles ( int(*)(const char *, const char *, void *)  ,
int(*)(const char *, size_t, void *)  ,
void(*)(void *)  ,
size_t  ,
void *   
)

Here is the caller graph for this function:

char* cgi_input ( const char *  name,
const unicode_char value,
int  size,
int  maxlength,
const char *  opts 
)

Definition at line 130 of file cgiinput.c.

{
       size_t cnt=1;
       char *buf;
       char *ptr;

       if (!flags)
              flags="";

       do_cgi_input(name, value, size, maxlength, flags, cnt_bytes, &cnt);

       buf=malloc(cnt);

       if (!buf)
              return NULL;

       ptr=buf;
       do_cgi_input(name, value, size, maxlength, flags, save_bytes, &ptr);
       *ptr=0;
       return buf;
}

Here is the call graph for this function:

char* cgi_multiple ( const char *  ,
const char *   
)

Definition at line 299 of file cgi.c.

{
struct cgi_arglist *argp;
size_t l=1;
char   *buf;

       for (argp=cgi_arglist; argp; argp=argp->next)
              if (strcmp(argp->argname, arg) == 0)
                     l += strlen(argp->argvalue)+strlen(sep);

       buf=malloc(l);
       if (!buf)     return(0);
       *buf=0;

       /*
       ** Because the cgi list is build from the tail end up, we go backwards
       ** now, so that we return options in the same order they were selected.
       */

       argp=cgi_arglist;
       while (argp && argp->next)
              argp=argp->next;

       for (; argp; argp=argp->prev)
              if (strcmp(argp->argname, arg) == 0)
              {
                     if (*buf)     strcat(buf, sep);
                     strcat(buf, argp->argvalue);
              }
       return (buf);
}

Here is the caller graph for this function:

void cgi_put ( const char *  ,
const char *   
)

Definition at line 372 of file cgi.c.

{
struct cgi_arglist *argp;

       for (argp=cgi_arglist; argp; argp=argp->next)
              if (strcmp(argp->argname, cginame) == 0)
              {
                     argp->argvalue=cgivalue;
                     return;
              }

       argp=malloc(sizeof(*cgi_arglist));
       if (!argp)    enomem();
       argp->next=cgi_arglist;
       argp->prev=0;
       if (argp->next)
              argp->next->prev=argp;
       cgi_arglist=argp;
       argp->argname=cginame;
       argp->argvalue=cgivalue;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* cgi_select ( const char *  name,
const char *  optvalues,
const char *  optlabels,
const char *  default_value,
size_t  list_size,
const char *  flags 
)

Definition at line 107 of file cgiselect.c.

{
       size_t cnt=1;
       char *buf;
       char *ptr;

       if (!flags)
              flags="";

       do_cgi_select(name, optvalues, optlabels, default_value,
                    list_size, flags, cnt_bytes, &cnt);

       buf=malloc(cnt);

       if (!buf)
              return NULL;

       ptr=buf;
       do_cgi_select(name, optvalues, optlabels, default_value,
                    list_size, flags, save_bytes, &ptr);
       *ptr=0;
       return buf;
}

Here is the call graph for this function:

int cgi_set_cookie_url ( struct cgi_set_cookie_info i,
const char *  url 
)

Definition at line 660 of file cgi.c.

{
       const char *p;

       if (cookie_info->domain)
              free(cookie_info->domain);
       if (cookie_info->path)
              free(cookie_info->domain);

       cookie_info->secure=0;

       if (strncmp(url, "https://", 8) == 0)
              cookie_info->secure=1;

       for (p=url; *p; p++)
       {
              if (*p == ':')
              {
                     url= ++p;
                     break;
              }

              if (*p == '/')
                     break;
       }

       if (strncmp(url, "//", 2) == 0)
       {
              p= url += 2;

              while (*url)
              {
                     if (*url == '/')
                            break;
                     ++url;
              }

              if ((cookie_info->domain=malloc(url-p+1)) == NULL)
                     return -1;

              memcpy(cookie_info->domain, p, url-p);
              cookie_info->domain[url-p]=0;
       }

       if ((cookie_info->path=strdup(url)) == NULL)
              return -1;
       return 0;
}

Here is the call graph for this function:

void cgi_set_cookies ( struct cgi_set_cookie_info cookies,
size_t  n_cookies 
)

Definition at line 710 of file cgi.c.

{
       size_t i;
       const char *sep="";

       printf("Set-Cookie: ");

       for (i=0; i<n_cookies; i++, cookies++)
       {
              printf("%s%s=\"%s\"; ", sep, cookies->name, cookies->value);
              sep="; ";

              if (cookies->path)
                     printf("Path=\"%s\"; ", cookies->path);

              if (cookies->secure)
                     printf("Secure; ");

              if (cookies->age >= 0)
                     printf("Max-Age=%d; ", cookies->age);
              printf("Version=1");
       }

       printf("\n");
       fflush(stdout);
}
void cgi_setcookie ( const char *  ,
const char *   
)

Definition at line 61 of file cgicookie.c.

{
char   *p;
const  char *sn;

       p=cgiurlencode(value);
       sn=getenv("SCRIPT_NAME");
       if (!sn || !*sn)
              sn="/";
       printf("Set-Cookie: %s=%s; path=%s\n", name, value, sn);
       free(p);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cgi_setup ( )

Definition at line 110 of file cgi.c.

{
struct cgi_arglist *p;

       cgi_setup_1();

       if (cgi_arglist)
              cgi_arglist->prev=0;

       /* Initialize the prev pointer */

       for (p=cgi_arglist; p; p=p->next)
              if (p->next)
                     p->next->prev=p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* cgi_textarea ( const char *  name,
int  rows,
int  cols,
const unicode_char value,
const char *  wrap,
const char *  opts 
)

Definition at line 95 of file cgitextarea.c.

{
       size_t cnt=1;
       char *buf;
       char *ptr;

       if (!opts)
              opts="";

       do_cgi_textarea(name, rows, cols, value, opts, wrap, cnt_bytes, &cnt);

       buf=malloc(cnt);

       if (!buf)
              return NULL;

       ptr=buf;
       do_cgi_textarea(name, rows, cols, value, opts, wrap, save_bytes, &ptr);
       *ptr=0;
       return buf;
}

Here is the call graph for this function:

int cgi_useragent ( const char *  )

Definition at line 17 of file cgiuseragent.c.

{
       const char *c=getenv("HTTP_USER_AGENT");

       for ( ; c && *c; c++)
       {
              size_t i;

              if (isalpha((int)(unsigned char)*c))
                     continue;

              for (i=0; p[i]; i++)
              {
                     int a,b;

                     a=(unsigned char)p[i];
                     b=(unsigned char)c[i+1];
                     if (!b)
                            break;

                     a=toupper(a);
                     b=toupper(b);
                     if (a != b)
                            break;
              }

              if (p[i] == 0)
              {
                     int b=(unsigned char)c[i+2];

                     if (b == 0 || !isalpha(b))
                            return (1);
              }
       }
       return (0);
}

Here is the caller graph for this function:

const char* cgiextrapath ( )

Definition at line 15 of file cgiextrapath.c.

{
       const char *pi=getenv("PATH_INFO");

       if (!pi) pi="";
       return pi;
}
void cgiformdatatempdir ( const char *  )

Here is the caller graph for this function:

int cgihasversion ( unsigned  ,
unsigned   
)

Definition at line 10 of file cgihasversion.c.

{
unsigned vmajor, vminor;

       cgiversion(&vmajor, &vminor);
       return (vmajor > major || (vmajor == major && vminor >= minor));
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* cgihttpscriptptr ( )

Definition at line 31 of file cgihttpscriptptr.c.

{
       if (!scriptptr)
       {
       char   *p=getenv("SCRIPT_NAME");
       char   *h=getenv("HTTP_HOST");
       char   *q;

              if (!h)       h="";
              if (!p)       p="";

              q=malloc(strlen(p)+strlen(h)+sizeof("http://"));
              if (!q)       error("Out of memory.");
              sprintf(q, "http:%s%s%s", (*h ? "//":""), h, p);
              scriptptr=q;
       }
       return (scriptptr);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 22 of file cgihttpscriptptr.c.

{
       /* Reinitialisation required when running as fastcgi */
       if (scriptptr) {
              free(scriptptr);
              scriptptr=0;
       }
}
const char* cgihttpsscriptptr ( )

Definition at line 30 of file cgihttpsscriptptr.c.

{
       if (!scriptptr)
       {
       char   *p=getenv("SCRIPT_NAME");
       char   *h=getenv("HTTP_HOST");
       char   *q;

              if (!h)       h="";
              if (!p)       p="";

              q=malloc(strlen(p)+strlen(h)+sizeof("https://"));
              if (!q)       error("Out of memory.");
              sprintf(q, "https:%s%s%s", (*h ? "//":""), h, p);
              scriptptr=q;
       }
       return (scriptptr);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 21 of file cgihttpsscriptptr.c.

{
       /* Reinitialisation required when running as fastcgi */ 
       if (scriptptr) {
              free(scriptptr);
              scriptptr=0;
       }
}
void cginocache ( )

Definition at line 11 of file cginocache.c.

{
       if (cgi_useragent("MSIE"))
       {
              printf("Cache-Control: private\n");
              printf("Pragma: private\n");
       }
       else
       {
              printf("Cache-Control: no-store\n");
              printf("Pragma: no-cache\n");
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cgiredirect ( const char *  )

Definition at line 11 of file cgiredirect.c.

{
       if (cgihasversion(1,1))
              printf("Status: 303 Moved\n");
                            /* Alas, Communicator can't handle it */
       cginocache();
       printf("Location: %s\n\n", buf);
       printf("URI: %s\n", buf);
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* cgirelscriptptr ( )

Definition at line 20 of file cgirelscriptptr.c.

{
       const char *p=getenv("HTTPS");

       if (p && strcasecmp(p, "on") == 0)
              return (cgihttpsscriptptr());

       return (cgihttpscriptptr());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cgiurldecode ( char *  )

Definition at line 342 of file cgi.c.

{
char   *p=q;
int    c;

       while (*q)
       {
              if (*q == '+')
              {
                     *p++=' ';
                     q++;
                     continue;
              }
              if (*q != '%')
              {
                     *p++=*q++;
                     continue;
              }
              ++q;
              c=0;
              q=nybble(q, &c);
              q=nybble(q, &c);

              if (c && c != '\r')
                     /* Ignore CRs we get in TEXTAREAS */
                     *p++=c;
       }
       *p++=0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* cgiurlencode ( const char *  )

Definition at line 261 of file cgi.c.

{
       return (cgiurlencode_common(buf, "\"?;<>&=/:%@+#"));
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* cgiurlencode_noamp ( const char *  )

Definition at line 266 of file cgi.c.

{
       return (cgiurlencode_common(buf, "\"?<>=/:%@+#"));
}

Here is the call graph for this function:

char* cgiurlencode_noeq ( const char *  )

Definition at line 271 of file cgi.c.

{
       return (cgiurlencode_common(buf, "\"?;<>&/:%@+#"));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cgiversion ( unsigned *  ,
unsigned *   
)

Definition at line 12 of file cgiversion.c.

{
const char *p=getenv("SERVER_PROTOCOL");

       *major=0;
       *minor=0;
       if (!p)       return;
       if ( toupper(*p++) != 'H' ||
              toupper(*p++) != 'T' ||
              toupper(*p++) != 'T' ||
              toupper(*p++) != 'P' ||
              *p++ != '/')  return;

       while (isdigit(*p))
              *major= *major * 10 + (*p++ - '0');
       if (*p++ == '.')
       {
              while (isdigit(*p))
                     *minor= *minor * 10 + (*p++ - '0');
       }
}

Here is the caller graph for this function:

void fake_exit ( int  )

Definition at line 29 of file showmsg2html.c.

{
       exit(rc);
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 74 of file cgi.c.