in common/src/main/java/org/apache/asyncweb/common/DefaultHttpRequest.java [323:454]
public void normalize() {
// Encode parameters.
Map<String, List<String>> params = getParameters();
if (!params.isEmpty()) {
try {
boolean first = true;
StringBuilder buf = new StringBuilder();
for (Map.Entry<String, List<String>> e: params.entrySet()) {
if (e.getValue().isEmpty()) {
continue;
}
for (String v: e.getValue()) {
if (!first) {
buf.append('&');
}
buf.append(URLEncoder.encode(
e.getKey(), HttpCodecUtils.DEFAULT_CHARSET_NAME));
buf.append('=');
buf.append(URLEncoder.encode(
v, HttpCodecUtils.DEFAULT_CHARSET_NAME));
first = false;
}
}
if (buf.length() > 0) {
String uri = getRequestUri().toString();
int queryIndex = uri.indexOf('?');
switch (getMethod()) {
case POST:
if (queryIndex >= 0) {
setRequestUri(new URI(uri.substring(0, queryIndex)));
}
IoBuffer content = IoBuffer.allocate(buf.length());
content.put(buf.toString().getBytes(HttpCodecUtils.US_ASCII_CHARSET_NAME));
content.flip();
setContent(content);
setHeader(
HttpHeaderConstants.KEY_CONTENT_TYPE,
HttpHeaderConstants.VALUE_URLENCODED_FORM);
break;
default:
if (queryIndex >= 0) {
setRequestUri(new URI(
uri.substring(0, queryIndex + 1) + buf));
} else {
setRequestUri(new URI(
uri + '?' + buf));
}
}
}
} catch (Exception e) {
throw (InternalError) new InternalError(
"Unexpected exception.").initCause(e);
}
}
// Encode Cookies
Set<Cookie> cookies = getCookies();
if (!cookies.isEmpty()) {
// Clear previous values.
removeHeader(HttpHeaderConstants.KEY_COOKIE);
// And encode.
for (Cookie c: cookies) {
StringBuilder buf = new StringBuilder();
buf.append(c.getName());
buf.append('=');
buf.append(c.getValue());
if (c.getVersion() > 0) {
buf.append("; version=");
buf.append(c.getVersion());
}
if (c.getPath() != null) {
buf.append("; path=");
buf.append(c.getPath());
}
long expiry = c.getMaxAge();
int version = c.getVersion();
if (expiry >= 0) {
if (version == 0) {
String expires = expiry == 0 ? EXPIRED_DATE
: getFormattedExpiry(System.currentTimeMillis()
+ 1000 * expiry);
buf.append("; Expires=");
buf.append(expires);
} else {
buf.append("; max-age=");
buf.append(c.getMaxAge());
}
}
if (c.isSecure()) {
buf.append("; secure");
}
if (c.isHttpOnly()) {
buf.append("; HTTPOnly");
}
buf.append(';');
addHeader( HttpHeaderConstants.KEY_COOKIE, buf.toString() );
}
}
// Add the Host header.
if (!containsHeader(HttpHeaderConstants.KEY_HOST)) {
URI uri = getRequestUri();
String host = uri.getHost();
if (host != null) {
if ((uri.getScheme().equalsIgnoreCase("http") &&
uri.getPort() != 80 && uri.getPort() > 0) ||
(uri.getScheme().equalsIgnoreCase("https") &&
uri.getPort() != 443 && uri.getPort() > 0)) {
setHeader(HttpHeaderConstants.KEY_HOST, host + ':' + uri.getPort());
} else {
setHeader(HttpHeaderConstants.KEY_HOST, host);
}
}
}
// Set Content-Length.
if (!containsHeader(HttpHeaderConstants.KEY_TRANSFER_CODING)) {
IoBuffer content = getContent();
int contentLength = content == null? 0 : content.remaining();
setHeader(
HttpHeaderConstants.KEY_CONTENT_LENGTH,
String.valueOf(contentLength));
}
}