Skip to content

Handle boundaries in multipart request #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2025
Merged
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
59 changes: 59 additions & 0 deletions common/src/main/java/com/genexus/CommonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.math.BigDecimal;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.*;
import java.util.*;

Expand Down Expand Up @@ -176,6 +179,62 @@ public Object initialValue()
throw new ExceptionInInitializerError("GXUtil static constructor error: " + e.getMessage());
}
}

public static String getContentType(String fileName) {
Path path = Paths.get(fileName);
String defaultContentType = "application/octet-stream";

try {
String probedContentType = Files.probeContentType(path);
if (probedContentType == null || probedContentType.equals(defaultContentType))
return findContentTypeByExtension(fileName);
return probedContentType;
} catch (IOException ioe) {
return findContentTypeByExtension(fileName);
}
}

private static String findContentTypeByExtension(String fileName) {
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
String contentType = contentTypes.get(fileExtension);
return contentType != null ? contentTypes.get(fileExtension) : "application/octet-stream";
}

private static Map<String, String> contentTypes = new HashMap<String, String>() {{
put("txt" , "text/plain");
put("rtx" , "text/richtext");
put("htm" , "text/html");
put("html" , "text/html");
put("xml" , "text/xml");
put("aif" , "audio/x-aiff");
put("au" , "audio/basic");
put("wav" , "audio/wav");
put("bmp" , "image/bmp");
put("gif" , "image/gif");
put("jpe" , "image/jpeg");
put("jpeg" , "image/jpeg");
put("jpg" , "image/jpeg");
put("jfif" , "image/pjpeg");
put("tif" , "image/tiff");
put("tiff" , "image/tiff");
put("png" , "image/x-png");
put("3gp" , "video/3gpp");
put("3g2" , "video/3gpp2");
put("mpg" , "video/mpeg");
put("mpeg" , "video/mpeg");
put("mov" , "video/quicktime");
put("qt" , "video/quicktime");
put("avi" , "video/x-msvideo");
put("exe" , "application/octet-stream");
put("dll" , "application/x-msdownload");
put("ps" , "application/postscript");
put("pdf" , "application/pdf");
put("svg" , "image/svg+xml");
put("tgz" , "application/x-compressed");
put("zip" , "application/x-zip-compressed");
put("gz" , "application/x-gzip");
put("json" , "application/json");
}};

public static String removeAllQuotes(String fileName)
{
Expand Down
41 changes: 24 additions & 17 deletions common/src/main/java/com/genexus/internet/GXHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -568,74 +568,81 @@ protected String setPathUrl(String url) {
return url;
}


protected File fileToPost;
protected String fileToPostName;

@SuppressWarnings("unchecked")
protected byte[] getData()
{
byte[] out = new byte[0];

for (Object key: getVariablesToSend().keySet())
for (Object key : getVariablesToSend().keySet())
{
String value = getMultipartTemplate().getFormDataTemplate((String)key, (String)getVariablesToSend().get(key));
getContentToSend().add(0, value); //Variables al principio
String value = getMultipartTemplate().getFormDataTemplate((String) key, (String) getVariablesToSend().get(key));
getContentToSend().add(0, value); // Variables al principio
}

for (int idx = 0; idx < getContentToSend().size(); idx++)
{
Object curr = getContentToSend().elementAt(idx);

if (curr instanceof String)
if (curr instanceof String)
{
try
{
if(contentEncoding != null)
if (contentEncoding != null)
{
out = addToArray(out, ((String)curr).getBytes(contentEncoding));
}else
out = addToArray(out, ((String) curr).getBytes(contentEncoding));
} else
{
out = addToArray(out, (String) curr);
}
}catch(UnsupportedEncodingException e)
} catch (UnsupportedEncodingException e)
{
System.err.println(e.toString());
out = addToArray(out, (String) curr);
}
}
else if (curr instanceof Object[])
else if (curr instanceof Object[])
{
StringWriter writer = (StringWriter)((Object[])curr)[0];
StringBuffer encoding = (StringBuffer)((Object[])curr)[1];
StringWriter writer = (StringWriter) ((Object[]) curr)[0];
StringBuffer encoding = (StringBuffer) ((Object[]) curr)[1];

if(encoding == null || encoding.length() == 0)
if (encoding == null || encoding.length() == 0)
{
encoding = new StringBuffer("UTF-8");
}
try
{
out = addToArray(out, writer.toString().getBytes(encoding.toString()));
}
catch(UnsupportedEncodingException e)
catch (UnsupportedEncodingException e)
{
out = addToArray(out, writer.toString());
}
}
else if (curr instanceof byte[])
else if (curr instanceof byte[])
{
out = addToArray(out, (byte[]) curr);
}
else //File or FormFile
else // File or FormFile
{
File file;
if (curr instanceof FormFile)
{
FormFile formFile = (FormFile)curr;
FormFile formFile = (FormFile) curr;
out = startMultipartFile(out, formFile.name, formFile.file);
file = new File(formFile.file);
fileToPostName = formFile.name;
}
else
{
file = (File) curr;
}
try (BufferedInputStream bis = new java.io.BufferedInputStream(new FileInputStream(file)))
fileToPost = file;

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)))
{
out = addToArray(out, CommonUtil.readToByteArray(bis));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import com.genexus.CommonUtil;
import com.genexus.util.GXService;
import com.genexus.util.StorageUtils;
import com.genexus.StructSdtMessages_Message;
Expand Down Expand Up @@ -292,62 +293,6 @@ public String copy(String objectUrl, String newName, String tableName, String fi
copyWithoutACL(objectUrl, newName, tableName, fieldName);
}

private String getContentType(String fileName) {
Path path = Paths.get(fileName);
String defaultContentType = "application/octet-stream";

try {
String probedContentType = Files.probeContentType(path);
if (probedContentType == null || probedContentType.equals(defaultContentType))
return findContentTypeByExtension(fileName);
return probedContentType;
} catch (IOException ioe) {
return findContentTypeByExtension(fileName);
}
}

private String findContentTypeByExtension(String fileName) {
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
String contentType = contentTypes.get(fileExtension);
return contentType != null ? contentTypes.get(fileExtension) : "application/octet-stream";
}

private static Map<String, String> contentTypes = new HashMap<String, String>() {{
put("txt" , "text/plain");
put("rtx" , "text/richtext");
put("htm" , "text/html");
put("html" , "text/html");
put("xml" , "text/xml");
put("aif" , "audio/x-aiff");
put("au" , "audio/basic");
put("wav" , "audio/wav");
put("bmp" , "image/bmp");
put("gif" , "image/gif");
put("jpe" , "image/jpeg");
put("jpeg" , "image/jpeg");
put("jpg" , "image/jpeg");
put("jfif" , "image/pjpeg");
put("tif" , "image/tiff");
put("tiff" , "image/tiff");
put("png" , "image/x-png");
put("3gp" , "video/3gpp");
put("3g2" , "video/3gpp2");
put("mpg" , "video/mpeg");
put("mpeg" , "video/mpeg");
put("mov" , "video/quicktime");
put("qt" , "video/quicktime");
put("avi" , "video/x-msvideo");
put("exe" , "application/octet-stream");
put("dll" , "application/x-msdownload");
put("ps" , "application/postscript");
put("pdf" , "application/pdf");
put("svg" , "image/svg+xml");
put("tgz" , "application/x-compressed");
put("zip" , "application/x-zip-compressed");
put("gz" , "application/x-gzip");
put("json" , "application/json");
}};

private String buildPath(String... pathPart) {
ArrayList<String> pathParts = new ArrayList<>();
for (String part : pathPart) {
Expand Down Expand Up @@ -701,7 +646,7 @@ private String copyWithACL(String objectUrl, String newName, String tableName, S
.bucket(bucket)
.key(resourceKey)
.metadata(metadata)
.contentType(getContentType(newName));
.contentType(CommonUtil.getContentType(newName));
if (endpointUrl.contains(".amazonaws.com"))
putObjectRequestBuilder = putObjectRequestBuilder.acl(internalToAWSACLWithACL(acl));
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
Expand Down Expand Up @@ -821,7 +766,7 @@ private String copyWithoutACL(String objectUrl, String newName, String tableName
.bucket(bucket)
.key(resourceKey)
.metadata(metadata)
.contentType(getContentType(newName));
.contentType(CommonUtil.getContentType(newName));
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
client.putObject(putObjectRequest, RequestBody.fromBytes(objectBytes.asByteArray()));

Expand Down
67 changes: 55 additions & 12 deletions java/src/main/java/com/genexus/internet/HttpClientJavaLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,24 +511,67 @@ public void execute(String method, String url) {
HttpPost httpPost = new HttpPost(url.trim());
httpPost.setConfig(reqConfig);
Set<String> keys = getheadersToSend().keySet();
boolean hasConentType = false;
boolean hasContentType = false;

for (String header : keys) {
httpPost.addHeader(header, getheadersToSend().get(header));
if (header.equalsIgnoreCase("Content-type"))
hasConentType = true;
if (header.equalsIgnoreCase("Content-Type")) {
hasContentType = true;
}
}
if (!hasConentType) // Si no se setea Content-type, se pone uno default
httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");

ByteArrayEntity dataToSend;
if (!getIsMultipart() && getVariablesToSend().size() > 0)
dataToSend = new ByteArrayEntity(CommonUtil.hashtable2query(getVariablesToSend()).getBytes());
else
dataToSend = new ByteArrayEntity(getData());
httpPost.setEntity(dataToSend);
if (getIsMultipart()) {
if (!hasContentType) {
httpPost.addHeader("Content-Type", "multipart/form-data");
}

response = httpClient.execute(httpPost, httpClientContext);
String boundary = "----Boundary" + System.currentTimeMillis();
httpPost.removeHeaders("Content-Type");
httpPost.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

for (Map.Entry<String, String> entry : ((Map<String, String>) getVariablesToSend()).entrySet()) {
if ("fileFieldName".equals(entry.getKey())) {
continue;
}
bos.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8));
bos.write(("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r\n\r\n").getBytes(StandardCharsets.UTF_8));
bos.write(entry.getValue().getBytes(StandardCharsets.UTF_8));
bos.write("\r\n".getBytes(StandardCharsets.UTF_8));
}
if (getData() != null && getData().length > 0) {
String fileFieldName = getVariablesToSend().containsKey("fileFieldName") ?
(String) getVariablesToSend().get("fileFieldName") : fileToPostName;
String fileName = fileToPost != null ? fileToPost.getName() : "uploadedFile";
String contentType = fileToPost != null ? CommonUtil.getContentType(fileName) : "application/octet-stream";

bos.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8));
bos.write(("Content-Disposition: form-data; name=\"" + fileFieldName + "\"; filename=\"" + fileName + "\"\r\n").getBytes(StandardCharsets.UTF_8));
bos.write(("Content-Type: " + contentType + "\r\n\r\n").getBytes(StandardCharsets.UTF_8));
bos.write(getData());
bos.write("\r\n".getBytes(StandardCharsets.UTF_8));
}

bos.write(("--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8));
ByteArrayEntity dataToSend = new ByteArrayEntity(bos.toByteArray());
httpPost.setEntity(dataToSend);
} else {
if (!hasContentType) {
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
}

ByteArrayEntity dataToSend;
if (getVariablesToSend().size() > 0) {
String formData = CommonUtil.hashtable2query(getVariablesToSend());
dataToSend = new ByteArrayEntity(formData.getBytes(StandardCharsets.UTF_8));
} else {
dataToSend = new ByteArrayEntity(getData());
}
httpPost.setEntity(dataToSend);
}

response = httpClient.execute(httpPost, httpClientContext);
} else if (method.equalsIgnoreCase("PUT")) {
HttpPut httpPut = new HttpPut(url.trim());
httpPut.setConfig(reqConfig);
Expand Down
Loading