Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to change the default ErrorHandler response type #11522

Merged
merged 4 commits into from
Mar 19, 2024

Conversation

dhs3000
Copy link
Contributor

@dhs3000 dhs3000 commented Mar 15, 2024

Currently, the server returns HTML responses by default. It would be great to easily change this without having to overwrite the whole error handler.

The error handler tries to find the right/accepted response type using the ACCEPT header, but in some cases the header is not yet there when an error happens, e.g. with an URI violation, it seems the request is not yet fully parsed and therefore the requested content type not recognised.

@joakime
Copy link
Contributor

joakime commented Mar 15, 2024

but in some cases the header is not yet there when an error happens, e.g. with an URI violation, it seems the request is not yet fully parsed and therefore the requested content type not recognised.

I think URI Violations happen after all of the headers are fully parsed, and before the dispatch to the handler tree.

Here -> https://github.com/jetty/jetty.project/blob/jetty-12.0.7/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java#L597-L603

@dhs3000
Copy link
Contributor Author

dhs3000 commented Mar 16, 2024

I think URI Violations happen after all of the headers are fully parsed, and before the dispatch to the handler tree.

@joakime But for some reason in the ErrorHandler the request headers are empty. I created an issue with test case for that.

However, independently of that, I thought it would be great to be able to change the default response content type for the error handler, in case a client does not specify any Accept header.

Comment on lines 166 to 169
protected Type defaultResponseMimeType() {
return Type.TEXT_HTML;
}

Copy link
Contributor

@sbordet sbordet Mar 18, 2024

Choose a reason for hiding this comment

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

I don't mind the idea of being able to configure a default response content type.
Please:

  1. Make a field defaultResponseContentType, with public getters and setters (so there is no need to extend ErrorHandler).
  2. Write a test case in org.eclipse.jetty.server.ErrorHandlerTest.

Let us know if you are willing to do the above.
Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I can look into that.

Copy link
Contributor Author

@dhs3000 dhs3000 Mar 18, 2024

Choose a reason for hiding this comment

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

@sbordet I applied & pushed the changes. Hope that is what you meant :)

Copy link
Contributor

@sbordet sbordet left a comment

Choose a reason for hiding this comment

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

@dhs3000 just a couple more changes and we should be good.

@@ -74,6 +74,7 @@ public class ErrorHandler implements Request.Handler

boolean _showStacks = true;
boolean _showMessageInTitle = true;
Type _defaultResponseMimeType = Type.TEXT_HTML;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use String, so that it will be easier to configure also from XML.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Type is changed to String

*/
public void setDefaultResponseMimeType(Type defaultResponseMimeType)
{
_defaultResponseMimeType = defaultResponseMimeType;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_defaultResponseMimeType = defaultResponseMimeType;
_defaultResponseMimeType = Objects.requireNonNull(defaultResponseMimeType);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Type is changed to String and also uses the requireNonNull helper now

Comment on lines 171 to 173
"GET / HTTP/1.1\r\n" +
"Host: Localhost\r\n" +
"\r\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a text block here 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haven't used Java in years ;-) Nice that that is possible. I updated the test.

@dhs3000
Copy link
Contributor Author

dhs3000 commented Mar 19, 2024

@sbordet I applied the requested changes, let me know if anything else :)

@sbordet sbordet merged commit d39dde5 into jetty:jetty-12.0.x Mar 19, 2024
3 of 6 checks passed
@sbordet
Copy link
Contributor

sbordet commented Mar 19, 2024

@dhs3000 Thanks!

@dhs3000 dhs3000 deleted the patch-1 branch March 20, 2024 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Return requested content type error response in case of URI Violation and not always text/html
3 participants