コード例 #1
1
ファイル: DefaultRequest.java プロジェクト: erdi/ratpack
  public String oneCookie(String name) {
    Cookie found = null;
    List<Cookie> allFound = null;
    for (Cookie cookie : getCookies()) {
      if (cookie.name().equals(name)) {
        if (found == null) {
          found = cookie;
        } else if (allFound == null) {
          allFound = new ArrayList<>(2);
          allFound.add(found);
        } else {
          allFound.add(cookie);
        }
      }
    }

    if (found == null) {
      return null;
    } else if (allFound != null) {
      StringBuilder s =
          new StringBuilder("Multiple cookies with name '").append(name).append("': ");
      int i = 0;
      for (Cookie cookie : allFound) {
        s.append(cookie.toString());
        if (++i < allFound.size()) {
          s.append(", ");
        }
      }

      throw new IllegalStateException(s.toString());
    } else {
      return found.value();
    }
  }
コード例 #2
0
 private void checkSession(Channel channel) {
   String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
   if (cookieString != null) {
     Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
     if (!cookies.isEmpty()) {
       for (Cookie elt : cookies) {
         if (elt.name().equalsIgnoreCase(R66SESSION + Configuration.configuration.getHOST_ID())) {
           logger.debug("Found session: " + elt);
           admin = elt;
           R66Session session = sessions.get(admin.value());
           if (session != null) {
             authentHttp = session;
             authentHttp.setStatus(73);
           } else {
             admin = null;
             continue;
           }
         } else if (elt.name().equalsIgnoreCase(I18NEXT)) {
           logger.debug("Found i18next: " + elt);
           lang = elt.value();
         }
       }
     }
   }
   if (admin == null) {
     logger.debug("NoSession: " + uriRequest + ":{}", admin);
   }
 }
コード例 #3
0
 private void handleCookies(HttpResponse response) {
   String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
   boolean i18nextFound = false;
   if (cookieString != null) {
     Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
     if (!cookies.isEmpty()) {
       // Reset the sessions if necessary.
       boolean findSession = false;
       for (Cookie cookie : cookies) {
         if (cookie
             .name()
             .equalsIgnoreCase(R66SESSION + Configuration.configuration.getHOST_ID())) {
           if (newSession) {
             findSession = false;
           } else {
             findSession = true;
             response
                 .headers()
                 .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
           }
         } else if (cookie.name().equalsIgnoreCase(I18NEXT)) {
           i18nextFound = true;
           cookie.setValue(lang);
           response
               .headers()
               .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
         } else {
           response
               .headers()
               .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
         }
       }
       if (!i18nextFound) {
         Cookie cookie = new DefaultCookie(I18NEXT, lang);
         response
             .headers()
             .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
       }
       newSession = false;
       if (!findSession) {
         if (admin != null) {
           response
               .headers()
               .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(admin));
           logger.debug("AddSession: " + uriRequest + ":{}", admin);
         }
       }
     }
   } else {
     Cookie cookie = new DefaultCookie(I18NEXT, lang);
     response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
     if (admin != null) {
       logger.debug("AddSession: " + uriRequest + ":{}", admin);
       response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(admin));
     }
   }
 }
コード例 #4
0
 private String getSessionId(FullHttpRequest req) {
   final Set<Cookie> cookies = NettyHttpRequest.getNettyCookies(req);
   if (cookies != null) {
     for (final Cookie c : cookies) {
       if (c != null && SESSION_COOKIE_KEY.equals(c.name())) return c.value();
     }
   }
   return null;
 }
コード例 #5
0
 private Map<String, List<Cookie>> readCookie(final HttpRequest httpRequest) {
   final Map<String, List<Cookie>> cookiesDownload = new HashMap<>();
   final String cookieString = httpRequest.headers().get(HttpHeaders.Names.COOKIE);
   if (cookieString != null) {
     for (Cookie cookie : ServerCookieDecoder.STRICT.decode(cookieString)) {
       if (cookiesDownload.containsKey(cookie.name())) {
         cookiesDownload.get(cookie.name()).add(cookie);
       } else {
         cookiesDownload.put(cookie.name(), new ArrayList<>(Arrays.asList(cookie)));
       }
     }
   }
   return cookiesDownload;
 }
コード例 #6
0
 private void clearSession() {
   if (admin != null) {
     R66Session lsession = sessions.remove(admin.value());
     admin = null;
     if (lsession != null) {
       lsession.setStatus(75);
       lsession.clear();
     }
   }
 }
コード例 #7
0
 @Override
 public Collection<Cookie> getRequestCookies() {
   final List<Cookie> newCookies = new ArrayList<>();
   final Set<io.netty.handler.codec.http.cookie.Cookie> cookies = request.getCookies();
   for (final io.netty.handler.codec.http.cookie.Cookie cookie : cookies) {
     final Cookie newCookie = new Cookie(cookie.name(), cookie.value());
     newCookie.setDomain(cookie.domain());
     newCookie.setPath(cookie.path());
     newCookie.setMaxAge((int) cookie.maxAge());
     newCookie.setSecure(cookie.isSecure());
     newCookie.setHttpOnly(cookie.isHttpOnly());
     newCookies.add(newCookie);
   }
   return newCookies;
 }
コード例 #8
0
 private io.netty.handler.codec.http.cookie.Cookie getNettyCookie(Cookie c) {
   io.netty.handler.codec.http.cookie.Cookie ret =
       new io.netty.handler.codec.http.cookie.DefaultCookie(c.getName(), c.getValue());
   ret.setDomain(c.getDomain());
   ret.setHttpOnly(c.isHttpOnly());
   ret.setMaxAge(c.getMaxAge());
   ret.setPath(c.getPath());
   ret.setSecure(c.isSecure());
   return ret;
 }
コード例 #9
0
 private void checkAuthent(ChannelHandlerContext ctx) {
   newSession = true;
   if (request.method() == HttpMethod.GET) {
     String logon = Logon();
     logon = logon.replaceAll(REPLACEMENT.XXXERRORMESGXXX.toString(), "");
     responseContent.append(logon);
     clearSession();
     writeResponse(ctx);
     return;
   } else if (request.method() == HttpMethod.POST) {
     getParams();
     if (params == null) {
       String logon = Logon();
       logon =
           logon.replaceAll(
               REPLACEMENT.XXXERRORMESGXXX.toString(),
               Messages.getString("HttpSslHandler.EmptyLogin"));
       responseContent.append(logon);
       clearSession();
       writeResponse(ctx);
       return;
     }
   }
   boolean getMenu = false;
   if (params.containsKey("Logon")) {
     String name = null, password = null;
     List<String> values = null;
     if (!params.isEmpty()) {
       // get values
       if (params.containsKey("name")) {
         values = params.get("name");
         if (values != null) {
           name = values.get(0);
           if (name == null || name.isEmpty()) {
             getMenu = true;
           }
         }
       } else {
         getMenu = true;
       }
       // search the nb param
       if ((!getMenu) && params.containsKey("passwd")) {
         values = params.get("passwd");
         if (values != null) {
           password = values.get(0);
           if (password == null || password.isEmpty()) {
             getMenu = true;
           } else {
             getMenu = false;
           }
         } else {
           getMenu = true;
         }
       } else {
         getMenu = true;
       }
     } else {
       getMenu = true;
     }
     if (!getMenu) {
       logger.debug(
           "Name="
               + name
               + " vs "
               + name.equals(Configuration.configuration.getADMINNAME())
               + " Passwd vs "
               + Arrays.equals(
                   password.getBytes(WaarpStringUtils.UTF8),
                   Configuration.configuration.getSERVERADMINKEY()));
       if (name.equals(Configuration.configuration.getADMINNAME())
           && Arrays.equals(
               password.getBytes(WaarpStringUtils.UTF8),
               Configuration.configuration.getSERVERADMINKEY())) {
         authentHttp
             .getAuth()
             .specialNoSessionAuth(true, Configuration.configuration.getHOST_ID());
         authentHttp.setStatus(70);
       } else {
         getMenu = true;
       }
       if (!authentHttp.isAuthenticated()) {
         authentHttp.setStatus(71);
         logger.debug("Still not authenticated: {}", authentHttp);
         getMenu = true;
       }
     }
   } else {
     getMenu = true;
   }
   if (getMenu) {
     String logon = Logon();
     logon =
         logon.replaceAll(
             REPLACEMENT.XXXERRORMESGXXX.toString(),
             Messages.getString("HttpSslHandler.BadLogin"));
     responseContent.append(logon);
     clearSession();
     writeResponse(ctx);
   } else {
     String index = index();
     responseContent.append(index);
     clearSession();
     admin =
         new DefaultCookie(
             R66SESSION + Configuration.configuration.getHOST_ID(),
             Configuration.configuration.getHOST_ID() + Long.toHexString(random.nextLong()));
     sessions.put(admin.value(), this.authentHttp);
     authentHttp.setStatus(72);
     logger.debug("CreateSession: " + uriRequest + ":{}", admin);
     writeResponse(ctx);
   }
 }
コード例 #10
0
ファイル: WebSite.java プロジェクト: Gadreel/divconq
  public Cookie resolveLocale(HttpContext context, UserContext usr, OperationContextBuilder ctxb) {
    if (this.locales.size() == 0) {
      // make sure we have at least 1 locale listed for the site
      String lvalue = this.getDefaultLocale();

      // add the list of locales supported for this site
      this.locales.put(lvalue, this.getLocaleDefinition(lvalue));
    }

    LocaleDefinition locale = null;

    // see if the path indicates a language
    CommonPath path = context.getRequest().getPath();

    if (path.getNameCount() > 0) {
      String lvalue = path.getName(0);

      locale = this.locales.get(lvalue);

      // extract the language from the path
      if (locale != null) context.getRequest().setPath(path.subpath(1));
    }

    // but respect the cookie if it matches something though
    Cookie langcookie = context.getRequest().getCookie("dcLang");

    if (locale == null) {
      if (langcookie != null) {
        String lvalue = langcookie.value();

        // if everything checks out set the op locale and done
        if (this.locales.containsKey(lvalue)) {
          ctxb.withOperatingLocale(lvalue);
          return null;
        }

        locale = this.getLocaleDefinition(lvalue);

        // use language if variant - still ok and done
        if (locale.hasVariant()) {
          if (this.locales.containsKey(locale.getLanguage())) {
            ctxb.withOperatingLocale(
                lvalue); // keep the variant part, it may be used in places on site - supporting a
                         // lang implicitly allows all variants
            return null;
          }
        }

        // otherwise ignore the cookie, will replace it
      }
    }

    // see if the domain is set for a specific language
    if (locale == null) {
      String domain = context.getRequest().getHeader("Host");

      if (domain.indexOf(':') > -1) domain = domain.substring(0, domain.indexOf(':'));

      locale = this.domainlocales.get(domain);
    }

    // see if the user has a preference
    if (locale == null) {
      String lvalue = usr.getLocale();

      if (StringUtil.isNotEmpty(lvalue)) locale = this.locales.get(lvalue);
    }

    // if we find any locale at all then to see if it is the default
    // if not use it, else use the default
    if ((locale != null) && !locale.equals(this.getDefaultLocaleDefinition())) {
      ctxb.withOperatingLocale(locale.getName());
      return new DefaultCookie("dcLang", locale.getName());
    }

    // clear the cookie if we are to use default locale
    if (langcookie != null) return new DefaultCookie("dcLang", "");

    // we are using default locale, nothing more to do
    return null;
  }