/[monit]/monit/util.c
ViewVC logotype

Diff of /monit/util.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.135 by hauk, Sun Dec 19 00:31:51 2004 UTC revision 1.136 by hauk, Mon Dec 20 05:39:34 2004 UTC
# Line 102  Line 102 
102  /* Private prototypes */  /* Private prototypes */
103  static char x2c(char *hex);  static char x2c(char *hex);
104  static char *is_str_defined(char *);  static char *is_str_defined(char *);
 static int is_unsafe(unsigned char *);  
105  static void printevents(unsigned int);  static void printevents(unsigned int);
106    static int is_url_unsafe(unsigned char *);
107    
108    
109  /**  /**
# Line 167  char *Util_basename(char* path) { Line 167  char *Util_basename(char* path) {
167    * @return The chomped string    * @return The chomped string
168    */    */
169  char *Util_chomp(char *s) {  char *Util_chomp(char *s) {
170    for (;(s&&*s); s++) {  
171      ASSERT(s);
172      
173      for (; *s; s++) {
174      if (('\r' == *s) || ('\n' == *s)) {      if (('\r' == *s) || ('\n' == *s)) {
175        *s= 0; break;        *s= 0; break;
176      }      }
177    }    }
178    
179    return s;    return s;
180      
181  }  }
182    
183    
184  /**  /**
185   * Remove leading and trailing space from the string   * Remove leading and trailing space from the string
186   * @param s A string   * @param s A string
187   * @return s with leading and trailing spaces removed   * @return s with leading and trailing spaces removed
188   */   */
189  char *Util_trim(char *s) {  char *Util_trim(char *s) {
190    
# Line 1191  char *Util_urlEncode(char *uri) { Line 1196  char *Util_urlEncode(char *uri) {
1196    str= (unsigned char *)xmalloc(3 * strlen(uri) + 1);    str= (unsigned char *)xmalloc(3 * strlen(uri) + 1);
1197    
1198    for(x = 0, y = 0; uri[x]; x++, y++) {    for(x = 0, y = 0; uri[x]; x++, y++) {
1199      if(is_unsafe((unsigned char*) &uri[x])) {      if(is_url_unsafe((unsigned char*) &uri[x])) {
1200        str[y++] = '%';        str[y++] = '%';
1201        str[y++] = hexchars[(unsigned char) uri[x] >> 4];        str[y++] = hexchars[(unsigned char) uri[x] >> 4];
1202        str[y] = hexchars[(unsigned char) uri[x] & 0xf];        str[y] = hexchars[(unsigned char) uri[x] & 0xf];
# Line 1229  char *Util_urlDecode(char *url) { Line 1234  char *Util_urlDecode(char *url) {
1234    
1235    
1236  /**  /**
  * Parse an url string.  
  * @param string an url string  
  * @return A pointer to the structure which describes particular url parts  
  */  
 Url_T Util_parseURL(char *string) {  
   
   int            len;  
   char          *start;  
   char          *stop;  
   char          *current;  
   char          *url_beg;  
   char          *url_end;  
   Url_T          u;  
   UrlProtocol_T *p = protocol;  
   
   ASSERT(string);  
   
   NEW(u);  
   
   u->url = string;  
   
   url_beg = start = Util_urlEncode(string);  
   url_end =  url_beg + strlen(url_beg);  
   
   /* Parse protocol */  
   while((*p).id != PROTOCOL_NULL)  
   {  
     len = strlen((*p).pattern);  
     if( !strncasecmp(url_beg, (*p).pattern, len) )  
     {  
       if((*p).id == PROTOCOL_HTTPS && !have_ssl())  
       {  
         log("HTTPS protocol not supported -- SSL support disabled\n");  
         goto error;  
       }  
       u->protocol = (*p).protocol;  
       start += len;  
       break;  
     }  
     p++;  
   }  
   if((*p).id == PROTOCOL_NULL)  
   {  
     log("Unknown protocol -- %s\n", string);  
     goto error;  
   }  
   
   /* Parse credentials if any */  
   stop = strpbrk (start, "@/?");  
   if(stop && stop != start && *stop == '@')  
   {  
     current = memchr(start, ':', stop - start);  
     if(current != start)  
     {  
       if(current)  
       {  
         u->user = xstrndup(start, current - start);  
         u->password = xstrndup(current + 1, stop - current - 1);  
       }  
       else  
       {  
         u->user = xstrndup(start, stop - start);  
       }  
     }  
     start = ++stop;  
   }  
   
   /* Parse host */  
   stop = strpbrk(start, ":/?");  
   if(!stop)  
   {  
     stop = url_end;  
   }  
   else if(stop == start)  
   {  
     log("Host not specified -- %s\n", string);  
     goto error;  
   }  
   u->hostname = xstrndup(start, stop - start);  
     
   start = stop;  
   
   /* Parse port */  
   if(*start == ':')  
   {  
     ++start;  
     stop = strpbrk(start, "/?");  
     if(!stop)  
     {  
       stop = url_end;  
     }  
     else if(stop == start)  
     {  
       log("Invalid port specification  -- %s\n", string);  
       goto error;  
     }  
     u->port = atoi(start);  
     start = stop;  
   }  
   else  
   {  
     u->port = (*p).port;  
   }  
   
   /* Parse path */  
   if(*start == '/')  
   {  
     current = strpbrk(start, "?");  
     stop = current?current:url_end;  
     u->path = xstrndup(start, stop - start);  
     start = stop;  
   }  
   else  
   {  
     u->path = xstrdup("/");  
   }  
   
   /* Parse query */  
   if(*start == '?' && start + 1 != url_end)  
   {  
     ++start;  
     u->query = xstrndup(start, url_end - start);  
   }  
   
   FREE(url_beg);  
   return u;  
   
 error:  
   gc_url(&u);  
   FREE(url_beg);  
   return NULL;  
 }  
   
   
 /**  
1237   * @return a Basic Authentication Authorization string (RFC 2617),   * @return a Basic Authentication Authorization string (RFC 2617),
1238   * with credentials from the Run object, NULL if credentials are not defined.   * with credentials from the Run object, NULL if credentials are not defined.
1239   */   */
# Line 1405  char *Util_getBasicAuthHeader() { Line 1275  char *Util_getBasicAuthHeader() {
1275    
1276    
1277  /**  /**
1278     * Creates a new String by merging a formated string and a variable
1279     * argument list. The caller must free the returned String.
1280     * @param s A format string
1281     * @return The new String or NULL if the string could not be created
1282     */
1283    char *Util_getString(const char *s, ...) {
1284    
1285      long l;
1286      char *v;
1287      va_list ap;
1288      
1289      ASSERT(s);
1290      
1291      if(s==NULL)
1292        return NULL;
1293      va_start(ap, s);
1294      v= Util_formatString(s, ap, &l);
1295      va_end(ap);
1296    
1297      return v;
1298    
1299    }
1300    
1301    
1302    /**
1303   * Do printf style format line parsing   * Do printf style format line parsing
1304   * @param s format string   * @param s format string
1305   * @param ap variable argument list   * @param ap variable argument list
# Line 1722  static char *is_str_defined(char *s) { Line 1617  static char *is_str_defined(char *s) {
1617   * @param c A unsigned char   * @param c A unsigned char
1618   * @return TRUE if the char is in the set of unsafe URL Characters   * @return TRUE if the char is in the set of unsafe URL Characters
1619   */   */
1620  static int is_unsafe(unsigned char *c) {  static int is_url_unsafe(unsigned char *c) {
1621    int i;    int i;
1622    static unsigned char unsafe[]= "<>\"#{}|\\^~[]`";    static unsigned char unsafe[]= "<>\"#{}|\\^~[]`";
1623        

Legend:
Removed from v.1.135  
changed lines
  Added in v.1.136

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26