Skip to content

Commit

Permalink
Prefix query params representing protocol header with protocol name
Browse files Browse the repository at this point in the history
  • Loading branch information
flowersinthesand committed May 4, 2017
1 parent e7b4642 commit 5838280
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
15 changes: 8 additions & 7 deletions server/src/main/java/io/cettia/DefaultServer.java
@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,6 +60,7 @@
*
* @author Donghwan Kim
*/
// TODO Extract constants
public class DefaultServer implements Server {

private Map<String, DefaultServerSocket> sockets = new ConcurrentHashMap<>();
Expand All @@ -71,10 +72,10 @@ public class DefaultServer implements Server {
public void on(ServerTransport transport) {
DefaultServerSocket socket = null;
Map<String, String> headers = HttpTransportServer.parseQuery(transport.uri());
String sid = headers.get("sid");
String socketId = headers.get("cettia-id");
// ConcurrentHashMap is not null-safe
if (sid != null) {
socket = sockets.get(sid);
if (socketId != null) {
socket = sockets.get(socketId);
}
if (socket == null) {
socket = createSocket(transport);
Expand Down Expand Up @@ -385,9 +386,9 @@ public void on() {
});

Map<String, String> headers = new LinkedHashMap<>();
headers.put("sid", id);
headers.put("heartbeat", options.get("heartbeat"));
headers.put("_heartbeat", options.get("_heartbeat"));
headers.put("cettia-id", id);
headers.put("cettia-heartbeat", options.get("heartbeat"));
headers.put("cettia-_heartbeat", options.get("_heartbeat"));
transport.send("?" + HttpTransportServer.formatQuery(headers));
actionsMap.get("open").fire();
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +57,7 @@
*
* @author Donghwan Kim
*/
// TODO Extract constants
public class HttpTransportServer implements TransportServer<ServerHttpExchange> {

private final Logger log = LoggerFactory.getLogger(HttpTransportServer.class);
Expand Down Expand Up @@ -137,9 +138,9 @@ public void on(final ServerHttpExchange http) {
break;
}
case GET: {
switch (params.get("when")) {
switch (params.get("cettia-transport-when")) {
case "open": {
String transportName = params.get("transport");
String transportName = params.get("cettia-transport-name");
switch (transportName) {
case "stream":
transportActions.fire(new StreamTransport(http));
Expand All @@ -155,7 +156,7 @@ public void on(final ServerHttpExchange http) {
break;
}
case "poll": {
String id = params.get("id");
String id = params.get("cettia-transport-id");
BaseTransport transport = transports.get(id);
if (transport != null && transport instanceof LongpollTransport) {
((LongpollTransport) transport).refresh(http);
Expand All @@ -166,7 +167,7 @@ public void on(final ServerHttpExchange http) {
break;
}
case "abort": {
String id = params.get("id");
String id = params.get("cettia-transport-id");
BaseTransport transport = transports.get(id);
if (transport != null) {
transport.close();
Expand All @@ -175,14 +176,14 @@ public void on(final ServerHttpExchange http) {
break;
}
default:
log.error("when, {}, is not supported", params.get("when"));
log.error("when, {}, is not supported", params.get("cettia-transport-when"));
http.setStatus(HttpStatus.NOT_IMPLEMENTED).end();
break;
}
break;
}
case POST: {
final String id = params.get("id");
final String id = params.get("cettia-transport-id");
switch (http.header("content-type") == null ? "" : http.header("content-type")
.toLowerCase()) {
case "text/plain; charset=utf-8":
Expand Down Expand Up @@ -310,7 +311,7 @@ private static class StreamTransport extends BaseTransport {
public StreamTransport(ServerHttpExchange http) {
super(http);
Map<String, String> query = new LinkedHashMap<>();
query.put("id", id);
query.put("cettia-transport-id", id);
http.onfinish(new VoidAction() {
@Override
public void on() {
Expand All @@ -329,7 +330,7 @@ public void on() {
closeActions.fire();
}
})
.setHeader("content-type", "text/" + ("true".equals(params.get("sse")) ? "event-stream" :
.setHeader("content-type", "text/" + ("true".equals(params.get("cettia-transport-sse")) ? "event-stream" :
"plain") + "; charset=utf-8")
.write(TEXT_2KB + "\ndata: ?" + formatQuery(query) + "\n\n");
}
Expand Down Expand Up @@ -384,7 +385,7 @@ public void refresh(ServerHttpExchange http) {
http.onfinish(new VoidAction() {
@Override
public void on() {
if (parameters.get("when").equals("poll") && !endedWithMessage.get()) {
if (parameters.get("cettia-transport-when").equals("poll") && !endedWithMessage.get()) {
closeActions.fire();
} else {
Timer timer = new Timer(true);
Expand All @@ -410,11 +411,11 @@ public void on() {
closeActions.fire();
}
});
String when = parameters.get("when");
String when = parameters.get("cettia-transport-when");
switch (when) {
case "open":
Map<String, String> query = new LinkedHashMap<>();
query.put("id", id);
query.put("cettia-transport-id", id);
endWithMessage(http, "?" + formatQuery(query));
break;
case "poll":
Expand Down Expand Up @@ -460,10 +461,10 @@ protected void doSend(String data) {
// Regard it as http.endWithMessage
private void endWithMessage(ServerHttpExchange http, String data) {
endedWithMessage.set(true);
boolean jsonp = "true".equals(params.get("jsonp"));
boolean jsonp = "true".equals(params.get("cettia-transport-jsonp"));
if (jsonp) {
try {
data = params.get("callback") + "(" + mapper.writeValueAsString(data) + ");";
data = params.get("cettia-transport-callback") + "(" + mapper.writeValueAsString(data) + ");";
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
8 changes: 4 additions & 4 deletions server/src/test/resources/package.json
@@ -1,9 +1,9 @@
{
"private": "true",
"devDependencies": {
"cettia-protocol": "1.0.0-Beta1",
"mocha": "2.4.5",
"chai": "3.5.0",
"minimist": "1.2.0"
"cettia-protocol": "cettia/cettia-protocol#f2a9df19c92541215c8ee7f19d2939181ba99b22",
"mocha": "^3.2.0",
"chai": "^3.5.0",
"minimist": "^1.2.0"
}
}

0 comments on commit 5838280

Please sign in to comment.