Пример #1
0
  @Override
  public void handle(final YokeRequest request, final Handler<Object> next) {
    // default session
    final YokeCookie cookie = new YokeCookie(name, hmacSHA256);
    cookie.setPath(path);
    cookie.setHttpOnly(httpOnly);
    cookie.setMaxAge(maxAge);

    // path validation mismatch
    if (request.path().indexOf(cookie.getPath()) != 0) {
      next.handle(null);
      return;
    }

    // find the session cookie
    final YokeCookie sessionCookie = request.getCookie(name);

    int hash = 0;

    if (sessionCookie != null) {
      // session cookies must be signed
      if (sessionCookie.isSigned()) {
        String unsigned = sessionCookie.getUnsignedValue();
        if (unsigned != null) {
          hash = crc16(unsigned);
          request.setSessionId(unsigned);
        }
      }
    }

    final int originalHash = hash;
    final YokeResponse response = request.response();

    // call us when headers are being set for the response
    response.headersHandler(
        new Handler<Void>() {
          @Override
          public void handle(Void done) {
            String sessionId = request.getSessionId();

            // removed
            if (sessionId == null) {
              if (sessionCookie != null) {
                cookie.setValue("");
                cookie.setMaxAge(0);
                response.addCookie(cookie);
              }
            } else {
              // only send secure cookies over https
              if (cookie.isSecure() && !request.isSecure()) {
                return;
              }

              // compare hashes, no need to set-cookie if unchanged
              if (originalHash != crc16(sessionId)) {
                // modified session
                cookie.setValue(sessionId);
                cookie.sign();
                response.addCookie(cookie);
              }
            }
          }
        });

    next.handle(null);
  }
Пример #2
0
  private void route(
      final YokeRequest request, final PatternBinding binding, final Handler<Object> next) {
    final Matcher m = binding.pattern.matcher(request.path());
    final Vertx vertx = vertx();

    if (m.matches()) {
      final MultiMap params = request.params();

      if (binding.paramNames != null) {
        // Named params
        new AsyncIterator<String>(binding.paramNames) {
          @Override
          public void handle(String param) {
            if (hasNext()) {
              params.set(param, m.group(param));
              final Middleware paramMiddleware = paramProcessors.get(param);
              if (paramMiddleware != null) {
                // do not block main loop
                vertx.runOnContext(
                    new Handler<Void>() {
                      @Override
                      public void handle(Void event) {
                        paramMiddleware.handle(
                            request,
                            new Handler<Object>() {
                              @Override
                              public void handle(Object err) {
                                if (err == null) {
                                  next();
                                } else {
                                  next.handle(err);
                                }
                              }
                            });
                      }
                    });
              } else {
                next();
              }
            } else {
              // middlewares
              new AsyncIterator<Middleware>(binding.middleware) {
                @Override
                public void handle(final Middleware middleware) {
                  if (hasNext()) {
                    // do not block main loop
                    vertx.runOnContext(
                        new Handler<Void>() {
                          @Override
                          public void handle(Void event) {
                            middleware.handle(
                                request,
                                new Handler<Object>() {
                                  @Override
                                  public void handle(Object err) {
                                    if (err == null) {
                                      next();
                                    } else {
                                      next.handle(err);
                                    }
                                  }
                                });
                          }
                        });
                  } else {
                    next.handle(null);
                  }
                }
              };
            }
          }
        };
      } else {
        // Un-named params
        for (int i = 0; i < m.groupCount(); i++) {
          params.set("param" + i, m.group(i + 1));
        }

        // middlewares
        new AsyncIterator<Middleware>(binding.middleware) {
          @Override
          public void handle(final Middleware middleware) {
            if (hasNext()) {
              // do not block main loop
              vertx.runOnContext(
                  new Handler<Void>() {
                    @Override
                    public void handle(Void event) {
                      middleware.handle(
                          request,
                          new Handler<Object>() {
                            @Override
                            public void handle(Object err) {
                              if (err == null) {
                                next();
                              } else {
                                next.handle(err);
                              }
                            }
                          });
                    }
                  });
            } else {
              next.handle(null);
            }
          }
        };
      }
    } else {
      next.handle(null);
    }
  }