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



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

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



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

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 != 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



472
473
474
475
476
477
478
479
480
481
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 472

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



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

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



437
438
439
440
441
442
443
444
445
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 437

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



427
428
429
430
431
432
433
434
435
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 427

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



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

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)


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

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



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

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



369
370
371
372
373
374
375
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 369

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



361
362
363
364
365
366
367
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 361

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



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

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



483
484
485
486
487
488
489
490
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 483

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

  ParserWrapper_init(wrapper);

  return Qtrue;
}

#status_codeObject



447
448
449
450
451
452
453
454
455
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 447

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)


400
401
402
403
404
405
# File 'ext/ruby_http_parser/ruby_http_parser.c', line 400

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

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

#upgrade_dataObject