123456789_123456789_123456789_123456789_123456789_

Class: Puma::HttpParser

Relationships & Source Files
Inherits: Object
Defined in: ext/puma_http11/puma_http11.c

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newparser

Creates a new parser.

[ GitHub ]

  
# File 'ext/puma_http11/puma_http11.c', line 320

VALUE HttpParser_init(VALUE self)
{
  puma_parser *http = NULL;
  DATA_GET(self, puma_parser, http);
  puma_parser_init(http);

  return self;
}

Instance Attribute Details

#error?Boolean (readonly)

Tells you whether the parser is in an error state.

[ GitHub ]

  
# File 'ext/puma_http11/puma_http11.c', line 419

VALUE HttpParser_has_error(VALUE self)
{
  puma_parser *http = NULL;
  DATA_GET(self, puma_parser, http);

  return puma_parser_has_error(http) ? Qtrue : Qfalse;
}

#finished?Boolean (readonly)

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

[ GitHub ]

  
# File 'ext/puma_http11/puma_http11.c', line 434

VALUE HttpParser_is_finished(VALUE self)
{
  puma_parser *http = NULL;
  DATA_GET(self, puma_parser, http);

  return puma_parser_is_finished(http) ? Qtrue : Qfalse;
}

Instance Method Details

#bodynil, String

If the request included a body, returns it.

[ GitHub ]

  
# File 'ext/puma_http11/puma_http11.c', line 464

VALUE HttpParser_body(VALUE self) {
  puma_parser *http = NULL;
  DATA_GET(self, puma_parser, http);

  return http->body;
}

#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 #finished? and #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.

[ GitHub ]

  
# File 'ext/puma_http11/puma_http11.c', line 381

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

  DATA_GET(self, puma_parser, http);

  from = FIX2INT(start);
  dptr = rb_extract_chars(data, &dlen);

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

    rb_free_chars(dptr);
    VALIDATE_MAX_LENGTH(puma_parser_nread(http), HEADER);

    if(puma_parser_has_error(http)) {
      rb_raise(eHttpParserError, "%s", "Invalid HTTP format, parsing fails.");
    } else {
      return INT2FIX(puma_parser_nread(http));
    }
  }
}

#finishBoolean

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.

[ GitHub ]

  
# File 'ext/puma_http11/puma_http11.c', line 354

VALUE HttpParser_finish(VALUE self)
{
  puma_parser *http = NULL;
  DATA_GET(self, puma_parser, http);
  puma_parser_finish(http);

  return puma_parser_is_finished(http) ? Qtrue : Qfalse;
}

#nreadInteger

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.

[ GitHub ]

  
# File 'ext/puma_http11/puma_http11.c', line 450

VALUE HttpParser_nread(VALUE self)
{
  puma_parser *http = NULL;
  DATA_GET(self, puma_parser, http);

  return INT2FIX(http->nread);
}

#resetnil

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

[ GitHub ]

  
# File 'ext/puma_http11/puma_http11.c', line 337

VALUE HttpParser_reset(VALUE self)
{
  puma_parser *http = NULL;
  DATA_GET(self, puma_parser, http);
  puma_parser_init(http);

  return Qnil;
}