aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/fastcgi.h
blob: 693ff829bdc8272a521ec750b6c3e9b6f153467d (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
/*
 * Copyright (C) 2007-2019 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_FAST_CGI_H
#define BU_FAST_CGI_H

#include "bu/string.h"
#include "bu/hash.h"
#include "bu/array.h"
#include "bu/sockettcp.h"
#include "bu/serversockettcp.h"

namespace Bu
{
    class Stream;

    class FastCgi
    {
    public:
        FastCgi( int iPort );
        FastCgi();
        virtual ~FastCgi();

        static bool isEmbedded();

        typedef Bu::Hash<Bu::String, Bu::String> StrHash;
        enum RequestType
        {
            typeBeginRequest    = 1,
            typeAbortRequest    = 2,
            typeEndRequest      = 3,
            typeParams          = 4,
            typeStdIn           = 5,
            typeStdOut          = 6,
            typeStdErr          = 7,
            typeData            = 8,
            typeGetValues       = 9,
            typeGetValuesResult = 10
        };

        enum Role
        {
            roleResponder       = 1,
            roleAuthorizer      = 2,
            roleFilter          = 3
        };

        enum Flags
        {
            flagsKeepConn       = 1
        };

        enum Status
        {
            statusRequestComplete   = 0,
            statusCantMpxConn       = 1,
            statusOverloaded        = 2,
            statusUnknownRole       = 3
        };

        typedef struct {
            uint8_t uVersion;
            uint8_t uType;
            uint16_t uRequestId;
            uint16_t uContentLength;
            uint8_t uPaddingLength;
            uint8_t uReserved;
        } Record;

        typedef struct {
            uint16_t uRole;
            uint8_t uFlags;
            uint8_t reserved[5];
        } BeginRequestBody;

        typedef struct {
            uint32_t uStatus;
            uint8_t uProtocolStatus;
            uint8_t reserved[3];
        } EndRequestBody;

        typedef struct Channel {
            Channel() : uFlags( 0 ) { }
            StrHash hParams;
            Bu::String sStdIn;
            Bu::String sData;
            uint8_t uFlags;
        } Channel;

        enum ChannelFlags
        {
            chflgParamsDone     = 0x01,
            chflgStdInDone      = 0x02,
            chflgDataDone       = 0x04,

            chflgAllDone        = 0x03
        };

        virtual void run();

        void stopRunning() { bRunning = false; }

        virtual void onInit() { };
        virtual int onRequest( const StrHash &hParams,
            const Bu::String &sStdIn, Bu::Stream &sStdOut,
            Bu::Stream &sStdErr )=0;
        virtual void onUninit() { };

    private:
        void read( Bu::Socket *pSock, Record &r );
        void read( Bu::Socket *pSock, BeginRequestBody &b );
        uint32_t readLen( Bu::Socket *pSock, uint16_t &uUsed );
        void readPair( Bu::Socket *pSock, StrHash &hParams, uint16_t &uUsed );

        void write( Bu::Socket *pSock, Record r );
        void write( Bu::Socket *pSock, EndRequestBody b );

        bool hasChannel( int iChan );

    private:
        Bu::ServerSocket *pSrv;
        bool bRunning;
        Bu::Array<Channel *> aChannel;
    };

    Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::FastCgi::Record &r );
};

#endif