Skip to content

Commit

Permalink
indirection_server: send Timezone parameter with routeToken request w…
Browse files Browse the repository at this point in the history
…hen geolocation is enabled

Signed-off-by: Rashesh Padia <[email protected]>
Change-Id: I600e66f1a5708a5273ee26c72bac5f2e87ac0b87
  • Loading branch information
Rash419 committed Aug 5, 2024
1 parent 64e8333 commit 37f7c2f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 46 deletions.
1 change: 1 addition & 0 deletions browser/html/cool.html.m4
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ m4_ifelse(MOBILEAPP, [true],
data-saved-ui-state = "%SAVED_UI_STATE%"
data-wasm-enabled = "%WASM_ENABLED%"
data-indirection-url = "%INDIRECTION_URL%"
data-geolocation-setup = "%GEOLOCATION_SETUP%"
/>
])

Expand Down
103 changes: 57 additions & 46 deletions browser/js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ class InitializerBase {
window.savedUIState = true;
window.wasmEnabled = false;
window.indirectionUrl = "";
window.geolocationSetup = false;

window.tileSize = 256;

Expand Down Expand Up @@ -406,6 +407,7 @@ class BrowserInitializer extends InitializerBase {
window.savedUIState = element.dataset.savedUiState.toLowerCase().trim() === "true";
window.wasmEnabled = element.dataset.wasmEnabled.toLowerCase().trim() === "true";
window.indirectionUrl = element.dataset.indirectionUrl;
window.geolocationSetup = element.dataset.geolocationSetup.toLowerCase().trim() === "true";
}

postMessageHandler(e) {
Expand Down Expand Up @@ -1385,54 +1387,63 @@ function getInitializerClass() {
global.parent.postMessage(JSON.stringify(msg), '*');
};

var http = new XMLHttpRequest();
let url = global.indirectionUrl + '?Uri=' + encodeURIComponent(that.uri);
http.open('GET', url, true);
http.responseType = 'json';
http.addEventListener('load', function() {
if (this.status === 200) {
var uriWithRouteToken = http.response.uri;
global.expectedServerId = http.response.serverId;
var params = (new URL(uriWithRouteToken)).searchParams;
global.routeToken = params.get('RouteToken');
global.app.console.log('updated routeToken: ' + global.routeToken);
that.innerSocket = new WebSocket(uriWithRouteToken);
that.innerSocket.binaryType = that.binaryType;
that.innerSocket.onerror = function() {
that.readyState = that.innerSocket.readyState;
that.onerror();
};
that.innerSocket.onclose = function() {
that.readyState = 3;
that.onclose();
that.innerSocket.onerror = function () {};
that.innerSocket.onclose = function () {};
that.innerSocket.onmessage = function () {};
};
that.innerSocket.onopen = function() {
that.readyState = 1;
that.onopen();
};
that.innerSocket.onmessage = function(e) {
that.readyState = that.innerSocket.readyState;
that.onmessage(e);
};
} else if (this.status === 202) {
if (!(window.app && window.app.socket && window.app.socket._reconnecting)) {
this.sendRouteTokenRequest = function (requestUri) {
var http = new XMLHttpRequest();
// let url = global.indirectionUrl + '?Uri=' + encodeURIComponent(that.uri);
http.open('GET', requestUri, true);
http.responseType = 'json';
http.addEventListener('load', function () {
if (this.status === 200) {
var uriWithRouteToken = http.response.uri;
global.expectedServerId = http.response.serverId;
var params = (new URL(uriWithRouteToken)).searchParams;
global.routeToken = params.get('RouteToken');
global.app.console.log('updated routeToken: ' + global.routeToken);
that.innerSocket = new WebSocket(uriWithRouteToken);
that.innerSocket.binaryType = that.binaryType;
that.innerSocket.onerror = function () {
that.readyState = that.innerSocket.readyState;
that.onerror();
};
that.innerSocket.onclose = function () {
that.readyState = 3;
that.onclose();
that.innerSocket.onerror = function () { };
that.innerSocket.onclose = function () { };
that.innerSocket.onmessage = function () { };
};
that.innerSocket.onopen = function () {
that.readyState = 1;
that.onopen();
};
that.innerSocket.onmessage = function (e) {
that.readyState = that.innerSocket.readyState;
that.onmessage(e);
};
} else if (this.status === 202) {
if (!(window.app && window.app.socket && window.app.socket._reconnecting)) {
that.sendPostMsg(http.response.errorCode);
}
var timeoutFn = function (requestUri) {
console.warn('Requesting again for routeToken');
this.open('GET', requestUri, true);
this.send();
}.bind(this);
setTimeout(timeoutFn, 3000, requestUri);
} else {
global.app.console.error('Indirection url: error on incoming response ' + this.status);
that.sendPostMsg(-1);
}
var timeoutFn = function (indirectionUrl, uri) {
console.warn('Requesting again for routeToken');
this.open('GET', indirectionUrl + '?Uri=' + encodeURIComponent(uri), true);
this.send();
}.bind(this);
setTimeout(timeoutFn, 3000, global.indirectionUrl, that.uri);
} else {
global.app.console.error('Indirection url: error on incoming response ' + this.status);
that.sendPostMsg(-1);
}
});
http.send();
});
http.send();
};

let requestUri = global.indirectionUrl + '?Uri=' + encodeURIComponent(that.uri);
if (global.geolocationSetup) {
let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
requestUri += "&TimeZone=" + timeZone;
}
this.sendRouteTokenRequest(requestUri);
};

global.createWebSocket = function(uri) {
Expand Down
3 changes: 3 additions & 0 deletions coolwsd.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@
<indirection_endpoint>
<url desc="URL endpoint to server which servers routeToken in json format" default=""></url>
<migration_timeout_secs desc="The maximum number of seconds waiting for shutdown migration message from indirection server before unloading an document. Defaults to 180 second." type="uint" default="180"></migration_timeout_secs>
<geolocation_setup>
<enable desc="Enable geolocation_setup when using indirection server with geolocation configuration" type="bool" default="false">false</enable>
</geolocation_setup>
</indirection_endpoint>

@LOCK_CONFIGURATION@
Expand Down
30 changes: 30 additions & 0 deletions wsd/ClientRequestDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,27 @@ std::string ClientRequestDispatcher::getDiscoveryXML()

#if !MOBILEAPP

/// Get IANA timezone name
static std::string getIANATimezone()
{
const char* tzfile = "/etc/localtime";
char buf[PATH_MAX];

ssize_t len = readlink(tzfile, buf, sizeof(buf) - 1);
if (len != -1)
{
buf[len] = '\0';
std::string fullPath(buf);

std::string prefix = "../usr/share/zoneinfo/";
if (fullPath.substr(0, prefix.size()) == prefix)
{
return fullPath.substr(prefix.size());
}
}
return std::string();
}

/// Create the /hosting/capabilities JSON and return as string.
static std::string getCapabilitiesJson(bool convertToAvailable)
{
Expand Down Expand Up @@ -2054,6 +2075,15 @@ static std::string getCapabilitiesJson(bool convertToAvailable)
if (const char* podName = std::getenv("POD_NAME"))
capabilities->set("podName", podName);

bool geoLocationSetup =
config::getBool(std::string("indirection_endpoint.geolocation_setup.enable"), false);
if (geoLocationSetup)
{
std::string timezoneName = getIANATimezone();
if (!timezoneName.empty())
capabilities->set("timezone", std::string(timezoneName));
}

std::ostringstream ostrJSON;
capabilities->stringify(ostrJSON);
return ostrJSON.str();
Expand Down
5 changes: 5 additions & 0 deletions wsd/FileServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,11 @@ FileServerRequestHandler::ResourceAccessDetails FileServerRequestHandler::prepro
Poco::URI indirectionURI(config.getString("indirection_endpoint.url", ""));
Poco::replaceInPlace(preprocess, std::string("%INDIRECTION_URL%"), indirectionURI.toString());

bool geoLocationSetup = config.getBool("indirection_endpoint.geolocation_setup.enable");
if (geoLocationSetup)
Poco::replaceInPlace(preprocess, std::string("%GEOLOCATION_SETUP%"),
boolToString(geoLocationSetup));

const std::string mimeType = "text/html";

// Document signing: if endpoint URL is configured, whitelist that for
Expand Down

0 comments on commit 37f7c2f

Please sign in to comment.