-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.cl
3975 lines (3314 loc) · 128 KB
/
main.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 -*-
;;
;; main.cl
;;
;; See the file LICENSE for the full license governing this code.
;; Description:
;; aserve's main loop
;;- This code in this file obeys the Lisp Coding Standard found in
;;- http://www.franz.com/~jkf/coding_standards.html
;;-
(in-package :net.aserve)
(eval-when (compile) (declaim (optimize (speed 3))))
#+ignore
(check-smp-consistency)
(defparameter *aserve-version* '(1 3 88))
(eval-when (eval load)
(require :sock)
(require :process)
#+(version>= 6) (require :acldns) ; not strictly required but this is preferred
)
(provide :aserve)
(defparameter *aserve-version-string*
;; for when we need it in string format
(format nil "~d.~d.~d"
(car *aserve-version*)
(cadr *aserve-version*)
(caddr *aserve-version*)))
;;;;;;; debug support
;; An alist of kind and the superkinds it belongs to.
(defparameter *debug-kinds* ())
(defparameter *debug-current* nil) ; current switches set
(defparameter *debug-stream* *initial-terminal-io*)
(defparameter *debug-format* :long)
; set to true to automatically close sockets about to be gc'ed
; open sockets should never be subject to gc unless there's a bug
; in the code leading to leaks.
(defvar *watch-for-open-sockets* t)
(defun find-kind-entry (kind)
(find kind *debug-kinds* :key #'car))
(defun add-new-kind (kind superkinds documentation)
(if* (find-kind-entry kind)
then (error "Debug kind ~s already defined." kind))
(dolist (superkind superkinds)
(if* (null (find-kind-entry superkind))
then (error "Can't find superkind ~s for ~s." superkind kind)))
(push (cons kind superkinds) *debug-kinds*)
(setf (get kind 'debug-description) documentation))
(defmacro define-debug-kind (kind superkinds documentation)
`(add-new-kind ,kind ',superkinds ,documentation))
(define-debug-kind :all ()
"The mother of all debug features.")
(define-debug-kind :notrap (:all)
"If set than errors in handlers cause a break loop to be entered.")
(define-debug-kind :zoom-on-error (:all)
"If set then print a zoom to the vhost-error-stream when an error occurs in a handler.")
(define-debug-kind :log (:all)
"Category of features that write some kind of log.")
(define-debug-kind :xmit (:log)
"Category of features that log the traffic between clients, servers.")
(define-debug-kind :info (:log)
"General information.")
(define-debug-kind :client (:all)
"Category of features that log client communication.")
(define-debug-kind :server (:all)
"Category of features that log server communication.")
(define-debug-kind :proxy (:all)
"Category of features that log proxy communication.")
(define-debug-kind :request (:all)
"Category of features that log requests.")
(define-debug-kind :response (:all)
"Category of features that log responses.")
(define-debug-kind :command (:all)
"Category of features that log http request commands.")
(define-debug-kind :headers (:all)
"Category of features that log request/response headers.")
(define-debug-kind :body (:all)
"Category of features that log request/response bodies.")
(define-debug-kind :xmit-client-request-command
(:xmit :client :request :command)
"If set then print the client request commands.")
(define-debug-kind :xmit-client-request-headers
(:xmit :client :request :headers)
"If set then print the client request headers.")
(define-debug-kind :xmit-client-request-body
(:xmit :client :request :body)
"If set then print the client request bodies.")
(define-debug-kind :xmit-client-response-headers
(:xmit :client :response :headers)
"If set then print the client response headers.")
(define-debug-kind :xmit-client-response-body
(:xmit :client :response :body)
"If set then print the client response bodies.")
(define-debug-kind :xmit-server-request-command
(:xmit :server :request :command)
"If set then print the server request commands.")
(define-debug-kind :xmit-server-request-headers
(:xmit :server :request :headers)
"If set then print the server request headers.")
(define-debug-kind :xmit-server-request-body
(:xmit :server :request :body)
"If set then print the server request bodies.")
(define-debug-kind :xmit-server-response-headers
(:xmit :server :response :headers)
"If set then print the server response headers.")
(define-debug-kind :xmit-server-response-body
(:xmit :server :response :body)
"If set then print the server response bodies.")
;; These are parallell to the client and server kinds, from the point
;; of view of the proxy. That is, :xmit-proxy-client-request-command
;; is what the proxy sends on as a client to the real server.
(define-debug-kind :xmit-proxy-client-request-command
(:xmit :proxy :client :request :command)
"If set then print the proxy request command sent to the real server.")
(define-debug-kind :xmit-proxy-client-request-headers
(:xmit :proxy :client :request :headers)
"If set then print the proxy request headers sent to the real server.")
(define-debug-kind :xmit-proxy-client-request-body
(:xmit :proxy :client :request :body)
"If set then print the proxy request bodies sent to the real server.")
(define-debug-kind :xmit-proxy-client-response-headers
(:xmit :proxy :client :response :headers)
"If set then print the proxy response headers sent by the real server.")
(define-debug-kind :xmit-proxy-client-response-body
(:xmit :proxy :client :response :body)
"If set then print the proxy response bodies sent by the real server.")
;; What the proxy as a server sends to the real client. Note there are
;; no :xmit-proxy-server-request-* kinds, because at the time of
;; reading the request it's not yet known whether it's the going to be
;; proxied so these show up as :xmit-server-request-*.
(define-debug-kind :xmit-proxy-server-response-headers
(:xmit :proxy :server :response :headers)
"If set then print the proxy response headers sent to the client.")
(define-debug-kind :xmit-proxy-server-response-body
(:xmit :proxy :server :response :body)
"If set then print the proxy response bodies sent by the client.")
(defun expand-kinds (kinds)
(dolist (kind kinds)
(if* (null (find-kind-entry kind))
then (error "Can't find kind ~s." kind)))
(let ((kinds kinds))
(loop for entry in (reverse *debug-kinds*)
do (destructuring-bind (kind &rest superkinds) entry
(when (intersection superkinds kinds)
(pushnew kind kinds))))
kinds))
(defun debug-on (&rest args)
;; add the given debug kinds to the log list
(if* (null args)
then (note-debug-set)
else (setq *debug-current*
(union *debug-current* (expand-kinds args)))))
(defun debug-off (&rest args)
;; turn off the debugging
(if* (null args)
then (note-debug-set)
else (setq *debug-current*
(set-difference *debug-current* (expand-kinds args)))))
(defun note-debug-set ()
;; describe what debugging switches exist and if they are on
;; and off
(dolist (entry (reverse *debug-kinds*))
(destructuring-bind (kind &rest superkinds) entry
(format t "~40s ~4a~% ~a~%~a"
kind
(if* (member kind *debug-current*)
then "on"
else "off")
(get kind 'debug-description)
(if* superkinds
then (format nil " (parent categories: ~{~s~^, ~})~%"
superkinds)
else "")))))
(defun format-debug-message (kind stream format args)
(declare (ignore kind))
(apply #'format stream format args))
(defmacro if-debug-action (kind &body body)
;; only do if the debug value is high enough
`(if* (member ,kind *debug-current* :test #'eq)
then ,@body))
;; For logging bodies (and also to nicely accumulate multiple debug
;; messages of the same kind into a single one, we need
;; with-output-to-buffer that supports growable arrays which was first
;; available as a patch to acl 8.2. If that feature is not supported
;; in the lisp, we print an explanatory message to *error-output* and
;; go on without being able to log dynamically computed bodies (i.e.
;; those that aren't published files). Also, since accumulation
;; doesn't work, one can have several entries for the same logical
;; thing.
(eval-when (:compile-toplevel)
(defparameter *with-output-to-buffer-can-grow-array*
(null (nth-value 1 (ignore-errors
(compile nil '(lambda ()
(with-output-to-buffer (s)))))))))
#+#.(cl:if net.aserve::*with-output-to-buffer-can-grow-array* '(and) '(or))
(progn
;; An alist that maps debug kinds to streams. See
;; maybe-accumulate-log.
(defvar *accumulating-kinds-and-streams* ())
(defun accumulator-stream-for-kind (kind)
(cdr (assoc kind *accumulating-kinds-and-streams*)))
(defmacro debug-format (kind format &rest args)
;; do the format to *debug-stream* if the kind of this info
;; is matched by the value of *debug-current*
`(if-debug-action ,kind
(let ((accumulator-stream (accumulator-stream-for-kind ,kind)))
(if accumulator-stream
(format accumulator-stream ,format ,@args)
(log1 ,kind :info (format-debug-message ,kind nil ,format
(list ,@args)))))))
(defmacro maybe-accumulate-log ((debug-action sink) &body body)
(let ((debug-output-stream (gensym "debug-output-stream"))
(tag (gensym "tag"))
(%sink (gensym "sink")))
`(flet ((body ()
,@body))
(let ((,%sink ,sink))
(catch ',tag
(or (if-debug-action ,debug-action
(with-output-to-buffer (,debug-output-stream)
(unwind-protect
(let ((*accumulating-kinds-and-streams*
(or (if-debug-action ,debug-action
(cons (cons ,debug-action
,debug-output-stream)
*accumulating-kinds-and-streams*))
*accumulating-kinds-and-streams*)))
(throw ',tag (body)))
(let ((string (multiple-value-bind (buffer length)
(get-output-stream-buffer
,debug-output-stream)
(octets-to-string
buffer :end length
:external-format :octets))))
(if (functionp ,%sink)
(funcall ,%sink string)
(debug-format ,debug-action ,%sink string))))))
(body))))))))
;; This doesn't accumulate at all. It's a bandaid for lisps without a
;; with-output-to-buffer capable of growing buffers.
#-#.(cl:if net.aserve::*with-output-to-buffer-can-grow-array* '(and) '(or))
(progn
(format
*error-output*
"NOTE: This lisp does not support with-output-to-buffer with growable arrays.
Logging dynamically computed bodies will not be possible and headers, bodies
will be logged with one log entry per line in some cases.")
(defvar *accumulating-kinds-and-sinks* ())
(defun accumulator-sink-for-kind (kind)
(cdr (assoc kind *accumulating-kinds-and-sinks*)))
(defmacro debug-format (kind format &rest args)
;; do the format to *debug-stream* if the kind of this info
;; is matched by the value of *debug-current*
`(if-debug-action ,kind
(let ((accumulator-sink (accumulator-sink-for-kind ,kind))
(message (format-debug-message ,kind nil ,format
(list ,@args))))
(if* (functionp accumulator-sink)
then (let ((*accumulating-kinds-and-sinks*
(remove ,kind *accumulating-kinds-and-sinks*
:key #'car)))
(funcall accumulator-sink message))
else (log1 ,kind :info (format nil (or accumulator-sink "~a")
message))))))
(defmacro maybe-accumulate-log ((debug-action sink) &body body)
(let ((tag (gensym "tag"))
(%sink (gensym "sink")))
`(flet ((body ()
,@body))
(let ((,%sink ,sink))
(catch ',tag
(or (if-debug-action ,debug-action
(let ((*accumulating-kinds-and-sinks*
(or (if-debug-action ,debug-action
(cons (cons ,debug-action ,%sink)
*accumulating-kinds-and-sinks*))
*accumulating-kinds-and-sinks*)))
(throw ',tag (body))))
(body))))))))
(defmacro format-dif (kind &rest args)
;; do the format and also do the same format to the
;; debug stream if the given debug keyword is set
`(progn (format ,@args)
(debug-format ,kind ,@(cdr args))))
(defun check-for-open-socket-before-gc (socket)
(if* (open-stream-p socket)
then (logmess
(let ((*print-readably* nil)
(socket:*print-hostname-in-stream* nil)
)
;; explicitly binding *print-readably* nil in order to avoid
;; a printer crash if the finalization is run in a thread
;; with, say, with-standard-io-syntax that binds *print-readably*
;; true.
;;
;; bug25695
;; Bind *print-hostname-in-stream* to nil so that we don't
;; invoke acldns which may have been interrupted by this gc
;; while it held a lock that we'll need to acquire to
;; complete the hostname lookup (thus resulting in a deadlock)
;; (see ensure-nameserver-queue-process-started).
(format nil
"socket ~s is open yet is about to be gc'ed. It will be closed"
socket)))
(ignore-errors (close socket))))
;;;;;;;;;;; end debug support ;;;;;;;;;;;;
;; foreign function imports
#+unix
(progn
(ff:def-foreign-call (setuid "setuid") ((x :int)) :returning :int)
(ff:def-foreign-call (setgid "setgid") ((x :int)) :returning :int)
(ff:def-foreign-call (getpid "getpid") (:void) :returning :int)
(ff:def-foreign-call (unix-fork "fork") (:void) :returning :int)
(ff:def-foreign-call (unix-kill "kill") ((pid :int) (sig :int))
:returning :int)
)
;; more specials
(defvar-mp *max-socket-fd* nil) ; set this to 0 to enable tracking and logging of
; the maximum fd returned by accept-connection
(defvar *aserve-debug-stream* nil) ; stream to which to seen debug messages
(defvar *debug-connection-reset-by-peer* nil) ; true to signal these too
(defvar *default-aserve-external-format* :latin1-base)
(defvar *worker-request*) ; set to current request object
(defvar *read-request-timeout* 20)
(defvar *read-request-body-timeout* 60)
(defvar *http-header-read-timeout* 60) ; seconds for complete header read
(defvar *http-response-timeout*
#+io-timeout 300 ; 5 minutes for timeout if we support i/o timeouts
#-io-timeout 120 ; 2 minutes if we use this for i/o timeouts too.
)
(defvar *http-io-timeout* 120
"The number of seconds that a read or write to the socket can be
blocked before we give up and assume the client on the other side has
died. Use nil to specify no timeout.")
(defvar *http-free-worker-timeout* 3
"Number of seconds to wait for a free worker thread.")
(defvar *enable-keepalive* nil
"If true turn on keepalive for all sockets created. If an integer then also
set the time of the first keepalive packet to be that many seconds
after the connection goes idle")
(defmacro wrap-enable-keepalive (&body body)
;; the body will return a socket.
;; We enable keepalive if *enable-keepalive* is true
(let ((gs (gensym)))
`(let ((,gs (progn ,@body)))
(enable-keepalive ,gs)
,gs)))
; usually set to the default server object created when aserve is loaded.
; users may wish to set or bind this variable to a different server
; object so it is the default for publish calls.
; Also bound to the current server object in accept threads, thus
; user response functions can use this to find the current wserver object.
(defvar *wserver*)
; type of socket stream built.
; :hiper is possible in acl6
(defvar *socket-stream-type*
#+(and allegro (version>= 6)) :hiper
#-(and allegro (version>= 6)) :stream
)
;; specials from other files
(defvar *header-block-sresource*)
(defvar *header-index-sresource*)
(defvar *header-keyword-array*
;; indexed by header-number, holds the keyword naming this header
)
(defvar *not-modified-entity*) ; used to send back not-modified message
(defvar-mp *thread-index* 0) ; globalcounter to gen process names
(defvar *log-wserver-name* nil)
;; set when we detect an http request has been sent
;; to a port configured for ssl
(defvar *http-request-ssl-port* nil)
;;;;;;;;;;;;; end special vars
(defclass wserver (#+smp lockable-object)
;; all the information contained in a web server
(
;;
;;-- user visible slots --
;; (accessors exported)
(socket ;; listening socket
:initform nil
:initarg :socket
:accessor wserver-socket)
;; This was never used. We'll leave it in place for backward
;; compatiblity.
;; We now use a global *enable-keepalive* variable which applies
;; to both the server and client.
(enable-keep-alive ;; Set to nil or the timeout used by keep-alive
:initform *http-header-read-timeout*
:initarg :enable-keep-alive
:accessor wserver-keep-alive-timeout)
(enable-chunking ;; do chunking if it's possible
:initform nil
:initarg :enable-chunking
:accessor wserver-enable-chunking)
(enable-compression ;; do compression if it's possible
:initform (member :zlib-deflate *features*)
:initarg :enable-compression
:accessor wserver-enable-compression)
(compression-file-types
;; list of file types and they type of compression
;; they imply
:initform '(("gz" . :gzip))
:accessor wserver-compression-file-types)
(locators
;; list of locators objects in search order
:initform (list (make-instance-locator-exact+name :exact)
(make-instance-locator-prefix+name :prefix))
:accessor wserver-locators)
(filters
;; if non nil is is a list of functions
;; of one arg (a request object)
;; to be called before looking for a locator. This function can
;; modify the request if it feels like it.
:initform nil
:accessor wserver-filters)
(logger
;; on opaque object that's passed to log1* on which it can
;; dispatch
:initarg :logger
:initform t
:accessor wserver-logger)
(log-function
;; function to call after the request is done to
;; do the logging
:initarg :log-function
:initform nil ; no logging initially
:accessor wserver-log-function)
(log-stream
;; place for log-function to store stream to log to if
;; it makes sense to do so.
:initarg :log-stream
:initform *initial-terminal-io*
:accessor wserver-log-stream)
(accept-hook
;; if non-nil the function to call passing the socket about to be
;; processed by aserve, and charged with returning the socket to
;; process
:initarg :accept-hook
:initform nil
:accessor wserver-accept-hook)
(external-format
;; used to bind *default-aserve-external-format* in each thread
:initarg :external-format
:initform :latin1-base
:accessor wserver-external-format)
(vhosts
;; map names to vhost objects
:initform (make-hash-table :test #'equalp)
:accessor wserver-vhosts)
(default-vhost
;; vhost representing situations with no virtual host
:initarg :default-vhost
:initform (make-instance-vhost-0)
:accessor wserver-default-vhost)
(response-timeout
;; seconds a response is allowed to take before it gives up
:initarg :response-timeout
:initform *http-response-timeout*
:accessor wserver-response-timeout)
(io-timeout
;; seconds an I/O operation to an http client is allowed to take
;; before an error is signalled. This is only effective on
;; acl6.1 or newer.
:initarg :io-timeout
:initform *http-io-timeout*
:accessor wserver-io-timeout)
(header-read-timeout
;; max time to read headers
:initarg :header-read-timeout
:initform *http-header-read-timeout*
:accessor wserver-header-read-timeout)
(free-worker-timeout
;; time to wait for a free worker thread
:initform *http-free-worker-timeout*
:initarg :free-worker-timeout
:accessor wserver-free-worker-timeout)
(max-content-length
;; maximum Content-Length we'll read
;; nil means no limit
:initform nil
:initarg :max-content-length
:accessor wserver-max-content-length)
;;
;; -- internal slots --
;;
(terminal-io ;; stream active when we started server
:initform *terminal-io*
:initarg :terminal-io
:accessor wserver-terminal-io)
(worker-threads ;; list of threads that can handle http requests
:initform nil
:accessor wserver-worker-threads)
(free-worker-threads
:initform (make-queue-with-timeout)
:accessor wserver-free-worker-threads)
(free-workers ;; estimate of the number of workers that are idle
:initform 0
:accessor wserver-free-workers)
(n-workers
:initform 0
:accessor wserver-n-workers)
(max-n-workers
:initform nil
:initarg :max-n-workers
:reader wserver-max-n-workers)
(accept-thread ;; thread accepting connetions and dispatching
:initform nil
:accessor wserver-accept-thread)
(link-scan-threads ;; threads scanning cached entries for links
:initform nil
:accessor wserver-link-scan-threads)
(uri-scan-threads ;; list of uri scanning processes
:initform nil
:accessor wserver-uri-scan-threads)
(invalid-request
;; entity to invoke given a request that can't be
;; satisfied
:initform nil ; will build on demand if not present
:accessor wserver-invalid-request)
(denied-request
;; entity to invoke given a request that was denied
:initform nil ; will build on demand if not present
:accessor wserver-denied-request)
(ipaddrs
;; list of the ip addresses by which this wserver has been contacted
:initform nil
:accessor wserver-ipaddrs
)
(pcache
;; proxy cache
:initform nil
:accessor wserver-pcache)
(proxy-control
;; nil o proxy-control object if we want to
;; filter which proxies are allowed
:initform nil
:accessor wserver-proxy-control)
(shutdown-hooks
;; list of functions to call, passing this wserver object as an arg
;; when the server shuts down
:initform nil
:accessor wserver-shutdown-hooks)
(ssl
:initform nil
:initarg :ssl
:accessor wserver-ssl)
(name
:initform (format nil "w~d" (atomic-incf *thread-index*))
:initarg :name
:reader wserver-name)
(redirect-http-to-ssl
;; record the value of this option to net.aserve:start
:initform nil
:initarg :redirect-http-to-ssl
:accessor wserver-redirect-http-to-ssl)
;; The following 3 used to be global variables, but logically they need to
;; be specific to each server instance.
(debug-connection-reset-by-peer
:initarg :debug-connection-reset-by-peer
:initform *debug-connection-reset-by-peer*
:accessor wserver-debug-connection-reset-by-peer)
(read-request-timeout
:initarg :read-request-timeout
:initform *read-request-timeout*
:accessor wserver-read-request-timeout)
(read-request-body-timeout
:initarg :read-request-body-timeout
:initform *read-request-body-timeout*
:accessor wserver-read-request-body-timeout)
))
(defmethod print-object ((wserver wserver) stream)
(print-unreadable-object (wserver stream :type t :identity t)
(format stream "port ~a"
(let ((sock (wserver-socket wserver)))
(if* sock
then (socket:local-port sock)
else "-no socket-")))))
(defmethod incf-free-workers ((wserver wserver) count)
(net.aserve::smp-case
((t :macros)
(with-locked-object (wserver :non-smp :without-interrupts)
(incf (wserver-free-workers wserver) count)))
(nil
(without-interrupts
(incf (wserver-free-workers wserver) count)))))
;;;;; virtual host class
(defclass vhost ()
((log-stream :accessor vhost-log-stream
:initarg :log-stream
:initform (ensure-stream-lock *initial-terminal-io*))
(error-stream :accessor vhost-error-stream
:initarg :error-stream
:initform (ensure-stream-lock *initial-terminal-io*))
(names :accessor vhost-names
:initarg :names
:initform nil)
; vhost specific filters, see wserver-filters for doc
(filters :accessor vhost-filters
:initarg :filters
:initform nil)
; property list for users to store per-vhost specific info
(plist :accessor vhost-plist
:initarg :plist
:initform nil)
))
;; Mention class in make-instance after class def to avoid bug24329.
(defun make-instance-vhost-0 ()
(make-instance 'vhost))
(defmethod print-object ((vhost vhost) stream)
(print-unreadable-object (vhost stream :type t :identity t)
(format stream "~{ ~a~}"
(let ((names (vhost-names vhost)))
(if* (or (null names) (consp names))
then names
else (list names))))))
;;;;;; macros
(defmacro with-http-response ((req ent
&key timeout
(check-modified t)
(response '*response-ok*)
content-type
format
trailers
)
&body body)
;;
;; setup to response to an http request
;; do the checks that can shortciruit the request
;;
(let ((g-req (gensym))
(g-ent (gensym))
(g-timeout (gensym))
(g-format (gensym))
(g-check-modified (gensym))
(g-trailers (gensym))
)
`(let* ((,g-req ,req)
(,g-ent ,ent)
(,g-format ,format)
(,g-timeout ,(or timeout
`(or
(entity-timeout ,g-ent)
(wserver-response-timeout *wserver*))))
(,g-check-modified ,check-modified)
(,g-trailers ,trailers)
)
(excl:with-unreachable-code-allowed
(catch 'with-http-response
;(format t "timeout is ~d~%" ,g-timeout)
(if* ,g-trailers then (check-trailers-ok ,g-req ,g-trailers))
(compute-strategy ,g-req ,g-ent ,g-format)
(up-to-date-check ,g-check-modified ,g-req ,g-ent)
(mp::with-timeout ((if* (and (fixnump ,g-timeout) ; ok w-t
(> ,g-timeout 0))
then ,g-timeout
else 9999999)
(timedout-response ,g-req ,g-ent))
,(if* response
then `(setf (request-reply-code ,g-req) ,response))
,(if* content-type
then `(setf (request-reply-content-type ,g-req) ,content-type)
else `(setf (request-reply-content-type ,g-req) (content-type ,g-ent)))
,@body
))))))
#+(and allegro (version>= 6 0))
(defun warn-if-crlf (external-format)
(let ((ef (find-external-format external-format)))
(if* (not (eq (crlf-base-ef ef) ef))
then (warn "~
External-format `~s' passed to make-http-client-request filters line endings.
Problems with protocol may occur." (ef-name ef)))))
(defun check-external-format (external-format)
(declare (ignorable external-format))
#+(and allegro (version>= 6 0 pre-final 1))
(if* (and (streamp *html-stream*)
(not (eq external-format
(stream-external-format *html-stream*))))
then (warn-if-crlf external-format)
(setf (stream-external-format *html-stream*)
external-format)))
(defmacro with-http-body ((req ent
&key headers
(external-format
'*default-aserve-external-format*))
&body body)
(declare (ignorable external-format))
(let ((g-req (gensym))
(g-ent (gensym))
(g-headers (gensym))
(g-external-format (gensym))
(g-old-request-reply-stream (gensym)))
(declare (ignorable g-old-request-reply-stream))
`(let ((,g-req ,req)
(,g-ent ,ent)
(,g-headers ,headers)
#+(and allegro (version>= 6 0 pre-final 1))
(,g-external-format (find-external-format ,external-format))
)
(declare (ignorable ,g-req ,g-ent ,g-external-format))
(compute-response-stream ,g-req ,g-ent)
(excl:with-unreachable-code-allowed
(if* (entity-headers ,g-ent)
then (bulk-set-reply-headers ,g-req (entity-headers ,g-ent)))
(if* ,g-headers
then (bulk-set-reply-headers ,g-req ,g-headers)))
(send-response-headers ,g-req ,g-ent :pre)
(if* (not (member :omit-body (request-reply-strategy ,g-req)
:test #'eq))
then #+#.(cl:if net.aserve::*with-output-to-buffer-can-grow-array* '(and) '(or))
(if* (member :xmit-server-response-body *debug-current*)
then (maybe-accumulate-log (:xmit-server-response-body "~s")
(let ((,g-old-request-reply-stream
(request-reply-stream ,g-req)))
(unwind-protect
(let ((*html-stream*
(make-broadcast-stream
(accumulator-stream-for-kind
:xmit-server-response-body)
(request-reply-stream ,g-req))))
(check-external-format ,g-external-format)
(setf (request-reply-stream ,g-req)
*html-stream*)
,@body)
(setf (request-reply-stream ,g-req)
,g-old-request-reply-stream))))
else (let ((*html-stream* (request-reply-stream ,g-req)))
(check-external-format ,g-external-format)
,@body))
#-#.(cl:if net.aserve::*with-output-to-buffer-can-grow-array* '(and) '(or))
(let ((*html-stream* (request-reply-stream ,g-req)))
(check-external-format ,g-external-format)
,@body))
(if* (member :keep-alive (request-reply-strategy ,g-req) :test #'eq)
then ; force the body to be read so we can continue
(get-request-body ,g-req))
(send-response-headers ,g-req ,g-ent :post))))
; safe versions during multiprocessing
;; atomic-incf and atomic-decf macro definitions moved to aserve/macs.cl
;;;;;;;;; end macros
(eval-when (compile load eval)
;; these are the common headers and are stored in slots in
;; the objects
;; the list consists of ("name" . name)
;; where name is symbol naming the accessor function
(defparameter *fast-headers*
(let (res)
(dolist (name '(:connection
:date
:transfer-encoding
:accept
:host
:user-agent
:content-length))
(push (list name ;; keyword symbol name
(read-from-string (format nil "reply-~a" name)) ;; symbol name
;; accessor name
(read-from-string
(format nil "request-header-~a" name))) res))
res))
(defparameter *fast-reply-headers*
;; list of headers for the reply that at stored in slots of
;; the http request object
(let (res)
(dolist (name '(:date
:content-type
:content-length))
(push (list name ;; string name
;; symbol naming slot
(read-from-string
(concatenate 'string (symbol-name :reply-)
(string name)))
;; accessor name
(read-from-string
(format nil "request-reply-~a" name))) res))
res))
)
(defmacro header-slot-value (req name)
;; name is a keyword symbol naming the header value.
;; retrive the slot's value from the http-request req req.
(let (ent)
(if* (stringp name)
then (header-name-error name))
(if* (setq ent (assoc name *fast-headers* :test #'eq))
then ; has a fast accesor
`(or (,(third ent) ,req)
(setf (,(third ent) ,req)
(header-buffer-req-header-value ,req ,name)
))
else ; must get it from the alist
`(header-slot-value-other ,req ,name))))
(defun header-slot-value-other (req name)
;; handle out of the the 'extra' headers
(let ((ent (assoc name (request-headers req) :test #'eq)))
(if* ent
then (cdr ent)
else (let ((ans (header-buffer-req-header-value req name)))
(push (cons name ans) (request-headers req))
ans))))
(defsetf header-slot-value (req name) (newval)
;; set the header value regardless of where it is stored
(let (ent)
(if* (stringp name)
then (header-name-error name))
(if* (setq ent (assoc name *fast-headers* :test #'eq))
then `(setf (,(third ent) ,req) ,newval)
else (let ((genvar (gensym))
(nreq (gensym)))
`(let* ((,nreq ,req)
(,genvar (assoc ,name (request-headers ,nreq)
:test #'eq)))
(if* (null ,genvar)
then (push (setq ,genvar (cons ,name nil))
(request-headers ,nreq)))
(setf (cdr ,genvar) ,newval))))))