本文整理匯總了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";
}