-
Notifications
You must be signed in to change notification settings - Fork 2
/
json_httpxx_example.txt
51 lines (45 loc) · 1.63 KB
/
json_httpxx_example.txt
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
/*
#include <http.hpp>
#include <json/json.h>
double random () {
return (double(std::rand()) / double(RAND_MAX));
}
std::size_t random ( std::size_t lower, std::size_t upper )
{
const double alpha = random();
const double blend = double(lower) + alpha*(double(upper-lower));
return (std::size_t(blend + 0.5));
}
void feed ( http::Message& message, const char * data, std::size_t size )
{
for ( std::size_t i = 0; (i < size-1); ) {
i += message.feed(data+i, random(1, size-i));
}
}
const char REQUEST[] =
"POST /demo HTTP/1.1\r\n"
"Content-Type: text/json\r\n"
"Content-Length: 17\r\n"
"\r\n"
"[\"test1\",\"test2\"]";
http::BufferedRequest request;
feed(request, REQUEST, sizeof(REQUEST));
if ( !request.complete() ) {
RS_Print(va("HTTPXX: Request still needs data.\n"));
return (EXIT_FAILURE);
}
// Show that we've parsed it correctly.
RS_Print(va("HTTPXX: Connection: '%s'\n", request.header("Connection").c_str()));
RS_Print(va("HTTPXX: Host: '%s'\n", request.header("Host").c_str()));
RS_Print(va("HTTPXX: Content-Type: '%s'\n", request.header("Content-Type").c_str()));
RS_Print(va("HTTPXX: Fubar: '%s'\n", request.header("Fubar").c_str()));
RS_Print(va("HTTPXX: Body: '%s'\n", request.body().c_str()));
Json::Value root;
Json::Value val = Json::Value("defaultvalue");
Json::Reader t;
t.parse(request.body(), root, false);
if(root.isArray()) {
Json::ArrayIndex dd = 1;
RS_Print(va("HTTPXX JSONCPP: ARRAY ELEMENT 1 = %s\n", root.get(dd, val).asString().c_str()));
}
*/