use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.
the class HttpUploadClient method formpost.
/**
* Standard post without multipart but already support on Factory (memory management)
*
* @return the list of HttpData object (attribute and file) to be reused on next post
*/
private static List<InterfaceHttpData> formpost(Bootstrap bootstrap, String host, int port, URI uriSimple, File file, HttpDataFactory factory, List<Entry<String, String>> headers) throws Exception {
// XXX /formpost
// Start the connection attempt.
ChannelFuture future = bootstrap.connect(SocketUtils.socketAddress(host, port));
// Wait until the connection attempt succeeds or fails.
Channel channel = future.sync().channel();
// Prepare the HTTP request.
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriSimple.toASCIIString());
// Use the PostBody encoder
HttpPostRequestEncoder bodyRequestEncoder = // false => not multipart
new HttpPostRequestEncoder(factory, request, false);
// it is legal to add directly header or cookie into the request until finalize
for (Entry<String, String> entry : headers) {
request.headers().set(entry.getKey(), entry.getValue());
}
// add Form attribute
bodyRequestEncoder.addBodyAttribute("getform", "POST");
bodyRequestEncoder.addBodyAttribute("info", "first value");
bodyRequestEncoder.addBodyAttribute("secondinfo", "secondvalue ���&");
bodyRequestEncoder.addBodyAttribute("thirdinfo", textArea);
bodyRequestEncoder.addBodyAttribute("fourthinfo", textAreaLong);
bodyRequestEncoder.addBodyFileUpload("myfile", file, "application/x-zip-compressed", false);
// finalize request
request = bodyRequestEncoder.finalizeRequest();
// Create the bodylist to be reused on the last version with Multipart support
List<InterfaceHttpData> bodylist = bodyRequestEncoder.getBodyListAttributes();
// send request
channel.write(request);
// test if request was chunked and if so, finish the write
if (bodyRequestEncoder.isChunked()) {
// could do either request.isChunked()
// either do it through ChunkedWriteHandler
channel.write(bodyRequestEncoder);
}
channel.flush();
// Do not clear here since we will reuse the InterfaceHttpData on the next request
// for the example (limit action on client side). Take this as a broadcast of the same
// request on both Post actions.
//
// On standard program, it is clearly recommended to clean all files after each request
// bodyRequestEncoder.cleanFiles();
// Wait for the server to close the connection.
channel.closeFuture().sync();
return bodylist;
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project riposte by Nike-Inc.
the class ProxyRouterEndpoint method generateSimplePassthroughRequest.
/**
* Helper method that generates a {@link HttpRequest} for the downstream call's first chunk that uses the given
* downstreamPath and downstreamMethod, and the query string and headers from the incomingRequest will be added and
* passed through without modification.
*/
@SuppressWarnings("UnusedParameters")
protected HttpRequest generateSimplePassthroughRequest(RequestInfo<?> incomingRequest, String downstreamPath, HttpMethod downstreamMethod, ChannelHandlerContext ctx) {
String queryString = extractQueryString(incomingRequest.getUri());
String downstreamUri = downstreamPath;
// TODO: Add logic to support when downstreamPath already has a query string on it. The two query strings should be combined
if (queryString != null)
downstreamUri += queryString;
HttpRequest downstreamRequestInfo = new DefaultHttpRequest(HttpVersion.HTTP_1_1, downstreamMethod, downstreamUri);
downstreamRequestInfo.headers().set(incomingRequest.getHeaders());
return downstreamRequestInfo;
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project riposte by Nike-Inc.
the class ExceptionHandlingHandlerTest method getRequestInfo_creates_new_RequestInfo_based_on_msg_if_state_requestInfo_is_null_and_msg_is_a_HttpRequest.
@Test
public void getRequestInfo_creates_new_RequestInfo_based_on_msg_if_state_requestInfo_is_null_and_msg_is_a_HttpRequest() {
// given
assertThat(state.getRequestInfo(), nullValue());
String expectedUri = "/some/uri/" + UUID.randomUUID().toString();
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, expectedUri);
// when
RequestInfo<?> result = handler.getRequestInfo(state, httpRequest);
// then
assertThat(result.getUri(), is(expectedUri));
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project async-http-client by AsyncHttpClient.
the class NettyRequestFactory method newNettyRequest.
public NettyRequest newNettyRequest(Request request, boolean forceConnect, ProxyServer proxyServer, Realm realm, Realm proxyRealm) {
Uri uri = request.getUri();
HttpMethod method = forceConnect ? HttpMethod.CONNECT : HttpMethod.valueOf(request.getMethod());
boolean connect = method == HttpMethod.CONNECT;
HttpVersion httpVersion = HttpVersion.HTTP_1_1;
String requestUri = requestUri(uri, proxyServer, connect);
NettyBody body = body(request, connect);
HttpRequest httpRequest;
NettyRequest nettyRequest;
if (body instanceof NettyDirectBody) {
ByteBuf buf = NettyDirectBody.class.cast(body).byteBuf();
httpRequest = new DefaultFullHttpRequest(httpVersion, method, requestUri, buf);
// body is passed as null as it's written directly with the request
nettyRequest = new NettyRequest(httpRequest, null);
} else if (body == null) {
httpRequest = new DefaultFullHttpRequest(httpVersion, method, requestUri, Unpooled.EMPTY_BUFFER);
nettyRequest = new NettyRequest(httpRequest, null);
} else {
httpRequest = new DefaultHttpRequest(httpVersion, method, requestUri);
nettyRequest = new NettyRequest(httpRequest, body);
}
HttpHeaders headers = httpRequest.headers();
if (connect) {
// assign proxy-auth as configured on request
headers.set(PROXY_AUTHORIZATION, request.getHeaders().getAll(PROXY_AUTHORIZATION));
} else {
// assign headers as configured on request
headers.set(request.getHeaders());
if (isNonEmpty(request.getCookies()))
headers.set(COOKIE, ClientCookieEncoder.STRICT.encode(request.getCookies()));
String userDefinedAcceptEncoding = headers.get(ACCEPT_ENCODING);
if (userDefinedAcceptEncoding != null) {
// we don't support Brotly ATM
if (userDefinedAcceptEncoding.endsWith(BROTLY_ACCEPT_ENCODING_SUFFIX)) {
headers.set(ACCEPT_ENCODING, userDefinedAcceptEncoding.subSequence(0, userDefinedAcceptEncoding.length() - BROTLY_ACCEPT_ENCODING_SUFFIX.length()));
}
} else if (config.isCompressionEnforced()) {
headers.set(ACCEPT_ENCODING, GZIP_DEFLATE);
}
}
if (body != null) {
if (body.getContentLength() < 0)
headers.set(TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
else
headers.set(CONTENT_LENGTH, body.getContentLength());
if (body.getContentType() != null)
headers.set(CONTENT_TYPE, body.getContentType());
}
// connection header and friends
if (!connect && uri.isWebSocket()) {
//
headers.set(UPGRADE, HttpHeaderValues.WEBSOCKET).set(CONNECTION, //
HttpHeaderValues.UPGRADE).set(ORIGIN, //
"http://" + uri.getHost() + ":" + uri.getExplicitPort()).set(SEC_WEBSOCKET_KEY, //
getKey()).set(SEC_WEBSOCKET_VERSION, "13");
} else if (!headers.contains(CONNECTION)) {
CharSequence connectionHeaderValue = connectionHeader(config.isKeepAlive(), httpVersion);
if (connectionHeaderValue != null)
headers.set(CONNECTION, connectionHeaderValue);
}
if (!headers.contains(HOST))
headers.set(HOST, hostHeader(request, uri));
// don't override authorization but append
addAuthorizationHeader(headers, perRequestAuthorizationHeader(realm));
// only set proxy auth on request over plain HTTP, or when performing CONNECT
if (!uri.isSecured() || connect) {
setProxyAuthorizationHeader(headers, perRequestProxyAuthorizationHeader(proxyRealm));
}
// Add default accept headers
if (!headers.contains(ACCEPT))
headers.set(ACCEPT, "*/*");
// Add default user agent
if (!headers.contains(USER_AGENT) && config.getUserAgent() != null)
headers.set(USER_AGENT, config.getUserAgent());
return nettyRequest;
}
Aggregations