怎样让squid follow 302的响应

http://blog.chinaunix.net/uid-8474831-id-3463461.html

大家都知道,http的302响应,是一种跳转。比如用户访问url:

http://www.sina.com/ 时,


点击(此处)折叠或打开

  1. GET / HTTP/1.1
  2. User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
  3. Host: www.sina.com
  4. Accept: */*

sina的服务器会返回一个301的响应(302也类似),让你的浏览器去访问 http://www.sina.com.cn/


点击(此处)折叠或打开

  1. HTTP/1.0 301 Moved Permanently
  2. Date: Sun, 06 Jan 2013 10:16:17 GMT
  3. Server: Apache
  4. Location: http://www.sina.com.cn/
  5. Cache-Control: max-age=3600
  6. Expires: Sun, 06 Jan 2013 11:16:17 GMT
  7. Vary: Accept-Encoding
  8. Content-Length: 231
  9. Content-Type: text/html; charset=iso-8859-1
  10. Age: 3015
  11. X-Cache: HIT from sh201-12.sina.com.cn
  12. Connection: close


squid在处理301或302的响应时,只能把这个30x响应给到客户端,然后由客户端发起对http://www.sina.com.cn/ 的请求。

那么,能不能让squid在客户端请求http://www.sina.com/ 的时候,直接返回http://www.sina.com.cn/ 的200内容呢?

其实非常简单,只要几十行代码就搞定!

接下来我就手把手教给你怎么做

 

首先,在client_side.c里面,加上这个函数clientProcess302Header


点击(此处)折叠或打开

  1. //add by xiaosi start

  2. static void clientProcess302Header(void *data, HttpReply * rep)

  3. {

  4.         clientHttpRequest *http = data;

  5.         HttpHeaderEntry *location_entry = httpHeaderFindEntry(&rep->header, HDR_LOCATION);

  6.         if(!location_entry)

  7.         { 

  8.                 http->orig_header_callback(data, rep);

  9.                 return;

  10.         } 

  11.  

  12.         char *location = xstrdup(strBuf(location_entry->value));

  13.         http->location = location;

  14.         http->out.offset = 0;

  15.         http->out.size = 0;

  16.         StoreEntry *e; 

  17.         if ((= http->entry))

  18.         { 

  19.                 http->entry = NULL;

  20.                 storeClientUnregister (http->sc, e, http);

  21.                 http->sc = NULL;

  22.                 storeUnlockObject (e);

  23.         } 

  24.         /* old_entry might still be set if we didn't yet get the reply

  25.          * code in clientHandleIMSReply() */

  26.         if ((= http->old_entry))

  27.         { 

  28.                 http->old_entry = NULL;

  29.                 storeClientUnregister (http->old_sc, e, http);

  30.                 http->old_sc = NULL;

  31.                 storeUnlockObject (e);

  32.         } 

  33.         clientRedirectStart(http);

  34. }

  35. //add by xiaosi end


然后修改storeClientCopyHeaders,改成下面的样子。注意add by xiaosi注释里面的代码


点击(此处)折叠或打开

  1. void

  2. storeClientCopyHeaders(store_client * sc, StoreEntry * e, STHCB * callback, void *callback_data)

  3. {

  4.     clientHttpRequest *http = callback_data;

  5. //add by xiaosi start

  6. // http->header_callback = callback;

  7.     http->header_callback = clientProcess302Header;

  8.     http->orig_header_callback = callback;

  9. //add by xiaosi end

  10.     http->header_entry = e;

  11.     storeClientCopy(http->sc, e, 0, 0, STORE_CLIENT_BUF_SZ, memAllocate(MEM_STORE_CLIENT_BUF), storeClientCopyHeadersCB, http);

  12. }


然后修改client_side_rewrite.c里面的clientRedirectStart,注意add by xiaosi注释里面的代码


点击(此处)折叠或打开

  1. void

  2. clientRedirectStart(clientHttpRequest * http)

  3. {

  4.     debug(33, 5) ("clientRedirectStart: '%s'\n", http->uri);

  5.     //add by xiaosi start

  6.     if(http->location)

  7.     { 

  8.             http->redirect_state = REDIRECT_PENDING;

  9.             char *location = xstrdup(http->location);

  10.             safe_free(http->location);

  11.             clientRedirectDone (http, location);

  12.             safe_free(location);

  13.             return;

  14.     } 

  15.     //add by xiaosi end

  16.     if (Config.Program.url_rewrite.command == NULL) {

  17.         clientRedirectDone(http, NULL);

  18.         return;

  19.     } 

  20.     if (Config.accessList.url_rewrite) {

  21.         http->acl_checklist = clientAclChecklistCreate(Config.accessList.url_rewrite, http);

  22.         aclNBCheck(http->acl_checklist, clientRedirectAccessCheckDone, http);

  23.     } else {

  24.         redirectStart(http, clientRedirectDone, http);

  25.     } 

  26. }

注意,我们在clientHttpRequest中加入了2个成员,location和orig_header_callback。在structs.h的struct _clientHttpRequest 结构的末尾加上他们。注意add by xiaosi注释里面的代码



点击(此处)折叠或打开

  1. struct _clientHttpRequest {

  2. // bla bla bla…

  3. // bla bla bla…

  4. // bla bla bla…

  5.  

  6.     dlink_node active;

  7.     squid_off_t maxBodySize;

  8.     STHCB *header_callback; /* Temporarily here for storeClientCopyHeaders */

  9.     StoreEntry *header_entry; /* Temporarily here for storeClientCopyHeaders */

  10.     int is_modified;

  11.     //add by xiaosi start

  12.     char *location;

  13.     STHCB *orig_header_callback; /* Temporarily here for storeClientCopyHeaders */

  14. //add by xiaosi end

  15. };

 

基本上就完工了。重新编译安装,重启squid。再通过squid请求一下http://www.sina.com/ 是不是看到200了呢?


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值