Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP Method QUERY support #499

Merged
merged 8 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion httpcore5/src/main/java/org/apache/hc/core5/http/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ public enum Method {
/**
* The HTTP {@code PATCH} method is unsafe and non-idempotent.
*/
PATCH(false, false);
PATCH(false, false),

/**
* The HTTP {@code QUERY} method is safe and idempotent.
*/
QUERY(true, true);

private final boolean safe;
private final boolean idempotent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public static ClassicRequestBuilder head(final String uri) {
return new ClassicRequestBuilder(Method.HEAD, uri);
}

public static ClassicRequestBuilder query() {
return new ClassicRequestBuilder(Method.QUERY);
}

public static ClassicRequestBuilder query(final URI uri) {
return new ClassicRequestBuilder(Method.QUERY, uri);
}

public static ClassicRequestBuilder query(final String uri) {
return new ClassicRequestBuilder(Method.QUERY, uri);
}

public static ClassicRequestBuilder patch() {
return new ClassicRequestBuilder(Method.PATCH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,12 @@ public static Header format(final String name, final String... tokens) {
}

/**
* @since 5.3
* @since 5.4
*/
public static void parseTokens(final CharSequence src, final ParserCursor cursor, final Consumer<String> consumer) {
public static void parseTokens(final CharSequence src,
final ParserCursor cursor,
final Tokenizer.Delimiter delimiterPredicate,
final Consumer<String> consumer) {
Args.notNull(src, "Source");
Args.notNull(cursor, "Cursor");
Args.notNull(consumer, "Consumer");
Expand All @@ -179,39 +182,65 @@ public static void parseTokens(final CharSequence src, final ParserCursor cursor
if (src.charAt(pos) == ',') {
cursor.updatePos(pos + 1);
}
final String token = Tokenizer.INSTANCE.parseToken(src, cursor, COMMA);
final String token = Tokenizer.INSTANCE.parseToken(src, cursor, delimiterPredicate);
consumer.accept(token);
}
}

/**
* @since 5.3
*/
public static void parseTokens(final Header header, final Consumer<String> consumer) {
public static void parseTokens(final CharSequence src, final ParserCursor cursor, final Consumer<String> consumer) {
parseTokens(src, cursor, COMMA, consumer);
}

/**
* @since 5.4
*/
public static void parseTokens(final Header header,
final Tokenizer.Delimiter delimiterPredicate,
final Consumer<String> consumer) {
Args.notNull(header, "Header");
if (header instanceof FormattedHeader) {
final CharArrayBuffer buf = ((FormattedHeader) header).getBuffer();
final ParserCursor cursor = new ParserCursor(0, buf.length());
cursor.updatePos(((FormattedHeader) header).getValuePos());
parseTokens(buf, cursor, consumer);
parseTokens(buf, cursor, delimiterPredicate, consumer);
} else {
final String value = header.getValue();
final ParserCursor cursor = new ParserCursor(0, value.length());
parseTokens(value, cursor, consumer);
parseTokens(value, cursor, delimiterPredicate, consumer);
}
}

/**
* @since 5.3
*/
public static void parseTokens(final MessageHeaders headers, final String headerName, final Consumer<String> consumer) {
public static void parseTokens(final Header header, final Consumer<String> consumer) {
parseTokens(header, COMMA, consumer);
}

/**
* @since 5.4
*/
public static void parseTokens(final MessageHeaders headers,
final String headerName,
final Tokenizer.Delimiter delimiterPredicate,
final Consumer<String> consumer) {
Args.notNull(headers, "Headers");
final Iterator<Header> it = headers.headerIterator(headerName);
while (it.hasNext()) {
parseTokens(it.next(), consumer);
parseTokens(it.next(), delimiterPredicate, consumer);
}
}

/**
* @since 5.3
*/
public static void parseTokens(final MessageHeaders headers, final String headerName, final Consumer<String> consumer) {
parseTokens(headers, headerName, COMMA, consumer);
}

public static Set<String> parseTokens(final CharSequence src, final ParserCursor cursor) {
Args.notNull(src, "Source");
Args.notNull(cursor, "Cursor");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public static AsyncRequestBuilder head(final String uri) {
return new AsyncRequestBuilder(Method.HEAD, uri);
}

public static AsyncRequestBuilder query() {
return new AsyncRequestBuilder(Method.QUERY);
}

public static AsyncRequestBuilder query(final URI uri) {
return new AsyncRequestBuilder(Method.QUERY, uri);
}

public static AsyncRequestBuilder query(final String uri) {
return new AsyncRequestBuilder(Method.QUERY, uri);
}

public static AsyncRequestBuilder patch() {
return new AsyncRequestBuilder(Method.PATCH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void process(final HttpRequest request, final EntityDetails entity, final
}

private boolean isContentEnclosingMethod(final String method) {
return Method.POST.isSame(method) || Method.PUT.isSame(method) || Method.PATCH.isSame(method);
return Method.POST.isSame(method) || Method.PUT.isSame(method) || Method.PATCH.isSame(method) || Method.QUERY.isSame(method);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ public static BasicRequestBuilder head(final String uri) {
return new BasicRequestBuilder(Method.HEAD, uri);
}

public static BasicRequestBuilder query() {
return new BasicRequestBuilder(Method.QUERY);
}

public static BasicRequestBuilder query(final URI uri) {
return new BasicRequestBuilder(Method.QUERY, uri);
}

public static BasicRequestBuilder query(final String uri) {
return new BasicRequestBuilder(Method.QUERY, uri);
}

public static BasicRequestBuilder patch() {
return new BasicRequestBuilder(Method.PATCH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ void head() throws UnknownHostException, URISyntaxException {
assertEquals("/localhost", classicRequestBuilder3.getPath());
}

@Test
void query() throws UnknownHostException, URISyntaxException {
final ClassicRequestBuilder classicRequestBuilder = ClassicRequestBuilder.query();
assertEquals(Method.QUERY.name(), classicRequestBuilder.getMethod());

final ClassicRequestBuilder classicRequestBuilder1 = ClassicRequestBuilder.query(URIBuilder.localhost().build());
assertEquals(Method.QUERY.name(), classicRequestBuilder1.getMethod());

final ClassicRequestBuilder classicRequestBuilder3 = ClassicRequestBuilder.query("/localhost");
assertEquals(Method.QUERY.name(), classicRequestBuilder3.getMethod());
assertEquals("/localhost", classicRequestBuilder3.getPath());
}

@Test
void patch() throws UnknownHostException, URISyntaxException {
final ClassicRequestBuilder classicRequestBuilder = ClassicRequestBuilder.patch();
Expand Down