root/trunk/thirdparty/chunga/output.lisp

Revision 3186, 5.8 kB (checked in by edi, 8 months ago)

Import current Chunga dev version from laptop

Previous history at http://trac.common-lisp.net/tbnl/browser/branches/chunga

Line 
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CHUNGA; Base: 10 -*-
2 ;;; $Header: /usr/local/cvsrep/chunga/output.lisp,v 1.14 2008/05/24 03:06:22 edi Exp $
3
4 ;;; Copyright (c) 2006-2008, Dr. Edmund Weitz.  All rights reserved.
5
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9
10 ;;;   * Redistributions of source code must retain the above copyright
11 ;;;     notice, this list of conditions and the following disclaimer.
12
13 ;;;   * Redistributions in binary form must reproduce the above
14 ;;;     copyright notice, this list of conditions and the following
15 ;;;     disclaimer in the documentation and/or other materials
16 ;;;     provided with the distribution.
17
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 (in-package :chunga)
31
32 (defmethod chunked-stream-output-chunking-p (object)
33   "The default method for all objects which are not of type
34 CHUNKED-OUTPUT-STREAM."
35   nil)
36
37 (defmethod write-chunk ((stream chunked-output-stream) sequence
38                         &key (start 0)
39                              (end (length sequence)))
40   "Writes the contents of SEQUENCE from START to END to the
41 underlying stream of STREAM as one chunk."
42   (let ((output-stream (chunked-stream-stream stream)))
43     ;; chunk size
44     (loop for char across (format nil "~X" (- end start))
45           do (write-byte (char-code char) output-stream))
46     (write-sequence +crlf+ output-stream)
47     ;; data
48     (write-sequence sequence output-stream :start start :end end)
49     (write-sequence +crlf+ output-stream)))
50
51 (defmethod flush-buffer ((stream chunked-output-stream))
52   "Uses WRITE-CHUNK to empty the output buffer unless it is
53 already empty."
54   (with-slots (output-buffer output-index)
55       stream
56     (when (plusp output-index)
57       (write-chunk stream output-buffer :end output-index)
58       (setq output-index 0))))
59
60 (defmethod (setf chunked-stream-output-chunking-p) (new-value (stream chunked-output-stream))
61   "Switches output chunking for STREAM on or off."
62   (unless (eq (not new-value) (not (chunked-stream-output-chunking-p stream)))
63     (with-slots (real-stream output-index)
64         stream
65       (cond (new-value
66              ;; get rid of "old" data
67              (force-output real-stream)
68              ;; initialize output buffer as being empty
69              (setq output-index 0))
70             (t (flush-buffer stream)
71                ;; last chunk to signal end of chunking
72                (write-byte #.(char-code #\0) real-stream)
73                (write-sequence +crlf+ real-stream)
74                (write-sequence +crlf+ real-stream)
75                (force-output real-stream)))))
76   (setf (slot-value stream 'output-chunking-p) new-value))
77
78 (defmethod stream-clear-output ((stream chunked-output-stream))
79   "We clear output by resetting the output buffer and clearing
80 the underlying stream."
81   (when (chunked-stream-output-chunking-p stream)
82     (setf (slot-value stream 'output-index) 0))
83   (clear-output (chunked-stream-stream stream)))
84
85 (defmethod stream-finish-output ((stream chunked-output-stream))
86   "Flush the output buffer if output chunking is on, then operate
87 on the underlying stream."
88   (when (chunked-stream-output-chunking-p stream)
89     (flush-buffer stream))
90   (finish-output (chunked-stream-stream stream)))
91
92 (defmethod stream-force-output ((stream chunked-output-stream))
93   "Flush the output buffer if output chunking is on, then operate
94 on the underlying stream."
95   (when (chunked-stream-output-chunking-p stream)
96     (flush-buffer stream))
97   (force-output (chunked-stream-stream stream)))
98
99 (defmethod stream-write-byte ((stream chunked-output-stream) byte)
100   "Writes one byte by simply adding it to the end of the output
101 buffer \(if output chunking is enabled).  The buffer is flushed
102 if necessary."
103   (unless (chunked-stream-output-chunking-p stream)
104     (return-from stream-write-byte
105       (write-byte byte (chunked-stream-stream stream))))
106   (with-slots (output-index output-buffer)
107       stream
108     (when (>= output-index +output-buffer-size+)
109       (flush-buffer stream))
110     (setf (aref output-buffer output-index) byte)
111     (incf output-index)
112     byte))
113
114 (defmethod stream-write-sequence ((stream chunked-output-stream) sequence start end &key)
115   "Outputs SEQUENCE by appending it to the output buffer if it's
116 small enough.  Large sequences are written directly using
117 WRITE-CHUNK."
118   (unless (chunked-stream-output-chunking-p stream)
119     (return-from stream-write-sequence
120       (write-sequence sequence (chunked-stream-stream stream) :start start :end end)))
121   (with-slots (output-buffer output-index)
122       stream
123     (let ((length (- end start)))
124       (cond ((<= length (- +output-buffer-size+ output-index))
125              (replace output-buffer sequence :start1 output-index
126                       :start2 start :end2 end)
127              (incf output-index length))
128             (t (flush-buffer stream)
129                (write-chunk stream sequence :start start :end end)))))
130   sequence)
131
132 (defmethod close ((stream chunked-output-stream) &key abort)
133   "When a stream is closed and ABORT isn't true we have to make
134 sure to send the last chunk."
135   (unless abort
136     (setf (chunked-stream-output-chunking-p stream) nil))
137   (call-next-method))
Note: See TracBrowser for help on using the browser.