Class: RFuzz::HttpClientParser

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

Instance Method Summary collapse

Constructor Details

#newObject

Creates a new parser.



133
134
135
136
137
138
139
140
# File 'ext/http11_client/http11_client.c', line 133

VALUE HttpClientParser_init(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);
  httpclient_parser_init(http);

  return self;
}

Instance Method Details

#error?Boolean

Tells you whether the parser is in an error state.

Returns:

  • (Boolean)


229
230
231
232
233
234
235
# File 'ext/http11_client/http11_client.c', line 229

VALUE HttpClientParser_has_error(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);

  return httpclient_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 HttpClientParser#finished? and HttpClientParser#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)


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'ext/http11_client/http11_client.c', line 194

VALUE HttpClientParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
{
  httpclient_parser *http = NULL;
  int from = 0;
  char *dptr = NULL;
  long dlen = 0;

  DATA_GET(self, httpclient_parser, http);

  from = FIX2INT(start);
  dptr = RSTRING(data)->ptr;
  dlen = RSTRING(data)->len;

  if(from >= dlen) {
    rb_raise(eHttpClientParserError, "Requested start is after data buffer end.");
  } else {
    http->data = (void *)req_hash;
    httpclient_parser_execute(http, dptr, dlen, from);

    if(httpclient_parser_has_error(http)) {
      rb_raise(eHttpClientParserError, "Invalid HTTP format, parsing fails.");
    } else {
      return INT2FIX(httpclient_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.



167
168
169
170
171
172
173
174
# File 'ext/http11_client/http11_client.c', line 167

VALUE HttpClientParser_finish(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);
  httpclient_parser_finish(http);

  return httpclient_parser_is_finished(http) ? Qtrue : Qfalse;
}

#finished?Boolean

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

Returns:

  • (Boolean)


244
245
246
247
248
249
250
# File 'ext/http11_client/http11_client.c', line 244

VALUE HttpClientParser_is_finished(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);

  return httpclient_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)


260
261
262
263
264
265
266
# File 'ext/http11_client/http11_client.c', line 260

VALUE HttpClientParser_nread(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_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)


150
151
152
153
154
155
156
157
# File 'ext/http11_client/http11_client.c', line 150

VALUE HttpClientParser_reset(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);
  httpclient_parser_init(http);

  return Qnil;
}