|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2014 Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.undertow.attribute; |
| 20 | + |
| 21 | +import io.undertow.server.HttpServerExchange; |
| 22 | +import io.undertow.util.StatusCodes; |
| 23 | + |
| 24 | +/** |
| 25 | + * Size of response in bytes, including headers |
| 26 | + * |
| 27 | + * @author Marek Jusko |
| 28 | + */ |
| 29 | + |
| 30 | +public class ResponseSizeAttribute implements ExchangeAttribute{ |
| 31 | + |
| 32 | + public static final String RESPONSE_SIZE_SHORT = "%O"; |
| 33 | + public static final String RESPONSE_SIZE = "%{RESPONSE_SIZE}"; |
| 34 | + public static final ExchangeAttribute INSTANCE = new ResponseSizeAttribute(); |
| 35 | + |
| 36 | + @Override |
| 37 | + public String readAttribute(HttpServerExchange exchange) { |
| 38 | + if (exchange.getResponseHeaders().size() == 0) { |
| 39 | + return "0"; |
| 40 | + } |
| 41 | + // Initialize requestSize to 2 bytes for the CRLF at the end of headers string |
| 42 | + long responseSize = 2; |
| 43 | + |
| 44 | + // Add the number of bytes sent in the response body |
| 45 | + responseSize += exchange.getResponseBytesSent(); |
| 46 | + |
| 47 | + // Add the size of the status line |
| 48 | + responseSize += calculateStatusLineSize(exchange); |
| 49 | + |
| 50 | + // Add the size of the headers |
| 51 | + responseSize += exchange.getResponseHeaders().getHeadersBytes(); |
| 52 | + |
| 53 | + // Add 4 bytes per header for ": " and CRLF |
| 54 | + responseSize += exchange.getResponseHeaders().size() * 4L; |
| 55 | + |
| 56 | + return Long.toString(responseSize); |
| 57 | + } |
| 58 | + |
| 59 | + // Status Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF |
| 60 | + private long calculateStatusLineSize(HttpServerExchange exchange) { |
| 61 | + // Initialize size to 7 bytes for the spaces, CRLF, and 3-digit status code |
| 62 | + long size = 7; // 3 for status code + 2 for spaces + 2 for CRLF |
| 63 | + |
| 64 | + // Add the length of the HTTP version |
| 65 | + size += exchange.getProtocol().length(); |
| 66 | + |
| 67 | + // Add the length of the status message |
| 68 | + size += StatusCodes.getReason(exchange.getStatusCode()).length(); |
| 69 | + |
| 70 | + return size; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void writeAttribute(HttpServerExchange exchange, String newValue) throws ReadOnlyAttributeException { |
| 75 | + throw new ReadOnlyAttributeException("Size of response, including headers", newValue); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String toString() { |
| 80 | + return RESPONSE_SIZE; |
| 81 | + } |
| 82 | + |
| 83 | + public static final class Builder implements ExchangeAttributeBuilder { |
| 84 | + |
| 85 | + @Override |
| 86 | + public String name() { |
| 87 | + return "Response size"; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public ExchangeAttribute build(String token) { |
| 92 | + if (token.equals(RESPONSE_SIZE) || token.equals(RESPONSE_SIZE_SHORT)) { |
| 93 | + return ResponseSizeAttribute.INSTANCE; |
| 94 | + } |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public int priority() { |
| 100 | + return 0; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments