This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
http_parser.js
181 lines (139 loc) · 4.34 KB
/
http_parser.js
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
var assert = process.assert;
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
var b = Buffer("GET / HTTP/1.1\r\n\r\n");
var InternalHTTPParser = process.binding("http_parser").HTTPParser;
function HTTPParser(type) {
EventEmitter.call(this);
this.internal = new InternalHTTPParser(type);
this.waiting = [];
}
inherits(HTTPParser, EventEmitter);
HTTPParser.prototype.pause = function() {
this.paused = true;
};
HTTPParser.prototype.resume = function() {
this.paused = false;
};
HTTPParser.prototype._addHeader = function() {
if (this.field && this.value) {
if (this._headersComplete) {
this.incoming.trailers.push([this.field, this.value]);
} else {
this.incoming.headers.push([this.field, this.value]);
}
this.field = null;
this.value = null;
}
};
HTTPParser.prototype._cycle = function() {
var x;
while (!this.paused && (x = this.waiting.shift())) {
var buf = x[0];
var off = x[1];
var len = x[2];
var tokens = this.internal.execute(buf, off, len);
console.log("%j", tokens);
assert(tokens.length % 3 == 0);
var i = 0;
debugger;
while (i < tokens.length) {
var type = tokens[i++];
var arg0 = tokens[i++];
var arg1 = tokens[i++];
switch (type) {
case "ERROR":
// Emit buffer and offset of error
this.emit('error', buf, arg0);
break;
case "NEEDS_INPUT":
this.emit('needsInput');
break;
case "NEEDS_DATA_ELEMENTS":
this.waiting.unshift(buf, arg0, (off + len) - arg0);
// go around again.
break;
case "REQ_MESSAGE_START":
this.incoming = { method: arg0,
headers: [],
trailers: [] };
break;
case "RES_MESSAGE_START":
this.incoming = { statusCode: arg0,
headers: [],
trailers: [] };
break;
case "VERSION":
this.incoming.httpVersionMajor = arg0;
this.incoming.httpVersionMinor = arg1;
this.incoming.httpVersion = arg0 + '.' + arg1;
break;
case "PATH":
case "FRAGMENT":
case "QUERY_STRING":
// not using these.
break;
case "URL":
var slice = buf.toString('ascii', arg0, arg0 + arg1);
if (this.incoming.url) {
this.incoming.url += slice
} else {
this.incoming.url = slice;
}
break;
case "HEADER_FIELD":
var slice = buf.toString('ascii', arg0, arg0 + arg1).toLowerCase();
// The token before this was 'HEADER_VALUE' and so we need to add
// the value to the headers before we start with this field.
if (this.value) this._addHeader();
if (this.field) {
this.field += slice;
} else {
this.field = slice;
}
break;
case "HEADER_VALUE":
var slice = buf.toString('ascii', arg0, arg0 + arg1);
if (this.value) {
this.value += slice;
} else {
this.value = slice;
}
break;
case "HEADERS_END":
// Add the last header
if (this.value) this._addHeader();
// XXX: upgrade, shouldKeepAlive ???
this.incoming._headersComplete = true;
this.emit("messageBegin", this.incoming);
break;
case "BODY":
var body = buf.slice(arg0, arg0 + arg1);
this.emit("body", body, this.incoming);
break
case "MESSAGE_END":
// Add the last trailing header
if (this.value) this._addHeader();
this.emit("messageEnd", this.incoming);
this.incoming = null;
break;
default:
console.log(type, arg0, arg1);
throw new Error("Unknown token");
}
}
}
};
HTTPParser.prototype.execute = function(buffer, off, len) {
this.waiting.push([buffer, off, len]);
this._cycle();
};
var p = new HTTPParser('request');
p.on('messageBegin', function(m) {
console.log('messageBegin ', m);
});
p.on('messageEnd', function(m) {
console.log('messageEnd ', m);
});
var b = new Buffer("GET /blah HTTP/1.1\r\nhello: world\r\nsomething: else\r\n\r\n");
p.execute(b, 0, b.length);