Java 类org.apache.http.impl.io.DefaultHttpResponseParser 实例源码

项目:pcap-reconst    文件:RecordedHttpMessageParser.java   
public static HttpResponse parseRecordedResponse(String respstring, MessageMetadata messdata) throws IOException, HttpException {
    MessageStringInputBuffer buf = new MessageStringInputBuffer(respstring);
    DefaultHttpResponseParser parser = new DefaultHttpResponseParser(buf, 
            new BasicLineParser(), 
            new RecordedHttpResponseFactory(messdata), 
            new BasicHttpParams());
    HttpResponse response = parser.parse();
    parseEntity(response, buf);
    return response;
}
项目:timbuctoo    文件:ResponseCommand.java   
private ExpectedResult parseExpectedResponse(Element element, Evaluator evaluator, ResultRecorder resultRecorder) {
  String contents = getTextAndRemoveIndent(element);

  contents = replaceVariableReferences(evaluator, contents, resultRecorder);

  SessionInputBufferImpl buffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), contents.length());
  buffer.bind(new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8)));
  DefaultHttpResponseParser defaultHttpResponseParser = new DefaultHttpResponseParser(buffer);

  ExpectedResult.ExpectedResultBuilder builder = expectedResult();
  String body = null;
  try {
    HttpResponse httpResponse = defaultHttpResponseParser.parse();
    StatusLine statusLine = httpResponse.getStatusLine();
    builder.withStatus(statusLine.getStatusCode());

    for (Header header : httpResponse.getAllHeaders()) {
      builder.withHeader(header.getName(), header.getValue());
    }

    if (buffer.hasBufferedData()) {
      body = "";

      while (buffer.hasBufferedData()) {
        body += (char) buffer.read();
      }
    }
    builder.withBody(body);
  } catch (IOException | HttpException e) {
    e.printStackTrace();
  }

  return builder.build();
}
项目:CommonCrawlDocumentDownload    文件:ArcRecord.java   
/**
 * <p>Returns an HTTP response object parsed from the ARC record payload.<p>
 * <p>Note: The payload is parsed on-demand, but is only parsed once.  The
 * parsed data is saved for subsequent calls.</p>
 *
 * @return The ARC record payload as an HTTP response object.  See the Apache
 * HttpComponents project.
 */
public HttpResponse getHttpResponse()
    throws IOException, HttpException {

  if (this._httpResponse != null) {
      return this._httpResponse;
  }

  if (this._payload == null) {
    LOG.error("Unable to parse HTTP response: Payload has not been set"); return null;
  }

  if (this._url != null && !this._url.startsWith("http://") && !this._url.startsWith("https://")) {
    LOG.error("Unable to parse HTTP response: URL protocol is not HTTP"); return null;
  }

  this._httpResponse = null;

  // Find where the HTTP headers stop
  int end = this._searchForCRLFCRLF(this._payload);

  if (end == -1) {
    LOG.error("Unable to parse HTTP response: End of HTTP headers not found"); return null;
  }

  // Parse the HTTP status line and headers
  DefaultHttpResponseParser parser =
    new DefaultHttpResponseParser(
      new ByteArraySessionInputBuffer(this._payload, 0, end),
      new BasicLineParser(),
      new DefaultHttpResponseFactory(),
      new BasicHttpParams()
    );

  this._httpResponse = parser.parse();

  if (this._httpResponse == null) {
    LOG.error("Unable to parse HTTP response"); return null;
  }

  // Set the reset of the payload as the HTTP entity.  Use an InputStreamEntity
  // to avoid a memory copy.
  //trim trailing '\n' if it exists
  int entityLength = _payload.length-end;
  if (_payload.length > 0 && _payload[_payload.length-1]=='\n') {
    entityLength--;
  }
  InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(this._payload, end, entityLength), entityLength);
  entity.setContentType(this._httpResponse.getFirstHeader("Content-Type"));
  entity.setContentEncoding(this._httpResponse.getFirstHeader("Content-Encoding"));
  this._httpResponse.setEntity(entity);

  return this._httpResponse;
}
项目:rdap-conformance    文件:RawURIRequest.java   
/** {@inheritDoc} */
public boolean run() {
    boolean success = false;
    String error = null;
    try {
        String bu = context.getSpecification().getBaseUrl();
        if (bu.startsWith("https")) {
            return true;
        }
        URI uri = new URI(bu);
        String host = uri.getHost();

        context.acquireRequestPermit();
        Socket socket = new Socket(host, HTTP_PORT);
        OutputStream os = socket.getOutputStream();
        String request = "GET " + rawUri + " HTTP/1.1\n"
                         + "Host: " + host + "\n"
                         + "Accept: application/rdap+json\n\n";
        os.write(request.getBytes("UTF-8"));
        InputStream is  = socket.getInputStream();
        SessionInputBufferImpl sibl =
            new SessionInputBufferImpl(
                new HttpTransportMetricsImpl(),
                BUFFER_SIZE
            );
        sibl.bind(is);
        DefaultHttpResponseParser dhrp =
            new DefaultHttpResponseParser(sibl);
        HttpResponse hr = dhrp.parse();
        HttpEntity he = hr.getEntity();
        /* It is assumed that this class is used to produce
         * invalid requests. The error codes aren't checked here;
         * it's just for confirming that the content (if present)
         * is JSON. With some servers, e.g. Jetty, it's not
         * possible to do things like setting the content type in
         * this sort of situation, so that is explicitly not
         * checked. */
        if ((he == null) || (he.getContentLength() == 0)) {
            success = true;
        } else {
            InputStream isc = he.getContent();
            InputStreamReader iscr = new InputStreamReader(isc, "UTF-8");
            new Gson().fromJson(iscr, Map.class);
            success = true;
        }
    } catch (Exception e) {
        error = e.toString();
    }

    Result nr = new Result(proto);
    nr.setPath(rawUri);
    nr.setCode("content");
    if (success) {
        nr.setStatus(Status.Success);
    } else if (!nr.getStatusSet()) {
        nr.setStatus(Status.Failure);
    }
    String prefix = (expectedSuccess) ? "content" : "error content";
    nr.setInfo(success ? prefix + " is empty or JSON"
                       : prefix + " is not empty or JSON: " + error);
    context.addResult(nr);

    return success;
}
项目:lams    文件:AbstractHttpClientConnection.java   
/**
 * Creates an instance of {@link HttpMessageParser} to be used for parsing
 * HTTP responses received over this connection.
 * <p>
 * This method can be overridden in a super class in order to provide
 * a different implementation of the {@link HttpMessageParser} interface or
 * to pass a different implementation of the {@link LineParser} to the
 * the {@link DefaultHttpResponseParser} constructor.
 *
 * @param buffer the session input buffer.
 * @param responseFactory the HTTP response factory.
 * @param params HTTP parameters.
 * @return HTTP message parser.
 */
protected HttpMessageParser<HttpResponse> createResponseParser(
        final SessionInputBuffer buffer,
        final HttpResponseFactory responseFactory,
        final HttpParams params) {
    return new DefaultHttpResponseParser(buffer, null, responseFactory, params);
}