📄 http.h
字号:
/* * http.h * * HyperText Transport Protocol classes. * * Portable Windows Library * * Copyright (c) 1993-1998 Equivalence Pty. Ltd. * * The contents of this file are subject to the Mozilla Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is Portable Windows Library. * * The Initial Developer of the Original Code is Equivalence Pty. Ltd. * * Portions are Copyright (C) 1993 Free Software Foundation, Inc. * All Rights Reserved. * * Contributor(s): ______________________________________. * * $Log: http.h,v $ * Revision 1.42 2000/05/02 08:28:10 craigs * Removed "memory leaks" caused by brain-dead GNU linker * * Revision 1.41 1999/09/17 01:11:14 robertj * Fixed some documentation typos. * * Revision 1.40 1999/05/13 04:04:04 robertj * Fixed problem of initialised commandName in ConnectionInfo. * * Revision 1.39 1999/05/04 15:26:01 robertj * Improved HTTP/1.1 compatibility (pass through user commands). * Fixed problems with quicktime installer. * * Revision 1.38 1999/04/21 01:58:08 robertj * Fixed problem with reading data for request using second form of PHTTPRequestInfo constructor. * * Revision 1.37 1999/03/09 08:01:46 robertj * Changed comments for doc++ support (more to come). * * Revision 1.36 1999/02/16 08:07:10 robertj * MSVC 6.0 compatibility changes. * * Revision 1.35 1998/11/30 02:50:47 robertj * New directory structure * * Revision 1.34 1998/10/31 12:49:21 robertj * Added read/write mutex to the HTTP space variable to avoid thread crashes. * * Revision 1.33 1998/10/25 01:00:46 craigs * Added ability to specify per-directory authorisation for PHTTPDirectory * * Revision 1.32 1998/09/23 06:19:29 robertj * Added open source copyright license. * * Revision 1.31 1998/07/24 06:58:42 robertj * Changed PostData function so just has string for data instead of dictionary. * * Revision 1.30 1998/06/16 03:33:33 robertj * Changed TCP connection shutdown to be parameterised. * Propagated persistence and proxy flags in new connection info instances. * * Revision 1.29 1998/04/14 03:42:59 robertj * Fixed error code propagation in HTTP client. * * Revision 1.28 1998/02/03 06:29:38 robertj * Added local address and port to PHTTPRequest. * * Revision 1.27 1998/01/26 00:24:24 robertj * Added more information to PHTTPConnectionInfo. * Added function to allow HTTPClient to automatically connect if URL has hostname. * * Revision 1.26 1997/10/30 10:22:52 robertj * Added multiple user basic authorisation scheme. * * Revision 1.25 1997/10/03 13:30:15 craigs * Added ability to access client socket from within HTTP resources * * Revision 1.24 1997/03/28 04:40:22 robertj * Added tags for cookies. * * Revision 1.23 1997/01/12 04:15:19 robertj * Globalised MIME tag strings. * * Revision 1.22 1996/10/26 03:31:05 robertj * Changed OnError so can pass in full HTML page as parameter. * * Revision 1.21 1996/09/14 13:09:10 robertj * Major upgrade: * rearranged sockets to help support IPX. * added indirect channel class and moved all protocols to descend from it, * separating the protocol from the low level byte transport. * * Revision 1.20 1996/08/22 13:20:55 robertj * Fixed bug in authorisation, missing virtual prevented polymorphism. * * Revision 1.19 1996/08/19 13:44:06 robertj * Fixed authorisation so if have no user/password on basic authentication, does not require it. * * Revision 1.18 1996/06/28 13:15:23 robertj * Modified HTTPAuthority so gets PHTTPReqest (mainly for URL) passed in. * Moved HTTP form resource to another compilation module. * * Revision 1.17 1996/06/07 13:52:20 robertj * Added PUT to HTTP proxy FTP. Necessitating redisign of entity body processing. * * Revision 1.16 1996/05/23 10:00:52 robertj * Added common function for GET and HEAD commands. * Fixed status codes to be the actual status code instead of sequential enum. * This fixed some problems with proxy pass through of status codes. * * Revision 1.14 1996/03/31 08:46:51 robertj * HTTP 1.1 upgrade. * * Revision 1.13 1996/03/17 05:41:57 robertj * Added hit count to PHTTPResource. * * Revision 1.12 1996/03/16 04:39:55 robertj * Added ParseReponse() for splitting reponse line into code and info. * Added client side support for HTTP socket. * Added hooks for proxy support in HTTP socket. * * Revision 1.11 1996/03/10 13:15:23 robertj * Redesign to make resources thread safe. * * Revision 1.10 1996/03/02 03:12:55 robertj * Added radio button and selection boxes to HTTP form resource. * * Revision 1.9 1996/02/25 11:14:21 robertj * Radio button support for forms. * * Revision 1.8 1996/02/25 02:57:48 robertj * Removed pass through HTTP resource. * * Revision 1.7 1996/02/19 13:25:43 robertj * Added overwrite option to AddResource(). * Added get/set string to PHTTPString resource. * Moved nested classes from PHTTPForm. * * Revision 1.6 1996/02/13 13:09:16 robertj * Added extra parameters to callback function in PHTTPResources, required * by descendants to make informed decisions on data being loaded. * * Revision 1.5 1996/02/08 12:04:19 robertj * Redesign of resource object callback virtuals. * Added HTML form resource type. * * Revision 1.4 1996/02/03 11:03:32 robertj * Added ismodified since and expires time checking. * Added PHTTPString that defaults to empty string. * * Revision 1.3 1996/01/28 14:15:38 robertj * Changed PCharArray in OnLoadData to PString for convenience in mangling data. * Beginning of pass through resource type. * * Revision 1.2 1996/01/26 02:24:26 robertj * Further implemetation. * * Revision 1.1 1996/01/23 13:04:20 robertj * Initial revision * */#ifndef _PHTTP#define _PHTTP#ifdef __GNUC__#pragma interface#endif#include <ptclib/inetprot.h>#include <ptclib/mime.h>#include <ptclib/url.h>#include <ptclib/html.h>#include <ptlib/ipsock.h>//////////////////////////////////////////////////////////////////////////////// PHTTPSpaceclass PHTTPResource;/** This class describes a name space that a Universal Resource Locator operates in. Each section of the hierarchy field of the URL points to a leg in the tree specified by this class. */class PHTTPSpace : public PContainer{ PCONTAINERINFO(PHTTPSpace, PContainer) public: /// Constructor for HTTP URL Name Space PHTTPSpace(); // New functions for class. enum AddOptions { ErrorOnExist, Overwrite }; /** Add a new resource to the URL space. If there is already a resource at the location in the tree, or that location in the tree is already in the path to another resource then the function will fail. The <CODE>overwrite</CODE> flag can be used to replace an existing resource. The function will still fail if the resource is on a partial path to another resource but not if it is a leaf node. @return TRUE if resource added, FALSE if failed. */ BOOL AddResource( PHTTPResource * resource, /// Resource to add to the name space. AddOptions overwrite = ErrorOnExist /// Flag to overwrite an existing resource if it already exists. ); /** Delete an existing resource to the URL space. If there is not a resource at the location in the tree, or that location in the tree is in the path to another resource then the function will fail. @return TRUE if resource deleted, FALSE if failed. */ BOOL DelResource( const PURL & url /// URL to search for in the name space. ); /** Locate the resource specified by the URL in the URL name space. @return The resource found or NULL if no resource at that position in hiearchy. */ PHTTPResource * FindResource( const PURL & url /// URL to search for in the name space. ); /** This function attempts to acquire the mutex for reading. */ void StartRead() const { mutex->StartRead(); } /** This function attempts to release the mutex for reading. */ void EndRead() const { mutex->EndRead(); } /** This function attempts to acquire the mutex for writing. */ void StartWrite() const { mutex->StartWrite(); } /** This function attempts to release the mutex for writing. */ void EndWrite() const { mutex->EndWrite(); } protected: PReadWriteMutex * mutex; class Node; PSORTED_LIST(ChildList, Node); class Node : public PString { PCLASSINFO(Node, PString) public: Node(const PString & name, Node * parentNode); ~Node(); Node * parent; ChildList children; PHTTPResource * resource; } * root; private: BOOL SetSize(PINDEX) { return FALSE; }};//////////////////////////////////////////////////////////////////////////////// PHTTP/** A common base class for TCP/IP socket for the HyperText Transfer Protocolversion 1.0 client and server. */class PHTTP : public PInternetProtocol{ PCLASSINFO(PHTTP, PInternetProtocol) 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 OK = 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 * AllowTag; static const char * AuthorizationTag; static const char * ContentEncodingTag; static const char * ContentLengthTag; static const char * ContentTypeTag; static const char * DateTag; static const char * ExpiresTag; static const char * FromTag; static const char * IfModifiedSinceTag; static const char * LastModifiedTag; static const char * LocationTag; static const char * PragmaTag; static const char * PragmaNoCacheTag; static const char * RefererTag; static const char * ServerTag; static const char * UserAgentTag; static const char * WWWAuthenticateTag; static const char * MIMEVersionTag; static const char * ConnectionTag; static const char * KeepAliveTag; static const char * ProxyConnectionTag; static const char * ProxyAuthorizationTag; static const char * ProxyAuthenticateTag; static const char * ForwardedTag; static const char * SetCookieTag; static const char * 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:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -