File: HttpHeaderBufferer.h

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (184 lines) | stat: -rw-r--r-- 5,009 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
/*
 *  Phusion Passenger - https://www.phusionpassenger.com/
 *  Copyright (c) 2011 Phusion
 *
 *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
#ifndef _PASSENGER_HTTP_HEADER_BUFFERER_
#define _PASSENGER_HTTP_HEADER_BUFFERER_

#include <string>
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <cassert>
#include <StaticString.h>
#include <Utils/StreamBoyerMooreHorspool.h>

namespace Passenger {

using namespace std;

/**
 * Buffers an entire HTTP header, including terminating "\r\n\r\n".
 *
 * Feed data until acceptingInput() is false. The entire HTTP header
 * will become available through getData(). Non-HTTP header data is
 * not consumed and will not be included in getData(). 100-Continue
 * messages are ignored.
 *
 * This class has zero-copy support. If the first feed already contains
 * an entire HTTP header getData() will point to the fed data. Otherwise
 * this class will put data in an internal buffer, and getData() will
 * point to the internal buffer.
 *
 * This class also supports size checking through setMax(). If the HTTP
 * header exceeds this size then this bufferer will enter an error state.
 * The default max size is 128 KB.
 */
class HttpHeaderBufferer {
private:
	struct StaticData {
		StreamBMH_Occ occ;
		
		StaticData() {
			sbmh_init(NULL, &occ, (const unsigned char *) "\r\n\r\n", 4);
		}
	};
	
	static StaticData staticData;
	
	string buffer;
	StaticString data;
	unsigned int max;
	enum {
		WORKING,
		DONE,
		ERROR
	} state;
	union {
		struct StreamBMH terminatorFinder;
		char padding[SBMH_SIZE(4)];
	} u;
	
	bool is100Continue(const StaticString &buffer) const {
		return buffer.size() >= sizeof("HTTP/1.1 100 Continue\r\n") - 1
			&& memcmp(buffer.data(), "HTTP/1.", sizeof("HTTP/1.") - 1) == 0
			&& memcmp(buffer.data() + sizeof("HTTP/1.1 ") - 1,
				"100 Continue\r\n", sizeof("100 Continue\r\n") - 1) == 0;
	}

public:
	HttpHeaderBufferer() {
		sbmh_init(&u.terminatorFinder,
			NULL,
			(const unsigned char *) "\r\n\r\n",
			4);
		max = 1024 * 128;
		reset();
	}
	
	void setMax(unsigned int value) {
		max = value;
	}
	
	void reset() {
		buffer.clear();
		data = StaticString("", 0);
		sbmh_reset(&u.terminatorFinder);
		state = WORKING;
	}
	
	size_t feed(const char *data, size_t size) {
		if (state == DONE || state == ERROR) {
			return 0;
		}
		
		size_t accepted, feedSize;
		
		if (buffer.empty()) {
			feedSize = std::min<size_t>(size, max);
			accepted = sbmh_feed(&u.terminatorFinder,
				&staticData.occ,
				(const unsigned char *) "\r\n\r\n",
				4,
				(const unsigned char *) data,
				feedSize);
			if (u.terminatorFinder.found) {
				if (is100Continue(StaticString(data, accepted))) {
					reset();
					accepted += feed(data + accepted, size - accepted);
				} else {
					state = DONE;
					this->data = StaticString(data, accepted);
				}
			} else if (feedSize == max) {
				state = ERROR;
				this->data = StaticString(data, accepted);
			} else {
				assert(accepted == size);
				buffer.append(data, size);
				this->data = buffer;
			}
		} else {
			feedSize = std::min<size_t>(size, max - buffer.size());
			accepted = sbmh_feed(&u.terminatorFinder,
				&staticData.occ,
				(const unsigned char *) "\r\n\r\n",
				4,
				(const unsigned char *) data,
				feedSize);
			buffer.append(data, accepted);
			this->data = buffer;
			if (u.terminatorFinder.found) {
				if (is100Continue(buffer)) {
					reset();
					accepted += feed(data + accepted, size - accepted);
				} else {
					state = DONE;
				}
			} else if (buffer.size() == (size_t) max) {
				state = ERROR;
			}
		}
		
		return accepted;
	}
	
	bool acceptingInput() const {
		return state == WORKING;
	}
	
	bool hasError() const {
		return state == ERROR;
	}
	
	/**
	 * Get the data that has been fed so far.
	 */
	StaticString getData() const {
		return data;
	}
};

} // namespace Passenger

#endif /* _PASSENGER_HTTP_HEADER_BUFFERER_ */