Project

General

Profile

Bug #7093 » issue_7093.cpp

Roel Standaert, 06/19/2019 12:25 PM

 
1

    
2
#include <Wt/WServer>
3
#include <Wt/WResource>
4
#include <Wt/Http/Client>
5
#include <Wt/Http/Request>
6
#include <Wt/Http/Response>
7
#include <Wt/Http/ResponseContinuation>
8

    
9
#include <memory>
10
#include <atomic>
11
#include <iostream>
12

    
13
// CMD:
14
// C:> server.exe --http-address 0.0.0.0 --docroot .
15

    
16
// Terminal (in parallel):
17
// $ while [ "$(curl -s 'http://localhost/FetchData')" == "done" ]; do   echo -n '.';   sleep 1; done
18

    
19
class FetchData : public Wt::WResource {
20
public:
21
   FetchData (Wt::WServer& server) : server(server) { }
22
   ~FetchData () override { beingDeleted(); }
23

    
24
  void handleRequest (const Wt::Http::Request& request, Wt::Http::Response& response) override
25
  {
26
    if (request.continuation()) {
27
      response.out().write("done", 4);
28
    } else {
29
      Wt::Http::ResponseContinuation *continuation = response.createContinuation();
30
      auto client = std::make_shared<Wt::Http::Client>();
31

    
32
      continuation->setData(client);
33

    
34
      client->done().connect([this, continuation] (boost::system::error_code err, const Wt::Http::Message& /*response*/, ...) {
35
         if (err) server.log("error") << err.message();
36
         continuation->haveMoreData();
37
      });
38

    
39
      if (client->get("http://localhost/Data")) {
40
        continuation->waitForMoreData();
41
      } else {
42
         server.log("error") << "something is wrong with the url";
43
      }
44
    }
45
  }
46

    
47
private:
48
   Wt::WServer& server;
49
};
50

    
51
class ServeData : public Wt::WResource {
52
public:
53
   ServeData () = default;
54
   ~ServeData () override { beingDeleted(); }
55

    
56
   void handleRequest (const Wt::Http::Request& /*request*/, Wt::Http::Response& /*response*/) override 
57
   {
58
   }
59
};
60

    
61

    
62

    
63

    
64

    
65

    
66

    
67
int main (int argc, char** argv)
68
try {
69
  Wt::WServer wserver{argc, argv};
70

    
71
  FetchData fetchdata(wserver);
72
  wserver.addResource(&fetchdata, "/FetchData");
73

    
74
  ServeData servedata;
75
  wserver.addResource(&servedata, "/Data");
76

    
77
  wserver.run();
78

    
79
  return 0;
80
}
81
catch (std::exception& e) {
82
  std::cerr << e.what() << std::endl;
83
  return 10;
84
}
85
 
(5-5/6)