@Nullable private static String toHeaderValue(MessageBytes value) { switch (value.getType()) { case MessageBytes.T_BYTES: { final ByteChunk chunk = value.getByteChunk(); return new String(chunk.getBuffer(), chunk.getOffset(), chunk.getLength(), StandardCharsets.US_ASCII); } case MessageBytes.T_CHARS: { final CharChunk chunk = value.getCharChunk(); return new String(chunk.getBuffer(), chunk.getOffset(), chunk.getLength()); } case MessageBytes.T_STR: { return value.getString(); } } return null; } }
public int doWrite(ByteChunk chunk) { final int start = chunk.getStart(); final int end = chunk.getEnd(); final int length = end - start; if (length == 0) { return 0; } // NB: We make a copy because Tomcat reuses the underlying byte array of 'chunk'. final byte[] content = Arrays.copyOfRange(chunk.getBuffer(), start, end); data.add(HttpData.of(content)); bytesWritten += length; return length; }
public int doRead(ByteChunk chunk) { if (!isNeedToRead()) { // Read only once. return -1; } read = true; final int readableBytes = content.length(); chunk.setBytes(content.array(), content.offset(), readableBytes); return readableBytes; }
request.getCoyoteRequest().getMimeHeaders() .getValue("authorization"); authorization.toBytes(); ByteChunk authorizationBC = authorization.getByteChunk(); if (authorizationBC.startsWithIgnoreCase("basic ", 0)) { authorizationBC.setOffset(authorizationBC.getOffset() + 6); CharChunk authorizationCC = authorization.getCharChunk(); Base64.decode(authorizationBC, authorizationCC); int colon = authorizationCC.indexOf(':'); if (colon < 0) { username = authorizationCC.toString(); } else { char[] buf = authorizationCC.getBuffer(); username = new String(buf, 0, colon); password = new String(buf, colon + 1, authorizationBC.setOffset(authorizationBC.getOffset() - 6); try { MessageBytes authenticate = response.getCoyoteResponse().getMimeHeaders() .addValue(AUTHENTICATE_BYTES, 0, AUTHENTICATE_BYTES.length); CharChunk authenticateCC = authenticate.getCharChunk(); authenticateCC.append("Basic realm=\"");
@Override public void parseCookieHeader(MimeHeaders headers, ServerCookies serverCookies) { if (headers == null) { // nothing to process return; } // process each "cookie" header int pos = headers.findHeader("Cookie", 0); while (pos >= 0) { MessageBytes cookieValue = headers.getValue(pos); if (cookieValue != null && !cookieValue.isNull() ) { if (cookieValue.getType() != MessageBytes.T_BYTES ) { Exception e = new Exception(); // TODO: Review this in light of HTTP/2 log.debug("Cookies: Parsing cookie as String. Expected bytes.", e); cookieValue.toBytes(); } if (log.isDebugEnabled()) { log.debug("Cookies: Parsing b[]: " + cookieValue.toString()); } ByteChunk bc = cookieValue.getByteChunk(); processCookieHeader(bc.getBytes(), bc.getOffset(), bc.getLength(), serverCookies); } // search from the next position pos = headers.findHeader("Cookie", ++pos); } }
rframe.associated = 0; MimeHeaders headers = response.getMimeHeaders(); for (int i = 0; i < headers.size(); i++) { MessageBytes mb = headers.getName(i); mb.toBytes(); ByteChunk bc = mb.getByteChunk(); byte[] bb = bc.getBuffer(); for (int j = bc.getStart(); j < bc.getEnd(); j++) { bb[j] = (byte) Ascii.toLower(bb[j]); rframe.headerName(bc.getBuffer(), bc.getStart(), bc.getLength()); mb = headers.getValue(i); mb.toBytes(); bc = mb.getByteChunk(); rframe.headerValue(bc.getBuffer(), bc.getStart(), bc.getLength()); if (response.getStatus() == 0) { rframe.addHeader(SpdyFrame.STATUS, SpdyFrame.OK200); } else { && HttpMessages.isSafeInHttpHeader(response.getMessage())) { message = response.getMessage();
throws Exception { ByteChunk bc = uri.getByteChunk(); int length = bc.getLength(); CharChunk cc = uri.getCharChunk(); cc.allocate(length, -1); B2CConverter conv = request.getURIConverter(); try { if (conv == null) { conv = new B2CConverter(enc); request.setURIConverter(conv); log.error("Invalid URI encoding; using HTTP default"); connector.setURIEncoding(null); byte[] bbuf = bc.getBuffer(); char[] cbuf = cc.getBuffer(); int start = bc.getStart(); for (int i = 0; i < length; i++) { cbuf[i] = (char) (bbuf[i + start] & 0xff);
protected void parseHost(MessageBytes valueMB) { if (valueMB == null || valueMB.isNull()) { populateHost(); return; ByteChunk valueBC = valueMB.getByteChunk(); byte[] valueB = valueBC.getBytes(); int valueL = valueBC.getLength(); int valueS = valueBC.getStart(); if (hostNameC.length < valueL) { hostNameC = new char[valueL]; request.setServerPort(port); hostNameC[i] = (char) valueB[i + valueS]; request.serverName().setChars(hostNameC, 0, valueL); getLog().info(message, e); break; case DEBUG: getLog().debug(message, e); response.setStatus(400); setErrorState(ErrorState.CLOSE_CLEAN, e);
public int doRead(ByteChunk responseChunk, Request req) throws IOException { if( log.isDebugEnabled()) log.debug( "doRead " + end_of_stream+ " " + responseChunk.getOffset()+ " " + responseChunk.getLength()); if( end_of_stream ) { return -1; } if( isFirst && isReadRequired ) { // Handle special first-body-chunk, but only if httpd expects it. if( !receive() ) { return 0; } } else if(isEmpty) { if ( !refillReadBuffer() ){ return -1; } } ByteChunk bc = bodyBuff.getByteChunk(); responseChunk.setBytes( bc.getBuffer(), bc.getStart(), bc.getLength() ); isEmpty = true; return responseChunk.getLength(); }
public void processParameters( MessageBytes data, String encoding ) { if( data==null || data.isNull() || data.getLength() <= 0 ) { return; } if( data.getType() != MessageBytes.T_BYTES ) { data.toBytes(); } ByteChunk bc=data.getByteChunk(); processParameters( bc.getBytes(), bc.getOffset(), bc.getLength(), encoding); }
/** * Read bytes. */ public int doRead(ByteChunk chunk, org.apache.coyote.Request request) throws IOException { int writeLength = 0; if (chunk.getLimit() > 0 && chunk.getLimit() < input.getLength()) { writeLength = chunk.getLimit(); } else { writeLength = input.getLength(); } if(input.getOffset()>= input.getEnd()) return -1; input.substract(chunk.getBuffer(), 0, writeLength); chunk.setOffset(0); chunk.setEnd(writeLength); return writeLength; }
/** * Character conversion of the URI. * * @param uri MessageBytes object containing the URI * @param request The Servlet request object * @throws IOException if a IO exception occurs sending an error to the client */ protected void convertURI(MessageBytes uri, Request request) throws IOException { ByteChunk bc = uri.getByteChunk(); int length = bc.getLength(); CharChunk cc = uri.getCharChunk(); cc.allocate(length, -1); Charset charset = connector.getURICharset(); B2CConverter conv = request.getURIConverter(); if (conv == null) { conv = new B2CConverter(charset, true); request.setURIConverter(conv); } else { conv.recycle(); } try { conv.convert(bc, cc, true); uri.setChars(cc.getBuffer(), cc.getStart(), cc.getLength()); } catch (IOException ioe) { // Should never happen as B2CConverter should replace // problematic characters request.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST); } }
private void decodeHeaders( MsgContext ep, Msg msg, Request req, MessageBytes tmpMB ) { MimeHeaders headers = req.getMimeHeaders(); vMB=headers.addValue( hName ); } else { ByteChunk bc=tmpMB.getByteChunk(); vMB=headers.addValue( bc.getBuffer(), bc.getStart(), bc.getLength() ); (hId == -1 && tmpMB.equalsIgnoreCase("Content-Length"))) { long cl = vMB.getLong(); if(cl < Integer.MAX_VALUE) req.setContentLength( (int)cl ); } else if (hId == AjpConstants.SC_REQ_CONTENT_TYPE || (hId == -1 && tmpMB.equalsIgnoreCase("Content-Type"))) { ByteChunk bchunk = vMB.getByteChunk(); req.contentType().setBytes(bchunk.getBytes(), bchunk.getOffset(), bchunk.getLength());
String password = null; final MessageBytes authorization = request.getCoyoteRequest().getMimeHeaders().getValue("authorization"); if (authorization != null) { authorization.toBytes(); final ByteChunk authorizationBC = authorization.getByteChunk(); if (authorizationBC.startsWithIgnoreCase("basic ", 0)) { authorizationBC.setOffset(authorizationBC.getOffset() + 6); final byte[] decoded = Base64.decodeBase64(authorizationBC.getBuffer(), authorizationBC.getOffset(), authorizationBC.getLength()); int colon = -1; for (int i = 0; i < decoded.length; i++) { authorizationBC.setOffset(authorizationBC.getOffset() - 6);
public void processParameters( MessageBytes data, String encoding ) { if( data==null || data.isNull() || data.getLength() <= 0 ) return; if( data.getType() == MessageBytes.T_BYTES ) { ByteChunk bc=data.getByteChunk(); processParameters( bc.getBytes(), bc.getOffset(), bc.getLength(), encoding); } else { if (data.getType()!= MessageBytes.T_CHARS ) data.toChars(); CharChunk cc=data.getCharChunk(); processParameters( cc.getChars(), cc.getOffset(), cc.getLength()); } }
/** * Read bytes into the specified chunk. */ public int doRead(ByteChunk chunk, Request req ) throws IOException { if (endOfStream) { return -1; } if (first && req.getContentLength() > 0) { // Handle special first-body-chunk if (!receive()) { return 0; } } else if (empty) { if (!refillReadBuffer()) { return -1; } } ByteChunk bc = bodyBytes.getByteChunk(); chunk.setBytes(bc.getBuffer(), bc.getStart(), bc.getLength()); empty = true; return chunk.getLength(); }
public void service(Request req, final Response res) throws Exception { MessageBytes mb = req.requestURI(); String uri = req.requestURI().toString(); if (uri.indexOf("..") >= 0) { res.setStatus(404); return; res.setStatus(404); return; res.setStatus(404); return; ByteChunk chunk = new ByteChunk(); int rd = 0; while ((rd = fis.read(b)) > 0) { chunk.setBytes(b, 0, rd); res.doWrite(chunk); req.action( ActionCode.ACTION_POST_REQUEST , null); }catch (Throwable t) { t.printStackTrace();
if (cb.getLength() > 0) { cb.flushBuffer(); if ((!coyoteResponse.isCommitted()) && (coyoteResponse.getContentLengthLong() == -1)) { if (!coyoteResponse.isCommitted()) { coyoteResponse.setContentLength(bb.getLength()); Request req = (Request) coyoteResponse.getRequest().getNote( CoyoteAdapter.ADAPTER_NOTES); req.inputBuffer.close();
protected void urlEncode(CharChunk out, ByteChunk bb) throws IOException { byte[] bytes = bb.getBuffer(); for (int j = bb.getStart(); j < bb.getEnd(); j++) { out.append('%'); char ch = Character.forDigit((bytes[j] >> 4) & 0xF, 16); out.append(ch); ch = Character.forDigit(bytes[j] & 0xF, 16); out.append(ch); } }
/** * Intercept the request and decide if we cache the static resource. If the * static resource is already cached, return it. */ public int handle(Request req, int handlerCode) throws IOException{ if (fileCache == null) return Handler.CONTINUE; if (handlerCode == Handler.RESPONSE_PROCEEDED && fileCache.isEnabled()){ String docroot = SelectorThread.getWebAppRootPath(); MessageBytes mb = req.requestURI(); ByteChunk requestURI = mb.getByteChunk(); String uri = req.requestURI().toString(); fileCache.add(FileCache.DEFAULT_SERVLET_NAME,docroot,uri, req.getResponse().getMimeHeaders(),false); } else if (handlerCode == Handler.HEADERS_PARSED) { ByteChunk requestURI = req.requestURI().getByteChunk(); if (fileCache.sendCache(requestURI.getBytes(), requestURI.getStart(), requestURI.getLength(), socketChannel, keepAlive(req))){ return Handler.BREAK; } } return Handler.CONTINUE; }