Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-dev] Content Length in Request Log(NCSARequestLog)

We are using the following workaround (as dirty hack):

public class LengthCorrectionLog extends NCSARequestLog {
    private static final Field contentLengthField;
    static {
        Field f = null;
        try {
            f = Response.class.getDeclaredField("_contentLength");
            f.setAccessible(true);
        } catch (Exception e) {
            // log exception here
        }
        contentLengthField = f;
    }

    LengthCorrectionLog(String filename) {
        super(filename);
    }

    @Override
    public void log(Request request, Response response) {
// NCSARequestLog in Jetty 9 uses response.getLongContentLength() to report response length. // This results in no length reported for chunked transfer encoding (preferred transfer method). // To fix it this class puts result of response.getContentCount() into the response private field // so that subsequent call to getLongContentLength() returns correct length. // This approach violates good practices and response integrity, but it solves problem // in an easiest way as it avoids any hassles with subclasses of both logger and response. // Also note that when logger is invoked the request is already completed and response integrity is no longer important.
        if (contentLengthField != null)
            try {
                long count = response.getContentCount();
                long length = contentLengthField.getLong(response);
if (length <= 0 && count > 0) // Only act if obviously needed.
                    contentLengthField.setLong(response, count);
            } catch (Exception e) {
                // log exception here
            }
        super.log(request, response);
    }
}

Thanks Joakim for your reply.

I agree with your point, but with other web servers like Glassfish, Tomcat etc.when the access log is enabled they do log the content length even when the content length header is missing in web app response.

Is there is any workaround available?

--Jhony



Back to the top