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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/tech/microservices/webserver_service.h"
#include "gate/results.h"
#include "gate/net/httpservers.h"
#include "gate/times.h"
#define WEBSERVER_CONFIG_NAME GATE_STRUCT_ROOT_NAME GATE_STRUCT_SEPARATOR WEBSERVER_ROOT_NAME GATE_STRUCT_SEPARATOR "config"
#define WEBSERVER_CONFIG_VDIR_NAME WEBSERVER_CONFIG_NAME GATE_STRUCT_SEPARATOR "vdir"
#define WEBSERVER_CONFIG_HTTPHANDLER_NAME WEBSERVER_CONFIG_NAME GATE_STRUCT_SEPARATOR "httphandler"
static gate_struct_item_t const webservice_request_members[] =
{
GATE_STRUCT_ITEM(gate_microservice_webservice_request_t, GATE_TYPE_STRING, verb),
GATE_STRUCT_ITEM(gate_microservice_webservice_request_t, GATE_TYPE_STRING, path),
GATE_STRUCT_ITEM(gate_microservice_webservice_request_t, GATE_TYPE_BLOB, content),
GATE_STRUCT_ITEM(gate_microservice_webservice_request_t, GATE_TYPE_STRING, content_type),
};
static gate_struct_descriptor_t const webservice_request_descriptor =
{
WEBSERVER_WEBSERVICE_REQUEST,
webservice_request_members,
sizeof(webservice_request_members) / sizeof(webservice_request_members[0]),
sizeof(gate_microservice_webservice_request_t)
};
gate_result_t gate_microservice_webservice_request_ctor(void* target, void const* src)
{
gate_result_t ret;
gate_microservice_webservice_request_t const* ptr_src = (gate_microservice_webservice_request_t const*)src;
gate_microservice_webservice_request_t* ptr_dst = (gate_microservice_webservice_request_t*)target;
if (src == NULL)
{
ret = gate_struct_init(&ptr_dst->struct_base, &webservice_request_descriptor);
}
else
{
gate_mem_clear(ptr_dst, sizeof(gate_microservice_webservice_request_t));
ret = gate_struct_copy(&ptr_dst->struct_base, &ptr_src->struct_base);
}
return ret;
}
static gate_struct_item_t const webservice_response_members[] =
{
GATE_STRUCT_ITEM(gate_microservice_webservice_response_t, GATE_TYPE_UI32, status_code),
GATE_STRUCT_ITEM(gate_microservice_webservice_response_t, GATE_TYPE_BLOB, content),
GATE_STRUCT_ITEM(gate_microservice_webservice_response_t, GATE_TYPE_STRING, content_type),
};
static gate_struct_descriptor_t const webservice_response_descriptor =
{
WEBSERVER_WEBSERVICE_RESPONSE,
webservice_response_members,
sizeof(webservice_response_members) / sizeof(webservice_response_members[0]),
sizeof(gate_microservice_webservice_response_t)
};
gate_result_t gate_microservice_webservice_response_ctor(void* target, void const* src)
{
gate_result_t ret;
gate_microservice_webservice_response_t const* ptr_src = (gate_microservice_webservice_response_t const*)src;
gate_microservice_webservice_response_t* ptr_dst = (gate_microservice_webservice_response_t*)target;
if (src == NULL)
{
ret = gate_struct_init(&ptr_dst->struct_base, &webservice_response_descriptor);
}
else
{
gate_mem_clear(ptr_dst, sizeof(gate_microservice_webservice_response_t));
ret = gate_struct_copy(&ptr_dst->struct_base, &ptr_src->struct_base);
}
return ret;
}
typedef struct webserver_config_vdir_class
{
gate_struct_t struct_base;
gate_string_t webpath;
gate_string_t filepath;
gate_bool_t webdav;
} webserver_config_vdir_t;
static gate_struct_item_t const webserver_config_vdir_members[] =
{
GATE_STRUCT_ITEM(webserver_config_vdir_t, GATE_TYPE_STRING, webpath),
GATE_STRUCT_ITEM(webserver_config_vdir_t, GATE_TYPE_STRING, filepath),
GATE_STRUCT_ITEM(webserver_config_vdir_t, GATE_TYPE_BOOL, webdav),
};
static gate_struct_descriptor_t const webserver_config_vdir_descriptor =
{
WEBSERVER_CONFIG_VDIR_NAME,
webserver_config_vdir_members,
sizeof(webserver_config_vdir_members) / sizeof(webserver_config_vdir_members[0]),
sizeof(webserver_config_vdir_t)
};
static gate_result_t webserver_config_vdir_ctor(void* target, void const* src)
{
gate_result_t ret;
webserver_config_vdir_t const* ptr_src = (webserver_config_vdir_t const*)src;
webserver_config_vdir_t* ptr_dst = (webserver_config_vdir_t*)target;
if (src == NULL)
{
ret = gate_struct_init(&ptr_dst->struct_base, &webserver_config_vdir_descriptor);
}
else
{
gate_mem_clear(ptr_dst, sizeof(webserver_config_vdir_t));
ret = gate_struct_copy(&ptr_dst->struct_base, &ptr_src->struct_base);
}
return ret;
}
typedef struct webserver_config_httphandler_class
{
gate_struct_t struct_base;
gate_string_t webpath;
gate_string_t handlername;
} webserver_config_httphandler_t;
static gate_struct_item_t const webserver_config_httphandler_members[] =
{
GATE_STRUCT_ITEM(webserver_config_httphandler_t, GATE_TYPE_STRING, webpath),
GATE_STRUCT_ITEM(webserver_config_httphandler_t, GATE_TYPE_STRING, handlername),
};
static gate_struct_descriptor_t const webserver_config_httphandler_descriptor =
{
WEBSERVER_CONFIG_HTTPHANDLER_NAME,
webserver_config_httphandler_members,
sizeof(webserver_config_httphandler_members) / sizeof(webserver_config_httphandler_members[0]),
sizeof(webserver_config_httphandler_t)
};
static gate_result_t webserver_config_httphandler_ctor(void* target, void const* src)
{
gate_result_t ret;
webserver_config_httphandler_t const* ptr_src = (webserver_config_httphandler_t const*)src;
webserver_config_httphandler_t* ptr_dst = (webserver_config_httphandler_t*)target;
if (src == NULL)
{
ret = gate_struct_init(&ptr_dst->struct_base, &webserver_config_httphandler_descriptor);
}
else
{
gate_mem_clear(ptr_dst, sizeof(webserver_config_httphandler_t));
ret = gate_struct_copy(&ptr_dst->struct_base, &ptr_src->struct_base);
}
return ret;
}
typedef struct webserver_config_webservice_class
{
gate_struct_t struct_base;
gate_string_t webpath;
gate_string_t service_address;
gate_string_t service_method;
} webserver_config_webservice_t;
static gate_struct_item_t const webserver_config_webservice_members[] =
{
GATE_STRUCT_ITEM(webserver_config_webservice_t, GATE_TYPE_STRING, webpath),
GATE_STRUCT_ITEM(webserver_config_webservice_t, GATE_TYPE_STRING, service_address),
GATE_STRUCT_ITEM(webserver_config_webservice_t, GATE_TYPE_STRING, service_method),
};
static gate_struct_descriptor_t const webserver_config_webservice_descriptor =
{
WEBSERVER_CONFIG_VDIR_NAME,
webserver_config_webservice_members,
sizeof(webserver_config_webservice_members) / sizeof(webserver_config_webservice_members[0]),
sizeof(webserver_config_webservice_t)
};
static gate_result_t webserver_config_webservice_ctor(void* target, void const* src)
{
gate_result_t ret;
webserver_config_webservice_t const* ptr_src = (webserver_config_webservice_t const*)src;
webserver_config_webservice_t* ptr_dst = (webserver_config_webservice_t*)target;
if (src == NULL)
{
ret = gate_struct_init(&ptr_dst->struct_base, &webserver_config_webservice_descriptor);
}
else
{
gate_mem_clear(ptr_dst, sizeof(webserver_config_webservice_t));
ret = gate_struct_copy(&ptr_dst->struct_base, &ptr_src->struct_base);
}
return ret;
}
typedef struct webserver_config_class
{
gate_struct_t struct_base;
gate_arraylist_t http;
gate_arraylist_t https;
gate_uint32_t idletimeout;
gate_arraylist_t vdirs;
gate_arraylist_t httphandlers;
gate_arraylist_t webservices;
} webserver_config_t;
static gate_struct_item_t const webserver_config_members[] =
{
GATE_STRUCT_ITEM(webserver_config_t, GATE_TYPE_ARRAYLIST_STRING, http),
GATE_STRUCT_ITEM(webserver_config_t, GATE_TYPE_ARRAYLIST_STRING, https),
GATE_STRUCT_ITEM(webserver_config_t, GATE_TYPE_UI32, idletimeout),
GATE_STRUCT_ITEM(webserver_config_t, GATE_TYPE_ARRAYLIST_STRUCT, vdirs),
GATE_STRUCT_ITEM(webserver_config_t, GATE_TYPE_ARRAYLIST_STRUCT, httphandlers),
GATE_STRUCT_ITEM(webserver_config_t, GATE_TYPE_ARRAYLIST_STRUCT, webservices)
};
static gate_struct_descriptor_t const webserver_config_descriptor =
{
WEBSERVER_CONFIG_NAME,
webserver_config_members,
sizeof(webserver_config_members) / sizeof(webserver_config_members[0]),
sizeof(webserver_config_t)
};
static gate_result_t webserver_config_ctor(void* target, void const* src)
{
gate_result_t ret;
webserver_config_t const* ptr_src = (webserver_config_t const*)src;
webserver_config_t* ptr_target = (webserver_config_t*)target;
do
{
gate_mem_clear(ptr_target, sizeof(webserver_config_t));
if (src)
{
ret = gate_struct_copy(&ptr_target->struct_base, &ptr_src->struct_base);
}
else
{
ret = gate_struct_init(&ptr_target->struct_base, &webserver_config_descriptor);
GATE_BREAK_IF_FAILED(ret);
ptr_target->vdirs = gate_arraylist_create(sizeof(webserver_config_vdir_t), NULL, 0,
&webserver_config_vdir_ctor, &gate_struct_destructor);
if (NULL == ptr_target->vdirs)
{
gate_struct_destructor(ptr_target);
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ptr_target->httphandlers = gate_arraylist_create(sizeof(webserver_config_httphandler_t), NULL, 0,
&webserver_config_httphandler_ctor, &gate_struct_destructor);
if (NULL == ptr_target->httphandlers)
{
gate_struct_destructor(ptr_target);
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ptr_target->webservices = gate_arraylist_create(sizeof(webserver_config_webservice_t), NULL, 0,
&webserver_config_webservice_ctor, &gate_struct_destructor);
if (NULL == ptr_target->webservices)
{
gate_struct_destructor(ptr_target);
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
} while (0);
return ret;
}
typedef struct webserver_data_class
{
gate_httpserver_t server;
gate_httpserver_host_id_t hosts[64];
gate_size_t hosts_usesd;
gate_httpserver_filehandler_config_t file_handlers[16];
gate_size_t file_handlers_used;
gate_httpserver_handler_t* http_handlers[64];
gate_size_t http_handlers_used;
gate_httpserver_filehandler_config_t service_handlers[16];
gate_size_t service_handlers_used;
webserver_config_t config;
} gate_webserver_data_t;
static gate_string_t* create_webservice_endpoint(gate_string_t const* ptr_address, gate_string_t const* ptr_method, gate_string_t* ptr_endpoint)
{
gate_string_t* ret = NULL;
gate_strbuilder_t builder;
gate_strbuilder_create(&builder, gate_string_length(ptr_address) + gate_string_length(ptr_method) + 4);
gate_strbuilder_append_string(&builder, ptr_address);
gate_strbuilder_append_chars(&builder, 1, '/');
gate_strbuilder_append_string(&builder, ptr_method);
ret = gate_strbuilder_to_string(&builder, ptr_endpoint);
gate_strbuilder_release(&builder);
return ret;
}
static gate_bool_t parse_service_endpoint(gate_string_t const* endpoint, gate_string_t* service_address, gate_string_t* service_endpoint)
{
gate_size_t pos;
pos = gate_string_char_pos_last(endpoint, '/');
if (pos != GATE_STR_NPOS)
{
gate_string_substr(service_address, endpoint, 0, pos);
gate_string_substr(service_endpoint, endpoint, pos + 1, GATE_STR_NPOS);
return true;
}
return false;
}
static gate_result_t webserver_on_request(gate_httpserver_t* server,
gate_httpserver_host_id_t host_id,
gate_http_request_t const* request,
gate_httpserver_response_t* response,
void* userparam)
{
gate_result_t ret = GATE_RESULT_NOMATCH;
gate_microservice_base_t* ptr_service = (gate_microservice_base_t*)userparam;
gate_webserver_data_t* ptr_data = (gate_webserver_data_t*)ptr_service->data_ptr;
gate_size_t ndx;
gate_string_t service_address = GATE_STRING_INIT_EMPTY;
gate_string_t service_method = GATE_STRING_INIT_EMPTY;
gate_microservice_webservice_request_t webrequest;
gate_microservice_webservice_response_t webresponse;
GATE_UNUSED_ARG(server);
do
{
/* check if we have a file-handler for the given request */
for (ndx = 0; ndx != ptr_data->file_handlers_used; ++ndx)
{
ret = gate_httpserver_filehandler_process(&ptr_data->file_handlers[ndx], request, response);
if (GATE_SUCCEEDED(ret))
{
break;
}
}
if (GATE_SUCCEEDED(ret))
{
/* file-handler has processed the request, we are done */
break;
}
for (ndx = 0; ndx != ptr_data->service_handlers_used; ++ndx)
{
if (gate_string_starts_with(&request->path, &ptr_data->service_handlers[ndx].http_request_root_path))
{
if (parse_service_endpoint(&ptr_data->service_handlers[ndx].filesystem_root_path, &service_address, &service_method))
{
do
{
ret = gate_microservice_webservice_request_ctor(&webrequest, NULL);
GATE_BREAK_IF_FAILED(ret);
ret = gate_microservice_webservice_response_ctor(&webresponse, NULL);
if (GATE_FAILED(ret))
{
gate_struct_release(&webrequest.struct_base);
break;
}
ret = gate_microhost_remote_invoke(ptr_service->host, &service_address, &service_method, &webrequest.struct_base, &webresponse.struct_base);
gate_httpserver_response_set_status(response, 200);
gate_httpserver_response_send_headers(response);
gate_stream_write(response, (char const*)webresponse.content.data, webresponse.content.length, NULL);
gate_stream_flush(response);
gate_struct_release(&webresponse.struct_base);
gate_struct_release(&webrequest.struct_base);
} while (0);
gate_string_release(&service_address);
gate_string_release(&service_method);
}
else
{
ret = GATE_RESULT_FAILED;
}
break;
}
}
if (GATE_SUCCEEDED(ret))
{
/* file-handler has processed the request, we are done */
break;
}
} while (0);
if (GATE_FAILED(ret))
{
if (!gate_httpserver_response_are_headers_sent(response))
{
gate_httpserver_response_set_status(response, ret == GATE_RESULT_NOMATCH ? 404 : 501);
gate_httpserver_response_send_headers(response);
}
gate_stream_flush(response);
ret = GATE_RESULT_OK;
}
return ret;
}
static void release_all_file_handler_configs(gate_httpserver_filehandler_config_t* cfgs, gate_size_t* count)
{
gate_size_t ndx;
for (ndx = 0; ndx != *count; ++ndx)
{
gate_httpserver_filehandler_config_release(&cfgs[ndx]);
}
*count = 0;
}
static gate_result_t webserver_on_start(gate_microservice_base_t* self)
{
static gate_string_t const msg_add_host_failed = GATE_STRING_INIT_STATIC("Failed to add host");
static gate_string_t const msg_add_host_success = GATE_STRING_INIT_STATIC("Webservice host successfully added");
static gate_string_t const msg_no_hosts = GATE_STRING_INIT_STATIC("Webservice contains no configured hosts");
static gate_string_t const msg_http_start_failed = GATE_STRING_INIT_STATIC("Failed to start webservice");
static gate_string_t const msg_http_start_succeeded = GATE_STRING_INIT_STATIC("Webservice successfully started");
gate_result_t ret = GATE_RESULT_FAILED;
gate_webserver_data_t* ptr_data = (gate_webserver_data_t*)self->data_ptr;
gate_httpserver_t* server = &ptr_data->server;
gate_httpserver_config_t http_config;
gate_httpserver_host_id_t http_host_id;
gate_size_t ndx;
gate_size_t count;
gate_string_t const* ptr_str;
webserver_config_vdir_t const* ptr_vdir;
webserver_config_webservice_t const* ptr_ws;
gate_size_t hosts_succeeded = 0;
gate_int16_t socket_family;
gate_string_t webservice_endpoint;
do
{
gate_microservice_base_set_condition_bits(self, GATE_MICROSERVICE_CONDITION_INITIALIZING);
/* export parameters from property storage to native config struct */
gate_property_export(&self->parameters, GATE_TYPE_STRUCT, &ptr_data->config.struct_base);
// load HTTP server bindings
count = gate_arraylist_length(ptr_data->config.http);
ret = GATE_RESULT_OK;<--- Variable 'ret' is reassigned a value before the old one has been used.
for (ndx = 0; ndx != count; ++ndx)
{
ptr_str = (gate_string_t const*)gate_arraylist_get(ptr_data->config.http, ndx);
gate_httpserver_config_init(&http_config);
ret = gate_socket_parse_address(ptr_str, &http_config.address, &http_config.port, &socket_family);
GATE_BREAK_IF_FAILED(ret);
http_config.max_connections = 32;
http_config.secure = false;
ret = gate_httpserver_add_host(server, &http_config, &webserver_on_request, self, &http_host_id);
gate_httpserver_config_destroy(&http_config);
if (GATE_SUCCEEDED(ret))
{
gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_INFO, 0, 0, &self->address, &msg_add_host_success);
++hosts_succeeded;
}
else
{
gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_WARNING, ret, 0, &self->address, &msg_add_host_failed);
}
}
if (hosts_succeeded == 0)
{
gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_WARNING, 0, 0, &self->address, &msg_no_hosts);
}
// load file-services configuration
count = gate_arraylist_length(ptr_data->config.vdirs);
ptr_data->file_handlers_used = 0;
for (ndx = 0; ndx != count; ++ndx)
{
ptr_vdir = (webserver_config_vdir_t const*)gate_arraylist_get(ptr_data->config.vdirs, ndx);
if (!ptr_vdir)
{
continue;
}
ret = gate_httpserver_filehandler_config_init(&ptr_data->file_handlers[ptr_data->file_handlers_used],
&ptr_vdir->webpath,
&ptr_vdir->filepath,
ptr_vdir->webdav);
if (GATE_SUCCEEDED(ret))
{
gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_INFO, 0, 0, &self->address, &ptr_vdir->webpath);
++ptr_data->file_handlers_used;
}
else
{
gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_WARNING, ret, 0, &self->address, &ptr_vdir->webpath);
}
}
// load http-handlers
/*
count = gate_arraylist_length(ptr_data->config.httphandlers);
ptr_data->http_handlers_used = 0;
for(ndx = 0; ndx != count; ++ndx)
{
webserver_config_httphandler_t const* const ptr_handler = (webserver_config_httphandler_t const*)(ptr_data->config.httphandlers, ndx);
if(!ptr_handler)
{
continue;
}
ret
}
*/
count = gate_arraylist_length(ptr_data->config.webservices);
ptr_data->service_handlers_used = 0;
for (ndx = 0; ndx != count; ++ndx)
{
ptr_ws = (webserver_config_webservice_t const*)gate_arraylist_get(ptr_data->config.webservices, ndx);
if (!ptr_ws)
{
continue;
}
if (NULL == create_webservice_endpoint(&ptr_ws->service_address, &ptr_ws->service_method, &webservice_endpoint))
{
continue;
}
ret = gate_httpserver_filehandler_config_init(
&ptr_data->service_handlers[ptr_data->service_handlers_used],
&ptr_ws->webpath,
&webservice_endpoint,
false);
if (GATE_SUCCEEDED(ret))
{
++ptr_data->service_handlers_used;
}
gate_string_release(&webservice_endpoint);
}
ret = gate_httpserver_start(server);<--- Variable 'ret' is reassigned a value before the old one has been used.
if (GATE_SUCCEEDED(ret))
{
gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_INFO, ret, 0, &self->address, &msg_http_start_succeeded);
count = gate_arraylist_length(ptr_data->config.http);
gate_microservice_base_set_condition_bits(self, (count == hosts_succeeded)
? GATE_MICROSERVICE_CONDITION_FULL_OPERATIONAL
: GATE_MICROSERVICE_CONDITION_PARTIAL_OPERATIONAL
);
}
else
{
gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_ERROR, ret, 0, &self->address, &msg_http_start_failed);
gate_microservice_base_set_condition_bits(self, GATE_MICROSERVICE_CONDITION_INIT_ERROR);
}
GATE_BREAK_IF_FAILED(ret);
} while (0);
gate_microservice_base_clear_condition_bits(self, GATE_MICROSERVICE_CONDITION_INITIALIZING);
if (GATE_FAILED(ret))
{
release_all_file_handler_configs(&ptr_data->file_handlers[0], &ptr_data->file_handlers_used);
}
return ret;
}
static gate_result_t webserver_on_stop(gate_microservice_base_t* self)
{
gate_result_t ret = GATE_RESULT_OK;
gate_webserver_data_t* webserver_data = (gate_webserver_data_t*)self->data_ptr;
do
{
gate_httpserver_remove_all_hosts(&webserver_data->server);
gate_httpserver_stop(&webserver_data->server);
release_all_file_handler_configs(&webserver_data->file_handlers[0], &webserver_data->file_handlers_used);
release_all_file_handler_configs(&webserver_data->service_handlers[0], &webserver_data->service_handlers_used);
} while (0);
return ret;
}
static void webserver_on_release(gate_microservice_base_t* self)
{
gate_webserver_data_t* ptr_data = (gate_webserver_data_t*)self->data_ptr;
webserver_on_stop(self);
gate_httpserver_destroy(&ptr_data->server);
gate_struct_release(&ptr_data->config.struct_base);
}
static gate_result_t webserver_on_message_received(gate_microservice_base_t* self,
gate_string_t const* source, gate_string_t const* destination,
gate_string_t const* msg_id, gate_string_t const* message)
{
gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
GATE_UNUSED_ARG(self);
GATE_UNUSED_ARG(source);
GATE_UNUSED_ARG(destination);
GATE_UNUSED_ARG(msg_id);
GATE_UNUSED_ARG(message);
return ret;
}
static gate_result_t webserver_on_object_received(gate_microservice_base_t* self,
gate_string_t const* source, gate_string_t const* destination,
gate_string_t const* obj_id, gate_object_t* obj)
{
gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
GATE_UNUSED_ARG(self);
GATE_UNUSED_ARG(source);
GATE_UNUSED_ARG(destination);
GATE_UNUSED_ARG(obj_id);
GATE_UNUSED_ARG(obj);
return ret;
}
static gate_result_t webserver_on_invoke(gate_microservice_base_t* self, gate_string_t const* method,
gate_struct_t const* request, gate_struct_t* response)
{
gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
GATE_UNUSED_ARG(self);
GATE_UNUSED_ARG(method);
GATE_UNUSED_ARG(request);
GATE_UNUSED_ARG(response);
return ret;
}
gate_microservice_t* gate_microservice_webserver_create()
{
gate_microservice_t* ret = NULL;
gate_microservice_base_t* webserver_service = NULL;
gate_webserver_data_t* webserver_data = NULL;
gate_result_t result;
do
{
webserver_service = gate_microservice_base_create(sizeof(gate_webserver_data_t), NULL);
if (NULL == webserver_service)
{
break;
}
webserver_service->on_release = &webserver_on_release;
webserver_service->on_start = &webserver_on_start;
webserver_service->on_stop = &webserver_on_stop;
webserver_service->on_message_received = &webserver_on_message_received;
webserver_service->on_object_received = &webserver_on_object_received;
webserver_service->on_invoke = &webserver_on_invoke;
/* init webserver internal data */
webserver_data = (gate_webserver_data_t*)webserver_service->data_ptr;
webserver_config_ctor(&webserver_data->config.struct_base, NULL);
gate_property_create_object(&webserver_service->parameters);
gate_property_import(&webserver_service->parameters, GATE_TYPE_STRUCT, &webserver_data->config.struct_base);
webserver_service->ptr_parameters = &webserver_data->config.struct_base;
result = gate_httpserver_create(&webserver_data->server);
GATE_BREAK_IF_FAILED(result);
ret = (gate_microservice_t*)webserver_service;
webserver_service = NULL;
} while (0);
if (webserver_service != NULL)
{
gate_object_release(webserver_service);
}
return ret;
}
|