@Override public OverviewList overview(long start, long end) { NntpFuture<OverviewResponse> future = this.sendCommand( Response.ResponseType.XZVER, Long.toString(start) + "-" + Long.toString(end)); return Futures.getUnchecked(future).list; }
static { long ping = -1L; try { ping = Long.parseLong(System.getProperty(GlobalOptions.PING_INTERVAL_MS_OPTION, "-1")); } catch (NumberFormatException ignored) { } PING_INTERVAL = ping; int builds = 1; try { builds = Math.min( Integer.parseInt( System.getProperty(GlobalOptions.MAX_SIMULTANEOUS_BUILDS_OPTION, "1")), Runtime.getRuntime().availableProcessors()); } catch (NumberFormatException ignored) { } MAX_SIMULTANEOUS_BUILD_SESSIONS = builds; }
void saveExceededSizeError(HttpRequest nettyRequest, Request request, Response response) { String warning = nettyRequest.getHeader(HttpHeaders.Names.WARNING); String length = nettyRequest.getHeader(HttpHeaders.Names.CONTENT_LENGTH); if (warning != null) { Logger.trace("saveExceededSizeError: begin"); try { StringBuilder error = new StringBuilder(); error.append("\u0000"); // Cannot put warning which is play.netty.content.length.exceeded // as Key as it will result error when printing error error.append("play.netty.maxContentLength"); error.append(":"); String size = null; try { size = JavaExtensions.formatSize(Long.parseLong(length)); } catch (Exception e) { size = length + " bytes"; } error.append(Messages.get(warning, size)); error.append("\u0001"); error.append(size); error.append("\u0000"); if (request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS") != null && request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS").value != null) { error.append(request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS").value); } String errorData = URLEncoder.encode(error.toString(), "utf-8"); Http.Cookie c = new Http.Cookie(); c.value = errorData; c.name = Scope.COOKIE_PREFIX + "_ERRORS"; request.cookies.put(Scope.COOKIE_PREFIX + "_ERRORS", c); Logger.trace("saveExceededSizeError: end"); } catch (Exception e) { throw new UnexpectedException("Error serialization problem", e); } } }
static String getApplicationStartTime() { return Long.toString(ApplicationManager.getApplication().getStartTime()); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { RequestV2 request = null; RendererConfiguration renderer = null; String userAgentString = null; StringBuilder unknownHeaders = new StringBuilder(); String separator = ""; boolean isWindowsMediaPlayer = false; HttpRequest nettyRequest = this.nettyRequest = (HttpRequest) e.getMessage(); InetSocketAddress remoteAddress = (InetSocketAddress) e.getChannel().getRemoteAddress(); InetAddress ia = remoteAddress.getAddress(); // Apply the IP filter if (filterIp(ia)) { e.getChannel().close(); LOGGER.trace("Access denied for address " + ia + " based on IP filter"); return; } LOGGER.trace("Opened request handler on socket " + remoteAddress); PMS.get().getRegistry().disableGoToSleep(); if (HttpMethod.GET.equals(nettyRequest.getMethod())) { request = new RequestV2("GET", nettyRequest.getUri().substring(1)); } else if (HttpMethod.POST.equals(nettyRequest.getMethod())) { request = new RequestV2("POST", nettyRequest.getUri().substring(1)); } else if (HttpMethod.HEAD.equals(nettyRequest.getMethod())) { request = new RequestV2("HEAD", nettyRequest.getUri().substring(1)); } else { request = new RequestV2(nettyRequest.getMethod().getName(), nettyRequest.getUri().substring(1)); } LOGGER.trace( "Request: " + nettyRequest.getProtocolVersion().getText() + " : " + request.getMethod() + " : " + request.getArgument()); if (nettyRequest.getProtocolVersion().getMinorVersion() == 0) { request.setHttp10(true); } // The handler makes a couple of attempts to recognize a renderer from its requests. // IP address matches from previous requests are preferred, when that fails request // header matches are attempted and if those fail as well we're stuck with the // default renderer. // Attempt 1: try to recognize the renderer by its socket address from previous requests renderer = RendererConfiguration.getRendererConfigurationBySocketAddress(ia); if (renderer != null) { if (!"WMP".equals(renderer.getRendererName())) { PMS.get().setRendererfound(renderer); request.setMediaRenderer(renderer); LOGGER.trace( "Matched media renderer \"" + renderer.getRendererName() + "\" based on address " + ia); } else { LOGGER.trace("Detected and blocked Windows Media Player"); isWindowsMediaPlayer = true; } } for (String name : nettyRequest.getHeaderNames()) { String headerLine = name + ": " + nettyRequest.getHeader(name); LOGGER.trace("Received on socket: " + headerLine); if (renderer == null && headerLine != null && headerLine.toUpperCase().startsWith("USER-AGENT")) { userAgentString = headerLine.substring(headerLine.indexOf(":") + 1).trim(); // Attempt 2: try to recognize the renderer by matching the "User-Agent" header renderer = RendererConfiguration.getRendererConfigurationByUA(userAgentString); if (renderer != null) { if (!"WMP".equals(renderer.getRendererName())) { request.setMediaRenderer(renderer); renderer.associateIP(ia); // Associate IP address for later requests PMS.get().setRendererfound(renderer); LOGGER.trace( "Matched media renderer \"" + renderer.getRendererName() + "\" based on header \"" + headerLine + "\""); } else if (!isWindowsMediaPlayer) { LOGGER.trace("Detected and blocked Windows Media Player"); isWindowsMediaPlayer = true; } } } if (renderer == null && headerLine != null) { // Attempt 3: try to recognize the renderer by matching an additional header renderer = RendererConfiguration.getRendererConfigurationByUAAHH(headerLine); if (renderer != null) { request.setMediaRenderer(renderer); renderer.associateIP(ia); // Associate IP address for later requests PMS.get().setRendererfound(renderer); LOGGER.trace( "Matched media renderer \"" + renderer.getRendererName() + "\" based on header \"" + headerLine + "\""); } } try { StringTokenizer s = new StringTokenizer(headerLine); String temp = s.nextToken(); if (temp.toUpperCase().equals("SOAPACTION:")) { request.setSoapaction(s.nextToken()); } else if (temp.toUpperCase().equals("CALLBACK:")) { request.setSoapaction(s.nextToken()); } else if (headerLine.toUpperCase().indexOf("RANGE: BYTES=") > -1) { String nums = headerLine.substring(headerLine.toUpperCase().indexOf("RANGE: BYTES=") + 13).trim(); StringTokenizer st = new StringTokenizer(nums, "-"); if (!nums.startsWith("-")) { request.setLowRange(Long.parseLong(st.nextToken())); } if (!nums.startsWith("-") && !nums.endsWith("-")) { request.setHighRange(Long.parseLong(st.nextToken())); } else { request.setHighRange(-1); } } else if (headerLine.toLowerCase().indexOf("transfermode.dlna.org:") > -1) { request.setTransferMode( headerLine .substring(headerLine.toLowerCase().indexOf("transfermode.dlna.org:") + 22) .trim()); } else if (headerLine.toLowerCase().indexOf("getcontentfeatures.dlna.org:") > -1) { request.setContentFeatures( headerLine .substring(headerLine.toLowerCase().indexOf("getcontentfeatures.dlna.org:") + 28) .trim()); } else { Matcher matcher = TIMERANGE_PATTERN.matcher(headerLine); if (matcher.find()) { String first = matcher.group(1); if (first != null) { request.setTimeRangeStartString(first); } String end = matcher.group(2); if (end != null) { request.setTimeRangeEndString(end); } } else { // If we made it to here, none of the previous header checks matched. // Unknown headers make interesting logging info when we cannot recognize // the media renderer, so keep track of the truly unknown ones. boolean isKnown = false; // Try to match possible known headers. for (String knownHeaderString : KNOWN_HEADERS) { if (headerLine.toLowerCase().startsWith(knownHeaderString.toLowerCase())) { isKnown = true; break; } } if (!isKnown) { // Truly unknown header, therefore interesting. Save for later use. unknownHeaders.append(separator).append(headerLine); separator = ", "; } } } } catch (Exception ee) { LOGGER.error("Error parsing HTTP headers", ee); } } if (!isWindowsMediaPlayer) { if (request != null) { // Still no media renderer recognized? if (request.getMediaRenderer() == null) { // Attempt 4: Not really an attempt; all other attempts to recognize // the renderer have failed. The only option left is to assume the // default renderer. request.setMediaRenderer(RendererConfiguration.getDefaultConf()); LOGGER.trace( "Using default media renderer: " + request.getMediaRenderer().getRendererName()); if (userAgentString != null && !userAgentString.equals("FDSSDP")) { // We have found an unknown renderer LOGGER.info( "Media renderer was not recognized. Possible identifying HTTP headers: User-Agent: " + userAgentString + ("".equals(unknownHeaders.toString()) ? "" : ", " + unknownHeaders.toString())); PMS.get().setRendererfound(request.getMediaRenderer()); } } else { if (userAgentString != null) { LOGGER.debug("HTTP User-Agent: " + userAgentString); } LOGGER.trace( "Recognized media renderer: " + request.getMediaRenderer().getRendererName()); } } if (HttpHeaders.getContentLength(nettyRequest) > 0) { byte data[] = new byte[(int) HttpHeaders.getContentLength(nettyRequest)]; ChannelBuffer content = nettyRequest.getContent(); content.readBytes(data); request.setTextContent(new String(data, "UTF-8")); } if (request != null) { LOGGER.trace( "HTTP: " + request.getArgument() + " / " + request.getLowRange() + "-" + request.getHighRange()); } writeResponse(e, request, ia); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (!readingChunks) { HttpResponse response = (HttpResponse) e.getMessage(); StringBuilder sb = new StringBuilder(); if (LOG.isDebugEnabled()) { sb.append("STATUS: ") .append(response.getStatus()) .append(", VERSION: ") .append(response.getProtocolVersion()) .append(", HEADER: "); } if (!response.getHeaderNames().isEmpty()) { for (String name : response.getHeaderNames()) { for (String value : response.getHeaders(name)) { if (LOG.isDebugEnabled()) { sb.append(name + " = " + value); } if (this.length == -1 && name.equals("Content-Length")) { this.length = Long.valueOf(value); } } } } if (LOG.isDebugEnabled()) { LOG.debug(sb.toString()); } if (response.getStatus() == HttpResponseStatus.NO_CONTENT) { LOG.info("There are no data corresponding to the request"); return; } this.raf = new RandomAccessFile(file, "rw"); this.fc = raf.getChannel(); if (response.isChunked()) { readingChunks = true; } else { ChannelBuffer content = response.getContent(); if (content.readable()) { fc.write(content.toByteBuffer()); } } } else { HttpChunk chunk = (HttpChunk) e.getMessage(); if (chunk.isLast()) { readingChunks = false; long fileLength = fc.position(); fc.close(); raf.close(); if (fileLength == length) { LOG.info("Data fetch is done (total received bytes: " + fileLength + ")"); } else { LOG.info( "Data fetch is done, but cannot get all data " + "(received/total: " + fileLength + "/" + length + ")"); } } else { fc.write(chunk.getContent().toByteBuffer()); } } }
private void websocketHandshake(final ChannelHandlerContext ctx, HttpRequest req, MessageEvent e) throws Exception { // Create the WebSocket handshake response. HttpResponse res = new DefaultHttpResponse( HttpVersion.HTTP_1_1, new HttpResponseStatus(101, "Web Socket Protocol Handshake")); res.addHeader(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET); res.addHeader(CONNECTION, HttpHeaders.Values.UPGRADE); // Fill in the headers and contents depending on handshake method. if (req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2)) { // New handshake method with a challenge: res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); res.addHeader( SEC_WEBSOCKET_LOCATION, "ws://" + req.getHeader(HttpHeaders.Names.HOST) + req.getUri()); 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); try { ChannelBuffer output = ChannelBuffers.wrappedBuffer(MessageDigest.getInstance("MD5").digest(input.array())); res.setContent(output); } catch (NoSuchAlgorithmException ex) { throw new UnexpectedException(ex); } } else { // Old handshake method with no challenge: res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); res.addHeader( WEBSOCKET_LOCATION, "ws://" + req.getHeader(HttpHeaders.Names.HOST) + req.getUri()); String protocol = req.getHeader(WEBSOCKET_PROTOCOL); if (protocol != null) { res.addHeader(WEBSOCKET_PROTOCOL, protocol); } } // Keep the original request Http.Request request = parseRequest(ctx, req); // Route the websocket request request.method = "WS"; Map<String, String> route = Router.route(request.method, request.path); if (!route.containsKey("action")) { // No route found to handle this websocket connection ctx.getChannel() .write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND)); return; } // Upgrade the connection and send the handshake response. ChannelPipeline p = ctx.getChannel().getPipeline(); p.remove("aggregator"); p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder()); // Connect ctx.getChannel().write(res); p.replace("encoder", "wsencoder", new WebSocketFrameEncoder()); req.setMethod(new HttpMethod("WEBSOCKET")); // Inbound Http.Inbound inbound = new Http.Inbound() { @Override public boolean isOpen() { return ctx.getChannel().isOpen(); } }; channels.put(ctx, inbound); // Outbound Http.Outbound outbound = new Http.Outbound() { final List<ChannelFuture> writeFutures = Collections.synchronizedList(new ArrayList<ChannelFuture>()); Promise<Void> closeTask; synchronized void writeAndClose(ChannelFuture writeFuture) { if (!writeFuture.isDone()) { writeFutures.add(writeFuture); writeFuture.addListener( new ChannelFutureListener() { public void operationComplete(ChannelFuture cf) throws Exception { writeFutures.remove(cf); futureClose(); } }); } } void futureClose() { if (closeTask != null && writeFutures.isEmpty()) { closeTask.invoke(null); } } @Override public void send(String data) { if (!isOpen()) { throw new IllegalStateException("The outbound channel is closed"); } writeAndClose(ctx.getChannel().write(new DefaultWebSocketFrame(data))); } @Override public void send(byte opcode, byte[] data, int offset, int length) { if (!isOpen()) { throw new IllegalStateException("The outbound channel is closed"); } writeAndClose( ctx.getChannel() .write(new DefaultWebSocketFrame(opcode, wrappedBuffer(data, offset, length)))); } @Override public synchronized boolean isOpen() { return ctx.getChannel().isOpen() && closeTask == null; } @Override public synchronized void close() { closeTask = new Promise<Void>(); closeTask.onRedeem( new Action<Promise<Void>>() { public void invoke(Promise<Void> completed) { writeFutures.clear(); ctx.getChannel().disconnect(); closeTask = null; } }); futureClose(); } }; Invoker.invoke(new WebSocketInvocation(route, request, inbound, outbound, ctx, e)); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { RequestV2 request = null; RendererConfiguration renderer = null; String userAgentString = null; StringBuilder unknownHeaders = new StringBuilder(); String separator = ""; HttpRequest nettyRequest = this.nettyRequest = (HttpRequest) e.getMessage(); InetSocketAddress remoteAddress = (InetSocketAddress) e.getChannel().getRemoteAddress(); InetAddress ia = remoteAddress.getAddress(); // Is the request from our own Cling service, i.e. self-originating? boolean isSelf = ia.getHostAddress().equals(PMS.get().getServer().getHost()) && nettyRequest.headers().get(HttpHeaders.Names.USER_AGENT) != null && nettyRequest.headers().get(HttpHeaders.Names.USER_AGENT).contains("UMS/"); // Filter if required if (isSelf || filterIp(ia)) { e.getChannel().close(); LOGGER.trace( isSelf ? ("Ignoring self-originating request from " + ia + ":" + remoteAddress.getPort()) : ("Access denied for address " + ia + " based on IP filter")); return; } LOGGER.trace("Opened request handler on socket " + remoteAddress); PMS.get().getRegistry().disableGoToSleep(); request = new RequestV2(nettyRequest.getMethod().getName(), nettyRequest.getUri().substring(1)); LOGGER.trace( "Request: " + nettyRequest.getProtocolVersion().getText() + " : " + request.getMethod() + " : " + request.getArgument()); if (nettyRequest.getProtocolVersion().getMinorVersion() == 0) { request.setHttp10(true); } HttpHeaders headers = nettyRequest.headers(); // The handler makes a couple of attempts to recognize a renderer from its requests. // IP address matches from previous requests are preferred, when that fails request // header matches are attempted and if those fail as well we're stuck with the // default renderer. // Attempt 1: try to recognize the renderer by its socket address from previous requests renderer = RendererConfiguration.getRendererConfigurationBySocketAddress(ia); // If the renderer exists but isn't marked as loaded it means it's unrecognized // by upnp and we still need to attempt http recognition here. if (renderer == null || !renderer.loaded) { // Attempt 2: try to recognize the renderer by matching headers renderer = RendererConfiguration.getRendererConfigurationByHeaders(headers.entries(), ia); } if (renderer != null) { request.setMediaRenderer(renderer); } Set<String> headerNames = headers.names(); Iterator<String> iterator = headerNames.iterator(); while (iterator.hasNext()) { String name = iterator.next(); String headerLine = name + ": " + headers.get(name); LOGGER.trace("Received on socket: " + headerLine); if (headerLine.toUpperCase().startsWith("USER-AGENT")) { userAgentString = headerLine.substring(headerLine.indexOf(':') + 1).trim(); } try { StringTokenizer s = new StringTokenizer(headerLine); String temp = s.nextToken(); if (temp.toUpperCase().equals("SOAPACTION:")) { request.setSoapaction(s.nextToken()); } else if (temp.toUpperCase().equals("CALLBACK:")) { request.setSoapaction(s.nextToken()); } else if (headerLine.toUpperCase().contains("RANGE: BYTES=")) { String nums = headerLine.substring(headerLine.toUpperCase().indexOf("RANGE: BYTES=") + 13).trim(); StringTokenizer st = new StringTokenizer(nums, "-"); if (!nums.startsWith("-")) { request.setLowRange(Long.parseLong(st.nextToken())); } if (!nums.startsWith("-") && !nums.endsWith("-")) { request.setHighRange(Long.parseLong(st.nextToken())); } else { request.setHighRange(-1); } } else if (headerLine.toLowerCase().contains("transfermode.dlna.org:")) { request.setTransferMode( headerLine .substring(headerLine.toLowerCase().indexOf("transfermode.dlna.org:") + 22) .trim()); } else if (headerLine.toLowerCase().contains("getcontentfeatures.dlna.org:")) { request.setContentFeatures( headerLine .substring(headerLine.toLowerCase().indexOf("getcontentfeatures.dlna.org:") + 28) .trim()); } else { Matcher matcher = TIMERANGE_PATTERN.matcher(headerLine); if (matcher.find()) { String first = matcher.group(1); if (first != null) { request.setTimeRangeStartString(first); } String end = matcher.group(2); if (end != null) { request.setTimeRangeEndString(end); } } else { /** * If we made it to here, none of the previous header checks matched. Unknown headers * make interesting logging info when we cannot recognize the media renderer, so keep * track of the truly unknown ones. */ boolean isKnown = false; // Try to match known headers. String lowerCaseHeaderLine = headerLine.toLowerCase(); for (String knownHeaderString : KNOWN_HEADERS) { if (lowerCaseHeaderLine.startsWith(knownHeaderString)) { isKnown = true; break; } } // It may be unusual but already known if (renderer != null) { String additionalHeader = renderer.getUserAgentAdditionalHttpHeader(); if (StringUtils.isNotBlank(additionalHeader) && lowerCaseHeaderLine.startsWith(additionalHeader)) { isKnown = true; } } if (!isKnown) { // Truly unknown header, therefore interesting. Save for later use. unknownHeaders.append(separator).append(headerLine); separator = ", "; } } } } catch (Exception ee) { LOGGER.error("Error parsing HTTP headers", ee); } } // Still no media renderer recognized? if (request.getMediaRenderer() == null) { // Attempt 3: Not really an attempt; all other attempts to recognize // the renderer have failed. The only option left is to assume the // default renderer. request.setMediaRenderer(RendererConfiguration.resolve(ia, null)); if (request.getMediaRenderer() != null) { LOGGER.trace( "Using default media renderer: " + request.getMediaRenderer().getRendererName()); if (userAgentString != null && !userAgentString.equals("FDSSDP")) { // We have found an unknown renderer LOGGER.info( "Media renderer was not recognized. Possible identifying HTTP headers: User-Agent: " + userAgentString + ("".equals(unknownHeaders.toString()) ? "" : ", " + unknownHeaders.toString())); PMS.get().setRendererFound(request.getMediaRenderer()); } } else { // If RendererConfiguration.resolve() didn't return the default renderer // it means we know via upnp that it's not really a renderer. return; } } else { if (userAgentString != null) { LOGGER.debug("HTTP User-Agent: " + userAgentString); } LOGGER.trace("Recognized media renderer: " + request.getMediaRenderer().getRendererName()); } if (nettyRequest.headers().contains(HttpHeaders.Names.CONTENT_LENGTH)) { byte data[] = new byte[(int) HttpHeaders.getContentLength(nettyRequest)]; ChannelBuffer content = nettyRequest.getContent(); content.readBytes(data); request.setTextContent(new String(data, "UTF-8")); } LOGGER.trace( "HTTP: " + request.getArgument() + " / " + request.getLowRange() + "-" + request.getHighRange()); writeResponse(ctx, e, request, ia); }