forked from franzinc/aserve
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.cl
2163 lines (1836 loc) · 77.2 KB
/
client.cl
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
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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; -*- mode: common-lisp; package: net.aserve.client -*-
;;
;; client.cl
;;
;; See the file LICENSE for the full license governing this code.
;;
;;
;; Description:
;; http client code.
;;- This code in this file obeys the Lisp Coding Standard found in
;;- http://www.franz.com/~jkf/coding_standards.html
;;-
;; this will evolve into the http client code but for now it's
;; just some simple stuff to allow us to test aserve
;;
(in-package :net.aserve.client)
(eval-when (:compile-toplevel) (declaim (optimize (speed 3))))
(net.aserve::check-smp-consistency)
(defclass client-request ()
((uri ;; uri we're accessing
:initarg :uri
:accessor client-request-uri)
(method ; :get, :put, etc
:initarg :method
:accessor client-request-method)
(headers ; alist of ("headername" . "value") or (:headername . "value")
:initform nil
:initarg :headers
:accessor client-request-headers)
(response-code ; response code (an integer)
:initform nil
:accessor client-request-response-code)
(socket ; the socket through which we'll talk to the server
:initarg :socket
:accessor client-request-socket)
(response-stream ; the (possibly) chunking, inflating stream
:initform nil
:accessor client-request-response-stream)
(protocol
; the protocol value returned by the web server
; note, even if the request is for http/1.0, apache will return
; http/1.1. I'm not sure this is kosher.
:accessor client-request-protocol)
(response-comment ;; comment passed back with the response
:accessor client-request-response-comment)
;
(bytes-left ;; indicates how many bytes in response left
; value is nil (no body)
; integer (that many bytes left, not chunking)
; :unknown - read until eof, not chunking
; :chunking - read until chunking eof
:accessor client-request-bytes-left
:initform nil)
(cookies ;; optionally a cookie jar for hold received and sent cookies
:accessor client-request-cookies
:initarg :cookies
:initform nil)
(cookie-string ;; if there are cookies then this is what's put in the header
:reader client-request-cookie-string
:initarg :cookie-string
:initform nil)
(accept ;; the client's Accept header value
:reader client-request-accept
:initarg :accept
:initform nil)
(return-connection
;; set to
;; :maybe - would like to return but only if no errors
;; :yes - return for sure
:initarg :return-connection
:accessor client-request-return-connection
:initform nil)
(content-encoding
;; if the content is itself encoded then it is one of
;; :gzip :deflate (but we don't support deflate yet)
:initarg :content-encoding
:initform nil
:accessor client-request-content-encoding)
(deferred-content
;; content we are delaying the transmission of due to
;; an Expect: 100-continue header.
:initarg :deferred-content
:initform nil
:accessor client-request-deferred-content)
(cacheable
;; true if the response is cacheable assuming the
;; response permits it
:initarg :cacheable
:accessor client-request-cacheable)
))
(defclass digest-authorization ()
((username :initarg :username
:initform ""
:reader digest-username)
(password :initarg :password
:initform ""
:reader digest-password)
(realm :initform ""
:accessor digest-realm)
(uri :initform nil
:accessor digest-uri)
(qop :initform nil
:accessor digest-qop)
(nonce :initform ""
:accessor digest-nonce)
; we sent unique cnonce each time
(nonce-count :initform "1"
:reader digest-nonce-count)
(cnonce :initform nil
:accessor digest-cnonce)
(opaque :initform nil
:accessor digest-opaque)
(response :initform nil
:accessor digest-response)
))
;; This class is meant to be subclassed.
;; Instances of this class can be passed as the content argument
;; to do-http-request in order to dynamically produced the content
;; to be sent with the http request to the web server.
(defclass computed-content ()
nil)
(defgeneric get-content-length (cc)
(:documentation "returns the number of bytes in the content")
(:method ((cc computed-content))
(declare (ignore cc))))
(defgeneric get-content-headers (cc &key protocol headers)
(:documentation "takes protocol and headers specified by the caller
of do-http-request and returns additional content-specific headers to
be included; can signal errors, but cannot modify the existing
headers")
;; Protocol and headers are passed as keyword arguments to prohibit
;; multiple dispatch which may easily get out of hand when default
;; methods are available.
(:method ((cc computed-content) &key protocol headers)
(declare (ignore protocol headers))))
(defgeneric write-content (cc stream)
(:documentation "writes the content to the given stream")
(:method ((cc computed-content) stream)
(declare (ignore cc stream))))
;; This subclass of computed-content will send the contents
;; of a file to the web server.
(defclass file-computed-content (computed-content)
((filename :initarg :filename :reader fcc-filename)))
(defmethod get-content-length ((fcc file-computed-content))
(with-open-file (p (fcc-filename fcc) :direction :input)
(file-length p)))
(defmethod write-content ((fcc file-computed-content) stream)
(let ((buffer (make-array 4096 :element-type '(unsigned-byte 8))))
(with-open-file (p (fcc-filename fcc)
:direction :input)
(loop
(let ((size (read-sequence buffer p)))
(if* (eql 0 size)
then (return)
else (write-sequence buffer stream :end size)))))))
;; This subclass of computed-content will send the data from the
;; specified stream, using the chunking mechanism.
(defclass stream-computed-content (computed-content)
((stream :initarg :stream
:reader stream-computed-content-stream)))
(defmethod get-content-length ((scc stream-computed-content))
nil)
(defmethod get-content-headers ((scc stream-computed-content) &key protocol headers)
(let ((transfer-encoding (cdr (assoc "Transfer-Encoding" headers :test #'equalp))))
(if* (not (eq protocol :http/1.1))
then (error "STREAM-COMPUTED-CONTENT uses chunking mechanism ~
which is only supported by HTTP/1.1.")
elseif (null transfer-encoding)
then '(("Transfer-Encoding" . "chunked"))
elseif (string= transfer-encoding "chunked")
then nil ;; correct Transfer-Encoding header is already there
else (error "Conflicting value of Transfer-Encoding header (~s): ~
STREAM-COMPUTED-CONTENT uses AllegroServe's plain chunking ~
mechanism so only \"chunked\" value is allowed."
transfer-encoding))))
(defmethod write-content ((scc stream-computed-content) stream)
;; Copy data from in-stream to out-stream.
(let* ((out-stream (net.aserve::make-instance-chunking-stream+output-handle stream))
(in-stream (stream-computed-content-stream scc))
(buffer (make-array 4096 :element-type (stream-element-type in-stream)))
(num-elements nil))
(loop
(setf num-elements (read-sequence buffer in-stream))
(when (zerop num-elements)
(return))
(write-sequence buffer out-stream :end num-elements))
(force-output out-stream)
(close out-stream)))
(defvar crlf (make-array 2 :element-type 'character
:initial-contents '(#\return #\linefeed)))
(defmacro with-better-scan-macros (&body body)
;; define the macros for scanning characters in a string
`(macrolet ((collect-to (ch buffer i max &optional downcasep eol-ok)
;; return a string containing up to the given char
`(let ((start ,i))
(loop
(if* (>= ,i ,max)
then ,(if* eol-ok
then `(return (buf-substr start ,i ,buffer ,downcasep))
else `(fail)))
(if* (eql ,ch (schar ,buffer ,i))
then (return (buf-substr start ,i ,buffer ,downcasep)))
(incf ,i)
)))
(collect-to-eol (buffer i max)
;; return a string containing up to the given char
`(let ((start ,i))
(loop
(if* (>= ,i ,max)
then (return (buf-substr start ,i ,buffer)))
(let ((thisch (schar ,buffer ,i)))
(if* (eq thisch #\return)
then (let ((ans (buf-substr start ,i ,buffer)))
(incf ,i) ; skip to linefeed
(return ans))
elseif (eq thisch #\linefeed)
then (return (buf-substr start ,i ,buffer))))
(incf ,i)
)))
(skip-to-not (ch buffer i max &optional (errorp t))
;; skip to first char not ch
`(loop
(if* (>= ,i ,max)
then ,(if* errorp
then `(fail)
else `(return)))
(if* (not (eq ,ch (schar ,buffer ,i)))
then (return))
(incf ,i)))
(buf-substr (from to buffer &optional downcasep)
;; create a string containing [from to }
;;
`(let ((res (make-string (- ,to ,from))))
(do ((ii ,from (1+ ii))
(ind 0 (1+ ind)))
((>= ii ,to))
(setf (schar res ind)
,(if* downcasep
then `(char-downcase (schar ,buffer ii))
else `(schar ,buffer ii))))
res)))
,@body))
(let ((bufsize 16384))
(defun buffered-read-body (stream format)
(flet ((buffer (size)
(if* (eq format :text)
then (make-string size)
else (make-array size :element-type '(unsigned-byte 8)))))
(let ((accum ()) (size 0) buffer read done)
(loop (setf buffer (buffer bufsize)
read (handler-bind
((excl::socket-chunking-end-of-file
(lambda (e)
(declare (ignore e))
(setf done t))))
(read-sequence buffer stream :partial-fill t)))
(push (cons buffer read) accum)
(incf size read)
(if* (or (zerop read) done) then (return)))
(setf accum (nreverse accum))
(let* ((bigbuf (buffer size))
(pos 0))
(dolist (acc accum)
(destructuring-bind (sub . size) acc
(replace bigbuf sub :start1 pos :end2 size)
(incf pos size)))
bigbuf)))))
(defun get-client-request-response-stream (creq)
(or (client-request-response-stream creq)
(let ((left (client-request-bytes-left creq))
(socket (client-request-socket creq)))
; the data is coming in chunked then unchunk it
(if* (eq :chunked left)
then (setq socket
(net.aserve::make-instance-unchunking-stream+input-handle socket)))
; if data is content-encoded, then decode
(if* (client-request-content-encoding creq)
then (setq socket (make-instance
'net.aserve::inflate-stream
:input-handle socket
;; bug20472. :skip-gzip-header deprecated
;; in code/inflate.003 patch for 8.2
;; and will be gone in 9.0.
#-(version>= 9 0) :skip-gzip-header
#-(version>= 9 0) t
:content-length (and (integerp left) left)))
(setf (client-request-bytes-left creq) :unknown))
(setf (client-request-response-stream creq) socket))))
(defun read-response-body (creq &key (format :text))
(let ((socket (get-client-request-response-stream creq))
(left (client-request-bytes-left creq)))
(setf (client-request-bytes-left creq) :eof)
(if* (null left)
then nil
elseif (integerp left)
then (let ((buffer (make-array left :element-type '(unsigned-byte 8))))
(read-sequence buffer socket)
(if* (eq format :text)
then (octets-to-string buffer :external-format
(stream-external-format
(client-request-socket creq)))
else buffer)
)
elseif (member left '(:chunked :unknown))
then (prog1 (buffered-read-body socket format)
)
elseif (eq left :eof)
then (error "Body already read."))))
;; bug22596
(defparameter *default-retry-on-timeout* nil)
(defparameter *redirect-codes*
'(#.(net.aserve::response-number *response-found*)
#.(net.aserve::response-number *response-moved-permanently*)
#.(net.aserve::response-number *response-temporary-redirect*)
#.(net.aserve::response-number *response-see-other*)))
(defun do-http-request (uri
&rest args
&key
(method :get)
(protocol :http/1.1)
(accept "*/*")
compress ; say we'll willing to accept compressed response
content
content-type
query
(format :text) ; or :binary
cookies ; nil or a cookie-jar
(redirect 5) ; auto redirect if needed
(redirect-methods '(:get :head))
(retry-on-timeout *default-retry-on-timeout*)
basic-authorization ; (name . password)
digest-authorization ; digest-authorization object
(keep-alive (eq method :connect)) ; if true, set con to keep alive
headers ; extra header lines, alist
proxy ; naming proxy server to access through
proxy-basic-authorization ; (name . password)
no-proxy ; list of domains to not proxy
user-agent
(external-format *default-aserve-external-format*)
ssl ; do an ssl connection
ssl-args ; plist of make-ssl-client-stream args
; (overrides other ssl args when
; specified)
skip-body ; fcn of request object
ssl-method
timeout
certificate
key
certificate-password
ca-file
ca-directory
crl-file
crl-check
verify
max-depth
connection ; existing socket to the server
read-body-hook
cache ;; client-cache object
;; internal
recursing-call ; true if we are calling ourself
)
;; send an http request and return the result as four values:
;; the body, the response code, the headers and the uri
(let ((creq (make-http-client-request
uri
:method method
:protocol protocol
:accept accept
:compress compress
:content content
:content-type content-type
:query query
:cookies cookies
:basic-authorization basic-authorization
:digest-authorization digest-authorization
:keep-alive keep-alive
:headers headers
:proxy proxy
:no-proxy no-proxy
:proxy-basic-authorization proxy-basic-authorization
:user-agent user-agent
:external-format external-format
:ssl ssl
:ssl-args ssl-args
:ssl-method ssl-method
:timeout timeout
:certificate certificate
:key key
:certificate-password certificate-password
:ca-file ca-file
:ca-directory ca-directory
:crl-file crl-file
:crl-check crl-check
:verify verify
:max-depth max-depth
:connection connection
:cache cache
)))
(if* (consp creq)
then ;; cache validation has computed the answer already
(return-from do-http-request (values-list creq)))
(if* (typep creq 'cache-entry)
then ;; make-http-client-request returned a cache entry
;; which has all the needed values
(return-from do-http-request
(if* (and (member (centry-code creq) *redirect-codes*)
redirect
(if* (integerp redirect)
then (> redirect 0)
else t))
then (let ((new-location (cdr (assoc :location (centry-headers creq)))))
(apply #'do-http-request
(net.uri:merge-uris new-location uri)
:redirect
(if* (integerp redirect)
then (1- redirect)
else redirect)
args))
else
(values (centry-body creq)
(centry-code creq)
(centry-headers creq)
uri
))))
(unwind-protect
(let (new-location)
(net.aserve::maybe-accumulate-log
(:xmit-client-response-headers "~s")
(loop
(if* (catch 'premature-eof
(read-client-response-headers creq
:throw-on-eof
(and connection
'premature-eof))
;; if it's a continue, then deliver any content we're holding
;; onto and start the read again
(if* (not (eql 100 (client-request-response-code creq)))
then (return)
else (let ((sock (client-request-socket creq)))
(send-request-body sock (client-request-deferred-content creq))
(force-output sock)))
nil)
then ; got eof .. likely due to bogus
; saved connection... so try again with
; no saved connection
(ignore-errors (close connection))
(setf (getf args :connection) nil)
(return-from do-http-request
(apply 'do-http-request uri args)))))
(if* (equalp "close" (cdr (assoc :connection (client-request-headers creq))))
then ; server forced the close
; so don't return the connection
(setf (client-request-return-connection creq) nil))
(if* (and (member (client-request-response-code creq) *redirect-codes*
:test #'eq)
redirect
(member method redirect-methods :test #'eq)
(if* (integerp redirect)
then (> redirect 0)
else t)) ; unrestricted depth
then
(setq new-location
(cdr (assoc :location (client-request-headers creq)
:test #'eq))))
(if* (and digest-authorization
(equal (client-request-response-code creq)
#.(net.aserve::response-number
*response-unauthorized*))
(not recursing-call))
then ; compute digest info and retry
(if* (compute-digest-authorization
creq digest-authorization)
then (client-request-close creq)
(return-from do-http-request
(apply #'do-http-request
uri
:recursing-call t
args))))
;; auto-retry if request times out.
(if* (and (eq (client-request-response-code creq)
#.(net.aserve::response-number *response-request-timeout*))
(if (integerp retry-on-timeout)
(> retry-on-timeout 0)
retry-on-timeout))
then (net.aserve::debug-format :info "Received 408 response. Retrying request because retry-on-timeout is ~a.." retry-on-timeout)
(return-from do-http-request
(apply #'do-http-request
uri
:retry-on-timeout
(if* (integerp retry-on-timeout)
then (1- retry-on-timeout)
else retry-on-timeout)
:recursing-call t
args)))
(if* (or (and (null new-location)
; not called when redirecting
(if* (functionp skip-body)
then (funcall skip-body creq)
else skip-body))
(member (client-request-response-code creq)
' (#.(net.aserve::response-number
*response-no-content*)
#.(net.aserve::response-number
*response-not-modified*)
))
(and (eq method :connect)
(eq (client-request-response-code creq)
#.(net.aserve::response-number *response-ok*))))
then
(return-from do-http-request
(values
nil ; no body
(client-request-response-code creq)
(client-request-headers creq)
(client-request-uri creq)
(and (client-request-return-connection creq)
(setf (client-request-return-connection creq)
:yes)
(client-request-socket creq))
)))
(if* read-body-hook
then (funcall read-body-hook creq :format format)
else (let ((body (read-response-body creq :format format)))
(net.aserve::debug-format :xmit-client-response-body "~s" body)
(if* (client-request-cacheable creq)
then (insert-into-cache cache
creq
body))
(if* new-location
then ; must do a redirect to get to the real site
(client-request-close creq)
(apply #'do-http-request
(net.uri:merge-uris new-location uri)
:redirect
(if* (integerp redirect)
then (1- redirect)
else redirect)
args)
else (if* (client-request-return-connection creq)
then (setf (client-request-return-connection creq)
:yes)
else (client-request-close creq))
(values
body
(client-request-response-code creq)
(client-request-headers creq)
(client-request-uri creq)
(client-request-socket creq)
)))))
;; protected form:
(and (not read-body-hook) (client-request-close creq)))))
(defun convert-trailers (trailers)
;; trailers looks like (("Foo" . "foo value") ("Bar" . "bar value"))
;; need to change header names to keyword symbols in lower case
(mapcar #'(lambda (trailer)
(cons (intern (string-downcase (car trailer)) (symbol-package :foo))
(cdr trailer)))
trailers))
(defun http-copy-file (url pathname &rest args &key
proxy
proxy-basic-authorization
(headers nil)
(protocol :http/1.1)
(basic-authorization nil)
progress-function
&allow-other-keys)
(ensure-directories-exist pathname)
(let ((creq
(make-http-client-request
url
:headers headers
:protocol protocol
:basic-authorization basic-authorization
:proxy proxy
:proxy-basic-authorization proxy-basic-authorization))
progress-at)
(if* progress-function
then (let ((size (bytes-at-url url
:proxy proxy
:proxy-basic-authorization proxy-basic-authorization
:headers headers
:protocol protocol
:basic-authorization basic-authorization)))
(do ((n 9 (1- n))
(size size))
((= n 0))
(push (truncate (* size (/ n 10))) progress-at))))
(net.aserve::maybe-accumulate-log
(:xmit-client-response-headers "~s")
(loop
(read-client-response-headers creq)
;; if it's a continue, then start the read again
(if* (not (eql 100 (client-request-response-code creq)))
then (return))))
(dolist (arg '(:proxy :proxy-basic-authorization :headers
:protocol :basic-authorization))
(remf args arg))
(apply #'http-copy-file-request creq pathname :progress-at progress-at args)))
(defun bytes-at-url (url &key
proxy
proxy-basic-authorization
(headers nil)
(protocol :http/1.1)
(basic-authorization nil))
"Return the size of the URL using a HEAD request."
(multiple-value-bind (res code hdrs)
(do-http-request url
:method :head
:proxy proxy
:proxy-basic-authorization proxy-basic-authorization
:headers headers
:protocol protocol
:basic-authorization basic-authorization)
(declare (ignore res))
(let (size temp)
(if* (not (eql 200 code))
then (error "~a: code ~a" url code))
(handler-case
(setq size
(parse-integer
(or (setq temp
(cdr (assoc :content-length hdrs :test #'eq)))
(error "Cannot determine content length for ~a."
url))))
(error ()
(error "Cannot parse content-length: ~a." temp)))
size)))
(defun http-copy-file-request (creq pathname
&rest args
&key (if-does-not-exist :error)
(redirect 5)
(buffer-size 1024)
(progress-function nil)
(tmp-name-function
(lambda (pathname)
(format nil "~a.tmp" pathname)))
progress-at
size
timeout
&aux (redirect-codes
'(#.(net.aserve::response-number
*response-found*)
#.(net.aserve::response-number
*response-moved-permanently*)
#.(net.aserve::response-number
*response-see-other*))))
"Copy the data from the CREQ into PATHNAME."
(ensure-directories-exist pathname)
(let* ((url (client-request-uri creq))
(uri (net.uri:parse-uri url))
(buf (make-array buffer-size :element-type '(unsigned-byte 8)))
end
code
new-location
s
tmp-pathname
(bytes-read 0))
(unwind-protect
(progn
(setq tmp-pathname (funcall tmp-name-function pathname))
(setq s (open tmp-pathname :direction :output
;; bug16130: in case one was left laying around:
:if-exists :supersede))
(if* (and (member (client-request-response-code creq)
redirect-codes :test #'eq)
redirect
(if* (integerp redirect)
then (> redirect 0)
else t)) ; unrestricted depth
then (setq new-location
(cdr (assoc :location (client-request-headers creq)
:test #'eq))))
(net.aserve::maybe-accumulate-log (:xmit-client-response-body "~s")
(loop
(if* (and timeout (numberp timeout))
then (let ((res (sys:with-timeout (timeout :timed-out)
(setq end
(client-request-read-sequence buf creq)))))
(if* (eq :timed-out res)
then (error "~a is not responding."
(net.uri:uri-host uri))))
else (setq end (client-request-read-sequence buf creq)))
(if* (zerop end)
then (if* progress-function
then (funcall progress-function -1 size))
(return)) ;; EOF
(if* progress-at
then (incf bytes-read buffer-size)
(if* (> bytes-read (car progress-at))
then (setq progress-at (cdr progress-at))
(ignore-errors (funcall progress-function bytes-read
size))))
(write-sequence buf s :end end)))
(setq code (client-request-response-code creq))
(if* new-location
then (client-request-close creq)
(close s)
(setq s nil)
;; created above, 0 length
(delete-file tmp-pathname)
(setq new-location (net.uri:merge-uris new-location url))
(return-from http-copy-file-request
(apply #'http-copy-file new-location pathname
:redirect (if* (integerp redirect)
then (1- redirect)
else redirect)
args))
elseif (eql 404 code)
then (let ((fs "~a does not exist."))
(if* (eq :error if-does-not-exist)
then (error fs url)
else (warn fs url)
(return-from http-copy-file-request nil)))
elseif (not (eql 200 code))
then (error "Bad code from webserver: ~s." code))
(close s)
(setq s nil)
(rename-file-raw tmp-pathname pathname))
(if* s
then ;; An error occurred.
(close s)
(ignore-errors (delete-file tmp-pathname))
(ignore-errors (delete-file pathname)))
(client-request-close creq))
t))
(defmacro with-socket-connect-timeout ((&key timeout host port)
&body body)
;;
;; to wrap around a call to make-socket
;;
`(mp:with-timeout ((or ,timeout 99999999)
(error 'allegroserve-error
:action (format nil "connecting to a socket at ~s:~s"
,host ,port)
:result (format nil "exceeded timeout of ~s seconds"
,timeout)
:identifier :connect-timeout))
,@body))
(defmacro with-stream-closed-on-failure ((stream) &body body)
(let ((%success (gensym)))
`(let ((,%success nil))
(unwind-protect
(multiple-value-prog1
(progn ,@body)
(setq ,%success t))
(when (and ,stream (not ,%success))
(ignore-errors (close ,stream :abort t)))))))
(defun send-request-body (sock content)
(if* content
then ;; content can be a vector or a list of vectors
;; or a computed-content object
(dolist (cont content)
(typecase cont
(computed-content
(write-content cont sock))
(t
(net.aserve::debug-format
:info "client sending content of ~d characters/bytes"
(length cont))
(net.aserve::debug-format
:xmit-client-request-body
"~s" (if (stringp cont)
cont
(octets-to-string cont :external-format :octets)))
(write-sequence cont sock))))))
(defun ssl-has-sni-p ()
;; Return T if the :ssl module is loaded *and* has the SNI feature.
;;
;; We cannot do this at compile time because :ssl isn't loaded when
;; aserve is compiled. In fact, the test is written so that autoloading
;; of :ssl is not triggered.
#-(and (not zacl) (version>= 10 1)) nil
#+(and (not zacl) (version>= 10 1))
(let ((sym (find-symbol (symbol-name :*ssl-features*)
(find-package :acl-socket))))
(when (and sym
(boundp sym)
(member :sni (symbol-value sym) :test #'eq))
t)))
(defun make-auth-hash (user password)
(when user
(format nil "Basic ~a" (base64-encode (format nil "~a:~a" user password)))))
(defun make-http-client-request (uri &rest args
&key
(method :get) ; :get, :post, ....
(protocol :http/1.1)
keep-alive
(accept "*/*")
cookies ; nil or a cookie-jar
basic-authorization
digest-authorization
compress
content
content-length
content-type
query
headers
proxy
proxy-basic-authorization
no-proxy
user-agent
(external-format
*default-aserve-external-format*)
ssl
ssl-args
ssl-method
timeout
certificate
key
certificate-password
ca-file
ca-directory
crl-file
crl-check
verify
max-depth
connection
use-socket
cache ;; client-cache object
)
;; scheme proxy ssl do
;; ------ ----- --- --
;; http nil nil http
;; http nil t ssl
;; https nil any ssl
;; http nonnil any http to proxy
;; https nonnil any connect and https through tunnel
;;
;; (note if proxy given but host is on the no-proxy list then
;; proxy is set to nil for this call)
;;
(declare (ignorable timeout certificate key certificate-password ca-file
ca-directory crl-file crl-check verify max-depth
ssl-method ssl-args))
(if* (and connection (not use-socket))
then ; try using it
(handler-case (return-from make-http-client-request
(apply #'make-http-client-request
uri
:use-socket connection
args))
(error (c)
(declare (ignore c))
(ignore-errors (close connection))
; drop into code to do it normally
)))
(if* (and (not use-socket)
proxy)
then (multiple-value-setq (use-socket proxy)
(apply #'maybe-setup-proxy-tunnel uri args)))
(let (host sock port fresh-uri scheme-default-port cookie-string cacheable)
;; start a request
(with-stream-closed-on-failure (sock)
;; CONNECT method requests do not require a uri
;; but do require a proxy host:port.
(if* (eq method :connect)
then (if* (not proxy)
then (error "A proxy argument must be supplied when making a connect request."))
else ; parse the uri we're accessing
(if* (not (typep uri 'net.uri:uri))
then (setq uri (net.uri:parse-uri uri)
fresh-uri t))
; make sure it's an http uri
(case (or (net.uri:uri-scheme uri) :http)
(:http nil)
(:https (setq ssl t))
(t (error "Can only do client access of http or https uri's, not ~s" uri)))
; make sure that there's a host
(if* (null (setq host (net.uri:uri-host uri)))
then (error "need a host in the client request: ~s" uri))
(setq scheme-default-port