GUACAMOLE-2362: Merge fix missing Shared Drive after connecting.
diff --git a/src/libguac/guacamole/user.h b/src/libguac/guacamole/user.h
index 0ea5895..a11ecb5 100644
--- a/src/libguac/guacamole/user.h
+++ b/src/libguac/guacamole/user.h
@@ -574,8 +574,9 @@
  *
  * @return
  *     Zero if the user's Guacamole connection was successfully handled and
- *     the user has disconnected, or non-zero if an error prevented the user's
- *     connection from being handled properly.
+ *     the user has disconnected, or non-zero if the user disconnected during
+ *     the handshake or an error prevented the user's connection from being
+ *     handled properly.
  */
 int guac_user_handle_connection(guac_user* user, int usec_timeout);
 
diff --git a/src/libguac/user-handshake.c b/src/libguac/user-handshake.c
index 376d66c..6fc1bc8 100644
--- a/src/libguac/user-handshake.c
+++ b/src/libguac/user-handshake.c
@@ -237,9 +237,10 @@
 /**
  * This function loops through the received instructions during the handshake
  * with the client attempting to join the connection, and runs the handlers
- * for each of the opcodes, ending when the connect instruction is received.
- * Returns zero if the handshake completes successfully with the connect opcode,
- * or a non-zero value if an error occurs.
+ * for each of the opcodes, ending when the connect or disconnect instruction
+ * is received. Returns zero if the handshake completes successfully with the
+ * connect opcode, or a non-zero value if the client disconnects or an error
+ * occurs.
  * 
  * @param user
  *     The guac_user attempting to join the connection.
@@ -252,7 +253,7 @@
  * 
  * @return
  *     Zero if the handshake completes successfully with the connect opcode,
- *     or non-zero if an error occurs.
+ *     or non-zero if the client disconnects or an error occurs.
  */
 static int __guac_user_handshake(guac_user* user, guac_parser* parser,
         int usec_timeout) {
@@ -266,6 +267,14 @@
         if (strcmp(parser->opcode, "connect") == 0)
             return 0;
         
+        /* If we receive the disconnect opcode, the client is leaving before
+         * completing the handshake, which is not an error. */
+        if (strcmp(parser->opcode, "disconnect") == 0) {
+            guac_user_log(user, GUAC_LOG_DEBUG,
+                    "Guacamole connection closed during handshake");
+            return 1;
+        }
+
         guac_user_log(user, GUAC_LOG_DEBUG, "Processing instruction: %s",
                 parser->opcode);
         
diff --git a/src/protocols/vnc/clipboard.c b/src/protocols/vnc/clipboard.c
index 1521f54..260b948 100644
--- a/src/protocols/vnc/clipboard.c
+++ b/src/protocols/vnc/clipboard.c
@@ -118,28 +118,22 @@
 
 #ifdef LIBVNC_CLIENT_HAS_EXTENDED_CLIPBOARD
     /*
-     * Guacamole stores clipboard text as UTF-8. The clipboard-encoding
-     * setting only applies to the classic VNC clipboard path, where text
-     * must be converted from UTF-8 to the configured wire encoding.
-     *
-     * If clipboard-encoding is UTF-8, try the Extended Clipboard  path first
-     * since it can send UTF-8 directly. Otherwise, or if that fails, fall
-     * back to classic clipboard conversion.
+     * Guacamole stores clipboard text as UTF-8, and the Extended Clipboard
+     * carries UTF-8 by definition, so it is tried first whenever the server
+     * announced it (SendClientCutTextUTF8() returns FALSE otherwise). The
+     * clipboard-encoding setting describes only the classic VNC clipboard,
+     * the fallback below, where text must be converted from UTF-8 to the
+     * configured wire encoding. Tying the Extended Clipboard to
+     * clipboard-encoding=UTF-8 would force a choice between Unicode on
+     * servers that support it and correct ISO 8859-1 on servers that do not.
      *
      * Text coming back from the VNC server follows the same idea in reverse:
      * classic clipboard text is decoded using clipboard-encoding, while
      * Extended Clipboard text is already UTF-8.
      */
-
-    const char* clipboard_encoding = vnc_client->settings->clipboard_encoding;
-    int use_utf8_clipboard = clipboard_encoding != NULL &&
-        strcmp(clipboard_encoding, "UTF-8") == 0;
-
-    if (use_utf8_clipboard) {
-        if (SendClientCutTextUTF8(rfb_client, vnc_client->clipboard->buffer,
-                    vnc_client->clipboard->length))
-            return 0;
-    }
+    if (SendClientCutTextUTF8(rfb_client, vnc_client->clipboard->buffer,
+                vnc_client->clipboard->length))
+        return 0;
 #endif
 
     /* Fall back to classic clipboard with encoding conversion */
@@ -217,6 +211,11 @@
         return;
     }
 
+    /* Extended Clipboard text is NUL-terminated on the wire; the terminator
+     * is not part of the clipboard contents */
+    while (textlen > 0 && text[textlen - 1] == '\0')
+        textlen--;
+
     const char* input = text;
     char* output = received_data;
 
@@ -226,6 +225,16 @@
     guac_iconv(GUAC_READ_UTF8, &input, textlen,
             GUAC_WRITE_UTF8, &output, GUAC_COMMON_CLIPBOARD_MAX_LENGTH);
 
+    /* Extended Clipboard text uses CRLF line endings; the classic path
+     * delivers LF, so the same text is converted here */
+    char* out = received_data;
+    for (const char* in = received_data; in < output; in++) {
+        if (*in == '\r' && in + 1 < output && *(in + 1) == '\n')
+            continue;
+        *(out++) = *in;
+    }
+    output = out;
+
     /* Send converted data */
     guac_common_clipboard_reset(vnc_client->clipboard, "text/plain");
     guac_common_clipboard_append(vnc_client->clipboard, received_data, output - received_data);