Class: Mongrel::HttpParser

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

Instance Method Summary collapse

Constructor Details

#newObject

Creates a new parser.



106
107
108
109
110
111
112
113
# File 'ext/http11/http11.c', line 106

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

Instance Method Details

#error?Boolean

Tells you whether the parser is in an error state.

Returns:

  • (Boolean)


185
186
187
188
189
190
191
# File 'ext/http11/http11.c', line 185

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) ⇒ 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.

Returns:

  • (Integer)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'ext/http11/http11.c', line 163

VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data)
{
  http_parser *http = NULL;
  DATA_GET(self, http_parser, http);

  http->data = (void *)req_hash;
  http_parser_execute(http, RSTRING(data)->ptr, RSTRING(data)->len);

  if(http_parser_has_error(http)) {
    rb_raise(rb_eStandardError, "HTTP Parsing failure");
  } else {
    return INT2FIX(http_parser_nread(http));
  }
}

#finishObject

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.



140
141
142
143
144
145
146
147
# File 'ext/http11/http11.c', line 140

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?Boolean

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

Returns:

  • (Boolean)


200
201
202
203
204
205
206
# File 'ext/http11/http11.c', line 200

VALUE HttpParser_is_finished(VALUE self)
{
  http_parser *http = NULL;
  DATA_GET(self, http_parser, http);
  
  return http_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.

Returns:

  • (Integer)


216
217
218
219
220
221
222
# File 'ext/http11/http11.c', line 216

VALUE HttpParser_nread(VALUE self)
{
  http_parser *http = NULL;
  DATA_GET(self, http_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.

Returns:

  • (nil)


123
124
125
126
127
128
129
130
# File 'ext/http11/http11.c', line 123

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