Skip to content

Commit 3d36dba

Browse files
committed
Merge pull request AsyncHttpClient#26 from rlubke/master
Several fixes/improvements
2 parents 44ef934 + 3287745 commit 3d36dba

11 files changed

+312
-115
lines changed

src/main/java/com/ning/http/client/AsyncHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public Request build() {
275275
if (i >= 0) {
276276
url = url.substring(0, i);
277277
}
278-
signatureCalculator.calculateAndAddSignature(baseURL, request, this);
278+
signatureCalculator.calculateAndAddSignature(url, request, this);
279279
}
280280
return super.build();
281281
}

src/main/java/com/ning/http/client/generators/InputStreamBodyGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public long getContentLength() {
6767
public long read(ByteBuffer buffer) throws IOException {
6868

6969
// To be safe.
70-
chunk = new byte[buffer.capacity() - 10];
70+
chunk = new byte[buffer.remaining() - 10];
7171

7272

7373
int read = -1;

src/main/java/com/ning/http/client/ntlm/NTLMEngine.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class NTLMEngine {
7676
java.security.SecureRandom rnd = null;
7777
try {
7878
rnd = java.security.SecureRandom.getInstance("SHA1PRNG");
79-
} catch (Exception e) {
79+
} catch (Exception ignored) {
8080
}
8181
RND_GEN = rnd;
8282
}
@@ -753,7 +753,7 @@ String getResponse() throws UnsupportedEncodingException {
753753
} else {
754754
resp = messageContents;
755755
}
756-
return new String(Base64.encode(resp));
756+
return Base64.encode(resp);
757757
}
758758

759759
}
@@ -996,8 +996,7 @@ String getResponse() throws UnsupportedEncodingException {
996996
int domainOffset = ntRespOffset + ntRespLen;
997997
int userOffset = domainOffset + domainLen;
998998
int hostOffset = userOffset + userLen;
999-
int sessionKeyOffset = hostOffset + hostLen;
1000-
int finalLength = sessionKeyOffset + 0;
999+
int finalLength = hostOffset + hostLen;
10011000

10021001
// Start the response. Length includes signature and type
10031002
prepareResponse(finalLength, 3);
@@ -1127,7 +1126,6 @@ void update(byte[] input) {
11271126
int transferAmt = input.length - inputIndex;
11281127
System.arraycopy(input, inputIndex, dataBuffer, curBufferPos, transferAmt);
11291128
count += transferAmt;
1130-
curBufferPos += transferAmt;
11311129
}
11321130
}
11331131

src/main/java/com/ning/http/client/oauth/OAuthSignatureCalculator.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ public class OAuthSignatureCalculator
4545
implements SignatureCalculator {
4646
public final static String HEADER_AUTHORIZATION = "Authorization";
4747

48-
private final String KEY_OAUTH_CONSUMER_KEY = "oauth_consumer_key";
49-
private final String KEY_OAUTH_NONCE = "oauth_nonce";
50-
private final String KEY_OAUTH_SIGNATURE = "oauth_signature";
51-
private final String KEY_OAUTH_SIGNATURE_METHOD = "oauth_signature_method";
52-
private final String KEY_OAUTH_TIMESTAMP = "oauth_timestamp";
53-
private final String KEY_OAUTH_TOKEN = "oauth_token";
54-
private final String KEY_OAUTH_VERSION = "oauth_version";
48+
private static final String KEY_OAUTH_CONSUMER_KEY = "oauth_consumer_key";
49+
private static final String KEY_OAUTH_NONCE = "oauth_nonce";
50+
private static final String KEY_OAUTH_SIGNATURE = "oauth_signature";
51+
private static final String KEY_OAUTH_SIGNATURE_METHOD = "oauth_signature_method";
52+
private static final String KEY_OAUTH_TIMESTAMP = "oauth_timestamp";
53+
private static final String KEY_OAUTH_TOKEN = "oauth_token";
54+
private static final String KEY_OAUTH_VERSION = "oauth_version";
5555

56-
private final String OAUTH_VERSION_1_0 = "1.0";
57-
private final String OAUTH_SIGNATURE_METHOD = "HMAC-SHA1";
56+
private static final String OAUTH_VERSION_1_0 = "1.0";
57+
private static final String OAUTH_SIGNATURE_METHOD = "HMAC-SHA1";
5858

5959
/**
6060
* To generate Nonce, need some (pseudo)randomness; no need for
@@ -84,12 +84,12 @@ public OAuthSignatureCalculator(ConsumerKey consumerAuth, RequestToken userAuth)
8484
//@Override // silly 1.5; doesn't allow this for interfaces
8585

8686
public void calculateAndAddSignature(String baseURL, Request request, RequestBuilderBase<?> requestBuilder) {
87-
String method = request.getMethod().toString(); // POST etc
87+
String method = request.getMethod(); // POST etc
8888
String nonce = generateNonce();
8989
long timestamp = System.currentTimeMillis() / 1000L;
9090
String signature = calculateSignature(method, baseURL, timestamp, nonce, request.getParams(), request.getQueryParams());
9191
String headerValue = constructAuthHeader(signature, nonce, timestamp);
92-
requestBuilder = requestBuilder.setHeader(HEADER_AUTHORIZATION, headerValue);
92+
requestBuilder.setHeader(HEADER_AUTHORIZATION, headerValue);
9393
}
9494

9595
/**
@@ -260,5 +260,25 @@ public int compareTo(Parameter other) {
260260
public String toString() {
261261
return key + "=" + value;
262262
}
263+
264+
@Override
265+
public boolean equals(Object o) {
266+
if (this == o) return true;
267+
if (o == null || getClass() != o.getClass()) return false;
268+
269+
Parameter parameter = (Parameter) o;
270+
271+
if (!key.equals(parameter.key)) return false;
272+
if (!value.equals(parameter.value)) return false;
273+
274+
return true;
275+
}
276+
277+
@Override
278+
public int hashCode() {
279+
int result = key.hashCode();
280+
result = 31 * result + value.hashCode();
281+
return result;
282+
}
263283
}
264284
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2011 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client.providers.grizzly;
14+
15+
import com.ning.http.client.Body;
16+
import com.ning.http.client.BodyGenerator;
17+
import java.io.IOException;
18+
import java.nio.ByteBuffer;
19+
import java.util.Queue;
20+
import java.util.concurrent.ConcurrentLinkedQueue;
21+
import java.util.concurrent.atomic.AtomicInteger;
22+
import org.glassfish.grizzly.Buffer;
23+
import org.glassfish.grizzly.filterchain.FilterChainContext;
24+
import org.glassfish.grizzly.http.HttpContent;
25+
import org.glassfish.grizzly.http.HttpRequestPacket;
26+
27+
/**
28+
* {@link BodyGenerator} which may return just part of the payload at the time
29+
* handler is requesting it. If it happens - PartialBodyGenerator becomes responsible
30+
* for finishing payload transferring asynchronously.
31+
*
32+
* @author The Grizzly Team
33+
* @since 1.7.0
34+
*/
35+
public class FeedableBodyGenerator implements BodyGenerator {
36+
private final Queue<BodyPart> queue = new ConcurrentLinkedQueue<BodyPart>();
37+
private final AtomicInteger queueSize = new AtomicInteger();
38+
39+
private volatile HttpRequestPacket requestPacket;
40+
private volatile FilterChainContext context;
41+
42+
@Override
43+
public Body createBody() throws IOException {
44+
return new EmptyBody();
45+
}
46+
47+
public void feed(final Buffer buffer, final boolean isLast)
48+
throws IOException {
49+
queue.offer(new BodyPart(buffer, isLast));
50+
queueSize.incrementAndGet();
51+
52+
if (context != null) {
53+
flushQueue();
54+
}
55+
}
56+
57+
void initializeAsynchronousTransfer(final FilterChainContext context,
58+
final HttpRequestPacket requestPacket) throws IOException {
59+
this.context = context;
60+
this.requestPacket = requestPacket;
61+
flushQueue();
62+
}
63+
64+
private void flushQueue() throws IOException {
65+
if (queueSize.get() > 0) {
66+
synchronized(this) {
67+
while(queueSize.get() > 0) {
68+
final BodyPart bodyPart = queue.poll();
69+
queueSize.decrementAndGet();
70+
final HttpContent content =
71+
requestPacket.httpContentBuilder()
72+
.content(bodyPart.buffer)
73+
.last(bodyPart.isLast)
74+
.build();
75+
context.write(content, ((!requestPacket.isCommitted()) ?
76+
context.getTransportContext().getCompletionHandler() :
77+
null));
78+
79+
}
80+
}
81+
}
82+
}
83+
84+
private final class EmptyBody implements Body {
85+
86+
@Override
87+
public long getContentLength() {
88+
return -1;
89+
}
90+
91+
@Override
92+
public long read(final ByteBuffer buffer) throws IOException {
93+
return 0;
94+
}
95+
96+
@Override
97+
public void close() throws IOException {
98+
context.completeAndRecycle();
99+
context = null;
100+
requestPacket = null;
101+
}
102+
}
103+
104+
private final static class BodyPart {
105+
private final boolean isLast;
106+
private final Buffer buffer;
107+
108+
public BodyPart(final Buffer buffer, final boolean isLast) {
109+
this.buffer = buffer;
110+
this.isLast = isLast;
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)