Ejemplo n.º 1
0
  protected static void addToRequest(HttpRequest nettyRequest, Request request) {
    for (String key : nettyRequest.getHeaderNames()) {
      Http.Header hd = new Http.Header();
      hd.name = key.toLowerCase();
      hd.values = new ArrayList<String>();
      for (String next : nettyRequest.getHeaders(key)) {
        hd.values.add(next);
      }
      request.headers.put(hd.name, hd);
    }

    String value = nettyRequest.getHeader(COOKIE);
    if (value != null) {
      Set<Cookie> cookies = new CookieDecoder().decode(value);
      if (cookies != null) {
        for (Cookie cookie : cookies) {
          Http.Cookie playCookie = new Http.Cookie();
          playCookie.name = cookie.getName();
          playCookie.path = cookie.getPath();
          playCookie.domain = cookie.getDomain();
          playCookie.secure = cookie.isSecure();
          playCookie.value = cookie.getValue();
          playCookie.httpOnly = cookie.isHttpOnly();
          request.cookies.put(playCookie.name, playCookie);
        }
      }
    }
  }
Ejemplo n.º 2
0
 @Override
 public void addResponseCookie(final Cookie cookie) {
   javax.servlet.http.Cookie c =
       new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue());
   c.setSecure(cookie.isSecure());
   c.setPath(cookie.getPath());
   c.setMaxAge(cookie.getMaxAge());
   c.setHttpOnly(cookie.isHttpOnly());
   c.setComment(cookie.getComment());
   c.setDomain(cookie.getDomain());
   this.response.addCookie(c);
 }
Ejemplo n.º 3
0
 private String getCookies(HttpConnection req, String path) {
   String host = req.getHost();
   if (path == null) path = "/";
   boolean includeSecure = req instanceof HttpsConnection;
   String res = "";
   for (int i = 0; i < savedCookies.size(); ++i) {
     Cookie c = (Cookie) savedCookies.elementAt(i);
     if (c.match(host, path) && (includeSecure || !c.isSecure())) {
       res += c.toString() + "; ";
       Log.logDebug("Sending Cookie: " + c.getName());
     }
   }
   return res;
 }
Ejemplo n.º 4
0
  /**
   * the method called by the DefaultCookiePolicyHandler.
   *
   * @return true if the cookie should be accepted
   */
  public synchronized boolean accept(Cookie cookie, DefaultCookiePolicyHandler h, String server) {
    // set the new values

    name_value_label.setText(cookie.getName() + "=" + cookie.getValue());
    domain_value.setText(cookie.getDomain());
    path_value.setText(cookie.getPath());
    if (cookie.expires() == null) expires_value.setText("never");
    else expires_value.setText(cookie.expires().toString());
    int pos = 2;
    if (cookie.isSecure()) add(secure_note, constr, pos++);
    if (cookie.discard()) add(discard_note, constr, pos++);

    if (cookie instanceof Cookie2) {
      Cookie2 cookie2 = (Cookie2) cookie;

      // set ports list
      if (cookie2.getPorts() != null) {
        ((GridLayout) left_panel.getLayout()).setRows(5);
        left_panel.add(ports_label, 2);
        ((GridLayout) right_panel.getLayout()).setRows(5);
        int[] ports = cookie2.getPorts();
        StringBuffer plist = new StringBuffer();
        plist.append(ports[0]);
        for (int idx = 1; idx < ports.length; idx++) {
          plist.append(", ");
          plist.append(ports[idx]);
        }
        ports_value.setText(plist.toString());
        right_panel.add(ports_value, 2);
      }

      // set comment url
      if (cookie2.getCommentURL() != null) {
        c_url_note.setText("For more info on this cookie see: " + cookie2.getCommentURL());
        add(c_url_note, constr, pos++);
      }

      // set comment
      if (cookie2.getComment() != null) {
        comment_value.setText(cookie2.getComment());
        add(comment_label, constr, pos++);
        add(comment_value, constr, pos++);
      }
    }

    // invalidate all labels, so that new values are displayed correctly

    name_value_label.invalidate();
    domain_value.invalidate();
    ports_value.invalidate();
    path_value.invalidate();
    expires_value.invalidate();
    left_panel.invalidate();
    right_panel.invalidate();
    secure_note.invalidate();
    discard_note.invalidate();
    c_url_note.invalidate();
    comment_value.invalidate();
    invalidate();

    // set default domain test

    domain.setText(cookie.getDomain());

    // display

    setResizable(true);
    pack();
    setResizable(false);
    setLocation(
        (screen.width - getPreferredSize().width) / 2,
        (int) ((screen.height - getPreferredSize().height) / 2 * .7));
    setVisible(true);
    default_focus.requestFocus();

    // wait for user input

    try {
      wait();
    } catch (InterruptedException e) {
    }

    setVisible(false);

    // reset popup

    remove(secure_note);
    remove(discard_note);
    left_panel.remove(ports_label);
    ((GridLayout) left_panel.getLayout()).setRows(4);
    right_panel.remove(ports_value);
    ((GridLayout) right_panel.getLayout()).setRows(4);
    remove(c_url_note);
    remove(comment_label);
    remove(comment_value);

    // handle accept/reject domain buttons

    if (accept_domain) {
      String dom = domain.getText().trim().toLowerCase();

      if (accept) h.addAcceptDomain(dom);
      else h.addRejectDomain(dom);
    }

    return accept;
  }
Ejemplo n.º 5
0
  private String encodeServerSide() {
    StringBuilder sb = new StringBuilder();

    for (Cookie cookie : cookies) {
      add(sb, cookie.getName(), cookie.getValue());

      if (cookie.getMaxAge() >= 0) {
        if (cookie.getVersion() == 0) {
          addUnquoted(
              sb,
              CookieHeaderNames.EXPIRES,
              new HttpHeaderDateFormat()
                  .format(new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L)));
        } else {
          add(sb, CookieHeaderNames.MAX_AGE, cookie.getMaxAge());
        }
      }

      if (cookie.getPath() != null) {
        if (cookie.getVersion() > 0) {
          add(sb, CookieHeaderNames.PATH, cookie.getPath());
        } else {
          addUnquoted(sb, CookieHeaderNames.PATH, cookie.getPath());
        }
      }

      if (cookie.getDomain() != null) {
        if (cookie.getVersion() > 0) {
          add(sb, CookieHeaderNames.DOMAIN, cookie.getDomain());
        } else {
          addUnquoted(sb, CookieHeaderNames.DOMAIN, cookie.getDomain());
        }
      }
      if (cookie.isSecure()) {
        sb.append(CookieHeaderNames.SECURE);
        sb.append((char) HttpConstants.SEMICOLON);
      }
      if (cookie.isHttpOnly()) {
        sb.append(CookieHeaderNames.HTTPONLY);
        sb.append((char) HttpConstants.SEMICOLON);
      }
      if (cookie.getVersion() >= 1) {
        if (cookie.getComment() != null) {
          add(sb, CookieHeaderNames.COMMENT, cookie.getComment());
        }

        add(sb, CookieHeaderNames.VERSION, 1);

        if (cookie.getCommentUrl() != null) {
          addQuoted(sb, CookieHeaderNames.COMMENTURL, cookie.getCommentUrl());
        }

        if (!cookie.getPorts().isEmpty()) {
          sb.append(CookieHeaderNames.PORT);
          sb.append((char) HttpConstants.EQUALS);
          sb.append((char) HttpConstants.DOUBLE_QUOTE);
          for (int port : cookie.getPorts()) {
            sb.append(port);
            sb.append((char) HttpConstants.COMMA);
          }
          sb.setCharAt(sb.length() - 1, (char) HttpConstants.DOUBLE_QUOTE);
          sb.append((char) HttpConstants.SEMICOLON);
        }
        if (cookie.isDiscard()) {
          sb.append(CookieHeaderNames.DISCARD);
          sb.append((char) HttpConstants.SEMICOLON);
        }
      }
    }

    if (sb.length() > 0) {
      sb.setLength(sb.length() - 1);
    }

    return sb.toString();
  }