Welcome to mirror list, hosted at ThFree Co, Russian Federation.

http.ts « common « base « vs « src - github.com/microsoft/vscode.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 932bc57e1e4b88a6555bbca764dc3623b0d2d9d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

// Methods
export var GET = 'GET';
export var POST = 'POST';
export var PUT = 'PUT';
export var DELETE = 'DELETE';

// Header
export namespace Header {
	export var CONTENT_TYPE = 'Content-Type';
	export var CONTENT_LENGTH = 'Content-Length';
	export var LAST_MODIFIED = 'Last-Modified';
	export var LOCATION = 'Location';
	export var ETAG = 'ETag';
	export var X_CONTENT_CHARSET = 'X-Content-Charset';
	export var X_CONTENT_TYPES = 'X-Content-Types';
	export var X_CONTENT_HASH = 'X-Content-Hash';
	export var X_FILEPATH = 'X-Filepath';
	export var X_RESOURCE = 'X-Resource';
}

// Mime
export namespace Mime {
	export var RAW = 'application/octet-stream';
	export var JSON = 'application/json';
	export var TEXT = 'text/plain';
	export var HTML = 'text/html';
}

// Charset
export namespace Charset {
	export var UTF8 = 'utf-8';
	export var UTF8_BOM = 'UTF8_BOM';
}

export interface IDataChunk {
	header(name:string):string;
	value():string;
}


export interface IXHROptions {
	type?:string;
	url?:string;
	user?:string;
	password?:string;
	responseType?:string;
	headers?:any;
	timeout?: number;
	followRedirects?: number;
	data?:any;
}

export interface IXHRResponse {
	responseText: string;
	status: number;

	readyState : number;
	getResponseHeader: (header:string) => string;
}

export function isRedirect(status: number) : boolean {
	return status >= 300 && status <= 303 || status === 307;
}

var contentLengthPattern = /X-Chunk-Length:(\d+)\r\n\r\n/gi,
	headerPattern = /(.+?):(.+?)\r\n(\r\n)?/gm;

function newDataChunk(responseText:string, headerStartOffset:number, headerEndOffset:number, contentLength:number):IDataChunk {

	var _value:string,
		_headers:{[name:string]:string};

	return {
		header: function(name:string):string {
			if(typeof _headers === 'undefined') {
				_headers = Object.create(null);
				headerPattern.lastIndex = headerStartOffset;
				while(true) {
					var match = headerPattern.exec(responseText);
					if(!match) {
						// no header found
						break;
					}
					_headers[match[1].toLowerCase()] = match[2];

					if(match[3]) {
						// the last header found
						break;
					}
				}
			}
			return _headers[name.toLowerCase()];
		},
		value: function() {
			if(typeof _value === 'undefined') {
				_value = responseText.substr(headerEndOffset + 2 /*crlf*/, contentLength);
			}
			return _value;
		}
	};
}

/**
 * Parses the response text of the provided request into individual data chunks. The chunks
 * are filled into the provided array.
 */
export function parseChunkedData(request:IXHRResponse, collection:IDataChunk[], offset:number = 0):number {

	var responseText = request.responseText;

	contentLengthPattern.lastIndex = offset;

	while(true) {
		var match = contentLengthPattern.exec(responseText);
		if(!match) {
			return offset;
		}
		var contentLength = parseInt(match[1], 10);
		if(responseText.length < contentLengthPattern.lastIndex + contentLength) {
			return offset;
		}

		collection.push(newDataChunk(responseText, offset, contentLengthPattern.lastIndex - 2, contentLength));
		offset = contentLengthPattern.lastIndex + contentLength;
	}
}