Search in sources :

Example 21 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class OAuthSignatureCalculatorTest method testPostCalculateSignature.

@Test
public void testPostCalculateSignature() throws UnsupportedEncodingException {
    // 
    StaticOAuthSignatureCalculator calc = new // 
    StaticOAuthSignatureCalculator(new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET), new RequestToken(TOKEN_KEY, TOKEN_SECRET), NONCE, TIMESTAMP);
    final Request req = post("http://photos.example.net/photos").addFormParam("file", "vacation.jpg").addFormParam("size", "original").setSignatureCalculator(calc).build();
    // From the signature tester, POST should look like:
    // normalized parameters:
    // file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original
    // signature base string:
    // POST&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal
    // signature: wPkvxykrw+BTdCcGqKr+3I+PsiM=
    // header: OAuth
    // realm="",oauth_version="1.0",oauth_consumer_key="dpf43f3p2l4k3l03",oauth_token="nnch734d00sl2jdk",oauth_timestamp="1191242096",oauth_nonce="kllo9940pd9333jh",oauth_signature_method="HMAC-SHA1",oauth_signature="wPkvxykrw%2BBTdCcGqKr%2B3I%2BPsiM%3D"
    String authHeader = req.getHeaders().get(AUTHORIZATION);
    Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader);
    assertEquals(m.find(), true);
    String encodedSig = m.group(1);
    String sig = URLDecoder.decode(encodedSig, "UTF-8");
    assertEquals(sig, "wPkvxykrw+BTdCcGqKr+3I+PsiM=");
}
Also used : Matcher(java.util.regex.Matcher) Request(org.asynchttpclient.Request) Test(org.testng.annotations.Test)

Example 22 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class OAuthSignatureCalculatorTest method testWithNullRequestToken.

@Test
public void testWithNullRequestToken() throws NoSuchAlgorithmException {
    final Request request = get("http://photos.example.net/photos?file=vacation.jpg&size=original").build();
    String signatureBaseString = new OAuthSignatureCalculatorInstance().signatureBaseString(new ConsumerKey("9djdj82h48djs9d2", CONSUMER_SECRET), new RequestToken(null, null), request.getUri(), request.getMethod(), request.getFormParams(), request.getQueryParams(), 137131201, Utf8UrlEncoder.percentEncodeQueryElement("ZLc92RAkooZcIO/0cctl0Q==")).toString();
    assertEquals(signatureBaseString, "GET&" + "http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" + "oauth_consumer_key%3D9djdj82h48djs9d2%26" + "oauth_nonce%3DZLc92RAkooZcIO%252F0cctl0Q%253D%253D%26" + "oauth_signature_method%3DHMAC-SHA1%26" + "oauth_timestamp%3D137131201%26" + "oauth_version%3D1.0%26size%3Doriginal");
}
Also used : Request(org.asynchttpclient.Request) Test(org.testng.annotations.Test)

Example 23 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class OAuthSignatureCalculatorTest method testSignatureBaseStringWithRawUri.

@Test
public void testSignatureBaseStringWithRawUri() throws NoSuchAlgorithmException {
    // note: @ is legal so don't decode it into %40 because it won't be
    // encoded back
    // note: we don't know how to fix a = that should have been encoded as
    // %3D but who would be stupid enough to do that?
    Request request = post("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r b").addFormParam("c2", "").addFormParam("a3", "2 q").build();
    testSignatureBaseString(request);
    testSignatureBaseStringWithEncodableOAuthToken(request);
}
Also used : Request(org.asynchttpclient.Request) Test(org.testng.annotations.Test)

Example 24 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class OAuthSignatureCalculatorTest method testGetWithRequestBuilder.

@Test
public void testGetWithRequestBuilder() throws UnsupportedEncodingException {
    StaticOAuthSignatureCalculator calc = new StaticOAuthSignatureCalculator(new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET), new RequestToken(TOKEN_KEY, TOKEN_SECRET), NONCE, TIMESTAMP);
    final Request req = get("http://photos.example.net/photos").addQueryParam("file", "vacation.jpg").addQueryParam("size", "original").setSignatureCalculator(calc).build();
    final List<Param> params = req.getQueryParams();
    assertEquals(params.size(), 2);
    // From the signature tester, the URL should look like:
    // normalized parameters:
    // file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original
    // signature base string:
    // GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal
    // signature: tR3+Ty81lMeYAr/Fid0kMTYa/WM=
    // Authorization header: OAuth
    // realm="",oauth_version="1.0",oauth_consumer_key="dpf43f3p2l4k3l03",oauth_token="nnch734d00sl2jdk",oauth_timestamp="1191242096",oauth_nonce="kllo9940pd9333jh",oauth_signature_method="HMAC-SHA1",oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D"
    String authHeader = req.getHeaders().get(AUTHORIZATION);
    Matcher m = Pattern.compile("oauth_signature=\"(.+?)\"").matcher(authHeader);
    assertEquals(m.find(), true);
    String encodedSig = m.group(1);
    String sig = URLDecoder.decode(encodedSig, "UTF-8");
    assertEquals(sig, "tR3+Ty81lMeYAr/Fid0kMTYa/WM=");
    assertEquals(req.getUrl(), "http://photos.example.net/photos?file=vacation.jpg&size=original");
}
Also used : Matcher(java.util.regex.Matcher) Request(org.asynchttpclient.Request) Param(org.asynchttpclient.Param) Test(org.testng.annotations.Test)

Example 25 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class NettyConnectListener method onSuccess.

public void onSuccess(Channel channel, InetSocketAddress remoteAddress) {
    if (connectionSemaphore != null) {
        // transfer lock from future to channel
        Object partitionKeyLock = future.takePartitionKeyLock();
        if (partitionKeyLock != null) {
            channel.closeFuture().addListener(future -> connectionSemaphore.releaseChannelLock(partitionKeyLock));
        }
    }
    Channels.setActiveToken(channel);
    TimeoutsHolder timeoutsHolder = future.getTimeoutsHolder();
    if (futureIsAlreadyCancelled(channel)) {
        return;
    }
    Request request = future.getTargetRequest();
    Uri uri = request.getUri();
    timeoutsHolder.setResolvedRemoteAddress(remoteAddress);
    ProxyServer proxyServer = future.getProxyServer();
    // in case of proxy tunneling, we'll add the SslHandler later, after the CONNECT request
    if ((proxyServer == null || proxyServer.getProxyType().isSocks()) && uri.isSecured()) {
        SslHandler sslHandler;
        try {
            sslHandler = channelManager.addSslHandler(channel.pipeline(), uri, request.getVirtualHost(), proxyServer != null);
        } catch (Exception sslError) {
            onFailure(channel, sslError);
            return;
        }
        final AsyncHandler<?> asyncHandler = future.getAsyncHandler();
        try {
            asyncHandler.onTlsHandshakeAttempt();
        } catch (Exception e) {
            LOGGER.error("onTlsHandshakeAttempt crashed", e);
            onFailure(channel, e);
            return;
        }
        sslHandler.handshakeFuture().addListener(new SimpleFutureListener<Channel>() {

            @Override
            protected void onSuccess(Channel value) {
                try {
                    asyncHandler.onTlsHandshakeSuccess(sslHandler.engine().getSession());
                } catch (Exception e) {
                    LOGGER.error("onTlsHandshakeSuccess crashed", e);
                    NettyConnectListener.this.onFailure(channel, e);
                    return;
                }
                writeRequest(channel);
            }

            @Override
            protected void onFailure(Throwable cause) {
                try {
                    asyncHandler.onTlsHandshakeFailure(cause);
                } catch (Exception e) {
                    LOGGER.error("onTlsHandshakeFailure crashed", e);
                    NettyConnectListener.this.onFailure(channel, e);
                    return;
                }
                NettyConnectListener.this.onFailure(channel, cause);
            }
        });
    } else {
        writeRequest(channel);
    }
}
Also used : Channel(io.netty.channel.Channel) HttpRequest(io.netty.handler.codec.http.HttpRequest) Request(org.asynchttpclient.Request) Uri(org.asynchttpclient.uri.Uri) SslHandler(io.netty.handler.ssl.SslHandler) ConnectException(java.net.ConnectException) TimeoutsHolder(org.asynchttpclient.netty.timeout.TimeoutsHolder) ProxyServer(org.asynchttpclient.proxy.ProxyServer)

Aggregations

Request (org.asynchttpclient.Request)67 Test (org.testng.annotations.Test)32 Response (org.asynchttpclient.Response)31 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)18 RequestBuilder (org.asynchttpclient.RequestBuilder)17 Test (org.junit.Test)16 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)8 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)8 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)7 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)6 ListenableFuture (org.asynchttpclient.ListenableFuture)6 Uri (org.asynchttpclient.uri.Uri)5 Realm (org.asynchttpclient.Realm)4 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 HttpResponse (io.netty.handler.codec.http.HttpResponse)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3