storeurl_rewrite是squid非常有用的功能,对同一个目标文件的访问,产生的url不一定是相同的,而squid是根据url进行缓存的,所以就会有大量的冗余的缓存,浪费了空间和squid的处理能力。squid可以调用外部程序进行storeurl_rewrite,其中jesred非常强大,它是用C写的,执行速度高于用shell完成的同样功能的脚本。 

我使用jesred进行storeurl重写的时候遇到了一些问题,而这种小程序的说明文档又很少,所以只好自己阅读了一下jesred的代码,发现了下面的问题。

squid2.7使用向rewrite使用的stdout为

http://xxx.test.com: $squid_listening_port /p_w_picpaths/course_simple.png $client_ip/- - GET - myip=$server_ip myport=$squid_listening_port

而jesred处理的只能是
http://xxx.test.com: $squid_listening_port /p_w_picpaths/course_simple.png $client_ip/- - GET
这部分
 
#这里   $squid_listening_port表示squid监听的端口,默认为3128
            $client_ip 表示访问用户的IP地址
            $server_ip是squid主机的IP地址
下面的处理正确的处理了squid的输出



jesred的parse_buff函数
 
  
  1. int 
  2. parse_buff(char *buff, char **url, char **src_addr, char **ident, 
  3.              char **method, ip_acl *ip, pattern_item *p) 
  4.     int c, i; 
  5.     struct in_addr address; 
  6.      //char *token, *new_token; 
  7.     char *token, *new_token , *wc_token//xtong add a new wc_token; 
  8.     char *end[4]; 
  9.  
  10.     c = 0;                                           
  11.     token = strchr(buff,' ');                  //token指向buff中第一个空格的位置,就是url的结束处 
  12.     if ( token ) {       /* URL */            //判断token是否为null,后面的判断会输出incorrect input 
  13.         c++; 
  14.         *token = '\0';                           // \0代表字符串的结束 
  15.         end[0] = token;                         //end数组是buff中空格的位置 
  16.         *url = buff;                               //将截取过的buff传递给url,也就是squid stdin的值 
  17.         new_token = strchr(++token,' ');  //寻找下一个空格的位置 
  18.         if (new_token) {     /* Address */ 
  19.             c++; 
  20.             *new_token = '\0'
  21.             end[1] = new_token; 
  22.             *src_addr = token; 
  23.             token = strchr(++new_token,' '); 
  24.             if (token) {      /* Ident */ 
  25.                 c++; 
  26.                 *token = '\0'
  27.                 end[2] = token; 
  28.                 *ident = new_token; 
  29.                 wc_token = ++token;  //xtong add this 
  30.                  //new_token = strchr(++token,'\n'); 
  31.                 new_token = strchr(wc_token,' ') ? strchr(wc_token,' ') : strchr(wc_token,'\n'); //xtong add this 
  32.                 if (new_token) { 
  33.                     c++; 
  34.                     *new_token = '\0'
  35.                     end[3] = new_token; 
  36.                     *method = token; 
  37.                 } 
  38.             } 
  39.         } 
  40.     } 
  41.  
  42.  
  43.     if(c != 4) { 
  44.         for(i = 0; i < c; i++) { 
  45.             if ( end[i] ) 
  46.                 *end[i] = ' '
  47.         } 
  48.         log(ERROR, "incorrect input (%d): %s", c, buff); 
  49.         return 1; 
  50.     } 
  51. #ifdef DEBUG 
  52.     log(DEBG, "Request: %s %s %s %s\n", *url, *src_addr, *ident, *method); 
  53. #endif 
  54.  
  55.     /* all methods must be GET or ICP_QUERY */ 
  56.     c = 0; 
  57.     if (allow_siblings && (! strcmp(*method, "ICP_QUERY")) ) 
  58.             c--; 
  59.     if( strcmp(*method, "GET") ) 
  60.         c++; 
  61.       if ( c ) { 
  62. #ifdef DEBUG 
  63.         for(c = 0; c < 4; c++) { 
  64.             if ( end[c] ) 
  65.                 *end[c] = ' '
  66.         } 
  67.         log(DEBG, "method not \"GET\" %s\n", buff); 
  68. #endif 
  69.         return 1; 
  70.     } 

其中红色部分为新加上去的代码,就可以简单的解决问题。