use of org.apache.http.RequestLine in project indy by Commonjava.
the class ProxyResponseWriter method doHandleEvent.
private void doHandleEvent(final ConduitStreamSinkChannel sinkChannel) {
if (directed) {
return;
}
ProxyMeter meter = new ProxyMeter(httpRequest.getRequestLine().getMethod(), httpRequest.getRequestLine().toString(), startNanos, sliMetricSet, restLogger, peerAddress);
try {
HttpConduitWrapper http = new HttpConduitWrapper(sinkChannel, httpRequest, contentController, cacheProvider);
if (httpRequest == null) {
if (error != null) {
logger.debug("Handling error from request reader: " + error.getMessage(), error);
handleError(error, http);
} else {
logger.debug("Invalid state (no error or request) from request reader. Sending 400.");
try {
http.writeStatus(ApplicationStatus.BAD_REQUEST);
} catch (final IOException e) {
logger.error("Failed to write BAD REQUEST for missing HTTP first-line to response channel.", e);
}
}
return;
}
restLogger.info("SERVE {} (from: {})", httpRequest.getRequestLine(), peerAddress);
// TODO: Can we handle this?
final String oldThreadName = Thread.currentThread().getName();
Thread.currentThread().setName("PROXY-" + httpRequest.getRequestLine().toString());
sinkChannel.getCloseSetter().set((c) -> {
logger.trace("Sink channel closing.");
Thread.currentThread().setName(oldThreadName);
if (sslTunnel != null) {
logger.trace("Close ssl tunnel");
sslTunnel.close();
}
});
logger.debug("\n\n\n>>>>>>> Handle write\n\n\n");
if (error == null) {
ProxyResponseHelper proxyResponseHelper = new ProxyResponseHelper(httpRequest, config, contentController, repoCreator, storeManager, metricsConfig, metricManager, cls);
try {
if (repoCreator == null) {
throw new IndyDataException("No valid instance of ProxyRepositoryCreator");
}
final UserPass proxyUserPass = parse(ApplicationHeader.proxy_authorization, httpRequest, null);
logger.info("Using proxy authentication: {}", proxyUserPass);
mdcManager.putExtraHeaders(httpRequest);
// FIXME: We cannot trace through this interface currently!
mdcManager.putExternalID(proxyUserPass == null ? null : proxyUserPass.getUser());
logger.debug("Proxy UserPass: {}\nConfig secured? {}\nConfig tracking type: {}", proxyUserPass, config.isSecured(), config.getTrackingType());
if (proxyUserPass == null && (config.isSecured() || TrackingType.ALWAYS == config.getTrackingType())) {
String realmInfo = String.format(PROXY_AUTHENTICATE_FORMAT, config.getProxyRealm());
logger.info("Not authenticated to proxy. Sending response: {} / {}: {}", PROXY_AUTHENTICATION_REQUIRED, proxy_authenticate, realmInfo);
http.writeStatus(PROXY_AUTHENTICATION_REQUIRED);
http.writeHeader(proxy_authenticate, realmInfo);
} else {
RequestLine requestLine = httpRequest.getRequestLine();
String method = requestLine.getMethod().toUpperCase();
String trackingId = null;
boolean authenticated = true;
if (proxyUserPass != null) {
TrackingKey trackingKey = proxyResponseHelper.getTrackingKey(proxyUserPass);
if (trackingKey != null) {
trackingId = trackingKey.getId();
RequestContextHelper.setContext(RequestContextHelper.CONTENT_TRACKING_ID, trackingId);
}
String authCacheKey = generateAuthCacheKey(proxyUserPass);
Boolean isAuthToken = proxyAuthCache.get(authCacheKey);
if (Boolean.TRUE.equals(isAuthToken)) {
authenticated = true;
logger.debug("Found auth key in cache");
} else {
logger.debug("Passing BASIC authentication credentials to Keycloak bearer-token translation authenticator");
authenticated = proxyAuthenticator.authenticate(proxyUserPass, http);
if (authenticated) {
proxyAuthCache.put(authCacheKey, Boolean.TRUE, config.getAuthCacheExpirationHours(), TimeUnit.HOURS);
}
}
logger.debug("Authentication done, result: {}", authenticated);
}
if (authenticated) {
switch(method) {
case GET_METHOD:
case HEAD_METHOD:
{
final URL url = new URL(requestLine.getUri());
logger.debug("getArtifactStore starts, trackingId: {}, url: {}", trackingId, url);
ArtifactStore store = proxyResponseHelper.getArtifactStore(trackingId, url);
proxyResponseHelper.transfer(http, store, url.getPath(), GET_METHOD.equals(method), proxyUserPass, meter);
break;
}
case OPTIONS_METHOD:
{
http.writeStatus(ApplicationStatus.OK);
http.writeHeader(ApplicationHeader.allow, ALLOW_HEADER_VALUE);
break;
}
case CONNECT_METHOD:
{
if (!config.isMITMEnabled()) {
logger.debug("CONNECT method not supported unless MITM-proxying is enabled.");
http.writeStatus(ApplicationStatus.BAD_REQUEST);
break;
}
// e.g, github.com:443
String uri = requestLine.getUri();
logger.debug("Get CONNECT request, uri: {}", uri);
String[] toks = uri.split(":");
String host = toks[0];
int port = parseInt(toks[1]);
directed = true;
// After this, the proxy simply opens a plain socket to the target server and relays
// everything between the initial client and the target server (including the TLS handshake).
SocketChannel socketChannel;
ProxyMITMSSLServer svr = new ProxyMITMSSLServer(host, port, trackingId, proxyUserPass, proxyResponseHelper, contentController, cacheProvider, config, meter);
tunnelAndMITMExecutor.submit(svr);
socketChannel = svr.getSocketChannel();
if (socketChannel == null) {
logger.debug("Failed to get MITM socket channel");
http.writeStatus(ApplicationStatus.SERVER_ERROR);
svr.stop();
break;
}
sslTunnel = new ProxySSLTunnel(sinkChannel, socketChannel, config);
tunnelAndMITMExecutor.submit(sslTunnel);
// client input will be directed to target socket
proxyRequestReader.setProxySSLTunnel(sslTunnel);
// When all is ready, send the 200 to client. Client send the SSL handshake to reader,
// reader direct it to tunnel to MITM. MITM finish the handshake and read the request data,
// retrieve remote content and send back to tunnel to client.
http.writeStatus(ApplicationStatus.OK);
http.writeHeader("Status", "200 OK\n");
break;
}
default:
{
http.writeStatus(ApplicationStatus.METHOD_NOT_ALLOWED);
}
}
}
}
logger.debug("Response complete.");
} catch (final Throwable e) {
error = e;
} finally {
mdcManager.clear();
}
}
if (error != null) {
handleError(error, http);
}
try {
if (directed) {
// do not close sink channel
;
} else {
http.close();
}
} catch (final IOException e) {
logger.error("Failed to shutdown response", e);
}
} finally {
meter.reportResponseSummary();
span.ifPresent(SpanAdapter::close);
}
}
use of org.apache.http.RequestLine in project uavstack by uavorg.
the class ApacheAsyncHttpClientIT method doAsyncStart.
/**
* for async http client
*
* @param args
* @return
*/
@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
public FutureCallback doAsyncStart(Object[] args) {
HttpAsyncRequestProducer requestProducer = null;
HttpAsyncResponseConsumer responseConsumer = null;
HttpContext context = null;
FutureCallback callback = null;
Map mObj = null;
if (args.length == 4) {
requestProducer = (HttpAsyncRequestProducer) args[0];
responseConsumer = (HttpAsyncResponseConsumer) args[1];
context = (HttpContext) args[2];
callback = (FutureCallback) args[3];
} else if (args.length == 5) {
requestProducer = (HttpAsyncRequestProducer) args[1];
responseConsumer = (HttpAsyncResponseConsumer) args[2];
context = (HttpContext) args[3];
callback = (FutureCallback) args[4];
}
String httpAction = null;
try {
HttpRequest hr = requestProducer.generateRequest();
/**
* 呵呵,就是把UAV的客户端标记加到http header里面,接收方就知道是哪个东东调用的了,便于实现来源快速匹配,这个模式只适合直连模式
*
* 对于代理模式,只匹配代理源即可
*/
hr.addHeader("UAV-Client-Src", MonitorServerUtil.getUAVClientSrc(this.applicationId));
RequestLine rl = hr.getRequestLine();
httpAction = rl.getMethod();
targetURL = rl.getUri();
// 部分HttpRequest中没有ip:port,需要从httpHost中拿到再拼接
if (!targetURL.startsWith("http")) {
targetURL = requestProducer.getTarget().toURI() + targetURL;
}
} catch (IOException e) {
// ignore thie exception
return null;
} catch (HttpException e) {
// ignore thie exception
return null;
}
Map<String, Object> params = new HashMap<String, Object>();
params.put(CaptureConstants.INFO_CLIENT_REQUEST_URL, targetURL);
params.put(CaptureConstants.INFO_CLIENT_REQUEST_ACTION, httpAction);
params.put(CaptureConstants.INFO_CLIENT_APPID, this.applicationId);
params.put(CaptureConstants.INFO_CLIENT_TYPE, "apache.http.AsyncClient");
if (logger.isDebugable()) {
logger.debug("Invoke START:" + targetURL + "," + httpAction + "," + this.applicationId, null);
}
/**
* for async, as not in the same thread
*/
ccMap = UAVServer.instance().runMonitorAsyncCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT, Monitor.CapturePhase.PRECAP, params, null);
// register invokechain adapter
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "registerAdapter", ApacheAsyncHttpClientAdapter.class);
ccMap4Chain = (Map<String, Object>) UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap", InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.PRECAP, params, ApacheAsyncHttpClientAdapter.class, args);
if (callback == null) {
return null;
}
callback = JDKProxyInvokeUtil.newProxyInstance(HttpContext.class.getClassLoader(), new Class<?>[] { FutureCallback.class }, new JDKProxyInvokeHandler<FutureCallback>(callback, new FutureCallbackProxyInvokeProcessor(ccMap4Chain)));
return callback;
}
use of org.apache.http.RequestLine in project uavstack by uavorg.
the class ApacheHttpClientIT method doStart.
/**
* for http client
*
* @param args
* @return
*/
@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
public void doStart(Object[] args) {
HttpRequest request = null;
HttpContext context = null;
HttpHost target = null;
Map mObj = null;
if (args.length == 3) {
target = (HttpHost) args[0];
request = (HttpRequest) args[1];
context = (HttpContext) args[2];
}
String httpAction = null;
// String targetURL = null;
/**
* 呵呵,就是把UAV的客户端标记加到http header里面,接收方就知道是哪个东东调用的了,便于实现来源快速匹配,这个模式只适合直连模式
*
* 对于代理模式,只匹配代理源即可
*/
// 由于httpclient会追加header,此处应该删除之前的数据
request.removeHeaders("UAV-Client-Src");
request.addHeader("UAV-Client-Src", MonitorServerUtil.getUAVClientSrc(this.applicationId));
RequestLine rl = request.getRequestLine();
httpAction = rl.getMethod();
targetURl = rl.getUri();
// 部分HttpRequest中没有ip:port,需要从httpHost中拿到再拼接
if (!targetURl.startsWith("http")) {
targetURl = target.toURI() + targetURl;
}
Map<String, Object> params = new HashMap<String, Object>();
params.put(CaptureConstants.INFO_CLIENT_REQUEST_URL, targetURl);
params.put(CaptureConstants.INFO_CLIENT_REQUEST_ACTION, httpAction);
params.put(CaptureConstants.INFO_CLIENT_APPID, this.applicationId);
params.put(CaptureConstants.INFO_CLIENT_TYPE, "apache.http.Client");
if (logger.isDebugable()) {
logger.debug("Invoke START:" + targetURl + "," + httpAction + "," + this.applicationId, null);
}
UAVServer.instance().runMonitorCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT, Monitor.CapturePhase.PRECAP, params);
// register adapter
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "registerAdapter", ApacheHttpClientAdapter.class);
ivcContextParams = (Map<String, Object>) UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap", InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.PRECAP, params, ApacheHttpClientAdapter.class, args);
}
use of org.apache.http.RequestLine in project cdap-ingest by caskdata.
the class StreamInfoHttpRequestHandler method handle.
@Override
public void handle(HttpRequest httpRequest, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
RequestLine requestLine = httpRequest.getRequestLine();
String method = requestLine.getMethod();
int statusCode;
if (!HttpMethod.GET.equals(method)) {
statusCode = HttpStatus.SC_NOT_IMPLEMENTED;
} else {
String uri = requestLine.getUri();
String streamName = TestUtils.getStreamNameFromUri(uri);
if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
statusCode = TestUtils.authorize(httpRequest);
} else if (streamName.contains(TestUtils.WRITER_TEST_STREAM_NAME_POSTFIX)) {
statusCode = HttpStatus.SC_OK;
} else {
statusCode = TestUtils.getStatusCodeByStreamName(streamName);
}
if (statusCode == HttpStatus.SC_OK) {
StringEntity entity = new StringEntity("{\"partitionDuration\":3600000,\"indexInterval\":10000,\"ttl\":" + RestTest.STREAM_TTL + "}");
entity.setContentType(MediaType.APPLICATION_JSON);
response.setEntity(entity);
}
}
response.setStatusCode(statusCode);
}
use of org.apache.http.RequestLine in project cdap-ingest by caskdata.
the class StreamTruncateHttpRequestHandler method handle.
@Override
public void handle(HttpRequest httpRequest, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
RequestLine requestLine = httpRequest.getRequestLine();
String method = requestLine.getMethod();
int statusCode;
if (!HttpMethod.POST.equals(method)) {
statusCode = HttpStatus.SC_NOT_IMPLEMENTED;
} else {
String uri = requestLine.getUri();
String streamName = TestUtils.getStreamNameFromUri(uri);
if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
statusCode = TestUtils.authorize(httpRequest);
} else {
statusCode = TestUtils.getStatusCodeByStreamName(streamName);
}
}
response.setStatusCode(statusCode);
}
Aggregations