public static URI getRequestURI(URI requestURI, String hostHeader, IoSession session) { boolean secure = SslUtils.isSecure(session); String authority = HttpUtils.getHostAndPort(hostHeader, secure); // Use getRawPath to get the un-decoded path; getPath returns the post-decode value. // This is required to handle special characters like spaces in the URI (KG-831). URI uri = URI.create("//" + authority + requestURI.getRawPath()); return uri; }
public static URI getRequestURI(HttpRequestMessage request, IoSession session) { URI requestURI = request.getRequestURI(); String host = request.getHeader("Host"); return getRequestURI(requestURI, host, session); }
public static boolean hasBeenModified(HttpSession session, String etag, File requestFile) { String ifNoneMatch = session.getReadHeader("If-None-Match"); String ifModifiedSince = session.getReadHeader("If-Modified-Since"); return hasBeenModified(requestFile, etag, ifNoneMatch, ifModifiedSince); }
private String getEmulatedOriginIfRequestMatchesOrigin(HttpRequestMessage httpRequest, String candidateOrigin) { String emulatedOrigin = null; String origin = httpRequest.getHeader("Origin"); URI requestURI = HttpUtils.getCanonicalURI(httpRequest.getRequestURI(), false); if (origin != null && requestURI != null) { URI originURI = HttpUtils.getCanonicalURI(origin, false); String originScheme = originURI.getScheme(); String originAuthority = originURI.getAuthority(); if (originAuthority != null && originAuthority.indexOf(':') == -1) { int port = "https".equals(originScheme) ? 443 : 80; originAuthority += ":" + port; } boolean isSecure = httpRequest.isSecure(); String scheme = isSecure ? "https" : "http"; String authority = HttpUtils.getHostAndPort(httpRequest, isSecure); if (scheme.equals(originScheme) && authority.equals(originAuthority)) { // cross-origin request emulated via same-origin request, // use .ko query parameter for Origin emulatedOrigin = candidateOrigin; } } return emulatedOrigin; } }
private String sendChallengeResponse(NextFilter nextFilter, IoSession session, HttpRequestMessage httpRequest, DefaultLoginResult loginResult, HttpRealmInfo[] realms, int realmIndex, LoginContext[] loginContexts) { HttpRealmInfo realm = realms[realmIndex]; Object[] challengeData = loginResult != null && loginResult.getType() == LoginResult.Type.CHALLENGE ? loginResult.getLoginChallengeData() : null; HttpResponseMessage httpResponse = challengeFactory.createChallenge(httpRequest, realm, challengeData); if (realmIndex > 0) { String challengeIdentity; do { challengeIdentity = HttpUtils.newSessionId(); httpResponse.setHeader(HEADER_SEC_CHALLENGE_IDENTITY, challengeIdentity); } while (expiringState != null && expiringState.putIfAbsent(challengeIdentity, loginContexts, 30, SECONDS) != null); } writeChallenge(httpResponse, nextFilter, session, realm.getChallengeScheme()); return httpResponse.getHeader(HEADER_WWW_AUTHENTICATE); }
String etag = HttpUtils.getETagHeaderValue(requestFile); boolean modified = HttpUtils.hasBeenModified(session, etag, requestFile); if (!modified) { HttpUtils.addLastModifiedHeader(session, requestFile); IoBufferEx buf = HttpUtils.getBufferForFile(allocator, requestFile);
String sessionId = HttpUtils.newSessionId(); String authority = HttpUtils.getHostAndPort(request.getAuthority(), protocol.isSecure()); final URI rtmpAddress = new URI(scheme, authority, acceptPath + sessionIdSuffix, request.getQuery(), request.getFragment());
public static void fileRequested(IoBufferAllocatorEx<?> allocator, HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, File requestFile) throws IOException { if (requestFile.isFile() && requestFile.exists()) { String etag = getETagHeaderValue(requestFile); String ifNoneMatch = httpRequest.getHeader("If-None-Match"); String ifModifiedSince = httpRequest.getHeader("If-Modified-Since"); if (!hasBeenModified(requestFile, etag, ifNoneMatch, ifModifiedSince)) { httpResponse.setHeader("ETag", etag); httpResponse.setStatus(HttpStatus.REDIRECT_NOT_MODIFIED); } else { FileInputStream in = new FileInputStream(requestFile); byte[] buf = new byte[8192]; IoBufferEx out = allocator.wrap(allocator.allocate(in.available())).setAutoExpander(allocator); int length; while ((length = in.read(buf)) > 0) { out.put(buf, 0, length); } out.flip(); in.close(); httpResponse.setHeader("ETag", etag); httpResponse.setHeader("Last-Modified", RFC822_FORMAT_PATTERN.format(requestFile.lastModified())); httpResponse.setHeader("Expires", RFC822_FORMAT_PATTERN.format(System.currentTimeMillis())); httpResponse.setContent(new HttpContentMessage(out, true)); // Note: callers are responsible for adding the Content-Type header, // per KG-866. See HttpCrossSiteBridgeFilter for an example. } } else { httpResponse.setStatus(HttpStatus.CLIENT_NOT_FOUND); } }
if (!HttpUtils.canStream(session)) {
String sessionId = HttpUtils.newSessionId();
public static void writeIfModified(HttpAcceptSession httpSession, File requestFile) throws IOException { if (requestFile.isFile() && requestFile.exists()) { String etag = getETagHeaderValue(requestFile); String ifNoneMatch = httpSession.getReadHeader("If-None-Match"); String ifModifiedSince = httpSession.getReadHeader("If-Modified-Since"); if (!hasBeenModified(requestFile, etag, ifNoneMatch, ifModifiedSince)) { httpSession.setWriteHeader("ETag", etag); httpSession.setStatus(REDIRECT_NOT_MODIFIED);
boolean useHttpStreaming = HttpUtils.canStream(httpSession);
private SseReconnectHandler createSession(final HttpAcceptSession httpSession) throws Exception { String sessionId = HttpUtils.newSessionId();
public static String getHostAndPort(HttpRequestMessage httpRequest, boolean secure) { String authority = httpRequest.getHeader("Host"); return getHostAndPort(authority, secure); }
public static void handleExtensionNegotiationException(HttpAcceptSession session, List<String> clientRequestedExtensions, ProtocolException e, Logger logger) { // This happens when the extension negotiation leads to // a fatal failure; the session should be closed because // the service REQUIRED some extension that the client // did not request. if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) { // KG-10384: make sure port is explicitly included in the request URI we use for lookup since it is always // included when the service registry is created since we force use of explicit port in accepts. // TODO: consider doing this "at the edge" when the HTTP request object (or http session) is created. URI requestURI = HttpUtils.getRequestURI(session.getRequestURL(), session.getReadHeader("Host"), session); logger.debug(format( "Rejected %s request for URI \"%s\" on session '%s': failed to negotiate client requested extensions '%s'" + " due to exception %s", session.getMethod(), requestURI, session, clientRequestedExtensions, e.toString())); } } session.setStatus(HttpStatus.CLIENT_NOT_FOUND); session.setReason("WebSocket Extensions not found or invalid"); session.close(false); }
private String getEmulatedOriginIfReferrerMatches(HttpRequestMessage httpRequest, String candidateOrigin) { String emulatedOrigin = null; // same-origin iframe will always send Referer (never cross-scheme) // so verify same-origin request to allow .ko query parameter String referer = httpRequest.getHeader("Referer"); if (referer != null) { URI refererURI = URI.create(referer); boolean isSecure = httpRequest.isSecure(); String scheme = isSecure ? "https" : "http"; String authority = HttpUtils.getHostAndPort(httpRequest, isSecure); String refererAuthority = HttpUtils.getHostAndPort(refererURI.getAuthority(), isSecure); if (refererURI.getScheme().equals(scheme) && refererAuthority.equals(authority)) { // cross-origin request emulated via same-origin request, // use .ko query parameter for Origin emulatedOrigin = candidateOrigin; } else { emulatedOrigin = "null"; } } return emulatedOrigin; }
public static URI getTransportURI(HttpRequestMessage request, IoSession session) { URI requestURI = request.getRequestURI(); String hostHeader = request.getHeader("Host"); boolean secure = SslUtils.isSecure(session); String authority = HttpUtils.getHostAndPort(hostHeader, secure); // Use getRawPath to get the un-decoded path; getPath returns the post-decode value. // This is required to handle special characters like spaces in the URI (KG-831). return URI.create("http://" + authority + requestURI.getRawPath()); }
String originScheme = originURI.getScheme(); String originAuthority = originURI.getAuthority(); originAuthority = HttpUtils.getHostAndPort(originAuthority, originScheme.equals("https")); boolean targetIsSecure = "https".equals(URIUtils.getScheme(targetURI)); String targetScheme = URIUtils.getScheme(targetURI); String targetAuthority = HttpUtils.getHostAndPort(URIUtils.getAuthority(targetURI), targetIsSecure); if ("privileged".equals(originScheme) || ((targetScheme.equals(originScheme) && targetAuthority.equals(originAuthority)))) { String targetAuthority = HttpUtils.getHostAndPort(httpRequest, targetIsSecure);