GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add RFC 10008 QUERY method and Accept-Query header support (#1077) · apache/pekko-http@4ac4b73

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs/src
    • main/paradox/routing-dsl/directives
      • alphabetically.md
      • method-directives
        • index.md
        • query.md
    • test
      • java/docs/http/javadsl/server/directives
        • MethodDirectivesExamplesTest.java
      • scala/docs/http/scaladsl/server/directives
        • MethodDirectivesExamplesSpec.scala
  • http-core/src
    • main
      • java/org/apache/pekko/http/javadsl/model
        • HttpMethods.java
        • HttpRequest.java
        • headers
          • AcceptQuery.java
      • scala/org/apache/pekko/http
        • impl/model/parser
          • AcceptHeader.scala
          • HeaderParser.scala
        • scaladsl/model
          • HttpMethod.scala
          • MediaRange.scala
          • headers
            • headers.scala
    • test/scala/org/apache/pekko/http
      • impl/model/parser
        • HttpHeaderSpec.scala
      • scaladsl/model
        • HttpMethodsSpec.scala
        • headers
          • HeaderSpec.scala
  • http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives
    • MethodDirectivesSpec.scala
  • http/src/main/scala/org/apache/pekko/http
    • javadsl/server/directives
      • MethodDirectives.scala
    • scaladsl/server/directives
      • MethodDirectives.scala
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/src/main/paradox/routing-dsl/directives/alphabetically.md‎

Copy file name to clipboardExpand all lines: docs/src/main/paradox/routing-dsl/directives/alphabetically.md
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
|@ref[post](method-directives/post.md) | Rejects all non-POST requests |
128128
|@ref[provide](basic-directives/provide.md) | Injects a given value into a directive |
129129
|@ref[put](method-directives/put.md) | Rejects all non-PUT requests |
130+
|@ref[query](method-directives/query.md) | Rejects all non-QUERY requests |
130131
|@ref[rawPathPrefix](path-directives/rawPathPrefix.md) | Applies the given matcher directly to a prefix of the unmatched path of the @apidoc[RequestContext], without implicitly consuming a leading slash |
131132
|@ref[rawPathPrefixTest](path-directives/rawPathPrefixTest.md) | Checks whether the unmatchedPath has a prefix matched by the given `PathMatcher` |
132133
|@ref[recoverRejections](basic-directives/recoverRejections.md) | Transforms rejections from the inner route with an @scala[`immutable.Seq[Rejection] => RouteResult`]@java[`Function<List<Rejection>, RouteResult>`] function |
Collapse file

‎docs/src/main/paradox/routing-dsl/directives/method-directives/index.md‎

Copy file name to clipboardExpand all lines: docs/src/main/paradox/routing-dsl/directives/method-directives/index.md
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
* [patch](patch.md)
1515
* [post](post.md)
1616
* [put](put.md)
17+
* [query](query.md)
1718

1819
@@@
Collapse file

‎docs/src/main/paradox/routing-dsl/directives/method-directives/query.md‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# query
2+
3+
Matches requests with HTTP method `QUERY` (RFC 10008).
4+
5+
@@@ div { .group-scala }
6+
7+
## Signature
8+
9+
@@signature [MethodDirectives.scala](/http/src/main/scala/org/apache/pekko/http/scaladsl/server/directives/MethodDirectives.scala) { #query }
10+
11+
@@@
12+
13+
## Description
14+
15+
This directive filters the incoming request by its HTTP method. Only requests with
16+
method `QUERY` are passed on to the inner route. All others are rejected with a
17+
@apidoc[MethodRejection], which is translated into a `405 Method Not Allowed` response
18+
by the default @ref[RejectionHandler](../../rejections.md#the-rejectionhandler).
19+
20+
The `QUERY` method is defined in [RFC 10008](https://www.rfc-editor.org/rfc/rfc10008).
21+
It is a safe and idempotent method that requests the target resource to process the
22+
enclosed content and respond with the result. Unlike `GET`, the `QUERY` method expects
23+
a request body containing the query payload.
24+
25+
## Example
26+
27+
Scala
28+
: @@snip [MethodDirectivesExamplesSpec.scala](/docs/src/test/scala/docs/http/scaladsl/server/directives/MethodDirectivesExamplesSpec.scala) { #query-method }
29+
30+
Java
31+
: @@snip [MethodDirectivesExamplesTest.java](/docs/src/test/java/docs/http/javadsl/server/directives/MethodDirectivesExamplesTest.java) { #query }
Collapse file

‎docs/src/test/java/docs/http/javadsl/server/directives/MethodDirectivesExamplesTest.java‎

Copy file name to clipboardExpand all lines: docs/src/test/java/docs/http/javadsl/server/directives/MethodDirectivesExamplesTest.java
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
import static org.apache.pekko.http.javadsl.server.Directives.put;
5252

5353
// #put
54+
// #query
55+
import static org.apache.pekko.http.javadsl.server.Directives.complete;
56+
import static org.apache.pekko.http.javadsl.server.Directives.query;
57+
58+
// #query
5459
// #method-example
5560
import static org.apache.pekko.http.javadsl.server.Directives.complete;
5661
import static org.apache.pekko.http.javadsl.server.Directives.method;
@@ -140,6 +145,17 @@ public void testPut() {
140145
// #put
141146
}
142147

148+
@Test
149+
public void testQuery() {
150+
// #query
151+
final Route route = query(() -> complete("This is a QUERY request."));
152+
153+
testRoute(route)
154+
.run(HttpRequest.QUERY("/").withEntity("query content"))
155+
.assertEntity("This is a QUERY request.");
156+
// #query
157+
}
158+
143159
@Test
144160
public void testMethodExample() {
145161
// #method-example
Collapse file

‎docs/src/test/scala/docs/http/scaladsl/server/directives/MethodDirectivesExamplesSpec.scala‎

Copy file name to clipboardExpand all lines: docs/src/test/scala/docs/http/scaladsl/server/directives/MethodDirectivesExamplesSpec.scala
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ class MethodDirectivesExamplesSpec extends RoutingSpec with CompileOnlySpec {
9898
// #put-method
9999
}
100100

101+
"query-method" in {
102+
// #query-method
103+
val route = query { complete("This is a QUERY request.") }
104+
105+
// tests:
106+
HttpRequest(method = HttpMethods.QUERY, uri = "/") ~> route ~> check {
107+
responseAs[String] shouldEqual "This is a QUERY request."
108+
}
109+
// #query-method
110+
}
111+
101112
"method-example" in {
102113
// #method-example
103114
val route = method(HttpMethods.PUT) { complete("This is a PUT request.") }
Collapse file

‎http-core/src/main/java/org/apache/pekko/http/javadsl/model/HttpMethods.java‎

Copy file name to clipboardExpand all lines: http-core/src/main/java/org/apache/pekko/http/javadsl/model/HttpMethods.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ private HttpMethods() {}
3131
public static final HttpMethod PATCH = org.apache.pekko.http.scaladsl.model.HttpMethods.PATCH();
3232
public static final HttpMethod POST = org.apache.pekko.http.scaladsl.model.HttpMethods.POST();
3333
public static final HttpMethod PUT = org.apache.pekko.http.scaladsl.model.HttpMethods.PUT();
34+
public static final HttpMethod QUERY = org.apache.pekko.http.scaladsl.model.HttpMethods.QUERY();
3435
public static final HttpMethod TRACE = org.apache.pekko.http.scaladsl.model.HttpMethods.TRACE();
3536

3637
/**
Collapse file

‎http-core/src/main/java/org/apache/pekko/http/javadsl/model/HttpRequest.java‎

Copy file name to clipboardExpand all lines: http-core/src/main/java/org/apache/pekko/http/javadsl/model/HttpRequest.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,9 @@ public static HttpRequest PATCH(String uri) {
8383
public static HttpRequest OPTIONS(String uri) {
8484
return create(uri).withMethod(HttpMethods.OPTIONS);
8585
}
86+
87+
/** A default QUERY request to be modified using the `withX` methods. */
88+
public static HttpRequest QUERY(String uri) {
89+
return create(uri).withMethod(HttpMethods.QUERY);
90+
}
8691
}
Collapse file

‎http-core/src/main/java/org/apache/pekko/http/javadsl/model/headers/AcceptQuery.java‎

Copy file name to clipboard
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.pekko.http.javadsl.model.headers;
19+
20+
import org.apache.pekko.http.impl.util.Util;
21+
import org.apache.pekko.http.javadsl.model.MediaRange;
22+
23+
/**
24+
* Model for the `Accept-Query` header. Specification:
25+
* https://www.rfc-editor.org/rfc/rfc10008.html#section-3
26+
*/
27+
public abstract class AcceptQuery extends org.apache.pekko.http.scaladsl.model.HttpHeader {
28+
public abstract Iterable<MediaRange> getMediaRanges();
29+
30+
public static AcceptQuery create(MediaRange... mediaRanges) {
31+
return new org.apache.pekko.http.scaladsl.model.headers.Accept$minusQuery(
32+
Util.<MediaRange, org.apache.pekko.http.scaladsl.model.MediaRange>convertArray(
33+
mediaRanges));
34+
}
35+
}
Collapse file

‎http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/AcceptHeader.scala‎

Copy file name to clipboardExpand all lines: http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/AcceptHeader.scala
+87-2Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,104 @@ package org.apache.pekko.http.impl.model.parser
1616
import scala.collection.immutable.TreeMap
1717

1818
import org.apache.pekko
19-
import org.parboiled2.Parser
19+
import org.parboiled2.{ CharPredicate, Parser, Rule1 }
2020
import pekko.http.scaladsl.model.headers._
21-
import pekko.http.scaladsl.model.{ MediaRange, MediaRanges }
21+
import pekko.http.scaladsl.model.{ MediaRange, MediaRanges, ParsingException }
2222
import pekko.http.impl.util._
2323

2424
private[parser] trait AcceptHeader { this: Parser with CommonRules with CommonActions =>
2525
import CharacterClasses._
2626

27+
private val acceptQueryMediaRangeChar = tchar ++ '/'
28+
private val sfStringChar = CharPredicate('\u0020' to '\u0021', '\u0023' to '\u005B', '\u005D' to '\u007E')
29+
private val sfStringEscapedChar = CharPredicate('"', '\\')
30+
2731
// http://tools.ietf.org/html/rfc7231#section-5.3.2
2832
def accept = rule {
2933
zeroOrMore(`media-range-decl`).separatedBy(listSep) ~ EOI ~> (Accept(_))
3034
}
3135

36+
// https://www.rfc-editor.org/rfc/rfc10008.html#section-3
37+
// Accept-Query uses Structured Fields syntax (RFC 9651): media types may appear
38+
// as tokens (application/json) or quoted strings ("application/jsonpath").
39+
def `accept-query` = rule {
40+
zeroOrMore(`accept-query-media-range-decl`).separatedBy(listSep) ~ EOI ~> (`Accept-Query`(_))
41+
}
42+
43+
def `accept-query-media-range-decl` = rule {
44+
`accept-query-media-range-def` ~ zeroOrMore(`accept-query-param`) ~> {
45+
(mediaRange: (String, String), params: Seq[(String, String)]) =>
46+
val (main, sub) = mediaRange
47+
val mediaRangeParams = TreeMap(params: _*)
48+
if (sub == "*") {
49+
val mainLower = main.toRootLowerCase
50+
MediaRanges.getForKey(mainLower) match {
51+
case Some(registered) =>
52+
if (mediaRangeParams.isEmpty) registered else MediaRange.customWithParams(mainLower, mediaRangeParams)
53+
case None => MediaRange.customWithParams(mainLower, mediaRangeParams)
54+
}
55+
} else {
56+
MediaRange(getMediaType(main, sub, mediaRangeParams contains "charset", mediaRangeParams))
57+
}
58+
}
59+
}
60+
61+
def `accept-query-media-range-def` = rule {
62+
`sf-token` ~> (parseAcceptQueryMediaRange _) |
63+
`sf-string` ~> (parseAcceptQueryMediaRange _)
64+
}
65+
66+
def `accept-query-param` = rule {
67+
';' ~ OWS ~ `sf-key` ~ '=' ~ `sf-param-value` ~> ((_, _))
68+
}
69+
70+
def `sf-param-value`: Rule1[String] = rule {
71+
`sf-string` | `sf-token`
72+
}
73+
74+
def `sf-key`: Rule1[String] = rule {
75+
capture((LOWER_ALPHA | '*') ~ zeroOrMore(LOWER_ALPHA | DIGIT | '_' | '-' | '.' | '*'))
76+
}
77+
78+
def `sf-token`: Rule1[String] = rule {
79+
capture((ALPHA | '*') ~ zeroOrMore(tchar | ':' | '/')) ~ OWS
80+
}
81+
82+
def `sf-string`: Rule1[String] = rule {
83+
DQUOTE ~ clearSB() ~ zeroOrMore(`sf-string-char` ~ appendSB() | '\\' ~ `sf-string-escaped-char` ~ appendSB()) ~
84+
push(sb.toString) ~ DQUOTE ~ OWS
85+
}
86+
87+
def `sf-string-char` = rule { sfStringChar }
88+
89+
def `sf-string-escaped-char` = rule { sfStringEscapedChar }
90+
91+
private def parseAcceptQueryMediaRange(value: String): (String, String) = {
92+
var slashIdx = -1
93+
var ix = 0
94+
while (ix < value.length) {
95+
val ch = value.charAt(ix)
96+
if (ch == '/') {
97+
if (slashIdx >= 0) invalidAcceptQueryMediaRange(value)
98+
slashIdx = ix
99+
} else if (!acceptQueryMediaRangeChar(ch)) invalidAcceptQueryMediaRange(value)
100+
ix += 1
101+
}
102+
103+
if (slashIdx <= 0 || slashIdx == value.length - 1) invalidAcceptQueryMediaRange(value)
104+
105+
val main = value.substring(0, slashIdx)
106+
val sub = value.substring(slashIdx + 1)
107+
if (main.indexOf('*') >= 0 && main != "*") invalidAcceptQueryMediaRange(value)
108+
if (sub.indexOf('*') >= 0 && sub != "*") invalidAcceptQueryMediaRange(value)
109+
if (main == "*" && sub != "*") invalidAcceptQueryMediaRange(value)
110+
111+
(main, sub)
112+
}
113+
114+
private def invalidAcceptQueryMediaRange(value: String): Nothing =
115+
throw ParsingException(s"Illegal Accept-Query media range '$value'")
116+
32117
def `media-range-decl` = rule {
33118
`media-range-def` ~ OWS ~ zeroOrMore(ws(';') ~ parameter) ~> { (main, sub, params) =>
34119
if (sub == "*") {
Collapse file

‎http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/HeaderParser.scala‎

Copy file name to clipboardExpand all lines: http-core/src/main/scala/org/apache/pekko/http/impl/model/parser/HeaderParser.scala
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ private[http] object HeaderParser {
144144
"accept-charset",
145145
"accept-encoding",
146146
"accept-language",
147+
"accept-query",
147148
"accept-ranges",
148149
"access-control-allow-credentials",
149150
"access-control-allow-headers",

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.