File: FCGIInputStream.java

package info (click to toggle)
iipimage 1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 7,376 kB
  • sloc: sh: 8,127; cpp: 7,791; ansic: 3,983; java: 872; makefile: 200; perl: 2
file content (381 lines) | stat: -rw-r--r-- 11,223 bytes parent folder | download | duplicates (14)
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
/*
 * @(#)FCGIInputStream.java
 *
 *      FastCGi compatibility package Interface
 *
 *
 *  Copyright (c) 1996 Open Market, Inc.
 *
 * See the file "LICENSE.TERMS" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * $Id: FCGIInputStream.java,v 1.4 2000/03/21 12:12:25 robs Exp $
 */
package com.fastcgi;

import java.io.*;

/**
 * This stream manages buffered reads of FCGI messages.
 */
public class FCGIInputStream extends InputStream
{
    private static final String RCSID = "$Id: FCGIInputStream.java,v 1.4 2000/03/21 12:12:25 robs Exp $";

    /* Stream vars */

    public int rdNext;
    public int stop;
    public boolean isClosed;

    /* require methods to set, get and clear */
    private int errno;
    private Exception errex;

    /* data vars */

    public byte buff[];
    public int buffLen;
    public int buffStop;
    public int type;
    public int contentLen;
    public int paddingLen;
    public boolean skip;
    public boolean eorStop;
    public FCGIRequest request;

    public InputStream in;


    /**
    * Creates a new input stream to manage fcgi prototcol stuff
    * @param in the input stream  bufLen  length of buffer streamType
    */
    public FCGIInputStream(FileInputStream inStream, int bufLen,
        int streamType,
        FCGIRequest inReq) {

        in = inStream;
        buffLen = Math.min(bufLen,FCGIGlobalDefs.def_FCGIMaxLen);
        buff = new byte[buffLen];
        type = streamType;
        stop = rdNext = buffStop = 0;
        isClosed = false;
        contentLen = 0;
        paddingLen = 0;
        skip = false;
        eorStop = false;
        request = inReq;

    }
    /**
    * Reads a byte of data. This method will block if no input is
    * available.
    * @return  the byte read, or -1 if the end of the
    *      stream is reached.
    * @exception IOException If an I/O error has occurred.
    */
    public int read() throws IOException {
        if (rdNext != stop) {
            return buff[rdNext++] & 0xff;
        }
        if (isClosed){
            return -1;
        }
        fill();
        if (rdNext != stop){
            return buff[rdNext++] & 0xff;
        }
        return -1;
    }
    /**
    * Reads into an array of bytes.  This method will
    * block until some input is available.
    * @param b the buffer into which the data is read
    * @return  the actual number of bytes read, -1 is
    *      returned when the end of the stream is reached.
    * @exception IOException If an I/O error has occurred.
    */
    public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }

    /**
    * Reads into an array of bytes.
    * Blocks until some input is available.
    * @param b the buffer into which the data is read
    * @param off the start offset of the data
    * @param len the maximum number of bytes read
    * @return  the actual number of bytes read, -1 is
    *      returned when the end of the stream is reached.
    * @exception IOException If an I/O error has occurred.
    */
    public int read(byte b[], int off, int len) throws IOException {
        int m, bytesMoved;

        if (len <= 0){
            return 0;
        }
        /*
        *Fast path: len bytes already available.
        */

        if (len <= stop - rdNext){
            System.arraycopy(buff, rdNext, b, off, len);
            rdNext += len;
            return len;
        }
        /*
        *General case: stream is closed or fill needs to be called
        */
        bytesMoved = 0;
        for(;;){
            if (rdNext != stop){
                m = Math.min(len - bytesMoved, stop - rdNext);
                System.arraycopy(buff, rdNext, b, off, m);
                bytesMoved += m;
                rdNext += m;
                if (bytesMoved == len)
                    return bytesMoved;
                off += m;
            }
            if (isClosed){
                return bytesMoved;
            }
            fill();

        }
    }
    /**
    * Reads into an array of bytes.  This method will
    * block until some input is available.
    * @param b the buffer into which the data is read
    * @param off the start offset of the data
    * @param len the maximum number of bytes read
    * @return  the actual number of bytes read, -1 is
    *      returned when the end of the stream is reached.
    * @exception IOException If an I/O error has occurred.
    */
    public void  fill() throws IOException {
        byte[] headerBuf = new byte[FCGIGlobalDefs.def_FCGIHeaderLen];
        int headerLen = 0;
        int status = 0;
        int count = 0;
        for(;;) {
            /*
            * If buffer is empty, do a read
            */
            if (rdNext == buffStop) {
                try {
                    count = in.read(buff, 0, buffLen);
                } catch (IOException e) {
                    setException(e);
                    return;
                }
                if (count == 0) {
                    setFCGIError(FCGIGlobalDefs.def_FCGIProtocolError);
                    return;
                }
                rdNext = 0;
                buffStop = count;       // 1 more than we read
            }
            /* Now buf is not empty: If the current record contains more content
             * bytes, deliver all that are present in buff to callers buffer
             * unless he asked for less than we have, in which case give him less
             */
            if (contentLen > 0) {
                count = Math.min(contentLen, buffStop - rdNext);
                contentLen -= count;
                if (!skip) {
                    stop = rdNext + count;
                    return;
                }
                else {
                    rdNext += count;
                    if (contentLen > 0) {
                        continue;
                    }
                    else {
                        skip = false;
                    }
                }
            }
            /* Content has been consumed by client.
             * If record was padded, skip over padding
             */
            if (paddingLen > 0) {
                count = Math.min(paddingLen, buffStop - rdNext);
                paddingLen -= count;
                rdNext += count;
                if (paddingLen > 0) {
                    continue;    // more padding to read
                }
            }
            /* All done with current record, including the padding.
             * If we are in a recursive call from Process Header, deliver EOF
             */
            if (eorStop){
                stop = rdNext;
                isClosed = true;
                return;
            }
            /*
             * Fill header with bytes from input buffer - get the whole header.
             */
            count = Math.min(headerBuf.length - headerLen, buffStop - rdNext);
            System.arraycopy(buff,rdNext, headerBuf, headerLen, count);
            headerLen += count;
            rdNext  += count;
            if (headerLen < headerBuf.length) {
                continue;
            }
            headerLen = 0;
            /*
             * Interperet the header. eorStop prevents ProcessHeader from
             * reading past the end of record when using stream to read content
             */
            eorStop = true;
            stop = rdNext;
            status = 0;
            status = new FCGIMessage(this).processHeader(headerBuf);
            eorStop = false;
            isClosed = false;
            switch (status){
            case FCGIGlobalDefs.def_FCGIStreamRecord:
                if (contentLen == 0) {
                    stop = rdNext;
                    isClosed = true;
                    return;
                }
                break;
            case FCGIGlobalDefs.def_FCGISkip:
                skip = true;
                break;
            case FCGIGlobalDefs.def_FCGIBeginRecord:
                /*
                * If this header marked the beginning of a new
                * request, return role info to caller
                */
                return;
            case FCGIGlobalDefs.def_FCGIMgmtRecord:
                break;
            default:
                /*
                * ASSERT
                */
                setFCGIError(status);
                return;

            }
        }
    }

    /**
     * Skips n bytes of input.
     * @param n the number of bytes to be skipped
     * @return  the actual number of bytes skipped.
     * @exception IOException If an I/O error has occurred.
     */
    public long skip(long n) throws IOException {
        byte data[] = new byte[(int)n];
        return in.read(data);
    }

    /*
     * An FCGI error has occurred. Save the error code in the stream
     * for diagnostic purposes and set the stream state so that
     * reads return EOF
     */
    public void setFCGIError(int errnum) {
        /*
        * Preserve only the first error.
        */
        if(errno == 0) {
            errno = errnum;
        }
        isClosed = true;
    }
    /*
    * An Exception has occurred. Save the Exception in the stream
    * for diagnostic purposes and set the stream state so that
    * reads return EOF
    */
    public void setException(Exception errexpt) {
        /*
        * Preserve only the first error.
        */
        if(errex == null) {
            errex = errexpt;
        }
        isClosed = true;
    }

    /*
    * Clear the stream error code and end-of-file indication.
    */
    public void clearFCGIError() {
        errno = 0;
        /*
        * isClosed = false;
        * XXX: should clear isClosed but work is needed to make it safe
        * to do so.
        */
    }
    /*
    * Clear the stream error code and end-of-file indication.
    */
    public void clearException() {
        errex = null;
        /*
        * isClosed = false;
        * XXX: should clear isClosed but work is needed to make it safe
        * to do so.
        */
    }

    /*
    * accessor method since var is private
    */
    public int getFCGIError() {
        return errno;
    }
    /*
    * accessor method since var is private
    */
    public Exception getException() {
        return errex;
    }
    /*
    * Re-initializes the stream to read data of the specified type.
    */
    public void setReaderType(int streamType) {

        type = streamType;
        eorStop = false;
        skip = false;
        contentLen = 0;
        paddingLen = 0;
        stop = rdNext;
        isClosed = false;
    }

    /*
    * Close the stream. This method does not really exist for BufferedInputStream in java,
    * but is implemented here for compatibility with the FCGI structures being used. It
    * doent really throw any IOExceptions either, but that's there for compatiblity with
    * the InputStreamInterface.
    */
    public void close() throws IOException{
        isClosed = true;
        stop = rdNext;
    }

    /*
    * Returns the number of bytes that can be read without blocking.
    */

    public int available() throws IOException {
        return stop - rdNext + in.available();
    }

}