Skip to content

Add charset UTF-8 to workspace text file responses#26912

Open
lohitkolluri wants to merge 2 commits into
jenkinsci:masterfrom
lohitkolluri:fix/26894-workspace-viewer-utf8-charset
Open

Add charset UTF-8 to workspace text file responses#26912
lohitkolluri wants to merge 2 commits into
jenkinsci:masterfrom
lohitkolluri:fix/26894-workspace-viewer-utf8-charset

Conversation

@lohitkolluri

Copy link
Copy Markdown

Fixes #26894

Testing done

Automated test added that writes a .java file containing Chinese characters (用户中心模块) to a freestyle project workspace, fetches it via the workspace browser, and verifies:

  • Content-Type header includes charset=utf-8
  • Chinese characters are served correctly (not garbled)

The full DirectoryBrowserSupportTest suite passes (23 tests, 0 failures, 2 skipped for platform-specific reasons).

The view path regression test (canViewRelativePath) also passes.

Screenshots (UI changes only)

N/A - backend change, no visual UI modification.

Proposed changelog entries

  • Include charset=UTF-8 in Content-Type when serving text/* files through the workspace and artifact browser

Proposed changelog category

/label bug

Proposed upgrade guidelines

N/A

Submitter checklist

  • The issue, if it exists, is well-described.
  • The changelog entries and upgrade guidelines are appropriate for the audience affected by the change (users or developers, depending on the change) and are in the imperative mood (see examples). Fill in the Proposed upgrade guidelines section only if there are breaking changes or changes that may require extra steps from users during upgrade.
  • There is automated testing or an explanation as to why this change has no tests.
  • New public classes, fields, and methods are annotated with @Restricted or have @since TODO Javadocs, as appropriate.
  • New deprecations are annotated with @Deprecated(since = "TODO") or @Deprecated(forRemoval = true, since = "TODO"), if applicable.
  • UI changes do not introduce regressions when enforcing the current default rules of Content Security Policy Plugin. In particular, new or substantially changed JavaScript is not defined inline and does not call eval to ease future introduction of Content Security Policy (CSP) directives (see documentation).
  • For dependency updates, there are links to external changelogs and, if possible, full differentials.
  • For new APIs and extension points, there is a link to at least one consumer.

When viewing source files in the workspace browser, the
Content-Type header lacked a charset parameter. Browsers
guessed the encoding, which failed for non-ASCII text.

The Stapler serveFile mime-type: prefix is now used to
explicitly set Content-Type with charset=UTF-8 for text/*
files. The *view* path and normal file serving path are
both updated. Binary files are unaffected.

Signed-off-by: Lohit Kolluri <lohitkolluri@gmail.com>
@lohitkolluri

Copy link
Copy Markdown
Author

Friendly ping @jenkinsci/core-pr-reviewers.

All checks are passing, and the issue reporter has confirmed that #26912 resolves the problem in their environment. I'd appreciate any review feedback when time permits. Thanks!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses JENKINS-26894 by ensuring that text files served via DirectoryBrowserSupport (workspace/artifact browser) include an explicit UTF-8 charset in the Content-Type response header, preventing browsers from guessing an incorrect encoding for non-ASCII content.

Changes:

  • Force Content-Type to include charset=UTF-8 when serving text/* files from DirectoryBrowserSupport.
  • Ensure the special *view* path serves as text/plain with UTF-8 charset.
  • Add a regression test validating both the Content-Type header and correct round-trip rendering of Chinese characters from a workspace .java file.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
core/src/main/java/hudson/model/DirectoryBrowserSupport.java Adds charset=UTF-8 for text/* responses (and *view* text/plain) when serving files.
test/src/test/java/hudson/model/DirectoryBrowserSupportTest.java Adds a regression test verifying charset presence and correct UTF-8 content rendering from workspace browsing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/src/test/java/hudson/model/DirectoryBrowserSupportTest.java
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ath-successful This PR has successfully passed the full acceptance-test-harness suite bug For changelog: Minor bug. Will be listed after features pct-successful This PR has successfully passed the full plugin-compatibility-test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Workspace viewer displays UTF-8 Java source files as garbled text when no charset is specified

3 participants