Debian Codegraph @

lib/rfc/mime-port.scm

raw
1 ;;;
2 ;;; mime-port.scm - submodule to read from mime part body
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
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 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; This module is autoloaded from rfc.mime. You don't need to "use" this
35 ;; directly.
36 (define-module rfc.mime-port
37 (use gauche.uvector)
38 (use gauche.vport)
39 (use data.queue)
40 (use util.match)
41 (export make-mime-port))
42 (select-module rfc.mime-port)
43
44 ;;===============================================================
45 ;; Virtual port to recognize mime boundary
46 ;;
47
48 (define-class <mime-port> (<buffered-input-port>)
49 ((state :init-form 'prologue)
50 ;; prologue -> boundary <-> body -> eof
51 ))
52
53 ;; Creates a procedural port, which reads from SRCPORT until it reaches
54 ;; either EOF or MIME boundary. Basically it runs a DFA.
55 (define (make-mime-port boundary srcport)
56 (define q (make-queue))
57 (define --boundary (string->u8vector #"--~boundary"))
58
59 (define port (make <mime-port>))
60
61 (define eof (read-from-string ""))
62
63 (define (deq! q)
64 (if (queue-empty? q) eof (dequeue! q)))
65
66 (define (fifo! q b)
67 (enqueue! q b) (dequeue! q))
68
69 (define (getb)
70 (if (queue-empty? q)
71 (case (ref port 'state)
72 [(prologue) (skip-prologue)]
73 [(boundary eof) eof]
74 [else (newb)])
75 (dequeue! q)))
76
77 (define (newb)
78 (match (read-byte srcport)
79 [(and #x0d b) ;; CR, check to see LF
80 (let1 b2 (peek-byte srcport)
81 (if (eqv? b2 #x0a)
82 (begin
83 (read-byte srcport)
84 (enqueue! q b #x0a)
85 (check-boundary))
86 b))]
87 [(and #x0a b) ;; LF, check boundary
88 (enqueue! q b) (check-boundary)]
89 [(? eof-object?) (set! (ref port 'state) 'eof) eof]
90 [b b]))
91
92 (define (check-boundary)
93 (let loop ((b (peek-byte srcport))
94 (ind 0)
95 (max (u8vector-length --boundary)))
96 (cond [(eof-object? b) (deq! q)]
97 [(= ind max)
98 (cond [(memv b '(#x0d #x0a)) ;;found boundary
99 (read-byte srcport) ;;consume LF or CRLF
100 (when (and (eqv? #x0d b)
101 (eqv? #x0a (peek-byte srcport)))
102 (read-byte srcport))
103 (dequeue-all! q)
104 (set! (ref port 'state) 'boundary)
105 eof]
106 [(eqv? b #x2d) ;; maybe end boundary
107 (enqueue! q (read-byte srcport))
108 (cond [(eqv? (peek-byte srcport) #x2d);; yes, end boundary
109 (read-byte srcport)
110 (dequeue-all! q)
111 (skip-epilogue)]
112 [else (deq! q)])]
113 [else (deq! q)])]
114 [(= b (u8vector-ref --boundary ind))
115 (enqueue! q (read-byte srcport))
116 (loop (peek-byte srcport) (+ ind 1) max)]
117 [(queue-empty? q) (newb)]
118 [else (dequeue! q)])))
119
120 ;; Reads past the first boundary. The first boundary may appear
121 ;; at the beginning of the message (instead of after CRLF), so
122 ;; we need slightly different handling than the normal getb.
123 (define (skip-prologue)
124 (let loop ((b (check-boundary)))
125 (cond
126 [(eof-object? b)
127 (cond [(eq? (ref port 'state) 'boundary) ; we've found the boundary
128 (set! (ref port 'state) 'body) (getb)]
129 [else ; no boundary found
130 (set! (ref port 'state) 'eof) eof])]
131 [(queue-empty? q) (loop (newb))]
132 [else (dequeue-all! q) (loop (newb))])))
133
134 (define (skip-epilogue)
135 (let loop ((b (read-byte srcport)))
136 (if (eof-object? b)
137 (begin (set! (ref port 'state) 'eof) b)
138 (loop (read-byte srcport)))))
139
140 ;; fills vector, until it sees either
141 ;; (1) vec got full
142 ;; (2) srcport reaches EOF
143 ;; (3) mime-boundary is read
144 (define (fill vec)
145 (let1 len (u8vector-length vec)
146 (let loop ((ind 0))
147 (if (= ind len)
148 len
149 (let1 b (getb)
150 (if (eof-object? b)
151 ind
152 (begin (u8vector-set! vec ind b)
153 (loop (+ ind 1)))))))))
154
155 (set! (ref port 'fill) fill)
156 port)