Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

cgi.c

Go to the documentation of this file.
00001 /*! \file cgi.c
00002     This file contains utilities for CGIs.
00003  */
00004 
00005 
00006 #include <stdlib.h>
00007 #include <stdio.h>
00008 #include <strings.h>
00009 
00010 
00011 /*! \brief Extract the login and the domain from a given email address (login@domain).
00012     \param address Pointer to a zero terminated string of characters that represents the email address.
00013     \param login Pointer to a memory location that will be used to store a zero terminated string of characters
00014                  that is the login.
00015     \param domain Pointer to a memory location that will be used to store a zero terminated string of characters
00016                   that is the domain.
00017     \return Upon successful completion, the function returns the value 0. Otherwise the function returns 1, this
00018             means that the given address does not contain any character '@'.
00019     \warning No test is done regarding the size of the memory chuncks pointed by login and domain. It is your
00020              responsability to allocate enough memory.
00021  */
00022 
00023 int get_domain (char *address, char *login, char *domain)
00024 {
00025   char *pt, c;
00026 
00027   pt = strstr(address, "@");
00028   if (pt == NULL) { return 1; }
00029 
00030   c = *pt;
00031 
00032   strcpy (domain, pt+1);
00033 
00034   *pt = 0;
00035 
00036   strcpy (login, address);
00037 
00038   *pt = c;
00039 
00040   return 0;
00041 }
00042 
00043 /*! \brief URL encode a string of characters.
00044     \param string Pointer to a zero terminated string that contains the string to encode.
00045     \return The function returns a pointer to the URL encoded string. If an error occured, then the function
00046             returns NULL.
00047     \warning Note that the returned string has been allocated by the function. Therefore you must free the memory.
00048  */ 
00049 
00050 char* url_escape(char *string)
00051 {
00052    int            alloc;
00053    char           *ns;
00054    unsigned char  in;
00055    int            newlen;
00056    int            index;
00057 
00058 
00059 
00060    newlen = alloc = strlen(string)+1;
00061    ns = (char*) malloc((size_t)alloc);
00062    if (ns == NULL) { return NULL; }
00063    index = 0;
00064 
00065    while((*string) != 0)
00066    {
00067       in = *string;
00068       if(' ' == in) { ns[index++] = '+'; }
00069       else if(!(in >= 'a' && in <= 'z') &&
00070               !(in >= 'A' && in <= 'Z') &&
00071               !(in >= '0' && in <= '9'))
00072            {
00073              /****************************************************/
00074              /* encode it:                                       */
00075              /* the size grows with two, since this'll become a  */
00076              /* %XX.                                             */
00077              /****************************************************/
00078                  
00079                  newlen += 2;
00080                  
00081                  if(newlen > alloc)
00082                  {
00083                    alloc *= 2;
00084                    ns = realloc(ns, alloc);
00085                    if (ns == NULL) { return NULL; }
00086                  }
00087                  
00088                  sprintf(&ns[index], "%%%02X", in);
00089                  index+=3;
00090            }
00091            else
00092            { ns[index++]=in; }
00093 
00094            string++;
00095    }
00096  
00097    ns[index]=0;
00098    return ns;
00099 }
00100 
00101 /*! \example test_url_encoding.c
00102     This file shows how to use the function url_escape().
00103  */
00104 
00105 /*! \brief URL decode a string of characters.
00106     \param string Pointer to a zero terminated string that contains the string to decode.
00107     \param length number of character to decode (should be the size of the string).
00108     \return The function returns a pointer to the URL decoded string. If an error occured, then the function
00109             returns NULL.
00110     \warning Note that the returned string has been allocated by the function. Therefore you must free the memory.             */
00111 
00112 char* url_unescape(char *string, int length)
00113 {
00114    int alloc; 
00115    char *ns;
00116    unsigned char in;
00117    int index;
00118    int hex;
00119    char querypart;       /* everything to the right of a '?' letter is
00120                             the "query part" where '+' should become ' '.
00121                             RFC 2316, section 3.10 */
00122 
00123    alloc = (length?length:strlen(string))+1;
00124    ns = malloc(alloc);
00125    if (ns == NULL) { return NULL; }
00126    index = 0;
00127    querypart = 0;
00128   
00129    while(--alloc > 0)
00130    {
00131       in = *string;
00132       if ((querypart == 1) && ('+' == in)) { in = ' '; }
00133       else if ((querypart == 0) && ('?' == in))
00134       {
00135         /* we have "walked in" to the query part */
00136         querypart = 1;
00137       }
00138       else if('%' == in)
00139            {
00140              /* encoded part */
00141              if(sscanf(string+1, "%02X", &hex))
00142              {
00143                in = hex;
00144                string+=2;
00145                alloc-=2;
00146              }
00147            }
00148 
00149       ns[index++] = in;
00150       string++;
00151    }
00152    
00153    ns[index]=0; /* terminate it */
00154    return ns;
00155 }  
00156 
00157 /*! \example test_url_encoding.c
00158     This file shows how to use the function url_unescape().
00159  */
00160 
00161 

Generated on Thu Apr 3 16:23:42 2003 for Common_C_libraries by doxygen1.3-rc1