Class: HTTP::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/http_parser.rb,
ext/ruby_http_parser/ruby_http_parser.c

Direct Known Subclasses

RequestParser, ResponseParser

Defined Under Namespace

Classes: Error

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 315

VALUE Parser_initialize(int argc, VALUE *argv, VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  VALUE default_header_value_type = Qnil;

  if (argc > 0 && RB_TYPE_P(argv[argc-1], T_HASH)) {
    ID keyword_ids[1];
    keyword_ids[0] = rb_intern("default_header_value_type");
    rb_get_kwargs(argv[argc-1], keyword_ids, 0, 1, &default_header_value_type);
    if (default_header_value_type == Qundef) {
      default_header_value_type = Qnil;
    }
    --argc;
  }

  if (argc == 1) {
    wrapper->callback_object = argv[0];
  }

  if (argc == 2) {
    wrapper->callback_object = argv[0];
    default_header_value_type = argv[1];
  }

  if (default_header_value_type == Qnil) {
    wrapper->header_value_type = rb_iv_get(CLASS_OF(self), "@default_header_value_type");
  } else {
    wrapper->header_value_type = default_header_value_type;
  }

  return self;
}

Class Attribute Details

.default_header_value_typeObject

Returns the value of attribute default_header_value_type.



9
10
11
# File 'lib/http_parser.rb', line 9

def default_header_value_type
  @default_header_value_type
end

Class Method Details

.strict?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 311

VALUE Parser_strict_p(VALUE klass) {
  return HTTP_PARSER_STRICT == 1 ? Qtrue : Qfalse;
}

Instance Method Details

#<<(data) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 349

VALUE Parser_execute(VALUE self, VALUE data) {
  ParserWrapper *wrapper = NULL;

  Check_Type(data, T_STRING);
  char *ptr = RSTRING_PTR(data);
  long len = RSTRING_LEN(data);

  DATA_GET(self, ParserWrapper, wrapper);

  wrapper->stopped = Qfalse;
  size_t nparsed = ryah_http_parser_execute(&wrapper->parser, &settings, ptr, len);

  if (wrapper->parser.upgrade) {
    if (RTEST(wrapper->stopped) && !RTEST(wrapper->completed))
      nparsed += 1;

    if (nparsed < len)
      rb_str_cat(wrapper->upgrade_data, ptr + nparsed, len - nparsed);
    
  } else if (nparsed != (size_t)len) {
    if (!RTEST(wrapper->stopped) && !RTEST(wrapper->completed))
      rb_raise(eParserError, "Could not parse data entirely (%zu != %zu)", nparsed, len);
    else
      nparsed += 1; // error states fail on the current character
  }

  return INT2FIX(nparsed);
}

#header_value_typeObject

#header_value_type=(val) ⇒ Object



487
488
489
490
491
492
493
494
495
496
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 487

VALUE Parser_set_header_value_type(VALUE self, VALUE val) {
  if (val != Sarrays && val != Sstrings && val != Smixed) {
    rb_raise(rb_eArgError, "Invalid header value type");
  }

  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);
  wrapper->header_value_type = val;
  return wrapper->header_value_type;
}

#headersObject

#http_majorObject



434
435
436
437
438
439
440
441
442
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 434

VALUE Parser_http_major(VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  if (wrapper->parser.http_major == 0 && wrapper->parser.http_minor == 0)
    return Qnil;
  else
    return INT2FIX(wrapper->parser.http_major);
}

#http_methodObject



454
455
456
457
458
459
460
461
462
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 454

VALUE Parser_http_method(VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  if (wrapper->parser.type == HTTP_REQUEST)
    return rb_str_new2(http_method_str(wrapper->parser.method));
  else
    return Qnil;
}

#http_minorObject



444
445
446
447
448
449
450
451
452
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 444

VALUE Parser_http_minor(VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  if (wrapper->parser.http_major == 0 && wrapper->parser.http_minor == 0)
    return Qnil;
  else
    return INT2FIX(wrapper->parser.http_minor);
}

#http_versionObject



424
425
426
427
428
429
430
431
432
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 424

VALUE Parser_http_version(VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  if (wrapper->parser.http_major == 0 && wrapper->parser.http_minor == 0)
    return Qnil;
  else
    return rb_ary_new3(2, INT2FIX(wrapper->parser.http_major), INT2FIX(wrapper->parser.http_minor));
}

#keep_alive?Boolean

Returns:

  • (Boolean)


410
411
412
413
414
415
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 410

VALUE Parser_keep_alive_p(VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  return http_should_keep_alive(&wrapper->parser) == 1 ? Qtrue : Qfalse;
}

#on_body=(callback) ⇒ Object



394
395
396
397
398
399
400
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 394

VALUE Parser_set_on_body(VALUE self, VALUE callback) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  wrapper->on_body = callback;
  return callback;
}

#on_headers_complete=(callback) ⇒ Object



386
387
388
389
390
391
392
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 386

VALUE Parser_set_on_headers_complete(VALUE self, VALUE callback) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  wrapper->on_headers_complete = callback;
  return callback;
}

#on_message_begin=(callback) ⇒ Object



378
379
380
381
382
383
384
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 378

VALUE Parser_set_on_message_begin(VALUE self, VALUE callback) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  wrapper->on_message_begin = callback;
  return callback;
}

#on_message_complete=(callback) ⇒ Object



402
403
404
405
406
407
408
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 402

VALUE Parser_set_on_message_complete(VALUE self, VALUE callback) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  wrapper->on_message_complete = callback;
  return callback;
}

#request_urlObject

#reset!Object



498
499
500
501
502
503
504
505
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 498

VALUE Parser_reset(VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  ParserWrapper_init(wrapper);

  return Qtrue;
}

#statusObject

#status_codeObject



464
465
466
467
468
469
470
471
472
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 464

VALUE Parser_status_code(VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  if (wrapper->parser.status_code)
    return INT2FIX(wrapper->parser.status_code);
  else
    return Qnil;
}

#upgrade?Boolean

Returns:

  • (Boolean)


417
418
419
420
421
422
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 417

VALUE Parser_upgrade_p(VALUE self) {
  ParserWrapper *wrapper = NULL;
  DATA_GET(self, ParserWrapper, wrapper);

  return wrapper->parser.upgrade ? Qtrue : Qfalse;
}

#upgrade_dataObject