-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathrubyhttp.cpp
More file actions
334 lines (270 loc) · 9.24 KB
/
rubyhttp.cpp
File metadata and controls
334 lines (270 loc) · 9.24 KB
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
329
330
331
332
333
334
/*****************************************************************************
File: libmain.cpp
Date: 06Apr06
Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
Gmail: garbagecat10
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
#include <ruby.h>
#include "http.h"
/**************************
class RubyHttpConnection_t
**************************/
class RubyHttpConnection_t: public HttpConnection_t
{
public:
RubyHttpConnection_t (VALUE v): Myself(v) {}
virtual ~RubyHttpConnection_t() {}
virtual void SendData (const char*, int);
virtual void CloseConnection (bool after_writing);
virtual void ProcessRequest (const char *request_method,
const char *cookie,
const char *ifnonematch,
const char *contenttype,
const char *query_string,
const char *path_info,
const char *request_uri,
const char *protocol,
int postlength,
const char *postdata,
const char *hdrblock,
int hdrblocksize);
virtual void ReceivePostData (const char *data, int len);
private:
VALUE Myself;
};
/******************************
RubyHttpConnection_t::SendData
******************************/
void RubyHttpConnection_t::SendData (const char *data, int length)
{
rb_funcall (Myself, rb_intern ("send_data"), 1, rb_str_new (data, length));
}
/*************************************
RubyHttpConnection_t::CloseConnection
*************************************/
void RubyHttpConnection_t::CloseConnection (bool after_writing)
{
VALUE v = rb_intern (after_writing ? "close_connection_after_writing" : "close_connection");
rb_funcall (Myself, v, 0);
}
/*************************************
RubyHttpConnection_t::ReceivePostData
*************************************/
void RubyHttpConnection_t::ReceivePostData (const char *data, int len)
{
VALUE data_val = Qnil;
if ((len > 0) && data) {
data_val = rb_str_new(data,len);
rb_funcall (Myself, rb_intern ("receive_post_data"), 1, data_val);
}
}
/************************************
RubyHttpConnection_t::ProcessRequest
************************************/
void RubyHttpConnection_t::ProcessRequest (const char *request_method,
const char *cookie,
const char *ifnonematch,
const char *contenttype,
const char *query_string,
const char *path_info,
const char *request_uri,
const char *protocol,
int post_length,
const char *post_content,
const char *hdr_block,
int hdr_block_size)
{
VALUE post = Qnil;
VALUE headers = Qnil;
VALUE req_method = Qnil;
VALUE cookie_val = Qnil;
VALUE ifnonematch_val = Qnil;
VALUE contenttype_val = Qnil;
VALUE path_info_val = Qnil;
VALUE query_string_val = Qnil;
VALUE request_uri_val = Qnil;
VALUE protocol_val = Qnil;
if ((post_length > 0) && post_content)
post = rb_str_new (post_content, post_length);
if (hdr_block && (hdr_block_size > 0))
headers = rb_str_new (hdr_block, hdr_block_size);
else
headers = rb_str_new ("", 0);
if (request_method && *request_method)
req_method = rb_str_new (request_method, strlen (request_method));
if (cookie && *cookie)
cookie_val = rb_str_new (cookie, strlen (cookie));
if (ifnonematch && *ifnonematch)
ifnonematch_val = rb_str_new (ifnonematch, strlen (ifnonematch));
if (contenttype && *contenttype)
contenttype_val = rb_str_new (contenttype, strlen (contenttype));
if (path_info && *path_info)
path_info_val = rb_str_new (path_info, strlen (path_info));
if (query_string && *query_string)
query_string_val = rb_str_new (query_string, strlen (query_string));
if (request_uri && *request_uri)
request_uri_val = rb_str_new (request_uri, strlen (request_uri));
if (protocol && *protocol)
protocol_val = rb_str_new (protocol, strlen (protocol));
rb_ivar_set (Myself, rb_intern ("@http_request_method"), req_method);
rb_ivar_set (Myself, rb_intern ("@http_cookie"), cookie_val);
rb_ivar_set (Myself, rb_intern ("@http_if_none_match"), ifnonematch_val);
rb_ivar_set (Myself, rb_intern ("@http_content_type"), contenttype_val);
rb_ivar_set (Myself, rb_intern ("@http_path_info"), path_info_val);
rb_ivar_set (Myself, rb_intern ("@http_request_uri"), request_uri_val);
rb_ivar_set (Myself, rb_intern ("@http_query_string"), query_string_val);
rb_ivar_set (Myself, rb_intern ("@http_post_content"), post);
rb_ivar_set (Myself, rb_intern ("@http_headers"), headers);
rb_ivar_set (Myself, rb_intern ("@http_protocol"), protocol_val);
rb_funcall (Myself, rb_intern ("process_http_request"), 0);
}
/*******
Statics
*******/
VALUE Intern_http_conn;
/********************
t_get_http_connection
********************/
static RubyHttpConnection_t *t_get_http_connection(VALUE self)
{
RubyHttpConnection_t *hc;
VALUE ivar = rb_ivar_get (self, Intern_http_conn);
if (ivar != Qnil)
Data_Get_Struct (ivar, RubyHttpConnection_t, hc);
else
hc = NULL;
return hc;
}
/********************
t_delete_http_connection
********************/
void t_delete_http_connection(RubyHttpConnection_t *hc)
{
delete hc;
}
/***********
t_post_init
***********/
static VALUE t_post_init (VALUE self)
{
RubyHttpConnection_t *hc = new RubyHttpConnection_t (self);
if (!hc)
throw std::runtime_error ("no http-connection object");
/*
HACK: http_connection should be given an actual type. No one should
be touching it from inside ruby, but still
*/
VALUE http_connection = Data_Wrap_Struct(CLASS_OF(self), 0, t_delete_http_connection, hc);
rb_ivar_set (self, Intern_http_conn, http_connection);
return Qnil;
}
/**************
t_receive_data
**************/
static VALUE t_receive_data (VALUE self, VALUE data)
{
int length = NUM2INT (rb_funcall (data, rb_intern ("length"), 0));
RubyHttpConnection_t *hc = t_get_http_connection (self);
if (hc)
hc->ConsumeData (StringValuePtr (data), length);
return Qnil;
}
/*******************
t_receive_post_data
*******************/
static VALUE t_receive_post_data (VALUE self, VALUE data)
{
/** This is a NOOP. It should be overridden. **/
return Qnil;
}
/********
t_unbind
********/
static VALUE t_unbind (VALUE self)
{
return Qnil;
}
/**********************
t_process_http_request
**********************/
static VALUE t_process_http_request (VALUE self)
{
// This is a stub in case the caller doesn't define it.
rb_funcall (self, rb_intern ("send_data"), 1, rb_str_new2 ("HTTP/1.1 200 ...\r\nContent-type: text/plain\r\nContent-length: 8\r\n\r\nMonorail"));
return Qnil;
}
/************************
t_no_environment_strings
************************/
static VALUE t_no_environment_strings (VALUE self)
{
RubyHttpConnection_t *hc = t_get_http_connection (self);
if (hc)
hc->SetNoEnvironmentStrings();
return Qnil;
}
/**********************
t_dont_accumulate_post
**********************/
static VALUE t_dont_accumulate_post (VALUE self)
{
RubyHttpConnection_t *hc = t_get_http_connection (self);
if (hc)
hc->SetDontAccumulatePost();
return Qnil;
}
/**********************
t_get_max_content_length
**********************/
static VALUE t_get_max_content_length (VALUE self)
{
RubyHttpConnection_t *hc = t_get_http_connection (self);
if (hc)
return INT2FIX (hc->GetMaxContentLength());
return Qnil;
}
/**********************
t_set_max_content_length
**********************/
static VALUE t_set_max_content_length (VALUE self, VALUE data)
{
RubyHttpConnection_t *hc = t_get_http_connection (self);
if (hc) {
hc->SetMaxContentLength(FIX2INT(data));
return INT2FIX (hc->GetMaxContentLength());
}
return Qnil;
}
/****************************
Init_eventmachine_httpserver
****************************/
extern "C" void Init_eventmachine_httpserver()
{
Intern_http_conn = rb_intern ("http_conn");
VALUE EmModule = rb_define_module ("EventMachine");
VALUE HttpServer = rb_define_module_under (EmModule, "HttpServer");
rb_define_method (HttpServer, "post_init", (VALUE(*)(...))t_post_init, 0);
rb_define_method (HttpServer, "receive_data", (VALUE(*)(...))t_receive_data, 1);
rb_define_method (HttpServer, "receive_post_data", (VALUE(*)(...))t_receive_post_data, 1);
rb_define_method (HttpServer, "unbind", (VALUE(*)(...))t_unbind, 0);
rb_define_method (HttpServer, "process_http_request", (VALUE(*)(...))t_process_http_request, 0);
rb_define_method (HttpServer, "no_environment_strings", (VALUE(*)(...))t_no_environment_strings, 0);
rb_define_method (HttpServer, "dont_accumulate_post", (VALUE(*)(...))t_dont_accumulate_post, 0);
rb_define_method (HttpServer, "max_content_length", (VALUE(*)(...))t_get_max_content_length, 0);
rb_define_method (HttpServer, "max_content_length=", (VALUE(*)(...))t_set_max_content_length, 1);
}