Пример #1
0
 /**
  * Creates an instance of AbstractMessageParser.
  *
  * @param buffer the session input buffer.
  * @param parser the line parser.
  * @param params HTTP parameters.
  * @deprecated (4.3) use {@link AbstractMessageParser#AbstractMessageParser(SessionInputBuffer,
  *     LineParser, MessageConstraints)}
  */
 @Deprecated
 public AbstractMessageParser(
     final SessionInputBuffer buffer, final LineParser parser, final HttpParams params) {
   super();
   Args.notNull(buffer, "Session input buffer");
   Args.notNull(params, "HTTP parameters");
   this.sessionBuffer = buffer;
   this.messageConstraints = HttpParamConfig.getMessageConstraints(params);
   this.lineParser = (parser != null) ? parser : BasicLineParser.INSTANCE;
   this.headerLines = new ArrayList<CharArrayBuffer>();
   this.state = HEAD_LINE;
 }
Пример #2
0
 /**
  * Obtains value of the {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET} parameter. If not set,
  * defaults to <code>ISO-8859-1</code>.
  *
  * @param params HTTP parameters.
  * @return HTTP content charset.
  */
 public static String getContentCharset(final HttpParams params) {
   Args.notNull(params, "HTTP parameters");
   String charset = (String) params.getParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET);
   if (charset == null) {
     charset = HTTP.DEF_CONTENT_CHARSET.name();
   }
   return charset;
 }
Пример #3
0
 /**
  * Obtains value of the {@link CoreProtocolPNames#PROTOCOL_VERSION} parameter. If not set,
  * defaults to {@link HttpVersion#HTTP_1_1}.
  *
  * @param params HTTP parameters.
  * @return HTTP protocol version.
  */
 public static ProtocolVersion getVersion(final HttpParams params) {
   Args.notNull(params, "HTTP parameters");
   final Object param = params.getParameter(CoreProtocolPNames.PROTOCOL_VERSION);
   if (param == null) {
     return HttpVersion.HTTP_1_1;
   }
   return (ProtocolVersion) param;
 }
Пример #4
0
 /**
  * Obtains the value of the {@link CoreProtocolPNames#HTTP_UNMAPPABLE_INPUT_ACTION} parameter.
  *
  * @param params HTTP parameters
  * @return Action to perform upon receiving a unmapped input
  * @since 4.2
  */
 public static CodingErrorAction getUnmappableInputAction(final HttpParams params) {
   Args.notNull(params, "HTTP parameters");
   final Object param = params.getParameter(CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION);
   if (param == null) {
     // the default CodingErrorAction
     return CodingErrorAction.REPORT;
   }
   return (CodingErrorAction) param;
 }
Пример #5
0
 /**
  * Creates new instance of AbstractMessageParser.
  *
  * @param buffer the session input buffer.
  * @param lineParser the line parser. If <code>null</code> {@link BasicLineParser#INSTANCE} will
  *     be used.
  * @param constraints the message constraints. If <code>null</code> {@link
  *     MessageConstraints#DEFAULT} will be used.
  * @since 4.3
  */
 public AbstractMessageParser(
     final SessionInputBuffer buffer,
     final LineParser lineParser,
     final MessageConstraints constraints) {
   super();
   this.sessionBuffer = Args.notNull(buffer, "Session input buffer");
   this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE;
   this.messageConstraints = constraints != null ? constraints : MessageConstraints.DEFAULT;
   this.headerLines = new ArrayList<CharArrayBuffer>();
   this.state = HEAD_LINE;
 }
Пример #6
0
 public void writeTo(final OutputStream outstream) throws IOException {
   Args.notNull(outstream, "Output stream");
   final InputStream instream = getContent();
   try {
     int l;
     final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
     while ((l = instream.read(tmp)) != -1) {
       outstream.write(tmp, 0, l);
     }
   } finally {
     instream.close();
   }
 }
Пример #7
0
  /**
   * Parses HTTP headers from the data receiver stream according to the generic format as given in
   * Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
   *
   * @param inbuffer Session input buffer
   * @param maxHeaderCount maximum number of headers allowed. If the number of headers received from
   *     the data stream exceeds maxCount value, an IOException will be thrown. Setting this
   *     parameter to a negative value or zero will disable the check.
   * @param maxLineLen maximum number of characters for a header line, including the continuation
   *     lines. Setting this parameter to a negative value or zero will disable the check.
   * @param parser line parser to use.
   * @param headerLines List of header lines. This list will be used to store intermediate results.
   *     This makes it possible to resume parsing of headers in case of a {@link
   *     java.io.InterruptedIOException}.
   * @return array of HTTP headers
   * @throws IOException in case of an I/O error
   * @throws HttpException in case of HTTP protocol violation
   * @since 4.1
   */
  public static Header[] parseHeaders(
      final SessionInputBuffer inbuffer,
      final int maxHeaderCount,
      final int maxLineLen,
      final LineParser parser,
      final List<CharArrayBuffer> headerLines)
      throws HttpException, IOException {
    Args.notNull(inbuffer, "Session input buffer");
    Args.notNull(parser, "Line parser");
    Args.notNull(headerLines, "Header line list");

    CharArrayBuffer current = null;
    CharArrayBuffer previous = null;
    for (; ; ) {
      if (current == null) {
        current = new CharArrayBuffer(64);
      } else {
        current.clear();
      }
      final int l = inbuffer.readLine(current);
      if (l == -1 || current.length() < 1) {
        break;
      }
      // Parse the header name and value
      // Check for folded headers first
      // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
      // discussion on folded headers
      if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
        // we have continuation folded header
        // so append value
        int i = 0;
        while (i < current.length()) {
          final char ch = current.charAt(i);
          if (ch != ' ' && ch != '\t') {
            break;
          }
          i++;
        }
        if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) {
          throw new MessageConstraintException("Maximum line length limit exceeded");
        }
        previous.append(' ');
        previous.append(current, i, current.length() - i);
      } else {
        headerLines.add(current);
        previous = current;
        current = null;
      }
      if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) {
        throw new MessageConstraintException("Maximum header count exceeded");
      }
    }
    final Header[] headers = new Header[headerLines.size()];
    for (int i = 0; i < headerLines.size(); i++) {
      final CharArrayBuffer buffer = headerLines.get(i);
      try {
        headers[i] = parser.parseHeader(buffer);
      } catch (final ParseException ex) {
        throw new ProtocolException(ex.getMessage());
      }
    }
    return headers;
  }
Пример #8
0
 /**
  * Sets value of the {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET} parameter.
  *
  * @param params HTTP parameters.
  * @param charset HTTP content charset.
  */
 public static void setContentCharset(final HttpParams params, final String charset) {
   Args.notNull(params, "HTTP parameters");
   params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset);
 }
Пример #9
0
 /**
  * Sets the value of the {@link CoreProtocolPNames#HTTP_UNMAPPABLE_INPUT_ACTION} parameter.
  *
  * @param params HTTP parameters
  * @param action action to perform on un mappable inputs
  * @since 4.2
  */
 public static void setUnmappableInputAction(
     final HttpParams params, final CodingErrorAction action) {
   Args.notNull(params, "HTTP parameters");
   params.setParameter(CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION, action);
 }
Пример #10
0
 /**
  * Sets value of the {@link CoreProtocolPNames#HTTP_MALFORMED_INPUT_ACTION} parameter.
  *
  * @param params HTTP parameters
  * @param action action to perform on malformed inputs
  * @since 4.2
  */
 public static void setMalformedInputAction(
     final HttpParams params, final CodingErrorAction action) {
   Args.notNull(params, "HTTP parameters");
   params.setParameter(CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION, action);
 }
Пример #11
0
 /**
  * Sets value of the {@link CoreProtocolPNames#USE_EXPECT_CONTINUE} parameter.
  *
  * @param params HTTP parameters.
  * @param b expect-continue flag.
  */
 public static void setUseExpectContinue(final HttpParams params, final boolean b) {
   Args.notNull(params, "HTTP parameters");
   params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, b);
 }
Пример #12
0
 /**
  * Obtains value of the {@link CoreProtocolPNames#USE_EXPECT_CONTINUE} parameter. If not set,
  * returns <code>false</code>.
  *
  * @param params HTTP parameters.
  * @return User agent string.
  */
 public static boolean useExpectContinue(final HttpParams params) {
   Args.notNull(params, "HTTP parameters");
   return params.getBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
 }
Пример #13
0
 /**
  * Sets value of the {@link CoreProtocolPNames#USER_AGENT} parameter.
  *
  * @param params HTTP parameters.
  * @param useragent User agent string.
  */
 public static void setUserAgent(final HttpParams params, final String useragent) {
   Args.notNull(params, "HTTP parameters");
   params.setParameter(CoreProtocolPNames.USER_AGENT, useragent);
 }
Пример #14
0
 /**
  * Obtains value of the {@link CoreProtocolPNames#USER_AGENT} parameter. If not set, returns
  * <code>null</code>.
  *
  * @param params HTTP parameters.
  * @return User agent string.
  */
 public static String getUserAgent(final HttpParams params) {
   Args.notNull(params, "HTTP parameters");
   return (String) params.getParameter(CoreProtocolPNames.USER_AGENT);
 }
Пример #15
0
 /**
  * Sets value of the {@link CoreProtocolPNames#PROTOCOL_VERSION} parameter.
  *
  * @param params HTTP parameters.
  * @param version HTTP protocol version.
  */
 public static void setVersion(final HttpParams params, final ProtocolVersion version) {
   Args.notNull(params, "HTTP parameters");
   params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, version);
 }