6
6
#include " accelerator/errors.h"
7
7
#include " cJSON.h"
8
8
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);
10
11
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
+ }
15
24
}
16
25
17
26
status_t set_response_content (status_t ret, char ** json_result) {
@@ -53,7 +62,7 @@ int main(int, char const**) {
53
62
mux.handle (" /mam/{bundle:[A-Z9]{81}}" )
54
63
.method (served::method::OPTIONS,
55
64
[&](served::response& res, const served::request& req) {
56
- set_options_method_header (res);
65
+ set_method_header (res, HTTP_METHOD_OPTIONS );
57
66
})
58
67
.get ([&](served::response& res, const served::request& req) {
59
68
status_t ret = SC_OK;
@@ -63,8 +72,7 @@ int main(int, char const**) {
63
72
&json_result);
64
73
ret = set_response_content (ret, &json_result);
65
74
66
- res.set_header (" Content-Type" , " application/json" );
67
- res.set_header (" Access-Control-Allow-Origin" , " *" );
75
+ set_method_header (res, HTTP_METHOD_GET);
68
76
res.set_status (ret);
69
77
res << json_result;
70
78
});
@@ -79,7 +87,7 @@ int main(int, char const**) {
79
87
mux.handle (" /tag/{tag:[A-Z9]{1,27}}/hashes" )
80
88
.method (served::method::OPTIONS,
81
89
[&](served::response& res, const served::request& req) {
82
- set_options_method_header (res);
90
+ set_method_header (res, HTTP_METHOD_OPTIONS );
83
91
})
84
92
.get ([&](served::response& res, const served::request& req) {
85
93
status_t ret = SC_OK;
@@ -88,8 +96,7 @@ int main(int, char const**) {
88
96
ret = api_find_transactions_by_tag (&service, req.params [" tag" ].c_str (),
89
97
&json_result);
90
98
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);
93
100
res.set_status (ret);
94
101
res << json_result;
95
102
});
@@ -104,7 +111,7 @@ int main(int, char const**) {
104
111
mux.handle (" /transaction/{tx:[A-Z9]{81}}" )
105
112
.method (served::method::OPTIONS,
106
113
[&](served::response& res, const served::request& req) {
107
- set_options_method_header (res);
114
+ set_method_header (res, HTTP_METHOD_OPTIONS );
108
115
})
109
116
.get ([&](served::response& res, const served::request& req) {
110
117
status_t ret = SC_OK;
@@ -113,8 +120,7 @@ int main(int, char const**) {
113
120
ret = api_get_transaction_object (&service, req.params [" tx" ].c_str (),
114
121
&json_result);
115
122
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);
118
124
res.set_status (ret);
119
125
res << json_result;
120
126
});
@@ -129,7 +135,7 @@ int main(int, char const**) {
129
135
mux.handle (" /tag/{tag:[A-Z9]{1,27}}" )
130
136
.method (served::method::OPTIONS,
131
137
[&](served::response& res, const served::request& req) {
132
- set_options_method_header (res);
138
+ set_method_header (res, HTTP_METHOD_OPTIONS );
133
139
})
134
140
.get ([&](served::response& res, const served::request& req) {
135
141
status_t ret = SC_OK;
@@ -138,8 +144,7 @@ int main(int, char const**) {
138
144
ret = api_find_transactions_obj_by_tag (
139
145
&service, req.params [" tag" ].c_str (), &json_result);
140
146
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);
143
148
res.set_status (ret);
144
149
res << json_result;
145
150
});
@@ -152,16 +157,15 @@ int main(int, char const**) {
152
157
mux.handle (" /tips/pair" )
153
158
.method (served::method::OPTIONS,
154
159
[&](served::response& res, const served::request& req) {
155
- set_options_method_header (res);
160
+ set_method_header (res, HTTP_METHOD_OPTIONS );
156
161
})
157
162
.get ([&](served::response& res, const served::request& req) {
158
163
status_t ret = SC_OK;
159
164
char * json_result;
160
165
161
166
ret = api_get_tips_pair (&service, &json_result);
162
167
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);
165
169
res.set_status (ret);
166
170
res << json_result;
167
171
});
@@ -174,16 +178,15 @@ int main(int, char const**) {
174
178
mux.handle (" /tips" )
175
179
.method (served::method::OPTIONS,
176
180
[&](served::response& res, const served::request& req) {
177
- set_options_method_header (res);
181
+ set_method_header (res, HTTP_METHOD_OPTIONS );
178
182
})
179
183
.get ([&](served::response& res, const served::request& req) {
180
184
status_t ret = SC_OK;
181
185
char * json_result;
182
186
183
187
ret = api_get_tips (&service, &json_result);
184
188
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);
187
190
res.set_status (ret);
188
191
res << json_result;
189
192
});
@@ -196,16 +199,15 @@ int main(int, char const**) {
196
199
mux.handle (" /address" )
197
200
.method (served::method::OPTIONS,
198
201
[&](served::response& res, const served::request& req) {
199
- set_options_method_header (res);
202
+ set_method_header (res, HTTP_METHOD_OPTIONS );
200
203
})
201
204
.get ([&](served::response& res, const served::request& req) {
202
205
status_t ret = SC_OK;
203
206
char * json_result;
204
207
205
208
ret = api_generate_address (&service, &json_result);
206
209
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);
209
211
res.set_status (ret);
210
212
res << json_result;
211
213
});
@@ -218,7 +220,7 @@ int main(int, char const**) {
218
220
mux.handle (" /transaction" )
219
221
.method (served::method::OPTIONS,
220
222
[&](served::response& res, const served::request& req) {
221
- set_options_method_header (res);
223
+ set_method_header (res, HTTP_METHOD_OPTIONS );
222
224
})
223
225
.post ([&](served::response& res, const served::request& req) {
224
226
status_t ret = SC_OK;
@@ -239,8 +241,7 @@ int main(int, char const**) {
239
241
res.set_status (ret);
240
242
}
241
243
242
- res.set_header (" Content-Type" , " application/json" );
243
- res.set_header (" Access-Control-Allow-Origin" , " *" );
244
+ set_method_header (res, HTTP_METHOD_POST);
244
245
res << json_result;
245
246
});
246
247
@@ -253,16 +254,15 @@ int main(int, char const**) {
253
254
mux.handle (" {*}" )
254
255
.method (served::method::OPTIONS,
255
256
[&](served::response& res, const served::request& req) {
256
- set_options_method_header (res);
257
+ set_method_header (res, HTTP_METHOD_OPTIONS );
257
258
})
258
259
.get ([](served::response& res, const served::request&) {
259
260
cJSON* json_obj = cJSON_CreateObject ();
260
261
cJSON_AddStringToObject (json_obj, " message" , " Invalid path" );
261
262
const char * json = cJSON_PrintUnformatted (json_obj);
262
263
263
264
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);
266
266
res << json;
267
267
268
268
cJSON_Delete (json_obj);
0 commit comments