| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  http_request.js                                                      */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2020-01-29 02:48:10 +01:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2021-01-01 20:13:46 +01:00
										 |  |  | /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const GodotHTTPRequest = { | 
					
						
							|  |  |  | 	$GodotHTTPRequest__deps: ['$GodotRuntime'], | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 	$GodotHTTPRequest: { | 
					
						
							|  |  |  | 		requests: [], | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 		getUnusedRequestId: function () { | 
					
						
							|  |  |  | 			const idMax = GodotHTTPRequest.requests.length; | 
					
						
							|  |  |  | 			for (let potentialId = 0; potentialId < idMax; ++potentialId) { | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 				if (GodotHTTPRequest.requests[potentialId] instanceof XMLHttpRequest) { | 
					
						
							|  |  |  | 					continue; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return potentialId; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 			GodotHTTPRequest.requests.push(null); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 			return idMax; | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 		setupRequest: function (xhr) { | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 			xhr.responseType = 'arraybuffer'; | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_new__sig: 'i', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_new: function () { | 
					
						
							|  |  |  | 		const newId = GodotHTTPRequest.getUnusedRequestId(); | 
					
						
							|  |  |  | 		GodotHTTPRequest.requests[newId] = new XMLHttpRequest(); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		GodotHTTPRequest.setupRequest(GodotHTTPRequest.requests[newId]); | 
					
						
							|  |  |  | 		return newId; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_reset__sig: 'vi', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_reset: function (xhrId) { | 
					
						
							|  |  |  | 		GodotHTTPRequest.requests[xhrId] = new XMLHttpRequest(); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		GodotHTTPRequest.setupRequest(GodotHTTPRequest.requests[xhrId]); | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_free__sig: 'vi', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_free: function (xhrId) { | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		GodotHTTPRequest.requests[xhrId].abort(); | 
					
						
							|  |  |  | 		GodotHTTPRequest.requests[xhrId] = null; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_open__sig: 'viiiii', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_open: function (xhrId, method, url, p_user, p_password) { | 
					
						
							| 
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 |  |  | 		const user = p_user > 0 ? GodotRuntime.parseString(p_user) : null; | 
					
						
							|  |  |  | 		const password = p_password > 0 ? GodotRuntime.parseString(p_password) : null; | 
					
						
							|  |  |  | 		GodotHTTPRequest.requests[xhrId].open(GodotRuntime.parseString(method), GodotRuntime.parseString(url), true, user, password); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_set_request_header__sig: 'viii', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_set_request_header: function (xhrId, header, value) { | 
					
						
							| 
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 |  |  | 		GodotHTTPRequest.requests[xhrId].setRequestHeader(GodotRuntime.parseString(header), GodotRuntime.parseString(value)); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-11 09:32:17 +01:00
										 |  |  | 	godot_xhr_send__sig: 'viii', | 
					
						
							|  |  |  | 	godot_xhr_send: function (xhrId, p_ptr, p_len) { | 
					
						
							|  |  |  | 		let data = null; | 
					
						
							|  |  |  | 		if (p_ptr && p_len) { | 
					
						
							|  |  |  | 			data = GodotRuntime.heapCopy(HEAP8, p_ptr, p_len); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-11 09:32:17 +01:00
										 |  |  | 		GodotHTTPRequest.requests[xhrId].send(data); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_abort__sig: 'vi', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_abort: function (xhrId) { | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		GodotHTTPRequest.requests[xhrId].abort(); | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_get_status__sig: 'ii', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_get_status: function (xhrId) { | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		return GodotHTTPRequest.requests[xhrId].status; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_get_ready_state__sig: 'ii', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_get_ready_state: function (xhrId) { | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		return GodotHTTPRequest.requests[xhrId].readyState; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_get_response_headers_length__sig: 'ii', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_get_response_headers_length: function (xhrId) { | 
					
						
							|  |  |  | 		const headers = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders(); | 
					
						
							| 
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 |  |  | 		return headers === null ? 0 : GodotRuntime.strlen(headers); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_get_response_headers__sig: 'viii', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_get_response_headers: function (xhrId, dst, len) { | 
					
						
							|  |  |  | 		const str = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders(); | 
					
						
							|  |  |  | 		if (str === null) { | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 |  |  | 		GodotRuntime.stringToHeap(str, dst, len); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_get_response_length__sig: 'ii', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_get_response_length: function (xhrId) { | 
					
						
							|  |  |  | 		const body = GodotHTTPRequest.requests[xhrId].response; | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		return body === null ? 0 : body.byteLength; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 12:51:17 +01:00
										 |  |  | 	godot_xhr_get_response__sig: 'viii', | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 	godot_xhr_get_response: function (xhrId, dst, len) { | 
					
						
							|  |  |  | 		let buf = GodotHTTPRequest.requests[xhrId].response; | 
					
						
							|  |  |  | 		if (buf === null) { | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | 		buf = new Uint8Array(buf).subarray(0, len); | 
					
						
							|  |  |  | 		HEAPU8.set(buf, dst); | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 12:13:52 +01:00
										 |  |  | autoAddDeps(GodotHTTPRequest, '$GodotHTTPRequest'); | 
					
						
							| 
									
										
										
										
											2017-10-26 03:39:41 +02:00
										 |  |  | mergeInto(LibraryManager.library, GodotHTTPRequest); |