deda-ca / cgi-node

CGI Node.js complete module to replace PHP on shared hosting such as GoDaddy
http://www.cgi-node.org/
MIT License
172 stars 21 forks source link

Multipart post #36

Closed neimanpinchas closed 7 years ago

neimanpinchas commented 7 years ago

I am using CGI-node a lot, the only downside was that the multi-part parser is not done I was supposed to be able to upload files, i looked around for a synchronous multi-part parser but did'nt found one, so i wrote it by my own. The only problem is that binary files are not parsed well. Looking in a hex editor i realized that some characters are expanded eg FF FF FF FF = EF BF BD EF BF BD EF BF BD EF BF BD.

Q1: is there another way to make file uploads?

Q2: can say me whats wrong with the code?

    // Traverse the parts and parse them as if they where a single HTTP header and body.
    for (var index = 0; index < post.parts.length; index++) {
        var partLines = post.parts[index].split('\n')
            var partSpec = partLines[1]
            var keys = (partSpec || '').split(";"),
        keysObj = {}
        log += require('util').format(partLines[1]) + '<br />'

        for (i in keys)
            keysObj[keys[i].split("=")[0].replace(/\s/g, "")] = (keys[i].split("=")[1] || "").replace(/\s/g, "")
            delete partLines[1]
            if (keys[0] == 'Content-Disposition: form-data') {

                if (keysObj.filename) {
                    delete partLines[2]
                    post.form[keysObj.name] = keysObj.filename.replace(/^\"|\"$/g, "")
                        var tmpName = 'tmp/' + new Date().getTime(),
                    data = partLines.join('\n').slice(6).slice(0, -2)
                        if (data.length > 0) {
                            fs.writeFileSync(tmpName, data)
                            post.files[keysObj.name] = {
                                tmpFile: tmpName,
                                size: data.length
                            }
                        }

                } else {
                    post.form[keysObj.name] = partLines.join('\n')
                }
            }
    }
neimanpinchas commented 7 years ago

I just did it by forcing encoding incoming STDIN to binary and then save as a file from a buffer