MOBILE-2253 ws: Fix HEAD requests

main
Dani Palou 2017-11-30 15:42:36 +01:00
parent 7df6933435
commit b65b3278fc
2 changed files with 15 additions and 10 deletions

View File

@ -15,6 +15,7 @@
import { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { IonicApp, IonicModule, Platform } from 'ionic-angular'; import { IonicApp, IonicModule, Platform } from 'ionic-angular';
import { HttpModule } from '@angular/http';
import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { SplashScreen } from '@ionic-native/splash-screen'; import { SplashScreen } from '@ionic-native/splash-screen';
@ -63,7 +64,8 @@ export function createTranslateLoader(http: HttpClient) {
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
HttpClientModule, HttpClientModule, // HttpClient is used to make JSON requests. It fails for HEAD requests because there is no content.
HttpModule,
IonicModule.forRoot(MyApp, { IonicModule.forRoot(MyApp, {
pageTransition: 'ios-transition' pageTransition: 'ios-transition'
}), }),

View File

@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { FileTransfer, FileUploadOptions } from '@ionic-native/file-transfer'; import { FileTransfer, FileUploadOptions } from '@ionic-native/file-transfer';
@ -75,7 +76,8 @@ export class CoreWSProvider {
constructor(private http: HttpClient, private translate: TranslateService, private appProvider: CoreAppProvider, constructor(private http: HttpClient, private translate: TranslateService, private appProvider: CoreAppProvider,
private textUtils: CoreTextUtilsProvider, logger: CoreLoggerProvider, private utils: CoreUtilsProvider, private textUtils: CoreTextUtilsProvider, logger: CoreLoggerProvider, private utils: CoreUtilsProvider,
private fileProvider: CoreFileProvider, private fileTransfer: FileTransfer, private mimeUtils: CoreMimetypeUtilsProvider) { private fileProvider: CoreFileProvider, private fileTransfer: FileTransfer, private commonHttp: Http,
private mimeUtils: CoreMimetypeUtilsProvider) {
this.logger = logger.getInstance('CoreWSProvider'); this.logger = logger.getInstance('CoreWSProvider');
} }
@ -373,7 +375,7 @@ export class CoreWSProvider {
} }
return this.performHead(url).then((data) => { return this.performHead(url).then((data) => {
let mimeType = data.headers('Content-Type'); let mimeType = data.headers.get('Content-Type');
if (mimeType) { if (mimeType) {
// Remove "parameters" like charset. // Remove "parameters" like charset.
mimeType = mimeType.split(';')[0]; mimeType = mimeType.split(';')[0];
@ -395,7 +397,7 @@ export class CoreWSProvider {
*/ */
getRemoteFileSize(url: string) : Promise<number> { getRemoteFileSize(url: string) : Promise<number> {
return this.performHead(url).then((data) => { return this.performHead(url).then((data) => {
let size = parseInt(data.headers('Content-Length'), 10); let size = parseInt(data.headers.get('Content-Length'), 10);
if (size) { if (size) {
return size; return size;
@ -432,8 +434,8 @@ export class CoreWSProvider {
let promise = this.getPromiseHttp('head', url); let promise = this.getPromiseHttp('head', url);
if (!promise) { if (!promise) {
promise = this.utils.observableToPromise(this.http.head(url).timeout(CoreConstants.wsTimeout)); promise = this.utils.observableToPromise(this.commonHttp.head(url).timeout(CoreConstants.wsTimeout));
this.setPromiseHttp(promise, 'head', url); promise = this.setPromiseHttp(promise, 'head', url);
} }
return promise; return promise;
@ -483,7 +485,7 @@ export class CoreWSProvider {
// Only process the queue one time. // Only process the queue one time.
if (this.retryTimeout == 0) { if (this.retryTimeout == 0) {
this.retryTimeout = parseInt(error.headers('Retry-After'), 10) || 5; this.retryTimeout = parseInt(error.headers.get('Retry-After'), 10) || 5;
this.logger.warn(`${error.statusText}. Retrying in ${this.retryTimeout} seconds. ` + this.logger.warn(`${error.statusText}. Retrying in ${this.retryTimeout} seconds. ` +
`${this.retryCalls.length} calls left.`); `${this.retryCalls.length} calls left.`);
@ -503,7 +505,7 @@ export class CoreWSProvider {
return Promise.reject(this.createFakeWSError('mm.core.serverconnection', true)); return Promise.reject(this.createFakeWSError('mm.core.serverconnection', true));
}); });
this.setPromiseHttp(promise, 'post', preSets.siteUrl, ajaxData); promise = this.setPromiseHttp(promise, 'post', preSets.siteUrl, ajaxData);
return promise; return promise;
} }
@ -532,8 +534,9 @@ export class CoreWSProvider {
* @param {string} method Method of the HTTP request. * @param {string} method Method of the HTTP request.
* @param {string} url Base URL of the HTTP request. * @param {string} url Base URL of the HTTP request.
* @param {any} [params] Params of the HTTP request. * @param {any} [params] Params of the HTTP request.
* @return {Promise<any>} The promise saved.
*/ */
protected setPromiseHttp(promise: Promise<any>, method: string, url: string, params?: any) : void { protected setPromiseHttp(promise: Promise<any>, method: string, url: string, params?: any) : Promise<any> {
let timeout, let timeout,
queueItemId = this.getQueueItemId(method, url, params); queueItemId = this.getQueueItemId(method, url, params);
@ -545,7 +548,7 @@ export class CoreWSProvider {
}, CoreConstants.wsTimeout); }, CoreConstants.wsTimeout);
// HTTP finished, delete from ongoing. // HTTP finished, delete from ongoing.
this.ongoingCalls[queueItemId].finally(() => { return promise.finally(() => {
delete this.ongoingCalls[queueItemId]; delete this.ongoingCalls[queueItemId];
clearTimeout(timeout); clearTimeout(timeout);