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

#473 Refactoring Request-Line validation #484

Merged
merged 11 commits into from
Jan 6, 2016
51 changes: 0 additions & 51 deletions src/main/java/org/takes/http/BkReuse.java

This file was deleted.

34 changes: 3 additions & 31 deletions src/main/java/org/takes/rq/RqHref.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
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;
import org.takes.misc.Href;
import org.takes.rq.RqRequestLine.Token;

/**
* HTTP URI query parsing.
Expand Down Expand Up @@ -63,15 +62,6 @@ public interface RqHref extends Request {
@EqualsAndHashCode(callSuper = true)
final class Base extends RqWrap implements RqHref {

/**
* HTTP Request-Line pattern.
* @see <a href="http://www
* .w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1">RFC 2616</a>
*/
private static final Pattern REQUEST_PATTERN = Pattern.compile(
"([!-~]+) ([^ ]+)( [^ ]+)?"
);

/**
* Ctor.
* @param req Original request
Expand All @@ -80,28 +70,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)
.requestLineHeaderToken(Token.URI);
return new Href(
String.format(
"http://%s%s",
Expand Down
20 changes: 3 additions & 17 deletions src/main/java/org/takes/rq/RqMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

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;
import org.takes.rq.RqRequestLine.Token;

/**
* HTTP method parsing.
Expand Down Expand Up @@ -112,14 +112,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
Expand All @@ -130,14 +122,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)
.requestLineHeaderToken(Token.METHOD);
if (SEPARATORS.matcher(method).find()) {
throw new IOException(
String.format("Invalid HTTP method: %s", method)
Expand Down
181 changes: 181 additions & 0 deletions src/main/java/org/takes/rq/RqRequestLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**
* 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.
*
* <p>All implementations of this interface must be immutable and thread-safe.
*
* @author Vladimir Maksimenko (xupypr@xupypr.com)
* @version $Id$
* @since 0.29.1
*/
public interface RqRequestLine extends Request {

public static enum Token {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xupyprmv oh, that's a very bad idea... just create methods for each "token" you need to get. Using "tokens" as method configurators is a very bad idea. I will review the rest soon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 Do you have any remarks about other parts of PR?

/**
* 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;
}
}

/**
* Get Request-Line header.
* @return HTTP Request-Line header
* @throws IOException If fails
*/
String requestLineHeader() throws IOException;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xupyprmv why not just header? there are many headers? we really need to say "request line header" in order not to confuse it with some other header? see http://www.yegor256.com/2015/01/12/compound-name-is-code-smell.html keep in mind, the longer the name - the more messy is your code. if you really need long names - it's a sign that your code needs refactoring


/**
* Get Request-Line header token.
* @param token Token
* @return HTTP Request-Line header token
* @throws IOException If fails
*/
String requestLineHeaderToken(Token token) throws IOException;

/**
* Request decorator for Request-Line header validation
*
* <p>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 <a href="http://www
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xupyprmv this won't work with JavaDoc. just put it all in one long line, checkstyle won't complain

* .w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1">RFC 2616</a>
*/
private static final Pattern PATTERN = Pattern.compile(
"([!-~]+) ([^ ]+)( [^ ]+)?"
);

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

@Override
public String requestLineHeader() throws IOException {
final String requestLine = this.getRequestLineHeader();
this.validateRequestLine(requestLine);
return requestLine;
}

@Override
public String requestLineHeaderToken(final Token token)
throws IOException {
final String requestLine = this.getRequestLineHeader();
final Matcher matcher = this.validateRequestLine(requestLine);
final String result = matcher.group(token.value);
if (result == null) {
throw new IllegalArgumentException(
String.format(
"There is no token %s in Request-Line header: %s",
token.toString(),
requestLine
)
);
}
return result.trim();
}

/**
* Get Request-Line header.
*
* @return Valid Request-Line header
* @throws IOException If fails
*/
private String getRequestLineHeader() throws IOException {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xupyprmv maybe just line() is good enough?

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.
*
* @param requestline Request-Line header
* @return Matcher that can be used to extract tokens
* @throws HttpException If fails
*/
private Matcher validateRequestLine(final String requestline)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xupyprmv requestline is a compound name, don't use it. just line is enough

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xupyprmv it's a bad name for the method. it should be called matcher, not validate

throws HttpException {
final Matcher matcher = PATTERN.matcher(requestline);
if (!matcher.matches()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
"Invalid HTTP Request-Line header: %s",
requestline
)
);
}
return matcher;
}
}
}
Loading