Uploaded image for project: 'Felix'
  1. Felix
  2. FELIX-6160

Dynamic HTTP(S) port assignment does not work with HTTP Jetty

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • http.jetty-4.0.10
    • http.jetty-4.0.12
    • HTTP Service
    • None

    Description

      The documentation of Felix HTTP describes the feature that "org.osgi.service.http.port"=0 or "org.osgi.service.http.port.secure"=0 will bind to a free port.

      Felix HTTP Jetty does bind to a free port as expected but then it does try to store the real port in its service properties and this is where it goes wrong.
      Http Jetty stores the real port in the properties "org.osgi.service.http.port" and "org.osgi.service.http.port.secure" of HttpService and HttpServiceRuntime.

      Unfortunately it uses JettyConfig.getHttp(s)Port() to do this. getHttpPort() calls determinePort() which calls getSocketPort(0). And getSocketPort(0) opens the next free socket, stores the port number and closes the port immediately.
      So the next time getHttp(s)Port() is called a different port might be returned.

      This is where for example getHttpPort() is called (getHttpsPort() is similar):

      JettyConfig.isUseHttp():
          return useHttp && getHttpPort() > 0;
      JettyConfig.setServiceProperties():
          props.put(HTTP_PORT, Integer.toString(getHttpPort()));
      JettyService.initializeJetty():
          message.append(" HTTP:").append(this.config.getHttpPort());
      JettyService.initializeHttp():
          configureConnector(connector, this.config.getHttpPort());
      

      So each time a different port is returned and there is no possibility to determine which the used port really is.
      For example in the log it mentions:

      [CM Configuration Updater (Update: pid=org.apache.felix.http)] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@74c8438a{SSL,[ssl, http/1.1]}{0.0.0.0:55707}
      [DEBUG] Adding bundle de.sma.ennexos.webui.rest.api.ioconfigurationapi:0.1.0.5C1DA7096F86D478F13D5148A119A1AFCB0AAA87 (111) : active
      [INFO] Started Jetty 9.4.12.v20180830 at port(s) HTTP:55705 HTTPS:55712 on context path / [minThreads=8,maxThreads=200,acceptors=1,selectors=2]
      

      So JettyService.initializeHttp() uses port 55707 for HTTPS (this is really the port that is used) whereas the log output (JettyService.initializeJetty()) one line below mentions port 55712 which is wrong. When I lock into the service properties of HttpService or HttpServiceRuntime "org.osgi.service.http.port.secure" is set to another wrong port (e.g. 55711).

      Possible fix:

      • either make sure that determinePort()/getHttpPort()/getHttpsPort() is only called once per HttpService (e.g. make it private) and only use the HTTP(S)_PORT property of the HTTP service
      • or make getHttpPort() remember the last returned port so that it can return the same port when it is called multiple times. I am not sure if each HttpService has an own JettyConfig otherwise this solution would probably not work. Also config updates should also make getHttpPort() return the new port.

      Attachments

        Activity

          People

            cziegeler Carsten Ziegeler
            tobgun Tobias Gunkel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: