From 592e71fe47ef7b8a40b1b35c35e8ea3ce1da5e29 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Tue, 20 Jun 2017 21:18:19 -0700 Subject: [PATCH] Improve HTTPRequest default Content-Type --- lime/_backend/html5/HTML5HTTPRequest.hx | 40 ++++++++++++++++++++--- lime/_backend/native/NativeHTTPRequest.hx | 37 ++++++++++++++++++--- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/lime/_backend/html5/HTML5HTTPRequest.hx b/lime/_backend/html5/HTML5HTTPRequest.hx index f3c82aa62..ad9e1e16b 100644 --- a/lime/_backend/html5/HTML5HTTPRequest.hx +++ b/lime/_backend/html5/HTML5HTTPRequest.hx @@ -96,8 +96,11 @@ class HTML5HTTPRequest { } request.open (Std.string (parent.method), uri, true); + if (parent.timeout > 0) { + request.timeout = parent.timeout; + } if (binary) { @@ -106,18 +109,45 @@ class HTML5HTTPRequest { } - var hasContentType = false; + var contentType = null; for (header in parent.headers) { - if (header.name == "Content-Type") hasContentType = true; - request.setRequestHeader (header.name, header.value); + if (header.name == "Content-Type") { + + contentType = header.value; + + } else { + + request.setRequestHeader (header.name, header.value); + + } } - if (!hasContentType && parent.contentType != null) { + if (parent.contentType != null) { - request.setRequestHeader ("Content-Type", parent.contentType); + contentType = parent.contentType; + + } + + if (contentType == null) { + + if (parent.data != null) { + + contentType = "application/octet-stream"; + + } else if (query != "") { + + contentType = "application/x-www-form-urlencoded"; + + } + + } + + if (contentType != null) { + + request.setRequestHeader ("Content-Type", contentType); } diff --git a/lime/_backend/native/NativeHTTPRequest.hx b/lime/_backend/native/NativeHTTPRequest.hx index fc5ccf847..7f173c5f4 100644 --- a/lime/_backend/native/NativeHTTPRequest.hx +++ b/lime/_backend/native/NativeHTTPRequest.hx @@ -261,18 +261,45 @@ class NativeHTTPRequest { var headers = []; headers.push ("Expect: "); - var hasContentType = false; + var contentType = null; for (header in cast (parent.headers, Array)) { - if (header.name == "Content-Type") hasContentType = true; - headers.push ('${header.name}: ${header.value}'); + if (header.name == "Content-Type") { + + contentType = header.value; + + } else { + + headers.push ('${header.name}: ${header.value}'); + + } } - if (!hasContentType) { + if (parent.contentType != null) { - headers.push ("Content-Type: " + parent.contentType); + contentType = parent.contentType; + + } + + if (contentType == null) { + + if (parent.data != null) { + + contentType = "application/octet-stream"; + + } else if (query != "") { + + contentType = "application/x-www-form-urlencoded"; + + } + + } + + if (contentType != null) { + + headers.push ("Content-Type: " + contentType); }