Пример #1
0
 private CacheBuffer welcomPage(File dir, String[] welcomlist) {
   CacheBuffer asyncFile = null;
   for (String welcom : welcomlist) {
     asyncFile = CacheBuffer.open(new File(dir, welcom));
     FileInfo info = asyncFile.getFileInfo();
     if (info.exists() && info.canRead() && info.isFile()) {
       return asyncFile;
     }
     asyncFile.close();
   }
   return null;
 }
Пример #2
0
  private boolean response() {
    HeaderParser requestHeader = getRequestHeader();
    String ifModifiedSince = requestHeader.getHeader(HeaderParser.IF_MODIFIED_SINCE_HEADER);
    String selfPath = requestHeader.getRequestUri();

    MappingResult mapping = getRequestMapping();
    File file = (File) getRequestAttribute(ATTRIBUTE_RESPONSE_FILE);
    if (file != null) { // レスポンスするファイルが、直接指定された場合
      // FileCacheInfo fileCacheInfo=null;
      boolean useCache = true;
      if (getRequestAttribute(ATTRIBUTE_RESPONSE_FILE_NOT_USE_CACHE) == null) {
        useCache = false;
      }
      CacheBuffer asyncFile = CacheBuffer.open(file, useCache);
      FileInfo fileInfo = asyncFile.getFileInfo();
      if (!fileInfo.exists()) {
        logger.debug("Not found." + file.getAbsolutePath());
        completeResponse("404", "file not exists");
        asyncFile.close();
        return true;
      }
      return sendFile(mapping, null, null, ifModifiedSince, asyncFile);
    }

    String path = mapping.getResolvePath();
    try {
      path = URLDecoder.decode(path, "utf-8");
    } catch (UnsupportedEncodingException e) {
      logger.error("URLDecoder.decode error", e);
      throw new IllegalArgumentException("URLDecoder.decode error", e);
    }
    // クエリの削除
    int pos = path.indexOf('?');
    if (pos >= 0) {
      path = path.substring(0, pos);
    }

    File baseDirectory = mapping.getDestinationFile();
    CacheBuffer asyncFile = CacheBuffer.open(new File(baseDirectory, path));
    FileInfo info = asyncFile.getFileInfo();
    if (info.isError()) {
      logger.warn("fail to getCanonicalPath.");
      completeResponse("500", "fail to getCanonicalPath.");
      asyncFile.close();
      return true;
      // TODO トラバーサル
      // }else if(!info.isInBase()){
      // //トラバーサルされたら、loggingして404
      // logger.warn("traversal error.");
      // completeResponse("404","traversal error");
      // return true;
    } else if (!info.exists() || !info.canRead()) {
      asyncFile.close();
      logger.debug("Not found." + info.getCanonicalFile());
      completeResponse("404", "file not exists");
      return true;
    }
    // welcomefile処理
    String[] welcomeFiles = getWelcomeFiles(mapping);
    if (info.isDirectory() && welcomeFiles != null) {
      File dir = info.getCanonicalFile();
      asyncFile.close();
      asyncFile = welcomPage(dir, welcomeFiles);
      if (asyncFile == null) { // welcomfileが無かった
        //				completeResponse("404", "file not exists");
        return fileListIfNessesary(mapping, selfPath, dir, "/".equals(path));
      }
      info = asyncFile.getFileInfo();
      if (info.exists() && info.canRead() && !path.endsWith("/")) {
        asyncFile.close();
        // もし、URIが"/"で終わっていなかったら相対が解決できないので、リダイレクト
        ServerParser selfServer = requestHeader.getServer();
        StringBuilder sb = new StringBuilder();
        if (isSsl()) {
          sb.append("https://");
        } else {
          sb.append("http://");
        }
        sb.append(selfServer.toString());
        sb.append(selfPath);
        sb.append("/");
        setHeader(HeaderParser.LOCATION_HEADER, sb.toString());
        completeResponse("302");
        return true;
      }
    }
    if (info.isFile()) { // ファイルだったら
      return sendFile(mapping, baseDirectory, path, ifModifiedSince, asyncFile);
    }
    asyncFile.close();
    File dir = info.getCanonicalFile();
    return fileListIfNessesary(mapping, selfPath, dir, "/".equals(path));
  }