示例#1
0
  public static DataEmitter getBodyDecoder(
      DataEmitter emitter, RawHeaders headers, boolean server) {
    long _contentLength;
    try {
      _contentLength = Long.parseLong(headers.get("Content-Length"));
    } catch (Exception ex) {
      _contentLength = -1;
    }
    final long contentLength = _contentLength;
    if (-1 != contentLength) {
      if (contentLength < 0) {
        EndEmitter ender =
            EndEmitter.create(
                emitter.getServer(),
                new BodyDecoderException(
                    "not using chunked encoding, and no content-length found."));
        ender.setDataEmitter(emitter);
        emitter = ender;
        return emitter;
      }
      if (contentLength == 0) {
        EndEmitter ender = EndEmitter.create(emitter.getServer(), null);
        ender.setDataEmitter(emitter);
        emitter = ender;
        return emitter;
      }
      ContentLengthFilter contentLengthWatcher = new ContentLengthFilter(contentLength);
      contentLengthWatcher.setDataEmitter(emitter);
      emitter = contentLengthWatcher;
    } else if ("chunked".equalsIgnoreCase(headers.get("Transfer-Encoding"))) {
      ChunkedInputFilter chunker = new ChunkedInputFilter();
      chunker.setDataEmitter(emitter);
      emitter = chunker;
    } else {
      if ((server || headers.getStatusLine().contains("HTTP/1.1"))
          && !"close".equalsIgnoreCase(headers.get("Connection"))) {
        // if this is the server, and the client has not indicated a request body, the client is
        // done
        EndEmitter ender = EndEmitter.create(emitter.getServer(), null);
        ender.setDataEmitter(emitter);
        emitter = ender;
        return emitter;
      }
    }

    if ("gzip".equals(headers.get("Content-Encoding"))) {
      GZIPInputFilter gunzipper = new GZIPInputFilter();
      gunzipper.setDataEmitter(emitter);
      emitter = gunzipper;
    } else if ("deflate".equals(headers.get("Content-Encoding"))) {
      InflaterInputFilter inflater = new InflaterInputFilter();
      inflater.setDataEmitter(emitter);
      emitter = inflater;
    }

    // conversely, if this is the client (http 1.0), and the server has not indicated a request
    // body, we do not report
    // the close/end event until the server actually closes the connection.
    return emitter;
  }
示例#2
0
  public static AsyncHttpRequestBody getBody(
      DataEmitter emitter, CompletedCallback reporter, RawHeaders headers) {
    String contentType = headers.get("Content-Type");
    if (contentType != null) {
      String[] values = contentType.split(";");
      for (int i = 0; i < values.length; i++) {
        values[i] = values[i].trim();
      }
      for (String ct : values) {
        if (UrlEncodedFormBody.CONTENT_TYPE.equals(ct)) {
          return new UrlEncodedFormBody();
        }
        if (JSONObjectBody.CONTENT_TYPE.equals(ct)) {
          return new JSONObjectBody();
        }
        if (StringBody.CONTENT_TYPE.equals(ct)) {
          return new StringBody();
        }
        if (MultipartFormDataBody.CONTENT_TYPE.equals(ct)) {
          return new MultipartFormDataBody(contentType, values);
        }
      }
    }

    return null;
  }
示例#3
0
  public static boolean isKeepAlive(RawHeaders headers) {
    boolean keepAlive;
    String connection = headers.get("Connection");
    if (connection != null) {
      keepAlive = "keep-alive".equalsIgnoreCase(connection);
    } else {
      keepAlive = headers.getHttpMinorVersion() >= 1;
    }

    return keepAlive;
  }
 protected AsyncHttpRequestBody onUnknownBody(RawHeaders headers) {
   return new UnknownRequestBody(headers.get("Content-Type"));
 }