|
| 1 | +import Foundation |
| 2 | +import Hummingbird |
| 3 | +import NovaMLXCore |
| 4 | + |
| 5 | +struct MultipartPart { |
| 6 | + let name: String |
| 7 | + let filename: String? |
| 8 | + let contentType: String? |
| 9 | + let body: Data |
| 10 | +} |
| 11 | + |
| 12 | +struct MultipartParser { |
| 13 | + static func parse(body: Data, contentType: String) throws -> [String: MultipartPart] { |
| 14 | + guard let boundary = extractBoundary(from: contentType) else { |
| 15 | + throw NovaMLXError.apiError("Missing or invalid boundary in Content-Type header") |
| 16 | + } |
| 17 | + |
| 18 | + let boundaryData = Data("--\(boundary)".utf8) |
| 19 | + let delimiterData = Data("\r\n\r\n".utf8) |
| 20 | + let crlfData = Data("\r\n".utf8) |
| 21 | + |
| 22 | + var parts: [String: MultipartPart] = [:] |
| 23 | + var pos = 0 |
| 24 | + |
| 25 | + // Skip preamble until first boundary |
| 26 | + guard let firstBoundary = body.range(of: boundaryData, in: pos..<body.count) else { |
| 27 | + throw NovaMLXError.apiError("No boundary found in multipart body") |
| 28 | + } |
| 29 | + pos = firstBoundary.upperBound |
| 30 | + |
| 31 | + while pos < body.count { |
| 32 | + // Skip \r\n after boundary |
| 33 | + if pos + 2 <= body.count, body[pos..<(pos + 2)] == crlfData { |
| 34 | + pos += 2 |
| 35 | + } |
| 36 | + |
| 37 | + // Find end of headers |
| 38 | + guard let headerEnd = body.range(of: delimiterData, in: pos..<body.count) else { break } |
| 39 | + |
| 40 | + let headerData = body[pos..<headerEnd.lowerBound] |
| 41 | + let headers = parseHeaders(headerData) |
| 42 | + pos = headerEnd.upperBound |
| 43 | + |
| 44 | + // Find next boundary |
| 45 | + let nextBoundary = Data("\r\n--\(boundary)".utf8) |
| 46 | + guard let partEnd = body.range(of: nextBoundary, in: pos..<body.count) else { |
| 47 | + // Last part ends at closing boundary -- |
| 48 | + let closingBoundary = Data("--\r\n".utf8) |
| 49 | + if let closeEnd = body.range(of: closingBoundary, in: pos..<body.count) { |
| 50 | + let partBody = Data(body[pos..<closeEnd.lowerBound]) |
| 51 | + if let name = headers["name"] { |
| 52 | + parts[name] = MultipartPart( |
| 53 | + name: name, filename: headers["filename"], |
| 54 | + contentType: headers["content-type"], body: partBody |
| 55 | + ) |
| 56 | + } |
| 57 | + } |
| 58 | + break |
| 59 | + } |
| 60 | + |
| 61 | + let partBody = Data(body[pos..<partEnd.lowerBound]) |
| 62 | + if let name = headers["name"] { |
| 63 | + parts[name] = MultipartPart( |
| 64 | + name: name, filename: headers["filename"], |
| 65 | + contentType: headers["content-type"], body: partBody |
| 66 | + ) |
| 67 | + } |
| 68 | + pos = partEnd.upperBound |
| 69 | + } |
| 70 | + |
| 71 | + return parts |
| 72 | + } |
| 73 | + |
| 74 | + static func extractBoundary(from contentType: String) -> String? { |
| 75 | + // Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryXYZ |
| 76 | + let lower = contentType.lowercased() |
| 77 | + guard lower.contains("multipart/form-data") else { return nil } |
| 78 | + |
| 79 | + for component in contentType.split(separator: ";") { |
| 80 | + let trimmed = component.trimmingCharacters(in: .whitespaces) |
| 81 | + if trimmed.hasPrefix("boundary=") { |
| 82 | + let boundary = String(trimmed.dropFirst("boundary=".count)) |
| 83 | + .trimmingCharacters(in: CharacterSet(charactersIn: "\"")) |
| 84 | + return boundary.isEmpty ? nil : boundary |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + private static func parseHeaders(_ data: Data) -> [String: String] { |
| 91 | + guard let headerStr = String(data: data, encoding: .utf8) else { return [:] } |
| 92 | + var result: [String: String] = [:] |
| 93 | + |
| 94 | + for line in headerStr.components(separatedBy: "\r\n") { |
| 95 | + if line.hasPrefix("Content-Disposition:") { |
| 96 | + // Extract name and filename from Content-Disposition |
| 97 | + for pair in line.split(separator: ";") { |
| 98 | + let trimmed = pair.trimmingCharacters(in: .whitespaces) |
| 99 | + if let eq = trimmed.firstIndex(of: "=") { |
| 100 | + let key = String(trimmed[..<eq]).trimmingCharacters(in: .whitespaces) |
| 101 | + let val = String(trimmed[trimmed.index(after: eq)...]) |
| 102 | + .trimmingCharacters(in: CharacterSet(charactersIn: " \"")) |
| 103 | + if key == "name" || key == "filename" { |
| 104 | + result[key] = val |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } else if line.hasPrefix("Content-Type:") { |
| 109 | + let val = String(line.dropFirst("Content-Type:".count)) |
| 110 | + .trimmingCharacters(in: .whitespaces) |
| 111 | + result["content-type"] = val |
| 112 | + } |
| 113 | + } |
| 114 | + return result |
| 115 | + } |
| 116 | +} |
0 commit comments