Skip to content

Commit e5ff41e

Browse files
committed
Merge pull request #124 from snmsts/support-scheme
add 'scheme' on ql-http:url. 'cl-http:fetch' support adding handler f…
2 parents 2ef7c73 + 1bd64b5 commit e5ff41e

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

quicklisp/http.lisp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@
322322

323323
(defun full-proxy-path (host port path)
324324
(format nil "~:[http~;https~]://~A~:[:~D~;~*~]~A"
325-
(= port 443)
325+
(eql port 443)
326326
host
327-
(or (= port 80)
328-
(= port 443))
327+
(or (eql port 80)
328+
(eql port 443))
329329
port
330330
path))
331331

@@ -365,7 +365,7 @@ information."
365365
(apply #'add-strings sink strings)
366366
(add-newline sink)))
367367
(add-line method " " path " HTTP/1.1")
368-
(add-line "Host: " host (if (= port 80) ""
368+
(add-line "Host: " host (if (eql port 80) ""
369369
(format nil ":~D" port)))
370370
(add-line "Connection: close")
371371
(add-line "User-Agent: " (user-agent-string))
@@ -589,24 +589,31 @@ the indexes in the header accordingly."
589589
;;; HTTP URL parsing
590590

591591
(defclass url ()
592-
((hostname
592+
((scheme
593+
:initarg :scheme
594+
:accessor scheme
595+
:initform nil)
596+
(hostname
593597
:initarg :hostname
594598
:accessor hostname
595599
:initform nil)
596600
(port
597601
:initarg :port
598602
:accessor port
599-
:initform 80)
603+
:initform nil)
600604
(path
601605
:initarg :path
602606
:accessor path
603607
:initform "/")))
604608

605609
(defun parse-urlstring (urlstring)
606610
(setf urlstring (string-trim " " urlstring))
607-
(let* ((pos (mismatch urlstring "http://" :test 'char-equal))
611+
(let* ((pos (position #\: urlstring))
612+
(scheme (or (and pos (subseq urlstring 0 pos)) "http"))
613+
(pos (mismatch urlstring "://" :test 'char-equal :start1 pos))
608614
(mark pos)
609615
(url (make-instance 'url)))
616+
(setf (scheme url) scheme)
610617
(labels ((save ()
611618
(subseq urlstring mark pos))
612619
(mark ()
@@ -673,13 +680,14 @@ the indexes in the header accordingly."
673680
(defgeneric request-buffer (method url)
674681
(:method (method url)
675682
(setf url (url url))
676-
(make-request-buffer (hostname url) (port url) (path url)
683+
(make-request-buffer (hostname url) (port url) (or (path url) 80)
677684
:method method)))
678685

679686
(defun urlstring (url)
680-
(format nil "~@[http://~A~]~@[:~D~]~A"
687+
(format nil "~@[~A://~]~@[~A~]~@[:~D~]~A"
688+
(and (hostname url) (scheme url))
681689
(hostname url)
682-
(and (/= 80 (port url)) (port url))
690+
(port url)
683691
(path url)))
684692

685693
(defmethod print-object ((url url) stream)
@@ -690,6 +698,8 @@ the indexes in the header accordingly."
690698
(setf url1 (url url1))
691699
(setf url2 (url url2))
692700
(make-instance 'url
701+
:scheme (or (scheme url1)
702+
(scheme url2))
693703
:hostname (or (hostname url1)
694704
(hostname url2))
695705
:port (or (port url1)
@@ -780,10 +790,22 @@ the indexes in the header accordingly."
780790
(too-many-redirects-count condition)
781791
(too-many-redirects-url condition)))))
782792

783-
(defun fetch (url file &key (follow-redirects t) quietly
793+
(defvar *fetch-scheme-functions*
794+
'(("http" . http-fetch))
795+
"assoc list to decide which scheme-function are called by FETCH function.")
796+
797+
(defun fetch (url file &rest rest)
798+
"Request URL and write the body of the response to FILE."
799+
(let* ((url (merge-urls url *default-url-defaults*))
800+
(call (cdr (assoc (scheme url) *fetch-scheme-functions* :test 'equal))))
801+
(if call
802+
(apply call (urlstring url) file rest)
803+
(error "Unknow scheme ~S" url))))
804+
805+
(defun http-fetch (url file &key (follow-redirects t) quietly
784806
(if-exists :rename-and-delete)
785807
(maximum-redirects *maximum-redirects*))
786-
"Request URL and write the body of the response to FILE."
808+
"default scheme-function for http protocol."
787809
(setf url (merge-urls url *default-url-defaults*))
788810
(setf file (merge-pathnames file))
789811
(let ((redirect-count 0)
@@ -797,7 +819,7 @@ the indexes in the header accordingly."
797819
(error 'too-many-redirects
798820
:url original-url
799821
:redirect-count redirect-count))
800-
(with-connection (connection (hostname connect-url) (port connect-url))
822+
(with-connection (connection (hostname connect-url) (or (port connect-url) 80))
801823
(let ((cbuf (make-instance 'cbuf :connection connection))
802824
(request (request-buffer "GET" url)))
803825
(write-octets request connection)

quicklisp/package.lisp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
(:use #:cl #:ql-network #:ql-progress #:ql-config)
9797
(:export #:*proxy-url*
9898
#:fetch
99+
#:http-fetch
100+
#:*fetch-scheme-functions*
101+
#:scheme
99102
#:hostname
100103
#:port
101104
#:path

0 commit comments

Comments
 (0)