<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          給 JDK 報了一個 P4 的 Bug,結(jié)果居然……

          共 6224字,需瀏覽 13分鐘

           ·

          2020-09-28 20:17

          背景

          分享一下之前踩的一個坑,背景是這樣的:

          我們的項目依賴于一個外部服務(wù),該外部服務(wù)提供 REST 接口供我方調(diào)用,這是很常見的一個場景。本地和測試環(huán)境測試都沒有問題,一切就緒上了生產(chǎn)后,程序調(diào)用接口就總是網(wǎng)絡(luò)不通。

          需要說明的是本地、測試環(huán)境、生產(chǎn)環(huán)境通過不同的域名訪問該外部服務(wù)。生產(chǎn)程序調(diào)用不通,神奇的是在生產(chǎn)環(huán)境通過?curl?等命令卻能夠正常調(diào)用對方接口。

          What??


          這 TM 就神奇了,唯一不同的就是發(fā)起 HTTP 請求的客戶端了,估計就是 http客戶端有問題了?通過最后排查發(fā)現(xiàn),居然發(fā)現(xiàn)了一枚 “JDK 的 bug”,然后石頭就提交到了 JDK 的官網(wǎng)……

          下面我們就來重現(xiàn)一下這個問題。

          ?

          server 端準備

          這里用 Nginx 模擬了一下 上文提到的 REST 服務(wù),假設(shè)調(diào)用正常返回?"Hello, World\n",Nginx 配置如下:

          server?{
          ????listen????80;
          ????server_name?test_1.tanglei.name;
          ????location?/testurl?{
          ????????add_header?Content-Type?'text/plain;?charset=utf-8';
          ????????return?200?"Hello,?World\n";
          ????}
          }


          ?

          不同的 client 請求

          下面用不同的 Http client (分別用命令行curl,python的?requests包,和 Java 的 URL 等嘗試)去請求。

          • curl?請求,正常。

          [root@VM_77_245_centos?vhost]#?curl?-i?"http://test_1.tanglei.name/testurl"
          HTTP/1.1?200?OK
          Server:?nginx
          Content-Length:?13
          Connection:?keep-alive
          Content-Type:?text/plain;?charset=utf-8

          Hello,?World
          [root@VM_77_245_centos?vhost]
          #
          • python requests?正常。

          >>>?import?requests
          >>>?r?=?requests.get("http://test_1.tanglei.name/testurl")
          >>>?r.text
          u'Hello,?World\n'
          • Java 的?java.net.URLConnection?同樣正常。

          static?String?getContent(java.net.URL?url)?throws?Exception?{
          ????java.net.URLConnection?conn?=?url.openConnection();
          ????java.io.InputStreamReader?in?=?new?java.io.InputStreamReader(conn.getInputStream(),?"utf-8");
          ????java.io.BufferedReader?reader?=?new?java.io.BufferedReader(in);????
          ????StringBuilder?sb?=?new?StringBuilder();
          ????int?c?=?-1;
          ????while?((c?=?reader.read())?!=?-1)?{
          ????????sb.append((char)c);
          ????}
          ????reader.close();
          ????in.close();
          ????String?response?=?sb.toString();
          ????return?response;
          }

          上面的這個方法?String getContent(java.net.URL url)?傳入一個構(gòu)造好的?java.net.URL?然后 get 請求,并以?String?方式返回 response。

          String?srcUrl?=?"http://test_1.tanglei.name/testurl";
          java.net.URL?url?=?new?java.net.URL(srcUrl);
          System.out.println("\nurl?result:\n"?+?getContent(url));?//?OK

          上面的語句輸出正常,結(jié)果如下:

          url?result:
          Hello,?World

          這就尼瑪神奇了吧。看看我們程序中用的 httpclient 的實現(xiàn),結(jié)果發(fā)現(xiàn)是有用?java.net.URI,心想,這不至于吧,用 URI 就不行了么。


          換?java.net.URI?試試? (這里不展開講URL和URI的區(qū)別聯(lián)系了,可以簡單的認為URL是URI的一個子集,詳細的可參考?URI、URL 和 URN[1],?wiki URI[2]

          直接通過java.net.URI構(gòu)造,再調(diào)用?URI.toURL?得到?URL,調(diào)用同樣正常。

          關(guān)鍵的來了,httpclient 源碼中用的構(gòu)造函數(shù)是另外一個:

          URI(String?scheme,?String?host,?String?path,?String?fragment)
          Constructs?a?hierarchical?URI?from?the?given?components.

          我用這個方法構(gòu)造URI,會構(gòu)造失敗:

          new?java.net.URI(uri.getScheme(),?uri.getHost(),?uri.getPath(),?null)?error:?protocol?=?http?host?=?null
          new?java.net.URI(url.getProtocol(),?url.getHost(),?url.getPath(),?null)?error:?Illegal?character?in?hostname?at?index?11:?http://test_1.tanglei.name/testurl

          所以問題發(fā)現(xiàn)了,我們的項目中依賴的第三方 httpclient包底層用到了?java.net.URI,恰好在?java.net.URI?中是不允許以下劃線(_)作為?hostname?字段的。

          即?uri.getHost()?和?uri.toURL().getHost()?居然能不相等。


          ?

          這是 JDK 的 Bug 吧?

          有理由懷疑,這是 JDK 的 Bug 吧?


          從官網(wǎng)上還真找到了關(guān)于包含下劃線作為hostname的bug提交issue,戳這里 JDK-8132508 : Bug JDK-8029354 reproduces with underscore in hostname[3],然后發(fā)現(xiàn)該 "bug" reporter 的情況貌似跟我的差不多,只不過引爆bug的點不一樣。

          該 "bug" reviewer 最后以 "Not an Issue" 關(guān)閉,給出的理由是:

          RFC 952 disallows _ underscores in hostnames. So, this is not a bug.

          確實,rfc952[4]?明確明確說了域名只能由 字母?(A-Z)、 數(shù)字(0-9)、 減號?(-)?和 點?(.)?組成。

          那 OK 吧,既然明確規(guī)定了 hostname 不能包含下劃線,為啥?java.net.URL?確允許呢?

          造成?java.net.URI?和?java.net.URL?在處理 hostname 時的標準不一致,且本身?java.net.URI?在構(gòu)造的時候也帶了 "有色"眼鏡,同一個url字符串 通過靜態(tài)方法?java.net.URI.create(String)?或者通過帶1個參數(shù)的構(gòu)造方法?java.net.URI(String)?都能成功構(gòu)造出 URI 的實例,但通過帶4個參數(shù)的構(gòu)造方法就不能構(gòu)造了。

          要知道,在 coding 過程中,盡早反饋異常信息更有利于軟件開發(fā)持續(xù)迭代的過程。我們在開發(fā)過程中也應(yīng)該遵循這一點原則。

          于是我就去 JDK 官網(wǎng)提交了一個 bug,大意是說?java.net.URI?和?java.net.URL?在處理hostname的時候標準不一致,容易使開發(fā)人員埋藏一些潛在的bug,同時也還把這個問題反饋到?stackoverflow[5]?了

          I am wondering, if hostname with underscore is not valid, why the result is differrent between java.net.URI and java.net.URL? Is it a bug or a feature? Here is the example.
          java.net.URL url = new java.net.URL("http://test_1.tanglei.name");?
          System.out.println(url.getHost()); //test_1.tanglei.name?
          java.net.URI uri = new java.net.URI("http://test_1.tanglei.name");?
          System.out.println(uri.getHost()); //null

          這個 JDK bug issue 詳細信息見?JDK-8170265 : underscore is allowed in java.net.URL while not in java.net.URI[6]openjdk JDK-8170265[7]

          經(jīng)過初步 Review,被認為是一個 P4 的 Bug,說的是?java.net.URL?遵循的是?RFC 2396?規(guī)范,確實不允許含有下劃線的 hostname,java.net.URI?做到了, 而?java.net.URL?沒有做到。


          重點來了,然后,卻被另外一個 Reviewer 直接個斃了。給出的原因是?java.net.URL?構(gòu)造方法中,API 文檔中說了本來也不會做驗證即?No validation of the inputs is performed by this constructor.??在線 api doc 戳這里[8]?(可以點連接,進去搜索關(guān)鍵字 "No validation")



          當初沒有收到及時反饋,就沒有來得及懟回去。

          其實就算 "No validation of the inputs is performed by this constructor." 是合理的,里面也只有3個構(gòu)造函數(shù)有這樣的說明,按照這樣的邏輯是不是說另外的構(gòu)造函數(shù)有驗證呢..... (示例中的默認的構(gòu)造函數(shù)都沒有說呀)

          這里有java.net.URL 的源碼[9],看興趣的同學(xué)可以看看。

          恩,以上就是結(jié)論了。

          不過,反正我自己感覺目前 Java API 關(guān)于這里的設(shè)計不太合理,歡迎大家討論。

          我在SO提問的這個回答[10]比較有意思,哈哈。

          The review is somewhat terse, but the reviewer's point is the URL constructor is behaving in accordance with its specification. Since the specification explicitly states that no validation is performed, this is not a bug in the code. This is indisputable.
          What he didn't spell out is that fixing this inconsistency (by changing the URL class specification) would break lots of peoples' 20+ year old code Java code. That would be a really bad idea. It can't happen.
          So ... this inconsistency is a "feature".

          附本文鏈接:

          [1] URI、URL 和 URN: https://www.ibm.com/developerworks/cn/xml/x-urlni.html

          [2]wiki URI: https://en.wikipedia.org/wiki/Uniform_Resource_Identifier

          [3]JDK-8132508 : Bug JDK-8029354 reproduces with underscore in hostname: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8132508

          [4]rfc952: https://tools.ietf.org/html/rfc952

          [5]stackoverflow: https://stackoverflow.com/questions/44226003/conflicts-between-java-net-url-and-java-net-uri-when-dealing-with-hostname-conta

          [6]JDK-8170265 : underscore is allowed in java.net.URL while not in java.net.URI: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8170265

          [7]openjdk JDK-8170265: https://bugs.openjdk.java.net/browse/JDK-8170265

          [8]在線 api doc 戳這里: https://docs.oracle.com/javase/8/docs/api/java/net/URL.html

          [9]這里有java.net.URL 的源碼: http://www.docjar.com/html/api/java/net/URL.java.html

          [10]我在SO提問的這個回答: https://stackoverflow.com/questions/44226003/conflicts-between-java-net-url-and-java-net-uri-when-dealing-with-hostname-conta?answertab=active#tab-to



          有道無術(shù),術(shù)可成;有術(shù)無道,止于術(shù)

          歡迎大家關(guān)注Java之道公眾號


          好文章,我在看??

          瀏覽 132
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲天堂免费视频 | 人人澡人人人妻人人奭 | 丁香六月婷婷综合缴 | 日韩爱爱爱| 大鸡巴成人性爱在线视频 |