Esempio n. 1
0
 public void onRequestHeader(HeaderParser requestHeader) {
   /* portal機能の場合path情報にportal機能用の情報が着いている場合がある。
    * この情報は削除してproxy対象サーバにリクエスト
    */
   MappingResult mapping = proxyHandler.getRequestMapping();
   String path = mapping.getResolvePath();
   Matcher matcher = null;
   synchronized (portalPathInfoPattern) {
     matcher = portalPathInfoPattern.matcher(path);
   }
   StringBuffer sb = null;
   String portalPathInfo = null;
   if (matcher.find()) {
     sb = new StringBuffer();
     matcher.appendReplacement(sb, "");
     portalPathInfo = matcher.group(1);
     matcher.appendTail(sb);
     path = sb.toString();
     mapping.setResolvePath(path);
     requestHeader.setRequestUri(mapping.getResolvePath());
     proxyHandler.setRequestAttribute(PORTAL_PATHINFO_KEY, portalPathInfo);
   }
   /*
    * proxyでAuthrizationヘッダを付加する作戦の場合
   String basicAuthHeader = getBasicAuthHeader(mapping.isResolvedHttps(),mapping.getResolveServer());
   if (basicAuthHeader != null) {
   	requestHeader.addHeader(HeaderParser.WWW_AUTHRIZATION_HEADER, basicAuthHeader);
   	proxyHandler.setRequestAttribute(HeaderParser.WWW_AUTHRIZATION_HEADER, basicAuthHeader);
   }
    */
 }
Esempio n. 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));
  }