]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Add a TODO on Client::serving_hls_playlist.
[cubemap] / server.cpp
index c12bc8dee370e7e51f692b2d3f5a52aab3cce0e8..61bdb3f698503a7dc3bdd9e8c838bc81532e682a 100644 (file)
@@ -525,7 +525,7 @@ void Server::process_client(Client *client)
 {
        switch (client->state) {
        case Client::READING_REQUEST: {
-               if (client->tls_context != nullptr) {
+               if (client->tls_context != nullptr && !client->in_ktls_mode) {
                        if (send_pending_tls_data(client)) {
                                // send_pending_tls_data() hit postconditions #1 or #4.
                                return;
@@ -536,10 +536,10 @@ read_request_again:
                // Try to read more of the request.
                char buf[1024];
                int ret;
-               if (client->tls_context == nullptr) {
-                       ret = read_nontls_data(client, buf, sizeof(buf));
+               if (client->tls_context == nullptr || client->in_ktls_mode) {
+                       ret = read_plain_data(client, buf, sizeof(buf));
                        if (ret == -1) {
-                               // read_nontls_data() hit postconditions #1 or #2.
+                               // read_plain_data() hit postconditions #1 or #2.
                                return;
                        }
                } else {
@@ -571,22 +571,6 @@ read_request_again:
 
                assert(status == RP_FINISHED);
 
-               if (client->tls_context && !client->in_ktls_mode && tls_established(client->tls_context)) {
-                       // We're ready to enter kTLS mode, unless we still have some
-                       // handshake data to send (which then must be sent as non-kTLS).
-                       if (send_pending_tls_data(client)) {
-                               // send_pending_tls_data() hit postconditions #1 or #4.
-                               return;
-                       }
-                       ret = tls_make_ktls(client->tls_context, client->sock);
-                       if (ret < 0) {
-                               log_tls_error("tls_make_ktls", ret);
-                               close_client(client);
-                               return;
-                       }
-                       client->in_ktls_mode = true;
-               }
-
                int error_code = parse_request(client);
                if (error_code == 200) {
                        if (client->serving_hls_playlist) {
@@ -886,7 +870,7 @@ send_data_again:
        goto send_data_again;
 }
 
-int Server::read_nontls_data(Client *client, char *buf, size_t max_size)
+int Server::read_plain_data(Client *client, char *buf, size_t max_size)
 {
        int ret;
        do {
@@ -915,6 +899,8 @@ int Server::read_nontls_data(Client *client, char *buf, size_t max_size)
 int Server::read_tls_data(Client *client, char *buf, size_t max_size)
 {
 read_again:
+       assert(!client->in_ktls_mode);
+
        int ret;
        do {
                ret = read(client->sock, buf, max_size);
@@ -965,6 +951,22 @@ read_again:
                return -1;
        }
 
+       if (tls_established(client->tls_context)) {
+               // We're ready to enter kTLS mode, unless we still have some
+               // handshake data to send (which then must be sent as non-kTLS).
+               if (send_pending_tls_data(client)) {
+                       // send_pending_tls_data() hit postconditions #1 or #4.
+                       return -1;
+               }
+               int err = tls_make_ktls(client->tls_context, client->sock);  // Don't overwrite ret.
+               if (err < 0) {
+                       log_tls_error("tls_make_ktls", ret);
+                       close_client(client);
+                       return -1;
+               }
+               client->in_ktls_mode = true;
+       }
+
        assert(ret > 0);
        return ret;
 }