plamo 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ #ifndef RUBY_PLAMO_FORM_DATA_FIELD_ARRAY_H
2
+ #define RUBY_PLAMO_FORM_DATA_FIELD_ARRAY_H
3
+
4
+ extern VALUE rb_cPlamoFormDataFieldArray;
5
+ extern const rb_data_type_t rb_plamo_form_data_field_array_type;
6
+
7
+ void Init_plamo_form_data_field_array(void);
8
+
9
+ #endif /* RUBY_PLAMO_FORM_DATA_FIELD_ARRAY_H */
@@ -0,0 +1,69 @@
1
+ #include "plamo.h"
2
+
3
+ VALUE rb_cPlamoFormDataFile;
4
+
5
+ const rb_data_type_t rb_plamo_form_data_file_type = {
6
+ "FormDataFile",
7
+ {
8
+ NULL,
9
+ NULL,
10
+ NULL,
11
+ },
12
+ NULL,
13
+ NULL,
14
+ 0,
15
+ };
16
+
17
+ static VALUE allocate(VALUE klass) {
18
+ return TypedData_Wrap_Struct(klass, &rb_plamo_form_data_file_type, NULL);
19
+ }
20
+
21
+ static VALUE get_content_type(VALUE self) {
22
+ PlamoFormDataFile *plamo_form_data_file;
23
+ TypedData_Get_Struct(self, PlamoFormDataFile, &rb_plamo_form_data_file_type, plamo_form_data_file);
24
+ const char *str = plamo_form_data_file_get_content_type(plamo_form_data_file);
25
+ if (str != NULL) {
26
+ return rb_str_new2(str);
27
+ } else {
28
+ return Qnil;
29
+ }
30
+ }
31
+
32
+ static VALUE get_file_name(VALUE self) {
33
+ PlamoFormDataFile *plamo_form_data_file;
34
+ TypedData_Get_Struct(self, PlamoFormDataFile, &rb_plamo_form_data_file_type, plamo_form_data_file);
35
+ const char *str = plamo_form_data_file_get_file_name(plamo_form_data_file);
36
+ if (str != NULL) {
37
+ return rb_str_new2(str);
38
+ } else {
39
+ return Qnil;
40
+ }
41
+ }
42
+
43
+ static VALUE get_body(VALUE self) {
44
+ PlamoFormDataFile *plamo_form_data_file;
45
+ TypedData_Get_Struct(self, PlamoFormDataFile, &rb_plamo_form_data_file_type, plamo_form_data_file);
46
+ const size_t size = plamo_form_data_file_get_body_size(plamo_form_data_file);
47
+ const unsigned char *body = plamo_form_data_file_get_body(plamo_form_data_file);
48
+ VALUE rb_array = rb_ary_new2(size);
49
+ for (int i = 0; i < size; i++) {
50
+ rb_ary_store(rb_array, i, CHR2FIX(*(body + i)));
51
+ }
52
+ OBJ_FREEZE(rb_array);
53
+ return rb_array;
54
+ }
55
+
56
+ static VALUE get_body_size(VALUE self) {
57
+ PlamoFormDataFile *plamo_form_data_file;
58
+ TypedData_Get_Struct(self, PlamoFormDataFile, &rb_plamo_form_data_file_type, plamo_form_data_file);
59
+ return SIZET2NUM(plamo_form_data_file_get_body_size(plamo_form_data_file));
60
+ }
61
+
62
+ void Init_plamo_form_data_file(void) {
63
+ rb_cPlamoFormDataFile = rb_define_class_under(rb_mPlamo, "FormDataFile", rb_cObject);
64
+ rb_define_alloc_func(rb_cPlamoFormDataFile, allocate);
65
+ rb_define_method(rb_cPlamoFormDataFile, "content_type", get_content_type, 0);
66
+ rb_define_method(rb_cPlamoFormDataFile, "file_name", get_file_name, 0);
67
+ rb_define_method(rb_cPlamoFormDataFile, "size", get_body_size, 0);
68
+ rb_define_method(rb_cPlamoFormDataFile, "body", get_body, 0);
69
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef RUBY_PLAMO_FORM_DATA_FILE_H
2
+ #define RUBY_PLAMO_FORM_DATA_FILE_H
3
+
4
+ extern VALUE rb_cPlamoFormDataFile;
5
+ extern const rb_data_type_t rb_plamo_form_data_file_type;
6
+
7
+ void Init_plamo_form_data_file(void);
8
+
9
+ #endif /* RUBY_PLAMO_FORM_DATA_FILE_H */
@@ -0,0 +1,62 @@
1
+ #include "plamo.h"
2
+
3
+ VALUE rb_cPlamoFormUrlencoded;
4
+
5
+ static void deallocate(void *plamo_form_urlencoded) {
6
+ plamo_form_urlencoded_destroy(plamo_form_urlencoded);
7
+ }
8
+
9
+ static const rb_data_type_t rb_plamo_form_urlencoded_type = {
10
+ "FormUrlencoded",
11
+ {
12
+ NULL,
13
+ deallocate,
14
+ NULL,
15
+ },
16
+ NULL,
17
+ NULL,
18
+ 0,
19
+ };
20
+
21
+ static VALUE allocate(VALUE klass) {
22
+ return TypedData_Wrap_Struct(klass, &rb_plamo_form_urlencoded_type, NULL);
23
+ }
24
+
25
+ static VALUE initialize(VALUE self, VALUE request) {
26
+ PlamoRequest *plamo_request;
27
+ TypedData_Get_Struct(request, PlamoRequest, &rb_plamo_request_type, plamo_request);
28
+ DATA_PTR(self) = plamo_form_urlencoded_new(plamo_request);
29
+ return self;
30
+ }
31
+
32
+ static VALUE get(VALUE self, VALUE key) {
33
+ PlamoFormUrlencoded *plamo_form_urlencoded;
34
+ TypedData_Get_Struct(self, PlamoFormUrlencoded, &rb_plamo_form_urlencoded_type, plamo_form_urlencoded);
35
+ const PlamoStringArray *plamo_string_array = plamo_form_urlencoded_get(plamo_form_urlencoded, StringValueCStr(key));
36
+ if (plamo_string_array != NULL) {
37
+ VALUE rb_plamo_string_array = TypedData_Wrap_Struct(rb_cPlamoStringArray, &rb_plamo_string_array_type, (PlamoStringArray*)plamo_string_array);
38
+ OBJ_FREEZE(rb_plamo_string_array);
39
+ return rb_plamo_string_array;
40
+ } else {
41
+ return Qnil;
42
+ }
43
+ }
44
+
45
+ static void execute_each(const char *key, const char *value) {
46
+ rb_yield(rb_ary_new3(2, rb_str_new2(key), rb_str_new2(value)));
47
+ }
48
+
49
+ static VALUE each(VALUE self) {
50
+ PlamoFormUrlencoded *plamo_form_urlencoded;
51
+ TypedData_Get_Struct(self, PlamoFormUrlencoded, &rb_plamo_form_urlencoded_type, plamo_form_urlencoded);
52
+ plamo_form_urlencoded_for_each(plamo_form_urlencoded, execute_each);
53
+ return Qnil;
54
+ }
55
+
56
+ void Init_plamo_form_urlencoded(void) {
57
+ rb_cPlamoFormUrlencoded = rb_define_class_under(rb_mPlamo, "FormUrlencoded", rb_cObject);
58
+ rb_define_alloc_func(rb_cPlamoFormUrlencoded, allocate);
59
+ rb_define_method(rb_cPlamoFormUrlencoded, "initialize", initialize, 1);
60
+ rb_define_method(rb_cPlamoFormUrlencoded, "[]", get, 1);
61
+ rb_define_method(rb_cPlamoFormUrlencoded, "each", each, 0);
62
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef RUBY_PLAMO_FORM_URLENCODED_H
2
+ #define RUBY_PLAMO_FORM_URLENCODED_H
3
+
4
+ void Init_plamo_form_urlencoded(void);
5
+
6
+ #endif /* RUBY_PLAMO_FORM_URLENCODED_H */
@@ -1,65 +1,66 @@
1
1
  #include "plamo.h"
2
- #include "wrapper.h"
3
2
 
4
3
  VALUE rb_cPlamoHttpHeader;
5
4
 
6
- static void deallocate(Wrapper *wrapper) {
7
- plamo_http_header_destroy(wrapper->inner);
8
- free(wrapper);
5
+ static void deallocate(void *plamo_http_header) {
6
+ plamo_http_header_destroy(plamo_http_header);
9
7
  }
10
8
 
9
+ const rb_data_type_t rb_plamo_http_header_type = {
10
+ "HttpHeader",
11
+ {
12
+ NULL,
13
+ deallocate,
14
+ NULL,
15
+ },
16
+ NULL,
17
+ NULL,
18
+ 0,
19
+ };
20
+
11
21
  static VALUE allocate(VALUE klass) {
12
- return Data_Wrap_Struct(klass, NULL, deallocate, malloc(sizeof(Wrapper)));
22
+ return TypedData_Wrap_Struct(klass, &rb_plamo_http_header_type, NULL);
13
23
  }
14
24
 
15
25
  static VALUE initialize(VALUE self) {
16
- Wrapper *wrapper;
17
- Data_Get_Struct(self, Wrapper, wrapper);
18
- wrapper->inner = plamo_http_header_new();
26
+ DATA_PTR(self) = plamo_http_header_new();
19
27
  return self;
20
28
  }
21
29
 
22
30
  static VALUE push(VALUE self, VALUE key, VALUE value) {
23
- Wrapper *wrapper;
24
- Data_Get_Struct(self, Wrapper, wrapper);
25
- plamo_http_header_add(wrapper->inner, StringValueCStr(key), StringValueCStr(value));
31
+ PlamoHttpHeader *plamo_http_header;
32
+ TypedData_Get_Struct(self, PlamoHttpHeader, &rb_plamo_http_header_type, plamo_http_header);
33
+ plamo_http_header_add(plamo_http_header, StringValueCStr(key), StringValueCStr(value));
26
34
  return Qnil;
27
35
  }
28
36
 
29
37
  static VALUE get(VALUE self, VALUE key) {
30
- Wrapper *wrapper;
31
- Data_Get_Struct(self, Wrapper, wrapper);
32
- PlamoStringArray *plamo_string_array = plamo_http_header_get(wrapper->inner, StringValueCStr(key));
38
+ PlamoHttpHeader *plamo_http_header;
39
+ TypedData_Get_Struct(self, PlamoHttpHeader, &rb_plamo_http_header_type, plamo_http_header);
40
+ PlamoStringArray *plamo_string_array = plamo_http_header_get(plamo_http_header, StringValueCStr(key));
33
41
  if (plamo_string_array != NULL) {
34
- VALUE rb_plamo_string_array = Data_Wrap_Struct(rb_cPlamoStringArray, NULL, free, malloc(sizeof(Wrapper)));
35
- Wrapper *wrapper2;
36
- Data_Get_Struct(rb_plamo_string_array, Wrapper, wrapper2);
37
- wrapper2->inner = plamo_string_array;
42
+ VALUE rb_plamo_string_array = TypedData_Wrap_Struct(rb_cPlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
38
43
  return rb_plamo_string_array;
39
44
  } else {
40
45
  return Qnil;
41
46
  }
42
47
  }
43
48
 
44
- static void execute_each(const char *key, PlamoStringArray *values) {
45
- Wrapper *wrapper;
46
- VALUE rb_plamo_string_array = Data_Wrap_Struct(rb_cPlamoStringArray, NULL, free, malloc(sizeof(Wrapper)));
47
- Data_Get_Struct(rb_plamo_string_array, Wrapper, wrapper);
48
- wrapper->inner = values;
49
- rb_yield(rb_ary_new3(2, rb_str_new2(key), rb_plamo_string_array));
49
+ static void execute_each(const char *key, const char *value) {
50
+ rb_yield(rb_ary_new3(2, rb_str_new2(key), rb_str_new2(value)));
50
51
  }
51
52
 
52
- static VALUE each(VALUE self, VALUE key) {
53
- Wrapper *wrapper;
54
- Data_Get_Struct(self, Wrapper, wrapper);
55
- plamo_http_header_for_each(wrapper->inner, execute_each);
53
+ static VALUE each(VALUE self) {
54
+ PlamoHttpHeader *plamo_http_header;
55
+ TypedData_Get_Struct(self, PlamoHttpHeader, &rb_plamo_http_header_type, plamo_http_header);
56
+ plamo_http_header_for_each(plamo_http_header, execute_each);
56
57
  return Qnil;
57
58
  }
58
59
 
59
60
  static VALUE delete_at(VALUE self, VALUE key) {
60
- Wrapper *wrapper;
61
- Data_Get_Struct(self, Wrapper, wrapper);
62
- if (plamo_http_header_remove(wrapper->inner, StringValueCStr(key))) {
61
+ PlamoHttpHeader *plamo_http_header;
62
+ TypedData_Get_Struct(self, PlamoHttpHeader, &rb_plamo_http_header_type, plamo_http_header);
63
+ if (plamo_http_header_remove(plamo_http_header, StringValueCStr(key))) {
63
64
  return Qtrue;
64
65
  } else {
65
66
  return Qfalse;
@@ -2,6 +2,7 @@
2
2
  #define RUBY_PLAMO_HTTP_HEADER_H
3
3
 
4
4
  extern VALUE rb_cPlamoHttpHeader;
5
+ extern const rb_data_type_t rb_plamo_http_header_type;
5
6
 
6
7
  void Init_plamo_http_header(void);
7
8
 
@@ -1,40 +1,45 @@
1
1
  #include "plamo.h"
2
- #include "wrapper.h"
3
2
 
4
3
  VALUE rb_cPlamoHttpQuery;
5
4
 
6
- static void deallocate(Wrapper *wrapper) {
7
- plamo_http_query_destroy(wrapper->inner);
8
- free(wrapper);
5
+ static void deallocate(void *plamo_http_query) {
6
+ plamo_http_query_destroy(plamo_http_query);
9
7
  }
10
8
 
9
+ const rb_data_type_t rb_plamo_http_query_type = {
10
+ "HttpQuery",
11
+ {
12
+ NULL,
13
+ deallocate,
14
+ NULL,
15
+ },
16
+ NULL,
17
+ NULL,
18
+ 0,
19
+ };
20
+
11
21
  static VALUE allocate(VALUE klass) {
12
- return Data_Wrap_Struct(klass, NULL, deallocate, malloc(sizeof(Wrapper)));
22
+ return TypedData_Wrap_Struct(klass, &rb_plamo_http_query_type, NULL);
13
23
  }
14
24
 
15
25
  static VALUE initialize(VALUE self) {
16
- Wrapper *wrapper;
17
- Data_Get_Struct(self, Wrapper, wrapper);
18
- wrapper->inner = plamo_http_query_new();
26
+ DATA_PTR(self) = plamo_http_query_new();
19
27
  return self;
20
28
  }
21
29
 
22
30
  static VALUE push(VALUE self, VALUE key, VALUE value) {
23
- Wrapper *wrapper;
24
- Data_Get_Struct(self, Wrapper, wrapper);
25
- plamo_http_query_add(wrapper->inner, StringValueCStr(key), StringValueCStr(value));
31
+ PlamoHttpQuery *plamo_http_query;
32
+ TypedData_Get_Struct(self, PlamoHttpQuery, &rb_plamo_http_query_type, plamo_http_query);
33
+ plamo_http_query_add(plamo_http_query, StringValueCStr(key), StringValueCStr(value));
26
34
  return Qnil;
27
35
  }
28
36
 
29
37
  static VALUE get(VALUE self, VALUE key) {
30
- Wrapper *wrapper;
31
- Data_Get_Struct(self, Wrapper, wrapper);
32
- PlamoStringArray *plamo_string_array = plamo_http_query_get(wrapper->inner, StringValueCStr(key));
38
+ PlamoHttpQuery *plamo_http_query;
39
+ TypedData_Get_Struct(self, PlamoHttpQuery, &rb_plamo_http_query_type, plamo_http_query);
40
+ PlamoStringArray *plamo_string_array = plamo_http_query_get(plamo_http_query, StringValueCStr(key));
33
41
  if (plamo_string_array != NULL) {
34
- VALUE rb_plamo_string_array = Data_Wrap_Struct(rb_cPlamoStringArray, NULL, free, malloc(sizeof(Wrapper)));
35
- Wrapper *wrapper2;
36
- Data_Get_Struct(rb_plamo_string_array, Wrapper, wrapper2);
37
- wrapper2->inner = plamo_string_array;
42
+ VALUE rb_plamo_string_array = TypedData_Wrap_Struct(rb_cPlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
38
43
  return rb_plamo_string_array;
39
44
  } else {
40
45
  return Qnil;
@@ -42,9 +47,9 @@ static VALUE get(VALUE self, VALUE key) {
42
47
  }
43
48
 
44
49
  static VALUE delete_at(VALUE self, VALUE key) {
45
- Wrapper *wrapper;
46
- Data_Get_Struct(self, Wrapper, wrapper);
47
- if (plamo_http_query_remove(wrapper->inner, StringValueCStr(key))) {
50
+ PlamoHttpQuery *plamo_http_query;
51
+ TypedData_Get_Struct(self, PlamoHttpQuery, &rb_plamo_http_query_type, plamo_http_query);
52
+ if (plamo_http_query_remove(plamo_http_query, StringValueCStr(key))) {
48
53
  return Qtrue;
49
54
  } else {
50
55
  return Qfalse;
@@ -2,6 +2,7 @@
2
2
  #define RUBY_PLAMO_HTTP_QUERY_H
3
3
 
4
4
  extern VALUE rb_cPlamoHttpQuery;
5
+ extern const rb_data_type_t rb_plamo_http_query_type;
5
6
 
6
7
  void Init_plamo_http_query(void);
7
8
 
@@ -1,11 +1,10 @@
1
1
  #include "plamo.h"
2
- #include "wrapper.h"
3
2
  #include <stdbool.h>
4
3
  #include <pthread.h>
5
4
  #include <ruby/thread.h>
6
5
 
7
6
  // This hack is reference the below post.
8
- // https://www.burgestrand.se//articles/asynchronous-callbacks-in-ruby-c-extensions/
7
+ // https://www.burgestrand.se/articles/asynchronous-callbacks-in-ruby-c-extensions/
9
8
 
10
9
  VALUE rb_cPlamoMiddleware;
11
10
 
@@ -46,13 +45,24 @@ static callback_t* g_callback_queue_pop(void) {
46
45
  return callback;
47
46
  }
48
47
 
49
- static void deallocate(Wrapper *wrapper) {
50
- plamo_middleware_destroy((PlamoMiddleware*)wrapper->inner);
51
- free(wrapper);
48
+ static void deallocate(void *plamo_middleware) {
49
+ plamo_middleware_destroy(plamo_middleware);
52
50
  }
53
51
 
52
+ const rb_data_type_t rb_plamo_middleware_type = {
53
+ "Middleware",
54
+ {
55
+ NULL,
56
+ deallocate,
57
+ NULL,
58
+ },
59
+ NULL,
60
+ NULL,
61
+ 0,
62
+ };
63
+
54
64
  static VALUE allocate(VALUE klass) {
55
- return Data_Wrap_Struct(klass, NULL, deallocate, malloc(sizeof(Wrapper)));
65
+ return TypedData_Wrap_Struct(klass, &rb_plamo_middleware_type, NULL);
56
66
  }
57
67
 
58
68
  static void* plamo_callback(void *args) {
@@ -98,14 +108,8 @@ static VALUE handle_callback(void *cb) {
98
108
  VALUE rb_config = callback->config->config;
99
109
  VALUE rb_proc = callback->config->callback;
100
110
 
101
- VALUE rb_plamo_request = Data_Wrap_Struct(rb_cPlamoRequest, NULL, free, malloc(sizeof(Wrapper)));
102
- Wrapper *plamo_request_wrapper;
103
- Data_Get_Struct(rb_plamo_request, Wrapper, plamo_request_wrapper);
104
- plamo_request_wrapper->inner = (PlamoRequest*)callback->plamo_request;
105
- VALUE rb_plamo_response = Data_Wrap_Struct(rb_cPlamoResponse, NULL, free, malloc(sizeof(Wrapper)));
106
- Wrapper *plamo_response_wrapper;
107
- Data_Get_Struct(rb_plamo_response, Wrapper, plamo_response_wrapper);
108
- plamo_response_wrapper->inner = callback->plamo_response;
111
+ VALUE rb_plamo_request = TypedData_Wrap_Struct(rb_cPlamoRequest, &rb_plamo_request_type, (PlamoRequest*)callback->plamo_request);
112
+ VALUE rb_plamo_response = TypedData_Wrap_Struct(rb_cPlamoResponse, &rb_plamo_response_type, callback->plamo_response);
109
113
 
110
114
  rb_proc_call(rb_proc, rb_ary_new3(3, rb_config, rb_plamo_request, rb_plamo_response));
111
115
 
@@ -158,13 +162,10 @@ static VALUE event_thread(void *_) {
158
162
  }
159
163
 
160
164
  static VALUE initialize(VALUE self, VALUE rb_config, VALUE rb_callback) {
161
- Wrapper *wrapper;
162
- Data_Get_Struct(self, Wrapper, wrapper);
163
165
  PlamoRbCallbackConfig *plamo_rb_calback_config = malloc(sizeof(PlamoRbCallbackConfig));
164
166
  plamo_rb_calback_config->config = rb_config;
165
167
  plamo_rb_calback_config->callback = rb_callback;
166
- wrapper->inner = plamo_middleware_new(plamo_rb_calback_config, before_plamo_callback);
167
-
168
+ DATA_PTR(self) = plamo_middleware_new(plamo_rb_calback_config, before_plamo_callback);
168
169
  return self;
169
170
  }
170
171
 
@@ -2,5 +2,6 @@
2
2
  #define RUBY_PLAMO_MIDDLEWARE_H
3
3
 
4
4
  void Init_plamo_middleware(void);
5
+ extern const rb_data_type_t rb_plamo_middleware_type;
5
6
 
6
7
  #endif /* RUBY_PLAMO_MIDDLEWARE_H */
@@ -1,22 +1,30 @@
1
1
  #include "plamo.h"
2
- #include "wrapper.h"
3
2
 
4
3
  VALUE rb_cPlamoRequest;
5
4
 
6
5
  static VALUE sym_http, sym_https, sym_http_2_0, sym_http_1_1, sym_http_1_0, sym_http_0_9;
7
6
 
8
- static void deallocate(Wrapper *wrapper) {
9
- plamo_request_destroy(wrapper->inner);
10
- free(wrapper);
7
+ static void deallocate(void *plamo_request) {
8
+ plamo_request_destroy(plamo_request);
11
9
  }
12
10
 
11
+ const rb_data_type_t rb_plamo_request_type = {
12
+ "Request",
13
+ {
14
+ NULL,
15
+ deallocate,
16
+ NULL,
17
+ },
18
+ NULL,
19
+ NULL,
20
+ 0,
21
+ };
22
+
13
23
  static VALUE allocate(VALUE klass) {
14
- return Data_Wrap_Struct(klass, NULL, deallocate, malloc(sizeof(Wrapper)));
24
+ return TypedData_Wrap_Struct(klass, &rb_plamo_request_type, NULL);
15
25
  }
16
26
 
17
27
  static VALUE initialize(VALUE self, VALUE rb_scheme, VALUE rb_version, VALUE rb_method, VALUE rb_path, VALUE rb_query, VALUE rb_header, VALUE rb_body) {
18
- Wrapper *wrapper;
19
- Data_Get_Struct(self, Wrapper, wrapper);
20
28
  const PlamoScheme scheme = sym_http == rb_scheme ? PlamoSchemeHttp : PlamoSchemeHttps;
21
29
  const PlamoHttpVersion version = sym_http_2_0 == rb_version ? PlamoHttpVersionHttp20
22
30
  : sym_http_1_1 == rb_version ? PlamoHttpVersionHttp11
@@ -29,26 +37,26 @@ static VALUE initialize(VALUE self, VALUE rb_scheme, VALUE rb_version, VALUE rb_
29
37
  method.undefined_http_method = StringValueCStr(rb_method);
30
38
  }
31
39
  const char *path = StringValueCStr(rb_path);
32
- Wrapper *plamo_http_query_wrapper;
33
- Data_Get_Struct(rb_query, Wrapper, plamo_http_query_wrapper);
34
- Wrapper *plamo_http_header_wrapper;
35
- Data_Get_Struct(rb_header, Wrapper, plamo_http_header_wrapper);
36
- Wrapper *plamo_byte_array_wrapper;
37
- Data_Get_Struct(rb_body, Wrapper, plamo_byte_array_wrapper);
38
- wrapper->inner = plamo_request_new(scheme, version, method, path, plamo_http_query_wrapper->inner, plamo_http_header_wrapper->inner, plamo_byte_array_wrapper->inner);
40
+ PlamoHttpQuery *plamo_http_query;
41
+ TypedData_Get_Struct(rb_query, PlamoHttpQuery, &rb_plamo_http_query_type, plamo_http_query);
42
+ PlamoHttpHeader *plamo_http_header;
43
+ TypedData_Get_Struct(rb_header, PlamoHttpHeader, &rb_plamo_http_header_type, plamo_http_header);
44
+ PlamoByteArray *plamo_byte_array;
45
+ TypedData_Get_Struct(rb_body, PlamoByteArray, &rb_plamo_byte_array_type, plamo_byte_array);
46
+ DATA_PTR(self) = plamo_request_new(scheme, version, method, path, plamo_http_query, plamo_http_header, plamo_byte_array);
39
47
  return self;
40
48
  }
41
49
 
42
50
  static VALUE scheme(VALUE self) {
43
- Wrapper *wrapper;
44
- Data_Get_Struct(self, Wrapper, wrapper);
45
- return ((PlamoRequest*)wrapper->inner)->scheme == PlamoSchemeHttp ? sym_http : sym_https;
51
+ PlamoRequest *plamo_request;
52
+ TypedData_Get_Struct(self, PlamoRequest, &rb_plamo_request_type, plamo_request);
53
+ return plamo_request->scheme == PlamoSchemeHttp ? sym_http : sym_https;
46
54
  }
47
55
 
48
56
  static VALUE version(VALUE self) {
49
- Wrapper *wrapper;
50
- Data_Get_Struct(self, Wrapper, wrapper);
51
- switch (((PlamoRequest*)wrapper->inner)->version) {
57
+ PlamoRequest *plamo_request;
58
+ TypedData_Get_Struct(self, PlamoRequest, &rb_plamo_request_type, plamo_request);
59
+ switch (plamo_request->version) {
52
60
  case PlamoHttpVersionHttp20:
53
61
  return sym_http_2_0;
54
62
  case PlamoHttpVersionHttp11:
@@ -61,9 +69,9 @@ static VALUE version(VALUE self) {
61
69
  }
62
70
 
63
71
  static VALUE method(VALUE self) {
64
- Wrapper *wrapper;
65
- Data_Get_Struct(self, Wrapper, wrapper);
66
- PlamoDefinedHttpMethod method = ((PlamoRequest*)wrapper->inner)->method.defined_http_method;
72
+ PlamoRequest *plamo_request;
73
+ TypedData_Get_Struct(self, PlamoRequest, &rb_plamo_request_type, plamo_request);
74
+ PlamoDefinedHttpMethod method = plamo_request->method.defined_http_method;
67
75
  if (method == PLAMO_HTTP_METHOD_GET) {
68
76
  return rb_str_new2("GET");
69
77
  } else if (method == PLAMO_HTTP_METHOD_POST) {
@@ -83,43 +91,34 @@ static VALUE method(VALUE self) {
83
91
  } else if (method == PLAMO_HTTP_METHOD_PATCH) {
84
92
  return rb_str_new2("PATCH");
85
93
  } else {
86
- return rb_str_new2(((PlamoRequest*)wrapper->inner)->method.undefined_http_method);
94
+ return rb_str_new2(plamo_request->method.undefined_http_method);
87
95
  }
88
96
  }
89
97
 
90
98
  static VALUE path(VALUE self) {
91
- Wrapper *wrapper;
92
- Data_Get_Struct(self, Wrapper, wrapper);
93
- return rb_str_new2(plamo_string_get_char(((PlamoRequest*)wrapper->inner)->path));
99
+ PlamoRequest *plamo_request;
100
+ TypedData_Get_Struct(self, PlamoRequest, &rb_plamo_request_type, plamo_request);
101
+ return rb_str_new2(plamo_string_get_char(plamo_request->path));
94
102
  }
95
103
 
96
104
  static VALUE query(VALUE self) {
97
- Wrapper *wrapper;
98
- Data_Get_Struct(self, Wrapper, wrapper);
99
- VALUE rb_plamo_http_query = Data_Wrap_Struct(rb_cPlamoHttpQuery, NULL, free, malloc(sizeof(Wrapper)));
100
- Wrapper *plamo_http_query_wrapper;
101
- Data_Get_Struct(rb_plamo_http_query, Wrapper, plamo_http_query_wrapper);
102
- plamo_http_query_wrapper->inner = ((PlamoRequest*)wrapper->inner)->query;
105
+ PlamoRequest *plamo_request;
106
+ TypedData_Get_Struct(self, PlamoRequest, &rb_plamo_request_type, plamo_request);
107
+ VALUE rb_plamo_http_query = TypedData_Wrap_Struct(rb_cPlamoHttpQuery, &rb_plamo_http_query_type, plamo_request->query);
103
108
  return rb_plamo_http_query;
104
109
  }
105
110
 
106
111
  static VALUE header(VALUE self) {
107
- Wrapper *wrapper;
108
- Data_Get_Struct(self, Wrapper, wrapper);
109
- VALUE rb_plamo_http_header = Data_Wrap_Struct(rb_cPlamoHttpHeader, NULL, free, malloc(sizeof(Wrapper)));
110
- Wrapper *plamo_http_header_wrapper;
111
- Data_Get_Struct(rb_plamo_http_header, Wrapper, plamo_http_header_wrapper);
112
- plamo_http_header_wrapper->inner = ((PlamoRequest*)wrapper->inner)->header;
112
+ PlamoRequest *plamo_request;
113
+ TypedData_Get_Struct(self, PlamoRequest, &rb_plamo_request_type, plamo_request);
114
+ VALUE rb_plamo_http_header = TypedData_Wrap_Struct(rb_cPlamoHttpHeader, &rb_plamo_http_header_type, plamo_request->header);
113
115
  return rb_plamo_http_header;
114
116
  }
115
117
 
116
118
  static VALUE body(VALUE self) {
117
- Wrapper *wrapper;
118
- Data_Get_Struct(self, Wrapper, wrapper);
119
- VALUE rb_plamo_byte_array = Data_Wrap_Struct(rb_cPlamoByteArray, NULL, free, malloc(sizeof(Wrapper)));
120
- Wrapper *plamo_byte_array_wrapper;
121
- Data_Get_Struct(rb_plamo_byte_array, Wrapper, plamo_byte_array_wrapper);
122
- plamo_byte_array_wrapper->inner = ((PlamoRequest*)wrapper->inner)->body;
119
+ PlamoRequest *plamo_request;
120
+ TypedData_Get_Struct(self, PlamoRequest, &rb_plamo_request_type, plamo_request);
121
+ VALUE rb_plamo_byte_array = TypedData_Wrap_Struct(rb_cPlamoByteArray, &rb_plamo_byte_array_type, plamo_request->body);
123
122
  return rb_plamo_byte_array;
124
123
  }
125
124