forked from franzinc/aserve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cl
1639 lines (1378 loc) · 50.8 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
;;
;; copyright (c) 1986-2005 Franz Inc, Berkeley, CA - All rights reserved.
;; copyright (c) 2000-2013 Franz Inc, Oakland, CA - All rights reserved.
;;
;; This code is free software; you can redistribute it and/or
;; modify it under the terms of the version 2.1 of
;; the GNU Lesser General Public License as published by
;; the Free Software Foundation, as clarified by the AllegroServe
;; prequel found in license-allegroserve.txt.
;;
;; This code 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
;; Lesser General Public License for more details.
;;
;; Version 2.1 of the GNU Lesser General Public License is in the file
;; license-lgpl.txt that was distributed with this file.
;; If it is not present, you can access it from
;; http://www.gnu.org/copyleft/lesser.txt (until superseded by a newer
;; version) or write to the Free Software Foundation, Inc., 59 Temple Place,
;; Suite 330, Boston, MA 02111-1307 USA
;;
;;
;; $Id: client.cl,v 1.58 2007/12/26 19:02:27 jkf Exp $
;; 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)
(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)
(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)
))
(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)
))
(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
(make-instance 'net.aserve::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."))))
(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))
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)
user-agent
(external-format *default-aserve-external-format*)
ssl ; do an ssl connection
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
;; 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
:proxy-basic-authorization proxy-basic-authorization
:user-agent user-agent
:external-format external-format
:ssl ssl
: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
)))
(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 start the read again
(if* (not (eql 100 (client-request-response-code creq)))
then (return))
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* (equal "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)
'(#.(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*))
: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))))
(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))
)))
(let ((body (read-response-body creq :format format)))
(net.aserve::debug-format :xmit-client-response-body "~s" 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
(values
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))
))))
;; protected form:
(client-request-close creq))))
(defun http-copy-file (url pathname
&rest args
&key (if-does-not-exist :error)
proxy
proxy-basic-authorization
(redirect 5)
(buffer-size 1024)
(headers nil)
(protocol :http/1.1)
(basic-authorization nil)
(progress-function nil)
(tmp-name-function
(lambda (pathname)
(format nil "~a.tmp" pathname)))
timeout
&aux (redirect-codes
'(#.(net.aserve::response-number
*response-found*)
#.(net.aserve::response-number
*response-moved-permanently*)
#.(net.aserve::response-number
*response-see-other*))))
(ensure-directories-exist pathname)
(let ((uri (net.uri:parse-uri url))
(creq
(make-http-client-request
url
:headers headers
:protocol protocol
:basic-authorization basic-authorization
:proxy proxy
:proxy-basic-authorization proxy-basic-authorization))
(buf (make-array buffer-size :element-type '(unsigned-byte 8)))
end
code
new-location
s
tmp-pathname
(bytes-read 0)
size
temp
progress-at)
(unwind-protect
(progn
(if* progress-function
then (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))
(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)))
(do ((n 9 (1- n))
(size size))
((= n 0))
(push (truncate (* size (/ n 10))) progress-at))))
(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))
(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))))
(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
(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 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))
(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
user-agent
(external-format
*default-aserve-external-format*)
ssl
ssl-method
timeout
certificate
key
certificate-password
ca-file
ca-directory
crl-file
crl-check
verify
max-depth
connection
use-socket
)
(declare (ignorable timeout certificate key certificate-password ca-file
ca-directory crl-file crl-check verify max-depth ssl-method))
(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
)))
(let (host sock port fresh-uri scheme-default-port)
;; start a request
;; 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
(case (or (net.uri:uri-scheme uri) (if* ssl
then :https
else :http))
(:http 80)
(:https 443)))
; default the port to what's appropriate for http or https
(setq port (or (net.uri:uri-port uri) scheme-default-port)))
(if* proxy
then ; sent request through a proxy server
(assert (stringp proxy) (proxy)
"proxy value ~s should be a string" proxy)
(multiple-value-bind (phost pport)
(net.aserve::get-host-port proxy)
(if* (null phost)
then (error "proxy arg should have form \"foo.com\" ~
or \"foo.com:8000\", not ~s" proxy))
(setq sock
(or use-socket
(with-socket-connect-timeout (:timeout timeout
:host phost
:port pport)
(socket:make-socket :remote-host phost
:remote-port pport
:format :bivalent
:type net.aserve::*socket-stream-type*
:nodelay t
)))))
elseif use-socket
then ; persistent connection
(setq sock use-socket)
else
(setq sock
(with-socket-connect-timeout (:timeout timeout
:host host
:port port)
(socket:make-socket :remote-host host
:remote-port port
:format :bivalent
:type
net.aserve::*socket-stream-type*
:nodelay t
)))
(if* ssl
then #+(version>= 8 0)
(setq sock
(funcall 'socket::make-ssl-client-stream sock
: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
:method (or ssl-method :sslv23)
:max-depth max-depth))
#-(version>= 8 0)
(setq sock
(funcall 'socket::make-ssl-client-stream sock))))
(if* (not use-socket)
then ; a fresh socket, so set params
#+(and allegro (version>= 6 0))
(let ((ef (find-external-format external-format)))
#+(version>= 6) (net.aserve::warn-if-crlf ef)
(setf (stream-external-format sock) ef))
(if* net.aserve::*watch-for-open-sockets*
then (schedule-finalization
sock
#'net.aserve::check-for-open-socket-before-gc))
#+io-timeout
(if* (integerp timeout)
then (socket:socket-control
sock
:read-timeout timeout
:write-timeout timeout)))
(if* query
then (case method
((:get :put) ; add info the uri
; must not blast a uri we were passed
(if* (not fresh-uri)
then (setq uri (net.uri:copy-uri uri)))
(setf (net.uri:uri-query uri) (query-to-form-urlencoded
query
:external-format
external-format)))
(:post ; make the content
(if* content
then (error "Can't specify both query ~s and content ~s"
query content))
(setq content (query-to-form-urlencoded
query :external-format external-format)
content-type "application/x-www-form-urlencoded"))))
(let ((command (format nil "~a ~a ~a"
(string-upcase (string method))
(if* (eq method :connect)
then ;; deliver 'uri' untouched
uri
else (if* proxy
then (net.uri:render-uri uri nil)
else (uri-path-etc uri)))
(string-upcase (string protocol)))))
(format sock "~a~a" command crlf)
(net.aserve::debug-format :xmit-client-request-command "~s" command))
; write often to trigger error if connection closed
(if* use-socket then (force-output sock))
; always send a Host header, required for http/1.1 and a good idea
; for http/1.0
(net.aserve::maybe-accumulate-log (:xmit-client-request-headers "~s")
(if* (not (eql scheme-default-port port))
then (net.aserve::format-dif :xmit-client-request-headers
sock "Host: ~a:~a~a" host port crlf)
else (net.aserve::format-dif :xmit-client-request-headers
sock "Host: ~a~a" host crlf))
; now the headers
(if* (and keep-alive (eq protocol :http/1.0))
then ; for http/1.1 keep alive is the default so no need
; to express it
(net.aserve::format-dif :xmit-client-request-headers
sock "Connection: Keep-Alive~a" crlf)
elseif (and (not keep-alive) (eq protocol :http/1.1))
then ; request it close for us
(net.aserve::format-dif :xmit-client-request-headers
sock "Connection: close~a" crlf))
(if* accept
then (net.aserve::format-dif :xmit-client-request-headers
sock "Accept: ~a~a" accept crlf))
(if* compress
then (net.aserve::format-dif :xmit-client-request-headers
sock "Accept-Encoding: gzip~a" crlf))
; some webservers (including AServe) have trouble with put/post
; requests without a body
(if* (and (not content) (member method '(:put :post)))
then (setf content ""))
; content can be a nil, a single vector or a list of vectors.
; canonicalize..
(if* (and content (atom content)) then (setq content (list content)))
(if* content
then (let ((computed-length 0))
(dolist (content-piece content)
(typecase content-piece
((array character (*))
(if* (null content-length)
then (incf computed-length
(native-string-sizeof
content-piece
:external-format external-format))))
((array (unsigned-byte 8) (*))
(if* (null content-length)
then (incf computed-length (length content-piece))))
(t (error "Illegal content array: ~s" content-piece))))
(if* (null content-length)
then (setq content-length computed-length))))
(if* content-length
then (net.aserve::format-dif :xmit-client-request-headers
sock "Content-Length: ~s~a" content-length crlf))
(if* cookies
then (let ((str (compute-cookie-string uri
cookies)))
(if* str
then (net.aserve::format-dif :xmit-client-request-headers
sock "Cookie: ~a~a" str crlf))))
(if* basic-authorization
then (net.aserve::format-dif :xmit-client-request-headers
sock "Authorization: Basic ~a~a"
(base64-encode
(format nil "~a:~a"
(car basic-authorization)
(cdr basic-authorization)))
crlf))
(if* proxy-basic-authorization
then (net.aserve::format-dif :xmit-client-request-headers
sock "Proxy-Authorization: Basic ~a~a"
(base64-encode
(format nil "~a:~a"
(car proxy-basic-authorization)
(cdr proxy-basic-authorization)))
crlf))
(if* (and digest-authorization
(digest-response digest-authorization))
then ; put out digest info
(net.aserve::format-dif
:xmit-client-request-headers sock
"Authorization: Digest username=~s, realm=~s, nonce=~s, uri=~s, qop=~a, nc=~a, cnonce=~s, response=~s~@[, opaque=~s~]~a"
(digest-username digest-authorization)
(digest-realm digest-authorization)
(digest-nonce digest-authorization)
(digest-uri digest-authorization)
(digest-qop digest-authorization)
(digest-nonce-count digest-authorization)
(digest-cnonce digest-authorization)
(digest-response digest-authorization)
(digest-opaque digest-authorization)
crlf))
(if* user-agent
then (if* (stringp user-agent)
thenret
elseif (eq :aserve user-agent)
then (setq user-agent net.aserve::*aserve-version-string*)
elseif (eq :netscape user-agent)
then (setq user-agent "Mozilla/4.7 [en] (WinNT; U)")
elseif (eq :ie user-agent)
then (setq user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)")
else (error "Illegal user-agent value: ~s" user-agent))
(net.aserve::format-dif :xmit-client-request-headers
sock "User-Agent: ~a~a" user-agent crlf))
(if* content-type
then (net.aserve::format-dif :xmit-client-request-headers
sock "Content-Type: ~a~a"
content-type
crlf))
(if* headers
then (dolist (header headers)
(net.aserve::format-dif :xmit-client-request-headers
sock "~a: ~a~a"
(car header) (cdr header) crlf)))
(if* use-socket then (force-output sock)))
(write-string crlf sock) ; final crlf
; send out the content if there is any.
; this has to be done differently so that if it looks like we're
; going to block doing the write we start another process do the
; the write.