Class: Mongrel::HttpParser

### Home Classes Methods

### In Files

- ext/http11/http11.c

### Parent

Object

### Methods

- ::new
- #error?
- #execute
- #finish
- #finished?
- #nread
- #reset

### Files

- CHANGELOG
- COPYING
- LICENSE
- README
- Makefile

### Class/Module Index

Quicksearch

- Mongrel
- Mongrel::CGIWrapper
- Mongrel::Camping
- Mongrel::Camping::CampingHandler
- Mongrel::Command
- Mongrel::Command::Base
- Mongrel::Command::Registry
- Mongrel::Configurator
- Mongrel::Const
- Mongrel::DeflateFilter
- Mongrel::DirHandler
- Mongrel::Error404Handler
- Mongrel::Gems
- Mongrel::HeaderOut
- Mongrel::HttpHandler
- Mongrel::HttpHandlerPlugin
- Mongrel::HttpParams
- Mongrel::HttpParser
- Mongrel::HttpParserError
- Mongrel::HttpRequest
- Mongrel::HttpResponse
- Mongrel::HttpServer
- Mongrel::Rails
- Mongrel::Rails::RailsConfigurator
- Mongrel::Rails::RailsHandler
- Mongrel::RedirectHandler
- Mongrel::StatisticsFilter
- Mongrel::Stats
- Mongrel::StatusHandler
- Mongrel::StopServer
- Mongrel::TimeoutError
- Mongrel::URIClassifier
- Mongrel::URIClassifier::RegistrationError
- Mongrel::URIClassifier::UsageError
- RequestLog
- RequestLog::Access
- RequestLog::Files
- RequestLog::Objects
- RequestLog::Params
- RequestLog::Threads
- IO
- Kernel
- MongrelDbg
- TCPServer

No matching classes.

# Mongrel::HttpParser

### Public Class Methods

new → parser click to toggle source

Creates a new parser.

    VALUE HttpParser_init(VALUE self)
    {
      http_parser *http = NULL;
      DATA_GET(self, http_parser, http);
      http_parser_init(http);

      return self;
    }

### Public Instance Methods

error? → true/false click to toggle source

Tells you whether the parser is in an error state.

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

      return http_parser_has_error(http) ? Qtrue : Qfalse;
    }

execute(req_hash, data, start) → Integer click to toggle source

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.

    VALUE 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, "Requested start is after data buffer end.");
      } else {
        http->data = (void *)req_hash;
        http_parser_execute(http, dptr, dlen, from);

        VALIDATE_MAX_LENGTH(http_parser_nread(http), HEADER);

        if(http_parser_has_error(http)) {
          rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
        } else {
          return INT2FIX(http_parser_nread(http));
        }
      }
    }

finish → true/false click to toggle source

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.

    VALUE HttpParser_finish(VALUE self)
    {
      http_parser *http = NULL;
      DATA_GET(self, http_parser, http);
      http_parser_finish(http);

      return http_parser_is_finished(http) ? Qtrue : Qfalse;
    }

finished? → true/false click to toggle source

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

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

      return http_parser_is_finished(http) ? Qtrue : Qfalse;
    }

nread → Integer click to toggle source

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.

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

      return INT2FIX(http->nread);
    }

reset → nil click to toggle source

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

    VALUE HttpParser_reset(VALUE self)
    {
      http_parser *http = NULL;
      DATA_GET(self, http_parser, http);
      http_parser_init(http);

      return Qnil;
    }

[Validate]

Generated with the Darkfish Rdoc Generator 2.