类 io.netty.handler.codec.http.HttpConstants 源码实例Demo

下面列出了怎么用 io.netty.handler.codec.http.HttpConstants 的API类实例代码及写法,或者点击链接到github查看源代码。


/**
 * Clean the String from any unallowed character 清除字符串中任何不允许的字符
 *
 * @return the cleaned String
 */
private static String cleanString(String field) {
    int size = field.length();
    StringBuilder sb = new StringBuilder(size);
    for (int i = 0; i < size; i++) {
        char nextChar = field.charAt(i);
        switch (nextChar) {
        case HttpConstants.COLON:
        case HttpConstants.COMMA:
        case HttpConstants.EQUALS:
        case HttpConstants.SEMICOLON:
        case HttpConstants.HT:
            sb.append(HttpConstants.SP_CHAR);
            break;
        case HttpConstants.DOUBLE_QUOTE:
            // nothing added, just removes it
            break;
        default:
            sb.append(nextChar);
            break;
        }
    }
    return sb.toString().trim();
}
 

/**
 * Skip one empty line
 *
 * @return True if one empty line was skipped
 */
private boolean skipOneLine() {
    if (!undecodedChunk.isReadable()) {
        return false;
    }
    byte nextByte = undecodedChunk.readByte();
    if (nextByte == HttpConstants.CR) {
        if (!undecodedChunk.isReadable()) {
            undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1);
            return false;
        }
        nextByte = undecodedChunk.readByte();
        if (nextByte == HttpConstants.LF) {
            return true;
        }
        undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 2);
        return false;
    }
    if (nextByte == HttpConstants.LF) {
        return true;
    }
    undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1);
    return false;
}
 

@Override
public boolean process(byte value) throws Exception {
    char nextByte = (char) (value & 0xFF);
    if (nextByte == HttpConstants.CR) {
        return true;
    }
    if (nextByte == HttpConstants.LF) {
        return false;
    }

    if (++ size > maxLength) {
        // TODO: Respond with Bad Request and discard the traffic
        //    or close the connection.
        //       No need to notify the upstream handlers - just log.
        //       If decoding a response, just throw an exception.
        throw newException(maxLength);
    }

    seq.append(nextByte);
    return true;
}
 

/**
 * Clean the String from any unallowed character
 *
 * @return the cleaned String
 */
private static String cleanString(String field) {
	int size = field.length();
	StringBuilder sb = new StringBuilder(size);
	for (int i = 0; i < size; i++) {
		char nextChar = field.charAt(i);
		switch (nextChar) {
		case HttpConstants.COLON:
		case HttpConstants.COMMA:
		case HttpConstants.EQUALS:
		case HttpConstants.SEMICOLON:
		case HttpConstants.HT:
			sb.append(HttpConstants.SP_CHAR);
			break;
		case HttpConstants.DOUBLE_QUOTE:
			// nothing added, just removes it
			break;
		default:
			sb.append(nextChar);
			break;
		}
	}
	return sb.toString().trim();
}
 

/**
 * Skip one empty line
 *
 * @return True if one empty line was skipped
 */
private boolean skipOneLine() {
	if (!undecodedChunk.isReadable()) {
		return false;
	}
	byte nextByte = undecodedChunk.readByte();
	if (nextByte == HttpConstants.CR) {
		if (!undecodedChunk.isReadable()) {
			undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1);
			return false;
		}
		nextByte = undecodedChunk.readByte();
		if (nextByte == HttpConstants.LF) {
			return true;
		}
		undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 2);
		return false;
	}
	if (nextByte == HttpConstants.LF) {
		return true;
	}
	undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1);
	return false;
}
 
源代码6 项目: NioImapClient   文件: LineParser.java

@Override
public boolean process(byte value) throws Exception {
  char nextByte = (char) value;
  if (nextByte == HttpConstants.CR) {
    return true;
  } else if (nextByte == HttpConstants.LF) {
    return false;
  } else {
    if (size >= maxLineLength) {
      throw new TooLongFrameException(
          "Line is larger than " + maxLineLength +
              " bytes.");
    }
    size++;
    seq.append(nextByte);
    return true;
  }
}
 

/**
 * Clean the String from any unallowed character
 *
 * @return the cleaned String
 */
@SuppressWarnings("IfStatementWithIdenticalBranches")
private static String cleanString(String field) {
    StringBuilder sb = new StringBuilder(field.length());
    for (int i = 0; i < field.length(); i++) {
        char nextChar = field.charAt(i);
        if (nextChar == HttpConstants.COLON) {
            sb.append(HttpConstants.SP);
        } else if (nextChar == HttpConstants.COMMA) {
            sb.append(HttpConstants.SP);
        } else if (nextChar == HttpConstants.EQUALS) {
            sb.append(HttpConstants.SP);
        } else if (nextChar == HttpConstants.SEMICOLON) {
            sb.append(HttpConstants.SP);
        } else if (nextChar == HttpConstants.HT) {
            sb.append(HttpConstants.SP);
        } else if (nextChar == HttpConstants.DOUBLE_QUOTE) {
            // nothing added, just removes it
        } else {
            sb.append(nextChar);
        }
    }
    return sb.toString().trim();
}
 

/**
 * Skip one empty line
 *
 * @return True if one empty line was skipped
 */
private boolean skipOneLine() {
    if (!undecodedChunk.isReadable()) {
        return false;
    }
    byte nextByte = undecodedChunk.readByte();
    if (nextByte == HttpConstants.CR) {
        if (!undecodedChunk.isReadable()) {
            undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1);
            return false;
        }
        nextByte = undecodedChunk.readByte();
        if (nextByte == HttpConstants.LF) {
            return true;
        }
        undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 2);
        return false;
    }
    if (nextByte == HttpConstants.LF) {
        return true;
    }
    undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1);
    return false;
}
 
源代码9 项目: netty-4.1.22   文件: CookieUtil.java

static void add(StringBuilder sb, String name, long val) {
    sb.append(name);
    sb.append((char) HttpConstants.EQUALS);
    sb.append(val);
    sb.append((char) HttpConstants.SEMICOLON);
    sb.append((char) HttpConstants.SP);
}
 
源代码10 项目: netty-4.1.22   文件: CookieUtil.java

static void add(StringBuilder sb, String name, String val) {
    sb.append(name);
    sb.append((char) HttpConstants.EQUALS);
    sb.append(val);
    sb.append((char) HttpConstants.SEMICOLON);
    sb.append((char) HttpConstants.SP);
}
 
源代码11 项目: netty-4.1.22   文件: CookieUtil.java

static void addQuoted(StringBuilder sb, String name, String val) {
    if (val == null) {
        val = "";
    }

    sb.append(name);
    sb.append((char) HttpConstants.EQUALS);
    sb.append((char) HttpConstants.DOUBLE_QUOTE);
    sb.append(val);
    sb.append((char) HttpConstants.DOUBLE_QUOTE);
    sb.append((char) HttpConstants.SEMICOLON);
    sb.append((char) HttpConstants.SP);
}
 

/**
 * Read one line up to the CRLF or LF
 *
 * @return the String from one line
 * @throws NotEnoughDataDecoderException
 *             Need more chunks and reset the {@code readerIndex} to the previous
 *             value
 */
private static String readLineStandard(ByteBuf undecodedChunk, Charset charset) {
    int readerIndex = undecodedChunk.readerIndex();
    try {
        ByteBuf line = buffer(64);

        while (undecodedChunk.isReadable()) {
            byte nextByte = undecodedChunk.readByte();
            if (nextByte == HttpConstants.CR) {
                // check but do not changed readerIndex
                nextByte = undecodedChunk.getByte(undecodedChunk.readerIndex());
                if (nextByte == HttpConstants.LF) {
                    // force read
                    undecodedChunk.readByte();
                    return line.toString(charset);
                } else {
                    // Write CR (not followed by LF)
                    line.writeByte(HttpConstants.CR);
                }
            } else if (nextByte == HttpConstants.LF) {
                return line.toString(charset);
            } else {
                line.writeByte(nextByte);
            }
        }
    } catch (IndexOutOfBoundsException e) {
        undecodedChunk.readerIndex(readerIndex);
        throw new NotEnoughDataDecoderException(e);
    }
    undecodedChunk.readerIndex(readerIndex);
    throw new NotEnoughDataDecoderException();
}
 

/**
 * Read one line up to the CRLF or LF
 *
 * @return the String from one line
 * @throws NotEnoughDataDecoderException
 *             Need more chunks and reset the {@code readerIndex} to the previous
 *             value
 */
private static String readLine(ByteBuf undecodedChunk, Charset charset) {
    if (!undecodedChunk.hasArray()) {
        return readLineStandard(undecodedChunk, charset);
    }
    SeekAheadOptimize sao = new SeekAheadOptimize(undecodedChunk);
    int readerIndex = undecodedChunk.readerIndex();
    try {
        ByteBuf line = buffer(64);

        while (sao.pos < sao.limit) {
            byte nextByte = sao.bytes[sao.pos++];
            if (nextByte == HttpConstants.CR) {
                if (sao.pos < sao.limit) {
                    nextByte = sao.bytes[sao.pos++];
                    if (nextByte == HttpConstants.LF) {
                        sao.setReadPosition(0);
                        return line.toString(charset);
                    } else {
                        // Write CR (not followed by LF)
                        sao.pos--;
                        line.writeByte(HttpConstants.CR);
                    }
                } else {
                    line.writeByte(nextByte);
                }
            } else if (nextByte == HttpConstants.LF) {
                sao.setReadPosition(0);
                return line.toString(charset);
            } else {
                line.writeByte(nextByte);
            }
        }
    } catch (IndexOutOfBoundsException e) {
        undecodedChunk.readerIndex(readerIndex);
        throw new NotEnoughDataDecoderException(e);
    }
    undecodedChunk.readerIndex(readerIndex);
    throw new NotEnoughDataDecoderException();
}
 

/**
 * Load the field value or file data from a Multipart request 从多部分请求加载字段值或文件数据
 *
 * @return {@code true} if the last chunk is loaded (boundary delimiter found), {@code false} if need more chunks
 * @throws ErrorDataDecoderException
 */
private static boolean loadDataMultipartStandard(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
    final int startReaderIndex = undecodedChunk.readerIndex();
    final int delimeterLength = delimiter.length();
    int index = 0;
    int lastPosition = startReaderIndex;
    byte prevByte = HttpConstants.LF;
    boolean delimiterFound = false;
    while (undecodedChunk.isReadable()) {
        final byte nextByte = undecodedChunk.readByte();
        // Check the delimiter
        if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
            index++;
            if (delimeterLength == index) {
                delimiterFound = true;
                break;
            }
            continue;
        }
        lastPosition = undecodedChunk.readerIndex();
        if (nextByte == HttpConstants.LF) {
            index = 0;
            lastPosition -= (prevByte == HttpConstants.CR)? 2 : 1;
        }
        prevByte = nextByte;
    }
    if (prevByte == HttpConstants.CR) {
        lastPosition--;
    }
    ByteBuf content = undecodedChunk.copy(startReaderIndex, lastPosition - startReaderIndex);
    try {
        httpData.addContent(content, delimiterFound);
    } catch (IOException e) {
        throw new ErrorDataDecoderException(e);
    }
    undecodedChunk.readerIndex(lastPosition);
    return delimiterFound;
}
 

@Override
public String getString(Charset encoding) {
    if (byteBuf == null) {
        return "";
    }
    if (encoding == null) {
        encoding = HttpConstants.DEFAULT_CHARSET;
    }
    return byteBuf.toString(encoding);
}
 

@Test
public void testDataIsMultipleOfChunkSize1() throws Exception {
    DefaultHttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
            HttpMethod.POST, "http://localhost");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(factory, request, true,
            HttpConstants.DEFAULT_CHARSET, HttpPostRequestEncoder.EncoderMode.RFC1738);

    MemoryFileUpload first = new MemoryFileUpload("resources", "", "application/json", null,
            CharsetUtil.UTF_8, -1);
    first.setMaxSize(-1);
    first.setContent(new ByteArrayInputStream(new byte[7955]));
    encoder.addBodyHttpData(first);

    MemoryFileUpload second = new MemoryFileUpload("resources2", "", "application/json", null,
            CharsetUtil.UTF_8, -1);
    second.setMaxSize(-1);
    second.setContent(new ByteArrayInputStream(new byte[7928]));
    encoder.addBodyHttpData(second);

    assertNotNull(encoder.finalizeRequest());

    checkNextChunkSize(encoder, 8080);
    checkNextChunkSize(encoder, 8080);

    HttpContent httpContent = encoder.readChunk((ByteBufAllocator) null);
    assertTrue("Expected LastHttpContent is not received", httpContent instanceof LastHttpContent);
    httpContent.release();

       assertTrue("Expected end of input is not receive", encoder.isEndOfInput());
}
 
源代码17 项目: spring-boot-protocol   文件: RtspProtocol.java

@Override
public boolean canSupport(ByteBuf msg) {
    int protocolEndIndex = IOUtil.indexOf(msg, HttpConstants.LF);
    if(protocolEndIndex < 9){
        return false;
    }

    if(msg.getByte(protocolEndIndex - 9) == 'R'
            && msg.getByte(protocolEndIndex - 8) == 'T'
            && msg.getByte(protocolEndIndex - 7) == 'S'
            &&  msg.getByte(protocolEndIndex - 6) == 'P'){
        return true;
    }
    return false;
}
 

/**
 * Read one line up to the CRLF or LF
 *
 * @return the String from one line
 * @throws NotEnoughDataDecoderException Need more chunks and reset the
 *                                       {@code readerIndex} to the previous
 *                                       value
 */
private static String readLineStandard(ByteBuf undecodedChunk, Charset charset) {
	int readerIndex = undecodedChunk.readerIndex();
	try {
		ByteBuf line = buffer(64);

		while (undecodedChunk.isReadable()) {
			byte nextByte = undecodedChunk.readByte();
			if (nextByte == HttpConstants.CR) {
				// check but do not changed readerIndex
				nextByte = undecodedChunk.getByte(undecodedChunk.readerIndex());
				if (nextByte == HttpConstants.LF) {
					// force read
					undecodedChunk.readByte();
					return line.toString(charset);
				} else {
					// Write CR (not followed by LF)
					line.writeByte(HttpConstants.CR);
				}
			} else if (nextByte == HttpConstants.LF) {
				return line.toString(charset);
			} else {
				line.writeByte(nextByte);
			}
		}
	} catch (IndexOutOfBoundsException e) {
		undecodedChunk.readerIndex(readerIndex);
		throw new NotEnoughDataDecoderException(e);
	}
	undecodedChunk.readerIndex(readerIndex);
	throw new NotEnoughDataDecoderException();
}
 

/**
 * Read one line up to the CRLF or LF
 *
 * @return the String from one line
 * @throws NotEnoughDataDecoderException Need more chunks and reset the
 *                                       {@code readerIndex} to the previous
 *                                       value
 */
private static String readLine(ByteBuf undecodedChunk, Charset charset) {
	if (!undecodedChunk.hasArray()) {
		return readLineStandard(undecodedChunk, charset);
	}
	SeekAheadOptimize sao = new SeekAheadOptimize(undecodedChunk);
	int readerIndex = undecodedChunk.readerIndex();
	try {
		ByteBuf line = buffer(64);

		while (sao.pos < sao.limit) {
			byte nextByte = sao.bytes[sao.pos++];
			if (nextByte == HttpConstants.CR) {
				if (sao.pos < sao.limit) {
					nextByte = sao.bytes[sao.pos++];
					if (nextByte == HttpConstants.LF) {
						sao.setReadPosition(0);
						return line.toString(charset);
					} else {
						// Write CR (not followed by LF)
						sao.pos--;
						line.writeByte(HttpConstants.CR);
					}
				} else {
					line.writeByte(nextByte);
				}
			} else if (nextByte == HttpConstants.LF) {
				sao.setReadPosition(0);
				return line.toString(charset);
			} else {
				line.writeByte(nextByte);
			}
		}
	} catch (IndexOutOfBoundsException e) {
		undecodedChunk.readerIndex(readerIndex);
		throw new NotEnoughDataDecoderException(e);
	}
	undecodedChunk.readerIndex(readerIndex);
	throw new NotEnoughDataDecoderException();
}
 

/**
 * Load the field value or file data from a Multipart request
 *
 * @return {@code true} if the last chunk is loaded (boundary delimiter found),
 *         {@code false} if need more chunks
 * @throws ErrorDataDecoderException
 */
private static boolean loadDataMultipartStandard(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
	final int startReaderIndex = undecodedChunk.readerIndex();
	final int delimeterLength = delimiter.length();
	int index = 0;
	int lastPosition = startReaderIndex;
	byte prevByte = HttpConstants.LF;
	boolean delimiterFound = false;
	while (undecodedChunk.isReadable()) {
		final byte nextByte = undecodedChunk.readByte();
		// Check the delimiter
		if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
			index++;
			if (delimeterLength == index) {
				delimiterFound = true;
				break;
			}
			continue;
		}
		lastPosition = undecodedChunk.readerIndex();
		if (nextByte == HttpConstants.LF) {
			index = 0;
			lastPosition -= (prevByte == HttpConstants.CR) ? 2 : 1;
		}
		prevByte = nextByte;
	}
	if (prevByte == HttpConstants.CR) {
		lastPosition--;
	}
	ByteBuf content = undecodedChunk.copy(startReaderIndex, lastPosition - startReaderIndex);
	try {
		httpData.addContent(content, delimiterFound);
	} catch (IOException e) {
		throw new ErrorDataDecoderException(e);
	}
	undecodedChunk.readerIndex(lastPosition);
	return delimiterFound;
}
 
源代码21 项目: styx   文件: CookieUtil.java

static void add(StringBuilder sb, String name, long val) {
    sb.append(name);
    sb.append((char) HttpConstants.EQUALS);
    sb.append(val);
    sb.append((char) HttpConstants.SEMICOLON);
    sb.append((char) HttpConstants.SP);
}
 
源代码22 项目: styx   文件: CookieUtil.java

static void add(StringBuilder sb, String name, String val) {
    sb.append(name);
    sb.append((char) HttpConstants.EQUALS);
    sb.append(val);
    sb.append((char) HttpConstants.SEMICOLON);
    sb.append((char) HttpConstants.SP);
}
 
源代码23 项目: styx   文件: CookieUtil.java

static void addQuoted(StringBuilder sb, String name, String val) {
    if (val == null) {
        val = "";
    }

    sb.append(name);
    sb.append((char) HttpConstants.EQUALS);
    sb.append((char) HttpConstants.DOUBLE_QUOTE);
    sb.append(val);
    sb.append((char) HttpConstants.DOUBLE_QUOTE);
    sb.append((char) HttpConstants.SEMICOLON);
    sb.append((char) HttpConstants.SP);
}
 
源代码24 项目: NioImapClient   文件: ResponseDecoder.java

private UntaggedSearchResponse parseSearch(ByteBuf in) {
  List<Long> ids = new ArrayList<>();
  for (; ; ) {
    char c = ((char) in.readUnsignedByte());
    in.readerIndex(in.readerIndex() - 1);
    if (c == HttpConstants.CR || c == HttpConstants.LF) {
      lineParser.parse(in);
      break;
    }

    ids.add(Long.parseLong(atomOrStringParser.parse(in)));
  }

  return new UntaggedSearchResponse(ids);
}
 
源代码25 项目: NioImapClient   文件: ResponseDecoder.java

/**
 * Reset checks to see if we are at the end of this response line. If not it fast forwards the buffer to the end of this line to prepare for the next response.
 *
 * @param in
 */
private void reset(ByteBuf in) {
  char c = (char) in.readUnsignedByte();
  if (c == UNTAGGED_PREFIX || c == CONTINUATION_PREFIX || c == TAGGED_PREFIX) { // We are already at the end of the line
    in.readerIndex(in.readerIndex() - 1);
  } else if (!(c == HttpConstants.CR || c == HttpConstants.LF)) {
    lineParser.parse(in);
  }

  discardSomeReadBytes();
  responseBuilder = new TaggedResponse.Builder();
  checkpoint(State.START_RESPONSE);
}
 

/**
 * Read one line up to the CRLF or LF
 *
 * @return the String from one line
 * @throws NotEnoughDataDecoderException
 *             Need more chunks and reset the readerInder to the previous
 *             value
 */
private String readLineStandard() {
    int readerIndex = undecodedChunk.readerIndex();
    try {
        ByteBuf line = buffer(64);

        while (undecodedChunk.isReadable()) {
            byte nextByte = undecodedChunk.readByte();
            if (nextByte == HttpConstants.CR) {
                // check but do not changed readerIndex
                nextByte = undecodedChunk.getByte(undecodedChunk.readerIndex());
                if (nextByte == HttpConstants.LF) {
                    // force read
                    undecodedChunk.readByte();
                    return line.toString(charset);
                } else {
                    // Write CR (not followed by LF)
                    line.writeByte(HttpConstants.CR);
                }
            } else if (nextByte == HttpConstants.LF) {
                return line.toString(charset);
            } else {
                line.writeByte(nextByte);
            }
        }
    } catch (IndexOutOfBoundsException e) {
        undecodedChunk.readerIndex(readerIndex);
        throw new NotEnoughDataDecoderException(e);
    }
    undecodedChunk.readerIndex(readerIndex);
    throw new NotEnoughDataDecoderException();
}
 

@Override
public String getString(Charset encoding) {
    if (byteBuf == null) {
        return "";
    }
    if (encoding == null) {
        encoding = HttpConstants.DEFAULT_CHARSET;
    }
    return byteBuf.toString(encoding);
}
 
源代码28 项目: armeria   文件: CookieUtil.java

static void add(StringBuilder sb, String name, long val) {
    sb.append(name);
    sb.append('=');
    sb.append(val);
    sb.append(';');
    sb.append(HttpConstants.SP_CHAR);
}
 
源代码29 项目: armeria   文件: CookieUtil.java

static void add(StringBuilder sb, String name, String val) {
    sb.append(name);
    sb.append('=');
    sb.append(val);
    sb.append(';');
    sb.append(HttpConstants.SP_CHAR);
}
 
源代码30 项目: armeria   文件: CookieUtil.java

static void addQuoted(StringBuilder sb, String name, @Nullable String val) {
    if (val == null) {
        val = "";
    }

    sb.append(name);
    sb.append('=');
    sb.append('"');
    sb.append(val);
    sb.append('"');
    sb.append(';');
    sb.append(HttpConstants.SP_CHAR);
}
 
 同包方法