|
| 1 | +importio.netty.buffer.ByteBuf; |
| 2 | +importio.netty.buffer.ByteBufAllocator; |
| 3 | +importio.netty.buffer.ByteBufUtil; |
| 4 | +importio.netty.buffer.Unpooled; |
| 5 | +importio.netty.handler.codec.http.DefaultFullHttpRequest; |
| 6 | +importio.netty.handler.codec.http.HttpHeaderNames; |
| 7 | +importio.netty.handler.codec.http.HttpHeaderValues; |
| 8 | +importio.netty.handler.codec.http.HttpHeaders; |
| 9 | +importio.netty.handler.codec.http.HttpMethod; |
| 10 | +importio.netty.handler.codec.http.HttpVersion; |
| 11 | +importio.netty.util.AsciiString; |
| 12 | + |
| 13 | +importjava.util.Iterator; |
| 14 | +importjava.util.Map; |
| 15 | + |
| 16 | +publicclassFoo { |
| 17 | + |
| 18 | +publicstaticvoidmain(String[]args) { |
| 19 | +HttpVersionhttpVersion =HttpVersion.HTTP_1_0; |
| 20 | +HttpMethodmethod =HttpMethod.GET; |
| 21 | +ByteBufbuf =ByteBufAllocator.DEFAULT.buffer(); |
| 22 | +ByteBufcontent =Unpooled.EMPTY_BUFFER; |
| 23 | +// new DefaultFullHttpRequest(httpVersion,method, urlAddress,content); |
| 24 | +DefaultFullHttpRequestnettyHttpRequest =newDefaultFullHttpRequest(httpVersion,method,"http://localhost/aaa/bbb",content); |
| 25 | +// 头里加入host信息 |
| 26 | +nettyHttpRequest.headers().add(HttpHeaderNames.HOST,newAsciiString("192.168.3.52:8081")); |
| 27 | +nettyHttpRequest.headers().add(HttpHeaderNames.USER_AGENT,newAsciiString("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,newAsciiString("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")); |
| 29 | +nettyHttpRequest.headers().add(HttpHeaderNames.ACCEPT_LANGUAGE,newAsciiString("zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")); |
| 30 | +nettyHttpRequest.headers().add(HttpHeaderNames.ACCEPT_ENCODING,newAsciiString("gzip, deflate")); |
| 31 | +nettyHttpRequest.headers().add(HttpHeaderNames.CONTENT_LENGTH,HttpHeaderValues.ZERO); |
| 32 | +nettyHttpRequest.headers().add(HttpHeaderNames.CONNECTION,HttpHeaderValues.KEEP_ALIVE); |
| 33 | +try { |
| 34 | +longst =System.nanoTime(); |
| 35 | +encodeHeaders(nettyHttpRequest.headers(),buf); |
| 36 | +longet =System.nanoTime(); |
| 37 | +System.out.println("cost time :" + ((et -st) /1_000_000)); |
| 38 | + }catch (Exceptione) { |
| 39 | + |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | +protectedstaticvoidencodeHeaders(HttpHeadersheaders,ByteBufbuf)throwsException { |
| 44 | +Iterator<Map.Entry<CharSequence,CharSequence>>iter =headers.iteratorCharSequence(); |
| 45 | +while (iter.hasNext()) { |
| 46 | +Map.Entry<CharSequence,CharSequence>header =iter.next(); |
| 47 | +longst =System.nanoTime(); |
| 48 | +encoderHeader(header.getKey(),header.getValue(),buf); |
| 49 | +longet =System.nanoTime(); |
| 50 | +System.out.println("encode header " +header.getKey() +" value " +header.getValue() +" cost time :" + ((et -st) /1_000_000)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | +publicstaticvoidencoderHeader(CharSequencename,CharSequencevalue,ByteBufbuf)throwsException { |
| 55 | +finalintnameLen =name.length(); |
| 56 | +finalintvalueLen =value.length(); |
| 57 | +finalintentryLen =nameLen +valueLen +4; |
| 58 | +buf.ensureWritable(entryLen); |
| 59 | +intoffset =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 | +privatestaticvoidwriteAscii(ByteBufbuf,intoffset,CharSequencevalue,intvalueLen) { |
| 72 | +if (valueinstanceofAsciiString) { |
| 73 | +longst =System.nanoTime(); |
| 74 | +ByteBufUtil.copy((AsciiString)value,0,buf,offset,valueLen); |
| 75 | +longet =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 | +privatestaticvoidwriteCharSequence(ByteBufbuf,intoffset,CharSequencevalue,intvalueLen) { |
| 86 | +for (inti =0;i <valueLen; ++i) { |
| 87 | +buf.setByte(offset++,c2b(value.charAt(i))); |
| 88 | + } |
| 89 | + } |
| 90 | +} |