season22 发表于 2018-12-27 10:08:22

关于squid2.7 使用jesred进行storeurl_rewrite

   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/images/course_simple.png $client_ip/- - GET - myip=$server_ip myport=$squid_listening_port




而jesred处理的只能是

http://xxx.test.com:$squid_listening_port/images/course_simple.png $client_ip/- - GET

这部分



#这里$squid_listening_port表示squid监听的端口,默认为3128

         $client_ip 表示访问用户的IP地址

         $server_ip是squid主机的IP地址

下面的处理正确的处理了squid的输出










jesred的parse_buff函数




[*]int
[*]parse_buff(char *buff, char **url, char **src_addr, char **ident,
[*]             char **method, ip_acl *ip, pattern_item *p)
[*]{
[*]    int c, i;
[*]    struct in_addr address;
[*]   //char *token, *new_token;
[*]    char *token, *new_token , *wc_token; //xtong add a new wc_token;
[*]    char *end;
[*]
[*]    c = 0;                                          
[*]    token = strchr(buff,' ');                  //token指向buff中第一个空格的位置,就是url的结束处
[*]    if ( token ) {       /* URL */            //判断token是否为null,后面的判断会输出incorrect input
[*]      c++;
[*]      *token = '\0';                           // \0代表字符串的结束
[*]      end = token;                         //end数组是buff中空格的位置
[*]      *url = buff;                               //将截取过的buff传递给url,也就是squid stdin的值
[*]      new_token = strchr(++token,' ');//寻找下一个空格的位置
[*]      if (new_token) {   /* Address */
[*]            c++;
[*]            *new_token = '\0';
[*]            end = new_token;
[*]            *src_addr = token;
[*]            token = strchr(++new_token,' ');
[*]            if (token) {      /* Ident */
[*]                c++;
[*]                *token = '\0';
[*]                end = token;
[*]                *ident = new_token;
[*]                wc_token = ++token;//xtong add this
[*]               //new_token = strchr(++token,'\n');
[*]                new_token = strchr(wc_token,' ') ? strchr(wc_token,' ') : strchr(wc_token,'\n'); //xtong add this
[*]                if (new_token) {
[*]                  c++;
[*]                  *new_token = '\0';
[*]                  end = new_token;
[*]                  *method = token;
[*]                }
[*]            }
[*]      }
[*]    }
[*]
[*]
[*]    if(c != 4) {
[*]      for(i = 0; i < c; i++) {
[*]            if ( end )
[*]                *end = ' ';
[*]      }
[*]      log(ERROR, &quot;incorrect input (%d): %s&quot;, c, buff);
[*]      return 1;
[*]    }
[*]#ifdef DEBUG
[*]    log(DEBG, &quot;Request: %s %s %s %s\n&quot;, *url, *src_addr, *ident, *method);
[*]#endif
[*]
[*]    /* all methods must be GET or ICP_QUERY */
[*]    c = 0;
[*]    if (allow_siblings && (! strcmp(*method, &quot;ICP_QUERY&quot;)) )
[*]            c--;
[*]    if( strcmp(*method, &quot;GET&quot;) )
[*]      c++;
[*]      if ( c ) {
[*]#ifdef DEBUG
[*]      for(c = 0; c < 4; c++) {
[*]            if ( end )
[*]                *end = ' ';
[*]      }
[*]      log(DEBG, &quot;method not \&quot;GET\&quot; %s\n&quot;, buff);
[*]#endif
[*]      return 1;
[*]    }

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



页: [1]
查看完整版本: 关于squid2.7 使用jesred进行storeurl_rewrite