http.stk 12.2 KB
Newer Older
1 2
;;;;
;;;; http.stk			-- HTTP utilities
3 4 5 6
;;;;
;;;; Copyright © 2007 Erick Gallesio - I3S-CNRS/ESSI <eg@essi.fr>
;;;;
;;;;
7 8 9 10
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
11
;;;;
12 13 14 15
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
16
;;;;
17 18
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
19
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 21 22 23
;;;; USA.
;;;;
;;;;
;;;; This code is a rewriting of several ScmPkg package written by
24
;;;; Manuel Serrano.
25 26
;;;;
;;;;    Creation date: 14-May-2007 19:12 (eg)
27
;;;; Last file update:  8-Jun-2007 14:44 (eg)
28 29 30 31 32 33 34 35 36 37 38
;;;;

(define-module HTTP-MODULE
  (export http-open
	  http-parse-status-line
	  http-parse-header
	  http-find-body
	  http-download)

;; -----------------------------------------------------------------------
;;	display-line ...
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
;;(define debug #t)
;;(define (%display x port)
;;  (when debug (display x (current-error-port)))
;;  (display x port))
(define %display display)

(define-macro (display-line . args)
  (let* ((sgra (reverse args))
	 (port (car sgra))
	 (vals (reverse (cdr sgra))))
    `(begin
       ,@(map (lambda (a) `(%display ,a ,port)) vals)
       (%display "\r\n" ,port))))

;; -----------------------------------------------------------------------
;;    generate-http-boundary ...
;; -----------------------------------------------------------------------
(define (generate-http-boundary)
  (let ((s (make-string 42 #\-))
	(chars "0123456789abcdef"))
    (let loop ((i 30))
      (when (< i 42)
	(let ((num (random-integer 16)))
	  (string-set! s i (string-ref chars num))
	  (loop (+ i 1)))))
    s))

;; -----------------------------------------------------------------------
;;    generate-post-boundary ...
;; -----------------------------------------------------------------------
(define (generate-http-post-body boundary args)
  (let ((port (open-output-string)))
    (if (null? args)
	(begin
	  (display-line port)
	  (close-output-port port))
	(let loop ((args args))
	  (if (null? args)
	      (begin
		(display-line boundary "--" port)
		(close-output-port port))
	      (let ((a (car args)))
		(display-line boundary port)
		(display-line "Content-Disposition: form-data; name=\""
			      (car a) "\"" port)
		(display-line port)
		(display-line (cadr a) port)
		(loop (cdr args))))))
    (get-output-string port)))

;; -----------------------------------------------------------------------
;;	http-open ...
;; ----------------------------------------------------------------------
(define (http-open #!key
		   (protocol 'http)
		   (method 'get)
		   (timeout 0)
		   (proxy #f)
		   (host "localhost")
		   (port 80)
		   (path "/")
		   (username #f)
		   (password #f)
		   (http-version "HTTP/1.0")
		   (content-type #f)
		   (header '())
		   (args  '()))

  (let* ((sock (if (string? proxy)
		   (let ((x (regexp-match "(.*):([0-9]+)" proxy)))
		     (if x
			 (make-client-socket (cadr x) (string->number (caddr x)))
			 (make-client-socket proxy 80)))
		   (make-client-socket host port)))
	 (out  (socket-output sock)))
    ;; header line
    (%display (string-upcase (format "~a" method)) out)
    (if (string? proxy)
	(display-line " http://" host ":" port path " " http-version out)
	(display-line " " path " " http-version out))
    ;; host and port
    (if (= port 80)
      (display-line "Host: " host out)
      (display-line "Host: " host ":" port out))
    ;; user additional header
    (for-each (lambda (h)
		(display-line (keyword->string (car h)) ": " (cadr h) out))
	      header)
    ;; authentication
    (when (and (string? username) (string? password))
      (let ((uinfo (base64-encode (string-append username ":" password))))
	(display-line "Authorization: Basic " uinfo out)))
    ;; connection keep-alive
    ;; (display-line "Keep-Alive: 300" out)
    ;; (display-line "Connection: keep-alive" out)
    ;; post method
    (if (eq? method 'post)
      (if (eq? content-type 'multipart/form-data)
	(let* ((boundary (generate-http-boundary))
	       (content  (generate-http-post-body boundary args)))
	  (display-line "Content-Length: " (string-length content) out)
	  (display-line "Content-Type: multipart/form-data; boundary="
			(substring boundary 2 (string-length boundary))
			out)
	  (display-line out)
	  (%display content out))
	(let ((content (x-www-form-urlencode args)))
	  (display-line "Content-Type: application/x-www-form-urlencoded" out)
	  (display-line "Content-Length: " (string-length content) out)
	  (display-line out)
	  (%display content out)
	  (display-line out)))
      (display-line out))

    (flush-output-port out)
    (close-output-port out)
    (let ((in (socket-input sock)))
      (port-close-hook-set! in (lambda () (socket-shutdown sock #t)))
      in)))

;; -----------------------------------------------------------------------
;;	http-parse-status-line ...
;; ----------------------------------------------------------------------
(define *status-line-regexp*
  (string->regexp "([Hh][Tt][Tt][Pp][Ss]?.*) +([0-9]+) +(.*)"))

(define (http-parse-status-line ip)
  (let* ((line (read-line ip))
	 (match (regexp-match *status-line-regexp* line)))
    (if match
	(values (cadr match)
		(string->number (caddr match))
		(cadddr match))
	(error "illegal status line syntax ~s" line))))

;; -----------------------------------------------------------------------
;;	http-parse-header ...
;; ----------------------------------------------------------------------
(define *header-assoc-regexp* (string->regexp "([^:]+): *(.*)"))

(define (http-parse-header ip)
  (let ((hostname #f)
	(port #f)
	(content-length -1)
	(transfer-encoding #f)
	(authorization #f)
	(proxy-authorization #f)
	(connection #f))
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    (let loop ((line    (read-line ip))
	       (header '()))
      (if (or (eof-object? line) (string=? line ""))
	  (values (reverse! header)
		  hostname
		  port
		  content-length
		  transfer-encoding
		  authorization
		  proxy-authorization
		  connection)
	  (let ((ln (regexp-match *header-assoc-regexp* line)))
	    (if ln
		(let ((key (make-keyword (string-downcase (cadr ln))))
		      (val (caddr ln)))
		  (case key
		    ((host:)
		     (let ((x (regexp-match "(.*):([0-9]+)" val)))
		       (if x
			   (begin
			     (set! hostname (cadr x))
			     (set! port (string->number (caddr x)))
			     (loop (read-line ip)
				   (list* (cons port: port)
					  (cons host: hostname)
					  header))))))
		    ((content-length:)
		     (set! content-length (string->number val))
		     (loop (read-line ip)
			   (cons (cons content-length: content-length) header)))
		    ((transfer-encoding:)
		     (set! transfer-encoding (string->symbol (string-downcase val)))
		     (loop (read-line ip)
			   (cons (cons transfer-encoding: transfer-encoding)
				 header)))
		    ((authorization:)
		     (set! authorization val)
		     (loop (read-line ip)
			   (cons (cons authorization: authorization) header)))
		    ((proxy-authorization:)
		     (set! proxy-authorization val)
		     ;; don't store the proxy-authorization in the header
		     (loop (read-line ip) header))
		    ((connection:)
		     (set! connection (string->symbol (string-downcase val)))
		     (loop (read-line ip)
			   (cons (cons connection: connection) header)))
		    (else
		     (loop (read-line ip)
			   (cons (cons key val) header)))))
		(error "incorrect header line format ~S" line)))))))

;; -----------------------------------------------------------------------
;;	http-find-body ...
;; ----------------------------------------------------------------------
(define (http-find-body ip)
  (receive (_ status-code explain)
	   (http-parse-status-line ip)
     (receive (header _host _port _clen tenc _aut _paut _conn)
	      (http-parse-header ip)
       (case status-code
	 ((204 304)
	  ;; no message body
	  (close-input-port ip)
	  (open-input-string ""))
	 ((301 302 307)
	  ;; redirection
	  (let ((loc (assq location: header)))
	    (if (not (pair? loc))
		(error "no URL for redirection!")
		(list 'redirection ip (cdr loc)))))
	 ((200)
	  ;; ok
	  (cond
	    ((eq? tenc 'chunked)
	     (error "chunked transfer not implemented yet"))
	    (else
	     ip)))
	 (else
	  (error "bad status code ~a: ~A" status-code explain))))))

;; ----------------------------------------------------------------------
;;	http-download ...
;; ----------------------------------------------------------------------
(define (http-download url dest)

  (define (get-username str)
    (let ((lst (string-split str ":")))
      (and (= (length lst) 2) (car lst))))
278

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  (define (get-password str)
    (let ((lst (string-split str ":")))
      (and (= (length lst) 2) (cadr lst))))

  (define (open-url url)
    (let* ((uri      (uri-parse url))
	   (protocol (key-get uri :scheme))
	   (userinfo (key-get uri :user))
	   (host     (key-get uri :host))
	   (port     (key-get uri :port))
	   (pth      (key-get uri :path))
	   (query    (key-get uri :query))
	   (path     (if (equal? query "") pth (format "~a?~a" pth query))))
      (let ((pt (http-find-body (http-open :protocol protocol
					   :username (get-username userinfo)
					   :password (get-password userinfo)
					   :host host
					   :port port
					   :path path))))
	(cond
	  ((input-port? pt)
	   pt)
Erick Gallesio's avatar
Erick Gallesio committed
301
	  ((and (list? pt) (= (length pt) 3) (eq? (car pt) 'redirection))
302
	   ;; We have a redirection     (redirection old-port new-url)
Erick Gallesio's avatar
Erick Gallesio committed
303 304 305 306 307 308 309 310
	   (let* ((old-port (cadr pt))
		  (new-url  (caddr pt))
		  (uri      (uri-parse new-url)))
	     (close-input-port old-port)
	     (let ((url (if (equal? (key-get uri :scheme #f) "http")
			    new-url
			    (format "http://~a:~a~a" host port new-url))))
	       (open-url url))))
311
	  (else
Erick Gallesio's avatar
Erick Gallesio committed
312
	   (error 'http-download "unexpected result ~S" pt))))))
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
  (let ((port (open-url url)))
    (cond
      ((string? dest)
       (let ((out (open-file dest "w")))
	 (unless out
	   (error "cannot open output file ~s" dest))
	 (copy-port port out)
	 (close-port out)))
      ((output-port? dest)
       (copy-port port dest))
      (else
       (error 'http-download "destination must be a path or an output port"
	      dest)))
    (close-input-port port)))

;; ----------------------------------------------------------------------
;;	form-urlencode ...
;; ----------------------------------------------------------------------
(define (form-urlencode args sep)
333

334 335 336 337 338
  (define (encode-char c port)
    (let ((n (char->integer c)))
      (if (fx< n 16)
	  (fprintf port "%0~x" n)
	  (fprintf port "%~x" n))))
339

340 341 342 343 344 345 346 347 348 349 350 351
  (define (encode-string str port)
    (dotimes (i (string-length str))
      (let ((c (string-ref str i)))
	(case c
	  ((#\; #\& #\= #\# #\" #\' #\+ #\% #\? #\: #\| #\~)
	   (encode-char c port))
	  ((#\space)
	   (write-char #\+ port))
	  (else
	   (if (or (char>=? c (integer->char 128)) (char<? c (integer->char 032)))
	       (encode-char c port)
	       (write-char c port)))))))
352

353 354 355 356 357 358
  (define (encode-arg arg port)
    (encode-string (car arg) port)
    (unless  (or (null? (cdr arg))
		 (eq? (cadr arg) (void)))
      (write-char #\= port)
      (encode-string (cadr arg) port)))
359

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  (let ((port (open-output-string)))
    (let loop ((a args))
      (if (null? a)
	  (get-output-string port)
	  (begin
	    (encode-arg (car a) port)
	    (unless (null? (cdr a))
	      (write-char sep port))
	    (loop (cdr a)))))))

;; ----------------------------------------------------------------------
;;	www-form-urlencode ...
;; ----------------------------------------------------------------------
(define (www-form-urlencode args)
  (form-urlencode args #\;))

;; ----------------------------------------------------------------------
;;	x-www-form-urlencode ...
;; ----------------------------------------------------------------------
(define (x-www-form-urlencode args)
  (form-urlencode args #\&))
381

382 383 384 385 386
;; End of HTTP-MODULE
)

(%redefine-module-exports (find-module 'HTTP-MODULE)
			  (find-module 'STklos))
387 388

(provide "http")