summaryrefslogtreecommitdiff
path: root/sapi/cgi/libfcgi/include/fcgio.h
blob: 865bff385bd3dcc1234e6772d54804fd074ea481 (plain)
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
//
// Provides support for FastCGI via C++ iostreams.
//
// $Id$
//
// This work is based on routines written by George Feinberg. They
// have been mostly re-written and extensively changed by
// Michael Richards.
//
// Rewritten again with bug fixes and numerous enhancements by
// Michael Shell.
// 
// And rewritten again by Rob Saccoccio. 
//
// Special Thanks to Dietmar Kuehl for his help and the numerous custom
// streambuf examples on his web site.
//
// Copyright (c) 2000 Tux the Linux Penguin
// Copyright (c) 2001 Rob Saccoccio and Chelsea Networks
//
// You are free to use this software without charge or royalty
// as long as this notice is not removed or altered, and recognition
// is given to the author(s)
//
// This code is offered as-is without any warranty either expressed or
// implied; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  If it breaks, you get to keep 
// both halves.

#ifndef FCGIO_H
#define FCGIO_H

#include <iostream.h>

#include "fcgiapp.h"

#ifndef DLLAPI
#ifdef _WIN32
#define DLLAPI __declspec(dllimport)
#else
#define DLLAPI
#endif
#endif

/*
 *  fcgi_streambuf
 */
class fcgi_streambuf : public streambuf
{
public:

    // Note that if no buf is assigned (the default), iostream methods
    // such as peek(), unget() and putback() will fail.  If a buf is
    // assigned, I/O is a bit less effecient and output streams will
    // have to be flushed (or the streambuf destroyed) before the next 
    // call to "accept".
    DLLAPI fcgi_streambuf(FCGX_Stream * fcgx, char * buf, int len);
    
    DLLAPI fcgi_streambuf(char * buf, int len);
    
    DLLAPI fcgi_streambuf(FCGX_Stream * fcgx = NULL);

    DLLAPI ~fcgi_streambuf(void);

    DLLAPI int attach(FCGX_Stream * fcgx);

protected:

    // Consume the put area (if buffered) and c (if c is not EOF).
    DLLAPI virtual int overflow(int);

    // Flush the put area (if buffered) and the FCGX buffer to the client.
    DLLAPI virtual int sync();

    // Remove and return the current character.
    DLLAPI virtual int uflow();

    // Fill the get area (if buffered) and return the current character.
    DLLAPI virtual int underflow();

    // Use a buffer.  The only reasons that a buffer would be useful is
    // to support the use of the unget()/putback() or seek() methods.  Using
    // a buffer will result in less efficient I/O.  Note: the underlying
    // FastCGI library (FCGX) maintains its own input and output buffers.  
    DLLAPI virtual streambuf * setbuf(char * buf, int len);

    DLLAPI virtual int xsgetn(char * s, int n);
    DLLAPI virtual int xsputn(const char * s, int n);

private:

    FCGX_Stream * fcgx;

    // buf is just handy to have around
    char * buf;

    // this isn't kept by the base class
    int bufsize;
    
    void init(FCGX_Stream * fcgx, char * buf, int bufsize);

    void reset(void);
};

/*
 *  fcgi_istream - deprecated
 */
class fcgi_istream : public istream
{
public:

    // deprecated
    DLLAPI fcgi_istream(FCGX_Stream * fcgx = NULL);
    
    // deprecated
    DLLAPI ~fcgi_istream(void) {}

    // deprecated
    DLLAPI virtual void attach(FCGX_Stream * fcgx);

private:

    fcgi_streambuf fcgi_strmbuf;
};

/*
 *  fcgi_ostream - deprecated
 */
class fcgi_ostream : public ostream
{
public:
    
    // deprecated
    DLLAPI fcgi_ostream(FCGX_Stream * fcgx = NULL);
    
    // deprecated
    DLLAPI ~fcgi_ostream(void) {}

    // deprecated
    DLLAPI virtual void attach(FCGX_Stream *fcgx);

private:

    fcgi_streambuf fcgi_strmbuf;
};

#endif /* FCGIO_H */