#include <Wt/WResource>
Public Member Functions | |
WResource (WObject *parent=0) | |
Create a new resource. | |
~WResource () | |
Destroy the resource. | |
void | suggestFileName (const std::string &name) |
Suggest a filename to the user for the data streamed by this resource. | |
const std::string | generateUrl () const |
Generate an URL for this resource. | |
bool | reentrant () const |
Can this resource be streamed reentrantly ? | |
void | setReentrant (bool reentrant) |
Specify if this resource may be streamed reentrantly. | |
void | write (std::ostream &out) |
Stream the resource to a stream. | |
Public Attributes | |
Signal< void > | dataChanged |
Emit this signal if the data presented in this resource has changed. | |
Protected Member Functions | |
virtual const std::string | resourceMimeType () const =0 |
Return the mimetype. | |
virtual bool | streamResourceData (std::ostream &stream, const ArgumentMap &arguments)=0 |
Stream the data for this resource. | |
void | flush () |
Flush data for this resource. | |
virtual void | setArguments (const ArgumentMap &arguments) |
Handle the request arguments. | |
void | addHeader (const std::string &name, const std::string &value) |
Add a HTTP header. |
Besides the main page, other objects may be rendered as additional resources, such as frames or dynamic images.
To serve resources that you create on the fly, you need to specialize this class and reimplement:
In addition, you may want to help the browser to start a suitable helper application, or the user to save the file with a suitable name, by setting an appropriate file name using suggestFileName().
For small resources, or resources that you do not want to generate on the fly, you can also use the WMemoryResource or WFileResource which stream data from respectively memory or a file.
A resource can be used directly in conjunction with WAnchor or a WImage, which has the benefit that these classes are aware of resource changes that you can indicate by emitting the dataChanged signal.
Wt::WResource::~WResource | ( | ) |
Destroy the resource.
You must make sure that the resource is nog longer in use (no longer referred within the application) when deleting it.
void Wt::WResource::suggestFileName | ( | const std::string & | name | ) |
Suggest a filename to the user for the data streamed by this resource.
For resources, intended to be downloaded by the user, suggest a name used for saving. The filename extension may also help the browser to identify the correct program for opening the resource.
const std::string Wt::WResource::generateUrl | ( | ) | const |
Generate an URL for this resource.
Generates a new url that refers to this resource. The url is unique to assure that it is not cached by the web browser, and can thus be used to refer to a new "version" of the resource, which can be indicated by emitting the dataChanged signal.
bool Wt::WResource::reentrant | ( | ) | const [inline] |
Can this resource be streamed reentrantly ?
Reentrant resources may be streamed concurrently to the user. Thus, its resourceMimeType() and streamResourceData() functions must be implemented in a thread-safe way.
void Wt::WResource::setReentrant | ( | bool | reentrant | ) |
Specify if this resource may be streamed reentrantly.
A reentrant resource may stream its data concurrently with other resources (and with the main event handling ? perhaps this is not necessary...)
void Wt::WResource::write | ( | std::ostream & | out | ) |
Stream the resource to a stream.
This is a convenience method to serialize to a stream (for example a files stream).
virtual const std::string Wt::WResource::resourceMimeType | ( | ) | const [protected, pure virtual] |
Return the mimetype.
Implement this method to return the correct mime type for your resource, e.g. "text/html".
Implemented in Wt::Ext::DataStore, Wt::WFileResource, Wt::WMemoryResource, Wt::WSvgImage, and Wt::WWidget.
virtual bool Wt::WResource::streamResourceData | ( | std::ostream & | stream, | |
const ArgumentMap & | arguments | |||
) | [protected, pure virtual] |
Stream the data for this resource.
Implement this method to output the data for this resource.
Returns whether all data has been streamed. If not, call flush() from outside the mean event loop to indicate that more data is available. This is how "server-push" is implemented. The stream is not closed until this function returns true.
void Wt::WResource::flush | ( | ) | [protected] |
Flush data for this resource.
This is only valid when a previous call to streamResourceData() returned false.
This will trigger a call to streamResourceData() to retrieve more data to be transmitted.
void Wt::WResource::setArguments | ( | const ArgumentMap & | arguments | ) | [protected, virtual] |
Handle the request arguments.
This method is called before serving the resource, and provides the request arguments. You may want to reimplement this method if you want to tailor how the resource is served based on request arguments.
The default implementation does nothing.
void Wt::WResource::addHeader | ( | const std::string & | name, | |
const std::string & | value | |||
) | [protected] |
Add a HTTP header.
Headers may be added only before the content-type is set, which is done before streaming the resource data. This method can only be used from within a reimplemented setArguments() method.