Class: OverSIP::WebSocket::HttpRequestParser

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

Instance Method Summary collapse

Constructor Details

#newObject

Creates a new parser.



385
386
387
388
389
390
391
392
393
# File 'ext/websocket_http_parser/ws_http_parser_ruby.c', line 385

VALUE HttpRequestParser_init(VALUE self)
{
  TRACE();
  ws_http_request_parser *parser = NULL;
  DATA_GET(self, ws_http_request_parser, parser);
  ws_http_request_parser_init(parser);

  return self;
}

Instance Method Details

#errorString

Returns a String showing the error by enclosing the exact wrong char between }}.

Returns:

  • (String)


486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'ext/websocket_http_parser/ws_http_parser_ruby.c', line 486

VALUE HttpRequestParser_error(VALUE self)
{
  TRACE();
  ws_http_request_parser *parser = NULL;
  DATA_GET(self, ws_http_request_parser, parser);

  if(ws_http_request_parser_has_error(parser)) {
    char *parsing_error_str;
    int parsing_error_str_len;
    int i;
    int j;
    VALUE rb_error_str;

    /* Duplicate error string length so '\r' and '\n' are displayed as CR and LF.
    Let 6 chars more for allocating {{{ and }}}. */
    parsing_error_str = ALLOC_N(char, 2*parser->error_len + 6);

    parsing_error_str_len=0;
    for(i=0, j=0; i < parser->error_len; i++) {
      if (i != parser->error_pos) {
        if (parser->error_start[i] == '\r') {
          parsing_error_str[j++] = '\\';
          parsing_error_str[j++] = 'r';
          parsing_error_str_len += 2;
        }
        else if (parser->error_start[i] == '\n') {
          parsing_error_str[j++] = '\\';
          parsing_error_str[j++] = 'n';
          parsing_error_str_len += 2;
        }
        else {
          parsing_error_str[j++] = parser->error_start[i];
          parsing_error_str_len++;
        }
      }
      else {
        parsing_error_str[j++] = '{';
        parsing_error_str[j++] = '{';
        parsing_error_str[j++] = '{';
        if (parser->error_start[i] == '\r') {
          parsing_error_str[j++] = '\\';
          parsing_error_str[j++] = 'r';
          parsing_error_str_len += 2;
        }
        else if (parser->error_start[i] == '\n') {
          parsing_error_str[j++] = '\\';
          parsing_error_str[j++] = 'n';
          parsing_error_str_len += 2;
        }
        else {
          parsing_error_str[j++] = parser->error_start[i];
          parsing_error_str_len++;
        }
        parsing_error_str[j++] = '}';
        parsing_error_str[j++] = '}';
        parsing_error_str[j++] = '}';
        parsing_error_str_len += 6;
      }
    }

    rb_error_str = rb_str_new(parsing_error_str, parsing_error_str_len);
    xfree(parsing_error_str);
    return rb_error_str;
  }
  else
    return Qnil;
}

#error?Boolean

Tells you whether the parser is in an error state.

Returns:

  • (Boolean)


470
471
472
473
474
475
476
477
# File 'ext/websocket_http_parser/ws_http_parser_ruby.c', line 470

VALUE HttpRequestParser_has_error(VALUE self)
{
  TRACE();
  ws_http_request_parser *parser = NULL;
  DATA_GET(self, ws_http_request_parser, parser);

  return ws_http_request_parser_has_error(parser) ? Qtrue : Qfalse;
}

#execute(req_hash, buffer, start) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'ext/websocket_http_parser/ws_http_parser_ruby.c', line 432

VALUE HttpRequestParser_execute(VALUE self, VALUE req_hash, VALUE buffer, VALUE start)
{
  TRACE();
  ws_http_request_parser *parser = NULL;
  int from = 0;
  char *dptr = NULL;
  long dlen = 0;

  REQUIRE_TYPE(req_hash, T_HASH);
  REQUIRE_TYPE(buffer, T_STRING);
  REQUIRE_TYPE(start, T_FIXNUM);

  DATA_GET(self, ws_http_request_parser, parser);

  from = FIX2INT(start);
  dptr = RSTRING_PTR(buffer);
  dlen = RSTRING_LEN(buffer);

  /* This should never occur or there is an error in the parser. */
  if(from >= dlen)
    rb_raise(eHttpRequestParserError, "requested start is after buffer end.");

  parser->data = (void *)req_hash;
  ws_http_request_parser_execute(parser, dptr, dlen, from);

  if(ws_http_request_parser_has_error(parser))
    return Qfalse;
  else
    return INT2FIX(ws_http_request_parser_nread(parser));
}

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



421
422
423
424
425
426
427
428
429
# File 'ext/websocket_http_parser/ws_http_parser_ruby.c', line 421

VALUE HttpRequestParser_finish(VALUE self)
{
  TRACE();
  ws_http_request_parser *parser = NULL;
  DATA_GET(self, ws_http_request_parser, parser);
  ws_http_request_parser_finish(parser);

  return ws_http_request_parser_is_finished(parser) ? Qtrue : Qfalse;
}

#finished?Boolean

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

Returns:

  • (Boolean)


561
562
563
564
565
566
567
568
# File 'ext/websocket_http_parser/ws_http_parser_ruby.c', line 561

VALUE HttpRequestParser_is_finished(VALUE self)
{
  TRACE();
  ws_http_request_parser *parser = NULL;
  DATA_GET(self, ws_http_request_parser, parser);

  return ws_http_request_parser_is_finished(parser) ? 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)


578
579
580
581
582
583
584
585
# File 'ext/websocket_http_parser/ws_http_parser_ruby.c', line 578

VALUE HttpRequestParser_nread(VALUE self)
{
  TRACE();
  ws_http_request_parser *parser = NULL;
  DATA_GET(self, ws_http_request_parser, parser);

  return INT2FIX(parser->nread);
}

#resetnil

Resets the parser to it’s initial state so that you can reuse it rather than making new ones.

Returns:

  • (nil)


403
404
405
406
407
408
409
410
411
# File 'ext/websocket_http_parser/ws_http_parser_ruby.c', line 403

VALUE HttpRequestParser_reset(VALUE self)
{
  TRACE();
  ws_http_request_parser *parser = NULL;
  DATA_GET(self, ws_http_request_parser, parser);
  ws_http_request_parser_init(parser);

  return Qnil;
}