'Is there a way to stop an HTTP POST request before my server downloads the file in the post?

I want to be able to stop malicious, or time consuming, downloads on my server.

Part of my Swift app builds an HTTP body to upload a file, as seen below. The body also has parameters regarding the user. Whatever params I need are included in the body.

I realize there is no perfect stop-gap, but I'd love to prevent someone from sending over a file that is stupidly big. But to the best of my understating, the Perl script, activated by the POST, is only run after the entire file/POST command has uploaded.

While I place file size limits in my Swift app itself, should someone find the script command they could just waste my server time.

So is there anything I can put in the POST body that does a confirmation before the entire file is uploaded?

func createDataBody(withParameters params: Parameters?, xFile: XFile?, boundary: String) -> Data {
    
    let lineBreak = "\r\n"
    var body = Data()
    
    if let parameters = params {
        for (key, value) in parameters {
            body.append("--\(boundary + lineBreak)")
            body.append("Content-Disposition: form-data; name=\"\(key)\"\(lineBreak + lineBreak)")
            body.append("\(value + lineBreak)")
        }
    }
    
    if let xFile = xFile {
            body.append("--\(boundary + lineBreak)")
            body.append("Content-Disposition: form-data; name=\"\(xFile.key)\"; filename=\"\(xFile.filename)\"\(lineBreak)")
        body.append("Content-Type: \(xFile.mimeType! + lineBreak + lineBreak)")
        body.append(xFile.data)
            body.append(lineBreak)
    }
    
    body.append("--\(boundary)--\(lineBreak)")
    
    return body
}
    
extension Data {
    mutating func append(_ string: String) {
        if let data = string.data(using: .utf8) {
            append(data)
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source