File: port-acl.lisp

package info (click to toggle)
hunchentoot 0.15.7.dfsg.2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 552 kB
  • ctags: 583
  • sloc: lisp: 4,355; makefile: 30
file content (141 lines) | stat: -rw-r--r-- 6,178 bytes parent folder | download
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
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
;;; $Header: /usr/local/cvsrep/hunchentoot/port-acl.lisp,v 1.12 2008/04/08 14:39:17 edi Exp $

;;; Copyright (c) 2004-2008, Dr. Edmund Weitz. All rights reserved.

;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:

;;;   * Redistributions of source code must retain the above copyright
;;;     notice, this list of conditions and the following disclaimer.

;;;   * Redistributions in binary form must reproduce the above
;;;     copyright notice, this list of conditions and the following
;;;     disclaimer in the documentation and/or other materials
;;;     provided with the distribution.

;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(in-package :hunchentoot)

(eval-when (:compile-toplevel :load-toplevel :execute)
  #-(and :allegro-version>= (version>= 7 0))
  (error "You need at least version 7.0 of AllegroCL.")
  ;; make sure code for sockets and OS interface is loaded
  (require :sock)
  (require :osi))

(defun make-lock (name)
  "See AllegroCL documentation for MP:MAKE-PROCESS-LOCK."
  (mp:make-process-lock :name name))

(defmacro with-lock ((lock) &body body)
  "See AllegroCL documentation for MP:WITH-PROCESS-LOCK."
  `(mp:with-process-lock (,lock) ,@body))

(defmacro atomic-incf (place &optional (delta 1))
  "Like INCF but wrapped with SYS:WITHOUT-SCHEDULING so other
threads can't interfer."
  `(sys:without-scheduling (incf ,place ,delta)))

(defun process-run-function (name function &rest args)
  "See AllegroCL documentation for MP:PROCESS-RUN-FUNCTION."
  (apply #'mp:process-run-function name function args))

(defun process-kill (process)
  "See AllegroCL documentation for MP:PROCESS-KILL."
  (mp:process-kill process))

(define-symbol-macro *current-process*
  mp:*current-process*)

(defun process-allow-scheduling ()
  "See AllegroCL documentation for MP:PROCESS-ALLOW-SCHEDULE."
  (mp:process-allow-schedule))

(defun start-up-server (&key service address process-name announce function &allow-other-keys)
  "Tries to \(partly) emulate LispWorks' COMM:START-UP-SERVER.  See
<http://www.lispworks.com/documentation/lw50/LWRM/html/lwref-56.htm>
for more info."
  (let (done)
    (flet ((open-socket-and-accept ()
             (handler-bind ((error (lambda (condition)
                                     (funcall announce nil condition)
                                     (setq done condition)
                                     (return-from open-socket-and-accept))))
               (let (socket)
                 (unwind-protect
                     (progn
                       (setf socket (socket:make-socket :address-family :internet
                                       :type :hiper
                                       :format :bivalent
                                       :connect :passive
                                       :local-host address
                                       :local-port service
                                       :reuse-address t
                                       :backlog 5))
                       (funcall announce socket)
                       (setq done socket)
                       (loop (funcall function (socket:accept-connection socket :wait t))))
                   (when socket
                     (cl:ignore-errors (close socket))))))))
      (let ((listener-thread (process-run-function process-name #'open-socket-and-accept)))
        (mp:process-wait "Waiting for server to start" (lambda () done))
        (typecase done
          (socket:socket listener-thread)
          (t (values nil done)))))))

(defun make-socket-stream (socket read-timeout write-timeout)
  "Accepts a socket `handle' SOCKET and creates and returns a
corresponding stream, setting its read and write timeout if
applicable.  Returns three other values - the address the request
arrived at, and the address and port of the remote host."
  ;; in the case of AllegroCL, SOCKET:ACCEPT-CONNECTION already
  ;; returned a stream
  (socket:set-socket-options socket :nodelay t)
  (socket:socket-control socket
                         :read-timeout read-timeout
                         :write-timeout write-timeout)
  (values socket
          (ignore-errors
            (socket:ipaddr-to-dotted (socket:local-host socket)))
          (ignore-errors
            (socket:ipaddr-to-dotted (socket:remote-host socket)))
          (ignore-errors
            (socket:remote-port socket))))

(defun get-backtrace (error)
  "This is the function that is used internally by Hunchentoot to
show or log backtraces.  It accepts a condition object ERROR and
returns a string with the corresponding backtrace."
  (with-output-to-string (s)
    (with-standard-io-syntax
      (let ((*print-readably* nil)
            (*print-miser-width* 40)
            (*print-pretty* t)
            (tpl:*zoom-print-circle* t)
            (tpl:*zoom-print-level* nil)
            (tpl:*zoom-print-length* nil))
        (cl:ignore-errors
          (format *terminal-io* "~
~@<An unhandled error condition has been signalled:~3I ~a~I~:@>~%~%"
                  error))
        (cl:ignore-errors
          (let ((*terminal-io* s)
                (*standard-output* s))
            (tpl:do-command "zoom"
                            :from-read-eval-print-loop nil
                            :count t
                            :all t)))))))