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

How can jetty parameters be configured? #82

Open
elrob opened this issue Apr 26, 2023 · 2 comments
Open

How can jetty parameters be configured? #82

elrob opened this issue Apr 26, 2023 · 2 comments

Comments

@elrob
Copy link

elrob commented Apr 26, 2023

Hi Seratch 🙌

I have attempted to increase jetty max "requestHeaderSize" but failed until I created a custom JettyLauncher.

Failed attempts:

web.xml:

    <context-param>
        <param-name>org.eclipse.jetty.server.Request.maxHeaderSize</param-name>
        <param-value>16384</param-value>
    </context-param>
    <context-param>
        <param-name>org.eclipse.jetty.server.HttpConfiguration.headerBufferSize</param-name>
        <param-value>16384</param-value>
    </context-param>
    <context-param>
        <param-name>org.eclipse.jetty.server.HttpConfiguration.requestHeaderSize</param-name>
        <param-value>16384</param-value>
    </context-param>

java jar start command:

java -Dorg.eclipse.jetty.server.HttpConfiguration.requestHeaderSize=40000 -jar /app.jar

Eventually I got it working by basically copy-pasting skinny.standalone.JettyServer (https://github.com/skinny-framework/skinny-micro/blob/master/micro-server/src/main/scala/skinny/standalone/JettyServer.scala) and modifying it very slightly to configure the requestHeaderSize:

import org.eclipse.jetty.server.{HttpConnectionFactory, Server, ServerConnector}
import org.eclipse.jetty.servlet.DefaultServlet
import org.eclipse.jetty.webapp.WebAppContext
import skinny.micro.SkinnyListener
import skinny.standalone.JettyServer

import javax.servlet.ServletContextListener

object CustomJettyLauncher extends JettyServer {

  def main(args: Array[String]): Unit = {
    start()
    server.join()
  }

  override def start(): Unit = {
    refreshServer()
    logger.info(s"Starting Jetty server on port ${port}")
    val context = new WebAppContext()
    val contextPath = sys.env.get("SKINNY_PREFIX").orElse(getEnvVarOrSysProp("skinny.prefix")).getOrElse("/")
    context.setContextPath(contextPath)
    context.setWar({
      val domain = this.getClass.getProtectionDomain
      val location = domain.getCodeSource.getLocation
      location.toExternalForm
    })
    context.addEventListener(listener)
    context.addServlet(classOf[DefaultServlet], "/")
    server.setHandler(context)

    // Set the request header size
    server.getConnectors()(0).asInstanceOf[ServerConnector]
      .getConnectionFactory(classOf[HttpConnectionFactory])
      .getHttpConfiguration
      .setRequestHeaderSize(40000)

    server.start()
    logger.info(s"Started Jetty server on port ${port}")
  }

  private[this] var server: Server = _

  private[this] var listener: ServletContextListener = new SkinnyListener

  private[this] var _port: Int = {
    // PORT: Heroku default env variable
    Option(System.getenv("PORT")).map(_.toInt).getOrElse(8080)
  }

  private[this] def port: Int = {
    val port = sys.env.get("SKINNY_PORT").orElse(getEnvVarOrSysProp("skinny.port")).map(_.toInt).getOrElse(_port)
    port
  }

  private[this] def newServer(port: Int): Server = new Server(port)

  private[this] def refreshServer(): Unit = {
    if (server != null) {
      stop()
      server.destroy()
      server.synchronized {
        server = newServer(port)
      }
    } else {
      server = newServer(port)
    }
  }

  private[this] def getEnvVarOrSysProp(key: String): Option[String] = {
    sys.env.get(key).orElse(sys.props.get(key))
  }
}

Do you know of any cleaner way to do this?

@seratch
Copy link
Member

seratch commented Apr 27, 2023

Hi @elrob, thanks for writing in!

hmm, this is interesting. I had been assuming that Jetty automatically reflects all the system properties runtime but it does not work in the way when you build embedded web server like this library does.

I don't have any other ideas so far. I think that the lines of code you added are safe enough for any apps. So, if you send a pull request to this repo (no pressure!), I am happy to merge it and release a new version with the improvement! Perhaps, the hard-coded 40000 should be customizable. In my opinion, reading the standard system property can be the right way to go.

@elrob
Copy link
Author

elrob commented Apr 27, 2023

Thanks!
Yeah, I don't think my change is general enough.
If I could work out how to pick up general jetty config then I think it would make sense as a PR but I've tried that for an hour now and couldn't work out how to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants