hapifhir / hapi-fhir

🔥 HAPI FHIR - Java API for HL7 FHIR Clients and Servers
http://hapifhir.io
Apache License 2.0
2.02k stars 1.32k forks source link

Bug in IncomingRequestAddressStrategy #1874

Open dconlan opened 4 years ago

dconlan commented 4 years ago

We have a hapi jpa server running 4.2 which is deployed on tomcat with a webapp name of fhir-server. The fhir endpoint is set to /fhir

The paging mechanism returned links that look like: https://hostname/fhir-server/fhir?_getpages=423358e7-0bdc-49c7-8c79-4a69e618d404&_getpagesoffset=40&_count=20&_pretty=true&_bundletype=searchset

But when you followed these links all subsequent links changed there root to https://hostname/fhir

Looking at the code for IncomingRequestAddressStrategy, there is an edge case which is causing the issue. In our case its around this code:

                       contextIndex = requestUrl.indexOf(servletPath + "/", startOfPath);
            if (contextIndex == -1) {
                contextIndex = requestUrl.indexOf(servletPath, startOfPath);
            }

In this case our requestUrl is 'https://hostname/fhir-server/fhir' and the servlet path is '/fhir'

The first search returns -1 as there is no '/fhir/' in the requestURL and in the second (searching for '/fhir'), we are matching the start of the webapp name instead of the servlet name.

One possible solution would be to use lastIndexOf instead of indexOf (without the startindex), but its possible this would cause issues in other cases.

Inspecting the code, its feasible a similar issue would exist if the webapp name includes the requestPath, if the servletPath is empty or '/'

ftbe commented 3 years ago

We have encountered the same problem in 5.2.

As a workaround, we had to set hapi.fhir.server_address in the configuration.

Another way to fix the problem would be to generate paging links with a trailing '/' in RestfulServerUtils.createPagingLink().

jtsaich commented 1 year ago

Has the same issue