summaryrefslogtreecommitdiff
path: root/src/HttpServer.cc
blob: 3191bc1a0f69c1f6d3fd1f27abd6a0f79c7413dd (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
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
#include <map>

#include "Http.h"
#include "HttpServer.h"
#include "Socket.h"
#include "BStream.h"

static const ev_tstamp s_httpTimeout = 15;

namespace Balau {

class HttpWorker : public Task {
  public:
      HttpWorker(IO<Socket> & io, void * server);
      ~HttpWorker();
  private:
    virtual void Do();
    virtual const char * getName();

    bool handleClient();
    void send400(Events::BaseEvent * evt);

    IO<Socket> m_socket;
    IO<BStream> m_strm;
    String m_name;

    uint8_t * m_postData;
};

};

Balau::HttpWorker::HttpWorker(IO<Socket> & io, void * _server) : m_socket(io), m_strm(new BStream(io)), m_postData(NULL) {
    HttpServer * server = (HttpServer *) _server;
    m_name.set("HttpWorker(%s)", m_socket->getName());
    // copy stuff from server, such as port number, root document, base URL, etc...
}

Balau::HttpWorker::~HttpWorker() {
    if (m_postData) {
        free(m_postData);
        m_postData = NULL;
    }
}

void Balau::HttpWorker::send400(Events::BaseEvent * evt) {
    static const char str[] =
"HTTP/1.0 400 Bad Request\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Connection: close\r\n"
"\r\n"
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
"  <head>\n"
"    <title>Bad Request</title>\n"
"  </head>\n"
"\n"
"  <body>\n"
"    The HTTP request you've sent is invalid.\n"
"  </body>\n"
"</html>\n";

    setOkayToEAgain(false);
    m_socket->forceWrite(str, sizeof(str), evt);
    Balau::Printer::elog(Balau::E_HTTPSERVER, "%s had an invalid request", m_name.to_charp());
}

bool Balau::HttpWorker::handleClient() {
    Events::Timeout evtTimeout(s_httpTimeout);
    waitFor(&evtTimeout);
    setOkayToEAgain(true);

    String line;
    bool gotFirst = false;
    int method = -1;
    String url;
    String HttpVersion;
    typedef std::map<String, String> HttpHeaders_t;
    HttpHeaders_t HttpHeaders;
    if (m_postData) {
        free(m_postData);
        m_postData = NULL;
    }
    bool persistent = false;

    // read client's request
    do {
        try {
            line = m_strm->readString();
        }
        catch (EAgain) {
            if (evtTimeout.gotSignal()) {
                Balau::Printer::elog(Balau::E_HTTPSERVER, "%s timed out getting request", m_name.to_charp());
                return false;
            }
            yield();
            continue;
        }
        // until we get a blank line
        if (line == "")
            break;

        if (!gotFirst) {
            gotFirst = true;
            int urlBegin = 0;

            // first line is in the form of METHOD URL HTTP/1.x
            switch(line[0]) {
            case 'G':
                if ((line[1] == 'E') && (line[2] == 'T') && (line[3] == ' ')) {
                    urlBegin = 4;
                    method = Http::GET;
                }
                break;
            case 'P':
                if ((line[1] == 'O') && (line[2] == 'S') && (line[3] == 'T') && (line[4] == ' ')) {
                    urlBegin = 5;
                    method = Http::POST;
                }
                break;
            }
            if (urlBegin == 0) {
                send400(&evtTimeout);
                return false;
            }

            int urlEnd = line.strrchr(' ') - 1;

            if (urlEnd < urlBegin) {
                send400(&evtTimeout);
                return false;
            }

            url = line.extract(urlBegin, urlEnd - urlBegin + 1);

            int httpBegin = urlEnd + 2;

            if ((httpBegin + 5) >= line.strlen()) {
                send400(&evtTimeout);
                return false;
            }

            if ((line[httpBegin + 0] == 'H') &&
                (line[httpBegin + 1] == 'T') &&
                (line[httpBegin + 2] == 'T') &&
                (line[httpBegin + 3] == 'P') &&
                (line[httpBegin + 4] == '/')) {
                HttpVersion = line.extract(httpBegin + 5);
            } else {
                send400(&evtTimeout);
                return false;
            }

            if ((HttpVersion != "1.0") && (HttpVersion != "1.1")) {
                send400(&evtTimeout);
                return false;
            }
        } else {
            // parse HTTP header.
            int colon = line.strchr(':');
            if (colon <= 0) {
                send400(&evtTimeout);
                return false;
            }

            String key = line.extract(0, colon);
            String value = line.extract(colon + 1);

            value.trim();

            HttpHeaders[key] = value;
        }
    } while(true);

    if (!gotFirst) {
        send400(&evtTimeout);
        return false;
    }

    if (method == Http::POST) {
        int lengthStr = 0;
        HttpHeaders_t::iterator i = HttpHeaders.find("Content-Length");

        if (i != HttpHeaders.end())
            lengthStr = i->second.to_int();

        m_postData = (uint8_t *) malloc(lengthStr);

        try {
            m_strm->forceRead(m_postData, lengthStr);
        }
        catch (EAgain) {
            Assert(evtTimeout.gotSignal());
            Balau::Printer::elog(Balau::E_HTTPSERVER, "%s timed out getting request (reading POST values)", m_name.to_charp());
            return false;
        }
    }

    if (HttpVersion == "1.1") {
        HttpHeaders_t::iterator i = HttpHeaders.find("Connection");

        if (i != HttpHeaders.end()) {
            if (i->second != "close") {
                send400(&evtTimeout);
                return false;
            }
        } else {
            persistent = true;
        }
    }

    // process query; everything should be here now



    // query process finished; wrapping up and exiting.

    return persistent;
}

void Balau::HttpWorker::Do() {
    bool clientStop = false;

    while (!clientStop)
        clientStop = !handleClient() || m_socket->isClosed();
}

const char * Balau::HttpWorker::getName() {
    return m_name.to_charp();
}

typedef Balau::Listener<Balau::HttpWorker> HttpListener;

void Balau::HttpServer::start() {
    Assert(!m_started);
    m_listenerPtr = new HttpListener(m_port, m_local.to_charp());
    m_started = true;
}

void Balau::HttpServer::stop() {
    Assert(m_started);
    reinterpret_cast<HttpListener *>(m_listenerPtr)->stop();
    m_started = false;
}