andrewducker (
andrewducker) wrote2011-11-24 08:07 am
Anyone here know much about Java?
I have user input for a URL field. I want them to be able to enter anything from "http://andrewducker.wordpress.com/xmlrpc.php" to "andrewducker.wordpress.com" and be able to end up at the same end point.
I've wasted a couple of hours messing around with the various constructors for URL and not got to anywhere satisfactory, should I just do string checking and construct it myself?
I should make it clear - I always want the /xmlrpc.php bit to be what's on the end of the URL, that's a Wordpress standard, so I don't need to do any complex discovery. I just need to append that if it's not there.
I was hoping that someone would have written a class that could append bits of URLs together, but the basic stuff in the built in URL class doesn't quite cut it.
I've wasted a couple of hours messing around with the various constructors for URL and not got to anywhere satisfactory, should I just do string checking and construct it myself?
I should make it clear - I always want the /xmlrpc.php bit to be what's on the end of the URL, that's a Wordpress standard, so I don't need to do any complex discovery. I just need to append that if it's not there.
I was hoping that someone would have written a class that could append bits of URLs together, but the basic stuff in the built in URL class doesn't quite cut it.
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject
Of course that's not going to work if their wordpress setup is behind https, but I don't know if that's at all likely. Or if it sits in a subdirectory, so that it's
"https://AShareddomain.co.uk/andy/blog/xmlrpc.php"
no subject
if ($url !~ m{^ https?:// }x) {
$url = 'http://' . $url;
}
if ($url !~ m{ /xmlrpc\.php $ }x) {
$url .= '/xmlrpc.php';
}
And then use your standard libraries to check whether that URL is valid or not. (Validation step might just be to call the resulting "url" and see if it works.)
no subject
s?to tge regex afterhttp. The other issue is handled already - the.*is greedy, and consumes as many characters as it can.Of course, we're now up to two edge cases with no guarantee we've thought of them all, which is the usual weakness of regexp-based approaches.
no subject
if first four characters are not "http" prepend "http://"
if last 10 characters are not "xmlrpc.php" then (check if the last character is a / and append one if not) append "xmlrpc.php"
As that basically satisfies my problem without involving regexes at all, which might be simpler :->
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject
1) construct a Uri from the string
2) read off some of the properties of the uri. Something like uri.Scheme + uri.Host + "/xmlrpc.php" should be what you want (unless port numbers are involved).
no subject
no subject
no subject
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
no subject