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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 321

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

  wrapper->header_value_type = rb_iv_get(CLASS_OF(self), "@default_header_value_type");

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

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

  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

Instance Method Details

#<<(data) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 339

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) {
    rb_str_cat(wrapper->upgrade_data, ptr + nparsed + 1, len - nparsed - 1);

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

  return INT2FIX(nparsed);
}

#fragmentObject

#header_value_typeObject

#header_value_type=(val) ⇒ Object



475
476
477
478
479
480
481
482
483
484
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 475

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



420
421
422
423
424
425
426
427
428
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 420

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



440
441
442
443
444
445
446
447
448
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 440

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



430
431
432
433
434
435
436
437
438
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 430

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



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

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)


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

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



380
381
382
383
384
385
386
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 380

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



372
373
374
375
376
377
378
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 372

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



364
365
366
367
368
369
370
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 364

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



388
389
390
391
392
393
394
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 388

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;
}

#query_stringObject

#request_pathObject

#request_urlObject

#reset!Object



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

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

  ParserWrapper_init(wrapper);

  return Qtrue;
}

#status_codeObject



450
451
452
453
454
455
456
457
458
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 450

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)


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

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

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

#upgrade_dataObject