"Fossies" - the Fresh Open Source Software Archive

Member "libping-1.15/lib/url.c" (27 Mar 2002, 7770 Bytes) of package /linux/privat/old/libping-1.15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "url.c" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * URL Processing
    3  *
    4  * Copyright (C) 2000, 2001, 2002 by
    5  * Jeffrey Fulmer - <jdfulmer@armstrong.com>
    6  * This file is distributed as part of Siege 
    7  *
    8  * This program is free software; you can redistribute it and/or modify
    9  * it under the terms of the GNU General Public License as published by
   10  * the Free Software Foundation; either version 2 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This program is distributed in the hope that it will be useful,
   14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16  * GNU General Public License for more details.
   17  *
   18  * You should have received a copy of the GNU General Public License
   19  * along with this program; if not, write to the Free Software
   20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
   21  *
   22  */
   23 #ifdef  HAVE_CONFIG_H
   24 # include <config.h>
   25 #endif/*HAVE_CONFIG_H*/
   26  
   27 #include <stdio.h>
   28 #include <stdlib.h>
   29 #ifdef STDC_HEADERS
   30 # include <string.h>
   31 #else
   32 # ifndef HAVE_STRCHR
   33 #  define strchr index
   34 #  define strrchr rindex
   35 # endif
   36 char *strchr (), *strrchr ();
   37 # ifndef HAVE_MEMCPY
   38 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
   39 #  define memmove(d, s, n) bcopy ((s), (d), (n))
   40 # endif
   41 #endif
   42 #include <errno.h>
   43 #include <url.h>
   44 #include <util.h>
   45 #include <setup.h>
   46 
   47 /*HARDCODED ALERT*/
   48 #define PLENGTH 84  /* length of the *prot[] array */
   49 
   50 /** 
   51  * ALERT: using hardcoded array lengths below,
   52  * if you change this array, then redefine PLENGTH
   53  * 
   54  * Currently http(prot[25]) and https(prot[26]) are 
   55  * the only supported protocols.  But all w3c supported 
   56  * protocols are listed for URL evaluation.
   57  */  
   58 static char *prot[] = {
   59   "about:",      "addrbook:",  "acap:",      "afp:",
   60   "afs:",        "callto:",    "chttp:",     "cid:",
   61   "clsid:",      "data:",      "date:",      "DAV:",
   62   "dns:",        "eid:",       "fax:",       "file:",
   63   "finger:",     "freenet:",   "ftp:",       "gopher:",
   64   "gsm:",        "h323:",      "h324:",      "hdl:",
   65   "hnews:",      "http:",      "https:",     "iioploc:",
   66   "ilu:",        "imap:",      "IOR:",       "irc:",
   67   "isbn:",       "java:",      "JavaRMI:",   "javascript:",
   68   "jdbc:",       "ldap:",      "lid:",       "lifn:",
   69   "livescript:", "lrq:",       "mailto:",    "mailserver:",
   70   "md5:",        "mid:",       "mocha:",     "modem:",
   71   "news:",       "nfs:",       "nntp:",      "opaquelocktoken:"
   72   "path:",       "phone:",     "pop:",       "pop3:",
   73   "printer:",    "prospero:",  "res:",       "rtsp:",
   74   "rvp:",        "rwhois:",    "rx:",        "sdp:",
   75   "sip:",        "shttp:",     "snews:",     "STANF:",
   76   "t120:",       "tel:",       "telephone:", "telnet:",
   77   "tip:",        "tn3270:",    "tv:",        "uuid:",
   78   "urn:",        "vemmi:",     "videotex:",  "view:",
   79   "wais:",       "whois++:",   "whodp:",     "z39.50r:",
   80   "z39.50s:"
   81   
   82 };
   83 
   84 /**
   85  * int value of the length of the protocol 
   86  * string passed to the function.
   87  */     
   88 int
   89 protocol_length( char *url )
   90 {
   91   int x;
   92   /** 
   93    * hardcoded protocol length!! see explanation above...
   94    */
   95   for( x = 0; x < PLENGTH; x ++ ){ 
   96     if( strncasecmp( url, prot[x], strlen( prot[x] )) == 0 )
   97       return strlen( prot[x] );
   98   } 
   99   return 0; 
  100 }
  101 
  102 /**
  103  * If a person edits an html file on the
  104  * Microsoft platform and copies it to a 
  105  * UNIX server, we are left to deal with 
  106  * ^M chars messing with our minds...
  107  */
  108 char *
  109 trim( char *str )
  110 {
  111   char *s=str;
  112  
  113   if( str == NULL ) return NULL;
  114  
  115   /* advance the ptr */
  116   while( *s ) s++;
  117  
  118   /* chomp the white space */
  119   while( str < s && isspace( *( s-1 )))
  120     *s-- = '\0';
  121  
  122   *s = '\0';
  123  
  124   return str; /* UNIX friendly */
  125 } 
  126 
  127 /**
  128  * boolean, returns true if the protocol is 
  129  * supported by siege, false if it is not.
  130  */ 
  131 int
  132 is_supported( char* url )
  133 {
  134   if( strncasecmp( url, prot[25], strlen( prot[25] )) == 0 )
  135     return TRUE;
  136   if( strncasecmp( url, prot[26], strlen( prot[26] )) == 0 )
  137     #ifdef HAVE_SSL
  138       return TRUE;
  139     #else
  140       return FALSE;
  141     #endif /* HAVE_SSL */
  142   else
  143     return FALSE;
  144 }
  145 
  146 /**
  147  * get_protocol
  148  * returns protocol char*
  149  */
  150 PROTOCOL
  151 get_protocol( const char *url )
  152 {
  153   if( strncasecmp( url, prot[25], strlen( prot[25] )) == 0 )
  154     return HTTP;
  155   if( strncasecmp( url, prot[26], strlen( prot[26] )) == 0 )
  156     #ifdef HAVE_SSL
  157       return HTTPS;
  158     #else
  159       return HTTP;
  160     #endif /* HAVE_SSL */
  161   else
  162     return UNSPRTD;
  163 }
  164 
  165 /**
  166  * get_default_port
  167  */
  168 int
  169 get_default_port( PROTOCOL p )
  170 {
  171   if( p == HTTP )
  172     return 80;
  173   if( p == HTTPS )
  174     #ifdef HAVE_SSL
  175       return 443;
  176     #else
  177       return 80;
  178     #endif /* HAVE_SSL */
  179   else
  180     return 80; 
  181 }
  182 
  183 char *
  184 url_encode( char *str )
  185 {
  186   int size = 0;
  187   char *ch, *bk;
  188   char *p, *buf;
  189   static char unsafe[]     = "<>{}#%|\"\\^~[]`@:\033";
  190   static char char2hex[16] = "0123456789ABCDEF";
  191 
  192   bk = ch  = str;
  193   do{
  194     if( strchr( unsafe, *ch ))
  195       size += 2;
  196     ch++; size ++;
  197   } while( *ch );
  198 
  199   buf = (char*)malloc( size +1 );
  200   p   = buf;
  201   ch  = bk;
  202   do{
  203     if( strchr( unsafe, *ch )){
  204       const char c = *ch;
  205       *p++ = '%';
  206       *p++ = char2hex[(c >> 4) & 0xf];
  207       *p++ = char2hex[c & 0xf];
  208     }
  209     else{
  210       *p++ = *ch;
  211     }
  212     ch ++;
  213   } while( *ch );
  214 
  215   *p = '\0';
  216   return( buf );
  217 }
  218 
  219 /**
  220  * process_post_data
  221  * populates URL->postdata with POST information
  222  * returns int
  223  */
  224 char *
  225 process_post_data( char *datap )
  226 {
  227   char *post_data;
  228 
  229   for( ; isspace(*datap); datap++ ){
  230     /* Advance past white space */
  231   }
  232   if( *datap == '<' ){
  233     /* Get Data from file */
  234   }
  235   else{
  236     return datap;
  237   }
  238 }
  239 
  240 URL
  241 build_url( char *url )
  242 {
  243   URL U;                   /* defined in setup.h  */
  244   int one, two, thr, fou;  /* placement counters. */ 
  245   char *post_cmd=NULL;     /* POST directive for server */
  246   char *tmp;
  247 
  248   post_cmd = strstr( url, " POST" ); 
  249   if( post_cmd != NULL ){
  250     /* How do we deal with handling the multi-headed url_t arrays */
  251     U.calltype = URL_POST;
  252     *post_cmd = 0;
  253     post_cmd += 5;
  254     U.postdata = (char*)strdup( process_post_data( post_cmd ));
  255     U.postlen  = strlen( U.postdata );
  256   }
  257   else{
  258     U.calltype   = URL_GET;
  259     U.postdata   = NULL;
  260     U.posttemp   = NULL;
  261     U.postlen    = 0;
  262   }
  263   if(( one = protocol_length( url )) > 0 && is_supported( url ) == TRUE ){
  264     one += 2;
  265   }
  266   else if(( one = protocol_length( url )) > 0 && is_supported( url ) == FALSE ){
  267     U.protocol = UNSPRTD;
  268     one += 2;
  269     perror( "unsupported protocol" );
  270   }
  271   else{
  272     /* we are dealing with who knows what */
  273     tmp = (char*)strstr( url, "://" );
  274     if( tmp != NULL ){
  275       one = (strlen(url) - (strlen(tmp) - 3 ));
  276     }
  277     else{
  278       one = 0;  /* no specified protocol, assuming http: */
  279     }
  280   }
  281 
  282   two = one;
  283   while( url[two] && url[two] != ':' && url[two] != '/' ) two++; 
  284 
  285   if( url[two] == ':' ){
  286     fou = two;
  287     while( url[two] && url[two] != '/' ){
  288       two++;
  289     }
  290   }
  291   else{ fou = two; }
  292   if( url[two] == '/' ){ thr = two; }
  293   else                 { thr = strlen( url ); } 
  294 
  295   /* here we piece it all together */
  296   if( one == 0 ){
  297     U.protocol = HTTP;
  298   }
  299   else{
  300     U.protocol = get_protocol( url );
  301   }
  302   U.hostname   = (char*)strdup(substring( url, one, ( fou - one )));
  303   if( fou == two ){
  304     U.port = get_default_port( U.protocol );
  305   }
  306   else{
  307     U.port = atoi(substring( url, fou+1, (thr-(fou+1))));
  308   }
  309   if(( U.pathname = (char *)strdup(substring( url, thr, strlen( url )))) == NULL ){
  310     U.pathname = (char *)strdup( "/" ); 
  311   }
  312   U.pathname = (strlen(U.pathname)==0)?strcpy(U.pathname, "/"):U.pathname; 
  313   trim( U.pathname );
  314 
  315   free( url ); 
  316   return( U );
  317 }
  318 
  319 /**
  320  * add_url
  321  * parses char * then populates and 
  322  * returns a URL with appropriate data.
  323  */
  324 URL
  325 add_url( char *url )
  326 {
  327 
  328   return build_url( url );
  329 }
  330 
  331