java.net.URL/URI consider an invalid URL as valid - java

Not all invalid urls will be consider as invalid by java.net.URL/URI
An example: http:www.google.com
Is it a bug or is it a feature?
Or is maybe the string above a valid url?

Without //, the URL is treated as a relative URL, that needs a context to be useful.
If you try using it, you'll get this error:
IllegalArgumentException: protocol = http host = null

Related

Is a HTTP request with request target to a proxy with query and fragment, a valid HTTP request?

My goal is to validate HTTP request of the "request-line" e.g GET http://www.ics.uci.edu/pub/ietf/uri/#Related HTTP/1.1. Currently, I am validating the "request-target"
My reference is RFC7230. Unfortunately, RFC7230 does not specify the term of absolute-form, a new term from the documentation with a confusing reason because the documentation also state absolute-form = absolute-uri. So, I am not sure whether that is a valid request-line.
I am using a RegEx to validate the request target.
static void parse(String requestTarget) throws HttpParsingException {
Pattern pattern = Pattern.compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(requestTarget);
if (!matcher.matches()) {
throw new HttpParsingException(HttpStatusCode.CLIENT_ERROR_400_BAD_REQUEST);
}
}
The request target values are (expected to success)
GET http://www.ics.uci.edu/pub/ietf/uri/#Related HTTP/1.1
GET / HTTP/1.1
GET /where
GET /where?q=now
The request target values are (expected to fail)
GET // HTTP/1.1
GET HTTP/1.1
The RegEx is from the Appendix B of [RFC3896]
Is a HTTP request with request target to a proxy with query and fragment, a valid HTTP request?
It is not valid.
The HTTP Message Syntax and Message Routing RFC requires that the request-target URL in an HTTP request message is an absolute-uri. (I see you have figured that out already ...)
If you track the absolute-uri term that back to its definition in the URI RFC, you will see that it means that a fragment is not allowed. The URI RFC says:
4.3. Absolute URI
Some protocol elements allow only the absolute form of a URI without a
fragment identifier. For example, defining a base URI for later use
by relative references calls for an absolute-URI syntax rule that does
not allow a fragment.
absolute-URI = scheme ":" hier-part [ "?" query ]
Note: that syntax does not allow a fragment.
I am not going to buy into the issue of writing regexes to do the URI parsing ... except to suggest that an alternative is to use the standard Java SE URI class.

Can I have a custom protocol on java type URL?

I'm writing (in Kotlin) below, and all good
val url = URL("http://my-page/content?page=0")
However, I'm trying to make a customer scheme (i.e use "myprotocol" instead of "http").
val url = URL("myprotocol://my-page/content?page=0")
It will crash Caused by: java.net.MalformedURLException: unknown protocol: myprotocol
Is there a way for me to have it allow a custom protocol?
i.e. I want to use it same as normal URL format, so I could extract the path, query etc, except that the protocol (scheme) is a custom one.
#JBNizet answer is perfect. Just use
val url = URI("http://my-page/content?page=0")

How to fix the URI does not specify a valid host name in ClientProtocolException:

When I execute the elastic search java API using jest client, I face the following error
org.apache.http.client.ClientProtocolException: URI does not specify a valid host name:localhost:9200/index/type/_search
I tries various possibilities to fix this error, but I am stuck. How can I fix this?
You just need to add http:// at the beginning of the URI.
If you are seeing the same error in JMeter while you want to use a response Json key value in another variable(by creating a new HTTP Request) then use JSON Extractor instead of Regular Expression extractor. Thread Group->Right click and Add->Post Processor->JSON Extractor)
enter image description here

how to get URL from address bar in JSP

Lets say I hit
http://localhost/webapp/wcs/stores/servlet/en/marksandspencer/l/women/dresses/party-and-cocktail-dresses
and this internally redirects me to custom 404.jsp page, But URL remain same in address bar.
I tried this code - <%= request.getAttribute("javax.servlet.forward.request_uri") %>; and it's returning me the path of 404.jsp
How can I get the entered URL which is there in address bar?
Use request.getAttribute("javax.servlet.error.request_uri") to get URI of requested page that not found (404 error). Check this: https://tomcat.apache.org/tomcat-7.0-doc/servletapi/constant-values.html
When error raised (because of some reason such as page not found (404), Internal Server Error (500), ...), the servlet engine will FORWARD the request to corresponding error page (configured in web.xml) using ERROR dispatcher type, NOT FORWARD dispatcher type so that is the reason we must use javax.servlet.error.request_uri, NOT use javax.servlet.forward.request_uri
I think you were close. javax.servlet.forward.request_uri is for normal forwarding, but for 404, you need javax.servlet.error.request_uri.
You can use :
String url = request.getRequestURL().toString();
but this doesn't hold Query String. So, to get query string, you may call
request.getQueryString()
You can do this to get the whole URL including parameters.
request.getRequestURL()+""+request.getQueryString();
use request.getHeader("Referer").
referer gives a url from where you redirected.

URL Malformed exception

I need to make a URL request from Java.
The problem is the following :
in Java, while creating the URL I can not skip the protocol (http:// or https://) ... and in the browser I only get to access the resource if I put as address the IP (without http:// or https://).
Any ideas?

Categories