#include "mordor/streams/file.h"
...
if (uri == "/MyData/Myfile.txt") {
FileStream::ptr fileStream(new FileStream("MyFile.txt"));
return respondStream(request, fileStream);
}
The key point is that you need to use a Mordor::Stream, not a
std::istream. respondStream will handle setting all of the HTTP
headers having to do with transferring the raw data (Content-Length,
Transfer-Encoding, doing Range requests, etc.), or doing a HEAD
instead of a GET; see . You can set additional headers if you would
like (Content-Type, E-Tag, Last-Modified-At).
One thing to be careful of is that file I/O is *not* asynchronous, so
while it is reading data from the file, it is blocking the current
thread, and not able to process concurrent requests. The easy way to
fix that is to create a WorkerPool of threads to handle file I/O, and
pass it to FileStream as the scheduler argument; then it will
automatically run any blocking calls on the worker threads, and not
block the main IOManager thread(s).
Cody
Cody
Cody
On Tue, Sep 20, 2011 at 1:16 PM, sanjeev kumar <sanje...@gmail.com> wrote:
Cody
On Wed, Sep 21, 2011 at 6:36 AM, sanjeev kumar <sanje...@gmail.com> wrote:
> other questions
>
> Case 1: If the uri == /html (where html is directory. )
> then wont all the files under this directory will be send via
> respondStream to the browser ?
> Case 2: If the uri == /html/index.html
> if (uri == "/html/index.html") {
> std::string cPath = boost::filesystem::current_path().string();
> std::cout << cPath.c_str() << std::endl;
> FileStream::ptr fileStream(
> new FileStream(cPath + uri.toString(), FileStream::READ));
> return respondStream(request, fileStream);
> }
> Then index.html is send and displayed in the browser. Now how to serve
> other links which are in index.html. (i.e the navigation, how should
> the server handle it.
>
> Thank you.
Cody
On Wed, Sep 21, 2011 at 9:32 AM, sanjeev kumar <sanje...@gmail.com> wrote:
> Thank you Cody,
> For Case 2 :
> So you mean I should do it in this way in the httpRequest method.
> if (uri == "/html/") {
> std::string cPath =
> boost::filesystem::current_path().string();
> std::cout << cPath.c_str() << std::endl;
> FileStream::ptr fileStream(
> new FileStream(cPath + uri.toString(), FileStream::READ));
> return respondStream(request, fileStream);
> }
>
> Also why are the servlets not getting invoked ?
> I do register the servlets now with a leading slash, but still I do
> not see any info in the log that the servlets are invoked ?
> ......
> HTTP::ServletDispatcher dispatcher;
> HTTP::Servlet::ptr files(new FileServlet), instrs(new
> InstrumentServlet);
> dispatcher.registerServlet("/", files);
> dispatcher.registerServlet("/instrument/", instrs);
> startHttpServer(ioManager);
> ioManager.dispatch();
>
> thank you
> Sanjeev
Cody
Scheduler::getThis()->schedule(boost::bind(&httpServer, s,
boost::ref(dispatcher));
Cody
What URLs are you using to access the other servlets? Because you
passed the trailing slash when registering them, you need to access
them with a trailing slash (i.e. /instrument will get routed to the
files controller registered for /, because it does not match
/instrument/).
Cody