Skip to content

Commit

Permalink
Transform URL to have proper host and protocol when fetching feed
Browse files Browse the repository at this point in the history
fixes #92
  • Loading branch information
msasikanth committed Sep 15, 2023
1 parent 73fca34 commit e505e31
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,26 @@ class FeedFetcher(private val httpClient: HttpClient, private val feedParser: Fe

suspend fun fetch(url: String): FeedFetchResult {
return try {
val transformedUrl = URLBuilder(url).apply { protocol = URLProtocol.HTTPS }.build()
val response = httpClient.get(transformedUrl)
// Currently Ktor Url parses relative URLs,
// if it fails to properly parse the given URL, it
// default to localhost.
//
// This will cause the network call to fail,
// so we are setting the host manually
// https://youtrack.jetbrains.com/issue/KTOR-360
val transformedUrl =
URLBuilder()
.apply {
protocol = URLProtocol.HTTPS
host = url.replace(Regex("^https?://"), "").replace(Regex("^www\\."), "")
}
.build()

val response = httpClient.get(transformedUrl.toString())

when (response.status) {
HttpStatusCode.OK -> {
parseContent(response, url)
parseContent(response, transformedUrl.toString())
}
HttpStatusCode.MultipleChoices,
HttpStatusCode.MovedPermanently,
Expand Down

0 comments on commit e505e31

Please sign in to comment.