Generating multipart boundary

后端 未结 4 1562
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 13:00

I\'m writing a script that uploads a file to a cgi script that expects a multipart request, such as a form on a HTML page. The boundary is a unique token that a

相关标签:
4条回答
  • 2020-12-13 13:27

    If you use something random enough like a GUID there shouldn't be any need to hunt through the payload to check for an alias of the boundary. Something like:-

    ----=NextPart_3676416B-9AD6-440C-B3C8-FC66DDC7DB45
    Header:....

    Payload
    ----=NextPart_3676416B-9AD6-440C-B3C8-FC66DDC7DB45--

    0 讨论(0)
  • 2020-12-13 13:30

    And for the Swift people (to balance the Java):

    func createBoundaryString() -> String {
        var str = ""
        let length = arc4random_uniform(11) + 30
        let charSet = [Character]("-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    
        for _ in 0..<length {
            str.append(charSet[Int(arc4random_uniform(UInt32(charSet.count)))])
        }
        return str
    }
    
    0 讨论(0)
  • 2020-12-13 13:38

    For Java guys :

    protected String generateBoundary() {
                 StringBuilder buffer = new StringBuilder();
                 Random rand = new Random();
                 int count = rand.nextInt(11) + 30; // a random size from 30 to 40
                 for (int i = 0; i < count; i++) {
                 buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
                 }
                 return buffer.toString();
            }
    
    private final static char[] MULTIPART_CHARS =
                 "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                      .toCharArray();
    

    Reference url : http://hc.apache.org/httpcomponents-client-ga/httpmime/xref/org/apache/http/entity/mime/MultipartEntity.html

    0 讨论(0)
  • 2020-12-13 13:46

    If you are feeling paranoid, you can generate a random boundary and search for it in the string to be sent, append random char (or re-create new) on find, repeat. But my experience is any arbitrary non-dictionary string of 10 or so characters is about impossible to occur, so picking something like ---BOUNDARY---BOUNDARY---BOUNDARY--- is perfectly sufficient.

    0 讨论(0)
提交回复
热议问题