docs: @SuppressWarnings("Duplicates")
      public static Optional<Charset> resolveCharset(HttpMessage<?> request) {
          try {
              Optional<Charset> contentTypeCharset = request
                  .getContentType()
                  .flatMap(contentType ->
                      contentType.getParameters()
                          .get(MediaType.CHARSET_PARAMETER)
                          .map(Charset::forName)
                  );
  
              if (contentTypeCharset.isPresent()) {
                  return contentTypeCharset;
              } else {
                  return request
                      .getHeaders()
                      .findFirst(HttpHeaders.ACCEPT_CHARSET)
                      .map(text -> {
                          int len = text.length();
                          if (len == 0 || (len == 1 && text.charAt(0) == '*')) {
                              return StandardCharsets.UTF_8;
                          }
                          if (text.indexOf(';') > -1) {
                              text = text.split(";")[0];
                          }
                          if (text.indexOf(',') > -1) {
                              text = text.split(",")[0];
                          }
                          try {
                              return Charset.forName(text);
                          } catch (Exception e) {
                              // unsupported charset, default to UTF-8
                              return StandardCharsets.UTF_8;
                          }
                      });
              }
          } catch (UnsupportedCharsetException e) {
              return Optional.empty();
          }
      }