private Realm kerberosChallenge(
      List<String> proxyAuth,
      Request request,
      ProxyServer proxyServer,
      FluentCaseInsensitiveStringsMap headers,
      Realm realm,
      NettyResponseFuture<?> future)
      throws NTLMEngineException {

    URI uri = request.getURI();
    String host =
        request.getVirtualHost() == null
            ? AsyncHttpProviderUtils.getHost(uri)
            : request.getVirtualHost();
    String server = proxyServer == null ? host : proxyServer.getHost();
    try {
      String challengeHeader = SpnegoEngine.instance().generateToken(server);
      headers.remove(HttpHeaders.Names.AUTHORIZATION);
      headers.add(HttpHeaders.Names.AUTHORIZATION, "Negotiate " + challengeHeader);

      return newRealmBuilder(realm) //
          .setUri(uri.getRawPath()) //
          .setMethodName(request.getMethod()) //
          .setScheme(Realm.AuthScheme.KERBEROS) //
          .build();

    } catch (Throwable throwable) {
      if (isNTLM(proxyAuth)) {
        return ntlmChallenge(proxyAuth, request, proxyServer, headers, realm, future);
      }
      channels.abort(future, throwable);
      return null;
    }
  }
Example #2
0
    private URI toURI(boolean encode) {

      if (originalUri == null) {
        logger.debug("setUrl hasn't been invoked. Using http://localhost");
        originalUri = DEFAULT_REQUEST_URL;
      }

      AsyncHttpProviderUtils.validateSupportedScheme(originalUri);

      StringBuilder builder = new StringBuilder();
      builder.append(originalUri.getScheme()).append("://").append(originalUri.getAuthority());
      if (isNonEmpty(originalUri.getRawPath())) {
        builder.append(originalUri.getRawPath());
      } else {
        builder.append("/");
      }

      if (isNonEmpty(queryParams)) {

        builder.append("?");

        for (Iterator<Entry<String, List<String>>> i = queryParams.iterator(); i.hasNext(); ) {
          Entry<String, List<String>> param = i.next();
          String name = param.getKey();
          for (Iterator<String> j = param.getValue().iterator(); j.hasNext(); ) {
            String value = j.next();
            if (encode) {
              UTF8UrlEncoder.appendEncoded(builder, name);
            } else {
              builder.append(name);
            }
            if (value != null) {
              builder.append('=');
              if (encode) {
                UTF8UrlEncoder.appendEncoded(builder, value);
              } else {
                builder.append(value);
              }
            }
            if (j.hasNext()) {
              builder.append('&');
            }
          }
          if (i.hasNext()) {
            builder.append('&');
          }
        }
      }

      return URI.create(builder.toString());
    }
  private Charset getCharset(final String charset) {

    String charsetLocal = charset;

    if (charsetLocal == null) {
      String contentType = getContentType();
      if (contentType != null) {
        charsetLocal = AsyncHttpProviderUtils.parseCharset(contentType);
      }
    }

    if (charsetLocal == null) {
      charsetLocal = Charsets.DEFAULT_CHARACTER_ENCODING;
    }

    return Charsets.lookupCharset(charsetLocal);
  }
 private void computeRequestCharset() {
   if (request.charset == null) {
     try {
       final String contentType = request.headers.getFirstValue("Content-Type");
       if (contentType != null) {
         final String charset = AsyncHttpProviderUtils.parseCharset(contentType);
         if (charset != null) {
           // ensure that if charset is provided with the Content-Type header,
           // we propagate that down to the charset of the Request object
           request.charset = charset;
         }
       }
     } catch (Throwable e) {
       // NoOp -- we can't fix the Content-Type or charset from here
     }
   }
 }
 public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
   // should be fine; except that it may split multi-byte chars (last char may become '?')
   charset = calculateCharset(charset);
   byte[] b = AsyncHttpProviderUtils.contentToBytes(bodyParts, maxLength);
   return new String(b, charset);
 }