Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

okhttp protocol: HTTP request header lacks protocol name and version #775

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;
Expand Down Expand Up @@ -360,6 +361,15 @@ private final byte[] toByteArray(final ResponseBody responseBody,

class HTTPHeadersInterceptor implements Interceptor {

private String getNormalizedProtocolName(Protocol protocol) {
String name = protocol.toString().toUpperCase(Locale.ROOT);
if ("H2".equals(name)) {
// back-ward compatible protocol version name
name = "HTTP/2";
}
return name;
}

@Override
public Response intercept(Interceptor.Chain chain) throws IOException {

Expand All @@ -377,8 +387,11 @@ public Response intercept(Interceptor.Chain chain) throws IOException {
String u = request.url().toString()
.substring(position + request.url().host().length());

String httpProtocol = getNormalizedProtocolName(
connection.protocol());

requestverbatim.append(request.method()).append(" ").append(u)
.append(" ").append("\r\n");
.append(" ").append(httpProtocol).append("\r\n");

Headers headers = request.headers();

Expand All @@ -395,9 +408,14 @@ public Response intercept(Interceptor.Chain chain) throws IOException {

StringBuilder responseverbatim = new StringBuilder();

/*
* Note: the protocol version between request and response may
* differ, a server may respond with HTTP/1.0 on a HTTP/1.1 request
*/
httpProtocol = getNormalizedProtocolName(response.protocol());

responseverbatim
.append(response.protocol().toString()
.toUpperCase(Locale.ROOT)).append(" ")
.append(httpProtocol).append(" ")
.append(response.code()).append(" ")
.append(response.message()).append("\r\n");

Expand Down