1
0

web.l 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. (
  2. ;;; Hosts ----------------------------------------------------------------
  3. (def mntmn [5bfa730f])
  4. (def facebook [1f0d5d03])
  5. (def tokyo [c0a80168])
  6. (def freenode [c0ba9d2b])
  7. (def dwigns [5eba9793])
  8. ;;; HTTP -----------------------------------------------------------------
  9. (def http-get (fn (do
  10. (def header (concat (concat "Host: " http-hostname) [0d0a0d0a]))
  11. (tcp-send (concat (concat (concat (concat "GET " http-path) " HTTP/1.1") [0d0a]) header))
  12. )))
  13. (def http-connect-handler (fn (http-get path)))
  14. (def html-parse-state 0)
  15. ; 0: in text
  16. ; 1: in tag
  17. ; 2: whitespace
  18. (def html-parse (fn raw (do
  19. (def i 0)
  20. (def j 0)
  21. (def k 0)
  22. (def l 0)
  23. (def rsz (usize raw))
  24. (def out (alloc-str rsz))
  25. (def html-attr (alloc-str 10))
  26. (def html-tag (alloc-str 10))
  27. (def attr-value (alloc-str 50))
  28. (def attr-i 0)
  29. (def tag-i 0)
  30. (def value-i 0)
  31. (def output-attr 0)
  32. (while (lt i rsz) (do
  33. (def c (uget raw i))
  34. (if (= html-parse-state 0) ; text
  35. (if (= c 60)
  36. (def html-parse-state 1)
  37. (if (or (= c 10) (= c 32))
  38. (do
  39. (uput out j c)
  40. (def j (+ j 1)) (def l (+ l 1))
  41. (def html-parse-state 2)
  42. )
  43. (do
  44. (uput out j c)
  45. (def j (+ j 1))
  46. (def l (+ l 1))
  47. )))
  48. (if (= html-parse-state 1) (do ; tag
  49. (if (= c 62) (do
  50. (print (list "html parsed tag: " html-tag))
  51. (def tag-i 0)
  52. (def html-parse-state 0)
  53. ))
  54. (if (and (gt c 96) (lt c 123)) (do ; tag name
  55. (put html-tag tag-i c)
  56. (put html-tag (+ tag-i 1) 0)
  57. (def tag-i (+ tag-i 1))
  58. ))
  59. (if (= c 32) (do
  60. (print (list "html parsed tag: " html-tag))
  61. (def tag-i 0)
  62. (def attr-i 0)
  63. (def html-parse-state 3)))
  64. )
  65. (if (= html-parse-state 2) ; whitespace
  66. (if (not (or (= c 32) (= c 10)))
  67. (if (= c 60)
  68. (def html-parse-state 1)
  69. (do
  70. (def html-parse-state 0)
  71. (uput out j c)
  72. (def j (+ j 1))
  73. (def l (+ l 1))
  74. )))
  75. (if (= html-parse-state 3) (do ; space between attrs
  76. (if (= c 62) (do ; tag closed
  77. (def html-parse-state 0)))
  78. (if (= c 32) (do ; end of attr
  79. (print (list "html parsed attr: " html-attr))
  80. (def attr-i 0)
  81. (put html-attr 0 0)
  82. ))
  83. (if (= c 61) (do ; '=' attr value follows
  84. (print (list "html parsed attr=: " html-attr))
  85. (def attr-i 0)
  86. (def html-parse-state 4)
  87. ))
  88. (if (and (gt c 96) (lt c 123)) (do ; attr name
  89. (put html-attr attr-i c)
  90. (put html-attr (+ attr-i 1) 0)
  91. (def attr-i (+ attr-i 1))
  92. )))
  93. (if (= html-parse-state 4) (do ; beginning of attr value
  94. (if (str= "href" html-attr) (do
  95. (print "href attr!")
  96. (uput out j (get "~" 0)) (def j (+ j 1))
  97. (def output-attr 1) ; we want to output the link
  98. ) (def output-attr 0)) ; else don't output
  99. (def value-i 0)
  100. (if (or (= c 39) (= c 34))
  101. (def attr-sep c)
  102. (def attr-sep 32) ; else
  103. )
  104. (def html-parse-state 5) ; go to the actual value
  105. )
  106. (if (= html-parse-state 5) (do ; attr value
  107. (if (= c attr-sep) (do ; value closed
  108. (print (list "attr value parsed: " attr-value))
  109. (if output-attr (do
  110. (uput out j 32) (def j (+ j 1))
  111. ))
  112. (def html-parse-state 3)
  113. (def value-i 0)
  114. )
  115. (if (and (= attr-sep 32) (= c 62)) (do ; tag closed
  116. (if output-attr (do
  117. (uput out j 32) (def j (+ j 1))
  118. ))
  119. (def html-parse-state 0)
  120. )
  121. (do ; append to attr value
  122. (put attr-value value-i c)
  123. (put attr-value (+ value-i 1) 0)
  124. (def value-i (+ value-i 1))
  125. (if output-attr (do
  126. (uput out j c) (def j (+ j 1))
  127. ))
  128. ))
  129. ))
  130. ))))))
  131. (def i (+ i 1))
  132. (if (and (= c 32) (gt l 80)) (do
  133. (uput out j 10) ; wrap and linefeed
  134. (def j (+ j 1))
  135. (def l 0)
  136. ) 0)
  137. ))
  138. out
  139. )))
  140. (def http-handler (fn (do
  141. (buf-append 0 (html-parse network-input))
  142. )))
  143. (def hget (fn ip hostname path (do
  144. (set-buf-str 0 (concat "loading… " [0a]))
  145. (def html-parse-state 0)
  146. (def http-path path)
  147. (def http-hostname hostname)
  148. (tcp-connect ip 80 http-connect-handler http-handler))))
  149. (def webtest (fn (hget dwigns "news.dieweltistgarnichtso.net" "/notes/nokia-n900.html")))
  150. (def uri-parse (fn str (do
  151. (def uri-parts (split str ":"))
  152. (def uri-proto (car uri-parts))
  153. (def uri-parts2 (split (car (cdr uri-parts)) "/"))
  154. (def uri-host (car uri-parts2))
  155. (def uri-path (cdr uri-parts2))
  156. (list uri-proto uri-host uri-path)
  157. )))
  158. (def plumb (fn word (do
  159. (print (cons "plumb" word))
  160. (if (= (uget word 0) (uchr "/"))
  161. (set-buf-str 0 (load word)))
  162. (if (= (uget word 0) (uchr "~")) (do
  163. (def plumb-uri (uri-parse (substr word 1 (usize word))))
  164. (if (str= "http" (car plumb-uri)) (do
  165. (def plumb-host (car (cdr plumb-uri)))
  166. (def path (join (car (cdr (cdr plumb-uri))) "/"))
  167. (if (lt (usize path) 1) (def path "/"))
  168. (buf-append 0 (join (list "hget" plumb-host path "…") " "))
  169. (hget mntmn plumb-host path)
  170. )
  171. (do ; FIXME total hack
  172. (def path (concat "/" (substr word 1 (usize word))))
  173. (hget mntmn plumb-host path)
  174. )
  175. )
  176. ))
  177. )))
  178. )