|
| 1 | +import io.netty.buffer.ByteBuf; |
| 2 | +import io.netty.buffer.ByteBufAllocator; |
| 3 | +import io.netty.buffer.ByteBufUtil; |
| 4 | +import io.netty.buffer.Unpooled; |
| 5 | +import io.netty.handler.codec.http.DefaultFullHttpRequest; |
| 6 | +import io.netty.handler.codec.http.HttpHeaderNames; |
| 7 | +import io.netty.handler.codec.http.HttpHeaderValues; |
| 8 | +import io.netty.handler.codec.http.HttpHeaders; |
| 9 | +import io.netty.handler.codec.http.HttpMethod; |
| 10 | +import io.netty.handler.codec.http.HttpVersion; |
| 11 | +import io.netty.util.AsciiString; |
| 12 | + |
| 13 | +import java.util.Iterator; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +public class Foo { |
| 17 | + |
| 18 | + public static void main(String[] args) { |
| 19 | + HttpVersion httpVersion = HttpVersion.HTTP_1_0; |
| 20 | + HttpMethod method = HttpMethod.GET; |
| 21 | + ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); |
| 22 | + ByteBuf content = Unpooled.EMPTY_BUFFER; |
| 23 | + // new DefaultFullHttpRequest(httpVersion,method, urlAddress,content); |
| 24 | + DefaultFullHttpRequest nettyHttpRequest = new DefaultFullHttpRequest(httpVersion, method, "http://localhost/aaa/bbb", content); |
| 25 | + // 头里加入host信息 |
| 26 | + nettyHttpRequest.headers().add(HttpHeaderNames.HOST, new AsciiString("192.168.3.52:8081")); |
| 27 | + nettyHttpRequest.headers().add(HttpHeaderNames.USER_AGENT, new AsciiString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0")); |
| 28 | + nettyHttpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, new AsciiString("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")); |
| 29 | + nettyHttpRequest.headers().add(HttpHeaderNames.ACCEPT_LANGUAGE, new AsciiString("zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")); |
| 30 | + nettyHttpRequest.headers().add(HttpHeaderNames.ACCEPT_ENCODING, new AsciiString("gzip, deflate")); |
| 31 | + nettyHttpRequest.headers().add(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO); |
| 32 | + nettyHttpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); |
| 33 | + try { |
| 34 | + long st = System.nanoTime(); |
| 35 | + encodeHeaders(nettyHttpRequest.headers(), buf); |
| 36 | + long et = System.nanoTime(); |
| 37 | + System.out.println("cost time :" + ((et - st) / 1_000_000)); |
| 38 | + } catch (Exception e) { |
| 39 | + |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + protected static void encodeHeaders(HttpHeaders headers, ByteBuf buf) throws Exception { |
| 44 | + Iterator<Map.Entry<CharSequence, CharSequence>> iter = headers.iteratorCharSequence(); |
| 45 | + while (iter.hasNext()) { |
| 46 | + Map.Entry<CharSequence, CharSequence> header = iter.next(); |
| 47 | + long st = System.nanoTime(); |
| 48 | + encoderHeader(header.getKey(), header.getValue(), buf); |
| 49 | + long et = System.nanoTime(); |
| 50 | + System.out.println("encode header " + header.getKey() + " value " + header.getValue() + " cost time :" + ((et - st) / 1_000_000)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void encoderHeader(CharSequence name, CharSequence value, ByteBuf buf) throws Exception { |
| 55 | + final int nameLen = name.length(); |
| 56 | + final int valueLen = value.length(); |
| 57 | + final int entryLen = nameLen + valueLen + 4; |
| 58 | + buf.ensureWritable(entryLen); |
| 59 | + int offset = buf.writerIndex(); |
| 60 | + writeAscii(buf, offset, name, nameLen); |
| 61 | + offset += nameLen; |
| 62 | + buf.setByte(offset++, ':'); |
| 63 | + buf.setByte(offset++, ' '); |
| 64 | + writeAscii(buf, offset, value, valueLen); |
| 65 | + offset += valueLen; |
| 66 | + buf.setByte(offset++, '\r'); |
| 67 | + buf.setByte(offset++, '\n'); |
| 68 | + buf.writerIndex(offset); |
| 69 | + } |
| 70 | + |
| 71 | + private static void writeAscii(ByteBuf buf, int offset, CharSequence value, int valueLen) { |
| 72 | + if (value instanceof AsciiString) { |
| 73 | + long st = System.nanoTime(); |
| 74 | + ByteBufUtil.copy((AsciiString) value, 0, buf, offset, valueLen); |
| 75 | + long et = System.nanoTime(); |
| 76 | + // System.out.println("writeAscii header "+value+" value cost time :"+(et-st)); |
| 77 | + |
| 78 | + } else { |
| 79 | + |
| 80 | + writeCharSequence(buf, offset, value, valueLen); |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static void writeCharSequence(ByteBuf buf, int offset, CharSequence value, int valueLen) { |
| 86 | + for (int i = 0; i < valueLen; ++i) { |
| 87 | + buf.setByte(offset++, c2b(value.charAt(i))); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments