oblac / jodd

Jodd! Lightweight. Java. Zero dependencies. Use what you like.

Home Page:https://jodd.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HttpBrowser will lose when using header to set cookie!

xun404 opened this issue · comments

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]));
	}
}

Thank you @xun404!!!

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

Nice find!