본문 바로가기

카테고리 없음

[Error] Error parsing HTTP request header

 

[2024-08-06 13:24:54:70292406]  INFO  1 --- [-8080-exec-1360] [] o.a.coyote.http11.Http11Processor        : Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
java.lang.IllegalStateException: More than the maximum allowed number of headers, [100], were detected.
        at org.apache.tomcat.util.http.MimeHeaders.createHeader(MimeHeaders.java:237)
        at org.apache.tomcat.util.http.MimeHeaders.addValue(MimeHeaders.java:282)
        at org.apache.coyote.http11.Http11InputBuffer.parseHeader(Http11InputBuffer.java:898)
        at org.apache.coyote.http11.Http11InputBuffer.parseHeaders(Http11InputBuffer.java:591)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:287)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
        at java.base/java.lang.Thread.run(Thread.java:840)

 

에러가 발생한 코드

    private WebClient webClient(Map<String, String> headers) {
        return webClientBuilder.baseUrl(baseUrl)
                .defaultHeaders(header -> {
                    header.add("token", headers.get("token"));
                    header.add("hashKey", headers.get("hashkey"));
                }).build();
    }

위 코드는 WebClient 객체를 생성할 때마다 새로운 헤더가 추가되기 때문에, 매번 WebClient를 새로 만들 때 이전에 추가된 헤더가 계속해서 쌓이는 코드.

그래서 에러가 발생했다. 이를 방지하려면, defaultHeaders 설정에서 매번 새로운 헤더만 추가되도록 해야한다. defaultHeaders는 헤더를 누적하여 추가하기 때문에 주의가 필요하다.

 

올바르게 사용하기 위해서는 header.set을 사용하여 헤더를 설정하는 방식으로 수정할 수 있다. 아래와 같이 set을 이용하면 기존 헤더를 덮어쓰게 되어 헤더가 계속 쌓이지 않는다.

private WebClient webClient(Map<String, String> headers) {
    return webClientBuilder.baseUrl(baseUrl)
            .defaultHeaders(header -> {
                header.set("token", headers.get("token"));
                header.set("hashKey", headers.get("hashkey"));
            }).build();
}