📄 http.h
字号:
/** A TCP/IP socket for the HyperText Transfer Protocol version 1.0.
When acting as a server, a descendant class would be created to override
at least the #HandleOpenMailbox()#, #HandleSendMessage()# and
#HandleDeleteMessage()# functions. Other functions may be overridden
for further enhancement to the sockets capabilities, but these will give a
basic POP3 server functionality.
The server socket thread would continuously call the
#ProcessMessage()# function until it returns FALSE. This will then
call the appropriate virtual function on parsing the POP3 protocol.
*/
class PHTTPServer : public PHTTP
{
PCLASSINFO(PHTTPServer, PHTTP)
public:
/** Create a TCP/IP HTTP protocol socket channel. The form with the single
<CODE>port</CODE> parameter creates an unopened socket, the form with
the <CODE>address</CODE> parameter makes a connection to a remote
system, opening the socket. The form with the <CODE>socket</CODE>
parameter opens the socket to an incoming call from a "listening"
socket.
*/
PHTTPServer();
PHTTPServer(
const PHTTPSpace & urlSpace ///< Name space to use for URLs received.
);
// New functions for class.
/** Get the name of the server.
@return
String name of the server.
*/
virtual PString GetServerName() const;
/** Get the name space being used by the HTTP server socket.
@return
URL name space tree.
*/
PHTTPSpace & GetURLSpace() { return urlSpace; }
/// Use a new URL name space for this HTTP socket.
void SetURLSpace(
const PHTTPSpace & space ///< New URL name space to use.
);
/** Process commands, dispatching to the appropriate virtual function. This
is used when the socket is acting as a server.
@return
TRUE if the request specified persistant mode and the request version
allows it, FALSE if the socket closed, timed out, the protocol does not
allow persistant mode, or the client did not request it
timed out
*/
virtual BOOL ProcessCommand();
/** Handle a GET command from a client.
The default implementation looks up the URL in the name space declared by
the #PHTTPSpace# class tree and despatches to the
#PHTTPResource# object contained therein.
@return
TRUE if the connection may persist, FALSE if the connection must close
If there is no ContentLength field in the response, this value must
be FALSE for correct operation.
*/
virtual BOOL OnGET(
const PURL & url, ///< Universal Resource Locator for document.
const PMIMEInfo & info, ///< Extra MIME information in command.
const PHTTPConnectionInfo & conInfo ///< HTTP connection information
);
/** Handle a HEAD command from a client.
The default implemetation looks up the URL in the name space declared by
the #PHTTPSpace# class tree and despatches to the
#PHTTPResource# object contained therein.
@return
TRUE if the connection may persist, FALSE if the connection must close
If there is no ContentLength field in the response, this value must
be FALSE for correct operation.
*/
virtual BOOL OnHEAD(
const PURL & url, ///< Universal Resource Locator for document.
const PMIMEInfo & info, ///< Extra MIME information in command.
const PHTTPConnectionInfo & conInfo ///< HTTP connection information
);
/** Handle a POST command from a client.
The default implementation looks up the URL in the name space declared by
the #PHTTPSpace# class tree and despatches to the
#PHTTPResource# object contained therein.
@return
TRUE if the connection may persist, FALSE if the connection must close
If there is no ContentLength field in the response, this value must
be FALSE for correct operation.
*/
virtual BOOL OnPOST(
const PURL & url, ///< Universal Resource Locator for document.
const PMIMEInfo & info, ///< Extra MIME information in command.
const PStringToString & data, ///< Variables provided in the POST data.
const PHTTPConnectionInfo & conInfo ///< HTTP connection information
);
/** Handle a proxy command request from a client. This will only get called
if the request was not for this particular server. If it was a proxy
request for this server (host and port number) then the appropriate
#OnGET()#, #OnHEAD()# or #OnPOST()# command is called.
The default implementation returns OnError(BadGateway).
@return
TRUE if the connection may persist, FALSE if the connection must close
If there is no ContentLength field in the response, this value must
be FALSE for correct operation.
*/
virtual BOOL OnProxy(
const PHTTPConnectionInfo & conInfo ///< HTTP connection information
);
/** Read the entity body associated with a HTTP request, and close the
socket if not a persistant connection.
@return
The entity body of the command
*/
virtual PString ReadEntityBody();
/** Handle an unknown command.
@return
TRUE if the connection may persist, FALSE if the connection must close
*/
virtual BOOL OnUnknown(
const PCaselessString & command, ///< Complete command line received.
const PHTTPConnectionInfo & connectInfo ///< HTTP connection information
);
/** Write a command reply back to the client, and construct some of the
outgoing MIME fields. The MIME fields are not sent.
The <CODE>bodySize</CODE> parameter determines the size of the
entity body associated with the response. If <CODE>bodySize</CODE> is
>= 0, then a ContentLength field will be added to the outgoing MIME
headers if one does not already exist.
If <CODE>bodySize</CODE> is < 0, then it is assumed that the size of
the entity body is unknown, or has already been added, and no
ContentLength field will be constructed.
If the version of the request is less than 1.0, then this function does
nothing.
@return
TRUE if requires v1.1 chunked transfer encoding.
*/
BOOL StartResponse(
StatusCode code, ///< Status code for the response.
PMIMEInfo & headers, ///< MIME variables included in response.
long bodySize ///< Size of the rest of the response.
);
/** Write an error response for the specified code.
Depending on the <CODE>code</CODE> parameter this function will also
send a HTML version of the status code for display on the remote client
viewer.
@return
TRUE if the connection may persist, FALSE if the connection must close
*/
virtual BOOL OnError(
StatusCode code, ///< Status code for the error response.
const PCaselessString & extra, ///< Extra information included in the response.
const PHTTPConnectionInfo & connectInfo ///< HTTP connection information
);
/** Set the default mime info
*/
void SetDefaultMIMEInfo(
PMIMEInfo & info, ///< Extra MIME information in command.
const PHTTPConnectionInfo & connectInfo
);
/**Get the connection info for this connection.
*/
PHTTPConnectionInfo & GetConnectionInfo() { return connectInfo; }
protected:
void Construct();
PHTTPSpace urlSpace;
PHTTPConnectionInfo connectInfo;
unsigned transactionCount;
PTimeInterval nextTimeout;
};
//////////////////////////////////////////////////////////////////////////////
// PHTTPRequest
/** This object describes a HyperText Transport Protocol request. An individual
request is passed to handler functions on #PHTTPResource# descendant
classes.
*/
class PHTTPRequest : public PObject
{
PCLASSINFO(PHTTPRequest, PObject)
public:
PHTTPRequest(
const PURL & url, ///< Universal Resource Locator for document.
const PMIMEInfo & inMIME, ///< Extra MIME information in command.
const PMultipartFormInfoArray & multipartFormInfo, ///< multipart form information (if any)
PHTTPServer & server ///< Server channel that request initiated on
);
PHTTPServer & server; ///< Server channel that request initiated on
const PURL & url; ///< Universal Resource Locator for document.
const PMIMEInfo & inMIME; ///< Extra MIME information in command.
const PMultipartFormInfoArray & multipartFormInfo; ///< multipart form information, if any
PHTTP::StatusCode code; ///< Status code for OnError() reply.
PMIMEInfo outMIME; ///< MIME information used in reply.
PString entityBody; ///< original entity body (POST only)
PINDEX contentSize; ///< Size of the body of the resource data.
PIPSocket::Address origin; ///< IP address of origin host for request
PIPSocket::Address localAddr; ///< IP address of local interface for request
WORD localPort; ///< Port number of local server for request
};
//////////////////////////////////////////////////////////////////////////////
// PHTTPAuthority
/** This abstract class describes the authorisation mechanism for a Universal
Resource Locator.
*/
class PHTTPAuthority : public PObject
{
PCLASSINFO(PHTTPAuthority, PObject)
public:
// New functions for class.
/** Get the realm or name space for the user authorisation name and
password as required by the basic authorisation system of HTTP/1.0.
@return
String for the authorisation realm name.
*/
virtual PString GetRealm(
const PHTTPRequest & request ///< Request information.
) const = 0;
/** Validate the user and password provided by the remote HTTP client for
the realm specified by the class instance.
@return
TRUE if the user and password are authorised in the realm.
*/
virtual BOOL Validate(
const PHTTPRequest & request, ///< Request information.
const PString & authInfo ///< Authority information string.
) const = 0;
/** Determine if the authorisation is to be applied. This could be used to
distinguish between net requiring authorisation and requiring autorisation
but having no password.
The default behaviour is to return TRUE.
@return
TRUE if the authorisation in the realm is to be applied.
*/
virtual BOOL IsActive() const;
protected:
static void DecodeBasicAuthority(
const PString & authInfo, ///< Authority information string.
PString & username, ///< User name decoded from authInfo
PString & password ///< Password decoded from authInfo
);
};
//////////////////////////////////////////////////////////////////////////////
// PHTTPSimpleAuth
/** This class describes the simplest authorisation mechanism for a Universal
Resource Locator, a fixed realm, username and password.
*/
class PHTTPSimpleAuth : public PHTTPAuthority
{
PCLASSINFO(PHTTPSimpleAuth, PHTTPAuthority)
public:
PHTTPSimpleAuth(
const PString & realm, ///< Name space for the username and password.
const PString & username, ///< Username that this object wiull authorise.
const PString & password ///< Password for the above username.
);
// Construct the simple authorisation structure.
// Overrides from class PObject.
/** Create a copy of the class on the heap. This is used by the
#PHTTPResource# classes for maintaining authorisation to
resources.
@return
pointer to new copy of the class instance.
*/
virtual PObject * Clone() const;
// Overrides from class PHTTPAuthority.
/** Get the realm or name space for the user authorisation name and
password as required by the basic authorisation system of HTTP/1.0.
@return
String for the authorisation realm name.
*/
virtual PString GetRealm(
const PHTTPRequest & request ///< Request information.
) const;
/** Validate the user and password provided by the remote HTTP client for
the realm specified by the class instance.
@return
TRUE if the user and password are authorised in the realm.
*/
virtual BOOL Validate(
const PHTTPRequest & request, ///< Request information.
const PString & authInfo ///< Authority information string.
) const;
/** Determine if the authorisation is to be applied. This could be used to
distinguish between net requiring authorisation and requiring autorisation
but having no password.
The default behaviour is to return TRUE.
@return
TRUE if the authorisation in the realm is to be applied.
*/
virtual BOOL IsActive() const;
/** Get the user name allocated to this simple authorisation.
@return
String for the authorisation user name.
*/
const PString & GetUserName() const { return username; }
/** Get the password allocated to this simple authorisation.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -