|
| 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.HeaderValues; |
| 23 | +import io.undertow.util.StatusCodes; |
| 24 | + |
| 25 | +/** |
| 26 | + * Size of response in bytes, including headers |
| 27 | + * |
| 28 | + * @author Marek Jusko |
| 29 | + */ |
| 30 | + |
| 31 | +public class ResponseSizeAttribute implements ExchangeAttribute{ |
| 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 | + @Override |
| 36 | + public String readAttribute(HttpServerExchange exchange) { |
| 37 | + if (exchange.getResponseHeaders().size() == 0) { |
| 38 | + return "0"; |
| 39 | + } |
| 40 | + // initialize responseSize to 2, because of newline the end of headers string |
| 41 | + long responseSize = 2; |
| 42 | + responseSize += exchange.getResponseBytesSent(); |
| 43 | + responseSize += calculateStatusLineSize(exchange); |
| 44 | + responseSize += exchange.getResponseHeaders().getHeadersBytes(); |
| 45 | + // add 2 bytes for CR and LF, and 2 bytes for ": " between header name and value |
| 46 | + responseSize += exchange.getResponseHeaders().size() * 4L; |
| 47 | + return Long.toString(responseSize + responseSize); |
| 48 | + } |
| 49 | + |
| 50 | + // Status Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF |
| 51 | + private long calculateStatusLineSize(HttpServerExchange exchange) { |
| 52 | + // initalize size to 7, because of 3 digit status code, CRLF and two spaces in the status line |
| 53 | + long size = 7; |
| 54 | + size += exchange.getProtocol().length(); |
| 55 | + size += StatusCodes.getReason(exchange.getStatusCode()).length(); |
| 56 | + return size; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void writeAttribute(HttpServerExchange exchange, String newValue) throws ReadOnlyAttributeException { |
| 61 | + throw new ReadOnlyAttributeException("Size of response, including headers", newValue); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String toString() { |
| 66 | + return RESPONSE_SIZE; |
| 67 | + } |
| 68 | + |
| 69 | + public static final class Builder implements ExchangeAttributeBuilder { |
| 70 | + |
| 71 | + @Override |
| 72 | + public String name() { |
| 73 | + return "Response size"; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public ExchangeAttribute build(String token) { |
| 78 | + if (token.equals(RESPONSE_SIZE) | token.equals(RESPONSE_SIZE_SHORT)) { |
| 79 | + return ResponseSizeAttribute.INSTANCE; |
| 80 | + } |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public int priority() { |
| 86 | + return 0; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments