Program: 29855
Project: 29855
Buggy source:
/** 
 * Serve the specified resource, optionally including the data content.
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param content Should the content be included?
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet-specified error occurs
 */
protected void serveResource(HttpServletRequest request,HttpServletResponse response,boolean content) throws IOException, ServletException {
  String path=getRelativePath(request);
  if (debug > 0) {
    if (content)     log("String_Node_Str" + path + "String_Node_Str");
 else     log("String_Node_Str" + path + "String_Node_Str");
  }
  CacheEntry cacheEntry=null;
  ProxyDirContext proxyDirContext=resources;
  if (alternateDocBases == null || alternateDocBases.size() == 0) {
    cacheEntry=proxyDirContext.lookupCache(path);
  }
 else {
    AlternateDocBase match=AlternateDocBase.findMatch(path,alternateDocBases);
    if (match != null) {
      cacheEntry=((ProxyDirContext)match.getResources()).lookupCache(path);
    }
 else {
      cacheEntry=proxyDirContext.lookupCache(path);
    }
  }
  if (!cacheEntry.exists) {
    ClassLoader cl=Thread.currentThread().getContextClassLoader();
    String metaInfResPath=Globals.META_INF_RESOURCES + path;
    if (cl instanceof WebappClassLoader) {
      WebappClassLoader wcl=(WebappClassLoader)cl;
      final URL resourceUrl=wcl.getResourceFromJars(metaInfResPath);
      if (resourceUrl != null) {
        ConcurrentHashMap<String,ResourceEntry> resourceEntries=wcl.getResourceEntries();
        ResourceEntry resourceEntry=resourceEntries.get(metaInfResPath);
        if (resourceEntry != null) {
          cacheEntry=new CacheEntry();
          try {
            URI resourceUri=resourceUrl.toURI();
            if ("String_Node_Str".equals(resourceUri.getScheme()) && (new File(resourceUri)).isDirectory()) {
              if (!path.endsWith("String_Node_Str")) {
                response.sendRedirect(response.encodeRedirectUrl(request.getContextPath() + path + "String_Node_Str"));
                return;
              }
              FileDirContext fileDirContext=new FileDirContext();
              String base=resourceUrl.getPath();
              int index=base.lastIndexOf(path);
              if (index != -1) {
                base=base.substring(0,index);
              }
              fileDirContext.setDocBase(base);
              proxyDirContext=new ProxyDirContext(new Hashtable<String,String>(),fileDirContext);
              cacheEntry.context=proxyDirContext;
            }
 else {
              cacheEntry.resource=new Resource(){
                public InputStream streamContent() throws IOException {
                  return resourceUrl.openStream();
                }
              }
;
            }
          }
 catch (          Exception ex) {
            throw new IOException(ex);
          }
          cacheEntry.name=path;
          cacheEntry.attributes=new ResourceAttributes();
          cacheEntry.exists=true;
        }
      }
    }
  }
  if (!cacheEntry.exists) {
    String requestUri=(String)request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
    if (requestUri != null) {
      throw new FileNotFoundException(requestUri);
    }
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return;
  }
  if (cacheEntry.context == null) {
    if (path.endsWith("String_Node_Str") || (path.endsWith("String_Node_Str"))) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
  }
  if (cacheEntry.context == null) {
    boolean included=(request.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH) != null);
    if (!included && !checkIfHeaders(request,response,cacheEntry.attributes)) {
      return;
    }
  }
  String contentType=cacheEntry.attributes.getMimeType();
  if (contentType == null) {
    contentType=getServletContext().getMimeType(cacheEntry.name);
    cacheEntry.attributes.setMimeType(contentType);
  }
  ArrayList<Range> ranges=null;
  long contentLength=-1L;
  if (cacheEntry.context != null) {
    if (!listings) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
    contentType="String_Node_Str";
  }
 else {
    if (useAcceptRanges) {
      response.setHeader("String_Node_Str","String_Node_Str");
    }
    ranges=parseRange(request,response,cacheEntry.attributes);
    response.setHeader("String_Node_Str",cacheEntry.attributes.getETag());
    response.setHeader("String_Node_Str",cacheEntry.attributes.getLastModifiedHttp());
    contentLength=cacheEntry.attributes.getContentLength();
    if (contentLength == 0L) {
      content=false;
    }
  }
  ServletOutputStream ostream=null;
  PrintWriter writer=null;
  if (content) {
    try {
      ostream=response.getOutputStream();
    }
 catch (    IllegalStateException e) {
      if ((contentType == null) || (contentType.startsWith("String_Node_Str")) || (contentType.startsWith("String_Node_Str"))) {
        writer=response.getWriter();
      }
 else {
        throw e;
      }
    }
  }
  if ((cacheEntry.context != null) || (((ranges == null) || (ranges.isEmpty())) && (request.getHeader("String_Node_Str") == null)) || (ranges == FULL)) {
    if (contentType != null) {
      if (debug > 0)       log("String_Node_Str" + contentType + "String_Node_Str");
      response.setContentType(contentType);
    }
    if ((cacheEntry.resource != null) && (contentLength >= 0)) {
      if (debug > 0)       log("String_Node_Str" + contentLength);
      if (contentLength < Integer.MAX_VALUE) {
        response.setContentLength((int)contentLength);
      }
 else {
        response.setHeader("String_Node_Str","String_Node_Str" + contentLength);
      }
    }
    InputStream renderResult=null;
    if (cacheEntry.context != null) {
      if (content) {
        renderResult=render(request.getContextPath(),cacheEntry,proxyDirContext);
      }
    }
    if (content) {
      try {
        response.setBufferSize(output);
      }
 catch (      IllegalStateException e) {
      }
      if (ostream != null) {
        if (!checkSendfile(request,response,cacheEntry,contentLength,null))         copy(cacheEntry,renderResult,ostream);
      }
 else {
        copy(cacheEntry,renderResult,writer);
      }
    }
  }
 else {
    if ((ranges == null) || (ranges.isEmpty()))     return;
    if (maxHeaderRangeItems >= 0 && ranges.size() > maxHeaderRangeItems) {
      response.sendError(HttpServletResponse.SC_FORBIDDEN);
      return;
    }
    response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
    if (ranges.size() == 1) {
      Range range=ranges.get(0);
      response.addHeader("String_Node_Str","String_Node_Str" + range.start + "String_Node_Str"+ range.end+ "String_Node_Str"+ range.length);
      long length=range.end - range.start + 1;
      if (length < Integer.MAX_VALUE) {
        response.setContentLength((int)length);
      }
 else {
        response.setHeader("String_Node_Str","String_Node_Str" + length);
      }
      if (contentType != null) {
        if (debug > 0)         log("String_Node_Str" + contentType + "String_Node_Str");
        response.setContentType(contentType);
      }
      if (content) {
        try {
          response.setBufferSize(output);
        }
 catch (        IllegalStateException e) {
        }
        if (ostream != null) {
          if (!checkSendfile(request,response,cacheEntry,range.end - range.start + 1,range))           copy(cacheEntry,ostream,range);
        }
 else {
          copy(cacheEntry,writer,range);
        }
      }
    }
 else {
      response.setContentType("String_Node_Str" + mimeSeparation);
      if (content) {
        try {
          response.setBufferSize(output);
        }
 catch (        IllegalStateException e) {
        }
        if (ostream != null) {
          copy(cacheEntry,ostream,ranges.iterator(),contentType);
        }
 else {
          copy(cacheEntry,writer,ranges.iterator(),contentType);
        }
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Serve the specified resource, optionally including the data content.
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param content Should the content be included?
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet-specified error occurs
 */
protected void serveResource(HttpServletRequest request,HttpServletResponse response,boolean content) throws IOException, ServletException {
  String path=getRelativePath(request);
  if (debug > 0) {
    if (content)     log("String_Node_Str" + path + "String_Node_Str");
 else     log("String_Node_Str" + path + "String_Node_Str");
  }
  CacheEntry cacheEntry=null;
  ProxyDirContext proxyDirContext=resources;
  if (alternateDocBases == null || alternateDocBases.size() == 0) {
    cacheEntry=proxyDirContext.lookupCache(path);
  }
 else {
    AlternateDocBase match=AlternateDocBase.findMatch(path,alternateDocBases);
    if (match != null) {
      cacheEntry=((ProxyDirContext)match.getResources()).lookupCache(path);
    }
 else {
      cacheEntry=proxyDirContext.lookupCache(path);
    }
  }
  if (!cacheEntry.exists) {
    ClassLoader cl=Thread.currentThread().getContextClassLoader();
    String metaInfResPath=Globals.META_INF_RESOURCES + path;
    if (cl instanceof WebappClassLoader) {
      WebappClassLoader wcl=(WebappClassLoader)cl;
      final URL resourceUrl=wcl.getResourceFromJars(metaInfResPath);
      if (resourceUrl != null) {
        ConcurrentHashMap<String,ResourceEntry> resourceEntries=wcl.getResourceEntries();
        ResourceEntry resourceEntry=resourceEntries.get(metaInfResPath);
        if (resourceEntry != null) {
          cacheEntry=new CacheEntry();
          try {
            URI resourceUri=resourceUrl.toURI();
            if ("String_Node_Str".equals(resourceUri.getScheme()) && (new File(resourceUri)).isDirectory()) {
              if (!path.endsWith("String_Node_Str")) {
                response.sendRedirect(response.encodeRedirectUrl(request.getContextPath() + path + "String_Node_Str"));
                return;
              }
              FileDirContext fileDirContext=new FileDirContext();
              String base=resourceUrl.getPath();
              int index=base.lastIndexOf(path);
              if (index != -1) {
                base=base.substring(0,index);
              }
              fileDirContext.setDocBase(base);
              proxyDirContext=new ProxyDirContext(new Hashtable<String,String>(),fileDirContext);
              cacheEntry.context=proxyDirContext;
            }
 else {
              cacheEntry.resource=new Resource(){
                public InputStream streamContent() throws IOException {
                  return resourceUrl.openStream();
                }
              }
;
            }
          }
 catch (          Exception ex) {
            throw new IOException(ex);
          }
          cacheEntry.name=path;
          cacheEntry.attributes=new ResourceAttributes();
          cacheEntry.exists=true;
        }
      }
    }
  }
  if (!cacheEntry.exists) {
    String requestUri=(String)request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
    if (requestUri != null) {
      throw new FileNotFoundException(requestUri);
    }
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return;
  }
  if (cacheEntry.context == null) {
    if (path.endsWith("String_Node_Str") || (path.endsWith("String_Node_Str"))) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
  }
  if (cacheEntry.context == null) {
    boolean included=(request.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH) != null);
    if (!included && !checkIfHeaders(request,response,cacheEntry.attributes)) {
      return;
    }
  }
  String contentType=cacheEntry.attributes.getMimeType();
  if (contentType == null && !cacheEntry.attributes.isMimeTypeInitialized()) {
    contentType=getServletContext().getMimeType(cacheEntry.name);
    cacheEntry.attributes.setMimeType(contentType);
  }
  ArrayList<Range> ranges=null;
  long contentLength=-1L;
  if (cacheEntry.context != null) {
    if (!listings) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
    contentType="String_Node_Str";
  }
 else {
    if (useAcceptRanges) {
      response.setHeader("String_Node_Str","String_Node_Str");
    }
    ranges=parseRange(request,response,cacheEntry.attributes);
    response.setHeader("String_Node_Str",cacheEntry.attributes.getETag());
    response.setHeader("String_Node_Str",cacheEntry.attributes.getLastModifiedHttp());
    contentLength=cacheEntry.attributes.getContentLength();
    if (contentLength == 0L) {
      content=false;
    }
  }
  ServletOutputStream ostream=null;
  PrintWriter writer=null;
  if (content) {
    try {
      ostream=response.getOutputStream();
    }
 catch (    IllegalStateException e) {
      if ((contentType == null) || (contentType.startsWith("String_Node_Str")) || (contentType.startsWith("String_Node_Str"))) {
        writer=response.getWriter();
      }
 else {
        throw e;
      }
    }
  }
  if ((cacheEntry.context != null) || (((ranges == null) || (ranges.isEmpty())) && (request.getHeader("String_Node_Str") == null)) || (ranges == FULL)) {
    if (contentType != null) {
      if (debug > 0)       log("String_Node_Str" + contentType + "String_Node_Str");
      response.setContentType(contentType);
    }
    if ((cacheEntry.resource != null) && (contentLength >= 0)) {
      if (debug > 0)       log("String_Node_Str" + contentLength);
      if (contentLength < Integer.MAX_VALUE) {
        response.setContentLength((int)contentLength);
      }
 else {
        response.setHeader("String_Node_Str","String_Node_Str" + contentLength);
      }
    }
    InputStream renderResult=null;
    if (cacheEntry.context != null) {
      if (content) {
        renderResult=render(request.getContextPath(),cacheEntry,proxyDirContext);
      }
    }
    if (content) {
      try {
        response.setBufferSize(output);
      }
 catch (      IllegalStateException e) {
      }
      if (ostream != null) {
        if (!checkSendfile(request,response,cacheEntry,contentLength,null))         copy(cacheEntry,renderResult,ostream);
      }
 else {
        copy(cacheEntry,renderResult,writer);
      }
    }
  }
 else {
    if ((ranges == null) || (ranges.isEmpty()))     return;
    if (maxHeaderRangeItems >= 0 && ranges.size() > maxHeaderRangeItems) {
      response.sendError(HttpServletResponse.SC_FORBIDDEN);
      return;
    }
    response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
    if (ranges.size() == 1) {
      Range range=ranges.get(0);
      response.addHeader("String_Node_Str","String_Node_Str" + range.start + "String_Node_Str"+ range.end+ "String_Node_Str"+ range.length);
      long length=range.end - range.start + 1;
      if (length < Integer.MAX_VALUE) {
        response.setContentLength((int)length);
      }
 else {
        response.setHeader("String_Node_Str","String_Node_Str" + length);
      }
      if (contentType != null) {
        if (debug > 0)         log("String_Node_Str" + contentType + "String_Node_Str");
        response.setContentType(contentType);
      }
      if (content) {
        try {
          response.setBufferSize(output);
        }
 catch (        IllegalStateException e) {
        }
        if (ostream != null) {
          if (!checkSendfile(request,response,cacheEntry,range.end - range.start + 1,range))           copy(cacheEntry,ostream,range);
        }
 else {
          copy(cacheEntry,writer,range);
        }
      }
    }
 else {
      response.setContentType("String_Node_Str" + mimeSeparation);
      if (content) {
        try {
          response.setBufferSize(output);
        }
 catch (        IllegalStateException e) {
        }
        if (ostream != null) {
          copy(cacheEntry,ostream,ranges.iterator(),contentType);
        }
 else {
          copy(cacheEntry,writer,ranges.iterator(),contentType);
        }
      }
    }
  }
}