Program: 60297
Project: 60297
Buggy source:
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  if (!StringUtil.isEmpty(password)) {
    String p=request.getParameter("String_Node_Str");
    if (StringUtil.isEmpty(p) || !p.equals(password)) {
      return;
    }
  }
  String uri=request.getRequestURI();
  uri=uri.replaceAll("String_Node_Str",SLASH);
  String servlet=request.getServletPath();
  int start=uri.indexOf(servlet);
  uri=uri.substring(start + servlet.length());
  if (uri.length() < 2) {
    return;
  }
  int last=uri.lastIndexOf(SLASH);
  String filename=uri.substring(last + 1);
  DBObject query=new BasicDBObject(BuguFS.FILENAME,filename);
  query.put(ImageUploader.DIMENSION,null);
  String bucket=GridFS.DEFAULT_BUCKET;
  int first=uri.indexOf(SLASH);
  if (first != last) {
    String sub=uri.substring(first + 1,last);
    String[] arr=sub.split(SLASH);
    for (int i=0; i < arr.length; i+=2) {
      if (arr[i].equals(BuguFS.BUCKET)) {
        bucket=arr[i + 1];
      }
 else {
        query.put(arr[i],arr[i + 1]);
      }
    }
  }
  if (!StringUtil.isEmpty(allowBucket) && !allowBucket.equalsIgnoreCase(bucket)) {
    return;
  }
  if (!StringUtil.isEmpty(forbidBucket) && forbidBucket.equalsIgnoreCase(bucket)) {
    return;
  }
  BuguFS fs=BuguFSFactory.getInstance().create(bucket);
  GridFSDBFile f=fs.findOne(query);
  if (f == null) {
    return;
  }
  InputStream is=f.getInputStream();
  OutputStream os=response.getOutputStream();
  try {
    int fileLength=(int)f.getLength();
    String ext=StringUtil.getExtention(filename);
    response.setContentType(getContentType(ext));
    String range=request.getHeader("String_Node_Str");
    if (StringUtil.isEmpty(range)) {
      response.setStatus(HttpServletResponse.SC_OK);
      response.setContentLength(fileLength);
      if (contentMD5) {
        response.setHeader("String_Node_Str",f.getMD5());
      }
      if (needCache(ext)) {
        String modifiedSince=request.getHeader("String_Node_Str");
        DateFormat df=new SimpleDateFormat("String_Node_Str",Locale.ENGLISH);
        df.setTimeZone(TimeZone.getTimeZone("String_Node_Str"));
        Date uploadDate=f.getUploadDate();
        String lastModified=df.format(uploadDate);
        if (modifiedSince != null) {
          Date modifiedDate=null;
          Date sinceDate=null;
          try {
            modifiedDate=df.parse(lastModified);
            sinceDate=df.parse(modifiedSince);
          }
 catch (          ParseException ex) {
            logger.error("String_Node_Str",ex);
          }
          if (modifiedDate != null && sinceDate != null && modifiedDate.compareTo(sinceDate) <= 0) {
            response.setStatus(304);
            return;
          }
        }
        response.setHeader("String_Node_Str","String_Node_Str" + ONE_YEAR_SECONDS);
        response.setHeader("String_Node_Str",lastModified);
        response.setDateHeader("String_Node_Str",uploadDate.getTime() + ONE_YEAR_MILLISECONDS);
      }
 else {
        response.setHeader("String_Node_Str","String_Node_Str");
        response.setHeader("String_Node_Str","String_Node_Str");
        response.setDateHeader("String_Node_Str",0);
      }
      f.writeTo(os);
    }
 else {
      range=range.substring("String_Node_Str".length());
      if (StringUtil.isEmpty(range)) {
        return;
      }
      int begin=0;
      int end=fileLength - 1;
      boolean onlyLast=range.startsWith("String_Node_Str");
      String[] rangeArray=range.split("String_Node_Str");
      if (rangeArray.length == 1) {
        if (onlyLast) {
          begin=fileLength - Integer.parseInt(rangeArray[0]);
        }
 else {
          begin=Integer.parseInt(rangeArray[0]);
        }
      }
 else       if (rangeArray.length == 2) {
        begin=Integer.parseInt(rangeArray[0]);
        end=Integer.parseInt(rangeArray[1]);
      }
      response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
      int contentLength=end - begin + 1;
      response.setContentLength(contentLength);
      response.setHeader("String_Node_Str","String_Node_Str" + begin + "String_Node_Str"+ end+ "String_Node_Str"+ contentLength);
      is.skip(begin);
      int read=-1;
      int bufferSize=(int)f.getChunkSize();
      byte[] buffer=new byte[bufferSize];
      int remain=contentLength;
      int readSize=Math.min(bufferSize,remain);
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      while ((read=is.read(buffer,0,readSize)) != -1) {
        baos.write(buffer,0,read);
        remain-=read;
        if (remain <= 0) {
          break;
        }
        readSize=Math.min(bufferSize,remain);
      }
      byte[] bytes=baos.toByteArray();
      if (contentMD5) {
        String md5=StringUtil.encodeMD5(bytes);
        response.setHeader("String_Node_Str",md5);
      }
      os.write(bytes);
      os.flush();
    }
  }
  finally {
    StreamUtil.safeClose(is);
    StreamUtil.safeClose(os);
  }
}

Fixed version (ground truth):
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  if (!StringUtil.isEmpty(password)) {
    String p=request.getParameter("String_Node_Str");
    if (StringUtil.isEmpty(p) || !p.equals(password)) {
      return;
    }
  }
  String uri=request.getRequestURI();
  uri=uri.replaceAll("String_Node_Str",SLASH);
  String servlet=request.getServletPath();
  int start=uri.indexOf(servlet);
  uri=uri.substring(start + servlet.length());
  if (uri.length() < 2) {
    return;
  }
  int last=uri.lastIndexOf(SLASH);
  String filename=uri.substring(last + 1);
  DBObject query=new BasicDBObject(BuguFS.FILENAME,filename);
  query.put(ImageUploader.DIMENSION,null);
  String bucket=GridFS.DEFAULT_BUCKET;
  int first=uri.indexOf(SLASH);
  if (first != last) {
    String sub=uri.substring(first + 1,last);
    String[] arr=sub.split(SLASH);
    for (int i=0; i < arr.length; i+=2) {
      if (arr[i].equals(BuguFS.BUCKET)) {
        bucket=arr[i + 1];
      }
 else {
        query.put(arr[i],arr[i + 1]);
      }
    }
  }
  if (!StringUtil.isEmpty(allowBucket) && !allowBucket.equalsIgnoreCase(bucket)) {
    return;
  }
  if (!StringUtil.isEmpty(forbidBucket) && forbidBucket.equalsIgnoreCase(bucket)) {
    return;
  }
  BuguFS fs=BuguFSFactory.getInstance().create(bucket);
  GridFSDBFile f=fs.findOne(query);
  if (f == null) {
    return;
  }
  InputStream is=f.getInputStream();
  OutputStream os=response.getOutputStream();
  try {
    int fileLength=(int)f.getLength();
    String ext=StringUtil.getExtention(filename);
    response.setContentType(getContentType(ext));
    String range=request.getHeader("String_Node_Str");
    if (StringUtil.isEmpty(range)) {
      response.setStatus(HttpServletResponse.SC_OK);
      response.setContentLength(fileLength);
      if (contentMD5) {
        response.setHeader("String_Node_Str",f.getMD5());
      }
      if (needCache(ext)) {
        String modifiedSince=request.getHeader("String_Node_Str");
        DateFormat df=new SimpleDateFormat("String_Node_Str",Locale.ENGLISH);
        df.setTimeZone(TimeZone.getTimeZone("String_Node_Str"));
        Date uploadDate=f.getUploadDate();
        String lastModified=df.format(uploadDate);
        if (modifiedSince != null) {
          Date modifiedDate=null;
          Date sinceDate=null;
          try {
            modifiedDate=df.parse(lastModified);
            sinceDate=df.parse(modifiedSince);
          }
 catch (          ParseException ex) {
            logger.error("String_Node_Str",ex);
          }
          if (modifiedDate != null && sinceDate != null && modifiedDate.compareTo(sinceDate) <= 0) {
            response.setStatus(304);
            return;
          }
        }
        response.setHeader("String_Node_Str","String_Node_Str" + ONE_YEAR_SECONDS);
        response.setHeader("String_Node_Str",lastModified);
        response.setDateHeader("String_Node_Str",uploadDate.getTime() + ONE_YEAR_MILLISECONDS);
      }
 else {
        response.setHeader("String_Node_Str","String_Node_Str");
        response.setHeader("String_Node_Str","String_Node_Str");
        response.setDateHeader("String_Node_Str",0);
      }
      f.writeTo(os);
    }
 else {
      range=range.substring("String_Node_Str".length());
      if (StringUtil.isEmpty(range)) {
        return;
      }
      int begin=0;
      int end=fileLength - 1;
      boolean onlyLast=range.startsWith("String_Node_Str");
      String[] rangeArray=range.split("String_Node_Str");
      if (rangeArray.length == 1) {
        if (onlyLast) {
          begin=fileLength - Integer.parseInt(rangeArray[0]);
        }
 else {
          begin=Integer.parseInt(rangeArray[0]);
        }
      }
 else       if (rangeArray.length == 2) {
        begin=Integer.parseInt(rangeArray[0]);
        end=Integer.parseInt(rangeArray[1]);
      }
      response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
      int contentLength=end - begin + 1;
      response.setContentLength(contentLength);
      response.setHeader("String_Node_Str","String_Node_Str" + begin + "String_Node_Str"+ end+ "String_Node_Str"+ fileLength);
      is.skip(begin);
      int read=-1;
      int bufferSize=(int)f.getChunkSize();
      byte[] buffer=new byte[bufferSize];
      int remain=contentLength;
      int readSize=Math.min(bufferSize,remain);
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      while ((read=is.read(buffer,0,readSize)) != -1) {
        baos.write(buffer,0,read);
        remain-=read;
        if (remain <= 0) {
          break;
        }
        readSize=Math.min(bufferSize,remain);
      }
      byte[] bytes=baos.toByteArray();
      if (contentMD5) {
        String md5=StringUtil.encodeMD5(bytes);
        if (!StringUtil.isEmpty(md5)) {
          response.setHeader("String_Node_Str",md5.toLowerCase());
        }
      }
      os.write(bytes);
      os.flush();
    }
  }
  finally {
    StreamUtil.safeClose(is);
    StreamUtil.safeClose(os);
  }
}