Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ardingchen opened this issue Nov 2, 2016 · 0 comments

Comments

@ardingchen
Copy link
Contributor

ardingchen commented Nov 2, 2016

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant