diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java index 7268205488..b8338cd1ac 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java @@ -22,6 +22,7 @@ import io.netty.handler.codec.DefaultTextHeaders; import io.netty.handler.codec.DefaultTextHeaders.DefaultTextValueTypeConverter; import io.netty.handler.codec.TextHeaders; +import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.Iterator; @@ -297,6 +298,12 @@ public class DefaultHttpHeaders extends HttpHeaders { return this; } + @Override + public HttpHeaders addInt(CharSequence name, int value) { + headers.addInt(name, value); + return this; + } + @Override public HttpHeaders remove(String name) { headers.remove(name); @@ -333,6 +340,12 @@ public class DefaultHttpHeaders extends HttpHeaders { return this; } + @Override + public HttpHeaders setInt(CharSequence name, int value) { + headers.setInt(name, value); + return this; + } + @Override public HttpHeaders clear() { headers.clear(); @@ -349,6 +362,35 @@ public class DefaultHttpHeaders extends HttpHeaders { return headers.getAndConvert(name); } + @Override + public Integer getInt(CharSequence name) { + return headers.getInt(name); + } + + @Override + public int getInt(CharSequence name, int defaultValue) { + return headers.getInt(name, defaultValue); + } + + @Override + public Date getDate(CharSequence name) { + return getDate(name, null); + } + + @Override + public Date getDate(CharSequence name, Date defaultValue) { + String value = get(name); + if (value == null) { + return defaultValue; + } + + try { + return HttpHeaderDateFormat.get().parse(value); + } catch (ParseException ignored) { + return defaultValue; + } + } + @Override public List getAll(String name) { return headers.getAllAndConvert(name); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpMessage.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpMessage.java index 96f15deda2..f68433818b 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpMessage.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpMessage.java @@ -68,7 +68,7 @@ public abstract class DefaultHttpMessage extends DefaultHttpObject implements Ht buf.append("(version: "); buf.append(protocolVersion().text()); buf.append(", keepAlive: "); - buf.append(HttpHeaders.isKeepAlive(this)); + buf.append(HttpHeaderUtil.isKeepAlive(this)); buf.append(')'); buf.append(StringUtil.NEWLINE); appendHeaders(buf); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultLastHttpContent.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultLastHttpContent.java index 67ae513a4a..5310b29e22 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultLastHttpContent.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultLastHttpContent.java @@ -17,13 +17,10 @@ package io.netty.handler.codec.http; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import io.netty.handler.codec.AsciiString; import io.netty.util.internal.StringUtil; import java.util.Map.Entry; -import static io.netty.handler.codec.http.HttpHeaders.Names.*; - /** * The default {@link LastHttpContent} implementation. */ @@ -119,9 +116,9 @@ public class DefaultLastHttpContent extends DefaultHttpContent implements LastHt public CharSequence convertName(CharSequence name) { name = super.convertName(name); if (validate) { - if (AsciiString.equalsIgnoreCase(CONTENT_LENGTH, name) - || AsciiString.equalsIgnoreCase(TRANSFER_ENCODING, name) - || AsciiString.equalsIgnoreCase(TRAILER, name)) { + if (HttpHeaderNames.CONTENT_LENGTH.equalsIgnoreCase(name) + || HttpHeaderNames.TRANSFER_ENCODING.equalsIgnoreCase(name) + || HttpHeaderNames.TRAILER.equalsIgnoreCase(name)) { throw new IllegalArgumentException("prohibited trailing header: " + name); } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java index 87367acd18..42dd05b05b 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java @@ -16,7 +16,6 @@ package io.netty.handler.codec.http; import io.netty.channel.embedded.EmbeddedChannel; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.compression.ZlibCodecFactory; import io.netty.handler.codec.compression.ZlibWrapper; import io.netty.util.internal.StringUtil; @@ -95,9 +94,9 @@ public class HttpContentCompressor extends HttpContentEncoder { @Override protected Result beginEncode(HttpResponse headers, String acceptEncoding) throws Exception { - String contentEncoding = headers.headers().get(HttpHeaders.Names.CONTENT_ENCODING); + String contentEncoding = headers.headers().get(HttpHeaderNames.CONTENT_ENCODING); if (contentEncoding != null && - !AsciiString.equalsIgnoreCase(HttpHeaders.Values.IDENTITY, contentEncoding)) { + !HttpHeaderValues.IDENTITY.equalsIgnoreCase(contentEncoding)) { return null; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java index 42ab3af2a1..dce58ac15d 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java @@ -44,6 +44,8 @@ import java.util.List; */ public abstract class HttpContentDecoder extends MessageToMessageDecoder { + static final String IDENTITY = HttpHeaderValues.IDENTITY.toString(); + private EmbeddedChannel decoder; private HttpMessage message; private boolean decodeStarted; @@ -87,30 +89,30 @@ public abstract class HttpContentDecoder extends MessageToMessageDecoder out) throws Exception { - String acceptedEncoding = msg.headers().get(HttpHeaders.Names.ACCEPT_ENCODING); + String acceptedEncoding = msg.headers().get(HttpHeaderNames.ACCEPT_ENCODING); if (acceptedEncoding == null) { - acceptedEncoding = HttpHeaders.Values.IDENTITY; + acceptedEncoding = HttpContentDecoder.IDENTITY; } acceptEncodingQueue.add(acceptedEncoding); out.add(ReferenceCountUtil.retain(msg)); @@ -133,11 +131,11 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec + * These are all defined as lowercase to support HTTP/2 requirements while also not + * violating HTTP/1.x requirements. New header names should always be lowercase. + */ +public final class HttpHeaderNames { + /** + * {@code "accept"} + */ + public static final AsciiString ACCEPT = new AsciiString("accept"); + /** + * {@code "accept-charset"} + */ + public static final AsciiString ACCEPT_CHARSET = new AsciiString("accept-charset"); + /** + * {@code "accept-encoding"} + */ + public static final AsciiString ACCEPT_ENCODING = new AsciiString("accept-encoding"); + /** + * {@code "accept-language"} + */ + public static final AsciiString ACCEPT_LANGUAGE = new AsciiString("accept-language"); + /** + * {@code "accept-ranges"} + */ + public static final AsciiString ACCEPT_RANGES = new AsciiString("accept-ranges"); + /** + * {@code "accept-patch"} + */ + public static final AsciiString ACCEPT_PATCH = new AsciiString("accept-patch"); + /** + * {@code "access-control-allow-credentials"} + */ + public static final AsciiString ACCESS_CONTROL_ALLOW_CREDENTIALS = + new AsciiString("access-control-allow-credentials"); + /** + * {@code "access-control-allow-headers"} + */ + public static final AsciiString ACCESS_CONTROL_ALLOW_HEADERS = + new AsciiString("access-control-allow-headers"); + /** + * {@code "access-control-allow-methods"} + */ + public static final AsciiString ACCESS_CONTROL_ALLOW_METHODS = + new AsciiString("access-control-allow-methods"); + /** + * {@code "access-control-allow-origin"} + */ + public static final AsciiString ACCESS_CONTROL_ALLOW_ORIGIN = + new AsciiString("access-control-allow-origin"); + /** + * {@code "access-control-expose-headers"} + */ + public static final AsciiString ACCESS_CONTROL_EXPOSE_HEADERS = + new AsciiString("access-control-expose-headers"); + /** + * {@code "access-control-max-age"} + */ + public static final AsciiString ACCESS_CONTROL_MAX_AGE = new AsciiString("access-control-max-age"); + /** + * {@code "access-control-request-headers"} + */ + public static final AsciiString ACCESS_CONTROL_REQUEST_HEADERS = + new AsciiString("access-control-request-headers"); + /** + * {@code "access-control-request-method"} + */ + public static final AsciiString ACCESS_CONTROL_REQUEST_METHOD = + new AsciiString("access-control-request-method"); + /** + * {@code "age"} + */ + public static final AsciiString AGE = new AsciiString("age"); + /** + * {@code "allow"} + */ + public static final AsciiString ALLOW = new AsciiString("allow"); + /** + * {@code "authorization"} + */ + public static final AsciiString AUTHORIZATION = new AsciiString("authorization"); + /** + * {@code "cache-control"} + */ + public static final AsciiString CACHE_CONTROL = new AsciiString("cache-control"); + /** + * {@code "connection"} + */ + public static final AsciiString CONNECTION = new AsciiString("connection"); + /** + * {@code "content-base"} + */ + public static final AsciiString CONTENT_BASE = new AsciiString("content-base"); + /** + * {@code "content-encoding"} + */ + public static final AsciiString CONTENT_ENCODING = new AsciiString("content-encoding"); + /** + * {@code "content-language"} + */ + public static final AsciiString CONTENT_LANGUAGE = new AsciiString("content-language"); + /** + * {@code "content-length"} + */ + public static final AsciiString CONTENT_LENGTH = new AsciiString("content-length"); + /** + * {@code "content-location"} + */ + public static final AsciiString CONTENT_LOCATION = new AsciiString("content-location"); + /** + * {@code "content-transfer-encoding"} + */ + public static final AsciiString CONTENT_TRANSFER_ENCODING = new AsciiString("content-transfer-encoding"); + /** + * {@code "content-disposition"} + */ + public static final AsciiString CONTENT_DISPOSITION = new AsciiString("content-disposition"); + /** + * {@code "content-md5"} + */ + public static final AsciiString CONTENT_MD5 = new AsciiString("content-md5"); + /** + * {@code "content-range"} + */ + public static final AsciiString CONTENT_RANGE = new AsciiString("content-range"); + /** + * {@code "content-type"} + */ + public static final AsciiString CONTENT_TYPE = new AsciiString("content-type"); + /** + * {@code "cookie"} + */ + public static final AsciiString COOKIE = new AsciiString("cookie"); + /** + * {@code "date"} + */ + public static final AsciiString DATE = new AsciiString("date"); + /** + * {@code "etag"} + */ + public static final AsciiString ETAG = new AsciiString("etag"); + /** + * {@code "expect"} + */ + public static final AsciiString EXPECT = new AsciiString("expect"); + /** + * {@code "expires"} + */ + public static final AsciiString EXPIRES = new AsciiString("expires"); + /** + * {@code "from"} + */ + public static final AsciiString FROM = new AsciiString("from"); + /** + * {@code "host"} + */ + public static final AsciiString HOST = new AsciiString("host"); + /** + * {@code "if-match"} + */ + public static final AsciiString IF_MATCH = new AsciiString("if-match"); + /** + * {@code "if-modified-since"} + */ + public static final AsciiString IF_MODIFIED_SINCE = new AsciiString("if-modified-since"); + /** + * {@code "if-none-match"} + */ + public static final AsciiString IF_NONE_MATCH = new AsciiString("if-none-match"); + /** + * {@code "if-range"} + */ + public static final AsciiString IF_RANGE = new AsciiString("if-range"); + /** + * {@code "if-unmodified-since"} + */ + public static final AsciiString IF_UNMODIFIED_SINCE = new AsciiString("if-unmodified-since"); + /** + * @deprecated use {@link #CONNECTION} + * + * {@code "keep-alive"} + */ + @Deprecated + public static final AsciiString KEEP_ALIVE = new AsciiString("keep-alive"); + /** + * {@code "last-modified"} + */ + public static final AsciiString LAST_MODIFIED = new AsciiString("last-modified"); + /** + * {@code "location"} + */ + public static final AsciiString LOCATION = new AsciiString("location"); + /** + * {@code "max-forwards"} + */ + public static final AsciiString MAX_FORWARDS = new AsciiString("max-forwards"); + /** + * {@code "origin"} + */ + public static final AsciiString ORIGIN = new AsciiString("origin"); + /** + * {@code "pragma"} + */ + public static final AsciiString PRAGMA = new AsciiString("pragma"); + /** + * {@code "proxy-authenticate"} + */ + public static final AsciiString PROXY_AUTHENTICATE = new AsciiString("proxy-authenticate"); + /** + * {@code "proxy-authorization"} + */ + public static final AsciiString PROXY_AUTHORIZATION = new AsciiString("proxy-authorization"); + /** + * @deprecated use {@link #CONNECTION} + * + * {@code "proxy-connection"} + */ + @Deprecated + public static final AsciiString PROXY_CONNECTION = new AsciiString("proxy-connection"); + /** + * {@code "range"} + */ + public static final AsciiString RANGE = new AsciiString("range"); + /** + * {@code "referer"} + */ + public static final AsciiString REFERER = new AsciiString("referer"); + /** + * {@code "retry-after"} + */ + public static final AsciiString RETRY_AFTER = new AsciiString("retry-after"); + /** + * {@code "sec-websocket-key1"} + */ + public static final AsciiString SEC_WEBSOCKET_KEY1 = new AsciiString("sec-websocket-key1"); + /** + * {@code "sec-websocket-key2"} + */ + public static final AsciiString SEC_WEBSOCKET_KEY2 = new AsciiString("sec-websocket-key2"); + /** + * {@code "sec-websocket-location"} + */ + public static final AsciiString SEC_WEBSOCKET_LOCATION = new AsciiString("sec-websocket-location"); + /** + * {@code "sec-websocket-origin"} + */ + public static final AsciiString SEC_WEBSOCKET_ORIGIN = new AsciiString("sec-websocket-origin"); + /** + * {@code "sec-websocket-protocol"} + */ + public static final AsciiString SEC_WEBSOCKET_PROTOCOL = new AsciiString("sec-websocket-protocol"); + /** + * {@code "sec-websocket-version"} + */ + public static final AsciiString SEC_WEBSOCKET_VERSION = new AsciiString("sec-websocket-version"); + /** + * {@code "sec-websocket-key"} + */ + public static final AsciiString SEC_WEBSOCKET_KEY = new AsciiString("sec-websocket-key"); + /** + * {@code "sec-websocket-accept"} + */ + public static final AsciiString SEC_WEBSOCKET_ACCEPT = new AsciiString("sec-websocket-accept"); + /** + * {@code "sec-websocket-protocol"} + */ + public static final AsciiString SEC_WEBSOCKET_EXTENSIONS = new AsciiString("sec-websocket-extensions"); + /** + * {@code "server"} + */ + public static final AsciiString SERVER = new AsciiString("server"); + /** + * {@code "set-cookie"} + */ + public static final AsciiString SET_COOKIE = new AsciiString("set-cookie"); + /** + * {@code "set-cookie2"} + */ + public static final AsciiString SET_COOKIE2 = new AsciiString("set-cookie2"); + /** + * {@code "te"} + */ + public static final AsciiString TE = new AsciiString("te"); + /** + * {@code "trailer"} + */ + public static final AsciiString TRAILER = new AsciiString("trailer"); + /** + * {@code "transfer-encoding"} + */ + public static final AsciiString TRANSFER_ENCODING = new AsciiString("transfer-encoding"); + /** + * {@code "upgrade"} + */ + public static final AsciiString UPGRADE = new AsciiString("upgrade"); + /** + * {@code "user-agent"} + */ + public static final AsciiString USER_AGENT = new AsciiString("user-agent"); + /** + * {@code "vary"} + */ + public static final AsciiString VARY = new AsciiString("vary"); + /** + * {@code "via"} + */ + public static final AsciiString VIA = new AsciiString("via"); + /** + * {@code "warning"} + */ + public static final AsciiString WARNING = new AsciiString("warning"); + /** + * {@code "websocket-location"} + */ + public static final AsciiString WEBSOCKET_LOCATION = new AsciiString("websocket-location"); + /** + * {@code "websocket-origin"} + */ + public static final AsciiString WEBSOCKET_ORIGIN = new AsciiString("websocket-origin"); + /** + * {@code "websocket-protocol"} + */ + public static final AsciiString WEBSOCKET_PROTOCOL = new AsciiString("websocket-protocol"); + /** + * {@code "www-authenticate"} + */ + public static final AsciiString WWW_AUTHENTICATE = new AsciiString("www-authenticate"); + + private HttpHeaderNames() { } +} diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderUtil.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderUtil.java index b1eb85b5ba..20f3e189aa 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderUtil.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderUtil.java @@ -17,9 +17,6 @@ package io.netty.handler.codec.http; import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.AsciiString; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import java.util.Iterator; import java.util.List; @@ -33,15 +30,15 @@ public final class HttpHeaderUtil { * {@link HttpVersion#isKeepAliveDefault()}. */ public static boolean isKeepAlive(HttpMessage message) { - CharSequence connection = message.headers().get(Names.CONNECTION); - if (connection != null && AsciiString.equalsIgnoreCase(Values.CLOSE, connection)) { + CharSequence connection = message.headers().get(HttpHeaderNames.CONNECTION); + if (connection != null && HttpHeaderValues.CLOSE.equalsIgnoreCase(connection)) { return false; } if (message.protocolVersion().isKeepAliveDefault()) { - return !AsciiString.equalsIgnoreCase(Values.CLOSE, connection); + return !HttpHeaderValues.CLOSE.equalsIgnoreCase(connection); } else { - return AsciiString.equalsIgnoreCase(Values.KEEP_ALIVE, connection); + return HttpHeaderValues.KEEP_ALIVE.equalsIgnoreCase(connection); } } @@ -68,15 +65,15 @@ public final class HttpHeaderUtil { HttpHeaders h = message.headers(); if (message.protocolVersion().isKeepAliveDefault()) { if (keepAlive) { - h.remove(Names.CONNECTION); + h.remove(HttpHeaderNames.CONNECTION); } else { - h.set(Names.CONNECTION, Values.CLOSE); + h.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); } } else { if (keepAlive) { - h.set(Names.CONNECTION, Values.KEEP_ALIVE); + h.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } else { - h.remove(Names.CONNECTION); + h.remove(HttpHeaderNames.CONNECTION); } } } @@ -94,7 +91,7 @@ public final class HttpHeaderUtil { * or its value is not a number */ public static long getContentLength(HttpMessage message) { - String value = message.headers().get(Names.CONTENT_LENGTH); + String value = message.headers().get(HttpHeaderNames.CONTENT_LENGTH); if (value != null) { return Long.parseLong(value); } @@ -107,7 +104,7 @@ public final class HttpHeaderUtil { } // Otherwise we don't. - throw new NumberFormatException("header not found: " + Names.CONTENT_LENGTH); + throw new NumberFormatException("header not found: " + HttpHeaderNames.CONTENT_LENGTH); } /** @@ -121,7 +118,7 @@ public final class HttpHeaderUtil { * a number */ public static long getContentLength(HttpMessage message, long defaultValue) { - String value = message.headers().get(Names.CONTENT_LENGTH); + String value = message.headers().get(HttpHeaderNames.CONTENT_LENGTH); if (value != null) { return Long.parseLong(value); } @@ -147,15 +144,15 @@ public final class HttpHeaderUtil { if (message instanceof HttpRequest) { HttpRequest req = (HttpRequest) message; if (HttpMethod.GET.equals(req.method()) && - h.contains(Names.SEC_WEBSOCKET_KEY1) && - h.contains(Names.SEC_WEBSOCKET_KEY2)) { + h.contains(HttpHeaderNames.SEC_WEBSOCKET_KEY1) && + h.contains(HttpHeaderNames.SEC_WEBSOCKET_KEY2)) { return 8; } } else if (message instanceof HttpResponse) { HttpResponse res = (HttpResponse) message; if (res.status().code() == 101 && - h.contains(Names.SEC_WEBSOCKET_ORIGIN) && - h.contains(Names.SEC_WEBSOCKET_LOCATION)) { + h.contains(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN) && + h.contains(HttpHeaderNames.SEC_WEBSOCKET_LOCATION)) { return 16; } } @@ -168,11 +165,11 @@ public final class HttpHeaderUtil { * Sets the {@code "Content-Length"} header. */ public static void setContentLength(HttpMessage message, long length) { - message.headers().set(Names.CONTENT_LENGTH, length); + message.headers().set(HttpHeaderNames.CONTENT_LENGTH, length); } public static boolean isContentLengthSet(HttpMessage m) { - return m.headers().contains(Names.CONTENT_LENGTH); + return m.headers().contains(HttpHeaderNames.CONTENT_LENGTH); } /** @@ -191,16 +188,16 @@ public final class HttpHeaderUtil { } // In most cases, there will be one or zero 'Expect' header. - CharSequence value = message.headers().get(Names.EXPECT); + CharSequence value = message.headers().get(HttpHeaderNames.EXPECT); if (value == null) { return false; } - if (AsciiString.equalsIgnoreCase(Values.CONTINUE, value)) { + if (HttpHeaderValues.CONTINUE.equalsIgnoreCase(value)) { return true; } // Multiple 'Expect' headers. Search through them. - return message.headers().contains(Names.EXPECT, Values.CONTINUE, true); + return message.headers().contains(HttpHeaderNames.EXPECT, HttpHeaderValues.CONTINUE, true); } /** @@ -212,9 +209,9 @@ public final class HttpHeaderUtil { */ public static void set100ContinueExpected(HttpMessage message, boolean expected) { if (expected) { - message.headers().set(Names.EXPECT, Values.CONTINUE); + message.headers().set(HttpHeaderNames.EXPECT, HttpHeaderValues.CONTINUE); } else { - message.headers().remove(Names.EXPECT); + message.headers().remove(HttpHeaderNames.EXPECT); } } @@ -225,29 +222,29 @@ public final class HttpHeaderUtil { * @return True if transfer encoding is chunked, otherwise false */ public static boolean isTransferEncodingChunked(HttpMessage message) { - return message.headers().contains(Names.TRANSFER_ENCODING, Values.CHUNKED, true); + return message.headers().contains(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED, true); } public static void setTransferEncodingChunked(HttpMessage m, boolean chunked) { if (chunked) { - m.headers().add(Names.TRANSFER_ENCODING, Values.CHUNKED); - m.headers().remove(Names.CONTENT_LENGTH); + m.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); + m.headers().remove(HttpHeaderNames.CONTENT_LENGTH); } else { - List values = m.headers().getAll(Names.TRANSFER_ENCODING); + List values = m.headers().getAll(HttpHeaderNames.TRANSFER_ENCODING); if (values.isEmpty()) { return; } Iterator valuesIt = values.iterator(); while (valuesIt.hasNext()) { CharSequence value = valuesIt.next(); - if (AsciiString.equalsIgnoreCase(value, Values.CHUNKED)) { + if (HttpHeaderValues.CHUNKED.equalsIgnoreCase(value)) { valuesIt.remove(); } } if (values.isEmpty()) { - m.headers().remove(Names.TRANSFER_ENCODING); + m.headers().remove(HttpHeaderNames.TRANSFER_ENCODING); } else { - m.headers().set(Names.TRANSFER_ENCODING, values); + m.headers().set(HttpHeaderNames.TRANSFER_ENCODING, values); } } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderValues.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderValues.java new file mode 100644 index 0000000000..ea907cba2b --- /dev/null +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderValues.java @@ -0,0 +1,193 @@ +/* + * Copyright 2014 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package io.netty.handler.codec.http; + +import io.netty.handler.codec.AsciiString; + +/** + * Standard HTTP header values. + */ +public final class HttpHeaderValues { + /** + * {@code "application/x-www-form-urlencoded"} + */ + public static final AsciiString APPLICATION_X_WWW_FORM_URLENCODED = + new AsciiString("application/x-www-form-urlencoded"); + /** + * {@code "application/octet-stream"} + */ + public static final AsciiString APPLICATION_OCTET_STREAM = new AsciiString("application/octet-stream"); + /** + * {@code "attachment"} + * See {@link HttpHeaderNames#CONTENT_DISPOSITION} + */ + public static final AsciiString ATTACHMENT = new AsciiString("attachment"); + /** + * {@code "base64"} + */ + public static final AsciiString BASE64 = new AsciiString("base64"); + /** + * {@code "binary"} + */ + public static final AsciiString BINARY = new AsciiString("binary"); + /** + * {@code "boundary"} + */ + public static final AsciiString BOUNDARY = new AsciiString("boundary"); + /** + * {@code "bytes"} + */ + public static final AsciiString BYTES = new AsciiString("bytes"); + /** + * {@code "charset"} + */ + public static final AsciiString CHARSET = new AsciiString("charset"); + /** + * {@code "chunked"} + */ + public static final AsciiString CHUNKED = new AsciiString("chunked"); + /** + * {@code "close"} + */ + public static final AsciiString CLOSE = new AsciiString("close"); + /** + * {@code "compress"} + */ + public static final AsciiString COMPRESS = new AsciiString("compress"); + /** + * {@code "100-continue"} + */ + public static final AsciiString CONTINUE = new AsciiString("100-continue"); + /** + * {@code "deflate"} + */ + public static final AsciiString DEFLATE = new AsciiString("deflate"); + /** + * {@code "file"} + * See {@link HttpHeaderNames#CONTENT_DISPOSITION} + */ + public static final AsciiString FILE = new AsciiString("file"); + /** + * {@code "filename"} + * See {@link HttpHeaderNames#CONTENT_DISPOSITION} + */ + public static final AsciiString FILENAME = new AsciiString("filename"); + /** + * {@code "form-data"} + * See {@link HttpHeaderNames#CONTENT_DISPOSITION} + */ + public static final AsciiString FORM_DATA = new AsciiString("form-data"); + /** + * {@code "gzip"} + */ + public static final AsciiString GZIP = new AsciiString("gzip"); + /** + * {@code "identity"} + */ + public static final AsciiString IDENTITY = new AsciiString("identity"); + /** + * {@code "keep-alive"} + */ + public static final AsciiString KEEP_ALIVE = new AsciiString("keep-alive"); + /** + * {@code "max-age"} + */ + public static final AsciiString MAX_AGE = new AsciiString("max-age"); + /** + * {@code "max-stale"} + */ + public static final AsciiString MAX_STALE = new AsciiString("max-stale"); + /** + * {@code "min-fresh"} + */ + public static final AsciiString MIN_FRESH = new AsciiString("min-fresh"); + /** + * {@code "multipart/form-data"} + */ + public static final AsciiString MULTIPART_FORM_DATA = new AsciiString("multipart/form-data"); + /** + * {@code "multipart/mixed"} + */ + public static final AsciiString MULTIPART_MIXED = new AsciiString("multipart/mixed"); + /** + * {@code "must-revalidate"} + */ + public static final AsciiString MUST_REVALIDATE = new AsciiString("must-revalidate"); + /** + * {@code "name"} + * See {@link HttpHeaderNames#CONTENT_DISPOSITION} + */ + public static final AsciiString NAME = new AsciiString("name"); + /** + * {@code "no-cache"} + */ + public static final AsciiString NO_CACHE = new AsciiString("no-cache"); + /** + * {@code "no-store"} + */ + public static final AsciiString NO_STORE = new AsciiString("no-store"); + /** + * {@code "no-transform"} + */ + public static final AsciiString NO_TRANSFORM = new AsciiString("no-transform"); + /** + * {@code "none"} + */ + public static final AsciiString NONE = new AsciiString("none"); + /** + * {@code "only-if-cached"} + */ + public static final AsciiString ONLY_IF_CACHED = new AsciiString("only-if-cached"); + /** + * {@code "private"} + */ + public static final AsciiString PRIVATE = new AsciiString("private"); + /** + * {@code "proxy-revalidate"} + */ + public static final AsciiString PROXY_REVALIDATE = new AsciiString("proxy-revalidate"); + /** + * {@code "public"} + */ + public static final AsciiString PUBLIC = new AsciiString("public"); + /** + * {@code "quoted-printable"} + */ + public static final AsciiString QUOTED_PRINTABLE = new AsciiString("quoted-printable"); + /** + * {@code "s-maxage"} + */ + public static final AsciiString S_MAXAGE = new AsciiString("s-maxage"); + /** + * {@code "text/plain"} + */ + public static final AsciiString TEXT_PLAIN = new AsciiString("text/plain"); + /** + * {@code "trailers"} + */ + public static final AsciiString TRAILERS = new AsciiString("trailers"); + /** + * {@code "Upgrade"} + */ + public static final AsciiString UPGRADE = new AsciiString("Upgrade"); + /** + * {@code "websocket"} + */ + public static final AsciiString WEBSOCKET = new AsciiString("websocket"); + + private HttpHeaderValues() { } +} diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java index 64bff4e636..e18990c3ea 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java @@ -15,10 +15,6 @@ */ package io.netty.handler.codec.http; -import static io.netty.handler.codec.http.HttpConstants.COLON; -import static io.netty.handler.codec.http.HttpConstants.CR; -import static io.netty.handler.codec.http.HttpConstants.LF; -import static io.netty.handler.codec.http.HttpConstants.SP; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.AsciiString; @@ -32,6 +28,8 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import static io.netty.handler.codec.http.HttpConstants.*; + /** * Provides the constants for the standard HTTP header names and values and * commonly used utility methods that accesses an {@link HttpMessage}. @@ -40,20 +38,6 @@ public abstract class HttpHeaders implements Iterable> private static final byte[] HEADER_SEPERATOR = { COLON, SP }; private static final byte[] CRLF = { CR, LF }; - private static final CharSequence CONTENT_LENGTH_ENTITY = Names.CONTENT_LENGTH; - private static final CharSequence CONNECTION_ENTITY = Names.CONNECTION; - private static final CharSequence CLOSE_ENTITY = Values.CLOSE; - private static final CharSequence KEEP_ALIVE_ENTITY = Values.KEEP_ALIVE; - private static final CharSequence HOST_ENTITY = Names.HOST; - private static final CharSequence DATE_ENTITY = Names.DATE; - private static final CharSequence EXPECT_ENTITY = Names.EXPECT; - private static final CharSequence CONTINUE_ENTITY = Values.CONTINUE; - private static final CharSequence TRANSFER_ENCODING_ENTITY = Names.TRANSFER_ENCODING; - private static final CharSequence CHUNKED_ENTITY = Values.CHUNKED; - private static final CharSequence SEC_WEBSOCKET_KEY1_ENTITY = Names.SEC_WEBSOCKET_KEY1; - private static final CharSequence SEC_WEBSOCKET_KEY2_ENTITY = Names.SEC_WEBSOCKET_KEY2; - private static final CharSequence SEC_WEBSOCKET_ORIGIN_ENTITY = Names.SEC_WEBSOCKET_ORIGIN; - private static final CharSequence SEC_WEBSOCKET_LOCATION_ENTITY = Names.SEC_WEBSOCKET_LOCATION; public static final HttpHeaders EMPTY_HEADERS = new HttpHeaders() { @Override @@ -61,6 +45,26 @@ public abstract class HttpHeaders implements Iterable> return null; } + @Override + public Integer getInt(CharSequence name) { + return null; + } + + @Override + public int getInt(CharSequence name, int defaultValue) { + return defaultValue; + } + + @Override + public Date getDate(CharSequence name) { + return null; + } + + @Override + public Date getDate(CharSequence name, Date defaultValue) { + return defaultValue; + } + @Override public List getAll(String name) { return Collections.emptyList(); @@ -96,6 +100,11 @@ public abstract class HttpHeaders implements Iterable> throw new UnsupportedOperationException("read only"); } + @Override + public HttpHeaders addInt(CharSequence name, int value) { + throw new UnsupportedOperationException("read only"); + } + @Override public HttpHeaders set(String name, Object value) { throw new UnsupportedOperationException("read only"); @@ -106,6 +115,11 @@ public abstract class HttpHeaders implements Iterable> throw new UnsupportedOperationException("read only"); } + @Override + public HttpHeaders setInt(CharSequence name, int value) { + throw new UnsupportedOperationException("read only"); + } + @Override public HttpHeaders remove(String name) { throw new UnsupportedOperationException("read only"); @@ -123,353 +137,321 @@ public abstract class HttpHeaders implements Iterable> }; /** + * @deprecated Use {@link HttpHeaderNames} instead. + * * Standard HTTP header names. - *

- * These are all defined as lowercase to support HTTP/2 requirements while also not - * violating HTTP/1.x requirements. New header names should always be lowercase. */ + @Deprecated public static final class Names { /** - * {@code "accept"} + * {@code "Accept"} */ - public static final AsciiString ACCEPT = new AsciiString("accept"); + public static final String ACCEPT = "Accept"; /** - * {@code "accept-charset"} + * {@code "Accept-Charset"} */ - public static final AsciiString ACCEPT_CHARSET = new AsciiString("accept-charset"); + public static final String ACCEPT_CHARSET = "Accept-Charset"; /** - * {@code "accept-encoding"} + * {@code "Accept-Encoding"} */ - public static final AsciiString ACCEPT_ENCODING = new AsciiString("accept-encoding"); + public static final String ACCEPT_ENCODING = "Accept-Encoding"; /** - * {@code "accept-language"} + * {@code "Accept-Language"} */ - public static final AsciiString ACCEPT_LANGUAGE = new AsciiString("accept-language"); + public static final String ACCEPT_LANGUAGE = "Accept-Language"; /** - * {@code "accept-ranges"} + * {@code "Accept-Ranges"} */ - public static final AsciiString ACCEPT_RANGES = new AsciiString("accept-ranges"); + public static final String ACCEPT_RANGES = "Accept-Ranges"; /** - * {@code "accept-patch"} + * {@code "Accept-Patch"} */ - public static final AsciiString ACCEPT_PATCH = new AsciiString("accept-patch"); + public static final String ACCEPT_PATCH = "Accept-Patch"; /** - * {@code "access-control-allow-credentials"} + * {@code "Access-Control-Allow-Credentials"} */ - public static final AsciiString ACCESS_CONTROL_ALLOW_CREDENTIALS = - new AsciiString("access-control-allow-credentials"); + public static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials"; /** - * {@code "access-control-allow-headers"} + * {@code "Access-Control-Allow-Headers"} */ - public static final AsciiString ACCESS_CONTROL_ALLOW_HEADERS = - new AsciiString("access-control-allow-headers"); + public static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers"; /** - * {@code "access-control-allow-methods"} + * {@code "Access-Control-Allow-Methods"} */ - public static final AsciiString ACCESS_CONTROL_ALLOW_METHODS = - new AsciiString("access-control-allow-methods"); + public static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods"; /** - * {@code "access-control-allow-origin"} + * {@code "Access-Control-Allow-Origin"} */ - public static final AsciiString ACCESS_CONTROL_ALLOW_ORIGIN = - new AsciiString("access-control-allow-origin"); + public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin"; /** - * {@code "access-control-expose-headers"} + * {@code "Access-Control-Expose-Headers"} */ - public static final AsciiString ACCESS_CONTROL_EXPOSE_HEADERS = - new AsciiString("access-control-expose-headers"); + public static final String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers"; /** - * {@code "access-control-max-age"} + * {@code "Access-Control-Max-Age"} */ - public static final AsciiString ACCESS_CONTROL_MAX_AGE = new AsciiString("access-control-max-age"); + public static final String ACCESS_CONTROL_MAX_AGE = "Access-Control-Max-Age"; /** - * {@code "access-control-request-headers"} + * {@code "Access-Control-Request-Headers"} */ - public static final AsciiString ACCESS_CONTROL_REQUEST_HEADERS = - new AsciiString("access-control-request-headers"); + public static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers"; /** - * {@code "access-control-request-method"} + * {@code "Access-Control-Request-Method"} */ - public static final AsciiString ACCESS_CONTROL_REQUEST_METHOD = - new AsciiString("access-control-request-method"); + public static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method"; /** - * {@code "age"} + * {@code "Age"} */ - public static final AsciiString AGE = new AsciiString("age"); + public static final String AGE = "Age"; /** - * {@code "allow"} + * {@code "Allow"} */ - public static final AsciiString ALLOW = new AsciiString("allow"); + public static final String ALLOW = "Allow"; /** - * {@code "authorization"} + * {@code "Authorization"} */ - public static final AsciiString AUTHORIZATION = new AsciiString("authorization"); + public static final String AUTHORIZATION = "Authorization"; /** - * {@code "cache-control"} + * {@code "Cache-Control"} */ - public static final AsciiString CACHE_CONTROL = new AsciiString("cache-control"); + public static final String CACHE_CONTROL = "Cache-Control"; /** - * {@code "connection"} + * {@code "Connection"} */ - public static final AsciiString CONNECTION = new AsciiString("connection"); + public static final String CONNECTION = "Connection"; /** - * {@code "content-base"} + * {@code "Content-Base"} */ - public static final AsciiString CONTENT_BASE = new AsciiString("content-base"); + public static final String CONTENT_BASE = "Content-Base"; /** - * {@code "content-encoding"} + * {@code "Content-Encoding"} */ - public static final AsciiString CONTENT_ENCODING = new AsciiString("content-encoding"); + public static final String CONTENT_ENCODING = "Content-Encoding"; /** - * {@code "content-language"} + * {@code "Content-Language"} */ - public static final AsciiString CONTENT_LANGUAGE = new AsciiString("content-language"); + public static final String CONTENT_LANGUAGE = "Content-Language"; /** - * {@code "content-length"} + * {@code "Content-Length"} */ - public static final AsciiString CONTENT_LENGTH = new AsciiString("content-length"); + public static final String CONTENT_LENGTH = "Content-Length"; /** - * {@code "content-location"} + * {@code "Content-Location"} */ - public static final AsciiString CONTENT_LOCATION = new AsciiString("content-location"); + public static final String CONTENT_LOCATION = "Content-Location"; /** - * {@code "content-transfer-encoding"} + * {@code "Content-Transfer-Encoding"} */ - public static final AsciiString CONTENT_TRANSFER_ENCODING = new AsciiString("content-transfer-encoding"); + public static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding"; /** - * {@code "content-disposition"} + * {@code "Content-MD5"} */ - public static final AsciiString CONTENT_DISPOSITION = new AsciiString("content-disposition"); + public static final String CONTENT_MD5 = "Content-MD5"; /** - * {@code "content-md5"} + * {@code "Content-Range"} */ - public static final AsciiString CONTENT_MD5 = new AsciiString("content-md5"); + public static final String CONTENT_RANGE = "Content-Range"; /** - * {@code "content-range"} + * {@code "Content-Type"} */ - public static final AsciiString CONTENT_RANGE = new AsciiString("content-range"); + public static final String CONTENT_TYPE = "Content-Type"; /** - * {@code "content-type"} + * {@code "Cookie"} */ - public static final AsciiString CONTENT_TYPE = new AsciiString("content-type"); + public static final String COOKIE = "Cookie"; /** - * {@code "cookie"} + * {@code "Date"} */ - public static final AsciiString COOKIE = new AsciiString("cookie"); + public static final String DATE = "Date"; /** - * {@code "date"} + * {@code "ETag"} */ - public static final AsciiString DATE = new AsciiString("date"); + public static final String ETAG = "ETag"; /** - * {@code "etag"} + * {@code "Expect"} */ - public static final AsciiString ETAG = new AsciiString("etag"); + public static final String EXPECT = "Expect"; /** - * {@code "expect"} + * {@code "Expires"} */ - public static final AsciiString EXPECT = new AsciiString("expect"); + public static final String EXPIRES = "Expires"; /** - * {@code "expires"} + * {@code "From"} */ - public static final AsciiString EXPIRES = new AsciiString("expires"); + public static final String FROM = "From"; /** - * {@code "from"} + * {@code "Host"} */ - public static final AsciiString FROM = new AsciiString("from"); + public static final String HOST = "Host"; /** - * {@code "host"} + * {@code "If-Match"} */ - public static final AsciiString HOST = new AsciiString("host"); + public static final String IF_MATCH = "If-Match"; /** - * {@code "if-match"} + * {@code "If-Modified-Since"} */ - public static final AsciiString IF_MATCH = new AsciiString("if-match"); + public static final String IF_MODIFIED_SINCE = "If-Modified-Since"; /** - * {@code "if-modified-since"} + * {@code "If-None-Match"} */ - public static final AsciiString IF_MODIFIED_SINCE = new AsciiString("if-modified-since"); + public static final String IF_NONE_MATCH = "If-None-Match"; /** - * {@code "if-none-match"} + * {@code "If-Range"} */ - public static final AsciiString IF_NONE_MATCH = new AsciiString("if-none-match"); + public static final String IF_RANGE = "If-Range"; /** - * {@code "if-range"} + * {@code "If-Unmodified-Since"} */ - public static final AsciiString IF_RANGE = new AsciiString("if-range"); + public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since"; /** - * {@code "if-unmodified-since"} + * {@code "Last-Modified"} */ - public static final AsciiString IF_UNMODIFIED_SINCE = new AsciiString("if-unmodified-since"); + public static final String LAST_MODIFIED = "Last-Modified"; /** - * {@code "last-modified"} + * {@code "Location"} */ - public static final AsciiString LAST_MODIFIED = new AsciiString("last-modified"); + public static final String LOCATION = "Location"; /** - * {@code "location"} + * {@code "Max-Forwards"} */ - public static final AsciiString LOCATION = new AsciiString("location"); + public static final String MAX_FORWARDS = "Max-Forwards"; /** - * {@code "max-forwards"} + * {@code "Origin"} */ - public static final AsciiString MAX_FORWARDS = new AsciiString("max-forwards"); + public static final String ORIGIN = "Origin"; /** - * {@code "origin"} + * {@code "Pragma"} */ - public static final AsciiString ORIGIN = new AsciiString("origin"); + public static final String PRAGMA = "Pragma"; /** - * {@code "pragma"} + * {@code "Proxy-Authenticate"} */ - public static final AsciiString PRAGMA = new AsciiString("pragma"); + public static final String PROXY_AUTHENTICATE = "Proxy-Authenticate"; /** - * {@code "proxy-authenticate"} + * {@code "Proxy-Authorization"} */ - public static final AsciiString PROXY_AUTHENTICATE = new AsciiString("proxy-authenticate"); + public static final String PROXY_AUTHORIZATION = "Proxy-Authorization"; /** - * {@code "proxy-authorization"} + * {@code "Range"} */ - public static final AsciiString PROXY_AUTHORIZATION = new AsciiString("proxy-authorization"); + public static final String RANGE = "Range"; /** - * {@code "range"} + * {@code "Referer"} */ - public static final AsciiString RANGE = new AsciiString("range"); + public static final String REFERER = "Referer"; /** - * {@code "referer"} + * {@code "Retry-After"} */ - public static final AsciiString REFERER = new AsciiString("referer"); + public static final String RETRY_AFTER = "Retry-After"; /** - * {@code "retry-after"} + * {@code "Sec-WebSocket-Key1"} */ - public static final AsciiString RETRY_AFTER = new AsciiString("retry-after"); + public static final String SEC_WEBSOCKET_KEY1 = "Sec-WebSocket-Key1"; /** - * {@code "sec-websocket-key1"} + * {@code "Sec-WebSocket-Key2"} */ - public static final AsciiString SEC_WEBSOCKET_KEY1 = new AsciiString("sec-websocket-key1"); + public static final String SEC_WEBSOCKET_KEY2 = "Sec-WebSocket-Key2"; /** - * {@code "sec-websocket-key2"} + * {@code "Sec-WebSocket-Location"} */ - public static final AsciiString SEC_WEBSOCKET_KEY2 = new AsciiString("sec-websocket-key2"); + public static final String SEC_WEBSOCKET_LOCATION = "Sec-WebSocket-Location"; /** - * {@code "sec-websocket-location"} + * {@code "Sec-WebSocket-Origin"} */ - public static final AsciiString SEC_WEBSOCKET_LOCATION = new AsciiString("sec-websocket-location"); + public static final String SEC_WEBSOCKET_ORIGIN = "Sec-WebSocket-Origin"; /** - * {@code "sec-websocket-origin"} + * {@code "Sec-WebSocket-Protocol"} */ - public static final AsciiString SEC_WEBSOCKET_ORIGIN = new AsciiString("sec-websocket-origin"); + public static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol"; /** - * {@code "sec-websocket-protocol"} + * {@code "Sec-WebSocket-Version"} */ - public static final AsciiString SEC_WEBSOCKET_PROTOCOL = new AsciiString("sec-websocket-protocol"); + public static final String SEC_WEBSOCKET_VERSION = "Sec-WebSocket-Version"; /** - * {@code "sec-websocket-version"} + * {@code "Sec-WebSocket-Key"} */ - public static final AsciiString SEC_WEBSOCKET_VERSION = new AsciiString("sec-websocket-version"); + public static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key"; /** - * {@code "sec-websocket-key"} + * {@code "Sec-WebSocket-Accept"} */ - public static final AsciiString SEC_WEBSOCKET_KEY = new AsciiString("sec-websocket-key"); + public static final String SEC_WEBSOCKET_ACCEPT = "Sec-WebSocket-Accept"; /** - * {@code "sec-websocket-accept"} + * {@code "Server"} */ - public static final AsciiString SEC_WEBSOCKET_ACCEPT = new AsciiString("sec-websocket-accept"); + public static final String SERVER = "Server"; /** - * {@code "sec-websocket-protocol"} + * {@code "Set-Cookie"} */ - public static final AsciiString SEC_WEBSOCKET_EXTENSIONS = new AsciiString("sec-websocket-extensions"); + public static final String SET_COOKIE = "Set-Cookie"; /** - * {@code "server"} + * {@code "Set-Cookie2"} */ - public static final AsciiString SERVER = new AsciiString("server"); + public static final String SET_COOKIE2 = "Set-Cookie2"; /** - * {@code "set-cookie"} + * {@code "TE"} */ - public static final AsciiString SET_COOKIE = new AsciiString("set-cookie"); + public static final String TE = "TE"; /** - * {@code "set-cookie2"} + * {@code "Trailer"} */ - public static final AsciiString SET_COOKIE2 = new AsciiString("set-cookie2"); + public static final String TRAILER = "Trailer"; /** - * {@code "te"} + * {@code "Transfer-Encoding"} */ - public static final AsciiString TE = new AsciiString("te"); + public static final String TRANSFER_ENCODING = "Transfer-Encoding"; /** - * {@code "trailer"} + * {@code "Upgrade"} */ - public static final AsciiString TRAILER = new AsciiString("trailer"); + public static final String UPGRADE = "Upgrade"; /** - * {@code "transfer-encoding"} + * {@code "User-Agent"} */ - public static final AsciiString TRANSFER_ENCODING = new AsciiString("transfer-encoding"); + public static final String USER_AGENT = "User-Agent"; /** - * {@code "upgrade"} + * {@code "Vary"} */ - public static final AsciiString UPGRADE = new AsciiString("upgrade"); + public static final String VARY = "Vary"; /** - * {@code "user-agent"} + * {@code "Via"} */ - public static final AsciiString USER_AGENT = new AsciiString("user-agent"); + public static final String VIA = "Via"; /** - * {@code "vary"} + * {@code "Warning"} */ - public static final AsciiString VARY = new AsciiString("vary"); + public static final String WARNING = "Warning"; /** - * {@code "via"} + * {@code "WebSocket-Location"} */ - public static final AsciiString VIA = new AsciiString("via"); + public static final String WEBSOCKET_LOCATION = "WebSocket-Location"; /** - * {@code "warning"} + * {@code "WebSocket-Origin"} */ - public static final AsciiString WARNING = new AsciiString("warning"); + public static final String WEBSOCKET_ORIGIN = "WebSocket-Origin"; /** - * {@code "websocket-location"} + * {@code "WebSocket-Protocol"} */ - public static final AsciiString WEBSOCKET_LOCATION = new AsciiString("websocket-location"); + public static final String WEBSOCKET_PROTOCOL = "WebSocket-Protocol"; /** - * {@code "websocket-origin"} + * {@code "WWW-Authenticate"} */ - public static final AsciiString WEBSOCKET_ORIGIN = new AsciiString("websocket-origin"); - /** - * {@code "websocket-protocol"} - */ - public static final AsciiString WEBSOCKET_PROTOCOL = new AsciiString("websocket-protocol"); - /** - * {@code "www-authenticate"} - */ - public static final AsciiString WWW_AUTHENTICATE = new AsciiString("www-authenticate"); - /** - * {@code "keep-alive"} - * @deprecated use {@link #CONNECTION} - */ - @Deprecated - public static final AsciiString KEEP_ALIVE = new AsciiString("keep-alive"); - /** - * {@code "proxy-connection"} - * @deprecated use {@link #CONNECTION} - */ - @Deprecated - public static final AsciiString PROXY_CONNECTION = new AsciiString("proxy-connection"); + public static final String WWW_AUTHENTICATE = "WWW-Authenticate"; private Names() { } } /** + * @deprecated Use {@link HttpHeaderValues} instead. + * * Standard HTTP header values. */ + @Deprecated public static final class Values { /** * {@code "application/x-www-form-urlencoded"} */ - public static final AsciiString APPLICATION_X_WWW_FORM_URLENCODED = - new AsciiString("application/x-www-form-urlencoded"); - /** - * {@code "application/octet-stream"} - */ - public static final AsciiString APPLICATION_OCTET_STREAM = new AsciiString("application/octet-stream"); - /** - * {@code "text/plain"} - */ - public static final AsciiString TEXT_PLAIN = new AsciiString("text/plain"); + public static final String APPLICATION_X_WWW_FORM_URLENCODED = + "application/x-www-form-urlencoded"; /** * {@code "base64"} */ @@ -538,10 +520,6 @@ public abstract class HttpHeaders implements Iterable> * {@code "multipart/form-data"} */ public static final String MULTIPART_FORM_DATA = "multipart/form-data"; - /** - * {@code "multipart/mixed"} - */ - public static final AsciiString MULTIPART_MIXED = new AsciiString("multipart/mixed"); /** * {@code "must-revalidate"} */ @@ -597,57 +575,28 @@ public abstract class HttpHeaders implements Iterable> /** * {@code "WebSocket"} */ - public static final AsciiString WEBSOCKET = new AsciiString("WebSocket"); - /** - * {@code "name"} - * See {@link Names#CONTENT_DISPOSITION} - */ - public static final AsciiString NAME = new AsciiString("name"); - /** - * {@code "filename"} - * See {@link Names#CONTENT_DISPOSITION} - */ - public static final AsciiString FILENAME = new AsciiString("filename"); - /** - * {@code "form-data"} - * See {@link Names#CONTENT_DISPOSITION} - */ - public static final AsciiString FORM_DATA = new AsciiString("form-data"); - /** - * {@code "attachment"} - * See {@link Names#CONTENT_DISPOSITION} - */ - public static final AsciiString ATTACHMENT = new AsciiString("attachment"); - /** - * {@code "file"} - * See {@link Names#CONTENT_DISPOSITION} - */ - public static final AsciiString FILE = new AsciiString("file"); + public static final String WEBSOCKET = "WebSocket"; private Values() { } } /** + * @deprecated Use {@link HttpHeaderUtil#isKeepAlive(HttpMessage)} instead. + * * Returns {@code true} if and only if the connection can remain open and * thus 'kept alive'. This methods respects the value of the * {@code "Connection"} header first and then the return value of * {@link HttpVersion#isKeepAliveDefault()}. */ + @Deprecated public static boolean isKeepAlive(HttpMessage message) { - String connection = message.headers().get(CONNECTION_ENTITY); - if (connection != null && AsciiString.equalsIgnoreCase(CLOSE_ENTITY, connection)) { - return false; - } - - if (message.protocolVersion().isKeepAliveDefault()) { - return !AsciiString.equalsIgnoreCase(CLOSE_ENTITY, connection); - } else { - return AsciiString.equalsIgnoreCase(KEEP_ALIVE_ENTITY, connection); - } + return HttpHeaderUtil.isKeepAlive(message); } /** + * @deprecated Use {@link HttpHeaderUtil#setKeepAlive(HttpMessage, boolean)} instead. + * * Sets the value of the {@code "Connection"} header depending on the * protocol version of the specified message. This getMethod sets or removes * the {@code "Connection"} header depending on what the default keep alive @@ -666,49 +615,48 @@ public abstract class HttpHeaders implements Iterable> * * */ + @Deprecated public static void setKeepAlive(HttpMessage message, boolean keepAlive) { - HttpHeaders h = message.headers(); - if (message.protocolVersion().isKeepAliveDefault()) { - if (keepAlive) { - h.remove(CONNECTION_ENTITY); - } else { - h.set(CONNECTION_ENTITY, CLOSE_ENTITY); - } - } else { - if (keepAlive) { - h.set(CONNECTION_ENTITY, KEEP_ALIVE_ENTITY); - } else { - h.remove(CONNECTION_ENTITY); - } - } + HttpHeaderUtil.setKeepAlive(message, keepAlive); } /** + * @deprecated Use {@link #get(CharSequence)} instead. + * * @see {@link #getHeader(HttpMessage, CharSequence)} */ + @Deprecated public static String getHeader(HttpMessage message, String name) { return message.headers().get(name); } /** + * @deprecated Use {@link #get(CharSequence)} instead. + * * Returns the header value with the specified header name. If there are * more than one header value for the specified header name, the first * value is returned. * * @return the header value or {@code null} if there is no such header */ + @Deprecated public static String getHeader(HttpMessage message, CharSequence name) { return message.headers().get(name); } /** + * @deprecated Use {@link #get(CharSequence, String)} instead. + * * @see {@link #getHeader(HttpMessage, CharSequence, String)} */ + @Deprecated public static String getHeader(HttpMessage message, String name, String defaultValue) { - return getHeader(message, (CharSequence) name, defaultValue); + return message.headers().get(name, defaultValue); } /** + * @deprecated Use {@link #get(CharSequence, String)} instead. + * * Returns the header value with the specified header name. If there are * more than one header value for the specified header name, the first * value is returned. @@ -716,22 +664,24 @@ public abstract class HttpHeaders implements Iterable> * @return the header value or the {@code defaultValue} if there is no such * header */ + @Deprecated public static String getHeader(HttpMessage message, CharSequence name, String defaultValue) { - String value = message.headers().get(name); - if (value == null) { - return defaultValue; - } - return value; + return message.headers().get(name, defaultValue); } /** + * @deprecated Use {@link #set(CharSequence, Object)} instead. + * * @see {@link #setHeader(HttpMessage, CharSequence, Object)} */ + @Deprecated public static void setHeader(HttpMessage message, String name, Object value) { message.headers().set(name, value); } /** + * @deprecated Use {@link #set(CharSequence, Object)} instead. + * * Sets a new header with the specified name and value. If there is an * existing header with the same name, the existing header is removed. * If the specified value is not a {@link String}, it is converted into a @@ -739,19 +689,24 @@ public abstract class HttpHeaders implements Iterable> * and {@link Calendar} which are formatted to the date format defined in * RFC2616. */ + @Deprecated public static void setHeader(HttpMessage message, CharSequence name, Object value) { message.headers().set(name, value); } /** + * @deprecated Use {@link #set(CharSequence, Iterable)} instead. * * @see {@link #setHeader(HttpMessage, CharSequence, Iterable)} */ + @Deprecated public static void setHeader(HttpMessage message, String name, Iterable values) { message.headers().set(name, values); } /** + * @deprecated Use {@link #set(CharSequence, Iterable)} instead. + * * Sets a new header with the specified name and values. If there is an * existing header with the same name, the existing header is removed. * This getMethod can be represented approximately as the following code: @@ -765,57 +720,78 @@ public abstract class HttpHeaders implements Iterable> * } * */ + @Deprecated public static void setHeader(HttpMessage message, CharSequence name, Iterable values) { message.headers().set(name, values); } /** + * @deprecated Use {@link #add(CharSequence, Object)} instead. + * * @see {@link #addHeader(HttpMessage, CharSequence, Object)} */ + @Deprecated public static void addHeader(HttpMessage message, String name, Object value) { message.headers().add(name, value); } /** + * @deprecated Use {@link #add(CharSequence, Object)} instead. + * * Adds a new header with the specified name and value. * If the specified value is not a {@link String}, it is converted into a * {@link String} by {@link Object#toString()}, except for {@link Date} * and {@link Calendar} which are formatted to the date format defined in * RFC2616. */ + @Deprecated public static void addHeader(HttpMessage message, CharSequence name, Object value) { message.headers().add(name, value); } /** + * @deprecated Use {@link #remove(CharSequence)} instead. + * * @see {@link #removeHeader(HttpMessage, CharSequence)} */ + @Deprecated public static void removeHeader(HttpMessage message, String name) { message.headers().remove(name); } /** + * @deprecated Use {@link #remove(CharSequence)} instead. + * * Removes the header with the specified name. */ + @Deprecated public static void removeHeader(HttpMessage message, CharSequence name) { message.headers().remove(name); } /** + * @deprecated Use {@link #clear()} instead. + * * Removes all headers from the specified message. */ + @Deprecated public static void clearHeaders(HttpMessage message) { message.headers().clear(); } /** + * @deprecated Use {@link #getInt(CharSequence)} instead. + * * @see {@link #getIntHeader(HttpMessage, CharSequence)} */ + @Deprecated public static int getIntHeader(HttpMessage message, String name) { return getIntHeader(message, (CharSequence) name); } /** + * @deprecated Use {@link #getInt(CharSequence)} instead. + * * Returns the integer header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. @@ -824,8 +800,9 @@ public abstract class HttpHeaders implements Iterable> * @throws NumberFormatException * if there is no such header or the header value is not a number */ + @Deprecated public static int getIntHeader(HttpMessage message, CharSequence name) { - String value = getHeader(message, name); + String value = message.headers().get(name); if (value == null) { throw new NumberFormatException("header not found: " + name); } @@ -833,13 +810,18 @@ public abstract class HttpHeaders implements Iterable> } /** + * @deprecated Use {@link #getInt(CharSequence, int)} instead. + * * @see {@link #getIntHeader(HttpMessage, CharSequence, int)} */ + @Deprecated public static int getIntHeader(HttpMessage message, String name, int defaultValue) { - return getIntHeader(message, (CharSequence) name, defaultValue); + return message.headers().getInt(name, defaultValue); } /** + * @deprecated Use {@link #getInt(CharSequence, int)} instead. + * * Returns the integer header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. @@ -847,72 +829,86 @@ public abstract class HttpHeaders implements Iterable> * @return the header value or the {@code defaultValue} if there is no such * header or the header value is not a number */ + @Deprecated public static int getIntHeader(HttpMessage message, CharSequence name, int defaultValue) { - String value = getHeader(message, name); - if (value == null) { - return defaultValue; - } - - try { - return Integer.parseInt(value); - } catch (NumberFormatException ignored) { - return defaultValue; - } + return message.headers().getInt(name, defaultValue); } /** + * @deprecated Use {@link #setInt(CharSequence, int)} instead. + * * @see {@link #setIntHeader(HttpMessage, CharSequence, int)} */ + @Deprecated public static void setIntHeader(HttpMessage message, String name, int value) { - message.headers().set(name, value); + message.headers().setInt(name, value); } /** + * @deprecated Use {@link #setInt(CharSequence, int)} instead. + * * Sets a new integer header with the specified name and value. If there * is an existing header with the same name, the existing header is removed. */ + @Deprecated public static void setIntHeader(HttpMessage message, CharSequence name, int value) { - message.headers().set(name, value); + message.headers().setInt(name, value); } /** + * @deprecated Use {@link #set(CharSequence, Iterable)} instead. + * * @see {@link #setIntHeader(HttpMessage, CharSequence, Iterable)} */ + @Deprecated public static void setIntHeader(HttpMessage message, String name, Iterable values) { message.headers().set(name, values); } /** + * @deprecated Use {@link #set(CharSequence, Iterable)} instead. + * * Sets a new integer header with the specified name and values. If there * is an existing header with the same name, the existing header is removed. */ + @Deprecated public static void setIntHeader(HttpMessage message, CharSequence name, Iterable values) { message.headers().set(name, values); } /** + * @deprecated Use {@link #add(CharSequence, Iterable)} instead. * * @see {@link #addIntHeader(HttpMessage, CharSequence, int)} */ + @Deprecated public static void addIntHeader(HttpMessage message, String name, int value) { message.headers().add(name, value); } /** + * @deprecated Use {@link #addInt(CharSequence, int)} instead. + * * Adds a new integer header with the specified name and value. */ + @Deprecated public static void addIntHeader(HttpMessage message, CharSequence name, int value) { - message.headers().add(name, value); + message.headers().addInt(name, value); } /** + * @deprecated Use {@link #getDate(CharSequence)} instead. + * * @see {@link #getDateHeader(HttpMessage, CharSequence)} */ + @Deprecated public static Date getDateHeader(HttpMessage message, String name) throws ParseException { return getDateHeader(message, (CharSequence) name); } /** + * @deprecated Use {@link #getDate(CharSequence)} instead. + * * Returns the date header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. @@ -921,8 +917,9 @@ public abstract class HttpHeaders implements Iterable> * @throws ParseException * if there is no such header or the header value is not a formatted date */ + @Deprecated public static Date getDateHeader(HttpMessage message, CharSequence name) throws ParseException { - String value = getHeader(message, name); + String value = message.headers().get(name); if (value == null) { throw new ParseException("header not found: " + name, 0); } @@ -930,13 +927,18 @@ public abstract class HttpHeaders implements Iterable> } /** + * @deprecated Use {@link #getDate(CharSequence, Date)} instead. + * * @see {@link #getDateHeader(HttpMessage, CharSequence, Date)} */ + @Deprecated public static Date getDateHeader(HttpMessage message, String name, Date defaultValue) { - return getDateHeader(message, (CharSequence) name, defaultValue); + return message.headers().getDate(name, defaultValue); } /** + * @deprecated Use {@link #getDate(CharSequence, Date)} instead. + * * Returns the date header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. @@ -944,32 +946,30 @@ public abstract class HttpHeaders implements Iterable> * @return the header value or the {@code defaultValue} if there is no such * header or the header value is not a formatted date */ + @Deprecated public static Date getDateHeader(HttpMessage message, CharSequence name, Date defaultValue) { - final String value = getHeader(message, name); - if (value == null) { - return defaultValue; - } - - try { - return HttpHeaderDateFormat.get().parse(value); - } catch (ParseException ignored) { - return defaultValue; - } + return message.headers().getDate(name, defaultValue); } /** + * @deprecated Use {@link #set(CharSequence, Object)} instead. + * * @see {@link #setDateHeader(HttpMessage, CharSequence, Date)} */ + @Deprecated public static void setDateHeader(HttpMessage message, String name, Date value) { setDateHeader(message, (CharSequence) name, value); } /** + * @deprecated Use {@link #set(CharSequence, Object)} instead. + * * Sets a new date header with the specified name and value. If there * is an existing header with the same name, the existing header is removed. * The specified value is formatted as defined in * RFC2616 */ + @Deprecated public static void setDateHeader(HttpMessage message, CharSequence name, Date value) { if (value != null) { message.headers().set(name, HttpHeaderDateFormat.get().format(value)); @@ -979,39 +979,53 @@ public abstract class HttpHeaders implements Iterable> } /** + * @deprecated Use {@link #set(CharSequence, Iterable)} instead. + * * @see {@link #setDateHeader(HttpMessage, CharSequence, Iterable)} */ + @Deprecated public static void setDateHeader(HttpMessage message, String name, Iterable values) { message.headers().set(name, values); } /** + * @deprecated Use {@link #set(CharSequence, Iterable)} instead. + * * Sets a new date header with the specified name and values. If there * is an existing header with the same name, the existing header is removed. * The specified values are formatted as defined in * RFC2616 */ + @Deprecated public static void setDateHeader(HttpMessage message, CharSequence name, Iterable values) { message.headers().set(name, values); } /** + * @deprecated Use {@link #add(CharSequence, Object)} instead. + * * @see {@link #addDateHeader(HttpMessage, CharSequence, Date)} */ + @Deprecated public static void addDateHeader(HttpMessage message, String name, Date value) { message.headers().add(name, value); } /** + * @deprecated Use {@link #add(CharSequence, Object)} instead. + * * Adds a new date header with the specified name and value. The specified * value is formatted as defined in * RFC2616 */ + @Deprecated public static void addDateHeader(HttpMessage message, CharSequence name, Date value) { message.headers().add(name, value); } /** + * @deprecated Use {@link HttpHeaderUtil#getContentLength(HttpMessage)} instead. + * * Returns the length of the content. Please note that this value is * not retrieved from {@link HttpContent#content()} but from the * {@code "Content-Length"} header, and thus they are independent from each @@ -1023,24 +1037,14 @@ public abstract class HttpHeaders implements Iterable> * if the message does not have the {@code "Content-Length"} header * or its value is not a number */ + @Deprecated public static long getContentLength(HttpMessage message) { - String value = getHeader(message, CONTENT_LENGTH_ENTITY); - if (value != null) { - return Long.parseLong(value); - } - - // We know the content length if it's a Web Socket message even if - // Content-Length header is missing. - long webSocketContentLength = getWebSocketContentLength(message); - if (webSocketContentLength >= 0) { - return webSocketContentLength; - } - - // Otherwise we don't. - throw new NumberFormatException("header not found: " + Names.CONTENT_LENGTH); + return HttpHeaderUtil.getContentLength(message); } /** + * @deprecated Use {@link HttpHeaderUtil#getContentLength(HttpMessage, long)} instead. + * * Returns the length of the content. Please note that this value is * not retrieved from {@link HttpContent#content()} but from the * {@code "Content-Length"} header, and thus they are independent from each @@ -1050,208 +1054,167 @@ public abstract class HttpHeaders implements Iterable> * not have the {@code "Content-Length"} header or its value is not * a number */ + @Deprecated public static long getContentLength(HttpMessage message, long defaultValue) { - String contentLength = message.headers().get(CONTENT_LENGTH_ENTITY); - if (contentLength != null) { - try { - return Long.parseLong(contentLength); - } catch (NumberFormatException ignored) { - return defaultValue; - } - } - - // We know the content length if it's a Web Socket message even if - // Content-Length header is missing. - long webSocketContentLength = getWebSocketContentLength(message); - if (webSocketContentLength >= 0) { - return webSocketContentLength; - } - - // Otherwise we don't. - return defaultValue; + return HttpHeaderUtil.getContentLength(message, defaultValue); } /** - * Returns the content length of the specified web socket message. If the - * specified message is not a web socket message, {@code -1} is returned. - */ - private static int getWebSocketContentLength(HttpMessage message) { - // WebSockset messages have constant content-lengths. - HttpHeaders h = message.headers(); - if (message instanceof HttpRequest) { - HttpRequest req = (HttpRequest) message; - if (HttpMethod.GET.equals(req.method()) && - h.contains(SEC_WEBSOCKET_KEY1_ENTITY) && - h.contains(SEC_WEBSOCKET_KEY2_ENTITY)) { - return 8; - } - } else if (message instanceof HttpResponse) { - HttpResponse res = (HttpResponse) message; - if (res.status().code() == 101 && - h.contains(SEC_WEBSOCKET_ORIGIN_ENTITY) && - h.contains(SEC_WEBSOCKET_LOCATION_ENTITY)) { - return 16; - } - } - - // Not a web socket message - return -1; - } - - /** - * Sets the {@code "Content-Length"} header. + * @deprecated Use {@link HttpHeaderUtil#setContentLength(HttpMessage, long)} instead. */ + @Deprecated public static void setContentLength(HttpMessage message, long length) { - message.headers().set(CONTENT_LENGTH_ENTITY, length); + HttpHeaderUtil.setContentLength(message, length); } /** + * @deprecated Use {@link #get(CharSequence)} instead. + * * Returns the value of the {@code "Host"} header. */ + @Deprecated public static String getHost(HttpMessage message) { - return message.headers().get(HOST_ENTITY); + return message.headers().get(HttpHeaderNames.HOST); } /** + * @deprecated Use {@link #get(CharSequence, String)} instead. + * * Returns the value of the {@code "Host"} header. If there is no such * header, the {@code defaultValue} is returned. */ + @Deprecated public static String getHost(HttpMessage message, String defaultValue) { - return getHeader(message, HOST_ENTITY, defaultValue); + return message.headers().get(HttpHeaderNames.HOST, defaultValue); } /** + * @deprecated Use {@link #set(CharSequence, Object)} instead. + * * @see {@link #setHost(HttpMessage, CharSequence)} */ + @Deprecated public static void setHost(HttpMessage message, String value) { - message.headers().set(HOST_ENTITY, value); + message.headers().set(HttpHeaderNames.HOST, value); } /** + * @deprecated Use {@link #set(CharSequence, Object)} instead. + * * Sets the {@code "Host"} header. */ + @Deprecated public static void setHost(HttpMessage message, CharSequence value) { - message.headers().set(HOST_ENTITY, value); + message.headers().set(HttpHeaderNames.HOST, value); } /** + * @deprecated Use {@link #getDate(CharSequence)} instead. + * * Returns the value of the {@code "Date"} header. * * @throws ParseException * if there is no such header or the header value is not a formatted date */ + @Deprecated public static Date getDate(HttpMessage message) throws ParseException { - return getDateHeader(message, DATE_ENTITY); + return getDateHeader(message, HttpHeaderNames.DATE); } /** + * @deprecated Use {@link #getDate(CharSequence, Date)} instead. + * * Returns the value of the {@code "Date"} header. If there is no such * header or the header is not a formatted date, the {@code defaultValue} * is returned. */ + @Deprecated public static Date getDate(HttpMessage message, Date defaultValue) { - return getDateHeader(message, DATE_ENTITY, defaultValue); + return message.headers().getDate(HttpHeaderNames.DATE, defaultValue); } /** + * @deprecated Use {@link #set(CharSequence, Object)} instead. + * * Sets the {@code "Date"} header. */ + @Deprecated public static void setDate(HttpMessage message, Date value) { - if (value != null) { - message.headers().set(DATE_ENTITY, HttpHeaderDateFormat.get().format(value)); - } else { - message.headers().set(DATE_ENTITY, null); - } + message.headers().set(HttpHeaderNames.DATE, value); } /** + * @deprecated Use {@link HttpHeaderUtil#is100ContinueExpected(HttpMessage)} instead. + * * Returns {@code true} if and only if the specified message contains the * {@code "Expect: 100-continue"} header. */ + @Deprecated public static boolean is100ContinueExpected(HttpMessage message) { - // Expect: 100-continue is for requests only. - if (!(message instanceof HttpRequest)) { - return false; - } - - // It works only on HTTP/1.1 or later. - if (message.protocolVersion().compareTo(HttpVersion.HTTP_1_1) < 0) { - return false; - } - - // In most cases, there will be one or zero 'Expect' header. - String value = message.headers().get(EXPECT_ENTITY); - if (value == null) { - return false; - } - if (AsciiString.equalsIgnoreCase(CONTINUE_ENTITY, value)) { - return true; - } - - // Multiple 'Expect' headers. Search through them. - return message.headers().contains(EXPECT_ENTITY, CONTINUE_ENTITY, true); + return HttpHeaderUtil.is100ContinueExpected(message); } /** + * @deprecated Use {@link HttpHeaderUtil#set100ContinueExpected(HttpMessage, boolean)} instead. + * * Sets the {@code "Expect: 100-continue"} header to the specified message. * If there is any existing {@code "Expect"} header, they are replaced with * the new one. */ + @Deprecated public static void set100ContinueExpected(HttpMessage message) { - set100ContinueExpected(message, true); + HttpHeaderUtil.set100ContinueExpected(message, true); } /** + * @deprecated Use {@link HttpHeaderUtil#set100ContinueExpected(HttpMessage, boolean)} instead. + * * Sets or removes the {@code "Expect: 100-continue"} header to / from the * specified message. If the specified {@code value} is {@code true}, * the {@code "Expect: 100-continue"} header is set and all other previous * {@code "Expect"} headers are removed. Otherwise, all {@code "Expect"} * headers are removed completely. */ + @Deprecated public static void set100ContinueExpected(HttpMessage message, boolean set) { - if (set) { - message.headers().set(EXPECT_ENTITY, CONTINUE_ENTITY); - } else { - message.headers().remove(EXPECT_ENTITY); - } + HttpHeaderUtil.set100ContinueExpected(message, set); } /** + * @deprecated Use {@link HttpHeaderUtil#isTransferEncodingChunked(HttpMessage)} instead. + * * Checks to see if the transfer encoding in a specified {@link HttpMessage} is chunked * * @param message The message to check * @return True if transfer encoding is chunked, otherwise false */ + @Deprecated public static boolean isTransferEncodingChunked(HttpMessage message) { - return message.headers().contains(TRANSFER_ENCODING_ENTITY, CHUNKED_ENTITY, true); + return HttpHeaderUtil.isTransferEncodingChunked(message); } + /** + * @deprecated Use {@link HttpHeaderUtil#setTransferEncodingChunked(HttpMessage, boolean)} instead. + */ + @Deprecated public static void removeTransferEncodingChunked(HttpMessage m) { - List values = m.headers().getAll(TRANSFER_ENCODING_ENTITY); - if (values.isEmpty()) { - return; - } - Iterator valuesIt = values.iterator(); - while (valuesIt.hasNext()) { - String value = valuesIt.next(); - if (AsciiString.equalsIgnoreCase(value, CHUNKED_ENTITY)) { - valuesIt.remove(); - } - } - if (values.isEmpty()) { - m.headers().remove(TRANSFER_ENCODING_ENTITY); - } else { - m.headers().set(TRANSFER_ENCODING_ENTITY, values); - } + HttpHeaderUtil.setTransferEncodingChunked(m, false); } + /** + * @deprecated Use {@link HttpHeaderUtil#setTransferEncodingChunked(HttpMessage, boolean)} instead. + */ + @Deprecated public static void setTransferEncodingChunked(HttpMessage m) { - addHeader(m, TRANSFER_ENCODING_ENTITY, CHUNKED_ENTITY); - removeHeader(m, CONTENT_LENGTH_ENTITY); + HttpHeaderUtil.setTransferEncodingChunked(m, true); } + /** + * @deprecated Use {@link HttpHeaderUtil#isContentLengthSet(HttpMessage)} instead. + */ + @Deprecated public static boolean isContentLengthSet(HttpMessage m) { - return m.headers().contains(CONTENT_LENGTH_ENTITY); + return HttpHeaderUtil.isContentLengthSet(m); } /** @@ -1297,9 +1260,12 @@ public abstract class HttpHeaders implements Iterable> } /** + * @deprecated Use {@link AsciiString} instead. + * * Create a new {@link CharSequence} which is optimized for reuse as {@link HttpHeaders} name or value. * So if you have a Header name or value that you want to reuse you should make use of this. */ + @Deprecated public static CharSequence newEntity(String name) { if (name == null) { throw new NullPointerException("name"); @@ -1325,6 +1291,63 @@ public abstract class HttpHeaders implements Iterable> return get(name.toString()); } + /** + * Returns the value of a header with the specified name. If there are + * more than one values for the specified name, the first value is returned. + * + * @param name The name of the header to search + * @return The first header value or {@code defaultValue} if there is no such header + */ + public String get(CharSequence name, String defaultValue) { + String value = get(name); + if (value == null) { + return defaultValue; + } + return value; + } + + /** + * Returns the integer value of a header with the specified name. If there are more than one values for the + * specified name, the first value is returned. + * + * @param name the name of the header to search + * @return the first header value if the header is found and its value is an integer. {@code null} if there's no + * such header or its value is not an integer. + */ + public abstract Integer getInt(CharSequence name); + + /** + * Returns the integer value of a header with the specified name. If there are more than one values for the + * specified name, the first value is returned. + * + * @param name the name of the header to search + * @param defaultValue the default value + * @return the first header value if the header is found and its value is an integer. {@code defaultValue} if + * there's no such header or its value is not an integer. + */ + public abstract int getInt(CharSequence name, int defaultValue); + + /** + * Returns the date value of a header with the specified name. If there are more than one values for the + * specified name, the first value is returned. + * + * @param name the name of the header to search + * @return the first header value if the header is found and its value is a date. {@code null} if there's no + * such header or its value is not a date. + */ + public abstract Date getDate(CharSequence name); + + /** + * Returns the date value of a header with the specified name. If there are more than one values for the + * specified name, the first value is returned. + * + * @param name the name of the header to search + * @param defaultValue the default value + * @return the first header value if the header is found and its value is a date. {@code defaultValue} if + * there's no such header or its value is not a date. + */ + public abstract Date getDate(CharSequence name, Date defaultValue); + /** * @see {@link #getAll(CharSequence)} */ @@ -1438,6 +1461,14 @@ public abstract class HttpHeaders implements Iterable> return this; } + /** + * Add the {@code name} to {@code value}. + * @param name The name to modify + * @param value The value + * @return {@code this} + */ + public abstract HttpHeaders addInt(CharSequence name, int value); + /** * @see {@link #set(CharSequence, Object)} */ @@ -1509,6 +1540,14 @@ public abstract class HttpHeaders implements Iterable> return this; } + /** + * Set the {@code name} to {@code value}. This will remove all previous values associated with {@code name}. + * @param name The name to modify + * @param value The value + * @return {@code this} + */ + public abstract HttpHeaders setInt(CharSequence name, int value); + /** * @see {@link #remove(CharSequence)} */ @@ -1542,7 +1581,7 @@ public abstract class HttpHeaders implements Iterable> for (String v: values) { if (ignoreCase) { - if (AsciiString.equalsIgnoreCase(v, value)) { + if (v.equalsIgnoreCase(value)) { return true; } } else { diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java index bbb073a449..a1f9fe3f99 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java @@ -58,7 +58,7 @@ public class HttpObjectAggregator HttpVersion.HTTP_1_1, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, Unpooled.EMPTY_BUFFER); static { - TOO_LARGE.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0); + TOO_LARGE.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0); } /** @@ -96,17 +96,17 @@ public class HttpObjectAggregator @Override protected boolean hasContentLength(HttpMessage start) throws Exception { - return HttpHeaders.isContentLengthSet(start); + return HttpHeaderUtil.isContentLengthSet(start); } @Override protected long contentLength(HttpMessage start) throws Exception { - return HttpHeaders.getContentLength(start); + return HttpHeaderUtil.getContentLength(start); } @Override protected Object newContinueResponse(HttpMessage start) throws Exception { - if (HttpHeaders.is100ContinueExpected(start)) { + if (HttpHeaderUtil.is100ContinueExpected(start)) { return CONTINUE; } else { return null; @@ -117,7 +117,7 @@ public class HttpObjectAggregator protected FullHttpMessage beginAggregation(HttpMessage start, ByteBuf content) throws Exception { assert !(start instanceof FullHttpMessage); - HttpHeaders.removeTransferEncodingChunked(start); + HttpHeaderUtil.setTransferEncodingChunked(start, false); AggregatedFullHttpMessage ret; if (start instanceof HttpRequest) { @@ -142,7 +142,7 @@ public class HttpObjectAggregator protected void finishAggregation(FullHttpMessage aggregated) throws Exception { // Set the 'Content-Length' header. aggregated.headers().set( - HttpHeaders.Names.CONTENT_LENGTH, + HttpHeaderNames.CONTENT_LENGTH, String.valueOf(aggregated.content().readableBytes())); } @@ -163,7 +163,7 @@ public class HttpObjectAggregator // If the client started to send data already, close because it's impossible to recover. // If keep-alive is off and 'Expect: 100-continue' is missing, no need to leave the connection open. if (oversized instanceof FullHttpMessage || - (!HttpHeaders.is100ContinueExpected(oversized) && !HttpHeaders.isKeepAlive(oversized))) { + (!HttpHeaderUtil.is100ContinueExpected(oversized) && !HttpHeaderUtil.isKeepAlive(oversized))) { future.addListener(ChannelFutureListener.CLOSE); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java index d717e40cfd..e2035245c0 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java @@ -20,7 +20,6 @@ import io.netty.buffer.ByteBufProcessor; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.TooLongFrameException; @@ -384,7 +383,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder { // Handle the last unfinished message. if (message != null) { - boolean chunked = HttpHeaders.isTransferEncodingChunked(message); + boolean chunked = HttpHeaderUtil.isTransferEncodingChunked(message); if (state() == State.READ_VARIABLE_LENGTH_CONTENT && !in.isReadable() && !chunked) { // End of connection. out.add(LastHttpContent.EMPTY_LAST_CONTENT); @@ -422,7 +421,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder { // - https://github.com/netty/netty/issues/222 if (code >= 100 && code < 200) { // One exception: Hixie 76 websocket handshake response - return !(code == 101 && !res.headers().contains(HttpHeaders.Names.SEC_WEBSOCKET_ACCEPT)); + return !(code == 101 && !res.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT)); } switch (code) { @@ -529,9 +528,9 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder { State nextState; if (isContentAlwaysEmpty(message)) { - HttpHeaders.removeTransferEncodingChunked(message); + HttpHeaderUtil.setTransferEncodingChunked(message, false); nextState = State.SKIP_CONTROL_CHARS; - } else if (HttpHeaders.isTransferEncodingChunked(message)) { + } else if (HttpHeaderUtil.isTransferEncodingChunked(message)) { nextState = State.READ_CHUNK_SIZE; } else if (contentLength() >= 0) { nextState = State.READ_FIXED_LENGTH_CONTENT; @@ -543,7 +542,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder { private long contentLength() { if (contentLength == Long.MIN_VALUE) { - contentLength = HttpHeaders.getContentLength(message, -1); + contentLength = HttpHeaderUtil.getContentLength(message, -1); } return contentLength; } @@ -571,9 +570,9 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder { } else { splitHeader(line); CharSequence headerName = name; - if (!AsciiString.equalsIgnoreCase(headerName, HttpHeaders.Names.CONTENT_LENGTH) && - !AsciiString.equalsIgnoreCase(headerName, HttpHeaders.Names.TRANSFER_ENCODING) && - !AsciiString.equalsIgnoreCase(headerName, HttpHeaders.Names.TRAILER)) { + if (!HttpHeaderNames.CONTENT_LENGTH.equalsIgnoreCase(headerName) && + !HttpHeaderNames.TRANSFER_ENCODING.equalsIgnoreCase(headerName) && + !HttpHeaderNames.TRAILER.equalsIgnoreCase(headerName)) { trailer.trailingHeaders().add(headerName, value); } lastHeader = name; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java index 327992b0de..28a55929e6 100755 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java @@ -72,7 +72,7 @@ public abstract class HttpObjectEncoder extends MessageTo encodeInitialLine(buf, m); HttpHeaders.encode(m.headers(), buf); buf.writeBytes(CRLF); - state = HttpHeaders.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK; + state = HttpHeaderUtil.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK; } // Bypass the encoder in case of an empty buffer, so that the following idiom works: diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsConfig.java b/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsConfig.java index 7ceff7a54f..fe1c71ee12 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsConfig.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsConfig.java @@ -16,8 +16,8 @@ package io.netty.handler.codec.http.cors; import io.netty.handler.codec.http.DefaultHttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpMethod; import io.netty.util.internal.StringUtil; @@ -510,8 +510,8 @@ public final class CorsConfig { */ public CorsConfig build() { if (preflightHeaders.isEmpty() && !noPreflightHeaders) { - preflightHeaders.put(Names.DATE, new DateValueGenerator()); - preflightHeaders.put(Names.CONTENT_LENGTH, new ConstantValueGenerator("0")); + preflightHeaders.put(HttpHeaderNames.DATE, new DateValueGenerator()); + preflightHeaders.put(HttpHeaderNames.CONTENT_LENGTH, new ConstantValueGenerator("0")); } return new CorsConfig(this); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java index b517ce4930..76d050081b 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java @@ -20,16 +20,16 @@ import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; -import static io.netty.handler.codec.http.HttpHeaders.Names.*; import static io.netty.handler.codec.http.HttpMethod.*; import static io.netty.handler.codec.http.HttpResponseStatus.*; -import static io.netty.util.ReferenceCountUtil.release; +import static io.netty.util.ReferenceCountUtil.*; /** * Handles Cross Origin Resource Sharing (CORS) requests. @@ -88,7 +88,7 @@ public class CorsHandler extends ChannelDuplexHandler { } private boolean setOrigin(final HttpResponse response) { - final CharSequence origin = request.headers().get(ORIGIN); + final String origin = request.headers().get(HttpHeaderNames.ORIGIN); if (origin != null) { if ("null".equals(origin) && config.isNullOriginAllowed()) { setAnyOrigin(response); @@ -118,7 +118,7 @@ public class CorsHandler extends ChannelDuplexHandler { return true; } - final CharSequence origin = request.headers().get(ORIGIN); + final String origin = request.headers().get(HttpHeaderNames.ORIGIN); if (origin == null) { // Not a CORS request so we cannot validate it. It may be a non CORS request. return true; @@ -132,50 +132,50 @@ public class CorsHandler extends ChannelDuplexHandler { } private void echoRequestOrigin(final HttpResponse response) { - setOrigin(response, request.headers().get(ORIGIN)); + setOrigin(response, request.headers().get(HttpHeaderNames.ORIGIN)); } private static void setVaryHeader(final HttpResponse response) { - response.headers().set(VARY, ORIGIN); + response.headers().set(HttpHeaderNames.VARY, HttpHeaderNames.ORIGIN); } private static void setAnyOrigin(final HttpResponse response) { setOrigin(response, "*"); } - private static void setOrigin(final HttpResponse response, final CharSequence origin) { - response.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, origin); + private static void setOrigin(final HttpResponse response, final String origin) { + response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin); } private void setAllowCredentials(final HttpResponse response) { if (config.isCredentialsAllowed()) { - response.headers().set(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); + response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); } } private static boolean isPreflightRequest(final HttpRequest request) { final HttpHeaders headers = request.headers(); return request.method().equals(OPTIONS) && - headers.contains(ORIGIN) && - headers.contains(ACCESS_CONTROL_REQUEST_METHOD); + headers.contains(HttpHeaderNames.ORIGIN) && + headers.contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD); } private void setExposeHeaders(final HttpResponse response) { if (!config.exposedHeaders().isEmpty()) { - response.headers().set(ACCESS_CONTROL_EXPOSE_HEADERS, config.exposedHeaders()); + response.headers().set(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS, config.exposedHeaders()); } } private void setAllowMethods(final HttpResponse response) { - response.headers().set(ACCESS_CONTROL_ALLOW_METHODS, config.allowedRequestMethods()); + response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, config.allowedRequestMethods()); } private void setAllowHeaders(final HttpResponse response) { - response.headers().set(ACCESS_CONTROL_ALLOW_HEADERS, config.allowedRequestHeaders()); + response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, config.allowedRequestHeaders()); } private void setMaxAge(final HttpResponse response) { - response.headers().set(ACCESS_CONTROL_MAX_AGE, config.maxAge()); + response.headers().set(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, config.maxAge()); } @Override diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/DiskFileUpload.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/DiskFileUpload.java index c674272932..5158702d05 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/DiskFileUpload.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/DiskFileUpload.java @@ -17,7 +17,8 @@ package io.netty.handler.codec.http.multipart; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelException; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import java.io.File; import java.io.IOException; @@ -132,12 +133,12 @@ public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload { // Should not occur. } - return HttpPostBodyUtil.CONTENT_DISPOSITION + ": " + - HttpPostBodyUtil.FORM_DATA + "; " + HttpPostBodyUtil.NAME + "=\"" + getName() + - "\"; " + HttpPostBodyUtil.FILENAME + "=\"" + filename + "\"\r\n" + - HttpHeaders.Names.CONTENT_TYPE + ": " + contentType + - (getCharset() != null? "; " + HttpHeaders.Values.CHARSET + '=' + getCharset() + "\r\n" : "\r\n") + - HttpHeaders.Names.CONTENT_LENGTH + ": " + length() + "\r\n" + + return HttpHeaderNames.CONTENT_DISPOSITION + ": " + + HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + getName() + + "\"; " + HttpHeaderValues.FILENAME + "=\"" + filename + "\"\r\n" + + HttpHeaderNames.CONTENT_TYPE + ": " + contentType + + (getCharset() != null? "; " + HttpHeaderValues.CHARSET + '=' + getCharset() + "\r\n" : "\r\n") + + HttpHeaderNames.CONTENT_LENGTH + ": " + length() + "\r\n" + "Completed: " + isCompleted() + "\r\nIsInMemory: " + isInMemory() + "\r\nRealFile: " + (file != null ? file.getAbsolutePath() : "null") + " DefaultDeleteAfter: " + diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostBodyUtil.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostBodyUtil.java index 169b2d872c..be2976c482 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostBodyUtil.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostBodyUtil.java @@ -16,10 +16,6 @@ package io.netty.handler.codec.http.multipart; import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.http.HttpHeaders; -import io.netty.util.CharsetUtil; - -import java.nio.charset.Charset; /** * Shared Static object between HttpMessageDecoder, HttpPostRequestDecoder and HttpPostRequestEncoder @@ -27,54 +23,16 @@ import java.nio.charset.Charset; final class HttpPostBodyUtil { public static final int chunkSize = 8096; - /** - * HTTP content disposition header name. - */ - public static final String CONTENT_DISPOSITION = HttpHeaders.Names.CONTENT_DISPOSITION.toString(); - - public static final String NAME = HttpHeaders.Values.NAME.toString(); - - public static final String FILENAME = HttpHeaders.Values.FILENAME.toString(); - - /** - * Content-disposition value for form data. - */ - public static final String FORM_DATA = HttpHeaders.Values.FORM_DATA.toString(); - - /** - * Content-disposition value for file attachment. - */ - public static final String ATTACHMENT = HttpHeaders.Values.ATTACHMENT.toString(); - - /** - * Content-disposition value for file attachment. - */ - public static final String FILE = HttpHeaders.Values.FILE.toString(); - - /** - * HTTP content type body attribute for multiple uploads. - */ - public static final String MULTIPART_MIXED = HttpHeaders.Values.MULTIPART_MIXED.toString(); - - /** - * Charset for 8BIT - */ - public static final Charset ISO_8859_1 = CharsetUtil.ISO_8859_1; - - /** - * Charset for 7BIT - */ - public static final Charset US_ASCII = CharsetUtil.US_ASCII; /** * Default Content-Type in binary form */ - public static final String DEFAULT_BINARY_CONTENT_TYPE = HttpHeaders.Values.APPLICATION_OCTET_STREAM.toString(); + public static final String DEFAULT_BINARY_CONTENT_TYPE = "application/octet-stream"; /** * Default Content-Type in Text form */ - public static final String DEFAULT_TEXT_CONTENT_TYPE = HttpHeaders.Values.TEXT_PLAIN.toString(); + public static final String DEFAULT_TEXT_CONTENT_TYPE = "text/plain"; /** * Allowed mechanism for multipart diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java index 3c438fa28c..3d22694d13 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java @@ -16,10 +16,10 @@ package io.netty.handler.codec.http.multipart; import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.http.HttpConstants; import io.netty.handler.codec.http.HttpContent; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.LastHttpContent; import io.netty.handler.codec.http.multipart.HttpPostBodyUtil.SeekAheadNoBackArrayException; @@ -29,6 +29,7 @@ import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDec import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException; import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.MultiPartStatus; import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.NotEnoughDataDecoderException; +import io.netty.util.CharsetUtil; import io.netty.util.internal.StringUtil; import java.io.IOException; @@ -182,7 +183,7 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest this.factory = factory; // Fill default values - setMultipart(this.request.headers().get(HttpHeaders.Names.CONTENT_TYPE)); + setMultipart(this.request.headers().get(HttpHeaderNames.CONTENT_TYPE)); if (request instanceof HttpContent) { // Offer automatically if the given request is als type of HttpContent // See #1089 @@ -490,7 +491,7 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest case FIELD: { // Now get value according to Content-Type and Charset Charset localCharset = null; - Attribute charsetAttribute = currentFieldAttributes.get(HttpHeaders.Values.CHARSET); + Attribute charsetAttribute = currentFieldAttributes.get(HttpHeaderValues.CHARSET); if (charsetAttribute != null) { try { localCharset = Charset.forName(charsetAttribute.getValue()); @@ -498,7 +499,7 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest throw new ErrorDataDecoderException(e); } } - Attribute nameAttribute = currentFieldAttributes.get(HttpPostBodyUtil.NAME); + Attribute nameAttribute = currentFieldAttributes.get(HttpHeaderValues.NAME); if (currentAttribute == null) { try { currentAttribute = factory.createAttribute(request, @@ -662,13 +663,13 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest return null; } String[] contents = splitMultipartHeader(newline); - if (contents[0].equalsIgnoreCase(HttpPostBodyUtil.CONTENT_DISPOSITION)) { + if (HttpHeaderNames.CONTENT_DISPOSITION.equalsIgnoreCase(contents[0])) { boolean checkSecondArg; if (currentStatus == MultiPartStatus.DISPOSITION) { - checkSecondArg = contents[1].equalsIgnoreCase(HttpPostBodyUtil.FORM_DATA); + checkSecondArg = HttpHeaderValues.FORM_DATA.equalsIgnoreCase(contents[1]); } else { - checkSecondArg = contents[1].equalsIgnoreCase(HttpPostBodyUtil.ATTACHMENT) - || contents[1].equalsIgnoreCase(HttpPostBodyUtil.FILE); + checkSecondArg = HttpHeaderValues.ATTACHMENT.equalsIgnoreCase(contents[1]) + || HttpHeaderValues.FILE.equalsIgnoreCase(contents[1]); } if (checkSecondArg) { // read next values and store them in the map as Attribute @@ -680,7 +681,7 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest String value = values[1]; // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html - if (HttpPostBodyUtil.FILENAME.equals(name)) { + if (HttpHeaderValues.FILENAME.contentEquals(name)) { // filename value is quoted string so strip them value = value.substring(1, value.length() - 1); } else { @@ -696,31 +697,31 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest currentFieldAttributes.put(attribute.getName(), attribute); } } - } else if (AsciiString.equalsIgnoreCase(contents[0], HttpHeaders.Names.CONTENT_TRANSFER_ENCODING)) { + } else if (HttpHeaderNames.CONTENT_TRANSFER_ENCODING.equalsIgnoreCase(contents[0])) { Attribute attribute; try { - attribute = factory.createAttribute(request, HttpHeaders.Names.CONTENT_TRANSFER_ENCODING.toString(), + attribute = factory.createAttribute(request, HttpHeaderNames.CONTENT_TRANSFER_ENCODING.toString(), cleanString(contents[1])); } catch (NullPointerException e) { throw new ErrorDataDecoderException(e); } catch (IllegalArgumentException e) { throw new ErrorDataDecoderException(e); } - currentFieldAttributes.put(HttpHeaders.Names.CONTENT_TRANSFER_ENCODING, attribute); - } else if (AsciiString.equalsIgnoreCase(contents[0], HttpHeaders.Names.CONTENT_LENGTH)) { + currentFieldAttributes.put(HttpHeaderNames.CONTENT_TRANSFER_ENCODING, attribute); + } else if (HttpHeaderNames.CONTENT_LENGTH.equalsIgnoreCase(contents[0])) { Attribute attribute; try { - attribute = factory.createAttribute(request, HttpHeaders.Names.CONTENT_LENGTH.toString(), + attribute = factory.createAttribute(request, HttpHeaderNames.CONTENT_LENGTH.toString(), cleanString(contents[1])); } catch (NullPointerException e) { throw new ErrorDataDecoderException(e); } catch (IllegalArgumentException e) { throw new ErrorDataDecoderException(e); } - currentFieldAttributes.put(HttpHeaders.Names.CONTENT_LENGTH, attribute); - } else if (AsciiString.equalsIgnoreCase(contents[0], HttpHeaders.Names.CONTENT_TYPE)) { + currentFieldAttributes.put(HttpHeaderNames.CONTENT_LENGTH, attribute); + } else if (HttpHeaderNames.CONTENT_TYPE.equalsIgnoreCase(contents[0])) { // Take care of possible "multipart/mixed" - if (contents[1].equalsIgnoreCase(HttpPostBodyUtil.MULTIPART_MIXED)) { + if (HttpHeaderValues.MULTIPART_MIXED.equalsIgnoreCase(contents[1])) { if (currentStatus == MultiPartStatus.DISPOSITION) { String values = StringUtil.substringAfter(contents[2], '='); multipartMixedBoundary = "--" + values; @@ -731,18 +732,18 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest } } else { for (int i = 1; i < contents.length; i++) { - if (contents[i].toLowerCase().startsWith(HttpHeaders.Values.CHARSET)) { + if (contents[i].toLowerCase().startsWith(HttpHeaderValues.CHARSET.toString())) { String values = StringUtil.substringAfter(contents[i], '='); Attribute attribute; try { - attribute = factory.createAttribute(request, HttpHeaders.Values.CHARSET, + attribute = factory.createAttribute(request, HttpHeaderValues.CHARSET.toString(), cleanString(values)); } catch (NullPointerException e) { throw new ErrorDataDecoderException(e); } catch (IllegalArgumentException e) { throw new ErrorDataDecoderException(e); } - currentFieldAttributes.put(HttpHeaders.Values.CHARSET, attribute); + currentFieldAttributes.put(HttpHeaderValues.CHARSET, attribute); } else { Attribute attribute; try { @@ -762,7 +763,7 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest } } // Is it a FileUpload - Attribute filenameAttribute = currentFieldAttributes.get(HttpPostBodyUtil.FILENAME); + Attribute filenameAttribute = currentFieldAttributes.get(HttpHeaderValues.FILENAME); if (currentStatus == MultiPartStatus.DISPOSITION) { if (filenameAttribute != null) { // FileUpload @@ -799,7 +800,7 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest protected InterfaceHttpData getFileUpload(String delimiter) { // eventually restart from existing FileUpload // Now get value according to Content-Type and Charset - Attribute encoding = currentFieldAttributes.get(HttpHeaders.Names.CONTENT_TRANSFER_ENCODING); + Attribute encoding = currentFieldAttributes.get(HttpHeaderNames.CONTENT_TRANSFER_ENCODING); Charset localCharset = charset; // Default TransferEncodingMechanism mechanism = TransferEncodingMechanism.BIT7; @@ -811,9 +812,9 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest throw new ErrorDataDecoderException(e); } if (code.equals(HttpPostBodyUtil.TransferEncodingMechanism.BIT7.value())) { - localCharset = HttpPostBodyUtil.US_ASCII; + localCharset = CharsetUtil.US_ASCII; } else if (code.equals(HttpPostBodyUtil.TransferEncodingMechanism.BIT8.value())) { - localCharset = HttpPostBodyUtil.ISO_8859_1; + localCharset = CharsetUtil.ISO_8859_1; mechanism = TransferEncodingMechanism.BIT8; } else if (code.equals(HttpPostBodyUtil.TransferEncodingMechanism.BINARY.value())) { // no real charset, so let the default @@ -822,7 +823,7 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest throw new ErrorDataDecoderException("TransferEncoding Unknown: " + code); } } - Attribute charsetAttribute = currentFieldAttributes.get(HttpHeaders.Values.CHARSET); + Attribute charsetAttribute = currentFieldAttributes.get(HttpHeaderValues.CHARSET); if (charsetAttribute != null) { try { localCharset = Charset.forName(charsetAttribute.getValue()); @@ -831,13 +832,13 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest } } if (currentFileUpload == null) { - Attribute filenameAttribute = currentFieldAttributes.get(HttpPostBodyUtil.FILENAME); - Attribute nameAttribute = currentFieldAttributes.get(HttpPostBodyUtil.NAME); - Attribute contentTypeAttribute = currentFieldAttributes.get(HttpHeaders.Names.CONTENT_TYPE); + Attribute filenameAttribute = currentFieldAttributes.get(HttpHeaderValues.FILENAME); + Attribute nameAttribute = currentFieldAttributes.get(HttpHeaderValues.NAME); + Attribute contentTypeAttribute = currentFieldAttributes.get(HttpHeaderNames.CONTENT_TYPE); if (contentTypeAttribute == null) { throw new ErrorDataDecoderException("Content-Type is absent but required"); } - Attribute lengthAttribute = currentFieldAttributes.get(HttpHeaders.Names.CONTENT_LENGTH); + Attribute lengthAttribute = currentFieldAttributes.get(HttpHeaderNames.CONTENT_LENGTH); long size; try { size = lengthAttribute != null ? Long.parseLong(lengthAttribute.getValue()) : 0L; @@ -933,11 +934,11 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest * Mixed mode */ private void cleanMixedAttributes() { - currentFieldAttributes.remove(HttpHeaders.Values.CHARSET); - currentFieldAttributes.remove(HttpHeaders.Names.CONTENT_LENGTH); - currentFieldAttributes.remove(HttpHeaders.Names.CONTENT_TRANSFER_ENCODING); - currentFieldAttributes.remove(HttpHeaders.Names.CONTENT_TYPE); - currentFieldAttributes.remove(HttpPostBodyUtil.FILENAME); + currentFieldAttributes.remove(HttpHeaderValues.CHARSET); + currentFieldAttributes.remove(HttpHeaderNames.CONTENT_LENGTH); + currentFieldAttributes.remove(HttpHeaderNames.CONTENT_TRANSFER_ENCODING); + currentFieldAttributes.remove(HttpHeaderNames.CONTENT_TYPE); + currentFieldAttributes.remove(HttpHeaderValues.FILENAME); } /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java index fbaeecf155..608e5ad661 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java @@ -18,7 +18,8 @@ package io.netty.handler.codec.http.multipart; import io.netty.handler.codec.DecoderException; import io.netty.handler.codec.http.HttpConstants; import io.netty.handler.codec.http.HttpContent; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpRequest; import io.netty.util.internal.StringUtil; @@ -139,8 +140,8 @@ public class HttpPostRequestDecoder implements InterfaceHttpPostRequestDecoder { * @return True if the request is a Multipart request */ public static boolean isMultipart(HttpRequest request) { - if (request.headers().contains(HttpHeaders.Names.CONTENT_TYPE)) { - return getMultipartDataBoundary(request.headers().get(HttpHeaders.Names.CONTENT_TYPE)) != null; + if (request.headers().contains(HttpHeaderNames.CONTENT_TYPE)) { + return getMultipartDataBoundary(request.headers().get(HttpHeaderNames.CONTENT_TYPE)) != null; } else { return false; } @@ -155,15 +156,15 @@ public class HttpPostRequestDecoder implements InterfaceHttpPostRequestDecoder { // Check if Post using "multipart/form-data; boundary=--89421926422648 [; charset=xxx]" String[] headerContentType = splitHeaderContentType(contentType); if (headerContentType[0].toLowerCase().startsWith( - HttpHeaders.Values.MULTIPART_FORM_DATA)) { + HttpHeaderValues.MULTIPART_FORM_DATA.toString())) { int mrank; int crank; if (headerContentType[1].toLowerCase().startsWith( - HttpHeaders.Values.BOUNDARY)) { + HttpHeaderValues.BOUNDARY.toString())) { mrank = 1; crank = 2; } else if (headerContentType[2].toLowerCase().startsWith( - HttpHeaders.Values.BOUNDARY)) { + HttpHeaderValues.BOUNDARY.toString())) { mrank = 2; crank = 1; } else { @@ -181,7 +182,7 @@ public class HttpPostRequestDecoder implements InterfaceHttpPostRequestDecoder { } } if (headerContentType[crank].toLowerCase().startsWith( - HttpHeaders.Values.CHARSET)) { + HttpHeaderValues.CHARSET.toString())) { String charset = StringUtil.substringAfter(headerContentType[crank], '='); if (charset != null) { return new String[] {"--" + boundary, charset}; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java index f9269a524e..5f91859670 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java @@ -17,13 +17,15 @@ package io.netty.handler.codec.http.multipart; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.DefaultHttpContent; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpConstants; import io.netty.handler.codec.http.HttpContent; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderUtil; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; @@ -502,14 +504,14 @@ public class HttpPostRequestEncoder implements ChunkedInput { internal.addValue("--" + multipartDataBoundary + "\r\n"); // content-disposition: form-data; name="field1" Attribute attribute = (Attribute) data; - internal.addValue(HttpPostBodyUtil.CONTENT_DISPOSITION + ": " + HttpPostBodyUtil.FORM_DATA + "; " - + HttpPostBodyUtil.NAME + "=\"" + attribute.getName() + "\"\r\n"); + internal.addValue(HttpHeaderNames.CONTENT_DISPOSITION + ": " + HttpHeaderValues.FORM_DATA + "; " + + HttpHeaderValues.NAME + "=\"" + attribute.getName() + "\"\r\n"); Charset localcharset = attribute.getCharset(); if (localcharset != null) { // Content-Type: text/plain; charset=charset - internal.addValue(HttpHeaders.Names.CONTENT_TYPE + ": " + + internal.addValue(HttpHeaderNames.CONTENT_TYPE + ": " + HttpPostBodyUtil.DEFAULT_TEXT_CONTENT_TYPE + "; " + - HttpHeaders.Values.CHARSET + '=' + HttpHeaderValues.CHARSET + '=' + localcharset + "\r\n"); } // CRLF between body header and data @@ -584,20 +586,20 @@ public class HttpPostRequestEncoder implements ChunkedInput { replacement.append(multipartDataBoundary); replacement.append("\r\n"); - replacement.append(HttpPostBodyUtil.CONTENT_DISPOSITION); + replacement.append(HttpHeaderNames.CONTENT_DISPOSITION); replacement.append(": "); - replacement.append(HttpPostBodyUtil.FORM_DATA); + replacement.append(HttpHeaderValues.FORM_DATA); replacement.append("; "); - replacement.append(HttpPostBodyUtil.NAME); + replacement.append(HttpHeaderValues.NAME); replacement.append("=\""); replacement.append(fileUpload.getName()); replacement.append("\"\r\n"); - replacement.append(HttpHeaders.Names.CONTENT_TYPE); + replacement.append(HttpHeaderNames.CONTENT_TYPE); replacement.append(": "); - replacement.append(HttpPostBodyUtil.MULTIPART_MIXED); + replacement.append(HttpHeaderValues.MULTIPART_MIXED); replacement.append("; "); - replacement.append(HttpHeaders.Values.BOUNDARY); + replacement.append(HttpHeaderValues.BOUNDARY); replacement.append('='); replacement.append(multipartMixedBoundary); replacement.append("\r\n\r\n"); @@ -606,11 +608,11 @@ public class HttpPostRequestEncoder implements ChunkedInput { replacement.append(multipartMixedBoundary); replacement.append("\r\n"); - replacement.append(HttpPostBodyUtil.CONTENT_DISPOSITION); + replacement.append(HttpHeaderNames.CONTENT_DISPOSITION); replacement.append(": "); - replacement.append(HttpPostBodyUtil.ATTACHMENT); + replacement.append(HttpHeaderValues.ATTACHMENT); replacement.append("; "); - replacement.append(HttpPostBodyUtil.FILENAME); + replacement.append(HttpHeaderValues.FILENAME); replacement.append("=\""); replacement.append(fileUpload.getFilename()); replacement.append("\"\r\n"); @@ -642,27 +644,27 @@ public class HttpPostRequestEncoder implements ChunkedInput { // Data to multipart list internal.addValue("--" + multipartMixedBoundary + "\r\n"); // Content-Disposition: attachment; filename="file1.txt" - internal.addValue(HttpPostBodyUtil.CONTENT_DISPOSITION + ": " + HttpPostBodyUtil.ATTACHMENT + "; " - + HttpPostBodyUtil.FILENAME + "=\"" + fileUpload.getFilename() + "\"\r\n"); + internal.addValue(HttpHeaderNames.CONTENT_DISPOSITION + ": " + HttpHeaderValues.ATTACHMENT + "; " + + HttpHeaderValues.FILENAME + "=\"" + fileUpload.getFilename() + "\"\r\n"); } else { internal.addValue("--" + multipartDataBoundary + "\r\n"); // Content-Disposition: form-data; name="files"; // filename="file1.txt" - internal.addValue(HttpPostBodyUtil.CONTENT_DISPOSITION + ": " + HttpPostBodyUtil.FORM_DATA + "; " - + HttpPostBodyUtil.NAME + "=\"" + fileUpload.getName() + "\"; " - + HttpPostBodyUtil.FILENAME + "=\"" + fileUpload.getFilename() + "\"\r\n"); + internal.addValue(HttpHeaderNames.CONTENT_DISPOSITION + ": " + HttpHeaderValues.FORM_DATA + "; " + + HttpHeaderValues.NAME + "=\"" + fileUpload.getName() + "\"; " + + HttpHeaderValues.FILENAME + "=\"" + fileUpload.getFilename() + "\"\r\n"); } // Content-Type: image/gif // Content-Type: text/plain; charset=ISO-8859-1 // Content-Transfer-Encoding: binary - internal.addValue(HttpHeaders.Names.CONTENT_TYPE + ": " + fileUpload.getContentType()); + internal.addValue(HttpHeaderNames.CONTENT_TYPE + ": " + fileUpload.getContentType()); String contentTransferEncoding = fileUpload.getContentTransferEncoding(); if (contentTransferEncoding != null && contentTransferEncoding.equals(HttpPostBodyUtil.TransferEncodingMechanism.BINARY.value())) { - internal.addValue("\r\n" + HttpHeaders.Names.CONTENT_TRANSFER_ENCODING + ": " + internal.addValue("\r\n" + HttpHeaderNames.CONTENT_TRANSFER_ENCODING + ": " + HttpPostBodyUtil.TransferEncodingMechanism.BINARY.value() + "\r\n\r\n"); } else if (fileUpload.getCharset() != null) { - internal.addValue("; " + HttpHeaders.Values.CHARSET + '=' + fileUpload.getCharset() + "\r\n\r\n"); + internal.addValue("; " + HttpHeaderValues.CHARSET + '=' + fileUpload.getCharset() + "\r\n\r\n"); } else { internal.addValue("\r\n\r\n"); } @@ -708,28 +710,28 @@ public class HttpPostRequestEncoder implements ChunkedInput { } HttpHeaders headers = request.headers(); - List contentTypes = headers.getAll(HttpHeaders.Names.CONTENT_TYPE); - List transferEncoding = headers.getAll(HttpHeaders.Names.TRANSFER_ENCODING); + List contentTypes = headers.getAll(HttpHeaderNames.CONTENT_TYPE); + List transferEncoding = headers.getAll(HttpHeaderNames.TRANSFER_ENCODING); if (contentTypes != null) { - headers.remove(HttpHeaders.Names.CONTENT_TYPE); + headers.remove(HttpHeaderNames.CONTENT_TYPE); for (String contentType : contentTypes) { // "multipart/form-data; boundary=--89421926422648" String lowercased = contentType.toLowerCase(); - if (lowercased.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA) || - lowercased.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED.toString())) { + if (lowercased.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString()) || + lowercased.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())) { // ignore } else { - headers.add(HttpHeaders.Names.CONTENT_TYPE, contentType); + headers.add(HttpHeaderNames.CONTENT_TYPE, contentType); } } } if (isMultipart) { - String value = HttpHeaders.Values.MULTIPART_FORM_DATA + "; " + HttpHeaders.Values.BOUNDARY + '=' + String value = HttpHeaderValues.MULTIPART_FORM_DATA + "; " + HttpHeaderValues.BOUNDARY + '=' + multipartDataBoundary; - headers.add(HttpHeaders.Names.CONTENT_TYPE, value); + headers.add(HttpHeaderNames.CONTENT_TYPE, value); } else { // Not multipart - headers.add(HttpHeaders.Names.CONTENT_TYPE, HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED); + headers.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED); } // Now consider size for chunk or not long realSize = globalBodySize; @@ -739,20 +741,20 @@ public class HttpPostRequestEncoder implements ChunkedInput { realSize -= 1; // last '&' removed iterator = multipartHttpDatas.listIterator(); } - headers.set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(realSize)); + headers.set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(realSize)); if (realSize > HttpPostBodyUtil.chunkSize || isMultipart) { isChunked = true; if (transferEncoding != null) { - headers.remove(HttpHeaders.Names.TRANSFER_ENCODING); + headers.remove(HttpHeaderNames.TRANSFER_ENCODING); for (CharSequence v : transferEncoding) { - if (AsciiString.equalsIgnoreCase(v, HttpHeaders.Values.CHUNKED)) { + if (HttpHeaderValues.CHUNKED.equalsIgnoreCase(v)) { // ignore } else { - headers.add(HttpHeaders.Names.TRANSFER_ENCODING, v); + headers.add(HttpHeaderNames.TRANSFER_ENCODING, v); } } } - HttpHeaders.setTransferEncodingChunked(request); + HttpHeaderUtil.setTransferEncodingChunked(request, true); // wrap to hide the possible content return new WrappedHttpRequest(request); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/MemoryFileUpload.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/MemoryFileUpload.java index 6100629b3b..90f2faf1f1 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/MemoryFileUpload.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/MemoryFileUpload.java @@ -17,7 +17,8 @@ package io.netty.handler.codec.http.multipart; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelException; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import java.io.IOException; import java.nio.charset.Charset; @@ -119,12 +120,12 @@ public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUplo @Override public String toString() { - return HttpPostBodyUtil.CONTENT_DISPOSITION + ": " + - HttpPostBodyUtil.FORM_DATA + "; " + HttpPostBodyUtil.NAME + "=\"" + getName() + - "\"; " + HttpPostBodyUtil.FILENAME + "=\"" + filename + "\"\r\n" + - HttpHeaders.Names.CONTENT_TYPE + ": " + contentType + - (getCharset() != null? "; " + HttpHeaders.Values.CHARSET + '=' + getCharset() + "\r\n" : "\r\n") + - HttpHeaders.Names.CONTENT_LENGTH + ": " + length() + "\r\n" + + return HttpHeaderNames.CONTENT_DISPOSITION + ": " + + HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + getName() + + "\"; " + HttpHeaderValues.FILENAME + "=\"" + filename + "\"\r\n" + + HttpHeaderNames.CONTENT_TYPE + ": " + contentType + + (getCharset() != null? "; " + HttpHeaderValues.CHARSET + '=' + getCharset() + "\r\n" : "\r\n") + + HttpHeaderNames.CONTENT_LENGTH + ": " + length() + "\r\n" + "Completed: " + isCompleted() + "\r\nIsInMemory: " + isInMemory(); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java index 59e3ed597a..ef91298e13 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java @@ -25,6 +25,7 @@ import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpClientCodec; import io.netty.handler.codec.http.HttpContentDecompressor; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpRequestEncoder; import io.netty.handler.codec.http.HttpResponseDecoder; @@ -203,7 +204,7 @@ public abstract class WebSocketClientHandshaker { // Verify the subprotocol that we received from the server. // This must be one of our expected subprotocols - or null/empty if we didn't want to speak a subprotocol - String receivedProtocol = response.headers().get(HttpHeaders.Names.SEC_WEBSOCKET_PROTOCOL); + String receivedProtocol = response.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL); receivedProtocol = receivedProtocol != null ? receivedProtocol.trim() : null; String expectedProtocol = expectedSubprotocol != null ? expectedSubprotocol : ""; boolean protocolValid = false; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java index 80439b4e0b..02db5549cc 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java @@ -21,9 +21,9 @@ import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; @@ -43,6 +43,8 @@ import java.nio.ByteBuffer; */ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { + private static final AsciiString WEBSOCKET = new AsciiString("WebSocket"); + private ByteBuf expectedChallengeResponseBytes; /** @@ -136,9 +138,9 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { // Format request FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); HttpHeaders headers = request.headers(); - headers.add(Names.UPGRADE, Values.WEBSOCKET) - .add(Names.CONNECTION, Values.UPGRADE) - .add(Names.HOST, wsURL.getHost()); + headers.add(HttpHeaderNames.UPGRADE, WEBSOCKET) + .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE) + .add(HttpHeaderNames.HOST, wsURL.getHost()); int wsPort = wsURL.getPort(); String originValue = "http://" + wsURL.getHost(); @@ -148,13 +150,13 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { originValue = originValue + ':' + wsPort; } - headers.add(Names.ORIGIN, originValue) - .add(Names.SEC_WEBSOCKET_KEY1, key1) - .add(Names.SEC_WEBSOCKET_KEY2, key2); + headers.add(HttpHeaderNames.ORIGIN, originValue) + .add(HttpHeaderNames.SEC_WEBSOCKET_KEY1, key1) + .add(HttpHeaderNames.SEC_WEBSOCKET_KEY2, key2); String expectedSubprotocol = expectedSubprotocol(); if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) { - headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); } if (customHeaders != null) { @@ -163,7 +165,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { // Set Content-Length to workaround some known defect. // See also: http://www.ietf.org/mail-archive/web/hybi/current/msg02149.html - headers.set(Names.CONTENT_LENGTH, key3.length); + headers.set(HttpHeaderNames.CONTENT_LENGTH, key3.length); request.content().writeBytes(key3); return request; } @@ -198,14 +200,14 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { HttpHeaders headers = response.headers(); - CharSequence upgrade = headers.get(Names.UPGRADE); - if (!AsciiString.equalsIgnoreCase(Values.WEBSOCKET, upgrade)) { + CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE); + if (!WEBSOCKET.equalsIgnoreCase(upgrade)) { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } - CharSequence connection = headers.get(Names.CONNECTION); - if (!AsciiString.equalsIgnoreCase(Values.UPGRADE, connection)) { + CharSequence connection = headers.get(HttpHeaderNames.CONNECTION); + if (!HttpHeaderValues.UPGRADE.equalsIgnoreCase(connection)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java index 3da78cb321..7d35aaecdd 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java @@ -15,13 +15,12 @@ */ package io.netty.handler.codec.http.websocketx; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; @@ -41,7 +40,6 @@ import java.net.URI; public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker { private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker07.class); - private static final CharSequence WEBSOCKET = Values.WEBSOCKET.toLowerCase(); public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; private String expectedChallengeResponseString; @@ -154,10 +152,10 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker { FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); HttpHeaders headers = request.headers(); - headers.add(Names.UPGRADE, WEBSOCKET) - .add(Names.CONNECTION, Values.UPGRADE) - .add(Names.SEC_WEBSOCKET_KEY, key) - .add(Names.HOST, wsURL.getHost()); + headers.add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET) + .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE) + .add(HttpHeaderNames.SEC_WEBSOCKET_KEY, key) + .add(HttpHeaderNames.HOST, wsURL.getHost()); int wsPort = wsURL.getPort(); String originValue = "http://" + wsURL.getHost(); @@ -166,14 +164,14 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker { // See http://tools.ietf.org/html/rfc6454#section-6.2 originValue = originValue + ':' + wsPort; } - headers.add(Names.SEC_WEBSOCKET_ORIGIN, originValue); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, originValue); String expectedSubprotocol = expectedSubprotocol(); if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) { - headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); } - headers.add(Names.SEC_WEBSOCKET_VERSION, "7"); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "7"); if (customHeaders != null) { headers.add(customHeaders); @@ -207,17 +205,17 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker { throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.status()); } - CharSequence upgrade = headers.get(Names.UPGRADE); - if (!AsciiString.equalsIgnoreCase(Values.WEBSOCKET, upgrade)) { + CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE); + if (!HttpHeaderValues.WEBSOCKET.equalsIgnoreCase(upgrade)) { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } - CharSequence connection = headers.get(Names.CONNECTION); - if (!AsciiString.equalsIgnoreCase(Values.UPGRADE, connection)) { + CharSequence connection = headers.get(HttpHeaderNames.CONNECTION); + if (!HttpHeaderValues.UPGRADE.equalsIgnoreCase(connection)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); } - CharSequence accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); + CharSequence accept = headers.get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT); if (accept == null || !accept.equals(expectedChallengeResponseString)) { throw new WebSocketHandshakeException(String.format( "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString)); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java index b8d072c2b8..666a36b27a 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java @@ -15,13 +15,12 @@ */ package io.netty.handler.codec.http.websocketx; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; @@ -41,7 +40,6 @@ import java.net.URI; public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker08.class); - private static final CharSequence WEBSOCKET = Values.WEBSOCKET.toLowerCase(); public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; @@ -155,10 +153,10 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); HttpHeaders headers = request.headers(); - headers.add(Names.UPGRADE, WEBSOCKET) - .add(Names.CONNECTION, Values.UPGRADE) - .add(Names.SEC_WEBSOCKET_KEY, key) - .add(Names.HOST, wsURL.getHost()); + headers.add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET) + .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE) + .add(HttpHeaderNames.SEC_WEBSOCKET_KEY, key) + .add(HttpHeaderNames.HOST, wsURL.getHost()); int wsPort = wsURL.getPort(); String originValue = "http://" + wsURL.getHost(); @@ -167,14 +165,14 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { // See http://tools.ietf.org/html/rfc6454#section-6.2 originValue = originValue + ':' + wsPort; } - headers.add(Names.SEC_WEBSOCKET_ORIGIN, originValue); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, originValue); String expectedSubprotocol = expectedSubprotocol(); if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) { - headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); } - headers.add(Names.SEC_WEBSOCKET_VERSION, "8"); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "8"); if (customHeaders != null) { headers.add(customHeaders); @@ -208,17 +206,17 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.status()); } - CharSequence upgrade = headers.get(Names.UPGRADE); - if (!AsciiString.equalsIgnoreCase(Values.WEBSOCKET, upgrade)) { + CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE); + if (!HttpHeaderValues.WEBSOCKET.equalsIgnoreCase(upgrade)) { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } - CharSequence connection = headers.get(Names.CONNECTION); - if (!AsciiString.equalsIgnoreCase(Values.UPGRADE, connection)) { + CharSequence connection = headers.get(HttpHeaderNames.CONNECTION); + if (!HttpHeaderValues.UPGRADE.equalsIgnoreCase(connection)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); } - CharSequence accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); + CharSequence accept = headers.get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT); if (accept == null || !accept.equals(expectedChallengeResponseString)) { throw new WebSocketHandshakeException(String.format( "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString)); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java index 4bf36acd1e..0be0886401 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java @@ -15,13 +15,12 @@ */ package io.netty.handler.codec.http.websocketx; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; @@ -41,7 +40,6 @@ import java.net.URI; public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker13.class); - private static final CharSequence WEBSOCKET = Values.WEBSOCKET.toLowerCase(); public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; @@ -166,10 +164,10 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); HttpHeaders headers = request.headers(); - headers.add(Names.UPGRADE, WEBSOCKET) - .add(Names.CONNECTION, Values.UPGRADE) - .add(Names.SEC_WEBSOCKET_KEY, key) - .add(Names.HOST, wsURL.getHost() + ':' + wsPort); + headers.add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET) + .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE) + .add(HttpHeaderNames.SEC_WEBSOCKET_KEY, key) + .add(HttpHeaderNames.HOST, wsURL.getHost() + ':' + wsPort); String originValue = "http://" + wsURL.getHost(); if (wsPort != 80 && wsPort != 443) { @@ -177,14 +175,14 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { // See http://tools.ietf.org/html/rfc6454#section-6.2 originValue = originValue + ':' + wsPort; } - headers.add(Names.SEC_WEBSOCKET_ORIGIN, originValue); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, originValue); String expectedSubprotocol = expectedSubprotocol(); if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) { - headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); } - headers.add(Names.SEC_WEBSOCKET_VERSION, "13"); + headers.add(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13"); if (customHeaders != null) { headers.add(customHeaders); @@ -218,17 +216,17 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.status()); } - CharSequence upgrade = headers.get(Names.UPGRADE); - if (!AsciiString.equalsIgnoreCase(Values.WEBSOCKET, upgrade)) { + CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE); + if (!HttpHeaderValues.WEBSOCKET.equalsIgnoreCase(upgrade)) { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } - CharSequence connection = headers.get(Names.CONNECTION); - if (!AsciiString.equalsIgnoreCase(Values.UPGRADE, connection)) { + CharSequence connection = headers.get(HttpHeaderNames.CONNECTION); + if (!HttpHeaderValues.UPGRADE.equalsIgnoreCase(connection)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); } - CharSequence accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); + CharSequence accept = headers.get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT); if (accept == null || !accept.equals(expectedChallengeResponseString)) { throw new WebSocketHandshakeException(String.format( "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString)); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java index 6f30d248f3..b3a135b528 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java @@ -20,19 +20,16 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelPromise; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpResponseStatus; import java.util.regex.Pattern; -import static io.netty.handler.codec.http.HttpHeaders.Names.*; -import static io.netty.handler.codec.http.HttpHeaders.Values.*; import static io.netty.handler.codec.http.HttpVersion.*; /** @@ -110,13 +107,14 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) { // Serve the WebSocket handshake request. - if (!AsciiString.equalsIgnoreCase(Values.UPGRADE, req.headers().get(CONNECTION)) - || !AsciiString.equalsIgnoreCase(WEBSOCKET, req.headers().get(Names.UPGRADE))) { + if (!HttpHeaderValues.UPGRADE.equalsIgnoreCase(req.headers().get(HttpHeaderNames.CONNECTION)) + || !HttpHeaderValues.WEBSOCKET.equalsIgnoreCase(req.headers().get(HttpHeaderNames.UPGRADE))) { throw new WebSocketHandshakeException("not a WebSocket handshake request: missing upgrade"); } // Hixie 75 does not contain these headers while Hixie 76 does - boolean isHixie76 = req.headers().contains(SEC_WEBSOCKET_KEY1) && req.headers().contains(SEC_WEBSOCKET_KEY2); + boolean isHixie76 = req.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_KEY1) && + req.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_KEY2); // Create the WebSocket handshake response. FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, new HttpResponseStatus(101, @@ -125,15 +123,15 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { res.headers().add(headers); } - res.headers().add(Names.UPGRADE, WEBSOCKET); - res.headers().add(CONNECTION, Values.UPGRADE); + res.headers().add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); + res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE); // Fill in the headers and contents depending on handshake getMethod. if (isHixie76) { // New handshake getMethod with a challenge: - res.headers().add(SEC_WEBSOCKET_ORIGIN, req.headers().get(ORIGIN)); - res.headers().add(SEC_WEBSOCKET_LOCATION, uri()); - String subprotocols = req.headers().get(SEC_WEBSOCKET_PROTOCOL); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, req.headers().get(HttpHeaderNames.ORIGIN)); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_LOCATION, uri()); + String subprotocols = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL); if (subprotocols != null) { String selectedSubprotocol = selectSubprotocol(subprotocols); if (selectedSubprotocol == null) { @@ -141,13 +139,13 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { logger.debug("Requested subprotocol(s) not supported: {}", subprotocols); } } else { - res.headers().add(SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); } } // Calculate the answer of the challenge. - String key1 = req.headers().get(SEC_WEBSOCKET_KEY1); - String key2 = req.headers().get(SEC_WEBSOCKET_KEY2); + String key1 = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY1); + String key2 = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY2); int a = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key1).replaceAll("")) / BEGINNING_SPACE.matcher(key1).replaceAll("").length()); int b = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key2).replaceAll("")) / @@ -160,11 +158,11 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { res.content().writeBytes(WebSocketUtil.md5(input.array())); } else { // Old Hixie 75 handshake getMethod with no challenge: - res.headers().add(WEBSOCKET_ORIGIN, req.headers().get(ORIGIN)); - res.headers().add(WEBSOCKET_LOCATION, uri()); - String protocol = req.headers().get(WEBSOCKET_PROTOCOL); + res.headers().add(HttpHeaderNames.WEBSOCKET_ORIGIN, req.headers().get(HttpHeaderNames.ORIGIN)); + res.headers().add(HttpHeaderNames.WEBSOCKET_LOCATION, uri()); + String protocol = req.headers().get(HttpHeaderNames.WEBSOCKET_PROTOCOL); if (protocol != null) { - res.headers().add(WEBSOCKET_PROTOCOL, selectSubprotocol(protocol)); + res.headers().add(HttpHeaderNames.WEBSOCKET_PROTOCOL, selectSubprotocol(protocol)); } } return res; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java index 9473a0841a..c575fe8cf6 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java @@ -18,9 +18,9 @@ package io.netty.handler.codec.http.websocketx; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.util.CharsetUtil; @@ -35,8 +35,6 @@ import static io.netty.handler.codec.http.HttpVersion.*; */ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker { - private static final CharSequence WEBSOCKET = Values.WEBSOCKET.toLowerCase(); - public static final String WEBSOCKET_07_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; private final boolean allowExtensions; @@ -129,7 +127,7 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker { res.headers().add(headers); } - CharSequence key = req.headers().get(Names.SEC_WEBSOCKET_KEY); + CharSequence key = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY); if (key == null) { throw new WebSocketHandshakeException("not a WebSocket request: missing key"); } @@ -141,10 +139,10 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker { logger.debug("WebSocket version 07 server handshake key: {}, response: {}.", key, accept); } - res.headers().add(Names.UPGRADE, WEBSOCKET); - res.headers().add(Names.CONNECTION, Names.UPGRADE); - res.headers().add(Names.SEC_WEBSOCKET_ACCEPT, accept); - String subprotocols = req.headers().get(Names.SEC_WEBSOCKET_PROTOCOL); + res.headers().add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); + res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, accept); + String subprotocols = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL); if (subprotocols != null) { String selectedSubprotocol = selectSubprotocol(subprotocols); if (selectedSubprotocol == null) { @@ -152,7 +150,7 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker { logger.debug("Requested subprotocol(s) not supported: {}", subprotocols); } } else { - res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); } } return res; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java index c2ffae2fb9..a4900a43a6 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java @@ -18,9 +18,9 @@ package io.netty.handler.codec.http.websocketx; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.util.CharsetUtil; @@ -35,8 +35,6 @@ import static io.netty.handler.codec.http.HttpVersion.*; */ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { - private static final CharSequence WEBSOCKET = Values.WEBSOCKET.toLowerCase(); - public static final String WEBSOCKET_08_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; private final boolean allowExtensions; @@ -128,7 +126,7 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { res.headers().add(headers); } - CharSequence key = req.headers().get(Names.SEC_WEBSOCKET_KEY); + CharSequence key = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY); if (key == null) { throw new WebSocketHandshakeException("not a WebSocket request: missing key"); } @@ -140,10 +138,10 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { logger.debug("WebSocket version 08 server handshake key: {}, response: {}", key, accept); } - res.headers().add(Names.UPGRADE, WEBSOCKET); - res.headers().add(Names.CONNECTION, Names.UPGRADE); - res.headers().add(Names.SEC_WEBSOCKET_ACCEPT, accept); - String subprotocols = req.headers().get(Names.SEC_WEBSOCKET_PROTOCOL); + res.headers().add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); + res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, accept); + String subprotocols = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL); if (subprotocols != null) { String selectedSubprotocol = selectSubprotocol(subprotocols); if (selectedSubprotocol == null) { @@ -151,7 +149,7 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { logger.debug("Requested subprotocol(s) not supported: {}", subprotocols); } } else { - res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); } } return res; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java index a07760443f..b010dddb47 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java @@ -18,9 +18,9 @@ package io.netty.handler.codec.http.websocketx; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.util.CharsetUtil; @@ -34,8 +34,6 @@ import static io.netty.handler.codec.http.HttpVersion.*; */ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { - private static final CharSequence WEBSOCKET = Values.WEBSOCKET.toLowerCase(); - public static final String WEBSOCKET_13_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; private final boolean allowExtensions; @@ -126,7 +124,7 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { res.headers().add(headers); } - CharSequence key = req.headers().get(Names.SEC_WEBSOCKET_KEY); + CharSequence key = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY); if (key == null) { throw new WebSocketHandshakeException("not a WebSocket request: missing key"); } @@ -138,10 +136,10 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { logger.debug("WebSocket version 13 server handshake key: {}, response: {}", key, accept); } - res.headers().add(Names.UPGRADE, WEBSOCKET); - res.headers().add(Names.CONNECTION, Names.UPGRADE); - res.headers().add(Names.SEC_WEBSOCKET_ACCEPT, accept); - String subprotocols = req.headers().get(Names.SEC_WEBSOCKET_PROTOCOL); + res.headers().add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); + res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, accept); + String subprotocols = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL); if (subprotocols != null) { String selectedSubprotocol = selectSubprotocol(subprotocols); if (selectedSubprotocol == null) { @@ -149,7 +147,7 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { logger.debug("Requested subprotocol(s) not supported: {}", subprotocols); } } else { - res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); + res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); } } return res; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshakerFactory.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshakerFactory.java index 9c482f931d..c609ec5cdb 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshakerFactory.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshakerFactory.java @@ -19,7 +19,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpResponse; -import io.netty.handler.codec.http.HttpHeaders.Names; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponseStatus; @@ -112,7 +112,7 @@ public class WebSocketServerHandshakerFactory { */ public WebSocketServerHandshaker newHandshaker(HttpRequest req) { - CharSequence version = req.headers().get(Names.SEC_WEBSOCKET_VERSION); + CharSequence version = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_VERSION); if (version != null) { if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) { // Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification). @@ -157,7 +157,7 @@ public class WebSocketServerHandshakerFactory { HttpResponse res = new DefaultHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.UPGRADE_REQUIRED); - res.headers().set(Names.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); + res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); return channel.write(res, promise); } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java index a36db7fa04..2199ce6837 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java @@ -22,11 +22,12 @@ import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.ssl.SslHandler; -import static io.netty.handler.codec.http.HttpHeaders.*; +import static io.netty.handler.codec.http.HttpHeaderUtil.*; import static io.netty.handler.codec.http.HttpMethod.*; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; @@ -101,7 +102,6 @@ class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapt // SSL in use so use Secure WebSockets protocol = "wss"; } - return protocol + "://" + req.headers().get(Names.HOST) + path; + return protocol + "://" + req.headers().get(HttpHeaderNames.HOST) + path; } - } diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaderNames.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaderNames.java new file mode 100644 index 0000000000..5ab154101b --- /dev/null +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaderNames.java @@ -0,0 +1,207 @@ +/* + * Copyright 2014 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package io.netty.handler.codec.rtsp; + +import io.netty.handler.codec.AsciiString; +import io.netty.handler.codec.http.HttpHeaderNames; + +/** + * Standard RTSP header names. + *

+ * These are all defined as lowercase to support HTTP/2 requirements while also not + * violating RTSP/1.x requirements. New header names should always be lowercase. + */ +public final class RtspHeaderNames { + /** + * {@code "accept"} + */ + public static final AsciiString ACCEPT = HttpHeaderNames.ACCEPT; + /** + * {@code "accept-encoding"} + */ + public static final AsciiString ACCEPT_ENCODING = HttpHeaderNames.ACCEPT_ENCODING; + /** + * {@code "accept-lanugage"} + */ + public static final AsciiString ACCEPT_LANGUAGE = HttpHeaderNames.ACCEPT_LANGUAGE; + /** + * {@code "allow"} + */ + public static final AsciiString ALLOW = new AsciiString("allow"); + /** + * {@code "authorization"} + */ + public static final AsciiString AUTHORIZATION = HttpHeaderNames.AUTHORIZATION; + /** + * {@code "bandwidth"} + */ + public static final AsciiString BANDWIDTH = new AsciiString("bandwidth"); + /** + * {@code "blocksize"} + */ + public static final AsciiString BLOCKSIZE = new AsciiString("blocksize"); + /** + * {@code "cache-control"} + */ + public static final AsciiString CACHE_CONTROL = HttpHeaderNames.CACHE_CONTROL; + /** + * {@code "conference"} + */ + public static final AsciiString CONFERENCE = new AsciiString("conference"); + /** + * {@code "connection"} + */ + public static final AsciiString CONNECTION = HttpHeaderNames.CONNECTION; + /** + * {@code "content-base"} + */ + public static final AsciiString CONTENT_BASE = HttpHeaderNames.CONTENT_BASE; + /** + * {@code "content-encoding"} + */ + public static final AsciiString CONTENT_ENCODING = HttpHeaderNames.CONTENT_ENCODING; + /** + * {@code "content-language"} + */ + public static final AsciiString CONTENT_LANGUAGE = HttpHeaderNames.CONTENT_LANGUAGE; + /** + * {@code "content-length"} + */ + public static final AsciiString CONTENT_LENGTH = HttpHeaderNames.CONTENT_LENGTH; + /** + * {@code "content-location"} + */ + public static final AsciiString CONTENT_LOCATION = HttpHeaderNames.CONTENT_LOCATION; + /** + * {@code "content-type"} + */ + public static final AsciiString CONTENT_TYPE = HttpHeaderNames.CONTENT_TYPE; + /** + * {@code "cseq"} + */ + public static final AsciiString CSEQ = new AsciiString("cseq"); + /** + * {@code "cate"} + */ + public static final AsciiString DATE = HttpHeaderNames.DATE; + /** + * {@code "expires"} + */ + public static final AsciiString EXPIRES = HttpHeaderNames.EXPIRES; + /** + * {@code "from"} + */ + public static final AsciiString FROM = HttpHeaderNames.FROM; + /** + * {@code "host"} + */ + public static final AsciiString HOST = HttpHeaderNames.HOST; + /** + * {@code "if-match"} + */ + public static final AsciiString IF_MATCH = HttpHeaderNames.IF_MATCH; + /** + * {@code "if-modified-since"} + */ + public static final AsciiString IF_MODIFIED_SINCE = HttpHeaderNames.IF_MODIFIED_SINCE; + /** + * {@code "keymgmt"} + */ + public static final AsciiString KEYMGMT = new AsciiString("keymgmt"); + /** + * {@code "last-modified"} + */ + public static final AsciiString LAST_MODIFIED = HttpHeaderNames.LAST_MODIFIED; + /** + * {@code "proxy-authenticate"} + */ + public static final AsciiString PROXY_AUTHENTICATE = HttpHeaderNames.PROXY_AUTHENTICATE; + /** + * {@code "proxy-require"} + */ + public static final AsciiString PROXY_REQUIRE = new AsciiString("proxy-require"); + /** + * {@code "public"} + */ + public static final AsciiString PUBLIC = new AsciiString("public"); + /** + * {@code "range"} + */ + public static final AsciiString RANGE = HttpHeaderNames.RANGE; + /** + * {@code "referer"} + */ + public static final AsciiString REFERER = HttpHeaderNames.REFERER; + /** + * {@code "require"} + */ + public static final AsciiString REQUIRE = new AsciiString("require"); + /** + * {@code "retry-after"} + */ + public static final AsciiString RETRT_AFTER = HttpHeaderNames.RETRY_AFTER; + /** + * {@code "rtp-info"} + */ + public static final AsciiString RTP_INFO = new AsciiString("rtp-info"); + /** + * {@code "scale"} + */ + public static final AsciiString SCALE = new AsciiString("scale"); + /** + * {@code "session"} + */ + public static final AsciiString SESSION = new AsciiString("session"); + /** + * {@code "server"} + */ + public static final AsciiString SERVER = HttpHeaderNames.SERVER; + /** + * {@code "speed"} + */ + public static final AsciiString SPEED = new AsciiString("speed"); + /** + * {@code "timestamp"} + */ + public static final AsciiString TIMESTAMP = new AsciiString("timestamp"); + /** + * {@code "transport"} + */ + public static final AsciiString TRANSPORT = new AsciiString("transport"); + /** + * {@code "unsupported"} + */ + public static final AsciiString UNSUPPORTED = new AsciiString("unsupported"); + /** + * {@code "user-agent"} + */ + public static final AsciiString USER_AGENT = HttpHeaderNames.USER_AGENT; + /** + * {@code "vary"} + */ + public static final AsciiString VARY = HttpHeaderNames.VARY; + /** + * {@code "via"} + */ + public static final AsciiString VIA = HttpHeaderNames.VIA; + /** + * {@code "www-authenticate"} + */ + public static final AsciiString WWW_AUTHENTICATE = HttpHeaderNames.WWW_AUTHENTICATE; + + private RtspHeaderNames() { } +} diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaderValues.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaderValues.java new file mode 100644 index 0000000000..86551e6963 --- /dev/null +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaderValues.java @@ -0,0 +1,196 @@ +/* + * Copyright 2014 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package io.netty.handler.codec.rtsp; + +import io.netty.handler.codec.AsciiString; +import io.netty.handler.codec.http.HttpHeaderValues; + +/** + * Standard RTSP header names. + */ +public final class RtspHeaderValues { + /** + * {@code "append"} + */ + public static final AsciiString APPEND = new AsciiString("append"); + /** + * {@code "AVP"} + */ + public static final AsciiString AVP = new AsciiString("AVP"); + /** + * {@code "bytes"} + */ + public static final AsciiString BYTES = HttpHeaderValues.BYTES; + /** + * {@code "charset"} + */ + public static final AsciiString CHARSET = HttpHeaderValues.CHARSET; + /** + * {@code "client_port"} + */ + public static final AsciiString CLIENT_PORT = new AsciiString("client_port"); + /** + * {@code "clock"} + */ + public static final AsciiString CLOCK = new AsciiString("clock"); + /** + * {@code "close"} + */ + public static final AsciiString CLOSE = HttpHeaderValues.CLOSE; + /** + * {@code "compress"} + */ + public static final AsciiString COMPRESS = HttpHeaderValues.COMPRESS; + /** + * {@code "100-continue"} + */ + public static final AsciiString CONTINUE = HttpHeaderValues.CONTINUE; + /** + * {@code "deflate"} + */ + public static final AsciiString DEFLATE = HttpHeaderValues.DEFLATE; + /** + * {@code "destination"} + */ + public static final AsciiString DESTINATION = new AsciiString("destination"); + /** + * {@code "gzip"} + */ + public static final AsciiString GZIP = HttpHeaderValues.GZIP; + /** + * {@code "identity"} + */ + public static final AsciiString IDENTITY = HttpHeaderValues.IDENTITY; + /** + * {@code "interleaved"} + */ + public static final AsciiString INTERLEAVED = new AsciiString("interleaved"); + /** + * {@code "keep-alive"} + */ + public static final AsciiString KEEP_ALIVE = HttpHeaderValues.KEEP_ALIVE; + /** + * {@code "layers"} + */ + public static final AsciiString LAYERS = new AsciiString("layers"); + /** + * {@code "max-age"} + */ + public static final AsciiString MAX_AGE = HttpHeaderValues.MAX_AGE; + /** + * {@code "max-stale"} + */ + public static final AsciiString MAX_STALE = HttpHeaderValues.MAX_STALE; + /** + * {@code "min-fresh"} + */ + public static final AsciiString MIN_FRESH = HttpHeaderValues.MIN_FRESH; + /** + * {@code "mode"} + */ + public static final AsciiString MODE = new AsciiString("mode"); + /** + * {@code "multicast"} + */ + public static final AsciiString MULTICAST = new AsciiString("multicast"); + /** + * {@code "must-revalidate"} + */ + public static final AsciiString MUST_REVALIDATE = HttpHeaderValues.MUST_REVALIDATE; + /** + * {@code "none"} + */ + public static final AsciiString NONE = HttpHeaderValues.NONE; + /** + * {@code "no-cache"} + */ + public static final AsciiString NO_CACHE = HttpHeaderValues.NO_CACHE; + /** + * {@code "no-transform"} + */ + public static final AsciiString NO_TRANSFORM = HttpHeaderValues.NO_TRANSFORM; + /** + * {@code "only-if-cached"} + */ + public static final AsciiString ONLY_IF_CACHED = HttpHeaderValues.ONLY_IF_CACHED; + /** + * {@code "port"} + */ + public static final AsciiString PORT = new AsciiString("port"); + /** + * {@code "private"} + */ + public static final AsciiString PRIVATE = HttpHeaderValues.PRIVATE; + /** + * {@code "proxy-revalidate"} + */ + public static final AsciiString PROXY_REVALIDATE = HttpHeaderValues.PROXY_REVALIDATE; + /** + * {@code "public"} + */ + public static final AsciiString PUBLIC = HttpHeaderValues.PUBLIC; + /** + * {@code "RTP"} + */ + public static final AsciiString RTP = new AsciiString("RTP"); + /** + * {@code "rtptime"} + */ + public static final AsciiString RTPTIME = new AsciiString("rtptime"); + /** + * {@code "seq"} + */ + public static final AsciiString SEQ = new AsciiString("seq"); + /** + * {@code "server_port"} + */ + public static final AsciiString SERVER_PORT = new AsciiString("server_port"); + /** + * {@code "ssrc"} + */ + public static final AsciiString SSRC = new AsciiString("ssrc"); + /** + * {@code "TCP"} + */ + public static final AsciiString TCP = new AsciiString("TCP"); + /** + * {@code "time"} + */ + public static final AsciiString TIME = new AsciiString("time"); + /** + * {@code "timeout"} + */ + public static final AsciiString TIMEOUT = new AsciiString("timeout"); + /** + * {@code "ttl"} + */ + public static final AsciiString TTL = new AsciiString("ttl"); + /** + * {@code "UDP"} + */ + public static final AsciiString UDP = new AsciiString("UDP"); + /** + * {@code "unicast"} + */ + public static final AsciiString UNICAST = new AsciiString("unicast"); + /** + * {@code "url"} + */ + public static final AsciiString URL = new AsciiString("url"); + + private RtspHeaderValues() { } +} diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaders.java index f5c2508047..6383705c29 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaders.java @@ -15,203 +15,211 @@ */ package io.netty.handler.codec.rtsp; -import io.netty.handler.codec.AsciiString; import io.netty.handler.codec.http.HttpHeaders; /** + * @deprecated Use {@link RtspHeaderNames} or {@link RtspHeaderValues} instead. + * Standard RTSP header names and values. */ +@Deprecated public final class RtspHeaders { /** + * @deprecated Use {@link RtspHeaderNames} instead. + * * Standard RTSP header names. */ + @Deprecated public static final class Names { /** * {@code "Accept"} */ - public static final AsciiString ACCEPT = HttpHeaders.Names.ACCEPT; + public static final String ACCEPT = HttpHeaders.Names.ACCEPT; /** * {@code "Accept-Encoding"} */ - public static final AsciiString ACCEPT_ENCODING = HttpHeaders.Names.ACCEPT_ENCODING; + public static final String ACCEPT_ENCODING = HttpHeaders.Names.ACCEPT_ENCODING; /** * {@code "Accept-Lanugage"} */ - public static final AsciiString ACCEPT_LANGUAGE = HttpHeaders.Names.ACCEPT_LANGUAGE; + public static final String ACCEPT_LANGUAGE = HttpHeaders.Names.ACCEPT_LANGUAGE; /** * {@code "Allow"} */ - public static final AsciiString ALLOW = new AsciiString("Allow"); + public static final String ALLOW = "Allow"; /** * {@code "Authorization"} */ - public static final AsciiString AUTHORIZATION = HttpHeaders.Names.AUTHORIZATION; + public static final String AUTHORIZATION = HttpHeaders.Names.AUTHORIZATION; /** * {@code "Bandwidth"} */ - public static final AsciiString BANDWIDTH = new AsciiString("Bandwidth"); + public static final String BANDWIDTH = "Bandwidth"; /** * {@code "Blocksize"} */ - public static final AsciiString BLOCKSIZE = new AsciiString("Blocksize"); + public static final String BLOCKSIZE = "Blocksize"; /** * {@code "Cache-Control"} */ - public static final AsciiString CACHE_CONTROL = HttpHeaders.Names.CACHE_CONTROL; + public static final String CACHE_CONTROL = HttpHeaders.Names.CACHE_CONTROL; /** * {@code "Conference"} */ - public static final AsciiString CONFERENCE = new AsciiString("Conference"); + public static final String CONFERENCE = "Conference"; /** * {@code "Connection"} */ - public static final AsciiString CONNECTION = HttpHeaders.Names.CONNECTION; + public static final String CONNECTION = HttpHeaders.Names.CONNECTION; /** * {@code "Content-Base"} */ - public static final AsciiString CONTENT_BASE = HttpHeaders.Names.CONTENT_BASE; + public static final String CONTENT_BASE = HttpHeaders.Names.CONTENT_BASE; /** * {@code "Content-Encoding"} */ - public static final AsciiString CONTENT_ENCODING = HttpHeaders.Names.CONTENT_ENCODING; + public static final String CONTENT_ENCODING = HttpHeaders.Names.CONTENT_ENCODING; /** * {@code "Content-Language"} */ - public static final AsciiString CONTENT_LANGUAGE = HttpHeaders.Names.CONTENT_LANGUAGE; + public static final String CONTENT_LANGUAGE = HttpHeaders.Names.CONTENT_LANGUAGE; /** * {@code "Content-Length"} */ - public static final AsciiString CONTENT_LENGTH = HttpHeaders.Names.CONTENT_LENGTH; + public static final String CONTENT_LENGTH = HttpHeaders.Names.CONTENT_LENGTH; /** * {@code "Content-Location"} */ - public static final AsciiString CONTENT_LOCATION = HttpHeaders.Names.CONTENT_LOCATION; + public static final String CONTENT_LOCATION = HttpHeaders.Names.CONTENT_LOCATION; /** * {@code "Content-Type"} */ - public static final AsciiString CONTENT_TYPE = HttpHeaders.Names.CONTENT_TYPE; + public static final String CONTENT_TYPE = HttpHeaders.Names.CONTENT_TYPE; /** * {@code "CSeq"} */ - public static final AsciiString CSEQ = new AsciiString("CSeq"); + public static final String CSEQ = "CSeq"; /** * {@code "Date"} */ - public static final AsciiString DATE = HttpHeaders.Names.DATE; + public static final String DATE = HttpHeaders.Names.DATE; /** * {@code "Expires"} */ - public static final AsciiString EXPIRES = HttpHeaders.Names.EXPIRES; + public static final String EXPIRES = HttpHeaders.Names.EXPIRES; /** * {@code "From"} */ - public static final AsciiString FROM = HttpHeaders.Names.FROM; + public static final String FROM = HttpHeaders.Names.FROM; /** * {@code "Host"} */ - public static final AsciiString HOST = HttpHeaders.Names.HOST; + public static final String HOST = HttpHeaders.Names.HOST; /** * {@code "If-Match"} */ - public static final AsciiString IF_MATCH = HttpHeaders.Names.IF_MATCH; + public static final String IF_MATCH = HttpHeaders.Names.IF_MATCH; /** * {@code "If-Modified-Since"} */ - public static final AsciiString IF_MODIFIED_SINCE = HttpHeaders.Names.IF_MODIFIED_SINCE; + public static final String IF_MODIFIED_SINCE = HttpHeaders.Names.IF_MODIFIED_SINCE; /** * {@code "KeyMgmt"} */ - public static final AsciiString KEYMGMT = new AsciiString("KeyMgmt"); + public static final String KEYMGMT = "KeyMgmt"; /** * {@code "Last-Modified"} */ - public static final AsciiString LAST_MODIFIED = HttpHeaders.Names.LAST_MODIFIED; + public static final String LAST_MODIFIED = HttpHeaders.Names.LAST_MODIFIED; /** * {@code "Proxy-Authenticate"} */ - public static final AsciiString PROXY_AUTHENTICATE = HttpHeaders.Names.PROXY_AUTHENTICATE; + public static final String PROXY_AUTHENTICATE = HttpHeaders.Names.PROXY_AUTHENTICATE; /** * {@code "Proxy-Require"} */ - public static final AsciiString PROXY_REQUIRE = new AsciiString("Proxy-Require"); + public static final String PROXY_REQUIRE = "Proxy-Require"; /** * {@code "Public"} */ - public static final AsciiString PUBLIC = new AsciiString("Public"); + public static final String PUBLIC = "Public"; /** * {@code "Range"} */ - public static final AsciiString RANGE = HttpHeaders.Names.RANGE; + public static final String RANGE = HttpHeaders.Names.RANGE; /** * {@code "Referer"} */ - public static final AsciiString REFERER = HttpHeaders.Names.REFERER; + public static final String REFERER = HttpHeaders.Names.REFERER; /** * {@code "Require"} */ - public static final AsciiString REQUIRE = new AsciiString("Require"); + public static final String REQUIRE = "Require"; /** * {@code "Retry-After"} */ - public static final AsciiString RETRT_AFTER = HttpHeaders.Names.RETRY_AFTER; + public static final String RETRT_AFTER = HttpHeaders.Names.RETRY_AFTER; /** * {@code "RTP-Info"} */ - public static final AsciiString RTP_INFO = new AsciiString("RTP-Info"); + public static final String RTP_INFO = "RTP-Info"; /** * {@code "Scale"} */ - public static final AsciiString SCALE = new AsciiString("Scale"); + public static final String SCALE = "Scale"; /** * {@code "Session"} */ - public static final AsciiString SESSION = new AsciiString("Session"); + public static final String SESSION = "Session"; /** * {@code "Server"} */ - public static final AsciiString SERVER = HttpHeaders.Names.SERVER; + public static final String SERVER = HttpHeaders.Names.SERVER; /** * {@code "Speed"} */ - public static final AsciiString SPEED = new AsciiString("Speed"); + public static final String SPEED = "Speed"; /** * {@code "Timestamp"} */ - public static final AsciiString TIMESTAMP = new AsciiString("Timestamp"); + public static final String TIMESTAMP = "Timestamp"; /** * {@code "Transport"} */ - public static final AsciiString TRANSPORT = new AsciiString("Transport"); + public static final String TRANSPORT = "Transport"; /** * {@code "Unsupported"} */ - public static final AsciiString UNSUPPORTED = new AsciiString("Unsupported"); + public static final String UNSUPPORTED = "Unsupported"; /** * {@code "User-Agent"} */ - public static final AsciiString USER_AGENT = HttpHeaders.Names.USER_AGENT; + public static final String USER_AGENT = HttpHeaders.Names.USER_AGENT; /** * {@code "Vary"} */ - public static final AsciiString VARY = HttpHeaders.Names.VARY; + public static final String VARY = HttpHeaders.Names.VARY; /** * {@code "Via"} */ - public static final AsciiString VIA = HttpHeaders.Names.VIA; + public static final String VIA = HttpHeaders.Names.VIA; /** * {@code "WWW-Authenticate"} */ - public static final AsciiString WWW_AUTHENTICATE = HttpHeaders.Names.WWW_AUTHENTICATE; + public static final String WWW_AUTHENTICATE = HttpHeaders.Names.WWW_AUTHENTICATE; private Names() { } } /** + * @deprecated Use {@link RtspHeaderValues} instead. + * * Standard RTSP header values. */ + @Deprecated public static final class Values { /** * {@code "append"} @@ -248,7 +256,7 @@ public final class RtspHeaders { /** * {@code "100-continue"} */ - public static final String CONTINUE = HttpHeaders.Values.CONTINUE; + public static final String CONTINUE = HttpHeaders.Values.CONTINUE; /** * {@code "deflate"} */ diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectDecoder.java index 7023569e22..f8bed912c2 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectDecoder.java @@ -79,7 +79,7 @@ public abstract class RtspObjectDecoder extends HttpObjectDecoder { if (empty) { return true; } - if (!msg.headers().contains(RtspHeaders.Names.CONTENT_LENGTH)) { + if (!msg.headers().contains(RtspHeaderNames.CONTENT_LENGTH)) { return true; } return empty; diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpDecoder.java index 92e2576258..4c87f98651 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpDecoder.java @@ -24,7 +24,8 @@ import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpMessage; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderUtil; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; @@ -170,14 +171,13 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { createHttpResponse(ctx, spdySynStreamFrame, validateHeaders); // Set the Stream-ID, Associated-To-Stream-ID, Priority, and URL as headers - HttpHeaders.setIntHeader(httpResponseWithEntity, Names.STREAM_ID, streamId); - HttpHeaders.setIntHeader( - httpResponseWithEntity, Names.ASSOCIATED_TO_STREAM_ID, associatedToStreamId); - HttpHeaders.setIntHeader(httpResponseWithEntity, Names.PRIORITY, spdySynStreamFrame.priority()); + httpResponseWithEntity.headers().setInt(Names.STREAM_ID, streamId); + httpResponseWithEntity.headers().setInt(Names.ASSOCIATED_TO_STREAM_ID, associatedToStreamId); + httpResponseWithEntity.headers().setInt(Names.PRIORITY, spdySynStreamFrame.priority()); httpResponseWithEntity.headers().set(Names.URL, URL); if (spdySynStreamFrame.isLast()) { - HttpHeaders.setContentLength(httpResponseWithEntity, 0); + HttpHeaderUtil.setContentLength(httpResponseWithEntity, 0); out.add(httpResponseWithEntity); } else { // Response body will follow in a series of Data Frames @@ -207,7 +207,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { FullHttpRequest httpRequestWithEntity = createHttpRequest(spdyVersion, spdySynStreamFrame); // Set the Stream-ID as a header - HttpHeaders.setIntHeader(httpRequestWithEntity, Names.STREAM_ID, streamId); + httpRequestWithEntity.headers().setInt(Names.STREAM_ID, streamId); if (spdySynStreamFrame.isLast()) { out.add(httpRequestWithEntity); @@ -246,10 +246,10 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { FullHttpResponse httpResponseWithEntity = createHttpResponse(ctx, spdySynReplyFrame, validateHeaders); // Set the Stream-ID as a header - HttpHeaders.setIntHeader(httpResponseWithEntity, Names.STREAM_ID, streamId); + httpResponseWithEntity.headers().setInt(Names.STREAM_ID, streamId); if (spdySynReplyFrame.isLast()) { - HttpHeaders.setContentLength(httpResponseWithEntity, 0); + HttpHeaderUtil.setContentLength(httpResponseWithEntity, 0); out.add(httpResponseWithEntity); } else { // Response body will follow in a series of Data Frames @@ -282,7 +282,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { } if (spdyHeadersFrame.isLast()) { - HttpHeaders.setContentLength(fullHttpMessage, fullHttpMessage.content().readableBytes()); + HttpHeaderUtil.setContentLength(fullHttpMessage, fullHttpMessage.content().readableBytes()); removeMessage(streamId); out.add(fullHttpMessage); } @@ -310,7 +310,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { content.writeBytes(spdyDataFrameData, spdyDataFrameData.readerIndex(), spdyDataFrameDataLen); if (spdyDataFrame.isLast()) { - HttpHeaders.setContentLength(fullHttpMessage, content.readableBytes()); + HttpHeaderUtil.setContentLength(fullHttpMessage, content.readableBytes()); removeMessage(streamId); out.add(fullHttpMessage); } @@ -342,17 +342,17 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { // Replace the SPDY host header with the HTTP host header CharSequence host = headers.get(HOST); headers.remove(HOST); - req.headers().set(HttpHeaders.Names.HOST, host); + req.headers().set(HttpHeaderNames.HOST, host); for (Map.Entry e: requestFrame.headers()) { req.headers().add(e.getKey(), e.getValue()); } // The Connection and Keep-Alive headers are no longer valid - HttpHeaders.setKeepAlive(req, true); + HttpHeaderUtil.setKeepAlive(req, true); // Transfer-Encoding header is not valid - req.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING); + req.headers().remove(HttpHeaderNames.TRANSFER_ENCODING); return req; } @@ -373,11 +373,11 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { } // The Connection and Keep-Alive headers are no longer valid - HttpHeaders.setKeepAlive(res, true); + HttpHeaderUtil.setKeepAlive(res, true); // Transfer-Encoding header is not valid - res.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING); - res.headers().remove(HttpHeaders.Names.TRAILER); + res.headers().remove(HttpHeaderNames.TRANSFER_ENCODING); + res.headers().remove(HttpHeaderNames.TRAILER); return res; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpEncoder.java index 6e7bc8a0cc..0433443d04 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpEncoder.java @@ -21,6 +21,7 @@ import io.netty.handler.codec.UnsupportedMessageTypeException; import io.netty.handler.codec.http.FullHttpMessage; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpContent; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpMessage; import io.netty.handler.codec.http.HttpObject; @@ -209,9 +210,9 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder { throws Exception { // Get the Stream-ID, Associated-To-Stream-ID, Priority, URL, and scheme from the headers final HttpHeaders httpHeaders = httpMessage.headers(); - int streamID = HttpHeaders.getIntHeader(httpMessage, Names.STREAM_ID); - int associatedToStreamId = HttpHeaders.getIntHeader(httpMessage, Names.ASSOCIATED_TO_STREAM_ID, 0); - byte priority = (byte) HttpHeaders.getIntHeader(httpMessage, Names.PRIORITY, 0); + int streamID = httpMessage.headers().getInt(Names.STREAM_ID); + int associatedToStreamId = httpMessage.headers().getInt(Names.ASSOCIATED_TO_STREAM_ID, 0); + byte priority = (byte) httpMessage.headers().getInt(Names.PRIORITY, 0); String URL = httpHeaders.get(Names.URL); String scheme = httpHeaders.get(Names.SCHEME); httpHeaders.remove(Names.STREAM_ID); @@ -222,10 +223,10 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder { // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding // headers are not valid and MUST not be sent. - httpHeaders.remove(HttpHeaders.Names.CONNECTION); + httpHeaders.remove(HttpHeaderNames.CONNECTION); httpHeaders.remove("Keep-Alive"); httpHeaders.remove("Proxy-Connection"); - httpHeaders.remove(HttpHeaders.Names.TRANSFER_ENCODING); + httpHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING); SpdySynStreamFrame spdySynStreamFrame = new DefaultSpdySynStreamFrame(streamID, associatedToStreamId, priority); @@ -248,8 +249,8 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder { // Replace the HTTP host header with the SPDY host header if (spdyVersion >= 3) { - String host = HttpHeaders.getHost(httpMessage); - httpHeaders.remove(HttpHeaders.Names.HOST); + String host = httpMessage.headers().get(HttpHeaderNames.HOST); + httpHeaders.remove(HttpHeaderNames.HOST); frameHeaders.set(HOST, host); } @@ -273,15 +274,15 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder { throws Exception { // Get the Stream-ID from the headers final HttpHeaders httpHeaders = httpResponse.headers(); - int streamID = HttpHeaders.getIntHeader(httpResponse, Names.STREAM_ID); + int streamID = httpResponse.headers().getInt(Names.STREAM_ID); httpHeaders.remove(Names.STREAM_ID); // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding // headers are not valid and MUST not be sent. - httpHeaders.remove(HttpHeaders.Names.CONNECTION); + httpHeaders.remove(HttpHeaderNames.CONNECTION); httpHeaders.remove("Keep-Alive"); httpHeaders.remove("Proxy-Connection"); - httpHeaders.remove(HttpHeaders.Names.TRANSFER_ENCODING); + httpHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING); SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamID); SpdyHeaders frameHeaders = spdySynReplyFrame.headers(); diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpResponseStreamIdHandler.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpResponseStreamIdHandler.java index 06f02adef7..4ad32e4dc2 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpResponseStreamIdHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpResponseStreamIdHandler.java @@ -17,7 +17,6 @@ package io.netty.handler.codec.spdy; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageCodec; -import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpMessage; import io.netty.handler.codec.spdy.SpdyHttpHeaders.Names; import io.netty.util.ReferenceCountUtil; @@ -45,7 +44,7 @@ public class SpdyHttpResponseStreamIdHandler extends protected void encode(ChannelHandlerContext ctx, HttpMessage msg, List out) throws Exception { Integer id = ids.poll(); if (id != null && id.intValue() != NO_ID && !msg.headers().contains(SpdyHttpHeaders.Names.STREAM_ID)) { - HttpHeaders.setIntHeader(msg, Names.STREAM_ID, id); + msg.headers().setInt(Names.STREAM_ID, id); } out.add(ReferenceCountUtil.retain(msg)); @@ -58,7 +57,7 @@ public class SpdyHttpResponseStreamIdHandler extends if (!contains) { ids.add(NO_ID); } else { - ids.add(HttpHeaders.getIntHeader((HttpMessage) msg, Names.STREAM_ID)); + ids.add(((HttpMessage) msg).headers().getInt(Names.STREAM_ID)); } } else if (msg instanceof SpdyRstStreamFrame) { ids.remove(((SpdyRstStreamFrame) msg).streamId()); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java index c63f04c596..56d98fe945 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java @@ -19,8 +19,6 @@ import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.compression.ZlibWrapper; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.util.CharsetUtil; import org.junit.Test; @@ -110,7 +108,7 @@ public class HttpContentCompressorTest { ch.writeInbound(newRequest()); HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED); + res.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); ch.writeOutbound(res); assertEncodedResponse(ch); @@ -151,7 +149,7 @@ public class HttpContentCompressorTest { ch.writeInbound(newRequest()); HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED); + res.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); ch.writeOutbound(res); assertEncodedResponse(ch); @@ -197,7 +195,7 @@ public class HttpContentCompressorTest { FullHttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer("Hello, World", CharsetUtil.US_ASCII)); - res.headers().set(Names.CONTENT_LENGTH, res.content().readableBytes()); + res.headers().set(HttpHeaderNames.CONTENT_LENGTH, res.content().readableBytes()); ch.writeOutbound(res); assertEncodedResponse(ch); @@ -262,10 +260,10 @@ public class HttpContentCompressorTest { assertThat(o, is(instanceOf(FullHttpResponse.class))); res = (FullHttpResponse) o; - assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.TRANSFER_ENCODING), is(nullValue())); // Content encoding shouldn't be modified. - assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING), is(nullValue())); assertThat(res.content().readableBytes(), is(0)); assertThat(res.content().toString(CharsetUtil.US_ASCII), is("")); res.release(); @@ -287,10 +285,10 @@ public class HttpContentCompressorTest { assertThat(o, is(instanceOf(FullHttpResponse.class))); res = (FullHttpResponse) o; - assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.TRANSFER_ENCODING), is(nullValue())); // Content encoding shouldn't be modified. - assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING), is(nullValue())); assertThat(res.content().readableBytes(), is(0)); assertThat(res.content().toString(CharsetUtil.US_ASCII), is("")); assertEquals("Netty", res.trailingHeaders().get("X-Test")); @@ -299,7 +297,7 @@ public class HttpContentCompressorTest { private static FullHttpRequest newRequest() { FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"); - req.headers().set(Names.ACCEPT_ENCODING, "gzip"); + req.headers().set(HttpHeaderNames.ACCEPT_ENCODING, "gzip"); return req; } @@ -309,8 +307,8 @@ public class HttpContentCompressorTest { HttpResponse res = (HttpResponse) o; assertThat(res, is(not(instanceOf(HttpContent.class)))); - assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked")); - assertThat(res.headers().get(Names.CONTENT_LENGTH), is(nullValue())); - assertThat(res.headers().get(Names.CONTENT_ENCODING), is("gzip")); + assertThat(res.headers().get(HttpHeaderNames.TRANSFER_ENCODING), is("chunked")); + assertThat(res.headers().get(HttpHeaderNames.CONTENT_LENGTH), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING), is("gzip")); } } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentEncoderTest.java index 681e19acfa..71f5d12848 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentEncoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentEncoderTest.java @@ -21,8 +21,6 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.MessageToByteEncoder; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.util.CharsetUtil; import org.junit.Test; @@ -83,7 +81,7 @@ public class HttpContentEncoderTest { ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/")); HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED); + res.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); ch.writeOutbound(res); assertEncodedResponse(ch); @@ -120,7 +118,7 @@ public class HttpContentEncoderTest { ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/")); HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED); + res.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); ch.writeOutbound(res); assertEncodedResponse(ch); @@ -161,7 +159,7 @@ public class HttpContentEncoderTest { FullHttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(new byte[42])); - res.headers().set(Names.CONTENT_LENGTH, 42); + res.headers().set(HttpHeaderNames.CONTENT_LENGTH, 42); ch.writeOutbound(res); assertEncodedResponse(ch); @@ -219,10 +217,10 @@ public class HttpContentEncoderTest { assertThat(o, is(instanceOf(FullHttpResponse.class))); res = (FullHttpResponse) o; - assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.TRANSFER_ENCODING), is(nullValue())); // Content encoding shouldn't be modified. - assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING), is(nullValue())); assertThat(res.content().readableBytes(), is(0)); assertThat(res.content().toString(CharsetUtil.US_ASCII), is("")); res.release(); @@ -244,10 +242,10 @@ public class HttpContentEncoderTest { assertThat(o, is(instanceOf(FullHttpResponse.class))); res = (FullHttpResponse) o; - assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.TRANSFER_ENCODING), is(nullValue())); // Content encoding shouldn't be modified. - assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING), is(nullValue())); assertThat(res.content().readableBytes(), is(0)); assertThat(res.content().toString(CharsetUtil.US_ASCII), is("")); assertEquals("Netty", res.trailingHeaders().get("X-Test")); @@ -260,8 +258,8 @@ public class HttpContentEncoderTest { HttpResponse res = (HttpResponse) o; assertThat(res, is(not(instanceOf(HttpContent.class)))); - assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked")); - assertThat(res.headers().get(Names.CONTENT_LENGTH), is(nullValue())); - assertThat(res.headers().get(Names.CONTENT_ENCODING), is("test")); + assertThat(res.headers().get(HttpHeaderNames.TRANSFER_ENCODING), is("chunked")); + assertThat(res.headers().get(HttpHeaderNames.CONTENT_LENGTH), is(nullValue())); + assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING), is("test")); } } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeadersTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeadersTest.java index fa67920f6c..dbf20b2a84 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeadersTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpHeadersTest.java @@ -28,9 +28,9 @@ public class HttpHeadersTest { @Test public void testRemoveTransferEncodingIgnoreCase() { HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - message.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, "Chunked"); + message.headers().set(HttpHeaderNames.TRANSFER_ENCODING, "Chunked"); assertFalse(message.headers().isEmpty()); - HttpHeaders.removeTransferEncodingChunked(message); + HttpHeaderUtil.setTransferEncodingChunked(message, false); assertTrue(message.headers().isEmpty()); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java index 21d9b865ae..95d2985501 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java @@ -22,7 +22,6 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.DecoderResultProvider; import io.netty.handler.codec.TooLongFrameException; -import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.util.CharsetUtil; import org.easymock.EasyMock; import org.junit.Test; @@ -42,7 +41,7 @@ public class HttpObjectAggregatorTest { EmbeddedChannel embedder = new EmbeddedChannel(aggr); HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost"); - HttpHeaders.setHeader(message, "X-Test", true); + message.headers().set("X-Test", true); HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); HttpContent chunk3 = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER); @@ -57,7 +56,7 @@ public class HttpObjectAggregatorTest { assertNotNull(aggratedMessage); assertEquals(chunk1.content().readableBytes() + chunk2.content().readableBytes(), - HttpHeaders.getContentLength(aggratedMessage)); + HttpHeaderUtil.getContentLength(aggratedMessage)); assertEquals(aggratedMessage.headers().get("X-Test"), Boolean.TRUE.toString()); checkContentBuffer(aggratedMessage); assertNull(embedder.readInbound()); @@ -80,8 +79,8 @@ public class HttpObjectAggregatorTest { HttpObjectAggregator aggr = new HttpObjectAggregator(1024 * 1024); EmbeddedChannel embedder = new EmbeddedChannel(aggr); HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost"); - HttpHeaders.setHeader(message, "X-Test", true); - HttpHeaders.setTransferEncodingChunked(message); + message.headers().set("X-Test", true); + HttpHeaderUtil.setTransferEncodingChunked(message, true); HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); LastHttpContent trailer = new DefaultLastHttpContent(); @@ -98,7 +97,7 @@ public class HttpObjectAggregatorTest { assertNotNull(aggratedMessage); assertEquals(chunk1.content().readableBytes() + chunk2.content().readableBytes(), - HttpHeaders.getContentLength(aggratedMessage)); + HttpHeaderUtil.getContentLength(aggratedMessage)); assertEquals(aggratedMessage.headers().get("X-Test"), Boolean.TRUE.toString()); assertEquals(aggratedMessage.trailingHeaders().get("X-Trailer"), Boolean.TRUE.toString()); checkContentBuffer(aggratedMessage); @@ -119,7 +118,7 @@ public class HttpObjectAggregatorTest { FullHttpResponse response = embedder.readOutbound(); assertEquals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status()); - assertEquals("0", response.headers().get(Names.CONTENT_LENGTH)); + assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH)); assertFalse(embedder.isOpen()); try { @@ -136,14 +135,14 @@ public class HttpObjectAggregatorTest { public void testOversizedRequestWithoutKeepAlive() { // send a HTTP/1.0 request with no keep-alive header HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.PUT, "http://localhost"); - HttpHeaders.setContentLength(message, 5); + HttpHeaderUtil.setContentLength(message, 5); checkOversizedRequest(message); } @Test public void testOversizedRequestWithContentLength() { HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "http://localhost"); - HttpHeaders.setContentLength(message, 5); + HttpHeaderUtil.setContentLength(message, 5); checkOversizedRequest(message); } @@ -153,8 +152,8 @@ public class HttpObjectAggregatorTest { // send an oversized request with 100 continue HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "http://localhost"); - HttpHeaders.set100ContinueExpected(message); - HttpHeaders.setContentLength(message, 16); + HttpHeaderUtil.set100ContinueExpected(message, true); + HttpHeaderUtil.setContentLength(message, 16); HttpContent chunk1 = releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer("some", CharsetUtil.US_ASCII))); HttpContent chunk2 = releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII))); @@ -166,7 +165,7 @@ public class HttpObjectAggregatorTest { // The agregator should respond with '413 Request Entity Too Large.' FullHttpResponse response = embedder.readOutbound(); assertEquals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status()); - assertEquals("0", response.headers().get(Names.CONTENT_LENGTH)); + assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH)); // An ill-behaving client could continue to send data without a respect, and such data should be discarded. assertFalse(embedder.writeInbound(chunk1)); @@ -186,9 +185,9 @@ public class HttpObjectAggregatorTest { assertEquals( chunk2.content().readableBytes() + chunk3.content().readableBytes(), - HttpHeaders.getContentLength(fullMsg)); + HttpHeaderUtil.getContentLength(fullMsg)); - assertEquals(HttpHeaders.getContentLength(fullMsg), fullMsg.content().readableBytes()); + assertEquals(HttpHeaderUtil.getContentLength(fullMsg), fullMsg.content().readableBytes()); fullMsg.release(); assertFalse(embedder.finish()); @@ -206,7 +205,7 @@ public class HttpObjectAggregatorTest { FullHttpResponse response = embedder.readOutbound(); assertEquals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status()); - assertEquals("0", response.headers().get(Names.CONTENT_LENGTH)); + assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH)); // Keep-alive is on by default in HTTP/1.1, so the connection should be still alive. assertTrue(embedder.isOpen()); @@ -229,8 +228,8 @@ public class HttpObjectAggregatorTest { // Write first request with Expect: 100-continue HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "http://localhost"); - HttpHeaders.set100ContinueExpected(message); - HttpHeaders.setContentLength(message, 16); + HttpHeaderUtil.set100ContinueExpected(message, true); + HttpHeaderUtil.setContentLength(message, 16); HttpContent chunk1 = releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer("some", CharsetUtil.US_ASCII))); HttpContent chunk2 = releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII))); @@ -242,7 +241,7 @@ public class HttpObjectAggregatorTest { // The agregator should respond with '413 Request Entity Too Large.' FullHttpResponse response = embedder.readOutbound(); assertEquals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status()); - assertEquals("0", response.headers().get(Names.CONTENT_LENGTH)); + assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH)); // An ill-behaving client could continue to send data without a respect, and such data should be discarded. assertFalse(embedder.writeInbound(chunk1)); @@ -262,9 +261,9 @@ public class HttpObjectAggregatorTest { assertEquals( chunk2.content().readableBytes() + chunk3.content().readableBytes(), - HttpHeaders.getContentLength(fullMsg)); + HttpHeaderUtil.getContentLength(fullMsg)); - assertEquals(HttpHeaders.getContentLength(fullMsg), fullMsg.content().readableBytes()); + assertEquals(HttpHeaderUtil.getContentLength(fullMsg), fullMsg.content().readableBytes()); fullMsg.release(); assertFalse(embedder.finish()); @@ -276,7 +275,7 @@ public class HttpObjectAggregatorTest { assertFalse(embedder.writeInbound(message)); HttpResponse response = embedder.readOutbound(); assertEquals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status()); - assertEquals("0", response.headers().get(Names.CONTENT_LENGTH)); + assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH)); if (serverShouldCloseConnection(message)) { assertFalse(embedder.isOpen()); @@ -289,10 +288,10 @@ public class HttpObjectAggregatorTest { private static boolean serverShouldCloseConnection(HttpRequest message) { // The connection should only be kept open if Expect: 100-continue is set, // or if keep-alive is on. - if (HttpHeaders.is100ContinueExpected(message)) { + if (HttpHeaderUtil.is100ContinueExpected(message)) { return false; } - if (HttpHeaders.isKeepAlive(message)) { + if (HttpHeaderUtil.isKeepAlive(message)) { return false; } return true; @@ -345,8 +344,8 @@ public class HttpObjectAggregatorTest { EmbeddedChannel embedder = new EmbeddedChannel(aggr); HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "http://localhost"); - HttpHeaders.setHeader(message, "X-Test", true); - HttpHeaders.setHeader(message, "Transfer-Encoding", "Chunked"); + message.headers().set("X-Test", true); + message.headers().set("Transfer-Encoding", "Chunked"); HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); HttpContent chunk3 = LastHttpContent.EMPTY_LAST_CONTENT; @@ -361,7 +360,7 @@ public class HttpObjectAggregatorTest { assertNotNull(aggratedMessage); assertEquals(chunk1.content().readableBytes() + chunk2.content().readableBytes(), - HttpHeaders.getContentLength(aggratedMessage)); + HttpHeaderUtil.getContentLength(aggratedMessage)); assertEquals(aggratedMessage.headers().get("X-Test"), Boolean.TRUE.toString()); checkContentBuffer(aggratedMessage); assertNull(embedder.readInbound()); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java index 03947a00fe..74117f31bb 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java @@ -17,7 +17,6 @@ package io.netty.handler.codec.http; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.util.CharsetUtil; import org.junit.Test; @@ -184,7 +183,7 @@ public class HttpResponseDecoderTest { HttpResponse res = ch.readInbound(); assertThat(res.protocolVersion(), sameInstance(HttpVersion.HTTP_1_1)); assertThat(res.status(), is(HttpResponseStatus.OK)); - assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked")); + assertThat(res.headers().get(HttpHeaderNames.TRANSFER_ENCODING), is("chunked")); assertThat(ch.readInbound(), is(nullValue())); // Close the connection without sending anything. @@ -205,7 +204,7 @@ public class HttpResponseDecoderTest { HttpResponse res = ch.readInbound(); assertThat(res.protocolVersion(), sameInstance(HttpVersion.HTTP_1_1)); assertThat(res.status(), is(HttpResponseStatus.OK)); - assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked")); + assertThat(res.headers().get(HttpHeaderNames.TRANSFER_ENCODING), is("chunked")); // Read the partial content. HttpContent content = ch.readInbound(); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java index bf9f99d4ea..923ea36d50 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java @@ -36,13 +36,13 @@ public class HttpResponseEncoderTest { public void testLargeFileRegionChunked() throws Exception { EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseEncoder()); HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - response.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); + response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); assertTrue(channel.writeOutbound(response)); ByteBuf buffer = channel.readOutbound(); - assertEquals("HTTP/1.1 200 OK\r\n" + HttpHeaders.Names.TRANSFER_ENCODING + ": " + - HttpHeaders.Values.CHUNKED + "\r\n\r\n", buffer.toString(CharsetUtil.US_ASCII)); + assertEquals("HTTP/1.1 200 OK\r\n" + HttpHeaderNames.TRANSFER_ENCODING + ": " + + HttpHeaderValues.CHUNKED + "\r\n\r\n", buffer.toString(CharsetUtil.US_ASCII)); buffer.release(); assertTrue(channel.writeOutbound(FILE_REGION)); buffer = channel.readOutbound(); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java index b9c5f61682..345e9e821f 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java @@ -21,7 +21,6 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.CharsetUtil; import org.junit.Test; -import static io.netty.handler.codec.http.HttpHeaders.Names.*; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; @@ -92,7 +91,7 @@ public class HttpServerCodecTest { // Ensure the aggregator generates a full request. FullHttpRequest req = ch.readInbound(); - assertThat(req.headers().get(CONTENT_LENGTH), is("1")); + assertThat(req.headers().get(HttpHeaderNames.CONTENT_LENGTH), is("1")); assertThat(req.content().readableBytes(), is(1)); assertThat(req.content().readByte(), is((byte) 42)); req.release(); @@ -103,13 +102,13 @@ public class HttpServerCodecTest { // Send the actual response. FullHttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CREATED); res.content().writeBytes("OK".getBytes(CharsetUtil.UTF_8)); - res.headers().set(CONTENT_LENGTH, 2); + res.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 2); ch.writeOutbound(res); // Ensure the encoder handles the response after handling 100 Continue. ByteBuf encodedRes = ch.readOutbound(); - assertThat(encodedRes.toString(CharsetUtil.UTF_8), is("HTTP/1.1 201 Created\r\n" + - CONTENT_LENGTH + ": 2\r\n\r\nOK")); + assertThat(encodedRes.toString(CharsetUtil.UTF_8), + is("HTTP/1.1 201 Created\r\n" + HttpHeaderNames.CONTENT_LENGTH + ": 2\r\n\r\nOK")); encodedRes.release(); ch.finish(); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsConfigTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsConfigTest.java index 748bb7017f..6ad0251fe7 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsConfigTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsConfigTest.java @@ -15,8 +15,8 @@ */ package io.netty.handler.codec.http.cors; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpMethod; import org.junit.Test; @@ -108,8 +108,8 @@ public class CorsConfigTest { @Test public void defaultPreflightResponseHeaders() { final CorsConfig cors = withAnyOrigin().build(); - assertThat(cors.preflightResponseHeaders().get(Names.DATE), is(notNullValue())); - assertThat(cors.preflightResponseHeaders().get(Names.CONTENT_LENGTH), is("0")); + assertThat(cors.preflightResponseHeaders().get(HttpHeaderNames.DATE), is(notNullValue())); + assertThat(cors.preflightResponseHeaders().get(HttpHeaderNames.CONTENT_LENGTH), is("0")); } @Test diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java index 6dc82e25c2..8215bc002d 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java @@ -28,10 +28,9 @@ import org.junit.Test; import java.util.Arrays; import java.util.concurrent.Callable; -import static io.netty.handler.codec.http.HttpHeaders.Names.*; +import static io.netty.handler.codec.http.HttpHeaderNames.*; import static io.netty.handler.codec.http.HttpMethod.*; -import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN; -import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.*; diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java index 500d47e386..ae54ffb34f 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java @@ -24,7 +24,8 @@ import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.DefaultHttpContent; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpContent; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.LastHttpContent; @@ -62,8 +63,8 @@ public class HttpPostRequestDecoderTest { "http://localhost"); req.setDecoderResult(DecoderResult.SUCCESS); - req.headers().add(HttpHeaders.Names.CONTENT_TYPE, contentTypeValue); - req.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); + req.headers().add(HttpHeaderNames.CONTENT_TYPE, contentTypeValue); + req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); // Force to use memory-based data. final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false); @@ -106,8 +107,8 @@ public class HttpPostRequestDecoderTest { "http://localhost"); req.setDecoderResult(DecoderResult.SUCCESS); - req.headers().add(HttpHeaders.Names.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); - req.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); + req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); + req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); // Force to use memory-based data. final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false); @@ -150,8 +151,8 @@ public class HttpPostRequestDecoderTest { final DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost"); req.setDecoderResult(DecoderResult.SUCCESS); - req.headers().add(HttpHeaders.Names.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); - req.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); + req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); + req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file" + i + "\"\r\n" + @@ -185,8 +186,8 @@ public class HttpPostRequestDecoderTest { "http://localhost"); req.setDecoderResult(DecoderResult.SUCCESS); - req.headers().add(HttpHeaders.Names.CONTENT_TYPE, "multipart/form-data; boundary=\"" + boundary + '"'); - req.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); + req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=\"" + boundary + '"'); + req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); // Force to use memory-based data. final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false); @@ -218,10 +219,10 @@ public class HttpPostRequestDecoderTest { DefaultHttpRequest aRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost"); - aRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, + aRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); - aRequest.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, - HttpHeaders.Values.CHUNKED); + aRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, + HttpHeaderValues.CHUNKED); HttpPostRequestDecoder aDecoder = new HttpPostRequestDecoder(aMemFactory, aRequest); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java index a74e1c1ce3..242d3d5a59 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java @@ -18,18 +18,17 @@ package io.netty.handler.codec.http.multipart; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.handler.codec.http.DefaultFullHttpRequest; -import static io.netty.handler.codec.http.HttpHeaders.Names.*; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode; import io.netty.util.CharsetUtil; import io.netty.util.internal.StringUtil; - import org.junit.Test; import java.io.File; import java.util.List; +import static io.netty.handler.codec.http.HttpHeaderNames.*; import static org.junit.Assert.*; /** {@link HttpPostRequestEncoder} test case. */ diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketRequestBuilder.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketRequestBuilder.java index 7f1ac827bc..155e900af8 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketRequestBuilder.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketRequestBuilder.java @@ -17,13 +17,13 @@ package io.netty.handler.codec.http.websocketx; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpVersion; -import static io.netty.handler.codec.http.HttpHeaders.Values.*; import static io.netty.handler.codec.http.HttpVersion.*; public class WebSocketRequestBuilder { @@ -48,33 +48,57 @@ public class WebSocketRequestBuilder { return this; } - public WebSocketRequestBuilder uri(String uri) { - this.uri = uri; + public WebSocketRequestBuilder uri(CharSequence uri) { + if (uri == null) { + this.uri = null; + } else { + this.uri = uri.toString(); + } return this; } - public WebSocketRequestBuilder host(String host) { - this.host = host; + public WebSocketRequestBuilder host(CharSequence host) { + if (host == null) { + this.host = null; + } else { + this.host = host.toString(); + } return this; } - public WebSocketRequestBuilder upgrade(String upgrade) { - this.upgrade = upgrade; + public WebSocketRequestBuilder upgrade(CharSequence upgrade) { + if (upgrade == null) { + this.upgrade = null; + } else { + this.upgrade = upgrade.toString(); + } return this; } - public WebSocketRequestBuilder connection(String connection) { - this.connection = connection; + public WebSocketRequestBuilder connection(CharSequence connection) { + if (connection == null) { + this.connection = null; + } else { + this.connection = connection.toString(); + } return this; } - public WebSocketRequestBuilder key(String key) { - this.key = key; + public WebSocketRequestBuilder key(CharSequence key) { + if (key == null) { + this.key = null; + } else { + this.key = key.toString(); + } return this; } - public WebSocketRequestBuilder origin(String origin) { - this.origin = origin; + public WebSocketRequestBuilder origin(CharSequence origin) { + if (origin == null) { + this.origin = null; + } else { + this.origin = origin.toString(); + } return this; } @@ -102,22 +126,22 @@ public class WebSocketRequestBuilder { HttpHeaders headers = req.headers(); if (host != null) { - headers.set(Names.HOST, host); + headers.set(HttpHeaderNames.HOST, host); } if (upgrade != null) { - headers.set(Names.UPGRADE, upgrade); + headers.set(HttpHeaderNames.UPGRADE, upgrade); } if (connection != null) { - headers.set(Names.CONNECTION, connection); + headers.set(HttpHeaderNames.CONNECTION, connection); } if (key != null) { - headers.set(Names.SEC_WEBSOCKET_KEY, key); + headers.set(HttpHeaderNames.SEC_WEBSOCKET_KEY, key); } if (origin != null) { - headers.set(Names.SEC_WEBSOCKET_ORIGIN, origin); + headers.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, origin); } if (version != null) { - headers.set(Names.SEC_WEBSOCKET_VERSION, version.toHttpHeaderValue()); + headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version.toHttpHeaderValue()); } return req; } @@ -127,7 +151,7 @@ public class WebSocketRequestBuilder { .method(HttpMethod.GET) .uri("/test") .host("server.example.com") - .upgrade(WEBSOCKET.toLowerCase().toString()) + .upgrade(HttpHeaderValues.WEBSOCKET) .key("dGhlIHNhbXBsZSBub25jZQ==") .origin("http://example.com") .version13() diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00Test.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00Test.java index c6e433948d..6fbcc3afa8 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00Test.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00Test.java @@ -19,7 +19,8 @@ import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpHeaders.Names; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestDecoder; @@ -32,7 +33,6 @@ import io.netty.util.ReferenceCountUtil; import org.junit.Assert; import org.junit.Test; -import static io.netty.handler.codec.http.HttpHeaders.Values.*; import static io.netty.handler.codec.http.HttpVersion.*; public class WebSocketServerHandshaker00Test { @@ -54,13 +54,13 @@ public class WebSocketServerHandshaker00Test { FullHttpRequest req = ReferenceCountUtil.releaseLater(new DefaultFullHttpRequest( HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII))); - req.headers().set(Names.HOST, "server.example.com"); - req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase()); - req.headers().set(Names.CONNECTION, "Upgrade"); - req.headers().set(Names.ORIGIN, "http://example.com"); - req.headers().set(Names.SEC_WEBSOCKET_KEY1, "4 @1 46546xW%0l 1 5"); - req.headers().set(Names.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1 .P00"); - req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); + req.headers().set(HttpHeaderNames.HOST, "server.example.com"); + req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); + req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade"); + req.headers().set(HttpHeaderNames.ORIGIN, "http://example.com"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1 46546xW%0l 1 5"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1 .P00"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); if (subProtocol) { new WebSocketServerHandshaker00( @@ -74,12 +74,12 @@ public class WebSocketServerHandshaker00Test { ch2.writeInbound(ch.readOutbound()); HttpResponse res = ch2.readInbound(); - Assert.assertEquals("ws://example.com/chat", res.headers().get(Names.SEC_WEBSOCKET_LOCATION)); + Assert.assertEquals("ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION)); if (subProtocol) { - Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL)); + Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } else { - Assert.assertNull(res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL)); + Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } LastHttpContent content = ch2.readInbound(); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08Test.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08Test.java index cb6d133287..c09dba8209 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08Test.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08Test.java @@ -19,7 +19,8 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpHeaders.Names; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestDecoder; @@ -30,7 +31,6 @@ import io.netty.util.ReferenceCountUtil; import org.junit.Assert; import org.junit.Test; -import static io.netty.handler.codec.http.HttpHeaders.Values.*; import static io.netty.handler.codec.http.HttpVersion.*; public class WebSocketServerHandshaker08Test { @@ -51,13 +51,13 @@ public class WebSocketServerHandshaker08Test { FullHttpRequest req = ReferenceCountUtil.releaseLater( new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat")); - req.headers().set(Names.HOST, "server.example.com"); - req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase()); - req.headers().set(Names.CONNECTION, "Upgrade"); - req.headers().set(Names.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ=="); - req.headers().set(Names.SEC_WEBSOCKET_ORIGIN, "http://example.com"); - req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); - req.headers().set(Names.SEC_WEBSOCKET_VERSION, "8"); + req.headers().set(HttpHeaderNames.HOST, "server.example.com"); + req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); + req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ=="); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "8"); if (subProtocol) { new WebSocketServerHandshaker08( @@ -74,11 +74,11 @@ public class WebSocketServerHandshaker08Test { HttpResponse res = ch2.readInbound(); Assert.assertEquals( - "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT)); + "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT)); if (subProtocol) { - Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL)); + Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } else { - Assert.assertNull(res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL)); + Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } ReferenceCountUtil.release(res); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13Test.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13Test.java index 7bc0368c90..47ad4d7c3b 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13Test.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13Test.java @@ -19,7 +19,8 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpHeaders.Names; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestDecoder; @@ -30,7 +31,6 @@ import io.netty.util.ReferenceCountUtil; import org.junit.Assert; import org.junit.Test; -import static io.netty.handler.codec.http.HttpHeaders.Values.*; import static io.netty.handler.codec.http.HttpVersion.*; public class WebSocketServerHandshaker13Test { @@ -51,13 +51,13 @@ public class WebSocketServerHandshaker13Test { FullHttpRequest req = ReferenceCountUtil.releaseLater( new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat")); - req.headers().set(Names.HOST, "server.example.com"); - req.headers().set(Names.UPGRADE, WEBSOCKET.toLowerCase()); - req.headers().set(Names.CONNECTION, "Upgrade"); - req.headers().set(Names.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ=="); - req.headers().set(Names.SEC_WEBSOCKET_ORIGIN, "http://example.com"); - req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); - req.headers().set(Names.SEC_WEBSOCKET_VERSION, "13"); + req.headers().set(HttpHeaderNames.HOST, "server.example.com"); + req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); + req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ=="); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); + req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13"); if (subProtocol) { new WebSocketServerHandshaker13( @@ -74,11 +74,11 @@ public class WebSocketServerHandshaker13Test { HttpResponse res = ch2.readInbound(); Assert.assertEquals( - "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT)); + "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT)); if (subProtocol) { - Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL)); + Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } else { - Assert.assertNull(res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL)); + Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } ReferenceCountUtil.release(res); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java index efe9cfabbc..3da5720cde 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java @@ -24,6 +24,7 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequestDecoder; @@ -35,7 +36,6 @@ import org.junit.Test; import java.util.ArrayDeque; import java.util.Queue; -import static io.netty.handler.codec.http.HttpHeaders.Values.*; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; import static org.junit.Assert.*; @@ -95,7 +95,7 @@ public class WebSocketServerProtocolHandlerTest { .uri("/test") .key(null) .connection("Upgrade") - .upgrade(WEBSOCKET.toLowerCase().toString()) + .upgrade(HttpHeaderValues.WEBSOCKET) .version13() .build(); diff --git a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java index add631edb1..7e6150eb7d 100644 --- a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java +++ b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java @@ -29,7 +29,9 @@ import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpChunkedInput; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderUtil; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.LastHttpContent; @@ -52,7 +54,6 @@ import java.util.Locale; import java.util.TimeZone; import java.util.regex.Pattern; -import static io.netty.handler.codec.http.HttpHeaders.Names.*; import static io.netty.handler.codec.http.HttpMethod.*; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; @@ -149,7 +150,7 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler if (msg instanceof HttpRequest) { HttpRequest request = this.request = (HttpRequest) msg; - if (HttpHeaders.is100ContinueExpected(request)) { + if (HttpHeaderUtil.is100ContinueExpected(request)) { send100Continue(ctx); } @@ -68,12 +70,12 @@ public class HttpSnoopServerHandler extends SimpleChannelInboundHandler buf.append("===================================\r\n"); buf.append("VERSION: ").append(request.protocolVersion()).append("\r\n"); - buf.append("HOSTNAME: ").append(HttpHeaders.getHost(request, "unknown")).append("\r\n"); + buf.append("HOSTNAME: ").append(request.headers().get(HttpHeaderNames.HOST, "unknown")).append("\r\n"); buf.append("REQUEST_URI: ").append(request.uri()).append("\r\n\r\n"); HttpHeaders headers = request.headers(); if (!headers.isEmpty()) { - for (Map.Entry h: headers) { + for (Map.Entry h: headers) { CharSequence key = h.getKey(); CharSequence value = h.getValue(); buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n"); @@ -144,36 +146,36 @@ public class HttpSnoopServerHandler extends SimpleChannelInboundHandler private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. - boolean keepAlive = HttpHeaders.isKeepAlive(request); + boolean keepAlive = HttpHeaderUtil.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.decoderResult().isSuccess()? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); - response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); + response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. - response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); + response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); // Add keep alive header as per: // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection - response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); + response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } // Encode the cookie. - String cookieString = request.headers().getAndConvert(COOKIE); + String cookieString = request.headers().get(HttpHeaderNames.COOKIE); if (cookieString != null) { Set cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie: cookies) { - response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); + response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. - response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); - response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); + response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); + response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. diff --git a/example/src/main/java/io/netty/example/http/upload/HttpUploadClient.java b/example/src/main/java/io/netty/example/http/upload/HttpUploadClient.java index b2f148d387..3ee0ae43b2 100644 --- a/example/src/main/java/io/netty/example/http/upload/HttpUploadClient.java +++ b/example/src/main/java/io/netty/example/http/upload/HttpUploadClient.java @@ -24,6 +24,8 @@ import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.ClientCookieEncoder; import io.netty.handler.codec.http.DefaultCookie; import io.netty.handler.codec.http.DefaultHttpRequest; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; @@ -162,28 +164,28 @@ public final class HttpUploadClient { URI uriGet = new URI(encoder.toString()); HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString()); HttpHeaders headers = request.headers(); - headers.set(HttpHeaders.Names.HOST, host); - headers.set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); - headers.set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP + ',' + HttpHeaders.Values.DEFLATE); + headers.set(HttpHeaderNames.HOST, host); + headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); + headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE); - headers.set(HttpHeaders.Names.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); - headers.set(HttpHeaders.Names.ACCEPT_LANGUAGE, "fr"); - headers.set(HttpHeaders.Names.REFERER, uriSimple.toString()); - headers.set(HttpHeaders.Names.USER_AGENT, "Netty Simple Http Client side"); - headers.set(HttpHeaders.Names.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + headers.set(HttpHeaderNames.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); + headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "fr"); + headers.set(HttpHeaderNames.REFERER, uriSimple.toString()); + headers.set(HttpHeaderNames.USER_AGENT, "Netty Simple Http Client side"); + headers.set(HttpHeaderNames.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); //connection will not close but needed // headers.set("Connection","keep-alive"); // headers.set("Keep-Alive","300"); headers.set( - HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode( + HttpHeaderNames.COOKIE, ClientCookieEncoder.encode( new DefaultCookie("my-cookie", "foo"), new DefaultCookie("another-cookie", "bar")) ); // send request - List> entries = headers.entriesConverted(); + List> entries = headers.entries(); channel.writeAndFlush(request); // Wait for the server to close the connection. diff --git a/example/src/main/java/io/netty/example/http/upload/HttpUploadClientHandler.java b/example/src/main/java/io/netty/example/http/upload/HttpUploadClientHandler.java index c58bfb0b4a..4db2c2c2e3 100644 --- a/example/src/main/java/io/netty/example/http/upload/HttpUploadClientHandler.java +++ b/example/src/main/java/io/netty/example/http/upload/HttpUploadClientHandler.java @@ -18,7 +18,7 @@ package io.netty.example.http.upload; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.HttpContent; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderUtil; import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.LastHttpContent; @@ -47,7 +47,7 @@ public class HttpUploadClientHandler extends SimpleChannelInboundHandler { @@ -113,14 +114,14 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler entry : request.headers()) { + for (Entry entry : request.headers()) { responseContent.append("HEADER: " + entry.getKey() + '=' + entry.getValue() + "\r\n"); } responseContent.append("\r\n\r\n"); // new getMethod Set cookies; - String value = request.headers().getAndConvert(COOKIE); + String value = request.headers().get(HttpHeaderNames.COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { @@ -158,7 +159,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler cookies; - String value = request.headers().getAndConvert(COOKIE); + String value = request.headers().get(HttpHeaderNames.COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { @@ -312,7 +313,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler ByteBuf content = WebSocketServerBenchmarkPage.getContent(getWebSocketLocation(req)); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); - res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); - HttpHeaders.setContentLength(res, content.readableBytes()); + res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8"); + HttpHeaderUtil.setContentLength(res, content.readableBytes()); sendHttpResponse(ctx, req, res); return; @@ -134,12 +134,12 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); - HttpHeaders.setContentLength(res, res.content().readableBytes()); + HttpHeaderUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); - if (!HttpHeaders.isKeepAlive(req) || res.status().code() != 200) { + if (!HttpHeaderUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } } @@ -151,7 +151,7 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler } private static String getWebSocketLocation(FullHttpRequest req) { - String location = req.headers().get(HOST) + WEBSOCKET_PATH; + String location = req.headers().get(HttpHeaderNames.HOST) + WEBSOCKET_PATH; if (WebSocketServer.SSL) { return "wss://" + location; } else { diff --git a/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServerHandler.java b/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServerHandler.java index 7712a71b83..76cd2bf221 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServerHandler.java +++ b/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServerHandler.java @@ -24,7 +24,8 @@ import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpHeaderUtil; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.PingWebSocketFrame; import io.netty.handler.codec.http.websocketx.PongWebSocketFrame; @@ -34,7 +35,6 @@ import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; import io.netty.util.CharsetUtil; -import static io.netty.handler.codec.http.HttpHeaders.Names.*; import static io.netty.handler.codec.http.HttpMethod.*; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; @@ -80,8 +80,8 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler ByteBuf content = WebSocketServerIndexPage.getContent(getWebSocketLocation(req)); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); - res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); - HttpHeaders.setContentLength(res, content.readableBytes()); + res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8"); + HttpHeaderUtil.setContentLength(res, content.readableBytes()); sendHttpResponse(ctx, req, res); return; @@ -132,12 +132,12 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); - HttpHeaders.setContentLength(res, res.content().readableBytes()); + HttpHeaderUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); - if (!HttpHeaders.isKeepAlive(req) || res.status().code() != 200) { + if (!HttpHeaderUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } } @@ -149,7 +149,7 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler } private static String getWebSocketLocation(FullHttpRequest req) { - String location = req.headers().get(HOST) + WEBSOCKET_PATH; + String location = req.headers().get(HttpHeaderNames.HOST) + WEBSOCKET_PATH; if (WebSocketServer.SSL) { return "wss://" + location; } else { diff --git a/example/src/main/java/io/netty/example/spdy/client/HttpResponseClientHandler.java b/example/src/main/java/io/netty/example/spdy/client/HttpResponseClientHandler.java index 44813f52b4..8983bb07cc 100644 --- a/example/src/main/java/io/netty/example/spdy/client/HttpResponseClientHandler.java +++ b/example/src/main/java/io/netty/example/spdy/client/HttpResponseClientHandler.java @@ -20,7 +20,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.example.http.snoop.HttpSnoopClientHandler; import io.netty.handler.codec.http.HttpContent; -import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpHeaderUtil; import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.LastHttpContent; @@ -55,7 +55,7 @@ public class HttpResponseClientHandler extends SimpleChannelInboundHandler { ByteBuf content = Unpooled.copiedBuffer("Hello World " + new Date(), CharsetUtil.UTF_8); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); - response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); - response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); + response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); + response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { - response.headers().set(CONNECTION, Values.KEEP_ALIVE); + response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.write(response); } } diff --git a/handler-proxy/src/main/java/io/netty/handler/proxy/HttpProxyHandler.java b/handler-proxy/src/main/java/io/netty/handler/proxy/HttpProxyHandler.java index 4cd9de41c4..55e450c0ed 100644 --- a/handler-proxy/src/main/java/io/netty/handler/proxy/HttpProxyHandler.java +++ b/handler-proxy/src/main/java/io/netty/handler/proxy/HttpProxyHandler.java @@ -25,7 +25,7 @@ import io.netty.handler.codec.base64.Base64; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpClientCodec; -import io.netty.handler.codec.http.HttpHeaders.Names; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponseStatus; @@ -127,11 +127,11 @@ public final class HttpProxyHandler extends ProxyHandler { SocketAddress proxyAddress = proxyAddress(); if (proxyAddress instanceof InetSocketAddress) { InetSocketAddress hostAddr = (InetSocketAddress) proxyAddress; - req.headers().set(Names.HOST, hostAddr.getHostString() + ':' + hostAddr.getPort()); + req.headers().set(HttpHeaderNames.HOST, hostAddr.getHostString() + ':' + hostAddr.getPort()); } if (authorization != null) { - req.headers().set(Names.AUTHORIZATION, authorization); + req.headers().set(HttpHeaderNames.AUTHORIZATION, authorization); } return req; diff --git a/handler-proxy/src/test/java/io/netty/handler/proxy/HttpProxyServer.java b/handler-proxy/src/test/java/io/netty/handler/proxy/HttpProxyServer.java index 7994490c7c..6097f75d39 100644 --- a/handler-proxy/src/test/java/io/netty/handler/proxy/HttpProxyServer.java +++ b/handler-proxy/src/test/java/io/netty/handler/proxy/HttpProxyServer.java @@ -26,7 +26,7 @@ import io.netty.handler.codec.base64.Base64; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; -import io.netty.handler.codec.http.HttpHeaders.Names; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestDecoder; @@ -86,7 +86,7 @@ final class HttpProxyServer extends ProxyServer { boolean authzSuccess = false; if (username != null) { - CharSequence authz = req.headers().get(Names.AUTHORIZATION); + CharSequence authz = req.headers().get(HttpHeaderNames.AUTHORIZATION); if (authz != null) { ByteBuf authzBuf64 = Unpooled.copiedBuffer(authz, CharsetUtil.US_ASCII); ByteBuf authzBuf = Base64.decode(authzBuf64); @@ -113,7 +113,7 @@ final class HttpProxyServer extends ProxyServer { FullHttpResponse res; if (!authenticate(ctx, req)) { res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED); - res.headers().set(Names.CONTENT_LENGTH, 0); + res.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0); } else { res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); String uri = req.uri(); @@ -144,10 +144,10 @@ final class HttpProxyServer extends ProxyServer { if (!authenticate(ctx, req)) { res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED); - res.headers().set(Names.CONTENT_LENGTH, 0); + res.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0); } else if (!req.uri().equals(destination.getHostString() + ':' + destination.getPort())) { res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN); - res.headers().set(Names.CONTENT_LENGTH, 0); + res.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0); } else { res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); sendGreeting = true; diff --git a/testsuite/src/test/java/io/netty/testsuite/websockets/autobahn/AutobahnServerHandler.java b/testsuite/src/test/java/io/netty/testsuite/websockets/autobahn/AutobahnServerHandler.java index 09dc4e9942..e999cb15c3 100644 --- a/testsuite/src/test/java/io/netty/testsuite/websockets/autobahn/AutobahnServerHandler.java +++ b/testsuite/src/test/java/io/netty/testsuite/websockets/autobahn/AutobahnServerHandler.java @@ -24,6 +24,7 @@ import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame; @@ -39,7 +40,7 @@ import io.netty.util.internal.StringUtil; import java.util.logging.Level; import java.util.logging.Logger; -import static io.netty.handler.codec.http.HttpHeaders.*; +import static io.netty.handler.codec.http.HttpHeaderUtil.*; import static io.netty.handler.codec.http.HttpMethod.*; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; @@ -142,6 +143,6 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter { } private static String getWebSocketLocation(FullHttpRequest req) { - return "ws://" + req.headers().get(Names.HOST); + return "ws://" + req.headers().get(HttpHeaderNames.HOST); } }