Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax hook 与 NSURLProtocol #30

Closed
wjiuxing opened this issue Jul 6, 2020 · 19 comments
Closed

Ajax hook 与 NSURLProtocol #30

wjiuxing opened this issue Jul 6, 2020 · 19 comments

Comments

@wjiuxing
Copy link
Contributor

wjiuxing commented Jul 6, 2020

感谢 @karosLi 的回复,为交流方便,把内容放到 github,谢谢。

我想用 Ajax hook,但不想有注册 protocol 的私有代码,这看起来有点难了。。。

  1. 首次(邮件)请教,内容如下:

你好,我是***。我公司的项目正在集成 KKJSBridge,很棒。
这几天发现项目中新加了 KKJSBridgeAjaxURLProtocol,没有想明白 和 Ajax 什么关系。
我在 KKJSBridge.js 中 看到 send 时通过 bridge.callNative 将请求给了 "ajax" module,像之前也没有用 Protocol。
在 cache body request 的 initialize 方法里注册了 URL Protocol。
想问下,这个 protocol 能做成可插拔的吗?我们之前也用 protocol,但是 WK 上要用私有接口才行,担心有审核风险没敢用…
祝好。

  1. @karosLi 回复:
  1. KKJSBridgeAjaxURLProtocol 是新的 hook 形式,是想要去提升 hook 的兼容性,因为 ajax hook 对于获取二进制的数据不太好处理,比如 ajax 获取一张图片,那就会获取失败。当然 ajax hook 对于纯文本的的请求是没有问题的。KKJSBridgeAjaxURLProtocol 目前还在测试阶段,后面的设想是替换掉目前的 ajax hook
  2. 关于 WK 上使用私有接口这点,没有审核风险。目前所有离线包都是需要注册 WK 的私有接口的。我们公司项目里也是注册私有接口去使用离线包的,既然已经使用了私有接口了,那再多一个 KKJSBridgeAjaxURLProtocol,影响也是不大的。
  3. 关于 protocol 热插拔,还是要看 WK 是否有注册,有注册 protocol 才会起作用,所以你想热插拔,可以控制是否开启 ajax hook 统一开关。
  4. 尽量还是在github上交流吧,邮件不一定来得及看。
@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

你使用 Ajax hook 的目的是什么?

@wjiuxing
Copy link
Contributor Author

wjiuxing commented Jul 6, 2020

为了做离线资源,我做了内置服务器 127.0.0.1,也因此,Ajax 需要被 hook 后转域名、添加路径,然后将请求结果通过 ajax hook delegate 返回去。。。

@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

嗯,你这个不需要ajax转域名,域名可以在本地服务器拼接

@wjiuxing
Copy link
Contributor Author

wjiuxing commented Jul 6, 2020

似的,OC 上能内嵌到 app 的服务器,也就那么一款,研究了半天,做不到 HTTP Proxy 的能力啊。。。

@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

baseURL 肯定是固定的,如果不固定,也可以通过服务器下发给你 baseURL。
拦截层,是可以根据 baseURL 和 相对url,拼接完整的 url

@wjiuxing
Copy link
Contributor Author

wjiuxing commented Jul 6, 2020

更新了 #30 (comment)

为了做离线资源,我做了内置服务器 127.0.0.1,也因此,Ajax 需要被 hook 后转域名、添加路径,然后将请求结果通过 ajax hook delegate 返回去。。。

上面的拦截层具体指什么?内置服务器里边的拦截层吗?内置服务器接到一个请求后,只能立马 return 一个文件或者 data,容不得异步走网络。。。

@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

可以的,可以使用 GCDWebServer,我之前尝试做过,是可以拦截到post请求的,然后在拦截代码里拼接完整url

@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

image
image

@wjiuxing
Copy link
Contributor Author

wjiuxing commented Jul 6, 2020

GCDWebServer 应该是不支持 HTTPS 的吧,需要 HTTPS

@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

@wjiuxing
Copy link
Contributor Author

wjiuxing commented Jul 6, 2020

是的,用的这个,看着都是从硬盘上直接读的文件,没看出来怎么将一个请求转到 proxy

@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

需要多看下他们的demo,一个http本地代理服务器,肯定是支持get和post的

@wjiuxing
Copy link
Contributor Author

wjiuxing commented Jul 6, 2020

有点懵,这和 get 还是 post 没关系吧?

@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

你的H5的请求都是绝对路径还是相对路径?
如果是相对路径,肯定会进入拦截方法里

@karosLi
Copy link
Owner

karosLi commented Jul 6, 2020

你要是实在想要去掉URLProtocol,我可以考虑分成两个subpod,可以指定想要哪种方式

@wjiuxing
Copy link
Contributor Author

wjiuxing commented Jul 6, 2020

你的H5的请求都是绝对路径还是相对路径?
如果是相对路径,肯定会进入拦截方法里

h5 那边应该是相对绝对都有。。。
回头我再多看看 CocoaHTTPServer 的 demo


你要是实在想要去掉URLProtocol,我可以考虑分成两个subpod,可以指定想要哪种方式

这样就太好了,感谢!

@karosLi
Copy link
Owner

karosLi commented Jul 9, 2020

试下这个版本,区分了新老代码,按需求引入
pod 'KKJSBridge/AjaxHook', '1.1.5-beta9'

@wjiuxing
Copy link
Contributor Author

wjiuxing commented Jul 10, 2020

好的,今天一直在忙别的,周末试试吧,谢谢!
我试了一下,没有问题,谢谢了!

@karosLi
Copy link
Owner

karosLi commented Jul 14, 2020

不客气,我先关闭了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants