gpt4 book ai didi

Firefox 代理 CONNECT 方法请求未触发 Java HTTPHandler

转载 作者:可可西里 更新时间:2023-11-01 17:05:19 31 4
gpt4 key购买 nike

问题:

有没有办法用 Oracle HTTPServer class 支持 CONNECT HTTP 请求? ?

Note: If you do not want to deal with this, Jetty seems to not have this problem with their HTTPServer class (even when not using their ProxyHTTP implementation ConnectHandler). Grizzly however seemed to have the same issue as the default Java implementation, not surprising since they were both made by Oracle.

说明及难点:

Note: I include this to provided a detailed explanation of what I believe to be the issue.

我最近一直在尝试为 Firefox 上的安全 (HTTPS) 连接制作 HTTP 代理。这是完成的方式是 CONNECT method (IETF RFC 2817) .来自 Firefox 的 CONNECT 请求的示例如下(在请求 Google 主页时发生)。

CONNECT www.google.com:443 HTTP/1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:58.0) Gecko/20100101 Firefox/58.0
Proxy-Connection: keep-alive
Connection: keep-alive
Host: www.google.com:443

这与普通的 GET 请求不同。

GET /index HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1

显着的区别是所谓的“请求目标”(有关详细信息,请参见 Mozilla article)。具体来说,正常的 GET 请求是“绝对路径”,而 CONNECT 请求目标是“授权表”。

在此 StackOverflow answer (simple Java HTTPServer implementation) 中使用演示服务器我们可以看到注册HTTPHandler的方法是server.createContext("/test", new MyHandler()),根据Oracle documentation必须以“/”开头。然而,因为“授权表格”在开头不包含“/”,所以永远不会触发句柄

Note: This is still somewhat speculation as Oracle's code is closed source, but it was confirmed by sending the same TCP request to the server with the same CONNECT request above, but one time with a leading '/' (so '/www.google.com' instead of 'www.google.com'). With the leading '/' the handler was triggered.

综上所述,我的问题是,

有没有办法解决这个问题,同时仍然使用 Java 的 Oracle 版本中包含的库?

Note: Other than writing an new HTTP Server from scratch, or using another library. In my testing Jetty did trigger the HTTPHandler. Grizzly did not (and, in my opinion, has some code quality issues).

最佳答案

您可以在下面的链接中查看源代码

https://github.com/akashche/java-1.8.0-openjdk-1.8.0.151-2.b12.fc28.ppc64le

下面是一些正在播放的文件

  • /sun/net/httpserver/ServerImpl.java
  • /sun/net/httpserver/ContextList.java

当 http 服务器尝试获取 CONNECT 请求的路径时

ctx = contexts.findContext (protocol, uri.getPath());

它的 uriwww.google.com:443 并且 uri.getPath() 返回 null如下图所示

Context path null

ContextList 类有以下上下文方法

public synchronized HttpContextImpl createContext (String path, HttpHandler handler) {
if (handler == null || path == null) {
throw new NullPointerException ("null handler, or path parameter");
}
HttpContextImpl context = new HttpContextImpl (protocol, path, handler, this);
contexts.add (context);
logger.config ("context created: " + path);
return context;
}

public synchronized HttpContextImpl createContext (String path) {
if (path == null) {
throw new NullPointerException ("null path parameter");
}
HttpContextImpl context = new HttpContextImpl (protocol, path, null, this);
contexts.add (context);
logger.config ("context created: " + path);
return context;
}

两者都检查路径 null 并且不允许您设置具有空路径的上下文。这意味着我们永远无法让上下文在这种情况下工作。为了使 CONNECT 请求正常工作,我们需要能够注册一个路径为 null 的处理程序或某种默认的 catch all 处理程序。正如我检查过的,当前代码不支持此功能。而且所有这些类都没有标记为公开,所以你不能从这些类继承并让它工作。

但是既然你有源代码,如果你真的想让它工作,你必须复制这两个文件并做一些修改才能让它工作。

如此简短的回答是,使用 Oracle 的原始 com.sun.net.httpserver.HttpServer 无法做到这一点。我检查了 JDK 1.8 而不是 1.9,但我怀疑这些类在 1.9 中会有很大变化

关于Firefox 代理 CONNECT 方法请求未触发 Java HTTPHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48757655/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com