33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import { IRequestOptions , IRequestContext } from 'vs/platform /request/common/request' ;
6+ import { IRequestOptions , IRequestContext } from 'vs/base/parts /request/common/request' ;
77import { CancellationToken } from 'vs/base/common/cancellation' ;
8- import { canceled } from 'vs/base/common/errors' ;
98import { IConfigurationService } from 'vs/platform/configuration/common/configuration' ;
109import { ILogService } from 'vs/platform/log/common/log' ;
11- import { assign } from 'vs/base/common/objects' ;
12- import { VSBuffer , bufferToStream } from 'vs/base/common/buffer' ;
10+ import { request } from 'vs/base/parts/request/browser/request' ;
1311
1412/**
1513 * This service exposes the `request` API, while using the global
@@ -28,69 +26,10 @@ export class RequestService {
2826 request ( options : IRequestOptions , token : CancellationToken ) : Promise < IRequestContext > {
2927 this . logService . trace ( 'RequestService#request' , options . url ) ;
3028
31- const authorization = this . configurationService . getValue < string > ( 'http.proxyAuthorization' ) ;
32- if ( authorization ) {
33- options . headers = assign ( options . headers || { } , { 'Proxy-Authorization' : authorization } ) ;
29+ if ( ! options . proxyAuthorization ) {
30+ options . proxyAuthorization = this . configurationService . getValue < string > ( 'http.proxyAuthorization' ) ;
3431 }
3532
36- const xhr = new XMLHttpRequest ( ) ;
37- return new Promise < IRequestContext > ( ( resolve , reject ) => {
38-
39- xhr . open ( options . type || 'GET' , options . url || '' , true , options . user , options . password ) ;
40- this . setRequestHeaders ( xhr , options ) ;
41-
42- xhr . responseType = 'arraybuffer' ;
43- xhr . onerror = e => reject ( new Error ( xhr . statusText && ( 'XHR failed: ' + xhr . statusText ) ) ) ;
44- xhr . onload = ( e ) => {
45- resolve ( {
46- res : {
47- statusCode : xhr . status ,
48- headers : this . getResponseHeaders ( xhr )
49- } ,
50- stream : bufferToStream ( VSBuffer . wrap ( new Uint8Array ( xhr . response ) ) )
51- } ) ;
52- } ;
53- xhr . ontimeout = e => reject ( new Error ( `XHR timeout: ${ options . timeout } ms` ) ) ;
54-
55- if ( options . timeout ) {
56- xhr . timeout = options . timeout ;
57- }
58-
59- xhr . send ( options . data ) ;
60-
61- // cancel
62- token . onCancellationRequested ( ( ) => {
63- xhr . abort ( ) ;
64- reject ( canceled ( ) ) ;
65- } ) ;
66- } ) ;
33+ return request ( options , token ) ;
6734 }
68-
69- private setRequestHeaders ( xhr : XMLHttpRequest , options : IRequestOptions ) : void {
70- if ( options . headers ) {
71- outer: for ( let k in options . headers ) {
72- switch ( k ) {
73- case 'User-Agent' :
74- case 'Accept-Encoding' :
75- case 'Content-Length' :
76- // unsafe headers
77- continue outer;
78- }
79- xhr . setRequestHeader ( k , options . headers [ k ] ) ;
80-
81- }
82- }
83- }
84-
85- private getResponseHeaders ( xhr : XMLHttpRequest ) : { [ name : string ] : string } {
86- const headers : { [ name : string ] : string } = Object . create ( null ) ;
87- for ( const line of xhr . getAllResponseHeaders ( ) . split ( / \r \n | \n | \r / g) ) {
88- if ( line ) {
89- const idx = line . indexOf ( ':' ) ;
90- headers [ line . substr ( 0 , idx ) . trim ( ) . toLowerCase ( ) ] = line . substr ( idx + 1 ) . trim ( ) ;
91- }
92- }
93- return headers ;
94- }
95-
96- }
35+ }
0 commit comments