📄 http.h
字号:
public:
// New functions for class.
enum Commands {
// HTTP/1.0 commands
GET, HEAD, POST,
// HTTP/1.1 commands
PUT, DELETE, TRACE, OPTIONS,
// HTTPS command
CONNECT,
NumCommands
};
enum StatusCode {
Continue = 100, ///< 100 - Continue
SwitchingProtocols, ///< 101 - upgrade allowed
RequestOK = 200, ///< 200 - request has succeeded
Created, ///< 201 - new resource created: entity body contains URL
Accepted, ///< 202 - request accepted, but not yet completed
NonAuthoritativeInformation, ///< 203 - not definitive entity header
NoContent, ///< 204 - no new information
ResetContent, ///< 205 - contents have been reset
PartialContent, ///< 206 - partial GET succeeded
MultipleChoices = 300, ///< 300 - requested resource available elsewehere
MovedPermanently, ///< 301 - resource moved permanently: location field has new URL
MovedTemporarily, ///< 302 - resource moved temporarily: location field has new URL
SeeOther, ///< 303 - see other URL
NotModified, ///< 304 - document has not been modified
UseProxy, ///< 305 - proxy redirect
BadRequest = 400, ///< 400 - request malformed or not understood
UnAuthorised, ///< 401 - request requires authentication
PaymentRequired, ///< 402 - reserved
Forbidden, ///< 403 - request is refused due to unsufficient authorisation
NotFound, ///< 404 - resource cannot be found
MethodNotAllowed, ///< 405 - not allowed on this resource
NoneAcceptable, ///< 406 - encoding not acceptable
ProxyAuthenticationRequired, ///< 407 - must authenticate with proxy first
RequestTimeout, ///< 408 - server timeout on request
Conflict, ///< 409 - resource conflict on action
Gone, ///< 410 - resource gone away
LengthRequired, ///< 411 - no Content-Length
UnlessTrue, ///< 412 - no Range header for TRUE Unless
InternalServerError = 500, ///< 500 - server has encountered an unexpected error
NotImplemented, ///< 501 - server does not implement request
BadGateway, ///< 502 - error whilst acting as gateway
ServiceUnavailable, ///< 503 - server temporarily unable to service request
GatewayTimeout ///< 504 - timeout whilst talking to gateway
};
// Common MIME header tags
static const char * const AllowTag;
static const char * const AuthorizationTag;
static const char * const ContentEncodingTag;
static const char * const ContentLengthTag;
static const char * const ContentTypeTag;
static const char * const DateTag;
static const char * const ExpiresTag;
static const char * const FromTag;
static const char * const IfModifiedSinceTag;
static const char * const LastModifiedTag;
static const char * const LocationTag;
static const char * const PragmaTag;
static const char * const PragmaNoCacheTag;
static const char * const RefererTag;
static const char * const ServerTag;
static const char * const UserAgentTag;
static const char * const WWWAuthenticateTag;
static const char * const MIMEVersionTag;
static const char * const ConnectionTag;
static const char * const KeepAliveTag;
static const char * const TransferEncodingTag;
static const char * const ChunkedTag;
static const char * const ProxyConnectionTag;
static const char * const ProxyAuthorizationTag;
static const char * const ProxyAuthenticateTag;
static const char * const ForwardedTag;
static const char * const SetCookieTag;
static const char * const CookieTag;
protected:
/** Create a TCP/IP HTTP protocol channel.
*/
PHTTP();
/** Parse a response line string into a response code and any extra info
on the line. Results are placed into the member variables
<CODE>lastResponseCode</CODE> and <CODE>lastResponseInfo</CODE>.
The default bahaviour looks for a space or a '-' and splits the code
and info either side of that character, then returns FALSE.
@return
Position of continuation character in response, 0 if no continuation
lines are possible.
*/
virtual PINDEX ParseResponse(
const PString & line ///< Input response line to be parsed
);
};
//////////////////////////////////////////////////////////////////////////////
// PHTTPClient
/** A TCP/IP socket for the HyperText Transfer Protocol version 1.0.
When acting as a client, the procedure is to make the connection to a
remote server, then to retrieve a document using the following procedure:
<PRE><CODE>
PHTTPSocket web("webserver");
if (web.IsOpen()) {
PINDEX len;
if (web.GetDocument("http://www.someone.com/somewhere/url", len)) {
PString html = web.ReadString(len);
if (!html.IsEmpty())
ProcessHTML(html);
}
else
PError << "Could not get page." << endl;
}
else
PError << "HTTP conection failed." << endl;
</PRE></CODE>
*/
class PHTTPClient : public PHTTP
{
PCLASSINFO(PHTTPClient, PHTTP)
public:
/// Create a new HTTP client channel.
PHTTPClient();
PHTTPClient(
const PString & userAgentName
);
// New functions for class.
/** Send a command and wait for the response header (including MIME fields).
Note that a body may still be on its way even if lasResponseCode is not
200!
@return
TRUE if all of header returned and ready to receive body.
*/
int ExecuteCommand(
Commands cmd,
const PURL & url,
PMIMEInfo & outMIME,
const PString & dataBody,
PMIMEInfo & replyMime,
BOOL persist = TRUE
);
int ExecuteCommand(
const PString & cmdName,
const PURL & url,
PMIMEInfo & outMIME,
const PString & dataBody,
PMIMEInfo & replyMime,
BOOL persist = TRUE
);
/// Write a HTTP command to server
BOOL WriteCommand(
Commands cmd,
const PString & url,
PMIMEInfo & outMIME,
const PString & dataBody
);
BOOL WriteCommand(
const PString & cmdName,
const PString & url,
PMIMEInfo & outMIME,
const PString & dataBody
);
/// Read a response from the server
BOOL ReadResponse(
PMIMEInfo & replyMIME
);
/// Read the body of the HTTP command
BOOL ReadContentBody(
PMIMEInfo & replyMIME,
PBYTEArray & body
);
BOOL ReadContentBody(
PMIMEInfo & replyMIME,
PString & body
);
/** Get the document specified by the URL.
@return
TRUE if document is being transferred.
*/
BOOL GetTextDocument(
const PURL & url, ///< Universal Resource Locator for document.
PString & document, ///< Body read
BOOL persist = TRUE ///< if TRUE, enable HTTP persistence
);
/** Get the document specified by the URL.
@return
TRUE if document is being transferred.
*/
BOOL GetDocument(
const PURL & url, ///< Universal Resource Locator for document.
PMIMEInfo & outMIME, ///< MIME info in request
PMIMEInfo & replyMIME, ///< MIME info in response
BOOL persist = TRUE ///< if TRUE, enable HTTP persistence
);
/** Get the header for the document specified by the URL.
@return
TRUE if document header is being transferred.
*/
BOOL GetHeader(
const PURL & url, ///< Universal Resource Locator for document.
PMIMEInfo & outMIME, ///< MIME info in request
PMIMEInfo & replyMIME, ///< MIME info in response
BOOL persist = TRUE ///< if TRUE, enable HTTP persistence
);
/** Post the data specified to the URL.
@return
TRUE if document is being transferred.
*/
BOOL PostData(
const PURL & url, ///< Universal Resource Locator for document.
PMIMEInfo & outMIME, ///< MIME info in request
const PString & data, ///< Information posted to the HTTP server.
PMIMEInfo & replyMIME, ///< MIME info in response
BOOL persist = TRUE ///< if TRUE, enable HTTP persistence
);
/** Post the data specified to the URL.
@return
TRUE if document is being transferred.
*/
BOOL PostData(
const PURL & url, ///< Universal Resource Locator for document.
PMIMEInfo & outMIME, ///< MIME info in request
const PString & data, ///< Information posted to the HTTP server.
PMIMEInfo & replyMIME, ///< MIME info in response
PString & replyBody, ///< Body of response
BOOL persist = TRUE ///< if TRUE, enable HTTP persistence
);
protected:
BOOL AssureConnect(const PURL & url, PMIMEInfo & outMIME);
BOOL InternalReadContentBody(
PMIMEInfo & replyMIME,
PAbstractArray & body
);
PString userAgentName;
};
#ifdef P_HTTPSVC
//////////////////////////////////////////////////////////////////////////////
// PMultipartFormInfo
/** This object describes the information associated with a multi-part
form entry
*/
class PMultipartFormInfo : public PObject
{
PCLASSINFO(PMultipartFormInfo, PObject);
public:
PMIMEInfo mime;
PString body;
};
PARRAY(PMultipartFormInfoArray, PMultipartFormInfo);
//////////////////////////////////////////////////////////////////////////////
// PHTTPConnectionInfo
class PHTTPServer;
/** This object describes the connectiono associated with a HyperText Transport
Protocol request. This information is required by handler functions on
#PHTTPResource# descendant classes to manage the connection correctly.
*/
class PHTTPConnectionInfo : public PObject
{
PCLASSINFO(PHTTPConnectionInfo, PObject)
public:
PHTTPConnectionInfo();
PHTTP::Commands GetCommandCode() const { return commandCode; }
const PString & GetCommandName() const { return commandName; }
const PURL & GetURL() const { return url; }
const PMIMEInfo & GetMIME() const { return mimeInfo; }
void SetMIME(const PString & tag, const PString & value);
BOOL IsCompatible(int major, int minor) const;
BOOL IsPersistant() const { return isPersistant; }
BOOL WasPersistant() const { return wasPersistant; }
BOOL IsProxyConnection() const { return isProxyConnection; }
int GetMajorVersion() const { return majorVersion; }
int GetMinorVersion() const { return minorVersion; }
long GetEntityBodyLength() const { return entityBodyLength; }
/**Get the maximum time a persistent connection may persist.
*/
PTimeInterval GetPersistenceTimeout() const { return persistenceTimeout; }
/**Set the maximum time a persistent connection may persist.
*/
void SetPersistenceTimeout(const PTimeInterval & t) { persistenceTimeout = t; }
/**Get the maximum number of transations (GET/POST etc) for persistent connection.
If this is zero then there is no maximum.
*/
unsigned GetPersistenceMaximumTransations() const { return persistenceMaximum; }
/**Set the maximum number of transations (GET/POST etc) for persistent connection.
If this is zero then there is no maximum.
*/
void SetPersistenceMaximumTransations(unsigned m) { persistenceMaximum = m; }
const PMultipartFormInfoArray & GetMultipartFormInfo() const
{ return multipartFormInfoArray; }
void ResetMultipartFormInfo()
{ multipartFormInfoArray.RemoveAll(); }
PString GetEntityBody() const { return entityBody; }
protected:
BOOL Initialise(PHTTPServer & server, PString & args);
void DecodeMultipartFormInfo(const PString & type, const PString & entityBody);
PHTTP::Commands commandCode;
PString commandName;
PURL url;
PMIMEInfo mimeInfo;
BOOL isPersistant;
BOOL wasPersistant;
BOOL isProxyConnection;
int majorVersion;
int minorVersion;
PString entityBody; // original entity body (POST only)
long entityBodyLength;
PTimeInterval persistenceTimeout;
unsigned persistenceMaximum;
PMultipartFormInfoArray multipartFormInfoArray;
friend class PHTTPServer;
};
//////////////////////////////////////////////////////////////////////////////
// PHTTPServer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -