mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-18 21:29:19 +02:00
add simple http client and server examples
This commit is contained in:
parent
e6d7e4fffb
commit
eecf561e62
2 changed files with 52 additions and 0 deletions
36
examples/simple-http-client.scm
Executable file
36
examples/simple-http-client.scm
Executable file
|
@ -0,0 +1,36 @@
|
|||
#! /usr/bin/env chibi-scheme
|
||||
|
||||
; Simple HTTP client
|
||||
; Retrieves the contents of the URL argument:
|
||||
|
||||
; Usage:
|
||||
; simple-http-client.scm [URL]
|
||||
;
|
||||
; Example:
|
||||
; simple-http-client.scm http://localhost:8000
|
||||
|
||||
(import (chibi) (chibi net) (chibi net http) (chibi io))
|
||||
|
||||
(if (> (length (command-line)) 1)
|
||||
(let ((url (car (cdr (command-line)))))
|
||||
(if (> (string-length url) 0)
|
||||
(begin
|
||||
(display (read-string 65536 (http-get url)))
|
||||
(newline))))
|
||||
(let ((progname (car (command-line))))
|
||||
(display "Retrieve the contents of a URL.")
|
||||
(newline)
|
||||
(display "Usage:")
|
||||
(newline)
|
||||
(newline)
|
||||
(display progname)
|
||||
(display " [URL]")
|
||||
(newline)))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
16
examples/simple-http-server.scm
Executable file
16
examples/simple-http-server.scm
Executable file
|
@ -0,0 +1,16 @@
|
|||
#! /usr/bin/env chibi-scheme
|
||||
|
||||
; Simple HTTP server
|
||||
; Returns a minimal HTML page with a single number incremented
|
||||
; every request. Binds to localhost port 8000.
|
||||
|
||||
(import (chibi) (chibi net http-server) (chibi net servlet) (chibi sxml))
|
||||
|
||||
(let ((count 0))
|
||||
(run-http-server
|
||||
8000
|
||||
(lambda (cfg request next restart)
|
||||
(set! count (+ 1 count))
|
||||
(servlet-write request (sxml->xml `(html (body
|
||||
(p "Count: \n")
|
||||
(p ,count))))))))
|
Loading…
Add table
Reference in a new issue