# class Unicorn::HttpParser

### Constants

CHUNK_MAX

  The maximum size a single chunk when using chunked transfer encoding. This is only a theoretical maximum used to detect errors in clients, it is highly unlikely to encounter clients that send more than several kilobytes at once.

LENGTH_MAX

  The maximum size of the body as specified by Content-Length. This is only a theoretical maximum, the actual limit is subject to the limits of the file system used for `Dir.tmpdir`.

### Public Class Methods

max_header_len=(p1) click to toggle source

this is only intended for use with Rainbows!

    static VALUE set_maxhdrlen(VALUE self, VALUE len)
    {
      return UINT2NUM(MAX_HEADER_LEN = NUM2UINT(len));
    }

new → parser click to toggle source

Creates a new parser.

    static VALUE HttpParser_init(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      http_parser_init(hp);
      hp->buf = rb_str_new(NULL, 0);
      hp->env = rb_hash_new();

      return self;
    }

### Public Instance Methods

add_parse(buffer) → env or nil click to toggle source

adds the contents of `buffer` to the internal buffer and attempts to continue parsing. Returns the `env` Hash on success or nil if more data is needed.

Raises `HttpParserError` if there are parsing errors.

    static VALUE HttpParser_add_parse(VALUE self, VALUE buffer)
    {
      struct http_parser *hp = data_get(self);

      Check_Type(buffer, T_STRING);
      rb_str_buf_append(hp->buf, buffer);

      return HttpParser_parse(self);
    }

body_eof? → true or false click to toggle source

Detects if we’re done filtering the body or not. This can be used to detect when to stop calling `HttpParser#filter_body`.

    static VALUE HttpParser_body_eof(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      if (HP_FL_TEST(hp, CHUNKED))
        return chunked_eof(hp) ? Qtrue : Qfalse;

      return hp->len.content == 0 ? Qtrue : Qfalse;
    }

buf() click to toggle source

    static VALUE HttpParser_buf(VALUE self)
    {
      return data_get(self)->buf;
    }

clear → parser click to toggle source

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

    static VALUE HttpParser_clear(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      /* we can't safely reuse .buf and .env if hijacked */
      if (HP_FL_TEST(hp, HIJACK))
        return HttpParser_init(self);

      http_parser_init(hp);
      rb_hash_clear(hp->env);

      return self;
    }

content_length → nil or Integer click to toggle source

Returns the number of bytes left to run through `HttpParser#filter_body`. This will initially be the value of the “Content-Length” HTTP header after header parsing is complete and will decrease in value as `HttpParser#filter_body` is called for each chunk. This should return zero for requests with no body.

This will return nil on “Transfer-Encoding: chunked” requests.

    static VALUE HttpParser_content_length(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      return HP_FL_TEST(hp, CHUNKED) ? Qnil : OFFT2NUM(hp->len.content);
    }

env() click to toggle source

    static VALUE HttpParser_env(VALUE self)
    {
      return data_get(self)->env;
    }

filter_body(dst, src) → nil/src click to toggle source

Takes a String of `src`, will modify data if dechunking is done. Returns `nil` if there is more data left to process. Returns `src` if body processing is complete. When returning `src`, it may modify `src` so the start of the string points to where the body ended so that trailer processing can begin.

Raises `HttpParserError` if there are dechunking errors. Basically this is a glorified memcpy(3) that copies `src` into `buf` while filtering it through the dechunker.

    static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src)
    {
      struct http_parser *hp = data_get(self);
      char *srcptr;
      long srclen;

      srcptr = RSTRING_PTR(src);
      srclen = RSTRING_LEN(src);

      StringValue(dst);

      if (HP_FL_TEST(hp, CHUNKED)) {
        if (!chunked_eof(hp)) {
          rb_str_modify(dst);
          rb_str_resize(dst, srclen); /* we can never copy more than srclen bytes */

          hp->s.dest_offset = 0;
          hp->cont = dst;
          hp->buf = src;
          http_parser_execute(hp, srcptr, srclen);
          if (hp->cs == http_parser_error)
            parser_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");

          assert(hp->s.dest_offset <= hp->offset &&
                 "destination buffer overflow");
          advance_str(src, hp->offset);
          rb_str_set_len(dst, hp->s.dest_offset);

          if (RSTRING_LEN(dst) == 0 && chunked_eof(hp)) {
            assert(hp->len.chunk == 0 && "chunk at EOF but more to parse");
          } else {
            src = Qnil;
          }
        }
      } else {
        /* no need to enter the Ragel machine for unchunked transfers */
        assert(hp->len.content >= 0 && "negative Content-Length");
        if (hp->len.content > 0) {
          long nr = MIN(srclen, hp->len.content);

          rb_str_modify(dst);
          rb_str_resize(dst, nr);
          /*
           * using rb_str_replace() to avoid memcpy() doesn't help in
           * most cases because a GC-aware programmer will pass an explicit
           * buffer to env["rack.input"].read and reuse the buffer in a loop.
           * This causes copy-on-write behavior to be triggered anyways
           * when the +src+ buffer is modified (when reading off the socket).
           */
          hp->buf = src;
          memcpy(RSTRING_PTR(dst), srcptr, nr);
          hp->len.content -= nr;
          if (hp->len.content == 0) {
            HP_FL_SET(hp, REQEOF);
            hp->cs = http_parser_first_final;
          }
          advance_str(src, nr);
          src = Qnil;
        }
      }
      hp->offset = 0; /* for trailer parsing */
      return src;
    }

headers(p1, p2) click to toggle source

    static VALUE HttpParser_headers(VALUE self, VALUE env, VALUE buf)
    {
      struct http_parser *hp = data_get(self);

      hp->env = env;
      hp->buf = buf;

      return HttpParser_parse(self);
    }

Also aliased as: trailers
headers? → true or false click to toggle source

This should be used to detect if a request has headers (and if the response will have headers as well). HTTP/0.9 requests should return false, all subsequent HTTP versions will return true

    static VALUE HttpParser_has_headers(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      return HP_FL_TEST(hp, HASHEADER) ? Qtrue : Qfalse;
    }

hijacked!() click to toggle source

    static VALUE HttpParser_hijacked_bang(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      HP_FL_SET(hp, HIJACK);

      return self;
    }

keepalive? → true or false click to toggle source

This should be used to detect if a request can really handle keepalives and pipelining. Currently, the rules are:

1. MUST be a GET or HEAD request

2. MUST be HTTP/1.1 `or` HTTP/1.0 with “Connection: keep-alive”

3. MUST NOT have “Connection: close” set

    static VALUE HttpParser_keepalive(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      return HP_FL_ALL(hp, KEEPALIVE) ? Qtrue : Qfalse;
    }

next? → true or false click to toggle source

Exactly like `HttpParser#keepalive?`, except it will reset the internal parser state on next parse if it returns true.

    static VALUE HttpParser_next(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      if (HP_FL_ALL(hp, KEEPALIVE)) {
        HP_FL_SET(hp, TO_CLEAR);
        return Qtrue;
      }
      return Qfalse;
    }

parse → env or nil click to toggle source

Takes a Hash and a String of data, parses the String of data filling in the Hash returning the Hash if parsing is finished, nil otherwise When returning the env Hash, it may modify data to point to where body processing should begin.

Raises `HttpParserError` if there are parsing errors.

    static VALUE HttpParser_parse(VALUE self)
    {
      struct http_parser *hp = data_get(self);
      VALUE data = hp->buf;

      if (HP_FL_TEST(hp, TO_CLEAR))
        HttpParser_clear(self);

      http_parser_execute(hp, RSTRING_PTR(data), RSTRING_LEN(data));
      if (hp->offset > MAX_HEADER_LEN)
        parser_raise(e413, "HTTP header is too large");

      if (hp->cs == http_parser_first_final ||
          hp->cs == http_parser_en_ChunkedBody) {
        advance_str(data, hp->offset + 1);
        hp->offset = 0;
        if (HP_FL_TEST(hp, INTRAILER))
          HP_FL_SET(hp, REQEOF);

        return hp->env;
      }

      if (hp->cs == http_parser_error)
        parser_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");

      return Qnil;
    }

response_start_sent() click to toggle source

    static VALUE HttpParser_rssget(VALUE self)
    {
      struct http_parser *hp = data_get(self);

      return HP_FL_TEST(hp, RESSTART) ? Qtrue : Qfalse;
    }

response_start_sent=(p1) click to toggle source

    static VALUE HttpParser_rssset(VALUE self, VALUE boolean)
    {
      struct http_parser *hp = data_get(self);

      if (RTEST(boolean))
        HP_FL_SET(hp, RESSTART);
      else
        HP_FL_UNSET(hp, RESSTART);

      return boolean; /* ignored by Ruby anyways */
    }

trailers(p1, p2)

This is an alias for `HttpParser#headers`

Alias for: headers