ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Parsing multipart request from Chrome

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parsing multipart request from Chrome

    I'm in the process of revamping a 20-year-old web application. One of the pieces of the application that has not been used in a long time is a file uploader. The Java servlet that parses the multipart upload includes the following code, that worked with all browsers long ago and still works with Firefox:
    Code:
    String boundary = "-----------------------------";            
       ServletInputStream in = req.getInputStream();              
       byte[] bytes = new byte[512];                              
       int state = 0;                                            
       ByteArrayOutputStream buffer = new ByteArrayOutputStream();
       String name = null,                                        
              value = null,                                      
              filename = null,                                    
              contentType = null;                                
       Dictionary fields = new Hashtable();                      
                                                                  
      //Read a line from the request input stream                    
         int i = in.readLine(bytes,0,512);                            
         while (-1 != i) {                                            
          String st = new String(bytes,0,i);                          
                                                                      
       //Beginning of parameter - write buffer from previous parameter
           if (st.startsWith(boundary)) {
    This code does not work with Chrome, because Chrome starts its multipart/form-data boundary with "----WebKitFormBoundary" instead of "-----------------------------".

    So I changed the servlet as follows:
    Code:
    String boundary = "-----------------------------";            
    String boundary2 = "----WebKitFormBoundary";                  
       ServletInputStream in = req.getInputStream();              
       byte[] bytes = new byte[512];                              
       int state = 0;                                            
       ByteArrayOutputStream buffer = new ByteArrayOutputStream();
       String name = null,                                        
              value = null,                                      
              filename = null,                                    
              contentType = null;                                
       Dictionary fields = new Hashtable();                      
                                                                  
      //Read a line from the request input stream                    
         int i = in.readLine(bytes,0,512);                            
         while (-1 != i) {                                            
          String st = new String(bytes,0,i);                          
                                                                      
       //Beginning of parameter - write buffer from previous parameter
    //       if (st.startsWith(boundary)) {                          
           if (st.startsWith(boundary) || st.startsWith(boundary2)) {  ​
    The results have not changed.
    Does anyone see anything obvious that I'm doing wrong?

  • #2
    With a packet sniffer, I found that, despite what it claims in the request headers, Chrome is actually starting its boundaries with "------WebKitFormBoundary" (note the two extra dashes at the beginning). I adjusted my code accordingly, problem solved.

    Comment

    Working...
    X