Skip to content

Commit

Permalink
Remove Map of headers
Browse files Browse the repository at this point in the history
  • Loading branch information
anod committed Feb 1, 2023
1 parent b07bfd0 commit cccdfe2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -89,6 +89,46 @@ public final void onCapabilitiesChanged(
private boolean m_metered;
}


class HeaderEntry {
final String key;
final String value;

public HeaderEntry(String key, String value) {
this.key = key;
this.value = value;
}
}

class Headers implements Iterator<HeaderEntry> {
final int length;
private final int[] header_length;
private final byte[] buffer;
private int current = 0;
private int offset = 0;

public Headers(int[] header_length, byte[] buffer) {
this.length = header_length.length;
this.header_length = header_length;
this.buffer = buffer;
}

@Override
public boolean hasNext() {
return current + 1 < length;
}

@Override
public HeaderEntry next() {
current += 2;
String k = new String(buffer, offset, header_length[current], UTF_8);
offset += header_length[current];
String v = new String(buffer, offset, header_length[current + 1], UTF_8);
offset += header_length[current + 1];
return new HeaderEntry(k,v);
}
}

class Request implements HttpClientRequest {

Request(
Expand All @@ -97,7 +137,7 @@ class Request implements HttpClientRequest {
String method,
byte[] body,
String request_id,
@NonNull Map<String, String> headers)
@NonNull Headers headers)
throws java.io.IOException {
m_parent = parent;
m_connection = (HttpURLConnection) parent.newUrl(url).openConnection();
Expand All @@ -108,8 +148,9 @@ class Request implements HttpClientRequest {
m_connection.setDoOutput(true);
}
m_request_id = request_id;
for (Map.Entry<String, String> header : headers.entrySet()) {
m_connection.setRequestProperty(header.getKey(), header.getValue());
while (headers.hasNext()) {
HeaderEntry header = headers.next();
m_connection.setRequestProperty(header.key, header.value);
}
}

Expand Down Expand Up @@ -185,22 +226,6 @@ public class HttpClient {
private static final String ANDROID_DEVICE_CLASS_PC = "Android.PC";
private static final String ANDROID_DEVICE_CLASS_PHONE = "Android.Phone";

static Map<String, String> convertHeaders(
int[] header_length,
byte[] header_buffer
) {
int offset = 0;
Map<String, String> result = new HashMap<>(header_length.length);
for (int i = 0; i + 1 < header_length.length; i += 2) {
String k = new String(header_buffer, offset, header_length[i], UTF_8);
offset += header_length[i];
String v = new String(header_buffer, offset, header_length[i + 1], UTF_8);
offset += header_length[i + 1];
result.put(k, v);
}
return result;
}

/** Shim FutureTask: we would like to @Keep the cancel method for JNI */
static class FutureShim extends FutureTask<Boolean> {
FutureShim(HttpClientRequest inner) {
Expand Down Expand Up @@ -394,7 +419,7 @@ public FutureTask<Boolean> createTask(
int[] header_index,
byte[] header_buffer) {
try {
Map<String, String> headers = HttpClient.convertHeaders(header_index, header_buffer);
Headers headers = new Headers(header_index, header_buffer);
HttpClientRequest r = m_requestFactory.create(this, url, method, body, request_id, headers);
return new FutureShim(r);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.Map;

public interface HttpClientRequest extends Runnable {

Expand All @@ -18,12 +17,12 @@ HttpClientRequest create(
String method,
byte[] body,
String request_id,
@NonNull Map<String, String> headers
@NonNull Headers headers
) throws IOException;

class AndroidUrlConnection implements HttpClientRequest.Factory {
@Override
public HttpClientRequest create(@NonNull HttpClient parent, String url, String method, byte[] body, String request_id, @NonNull Map<String, String> headers) throws IOException {
public HttpClientRequest create(@NonNull HttpClient parent, String url, String method, byte[] body, String request_id, @NonNull Headers headers) throws IOException {
return new Request(parent, url, method, body, request_id, headers);
}
}
Expand Down

0 comments on commit cccdfe2

Please sign in to comment.