支持我的代理服务器中的持久HTTP连接

时间:2022-08-24 03:47:54

I am implementing a HTTP caching proxy server in C++.I am done with most part of it but i am stuck at a point.
What i am doing is creating each thread with a socket to handle each time a request from browser comes. I parse the request, check for its availability in cache and if not found forward it to end www server.In both cases i write the response received on the connected socket. Now the problem is until and unless i close the socket, the browser doesn't assumes the transfer to be complete and waits indefinitely.
This way I can't use a socket for more than one connection, in other words I can't support persistent connections.
Any help will be appreciated..

我正在用C ++实现一个HTTP缓存代理服务器。我完成了它的大部分工作,但我陷入了困境。我正在做的是创建每个线程与一个套接字来处理每次来自浏览器的请求。我解析请求,检查它在缓存中的可用性,如果没有找到它转发到www服务器。在这两种情况下,我写在连接的套接字上收到的响应。现在问题是,除非我关闭套接字,否则浏览器不会假定传输完成并无限期等待。这样我就不能将套接字用于多个连接,换句话说我不能支持持久连接。任何帮助将不胜感激..

Thanks,

2 个解决方案

#1


What headers are you sending back to the client?

你发送回客户端的标题是什么?

You should be including:

你应该包括:

Content-Length: ...
Keep-Alive: timeout=..., max=...
Connection: Keep-Alive

In particular, the Content-Length header is essential with persistent connections so that the client knows how much data to read. See section 8.1.2.1 of RFC 2616.

特别是,Content-Length头对于持久连接至关重要,因此客户端知道要读取多少数据。请参阅RFC 2616的8.1.2.1节。

Alternatively, if you want to tell the client to break the connection, send:

或者,如果您想告诉客户端断开连接,请发送:

Connection: close

#2


Now the problem is until and unless i close the socket, the browser doesn't assumes the transfer to be complete and waits indefinitely.

现在问题是,除非我关闭套接字,否则浏览器不会假定传输完成并无限期等待。

Right. HTTP 1.1 uses Keep-Alive by default.

对。 HTTP 1.1默认使用Keep-Alive。

This way I can't use a socket for more than one connection, in other words I can't support persistent connections.

这样我就不能将套接字用于多个连接,换句话说我不能支持持久连接。

I'm not sure I understand you, because that persistent connection you have IS a persistent connection.

我不确定我理解你,因为你持久的连接是一个持久的联系。

#1


What headers are you sending back to the client?

你发送回客户端的标题是什么?

You should be including:

你应该包括:

Content-Length: ...
Keep-Alive: timeout=..., max=...
Connection: Keep-Alive

In particular, the Content-Length header is essential with persistent connections so that the client knows how much data to read. See section 8.1.2.1 of RFC 2616.

特别是,Content-Length头对于持久连接至关重要,因此客户端知道要读取多少数据。请参阅RFC 2616的8.1.2.1节。

Alternatively, if you want to tell the client to break the connection, send:

或者,如果您想告诉客户端断开连接,请发送:

Connection: close

#2


Now the problem is until and unless i close the socket, the browser doesn't assumes the transfer to be complete and waits indefinitely.

现在问题是,除非我关闭套接字,否则浏览器不会假定传输完成并无限期等待。

Right. HTTP 1.1 uses Keep-Alive by default.

对。 HTTP 1.1默认使用Keep-Alive。

This way I can't use a socket for more than one connection, in other words I can't support persistent connections.

这样我就不能将套接字用于多个连接,换句话说我不能支持持久连接。

I'm not sure I understand you, because that persistent connection you have IS a persistent connection.

我不确定我理解你,因为你持久的连接是一个持久的联系。