Class: Thin::HttpParser
- Inherits:
-
Object
- Object
- Thin::HttpParser
- Defined in:
- ext/thin_parser/thin.c
Instance Method Summary collapse
-
#error? ⇒ Boolean
Tells you whether the parser is in an error state.
-
#execute(req_hash, data, start) ⇒ Integer
Takes a Hash and a String of data, parses the String of data filling in the Hash returning an Integer to indicate how much of the data has been read.
-
#finish ⇒ Object
Finishes a parser early which could put in a “good” or bad state.
-
#finished? ⇒ Boolean
Tells you whether the parser is finished or not and in a good state.
-
#new ⇒ Object
constructor
Creates a new parser.
-
#nread ⇒ Integer
Returns the amount of data processed so far during this processing cycle.
-
#reset ⇒ nil
Resets the parser to it’s initial state so that you can reuse it rather than making new ones.
Constructor Details
#new ⇒ Object
Creates a new parser.
256 257 258 259 260 261 262 263 |
# File 'ext/thin_parser/thin.c', line 256
VALUE Thin_HttpParser_init(VALUE self)
{
http_parser *http = NULL;
DATA_GET(self, http_parser, http);
thin_http_parser_init(http);
return self;
}
|
Instance Method Details
#error? ⇒ Boolean
Tells you whether the parser is in an error state.
354 355 356 357 358 359 360 |
# File 'ext/thin_parser/thin.c', line 354
VALUE Thin_HttpParser_has_error(VALUE self)
{
http_parser *http = NULL;
DATA_GET(self, http_parser, http);
return thin_http_parser_has_error(http) ? Qtrue : Qfalse;
}
|
#execute(req_hash, data, start) ⇒ Integer
Takes a Hash and a String of data, parses the String of data filling in the Hash returning an Integer to indicate how much of the data has been read. No matter what the return value, you should call HttpParser#finished? and HttpParser#error? to figure out if it’s done parsing or there was an error.
This function now throws an exception when there is a parsing error. This makes the logic for working with the parser much easier. You can still test for an error, but now you need to wrap the parser with an exception handling block.
The third argument allows for parsing a partial request and then continuing the parsing from that position. It needs all of the original data as well so you have to append to the data buffer as you read.
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'ext/thin_parser/thin.c', line 317
VALUE Thin_HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
{
http_parser *http = NULL;
int from = 0;
char *dptr = NULL;
long dlen = 0;
DATA_GET(self, http_parser, http);
from = FIX2INT(start);
dptr = RSTRING_PTR(data);
dlen = RSTRING_LEN(data);
if(from >= dlen) {
rb_raise(eHttpParserError, "%s", "Requested start is after data buffer end.");
} else {
http->data = (void *)req_hash;
thin_http_parser_execute(http, dptr, dlen, from);
VALIDATE_MAX_LENGTH(http_parser_nread(http), HEADER);
if(thin_http_parser_has_error(http)) {
rb_raise(eHttpParserError, "%s", "Invalid HTTP format, parsing fails.");
} else {
return INT2FIX(http_parser_nread(http));
}
}
}
|
#finish ⇒ Object
Finishes a parser early which could put in a “good” or bad state. You should call reset after finish it or bad things will happen.
290 291 292 293 294 295 296 297 |
# File 'ext/thin_parser/thin.c', line 290
VALUE Thin_HttpParser_finish(VALUE self)
{
http_parser *http = NULL;
DATA_GET(self, http_parser, http);
thin_http_parser_finish(http);
return thin_http_parser_is_finished(http) ? Qtrue : Qfalse;
}
|
#finished? ⇒ Boolean
Tells you whether the parser is finished or not and in a good state.
369 370 371 372 373 374 375 |
# File 'ext/thin_parser/thin.c', line 369
VALUE Thin_HttpParser_is_finished(VALUE self)
{
http_parser *http = NULL;
DATA_GET(self, http_parser, http);
return thin_http_parser_is_finished(http) ? Qtrue : Qfalse;
}
|
#nread ⇒ Integer
Returns the amount of data processed so far during this processing cycle. It is set to 0 on initialize or reset calls and is incremented each time execute is called.
385 386 387 388 389 390 391 |
# File 'ext/thin_parser/thin.c', line 385
VALUE Thin_HttpParser_nread(VALUE self)
{
http_parser *http = NULL;
DATA_GET(self, http_parser, http);
return INT2FIX(http->nread);
}
|
#reset ⇒ nil
Resets the parser to it’s initial state so that you can reuse it rather than making new ones.
273 274 275 276 277 278 279 280 |
# File 'ext/thin_parser/thin.c', line 273
VALUE Thin_HttpParser_reset(VALUE self)
{
http_parser *http = NULL;
DATA_GET(self, http_parser, http);
thin_http_parser_init(http);
return Qnil;
}
|