Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Enable gzip compressed payload #222

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.getcapacitor.JSArray;
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -20,6 +21,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.zip.GZIPOutputStream;
import org.json.JSONException;

public class CapacitorHttpUrlConnection implements ICapacitorHttpUrlConnection {
Expand Down Expand Up @@ -170,6 +172,7 @@ public void setDoOutput(boolean shouldDoOutput) {
*/
public void setRequestBody(PluginCall call, JSValue body) throws JSONException, IOException {
String contentType = connection.getRequestProperty("Content-Type");
Boolean gzipCompression = call.getBoolean("gzipCompression", false);
String dataString = "";

if (contentType == null || contentType.isEmpty()) return;
Expand All @@ -186,7 +189,7 @@ public void setRequestBody(PluginCall call, JSValue body) throws JSONException,
} else if (body == null) {
dataString = call.getString("data");
}
this.writeRequestBody(dataString.toString());
this.writeRequestBody(dataString.toString(), gzipCompression);
} else if (contentType.contains("application/x-www-form-urlencoded")) {
StringBuilder builder = new StringBuilder();

Expand All @@ -201,7 +204,7 @@ public void setRequestBody(PluginCall call, JSValue body) throws JSONException,
builder.append("&");
}
}
this.writeRequestBody(builder.toString());
this.writeRequestBody(builder.toString(), gzipCompression);
} else if (contentType.contains("multipart/form-data")) {
FormUploader uploader = new FormUploader(connection);

Expand All @@ -215,7 +218,7 @@ public void setRequestBody(PluginCall call, JSValue body) throws JSONException,
}
uploader.finish();
} else {
this.writeRequestBody(body.toString());
this.writeRequestBody(body.toString(), gzipCompression);
}
}

Expand All @@ -224,10 +227,17 @@ public void setRequestBody(PluginCall call, JSValue body) throws JSONException,
*
* @param body The string value to write to the connection stream.
*/
private void writeRequestBody(String body) throws IOException {
try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) {
os.write(body.getBytes(StandardCharsets.UTF_8));
os.flush();
private void writeRequestBody(String body, boolean gzipCompression) throws IOException {
if (gzipCompression) {
try (GZIPOutputStream gos = new GZIPOutputStream(connection.getOutputStream())) {
gos.write(body.getBytes(StandardCharsets.UTF_8));
gos.flush();
}
} else {
try (DataOutputStream dos = new DataOutputStream(connection.getOutputStream())) {
dos.write(body.getBytes(StandardCharsets.UTF_8));
dos.flush();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public HttpURLConnectionBuilder setUrlParams(JSObject params, boolean shouldEnco
String initialQueryBuilderStr = initialQuery == null ? "" : initialQuery;

Iterator<String> keys = params.keys();

if (!keys.hasNext()) {
return this;
}

StringBuilder urlQueryBuilder = new StringBuilder(initialQueryBuilderStr);

// Build the new query string
Expand Down Expand Up @@ -167,7 +167,13 @@ public HttpURLConnectionBuilder setUrlParams(JSObject params, boolean shouldEnco
URI encodedUri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), urlQuery, uri.getFragment());
this.url = encodedUri.toURL();
} else {
String unEncodedUrlString = uri.getScheme() + "://" + uri.getAuthority() + uri.getPath() + ((!urlQuery.equals("")) ? "?" + urlQuery : "") + ((uri.getFragment() != null) ? uri.getFragment() : "");
String unEncodedUrlString =
uri.getScheme() +
"://" +
uri.getAuthority() +
uri.getPath() +
((!urlQuery.equals("")) ? "?" + urlQuery : "") +
((uri.getFragment() != null) ? uri.getFragment() : "");
this.url = new URL(unEncodedUrlString);
}

Expand Down Expand Up @@ -368,12 +374,18 @@ public static JSObject request(PluginCall call, String httpMethod) throws IOExce
Integer readTimeout = call.getInt("readTimeout");
Boolean disableRedirects = call.getBoolean("disableRedirects");
Boolean shouldEncode = call.getBoolean("shouldEncodeUrlParams", true);
Boolean gzipCompression = call.getBoolean("gzipCompression", false);
ResponseType responseType = ResponseType.parse(call.getString("responseType"));

String method = httpMethod != null ? httpMethod.toUpperCase() : call.getString("method", "").toUpperCase();

boolean isHttpMutate = method.equals("DELETE") || method.equals("PATCH") || method.equals("POST") || method.equals("PUT");

// Set gzip header if compression is enabled
if (gzipCompression) {
headers.put("Content-Encoding", "gzip");
}

URL url = new URL(urlString);
HttpURLConnectionBuilder connectionBuilder = new HttpURLConnectionBuilder()
.setUrl(url)
Expand Down
19 changes: 16 additions & 3 deletions ios/Plugin/CapacitorUrlRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,24 @@ public class CapacitorUrlRequest: NSObject, URLSessionTaskDelegate {
}
}

public func setRequestBody(_ body: JSValue) throws {
let contentType = self.getRequestHeader("Content-Type") as? String
public func getRequestDataAsGzip(_ body: JSValue) throws -> Data? {
// string to Data
let dataBody = try getRequestDataAsString(body)

// gzip compression
let compressedData: Data = try dataBody.gzipped()
return compressedData
}

public func setRequestBody(_ body: JSValue, _ gzipCompression: Bool) throws {
let contentType = self.getRequestHeader("Content-Type") as? String

if contentType != nil {
request.httpBody = try getRequestData(body, contentType!)
if(gzipCompression) {
request.httpBody = try getRequestDataAsGzip(body)
} else {
request.httpBody = try getRequestData(body, contentType!)
}
}
}

Expand Down
Loading