更改这一行是否会实现持久的保持活动连接?
在适当的初始化之后,这是一个无限循环来服务传入的 HTTPS 请求,但每个请求只有一个连接(假设请求只需要一次读取):
while TRUE do
begin // wait for incoming TCP connection
if listen(listen_socket, 100) 0 then continue; // listen failed
client_len := SizeOf(sa_cli);
sock := accept(listen_socket, @sa_cli, @client_len); // create socket for connection
if sock = INVALID_SOCKET then continue; // accept failed
ssl := SSL_new(ctx); // TCP connection ready, create ssl structure
if assigned(ssl) then
begin
SSL_set_fd(ssl, sock); // assign socket to ssl structure
if SSL_accept(ssl) = 1 then // handshake worked
begin
bytesin := SSL_read(ssl, buffer, sizeof(buffer)-1);
if bytesin > 0 then
begin
buffer[bytesin] := #0;
// decide on response here...
response := 'HTTP/1.0 200 OK'#13#10 + etc;
SSL_write(ssl, pchar(response)^, length(response));
end; // else read empty or failed
end; // else handshake failed
SSL_set_shutdown(ssl, SSL_SENT_SHUTDOWN or SSL_RECEIVED_SHUTDOWN);
CloseSocket(sock);
SSL_free(ssl);
end; // else ssl creation failed
end; // while
正在更改
if ssl_accept(ssl) = 1 then
为
while ssl_accept(ssl) = 1 do
正确支持默认 HTTP 1.1 keep-alive 所需的所有内容(即多个每个连接的请求数)?
After the appropriate initializations, here's an infinite loop to service incoming HTTPS requests, but only one connection per request (and assuming requests need only one read):
while TRUE do
begin // wait for incoming TCP connection
if listen(listen_socket, 100) 0 then continue; // listen failed
client_len := SizeOf(sa_cli);
sock := accept(listen_socket, @sa_cli, @client_len); // create socket for connection
if sock = INVALID_SOCKET then continue; // accept failed
ssl := SSL_new(ctx); // TCP connection ready, create ssl structure
if assigned(ssl) then
begin
SSL_set_fd(ssl, sock); // assign socket to ssl structure
if SSL_accept(ssl) = 1 then // handshake worked
begin
bytesin := SSL_read(ssl, buffer, sizeof(buffer)-1);
if bytesin > 0 then
begin
buffer[bytesin] := #0;
// decide on response here...
response := 'HTTP/1.0 200 OK'#13#10 + etc;
SSL_write(ssl, pchar(response)^, length(response));
end; // else read empty or failed
end; // else handshake failed
SSL_set_shutdown(ssl, SSL_SENT_SHUTDOWN or SSL_RECEIVED_SHUTDOWN);
CloseSocket(sock);
SSL_free(ssl);
end; // else ssl creation failed
end; // while
Is changing
if ssl_accept(ssl) = 1 then
to
while ssl_accept(ssl) = 1 do
all that's needed to correctly support default HTTP 1.1 keep-alive (ie, multiple requests per connection)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可以。每个连接只能调用
ssl_new()
和ssl_accept()
一次。一旦建立连接并协商 SSL 会话,就无需再次进行。 HTTP 保持活动旨在避免每次请求时都必须重新连接。您需要循环调用ssl_read()
和 SSL_write()。另外,不要忘记检查客户端的 HTTP 版本。 HTTP 1.1 客户端默认支持 keep-alive,无需请求。 HTTP 1.0 及更早版本的客户端必须显式包含“Connection: keep-alive”请求标头。无论哪种方式,服务器都需要发送“Connection: close”或“Connection: keep-alive”响应头来分别通知客户端连接是否正在关闭或保持打开状态。
基本上,您需要实现这种模型(伪代码):
No.
ssl_new()
andssl_accept()
should be called only once per connection. Once a connection and negotiated an SSL session, there is no need to do it again. HTTP keep-alives are designed to avoid having to reconnect on each request. You need to loop your calls tossl_read()
and SSL_write() instead.Also, don't forget to check the client's HTTP version. HTTP 1.1 clients are expected to support keep-alives by default without having to ask for them. HTTP 1.0 and earlier clients have to explicitly include a 'Connection: keep-alive' request header instead. Either way, the server needs to send a 'Connection: close' or 'Connection: keep-alive' response header to inform the client whether the connection is being closed or is being kept open, respectively.
Basically, you need to implement this kind of model (pseudo-code):