Skip to content

TypeError: Cannot read property 'split' of undefined at _multipartparser #24

Description

@ardingchen

There are some errors when handling invalid multipart-data:
TypeError: Cannot read property 'split' of undefined
at _multipartparser (./node_modules/node-simple-router/lib/router.js:354:47)
at IncomingMessage. (./node_modules/node-simple-router/lib/router.js:137:26
......

The code at that positon is:

  boundary = content_type.split(/;\s+/)[1].split('=')[1].trim(); 

May I modify this code to(javascript):

    _multipartparser = function(body, content_type) {
      var boundary, i, len1, m, obj, part, parts, resp;
      resp = {
        "multipart-data": []
      };
      // boundary = content_type.split(/;\s+/)[1].split('=')[1].trim();  // -- the old code
      boundary = content_type.split(/;\s+/)[1];
      if (boundary) {
        boundary = boundary.split('=')[1];
        if (boundary) {
          boundary = boundary.trim();
          parts = body.split("--" + boundary);
          for (i = 0, len1 = parts.length; i < len1; i++) {
            part = parts[i];
            if (part && part.match(/Content-Disposition:/i)) {
              obj = {};
              m = part.match(/Content-Disposition:\s+(.+?);/i);
              if (m) {
                obj.contentDisposition = m[1];
              }
              m = part.match(/name="(.+?)"/i);
              if (m) {
                obj.fieldName = m[1];
              }
              m = part.match(/filename="(.+?)"/i);
              if (m) {
                obj.fileName = m[1];
              }
              m = part.match(/Content-Type:\s+(.+?)\s/i);
              if (m) {
                obj.fileType = m[1];
              } else {
                obj.fileType = 'text/plain';
              }
              m = part.match(/Content-Length:\s+(\d+?)/i);
              if (m) {
                obj.contentLength = m[1];
              }
              m = part.match(/\r\n\r\n/);
              if (m) {
                obj.fileData = part.slice(m.index + 4, -2);
                obj.fileLen = obj.fileData.length;
              }
              resp['multipart-data'].push(obj);
            }   
          }
        }
      } 
      return resp;
    };

and the coffee script is:

_multipartparser = (body, content_type) ->
    resp = "multipart-data": []
    # boundary = content_type.split(/;\s+/)[1].split('=')[1].trim()   #-- the old code
    boundary = content_type.split(/;\s+/)[1]
    if boundary
      boundary = boundary.split('=')[1]
      if boundary
        boundary = boundary.trim()
        parts = body.split("--#{boundary}")
        for part in parts
          if part and part.match(/Content-Disposition:/i)
            #dispatch.log "PART: #{part}"
            obj = {}
            m = part.match(/Content-Disposition:\s+(.+?);/i)
            if m
              obj.contentDisposition = m[1]
            m = part.match(/name="(.+?)"/i)
            if m
              obj.fieldName = m[1]
            m = part.match(/filename="(.+?)"/i)
            if m
              obj.fileName = m[1]
            m = part.match(/Content-Type:\s+(.+?)\s/i)
            if m
              obj.fileType = m[1]
            else
              obj.fileType = 'text/plain'
            m = part.match(/Content-Length:\s+(\d+?)/i)
            if m
              obj.contentLength = m[1]
    
            m = part.match /\r\n\r\n/
            if m
              obj.fileData = part.slice(m.index + 4, -2)
              obj.fileLen = obj.fileData.length
            
            resp['multipart-data'].push obj
    resp

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions