Aynchronous file creation for response
Added by Somsubhra Sharangi over 12 years ago
Hi,
I am trying to create a file dynamically on the server before serving it. When I try to access the file using wget an empty file gets downloaded. My guess is that I am not using the asynchronous response feature correctly. Can you please give suggestions on how to use this properly.
class GhostFileResource : public Wt::WResource
{
public:
virtual ~GhostFileResource(){
beingDeleted();
}
protected:
virtual void handleRequest(const Wt::Http::Request &request,Wt::Http::Response &response){
std::string fetch_command_prefix = "wget http://www.webtoolkit.eu/videos";
std::string dynamic_command = fetch_command_prefix + request.path();
std::cout<< dynamic_command <<std::endl;
system(dynamic_command.c_str());
/*maybe do some more processing here*/
WFileResource *file = new WFileResource( "/sintel_trailer.mp4" );
response.setMimeType ("video/mp4");
response.out() << file;
}
};
//ask for a file that currently does not exist on server
//GhostFileResource gfr;
//server.addResource(&gfr, "/sintel_trailer.mp4");
$ wget http://<snip>:9090/sintel_trailer.mp4
--2012-06-08 14:08:11-- http://<snip>:9090/sintel_trailer.mp4
Connecting to <snip>:9090... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [video/mp4]
Saving to: `sintel_trailer.mp4'
[ <=> ] 10 --.-K/s in 0s
2012-06-08 14:08:14 (1.12 MB/s) - `sintel_trailer.mp4' saved [10]
$ ./bin/WebAvStatic --docroot . --http-address 0.0.0.0 --http-port 9090
[2012-Jun-08 14:08:02.997335] 25611 - [info] "config: reading Wt config file: /etc/wt/wt_config.xml (location = './bin/WebAvStatic')"
[2012-Jun-08 14:08:02.998749] 25611 - [info] "WServer/wthttp: initializing built-in wthttpd"
[2012-Jun-08 14:08:02.999157] 25611 - [info] "wthttp: started server: http://0.0.0.0:9090"
wget http://www.webtoolkit.eu/videos/sintel_trailer.mp4
--2012-06-08 14:08:10-- http://www.webtoolkit.eu/videos/sintel_trailer.mp4
Resolving www.webtoolkit.eu (www.webtoolkit.eu)... 207.210.65.76
Connecting to www.webtoolkit.eu (www.webtoolkit.eu)|207.210.65.76|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4357159 (4.2M) [video/mp4]
Saving to: `sintel_trailer.mp4'
100%[===================================================================================================================>] 4,357,159 1.76M/s in 2.4s
2012-06-08 14:08:13 (1.76 MB/s) - `sintel_trailer.mp4' saved [4357159/4357159]
142.58.185.39 - - [2012-Jun-08 14:08:13.434861] "GET /sintel_trailer.mp4 HTTP/1.0" 200 10
[2012-Jun-08 14:08:13.435235] 25611 - [info] "WebRequest: took 2529.36ms"
Replies (4)
RE: Aynchronous file creation for response - Added by Wim Dumon over 12 years ago
Hello,
What you do here is send the textual conversion of the pointer to file
as response to your query. What you will need to do, is to copy the contents of the file that you just downloaded into the response.out() stream.
If I'm not mistaken, you can do this as follows:
ifstream file("file.mp4", std::ios::in | std::ios::binary);
response.out() << file.rdbuf();
Best regards,
Wim.
RE: Aynchronous file creation for response - Added by Somsubhra Sharangi over 12 years ago
Hi Wim,
Thanks for your suggestion but it is not working. The wget requests still behave the same way. Attached is the updated code.
Just out of curiosity, should there be much difference between ifstream and WFileResource? Although I have not looked at the source, I assumed WFileResource as a Wt wrapper over ifstream.
Regards,
Som
RE: Aynchronous file creation for response - Added by Wim Dumon over 12 years ago
Does it work if you send the 'Hi', in the line that is commented out?
Did you verify that the file is properly opened? That my proposed method to copy from stream to stream is actually correct?
WFileResource is a WResource that sends a file on the server to the client. It has no similarity with an ifstream.
Regards,
Wim.
RE: Aynchronous file creation for response - Added by Somsubhra Sharangi over 12 years ago
I finally found the bug. The file name string had the leading '/' from the request.path(). Thanks.
Regards,
Som