本文整理汇总了Java中fi.iki.elonen.NanoHTTPD.IHTTPSession.getHeaders方法的典型用法代码示例。如果您正苦于以下问题:Java IHTTPSession.getHeaders方法的具体用法?Java IHTTPSession.getHeaders怎么用?Java IHTTPSession.getHeaders使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fi.iki.elonen.NanoHTTPD.IHTTPSession
的用法示例。
在下文中一共展示了IHTTPSession.getHeaders方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serve
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入方法依赖的package包/类
public Response serve(final IHTTPSession session) {
Map<String, String> headers = session.getHeaders();
if (isWebsocketRequested(session)) {
if (!HEADER_WEBSOCKET_VERSION_VALUE.equalsIgnoreCase(headers.get(HEADER_WEBSOCKET_VERSION))) {
return new Response(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT,
"Invalid Websocket-Version " + headers.get(HEADER_WEBSOCKET_VERSION));
}
if (!headers.containsKey(HEADER_WEBSOCKET_KEY)) {
return new Response(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT,
"Missing Websocket-Key");
}
WebSocket webSocket = webSocketFactory.openWebSocket(session);
Response handshakeResponse = webSocket.getHandshakeResponse();
try {
handshakeResponse.addHeader(HEADER_WEBSOCKET_ACCEPT, makeAcceptKey(headers.get(HEADER_WEBSOCKET_KEY)));
} catch (NoSuchAlgorithmException e) {
return new Response(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT,
"The SHA-1 Algorithm required for websockets is not available on the server.");
}
if (headers.containsKey(HEADER_WEBSOCKET_PROTOCOL)) {
handshakeResponse.addHeader(HEADER_WEBSOCKET_PROTOCOL, headers.get(HEADER_WEBSOCKET_PROTOCOL).split(",")[0]);
}
return handshakeResponse;
} else {
return null;
}
}
示例2: isWebsocketRequested
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入方法依赖的package包/类
protected boolean isWebsocketRequested(IHTTPSession session) {
Map<String, String> headers = session.getHeaders();
String upgrade = headers.get(HEADER_UPGRADE);
boolean isCorrectConnection = isWebSocketConnectionHeader(headers);
boolean isUpgrade = HEADER_UPGRADE_VALUE.equalsIgnoreCase(upgrade);
return (isUpgrade && isCorrectConnection);
}
示例3: serve
import fi.iki.elonen.NanoHTTPD.IHTTPSession; //导入方法依赖的package包/类
@Override
public Response serve(final IHTTPSession session) {
if (!checkHeaderSize(session)) {
// NanoHTTPDでは、バッファサイズを超えたHTTPヘッダーが送られてくると
// 挙動がおかしくなるのでここでエラーを返却して対応する。
Response response = newFixedLengthResponse(DConnectStatus.ENTITY_TOO_LARGE, MIME_APPLICATION_JSON,
"{\"result\" : 1, \"errorCode\" : 1, \"errorMessage\" : \"Request Entity Too Large.\"}");
response.closeConnection(true);
return response;
}
if (isWebsocketRequested(session)) {
Map<String, String> headers = session.getHeaders();
if (!NanoWSD.HEADER_WEBSOCKET_VERSION_VALUE.equalsIgnoreCase(headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION))) {
return newFixedLengthResponse(BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT,
"Invalid Websocket-Version " + headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION));
}
if (!headers.containsKey(NanoWSD.HEADER_WEBSOCKET_KEY)) {
return newFixedLengthResponse(BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Missing Websocket-Key");
}
// TODO: WebSocketの最大個数をチェックする
WebSocket webSocket = openWebSocket(session);
Response handshakeResponse = webSocket.getHandshakeResponse();
try {
handshakeResponse.addHeader(NanoWSD.HEADER_WEBSOCKET_ACCEPT, makeAcceptKey(headers.get(NanoWSD.HEADER_WEBSOCKET_KEY)));
} catch (NoSuchAlgorithmException e) {
return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT,
"The SHA-1 Algorithm required for websockets is not available on the server.");
}
if (headers.containsKey(NanoWSD.HEADER_WEBSOCKET_PROTOCOL)) {
handshakeResponse.addHeader(NanoWSD.HEADER_WEBSOCKET_PROTOCOL, headers.get(NanoWSD.HEADER_WEBSOCKET_PROTOCOL).split(",")[0]);
}
return handshakeResponse;
} else {
Response nanoRes = serveHttp(session);
addCORSHeaders(session.getHeaders(), nanoRes);
return nanoRes;
}
}