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

Show meaningful error when xmlrpc broken #48

Merged
merged 7 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ PODS:
- OHHTTPStubs/Swift (6.1.0):
- OHHTTPStubs/Default
- UIDeviceIdentifier (0.5.0)
- WordPressKit (1.4.3-beta.1):
- WordPressKit (1.4.3-beta.2):
- Alamofire (~> 4.7.3)
- CocoaLumberjack (= 3.4.2)
- NSObject-SafeExpectations (= 0.0.3)
Expand Down Expand Up @@ -70,7 +70,7 @@ SPEC CHECKSUMS:
OCMock: ebe9ee1dca7fbed0ff9193ac0b3e2d8862ea56f6
OHHTTPStubs: 1e21c7d2c084b8153fc53d48400d8919d2d432d0
UIDeviceIdentifier: a959a6d4f51036b4180dd31fb26483a820f1cc46
WordPressKit: 07501ceb3e9cbebbbe64ad02dcaff44807406450
WordPressKit: 7b6fec9b382c0a9f553b82a4eb133ad685b9f879
WordPressShared: 7ef0253d54989b195e020a74100a8500be0a4315
wpxmlrpc: bfc572f62ce7ee897f6f38b098d2ba08732ecef4

Expand Down
2 changes: 1 addition & 1 deletion WordPressKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "WordPressKit"
s.version = "1.4.3-beta.1"
s.version = "1.4.3-beta.2"

s.summary = "WordPressKit offers a clean and simple WordPress.com and WordPress.org API."

Expand Down
15 changes: 13 additions & 2 deletions WordPressKit/WordPressOrgXMLRPCValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CocoaLumberjack
case forbidden = 403 // Server returned a 403 error while reading xmlrpc file
case blocked = 405 // Server returned a 405 error while reading xmlrpc file
case invalid // Doesn't look to be valid XMLRPC Endpoint.
case xmlrpc_missing // site contains RSD link but XML-RPC information is missing

public var localizedDescription: String {
switch (self) {
Expand All @@ -25,11 +26,13 @@ import CocoaLumberjack
case .mobilePluginRedirectedError:
return NSLocalizedString("You seem to have installed a mobile plugin from DudaMobile which is preventing the app to connect to your blog", comment: "")
case .invalid:
return NSLocalizedString("We're sure this is a great site - but it's not a WordPress site, so you can't connect to it with this app.", comment: "Error message shown a URL points to a valid site but not a WordPress site.")
return NSLocalizedString("Couldn't connect to the WordPress site. There is no valid WordPress site at this address. Check the site address (URL) you entered.", comment: "Error message shown a URL points to a valid site but not a WordPress site.")
case .blocked:
return NSLocalizedString("Couldn't connect. Your host is blocking POST requests, and the app needs that in order to communicate with your site. Contact your host to solve this problem.", comment: "Message to show to user when he tries to add a self-hosted site but the host returned a 405 error, meaning that the host is blocking POST requests on /xmlrpc.php file.")
case .forbidden:
return NSLocalizedString("Couldn't connect. We received a 403 error when trying to access your site XMLRPC endpoint. The app needs that in order to communicate with your site. Contact your host to solve this problem.", comment: "Message to show to user when he tries to add a self-hosted site but the host returned a 403 error, meaning that the access to the /xmlrpc.php file is forbidden.")
case .xmlrpc_missing:
return NSLocalizedString("Couldn't connect. Required XML-RPC methods are missing on the server.", comment: "Message to show to user when he tries to add a self-hosted site with RSD link present, but xmlrpc is missing.")
}
}
}
Expand Down Expand Up @@ -192,6 +195,7 @@ open class WordPressOrgXMLRPCValidator: NSObject {
redirectCount: Int = 0,
success: @escaping (_ xmlrpcURL: URL) -> (),
failure: @escaping (_ error: NSError) -> ()) {

guard redirectCount < redirectLimit else {
let error = NSError(domain: URLError.errorDomain,
code: URLError.httpTooManyRedirects.rawValue,
Expand Down Expand Up @@ -247,6 +251,8 @@ open class WordPressOrgXMLRPCValidator: NSObject {
success: @escaping (_ xmlrpcURL: URL) -> (),
failure: @escaping (_ error: NSError) -> ()) {
DDLogInfo("Fetch the original url and look for the RSD link by using RegExp")

var isWpSite = false
let session = URLSession(configuration: URLSessionConfiguration.ephemeral)
let dataTask = session.dataTask(with: htmlURL, completionHandler: { (data, response, error) in
if let error = error {
Expand All @@ -261,6 +267,9 @@ open class WordPressOrgXMLRPCValidator: NSObject {
return
}

// If the site contains RSD link, it is WP.org site
isWpSite = true

// Try removing "?rsd" from the url, it should point to the XML-RPC endpoint
let xmlrpc = rsdURL.replacingOccurrences(of: "?rsd", with: "")
if xmlrpc != rsdURL {
Expand All @@ -273,7 +282,9 @@ open class WordPressOrgXMLRPCValidator: NSObject {
if error.code == 403 || error.code == 405, let xmlrpcValidatorError = error as? WordPressOrgXMLRPCValidatorError {
failure(xmlrpcValidatorError as NSError)
} else {
failure(WordPressOrgXMLRPCValidatorError.invalid as NSError)
let validatorError = isWpSite ? WordPressOrgXMLRPCValidatorError.xmlrpc_missing :
WordPressOrgXMLRPCValidatorError.invalid
failure(validatorError as NSError)
}
})
} else {
Expand Down