change http header types
This commit is contained in:
@@ -11,10 +11,7 @@ import java.io.*;
|
|||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class PollingXHR extends Polling {
|
public class PollingXHR extends Polling {
|
||||||
@@ -183,16 +180,18 @@ public class PollingXHR extends Polling {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
|
||||||
|
|
||||||
if ("POST".equals(this.method)) {
|
if ("POST".equals(this.method)) {
|
||||||
xhr.setDoOutput(true);
|
xhr.setDoOutput(true);
|
||||||
headers.put("Content-type", "application/octet-stream");
|
headers.put("Content-type", new LinkedList<String>(Arrays.asList("application/octet-stream")));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.onRequestHeaders(headers);
|
self.onRequestHeaders(headers);
|
||||||
for (Map.Entry<String, String> header : headers.entrySet()) {
|
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
|
||||||
xhr.setRequestProperty(header.getKey(), header.getValue());
|
for (String v : header.getValue()){
|
||||||
|
xhr.addRequestProperty(header.getKey(), v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.fine(String.format("sending xhr with url %s | data %s", this.uri, this.data));
|
logger.fine(String.format("sending xhr with url %s | data %s", this.uri, this.data));
|
||||||
@@ -208,16 +207,7 @@ public class PollingXHR extends Polling {
|
|||||||
output.flush();
|
output.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
Map<String, List<String>> headers = xhr.getHeaderFields();
|
||||||
|
|
||||||
Map<String, List<String>> xhrHeaderFields = xhr.getHeaderFields();
|
|
||||||
if(xhrHeaderFields != null) {
|
|
||||||
for (String key : xhrHeaderFields.keySet()) {
|
|
||||||
if (key == null) continue;
|
|
||||||
headers.put(key, xhr.getHeaderField(key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.onResponseHeaders(headers);
|
self.onResponseHeaders(headers);
|
||||||
|
|
||||||
final int statusCode = xhr.getResponseCode();
|
final int statusCode = xhr.getResponseCode();
|
||||||
@@ -257,11 +247,11 @@ public class PollingXHR extends Polling {
|
|||||||
this.cleanup();
|
this.cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onRequestHeaders(Map<String, String> headers) {
|
private void onRequestHeaders(Map<String, List<String>> headers) {
|
||||||
this.emit(EVENT_REQUEST_HEADERS, headers);
|
this.emit(EVENT_REQUEST_HEADERS, headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onResponseHeaders(Map<String, String> headers) {
|
private void onResponseHeaders(Map<String, List<String>> headers) {
|
||||||
this.emit(EVENT_RESPONSE_HEADERS, headers);
|
this.emit(EVENT_RESPONSE_HEADERS, headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import com.github.nkzawa.engineio.parser.Packet;
|
|||||||
import com.github.nkzawa.engineio.parser.Parser;
|
import com.github.nkzawa.engineio.parser.Parser;
|
||||||
import com.github.nkzawa.parseqs.ParseQS;
|
import com.github.nkzawa.parseqs.ParseQS;
|
||||||
import com.github.nkzawa.thread.EventThread;
|
import com.github.nkzawa.thread.EventThread;
|
||||||
import com.squareup.okhttp.Headers;
|
|
||||||
import com.squareup.okhttp.OkHttpClient;
|
import com.squareup.okhttp.OkHttpClient;
|
||||||
import com.squareup.okhttp.Request;
|
import com.squareup.okhttp.Request;
|
||||||
import com.squareup.okhttp.Response;
|
import com.squareup.okhttp.Response;
|
||||||
@@ -18,10 +17,7 @@ import okio.BufferedSource;
|
|||||||
|
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Date;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static com.squareup.okhttp.ws.WebSocket.PayloadType.BINARY;
|
import static com.squareup.okhttp.ws.WebSocket.PayloadType.BINARY;
|
||||||
@@ -42,7 +38,7 @@ public class WebSocket extends Transport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void doOpen() {
|
protected void doOpen() {
|
||||||
Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
|
||||||
this.emit(EVENT_REQUEST_HEADERS, headers);
|
this.emit(EVENT_REQUEST_HEADERS, headers);
|
||||||
|
|
||||||
final WebSocket self = this;
|
final WebSocket self = this;
|
||||||
@@ -55,19 +51,17 @@ public class WebSocket extends Transport {
|
|||||||
client.setHostnameVerifier(this.hostnameVerifier);
|
client.setHostnameVerifier(this.hostnameVerifier);
|
||||||
}
|
}
|
||||||
Request.Builder builder = new Request.Builder().url(uri());
|
Request.Builder builder = new Request.Builder().url(uri());
|
||||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
|
||||||
builder.addHeader(entry.getKey(), entry.getValue());
|
for (String v : entry.getValue()) {
|
||||||
|
builder.addHeader(entry.getKey(), v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
final Request request = builder.build();
|
final Request request = builder.build();
|
||||||
(wsCall = WebSocketCall.create(client, request)).enqueue(new WebSocketListener() {
|
(wsCall = WebSocketCall.create(client, request)).enqueue(new WebSocketListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(com.squareup.okhttp.ws.WebSocket webSocket, Response response) {
|
public void onOpen(com.squareup.okhttp.ws.WebSocket webSocket, Response response) {
|
||||||
ws = webSocket;
|
ws = webSocket;
|
||||||
final Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
final Map<String, List<String>> headers = response.headers().toMultimap();
|
||||||
Headers responseHeaders = response.headers();
|
|
||||||
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
|
|
||||||
headers.put(responseHeaders.name(i), responseHeaders.value(i));
|
|
||||||
}
|
|
||||||
EventThread.exec(new Runnable() {
|
EventThread.exec(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import org.junit.runner.RunWith;
|
|||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
@@ -61,7 +63,7 @@ public class ServerConnectionTest extends Connection {
|
|||||||
}).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
|
}).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void call(Object... args) {
|
public void call(Object... args) {
|
||||||
events.offer((String)args[0]);
|
events.offer((String) args[0]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
socket.open();
|
socket.open();
|
||||||
@@ -89,7 +91,7 @@ public class ServerConnectionTest extends Connection {
|
|||||||
HandshakeData data = (HandshakeData)args[0];
|
HandshakeData data = (HandshakeData)args[0];
|
||||||
assertThat(data.sid, is(notNullValue()));
|
assertThat(data.sid, is(notNullValue()));
|
||||||
assertThat(data.upgrades, is(not(emptyArray())));
|
assertThat(data.upgrades, is(not(emptyArray())));
|
||||||
assertThat(data.pingTimeout, is(greaterThan((long)0)));
|
assertThat(data.pingTimeout, is(greaterThan((long) 0)));
|
||||||
assertThat(data.pingInterval, is(greaterThan((long) 0)));
|
assertThat(data.pingInterval, is(greaterThan((long) 0)));
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
@@ -144,15 +146,17 @@ public class ServerConnectionTest extends Connection {
|
|||||||
@Override
|
@Override
|
||||||
public void call(Object... args) {
|
public void call(Object... args) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, String> headers = (Map<String, String>)args[0];
|
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
|
||||||
headers.put("X-EngineIO", "foo");
|
headers.put("X-EngineIO", Arrays.asList("foo"));
|
||||||
}
|
}
|
||||||
}).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
|
}).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void call(Object... args) {
|
public void call(Object... args) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, String> headers = (Map<String, String>)args[0];
|
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
|
||||||
messages.offer(headers.get("X-EngineIO"));
|
List<String> values = headers.get("X-EngineIO");
|
||||||
|
messages.offer(values.get(0));
|
||||||
|
messages.offer(values.get(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -160,7 +164,7 @@ public class ServerConnectionTest extends Connection {
|
|||||||
socket.open();
|
socket.open();
|
||||||
|
|
||||||
assertThat(messages.take(), is("foo"));
|
assertThat(messages.take(), is("foo"));
|
||||||
assertThat(messages.take(), is("foo"));
|
assertThat(messages.take(), is("hi"));
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,21 +184,24 @@ public class ServerConnectionTest extends Connection {
|
|||||||
@Override
|
@Override
|
||||||
public void call(Object... args) {
|
public void call(Object... args) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, String> headers = (Map<String, String>)args[0];
|
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
|
||||||
headers.put("X-EngineIO", "foo");
|
headers.put("X-EngineIO", Arrays.asList("foo"));
|
||||||
}
|
}
|
||||||
}).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
|
}).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void call(Object... args) {
|
public void call(Object... args) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, String> headers = (Map<String, String>)args[0];
|
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
|
||||||
messages.offer(headers.get("X-EngineIO"));
|
List<String> values = headers.get("X-EngineIO");
|
||||||
|
messages.offer(values.get(0));
|
||||||
|
messages.offer(values.get(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
socket.open();
|
socket.open();
|
||||||
|
|
||||||
|
assertThat(messages.take(), is("hi"));
|
||||||
assertThat(messages.take(), is("foo"));
|
assertThat(messages.take(), is("foo"));
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
@@ -265,7 +272,7 @@ public class ServerConnectionTest extends Connection {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assertThat((String)values.take(), is(Polling.NAME));
|
assertThat((String) values.take(), is(Polling.NAME));
|
||||||
assertThat((String)values.take(), is(not(WebSocket.NAME)));
|
assertThat((String)values.take(), is(not(WebSocket.NAME)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ before(server, 'handleRequest', function(req, res) {
|
|||||||
// echo a header value
|
// echo a header value
|
||||||
var value = req.headers['x-engineio'];
|
var value = req.headers['x-engineio'];
|
||||||
if (!value) return;
|
if (!value) return;
|
||||||
res.setHeader('X-EngineIO', value);
|
res.setHeader('X-EngineIO', ['hi', value]);
|
||||||
});
|
});
|
||||||
|
|
||||||
before(server, 'handleUpgrade', function(req, socket, head) {
|
before(server, 'handleUpgrade', function(req, socket, head) {
|
||||||
@@ -53,6 +53,7 @@ before(server, 'handleUpgrade', function(req, socket, head) {
|
|||||||
var value = req.headers['x-engineio'];
|
var value = req.headers['x-engineio'];
|
||||||
if (!value) return;
|
if (!value) return;
|
||||||
this.ws.once('headers', function(headers) {
|
this.ws.once('headers', function(headers) {
|
||||||
|
headers.push('X-EngineIO: hi');
|
||||||
headers.push('X-EngineIO: ' + value);
|
headers.push('X-EngineIO: ' + value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user