Skip to content

Commit

Permalink
添加 Cookie 支持 (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx567888 authored Sep 20, 2024
1 parent f06c233 commit 06c3caf
Show file tree
Hide file tree
Showing 19 changed files with 320 additions and 13 deletions.
2 changes: 1 addition & 1 deletion scx-core/src/main/java/cool/scx/core/ScxHttpRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import cool.scx.http.HttpFieldName;
import cool.scx.http.HttpMethod;
import cool.scx.http.routing.handler.CorsHandler;
import cool.scx.http.routing.Route;
import cool.scx.http.routing.RouterImpl;
import cool.scx.http.routing.handler.CorsHandler;

import static cool.scx.http.HttpFieldName.*;
import static cool.scx.http.HttpMethod.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cool.scx.http.helidon;

import cool.scx.http.cookie.Cookie;
import cool.scx.http.cookie.Cookies;
import io.helidon.common.parameters.Parameters;

import java.util.Iterator;

public class HelidonCookies implements Cookies {

private final Parameters cookies;

public HelidonCookies(Parameters cookies) {
this.cookies = cookies;
}

@Override
public long size() {
return cookies.size();
}

@Override
public Cookie get(String name) {
var value = cookies.get(name);
if (value == null) {
return null;
} else {
return Cookie.of(name, value);
}
}

@Override
public Iterator<Cookie> iterator() {
var itr = cookies.toMap().entrySet().iterator();
return new Iterator<Cookie>() {
@Override
public boolean hasNext() {
return itr.hasNext();
}

@Override
public Cookie next() {
var next = itr.next();
var key = next.getKey();
var value = next.getValue();
return Cookie.of(key, value.size() > 0 ? value.get(0) : null);
}
};
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cool.scx.http.helidon;

import cool.scx.http.*;
import cool.scx.http.cookie.Cookies;
import cool.scx.http.uri.ScxURI;
import io.helidon.webserver.ConnectionContext;
import io.helidon.webserver.http.RoutingRequest;
Expand All @@ -19,6 +20,7 @@ class HelidonHttpServerRequest implements ScxHttpServerRequest {
private final ScxHttpServerResponse response;
private final HelidonPeerInfo remotePeer;
private final HelidonPeerInfo localPeer;
private final Cookies cookies;

public HelidonHttpServerRequest(ConnectionContext ctx, RoutingRequest request, RoutingResponse response) {
var p = request.prologue();
Expand All @@ -31,6 +33,7 @@ public HelidonHttpServerRequest(ConnectionContext ctx, RoutingRequest request, R
this.response = new HelidonHttpServerResponse(response);
this.remotePeer = new HelidonPeerInfo(request.remotePeer());
this.localPeer = new HelidonPeerInfo(request.localPeer());
this.cookies = new HelidonCookies(request.headers().cookies());
}

@Override
Expand Down Expand Up @@ -73,4 +76,9 @@ public PeerInfo localPeer() {
return localPeer;
}

@Override
public Cookies cookies() {
return cookies;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import cool.scx.http.HttpStatusCode;
import cool.scx.http.ScxHttpHeadersWritable;
import cool.scx.http.ScxHttpServerResponse;
import cool.scx.http.cookie.Cookie;
import io.helidon.http.SetCookie;
import io.helidon.webserver.http.RoutingResponse;

import java.io.OutputStream;
import java.time.Duration;

/**
* HelidonHttpServerResponse
Expand Down Expand Up @@ -66,4 +69,25 @@ public boolean isClosed() {
return this.response.isSent();
}

@Override
public ScxHttpServerResponse addCookie(Cookie cookie) {
var c = SetCookie
.builder(cookie.name(), cookie.name())
.domain(cookie.domain())
.path(cookie.path())
.maxAge(Duration.ofSeconds(cookie.maxAge()))
.secure(cookie.secure())
.httpOnly(cookie.httpOnly())
.sameSite(SetCookie.SameSite.valueOf(cookie.sameSite().name()))
.build();
this.response.headers().addCookie(c);
return this;
}

@Override
public ScxHttpServerResponse removeCookie(String name) {
this.response.headers().clearCookie(name);
return this;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cool.scx.http;

//todo
/**
* ScxHttpClientOptions
*/
public class ScxHttpClientOptions {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package cool.scx.http;

import cool.scx.http.content_type.ContentType;
import cool.scx.http.cookie.Cookie;
import cool.scx.http.cookie.Cookies;
import cool.scx.http.uri.ScxURI;
import cool.scx.http.uri.URIPath;
import cool.scx.http.uri.URIQuery;
Expand All @@ -25,6 +28,8 @@ public interface ScxHttpServerRequest {

PeerInfo localPeer();

Cookies cookies();

default URIPath path() {
return uri().path();
}
Expand All @@ -45,4 +50,8 @@ default ContentType contentType() {
return ContentType.of(getHeader(HttpFieldName.CONTENT_TYPE));
}

default Cookie getCookie(String name) {
return cookies().get(name);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cool.scx.http;

import cool.scx.http.cookie.Cookie;

import java.io.OutputStream;

/**
Expand All @@ -25,6 +27,10 @@ public interface ScxHttpServerResponse {

boolean isClosed();

ScxHttpServerResponse addCookie(Cookie cookie);

ScxHttpServerResponse removeCookie(String name);

default ScxHttpServerResponse setHeader(ScxHttpHeaderName headerName, String... values) {
this.headers().set(headerName, values);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package cool.scx.http;
package cool.scx.http.content_type;

import cool.scx.http.Parameters;
import cool.scx.http.ScxMediaType;

import java.nio.charset.Charset;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package cool.scx.http;
package cool.scx.http.content_type;

import cool.scx.http.Parameters;
import cool.scx.http.ScxMediaType;

public class ContentTypeImpl implements ContentTypeWritable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package cool.scx.http;
package cool.scx.http.content_type;

import cool.scx.http.Parameters;
import cool.scx.http.ScxMediaType;

public interface ContentTypeWritable extends ContentType {

Expand Down
25 changes: 25 additions & 0 deletions scx-http/src/main/java/cool/scx/http/cookie/Cookie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cool.scx.http.cookie;

public interface Cookie {

static CookieWritable of(String name, String value) {
return new CookieImpl(name, value);
}

String name();

String value();

String domain();

String path();

long maxAge();

boolean secure();

boolean httpOnly();

CookieSameSite sameSite();

}
126 changes: 126 additions & 0 deletions scx-http/src/main/java/cool/scx/http/cookie/CookieImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package cool.scx.http.cookie;

class CookieImpl implements CookieWritable {

private String name;
private String value;
private String domain;
private String path;
private long maxAge;
private boolean secure;
private boolean httpOnly;
private CookieSameSite sameSite;

public CookieImpl(String name, String value) {
this.name = name;
this.value = value;
}

@Override
public CookieWritable domain(String domain) {
this.domain = domain;
return this;
}

@Override
public CookieWritable path(String path) {
this.path = path;
return this;
}

@Override
public CookieWritable maxAge(long maxAge) {
this.maxAge = maxAge;
return this;
}

@Override
public CookieWritable secure(boolean secure) {
this.secure = secure;
return this;
}

@Override
public CookieWritable httpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}

@Override
public CookieWritable sameSite(CookieSameSite sameSite) {
this.sameSite = sameSite;
return this;
}

@Override
public String name() {
return name;
}

@Override
public String value() {
return value;
}

@Override
public String domain() {
return domain;
}

@Override
public String path() {
return path;
}

@Override
public long maxAge() {
return maxAge;
}

@Override
public boolean secure() {
return secure;
}

@Override
public boolean httpOnly() {
return httpOnly;
}

@Override
public CookieSameSite sameSite() {
return sameSite;
}

@Override
public String toString() {
var buf = new StringBuilder()
.append(name)
.append('=')
.append(value);
if (domain != null) {
buf.append(", domain=")
.append(domain);
}
if (path != null) {
buf.append(", path=")
.append(path);
}
if (maxAge >= 0) {
buf.append(", maxAge=")
.append(maxAge)
.append('s');
}
if (secure) {
buf.append(", secure");
}
if (httpOnly) {
buf.append(", HTTPOnly");
}
if (sameSite != null) {
buf.append(", SameSite=").append(sameSite.value());
}
return buf.toString();
}

}
21 changes: 21 additions & 0 deletions scx-http/src/main/java/cool/scx/http/cookie/CookieSameSite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cool.scx.http.cookie;

public enum CookieSameSite {

NONE("None"),

STRICT("Strict"),

LAX("Lax");

private final String value;

CookieSameSite(String label) {
this.value = label;
}

public String value() {
return value;
}

}
Loading

0 comments on commit 06c3caf

Please sign in to comment.