Skip to content
Permalink
Browse files

Added URL support for file:///

Added support for AjaxRequests with non-POST
Added support for remote logging to use a different URL
  • Loading branch information...
darkfrog26 committed Aug 21, 2019
1 parent c5da38e commit 0817c3a6c9a0ce5a4fa71afa120785cb21be5dd1
@@ -98,7 +98,8 @@ trait ClientApplication extends YouIApplication with ScreenManager {
}

object ClientApplication {
def logWriter(maximumBytes: Long = -1L,
def logWriter(baseURL: => URL = History.url(),
maximumBytes: Long = -1L,
maximumRecords: Int = -1,
maximumErrors: Int = -1): Writer = new Writer {
private var bytesWritten = 0L
@@ -111,7 +112,7 @@ object ClientApplication {
bytesWritten += text.length
recordsWritten += 1
if (record.level >= Level.Error) errorsWritten += 1
sendLog(JavaScriptLog(text))
sendLog(baseURL, JavaScriptLog(text))
if (maximumBytes != -1L && bytesWritten >= maximumBytes) {
enabled = false
} else if (maximumRecords != -1 && recordsWritten >= maximumRecords) {
@@ -140,11 +141,15 @@ object ClientApplication {
ErrorTrace.toError(event).flatMap(sendError)
}

def sendLog(log: JavaScriptLog): Future[XMLHttpRequest] = {
def sendLog(baseURL: URL, log: JavaScriptLog): Future[XMLHttpRequest] = {
val formData = new FormData
val jsonString = JsonUtil.toJsonString(log)
formData.append("message", jsonString)
val request = new AjaxRequest(History.url().replacePathAndParams(instance.logPath), data = Some(formData))
val request = new AjaxRequest(
url = baseURL.replacePathAndParams(instance.logPath),
data = Some(formData),
withCredentials = false
)
request.send()
}
}
@@ -3,6 +3,7 @@ package io.youi.app.screen
import reactify.{Val, Var}
import io.youi.{History, dom}
import io.youi.dom._
import io.youi.http.HttpMethod
import io.youi.net.Parameters
import io.youi.stream.StreamURL
import org.scalajs.dom.html
@@ -44,7 +45,7 @@ trait ContentScreen extends Screen with PathActivation {
.withParam("selector", "screen")
scribe.debug(s"Loading content $url...")
StreamURL
.stream(url)
.stream(url, method = HttpMethod.Get)
.map { html => // Fix for non-youi-server
val start = html.indexOf("<screen>")
val end = html.indexOf("</screen>")
@@ -1,5 +1,6 @@
package io.youi.ajax

import io.youi.http.HttpMethod
import io.youi.net.URL
import org.scalajs.dom.XMLHttpRequest
import org.scalajs.dom.raw.FormData
@@ -16,12 +17,13 @@ class AjaxManager(val maxConcurrent: Int) extends Logging {
def running: Int = _running.size

def enqueue(url: URL,
method: HttpMethod = HttpMethod.Post,
data: Option[FormData] = None,
timeout: Int = 0,
headers: Map[String, String] = Map.empty,
withCredentials: Boolean = true,
responseType: String = ""): AjaxAction = {
val request = new AjaxRequest(url, data, timeout, headers, withCredentials, responseType)
val request = new AjaxRequest(url, method, data, timeout, headers, withCredentials, responseType)
val action = new AjaxAction(request)
enqueue(action)
action
@@ -1,5 +1,6 @@
package io.youi.ajax

import io.youi.http.HttpMethod
import io.youi.net.URL
import org.scalajs.dom
import org.scalajs.dom._
@@ -11,6 +12,7 @@ import scala.scalajs.js
import scala.scalajs.js.|

class AjaxRequest(url: URL,
method: HttpMethod = HttpMethod.Post,
data: Option[FormData | String] = None,
timeout: Int = 0,
headers: Map[String, String] = Map.empty,
@@ -38,7 +40,7 @@ class AjaxRequest(url: URL,
val p = math.round(math.floor((evt.loaded / evt.total) * 100)).toInt
percentage.asInstanceOf[Var[Int]] := p
})
req.open("POST", url.toString)
req.open(method.value, url.toString)
req.responseType = responseType
req.timeout = timeout
req.withCredentials = withCredentials
@@ -1,6 +1,7 @@
package io.youi.stream

import io.youi.ajax.AjaxRequest
import io.youi.http.HttpMethod
import io.youi.net.URL
import org.scalajs.dom.FormData

@@ -9,12 +10,13 @@ import scala.concurrent.ExecutionContext.Implicits.global

object StreamURL {
def stream(url: URL,
method: HttpMethod = HttpMethod.Post,
data: Option[FormData] = None,
timeout: Int = 0,
headers: Map[String, String] = Map.empty,
withCredentials: Boolean = true,
responseType: String = ""): Future[String] = {
val request = new AjaxRequest(url, data, timeout, headers + ("streaming" -> "true"), withCredentials, responseType)
val request = new AjaxRequest(url, method, data, timeout, headers + ("streaming" -> "true"), withCredentials, responseType)
val future = request.send()
future.map(_.responseText)
}
@@ -66,7 +66,7 @@ case class URL(protocol: Protocol = Protocol.Http,
b.append(protocol.scheme)
b.append("://")
b.append(host)
if (!protocol.defaultPort.contains(port)) {
if (!protocol.defaultPort.contains(port) && port != -1) {
b.append(s":$port") // Not using the default port for the protocol
}
b.toString()
@@ -157,7 +157,7 @@ object URL {
}
val colonIndex2 = hostAndPort.indexOf(':')
val (host, port) = if (colonIndex2 == -1) {
hostAndPort -> protocol.defaultPort.getOrElse(throw new RuntimeException(s"Unknown port for $url."))
hostAndPort -> protocol.defaultPort.getOrElse(-1)
} else {
hostAndPort.substring(0, colonIndex2) -> hostAndPort.substring(colonIndex2 + 1).toInt
}
@@ -188,7 +188,7 @@ object URL {
Some(URL(protocol = protocol, host = host, port = port, path = path, parameters = parameters, fragment = fragment))
} catch {
case t: Throwable => {
scribe.warn(s"Unable to parse URL [$url]. Exception: ${t.getMessage}")
scribe.warn(s"Unable to parse URL [$url]. Exception: ${t.getMessage}", t)
None
}
}
@@ -17,6 +17,12 @@ class URLSpec extends WordSpec with Matchers {
val url = URL("http://www.outr.com/test?wsdl")
url.parameters.encoded should be("?wsdl")
}
"properly parse a simple file URL" in {
val url = URL("file:///android_asset/www/app/test.js")
url.host should be("")
url.path.encoded should be("/android_asset/www/app/test.js")
url.toString should be("file:///android_asset/www/app/test.js")
}
"properly parse a URL with two for the same key" in {
val url = URL("http://www.outr.com/test?test=one&test=two")
url.paramList("test") should be(List("one", "two"))

0 comments on commit 0817c3a

Please sign in to comment.
You can’t perform that action at this time.