@Override public HttpHandler wrap(HttpHandler handler) { HttpString[] strings = new HttpString[methods.length]; for(int i = 0; i < methods.length; ++i) { strings[i] = new HttpString(methods[i]); } return new DisallowedMethodsHandler(handler, strings); } }
@Override public HttpHandler wrap(HttpHandler handler) { HttpString[] strings = new HttpString[methods.length]; for(int i = 0; i < methods.length; ++i) { strings[i] = new HttpString(methods[i]); } return new AllowedMethodsHandler(handler, strings); } }
public static HttpString fromString(String method) { HttpString res = METHODS.get(method); if(res == null) { HttpString httpString = new HttpString(method); Connectors.verifyToken(httpString); return httpString; } return res; }
public synchronized RoutingHandler add(final String method, final String template, HttpHandler handler) { return add(new HttpString(method), template, handler); }
public synchronized RoutingHandler add(final String method, final String template, Predicate predicate, HttpHandler handler) { return add(new HttpString(method), template, predicate, handler); }
static RequestData buildRequestData(final HttpServerExchange exchange, Map<String, Deque<String>> params) { final RequestData data = new RequestData(); for (final Map.Entry<String, Deque<String>> entry : params.entrySet()) { final HttpString name = new HttpString(entry.getKey()); data.addValues(name, entry.getValue()); } return data; }
/** * Attempt to convert a {@code String} to an {@code HttpString}. If the string cannot be converted, * {@code null} is returned. * * @param string the string to try * @return the HTTP string, or {@code null} if the string is not in a compatible encoding */ public static HttpString tryFromString(String string) { HttpString cached = Headers.fromCache(string); if(cached != null) { return cached; } final int len = string.length(); final byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { char c = string.charAt(i); if (c > 0xff) { return null; } bytes[i] = (byte) c; } return new HttpString(bytes, string); }
@Override public AuthenticationMechanism create(String mechanismName, IdentityManager identityManager, FormParserFactory formParserFactory, Map<String, String> properties) { String identity = properties.get(IDENTITY_HEADER); if(identity == null) { throw UndertowMessages.MESSAGES.authenticationPropertyNotSet(mechanismName, IDENTITY_HEADER); } String session = properties.get(SESSION_HEADER); if(session == null) { throw UndertowMessages.MESSAGES.authenticationPropertyNotSet(mechanismName, SESSION_HEADER); } List<HttpString> ids = new ArrayList<>(); for(String s : identity.split(",")) { ids.add(new HttpString(s)); } List<String> sessions = new ArrayList<>(); for(String s : session.split(",")) { sessions.add(s); } return new GenericHeaderAuthenticationMechanism(mechanismName, ids, sessions, identityManager); } }
private HttpString readHeaderName(ByteBuffer buffer, int prefixLength) throws HpackException { buffer.position(buffer.position() - 1); //unget the byte int index = Hpack.decodeInteger(buffer, prefixLength); if (index == -1) { return null; } else if (index != 0) { return handleIndexedHeaderName(index); } else { String string = readHpackString(buffer); if (string == null) { return null; } return new HttpString(string); } }
public SetHeaderHandler(final String header, final String value) { if(value == null) { throw UndertowMessages.MESSAGES.argumentCannotBeNull("value"); } if(header == null) { throw UndertowMessages.MESSAGES.argumentCannotBeNull("header"); } this.next = ResponseCodeHandler.HANDLE_404; this.value = ExchangeAttributes.constant(value); this.header = new HttpString(header); }
private static boolean persistentConnection(HttpServerExchange exchange, String connectionHeader) { if (exchange.isHttp11()) { return !(connectionHeader != null && Headers.CLOSE.equalToString(connectionHeader)); } else if (exchange.isHttp10()) { if (connectionHeader != null) { if (Headers.KEEP_ALIVE.equals(new HttpString(connectionHeader))) { return true; } } } log.trace("Connection not persistent"); return false; }
public SetHeaderHandler(final HttpHandler next, final String header, final ExchangeAttribute value) { if(value == null) { throw UndertowMessages.MESSAGES.argumentCannotBeNull("value"); } if(header == null) { throw UndertowMessages.MESSAGES.argumentCannotBeNull("header"); } if(next == null) { throw UndertowMessages.MESSAGES.argumentCannotBeNull("next"); } this.next = next; this.value = value; this.header = new HttpString(header); }
private static Predicate[] getCompressionPredicates(Compression compression) { List<Predicate> predicates = new ArrayList<>(); predicates.add( new MaxSizePredicate((int) compression.getMinResponseSize().toBytes())); predicates.add(new CompressibleMimeTypePredicate(compression.getMimeTypes())); if (compression.getExcludedUserAgents() != null) { for (String agent : compression.getExcludedUserAgents()) { RequestHeaderAttribute agentHeader = new RequestHeaderAttribute( new HttpString(HttpHeaders.USER_AGENT)); predicates.add(Predicates.not(Predicates.regex(agentHeader, agent))); } } return predicates.toArray(new Predicate[0]); }
protected ExchangeAttribute getServerToClientElement( PatternTokenizer tokenizer) throws IOException { if (tokenizer.hasSubToken()) { String token = tokenizer.getToken(); if ("status".equals(token)) { return ResponseCodeAttribute.INSTANCE; } else if ("comment".equals(token)) { return new ConstantExchangeAttribute("?"); } } else if (tokenizer.hasParameter()) { String parameter = tokenizer.getParameter(); if (parameter == null) { UndertowLogger.ROOT_LOGGER.extendedAccessLogMissingClosing(); return null; } return new QuotingExchangeAttribute(new ResponseHeaderAttribute(new HttpString(parameter))); } UndertowLogger.ROOT_LOGGER.extendedAccessLogCannotDecode(tokenizer.getRemains()); return null; }
/** * Send an error message. * * @param type the error type * @param errString the error string * @param exchange the http server exchange */ static void processError(String type, String errString, HttpServerExchange exchange) { exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR); exchange.getResponseHeaders().add(Headers.CONTENT_TYPE, CONTENT_TYPE); exchange.getResponseHeaders().add(new HttpString("Version"), VERSION_PROTOCOL); exchange.getResponseHeaders().add(new HttpString("Type"), type); exchange.getResponseHeaders().add(new HttpString("Mess"), errString); exchange.endExchange(); UndertowLogger.ROOT_LOGGER.mcmpProcessingError(type, errString); }
public SetHeaderHandler(final HttpHandler next, final String header, final String value) { if(value == null) { throw UndertowMessages.MESSAGES.argumentCannotBeNull("value"); } if(header == null) { throw UndertowMessages.MESSAGES.argumentCannotBeNull("header"); } if(next == null) { throw UndertowMessages.MESSAGES.argumentCannotBeNull("next"); } this.next = next; this.value = ExchangeAttributes.constant(value); this.header = new HttpString(header); } @Override
/** * Transform the form data into an intermediate request data which can me used * by the web manager * * @param exchange the http server exchange * @return * @throws IOException */ RequestData parseFormData(final HttpServerExchange exchange) throws IOException { // Read post parameters final FormDataParser parser = parserFactory.createParser(exchange); final FormData formData = parser.parseBlocking(); final RequestData data = new RequestData(); for (String name : formData) { final HttpString key = new HttpString(name); data.add(key, formData.get(name)); } return data; }
private static StreamSinkConduit handleExplicitTransferEncoding(HttpServerExchange exchange, StreamSinkConduit channel, ConduitListener<StreamSinkConduit> finishListener, HeaderMap responseHeaders, String transferEncodingHeader, boolean headRequest) { HttpString transferEncoding = new HttpString(transferEncodingHeader); if (transferEncoding.equals(Headers.CHUNKED)) { if (headRequest) { return channel; } Boolean preChunked = exchange.getAttachment(HttpAttachments.PRE_CHUNKED_RESPONSE); if(preChunked != null && preChunked) { return new PreChunkedStreamSinkConduit(channel, finishListener, exchange); } else { return new ChunkedStreamSinkConduit(channel, exchange.getConnection().getByteBufferPool(), true, !exchange.isPersistent(), responseHeaders, finishListener, exchange); } } else { if (headRequest) { return channel; } log.trace("Cancelling persistence because response is identity with no content length"); // make it not persistent - very unfortunate for the next request handler really... exchange.setPersistent(false); responseHeaders.put(Headers.CONNECTION, Headers.CLOSE.toString()); return new FinishableStreamSinkConduit(channel, terminateResponseListener(exchange)); } }
@Override public void sendRequest(final ClientRequest request, final ClientCallback<ClientExchange> clientCallback) { if(http2Delegate != null) { http2Delegate.sendRequest(request, clientCallback); return; } if (anyAreSet(state, UPGRADE_REQUESTED | UPGRADED | CLOSE_REQ | CLOSED)) { clientCallback.failed(UndertowClientMessages.MESSAGES.invalidConnectionState()); return; } final HttpClientExchange httpClientExchange = new HttpClientExchange(clientCallback, request, this); boolean ssl = this.connection instanceof SslConnection; if(!ssl && !http2Tried && options.get(UndertowOptions.ENABLE_HTTP2, false) && !request.getRequestHeaders().contains(Headers.UPGRADE)) { //this is the first request, as we want to try a HTTP2 upgrade request.getRequestHeaders().put(new HttpString("HTTP2-Settings"), Http2ClearClientProvider.createSettingsFrame(options, bufferPool)); request.getRequestHeaders().put(Headers.UPGRADE, Http2Channel.CLEARTEXT_UPGRADE_STRING); request.getRequestHeaders().put(Headers.CONNECTION, "Upgrade, HTTP2-Settings"); http2Tried = true; } if (currentRequest == null) { initiateRequest(httpClientExchange); } else { pendingQueue.add(httpClientExchange); } }
private void prepareResponseChannel(ClientResponse response, ClientExchange exchange) { String encoding = response.getResponseHeaders().getLast(Headers.TRANSFER_ENCODING); boolean chunked = encoding != null && Headers.CHUNKED.equals(new HttpString(encoding)); String length = response.getResponseHeaders().getFirst(Headers.CONTENT_LENGTH); if (exchange.getRequest().getMethod().equals(Methods.HEAD)) { connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener)); } else if (chunked) { connection.getSourceChannel().setConduit(new ChunkedStreamSourceConduit(connection.getSourceChannel().getConduit(), pushBackStreamSourceConduit, bufferPool, responseFinishedListener, exchange, connection)); } else if (length != null) { try { long contentLength = Long.parseLong(length); connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), contentLength, responseFinishedListener)); } catch (NumberFormatException e) { handleError(e); throw e; } } else if (response.getProtocol().equals(Protocols.HTTP_1_1) && !Connectors.isEntityBodyAllowed(response.getResponseCode())) { connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener)); } else { connection.getSourceChannel().setConduit(new FinishableStreamSourceConduit(connection.getSourceChannel().getConduit(), responseFinishedListener)); state |= CLOSE_REQ; } }