Skip to content

Commit

Permalink
Transport layer for proxy handshake (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
SreeramGarlapati authored Sep 13, 2018
1 parent cffc1c5 commit 2d2d5d1
Show file tree
Hide file tree
Showing 8 changed files with 1,099 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>com.microsoft.azure</groupId>
<artifactId>qpid-proton-j-extensions</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>

<url>https://github.com/Azure/qpid-proton-j-extensions</url>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

package com.microsoft.azure.proton.transport.proxy;

import java.util.Map;

import org.apache.qpid.proton.engine.Transport;

public interface Proxy {
enum ProxyState {
PN_PROXY_NOT_STARTED,
PN_PROXY_CONNECTING,
PN_PROXY_CONNECTED,
PN_PROXY_FAILED
}

void configure(
String host,
Map<String, String> headers,
ProxyHandler proxyHandler,
Transport underlyingTransport);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

package com.microsoft.azure.proton.transport.proxy;

import java.nio.ByteBuffer;
import java.util.Map;

public interface ProxyHandler {

class ProxyResponseResult {
private Boolean isSuccess;
private String error;

public ProxyResponseResult(final Boolean isSuccess, final String error) {
this.isSuccess = isSuccess;
this.error = error;
}

public Boolean getIsSuccess() {
return isSuccess;
}

public String getError() {
return error;
}
}

String createProxyRequest(String hostName, Map<String, String> additionalHeaders);

ProxyResponseResult validateProxyResponse(ByteBuffer buffer);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

package com.microsoft.azure.proton.transport.proxy.impl;

import com.microsoft.azure.proton.transport.proxy.ProxyHandler;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Scanner;

public class ProxyHandlerImpl implements ProxyHandler {

@Override
public String createProxyRequest(String hostName, Map<String, String> additionalHeaders) {
final String endOfLine = "\r\n";
final StringBuilder connectRequestBuilder = new StringBuilder();
connectRequestBuilder.append(
String.format(
"CONNECT %1$s HTTP/1.1%2$sHost: %1$s%2$sConnection: Keep-Alive%2$s",
hostName,
endOfLine));
if (additionalHeaders != null) {
for (Map.Entry<String, String> entry: additionalHeaders.entrySet()) {
connectRequestBuilder.append(entry.getKey());
connectRequestBuilder.append(": ");
connectRequestBuilder.append(entry.getValue());
connectRequestBuilder.append(endOfLine);
}
}
connectRequestBuilder.append(endOfLine);
return connectRequestBuilder.toString();
}

@Override
public ProxyResponseResult validateProxyResponse(ByteBuffer buffer) {
int size = buffer.remaining();
String response = null;

if (size > 0) {
byte[] responseBytes = new byte[buffer.remaining()];
buffer.get(responseBytes);
response = new String(responseBytes, StandardCharsets.UTF_8);
final Scanner responseScanner = new Scanner(response);
if (responseScanner.hasNextLine()) {
final String firstLine = responseScanner.nextLine();
if (firstLine.toLowerCase().contains("http/1.1")

This comment has been minimized.

Copy link
@jugi92

jugi92 May 2, 2019

Is it possible to consider http/1.0 as a valid request as well? Because as far as I can see it would work with that version aswell, so in my opinion this restriction unnecessarily limits the use of the library.

This comment has been minimized.

Copy link
@JamesBirdsall

JamesBirdsall May 2, 2019

Contributor

Are there a lot of proxies which only do 1.0? We've been testing with Squid, and the customers we're working with haven't complained, but we don't have a broad overview of the world of proxies.

This comment has been minimized.

Copy link
@jugi92

jugi92 May 3, 2019

I neither have a broad overview but I know that we are facing the issue. And as it works when I test it and change the value manually to 1.1 to pass this if statement, it is in my opinion an unnecessary limitation of the library.

This comment has been minimized.

Copy link
@jugi92

jugi92 May 9, 2019

@JamesBirdsall Any thoughts on this?

This comment has been minimized.

Copy link
@JamesBirdsall

JamesBirdsall May 9, 2019

Contributor

If you are actually hitting the issue, that's different. I thought this was a theoretical objection. We will be doing a new release in the next few weeks and I will make the change.

This comment has been minimized.

Copy link
@JamesBirdsall

JamesBirdsall May 9, 2019

Contributor

@jugi92 I have pushed a new branch "http10" with the change you described. I think that's all that's needed to support a http 1.0 proxy, but since I don't have one, I can't really test it. Do you have the ability to build from the branch and try it?

&& firstLine.contains("200")
&& firstLine.toLowerCase().contains("connection established")) {
return new ProxyResponseResult(true, null);
}
}
}

return new ProxyResponseResult(false, response);
}
}
Loading

0 comments on commit 2d2d5d1

Please sign in to comment.