Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add access to host name in Request API
Make HTTP header utility classes more consistent in their usage. Closes #55
- Loading branch information
1 parent
382cbd7
commit d04db79
Showing
19 changed files
with
220 additions
and
83 deletions.
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
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
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,49 @@ | ||
package com.vtence.molecule.http; | ||
|
||
import com.vtence.molecule.Request; | ||
|
||
import static com.vtence.molecule.http.HeaderNames.HOST; | ||
|
||
public class Host { | ||
private final String hostname; | ||
private final Integer port; | ||
|
||
public static Host of(Request request) { | ||
String header = request.header(HOST); | ||
return header != null ? parse(header) : null; | ||
} | ||
|
||
public static Host parse(String header) { | ||
return new Host(parseHostName(header), parsePort(header)); | ||
} | ||
|
||
private static String parseHostName(String header) { | ||
if (header.startsWith("[")) { | ||
return header.substring(1, header.indexOf(']')); | ||
} | ||
|
||
int colonIndex = header.indexOf(':'); | ||
return colonIndex != -1 ? header.substring(0, colonIndex) : header; | ||
} | ||
|
||
private static Integer parsePort(String header) { | ||
int colonIndex = header.startsWith("[") ? | ||
header.indexOf(':', header.indexOf(']')) : | ||
header.indexOf(':'); | ||
|
||
return colonIndex != -1 ? Integer.parseInt(header.substring(colonIndex + 1)) : null; | ||
} | ||
|
||
public Host(String hostname, Integer port) { | ||
this.hostname = hostname; | ||
this.port = port; | ||
} | ||
|
||
public String name() { | ||
return hostname; | ||
} | ||
|
||
public int port(int defaultPort) { | ||
return port != null ? port : defaultPort; | ||
} | ||
} |
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
Oops, something went wrong.