Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@
## 2025-01-24 - 단일 readAttributes 호출로 파일 속성 조회 최적화
**학습:** `isDirectory`, `!it.isDirectory()`, `isSymbolicLink` 3개의 개별적인 파일 시스템 I/O 호출을 수행하면 성능 저하가 큽니다. 이를 단일 `Files.readAttributes` 호출로 변경하여 메타데이터를 한 번에 조회함으로써 I/O 오버헤드를 대폭 줄일 수 있음을 확인했습니다.
**조치:** 디렉토리 순회 시 파일의 여러 속성을 확인할 때는 개별적인 stat 호출보다 `Files.readAttributes`를 사용하여 필요한 모든 속성을 한 번에 가져오는 방식을 우선적으로 고려해야 합니다.

## 2026-07-30 - 루프 내 정적 문자열 및 해시 계산 추출
**Learning:** process_dir과 같이 루프 내에서 반복적으로 호출되는 함수에서, 큰 정적 문자열이나 SHA-256 해싱과 같은 계산을 매번 할당하고 실행하는 것은 메모리와 CPU 자원을 낭비합니다. 특히 디렉토리가 많은 환경에서는 이 오버헤드가 누적됩니다.
**Action:** 이런 정적인 데이터와 결정론적 계산은 private object로 추출하여 프로그램 실행 시 한 번만 초기화되도록 하여 성능을 최적화합니다.
37 changes: 20 additions & 17 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,8 @@ fun write_index_file(curr_dir: File, content: String) {
}
}

fun process_dir(curr_dir: File, excludeSet: Set<String>? = null, dirFiles: Array<File>? = null){

val exclude: Set<String> = excludeSet ?: process_ignore_file(curr_dir)

val cssContent = """
private object ProcessDirConstants {
const val cssContent = """
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
line-height: 1.5;
Expand Down Expand Up @@ -313,25 +310,39 @@ fun process_dir(curr_dir: File, excludeSet: Set<String>? = null, dirFiles: Array
}
"""

@JvmField
val styleHash = "sha256-" + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(cssContent.toByteArray(Charsets.UTF_8)))

val css = """
const val css = """
<style>
${cssContent} </style>
"""

const val index_bottom = """
</ul>
</nav>
</main>
</body>
</html>
"""
}

fun process_dir(curr_dir: File, excludeSet: Set<String>? = null, dirFiles: Array<File>? = null){

val exclude: Set<String> = excludeSet ?: process_ignore_file(curr_dir)

val index_top = """<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="light dark">
<!-- 보안 향상: 인라인 스크립트 실행 방지 -->
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src '${styleHash}'; base-uri 'none'; form-action 'none';">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src '${ProcessDirConstants.styleHash}'; base-uri 'none'; form-action 'none';">
<!-- 보안 향상: 리퍼러를 통한 디렉토리 경로 노출 방지 -->
<meta name="referrer" content="no-referrer">
<title>${curr_dir.getName().escapeHtml()}</title>
${css}
${ProcessDirConstants.css}
</head>
<body>
<main>
Expand Down Expand Up @@ -380,16 +391,8 @@ ${cssContent} </style>
return l.toString();
}

val index_bottom="""
</ul>
</nav>
</main>
</body>
</html>
"""

try {
write_index_file(curr_dir, index_top+index_middle()+index_bottom)
write_index_file(curr_dir, index_top+index_middle()+ProcessDirConstants.index_bottom)
} catch (e: Exception) {
// 보안 향상: 디렉토리에 쓰기 권한이 없거나 파일 시스템 오류가 발생했을 때
// 전체 크롤링(프로세스)이 중단되는 DoS를 방지합니다. (Fail Securely)
Expand Down
Loading