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 a61567e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 45 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
package com.microsoft.applications.events.maesdktest;

import com.microsoft.applications.events.HttpClientRequest;

import org.junit.Assert;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.*;

public class HttpClientRequestTest {
@Test
public void testHeaders() {
byte[] bytes = "key1value1key2value2".getBytes(UTF_8);
int[] header_lengths = new int[] { 4, 6, 4, 6 };

HttpClientRequest.Headers headers = new HttpClientRequest.Headers(header_lengths, bytes);
ArrayList<String> actual = new ArrayList<>();
while (headers.hasNext()) {
HttpClientRequest.HeaderEntry entry = headers.next();
actual.add(entry.key);
actual.add(entry.value);
}

Assert.assertEquals(4, headers.length);
Assert.assertArrayEquals(
new String[] {"key1", "value1", "key2", "value2"},
actual.toArray()
);
}
}
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 @@ -97,7 +97,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 +108,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 +186,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 +379,7 @@ public FutureTask<Boolean> createTask(
int[] header_index,
byte[] header_buffer) {
try {
Map<String, String> headers = HttpClient.convertHeaders(header_index, header_buffer);
HttpClientRequest.Headers headers = new HttpClientRequest.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,9 @@
import androidx.annotation.NonNull;

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

import static java.nio.charset.StandardCharsets.UTF_8;

public interface HttpClientRequest extends Runnable {

Expand All @@ -18,14 +20,53 @@ 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);
}
}
}

final class HeaderEntry {
public final String key;
public final String value;

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

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

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

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

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

0 comments on commit a61567e

Please sign in to comment.