📄 parseutil.h
字号:
/////////////////////////////////////////////////////////////////////////////// Copyright (c) 2000 Intel Corporation// All rights reserved.//// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright notice,// this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright notice,// this list of conditions and the following disclaimer in the documentation// and/or other materials provided with the distribution.// * Neither name of the Intel Corporation nor the names of its contributors// may be used to endorse or promote products derived from this software// without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE./////////////////////////////////////////////////////////////////////////////// $Revision: 1.1.1.6 $// $Date: 2001/06/15 00:22:16 $#ifndef GENLIB_NET_HTTP_PARSEUTIL_H#define GENLIB_NET_HTTP_PARSEUTIL_H#include <genlib/util/xstring.h>#include <genlib/util/xdlist.h>#include <genlib/net/http/tokenizer.h>#include <genlib/http_client/http_client.h>#include <genlib/util/xstring.h>#include <genlib/util/dbllist.h>#include <netinet/in.h>// IDs for HTTP headers#define HDR_UNKNOWN -1 /* UnknownHeader */// Std. HTTP Headers#define HDR_ACCEPT 1 /* CommaSeparatedList<MediaRange> */#define HDR_ACCEPT_CHARSET 2 /* CommaSeparatedList<IdentifierQValue> */#define HDR_ACCEPT_ENCODING 3 /* CommaSeparatedList<IdentifierQValue> */#define HDR_ACCEPT_LANGUAGE 4 /* CommaSeparatedList<LanguageTag> */#define HDR_ACCEPT_RANGES 5 /* RawHeaderValue */#define HDR_AGE 6 /* HttpNumber */#define HDR_ALLOW 7 /* CommaSeparatedList<IdentifierValue> */#define HDR_AUTHORIZATION 8 /* RawHeaderValue */#define HDR_CACHE_CONTROL 9 /* CommaSeparatedList<CacheDirective> */#define HDR_CONNECTION 10 /* CommaSeparatedList<IdentifierValue> */#define HDR_CONTENT_ENCODING 11 /* CommaSeparatedList<IdentifierValue> */#define HDR_CONTENT_LENGTH 12 /* HttpNumber */#define HDR_CONTENT_LANGUAGE 13 /* CommaSepartedList<LanguageTag> */#define HDR_CONTENT_LOCATION 14 /* UriType */#define HDR_CONTENT_MD5 15 /* RawHeaderValue */#define HDR_CONTENT_RANGE 16 /* RawHeaderValue */#define HDR_CONTENT_TYPE 17 /* MediaRange */#define HDR_DATE 18 /* HttpDateValue */#define HDR_ETAG 19 /* RawHeadervalue */#define HDR_EXPECT 20 /* RawHeaderValue */#define HDR_EXPIRES 21 /* HttpDateValue */#define HDR_FROM 22 /* RawHeaderValue */#define HDR_HOST 23 /* HostPortValue */#define HDR_IF_MATCH 24 /* RawHeaderValue */#define HDR_IF_MODIFIED_SINCE 25 /* HttpDateValue */#define HDR_IF_NONE_MATCH 26 /* RawHeaderValue */#define HDR_IF_RANGE 27 /* RawHeaderValue */#define HDR_IF_UNMODIFIED_SINCE 28 /* HttpDateValue */#define HDR_LAST_MODIFIED 29 /* HttpDateValue */#define HDR_LOCATION 30 /* UriType (absolute)*/#define HDR_MAX_FORWARDS 31 /* HttpNumber */#define HDR_PRAGMA 32 /* RawHeaderValue */#define HDR_PROXY_AUTHENTICATE 33 /* RawHeaderValue */#define HDR_PROXY_AUTHORIZATION 34 /* RawHeaderValue */#define HDR_RANGE 35 /* RawHeaderValue */#define HDR_REFERER 36 /* RawHeaderValue */#define HDR_RETRY_AFTER 37 /* HttpDateOrSeconds */#define HDR_SERVER 38 /* RawHeaderValue */#define HDR_TE 39 /* RawHeaderValue */#define HDR_TRAILER 40 /* RawHeaderValue */#define HDR_TRANSFER_ENCODING 41 /* CommaSeparatedList<IdentifierValue> */#define HDR_USER_AGENT 42 /* RawHeaderValue */#define HDR_VARY 43 /* RawHeaderValue */#define HDR_VIA 44 /* RawHeaderValue */#define HDR_WARNING 45 /* RawHeaderValue */#define HDR_WWW_AUTHENTICATE 46 /* RawHeaderValue */// SSDP#define HDR_UPNP_USN 100 /* RawHeaderValue */#define HDR_UPNP_ST 101 /* RawHeaderValue */#define HDR_UPNP_MAN 102 /* RawHeaderValue */// GENA#define HDR_UPNP_NT 200 /* RawHeaderValue */#define HDR_UPNP_NTS 201 /* NTSType */#define HDR_UPNP_CALLBACK 202 /* RawHeaderValue */#define HDR_UPNP_SID 203 /* RawHeaderValue */// SOAP#define HDR_UPNP_SOAPACTION 300 /* RawHeaderValue *///////////////////////////////////////////// parse error codes#define PARSERR_COLON_NOT_FOUND -2#define PARSERR_BAD_REQUEST_LINE -3// invalid integer format or -ve number#define PARSERR_BAD_NUMBER -4 // list does not satisfy [min, max] range required#define PARSERR_BAD_LISTCOUNT -5// bad comma-separated list#define PARSERR_BAD_COMMALIST -6// method name not known#define PARSERR_UNKNOWN_METHOD -7#define PARSERR_CONTENT_LENGTH_MISSING -9#define PARSERR_BAD_ENTITY -10#define PARSERR_INCOMPLETE_ENTITY -11#define PARSERR_BAD_HEADER_NAME -12#define PARSERR_BAD_FORMAT -15class HttpParseException : public BasicException{public: HttpParseException() : BasicException() { } HttpParseException( const char* s, int lineNumber = -1 ); protected: virtual const char * getClassName() const { return "HttpParseException"; }};CREATE_NEW_EXCEPTION_TYPE( ParseNoMatchException, HttpParseException, "ParseNoMatchException" )CREATE_NEW_EXCEPTION_TYPE( ParseFailException, HttpParseException, "ParseFailException" )void SkipOptionalLWS( Tokenizer& scanner );// UPNP_POST, UPNP_MPOST, NOTIFY(opt)enum UpnpMethodType { HTTP_UNKNOWN_METHOD = -1, HTTP_GET, HTTP_HEAD, UPNP_NOTIFY, UPNP_MSEARCH, UPNP_POST, UPNP_MPOST, UPNP_SUBSCRIBE, UPNP_UNSUBSCRIBE };// base class for all header valuesclass HttpHeaderValue{public: virtual void toString( xstring& s ) = 0; virtual void load( Tokenizer& scanner ) = 0; virtual ~HttpHeaderValue() { }};// also allows optionally using q for each valueclass HttpQHeaderValue : public HttpHeaderValue{public: HttpQHeaderValue() { qIsUsed = true; } virtual void toString( xstring& s ); float q; bool qIsUsed; protected: void loadOptionalQValue( IN Tokenizer& scanner );};//typedef xdlist<HttpHeaderValue*> HttpHeaderValueList;//typedef xdlistNode<HttpHeaderValue*> HttpHeaderValueNode;typedef dblList HttpHeaderValueList; // list<HttpHeaderValue*>typedef dblListNode HttpHeaderValueNode; // node<HttpHeaderValue*>class HttpHeader{public: HttpHeader(); virtual ~HttpHeader(); void toString( xstring& s ); public: int type; // header type code: content-length etc HttpHeaderValue* value;};//typedef xdlist<HttpHeader> HttpHeaderList;//typedef xdlistNode<HttpHeader> HttpHeaderNode;typedef dblList HttpHeaderList; // list<HttpHeader*>typedef dblListNode HttpHeaderNode; // node<HttpHeader*>// creates a new HttpHeaderValue objecttypedef HttpHeaderValue* (*CreateNewValueCallback)(void);///////////////////////////////////////////////////////// stores HttpHeaderValues unsorted or sorted by qclass CommaSeparatedList : public HttpHeaderValue{public: // if HttpQHeaderValue is used, qIsUsed should be true; // at least minimumItems will be present in the list // at most maximum items will be present; infinite items if // maximumItems == -1 // set create_callback to NULL if not using load() CommaSeparatedList( bool qIsUsed, CreateNewValueCallback create_callback, int minimumItems = 0, int maximumItems = -1 ) : qUsed(qIsUsed), minItems(minimumItems), maxItems(maximumItems) { createCallback = create_callback; } void toString( xstring& s ); void load( Tokenizer& scanner ); public: HttpHeaderValueList valueList; CreateNewValueCallback createCallback; private: bool qUsed; int minItems; int maxItems;};// value is a simple identifier (token as per Http EBNF)class IdentifierValue : public HttpHeaderValue{public: IdentifierValue() { } void toString( xstring& s ); void load( Tokenizer& scanner ); public: xstring value;};// similar to IdentifierValue; except that this class// also supports a qclass IdentifierQValue : public HttpQHeaderValue{public: IdentifierQValue() : HttpQHeaderValue() { } void toString( xstring& s ); void load( Tokenizer& scanner ); public: xstring value;};//////////////////////////////////////////////////// used when http value is a integerclass HttpNumber : public HttpHeaderValue{public: HttpNumber() { } void toString( xstring& s ); void load( Tokenizer& scanner ); public: int num;};///////////////////////// number is encoded in HEXclass HttpHexNumber : public HttpHeaderValue{public: HttpHexNumber() { } void toString( xstring& s ); void load( Tokenizer& scanner ); public: int num;};class MediaExtension : public HttpHeaderValue{public: MediaExtension() { } void toString( xstring& s ); void load( Tokenizer& scanner ); public: xstring name; xstring value;};class MediaParam : public HttpQHeaderValue{public: MediaParam() { } void toString( xstring& s ); void load( Tokenizer& scanner ); public: HttpHeaderValueList extList; // MediaExtension list};// MediaRange (type/subtype of entity)class MediaRange : public HttpQHeaderValue{public: MediaRange() { } void toString( xstring& s ); void load( Tokenizer& scanner ); int compare( const MediaRange& r ) const; public: xstring type; xstring subtype; MediaParam mparam;};// MediaRangeListclass MediaRangeList : public HttpHeaderValue{public: MediaRangeList() { } void toString( xstring& s ); void load( Tokenizer& scanner ); public: HttpHeaderValueList mediaList; // MediaRange list};// matches a language tag eg: en-USclass LanguageTag : public HttpQHeaderValue{public: LanguageTag() { qIsUsed = false; } void toString( xstring& s ); void load( Tokenizer& scanner ); const char* getTag() const { return lang.c_str(); } // returns true if value was set successfully
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -