about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-10-23 14:25:13 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-10-23 15:34:20 -0700
commit300a1d6e8033f1766d5203b15ff3ea8570e4b79b (patch)
tree0e3ecd53d33f116aa43d6b859914ff96fd767454
parent2e760d6df479b082b22aa78fe7bf6f03667427c0 (diff)
downloadmu-300a1d6e8033f1766d5203b15ff3ea8570e4b79b.tar.gz
3564
Change the interface for reading a URL slightly so that we can directly
use the path in `assume-resources`.
-rw-r--r--092socket.mu35
-rw-r--r--http-client.mu2
2 files changed, 35 insertions, 2 deletions
diff --git a/092socket.mu b/092socket.mu
index 6a5c427a..7acaea63 100644
--- a/092socket.mu
+++ b/092socket.mu
@@ -57,12 +57,13 @@ scenario write-to-fake-socket [
   ]
 ]
 
-def start-reading-from-network resources:&:resources, host:text, path:text -> contents:&:source:char [
+def start-reading-from-network resources:&:resources, uri:text -> contents:&:source:char [
   local-scope
   load-ingredients
   {
     break-if resources
     # real network
+    host:text, path:text <- split-at uri, 47/slash
     socket:num <- $open-client-socket host, 80/http-port
     assert socket, [contents]
     req:text <- interpolate [GET _ HTTP/1.1], path
@@ -195,3 +196,35 @@ def write-to-socket socket:num, s:text [
     loop
   }
 ]
+
+# like split-first, but don't eat the delimiter
+def split-at text:text, delim:char -> x:text, y:text [
+  local-scope
+  load-ingredients
+  # empty text? return empty texts
+  len:num <- length *text
+  {
+    empty?:bool <- equal len, 0
+    break-unless empty?
+    x:text <- new []
+    y:text <- new []
+    return
+  }
+  idx:num <- find-next text, delim, 0
+  x:text <- copy-range text, 0, idx
+  y:text <- copy-range text, idx, len
+]
+
+scenario text-split-at [
+  local-scope
+  x:text <- new [a/b]
+  run [
+    y:text, z:text <- split-at x, 47/slash
+    10:@:char/raw <- copy *y
+    20:@:char/raw <- copy *z
+  ]
+  memory-should-contain [
+    10:array:character <- [a]
+    20:array:character <- [/b]
+  ]
+]
diff --git a/http-client.mu b/http-client.mu
index 8c781e5f..f8e86046 100644
--- a/http-client.mu
+++ b/http-client.mu
@@ -2,7 +2,7 @@
 
 def main [
   local-scope
-  google:&:source:char <- start-reading-from-network 0/real-resources, [google.com], [/]
+  google:&:source:char <- start-reading-from-network 0/real-resources, [google.com/]
   n:num <- copy 0
   buf:&:buffer <- new-buffer 30
   {