classPiiHttpProtocol
#include <PiiHttpProtocol.h>
An implementation of the HTTP protocol.
Inherits PiiNetworkProtocol
Description
The role of PiiHttpProtocol is to map server URIs into
URI handlers. When a request comes in, the server looks at
the request URI and sequentially matches its beginning to
registered handlers. The handler with the most specific match will
be given the task to encode the request body and to reply to the
client. PiiHttpProtocol uses PiiHttpDevice as the
communication channel.
All functions in this class are thread-safe.
Classes
| class |
Limits the time a URI handler can run. |
| class |
An interface for objects that handle requests to specified URIs. |
Public types
|
enum
|
{ ContinueStatus = 100, SwitchingProtocolsStatus = 101,
ProcessingStatus = 102, OkStatus = 200, CreatedStatus = 201,
AcceptedStatus = 202, NonAuthoritativeInformationStatus = 203,
NoContentStatus = 204, ResetContentStatus = 205,
PartialContentStatus = 206, MultiStatus = 207, ImUsedStatus = 226,
MultipleChoicesStatus = 300, MovedPermanentlyStatus = 301,
FoundStatus = 302, SeeOtherStatus = 303, NotModifiedStatus = 304,
UseProxyStatus = 305, ReservedStatus = 306, TemporaryRedirectStatus
= 307, BadRequestStatus = 400, UnauthorizedStatus = 401,
PaymentRequiredStatus = 402, ForbiddenStatus = 403, NotFoundStatus
= 404, MethodNotAllowedStatus = 405, NotAcceptableStatus = 406,
ProxyAuthenticationRequiredStatus = 407, RequestTimeoutStatus =
408, ConflictStatus = 409, GoneStatus = 410, LengthRequiredStatus =
411, PreconditionFailedStatus = 412, RequestEntityTooLargeStatus =
413, RequestUriTooLongStatus = 414, UnsupportedMediaTypeStatus =
415, RequestedRangeNotSatisfiableStatus = 416,
ExpectationFailedStatus = 417, UnprocessableEntityStatus = 422,
LockedStatus = 423, FailedDependencyStatus = 424,
UpgradeRequiredStatus = 426, InternalServerErrorStatus = 500,
NotImplementedStatus = 501, BadGatewayStatus = 502,
ServiceUnavailableStatus = 503, GatewayTimeoutStatus = 504,
HttpVersionNotSupportedStatus = 505, VariantAlsoNegotiatesStatus =
506, InsufficientStorageStatus = 507, NotExtendedStatus = 510 }
Known HTTP status codes. |
Constructors and destructor
|
( )
|
|
|
( )
|
Public member functions
|
virtual void
|
The implementation of a network protocol. |
|
void
|
Register a URI handler. |
|
void
|
Unregister all occurrences of |
|
void
|
Unregister all occurrences of |
|
void
|
Unregister a handler at |
|
Get the handler (if any) that handles requests to |
Static public member functions
|
static QString
|
(
Returns the status message for a numerical HTTP status code. |
Enumeration details
-
enum Status
Known HTTP status codes.
This is not a complete list of application-specific status codes, but covers most typical uses.
Function details
-
PiiHttpProtocol
() -
~PiiHttpProtocol
() -
The implementation of a network protocol.
This function should handle all the communication with a client. It is called by a network server implementation when the I/O channel with a client has been opened, but no data has been either received or sent.
Once this function returns, either by finishing the connection or by an interrupt signal, the protocol object must reset to its initial state. The same protocol object may be used to serve many successive clients.
Parameters
- dev
-
the I/O device used for two-way communication.
- controller
-
a progress controller that either allows or disallows the communication to proceed. The implementation must call the canContinue() function from time to time to check if it is still allowed to run.
Reimplemented from PiiNetworkProtocol.
-
void registerUriHandler
Register a URI handler.
He caller retains the ownership of the handler. The same handler can be register many times in different places. The
uriparameter to the handleRequest() function tells the handler the URI it was registered at.The server will always look for the most specific handler. That is, if you register handler A at "/" and handler B at "/myuri/", every request beginning with "/myuri/" will be handled by B, and every other request by A. Note that a request to "/myuri" (without the trailing slash) will be served by A.
PiiHttpProtocol protocol; // A handler that fetches files from the file system PiiHttpFileSystemHandler* files = new PiiHttpFileSystemHandler("/var/www/html"); protocol.registerHandler("/", files); // Use the WebDAV protocol to server requests to /dav and /repository // Just an illustrative example, MyHttpDavHandler does not exist. MyHttpDavHandler* dav = new MyHttpDavHandler("/home/dav/files"); protocol.registerHandler("/dav/", dav); protocol.registerHandler("/repository/", dav);
Now, if a client requests "/dav/foobar", the handler named
davwill be invoked with "/dav/" as theuriparameter.Parameters
- uri
-
the URI of the handler, relative to the server root. Typically, slash-separated paths are used, but any valid URI string will work. If a handler already exists at this URI, the old handler will be replaced. If the URI does no start with a slash, the function has no effect.
- handler
-
the handler. When a request to the registered URI is received, the handler will be invoked.
-
void unregisterAllHandlers
Unregister all occurrences of
handler.Note that it may not be safe to delete the handler even if it has been unregistered. One must first ensure that all connections have been terminated. It is usually a good idea to shut down the server running the protocol before deleting handlers.
Parameters
- handler
-
the handler that is to be unregistered in all locations it has been registered to. If zero, the whole handler registry will be cleared.
-
void unregisterUriHandler
Unregister all occurrences of
handler. -
void unregisterUriHandler
Unregister a handler at
uri. -
UriHandler * uriHandler
Get the handler (if any) that handles requests to
uri.If
exactMatchistrue, require an exact match. Otherwise find the most specific match, even if not exact.Returns
the URI handler that serves requests to
uri, or 0 if no such handler exists. -
Returns the status message for a numerical HTTP status code.
Returns
a HTTP status string, such as "OK" (200) or "Moved permanently" (301). If the code is not known, an empty string will be returned.
Add a note
Not a single note added yet. Be the first, add yours.