From a983d1c14b8f77d8bae01f2dd8162f97018efcfe Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 19 Jul 2019 11:45:12 +0200 Subject: [PATCH] MOBILE-3106 core: Use GET as fallback in get public config --- src/classes/site.ts | 19 +++++++++++++++++-- src/providers/ws.ts | 36 +++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/classes/site.ts b/src/classes/site.ts index 9253f83e6..47a64ffe8 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -21,7 +21,7 @@ import { CoreDbProvider } from '@providers/db'; import { CoreEventsProvider } from '@providers/events'; import { CoreFileProvider } from '@providers/file'; import { CoreLoggerProvider } from '@providers/logger'; -import { CoreWSProvider, CoreWSPreSets, CoreWSFileUploadOptions } from '@providers/ws'; +import { CoreWSProvider, CoreWSPreSets, CoreWSFileUploadOptions, CoreWSAjaxPreSets } from '@providers/ws'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTimeUtilsProvider } from '@providers/utils/time'; @@ -1432,7 +1432,22 @@ export class CoreSite { * @return {Promise} Promise resolved with public config. Rejected with an object if error, see CoreWSProvider.callAjax. */ getPublicConfig(): Promise { - return this.wsProvider.callAjax('tool_mobile_get_public_config', {}, { siteUrl: this.siteUrl }).then((config) => { + const preSets: CoreWSAjaxPreSets = { + siteUrl: this.siteUrl + }; + + return this.wsProvider.callAjax('tool_mobile_get_public_config', {}, preSets).catch((error) => { + + if ((!this.getInfo() || this.isVersionGreaterEqualThan('3.8')) && error && error.errorcode == 'codingerror') { + // This error probably means that there is a redirect in the site. Try to use a GET request. + preSets.noLogin = true; + preSets.useGet = true; + + return this.wsProvider.callAjax('tool_mobile_get_public_config', {}, preSets); + } + + return Promise.reject(error); + }).then((config) => { // Use the wwwroot returned by the server. if (config.httpswwwroot) { this.siteUrl = config.httpswwwroot; diff --git a/src/providers/ws.ts b/src/providers/ws.ts index 0cc906f5a..08d53055c 100644 --- a/src/providers/ws.ts +++ b/src/providers/ws.ts @@ -76,6 +76,18 @@ export interface CoreWSAjaxPreSets { * @type {boolean} */ responseExpected?: boolean; + + /** + * Whether to use the no-login endpoint instead of the normal one. Use it for requests that don't require authentication. + * @type {boolean} + */ + noLogin?: boolean; + + /** + * Whether to send the parameters via GET. Only if noLogin is true. + * @type {boolean} + */ + useGet?: boolean; } /** @@ -215,8 +227,7 @@ export class CoreWSProvider { * - available: 0 if unknown, 1 if available, -1 if not available. */ callAjax(method: string, data: any, preSets: CoreWSAjaxPreSets): Promise { - let siteUrl, - ajaxData; + let promise; if (typeof preSets.siteUrl == 'undefined') { return rejectWithError(this.createFakeWSError('core.unexpectederror', true)); @@ -228,17 +239,24 @@ export class CoreWSProvider { preSets.responseExpected = true; } - ajaxData = [{ - index: 0, - methodname: method, - args: this.convertValuesToString(data) - }]; + const script = preSets.noLogin ? 'service-nologin.php' : 'service.php', + ajaxData = JSON.stringify([{ + index: 0, + methodname: method, + args: this.convertValuesToString(data) + }]); // The info= parameter has no function. It is just to help with debugging. // We call it info to match the parameter name use by Moodle's AMD ajax module. - siteUrl = preSets.siteUrl + '/lib/ajax/service.php?info=' + method; + let siteUrl = preSets.siteUrl + '/lib/ajax/' + script + '?info=' + method; - const promise = this.http.post(siteUrl, JSON.stringify(ajaxData)).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + if (preSets.noLogin && preSets.useGet) { + // Send params using GET. + siteUrl += '&args=' + encodeURIComponent(ajaxData); + promise = this.http.get(siteUrl).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + } else { + promise = this.http.post(siteUrl, ajaxData).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + } return promise.then((data: any) => { // Some moodle web services return null.