Skip to content

Commit

Permalink
fix(android): fix boundary value extraction for form-data requests
Browse files Browse the repository at this point in the history
Applies changes made to iOS in 58045b0 to android platform
  • Loading branch information
michaelwolz committed Jun 19, 2024
1 parent 58045b0 commit bc6a6e0
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private void writeObjectRequestBody(JSObject object) throws IOException, JSONExc

private void writeFormDataRequestBody(String contentType, JSArray entries) throws IOException, JSONException {
try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) {
String boundary = contentType.split(";")[1].split("=")[1];
String boundary = extractBoundary(contentType);
String lineEnd = "\r\n";
String twoHyphens = "--";

Expand Down Expand Up @@ -301,6 +301,39 @@ private void writeFormDataRequestBody(String contentType, JSArray entries) throw
}
}

/**
* Extracts the boundary value from the `Content-Type` header for multipart/form-data requests, if provided.
*
* The boundary value might be surrounded by double quotes (") which will be stripped away.
*
* @param contentType The `Content-Type` header string.
* @return The boundary value if found, otherwise `null`.
*/
public static String extractBoundary(String contentType) {
String boundaryPrefix = "boundary=";
int boundaryIndex = contentType.indexOf(boundaryPrefix);
if (boundaryIndex == -1) {
return null;
}

// Extract the substring starting right after "boundary="
String boundary = contentType.substring(boundaryIndex + boundaryPrefix.length());

// Find the end of the boundary value by looking for the next ";"
int endIndex = boundary.indexOf(";");
if (endIndex != -1) {
boundary = boundary.substring(0, endIndex);
}

// Remove surrounding double quotes if present
boundary = boundary.trim();
if (boundary.startsWith("\"") && boundary.endsWith("\"")) {
boundary = boundary.substring(1, boundary.length() - 1);
}

return boundary;
}

/**
* Opens a communications link to the resource referenced by this
* URL, if such a connection has not already been established.
Expand Down

0 comments on commit bc6a6e0

Please sign in to comment.