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.



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

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)


296
297
298
299
300
301
302
# File 'ext/http11/http11.c', line 296

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

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)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'ext/http11/http11.c', line 259

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(data)->ptr;
  dlen = RSTRING(data)->len;
  
  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));
    }
  }
}

#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.



232
233
234
235
236
237
238
239
# File 'ext/http11/http11.c', line 232

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)


311
312
313
314
315
316
317
# File 'ext/http11/http11.c', line 311

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)


327
328
329
330
331
332
333
# File 'ext/http11/http11.c', line 327

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)


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

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