-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cl
3107 lines (2570 loc) · 91.4 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
;;
;; copyright (c) 1986-2000 Franz Inc, Berkeley, CA - All rights reserved.
;; copyright (c) 2000-2004 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: main.cl,v 1.49 2011/06/20 18:06:23 kevinrosenberg Exp $
;; 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)
(defparameter *aserve-version* '(1 2 42))
#+allegro
(eval-when (eval load)
(require :sock)
(require :process)
#+ (and allegro (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
(defparameter *debug-all* nil) ; all of the debugging switches
(defparameter *debug-log* nil) ; all debugging switches that write info
; to the *debug-stream*
(defparameter *debug-current* nil) ; current switches set
(defparameter *debug-stream* *initial-terminal-io*)
; 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* #+allegro t #-allegro nil)
(defmacro define-debug-kind (name class what)
`(progn (ecase ,class
(:all (pushnew ,name *debug-all*))
(:log (pushnew ,name *debug-log*)
(pushnew ,name *debug-all*)))
(setf (get ,name 'debug-description) ,what)))
(define-debug-kind :notrap :all
"If set than errors in handlers cause a break loop to be entered")
(define-debug-kind :xmit :log
"If set then most of the traffic between clients and servers is also sent to the debug stream")
(define-debug-kind :info :log
"General information")
(defun debug-on (&rest args)
;; add the given debug kinds to the log list
(if* (null args)
then (note-debug-set)
else (dolist (arg args)
(case arg
(:all (setq *debug-current* *debug-all*))
(:log (setq *debug-current*
(union *debug-current* *debug-log*)))
(t (pushnew arg *debug-current*))))))
(defun debug-off (&rest args)
;; turn off the debugging
(if* (null args)
then (note-debug-set)
else (dolist (arg args)
(case arg
(:all (setq *debug-current* nil))
(:log (setq *debug-current*
(set-difference *debug-current* *debug-log*)))
(t (setq *debug-current* (remove arg *debug-current*)))))))
(defun note-debug-set ()
;; describe what debugging switches exist and if they are on
;; and off
(dolist (kind *debug-all*)
(format t "~7s ~4a ~a~%"
kind
(if* (member kind *debug-current*)
then "on"
else "off")
(get kind 'debug-description))))
(defmacro debug-format (kind &rest args)
;; do the format to *debug-stream* if the kind of this info
;; is matched by the value of *debug-current*
`(if* (member ,kind *debug-current* :test #'eq)
then (write-sequence
(concatenate 'string
(format nil "d> (~a): " (acl-compat.mp:process-name acl-compat.mp:*current-process*))
(format nil ,@args))
*debug-stream*)))
(defmacro format-dif (debug-key &rest args)
;; do the format and also do the same format to the
;; debug stream if the given debug keyword is set
;; do the format and then send to *initial-terminal-io*
`(progn (format ,@args)
(if* (member ,debug-key *debug-current* :test #'eq)
then ; do extra consing to ensure that it all be written out
; at once
(write-sequence
(concatenate 'string
(format nil "x>(~a): "
(acl-compat.mp:process-name acl-compat.mp:*current-process*))
(format nil ,@(cdr args)))
*debug-stream*))))
(defmacro if-debug-action (kind &body body)
;; only do if the debug value is high enough
`(progn (if* (member ,kind *debug-current* :test #'eq)
then ,@body)))
(defun check-for-open-socket-before-gc (socket)
(if* (open-stream-p socket)
then (logmess
(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
#+(and allegro 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)
)
#+(and lispworks unix)
(progn
(fli:define-foreign-function (setuid "setuid" :source)
((x :int))
:result-type :int
:language :ansi-c)
(fli:define-foreign-function (setgid "setgid" :source)
((x :int))
:result-type :int
:language :ansi-c)
(fli:define-foreign-function (getpid "getpid" :source)
()
:result-type :int
:language :ansi-c)
(fli:define-foreign-function (unix-fork "fork" :source)
()
:result-type :int
:language :ansi-c)
(fli:define-foreign-function (unix-kill "kill" :source)
((pid :int) (sig :int))
:result-type :int
:language :ansi-c)
(fli:define-foreign-function (unix-signal "signal" :source)
((sig :int) (hdl :int))
:result-type :void
:language :ansi-c))
#+(and cmu unix)
(progn
(alien:def-alien-routine "setuid" integer (x integer))
(alien:def-alien-routine "setgid" integer (x integer))
(alien:def-alien-routine "getpid" integer)
(alien:def-alien-routine ("fork" unix-fork) integer)
(alien:def-alien-routine ("kill" unix-kill) integer (pid integer) (sig integer))
(alien:def-alien-routine ("signal" unix-signal) c-call:void (sig integer) (hdl integer))
)
#+(and clisp unix)
(defun getpid () (unix:getpid))
#+ (and sbcl unix)
(progn
(defun getpid () (sb-posix:getpid))
(defun setuid (uid) (sb-posix:setuid uid))
(defun setgid (gid) (sb-posix:setgid gid))
(defun unix-fork () (sb-posix:fork)))
#+openmcl
(defun getpid () (ccl::getpid))
;; more specials
(defvar *max-socket-fd* 0) ; 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
;; NDL 2004-06-04 -- external-formats are implementation-dependent...
(defvar *default-aserve-external-format* #-lispworks :latin1-base #+lispworks :latin-1)
(defvar *worker-request*) ; set to current request object
(defvar *read-request-timeout* 20)
(defvar *read-request-body-timeout* 60)
(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.
)
; this is only useful on acl6.1 where we do timeout on I/O operations
(defvar *http-io-timeout* 120)
; 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
(declaim (special *header-block-sresource* *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
;;;;;;;;;;;;; end special vars
(defclass wserver ()
;; all the information contained in a web server
(
;;
;;-- user visible slots --
;; (accessors exported)
(socket ;; listening socket
:initform nil
:initarg :socket
:accessor wserver-socket)
(enable-keep-alive ;; do keep alive if it's possible
:initform t
:initarg :enable-keep-alive
:accessor wserver-enable-keep-alive)
(enable-chunking ;; do chunking if it's possible
:initform t
:initarg :enable-chunking
:accessor wserver-enable-chunking)
(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)
(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)
: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)
;;
;; -- 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-workers ;; estimate of the number of workers that are idle
:initform #-openmcl-native-threads 0 #+openmcl-native-threads (ccl:make-semaphore)
:accessor wserver-free-workers)
#+openmcl-native-threads
(work-list
:initform (ccl::make-locked-dll-header)
:accessor wserver-work-list)
(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)
(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)
))
(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 (acl-compat.socket:local-port sock)
else "-no socket-")))))
#+openmcl-native-threads
(defstruct (work-list-element (:include ccl::dll-node))
semaphore ; used to wake up the worker thread
socket)
;;;;; virtual host class
(defclass vhost ()
((log-stream :accessor vhost-log-stream
:initarg :log-stream
:initform (ensure-stream-lock *trace-output*))
(error-stream :accessor vhost-error-stream
:initarg :error-stream
:initform (ensure-stream-lock *trace-output*))
(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)
))
(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
)
&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)))
`(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)
)
(catch 'with-http-response
;(format t "timeout is ~d~%" ,g-timeout)
(compute-strategy ,g-req ,g-ent ,g-format)
(up-to-date-check ,g-check-modified ,g-req ,g-ent)
(acl-compat.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)))))
(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))
#+allegro (g-external-format (gensym))
)
`(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 #+allegro (ignore-if-unused ,g-req ,g-ent ,g-external-format)
#-allegro (ignorable ,g-req ,g-ent))
,(if* body
then `(compute-response-stream ,g-req ,g-ent))
(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 (let ((*html-stream* (request-reply-stream ,g-req)))
#+(and allegro (version>= 6 0 pre-final 1))
(if* (and (streamp *html-stream*)
(not (eq ,g-external-format
(stream-external-format *html-stream*))))
then (warn-if-crlf ,g-external-format)
(setf (stream-external-format *html-stream*)
,g-external-format))
(progn ,@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))))
(defmacro atomic-incf (var)
;;; mt +++ these are giving me problems in Clozure Common Lisp Version 1.7-r14925M, so reverting to acl-compat version
; #+openmcl-native-threads
; `(ccl::atomic-incf ,var)
#+sbcl
`(acl-compat.mp::atomic-incf ,var)
#-(or sbcl); openmcl-native-threads
`(acl-compat.mp:without-scheduling (incf ,var)))
(defmacro atomic-decf (var)
; #+openmcl-native-threads
; `(ccl::atomic-decf ,var)
#+sbcl
`(acl-compat.mp::atomic-decf ,var)
#-(or sbcl) ; openmcl-native-threads
`(acl-compat.mp:without-scheduling (decf ,var)))
(defun atomic-change-wserver-free-workers (server incf)
#-(and lispworks (not (or lispworks4 lispworks5)))
(if incf
(atomic-incf (wserver-free-workers server))
(atomic-decf (wserver-free-workers server)))
;;; Tis is quite inefficient, and can be much much faster
;;; if it was another accessor. But it i snot used that often.
#+(and lispworks (not (or lispworks4 lispworks5)))
(sys:atomic-incf (slot-value server 'free-workers)
(if incf 1 -1)))
;;;;;;;;; end macros
#+aserve-not-using-run-reasons
(progn
(defun accept-thread-maybe-activate-worker-thread (thread socket)
#+lispworks
(lw:when-let (mailbox (mp:process-mailbox thread))
(when (and (mp:mailbox-empty-p mailbox)
(not (mp:get-process-private-property 'busy thread)))
(mp:process-send thread socket) ;; process-send also "Poke" it, which
;; is needed because we use process-wait-local
t)))
(defun worker-thread-get-socket-for-connection ()
#+lispworks
(let ((mailbox (mp:ensure-process-mailbox)))
(setf (mp:process-private-property 'busy) nil)
(mp:process-wait-local "Waiting for a socket in the mailbox"
'mp:mailbox-peek mailbox)
(setf (mp:process-private-property 'busy) t) ;;; before reading the message
(sys:ensure-memory-after-store)
(mp:mailbox-read mailbox)))
;; This is needed just because the code with run-reason needs it.
(defun worker-thread-done-processing (sock)
(declare (ignorable sock)))
(defun make-worker-thread-internal (name initial-bindings)
#+lispworks (let ((mp:*process-initial-bindings* initial-bindings))
(atomic-change-wserver-free-workers *wserver* t)
(mp:process-run-function name () 'http-worker-thread)))
) ; progn #+not-using-run-reasons
#-aserve-not-using-run-reasons
(progn
(defun accept-thread-maybe-activate-worker-thread (thread socket)
(when (null (acl-compat.mp:process-run-reasons thread))
(acl-compat.mp:process-add-run-reason thread socket)
t))
(defun worker-thread-get-socket-for-connection ()
(car (acl-compat.mp:process-run-reasons acl-compat.mp:*current-process*)))
(defun worker-thread-done-processing (sock)
(acl-compat.mp:process-revoke-run-reason acl-compat.mp:*current-process* sock))
(defun make-worker-thread-internal (name initial-bindings)
(let ((proc (acl-compat.mp:make-process :name name
:initial-bindings initial-bindings)))
(acl-compat.mp:process-preset proc #'http-worker-thread)
#-openmcl-native-threads
(atomic-change-wserver-free-workers *wserver* t)
#+openmcl-native-threads
(ccl:process-enable proc)
proc))
) ;; progn #-aserve-not-using-run-reasons
(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))))
;; The following is a workaround for a CormanLisp 2.0 bug.
;; When fixed I'll remove this code.
;; See my bug report to the mailing list:
;; http://groups.yahoo.com/group/cormanlisp/message/1131
;;
#+cormanlisp
(defun request-headers (req)
(declare (ignore req)))
#+cormanlisp
(defun (setf request-headers) (value req)
(declare (ignore req value)))
(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))))))
(defmacro reply-header-slot-value (req name)
;; name is a string naming the header value (all lower case)
;; 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-reply-headers* :test #'eq))
then ; has a fast accesor
`(,(third ent) ,req)
else ; must get it from the alist
`(cdr (assoc ,name (request-reply-headers ,req) :test #'eq)))))
(defsetf reply-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-reply-headers* :test #'eq))
then `(setf (,(third ent) ,req) ,newval)
else (let ((genvar (gensym))
(nreq (gensym)))
`(let* ((,nreq ,req)
(,genvar (assoc ,name (request-reply-headers ,nreq)
:test #'eq)))
(if* (null ,genvar)
then (push (setq ,genvar (cons ,name nil))
(request-reply-headers ,nreq)))
(setf (cdr ,genvar) ,newval))))))
(defun header-name-error (name)
(error "You attempted to reference header ~s. Headers are now named
by keyword symbols and not by strings"
name))
(defmacro header-slot-value-integer (req name)
;; if the header value exists and has an integer value
;; return two values: the value of the integer and t
;; else return nil
`(header-decode-integer (header-slot-value ,req ,name)))
(defclass http-header-mixin ()
;; List of all the important headers we can see in any of the protocols.
;;
#.(let (res)
;; generate a list of slot descriptors for all of the
;; fast header slots
(dolist (head *fast-headers*)
(push `(,(third head) :accessor ,(third head)
:initform nil
:initarg
,(intern (symbol-name (second head)) :keyword))
res))
res))
(defclass http-request (http-header-mixin)
;;
;; incoming request and information about the reply we send to it
;;
(
;;
;; -- external slots --
;; (accessors exported)
(method ;; keyword giving the command in this request :get .. etc.
:initarg :method
:accessor request-method)
(uri ;; uri object holding the current request with the scheme, host
;; and port filled in.
:initarg :uri
:accessor request-uri)
(raw-uri ;; uri object holding the actual uri from the command
:initarg :raw-uri
:accessor request-raw-uri)
(decoded-uri-path
:initarg :decoded-uri-path
:accessor request-decoded-uri-path)
(protocol ;; symbol naming the http protocol (e.g. :http/1.0)
:initarg :protocol
:reader request-protocol)
(protocol-string ;; string naming the protcol requested
:initarg :protocol-string
:reader request-protocol-string)
(socket ;; the socket we're communicating through
:initarg :socket
:reader request-socket)
(wserver ;; wserver object for web server this request came to
:initarg :wserver
:reader request-wserver)
(raw-request ;; the actual command line from the browser
:initarg :raw-request
:reader request-raw-request)
(vhost ;; the virtual host to which this request is directed
:initarg :vhost
:initform (wserver-default-vhost *wserver*)
:accessor request-vhost)
;;
;; -- internal slots --
;;
(query-alist
;; list of conses (name . value) for the query part of the
;; current uri. This slot is filled in when information
;; is first requested by the request-query function
:initform :empty
:accessor request-query-alist)
(headers ;; alist of headers *not* stored in slots
;* use header-slot-value to retrieve header values
; rather than looking here since not all headers are stored
; here
:initform nil
:accessor request-headers)
(header-block ;; *header-block-sresource* object
:initform nil
:accessor request-header-block)
(request-body
;; if we've read the request body then this
;; is the string holding it.
:initform nil
:accessor request-request-body)
;; response
(reply-code ;; one of the *response-xx* objects
:initform nil
:accessor request-reply-code)
(reply-date
:initform (get-universal-time) ; when we're responding
:reader request-reply-date)
(reply-headers ;; alist of headers to send out
:initform nil
:accessor request-reply-headers)
(reply-content-type ;; mime type of the response
:initform nil
:accessor request-reply-content-type)
(reply-stream ;; stream to which to send response
:initform nil
:accessor request-reply-stream)
(reply-content-length
:initform nil ;; nil means "i don't know"
:accessor request-reply-content-length)
(reply-strategy ;; list of strategy objects
:initform nil
:accessor request-reply-strategy)
(reply-plist ;; general stuff in a property list form
:initform nil
:accessor request-reply-plist)
(reply-protocol-sring
;; A web server announces the highest minor level of the
;; major level of the protocol that was requested by the client.
;; Thus for now we're always http/1.1
:initform "HTTP/1.1"
:accessor request-reply-protocol-string)
)
)
(defstruct (response (:constructor make-resp (number desc)))
number
desc)
(defparameter *response-continue* (make-resp 100 "Continue"))
(defparameter *response-ok* (make-resp 200 "OK"))
(defparameter *response-created* (make-resp 201 "Created"))
(defparameter *response-accepted* (make-resp 202 "Accepted"))
(defparameter *response-partial-content*
(make-resp 206 "Partial Content"))
(defparameter *response-moved-permanently* (make-resp 301 "Moved Permanently"))
(defparameter *response-found* (make-resp 302 "Found"))
(defparameter *response-see-other* (make-resp 303 "See Other"))
(defparameter *response-not-modified* (make-resp 304 "Not Modified"))