-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.scm
More file actions
64 lines (61 loc) · 1.76 KB
/
http.scm
File metadata and controls
64 lines (61 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
;;-*-mode:lisp-*-
;; 15-MAR-95. George Carrette. GJC@DELPHI.COM
;; A hyper-text-protocol (HTTP) client.
;; $Id: http.scm,v 1.1 1996/10/17 18:40:18 gjc Exp $
(define (http-get-file server url hfile dfile)
(let ((s (s-open (if (pair? server) (car server) server)
(if (pair? server) (car (cdr server)) 80)))
(hf (and hfile (not (pair? hfile)) (fopen hfile "w")))
(df (and dfile (not (pair? dfile))) (fopen dfile "w"))
(line nil)
(c nil)
(hdr nil))
(if (pair? url)
(while url
(s-puts (car url) s)
(s-puts "\r\n" s)
(set! url (cdr url)))
(s-puts (string-append "GET " url " HTTP/1.0\r\n\r\n")
s))
(s-force-output s)
(or hf
(eqv? hfile t)
(puts "---Response Header---\n"))
(while (and (set! line (s-gets s))
(not (or (equal? line "\r\n")
(equal? line "\n"))))
(puts line hf)
(set! hdr (cons line hdr)))
(and hf (fclose hf))
(or df
(puts "---Response Data---\n"))
(while (set! c (s-getc s)) (putc c df))
(and df (fclose df))
(s-close s)
(nreverse hdr)))
(define (http-post server url data hfile dfile)
(http-get-file server
(list (string-append "POST "url " HTTP/1.0")
"User-Agent: Hyper Text Query System in Lisp"
"Content-type: application/x-www-form-urlencoded"
(string-append "Content-length: "
(number->string (string-length data)))
""
data)
hfile
dfile))
(define (url-encode-form-data . l)
(define (loop x)
(if (not x)
nil
(if (not (cdr x))
(error "odd number of arguments")
(let ((rest (loop (cddr x))))
(cons (car x)
(cons "="
(cons (if (number? (cadr x))
(number->string (cadr x))
(url-encode (cadr x)))
(if rest
(cons "&" rest)))))))))
(apply string-append (loop l)))