更改这一行是否实现了持久的保持活动连接? [英] Does changing this one line implement persistent keep-alive connections?

查看:115
本文介绍了更改这一行是否实现了持久的保持活动连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在适当的初始化之后,这是一个无限循环来为传入的HTTPS请求提供服务,但每个请求只有一个连接(假设请求只需要一次读取):

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

正在改变

if ssl_accept(ssl) = 1 then

while ssl_accept(ssl) = 1 do

正确支持默认HTTP 1.1 keep-alive所需的所有内容(即每个连接多个请求)?

all that's needed to correctly support default HTTP 1.1 keep-alive (ie, multiple requests per connection)?

推荐答案

没有。每个连接只应调用一次 ssl_new() ssl_accept()。一旦建立连接并协商SSL会话,就不需要再次进行。 HTTP keep-alives旨在避免在每个请求上重新连接。您需要将调用循环到 ssl_read()和SSL_write()。

No. ssl_new() and ssl_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 to ssl_read() and SSL_write() instead.

此外,不要忘记检查客户端的HTTP版本。预计HTTP 1.1客户端默认支持keep-alives,而不必请求它们。 HTTP 1.0和更早版本的客户端必须显式包含Connection:keep-alive请求标头。无论哪种方式,服务器都需要发送一个'Connection:close'或'Connection:keep-alive'响应头来分别通知客户端连接是关闭还是保持打开状态。

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):

while True do
begin
  accept an incoming connection...

  initialize SSL...

  repeat
    read a request...
    if not Connected then Break;

    KeepAlive := ((client is HTTP1.1+) and (request['Connection'] = '')) or (request['Connection'] = 'keep-alive');

    prepare reply...
    response['Connection'] := iif(KeepAlive, 'keep-alive', 'close');

    send reply...

  while KeepAlive and Connected;

  cleanup SSL...
  close socket...
end;

这篇关于更改这一行是否实现了持久的保持活动连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆