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

HTTP Server runtime based on Java built-in HTTP Server #893

Merged
merged 28 commits into from
Mar 23, 2025

Conversation

sdelamo
Copy link
Contributor

@sdelamo sdelamo commented Mar 20, 2025

Micronaut HTTP Server Module

This pull request adds a HTTP Server implementation based on the Java built-in HTTP Server. To use it, users need to add the following dependency:

io.micronaut.servlet:micronaut-http-server.

For users, it would probably be best to use something more battled tested like netty or any of the servlet implementations as their runtime. Still, it is pretty cool to be able to have a Micronaut application with both a server and a client based on the built-in Java HTTP server and HTTP client while keeping your Micronaut Application with the same Micronaut APIs.

Micronaut HTTP Server Module Implementation.

To power it, I created a new module that depends on servlet-engine. It leverages the API ServletHttpHandler<HttpServletRequest, HttpServletResponse> to implement a com.sun.net.httpserver.HttpHandler.

I created implementations of HttpServletRequest and HttpServletResponse to interact with com.sun.net.httpserver.HttpExchange. Although, I have left many methods with throw new UnsupportedOperationException("Not implemented");, the TCK passes fully but for one test which fails for every other Servlet implementation.

Consuming Java built-in HTTP Server runtime in function test modules

I have created hooks so that the serverless test modules (gcp , azure, and aws) can be powered by the built-in Java http server runtime without relying on servlet-runtime. Instead, they will provide their own implementation of HttpHandler based on the function and they will exposes the function`s application context as the server application context.

The following draft PR shows the implementation in the AWS module:

micronaut-projects/micronaut-aws#2315

This will allow us to stop relying on Jetty 11, which has an opened CVE, in all those modules.

Files Moves

QueryStringDecoder

I copied QueryStringDecoder from http-poja-common and package io.micronaut.http.poja.util.QueryStringDecoder into servlet-core and package io.micronaut.servlet.http.utils. I have deprecated from removal io.micronaut.http.poja.util.QueryStringDecoder

Server Classes

I have copied the server classes (ServletServerFactory, ServletStaticResourceConfiguration, and AbstractServletServer) from server-runtime into server-core. I have deprecated, for removal, these classes in the server-runtime module.

Event Publication

  • AbstractServletServer publishes only ServerShutDown if there is a bean of type ApplicationEventPublisher<ServerShutdownEvent>
  • I made discovery-client a compile-only dependency. I created a listener of ServerStartupEvent which publishes ServiceReadyEvent.

@sdelamo sdelamo changed the title Http server implementation runtime HTTP Server runtime based on Java built-in HTTP Server Mar 20, 2025
@sdelamo sdelamo changed the base branch from 5.1.x to 5.2.x March 20, 2025 17:33
@sdelamo sdelamo marked this pull request as ready for review March 20, 2025 18:26
@sdelamo sdelamo requested review from graemerocher and yawkat March 20, 2025 18:27

void writeBody(HttpExchange exchange, byte[] body) throws IOException {
if (body.length > 0) {
OutputStream outputStream = exchange.getResponseBody();
Copy link
Member

Choose a reason for hiding this comment

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

Why do the buffering to byte[] and not just directly write to the response body stream?

@yawkat yawkat added the type: enhancement New feature or request label Mar 21, 2025
*/
@Experimental
@Internal
class HttpExchangeHttpServletRequest implements HttpServletRequest {
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't it be easier to implement Micronaut's HTTP request interface instead of trying to create a servlet bridge?

@sdelamo sdelamo requested review from graemerocher and yawkat March 21, 2025 11:59
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
57.7% Coverage on New Code (required ≥ 70%)
1 New Bugs (required ≤ 0)
3 New Critical Issues (required ≤ 0)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@sdelamo sdelamo merged commit 502db53 into 5.2.x Mar 23, 2025
35 of 39 checks passed
@sdelamo sdelamo deleted the http-server-implementation-runtime branch March 23, 2025 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants