diff --git a/src/main/java/org/takes/rq/RqHref.java b/src/main/java/org/takes/rq/RqHref.java index f52b61321..709e10507 100644 --- a/src/main/java/org/takes/rq/RqHref.java +++ b/src/main/java/org/takes/rq/RqHref.java @@ -27,8 +27,6 @@ import java.io.InputStream; import java.net.HttpURLConnection; import java.util.Iterator; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import lombok.EqualsAndHashCode; import org.takes.HttpException; import org.takes.Request; @@ -63,15 +61,6 @@ public interface RqHref extends Request { @EqualsAndHashCode(callSuper = true) final class Base extends RqWrap implements RqHref { - /** - * HTTP Request-Line pattern. - * @see RFC 2616 - */ - private static final Pattern REQUEST_PATTERN = Pattern.compile( - "([!-~]+) ([^ ]+)( [^ ]+)?" - ); - /** * Ctor. * @param req Original request @@ -80,28 +69,10 @@ public Base(final Request req) { super(req); } - // @todo #445:30min/DEV RqMethod already validates Request-Line and - // extracts HTTP Method from it. We should extract all important - // information from Request-Line (HTTP method, URI and HTTP version) - // in one place to enforce DRY principle. @Override public Href href() throws IOException { - if (!this.head().iterator().hasNext()) { - throw new HttpException( - HttpURLConnection.HTTP_BAD_REQUEST, - "HTTP Request should have Request-Line" - ); - } - final String line = this.head().iterator().next(); - final Matcher matcher = - REQUEST_PATTERN.matcher(line); - if (!matcher.matches()) { - throw new HttpException( - HttpURLConnection.HTTP_BAD_REQUEST, - String.format("Illegal Request-Line: %s", line) - ); - } - final String uri = matcher.group(2); + final String uri = new RqRequestLine.Base(this) + .uri(); return new Href( String.format( "http://%s%s", diff --git a/src/main/java/org/takes/rq/RqMethod.java b/src/main/java/org/takes/rq/RqMethod.java index 7818c4db4..db447d5f6 100644 --- a/src/main/java/org/takes/rq/RqMethod.java +++ b/src/main/java/org/takes/rq/RqMethod.java @@ -25,7 +25,6 @@ import java.io.IOException; import java.util.Locale; -import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.EqualsAndHashCode; import org.takes.Request; @@ -112,14 +111,6 @@ final class Base extends RqWrap implements RqMethod { "[()<>@,;:\\\"/\\[\\]?={}]" ); - /** - * HTTP method line pattern. - * [!-~] is for method or extension-method token (octets 33 - 126). - */ - private static final Pattern PATTERN = Pattern.compile( - "([!-~]+) [^ ]+( [^ ]+){0,1}" - ); - /** * Ctor. * @param req Original request @@ -130,14 +121,8 @@ public Base(final Request req) { @Override public String method() throws IOException { - final String line = this.head().iterator().next(); - final Matcher matcher = PATTERN.matcher(line); - if (!matcher.matches()) { - throw new IOException( - String.format("Invalid HTTP method line: %s", line) - ); - } - final String method = matcher.group(1); + final String method = new RqRequestLine.Base(this) + .method(); if (SEPARATORS.matcher(method).find()) { throw new IOException( String.format("Invalid HTTP method: %s", method) diff --git a/src/main/java/org/takes/rq/RqRequestLine.java b/src/main/java/org/takes/rq/RqRequestLine.java new file mode 100644 index 000000000..48781a60f --- /dev/null +++ b/src/main/java/org/takes/rq/RqRequestLine.java @@ -0,0 +1,246 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 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.net.HttpURLConnection; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import lombok.EqualsAndHashCode; +import org.takes.HttpException; +import org.takes.Request; + +/** + * HTTP Request-Line parsing. + * + *

All implementations of this interface must be immutable and thread-safe. + * + * @author Vladimir Maksimenko (xupypr@xupypr.com) + * @version $Id$ + * @since 0.29.1 + */ +@SuppressWarnings("PMD.TooManyMethods") +public interface RqRequestLine extends Request { + + /** + * Get Request-Line header. + * @return HTTP Request-Line header + * @throws IOException If fails + */ + String header() throws IOException; + + /** + * Get Request-Line method token. + * @return HTTP Request-Line method token + * @throws IOException If fails + */ + String method() throws IOException; + + /** + * Get Request-Line Request-URI token. + * @return HTTP Request-Line method token + * @throws IOException If fails + */ + String uri() throws IOException; + + /** + * Get Request-Line HTTP-Version token. + * @return HTTP Request-Line method token + * @throws IOException If fails + */ + String version() throws IOException; + + /** + * Request decorator for Request-Line header validation + * + *

The class is immutable and thread-safe. + * @author Vladimir Maksimenko (xupypr@xupypr.com) + * @version $Id$ + * @since 1.0 + */ + @EqualsAndHashCode(callSuper = true) + final class Base extends RqWrap implements RqRequestLine { + /** + * HTTP Request-line pattern. + * [!-~] is for method or extension-method token (octets 33 - 126). + * @see RFC 2616 + */ + private static final Pattern PATTERN = Pattern.compile( + "([!-~]+) ([^ ]+)( [^ ]+)?" + ); + + private static enum Token { + /** + * METHOD token. + */ + METHOD(1), + + /** + * URI token. + */ + URI(2), + + /** + * HTTPVERSION token. + */ + HTTPVERSION(3); + + /** + * Value. + */ + private final int value; + + /** + * Ctor. + * @param val Value + */ + private Token(final int val) { + this.value = val; + } + } + + /** + * Ctor. + * @param req Original request + */ + public Base(final Request req) { + super(req); + } + + @Override + public String header() throws IOException { + return this.validated(this.line()); + } + + @Override + public String method() throws IOException { + return this.token(Token.METHOD); + } + + @Override + public String uri() throws IOException { + return this.token(Token.URI); + } + + @Override + public String version() throws IOException { + return this.token(Token.HTTPVERSION); + } + + /** + * Get Request-Line header token. + * @param token Token + * @return HTTP Request-Line header token + * @throws IOException If fails + */ + private String token(final Token token) + throws IOException { + return this.trimmed( + this.matcher(this.line()).group(token.value), + token + ); + } + + /** + * Get Request-Line header. + * + * @return Valid Request-Line header + * @throws IOException If fails + */ + private String line() throws IOException { + if (!this.head().iterator().hasNext()) { + throw new HttpException( + HttpURLConnection.HTTP_BAD_REQUEST, + "HTTP Request should have Request-Line" + ); + } + return this.head().iterator().next(); + } + + /** + * Validate Request-Line according to PATTERN + * and return matcher. + * + * @param line Request-Line header + * @return Matcher that can be used to extract tokens + * @throws HttpException If fails + */ + private Matcher matcher(final String line) + throws HttpException { + final Matcher matcher = PATTERN.matcher(line); + if (!matcher.matches()) { + throw new HttpException( + HttpURLConnection.HTTP_BAD_REQUEST, + String.format( + // @checkstyle MultipleStringLiteralsCheck (1 line) + "Invalid HTTP Request-Line header: %s", + line + ) + ); + } + return matcher; + } + + /** + * Validate Request-Line according to PATTERN. + * + * @param line Request-Line header + * @return Validated Request-Line header + * @throws HttpException If fails + */ + private String validated(final String line) throws HttpException { + if (!PATTERN.matcher(line).matches()) { + throw new HttpException( + HttpURLConnection.HTTP_BAD_REQUEST, + String.format( + // @checkstyle MultipleStringLiteralsCheck (1 line) + "Invalid HTTP Request-Line header: %s", + line + ) + ); + } + return line; + } + + /** + * Check that token value is not null and + * return trimmed value. + * + * @param value Token value + * @param token Token + * @return Trimmed token value + */ + private String trimmed(final String value, final Token token) { + if (value == null) { + throw new IllegalArgumentException( + String.format( + "There is no token %s in Request-Line header", + token.toString() + ) + ); + } + return value.trim(); + } + } +} diff --git a/src/test/java/org/takes/rq/RqRequestLineTest.java b/src/test/java/org/takes/rq/RqRequestLineTest.java new file mode 100644 index 000000000..a01ac53eb --- /dev/null +++ b/src/test/java/org/takes/rq/RqRequestLineTest.java @@ -0,0 +1,217 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 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.Arrays; +import java.util.Collections; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.takes.HttpException; + +/** + * Test case for {@link RqRequestLine.Base}. + * @author Vladimir Maksimenko (xupypr@xupypr.com) + * @version $Id$ + * @since 0.29.1 + */ +@SuppressWarnings("PMD.TooManyMethods") +public final class RqRequestLineTest { + + /** + * RqRequestLine.Base should throw {@link HttpException} when + * we call requestLineHeader with + * Request without Request-Line. + * @throws IOException If some problem inside + */ + @Test(expected = HttpException.class) + public void failsOnAbsentRequestLine() throws IOException { + new RqRequestLine.Base( + new RqSimple(Collections.emptyList(), null) + ).header(); + } + + /** + * RqRequestLine.Base should throw {@link HttpException} when + * we call requestLineHeader with + * Request with illegal Request-Line. + * @throws IOException If some problem inside + */ + @Test(expected = HttpException.class) + public void failsOnIllegalRequestLine() throws IOException { + new RqRequestLine.Base( + new RqFake( + Arrays.asList( + "GIVE/contacts2", + "Host: 1.example.com" + ), + "" + ) + ).header(); + } + + /** + * RqRequestLine.Base can return Request-Line header + * we call requestLineHeader with valid Request-Line. + * @throws IOException If some problem inside + */ + @Test + public void extractsParams() throws IOException { + final String requestline = "GET /hello?a=6&b=7&c&d=9%28x%29&ff"; + MatcherAssert.assertThat( + new RqRequestLine.Base( + new RqFake( + Arrays.asList( + requestline, + "Host: a.example.com", + "Content-type: text/xml" + ), + "" + ) + ).header(), + Matchers.equalToIgnoringCase(requestline) + ); + } + + /** + * RqRequestLine.Base should throw {@link HttpException} when + * we call requestLineHeaderToken with + * Request without Request-Line. + * @throws IOException If some problem inside + */ + @Test(expected = HttpException.class) + public void failsOnAbsentRequestLineToken() throws IOException { + new RqRequestLine.Base( + new RqSimple(Collections.emptyList(), null) + ).method(); + } + + /** + * RqRequestLine.Base should throw {@link HttpException} when + * we call requestLineHeaderToken with + * Request with illegal Request-Line. + * @throws IOException If some problem inside + */ + @Test(expected = HttpException.class) + public void failsOnIllegalRequestLineToken() throws IOException { + new RqRequestLine.Base( + new RqFake( + Arrays.asList( + "GIVE/contacts", + "Host: 3.example.com" + ), + "" + ) + ).method(); + } + + /** + * RqRequestLine.Base can extract first token (METHOD) + * when we call requestLineHeaderToken + * with valid Request-Line. + * @throws IOException If some problem inside + */ + @Test + public void extractsFirstParam() throws IOException { + MatcherAssert.assertThat( + new RqRequestLine.Base( + new RqFake( + Arrays.asList( + "GET /hello?since=3431", + "Host: f1.example.com" + ), + "" + ) + ).method(), + Matchers.equalToIgnoringCase("GET") + ); + } + + /** + * RqRequestLine.Base can extract second token (URI) + * when we call requestLineHeaderToken + * with valid Request-Line. + * @throws IOException If some problem inside + */ + @Test + public void extractsSecondParam() throws IOException { + MatcherAssert.assertThat( + new RqRequestLine.Base( + new RqFake( + Arrays.asList( + "GET /hello?since=3432", + "Host: f2.example.com" + ), + "" + ) + ).uri(), + Matchers.equalToIgnoringCase("/hello?since=3432") + ); + } + + /** + * RqRequestLine.Base can extract third token (HTTP VERSION) + * when we call requestLineHeaderToken + * with valid Request-Line. + * @throws IOException If some problem inside + */ + @Test + public void extractsThirdParam() throws IOException { + MatcherAssert.assertThat( + new RqRequestLine.Base( + new RqFake( + Arrays.asList( + "GET /hello?since=343 HTTP/1.1", + "Host: f3.example.com" + ), + "" + ) + ).version(), + Matchers.equalToIgnoringCase("HTTP/1.1") + ); + } + + /** + * RqRequestLine.Base should throw {@link IllegalArgumentException} + * when we call requestLineHeaderToken(Token.HTTPVERSION) + * even for valid Request-Line without HTTP VERSION. + * @throws IOException If some problem inside + */ + @Test(expected = IllegalArgumentException.class) + public void extractsEmptyThirdParam() throws IOException { + MatcherAssert.assertThat( + new RqRequestLine.Base( + new RqFake( + Arrays.asList( + "GET /hello?since=3433", + "Host: f4.example.com" + ), + "" + ) + ).version(), + Matchers.equalTo(null) + ); + } +}