Class: Puma::HttpParser

Inherits:
Object
  • Object
show all
Defined in:
ext/puma_http11/puma_http11.c

Instance Method Summary collapse

Constructor Details

#new ⇒ Object

Creates a new parser.



326
327
328
329
330
331
332
# File 'ext/puma_http11/puma_http11.c', line 326

static VALUE HttpParser_init(VALUE self)
{
  puma_parser *http = HttpParser_unwrap(self);
  puma_parser_init(http);

  return self;
}

Instance Method Details

#body ⇒ nil, String

If the request included a body, returns it.

Returns:

  • (nil, String)


461
462
463
464
465
# File 'ext/puma_http11/puma_http11.c', line 461

static VALUE HttpParser_body(VALUE self) {
  puma_parser *http = HttpParser_unwrap(self);

  return http->body;
}

#error? ⇒ Boolean

Tells you whether the parser is in an error state.

Returns:

  • (Boolean)


419
420
421
422
423
424
# File 'ext/puma_http11/puma_http11.c', line 419

static VALUE HttpParser_has_error(VALUE self)
{
  puma_parser *http = HttpParser_unwrap(self);

  return puma_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.

Returns:

  • (Integer)


384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'ext/puma_http11/puma_http11.c', line 384

static VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
{
  puma_parser *http = HttpParser_unwrap(self);
  int from = 0;
  char *dptr = NULL;
  long dlen = 0;

  from = FIX2INT(start);
  dptr = RSTRING_PTR(data);
  dlen = (long)RSTRING_LEN(data);

  if(from >= dlen) {
    rb_raise(eHttpParserError, "%s", "Requested start is after data buffer end.");
  } else {
    http->request = req_hash;
    puma_parser_execute(http, dptr, dlen, from);

    VALIDATE_MAX_LENGTH(puma_parser_nread(http), HEADER);

    if(puma_parser_has_error(http)) {
      rb_raise(eHttpParserError, "%s", "Invalid HTTP format, parsing fails. Are you trying to open an SSL connection to a non-SSL Puma?");
    } else {
      return SIZET2NUM(puma_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.



358
359
360
361
362
363
364
# File 'ext/puma_http11/puma_http11.c', line 358

static VALUE HttpParser_finish(VALUE self)
{
  puma_parser *http = HttpParser_unwrap(self);
  puma_parser_finish(http);

  return puma_parser_is_finished(http) ? Qtrue : Qfalse;
}

#finished? ⇒ Boolean

Tells you whether the parser is finished or not and in a good state.

Returns:

  • (Boolean)


433
434
435
436
437
438
# File 'ext/puma_http11/puma_http11.c', line 433

static VALUE HttpParser_is_finished(VALUE self)
{
  puma_parser *http = HttpParser_unwrap(self);

  return puma_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.

Returns:

  • (Integer)


448
449
450
451
452
453
# File 'ext/puma_http11/puma_http11.c', line 448

static VALUE HttpParser_nread(VALUE self)
{
  puma_parser *http = HttpParser_unwrap(self);

  return SIZET2NUM(http->nread);
}

#reset ⇒ nil

Resets the parser to it's initial state so that you can reuse it rather than making new ones.

Returns:

  • (nil)


342
343
344
345
346
347
348
# File 'ext/puma_http11/puma_http11.c', line 342

static VALUE HttpParser_reset(VALUE self)
{
  puma_parser *http = HttpParser_unwrap(self);
  puma_parser_init(http);

  return Qnil;
}