oblac / jodd

Jodd! Lightweight. Java. Zero dependencies. Use what you like.
https://jodd.org
BSD 2-Clause "Simplified" License
4.06k stars 723 forks source link

HttpBrowser will lose when using header to set cookie! #722

Closed xun404 closed 5 years ago

xun404 commented 5 years ago

HttpBrowser() has a problem with the cookie update mechanism. If the cookie is set by the header(), the information of the set cookie cannot be correctly passed down after a redirect occurs.

/jodd/jodd-http/src/main/java/jodd/http/HttpBrowser.java#addCookies

fix:

/**
* Add cookies to the request.
*/
protected void addCookies(final HttpRequest httpRequest) {
    // prepare all cookies
    List<Cookie> cookiesList = new ArrayList<>();

    if (StringUtil.isNotBlank(httpRequest.header("cookie"))) {
        for (String cv: httpRequest.header("cookie").split(";")) {
            Cookie cookie = new Cookie(cv);
            cookies.set(cookie.getName(),cookie);
        }
    }

    if (!cookies.isEmpty()) {
        for (Map.Entry<String, Cookie> cookieEntry : cookies) {
            cookiesList.add(cookieEntry.getValue());
        }

        httpRequest.cookies(cookiesList.toArray(new Cookie[0]));
    }
}
igr commented 5 years ago

Thank you @xun404!!!

See 6418ea7 for additional changes, I extracted cookie parsing from the headers in cookies() method.

Nice find!