You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by "Tobias Gunkel (JIRA)" <ji...@apache.org> on 2019/07/12 14:37:00 UTC

[jira] [Created] (FELIX-6160) Dynamic HTTP(S) port assignment does not work with HTTP Jetty

Tobias Gunkel created FELIX-6160:
------------------------------------

             Summary: Dynamic HTTP(S) port assignment does not work with HTTP Jetty
                 Key: FELIX-6160
                 URL: https://issues.apache.org/jira/browse/FELIX-6160
             Project: Felix
          Issue Type: Bug
          Components: HTTP Service
    Affects Versions: http.jetty-4.0.10
            Reporter: Tobias Gunkel


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):
{code}
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());
{code}

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:
{code}
[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]
{code}

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.



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)