List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract ByteBuf readerIndex(int readerIndex);
From source file:divconq.web.http.ServerHandler.java
License:Open Source License
public void handleHttpRequest(ChannelHandlerContext ctx, HttpObject httpobj) throws Exception { if (httpobj instanceof HttpContent) { this.context.offerContent((HttpContent) httpobj); return;/*from w w w . ja v a2 s. co m*/ } if (!(httpobj instanceof HttpRequest)) { this.context.sendRequestBad(); return; } HttpRequest httpreq = (HttpRequest) httpobj; this.context.load(ctx, httpreq); // Handle a bad request. if (!httpreq.getDecoderResult().isSuccess()) { this.context.sendRequestBad(); return; } Request req = this.context.getRequest(); Response resp = this.context.getResponse(); // to avoid lots of unused sessions if (req.pathEquals("/favicon.ico") || req.pathEquals("/robots.txt")) { this.context.sendNotFound(); return; } // make sure we don't have a leftover task context OperationContext.clear(); String origin = "http:" + NetUtil.formatIpAddress((InetSocketAddress) ctx.channel().remoteAddress()); // TODO use X-Forwarded-For if available, maybe a plug in approach to getting client's IP? DomainInfo dinfo = this.context.getSiteman().resolveDomainInfo(req.getHeader("Host")); if (dinfo == null) { this.context.sendForbidden(); return; } WebDomain wdomain = this.context.getSiteman().getDomain(dinfo.getId()); // check into url re-routing String reroute = wdomain.route(req, (SslHandler) ctx.channel().pipeline().get("ssl")); if (StringUtil.isNotEmpty(reroute)) { this.context.getResponse().setStatus(HttpResponseStatus.FOUND); this.context.getResponse().setHeader("Location", reroute); this.context.send(); return; } Cookie sesscookie = req.getCookie("SessionId"); Session sess = null; if (sesscookie != null) { String v = sesscookie.getValue(); String sessionid = v.substring(0, v.lastIndexOf('_')); String accesscode = v.substring(v.lastIndexOf('_') + 1); sess = Hub.instance.getSessions().lookupAuth(sessionid, accesscode); } if (sess == null) { sess = Hub.instance.getSessions().create(origin, dinfo.getId()); Logger.info("Started new session: " + sess.getId() + " on " + req.getPath() + " for " + origin); // TODO if ssl set client key on user context //req.getSecuritySession().getPeerCertificates(); sess.setAdatper(new ISessionAdapter() { protected volatile ListStruct msgs = new ListStruct(); @Override public void stop() { ServerHandler.this.context.close(); } @Override public ListStruct popMessages() { ListStruct ret = this.msgs; this.msgs = new ListStruct(); return ret; } @Override public void deliver(Message msg) { // keep no more than 100 messages - this is not a "reliable" approach, just basic comm help while (this.msgs.getSize() > 99) this.msgs.removeItem(0); this.msgs.addItem(msg); } }); Cookie sk = new DefaultCookie("SessionId", sess.getId() + "_" + sess.getKey()); sk.setPath("/"); sk.setHttpOnly(true); resp.setCookie(sk); } this.context.setSession(sess); sess.touch(); OperationContext tc = sess.setContext(origin); tc.info("Web request for host: " + req.getHeader("Host") + " url: " + req.getPath() + " by: " + origin + " session: " + sess.getId()); /* System.out.println("sess proto: " + ((SslHandler)ctx.channel().pipeline().get("ssl")).engine().getSession().getProtocol()); System.out.println("sess suite: " + ((SslHandler)ctx.channel().pipeline().get("ssl")).engine().getSession().getCipherSuite()); */ try { if (req.pathEquals(ServerHandler.BUS_PATH)) { // Allow only GET methods. if (req.getMethod() != HttpMethod.GET) { this.context.sendForbidden(); return; } // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( ServerHandler.getWebSocketLocation( "True".equals(this.context.getConfig().getAttribute("Secure")), httpreq), null, false); this.handshaker = wsFactory.newHandshaker(httpreq); if (this.handshaker == null) WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); else { DefaultFullHttpRequest freq = new DefaultFullHttpRequest(httpreq.getProtocolVersion(), httpreq.getMethod(), httpreq.getUri()); freq.headers().add(httpreq.headers()); this.handshaker.handshake(ctx.channel(), freq); return; } this.context.sendForbidden(); return; } // "upload" is it's own built-in extension. if ((req.getPath().getNameCount() == 3) && req.getPath().getName(0).equals(ServerHandler.UPLOAD_PATH)) { if (!Hub.instance.isRunning()) { // only allow uploads when running this.context.sendRequestBad(); return; } // currently only supporting POST/PUT of pure binary - though support for form uploads can be restored, see below // we cannot rely on content type being meaningful //if (!"application/octet-stream".equals(req.getContentType().getPrimary())) { // this.context.sendRequestBad(); // return; //} // TODO add CORS support if needed if ((req.getMethod() != HttpMethod.PUT) && (req.getMethod() != HttpMethod.POST)) { this.context.sendRequestBad(); return; } final String cid = req.getPath().getName(1); final String op = req.getPath().getName(2); final DataStreamChannel dsc = sess.getChannel(cid); if (dsc == null) { this.context.sendRequestBad(); return; } dsc.setDriver(new IStreamDriver() { @Override public void cancel() { Logger.error("Transfer canceled on channel: " + cid); dsc.complete(); ServerHandler.this.context.sendRequestBad(); // TODO headers? } @Override public void nextChunk() { Logger.debug("Continue on channel: " + cid); ServerHandler.this.context.sendRequestOk(); } @Override public void message(StreamMessage msg) { if (msg.isFinal()) { Logger.debug("Final on channel: " + cid); dsc.complete(); ServerHandler.this.context.sendRequestOk(); } } }); //if (req.getMethod() == HttpMethod.PUT) { this.context.setDecoder(new IContentDecoder() { protected boolean completed = false; protected int seq = 0; @Override public void release() { // trust that http connection is closing or what ever needs to happen, we just need to deal with datastream Logger.debug("Releasing data stream"); // if not done with request then something went wrong, kill data channel if (!this.completed) dsc.abort(); } @Override public void offer(HttpContent chunk) { boolean finalchunk = (chunk instanceof LastHttpContent); //System.out.println("Chunk: " + finalchunk); ByteBuf buffer = chunk.content(); if (!dsc.isClosed()) { int size = buffer.readableBytes(); //System.out.println("Chunk size: " + size); dsc.touch(); // TODO try to set progress on dsc // TODO set hint in netty as to where this buffer was handled and sent if (size > 0) { buffer.retain(); // we will be using a reference up during send StreamMessage b = new StreamMessage("Block", buffer); b.setField("Sequence", this.seq); //System.out.println("Buffer ref cnt a: " + buffer.refCnt()); OperationResult or = dsc.send(b); //System.out.println("Buffer ref cnt b: " + buffer.refCnt()); // indicate we have read the buffer? buffer.readerIndex(buffer.writerIndex()); if (or.hasErrors()) { dsc.close(); return; } this.seq++; } // if last buffer of last block then mark the upload as completed if (finalchunk) { if ("Final".equals(op)) dsc.send(MessageUtil.streamFinal()); else dsc.getDriver().nextChunk(); } } // means this block is completed, not necessarily entire file uploaded if (finalchunk) this.completed = true; } }); //return; //} /* old approach that supported multipart posts TODO review/remove if (req.getMethod() == HttpMethod.POST) { StreamingDataFactory sdf = new StreamingDataFactory(dsc, op); // TODO consider supporting non-multipart? final HttpPostMultipartRequestDecoder prd = new HttpPostMultipartRequestDecoder(sdf, httpreq); this.context.setDecoder(new IContentDecoder() { @Override public void release() { // trust that http connection is closing or what ever needs to happen, we just need to deal with datastream // if not done with request then something went wrong, kill data channel if ((prd.getStatus() != MultiPartStatus.EPILOGUE) && (prd.getStatus() != MultiPartStatus.PREEPILOGUE)) dsc.kill(); } @Override public void offer(HttpContent chunk) { //the only thing we care about is the file, the file will stream to dsc - the rest can disappear prd.offer(chunk); } }); return; } */ //this.context.sendRequestBad(); return; } // "download" is it's own built-in extension. if ((req.getPath().getNameCount() == 2) && req.getPath().getName(0).equals(ServerHandler.DOWNLOAD_PATH)) { if (!Hub.instance.isRunning()) { // only allow downloads when running this.context.sendRequestBad(); return; } if (req.getMethod() != HttpMethod.GET) { this.context.sendRequestBad(); return; } String cid = req.getPath().getName(1); final DataStreamChannel dsc = sess.getChannel(cid); if (dsc == null) { this.context.sendRequestBad(); return; } dsc.setDriver(new IStreamDriver() { //protected long amt = 0; protected long seq = 0; @Override public void cancel() { dsc.complete(); ServerHandler.this.context.close(); } @Override public void nextChunk() { // meaningless in download } @Override public void message(StreamMessage msg) { int seqnum = (int) msg.getFieldAsInteger("Sequence", 0); if (seqnum != this.seq) { this.error(1, "Bad sequence number: " + seqnum); return; } if (msg.hasData()) { //this.amt += msg.getData().readableBytes(); HttpContent b = new DefaultHttpContent(Unpooled.copiedBuffer(msg.getData())); // TODO not copied ServerHandler.this.context.sendDownload(b); } this.seq++; // TODO update progress if (msg.isFinal()) { ServerHandler.this.context.sendDownload(new DefaultLastHttpContent()); ServerHandler.this.context.close(); dsc.complete(); } } public void error(int code, String msg) { dsc.send(MessageUtil.streamError(code, msg)); ServerHandler.this.context.close(); } }); // for some reason HyperSession is sending content. this.context.setDecoder(new IContentDecoder() { @Override public void release() { } @Override public void offer(HttpContent chunk) { if (!(chunk instanceof LastHttpContent)) Logger.error("Unexplained and unwanted content during download: " + chunk); } }); // tell the client that chunked content is coming this.context.sendDownloadHeaders(dsc.getPath() != null ? dsc.getPath().getFileName() : null, dsc.getMime()); // get the data flowing dsc.send(new StreamMessage("Start")); return; } if ((req.getPath().getNameCount() == 1) && req.getPath().getName(0).equals(ServerHandler.STATUS_PATH)) { if (Hub.instance.getState() == HubState.Running) this.context.sendRequestOk(); else this.context.sendRequestBad(); return; } // "rpc" is it's own built-in extension. all requests to rpc are routed through // DivConq bus, if the request is valid if (req.pathEquals(ServerHandler.RPC_PATH)) { if (req.getMethod() != HttpMethod.POST) { this.context.sendRequestBad(); return; } //System.out.println("looks like we have a rpc message"); // max 4MB of json? -- TODO is that max chunk size or max total? we don't need 4MB chunk... this.context.setDecoder(new HttpBodyRequestDecoder(4096 * 1024, new RpcHandler(this.context))); return; } // otherwise we need to figure out which extension is being called // "local" is also used to mean default extension String ext = req.pathEquals("/") ? "local" : req.getPath().getName(0); IWebExtension ex = "local".equals(ext) ? this.context.getSiteman().getDefaultExtension() : this.context.getSiteman().getExtension(ext); // still cannot figure it out, use default if (ex == null) ex = this.context.getSiteman().getDefaultExtension(); // then have extension handle it if (ex != null) { //OperationResult res = new OperationResult(); OperationResult res = ex.handle(sess, this.context); //resp.addBody("Hello"); //this.context.send(); // no errors starting page processing, return if (!res.hasErrors()) return; resp.setHeader("X-dcResultCode", res.getCode() + ""); resp.setHeader("X-dcResultMesage", res.getMessage()); this.context.sendNotFound(); return; } } catch (Exception x) { this.context.sendInternalError(); return; } this.context.sendNotFound(); }
From source file:dorkbox.network.pipeline.ByteBufInput.java
License:Apache License
private void readUtf8(int charCount) { ByteBuf buffer = byteBuf; char[] chars = this.inputChars; // will be the correct size. // Try to read 7 bit ASCII chars. int charIndex = 0; int count = charCount; int b;//from w ww . j a v a2 s.c om while (charIndex < count) { b = buffer.readByte(); if (b < 0) { buffer.readerIndex(buffer.readerIndex() - 1); break; } chars[charIndex++] = (char) b; } // If buffer didn't hold all chars or any were not ASCII, use slow path for remainder. if (charIndex < charCount) { readUtf8_slow(charCount, charIndex); } }
From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoder.java
License:Apache License
/** * Skips over min(in.readableBytes(), toSkip) bytes in the Netty ByteBuf and returns how many bytes remain to be * skipped.// w w w .ja v a 2 s . co m * * @return remaining bytes to be skipped */ private int skipBytes(ByteBuf in, int toSkip) { if (toSkip <= in.readableBytes()) { in.readBytes(toSkip); return 0; } int remainingToSkip = toSkip - in.readableBytes(); in.readerIndex(in.readerIndex() + in.readableBytes()); return remainingToSkip; }
From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java
License:Apache License
@Test public void testEncodeDecode() throws Exception { final EmbeddedChannel ch = new EmbeddedChannel(new OutboundEnvelopeEncoder(), new InboundEnvelopeDecoder(this.bufferProviderBroker)); when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId())) .thenReturn(this.bufferProvider); when(this.bufferProvider.requestBuffer(anyInt())).thenAnswer(new Answer<Object>() { @Override// w w w. ja va 2 s .co m public Object answer(InvocationOnMock invocation) throws Throwable { // fulfill the buffer request return allocBuffer((Integer) invocation.getArguments()[0]); } }); // -------------------------------------------------------------------- Envelope[] envelopes = new Envelope[] { nextEnvelope(0), nextEnvelope(2), nextEnvelope(32768), nextEnvelope(3782, new TestEvent1(34872527)), nextEnvelope(88, new TestEvent1(8749653), new TestEvent1(365345)), nextEnvelope(0, new TestEvent2(34563456), new TestEvent1(598432), new TestEvent2(976293845)), nextEnvelope(23) }; ByteBuf buf = encode(ch, envelopes); // 1. complete ByteBuf as input int refCount = buf.retain().refCnt(); decodeAndVerify(ch, buf, envelopes); Assert.assertEquals(refCount - 1, buf.refCnt()); // 2. random slices buf.readerIndex(0); ByteBuf[] slices = randomSlices(buf); ch.writeInbound(slices); for (ByteBuf slice : slices) { Assert.assertEquals(1, slice.refCnt()); } decodeAndVerify(ch, envelopes); buf.release(); }
From source file:io.advantageous.conekt.buffer.impl.BufferImpl.java
License:Open Source License
public Buffer appendBuffer(Buffer buff) { ByteBuf cb = buff.getByteBuf(); buffer.writeBytes(buff.getByteBuf()); cb.readerIndex(0); // Need to reset readerindex since Netty write modifies readerIndex of source! return this; }
From source file:io.codis.nedis.handler.RedisResponseDecoder.java
License:Apache License
private void setReaderIndex(ByteBuf in, int index) { in.readerIndex(index == -1 ? in.writerIndex() : index + 1); }
From source file:io.crate.blob.DigestBlob.java
License:Apache License
private void addContent(ByteBuf buffer, boolean last) throws IOException { if (buffer != null) { int readableBytes = buffer.readableBytes(); ByteBuffer byteBuffer = buffer.nioBuffer(); if (file == null) { file = createTmpFile();/*ww w . j a v a 2 s.c om*/ } if (fileChannel == null) { FileOutputStream outputStream = new FileOutputStream(file); fileChannel = outputStream.getChannel(); } int written = 0; do { if (headLength == 0) { updateDigest(byteBuffer); } written += fileChannel.write(byteBuffer); } while (written < readableBytes); size += readableBytes; buffer.readerIndex(buffer.readerIndex() + written); chunks++; } if (last) { if (file == null) { file = createTmpFile(); } if (fileChannel == null) { FileOutputStream outputStream = new FileOutputStream(file); fileChannel = outputStream.getChannel(); } fileChannel.force(false); fileChannel.close(); fileChannel = null; } else { if (buffer == null) { throw new NullPointerException("buffer"); } } }
From source file:io.datty.msgpack.core.reader.ByteReader.java
License:Apache License
@Override public Byte read(ByteBuf buffer, boolean copy) { if (!hasNext(buffer)) { return null; }/*from www. j av a 2 s. c o m*/ byte b = readByte(buffer); if (Code.isFixInt(b)) { return (byte) b; } if (Code.isFixedRaw(b)) { // FixRaw return parseByte(readString(b, buffer)); } switch (b) { case Code.NIL: return null; case Code.FALSE: return 0; case Code.TRUE: return 1; case Code.UINT8: short u8 = readUnsignedByte(buffer); if (u8 > (short) Byte.MAX_VALUE) { throw new MessageNumberOverflowException(u8); } return (byte) u8; case Code.UINT16: int u16 = readUnsignedShort(buffer); if (u16 > (int) Byte.MAX_VALUE) { throw new MessageNumberOverflowException(u16); } return (byte) u16; case Code.UINT32: long u32 = readUnsignedInt(buffer); if (u32 > (long) Byte.MAX_VALUE) { throw new MessageNumberOverflowException(u32); } return (byte) u32; case Code.UINT64: long u64 = readUnsignedLong(buffer); if (u64 > (long) Byte.MAX_VALUE) { throw new MessageNumberOverflowException(u64); } return (byte) u64; case Code.INT8: return readByte(buffer); case Code.INT16: short i16 = readShort(buffer); if (i16 < (short) Byte.MIN_VALUE || i16 > (short) Byte.MAX_VALUE) { throw new MessageNumberOverflowException(i16); } return (byte) i16; case Code.INT32: int i32 = readInt(buffer); if (i32 < (int) Byte.MIN_VALUE || i32 > (int) Byte.MAX_VALUE) { throw new MessageNumberOverflowException(i32); } return (byte) i32; case Code.INT64: long i64 = readLong(buffer); if (i64 < (long) Byte.MIN_VALUE || i64 > (long) Byte.MAX_VALUE) { throw new MessageNumberOverflowException(i64); } return (byte) i64; case Code.FLOAT32: return (byte) readFloat(buffer); case Code.FLOAT64: return (byte) readDouble(buffer); case Code.STR8: case Code.STR16: case Code.STR32: return parseByte(readString(b, buffer)); case Code.BIN8: case Code.BIN16: case Code.BIN32: return parseByte(readAsciiString(b, buffer)); default: buffer.readerIndex(buffer.readerIndex() - 1); skipValue(buffer); return null; } }
From source file:io.datty.msgpack.core.reader.DoubleReader.java
License:Apache License
@Override public Double read(ByteBuf buffer, boolean copy) { if (!hasNext(buffer)) { return null; }/*from w w w.jav a 2 s. co m*/ byte b = readByte(buffer); if (Code.isFixInt(b)) { return (double) b; } if (Code.isFixedRaw(b)) { // FixRaw return parseDouble(readString(b, buffer)); } switch (b) { case Code.NIL: return null; case Code.FALSE: return 0.0; case Code.TRUE: return 1.0; case Code.UINT8: return (double) readUnsignedByte(buffer); case Code.UINT16: return (double) readUnsignedShort(buffer); case Code.UINT32: return (double) readUnsignedInt(buffer); case Code.UINT64: return (double) readUnsignedLong(buffer); case Code.INT8: return (double) readByte(buffer); case Code.INT16: return (double) readShort(buffer); case Code.INT32: return (double) readInt(buffer); case Code.INT64: return (double) readLong(buffer); case Code.FLOAT32: return (double) readFloat(buffer); case Code.FLOAT64: return readDouble(buffer); case Code.STR8: case Code.STR16: case Code.STR32: return parseDouble(readString(b, buffer)); case Code.BIN8: case Code.BIN16: case Code.BIN32: return parseDouble(readAsciiString(b, buffer)); default: buffer.readerIndex(buffer.readerIndex() - 1); skipValue(buffer); return null; } }
From source file:io.datty.msgpack.core.reader.FloatReader.java
License:Apache License
@Override public Float read(ByteBuf buffer, boolean copy) { if (!hasNext(buffer)) { return null; }/*from w w w . j a v a 2 s . co m*/ byte b = readByte(buffer); if (Code.isFixInt(b)) { return (float) b; } if (Code.isFixedRaw(b)) { // FixRaw return parseFloat(readString(b, buffer)); } switch (b) { case Code.NIL: return null; case Code.FALSE: return 0.0f; case Code.TRUE: return 1.0f; case Code.UINT8: return (float) readUnsignedByte(buffer); case Code.UINT16: return (float) readUnsignedShort(buffer); case Code.UINT32: return (float) readUnsignedInt(buffer); case Code.UINT64: return (float) readUnsignedLong(buffer); case Code.INT8: return (float) readByte(buffer); case Code.INT16: return (float) readShort(buffer); case Code.INT32: return (float) readInt(buffer); case Code.INT64: return (float) readLong(buffer); case Code.FLOAT32: return readFloat(buffer); case Code.FLOAT64: return (float) readDouble(buffer); case Code.STR8: case Code.STR16: case Code.STR32: return parseFloat(readString(b, buffer)); case Code.BIN8: case Code.BIN16: case Code.BIN32: return parseFloat(readAsciiString(b, buffer)); default: buffer.readerIndex(buffer.readerIndex() - 1); skipValue(buffer); return null; } }