public boolean hasGetVersionRequestHeader() { boolean result = false; String headerValue = this.request.getHeader(RestMessageHeaders.X_VOLD_GET_VERSION); if(headerValue != null) { result = true; } return result; }
/** * Retrieve and validate the zone id value from the REST request. * "X-VOLD-Zone-Id" is the zone id header. * * @return valid zone id or -1 if there is no/invalid zone id */ protected int parseZoneId() { int result = -1; String zoneIdStr = this.request.getHeader(RestMessageHeaders.X_VOLD_ZONE_ID); if(zoneIdStr != null) { try { int zoneId = Integer.parseInt(zoneIdStr); if(zoneId < 0) { logger.error("ZoneId cannot be negative. Assuming the default zone id."); } else { result = zoneId; } } catch(NumberFormatException nfe) { logger.error("Exception when validating request. Incorrect zone id parameter. Cannot parse this to int: " + zoneIdStr, nfe); } } return result; }
/** * Retrieves and validates the content type from the REST requests * * @return true if has content type. */ protected boolean hasContentType() { boolean result = false; if(this.request.getHeader(RestMessageHeaders.CONTENT_TYPE) != null) { result = true; } else { logger.error("Error when validating put request. Missing Content-Type header."); RestErrorHandler.writeErrorResponse(this.messageEvent, HttpResponseStatus.BAD_REQUEST, "Missing Content-Type header"); } return result; }
/** * Retrieves and validates the content length from the REST request. * * @return true if has content length */ protected boolean hasContentLength() { boolean result = false; String contentLength = this.request.getHeader(RestMessageHeaders.CONTENT_LENGTH); if(contentLength != null) { try { Long.parseLong(contentLength); result = true; } catch(NumberFormatException nfe) { logger.error("Exception when validating put request. Incorrect content length parameter. Cannot parse this to long: " + contentLength + ". Details: " + nfe.getMessage(), nfe); RestErrorHandler.writeErrorResponse(this.messageEvent, HttpResponseStatus.BAD_REQUEST, "Incorrect content length parameter. Cannot parse this to long: " + contentLength + ". Details: " + nfe.getMessage()); } } else { logger.error("Error when validating put request. Missing Content-Length header."); RestErrorHandler.writeErrorResponse(this.messageEvent, HttpResponseStatus.BAD_REQUEST, "Missing Content-Length header"); } return result; }
String rtCode = this.request.getHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE); if(rtCode != null) { try {
String timeoutValStr = this.request.getHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS); if(timeoutValStr != null) { try {
String originTime = request.getHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS); boolean result = false; if(originTime != null) {
boolean result = false; String vectorClockHeader = this.request.getHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK); if(vectorClockHeader != null) { ObjectMapper mapper = new ObjectMapper();
@Override public String header(String name) { return httpRequest.getHeader(name); }
private String getWebSocketProtocol(HttpRequest req) { if(req.getHeader(HttpHeaders.Names.ORIGIN).matches("(?s)https://.*")) { return "wss://"; } else { return "ws://"; } } }
private String getWebSocketProtocol(HttpRequest req) { if(req.getHeader(HttpHeaders.Names.ORIGIN).matches("(?s)https://.*")) { return "wss://"; } else { return "ws://"; } } }
private Integer getHybiVersion() { return req.containsHeader(SEC_WEBSOCKET_VERSION) ? Integer.parseInt(req.getHeader(SEC_WEBSOCKET_VERSION).trim()) : null; }
private String getWebSocketLocation(HttpRequest req) { return getWebSocketProtocol(req) + req.getHeader(HttpHeaders.Names.HOST) + req.getUri(); }
private String getWebSocketLocation(HttpRequest req) { return getWebSocketProtocol(req) + req.getHeader(HttpHeaders.Names.HOST) + req.getUri(); }
@Override public void prepareHandshakeResponse(NettyWebSocketConnection webSocketConnection) { webSocketConnection.setVersion("HIXIE-76"); res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake")); res.addHeader(UPGRADE, WEBSOCKET); res.addHeader(CONNECTION, UPGRADE); res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req)); String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL); if (protocol != null) { res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol); } // Calculate the answer of the challenge. String key1 = req.getHeader(SEC_WEBSOCKET_KEY1); String key2 = req.getHeader(SEC_WEBSOCKET_KEY2); int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length()); int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length()); long c = req.getContent().readLong(); ChannelBuffer input = ChannelBuffers.buffer(16); input.writeInt(a); input.writeInt(b); input.writeLong(c); ChannelBuffer output = ChannelBuffers.wrappedBuffer(MD5.digest(input.array())); res.setContent(output); }
@Override public void prepareHandshakeResponse(NettyWebSocketConnection webSocketConnection) { webSocketConnection.setVersion("HIXIE-75"); res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake")); res.addHeader(UPGRADE, WEBSOCKET); res.addHeader(CONNECTION, HttpHeaders.Values.UPGRADE); String origin = req.getHeader(ORIGIN); if (origin != null) { res.addHeader(WEBSOCKET_ORIGIN, origin); } res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req)); String protocol = req.getHeader(WEBSOCKET_PROTOCOL); if (protocol != null) { res.addHeader(WEBSOCKET_PROTOCOL, protocol); } }
@Override public void prepareHandshakeResponse(NettyWebSocketConnection webSocketConnection) { webSocketConnection.setHybiWebSocketVersion(getHybiVersion()); if (getHybiVersion() < MIN_HYBI_VERSION) { res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED); res.setHeader(SEC_WEBSOCKET_VERSION, String.valueOf(MIN_HYBI_VERSION)); return; } String key = req.getHeader(SEC_WEBSOCKET_KEY); if (key == null) { res.setStatus(HttpResponseStatus.BAD_REQUEST); return; } String accept = Base64.encode(sha1(key + ACCEPT_GUID)); res.setStatus(new HttpResponseStatus(101, "Switching Protocols")); res.addHeader(UPGRADE, WEBSOCKET.toLowerCase()); res.addHeader(CONNECTION, UPGRADE); res.addHeader(SEC_WEBSOCKET_ACCEPT, accept); String webSocketProtocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL); if (webSocketProtocol != null) { res.addHeader(SEC_WEBSOCKET_PROTOCOL, webSocketProtocol); } }
public static boolean matches(HttpRequest request) { String sVers = request.getHeader("Sec-WebSocket-Version"); if (sVers != null) { Integer ver = Integer.parseInt(sVers); return request.containsHeader("Sec-WebSocket-Key") && ver == 13; } else { return false; } }
public static boolean matches(HttpRequest request) { String sVers = request.getHeader("Sec-WebSocket-Version"); if (sVers != null) { Integer ver = Integer.parseInt(sVers); return request.containsHeader("Sec-WebSocket-Key") && ver >= 7; } else { return false; } }
private boolean hasBadProtocolHeader(HttpRequest request) { String protocol = request.getHeader(HttpHeaders.Names.SEC_WEBSOCKET_PROTOCOL); if (protocol == null) { // no protocol sent by client, by default, first protocol listed in configuration will be used return false; } if (protocol.isEmpty()) { // empty value of websocket protocol header, violation of hixie and hybi protocols return true; } return !engine.getConfiguration().getDataControllers().hasController(protocol); }