forked from microsoft/cpprestsdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP_FULL_SERVER.cpp
More file actions
384 lines (321 loc) · 10.4 KB
/
HTTP_FULL_SERVER.cpp
File metadata and controls
384 lines (321 loc) · 10.4 KB
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
#include "http_svr.h"
// https://mariusbancila.ro/blog/2013/08/19/full-fledged-client-server-example-with-cpprest-sdk-110/
//看这个 https://mariusbancila.ro/blog/2017/11/19/revisited-full-fledged-client-server-example-with-c-rest-sdk-2-10/
namespace full_svr
{
#if 0
void display_json(json::value const& jvalue, utility::string_t const& prefix)
{
std::wcout << prefix << jvalue.serialize() << std::endl;
}
/*
n this simple implementation the dictionary is a std::map.
Its content is not persisted to disk, it is reloaded each time the server starts.
*/
std::map<utility::string_t, utility::string_t> dictionary;
//http://cn.voidcc.com/question/p-uoiiighz-boe.html
typedef std::vector<std::pair<utility::string_t, json::value>> field_map;
/*
Let’s now look at the handlers. As mentioned earlier the GET method is a bit different than the others. A GET request
should return all the key-value pairs in the server’s dictionary. Its implementation looks like this:
handlers implementation */
void handle_get(http_request request)
{
TRACE(L"\nhandle GET\n");
//是不是库里没有的
// json::value::field_map answer;
field_map answer;
for (auto const& p : dictionary)
{
// answer.push_back(std::make_pair(json::value(p.first), json::value(p.second)));
answer.push_back(std::make_pair(p.first, json::value(p.second)));
}
//http://cn.voidcc.com/question/p-uoiiighz-boe.html
request.reply(status_codes::OK, json::value::object(answer));
}
void handle_request(http_request request, std::function<void(json::value&, field_map&)> action)
{
field_map answer;
request.extract_json()
.then([&answer, &action](pplx::task<json::value> task) {
try
{
//这里会有报错
auto& const jvalue = task.get();
if (!jvalue.is_null())
{
action(jvalue, answer);
}
}
catch (http_exception const& e)
{
std::wcout << e.what() << std::endl;
}
})
.wait();
request.reply(status_codes::OK, json::value::object(answer));
}
void handle_post(http_request request)
{
TRACE("\nhandle POST\n");
handle_request(request, [](json::value& jvalue, field_map& answer) {
//数组 遍历
for (auto const& e : jvalue.as_array())
{
//数组中的每一项
if (e.is_string())
{
auto key = e.as_string();
//在服务端的字典里里找这个key,生成answer
auto pos = dictionary.find(key);
if (pos == dictionary.end())
{
answer.push_back(make_pair(key, json::value(L"<nil>")));
}
else
{
answer.push_back(make_pair(pos->first, json::value(pos->second)));
}
}
}
});
}
void handle_put(http_request request)
{
TRACE("\nhandle PUT\n");
handle_request(request, [](json::value& jvalue, field_map& answer) {
for (auto const& e : jvalue.as_object())
{
if (e.second.is_string())
{
auto key = e.first;
auto value = e.second.as_string();
if (dictionary.find(key) == dictionary.end())
{
TRACE_ACTION(L"added", key, value);
answer.push_back(make_pair(key, json::value(L"<put>")));
}
else
{
TRACE_ACTION(L"updated", key, value);
answer.push_back(make_pair(key, json::value(L"<updated>")));
}
dictionary[key] = value;
}
}
});
}
void handle_del(http_request request)
{
TRACE("\nhandle DEL\n");
//调用handle_request,传入lambda表达式 std::function<void(json::value&, field_map&)> action
handle_request(request, [](json::value& jvalue, field_map& answer) {
std::set<utility::string_t> keys;
for (auto const& e : jvalue.as_array())
{
if (e.is_string())
{
auto key = e.as_string();
auto pos = dictionary.find(key);
if (pos == dictionary.end())
{
answer.push_back(make_pair(key, json::value(L"<failed>")));
}
else
{
TRACE_ACTION(L"deleted", pos->first, pos->second);
answer.push_back(make_pair(key, json::value(L"<deleted>")));
keys.insert(key);
}
}
}
for (auto const& key : keys)
dictionary.erase(key);
});
}
int svr_main(int argc, char** argv)
{
http_listener listener(L"http://localhost/restdemo");
listener.support(methods::GET, handle_get);
listener.support(methods::POST, handle_post);
listener.support(methods::PUT, handle_put);
listener.support(methods::DEL, handle_del);
try
{
listener.open().then([&listener]() { TRACE(L"\nstarting to listen\n"); }).wait();
while (true)
;
}
catch (std::exception const& e)
{
std::wcout << e.what() << std::endl;
}
return 0;
}
#else /// https://mariusbancila.ro/blog/2017/11/19/revisited-full-fledged-client-server-example-with-c-rest-sdk-2-10/
/*
I will not reiterate all the details described in the former article. However, in summary, the server maintains a
dictionary of values (both keys and values are strings). Through HTTP calls a client can retrieve the content of the
dictionary, add new values, update or delete existing ones.
客户端主要是对服务端的dictionary做读取/修改
*/
std::map<utility::string_t, utility::string_t> dictionary;
void display_json(json::value const& jvalue, utility::string_t const& prefix)
{
std::wcout << prefix << jvalue.serialize() << std::endl;
}
void handle_get(http_request request)
{
TRACE(L"\nhandle GET\n");
auto answer = json::value::object();
for (auto const& p : dictionary)
{
answer[p.first] = json::value::string(p.second);
}
display_json(json::value::null(), L"R: ");
display_json(answer, L"S: ");
request.reply(status_codes::OK, answer);
}
void handle_request(http_request request, std::function<void(web::json::value const&, web::json::value&)> action)
{
auto answer = web::json::value::object();
request.extract_json()
.then([&answer, &action](pplx::task<web::json::value> task) {
try
{
auto const& jvalue = task.get();
display_json(jvalue, L"R: ");
if (!jvalue.is_null())
{
action(jvalue, answer);
}
else
{
std::wcout << "jvalue.is_null() !!!!err!!!!" << std::endl;
}
}
catch (http_exception const& e)
{
std::wcout << e.what() << std::endl;
}
})
.wait();
display_json(answer, L"S: ");
request.reply(status_codes::OK, answer);
}
void handle_post(http_request request)
{
TRACE("\nhandle POST\n");
handle_request(request, [](json::value const& jvalue, json::value& answer) {
for (auto const& e : jvalue.as_array())
{
if (e.is_string())
{
auto key = e.as_string();
auto pos = dictionary.find(key);
if (pos == dictionary.end())
{
answer[key] = json::value::string(L"<nil>");
}
else
{
answer[pos->first] = json::value::string(pos->second);
}
}
}
});
}
void handle_put(http_request request)
{
TRACE("\nhandle PUT\n");
handle_request(request, [](json::value const& jvalue, json::value& answer) {
for (auto const& e : jvalue.as_object())
{
if (e.second.is_string())
{
auto key = e.first;
auto value = e.second.as_string();
if (dictionary.find(key) == dictionary.end())
{
TRACE_ACTION(L"added", key, value);
answer[key] = json::value::string(L"<put>");
}
else
{
TRACE_ACTION(L"updated", key, value);
answer[key] = json::value::string(L"<updated>");
}
dictionary[key] = value;
}
}
});
}
void handle_del(http_request request)
{
TRACE("\nhandle DEL\n");
handle_request(request, [](json::value const& jvalue, json::value& answer) {
std::set<utility::string_t> keys;
for (auto const& e : jvalue.as_array())
{
if (e.is_string())
{
auto key = e.as_string();
auto pos = dictionary.find(key);
if (pos == dictionary.end())
{
answer[key] = json::value::string(L"<failed>");
}
else
{
TRACE_ACTION(L"deleted", pos->first, pos->second);
answer[key] = json::value::string(L"<deleted>");
keys.insert(key);
}
}
}
for (auto const& key : keys)
dictionary.erase(key);
});
}
}
/*
starting to listen
handle PUT
R: {"one":"100","two":"200"}
added (one, 100)
added (two, 200)
S: {"one":"<put>","two":"<put>"}
handle POST
R: ["one","two","three"]
S: {"one":"100","three":"<nil>","two":"200"}
handle DEL
R: ["one"]
deleted (one, 100)
S: {"one":"<deleted>"}
handle POST
R: ["one","two","three"]
S: {"one":"<nil>","three":"<nil>","two":"200"}
handle GET
R: null
S: {"two":"200"}
*/
int full_svr_main(int argc, char** argv)
{
http_listener listener(L"http://localhost:8888/restdemo");
listener.support(methods::GET, full_svr::handle_get);
listener.support(methods::POST, full_svr::handle_post);
listener.support(methods::PUT, full_svr::handle_put);
listener.support(methods::DEL, full_svr::handle_del);
try
{
listener.open().then([&listener]() { TRACE(L"\nstarting to listen\n"); }).wait();
while (true)
;
}
catch (std::exception const& e)
{
std::wcout << e.what() << std::endl;
}
return 0;
}
#endif