Chinaunix首页 | 论坛 | 博客
  • 博客访问: 143948
  • 博文数量: 7
  • 博客积分: 67
  • 博客等级: 民兵
  • 技术积分: 184
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-04 05:16
文章分类

全部博文(7)

文章存档

2013年(7)

我的朋友

分类: LINUX

2013-01-07 11:20:33

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

时,


点击(此处)折叠或打开

  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:
  4. Accept: */*


sina的服务器会返回一个301的响应(302也类似),让你的浏览器去访问


点击(此处)折叠或打开

  1. HTTP/1.0 301 Moved Permanently
  2. Date: Sun, 06 Jan 2013 10:16:17 GMT
  3. Server: Apache
  4. Location: http://.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在处理301302的响应时,只能把这个30x响应给到客户端,然后由客户端发起对 的请求。

那么,能不能让squid在客户端请求 的时候,直接返回 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 ((e = 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 ((e = 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个成员,locationorig_header_callback。在structs.hstruct _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请求一下 是不是看到200了呢?

阅读(4785) | 评论(1) | 转发(1) |
0

上一篇:没有了

下一篇:Squid日志中为何会有HIT,并且DIRECT的记录?

给主人留下些什么吧!~~

tym88652013-08-29 22:43:29

小斯兄,请教个问题,比如我用squid做正向代理。第一次访问百度,squid肯定没有缓存,需要去源服务去取。但是第二次再访问百度的话,squid应该是有缓存了,可是clientProcessRequest2中查找storeGetPublicByRequest还是返回空。然后我就直接在clientCacheHit打断点,继续访问百度,可是页面已经显示出来,程序还是没有走到clientCacheHit这个函数。这是为什么呢?小斯兄,有空能不能在nginx源码群里冒个泡