MOBILE-3106 core: Use GET as fallback in get public config

main
Dani Palou 2019-07-19 11:45:12 +02:00
parent 5c4872288c
commit a983d1c14b
2 changed files with 44 additions and 11 deletions

View File

@ -21,7 +21,7 @@ import { CoreDbProvider } from '@providers/db';
import { CoreEventsProvider } from '@providers/events'; import { CoreEventsProvider } from '@providers/events';
import { CoreFileProvider } from '@providers/file'; import { CoreFileProvider } from '@providers/file';
import { CoreLoggerProvider } from '@providers/logger'; 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 { CoreDomUtilsProvider } from '@providers/utils/dom';
import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTextUtilsProvider } from '@providers/utils/text';
import { CoreTimeUtilsProvider } from '@providers/utils/time'; import { CoreTimeUtilsProvider } from '@providers/utils/time';
@ -1432,7 +1432,22 @@ export class CoreSite {
* @return {Promise<any>} Promise resolved with public config. Rejected with an object if error, see CoreWSProvider.callAjax. * @return {Promise<any>} Promise resolved with public config. Rejected with an object if error, see CoreWSProvider.callAjax.
*/ */
getPublicConfig(): Promise<any> { getPublicConfig(): Promise<any> {
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. // Use the wwwroot returned by the server.
if (config.httpswwwroot) { if (config.httpswwwroot) {
this.siteUrl = config.httpswwwroot; this.siteUrl = config.httpswwwroot;

View File

@ -76,6 +76,18 @@ export interface CoreWSAjaxPreSets {
* @type {boolean} * @type {boolean}
*/ */
responseExpected?: 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. * - available: 0 if unknown, 1 if available, -1 if not available.
*/ */
callAjax(method: string, data: any, preSets: CoreWSAjaxPreSets): Promise<any> { callAjax(method: string, data: any, preSets: CoreWSAjaxPreSets): Promise<any> {
let siteUrl, let promise;
ajaxData;
if (typeof preSets.siteUrl == 'undefined') { if (typeof preSets.siteUrl == 'undefined') {
return rejectWithError(this.createFakeWSError('core.unexpectederror', true)); return rejectWithError(this.createFakeWSError('core.unexpectederror', true));
@ -228,17 +239,24 @@ export class CoreWSProvider {
preSets.responseExpected = true; preSets.responseExpected = true;
} }
ajaxData = [{ const script = preSets.noLogin ? 'service-nologin.php' : 'service.php',
index: 0, ajaxData = JSON.stringify([{
methodname: method, index: 0,
args: this.convertValuesToString(data) methodname: method,
}]; args: this.convertValuesToString(data)
}]);
// The info= parameter has no function. It is just to help with debugging. // 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. // 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) => { return promise.then((data: any) => {
// Some moodle web services return null. // Some moodle web services return null.