use of io.undertow.util.HeaderValues in project undertow by undertow-io.
the class LotsOfHeadersRequestTestCase method setup.
@BeforeClass
public static void setup() {
Assume.assumeFalse(DefaultServer.isH2upgrade());
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
HeaderMap headers = exchange.getRequestHeaders();
for (HeaderValues header : headers) {
for (String val : header) {
exchange.getResponseHeaders().put(HttpString.tryFromString(header.getHeaderName().toString()), val);
}
}
}
});
}
use of io.undertow.util.HeaderValues in project undertow by undertow-io.
the class RequestDumpingHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final StringBuilder sb = new StringBuilder();
// Log pre-service information
final SecurityContext sc = exchange.getSecurityContext();
sb.append("\n----------------------------REQUEST---------------------------\n");
sb.append(" URI=" + exchange.getRequestURI() + "\n");
sb.append(" characterEncoding=" + exchange.getRequestHeaders().get(Headers.CONTENT_ENCODING) + "\n");
sb.append(" contentLength=" + exchange.getRequestContentLength() + "\n");
sb.append(" contentType=" + exchange.getRequestHeaders().get(Headers.CONTENT_TYPE) + "\n");
//sb.append(" contextPath=" + exchange.getContextPath());
if (sc != null) {
if (sc.isAuthenticated()) {
sb.append(" authType=" + sc.getMechanismName() + "\n");
sb.append(" principle=" + sc.getAuthenticatedAccount().getPrincipal() + "\n");
} else {
sb.append(" authType=none" + "\n");
}
}
Map<String, Cookie> cookies = exchange.getRequestCookies();
if (cookies != null) {
for (Map.Entry<String, Cookie> entry : cookies.entrySet()) {
Cookie cookie = entry.getValue();
sb.append(" cookie=" + cookie.getName() + "=" + cookie.getValue() + "\n");
}
}
for (HeaderValues header : exchange.getRequestHeaders()) {
for (String value : header) {
sb.append(" header=" + header.getHeaderName() + "=" + value + "\n");
}
}
sb.append(" locale=" + LocaleUtils.getLocalesFromHeader(exchange.getRequestHeaders().get(Headers.ACCEPT_LANGUAGE)) + "\n");
sb.append(" method=" + exchange.getRequestMethod() + "\n");
Map<String, Deque<String>> pnames = exchange.getQueryParameters();
for (Map.Entry<String, Deque<String>> entry : pnames.entrySet()) {
String pname = entry.getKey();
Iterator<String> pvalues = entry.getValue().iterator();
sb.append(" parameter=");
sb.append(pname);
sb.append('=');
while (pvalues.hasNext()) {
sb.append(pvalues.next());
if (pvalues.hasNext()) {
sb.append(", ");
}
}
sb.append("\n");
}
//sb.append(" pathInfo=" + exchange.getPathInfo());
sb.append(" protocol=" + exchange.getProtocol() + "\n");
sb.append(" queryString=" + exchange.getQueryString() + "\n");
sb.append(" remoteAddr=" + exchange.getSourceAddress() + "\n");
sb.append(" remoteHost=" + exchange.getSourceAddress().getHostName() + "\n");
//sb.append("requestedSessionId=" + exchange.getRequestedSessionId());
sb.append(" scheme=" + exchange.getRequestScheme() + "\n");
sb.append(" host=" + exchange.getRequestHeaders().getFirst(Headers.HOST) + "\n");
sb.append(" serverPort=" + exchange.getDestinationAddress().getPort() + "\n");
//sb.append(" servletPath=" + exchange.getServletPath());
//sb.append(" isSecure=" + exchange.isSecure());
exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {
@Override
public void exchangeEvent(final HttpServerExchange exchange, final NextListener nextListener) {
// Log post-service information
sb.append("--------------------------RESPONSE--------------------------\n");
if (sc != null) {
if (sc.isAuthenticated()) {
sb.append(" authType=" + sc.getMechanismName() + "\n");
sb.append(" principle=" + sc.getAuthenticatedAccount().getPrincipal() + "\n");
} else {
sb.append(" authType=none" + "\n");
}
}
sb.append(" contentLength=" + exchange.getResponseContentLength() + "\n");
sb.append(" contentType=" + exchange.getResponseHeaders().getFirst(Headers.CONTENT_TYPE) + "\n");
Map<String, Cookie> cookies = exchange.getResponseCookies();
if (cookies != null) {
for (Cookie cookie : cookies.values()) {
sb.append(" cookie=" + cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain() + "; path=" + cookie.getPath() + "\n");
}
}
for (HeaderValues header : exchange.getResponseHeaders()) {
for (String value : header) {
sb.append(" header=" + header.getHeaderName() + "=" + value + "\n");
}
}
sb.append(" status=" + exchange.getStatusCode() + "\n");
String storedResponse = StoredResponse.INSTANCE.readAttribute(exchange);
if (storedResponse != null) {
sb.append("body=\n");
sb.append(storedResponse);
}
sb.append("==============================================================");
nextListener.proceed();
UndertowLogger.REQUEST_DUMPER_LOGGER.info(sb.toString());
}
});
// Perform the exchange
next.handleRequest(exchange);
}
use of io.undertow.util.HeaderValues in project undertow by undertow-io.
the class ExtendedAccessLogParser method getXParameterElement.
protected ExchangeAttribute getXParameterElement(PatternTokenizer tokenizer) throws IOException {
if (!tokenizer.hasSubToken()) {
UndertowLogger.ROOT_LOGGER.extendedAccessLogBadXParam();
return null;
}
final String token = tokenizer.getToken();
if (!tokenizer.hasParameter()) {
UndertowLogger.ROOT_LOGGER.extendedAccessLogBadXParam();
return null;
}
String parameter = tokenizer.getParameter();
if (parameter == null) {
UndertowLogger.ROOT_LOGGER.extendedAccessLogMissingClosing();
return null;
}
if ("A".equals(token)) {
parser.parse("%{sc," + parameter + "}");
} else if ("C".equals(token)) {
return new QuotingExchangeAttribute(new CookieAttribute(parameter));
} else if ("R".equals(token)) {
parser.parse("%{r," + parameter + "}");
} else if ("S".equals(token)) {
parser.parse("%{s," + parameter + "}");
} else if ("H".equals(token)) {
return getServletRequestElement(parameter);
} else if ("P".equals(token)) {
parser.parse("%{rp," + parameter + "}");
} else if ("O".equals(token)) {
return new QuotingExchangeAttribute(new ExchangeAttribute() {
@Override
public String readAttribute(HttpServerExchange exchange) {
HeaderValues values = exchange.getResponseHeaders().get(token);
if (values != null && values.size() > 0) {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < values.size(); i++) {
String string = values.get(i);
buffer.append(string);
if (i + 1 < values.size())
buffer.append(",");
}
return buffer.toString();
}
return null;
}
@Override
public void writeAttribute(HttpServerExchange exchange, String newValue) throws ReadOnlyAttributeException {
throw new ReadOnlyAttributeException();
}
});
}
UndertowLogger.ROOT_LOGGER.extendedAccessLogCannotDecodeXParamValue(token);
return null;
}
use of io.undertow.util.HeaderValues in project undertow by undertow-io.
the class ProxyHandler method copyHeaders.
static void copyHeaders(final HeaderMap to, final HeaderMap from) {
long f = from.fastIterateNonEmpty();
HeaderValues values;
while (f != -1L) {
values = from.fiCurrent(f);
if (!to.contains(values.getHeaderName())) {
//don't over write existing headers, normally the map will be empty, if it is not we assume it is not for a reason
to.putAll(values.getHeaderName(), values);
}
f = from.fiNextNonEmpty(f);
}
}
use of io.undertow.util.HeaderValues in project undertow by undertow-io.
the class HttpTraceHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if (exchange.getRequestMethod().equals(Methods.TRACE)) {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "message/http");
StringBuilder body = new StringBuilder("TRACE ");
body.append(exchange.getRequestURI());
if (!exchange.getQueryString().isEmpty()) {
body.append('?');
body.append(exchange.getQueryString());
}
body.append(' ');
body.append(exchange.getProtocol().toString());
body.append("\r\n");
for (HeaderValues header : exchange.getRequestHeaders()) {
for (String value : header) {
body.append(header.getHeaderName());
body.append(": ");
body.append(value);
body.append("\r\n");
}
}
body.append("\r\n");
exchange.getResponseSender().send(body.toString());
} else {
handler.handleRequest(exchange);
}
}
Aggregations