Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 5672d82

Browse files
author
Yu Wei Wu
committed
feat(server): Add Server name in header
Add "tangle-accelerator" as server name and version to the header. Versioning is also added to config header file. Future releases require update this macro.
1 parent c8ea48f commit 5672d82

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

accelerator/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern "C" {
1212

1313
/** @name tangle-accelerator config */
1414
/** @{ */
15+
#define TA_VERSION "tangle-accelerator/0.3.0"
1516
#define TA_HOST "localhost" /**< Binding address of tangle-acclerator */
1617
#define TA_PORT "8000" /**< Binding port of tangle-acclerator */
1718
#define TA_THREAD_COUNT 10 /**< Thread count of tangle-acclerator instance */

accelerator/errors.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ typedef enum {
9999
/**< Error in mam response */
100100
} status_t;
101101

102+
typedef enum {
103+
HTTP_METHOD_GET = 0, /**< HTTP GET method */
104+
HTTP_METHOD_POST = 1, /**< HTTP POST method */
105+
HTTP_METHOD_OPTIONS = 2, /**< HTTP OPTIONS method */
106+
} http_method_t;
107+
102108
#ifdef __cplusplus
103109
}
104110
#endif

accelerator/server.cc

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
#include "accelerator/errors.h"
77
#include "cJSON.h"
88

9-
void set_options_method_header(served::response& res) {
9+
void set_method_header(served::response& res, http_method_t method) {
10+
res.set_header("Server", TA_VERSION);
1011
res.set_header("Access-Control-Allow-Origin", "*");
11-
res.set_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
12-
res.set_header("Access-Control-Allow-Headers",
13-
"Origin, Content-Type, Accept");
14-
res.set_header("Access-Control-Max-Age", "86400");
12+
13+
switch (method) {
14+
case HTTP_METHOD_OPTIONS:
15+
res.set_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
16+
res.set_header("Access-Control-Allow-Headers",
17+
"Origin, Content-Type, Accept");
18+
res.set_header("Access-Control-Max-Age", "86400");
19+
break;
20+
default:
21+
res.set_header("Content-Type", "application/json");
22+
break;
23+
}
1524
}
1625

1726
status_t set_response_content(status_t ret, char** json_result) {
@@ -53,7 +62,7 @@ int main(int, char const**) {
5362
mux.handle("/mam/{bundle:[A-Z9]{81}}")
5463
.method(served::method::OPTIONS,
5564
[&](served::response& res, const served::request& req) {
56-
set_options_method_header(res);
65+
set_method_header(res, HTTP_METHOD_OPTIONS);
5766
})
5867
.get([&](served::response& res, const served::request& req) {
5968
status_t ret = SC_OK;
@@ -63,8 +72,7 @@ int main(int, char const**) {
6372
&json_result);
6473
ret = set_response_content(ret, &json_result);
6574

66-
res.set_header("Content-Type", "application/json");
67-
res.set_header("Access-Control-Allow-Origin", "*");
75+
set_method_header(res, HTTP_METHOD_GET);
6876
res.set_status(ret);
6977
res << json_result;
7078
});
@@ -79,7 +87,7 @@ int main(int, char const**) {
7987
mux.handle("/tag/{tag:[A-Z9]{1,27}}/hashes")
8088
.method(served::method::OPTIONS,
8189
[&](served::response& res, const served::request& req) {
82-
set_options_method_header(res);
90+
set_method_header(res, HTTP_METHOD_OPTIONS);
8391
})
8492
.get([&](served::response& res, const served::request& req) {
8593
status_t ret = SC_OK;
@@ -88,8 +96,7 @@ int main(int, char const**) {
8896
ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
8997
&json_result);
9098
ret = set_response_content(ret, &json_result);
91-
res.set_header("Content-Type", "application/json");
92-
res.set_header("Access-Control-Allow-Origin", "*");
99+
set_method_header(res, HTTP_METHOD_GET);
93100
res.set_status(ret);
94101
res << json_result;
95102
});
@@ -104,7 +111,7 @@ int main(int, char const**) {
104111
mux.handle("/transaction/{tx:[A-Z9]{81}}")
105112
.method(served::method::OPTIONS,
106113
[&](served::response& res, const served::request& req) {
107-
set_options_method_header(res);
114+
set_method_header(res, HTTP_METHOD_OPTIONS);
108115
})
109116
.get([&](served::response& res, const served::request& req) {
110117
status_t ret = SC_OK;
@@ -113,8 +120,7 @@ int main(int, char const**) {
113120
ret = api_get_transaction_object(&service, req.params["tx"].c_str(),
114121
&json_result);
115122
ret = set_response_content(ret, &json_result);
116-
res.set_header("Content-Type", "application/json");
117-
res.set_header("Access-Control-Allow-Origin", "*");
123+
set_method_header(res, HTTP_METHOD_GET);
118124
res.set_status(ret);
119125
res << json_result;
120126
});
@@ -129,7 +135,7 @@ int main(int, char const**) {
129135
mux.handle("/tag/{tag:[A-Z9]{1,27}}")
130136
.method(served::method::OPTIONS,
131137
[&](served::response& res, const served::request& req) {
132-
set_options_method_header(res);
138+
set_method_header(res, HTTP_METHOD_OPTIONS);
133139
})
134140
.get([&](served::response& res, const served::request& req) {
135141
status_t ret = SC_OK;
@@ -138,8 +144,7 @@ int main(int, char const**) {
138144
ret = api_find_transactions_obj_by_tag(
139145
&service, req.params["tag"].c_str(), &json_result);
140146
ret = set_response_content(ret, &json_result);
141-
res.set_header("Content-Type", "application/json");
142-
res.set_header("Access-Control-Allow-Origin", "*");
147+
set_method_header(res, HTTP_METHOD_GET);
143148
res.set_status(ret);
144149
res << json_result;
145150
});
@@ -152,16 +157,15 @@ int main(int, char const**) {
152157
mux.handle("/tips/pair")
153158
.method(served::method::OPTIONS,
154159
[&](served::response& res, const served::request& req) {
155-
set_options_method_header(res);
160+
set_method_header(res, HTTP_METHOD_OPTIONS);
156161
})
157162
.get([&](served::response& res, const served::request& req) {
158163
status_t ret = SC_OK;
159164
char* json_result;
160165

161166
ret = api_get_tips_pair(&service, &json_result);
162167
ret = set_response_content(ret, &json_result);
163-
res.set_header("Content-Type", "application/json");
164-
res.set_header("Access-Control-Allow-Origin", "*");
168+
set_method_header(res, HTTP_METHOD_GET);
165169
res.set_status(ret);
166170
res << json_result;
167171
});
@@ -174,16 +178,15 @@ int main(int, char const**) {
174178
mux.handle("/tips")
175179
.method(served::method::OPTIONS,
176180
[&](served::response& res, const served::request& req) {
177-
set_options_method_header(res);
181+
set_method_header(res, HTTP_METHOD_OPTIONS);
178182
})
179183
.get([&](served::response& res, const served::request& req) {
180184
status_t ret = SC_OK;
181185
char* json_result;
182186

183187
ret = api_get_tips(&service, &json_result);
184188
ret = set_response_content(ret, &json_result);
185-
res.set_header("Content-Type", "application/json");
186-
res.set_header("Access-Control-Allow-Origin", "*");
189+
set_method_header(res, HTTP_METHOD_GET);
187190
res.set_status(ret);
188191
res << json_result;
189192
});
@@ -196,16 +199,15 @@ int main(int, char const**) {
196199
mux.handle("/address")
197200
.method(served::method::OPTIONS,
198201
[&](served::response& res, const served::request& req) {
199-
set_options_method_header(res);
202+
set_method_header(res, HTTP_METHOD_OPTIONS);
200203
})
201204
.get([&](served::response& res, const served::request& req) {
202205
status_t ret = SC_OK;
203206
char* json_result;
204207

205208
ret = api_generate_address(&service, &json_result);
206209
ret = set_response_content(ret, &json_result);
207-
res.set_header("Content-Type", "application/json");
208-
res.set_header("Access-Control-Allow-Origin", "*");
210+
set_method_header(res, HTTP_METHOD_GET);
209211
res.set_status(ret);
210212
res << json_result;
211213
});
@@ -218,7 +220,7 @@ int main(int, char const**) {
218220
mux.handle("/transaction")
219221
.method(served::method::OPTIONS,
220222
[&](served::response& res, const served::request& req) {
221-
set_options_method_header(res);
223+
set_method_header(res, HTTP_METHOD_OPTIONS);
222224
})
223225
.post([&](served::response& res, const served::request& req) {
224226
status_t ret = SC_OK;
@@ -239,8 +241,7 @@ int main(int, char const**) {
239241
res.set_status(ret);
240242
}
241243

242-
res.set_header("Content-Type", "application/json");
243-
res.set_header("Access-Control-Allow-Origin", "*");
244+
set_method_header(res, HTTP_METHOD_POST);
244245
res << json_result;
245246
});
246247

@@ -253,16 +254,15 @@ int main(int, char const**) {
253254
mux.handle("{*}")
254255
.method(served::method::OPTIONS,
255256
[&](served::response& res, const served::request& req) {
256-
set_options_method_header(res);
257+
set_method_header(res, HTTP_METHOD_OPTIONS);
257258
})
258259
.get([](served::response& res, const served::request&) {
259260
cJSON* json_obj = cJSON_CreateObject();
260261
cJSON_AddStringToObject(json_obj, "message", "Invalid path");
261262
const char* json = cJSON_PrintUnformatted(json_obj);
262263

263264
res.set_status(SC_HTTP_BAD_REQUEST);
264-
res.set_header("Content-Type", "application/json");
265-
res.set_header("Access-Control-Allow-Origin", "*");
265+
set_method_header(res, HTTP_METHOD_GET);
266266
res << json;
267267

268268
cJSON_Delete(json_obj);

0 commit comments

Comments
 (0)