本文整理汇总了Java中io.netty.handler.codec.http.HttpHeaders.get方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders.get方法的具体用法?Java HttpHeaders.get怎么用?Java HttpHeaders.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDownFileSize
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* 取下载文件的总大小
*/
public static long getDownFileSize(HttpHeaders resHeaders) {
String contentRange = resHeaders.get(HttpHeaderNames.CONTENT_RANGE);
if (contentRange != null) {
Pattern pattern = Pattern.compile("^[^\\d]*(\\d+)-(\\d+)/.*$");
Matcher matcher = pattern.matcher(contentRange);
if (matcher.find()) {
long startSize = Long.parseLong(matcher.group(1));
long endSize = Long.parseLong(matcher.group(2));
return endSize - startSize + 1;
}
} else {
String contentLength = resHeaders.get(HttpHeaderNames.CONTENT_LENGTH);
if (contentLength != null) {
return Long.valueOf(resHeaders.get(HttpHeaderNames.CONTENT_LENGTH));
}
}
return 0;
}
示例2: determineCharsetFromContentType
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* @param headers
* The headers containing the content-type header to parse
* @param def
* The default charset to use if one wasn't found in the headers
*
* @return The encoding specified in the header or the default Charset if not specified.
**/
public static Charset determineCharsetFromContentType(HttpHeaders headers, Charset def) {
if (headers == null)
return def;
String contentTypeHeader = headers.get(HttpHeaders.Names.CONTENT_TYPE);
if (contentTypeHeader == null)
return def;
String charset;
Matcher m = CONTENT_TYPE_CHARSET_EXTRACTOR_PATTERN.matcher(contentTypeHeader);
if (m.find()) {
charset = m.group(1).trim().toUpperCase();
try {
return Charset.forName(charset);
}
catch (Exception ex) {
throw new InvalidCharsetInContentTypeHeaderException("Invalid charset in Content-Type header", ex,
contentTypeHeader);
}
}
return def;
}
示例3: preCall
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public boolean preCall(HttpRequest request, HttpResponder responder, ServiceMethodInfo serviceMethodInfo) {
HttpHeaders headers = request.headers();
if (headers != null) {
String authHeader = headers.get(HttpHeaders.Names.AUTHORIZATION);
if (authHeader != null) {
String authType = authHeader.substring(0, AUTH_TYPE_BASIC_LENGTH);
String authEncoded = authHeader.substring(AUTH_TYPE_BASIC_LENGTH).trim();
if (AUTH_TYPE_BASIC.equals(authType) && !authEncoded.isEmpty()) {
byte[] decodedByte = authEncoded.getBytes(Charset.forName("UTF-8"));
String authDecoded = new String(Base64.getDecoder().decode(decodedByte), Charset.forName("UTF-8"));
String[] authParts = authDecoded.split(":");
String username = authParts[0];
String password = authParts[1];
if (authenticate(username, password)) {
return true;
}
}
}
}
Multimap<String, String> map = ArrayListMultimap.create();
map.put(HttpHeaders.Names.WWW_AUTHENTICATE, AUTH_TYPE_BASIC);
responder.sendStatus(HttpResponseStatus.UNAUTHORIZED, map);
return false;
}
开发者ID:sagara-gunathunga,项目名称:msf4j-intro-webinar-samples,代码行数:27,代码来源:AbstractBasicAuthInterceptor.java
示例4: afterResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse,
HttpProxyInterceptPipeline pipeline) throws Exception {
boolean downFlag = false;
if ((httpResponse.status().code() + "").indexOf("20") == 0) { //响应码为20x
HttpHeaders httpResHeaders = httpResponse.headers();
String accept = pipeline.getHttpRequest().headers().get(HttpHeaderNames.ACCEPT);
String contentType = httpResHeaders.get(HttpHeaderNames.CONTENT_TYPE);
if (accept != null
&& accept.matches("^.*text/html.*$") //直接url的方式访问不是以HTML标签加载的(a标签除外)
&& contentType != null
&& !contentType.matches("^.*text/.*$")) { //响应体不是text/html报文
//有两种情况进行下载 1.url后缀为.xxx 2.带有CONTENT_DISPOSITION:ATTACHMENT响应头
String disposition = httpResHeaders.get(HttpHeaderNames.CONTENT_DISPOSITION);
if (pipeline.getHttpRequest().uri().matches("^.*\\.[^./]{1,5}(\\?[^?]*)?$")
|| (disposition != null && disposition.contains(HttpHeaderValues.ATTACHMENT))) {
downFlag = true;
}
}
HttpRequestInfo httpRequestInfo = (HttpRequestInfo) pipeline.getHttpRequest();
if (downFlag) { //如果是下载
proxyChannel.close();//关闭嗅探下载连接
HttpDownServer.LOGGER.debug("=====================下载===========================\n" +
pipeline.getHttpRequest().toString() + "\n" +
httpResponse.toString() + "\n" +
"================================================");
//原始的请求协议
httpRequestInfo.setRequestProto(pipeline.getRequestProto());
pipeline.afterResponse(clientChannel, proxyChannel, httpResponse);
} else {
if (httpRequestInfo.content() != null) {
httpRequestInfo.setContent(null);
}
}
}
pipeline.getDefault().afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
}
示例5: checkHeaderDiffer
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private void checkHeaderDiffer(HttpHeaders headers1, HttpHeaders headers2,
List<String> mh) {
for (String headerName : headers1.names()) {
if (!s_vaStrict
&& s_tolerantHeaders.contains(headerName.toUpperCase())) {
continue;
}
String v1 = headers1.get(headerName);
String v2 = headers2.get(headerName);
if (!v1.equals(v2)) {
mh.add(headerName);
}
}
}
示例6: getClientVersion
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Get the value of the X-Cerberus-Client header or "Unknown" if not found.
*/
public static String getClientVersion(RequestInfo request) {
final HttpHeaders headers = request.getHeaders();
if (headers != null) {
String value = headers.get(HEADER_X_CERBERUS_CLIENT);
if (value != null) {
return value;
}
}
return UNKNOWN;
}
示例7: getXForwardedClientIp
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Get the first IP address from the Http header "X-Forwarded-For"
*
* E.g. "X-Forwarded-For: ip1, ip2, ip3" would return "ip1"
*/
public static String getXForwardedClientIp(RequestInfo request) {
final HttpHeaders headers = request.getHeaders();
if (headers != null) {
String value = headers.get(HEADER_X_FORWARDED_FOR);
if (value != null) {
return StringUtils.substringBefore(value, ",").trim();
}
}
return UNKNOWN;
}
示例8: getHeader
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public String getHeader(String headerName) {
HttpHeaders headers = httpRequest.headers();
if (headers == null)
return null;
return headers.get(headerName);
}
示例9: extractMimeTypeFromContentTypeHeader
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
protected String extractMimeTypeFromContentTypeHeader(HttpHeaders headers, String def) {
if (headers == null)
return def;
String contentTypeHeader = headers.get(HttpHeaders.Names.CONTENT_TYPE);
if (contentTypeHeader == null || contentTypeHeader.trim().length() == 0)
return def;
if (contentTypeHeader.contains(";"))
contentTypeHeader = contentTypeHeader.substring(0, contentTypeHeader.indexOf(";"));
return contentTypeHeader.trim();
}
示例10: extractCookies
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
public static Set<Cookie> extractCookies(HttpRequest request) {
Set<Cookie> cookies = new HashSet<>();
HttpHeaders trailingHeaders = extractTrailingHeadersIfPossible(request);
String cookieString = request.headers().get(COOKIE);
if (cookieString == null && trailingHeaders != null)
cookieString = trailingHeaders.get(COOKIE);
if (cookieString != null)
cookies.addAll(ServerCookieDecoder.LAX.decode(cookieString));
return cookies;
}
示例11: newMultipartBody
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Creates a new multipart entity containing the given parts.
*
* @param parts the parts to include.
* @param requestHeaders the request headers
* @return a MultipartBody
*/
public static MultipartBody newMultipartBody(List<Part> parts, HttpHeaders requestHeaders) {
assertNotNull(parts, "parts");
byte[] boundary;
String contentType;
String contentTypeHeader = requestHeaders.get(HttpHeaders.Names.CONTENT_TYPE);
if (isNonEmpty(contentTypeHeader)) {
int boundaryLocation = contentTypeHeader.indexOf("boundary=");
if (boundaryLocation != -1) {
// boundary defined in existing Content-Type
contentType = contentTypeHeader;
boundary = (contentTypeHeader.substring(boundaryLocation + "boundary=".length()).trim()).getBytes(US_ASCII);
} else {
// generate boundary and append it to existing Content-Type
boundary = generateBoundary();
contentType = computeContentType(contentTypeHeader, boundary);
}
} else {
boundary = generateBoundary();
contentType = computeContentType(MULTIPART_FORM_DATA, boundary);
}
List<MultipartPart<? extends Part>> multipartParts = generateMultipartParts(parts, boundary);
return new MultipartBody(multipartParts, contentType, boundary);
}
示例12: build
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public TransportRequest build(
HttpRequest request, TransportBody body, AttributeMap channelAttrs) {
HttpHeaders httpHeaders = request.headers();
// Keep track of request information
String service = httpHeaders.get(HeaderMapper.SERVICE);
channelAttrs.attr(ChannelAttributes.SERVICE).set(service);
String procedure = httpHeaders.get(HeaderMapper.PROCEDURE);
channelAttrs.attr(ChannelAttributes.PROCEDURE).set(procedure);
String caller = httpHeaders.get(HeaderMapper.CALLER);
channelAttrs.attr(ChannelAttributes.CALLER).set(caller);
// Keep track of when the request started
Instant start = Instant.now();
channelAttrs.attr(ChannelAttributes.REQUEST_START).set(start);
Instant deadline =
createDeadline(start, service, procedure, httpHeaders.get(HeaderMapper.TIMEOUT));
Span span = createSpan(start, service, procedure, caller, httpHeaders);
channelAttrs.attr(ChannelAttributes.SPAN).set(span);
return DefaultTransportRequest.builder()
.service(service)
.procedure(procedure)
.deadline(deadline)
.caller(caller)
.encoding(httpHeaders.get(HeaderMapper.ENCODING))
.shardKey(httpHeaders.get(HeaderMapper.SHARD_KEY))
.routingKey(httpHeaders.get(HeaderMapper.ROUTING_KEY))
.routingDelegate(httpHeaders.get(HeaderMapper.ROUTING_DELEGATE))
.headers(HeaderMapper.fromHttpHeaders(httpHeaders))
.span(span)
.body(body)
.build();
}
示例13: removeSDCHEncoding
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Remove sdch from encodings we accept since we can't decode it.
*
* @param headers
* The headers to modify
*/
private void removeSDCHEncoding(HttpHeaders headers) {
String ae = headers.get(HttpHeaders.Names.ACCEPT_ENCODING);
if (StringUtils.isNotBlank(ae)) {
//
String noSdch = ae.replace(",sdch", "").replace("sdch", "");
headers.set(HttpHeaders.Names.ACCEPT_ENCODING, noSdch);
LOG.debug("Removed sdch and inserted: {}", noSdch);
}
}
示例14: switchProxyConnectionHeader
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Switch the de-facto standard "Proxy-Connection" header to "Connection"
* when we pass it along to the remote host. This is largely undocumented
* but seems to be what most browsers and servers expect.
*
* @param headers
* The headers to modify
*/
private void switchProxyConnectionHeader(HttpHeaders headers) {
String proxyConnectionKey = "Proxy-Connection";
if (headers.contains(proxyConnectionKey)) {
String header = headers.get(proxyConnectionKey);
headers.remove(proxyConnectionKey);
headers.set(HttpHeaders.Names.CONNECTION, header);
}
}
示例15: getIP
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
public static String getIP(HttpHeaders headers) {
String[] ips = proxyIP(headers);
if (ips.length > 0 && !"".equals(ips[0])) {
return ips[0].split(":")[0];
}
CharSequence realIpChar = headers.get(X_REAL_IP);
if (realIpChar != null) {
String[] realIp = realIpChar.toString().split(":");
if (realIp.length > 0 && !"[".equals(realIp[0])) {
return realIp[0];
}
}
return "127.0.0.1";
}