org.apache.http.impl.io.DefaultHttpRequestParser Java Examples

The following examples show how to use org.apache.http.impl.io.DefaultHttpRequestParser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpenstackNetworkingUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Deserializes raw payload into HttpRequest object.
 *
 * @param rawData raw http payload
 * @return HttpRequest object
 */
public static HttpRequest parseHttpRequest(byte[] rawData) {
    SessionInputBufferImpl sessionInputBuffer =
            new SessionInputBufferImpl(
                    new HttpTransportMetricsImpl(), HTTP_PAYLOAD_BUFFER);
    sessionInputBuffer.bind(new ByteArrayInputStream(rawData));
    DefaultHttpRequestParser requestParser =
                            new DefaultHttpRequestParser(sessionInputBuffer);
    try {
        return requestParser.parse();
    } catch (IOException | HttpException e) {
        log.warn("Failed to parse HttpRequest, due to {}", e);
    }

    return null;
}
 
Example #2
Source File: HttpRequestWarcRecord.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
private static HttpRequest readPayload(final BoundSessionInputBuffer buffer) throws IOException {
	DefaultHttpRequestParser requestParser = new DefaultHttpRequestParser(buffer);
	try {
		return requestParser.parse();
	} catch (HttpException e) {
		throw new WarcFormatException("Can't parse the request", e);
	}
}
 
Example #3
Source File: RequestCommand.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private HttpRequest parseRequest(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)));
  DefaultHttpRequestParser defaultHttpRequestParser = new DefaultHttpRequestParser(buffer);
  LinkedListMultimap<String, String> queryParameters = LinkedListMultimap.create();

  String method = "";
  String url = "";
  LinkedListMultimap<String, String> headers = LinkedListMultimap.create();
  String body = null;
  String server = null;
  try {
    org.apache.http.HttpRequest httpRequest = defaultHttpRequestParser.parse();
    method = httpRequest.getRequestLine().getMethod();
    url = httpRequest.getRequestLine().getUri();
    if (url.startsWith("#")) {
      url = "" + evaluator.evaluate(url);
    }
    Matcher matcher = Pattern.compile("(https?://[^/]+)(/.*)").matcher(url);
    if (matcher.matches()) {
      server = matcher.group(1);
      url = matcher.group(2);
    }

    if (url.contains("?")) {
      String[] urlAndQueryParameters = url.split("\\?");
      url = urlAndQueryParameters[0];
      for (String queryParameter : urlAndQueryParameters[1].split("&")) {
        String[] parameter = queryParameter.split("=");

        queryParameters.put(parameter[0], parameter[1]);
      }
    }

    for (Header header : httpRequest.getAllHeaders()) {
      headers.put(header.getName(), header.getValue());
    }

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

      while (buffer.hasBufferedData()) {
        body += (char) buffer.read();
      }
    }

  } catch (IOException | HttpException e) {
    e.printStackTrace();
  }


  return new HttpRequest(method, url, headers, body, server, queryParameters);
}