Beispiel #1
0
 private void writeMethod(final ByteBuf buf, final AsciiString method) {
   if (method.equals(HttpMethod.GET.asciiName())) {
     Hpack.writeIndexedHeaderField(buf, 2);
   } else if (method.equals(HttpMethod.POST.asciiName())) {
     Hpack.writeIndexedHeaderField(buf, 3);
   } else {
     writeLiteralHeaderFieldIncrementalIndexingIndexedName(buf, 2);
     writeString(buf, method);
   }
 }
  public static final class Methods {
    public static final String CONNECT = HttpMethod.CONNECT.name();
    public static final String DELETE = HttpMethod.DELETE.name();
    public static final String GET = HttpMethod.GET.name();
    public static final String HEAD = HttpMethod.HEAD.name();
    public static final String OPTIONS = HttpMethod.OPTIONS.name();
    public static final String PATCH = HttpMethod.PATCH.name();
    public static final String POST = HttpMethod.POST.name();
    public static final String PUT = HttpMethod.PUT.name();
    public static final String TRACE = HttpMethod.TRACE.name();

    private Methods() {}
  }
  private static boolean isRequestTo(HttpRequest request, String uri) {
    URI decodedUri;
    try {
      decodedUri = new URI(request.getUri());
    } catch (URISyntaxException e) {
      return false;
    }

    return HttpVersion.HTTP_1_1.equals(request.getProtocolVersion())
        && USER_AGENT.equals(request.getHeader(HttpHeaders.Names.USER_AGENT))
        && HttpMethod.POST.equals(request.getMethod())
        && uri.equals(decodedUri.getPath());
  }
 @Test(groups = "contentaware", dependsOnGroups = "passthroughPost")
 public void messageEchoingFromProcessorTestCase() {
   String testValue = "Test Message";
   try {
     HttpURLConnection urlConn = TestUtil.request(baseURI, "/", HttpMethod.POST.name(), true);
     TestUtil.writeContent(urlConn, testValue);
     assertEquals(200, urlConn.getResponseCode());
     String content = TestUtil.getContent(urlConn);
     assertEquals(testValue, content);
     urlConn.disconnect();
   } catch (IOException e) {
     LOGGER.error("IO Exception occurred", e);
     assertTrue(false);
   }
 }
  @Test(groups = "contentaware", dependsOnMethods = "requestResponseStreamingFromProcessorTestCase")
  public void requestResponseTransformStreamingFromProcessorTestCase() {

    String requestValue = "<A><B><C>Test Message</C></B></A>";
    try {
      CarbonMessageProcessor carbonMessageProcessor =
          new RequestResponseTransformStreamingProcessor();
      TestUtil.updateMessageProcessor(
          carbonMessageProcessor, senderConfiguration, listenerConfiguration);
      HttpURLConnection urlConn = TestUtil.request(baseURI, "/", HttpMethod.POST.name(), true);
      TestUtil.writeContent(urlConn, requestValue);
      assertEquals(200, urlConn.getResponseCode());
      String content = TestUtil.getContent(urlConn);
      assertEquals(requestValue, content);
      urlConn.disconnect();
    } catch (IOException e) {
      LOGGER.error("IO Exception occurred", e);
      assertTrue(false);
    }
  }
 @Override
 protected void channelRead0(ChannelHandlerContext ctx, WeixinRequest request)
     throws WeixinException {
   final AesToken aesToken = request.getAesToken();
   if (aesToken == null
       || (StringUtil.isBlank(request.getSignature())
           && StringUtil.isBlank(request.getMsgSignature()))) {
     ctx.writeAndFlush(HttpUtil.createHttpResponse(BAD_REQUEST))
         .addListener(ChannelFutureListener.CLOSE);
     return;
   }
   /** 公众平台:无论Get,Post都带signature参数,当开启aes模式时带msg_signature参数 企业号:无论Get,Post都带msg_signature参数 */
   if (request.getMethod().equals(HttpMethod.GET.name())) {
     if (!StringUtil.isBlank(request.getSignature())
         && MessageUtil.signature(aesToken.getToken(), request.getTimeStamp(), request.getNonce())
             .equals(request.getSignature())) {
       ctx.write(new SingleResponse(request.getEchoStr()));
       return;
     }
     if (!StringUtil.isBlank(request.getMsgSignature())
         && MessageUtil.signature(
                 aesToken.getToken(),
                 request.getTimeStamp(),
                 request.getNonce(),
                 request.getEchoStr())
             .equals(request.getMsgSignature())) {
       ctx.write(
           new SingleResponse(
               MessageUtil.aesDecrypt(null, aesToken.getAesKey(), request.getEchoStr())));
       return;
     }
     ctx.writeAndFlush(HttpUtil.createHttpResponse(FORBIDDEN))
         .addListener(ChannelFutureListener.CLOSE);
     return;
   } else if (request.getMethod().equals(HttpMethod.POST.name())) {
     if (!StringUtil.isBlank(request.getSignature())
         && !MessageUtil.signature(aesToken.getToken(), request.getTimeStamp(), request.getNonce())
             .equals(request.getSignature())) {
       ctx.writeAndFlush(HttpUtil.createHttpResponse(FORBIDDEN))
           .addListener(ChannelFutureListener.CLOSE);
       return;
     }
     if (request.getEncryptType() == EncryptType.AES
         && !MessageUtil.signature(
                 aesToken.getToken(),
                 request.getTimeStamp(),
                 request.getNonce(),
                 request.getEncryptContent())
             .equals(request.getMsgSignature())) {
       ctx.writeAndFlush(HttpUtil.createHttpResponse(FORBIDDEN))
           .addListener(ChannelFutureListener.CLOSE);
       return;
     }
   } else {
     ctx.writeAndFlush(HttpUtil.createHttpResponse(METHOD_NOT_ALLOWED))
         .addListener(ChannelFutureListener.CLOSE);
     return;
   }
   WeixinMessageTransfer messageTransfer = MessageTransferHandler.parser(request);
   ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
   messageDispatcher.doDispatch(ctx, request, messageTransfer);
 }