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

Http Response 应该设计为immutable #1397

Closed
lihongjie0209 opened this issue Feb 6, 2018 · 20 comments
Closed

Http Response 应该设计为immutable #1397

lihongjie0209 opened this issue Feb 6, 2018 · 20 comments
Milestone

Comments

@lihongjie0209
Copy link
Contributor

lihongjie0209 commented Feb 6, 2018

首先是生产代码中遇到的问题:

public class HttpTest {

        // this is ok
	@Test
	public void HttpResponse() throws Exception {
		Response response = Http.get("https://nutz.cn/yvr/list");
		String content = response.getContent();
		String content1 = response.getContent();

	}
        // java.lang.RuntimeException: java.io.IOException: Stream closed
	@Test
	public void HttpResponseGetContentWithCharSet() throws Exception {
		Response response = Http.get("https://nutz.cn/yvr/list");
		String content = response.getContent(StandardCharsets.UTF_8.name());
		String content1 = response.getContent(StandardCharsets.UTF_8.name());
	}
}

然后是这个接口存在的问题:

  1. 根据CQS原则, Query不应该修改对象的内部状态, 但是带参数的getContent却把流给关闭了.
  2. 从接口来看, getContent() 应该是 getContent(chartsetname) 使用一个默认的Charset, 但却不是.

最后希望可以:

  1. getContent()getContent(chartsetname) 表现应该是一致的
  2. getContent()getContent(chartsetname) 不应该引起副作用
  3. response 对象中还有其他有副作用的函数希望修改一下
@lihongjie0209
Copy link
Contributor Author

@wendal @zozoh

@wendal
Copy link
Member

wendal commented Feb 6, 2018

额,待我吃个早餐看看。。。

@wendal
Copy link
Member

wendal commented Feb 6, 2018

  1. done
  2. done
  3. getStream() 和 getReader() 没法做到吧,除非缓存到文件

@lihongjie0209
Copy link
Contributor Author

@wendal 调用前复制一份

@wendal
Copy link
Member

wendal commented Feb 6, 2018

怎么复制一个流对象? 给点示例代码看看?

@ywjno
Copy link
Member

ywjno commented Feb 6, 2018

response 里面的内容不是只要触碰了一次之后就没法再获取到了么,apache HttpClient 都是这逻辑

@pangwu86
Copy link
Member

pangwu86 commented Feb 6, 2018

get两次是什么需求?难道不是该自己缓存?

@lihongjie0209
Copy link
Contributor Author

@pangwu86 作为一个查询函数, 调用多少次都不应该有问题

@lihongjie0209
Copy link
Contributor Author

@wendal
Copy link
Member

wendal commented Feb 6, 2018

那是缓存到内存的,会死人的

@ywjno
Copy link
Member

ywjno commented Feb 6, 2018

你说的是 http 协议的 get 方法吧,从现象上看是幂等,但这跟程序实现是两个东西

@lihongjie0209
Copy link
Contributor Author

@wendal 好吧

@lihongjie0209
Copy link
Contributor Author

@ywjno 我说的是有返回值的方法应该尽量没有副作用

@pangwu86
Copy link
Member

pangwu86 commented Feb 7, 2018

查询?那是个io接口 一般来说io都是读一次就关闭的 结果需要多次使用就自己缓存

@wendal wendal added this to the 1.r.65 milestone Feb 7, 2018
@wendal wendal closed this as completed Feb 7, 2018
@lihongjie0209
Copy link
Contributor Author

@wendal Java8中的buffer应该可以满足需求

@wendal
Copy link
Member

wendal commented Apr 8, 2018

@lihongjie0209 buffer也没用吧?? 要是能无限次读取, 那这个数据必然要缓存起来, 内存?磁盘? 总得有个地方存的...

@lihongjie0209
Copy link
Contributor Author

@wendal buffer就是缓存在内存中

@lihongjie0209
Copy link
Contributor Author

@wendal
问题在于可以调用getContent()多次, 因为Response缓存了String,
但是却不可以调用getStream多次, 因为流会被关闭
这就造成了不一致性, 可以参考 square/okhttp#1240 (comment)

统一只能调用一次

@ywjno
Copy link
Member

ywjno commented Apr 8, 2018

nutz是要支持jdk6的,那个buffer没法用

@lihongjie0209
Copy link
Contributor Author

@ywjno 嗯, 这些都是实现的具体细节, 主要的想法还是保证了接口的一致性

问题在于可以调用getContent()多次, 因为Response缓存了String,
但是却不可以调用getStream多次, 因为流会被关闭
这就造成了不一致性, 可以参考 square/okhttp#1240 (comment)

统一只能调用一次

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

4 participants