File: htmlskip.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (277 lines) | stat: -rw-r--r-- 6,536 bytes parent folder | download
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
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

// A state machine to skip extraneous HTML in an
// HTTP CONNECT proxy response.

// Matches on typical HTML blocks:
//
// <!doctype html> ... </html>
// <html> ... </html>
//
// Notes:
// 1. Case insensitive
// 2. </html> can be followed by CR/LF chars

#ifndef OPENVPN_HTTP_HTMLSKIP_H
#define OPENVPN_HTTP_HTMLSKIP_H

#include <openvpn/buffer/buffer.hpp>

namespace openvpn::HTTP {

class HTMLSkip
{
  public:
    enum Status
    {
        PENDING,
        MATCH,
        NOMATCH,
    };

    HTMLSkip()
        : state(INITIAL),
          residual(64, 0),
          bytes(0)
    {
    }

    Status add(unsigned char c)
    {
        bool retain = false;

        ++bytes;
        switch (state)
        {
        case INITIAL:
            retain = true;
            if (c == '<')
                state = O_OPEN;
            else
                state = FAIL;
            break;
        case O_OPEN:
            retain = true;
            if (c == '!')
                state = O_BANG;
            else if (c == 'h' || c == 'H')
                state = O_HTML_H;
            else
                state = FAIL;
            break;
        case O_BANG:
            retain = true;
            if (c == 'd' || c == 'D')
                state = O_DOCTYPE_D;
            else
                state = FAIL;
            break;
        case O_DOCTYPE_D:
            retain = true;
            if (c == 'o' || c == 'O')
                state = O_DOCTYPE_O;
            else
                state = FAIL;
            break;
        case O_DOCTYPE_O:
            retain = true;
            if (c == 'c' || c == 'C')
                state = O_DOCTYPE_C;
            else
                state = FAIL;
            break;
        case O_DOCTYPE_C:
            retain = true;
            if (c == 't' || c == 'T')
                state = O_DOCTYPE_T;
            else
                state = FAIL;
            break;
        case O_DOCTYPE_T:
            retain = true;
            if (c == 'y' || c == 'Y')
                state = O_DOCTYPE_Y;
            else
                state = FAIL;
            break;
        case O_DOCTYPE_Y:
            retain = true;
            if (c == 'p' || c == 'P')
                state = O_DOCTYPE_P;
            else
                state = FAIL;
            break;
        case O_DOCTYPE_P:
            retain = true;
            if (c == 'e' || c == 'E')
                state = O_DOCTYPE_SPACE;
            else
                state = FAIL;
            break;
        case O_DOCTYPE_SPACE:
            retain = true;
            if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
                break;
            else if (c == 'h' || c == 'H')
                state = O_HTML_H;
            else
                state = FAIL;
            break;
        case O_HTML_H:
            retain = true;
            if (c == 't' || c == 'T')
                state = O_HTML_T;
            else
                state = FAIL;
            break;
        case O_HTML_T:
            retain = true;
            if (c == 'm' || c == 'M')
                state = O_HTML_M;
            else
                state = FAIL;
            break;
        case O_HTML_M:
            if (c == 'l' || c == 'L')
            {
                state = CONTENT;
                residual.reset_content();
            }
            else
            {
                state = FAIL;
                retain = true;
            }
            break;
        case CONTENT:
            if (c == '<')
                state = C_OPEN;
            break;
        case C_OPEN:
            if (c == '/')
                state = C_SLASH;
            else
                state = CONTENT;
            break;
        case C_SLASH:
            if (c == 'h' || c == 'H')
                state = C_HTML_H;
            else
                state = CONTENT;
            break;
        case C_HTML_H:
            if (c == 't' || c == 'T')
                state = C_HTML_T;
            else
                state = CONTENT;
            break;
        case C_HTML_T:
            if (c == 'm' || c == 'M')
                state = C_HTML_M;
            else
                state = CONTENT;
            break;
        case C_HTML_M:
            if (c == 'l' || c == 'L')
                state = C_HTML_L;
            else
                state = CONTENT;
            break;
        case C_HTML_L:
            if (c == '>')
                state = C_CRLF;
            else
                state = CONTENT;
            break;
        case C_CRLF:
            if (!(c == '\n' || c == '\r'))
            {
                residual.reset_content();
                residual.push_back(c);
                state = DONE;
            }
            break;
        default:
            retain = true;
            break;
        }

        if (retain)
            residual.push_back(c);

        switch (state)
        {
        case DONE:
            return MATCH;
        case FAIL:
            return NOMATCH;
        default:
            return PENDING;
        }
    }

    void get_residual(BufferAllocated &buf) const
    {
        if (residual.size() <= buf.offset())
        {
            buf.prepend(residual.c_data(), residual.size());
        }
        else
        {
            BufferAllocated newbuf(residual.size() + buf.size(), 0);
            newbuf.write(residual.c_data(), residual.size());
            newbuf.write(buf.c_data(), buf.size());
            buf.move(newbuf);
        }
    }

    unsigned long n_bytes() const
    {
        return bytes;
    }

  private:
    enum State
    {
        DONE,
        FAIL,
        INITIAL,
        O_OPEN,
        O_BANG,
        O_DOCTYPE_D,
        O_DOCTYPE_O,
        O_DOCTYPE_C,
        O_DOCTYPE_T,
        O_DOCTYPE_Y,
        O_DOCTYPE_P,
        O_DOCTYPE_SPACE,
        O_HTML_H,
        O_HTML_T,
        O_HTML_M,
        CONTENT,
        C_OPEN,
        C_SLASH,
        C_HTML_H,
        C_HTML_T,
        C_HTML_M,
        C_HTML_L,
        C_CRLF,
    };

    State state;
    BufferAllocated residual;
    unsigned long bytes;
};

} // namespace openvpn::HTTP

#endif