-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (42 loc) · 1.36 KB
/
Copy pathmain.cpp
File metadata and controls
61 lines (42 loc) · 1.36 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
#include <nodepp/nodepp.h>
#include <nodepp/worker.h>
#include <nodepp/http.h>
using namespace nodepp;
void server() {
auto server = http::server([=]( http_t cli ){
console::log( cli.path, cli.get_fd() );
file_t file ( "LICENSE", "r" );
// file.set_buffer_size( 1024 );
cli.write_header( 200, header_t({
{ "Content-Type" , "text/html" },
{ "Transfer-Encoding", "chunked" },
// { "Content-Length", string::to_string( file.size() ) }
}));
stream::pipe( file, cli );
});
server.listen( "localhost", 8000, [=]( socket_t server ){
console::log("server started at http://localhost:8000");
});
}
void client() {
fetch_t args;
args.method = "GET";
args.url = "http://localhost:8000/";
args.headers = header_t({
{ "Host", url::host(args.url) }
});
http::fetch( args )
.then([]( http_t cli ){
cli.onClose([](){ console::log("closed"); });
cli.onData ([]( string_t chunk ){
console::log( chunk );
}); stream::pipe( cli );
})
.fail([]( except_t err ){
console::error( err );
});
}
void onMain() {
worker::add([=](){ server(); process::wait(); return -1; });
worker::add([=](){ client(); process::wait(); return -1; });
}