-
Notifications
You must be signed in to change notification settings - Fork 203
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
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
640823e
Refactoring for `RqHref.java:83-86: RqMethod already validates Reques…
xupyprmv 20c6806
annotation fix
xupyprmv f43f53b
checkstyle modifications
xupyprmv 6665b3d
#473 refactoring according to CR
xupyprmv ffb721e
smallfix
xupyprmv dd49071
identation fix
xupyprmv b66a214
rollback
xupyprmv a9d7c99
refactoring for #473 according to @yegor256 CR
xupyprmv a28422d
#473 refacroting according to @yegor256 CR
xupyprmv a961981
check style modifications
xupyprmv c343a53
#473 more FP
xupyprmv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
/** | ||
* 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 | ||
*/ | ||
@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 | ||
* | ||
* <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.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1">RFC 2616</a> | ||
*/ | ||
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 { | ||
final String line = this.line(); | ||
this.matcher(line); | ||
return 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 { | ||
final String result = this.matcher(this.line()) | ||
.group(token.value); | ||
if (result == null) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"There is no token %s in Request-Line header", | ||
token.toString() | ||
) | ||
); | ||
} | ||
return result.trim(); | ||
} | ||
|
||
/** | ||
* 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. | ||
* | ||
* @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( | ||
"Invalid HTTP Request-Line header: %s", | ||
line | ||
) | ||
); | ||
} | ||
return matcher; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xupyprmv read this one again: http://www.yegor256.com/2015/12/08/temporal-coupling-between-method-calls.html Your code here should look like this:
See the idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xupyprmv maybe I'm wrong with this exact example, but
this.matcher(line)
is a code smell. You should never use functions as procedures, ignoring their returns.