Skip to content

Commit de8cb7b

Browse files
committed
fix: add extractFormFields() extension for URLRouting trait
- Implement extractFormFields() method on RFC_2046.Multipart - Extracts form fields from multipart/form-data into dictionary - Parses Content-Disposition header to get field names - Converts body content to string values - Required for URLRouting trait functionality - URLRouting trait remains opt-in Fixes compilation error when URLRouting trait is enabled
1 parent 7202f87 commit de8cb7b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Multipart+FormFields.swift
3+
// MultipartFormCoding
4+
//
5+
// Created by swift-multipart-form-coding contributors
6+
//
7+
8+
import Foundation
9+
import RFC_2046
10+
11+
extension RFC_2046.Multipart {
12+
/// Extracts form fields from multipart/form-data into a dictionary
13+
/// - Returns: Dictionary mapping field names to their values
14+
public func extractFormFields() -> [String: Any] {
15+
var fields: [String: Any] = [:]
16+
17+
for part in parts {
18+
// Extract the field name from Content-Disposition header (case-insensitive)
19+
guard let contentDisposition = part.headers.first(where: { $0.key.lowercased() == "content-disposition" })?.value else {
20+
continue
21+
}
22+
23+
// Parse the name parameter from Content-Disposition
24+
// Format: form-data; name="fieldname"
25+
guard let nameStart = contentDisposition.range(of: "name=\""),
26+
let nameEnd = contentDisposition[nameStart.upperBound...].range(of: "\"") else {
27+
continue
28+
}
29+
30+
let fieldName = String(contentDisposition[nameStart.upperBound..<nameEnd.lowerBound])
31+
32+
// Convert content data to string
33+
if let stringValue = String(data: part.content, encoding: .utf8) {
34+
fields[fieldName] = stringValue
35+
}
36+
}
37+
38+
return fields
39+
}
40+
}

0 commit comments

Comments
 (0)