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

Add HttpUrl class #49

Merged
merged 4 commits into from
Apr 17, 2019
Merged

Add HttpUrl class #49

merged 4 commits into from
Apr 17, 2019

Conversation

uschi2000
Copy link
Contributor

No description provided.

@uschi2000
Copy link
Contributor Author

fyi @ellisjoe

@uschi2000
Copy link
Contributor Author

nvm, will have to tweak this to be correct.

import java.util.List;

/** A simplistic URL builder, not tuned for performance. */
public final class HttpUrl {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe UrlBuilder?

Copy link
Contributor

Choose a reason for hiding this comment

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

also might consider just making the top-level class the builder and have the 'build' method be toUrl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

private String host;
private int port = -1;
private List<String> pathSegments = new ArrayList<>();
private List<String> queryNamesAndValues = new ArrayList<>(); // alternating (name, value) pairs
Copy link
Contributor

Choose a reason for hiding this comment

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

might make more sense for this to be a Multimap

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suspect the list construction has fewer allocations; I think it's OK since it's just used internally?


private static String encodePath(List<String> pairs) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < pairs.size(); i += 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

possibly nicer to write this as:

Joiner.on("/").join(paths)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done here, but not below to save object allocations

return URLEncoder.encode(string, StandardCharsets.UTF_8);
}

private static String encodePath(List<String> pairs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

pairs -> paths

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@markelliot
Copy link
Contributor

you may want to consider looking at URI, the docs on URL say this:

The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL().

@uschi2000
Copy link
Contributor Author

problem with the URI class is that it cannot distinguish between url.pathSegment("foo").pathSegment("bar") (which gives http://host/foo/bar) and url.pathSegment("foo/bar") (which gives http://host/foo%2Fbar).

@uschi2000
Copy link
Contributor Author

take another look please, @markelliot

@uschi2000
Copy link
Contributor Author

take another look please, @markelliot

@uschi2000 uschi2000 merged commit 83e0db0 into develop Apr 17, 2019
@uschi2000 uschi2000 deleted the rfink/http-url branch April 17, 2019 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants