/** * Set the body of the request by providing an InputStream * * @param body the bytes of the body * @param contentLength how long the body is supposed to be */ public EventBuilder withBody(InputStream body, int contentLength) { Objects.requireNonNull(body, "body"); if (contentLength < 0) { throw new IllegalArgumentException("Invalid contentLength"); } // This is for safety. Because we concatenate events, an input stream shorter than content length will cause // the implementation to continue reading through to the next http request. We need to avoid a sort of // buffer overrun. // FIXME: Make InputStream handling simpler. SessionInputBufferImpl sib = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 65535); sib.bind(body); this.body = new ContentLengthInputStream(sib, contentLength); this.contentLength = contentLength; return this; }
public HttpRequest receiveRequest() throws HttpException, IOException { HttpRequest request = (HttpRequest) this.requestParser.parse(); if (HEADERLOG.isDebugEnabled()) { HEADERLOG.debug(">> " + request.getRequestLine().toString()); for (HeaderIterator it = request.headerIterator(); it.hasNext(); ) { HEADERLOG.debug(">> " + it.nextHeader().toString()); } } // Prepare input stream this.in = null; if (request instanceof HttpEntityEnclosingRequest) { long len = this.contentLenStrategy.determineLength(request); if (len == ContentLengthStrategy.CHUNKED) { this.in = new ChunkedInputStream(this.inbuffer); } else if (len == ContentLengthStrategy.IDENTITY) { this.in = new IdentityInputStream(this.inbuffer); } else { this.in = new ContentLengthInputStream(inbuffer, len); } } return request; }
/** * Creates a {@link BasicHttpEntity} based on properties of the given * message. The content of the entity is created by wrapping * {@link SessionInputBuffer} with a content decoder depending on the * transfer mechanism used by the message. * <p> * This method is called by the public * {@link #deserialize(SessionInputBuffer, HttpMessage)}. * * @param inbuffer the session input buffer. * @param message the message. * @return HTTP entity. * @throws HttpException in case of HTTP protocol violation. * @throws IOException in case of an I/O error. */ protected BasicHttpEntity doDeserialize( final SessionInputBuffer inbuffer, final HttpMessage message) throws HttpException, IOException { BasicHttpEntity entity = new BasicHttpEntity(); long len = this.lenStrategy.determineLength(message); if (len == ContentLengthStrategy.CHUNKED) { entity.setChunked(true); entity.setContentLength(-1); entity.setContent(new ChunkedInputStream(inbuffer)); } else if (len == ContentLengthStrategy.IDENTITY) { entity.setChunked(false); entity.setContentLength(-1); entity.setContent(new IdentityInputStream(inbuffer)); } else { entity.setChunked(false); entity.setContentLength(len); entity.setContent(new ContentLengthInputStream(inbuffer, len)); } Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE); if (contentTypeHeader != null) { entity.setContentType(contentTypeHeader); } Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING); if (contentEncodingHeader != null) { entity.setContentEncoding(contentEncodingHeader); } return entity; }
public FnHttpEventBuilder withBody(InputStream body, int contentLength) { Objects.requireNonNull(body, "body"); if (contentLength < 0) { throw new IllegalArgumentException("Invalid contentLength"); } // This is for safety. Because we concatenate events, an input stream shorter than content length will cause // the implementation to continue reading through to the next http request. We need to avoid a sort of // buffer overrun. // FIXME: Make InputStream handling simpler. SessionInputBufferImpl sib = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 65535); sib.bind(body); this.bodyStream = new ContentLengthInputStream(sib, contentLength); this.contentLength = contentLength; return this; }
/** * Creates a new {@link SessionInputBuffer} bounded to a given maximum length. * * @param buffer the buffer to wrap * @param length the maximum number of bytes to read (from the buffered stream). */ public BoundSessionInputBuffer(final SessionInputBuffer buffer, final long length) { super(new HttpTransportMetricsImpl(), BUFFER_SIZE, 0, null, null); this.bounded = new ContentLengthInputStream(buffer, length); this.input = new CountingInputStream(this.bounded); super.bind(this.input); this.length = length; }
public HttpResponseWarcRecord(SessionInputBuffer buffer) throws IOException { super(buffer); buffer = warpSessionInputBuffer(new ByteArrayInputStream(this.payload)); this.headers = readCRLFSeparatedBlock(buffer); long contentLength = contentLength(this.headers); InputStream blockStream = new ContentLengthInputStream(buffer, contentLength); this.entity = new byte[(int)contentLength]; blockStream.read(this.entity); blockStream.close(); }