url的规范化问题

今天公司的项目中代码中被同事发现一个bug,是一个url相对路径转换为绝对路径函数里的问题。

之前直接copy了开源项目crawler里的NormalizeLink()函数,这个函数内部也是利用.net frameword 中的Uri类进行处理的,多了几步判断处理。

不过当url参数中含有已经经过urlencode处理的字符时,结果就会出现乱码。猜测Uri内部对url 进行了urldecode处理,而urldecode的encoding与之前urlencode的encoding不同,所以导致乱码。在reflector粗略看了下Uri的源码发现实现有点复杂,也就没继续了。

呵呵,在面对复杂的不规范的web环境下,拿来的东西终究靠不住。

晚上回来随手搜集下了url规范化相关的资料:

1,这是rfc3986的Syntax Components段摘取的

The generic URI syntax consists of a hierarchical sequence of
   components referred to as the scheme, authority, path, query, and
   fragment.
      URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
      hier-part   = "//" authority path-abempty
                  / path-absolute
                  / path-rootless
                  / path-empty
   The scheme and path components are required, though the path may be
   empty (no characters).  When authority is present, the path must
   either be empty or begin with a slash ("/") character.  When
   authority is not present, the path cannot begin with two slash
   characters ("//").  These restrictions result in five different ABNF
   rules for a path (Section 3.3), only one of which will match any
   given URI reference.

   The following are two example URIs and their component parts:

         foo://example.com:8042/over/there?name=ferret#nose
         \_/   \______________/\_________/ \_________/ \__/
          |           |            |            |        |
       scheme     authority       path        query   fragment
          |   _____________________|__
         / \ /                        \
         urn:example:animal:ferret:nose

  (吐糟一下,添加了引用之后光标就跳不了引用框之后了,在html模式下插入p,才解决,fuck)

2,这个从一网友blog中摘取的不规范url的例子:

1,相对路径

# 同目錄下的 step2.aspx 頁面

step2.aspx

# 網站根目錄下的 index.aspx 頁面

/index.aspx

# 上層目錄的 sitemap.aspx 頁面

../sitemap.aspx

# 上兩層目錄的 default.htm 頁面

http://www.cnblogs.com/default.htm

# 上層目錄下的 images 目錄下的 dot 目錄下的 red.gif 檔案

../images/dot/red.gif

  2,忽略 scheme、 authority、path 部分

 # 跳到第 2 頁

<a href="?pageNo=2">第 2 頁</a>

  3,忽略 scheme、 authority、path 、query部分

# 在页面内跳转到顶部
<a href="#top">Top</a>

     4,忽略 scheme 的部分

  <img src="//l.yimg.com/tw.yimg.com/i/tw/hp/spirit/yahoo_logo.gif" />

考虑自己写url规范函数,今天就到此为止了。

posted @ 2012-02-23 01:44  花生!~~  阅读(433)  评论(0编辑  收藏  举报