ovirt-engine-sdk 4.0.1 → 4.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,75 @@
1
+ /*
2
+ Copyright (c) 2016-2017 Red Hat, Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ #ifndef __OV_HTTP_CLIENT_H__
18
+ #define __OV_HTTP_CLIENT_H__
19
+
20
+ #include <ruby.h>
21
+
22
+ #include <curl/curl.h>
23
+ #include <stdbool.h>
24
+
25
+ /* Data type and class: */
26
+ extern rb_data_type_t ov_http_client_type;
27
+ extern VALUE ov_http_client_class;
28
+
29
+ /* Content: */
30
+ typedef struct {
31
+ /* The libcurl multi handle, used to implement multiple simultaneous requests: */
32
+ CURLM* handle;
33
+
34
+ /* The libcurl share handle, used to share cookie data between multiple requests: */
35
+ CURLSH* share;
36
+
37
+ /* The logger: */
38
+ VALUE log;
39
+
40
+ /* The max number of requests that can be processed simultaneously by libcurl. Will be calculated multiplying the
41
+ max number of connections by the pipeline length: */
42
+ int limit;
43
+
44
+ /* This queue contains the requests that have not yet been sent to libcurl for processing: */
45
+ VALUE queue;
46
+
47
+ /* This hash stores the transfers that are pending. The key of the hash is the request that initiated the transfer,
48
+ and the value is the transfer itself. */
49
+ VALUE pending;
50
+
51
+ /* This hash stores the completed transfers. The key of the hash is the request, and the value is either the
52
+ response to that request, or else the exception that was generated while trying to process it. */
53
+ VALUE completed;
54
+
55
+ /* Copies of the options passed to the constructor: */
56
+ bool compress;
57
+ bool debug;
58
+ bool insecure;
59
+ char* ca_file;
60
+ char* proxy_url;
61
+ char* proxy_username;
62
+ char* proxy_password;
63
+ int timeout;
64
+ int connect_timeout;
65
+ char* cookies;
66
+ } ov_http_client_object;
67
+
68
+ /* Macro to get the pointer: */
69
+ #define ov_http_client_ptr(object, ptr) \
70
+ TypedData_Get_Struct((object), ov_http_client_object, &ov_http_client_type, (ptr))
71
+
72
+ /* Initialization function: */
73
+ extern void ov_http_client_define(void);
74
+
75
+ #endif
@@ -0,0 +1,397 @@
1
+ /*
2
+ Copyright (c) 2016-2017 Red Hat, Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ #include <ruby.h>
18
+
19
+ #include "ov_module.h"
20
+ #include "ov_error.h"
21
+ #include "ov_http_request.h"
22
+
23
+ /* Class: */
24
+ VALUE ov_http_request_class;
25
+
26
+ /* Symbols for HTTP methods: */
27
+ VALUE GET_SYMBOL;
28
+ VALUE POST_SYMBOL;
29
+ VALUE PUT_SYMBOL;
30
+ VALUE DELETE_SYMBOL;
31
+
32
+ /* Symbols for the attributes: */
33
+ static VALUE METHOD_SYMBOL;
34
+ static VALUE URL_SYMBOL;
35
+ static VALUE QUERY_SYMBOL;
36
+ static VALUE HEADERS_SYMBOL;
37
+ static VALUE USERNAME_SYMBOL;
38
+ static VALUE PASSWORD_SYMBOL;
39
+ static VALUE TOKEN_SYMBOL;
40
+ static VALUE KERBEROS_SYMBOL;
41
+ static VALUE BODY_SYMBOL;
42
+ static VALUE TIMEOUT_SYMBOL;
43
+ static VALUE CONNECT_TIMEOUT_SYMBOL;
44
+
45
+ static void ov_http_request_mark(void* vptr) {
46
+ ov_http_request_object* ptr;
47
+
48
+ ptr = vptr;
49
+ rb_gc_mark(ptr->method);
50
+ rb_gc_mark(ptr->url);
51
+ rb_gc_mark(ptr->query);
52
+ rb_gc_mark(ptr->headers);
53
+ rb_gc_mark(ptr->username);
54
+ rb_gc_mark(ptr->password);
55
+ rb_gc_mark(ptr->token);
56
+ rb_gc_mark(ptr->kerberos);
57
+ rb_gc_mark(ptr->body);
58
+ rb_gc_mark(ptr->timeout);
59
+ rb_gc_mark(ptr->connect_timeout);
60
+ }
61
+
62
+ static void ov_http_request_free(void* vptr) {
63
+ ov_http_request_object* ptr;
64
+
65
+ ptr = vptr;
66
+ xfree(ptr);
67
+ }
68
+
69
+ rb_data_type_t ov_http_request_type = {
70
+ .wrap_struct_name = "OVHTTPREQUEST",
71
+ .function = {
72
+ .dmark = ov_http_request_mark,
73
+ .dfree = ov_http_request_free,
74
+ .dsize = NULL,
75
+ .reserved = { NULL, NULL }
76
+ },
77
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
78
+ .parent = NULL,
79
+ .data = NULL,
80
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
81
+ #endif
82
+ };
83
+
84
+ static VALUE ov_http_request_alloc(VALUE klass) {
85
+ ov_http_request_object* ptr;
86
+
87
+ ptr = ALLOC(ov_http_request_object);
88
+ ptr->method = Qnil;
89
+ ptr->url = Qnil;
90
+ ptr->query = Qnil;
91
+ ptr->headers = Qnil;
92
+ ptr->username = Qnil;
93
+ ptr->password = Qnil;
94
+ ptr->token = Qnil;
95
+ ptr->kerberos = Qnil;
96
+ ptr->body = Qnil;
97
+ ptr->timeout = Qnil;
98
+ ptr->connect_timeout = Qnil;
99
+ return TypedData_Wrap_Struct(klass, &ov_http_request_type, ptr);
100
+ }
101
+
102
+ static VALUE ov_http_request_get_method(VALUE self) {
103
+ ov_http_request_object* ptr;
104
+
105
+ ov_http_request_ptr(self, ptr);
106
+ return ptr->method;
107
+ }
108
+
109
+ static VALUE ov_http_request_set_method(VALUE self, VALUE value) {
110
+ ov_http_request_object* ptr;
111
+
112
+ ov_http_request_ptr(self, ptr);
113
+ if (NIL_P(value)) {
114
+ ptr->method = GET_SYMBOL;
115
+ }
116
+ else {
117
+ Check_Type(value, T_SYMBOL);
118
+ ptr->method = value;
119
+ }
120
+ return Qnil;
121
+ }
122
+
123
+ static VALUE ov_http_request_get_url(VALUE self) {
124
+ ov_http_request_object* ptr;
125
+
126
+ ov_http_request_ptr(self, ptr);
127
+ return ptr->url;
128
+ }
129
+
130
+ static VALUE ov_http_request_set_url(VALUE self, VALUE value) {
131
+ ov_http_request_object* ptr;
132
+
133
+ ov_http_request_ptr(self, ptr);
134
+ if (!NIL_P(value)) {
135
+ Check_Type(value, T_STRING);
136
+ }
137
+ ptr->url = value;
138
+ return Qnil;
139
+ }
140
+
141
+ static VALUE ov_http_request_get_query(VALUE self) {
142
+ ov_http_request_object* ptr;
143
+
144
+ ov_http_request_ptr(self, ptr);
145
+ return ptr->query;
146
+ }
147
+
148
+ static VALUE ov_http_request_set_query(VALUE self, VALUE value) {
149
+ ov_http_request_object* ptr;
150
+
151
+ ov_http_request_ptr(self, ptr);
152
+ if (!NIL_P(value)) {
153
+ Check_Type(value, T_HASH);
154
+ }
155
+ ptr->query = value;
156
+ return Qnil;
157
+ }
158
+
159
+ static VALUE ov_http_request_get_headers(VALUE self) {
160
+ ov_http_request_object* ptr;
161
+
162
+ ov_http_request_ptr(self, ptr);
163
+ return ptr->headers;
164
+ }
165
+
166
+ static VALUE ov_http_request_set_headers(VALUE self, VALUE value) {
167
+ ov_http_request_object* ptr;
168
+
169
+ ov_http_request_ptr(self, ptr);
170
+ if (NIL_P(value)) {
171
+ ptr->headers = rb_hash_new();
172
+ }
173
+ else {
174
+ Check_Type(value, T_HASH);
175
+ ptr->headers = value;
176
+ }
177
+ return Qnil;
178
+ }
179
+
180
+ static VALUE ov_http_request_get_username(VALUE self) {
181
+ ov_http_request_object* ptr;
182
+
183
+ ov_http_request_ptr(self, ptr);
184
+ return ptr->username;
185
+ }
186
+
187
+ static VALUE ov_http_request_set_username(VALUE self, VALUE value) {
188
+ ov_http_request_object* ptr;
189
+
190
+ ov_http_request_ptr(self, ptr);
191
+ if (!NIL_P(value)) {
192
+ Check_Type(value, T_STRING);
193
+ }
194
+ ptr->username = value;
195
+ return Qnil;
196
+ }
197
+
198
+ static VALUE ov_http_request_get_password(VALUE self) {
199
+ ov_http_request_object* ptr;
200
+
201
+ ov_http_request_ptr(self, ptr);
202
+ return ptr->password;
203
+ }
204
+
205
+ static VALUE ov_http_request_set_password(VALUE self, VALUE value) {
206
+ ov_http_request_object* ptr;
207
+
208
+ ov_http_request_ptr(self, ptr);
209
+ if (!NIL_P(value)) {
210
+ Check_Type(value, T_STRING);
211
+ }
212
+ ptr->password = value;
213
+ return Qnil;
214
+ }
215
+
216
+ static VALUE ov_http_request_get_token(VALUE self) {
217
+ ov_http_request_object* ptr;
218
+
219
+ ov_http_request_ptr(self, ptr);
220
+ return ptr->token;
221
+ }
222
+
223
+ static VALUE ov_http_request_set_token(VALUE self, VALUE value) {
224
+ ov_http_request_object* ptr;
225
+
226
+ ov_http_request_ptr(self, ptr);
227
+ if (!NIL_P(value)) {
228
+ Check_Type(value, T_STRING);
229
+ }
230
+ ptr->token = value;
231
+ return Qnil;
232
+ }
233
+
234
+ static VALUE ov_http_request_get_kerberos(VALUE self) {
235
+ ov_http_request_object* ptr;
236
+
237
+ ov_http_request_ptr(self, ptr);
238
+ return ptr->kerberos;
239
+ }
240
+
241
+ static VALUE ov_http_request_set_kerberos(VALUE self, VALUE value) {
242
+ ov_http_request_object* ptr;
243
+
244
+ ov_http_request_ptr(self, ptr);
245
+ ptr->kerberos = RTEST(value);
246
+ return Qnil;
247
+ }
248
+
249
+ static VALUE ov_http_request_get_body(VALUE self) {
250
+ ov_http_request_object* ptr;
251
+
252
+ ov_http_request_ptr(self, ptr);
253
+ return ptr->body;
254
+ }
255
+
256
+ static VALUE ov_http_request_set_body(VALUE self, VALUE value) {
257
+ ov_http_request_object* ptr;
258
+
259
+ ov_http_request_ptr(self, ptr);
260
+ if (!NIL_P(value)) {
261
+ Check_Type(value, T_STRING);
262
+ }
263
+ ptr->body = value;
264
+ return Qnil;
265
+ }
266
+
267
+ static VALUE ov_http_request_get_timeout(VALUE self) {
268
+ ov_http_request_object* ptr;
269
+
270
+ ov_http_request_ptr(self, ptr);
271
+ return ptr->timeout;
272
+ }
273
+
274
+ static VALUE ov_http_request_set_timeout(VALUE self, VALUE value) {
275
+ ov_http_request_object* ptr;
276
+
277
+ ov_http_request_ptr(self, ptr);
278
+ if (!NIL_P(value)) {
279
+ Check_Type(value, T_FIXNUM);
280
+ }
281
+ ptr->timeout = value;
282
+ return Qnil;
283
+ }
284
+
285
+ static VALUE ov_http_request_get_connect_timeout(VALUE self) {
286
+ ov_http_request_object* ptr;
287
+
288
+ ov_http_request_ptr(self, ptr);
289
+ return ptr->connect_timeout;
290
+ }
291
+
292
+ static VALUE ov_http_request_set_connect_timeout(VALUE self, VALUE value) {
293
+ ov_http_request_object* ptr;
294
+
295
+ ov_http_request_ptr(self, ptr);
296
+ if (!NIL_P(value)) {
297
+ Check_Type(value, T_FIXNUM);
298
+ }
299
+ ptr->connect_timeout = value;
300
+ return Qnil;
301
+ }
302
+
303
+ static VALUE ov_http_request_inspect(VALUE self) {
304
+ ov_http_request_object* ptr;
305
+
306
+ ov_http_request_ptr(self, ptr);
307
+ return rb_sprintf(
308
+ "#<%"PRIsVALUE":%"PRIsVALUE" %"PRIsVALUE">",
309
+ ov_http_request_class,
310
+ ptr->method,
311
+ ptr->url
312
+ );
313
+ }
314
+
315
+ static VALUE ov_http_request_initialize(int argc, VALUE* argv, VALUE self) {
316
+ VALUE opts;
317
+
318
+ /* Check the number of arguments: */
319
+ if (argc > 1) {
320
+ rb_raise(ov_error_class, "Expected at most one argument, 'opts', but received %d", argc);
321
+ }
322
+ opts = argc > 0? argv[0]: Qnil;
323
+ if (NIL_P(opts)) {
324
+ opts = rb_hash_new();
325
+ }
326
+ else {
327
+ Check_Type(opts, T_HASH);
328
+ }
329
+
330
+ /* Get the values of the options: */
331
+ ov_http_request_set_method(self, rb_hash_aref(opts, METHOD_SYMBOL));
332
+ ov_http_request_set_url(self, rb_hash_aref(opts, URL_SYMBOL));
333
+ ov_http_request_set_query(self, rb_hash_aref(opts, QUERY_SYMBOL));
334
+ ov_http_request_set_headers(self, rb_hash_aref(opts, HEADERS_SYMBOL));
335
+ ov_http_request_set_username(self, rb_hash_aref(opts, USERNAME_SYMBOL));
336
+ ov_http_request_set_password(self, rb_hash_aref(opts, PASSWORD_SYMBOL));
337
+ ov_http_request_set_token(self, rb_hash_aref(opts, TOKEN_SYMBOL));
338
+ ov_http_request_set_body(self, rb_hash_aref(opts, BODY_SYMBOL));
339
+ ov_http_request_set_timeout(self, rb_hash_aref(opts, TIMEOUT_SYMBOL));
340
+ ov_http_request_set_connect_timeout(self, rb_hash_aref(opts, CONNECT_TIMEOUT_SYMBOL));
341
+
342
+ return self;
343
+ }
344
+
345
+ void ov_http_request_define(void) {
346
+ /* Define the class: */
347
+ ov_http_request_class = rb_define_class_under(ov_module, "HttpRequest", rb_cData);
348
+
349
+ /* Define the constructor: */
350
+ rb_define_alloc_func(ov_http_request_class, ov_http_request_alloc);
351
+ rb_define_method(ov_http_request_class, "initialize", ov_http_request_initialize, -1);
352
+
353
+ /* Define the methods: */
354
+ rb_define_method(ov_http_request_class, "method", ov_http_request_get_method, 0);
355
+ rb_define_method(ov_http_request_class, "method=", ov_http_request_set_method, 1);
356
+ rb_define_method(ov_http_request_class, "url", ov_http_request_get_url, 0);
357
+ rb_define_method(ov_http_request_class, "url=", ov_http_request_set_url, 1);
358
+ rb_define_method(ov_http_request_class, "query", ov_http_request_get_query, 0);
359
+ rb_define_method(ov_http_request_class, "query=", ov_http_request_set_query, 1);
360
+ rb_define_method(ov_http_request_class, "headers", ov_http_request_get_headers, 0);
361
+ rb_define_method(ov_http_request_class, "headers=", ov_http_request_set_headers, 1);
362
+ rb_define_method(ov_http_request_class, "username", ov_http_request_get_username, 0);
363
+ rb_define_method(ov_http_request_class, "username=", ov_http_request_set_username, 1);
364
+ rb_define_method(ov_http_request_class, "password", ov_http_request_get_password, 0);
365
+ rb_define_method(ov_http_request_class, "password=", ov_http_request_set_password, 1);
366
+ rb_define_method(ov_http_request_class, "token", ov_http_request_get_token, 0);
367
+ rb_define_method(ov_http_request_class, "token=", ov_http_request_set_token, 1);
368
+ rb_define_method(ov_http_request_class, "kerberos", ov_http_request_get_kerberos, 0);
369
+ rb_define_method(ov_http_request_class, "kerberos=", ov_http_request_set_kerberos, 1);
370
+ rb_define_method(ov_http_request_class, "body", ov_http_request_get_body, 0);
371
+ rb_define_method(ov_http_request_class, "body=", ov_http_request_set_body, 1);
372
+ rb_define_method(ov_http_request_class, "timeout", ov_http_request_get_timeout, 0);
373
+ rb_define_method(ov_http_request_class, "timeout=", ov_http_request_set_timeout, 1);
374
+ rb_define_method(ov_http_request_class, "connect_timeout", ov_http_request_get_connect_timeout, 0);
375
+ rb_define_method(ov_http_request_class, "connect_timeout=", ov_http_request_set_connect_timeout, 1);
376
+ rb_define_method(ov_http_request_class, "inspect", ov_http_request_inspect, 0);
377
+ rb_define_method(ov_http_request_class, "to_s", ov_http_request_inspect, 0);
378
+
379
+ /* Define the symbols for the attributes: */
380
+ URL_SYMBOL = ID2SYM(rb_intern("url"));
381
+ METHOD_SYMBOL = ID2SYM(rb_intern("method"));
382
+ QUERY_SYMBOL = ID2SYM(rb_intern("query"));
383
+ HEADERS_SYMBOL = ID2SYM(rb_intern("headers"));
384
+ USERNAME_SYMBOL = ID2SYM(rb_intern("username"));
385
+ PASSWORD_SYMBOL = ID2SYM(rb_intern("password"));
386
+ TOKEN_SYMBOL = ID2SYM(rb_intern("token"));
387
+ KERBEROS_SYMBOL = ID2SYM(rb_intern("kerberos"));
388
+ BODY_SYMBOL = ID2SYM(rb_intern("body"));
389
+ TIMEOUT_SYMBOL = ID2SYM(rb_intern("timeout"));
390
+ CONNECT_TIMEOUT_SYMBOL = ID2SYM(rb_intern("connect_timeout"));
391
+
392
+ /* Define the symbols for the HTTP methods: */
393
+ GET_SYMBOL = ID2SYM(rb_intern("GET"));
394
+ POST_SYMBOL = ID2SYM(rb_intern("POST"));
395
+ PUT_SYMBOL = ID2SYM(rb_intern("PUT"));
396
+ DELETE_SYMBOL = ID2SYM(rb_intern("DELETE"));
397
+ }
@@ -0,0 +1,54 @@
1
+ /*
2
+ Copyright (c) 2016-2017 Red Hat, Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ #ifndef __OV_HTTP_REQUEST_H__
18
+ #define __OV_HTTP_REQUEST_H__
19
+
20
+ #include <ruby.h>
21
+
22
+ /* Data type and class: */
23
+ extern rb_data_type_t ov_http_request_type;
24
+ extern VALUE ov_http_request_class;
25
+
26
+ /* Symbols for HTTP methods: */
27
+ extern VALUE GET_SYMBOL;
28
+ extern VALUE POST_SYMBOL;
29
+ extern VALUE PUT_SYMBOL;
30
+ extern VALUE DELETE_SYMBOL;
31
+
32
+ /* Content: */
33
+ typedef struct {
34
+ VALUE method; /* Symbol */
35
+ VALUE url; /* String */
36
+ VALUE query; /* Hash<String, String> */
37
+ VALUE headers; /* Hash<String, String> */
38
+ VALUE username; /* String */
39
+ VALUE password; /* String */
40
+ VALUE token; /* String */
41
+ VALUE kerberos; /* Boolean */
42
+ VALUE body; /* String */
43
+ VALUE timeout; /* Integer */
44
+ VALUE connect_timeout; /* Integer */
45
+ } ov_http_request_object;
46
+
47
+ /* Macro to get the pointer: */
48
+ #define ov_http_request_ptr(object, ptr) \
49
+ TypedData_Get_Struct((object), ov_http_request_object, &ov_http_request_type, (ptr))
50
+
51
+ /* Initialization function: */
52
+ extern void ov_http_request_define(void);
53
+
54
+ #endif