File: http11_client.c

package info (click to toggle)
ruby-em-http-request 0.3.0-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 424 kB
  • sloc: ruby: 2,381; ansic: 999; makefile: 2
file content (328 lines) | stat: -rw-r--r-- 8,106 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
 * Copyright (c) 2005 Zed A. Shaw
 * You can redistribute it and/or modify it under the same terms as Ruby.
 */

#include "ruby.h"
#include "ext_help.h"
#include <assert.h>
#include <string.h>
#include "http11_parser.h"
#include <ctype.h>

static VALUE mEm;
static VALUE cHttpClientParser;
static VALUE eHttpClientParserError;

#define  id_reason rb_intern("@http_reason")
#define  id_status rb_intern("@http_status")
#define  id_version rb_intern("@http_version")
#define  id_body rb_intern("@http_body")
#define  id_chunk_size rb_intern("@http_chunk_size")
#define  id_last_chunk rb_intern("@last_chunk")

#ifndef RHASH_TBL
/* rb_hash_lookup() is only in Ruby 1.8.7 */
static VALUE rb_hash_lookup(VALUE hash, VALUE key)
{
  VALUE val;

  if (!st_lookup(RHASH(hash)->tbl, key, &val)) {
    return Qnil; /* without Hash#default */
  }

  return val;
}
#endif

void client_http_field(void *data, const char *field, size_t flen, const char *value, size_t vlen)
{
  char *ch, *end;
  VALUE req = (VALUE)data;
  VALUE v = Qnil;
  VALUE f = Qnil;
  VALUE el = Qnil;

  v = rb_str_new(value, vlen);
  f = rb_str_new(field, flen);

  /* Yes Children, rb_str_upcase_bang isn't even available as an intern.h function.
   * how incredibly handy to not have that.  Nope, I have to do it by hand.*/
  for(ch = RSTRING_PTR(f), end = ch + RSTRING_LEN(f); ch < end; ch++) {
    if(*ch == '-') {
      *ch = '_';
    } else {
      *ch = toupper(*ch);
    }
  }

  el = rb_hash_lookup(req, f);
  switch(TYPE(el)) {
    case T_ARRAY:
      rb_ary_push(el, v);
      break;
    case T_STRING:
      rb_hash_aset(req, f, rb_ary_new3(2, el, v));
      break;
    default:
      rb_hash_aset(req, f, v);
      break;
  }
}

void client_reason_phrase(void *data, const char *at, size_t length)
{
  VALUE req = (VALUE)data;
  VALUE v = Qnil;

  v = rb_str_new(at, length);

  rb_ivar_set(req, id_reason, v);
}

void client_status_code(void *data, const char *at, size_t length)
{
  VALUE req = (VALUE)data;
  VALUE v = Qnil;

  v = rb_str_new(at, length);

  rb_ivar_set(req, id_status, v);
}

void client_http_version(void *data, const char *at, size_t length)
{
  VALUE req = (VALUE)data;
  VALUE v = Qnil;

  v = rb_str_new(at, length);

  rb_ivar_set(req, id_version, v);
}

/** Finalizes the request header to have a bunch of stuff that's
  needed. */
void client_header_done(void *data, const char *at, size_t length)
{
  VALUE req = (VALUE)data;
  VALUE v = Qnil;

  v = rb_str_new(at, length);
  rb_ivar_set(req, id_body, v);
}

void client_chunk_size(void *data, const char *at, size_t length)
{
  VALUE req = (VALUE)data;
  VALUE v = Qnil;

  if(length <= 0) {
    rb_raise(eHttpClientParserError, "Chunked Encoding gave <= 0 chunk size.");
  }

  v = rb_str_new(at, length);

  rb_ivar_set(req, id_chunk_size, v);
}

void client_last_chunk(void *data, const char *at, size_t length) {
  VALUE req = (VALUE)data;
  rb_ivar_set(req, id_last_chunk,Qtrue);
}


void HttpClientParser_free(void *data) {
  TRACE();

  if(data) {
    free(data);
  }
}


VALUE HttpClientParser_alloc(VALUE klass)
{
  VALUE obj;
  httpclient_parser *hp = ALLOC_N(httpclient_parser, 1);
  TRACE();
  hp->http_field = client_http_field;
  hp->status_code = client_status_code;
  hp->reason_phrase = client_reason_phrase;
  hp->http_version = client_http_version;
  hp->header_done = client_header_done;
  hp->chunk_size = client_chunk_size;
  hp->last_chunk = client_last_chunk;
  httpclient_parser_init(hp);

  obj = Data_Wrap_Struct(klass, NULL, HttpClientParser_free, hp);

  return obj;
}


/**
 * call-seq:
 *    parser.new -> parser
 *
 * Creates a new parser.
 */
VALUE HttpClientParser_init(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);
  httpclient_parser_init(http);

  return self;
}


/**
 * call-seq:
 *    parser.reset -> nil
 *
 * Resets the parser to it's initial state so that you can reuse it
 * rather than making new ones.
 */
VALUE HttpClientParser_reset(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);
  httpclient_parser_init(http);

  return Qnil;
}


/**
 * call-seq:
 *    parser.finish -> true/false
 *
 * 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.
 */
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;
}


/**
 * call-seq:
 *    parser.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.
 */
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;

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

  DATA_GET(self, httpclient_parser, http);

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

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



/**
 * call-seq:
 *    parser.error? -> true/false
 *
 * Tells you whether the parser is in an error state.
 */
VALUE HttpClientParser_has_error(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);

  return httpclient_parser_has_error(http) ? Qtrue : Qfalse;
}


/**
 * call-seq:
 *    parser.finished? -> true/false
 *
 * Tells you whether the parser is finished or not and in a good state.
 */
VALUE HttpClientParser_is_finished(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);

  return httpclient_parser_is_finished(http) ? Qtrue : Qfalse;
}


/**
 * call-seq:
 *    parser.nread -> Integer
 *
 * 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.
 */
VALUE HttpClientParser_nread(VALUE self)
{
  httpclient_parser *http = NULL;
  DATA_GET(self, httpclient_parser, http);

  return INT2FIX(http->nread);
}



void Init_http11_client()
{

  mEm = rb_define_module("EventMachine");

  eHttpClientParserError = rb_define_class_under(mEm, "HttpClientParserError", rb_eIOError);

  cHttpClientParser = rb_define_class_under(mEm, "HttpClientParser", rb_cObject);
  rb_define_alloc_func(cHttpClientParser, HttpClientParser_alloc);
  rb_define_method(cHttpClientParser, "initialize", HttpClientParser_init,0);
  rb_define_method(cHttpClientParser, "reset", HttpClientParser_reset,0);
  rb_define_method(cHttpClientParser, "finish", HttpClientParser_finish,0);
  rb_define_method(cHttpClientParser, "execute", HttpClientParser_execute,3);
  rb_define_method(cHttpClientParser, "error?", HttpClientParser_has_error,0);
  rb_define_method(cHttpClientParser, "finished?", HttpClientParser_is_finished,0);
  rb_define_method(cHttpClientParser, "nread", HttpClientParser_nread,0);
}