Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartofanych committed Dec 14, 2024
1 parent 2f7bc6c commit f047e75
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/fallback/FbLog4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.cactoos.bytes.BytesOf;
import org.cactoos.text.TextOf;
import org.takes.misc.Opt;
import org.takes.rq.RqBaseMethod;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;

/**
* Fallback that logs all problems through Log4J.
Expand Down Expand Up @@ -60,7 +60,7 @@ private static void log(final RqFallback req) throws IOException {
Logger.getLogger(FbLog4j.class).error(
String.format(
"%s %s failed with %s: %s",
new RqMethod.Base(req).method(),
new RqBaseMethod(req).method(),
new RqHref.Base(req).href(),
req.code(),
new TextOf(new BytesOf(req.throwable()))
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/fallback/FbSlf4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.takes.misc.Opt;
import org.takes.rq.RqBaseMethod;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;

/**
* Fallback that logs all problems through SFL4J.
Expand Down Expand Up @@ -68,7 +68,7 @@ public FbSlf4j() {
private static void log(final RqFallback req) throws IOException {
FbSlf4j.LOGGER.error(
"{} {} failed with {}: {}",
new RqMethod.Base(req).method(),
new RqBaseMethod(req).method(),
new RqHref.Base(req).href(),
req.code(),
new TextOf(new BytesOf(req.throwable()))
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/fallback/TkFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.takes.Response;
import org.takes.Take;
import org.takes.misc.Opt;
import org.takes.rq.RqBaseMethod;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;
import org.takes.rs.ResponseOf;
import org.takes.tk.TkWrap;

Expand Down Expand Up @@ -231,7 +231,7 @@ private static Throwable error(final Throwable exp, final Request req,
return new IllegalStateException(
String.format(
"[%s %s] failed in %s: %s",
new RqMethod.Base(req).method(),
new RqBaseMethod(req).method(),
new RqHref.Base(req).href(),
time, TkFallback.msg(exp)
),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/fork/FkMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.takes.Response;
import org.takes.Take;
import org.takes.misc.Opt;
import org.takes.rq.RqMethod;
import org.takes.rq.RqBaseMethod;
import org.takes.tk.TkFixed;

/**
Expand Down Expand Up @@ -94,7 +94,7 @@ public FkMethods(final Collection<String> mtds, final Take tke) {

@Override
public Opt<Response> route(final Request req) throws Exception {
final String mtd = new RqMethod.Base(req).method();
final String mtd = new RqBaseMethod(req).method();
final Opt<Response> resp;
if (this.methods.contains(mtd)) {
resp = new Opt.Single<>(this.take.act(req));
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/org/takes/rq/RqBaseMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2024 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.rq;

import java.io.IOException;
import java.util.Locale;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import org.takes.Request;

/**
* Request decorator, for HTTP method parsing.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.13.7
*/
@EqualsAndHashCode(callSuper = true)
public final class RqBaseMethod extends RqWrap implements RqMethod {

/**
* HTTP token separators which are not already excluded by PATTERN.
*/
private static final Pattern SEPARATORS = Pattern.compile(
"[()<>@,;:\\\"/\\[\\]?={}]"
);

/**
* Ctor.
* @param req Original request
*/
public RqBaseMethod(final Request req) {
super(req);
}

@Override
public String method() throws IOException {
final String method = new RqRequestLine.Base(this).method();
if (RqBaseMethod.SEPARATORS.matcher(method).find()) {
throw new IOException(
String.format("Invalid HTTP method: %s", method)
);
}
return method.toUpperCase(Locale.ENGLISH);
}
}
38 changes: 0 additions & 38 deletions src/main/java/org/takes/rq/RqMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,42 +89,4 @@ public interface RqMethod extends Request {
* @throws IOException If fails
*/
String method() throws IOException;

/**
* Request decorator, for HTTP method parsing.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.13.7
*/
@EqualsAndHashCode(callSuper = true)
final class Base extends RqWrap implements RqMethod {

/**
* HTTP token separators which are not already excluded by PATTERN.
*/
private static final Pattern SEPARATORS = Pattern.compile(
"[()<>@,;:\\\"/\\[\\]?={}]"
);

/**
* Ctor.
* @param req Original request
*/
public Base(final Request req) {
super(req);
}

@Override
public String method() throws IOException {
final String method = new RqRequestLine.Base(this)
.method();
if (Base.SEPARATORS.matcher(method).find()) {
throw new IOException(
String.format("Invalid HTTP method: %s", method)
);
}
return method.toUpperCase(Locale.ENGLISH);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/takes/servlet/HttpServletRequestFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
import java.util.Map;
import java.util.NoSuchElementException;
import org.takes.Request;
import org.takes.rq.RqBaseMethod;
import org.takes.rq.RqHeaders;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;

/**
* Fake HttpServletRequest (for unit tests).
Expand Down Expand Up @@ -123,7 +123,7 @@ public Enumeration<String> getHeaderNames() {
@Override
public String getMethod() {
try {
return new RqMethod.Base(this.request).method();
return new RqBaseMethod(this.request).method();
} catch (final IOException ex) {
throw new IllegalArgumentException(
"Failed to get method from the request",
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/tk/TkProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rq.RqBaseMethod;
import org.takes.rq.RqHeaders;
import org.takes.rq.RqLengthAware;
import org.takes.rq.RqMethod;
import org.takes.rq.RqPrint;
import org.takes.rq.RqRequestLine;
import org.takes.rs.RsWithBody;
Expand Down Expand Up @@ -130,7 +130,7 @@ public Response act(final Request req) throws Exception {
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private com.jcabi.http.Request request(final Request req,
final URI dest) throws Exception {
final String method = new RqMethod.Base(req).method();
final String method = new RqBaseMethod(req).method();
com.jcabi.http.Request proxied = new JdkRequest(dest).method(method);
final RqHeaders headers = new RqHeaders.Base(req);
for (final String name : headers.names()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/tk/TkSlf4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rq.RqBaseMethod;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;

/**
* Logs Take.act() calls.
Expand Down Expand Up @@ -100,7 +100,7 @@ public Response act(final Request req) throws Exception {
new TextOf(" "),
new FormattedText(
"[%s %s]",
new RqMethod.Base(req).method(),
new RqBaseMethod(req).method(),
new RqHref.Base(req).href()
),
new FormattedText(params.getKey(), params.getValue()),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/tk/TkVerbose.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import lombok.ToString;
import org.takes.HttpException;
import org.takes.Take;
import org.takes.rq.RqBaseMethod;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;

/**
* Take that makes all not-found exceptions location aware.
Expand All @@ -55,7 +55,7 @@ public TkVerbose(final Take take) {
ex.code(),
String.format(
"%s %s",
new RqMethod.Base(request).method(),
new RqBaseMethod(request).method(),
new RqHref.Base(request).href()
),
ex
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/takes/rq/RqMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class RqMethodTest {
@Test
void returnsMethod() throws IOException {
MatcherAssert.assertThat(
new RqMethod.Base(new RqFake(RqMethod.POST)).method(),
new RqBaseMethod(new RqFake(RqMethod.POST)).method(),
Matchers.equalTo(RqMethod.POST)
);
}
Expand All @@ -55,7 +55,7 @@ void supportsAllStandardMethods() throws IOException {
)
) {
MatcherAssert.assertThat(
new RqMethod.Base(new RqFake(method)).method(),
new RqBaseMethod(new RqFake(method)).method(),
Matchers.equalTo(method)
);
}
Expand All @@ -65,14 +65,14 @@ void supportsAllStandardMethods() throws IOException {
void supportsExtensionMethods() throws IOException {
final String method = "CUSTOM";
MatcherAssert.assertThat(
new RqMethod.Base(new RqFake(method)).method(),
new RqBaseMethod(new RqFake(method)).method(),
Matchers.equalTo(method)
);
}

@Test
void failsOnMissingUri() {
final RqMethod.Base req = new RqMethod.Base(
final RqBaseMethod req = new RqBaseMethod(
new RqSimple(Arrays.asList("GET"), null)
);
Assertions.assertThrows(
Expand All @@ -85,7 +85,7 @@ void failsOnMissingUri() {
void failsOnExtraLineElement() {
Assertions.assertThrows(
IOException.class,
() -> new RqMethod.Base(
() -> new RqBaseMethod(
new RqSimple(Arrays.asList("GET / HTTP/1.1 abc"), null)
).method()
);
Expand All @@ -95,7 +95,7 @@ void failsOnExtraLineElement() {
void failsOnExtraSpaces() {
Assertions.assertThrows(
IOException.class,
() -> new RqMethod.Base(
() -> new RqBaseMethod(
new RqSimple(Arrays.asList("GET / HTTP/1.1"), null)
).method()
);
Expand All @@ -105,7 +105,7 @@ void failsOnExtraSpaces() {
void failsOnSeparatorsInExtensionMethod() {
Assertions.assertThrows(
IOException.class,
() -> new RqMethod.Base(new RqFake("CUSTO{M)")).method()
() -> new RqBaseMethod(new RqFake("CUSTO{M)")).method()
);
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/takes/tk/TkProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.takes.facets.fork.FkMethods;
import org.takes.facets.fork.TkFork;
import org.takes.http.FtRemote;
import org.takes.rq.RqBaseMethod;
import org.takes.rq.RqFake;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;
Expand Down Expand Up @@ -277,7 +278,7 @@ void addsAllInitialHeaders() throws Exception {
}

private static Request createEchoRequest(final Request req) throws IOException {
final String method = new RqMethod.Base(req).method();
final String method = new RqBaseMethod(req).method();
return TkProxyTest.NOBODIES.getOrDefault(method, rq -> req).create(req);
}

Expand Down

0 comments on commit f047e75

Please sign in to comment.