更改这一行是否会实现持久的保持活动连接?

发布于 2024-11-19 02:10:28 字数 1338 浏览 4 评论 0原文

在适当的初始化之后,这是一个无限循环来服务传入的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-11-26 02:10:28

不可以。每个连接只能调用 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”响应头来分别通知客户端连接是否正在关闭或保持打开状态。

基本上,您需要实现这种模型(伪代码):

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;

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.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文