【问题标题】:Why java.net.URI isAbsolute() method return true for an URI that have fragment?为什么 java.net.URI isAbsolute() 方法为具有片段的 URI 返回 true?
【发布时间】:2021-07-12 18:27:54
【问题描述】:

我的目标是遵循附录 A 中的 absolute-URI = scheme ":" hier-part [ "?" query ] 术语。从 [RFC3986] 中收集的 URI ABNF

我的理解是absolute-uri 不能有fragment。我用java.net.URI 对其进行了测试。我检查了isAbsolute() method 只检查方案!= null 而不是检查整个字符串。

我的预期结果是 URI 的 isAbsolute() 方法返回:

  1. Uri http://wwww.example.com/index.html#Related 为 false,因为它有片段。
  2. Uri http://www.example.com/index.html 为真。

我的实际结果是 URI 的 isAbsolute() 方法返回 true。

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import java.net.URI;
import java.net.URISyntaxException;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RegExTest {

  @Test
  void validAbsoluteUri() throws URISyntaxException {
    URI uri1 = new URI("http://www.example.com/index.html#Related");
    Assertions.assertEquals(false, uri1.isAbsolute());
  }
}

【问题讨论】:

    标签: java uri


    【解决方案1】:

    附录 A 中的 BNF 并不能说明全部情况。如果我们阅读整个规范,尤其是section 4.3,我们会看到:

    一些协议元素只允许绝对形式的 URI 片段标识符。例如,为以后定义一个基本 URI 由相对引用使用需要一个绝对 URI 语法规则,该规则 不允许片段。

    absolute-URI = scheme ":" hier-part [ "?"查询]

    (强调我的。)

    一些协议元素使用这种形式的绝对 URI。 httphttps 不在这些协议中,except when used as a base URI

    请注意,the general syntax 允许带有片段的绝对 URI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 2012-12-05
      相关资源
      最近更新 更多