forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
okhttp: make okhttp dependencies compile only (grpc#8971)
* okhttp: forked required files to make okhttp dep compile only * okhttp: forked missing file to make okhttp dep compile only * okhttp: moved url and request files to proxy packge * okhttp: removed unused methods from forked files; fixed build
- Loading branch information
1 parent
5113e92
commit b20ce17
Showing
8 changed files
with
843 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/Credentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (C) 2014 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* | ||
* Forked from OkHttp 2.7.0 | ||
*/ | ||
package io.grpc.okhttp.internal; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import okio.ByteString; | ||
|
||
/** Factory for HTTP authorization credentials. */ | ||
public final class Credentials { | ||
private Credentials() { | ||
} | ||
|
||
/** Returns an auth credential for the Basic scheme. */ | ||
public static String basic(String userName, String password) { | ||
try { | ||
String usernameAndPassword = userName + ":" + password; | ||
byte[] bytes = usernameAndPassword.getBytes("ISO-8859-1"); | ||
String encoded = ByteString.of(bytes).base64(); | ||
return "Basic " + encoded; | ||
} catch (UnsupportedEncodingException e) { | ||
throw new AssertionError(); | ||
} | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/Headers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* | ||
* Forked from OkHttp 2.7.0 com.squareup.okhttp.Headers | ||
*/ | ||
package io.grpc.okhttp.internal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* The header fields of a single HTTP message. Values are uninterpreted strings; | ||
* | ||
* <p>This class trims whitespace from values. It never returns values with | ||
* leading or trailing whitespace. | ||
* | ||
* <p>Instances of this class are immutable. Use {@link Builder} to create | ||
* instances. | ||
*/ | ||
public final class Headers { | ||
private final String[] namesAndValues; | ||
|
||
private Headers(Builder builder) { | ||
this.namesAndValues = builder.namesAndValues.toArray(new String[builder.namesAndValues.size()]); | ||
} | ||
|
||
/** Returns the last value corresponding to the specified field, or null. */ | ||
public String get(String name) { | ||
return get(namesAndValues, name); | ||
} | ||
|
||
/** Returns the number of field values. */ | ||
public int size() { | ||
return namesAndValues.length / 2; | ||
} | ||
|
||
/** Returns the field at {@code position} or null if that is out of range. */ | ||
public String name(int index) { | ||
int nameIndex = index * 2; | ||
if (nameIndex < 0 || nameIndex >= namesAndValues.length) { | ||
return null; | ||
} | ||
return namesAndValues[nameIndex]; | ||
} | ||
|
||
/** Returns the value at {@code index} or null if that is out of range. */ | ||
public String value(int index) { | ||
int valueIndex = index * 2 + 1; | ||
if (valueIndex < 0 || valueIndex >= namesAndValues.length) { | ||
return null; | ||
} | ||
return namesAndValues[valueIndex]; | ||
} | ||
|
||
public Builder newBuilder() { | ||
Builder result = new Builder(); | ||
Collections.addAll(result.namesAndValues, namesAndValues); | ||
return result; | ||
} | ||
|
||
@Override public String toString() { | ||
StringBuilder result = new StringBuilder(); | ||
for (int i = 0, size = size(); i < size; i++) { | ||
result.append(name(i)).append(": ").append(value(i)).append("\n"); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
private static String get(String[] namesAndValues, String name) { | ||
for (int i = namesAndValues.length - 2; i >= 0; i -= 2) { | ||
if (name.equalsIgnoreCase(namesAndValues[i])) { | ||
return namesAndValues[i + 1]; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static final class Builder { | ||
private final List<String> namesAndValues = new ArrayList<>(20); | ||
|
||
/** | ||
* Add a field with the specified value without any validation. Only | ||
* appropriate for headers from the remote peer or cache. | ||
*/ | ||
Builder addLenient(String name, String value) { | ||
namesAndValues.add(name); | ||
namesAndValues.add(value.trim()); | ||
return this; | ||
} | ||
|
||
public Builder removeAll(String name) { | ||
for (int i = 0; i < namesAndValues.size(); i += 2) { | ||
if (name.equalsIgnoreCase(namesAndValues.get(i))) { | ||
namesAndValues.remove(i); // name | ||
namesAndValues.remove(i); // value | ||
i -= 2; | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Set a field with the specified value. If the field is not found, it is | ||
* added. If the field is found, the existing values are replaced. | ||
*/ | ||
public Builder set(String name, String value) { | ||
checkNameAndValue(name, value); | ||
removeAll(name); | ||
addLenient(name, value); | ||
return this; | ||
} | ||
|
||
private void checkNameAndValue(String name, String value) { | ||
if (name == null) throw new IllegalArgumentException("name == null"); | ||
if (name.isEmpty()) throw new IllegalArgumentException("name is empty"); | ||
for (int i = 0, length = name.length(); i < length; i++) { | ||
char c = name.charAt(i); | ||
if (c <= '\u001f' || c >= '\u007f') { | ||
throw new IllegalArgumentException(String.format( | ||
"Unexpected char %#04x at %d in header name: %s", (int) c, i, name)); | ||
} | ||
} | ||
if (value == null) throw new IllegalArgumentException("value == null"); | ||
for (int i = 0, length = value.length(); i < length; i++) { | ||
char c = value.charAt(i); | ||
if (c <= '\u001f' || c >= '\u007f') { | ||
throw new IllegalArgumentException(String.format( | ||
"Unexpected char %#04x at %d in header value: %s", (int) c, i, value)); | ||
} | ||
} | ||
} | ||
|
||
public Headers build() { | ||
return new Headers(this); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/StatusLine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Forked from OkHttp 2.7.0 | ||
*/ | ||
package io.grpc.okhttp.internal; | ||
|
||
import java.io.IOException; | ||
import java.net.ProtocolException; | ||
|
||
/** An HTTP response status line like "HTTP/1.1 200 OK". */ | ||
public final class StatusLine { | ||
/** Numeric status code, 307: Temporary Redirect. */ | ||
public static final int HTTP_TEMP_REDIRECT = 307; | ||
public static final int HTTP_PERM_REDIRECT = 308; | ||
public static final int HTTP_CONTINUE = 100; | ||
|
||
public final Protocol protocol; | ||
public final int code; | ||
public final String message; | ||
|
||
public StatusLine(Protocol protocol, int code, String message) { | ||
this.protocol = protocol; | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public static StatusLine parse(String statusLine) throws IOException { | ||
// H T T P / 1 . 1 2 0 0 T e m p o r a r y R e d i r e c t | ||
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 | ||
|
||
// Parse protocol like "HTTP/1.1" followed by a space. | ||
int codeStart; | ||
Protocol protocol; | ||
if (statusLine.startsWith("HTTP/1.")) { | ||
if (statusLine.length() < 9 || statusLine.charAt(8) != ' ') { | ||
throw new ProtocolException("Unexpected status line: " + statusLine); | ||
} | ||
int httpMinorVersion = statusLine.charAt(7) - '0'; | ||
codeStart = 9; | ||
if (httpMinorVersion == 0) { | ||
protocol = Protocol.HTTP_1_0; | ||
} else if (httpMinorVersion == 1) { | ||
protocol = Protocol.HTTP_1_1; | ||
} else { | ||
throw new ProtocolException("Unexpected status line: " + statusLine); | ||
} | ||
} else if (statusLine.startsWith("ICY ")) { | ||
// Shoutcast uses ICY instead of "HTTP/1.0". | ||
protocol = Protocol.HTTP_1_0; | ||
codeStart = 4; | ||
} else { | ||
throw new ProtocolException("Unexpected status line: " + statusLine); | ||
} | ||
|
||
// Parse response code like "200". Always 3 digits. | ||
if (statusLine.length() < codeStart + 3) { | ||
throw new ProtocolException("Unexpected status line: " + statusLine); | ||
} | ||
int code; | ||
try { | ||
code = Integer.parseInt(statusLine.substring(codeStart, codeStart + 3)); | ||
} catch (NumberFormatException e) { | ||
throw new ProtocolException("Unexpected status line: " + statusLine); | ||
} | ||
|
||
// Parse an optional response message like "OK" or "Not Modified". If it | ||
// exists, it is separated from the response code by a space. | ||
String message = ""; | ||
if (statusLine.length() > codeStart + 3) { | ||
if (statusLine.charAt(codeStart + 3) != ' ') { | ||
throw new ProtocolException("Unexpected status line: " + statusLine); | ||
} | ||
message = statusLine.substring(codeStart + 4); | ||
} | ||
|
||
return new StatusLine(protocol, code, message); | ||
} | ||
|
||
@Override public String toString() { | ||
StringBuilder result = new StringBuilder(); | ||
result.append(protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1"); | ||
result.append(' ').append(code); | ||
if (message != null) { | ||
result.append(' ').append(message); | ||
} | ||
return result.toString(); | ||
} | ||
} |
Oops, something went wrong.