use of org.apache.http.impl.io.HttpTransportMetricsImpl in project timbuctoo by HuygensING.
the class ResponseCommand method parseExpectedResponse.
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();
}
use of org.apache.http.impl.io.HttpTransportMetricsImpl in project indy by Commonjava.
the class ProxyRequestReader method handleEvent.
// TODO: May need to tune this to preserve request body.
// TODO: NONE of the request headers (except authorization) are passed through!
@Override
public void handleEvent(final ConduitStreamSourceChannel sourceChannel) {
boolean sendResponse = false;
try {
final int read = doRead(sourceChannel);
if (read <= 0) {
logger.debug("Reads: {} ", read);
return;
}
byte[] bytes = bReq.toByteArray();
if (sslTunnel != null) {
logger.debug("Send to ssl tunnel, {}, bytes:\n\n {}\n", new String(bytes), Hex.encodeHexString(bytes));
directTo(sslTunnel);
return;
}
logger.debug("Request in progress is:\n\n{}", new String(bytes));
if (headDone) {
logger.debug("Request done. parsing.");
MessageConstraints mc = MessageConstraints.DEFAULT;
SessionInputBufferImpl inbuf = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 1024);
HttpRequestFactory requestFactory = new DefaultHttpRequestFactory();
LineParser lp = new BasicLineParser();
DefaultHttpRequestParser requestParser = new DefaultHttpRequestParser(inbuf, lp, requestFactory, mc);
inbuf.bind(new ByteArrayInputStream(bytes));
try {
logger.debug("Passing parsed http request off to response writer.");
HttpRequest request = requestParser.parse();
Optional<SpanAdapter> span;
if (traceManager.isPresent())
span = traceManager.get().startServiceRootSpan("httprox-get", contextExtractor(request));
else
span = Optional.empty();
logger.debug("Request contains {} header: '{}'", ApplicationHeader.authorization.key(), request.getHeaders(ApplicationHeader.authorization.key()));
writer.setHttpRequest(request);
writer.setSpan(span);
sendResponse = true;
} catch (ConnectionClosedException e) {
logger.warn("Client closed connection. Aborting proxy request.");
sendResponse = false;
sourceChannel.shutdownReads();
} catch (HttpException e) {
logger.error("Failed to parse http request: " + e.getMessage(), e);
writer.setError(e);
}
} else {
logger.debug("Request not finished. Pausing until more reads are available.");
sourceChannel.resumeReads();
}
} catch (final IOException e) {
writer.setError(e);
sendResponse = true;
}
if (sendResponse) {
sinkChannel.resumeWrites();
}
}
use of org.apache.http.impl.io.HttpTransportMetricsImpl in project timbuctoo by HuygensING.
the class RequestCommand method parseRequest.
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);
}
Aggregations