From 9dc7e16c93af83f2bd64a55882d06a29a0652023 Mon Sep 17 00:00:00 2001 From: Katsute Date: Thu, 30 Apr 2020 17:14:52 -0400 Subject: [PATCH 1/6] Start implementation --- .../simplehttpserver/SimpleHttpCookie.java | 120 +++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java index b8eb12e..e62b6e6 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java @@ -29,6 +29,8 @@ public class SimpleHttpCookie { /** * Creates an HTTP cookie. All fields except for name, secure, httpOnly, and value can be set to null if unused. * + * @deprecated Use {@link Builder} class instead. + * * @param name name of the cookie * @param value value of the cookie * @param domain what domain to send the cookie to @@ -41,7 +43,9 @@ public class SimpleHttpCookie { * * @since 02.00.00 * @author Ktt Development + * */ + @Deprecated public SimpleHttpCookie(final String name, final String value, final String domain, final String path, final String sameSite, final Date expires, final Integer maxAge, final boolean secure, final boolean httpOnly){ if(name == null) throw new NullPointerException("Cookie name can not be null"); @@ -63,6 +67,8 @@ public SimpleHttpCookie(final String name, final String value, final String doma /** * Converts the cookie to a readable string for the cookie header. * + * @deprecated Use {@link #toCookieHeaderString()} instead. + * * @return cookie header * * @see SimpleHttpExchange#setCookie(SimpleHttpCookie) @@ -70,7 +76,7 @@ public SimpleHttpCookie(final String name, final String value, final String doma * @author Ktt Development */ @SuppressWarnings({"ConstantConditions", "SpellCheckingInspection"}) - @Override + @Override @Deprecated public final String toString(){ StringBuilder OUT = new StringBuilder(); @@ -93,4 +99,116 @@ public final String toString(){ return OUT.toString(); } + public final String toCookieHeaderString(){ + final StringBuilder OUT = new StringBuilder(); + + OUT.append(name).append("=").append(value); + if(expires != null) + OUT.append("; Expires=").append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(expires)).append(" GMT"); + if(maxAge != null) + OUT.append("; Max-Age=").append(maxAge); + if(domain != null) + OUT.append("; Domain=").append(domain); + if(path != null) + OUT.append("; Path=").append(path); + if(secure) + OUT.append("; Secure=").append(secure); + if(httpOnly) + OUT.append("; HttpOnly=").append(httpOnly); + if(sameSite != null) + OUT.append("; SameSite=").append(sameSite); + + return OUT.toString(); + } + + public static class Builder { + + private final String name; + private final String value; + + private String domain; + private String path; + private String sameSite; + private Date expires; + private int maxAge; + private boolean secure = false; + private boolean httpOnly = false; + + public Builder(final String name, final String value){ + if((this.name = name) == null) + throw new NullPointerException("Cookie name can not be null"); + if((this.value = value) == null) + throw new NullPointerException("Cookie value can not be null"); + } + + public final String getName(){ + return name; + } + + public final String getValue(){ + return value; + } + + public final String getDomain(){ + return domain; + } + + public final void setDomain(final String domain){ + this.domain = domain; + } + + public final String getPath(){ + return path; + } + + public final void setPath(final String path){ + this.path = path; + } + + public final String getSameSite(){ + return sameSite; + } + + public final void setSameSite(final String sameSite){ + this.sameSite = sameSite; + } + + public final Date getExpires(){ + return expires; + } + + public final void setExpires(final Date expires){ + this.expires = expires; + } + + public final int getMaxAge(){ + return maxAge; + } + + public final void setMaxAge(final int maxAge){ + this.maxAge = maxAge; + } + + public final boolean isSecure(){ + return secure; + } + + public final void setSecure(final boolean secure){ + this.secure = secure; + } + + public final boolean isHttpOnly(){ + return httpOnly; + } + + public final void setHttpOnly(final boolean httpOnly){ + this.httpOnly = httpOnly; + } + + public final SimpleHttpCookie build(){ + return new SimpleHttpCookie(name,value,domain,path,sameSite,expires,maxAge,secure,httpOnly); + } + + } + } From 41e918d873426edb9a081372e02769ecc7da1d34 Mon Sep 17 00:00:00 2001 From: Katsute Date: Thu, 30 Apr 2020 17:37:37 -0400 Subject: [PATCH 2/6] Added documentation --- .../simplehttpserver/SimpleHttpCookie.java | 210 +++++++++++++++++- 1 file changed, 205 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java index e62b6e6..1cefe75 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java @@ -7,8 +7,9 @@ * An HTTP Cookie to be sent in a response header. * * @see SimpleHttpExchange + * @see Builder * @since 02.00.00 - * @version 02.00.00 + * @version 02.03.00 * @author Ktt Development */ public class SimpleHttpCookie { @@ -29,7 +30,7 @@ public class SimpleHttpCookie { /** * Creates an HTTP cookie. All fields except for name, secure, httpOnly, and value can be set to null if unused. * - * @deprecated Use {@link Builder} class instead. + * @deprecated Use {@link Builder} class instead. This method will be removed in the future. * * @param name name of the cookie * @param value value of the cookie @@ -43,7 +44,6 @@ public class SimpleHttpCookie { * * @since 02.00.00 * @author Ktt Development - * */ @Deprecated public SimpleHttpCookie(final String name, final String value, final String domain, final String path, final String sameSite, final Date expires, final Integer maxAge, final boolean secure, final boolean httpOnly){ @@ -99,12 +99,22 @@ public final String toString(){ return OUT.toString(); } + private final SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); + + /** + * Converts the cookie to a readable string for a response header. + * + * @return cookie as a header string + * + * @since 02.03.00 + * @author Ktt Development + */ public final String toCookieHeaderString(){ final StringBuilder OUT = new StringBuilder(); OUT.append(name).append("=").append(value); if(expires != null) - OUT.append("; Expires=").append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(expires)).append(" GMT"); + OUT.append("; Expires=").append(sdf.format(expires)).append(" GMT"); if(maxAge != null) OUT.append("; Max-Age=").append(maxAge); if(domain != null) @@ -121,6 +131,15 @@ public final String toCookieHeaderString(){ return OUT.toString(); } + /** + * Builder class for {@link SimpleHttpCookie}. + * + * @see SimpleHttpCookie + * + * @since 02.03.00 + * @version 02.03.00 + * @author Ktt Development + */ public static class Builder { private final String name; @@ -134,6 +153,15 @@ public static class Builder { private boolean secure = false; private boolean httpOnly = false; + /** + * Creates an HTTP cookie builder given a key and value. + * + * @param name Name of the cookie + * @param value Value of the cookie + * + * @since 02.03.00 + * @author Ktt Development + */ public Builder(final String name, final String value){ if((this.name = name) == null) throw new NullPointerException("Cookie name can not be null"); @@ -141,70 +169,242 @@ public Builder(final String name, final String value){ throw new NullPointerException("Cookie value can not be null"); } + /** + * Returns the name of the cookie. + * + * @return cookie name + * + * @since 02.00.00 + * @author Ktt Development + */ public final String getName(){ return name; } + /** + * Returns the value of the cookie. + * + * @return cookie value + * + * @since 02.03.00 + * @author Ktt Development + */ public final String getValue(){ return value; } + /** + * Returns the domain to send the cookie to. + * + * @return domain to send the cookie to + * + * @see #setDomain(String) + * + * @since 02.03.00 + * @author Ktt Development + */ public final String getDomain(){ return domain; } + /** + * Sets the domain of the cookie. + * + * @param domain what domain to send the cookie to + * + * @see #getDomain() + * + * @since 02.03.00 + * @author Ktt Development + */ public final void setDomain(final String domain){ this.domain = domain; } + /** + * Returns the path to send the cookie to. + * + * @return what path to send the cookie to + * + * @see #setPath(String) + * + * @since 02.03.00 + * @author Ktt Development + */ public final String getPath(){ return path; } + /** + * Sets the path of the cookie. + * + * @param path what path to send the cookie to + * + * @see #getPath() + * + * @since 02.03.00 + * @author Ktt Development + */ public final void setPath(final String path){ this.path = path; } - public final String getSameSite(){ + /** + * Returns if the cookie should be prevented from being sent cross-site. + * + * @return if the cookie should be prevented from being sent cross-site. + * + * @see #setSameSite(String) + * + * @since 02.03.00 + * @author Ktt Development + */ + public final String isSameSite(){ return sameSite; } + /** + * Sets if the cookie should be prevented from being sent cross-site. + * + * @param sameSite if the cookie should be prevented from being sent cross-site + * + * @see #isSameSite() + * + * @since 02.03.00 + * @author Ktt Development + */ public final void setSameSite(final String sameSite){ this.sameSite = sameSite; } + /** + * Returns when the cookie should expire. + * + * @return when the cookie should expire. + * + * @see #setExpires(Date) + * @see #getMaxAge() + * @see #setMaxAge(int) + * + * @since 02.03.00 + * @author Ktt Development + */ public final Date getExpires(){ return expires; } + /** + * Sets when the cookie should expire. + * + * @param expires when the cookie should expire + * + * @see #getExpires() + * @see #getMaxAge() + * @see #setMaxAge(int) + * + * @since 02.03.00 + * @author Ktt Development + */ public final void setExpires(final Date expires){ this.expires = expires; } + /** + * Returns how long the cookie should exist for. + * + * @return how long the cookie should exist for + * + * @see #getExpires() + * @see #setExpires(Date) + * @see #setMaxAge(int) + * + * @since 02.03.00 + * @author Ktt Development + */ public final int getMaxAge(){ return maxAge; } + /** + * Sets how long the cookie should exist for. + * + * @param maxAge how long the cookie should exist for + * + * @see #getExpires() + * @see #setExpires(Date) + * @see #getMaxAge() + * + * @since 02.03.00 + * @author Ktt Development + */ public final void setMaxAge(final int maxAge){ this.maxAge = maxAge; } + /** + * Returns if the cookie must be sent over a secure/HTTPS protocol. + * + * @return if the cookie must be sent over a secure/HTTPS protocol + * + * @see #isSecure() + * + * @since 02.03.00 + * @author Ktt Development + */ public final boolean isSecure(){ return secure; } + /** + * Sets if the cookie must be sent over a secure/HTTPS protocol. + * + * @param secure if the cookie must be sent over a secure/HTTPS protocol. + * + * @see #setSecure(boolean) + * + * @since 02.03.00 + * @author Ktt Development + */ public final void setSecure(final boolean secure){ this.secure = secure; } + /** + * Returns if only the server should have access to the cookies. + * + * @return if only the server should have access to the cookies. + * + * @see #setHttpOnly(boolean) + * + * @since 02.03.00 + * @author Ktt Development + */ public final boolean isHttpOnly(){ return httpOnly; } + /** + * Sets if only the server should have access to the cookies. + * + * @param httpOnly if only the server should have access to the cookies + * + * @see #isHttpOnly() + * + * @since 02.03.00 + * @author Ktt Development + */ public final void setHttpOnly(final boolean httpOnly){ this.httpOnly = httpOnly; } + /** + * Returns the completed cookie. + * + * @return simple http cookie + * + * @since 02.03.00 + * @author Ktt Development + */ public final SimpleHttpCookie build(){ return new SimpleHttpCookie(name,value,domain,path,sameSite,expires,maxAge,secure,httpOnly); } From e2c21c82b9fd316b6cbe8fe94cc7be39abfc294a Mon Sep 17 00:00:00 2001 From: Katsute Date: Thu, 30 Apr 2020 17:47:13 -0400 Subject: [PATCH 3/6] Updated dependencies --- .../simplehttpserver/SimpleHttpCookie.java | 28 ++++++++++++++----- .../simplehttpserver/SimpleHttpExchange.java | 2 +- .../SimpleHttpExchangeImpl.java | 10 +++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java index 1cefe75..0c9e2f6 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java @@ -211,14 +211,16 @@ public final String getDomain(){ * Sets the domain of the cookie. * * @param domain what domain to send the cookie to + * @return cookie builder * * @see #getDomain() * * @since 02.03.00 * @author Ktt Development */ - public final void setDomain(final String domain){ + public final Builder setDomain(final String domain){ this.domain = domain; + return this; } /** @@ -239,14 +241,16 @@ public final String getPath(){ * Sets the path of the cookie. * * @param path what path to send the cookie to + * @return cookie builder * * @see #getPath() * * @since 02.03.00 * @author Ktt Development */ - public final void setPath(final String path){ + public final Builder setPath(final String path){ this.path = path; + return this; } /** @@ -267,14 +271,16 @@ public final String isSameSite(){ * Sets if the cookie should be prevented from being sent cross-site. * * @param sameSite if the cookie should be prevented from being sent cross-site + * @return cookie builder * * @see #isSameSite() * * @since 02.03.00 * @author Ktt Development */ - public final void setSameSite(final String sameSite){ + public final Builder setSameSite(final String sameSite){ this.sameSite = sameSite; + return this; } /** @@ -297,6 +303,7 @@ public final Date getExpires(){ * Sets when the cookie should expire. * * @param expires when the cookie should expire + * @return cookie builder * * @see #getExpires() * @see #getMaxAge() @@ -305,8 +312,9 @@ public final Date getExpires(){ * @since 02.03.00 * @author Ktt Development */ - public final void setExpires(final Date expires){ + public final Builder setExpires(final Date expires){ this.expires = expires; + return this; } /** @@ -329,6 +337,7 @@ public final int getMaxAge(){ * Sets how long the cookie should exist for. * * @param maxAge how long the cookie should exist for + * @return cookie builder * * @see #getExpires() * @see #setExpires(Date) @@ -337,8 +346,9 @@ public final int getMaxAge(){ * @since 02.03.00 * @author Ktt Development */ - public final void setMaxAge(final int maxAge){ + public final Builder setMaxAge(final int maxAge){ this.maxAge = maxAge; + return this; } /** @@ -359,14 +369,16 @@ public final boolean isSecure(){ * Sets if the cookie must be sent over a secure/HTTPS protocol. * * @param secure if the cookie must be sent over a secure/HTTPS protocol. + * @return cookie builder * * @see #setSecure(boolean) * * @since 02.03.00 * @author Ktt Development */ - public final void setSecure(final boolean secure){ + public final Builder setSecure(final boolean secure){ this.secure = secure; + return this; } /** @@ -387,14 +399,16 @@ public final boolean isHttpOnly(){ * Sets if only the server should have access to the cookies. * * @param httpOnly if only the server should have access to the cookies + * @return cookie builder * * @see #isHttpOnly() * * @since 02.03.00 * @author Ktt Development */ - public final void setHttpOnly(final boolean httpOnly){ + public final Builder setHttpOnly(final boolean httpOnly){ this.httpOnly = httpOnly; + return this; } /** diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java index e54b489..2916d6c 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java @@ -304,7 +304,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){ // /** - * Returns the client's existing cookies. + * Returns the client's existing cookies as a map of keys and values. * * @return client's cookies * diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java index 39130db..fc50a5f 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java @@ -17,7 +17,7 @@ * * @see SimpleHttpExchange * @since 02.00.00 - * @version 02.00.00 + * @version 02.03.00 * @author Ktt Development */ @SuppressWarnings("SpellCheckingInspection") @@ -311,7 +311,7 @@ public final HashMap getCookies(){ @Override public synchronized final void setCookie(final SimpleHttpCookie cookie){ - getResponseHeaders().add("Set-Cookie",cookie.toString()); + getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); } // @@ -322,7 +322,11 @@ public synchronized final HttpSession getHttpSession(){ final HttpSession session; if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){ session = HttpSession.create(); - setCookie(new SimpleHttpCookie("__session-id", session.getSessionID(), null, null, null, null, null, false, true)); + setCookie( + new SimpleHttpCookie.Builder("__sesion-id",session.getSessionID()) + .setHttpOnly(true) + .build() + ); }else{ session = HttpSession.sessions.get(sessionId); } From f322440cbb8d575c9b241bb3bf385f717dfa387a Mon Sep 17 00:00:00 2001 From: Katsute Date: Thu, 30 Apr 2020 20:16:54 -0400 Subject: [PATCH 4/6] Added setCookie shortcut; allowed boolean values to be null --- .../simplehttpserver/SimpleHttpCookie.java | 35 ++++++------------- .../simplehttpserver/SimpleHttpExchange.java | 19 ++++++++++ .../SimpleHttpExchangeImpl.java | 5 +++ 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java index 0c9e2f6..403bef4 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java @@ -23,7 +23,7 @@ public class SimpleHttpCookie { private final Date expires; private final Integer maxAge; - private final boolean + private final Boolean secure, httpOnly; @@ -46,7 +46,7 @@ public class SimpleHttpCookie { * @author Ktt Development */ @Deprecated - public SimpleHttpCookie(final String name, final String value, final String domain, final String path, final String sameSite, final Date expires, final Integer maxAge, final boolean secure, final boolean httpOnly){ + public SimpleHttpCookie(final String name, final String value, final String domain, final String path, final String sameSite, final Date expires, final Integer maxAge, final Boolean secure, final Boolean httpOnly){ if(name == null) throw new NullPointerException("Cookie name can not be null"); else @@ -78,7 +78,7 @@ public SimpleHttpCookie(final String name, final String value, final String doma @SuppressWarnings({"ConstantConditions", "SpellCheckingInspection"}) @Override @Deprecated public final String toString(){ - StringBuilder OUT = new StringBuilder(); + final StringBuilder OUT = new StringBuilder(); OUT.append(name).append("=").append(value); if(expires != null) @@ -89,9 +89,9 @@ public final String toString(){ OUT.append("; Domain=").append(domain); if(path != null) OUT.append("; Path=").append(path); - if(secure) + if(secure != null && secure) OUT.append("; Secure=").append(secure); - if(httpOnly) + if(httpOnly != null && httpOnly) OUT.append("; HttpOnly=").append(httpOnly); if(sameSite != null) OUT.append("; SameSite=").append(sameSite); @@ -114,16 +114,16 @@ public final String toCookieHeaderString(){ OUT.append(name).append("=").append(value); if(expires != null) - OUT.append("; Expires=").append(sdf.format(expires)).append(" GMT"); + OUT.append("; Expires=").append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(expires)).append(" GMT"); if(maxAge != null) OUT.append("; Max-Age=").append(maxAge); if(domain != null) OUT.append("; Domain=").append(domain); if(path != null) OUT.append("; Path=").append(path); - if(secure) + if(secure != null && secure) OUT.append("; Secure=").append(secure); - if(httpOnly) + if(httpOnly != null && httpOnly) OUT.append("; HttpOnly=").append(httpOnly); if(sameSite != null) OUT.append("; SameSite=").append(sameSite); @@ -135,7 +135,6 @@ public final String toCookieHeaderString(){ * Builder class for {@link SimpleHttpCookie}. * * @see SimpleHttpCookie - * * @since 02.03.00 * @version 02.03.00 * @author Ktt Development @@ -150,8 +149,8 @@ public static class Builder { private String sameSite; private Date expires; private int maxAge; - private boolean secure = false; - private boolean httpOnly = false; + private Boolean secure; + private Boolean httpOnly; /** * Creates an HTTP cookie builder given a key and value. @@ -199,7 +198,6 @@ public final String getValue(){ * @return domain to send the cookie to * * @see #setDomain(String) - * * @since 02.03.00 * @author Ktt Development */ @@ -214,7 +212,6 @@ public final String getDomain(){ * @return cookie builder * * @see #getDomain() - * * @since 02.03.00 * @author Ktt Development */ @@ -229,7 +226,6 @@ public final Builder setDomain(final String domain){ * @return what path to send the cookie to * * @see #setPath(String) - * * @since 02.03.00 * @author Ktt Development */ @@ -244,7 +240,6 @@ public final String getPath(){ * @return cookie builder * * @see #getPath() - * * @since 02.03.00 * @author Ktt Development */ @@ -259,7 +254,6 @@ public final Builder setPath(final String path){ * @return if the cookie should be prevented from being sent cross-site. * * @see #setSameSite(String) - * * @since 02.03.00 * @author Ktt Development */ @@ -274,7 +268,6 @@ public final String isSameSite(){ * @return cookie builder * * @see #isSameSite() - * * @since 02.03.00 * @author Ktt Development */ @@ -291,7 +284,6 @@ public final Builder setSameSite(final String sameSite){ * @see #setExpires(Date) * @see #getMaxAge() * @see #setMaxAge(int) - * * @since 02.03.00 * @author Ktt Development */ @@ -308,7 +300,6 @@ public final Date getExpires(){ * @see #getExpires() * @see #getMaxAge() * @see #setMaxAge(int) - * * @since 02.03.00 * @author Ktt Development */ @@ -325,7 +316,6 @@ public final Builder setExpires(final Date expires){ * @see #getExpires() * @see #setExpires(Date) * @see #setMaxAge(int) - * * @since 02.03.00 * @author Ktt Development */ @@ -342,7 +332,6 @@ public final int getMaxAge(){ * @see #getExpires() * @see #setExpires(Date) * @see #getMaxAge() - * * @since 02.03.00 * @author Ktt Development */ @@ -357,7 +346,6 @@ public final Builder setMaxAge(final int maxAge){ * @return if the cookie must be sent over a secure/HTTPS protocol * * @see #isSecure() - * * @since 02.03.00 * @author Ktt Development */ @@ -372,7 +360,6 @@ public final boolean isSecure(){ * @return cookie builder * * @see #setSecure(boolean) - * * @since 02.03.00 * @author Ktt Development */ @@ -387,7 +374,6 @@ public final Builder setSecure(final boolean secure){ * @return if only the server should have access to the cookies. * * @see #setHttpOnly(boolean) - * * @since 02.03.00 * @author Ktt Development */ @@ -402,7 +388,6 @@ public final boolean isHttpOnly(){ * @return cookie builder * * @see #isHttpOnly() - * * @since 02.03.00 * @author Ktt Development */ diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java index 2916d6c..236c038 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchange.java @@ -308,17 +308,36 @@ static SimpleHttpExchange create(final HttpExchange exchange){ * * @return client's cookies * + * @see #setCookie(String, String) + * @see #setCookie(SimpleHttpCookie) * @since 02.00.00 * @author Ktt Development */ public abstract HashMap getCookies(); + /** + * Sets a cookie in the response header. + * + * @param key name of cookie to set + * @param value value of cookie + * + * @see SimpleHttpCookie + * @see #setCookie(SimpleHttpCookie) + * @see #getCookies() + * @see #getResponseHeaders() + * @since 02.03.00 + * @author Ktt Development + */ + public abstract void setCookie(final String key, final String value); + /** * Sets a cookie in the response header. * * @param cookie cookie to set * * @see SimpleHttpCookie + * @see #setCookie(String, String) + * @see #getCookies() * @see #getResponseHeaders() * @since 02.00.00 * @author Ktt Development diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java index fc50a5f..a7f5315 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java @@ -309,6 +309,11 @@ public final HashMap getCookies(){ return new HashMap<>(cookies); } + @Override + public synchronized final void setCookie(final String key, final String value){ + setCookie(new SimpleHttpCookie.Builder(key,value).build()); + } + @Override public synchronized final void setCookie(final SimpleHttpCookie cookie){ getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); From 4336cf2e105dcc8047387e6a0fd8fa3e4f226d0b Mon Sep 17 00:00:00 2001 From: Katsute Date: Fri, 1 May 2020 20:17:49 -0400 Subject: [PATCH 5/6] Bug fixes --- .../com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java | 3 +-- .../simplehttpserver/SimpleHttpExchangeImpl.java | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java index 403bef4..5d41a6d 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java @@ -111,7 +111,6 @@ public final String toString(){ */ public final String toCookieHeaderString(){ final StringBuilder OUT = new StringBuilder(); - OUT.append(name).append("=").append(value); if(expires != null) OUT.append("; Expires=").append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(expires)).append(" GMT"); @@ -148,7 +147,7 @@ public static class Builder { private String path; private String sameSite; private Date expires; - private int maxAge; + private Integer maxAge; private Boolean secure; private Boolean httpOnly; diff --git a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java index a7f5315..40771ff 100644 --- a/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java +++ b/src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java @@ -328,9 +328,7 @@ public synchronized final HttpSession getHttpSession(){ if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){ session = HttpSession.create(); setCookie( - new SimpleHttpCookie.Builder("__sesion-id",session.getSessionID()) - .setHttpOnly(true) - .build() + new SimpleHttpCookie.Builder("__session-id",session.getSessionID()).build() ); }else{ session = HttpSession.sessions.get(sessionId); From 5e02232c77cefab73d22daddd323873029f1c9d8 Mon Sep 17 00:00:00 2001 From: Katsute Date: Fri, 1 May 2020 20:23:24 -0400 Subject: [PATCH 6/6] Update pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 2075ef1..bd45f9d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.kttdevelopment simplehttpserver - 03.02.00 + 03.03.00 jar https://github.com/Ktt-Development/simplehttpserver