55import primitive .collection .ByteList ;
66
77public class ParserSettings extends http_parser .lolevel .ParserSettings {
8-
8+
99 public HTTPCallback on_message_begin ;
1010 public HTTPDataCallback on_path ;
1111 public HTTPDataCallback on_query_string ;
1212 public HTTPDataCallback on_url ;
1313 public HTTPDataCallback on_fragment ;
14+ public HTTPCallback on_status_complete ;
1415 public HTTPDataCallback on_header_field ;
1516 public HTTPDataCallback on_header_value ;
16-
17+
1718 public HTTPCallback on_headers_complete ;
1819 public HTTPDataCallback on_body ;
1920 public HTTPCallback on_message_complete ;
20-
21+
2122 public HTTPErrorCallback on_error ;
22-
23+
2324 private HTTPCallback _on_message_begin ;
2425 private HTTPDataCallback _on_path ;
2526 private HTTPDataCallback _on_query_string ;
2627 private HTTPDataCallback _on_url ;
2728 private HTTPDataCallback _on_fragment ;
29+ private HTTPCallback _on_status_complete ;
2830 private HTTPDataCallback _on_header_field ;
2931 private HTTPDataCallback _on_header_value ;
3032 private HTTPCallback _on_headers_complete ;
3133 private HTTPDataCallback _on_body ;
3234 private HTTPCallback _on_message_complete ;
3335 private HTTPErrorCallback _on_error ;
34-
36+
3537 private http_parser .lolevel .ParserSettings settings ;
36-
38+
3739 protected ByteList field = new ByteList ();
3840 protected ByteList value = new ByteList ();
3941 protected ByteList body = new ByteList ();
40-
42+
4143 public ParserSettings () {
4244 this .settings = new http_parser .lolevel .ParserSettings ();
4345 createMirrorCallbacks ();
4446 attachCallbacks ();
4547 }
46-
48+
4749 protected http_parser .lolevel .ParserSettings getLoLevelSettings () {
4850 return this .settings ;
4951 }
@@ -93,19 +95,28 @@ public int cb(HTTPParser p, byte[] by, int pos, int len) {
9395 return 0 ;
9496 }
9597 };
96- this ._on_error = new HTTPErrorCallback () {
98+ this ._on_status_complete = new HTTPCallback () {
99+ @ Override
100+ public int cb (HTTPParser p ) {
101+ if (null != ParserSettings .this .on_status_complete ) {
102+ return ParserSettings .this .on_status_complete .cb (p );
103+ }
104+ return 0 ;
105+ }
106+ };
107+ this ._on_error = new HTTPErrorCallback () {
97108 @ Override
98109 public void cb (HTTPParser parser , String error ) {
99110 if (null != ParserSettings .this .on_error ) {
100111 ParserSettings .this .on_error .cb (parser , error );
101112 } else {
102113 throw new HTTPException (error );
103114 }
104-
115+
105116 }
106117 };
107-
108-
118+
119+
109120
110121// (on_header_field and on_header_value shortened to on_h_*)
111122// ------------------------ ------------ --------------------------------------------
@@ -142,19 +153,19 @@ public int cb(HTTPParser p, byte[] by, int pos, int len) {
142153 ParserSettings .this .value .clear ();
143154 }
144155 }
145-
156+
146157 if (null == ParserSettings .this .on_header_field ) {
147158 return 0 ;
148159 }
149-
160+
150161 ParserSettings .this .field .addAll (by );
151162 return 0 ;
152163 }
153164 };
154- this ._on_header_value = new HTTPDataCallback () {
165+ this ._on_header_value = new HTTPDataCallback () {
155166 @ Override
156167 public int cb (HTTPParser p , byte [] by , int pos , int len ) {
157-
168+
158169 // previous field complete, call on_field with full field value, reset field.
159170 if (0 != ParserSettings .this .field .size ()) {
160171 // check we're even interested...
@@ -167,7 +178,7 @@ public int cb(HTTPParser p, byte[] by, int pos, int len) {
167178 ParserSettings .this .field .clear ();
168179 }
169180 }
170-
181+
171182 if (null == ParserSettings .this .on_header_value ) {
172183 return 0 ;
173184 }
@@ -195,9 +206,9 @@ public int cb(HTTPParser parser) {
195206 }
196207 return 0 ;
197208 }
198-
209+
199210 };
200- this ._on_body = new HTTPDataCallback () {
211+ this ._on_body = new HTTPDataCallback () {
201212 @ Override
202213 public int cb (HTTPParser p , byte [] by , int pos , int len ) {
203214 if (null != ParserSettings .this .on_body ) {
@@ -206,8 +217,8 @@ public int cb(HTTPParser p, byte[] by, int pos, int len) {
206217 return 0 ;
207218 }
208219 };
209-
210- this ._on_message_complete = new HTTPCallback () {
220+
221+ this ._on_message_complete = new HTTPCallback () {
211222 @ Override
212223 public int cb (HTTPParser parser ) {
213224 if (null != ParserSettings .this .on_body ) {
@@ -224,7 +235,7 @@ public int cb(HTTPParser parser) {
224235 return 0 ;
225236 }
226237 };
227-
238+
228239 }
229240
230241 private void attachCallbacks () {
@@ -234,90 +245,12 @@ private void attachCallbacks() {
234245 this .settings .on_query_string = this ._on_query_string ;
235246 this .settings .on_url = this ._on_url ;
236247 this .settings .on_fragment = this ._on_fragment ;
248+ this .settings .on_status_complete = this ._on_status_complete ;
237249 this .settings .on_header_field = this ._on_header_field ;
238- this .settings .on_header_value = this ._on_header_value ;
250+ this .settings .on_header_value = this ._on_header_value ;
239251 this .settings .on_headers_complete = this ._on_headers_complete ;
240252 this .settings .on_body = this ._on_body ;
241253 this .settings .on_message_complete = this ._on_message_complete ;
242254 this .settings .on_error = this ._on_error ;
243255 }
244256}
245- //import http_parser.HTTPException;
246- //public class ParserSettings extends http_parser.lolevel.ParserSettings{
247- //
248- //
249- //
250- //
251- // public HTTPCallback on_message_begin;
252- // public HTTPDataCallback on_path;
253- // public HTTPDataCallback on_query_string;
254- // public HTTPDataCallback on_url;
255- // public HTTPDataCallback on_fragment;
256- // public HTTPDataCallback on_header_field;
257- // public HTTPDataCallback on_header_value;
258- // public HTTPCallback on_headers_complete;
259- // public HTTPDataCallback on_body;
260- // public HTTPCallback on_message_complete;
261- // public HTTPErrorCallback on_error;
262- //
263- // void call_on_message_begin (HTTPParser p) {
264- // call_on(on_message_begin, p);
265- // }
266- //
267- // void call_on_message_complete (HTTPParser p) {
268- // call_on(on_message_complete, p);
269- // }
270- //
271- // // this one is a little bit different:
272- // // the current `position` of the buffer is the location of the
273- // // error, `ini_pos` indicates where the position of
274- // // the buffer when it was passed to the `execute` method of the parser, i.e.
275- // // using this information and `limit` we'll know all the valid data
276- // // in the buffer around the error we can use to print pretty error
277- // // messages.
278- // void call_on_error (HTTPParser p, String mes, ByteBuffer buf, int ini_pos) {
279- // if (null != on_error) {
280- // on_error.cb(p, mes, buf, ini_pos);
281- // }
282- // // if on_error gets called it MUST throw an exception, else the parser
283- // // will attempt to continue parsing, which it can't because it's
284- // // in an invalid state.
285- // throw new HTTPException(mes);
286- // }
287- //
288- // void call_on_header_field (HTTPParser p, ByteBuffer buf, int pos, int len) {
289- // call_on(on_header_field, p, buf, pos, len);
290- // }
291- // void call_on_query_string (HTTPParser p, ByteBuffer buf, int pos, int len) {
292- // call_on(on_query_string, p, buf, pos, len);
293- // }
294- // void call_on_fragment (HTTPParser p, ByteBuffer buf, int pos, int len) {
295- // call_on(on_fragment, p, buf, pos, len);
296- // }
297- // void call_on_path (HTTPParser p, ByteBuffer buf, int pos, int len) {
298- // call_on(on_path, p, buf, pos, len);
299- // }
300- // void call_on_header_value (HTTPParser p, ByteBuffer buf, int pos, int len) {
301- // call_on(on_header_value, p, buf, pos, len);
302- // }
303- // void call_on_url (HTTPParser p, ByteBuffer buf, int pos, int len) {
304- // call_on(on_url, p, buf, pos, len);
305- // }
306- // void call_on_body(HTTPParser p, ByteBuffer buf, int pos, int len) {
307- // call_on(on_body, p, buf, pos, len);
308- // }
309- // void call_on_headers_complete(HTTPParser p) {
310- // call_on(on_headers_complete, p);
311- // }
312- // void call_on (HTTPCallback cb, HTTPParser p) {
313- // // cf. CALLBACK2 macro
314- // if (null != cb) {
315- // cb.cb(p);
316- // }
317- // }
318- // void call_on (HTTPDataCallback cb, HTTPParser p, ByteBuffer buf, int pos, int len) {
319- // if (null != cb && -1 != pos) {
320- // cb.cb(p,buf,pos,len);
321- // }
322- // }
323- //}
0 commit comments