Skip to content

Commit

Permalink
Don't start multipart form data with a line break. Closes #1212
Browse files Browse the repository at this point in the history
  • Loading branch information
F43nd1r committed Jun 18, 2023
1 parent 2088e60 commit 3c48f3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 4 additions & 2 deletions acra-http/src/main/java/org/acra/http/MultipartHttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class MultipartHttpRequest(config: CoreConfiguration, private val context: Conte
.format(CONTENT_TYPE, contentType)
.append(NEW_LINE)
.append(content.first)
.append(NEW_LINE)
for (uri in content.second) {
try {
val name = UriUtils.getFileNameFromUri(context, uri)
Expand All @@ -56,6 +57,7 @@ class MultipartHttpRequest(config: CoreConfiguration, private val context: Conte
.append(NEW_LINE)
.flush()
UriUtils.copyFromUri(context, outputStream, uri)
writer.append(NEW_LINE)
} catch (e: FileNotFoundException) {
warn(e) { "Not sending attachment" }
}
Expand All @@ -67,8 +69,8 @@ class MultipartHttpRequest(config: CoreConfiguration, private val context: Conte
private const val BOUNDARY = "%&ACRA_REPORT_DIVIDER&%"
private const val BOUNDARY_FIX = "--"
private const val NEW_LINE = "\r\n"
private const val SECTION_START = NEW_LINE + BOUNDARY_FIX + BOUNDARY + NEW_LINE
private const val MESSAGE_END = NEW_LINE + BOUNDARY_FIX + BOUNDARY + BOUNDARY_FIX + NEW_LINE
private const val SECTION_START = BOUNDARY_FIX + BOUNDARY + NEW_LINE
private const val MESSAGE_END = BOUNDARY_FIX + BOUNDARY + BOUNDARY_FIX + NEW_LINE
private const val CONTENT_DISPOSITION = "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"$NEW_LINE"
private const val CONTENT_TYPE = "Content-Type: %s$NEW_LINE"
}
Expand Down
20 changes: 15 additions & 5 deletions acra-http/src/main/java/org/acra/util/UriUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,25 @@ import java.io.OutputStream
object UriUtils {
@Throws(IOException::class)
fun copyFromUri(context: Context, outputStream: OutputStream, uri: Uri) {
context.contentResolver.openInputStream(uri)?.copyTo(outputStream, bufferSize = ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES) ?: throw FileNotFoundException(
"Could not open $uri")
context.contentResolver.openInputStream(uri)?.use {
it.copyTo(outputStream, bufferSize = ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES)
} ?: throw FileNotFoundException("Could not open $uri")
}

@Throws(FileNotFoundException::class)
fun getFileNameFromUri(context: Context, uri: Uri): String {
context.contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { cursor ->
if (cursor.moveToFirst()) {
return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
val cursor = context.contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)
if (cursor != null) {
@Suppress("ConvertTryFinallyToUseCall") // cursor is not Closeable until API 16
try {
if (cursor.moveToFirst()) {
val columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
if (columnIndex != -1) {
return cursor.getString(columnIndex)
}
}
} finally {
cursor.close()
}
}
throw FileNotFoundException("Could not resolve filename of $uri")
Expand Down

0 comments on commit 3c48f3f

Please sign in to comment.